Skip to content

Commit

Permalink
internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 566410311
Change-Id: Iee5de71e7ad900d0a39e603175228911c8c03d1f
  • Loading branch information
ted-xie authored and copybara-github committed Sep 18, 2023
1 parent 6add8f1 commit 8144955
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 89 deletions.
103 changes: 15 additions & 88 deletions kokoro/presubmit/kokoro_presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,103 +16,30 @@
set -e
set -x

function DownloadBazelisk() {
# Utility function to download a specified version of bazelisk to a given
# installation directory. Adds the directory to PATH.
# Positional arguments:
# version: The version to install.
# platform: The platform to install. Currently only "linux" has been
# validated.
# arch: Architecture to install. Currently only "arm64" has been validated.
# dest: Where to install Bazelisk. Must be a user-writeable directory,
# otherwise the root user must call this function through sudo.
(
set -euxo pipefail

# Positional arguments
local version="${1:-1.18.0}"
local platform="${2:-linux}"
local arch="${3:-amd64}"
local dest="${4:-${TMPDIR}/bazelisk-release}"

download_url="https://github.com/bazelbuild/bazelisk/releases/download/v${version}/bazelisk-${platform}-${arch}"
mkdir -p "${dest}"
wget -nv ${download_url} -O "${dest}/bazelisk"
chmod +x "${dest}/bazelisk"
ln -s "${dest}/bazelisk" "${dest}/bazel"
export PATH="${dest}:${PATH}"
type -a bazel
echo "Bazelisk ${version} installation completed."
)
}

echo "== installing bazelisk ========================================="
bazel_install_dir=$(mktemp -d)
BAZELISK_VERSION="1.18.0"
export USE_BAZEL_VERSION="last_green"
DownloadBazelisk "$BAZELISK_VERSION" linux amd64 "$bazel_install_dir"
bazel="$bazel_install_dir/bazel"
bazel_detected_version=$("$bazel" version | grep "Build label" | awk -F": " '{print $2}')
echo "============================================================="

function Cleanup() {
# Clean up all temporary directories: bazelisk install, sandbox, and
# android_tools.
rm -rf "$bazel_install_dir"
}
trap Cleanup EXIT
# Set up the SDK as root to avoid having to deal with user file permissions

# Kokoro is no longer updating toolchains in their images, so install newer
# android build tools, because the latest one installed (26.0.2) has some bug
# in APPT2 which causes the magic number to be incorrect for some files it
# outputs.
#
# Use "yes" to accept sdk licenses.
cd "$ANDROID_HOME"

# Use "yes" to accept sdk licenses.
yes | cmdline-tools/latest/bin/sdkmanager --install "build-tools;30.0.3" "extras;android;m2repository" &>/dev/null
yes | cmdline-tools/latest/bin/sdkmanager --licenses &>/dev/null
chmod -R o=rx "$ANDROID_HOME"

# ANDROID_HOME is already in the environment.
export ANDROID_NDK_HOME="/opt/android-ndk-r16b"

# Create a tmpfs in the sandbox at "/tmp/hsperfdata_$USERNAME" to avoid the
# problems described in https://github.com/bazelbuild/bazel/issues/3236
# Basically, the JVM creates a file at /tmp/hsperfdata_$USERNAME/$PID, but
# processes all get a PID of 2 in the sandbox, so concurrent Java build actions
# could crash because they're trying to modify the same file. So, tell the
# sandbox to mount a tmpfs at /tmp/hsperfdata_$(whoami) so that each JVM gets
# its own version of that directory.
hsperfdata_dir="/tmp/hsperfdata_$(whoami)_rules_android"
mkdir "$hsperfdata_dir"

COMMON_ARGS=(
"--sandbox_tmpfs_path=$hsperfdata_dir"
"--verbose_failures"
"--experimental_google_legacy_api"
"--experimental_enable_android_migration_apis"
"--build_tests_only"
# Java tests use language version at least 11, but they might depend on
# libraries that were built for Java 17.
"--java_language_version=11"
"--java_runtime_version=17"
"--test_output=errors"
)

# Go to rules_android workspace and run relevant tests.
cd "${KOKORO_ARTIFACTS_DIR}/git/rules_android"

# Fetch all external deps; should reveal any bugs related to external dep
# references.
"$bazel" aquery 'deps(...)' 2>&1 > /dev/null

"$bazel" test "${COMMON_ARGS[@]}" //src/common/golang/... \
//src/tools/ak/... \
//src/tools/javatests/... \
//src/tools/jdeps/... \
//src/tools/java/... \
//test/...
# Remainder of this file deals with setting up a non-root build account,
# and using it to run presubmit
# User account needs to be able to read $ANDROID_HOME (+r) and traverse directories (+x)
chmod -R o=rx "$ANDROID_HOME"

# Go to basic app workspace in the source tree
cd "${KOKORO_ARTIFACTS_DIR}/git/rules_android/examples/basicapp"
"$bazel" build "${COMMON_ARGS[@]}" //java/com/basicapp:basic_app
# Make the non-root account
export KOKORO_USER="bazel-builder"
useradd -m -s /bin/bash "$KOKORO_USER"

# Run presubmit as bazel-builder, and pass ANDROID_HOME and KOKORO_ARTIFACTS_DIR
# from root user's environment to bazel-builder's environment
runuser -w ANDROID_HOME,KOKORO_ARTIFACTS_DIR -l "$KOKORO_USER" \
-c "bash ${KOKORO_ARTIFACTS_DIR}/git/rules_android/kokoro/presubmit/presubmit_main.sh"
4 changes: 3 additions & 1 deletion kokoro/presubmit/presubmit_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,6 @@ function main() {
# Go to basic app workspace in the source tree
cd "${KOKORO_ARTIFACTS_DIR}/git/rules_android/examples/basicapp"
"$bazel" build "${COMMON_ARGS[@]}" //java/com/basicapp:basic_app
}
}

main

0 comments on commit 8144955

Please sign in to comment.