Skip to content

Commit 41b3573

Browse files
authored
Build ffi on alpine (#65)
* Build ffi on alpine * Add arm build - improve caching
1 parent 1d4b8c8 commit 41b3573

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
build/
3+
.github/
4+
Dockerfile.build
5+
.git/
6+
docker-bake.hcl

.github/buildkitd.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[worker.oci]
2+
# Set max parallelism for buildx bake workers to prevent builds running slower due to too many steps being executed at the same time
3+
max-parallelism = 4

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,32 @@ jobs:
9494
cd examples/ffi/build
9595
cmake -S .. -DDatadog_ROOT=$OUTPUT_FOLDER
9696
cmake --build .
97+
98+
ffi_bake:
99+
strategy:
100+
matrix:
101+
target: [alpine-build] # debian-build-aarch64 is oom killed at the moment
102+
name: "FFI ${{ matrix.target }} via docker bake"
103+
104+
concurrency:
105+
group: ci-${{ github.ref }}-${{ matrix.target }}
106+
cancel-in-progress: true
107+
108+
runs-on: ubuntu-latest
109+
steps:
110+
-
111+
name: Checkout
112+
uses: actions/checkout@v3
113+
-
114+
name: Set up Docker Buildx
115+
uses: docker/setup-buildx-action@v2
116+
with:
117+
config: .github/buildkitd.toml
118+
-
119+
name: Build and Generate FFI
120+
uses: docker/bake-action@v2
121+
with:
122+
targets: ${{ matrix.target }}
123+
set: |
124+
*.cache-from=type=gha,scope=${{ matrix.target }}
125+
*.cache-to=type=gha,mode=max,scope=${{ matrix.target }}

docker-bake.hcl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
target "alpine-base" {
2+
dockerfile = "tools/docker/Dockerfile.build"
3+
tags = ["ghcr.io/datadog/libdatadog-build:alpine-base"]
4+
target = "alpine_builder"
5+
}
6+
7+
target "alpine-build" {
8+
dockerfile = "tools/docker/Dockerfile.build"
9+
args = {
10+
BUILDER_IMAGE = "alpine_builder"
11+
}
12+
target = "ffi_build_output"
13+
platforms = ["linux/amd64"]
14+
output = ["build/x86_64-alpine-linux-musl"]
15+
}
16+
17+
target "debian-build" {
18+
dockerfile = "tools/docker/Dockerfile.build"
19+
args = {
20+
BUILDER_IMAGE = "debian_builder"
21+
}
22+
target = "ffi_build_output"
23+
platforms = ["linux/amd64"]
24+
output = ["build/x86_64-unknown-linux-gnu"]
25+
}
26+
27+
target "alpine-build-aarch64" {
28+
inherits = ["alpine-build"]
29+
platforms = ["linux/arm64"]
30+
output = ["build/aarch64-alpine-linux-musl"]
31+
}
32+
33+
target "debian-build-aarch64" {
34+
inherits = ["debian-build"]
35+
platforms = ["linux/arm64"]
36+
output = ["build/aarch64-unknown-linux-gnu"]
37+
}

tools/docker/Dockerfile.build

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
ARG ALPINE_BASE_IMAGE="alpine:3.16"
2+
ARG CARGO_BUILD_INCREMENTAL="true"
3+
ARG CARGO_NET_RETRY="2"
4+
ARG BUILDER_IMAGE=debian_builder
5+
6+
### Debian builder
7+
FROM rust:1-slim-buster as debian_builder
8+
ENV CARGO_HOME="/root/.cargo"
9+
WORKDIR /build
10+
RUN cargo install cbindgen; mv /root/.cargo/bin/cbindgen /usr/bin/; rm -rf /root/.cargo
11+
12+
### Debian buildplatform builder
13+
FROM --platform=$BUILDPLATFORM rust:1-slim-buster as debian_builder_platform_native
14+
ENV CARGO_HOME="/root/.cargo"
15+
WORKDIR /build
16+
17+
### Alpine builder
18+
FROM ${ALPINE_BASE_IMAGE} as alpine_base
19+
ENV CARGO_HOME="/root/.cargo"
20+
WORKDIR /build
21+
22+
RUN apk update \
23+
&& apk add --no-cache \
24+
build-base \
25+
cargo \
26+
curl \
27+
git \
28+
make \
29+
patchelf \
30+
protoc \
31+
pkgconf \
32+
unzip \
33+
bash \
34+
&& mkdir /usr/local/src
35+
36+
# Tell docker to use bash as the default
37+
SHELL ["/bin/bash", "-c"]
38+
39+
# Don't use rustup! For some reason it provides different native-static-libs
40+
# and this can cause problems for users.
41+
# Also, it doesn't understand x86_64-alpine-linux-musl like the OS's cargo.
42+
#RUN rustup-init -y --no-modify-path --default-toolchain stable
43+
44+
FROM alpine_base as alpine_aws_cli
45+
RUN apk add --no-cache \
46+
python3 \
47+
py3-pip \
48+
groff \
49+
&& pip3 install --upgrade pip \
50+
&& pip3 install --no-cache-dir \
51+
awscli \
52+
&& rm -rf /var/cache/apk/*
53+
54+
RUN aws --version # Just to make sure its installed alright
55+
56+
FROM alpine_base as alpine_cbindgen
57+
ENV PATH="/root/.cargo/bin:$PATH"
58+
ARG CARGO_BUILD_INCREMENTAL
59+
ARG CARGO_NET_RETRY
60+
ENV CARGO_NET_RETRY="${CARGO_NET_RETRY}"
61+
RUN cargo install cbindgen && rm -rf /root/.cargo/registry /root/.cargo/git
62+
63+
FROM alpine_aws_cli as alpine_builder
64+
COPY --from=alpine_cbindgen /root/.cargo/bin/cbindgen /usr/local/bin/cbindgen
65+
66+
67+
### Cache cargo metadata between builds
68+
FROM debian_builder_platform_native AS ffi_build_platform_agnostic_cache_build
69+
# update cache cargo.io metadata
70+
RUN cargo search nothing --limit 1
71+
72+
# create stubs to cache compilation of dependendencies
73+
COPY [ "Cargo.lock", "Cargo.toml", "./"]
74+
COPY "ddcommon/Cargo.toml" "ddcommon/"
75+
COPY "ddcommon-ffi/Cargo.toml" "ddcommon-ffi/"
76+
COPY "ddtelemetry/Cargo.toml" "ddtelemetry/"
77+
COPY "ddtelemetry-ffi/Cargo.toml" "ddtelemetry-ffi/"
78+
COPY "profiling/Cargo.toml" "profiling/"
79+
COPY "profiling-ffi/Cargo.toml" "profiling-ffi/"
80+
COPY "tools/Cargo.toml" "tools/"
81+
RUN find -name "Cargo.toml" | sed -e s#Cargo.toml#src/lib.rs#g | xargs -n 1 sh -c 'mkdir -p $(dirname $1); touch $1; echo $1' create_stubs
82+
RUN echo tools/src/bin/dedup_headers.rs ddtelemetry/examples/tm-worker-test.rs | xargs -n 1 sh -c 'mkdir -p $(dirname $1); touch $1; echo $1' create_stubs
83+
84+
# cache dependencies
85+
RUN cargo fetch --locked
86+
87+
# extract cargo cache
88+
FROM --platform=$BUILDPLATFORM scratch as ffi_build_platform_agnostic_cache
89+
COPY --from=ffi_build_platform_agnostic_cache_build /root/.cargo /root/.cargo
90+
COPY --from=ffi_build_platform_agnostic_cache_build /build /build
91+
92+
### FFI builder
93+
FROM ${BUILDER_IMAGE} AS ffi_build
94+
COPY --from=ffi_build_platform_agnostic_cache /root/.cargo /root/.cargo/
95+
COPY --from=ffi_build_platform_agnostic_cache /build /build
96+
WORKDIR /build
97+
# cache debug dependency build
98+
RUN cargo build --lib --all
99+
# cache release dependency build
100+
RUN cargo build --release --lib --all
101+
102+
COPY ./ ./
103+
RUN ./build-profiling-ffi.sh /build/output
104+
105+
FROM scratch as ffi_build_output
106+
COPY --from=ffi_build /build/output/ ./

0 commit comments

Comments
 (0)