forked from bazel-contrib/toolchains_llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_xcompile_tests.sh
More file actions
executable file
·104 lines (93 loc) · 3.66 KB
/
Copy pathrun_xcompile_tests.sh
File metadata and controls
executable file
·104 lines (93 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Copyright 2021 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euo pipefail
scripts_dir="$(dirname "${BASH_SOURCE[0]}")"
source "${scripts_dir}/bazel.sh"
"${bazel}" version
cd "${scripts_dir}"
# Returns the absolute path of the //:stdlib_test binary. The cross-compiled
# output lands in the host-configuration bazel-bin (the `--platforms` flag does
# not change the output directory mnemonic here), so we resolve it with
# `bazel info bazel-bin` using only common_args. We must not pass `--platforms`
# to `info`, as it resolves the label against the wrong repo mapping and fails.
# `info bazel-bin` returns an absolute path; cquery's --output=files would be
# relative to the workspace root, which breaks since this script runs from a
# subdirectory.
binpath_for() {
local bazel_bin
bazel_bin="$("${bazel}" --bazelrc=/dev/null info "${common_args[@]}" bazel-bin)"
echo "${bazel_bin}/stdlib_test"
}
# Runs the cross-compiled binary inside a container for the matching platform.
# Skipped in CI because the macOS GitHub Action runners do not have docker.
check_with_image() {
if "${CI:-false}"; then
return
fi
local docker_platform="$1"
local image="$2"
local binpath="$3"
docker run --rm -it --platform="${docker_platform}" \
--mount "type=bind,source=${binpath},target=/stdlib_test" "${image}" /stdlib_test
}
# Cross-compiles //:stdlib_test for a target and verifies the produced binary is
# an ELF for the expected architecture, both with dynamically and statically
# linked system libraries.
#
# Args:
# 1: bazel target platform label
# 2: cc toolchain label for the sysroot toolchain
# 3: regex matched against `file` output to assert the ELF architecture
# 4: docker --platform value used to smoke-test the binary
xcompile_test() {
local platform="$1"
local toolchain="$2"
local arch_regex="$3"
local docker_platform="$4"
local build_args=(
"${common_args[@]}"
--platforms="${platform}"
--extra_toolchains="${toolchain}"
--symlink_prefix=/
--color=yes
--show_progress_rate_limit=30
)
echo ""
echo "Testing ${platform}: static linked user libraries and dynamic linked system libraries"
"${bazel}" --bazelrc=/dev/null build "${build_args[@]}" //:stdlib_test
local binpath
binpath="$(binpath_for)"
file "${binpath}" | tee /dev/stderr | grep -qE "${arch_regex}"
check_with_image "${docker_platform}" "gcr.io/distroless/cc-debian11" "${binpath}" # Need glibc image for system libraries.
echo ""
echo "Testing ${platform}: static linked user and system libraries"
build_args+=(
--features=fully_static_link
)
"${bazel}" --bazelrc=/dev/null build "${build_args[@]}" //:stdlib_test
binpath="$(binpath_for)"
file "${binpath}" | tee /dev/stderr | grep -qE "${arch_regex}"
check_with_image "${docker_platform}" "gcr.io/distroless/static-debian11" "${binpath}"
}
xcompile_test \
"@toolchains_llvm//platforms:linux-x86_64" \
"@llvm_toolchain_with_sysroot//:cc-toolchain-x86_64-linux" \
"ELF.*x86-64" \
"linux/amd64"
xcompile_test \
"@toolchains_llvm//platforms:linux-aarch64" \
"@llvm_toolchain_with_sysroot//:cc-toolchain-aarch64-linux" \
"ELF.*(aarch64|ARM aarch64)" \
"linux/arm64"