Skip to content

Commit

Permalink
chore: add Docker-based development environment
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed May 8, 2024
1 parent da13767 commit 49d90e7
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 172 deletions.
9 changes: 0 additions & 9 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
[alias]
xtask = "run --package xtask --"

[target.x86_64-unknown-linux-gnu]
linker = "x86_64-linux-gnu-gcc"

[target.arm-unknown-linux-musleabihf]
linker = "arm-linux-gnueabihf-gcc"

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-gnu-gcc"
65 changes: 1 addition & 64 deletions bakery/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,67 +1,4 @@
#########################################################################################
# Rust Build Environment
#
# The image contains everything necessary to (cross)-compile our Rust crates.
#########################################################################################
FROM debian:latest as build-env

COPY bakery/layers/build-env/00-base.sh /tmp/rugpi-docker/00-base.sh
RUN /tmp/rugpi-docker/00-base.sh

# Install Rust toolchain.
ENV RUSTUP_HOME="/usr/local/rustup" \
CARGO_HOME="/usr/local/cargo" \
PATH="/usr/local/cargo/bin:${PATH}" \
RUST_VERSION="1.76.0"

COPY bakery/layers/build-env/10-rust.sh /tmp/rugpi-docker/10-rust.sh
RUN /tmp/rugpi-docker/10-rust.sh

# Install libraries and configure for cross compilation.
ENV PKG_CONFIG_SYSROOT_DIR="/"

COPY bakery/layers/build-env/20-libs.sh /tmp/rugpi-docker/20-libs.sh
RUN /tmp/rugpi-docker/20-libs.sh

ENV RUGPI_BUILD_ENV="true"

WORKDIR /project
CMD /usr/bin/zsh


#########################################################################################
# `cargo-chef` Planner
#
# We are using `cargo-chef` to speed up builds of the image.
#########################################################################################
FROM build-env AS planner

COPY . .
RUN cargo chef prepare --recipe-path recipe.json


#########################################################################################
# `cargo-chef` Builder
#
# Build all Rust crates with `cargo-chef`.
#########################################################################################
FROM build-env AS builder

COPY --from=planner /project/recipe.json recipe.json
COPY bakery/layers/builder/00-prepare.sh /tmp/rugpi-docker/00-prepare.sh
RUN /tmp/rugpi-docker/00-prepare.sh

COPY . .
COPY bakery/layers/builder/10-build.sh /tmp/rugpi-docker/10-build.sh
RUN /tmp/rugpi-docker/10-build.sh


#########################################################################################
# Rugpi Bakery Image
#
# Now combine everything in the `rugpi-bakery` image.
#########################################################################################
FROM debian:latest AS bakery
FROM debian:latest

ARG TARGETPLATFORM

Expand Down
22 changes: 0 additions & 22 deletions bakery/layers/build-env/00-base.sh

This file was deleted.

45 changes: 0 additions & 45 deletions bakery/layers/build-env/10-rust.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bakery/layers/build-env/20-libs.sh

This file was deleted.

9 changes: 0 additions & 9 deletions bakery/layers/builder/00-prepare.sh

This file was deleted.

9 changes: 0 additions & 9 deletions bakery/layers/builder/10-build.sh

This file was deleted.

32 changes: 31 additions & 1 deletion xfile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
!cargo xtask
#!/usr/bin/env bash

set -euo pipefail

DOCKER=${DOCKER:-"docker"}

COMPOSE_FILE="./xtask/devenv/compose.yml"

if [ $($DOCKER compose --file "$COMPOSE_FILE" ps -q | wc -l) -gt 0 ]; then
echo "Development environment is already running."
else
echo "Starting development environment."
$DOCKER compose --file "$COMPOSE_FILE" up --build -d
fi

if [ "${1:-}" = "devenv" ]; then
shift
exec $DOCKER compose \
--file ./xtask/devenv/compose.yml \
"$@"
fi

