blob: 50e418b266e011acff9fd890baffdf9fb69c345c [file] [log] [blame]
Colin Cross6ca8d252024-01-18 10:38:47 -08001#!/bin/bash -ex
2
3: "${OUT_DIR:?Must set OUT_DIR}"
4TOP=$(cd $(dirname $0)/../../..; pwd)
5cd ${TOP}
6
7UNAME="$(uname)"
8case "$UNAME" in
9Linux)
10 OS='linux'
11 ;;
12Darwin)
13 OS='darwin'
14 ;;
15*)
16 exit 1
17 ;;
18esac
19
20# Verify that go test and go build work on all the same projects that are parsed by
21# build/soong/build_kzip.bash
22declare -ar go_modules=(build/blueprint build/soong
23 build/make/tools/canoninja build/make/tools/compliance build/make/tools/rbcrun)
24export GOROOT=${TOP}/prebuilts/go/${OS}-x86
25export GOENV=off
26export GOPROXY=off
27abs_out_dir=$(cd ${OUT_DIR}; pwd)
28export GOPATH=${abs_out_dir}/gopath
29export GOCACHE=${abs_out_dir}/gocache
30export GOMODCACHE=${abs_out_dir}/gomodcache
31export TMPDIR=${abs_out_dir}/gotemp
32mkdir -p ${TMPDIR}
33${GOROOT}/bin/go env
34
35# androidmk_test.go gets confused if ANDROID_BUILD_TOP is set.
36unset ANDROID_BUILD_TOP
37
38network_jail=""
39if [[ ${OS} = linux ]]; then
40 # The go tools often try to fetch dependencies from the network,
41 # wrap them in an nsjail to prevent network access.
42 network_jail=${TOP}/prebuilts/build-tools/linux-x86/bin/nsjail
43 # Quiet
44 network_jail="${network_jail} -q"
45 # No timeout
46 network_jail="${network_jail} -t 0"
47 # Set working directory
48 network_jail="${network_jail} --cwd=\$PWD"
49 # Pass environment variables through
50 network_jail="${network_jail} -e"
51 # Allow read-only access to everything
52 network_jail="${network_jail} -R /"
53 # Allow write access to the out directory
54 network_jail="${network_jail} -B ${abs_out_dir}"
55 # Allow write access to the /tmp directory
56 network_jail="${network_jail} -B /tmp"
57 # Set high values, as network_jail uses low defaults.
58 network_jail="${network_jail} --rlimit_as soft"
59 network_jail="${network_jail} --rlimit_core soft"
60 network_jail="${network_jail} --rlimit_cpu soft"
61 network_jail="${network_jail} --rlimit_fsize soft"
62 network_jail="${network_jail} --rlimit_nofile soft"
63fi
64
65for dir in "${go_modules[@]}"; do
66 (cd "$dir";
67 eval ${network_jail} -- ${GOROOT}/bin/go build ./...
68 eval ${network_jail} -- ${GOROOT}/bin/go test ./...
69 )
70done