Skip to content

Commit e3bbfee

Browse files
authored
Merge pull request #7 from rucoder/rucoder/docs-cross
2 parents 92a0c01 + 08e5528 commit e3bbfee

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# eve-rust
22

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
44
such as `rustc`, `cargo`, `rustup`, etc., as well as additional plugins and targets. As of this writing:
55

66
* `cargo-chef` - a tool for building and caching dependencies for a rust project
77
* `cargo-sbom` - a tool for generating SBoMs for rust-based projects
88
* targets to build for linux amd64, arm64 and riscv64 on linux for both musl and glibc
99

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+
1015
## Usage
1116

1217
Use this image as a base `FROM` when building in your EVE Dockerfile. For example:
@@ -58,6 +63,47 @@ RUN cargo build --release
5863
RUN cargo sbom > sbom.spdx.json
5964
```
6065

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+
61107
## Supported platforms
62108

63109
This image is built for linux/amd64 and linux/arm64. riscv64 is

0 commit comments

Comments
 (0)