xref: /aosp_15_r20/external/bazelbuild-rules_android/kokoro/presubmit/presubmit_main.sh (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1#!/bin/bash
2# Copyright 2023 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16function DownloadBazelisk()  {
17  # Utility function to download a specified version of bazelisk to a given
18  # installation directory. Adds the directory to PATH.
19  # Positional arguments:
20  #   version: The version to install.
21  #   platform: The platform to install. Currently only "linux" has been
22  #     validated.
23  #   arch: Architecture to install. Currently only "arm64" has been validated.
24  #   dest: Where to install Bazelisk. Must be a user-writeable directory,
25  #     otherwise the root user must call this function through sudo.
26  (
27    set -euxo pipefail
28
29    # Positional arguments
30    local version="${1:-1.18.0}"
31    local platform="${2:-linux}"
32    local arch="${3:-amd64}"
33    local dest="${4:-${TMPDIR}/bazelisk-release}"
34
35    download_url="https://github.com/bazelbuild/bazelisk/releases/download/v${version}/bazelisk-${platform}-${arch}"
36    mkdir -p "${dest}"
37    wget -nv ${download_url} -O "${dest}/bazelisk"
38    chmod +x "${dest}/bazelisk"
39    ln -s "${dest}/bazelisk" "${dest}/bazel"
40    export PATH="${dest}:${PATH}"
41    type -a bazel
42    echo "Bazelisk ${version} installation completed."
43  )
44}
45
46function main() {
47  set -euxo pipefail
48  echo "== installing bazelisk ========================================="
49  bazel_install_dir=$(mktemp -d)
50  BAZELISK_VERSION="1.18.0"
51  export USE_BAZEL_VERSION="last_green"
52  DownloadBazelisk "$BAZELISK_VERSION" linux amd64 "$bazel_install_dir"
53  bazel="$bazel_install_dir/bazel"
54  echo "============================================================="
55
56  function Cleanup() {
57    # Clean up all temporary directories: bazelisk install, sandbox, and
58    # android_tools.
59    rm -rf "$bazel_install_dir"
60  }
61  trap Cleanup EXIT
62
63  function cd () {
64    # This is necessary due to a weird docker image issue where non-root
65    # accounts have `cd` overriden by a function that has an unbound variable.
66    # The unbound variable caues presubmit failure to due `set -u` above.
67    # The `cd` override only happens for non-root users.
68    builtin cd "$@"
69  }
70
71  # ANDROID_HOME is already in the environment.
72  export ANDROID_NDK_HOME="/opt/android-ndk-r16b"
73
74  # Create a tmpfs in the sandbox at "/tmp/hsperfdata_$USERNAME" to avoid the
75  # problems described in https://github.com/bazelbuild/bazel/issues/3236
76  # Basically, the JVM creates a file at /tmp/hsperfdata_$USERNAME/$PID, but
77  # processes all get a PID of 2 in the sandbox, so concurrent Java build actions
78  # could crash because they're trying to modify the same file. So, tell the
79  # sandbox to mount a tmpfs at /tmp/hsperfdata_$(whoami) so that each JVM gets
80  # its own version of that directory.
81  hsperfdata_dir="/tmp/hsperfdata_$(whoami)_rules_android"
82  mkdir "$hsperfdata_dir"
83
84  COMMON_ARGS=(
85    "--sandbox_tmpfs_path=$hsperfdata_dir"
86    "--verbose_failures"
87    "--experimental_google_legacy_api"
88    "--experimental_enable_android_migration_apis"
89    "--build_tests_only"
90    # Java tests use language version at least 11, but they might depend on
91    # libraries that were built for Java 17.
92    "--java_language_version=11"
93    "--java_runtime_version=17"
94    "--test_output=errors"
95  )
96
97  # Go to rules_android workspace and run relevant tests.
98  cd "${KOKORO_ARTIFACTS_DIR}/git/rules_android"
99
100  # Fetch all external deps; should reveal any bugs related to external dep
101  # references. First run this query with bzlmod enabled to catch missing
102  # bzlmod deps.
103  "$bazel" aquery 'deps(...)' --enable_bzlmod > /dev/null
104  # Perform the same aquery with bzlmod disabled to sniff out WORKSPACE issues
105  "$bazel" aquery 'deps(...)' --noenable_bzlmod > /dev/null
106
107  "$bazel" test "${COMMON_ARGS[@]}" //src/common/golang/... \
108    //src/tools/ak/... \
109    //src/tools/javatests/... \
110    //src/tools/jdeps/... \
111    //src/tools/java/... \
112    //src/tools/mi/... \
113    //test/...
114
115  # Go to basic app workspace in the source tree
116  cd "${KOKORO_ARTIFACTS_DIR}/git/rules_android/examples/basicapp"
117  "$bazel" build "${COMMON_ARGS[@]}" //java/com/basicapp:basic_app
118}
119
120main