diff --git a/earthly/rust/Earthfile b/earthly/rust/Earthfile new file mode 100644 index 000000000..ed1f0af7f --- /dev/null +++ b/earthly/rust/Earthfile @@ -0,0 +1,103 @@ +# Common Rust UDCs and Builders. +VERSION 0.7 + +# cspell: words rustup rustc automake autotools xutils miri nextest kani + +# Base Rustup build container. +rustup: + # Base it on Debian bookworm-slim + FROM debian:bookworm-slim + + WORKDIR /root + + # Set necessary env vars from this setup. + ENV RUSTUP_HOME=/usr/local/rustup + ENV CARGO_HOME=/usr/local/cargo + ENV CARGO_HOME_BIN=$CARGO_HOME/bin + ENV PATH=$CARGO_HOME_BIN:$PATH + + # Ensure correct directory permissions on the install locations. + RUN mkdir -p $RUSTUP_HOME && \ + mkdir -p $CARGO_HOME_BIN && \ + chmod -R a+w $RUSTUP_HOME $CARGO_HOME + + # Install necessary packages + # Expand this list as needed, rather than adding more tools in later containers. + RUN apt-get update && \ + apt-get install --no-install-recommends -y \ + ca-certificates \ + curl \ + file \ + build-essential \ + autoconf \ + automake \ + autotools-dev \ + libtool \ + xutils-dev \ + mold \ + python3 \ + python3-pip \ + musl-tools \ + clang && \ + rm -rf /var/lib/apt/lists/* + + # Install OpenSSL here but ONLY if we can't avoid using it. + + # Install rustup. + RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain none -y -v + + # Install the default cargo config. + COPY config.toml $CARGO_HOME/config.toml + +# Common Rust setup +# Parameters: +# * toolchain : The `rust-toolchain` toml file. +RUST_SETUP: + COMMAND + ARG toolchain=./rust-toolchain.toml + + # Poetry Installation directory. + # Gives poetry and our poetry project a reliable location. + WORKDIR /build + + # Copy our toolchain dependency. + COPY $toolchain ./rust-toolchain.toml + ENV default_rust_channel=$(grep -oP 'channel\s*=\s*"\K[^"]+' rust-toolchain.toml) + + # Install pinned Rustup from `rust-toolchain.toml` + # Plus nightly latest so we can use it for docs, lints, etc. + RUN rustup default $default_rust_channel && \ + rustup show && \ + rustup toolchain install nightly --component miri --component rust-src && \ + cargo --version && \ + cargo +nightly --version + + # Install tools we use commonly with `cargo`. + RUN cargo install cargo-nextest --locked && \ + cargo install cargo-chef --locked && \ + cargo install kani-verifier --locked && \ + cargo kani setup && \ + cargo install refinery_cli --locked && \ + cargo install cargo-machete --locked + +# Test rust build container +check: + FROM +rustup + + DO +RUST_SETUP --toolchain=example/rust-toolchain.toml + + # Check all the expected tooling is installed and works for both the stable and nightly versions. + RUN rustc --version && \ + rustc +nightly --version && \ + cargo --version && \ + cargo +nightly --version && \ + cargo clippy --version && \ + cargo +nightly clippy --version && \ + cargo nextest --version && \ + cargo chef --version && \ + cargo kani --version && \ + refinery --version && \ + mold --version + + + diff --git a/earthly/rust/Readme.md b/earthly/rust/Readme.md new file mode 100644 index 000000000..61c1a3e0b --- /dev/null +++ b/earthly/rust/Readme.md @@ -0,0 +1,27 @@ +# Rust Earthly Build Containers and UDCs + + + +This repo defines common rust targets and UDCs for use with rust. + +## User Defined Commands + +### RUST_SETUP + +This UDC sets up a rust build environment. + +Rust build environments are locked to the `rust-toolchain.toml` file in the repo. +This ensures that the version of the toolchain used is locked with the dependencies. + +#### Invocation + +In an `Earthfile` in your source repository add: + +```Earthfile +example_rust_builder: + FROM +rustup + + DO +RUST_SETUP --toolchain=./rust-toolchain.toml +``` + +This builder can then be used to build the projects source. diff --git a/earthly/rust/config.toml b/earthly/rust/config.toml new file mode 100644 index 000000000..bc0cacbd0 --- /dev/null +++ b/earthly/rust/config.toml @@ -0,0 +1,52 @@ +# Use MOLD linker where possible. +# cspell: words rustflags armv gnueabihf msvc nextest + +# Should be the default to have fully static rust programs. +[target.x86_64-unknown-linux-musl] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + "-C", "target-feature=+crt-static" +] + +[target.x86_64-unknown-linux-gnu] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + # "-C", "target-feature=+crt-static" - Doesn't work for nextest. +] + +[target.aarch64-unknown-linux-gnu] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + "-C", "target-feature=+crt-static" +] + +[target.aarch64-unknown-linux-musl] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + "-C", "target-feature=+crt-static" +] + +[target.armv7-unknown-linux-gnueabihf] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + "-C", "target-feature=+crt-static" +] + +[target.x86_64-pc-windows-gnu] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + "-C", "target-feature=+crt-static" +] + +[target.x86_64-pc-windows-msvc] +linker = "clang" +rustflags = [ + "-C", "link-arg=-fuse-ld=/usr/bin/mold", + "-C", "target-feature=+crt-static" +] diff --git a/earthly/rust/example/rust-toolchain.toml b/earthly/rust/example/rust-toolchain.toml new file mode 100644 index 000000000..c17e06b4c --- /dev/null +++ b/earthly/rust/example/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.73.0" +profile = "default" +components = [ ]