|
1 | 1 | # eve-rust
|
2 | 2 |
|
3 |
| -Base image for building rust-based executables in containers for the EVE platform. Contains the baic rust binaries |
| 3 | +Base image for building rust-based executables in containers for the EVE platform. Contains the basic rust binaries |
4 | 4 | such as `rustc`, `cargo`, `rustup`, etc., as well as additional plugins and targets. As of this writing:
|
5 | 5 |
|
6 | 6 | * `cargo-chef` - a tool for building and caching dependencies for a rust project
|
7 | 7 | * `cargo-sbom` - a tool for generating SBoMs for rust-based projects
|
8 | 8 | * targets to build for linux amd64, arm64 and riscv64 on linux for both musl and glibc
|
9 | 9 |
|
| 10 | +The image also includes additional tools to support cross-compilation |
| 11 | + |
| 12 | +* `mold` - a very fast linker for cross-compilation targets |
| 13 | +* `clang` - to invoke `mold` for cross-compilation targets |
| 14 | + |
10 | 15 | ## Usage
|
11 | 16 |
|
12 | 17 | Use this image as a base `FROM` when building in your EVE Dockerfile. For example:
|
@@ -58,6 +63,47 @@ RUN cargo build --release
|
58 | 63 | RUN cargo sbom > sbom.spdx.json
|
59 | 64 | ```
|
60 | 65 |
|
| 66 | +To enable cross-compilation we need few extra steps. By default cargo builds for host platform so the target must be specified explicitly either using `--target <target>` or by setting `CARGO_BUILD_TARGET` environment variable. See [Cargo docs](https://doc.rust-lang.org/cargo/reference/environment-variables.html?highlight=CARGO_BUILD_TARGET#configuration-environment-variables) |
| 67 | + |
| 68 | +```Dockerfile |
| 69 | +# we use host tools to avoid emulation and slow builds |
| 70 | +FROM --platform=$BUILDPLATFORM lfedge/eve-rust:1.80.1 AS rust-host |
| 71 | +ARG TARGETARCH |
| 72 | + |
| 73 | +# map Docker's $TARGETARCH to Rust target |
| 74 | +FROM rust-host AS target-amd64 |
| 75 | +ENV CARGO_BUILD_TARGET="x86_64-unknown-linux-musl" |
| 76 | + |
| 77 | +FROM rust-host AS target-arm64 |
| 78 | +ENV CARGO_BUILD_TARGET="aarch64-unknown-linux-musl" |
| 79 | + |
| 80 | +FROM rust-host AS target-riscv64 |
| 81 | +ENV CARGO_BUILD_TARGET="riscv64gc-unknown-linux-gnu" |
| 82 | + |
| 83 | +FROM target-$TARGETARCH AS rust |
| 84 | + |
| 85 | +ADD https://github.com/foo/bar.git#v1.2.3 /src/foo |
| 86 | +WORKDIR /src/foo |
| 87 | + |
| 88 | +# invoke you build here e.g. cargo build --release |
| 89 | + |
| 90 | +# cargo creates a subdirectory /<your app>/target/$CARGO_BUILD_TARGET |
| 91 | +# copy build artifacts to a common place to avoid passing extra ARG to following |
| 92 | +# stage that doesn't inherit the environment |
| 93 | +RUN cp /src/foo/target/$CARGO_BUILD_TARGET/release/foo /src/foo/target/release/foo |
| 94 | + |
| 95 | + |
| 96 | +FROM lfedge/eve-alpine:1f7685f95a475c6bbe682f0b976f12180b6c8726 AS build |
| 97 | +# do the rest of your regular eve-alpine work |
| 98 | + |
| 99 | +COPY --from=rust /src/foo/target/release/foo /out/foo |
| 100 | + |
| 101 | +FROM scratch |
| 102 | + |
| 103 | +COPY --from=build /out/ / |
| 104 | +``` |
| 105 | + |
| 106 | + |
61 | 107 | ## Supported platforms
|
62 | 108 |
|
63 | 109 | This image is built for linux/amd64 and linux/arm64. riscv64 is
|
|
0 commit comments