Skip to content

Commit 23d8fb0

Browse files
committed
Make CI pass by tweaking linker setups
1 parent b349d91 commit 23d8fb0

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

.cargo/config.toml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
11
[alias]
22
license = "dd-rust-license-tool check"
33
format = "fmt --all && clippy --workspace --all-features --fix"
4-
5-
# libddwaf's bindgen uses `clang-sys` which by default dynamically opens
6-
# `libclang.so`; however this is not supported on Alpine Linux (the usual musl
7-
# target).
8-
# Enabling the `static` feature of `bindgen`/`clang-sys` creates additional
9-
# build requirements (`libLLVM.a`), which Alpine's `llvm-static` does not
10-
# provide (it has its content presented as many `libLLVM*.a` files). Disabling
11-
# statically linking to the C runtime alleviates this issue, but can result in
12-
# `proc-macro` not being usable; which notably causes doc tests to fail with
13-
# a big linker error.
14-
[target.aarch64-unknown-linux-musl]
15-
rustflags = ["-C", "target-feature=-crt-static"]
16-
[target.x86_64-unknown-linux-musl]
17-
rustflags = ["-C", "target-feature=-crt-static"]

bottlecap/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,16 @@ fips = [
114114
[lints.rust]
115115
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] }
116116

117+
# libddwaf's bindgen uses `clang-sys` which by default dynamically opens
118+
# `libclang.so`; however this is not supported on Alpine Linux (the usual musl
119+
# target).
120+
# Enabling the `static` feature of `bindgen`/`clang-sys` creates additional
121+
# build requirements (`libLLVM.a`), which Alpine's `llvm-static` does not
122+
# provide (it has its content presented as many `libLLVM*.a` files). Disabling
123+
# statically linking to the C runtime alleviates this issue, but can result in
124+
# `proc-macro` not being usable; which notably causes doc tests to fail with
125+
# a big linker error.
126+
[target.aarch64-unknown-linux-musl]
127+
rustflags = ["-C", "target-feature=-crt-static"]
128+
[target.x86_64-unknown-linux-musl]
129+
rustflags = ["-C", "target-feature=-crt-static"]

images/Dockerfile.bottlecap.alpine.compile

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG PLATFORM
44
ARG FIPS
55

66
# Install dependencies
7-
RUN apk add --no-cache curl gcc musl-dev make unzip bash \
7+
RUN apk add --no-cache curl gcc build-base make unzip bash \
88
autoconf automake libtool g++ perl go cmake linux-headers \
99
clang-libclang
1010

@@ -28,11 +28,15 @@ COPY ./bottlecap/Cargo.lock /tmp/dd/bottlecap/Cargo.lock
2828

2929
# Build the binary
3030
#
31-
# Added `-C link-arg=-lgcc` for alpine.
32-
ENV RUSTFLAGS="-C panic=abort -C link-arg=-lgcc"
31+
# Added `-C link-arg=-lgcc` and `-Ctarget-feature=-crt-static` for alpine, as otherwise `bindgen` is
32+
# unable to load `libclang` dynamically (on `musl`, the C runtime is static by default, and that
33+
# makes it incapable of dynamically loading libraries - static-linking `libclang` would be possible
34+
# if it was not such a pain to build in that case).
35+
ENV RUSTFLAGS="-Cpanic=abort -Clink-arg=-lgcc -Ctarget-feature=-crt-static"
3336

3437
WORKDIR /tmp/dd/bottlecap
35-
RUN --mount=type=cache,target=/root/.cargo/registry \
38+
RUN --mount=type=cache,target=/root/.cargo/git \
39+
--mount=type=cache,target=/root/.cargo/registry \
3640
export PROFILE="release"; \
3741
if [ "$FIPS" = "1" ]; then \
3842
export FEATURES=fips; \
@@ -43,8 +47,12 @@ RUN --mount=type=cache,target=/root/.cargo/registry \
4347
export FEATURES=default; \
4448
fi; \
4549
env; \
46-
cargo +stable build --verbose --no-default-features --features $FEATURES --profile $PROFILE --target $PLATFORM-unknown-linux-musl; \
47-
cp /tmp/dd/bottlecap/target/$PLATFORM-unknown-linux-musl/$PROFILE/bottlecap /tmp/dd/bottlecap/bottlecap
50+
# Explicitly NOT passing `--target` as this makes the `RUSTFLAGS` (from any source) not apply to `build.rs`
51+
# compilation, which in turns means `libddwaf`'s builder fails to build because on MUSL platforms it can only work
52+
# with `-Ctarget-feature=-crt-runtime` being specified. There is currently no way to set `RUSTFLAGS` for `build.rs`
53+
# compilation if `--target` is passed (see: https://github.com/rust-lang/cargo/issues/4423).
54+
cargo +stable build --verbose --no-default-features --features $FEATURES --profile $PROFILE; \
55+
cp /tmp/dd/bottlecap/target/$PROFILE/bottlecap /tmp/dd/bottlecap/bottlecap
4856

4957
# Use the smallest image possible
5058
FROM scratch

images/Dockerfile.bottlecap.compile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG PLATFORM
44
ARG FIPS
55

66
# Install dependencies
7-
RUN yum install -y curl clang make unzip cmake3 perl go
7+
RUN yum install -y curl clang compiler-rt make unzip cmake3 perl go
88

99
# Install Protocol Buffers compiler by hand, since AL2 does not have a recent enough version.
1010
COPY ./scripts/install-protoc.sh /
@@ -24,7 +24,7 @@ COPY ./bottlecap/Cargo.toml /tmp/dd/bottlecap/Cargo.toml
2424
COPY ./bottlecap/Cargo.lock /tmp/dd/bottlecap/Cargo.lock
2525

2626
# Build the binary
27-
ENV RUSTFLAGS="-C panic=abort"
27+
ENV RUSTFLAGS="-Cpanic=abort"
2828
ENV AWS_LC_FIPS_SYS_CC=clang
2929
ENV AWS_LC_FIPS_SYS_CXX=clang++
3030

@@ -35,6 +35,7 @@ RUN --mount=type=cache,target=/usr/local/cargo/registry \
3535
else \
3636
export FEATURES=default; \
3737
fi; \
38+
export RUSTFLAGS="${RUSTFLAGS} -Clinker=clang -L$(dirname $(clang --print-file-name="libclang_rt.builtins-$(uname -m).a")) -lclang_rt.builtins-$(uname -m)"; \
3839
cargo +stable build --no-default-features --features $FEATURES --release --target $PLATFORM-unknown-linux-gnu;
3940
RUN cp /tmp/dd/bottlecap/target/$PLATFORM-unknown-linux-gnu/release/bottlecap /tmp/dd/bottlecap/bottlecap
4041

scripts/build_bottlecap_layer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ rm -rf ${EXTENSION_PATH} 2>/dev/null
3737

3838
cd $ROOT_DIR
3939

40-
ALPINE=0
40+
ALPINE=${ALPINE:-0}
4141
ARCHITECTURE=$ARCHITECTURE ALPINE=$ALPINE FILE_SUFFIX=$FILE_SUFFIX .gitlab/scripts/compile_bottlecap.sh
4242

4343
docker buildx build --platform linux/${ARCHITECTURE} \

0 commit comments

Comments
 (0)