Skip to content

Commit 1978758

Browse files
authored
chore: Refactor test.sh (#165)
Effectively the same, except `docker_build` now uses `ldd` in the container instead of `file`, like the other tests as the QEMU bug has been resolved since Aug 2023 (QEMU 8.1.0).
1 parent 6b436d1 commit 1978758

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

test.sh

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,73 @@
11
#!/bin/bash
2-
set -ex
2+
set -ex -o pipefail
33

4-
docker_build() {
5-
echo "Target dir: $TARGET_DIR"
6-
echo "Platform: $PLATFORM"
4+
# Common vars:
5+
CRATE_NAME="${1}crate"
6+
CRATE_PATH="./test/${CRATE_NAME}"
7+
8+
# Build and verify successful static compilation of a crate:
9+
function docker_build() {
10+
echo "Target dir: ${TARGET_DIR}"
11+
echo "Platform: ${PLATFORM}"
712

813
# NB: add -vv to cargo build when debugging
9-
local -r crate="$1"crate
10-
docker run --rm \
11-
-v "$PWD/test/${crate}:/volume" \
12-
-v cargo-cache:/root/.cargo/registry \
13-
-e RUST_BACKTRACE=1 \
14+
docker run --rm -it \
15+
--env RUST_BACKTRACE=1 \
16+
--volume "${CRATE_PATH}:/volume" \
17+
--volume cargo-cache:/opt/cargo/registry \
1418
--platform "${PLATFORM}" \
1519
rustmusl-temp \
1620
cargo build
1721

18-
cd "test/${crate}"
19-
20-
# Ideally we would use `ldd` but due to a qemu bug we can't :(
21-
# See https://github.com/multiarch/qemu-user-static/issues/172
22-
# Instead we use `file`.
23-
docker run --rm \
24-
-v "$PWD:/volume" \
25-
-e RUST_BACKTRACE=1 \
22+
# Verify the build artifact works and is statically linked:
23+
# (A container is used for `ldd` so that a non-native platform can also be tested)
24+
local CRATE_ARTIFACT="./target/${TARGET_DIR}/debug/${CRATE_NAME}"
25+
docker run --rm -it \
26+
--env RUST_BACKTRACE=1 \
27+
--volume "${CRATE_PATH}:/volume" \
28+
--workdir /volume \
2629
--platform "${PLATFORM}" \
2730
test-runner \
28-
bash -c "cd volume; ./target/$TARGET_DIR/debug/${crate} && file ./target/$TARGET_DIR/debug/${crate} && file /volume/target/$TARGET_DIR/debug/${crate} 2>&1 | grep -qE 'static-pie linked|statically linked' && echo ${crate} is a static executable"
31+
bash -ex -o pipefail -c "
32+
'${CRATE_ARTIFACT}'
33+
ldd '${CRATE_ARTIFACT}' 2>&1 \
34+
| grep -qE 'not a dynamic|statically linked' \
35+
&& echo '${crate} is a static executable'
36+
"
2937
}
3038

31-
# Helper to check how ekidd/rust-musl-builder does it
32-
docker_build_ekidd() {
33-
local -r crate="$1"crate
34-
docker run --rm \
35-
-v "$PWD/test/${crate}:/home/rust/src" \
36-
-v cargo-cache:/home/rust/.cargo \
37-
-e RUST_BACKTRACE=1 \
38-
-it ekidd/rust-musl-builder:nightly \
39+
# Reference - Helpers to locally compare builds from alternative images (x86_64 arch only):
40+
# - `ekidd/rust-musl-builder`
41+
# - `golddranks/rust_musl_docker`
42+
function docker_build_ekidd() {
43+
docker run --rm -it \
44+
--env RUST_BACKTRACE=1 \
45+
--volume "${CRATE_PATH}:/home/rust/src" \
46+
--volume cargo-cache:/home/rust/.cargo \
47+
ekidd/rust-musl-builder:nightly \
3948
cargo build -vv
40-
cd "test/${crate}"
41-
./target/x86_64-unknown-linux-musl/debug/"${crate}"
42-
ldd "target/x86_64-unknown-linux-musl/debug/${crate}" 2>&1 | grep -qE "not a dynamic|statically linked" && \
43-
echo "${crate} is a static executable"
49+
50+
check_crate_build_locally
4451
}
4552

46-
# Helper to check how golddranks/rust_musl_docker does it
47-
docker_build_golddranks() {
48-
local -r crate="$1"crate
49-
docker run --rm \
50-
-v "$PWD/test/${crate}:/workdir" \
51-
-e RUST_BACKTRACE=1 \
52-
-it golddranks/rust_musl_docker:nightly-2017-10-03 \
53+
function docker_build_golddranks() {
54+
docker run --rm -it \
55+
--env RUST_BACKTRACE=1 \
56+
--volume "${CRATE_PATH}:/workdir" \
57+
golddranks/rust_musl_docker:nightly-2017-10-03 \
5358
cargo build -vv --target=x86_64-unknown-linux-musl
54-
cd "test/${crate}"
55-
./target/x86_64-unknown-linux-musl/debug/"${crate}"
56-
ldd "target/x86_64-unknown-linux-musl/debug/${crate}" 2>&1 | grep -qE "not a dynamic|statically linked" && \
57-
echo "${crate} is a static executable"
59+
60+
check_crate_build_locally
61+
}
62+
63+
# Verify the build artifact works and is statically linked:
64+
function check_crate_build_locally() {
65+
local CRATE_ARTIFACT="${CRATE_PATH}/target/x86_64-unknown-linux-musl/debug/${CRATE_NAME}"
66+
67+
"${CRATE_ARTIFACT}"
68+
ldd "${CRATE_ARTIFACT}" 2>&1 \
69+
| grep -qE 'not a dynamic|statically linked' \
70+
&& echo "${CRATE_NAME} is a static executable"
5871
}
5972

60-
docker_build "$1"
73+
docker_build

0 commit comments

Comments
 (0)