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