xref: /aosp_15_r20/external/protobuf/kokoro/linux/bazel_distcheck/build.sh (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1#!/bin/bash
2#
3# Build file to set up and run tests using bazel-build dist archive
4#
5# Note that the builds use WORKSPACE to fetch external sources, not
6# git submodules.
7
8set -eux
9
10BUILD_ONLY_TARGETS=(
11  //pkg:all
12  //:protoc
13  //:protobuf
14  //:protobuf_python
15)
16
17TEST_TARGETS=(
18  //build_defs:all
19  //conformance:all
20  //java:tests
21  //:protobuf_test
22)
23
24use_bazel.sh 5.0.0 || true
25bazel version
26
27# Change to repo root
28cd $(dirname $0)/../../..
29
30# Construct temp directory for running the dist build.
31# If you want to run locally and keep the build dir, create a directory
32# and pass it in the DIST_WORK_ROOT env var.
33if [[ -z ${DIST_WORK_ROOT:-} ]]; then
34  : ${DIST_WORK_ROOT:=$(mktemp -d)}
35  function dist_cleanup() {
36    rm -rf ${DIST_WORK_ROOT}
37  }
38  trap dist_cleanup EXIT
39fi
40
41# Let Bazel share the distdir.
42TMP_DISTDIR=${DIST_WORK_ROOT}/bazel-distdir
43mkdir -p ${TMP_DISTDIR}
44
45# Build distribution archive
46date
47bazel fetch --distdir=${TMP_DISTDIR} //pkg:dist_all_tar
48bazel build --distdir=${TMP_DISTDIR} //pkg:dist_all_tar
49DIST_ARCHIVE=$(readlink $(bazel info bazel-bin)/pkg/dist_all_tar.tar.gz)
50bazel shutdown
51
52# The `pkg_tar` rule emits a symlink based on the rule name. The actual
53# file is named with the current version.
54date
55echo "Resolved archive path: ${DIST_ARCHIVE}"
56
57# Extract the dist archive.
58date
59DIST_WORKSPACE=${DIST_WORK_ROOT}/protobuf
60mkdir -p ${DIST_WORKSPACE}
61tar -C ${DIST_WORKSPACE} --strip-components=1 -axf ${DIST_ARCHIVE}
62
63# Perform build steps in the extracted dist sources.
64
65cd ${DIST_WORKSPACE}
66FAILED=false
67
68date
69bazel fetch --distdir=${TMP_DISTDIR} "${BUILD_ONLY_TARGETS[@]}" "${TEST_TARGETS[@]}"
70
71date
72bazel build --distdir=${TMP_DISTDIR} -k \
73  "${BUILD_ONLY_TARGETS[@]}" || FAILED=true
74
75date
76bazel test --distdir=${TMP_DISTDIR} --test_output=errors -k \
77  "${TEST_TARGETS[@]}" || FAILED=true
78
79date
80cd examples
81bazel build --distdir=${TMP_DISTDIR} //... || FAILED=true
82
83if ${FAILED}; then
84   echo FAILED
85   exit 1
86fi
87echo PASS
88