if [ "${1:-}" = "shell" ]; then
exec $DOCKER compose \
--file ./xtask/devenv/compose.yml \
exec --privileged devenv "/project/xtask/devenv/run.sh" zsh
fi

exec $DOCKER compose \
--file ./xtask/devenv/compose.yml \
exec --privileged devenv "/project/xtask/devenv/run.sh" cargo xtask "$@"
7 changes: 7 additions & 0 deletions xtask/devenv/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM rust:latest

COPY xtask/devenv/setup.sh .

RUN ./setup.sh && rm -f ./setup.sh

WORKDIR /project
14 changes: 14 additions & 0 deletions xtask/devenv/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: rugpi

services:
devenv:
build:
context: ../../
dockerfile: xtask/devenv/Dockerfile
environment:
- CARGO_HOME=/tmp/.cargo
- CARGO_TARGET_DIR=/tmp/target
volumes:
- ../../:/project
- /var/run/docker.sock:/var/run/docker.sock
entrypoint: "sleep infinity"
14 changes: 14 additions & 0 deletions xtask/devenv/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -euo pipefail

export CC_x86_64_unknown_linux_musl=x86_64-linux-gnu-gcc
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld"

export CC_aarch64_unknown_linux_musl=aarch64-linux-gnu-gcc
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld"

export CC_arm_unknown_linux_musleabihf=arm-linux-gnueabihf-gcc
export CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld"

exec "$@"
48 changes: 48 additions & 0 deletions xtask/devenv/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

set -euo pipefail

HOST_ARCH=$(dpkg --print-architecture)

echo "Host Architecture: ${HOST_ARCH}"

export DEBIAN_FRONTEND=noninteractive

apt-get -y update

apt-get -y install \
build-essential \
curl \
file \
git \
python3 \
python3-pip \
wget \
zsh

wget -O /etc/zsh/zshrc https://git.grml.org/f/grml-etc-core/etc/zsh/zshrc
touch /root/.zshrc

case "${HOST_ARCH}" in
"arm64")
apt-get -y install gcc-x86-64-linux-gnu libc6-dev-amd64-cross
;;
"amd64")
apt-get -y install gcc-aarch64-linux-gnu libc6-dev-arm64-cross
;;
*)
echo "Error: Unsupported architecture \`${HOST_ARCH}\`."
exit 1
;;
esac

# Add `armhf` toolchain regardless of host architecture.
apt-get install -y gcc-arm-linux-gnueabihf libc6-dev-armhf-cross

apt-get install -y musl-tools clang pkg-config docker.io

apt-get -y clean && rm -rf /var/lib/apt/lists/*

rustup target add arm-unknown-linux-musleabihf
rustup target add aarch64-unknown-linux-musl
rustup target add x86_64-unknown-linux-musl
12 changes: 10 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ pub fn project_path() -> PathBuf {
path
}

pub fn get_target_dir() -> PathBuf {
if let Ok(target_dir) = std::env::var("CARGO_TARGET_DIR") {
target_dir.into()
} else {
project_path().join("target")
}
}

fn main() -> anyhow::Result<()> {
let args = Args::parse();
let env = LocalEnv::new(project_path());
Expand Down Expand Up @@ -63,7 +71,7 @@ fn main() -> anyhow::Result<()> {
std::fs::remove_dir_all(&binaries_dir)?;
}
std::fs::create_dir_all(&binaries_dir)?;
let target_dir = project_path().join("target").join(target).join("release");
let target_dir = get_target_dir().join(target).join("release");
for entry in std::fs::read_dir(&target_dir)? {
let entry = entry?;
let file_type = entry.file_type()?;
Expand All @@ -76,7 +84,7 @@ fn main() -> anyhow::Result<()> {
if !file_name.starts_with("rugpi-") || file_name.ends_with(".d") {
continue;
}
std::fs::hard_link(entry.path(), binaries_dir.join(file_name))?;
std::fs::copy(entry.path(), binaries_dir.join(file_name))?;
}
}
}
Expand Down

0 comments on commit 49d90e7

Please sign in to comment.