Skip to content

Commit

Permalink
feat: Rust builder setup (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenj authored Oct 20, 2023
1 parent 5078145 commit 178429b
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
103 changes: 103 additions & 0 deletions earthly/rust/Earthfile
Original file line number Diff line number Diff line change
@@ -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



27 changes: 27 additions & 0 deletions earthly/rust/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Rust Earthly Build Containers and UDCs

<!-- cspell: words rustup -->

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.
52 changes: 52 additions & 0 deletions earthly/rust/config.toml
Original file line number Diff line number Diff line change
@@ -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"
]
4 changes: 4 additions & 0 deletions earthly/rust/example/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.73.0"
profile = "default"
components = [ ]

0 comments on commit 178429b

Please sign in to comment.