• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
35if [[ ${OS} = linux ]]; then
36    # Building with the race detector enabled uses the host linker, set the
37    # path to use the hermetic one.
38    CLANG_VERSION=$(build/soong/scripts/get_clang_version.py)
39    export CC="${TOP}/prebuilts/clang/host/${OS}-x86/${CLANG_VERSION}/bin/clang"
40    export CXX="${TOP}/prebuilts/clang/host/${OS}-x86/${CLANG_VERSION}/bin/clang++"
41    glibc_dir="${TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8"
42    export CGO_CFLAGS="--sysroot ${glibc_dir}/sysroot/"
43    export CGO_CPPFLAGS="--sysroot ${glibc_dir}/sysroot/"
44    export CGO_CXXFLAGS="--sysroot ${glibc_dir}/sysroot/"
45    export CGO_LDFLAGS="--sysroot ${glibc_dir}/sysroot/ -B ${glibc_dir}/lib/gcc/x86_64-linux/4.8.3 -L ${glibc_dir}/lib/gcc/x86_64-linux/4.8.3 -L ${glibc_dir}/x86_64-linux/lib64"
46fi
47
48# androidmk_test.go gets confused if ANDROID_BUILD_TOP is set.
49unset ANDROID_BUILD_TOP
50
51network_jail=""
52if [[ ${OS} = linux ]]; then
53    # The go tools often try to fetch dependencies from the network,
54    # wrap them in an nsjail to prevent network access.
55    network_jail=${TOP}/prebuilts/build-tools/linux-x86/bin/nsjail
56    # Quiet
57    network_jail="${network_jail} -q"
58    # No timeout
59    network_jail="${network_jail} -t 0"
60    # Set working directory
61    network_jail="${network_jail} --cwd=\$PWD"
62    # Pass environment variables through
63    network_jail="${network_jail} -e"
64    # Allow read-only access to everything
65    network_jail="${network_jail} -R /"
66    # Allow write access to the out directory
67    network_jail="${network_jail} -B ${abs_out_dir}"
68    # Allow write access to the /tmp directory
69    network_jail="${network_jail} -B /tmp"
70    # Set high values, as network_jail uses low defaults.
71    network_jail="${network_jail} --rlimit_as soft"
72    network_jail="${network_jail} --rlimit_core soft"
73    network_jail="${network_jail} --rlimit_cpu soft"
74    network_jail="${network_jail} --rlimit_fsize soft"
75    network_jail="${network_jail} --rlimit_nofile soft"
76fi
77
78for dir in "${go_modules[@]}"; do
79    (cd "$dir";
80     eval ${network_jail} -- ${GOROOT}/bin/go build ./...
81     eval ${network_jail} -- ${GOROOT}/bin/go test ./...
82     eval ${network_jail} -- ${GOROOT}/bin/go test -race -timeout 20m -short ./...
83    )
84done
85