Skip to content

Commit

Permalink
feat!: cross compile rust application binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
vfusco committed Dec 6, 2023
1 parent 712e2d4 commit e86adc9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 50 deletions.
76 changes: 56 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
build-essential \
ca-certificates \
git \
protobuf-compiler \
rsync \
wget \
rust-all=1.58.1+dfsg1~ubuntu1-0ubuntu2 \
&& \
wget -O ${LINUX_HEADERS_FILEPATH} ${LINUX_HEADERS_URLPATH} && \
echo "aace918b830ee2d682c329b3361a3458ebc42ba5193ccb821fd0b2071ac821b3 ${LINUX_HEADERS_FILEPATH}" | sha256sum --check && \
Expand All @@ -54,44 +52,82 @@ RUN make -C ${BUILD_BASE}/tools/sys-utils/rollup/ CROSS_COMPILE="" rollup.toolch

# build rust tools
# ------------------------------------------------------------------------------
FROM builder as rust-builder
FROM --platform=${BUILDPLATFORM} ubuntu:22.04 as rust-builder
ARG IMAGE_KERNEL_VERSION=v0.19.1
ARG LINUX_VERSION=6.5.9-ctsi-1
ARG BUILD_BASE=/opt/cartesi
ENV LINUX_HEADERS_FILEPATH=/tmp/linux-libc-dev-riscv64-cross-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb

# NOTE: cargo update RAM usage keeps going up without this
RUN mkdir -p $HOME/.cargo && \
echo "[net]" >> $HOME/.cargo/config && \
echo "git-fetch-with-cli = true" >> $HOME/.cargo/config
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
apt-get upgrade -y && \
apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
git \
wget \
crossbuild-essential-riscv64 \
gcc-12-riscv64-linux-gnu \
&& \
wget -O ${LINUX_HEADERS_FILEPATH} https://github.com/cartesi/image-kernel/releases/download/${IMAGE_KERNEL_VERSION}/linux-libc-dev-riscv64-cross-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb && \
echo "efdb2243d9b6828e90c826be0f178110f0cc590cb00e8fa588cb20723126c2a4 ${LINUX_HEADERS_FILEPATH}" | sha256sum --check && \
apt-get install -y --no-install-recommends ${LINUX_HEADERS_FILEPATH} && \
adduser developer -u 499 --gecos ",,," --disabled-password && \
mkdir -p ${BUILD_BASE}/tools && chown -R developer:developer ${BUILD_BASE}/tools && \
rm -rf /var/lib/apt/lists/* ${LINUX_HEADERS_FILEPATH}

COPY rollup-http/rollup-init ${BUILD_BASE}/tools/rollup-http/rollup-init
COPY rollup-http/rollup-http-client ${BUILD_BASE}/tools/rollup-http/rollup-http-client
USER developer
RUN \
cd && \
wget https://github.com/rust-lang/rustup/archive/refs/tags/1.26.0.tar.gz && \
echo "6f20ff98f2f1dbde6886f8d133fe0d7aed24bc76c670ea1fca18eb33baadd808 1.26.0.tar.gz" | sha256sum --check && \
tar -xzf 1.26.0.tar.gz && \
bash rustup-1.26.0/rustup-init.sh \
-y \
--default-toolchain 1.74.0 \
--profile minimal \
--target riscv64gc-unknown-linux-gnu && \
rm -rf rustup-1.26.0 1.26.0.tar.gz


ENV PATH="/home/developer/.cargo/bin:${PATH}"
ENV CC="riscv64-linux-gnu-gcc-12"
ENV CXX="riscv64-linux-gnu-g++-12"
ENV RISCV_ARCH="rv64gc"
ENV RISCV_ABI="lp64d"
ENV CFLAGS="-march=$RISCV_ARCH -mabi=$RISCV_ABI"

COPY --chown=developer:developer rollup-http/rollup-init ${BUILD_BASE}/tools/rollup-http/rollup-init
COPY --chown=developer:developer rollup-http/rollup-http-client ${BUILD_BASE}/tools/rollup-http/rollup-http-client
COPY --chown=developer:developer rollup-http/.cargo ${BUILD_BASE}/tools/rollup-http/.cargo

# build rollup-http-server dependencies
FROM rust-builder as http-server-dep-builder
COPY rollup-http/rollup-http-server/Cargo.toml rollup-http/rollup-http-server/Cargo.lock ${BUILD_BASE}/tools/rollup-http/rollup-http-server/
COPY --chown=developer:developer rollup-http/rollup-http-server/Cargo.toml rollup-http/rollup-http-server/Cargo.lock ${BUILD_BASE}/tools/rollup-http/rollup-http-server/
RUN cd ${BUILD_BASE}/tools/rollup-http/rollup-http-server && \
mkdir src/ && \
echo "fn main() {}" > src/main.rs && \
echo "pub fn dummy() {}" > src/lib.rs && \
cargo build --release
cargo build --target riscv64gc-unknown-linux-gnu --release

# build rollup-http-server
FROM http-server-dep-builder as http-server-builder
COPY rollup-http/rollup-http-server/build.rs ${BUILD_BASE}/tools/rollup-http/rollup-http-server/
COPY rollup-http/rollup-http-server/src ${BUILD_BASE}/tools/rollup-http/rollup-http-server/src
COPY --chown=developer:developer rollup-http/rollup-http-server/build.rs ${BUILD_BASE}/tools/rollup-http/rollup-http-server/
COPY --chown=developer:developer rollup-http/rollup-http-server/src ${BUILD_BASE}/tools/rollup-http/rollup-http-server/src
RUN cd ${BUILD_BASE}/tools/rollup-http/rollup-http-server && \
cargo build --release
cargo build --target riscv64gc-unknown-linux-gnu --release

# build echo-dapp dependencies
FROM rust-builder as echo-dapp-dep-builder
COPY rollup-http/echo-dapp/Cargo.toml rollup-http/echo-dapp/Cargo.lock ${BUILD_BASE}/tools/rollup-http/echo-dapp/
COPY --chown=developer:developer rollup-http/echo-dapp/Cargo.toml rollup-http/echo-dapp/Cargo.lock ${BUILD_BASE}/tools/rollup-http/echo-dapp/
RUN cd ${BUILD_BASE}/tools/rollup-http/echo-dapp && \
mkdir src/ && echo "fn main() {}" > src/main.rs && \
cargo build --release
cargo build --target riscv64gc-unknown-linux-gnu --release

# build echo-dapp
FROM echo-dapp-dep-builder as echo-dapp-builder
COPY rollup-http/echo-dapp/src ${BUILD_BASE}/tools/rollup-http/echo-dapp/src
COPY --chown=developer:developer rollup-http/echo-dapp/src ${BUILD_BASE}/tools/rollup-http/echo-dapp/src
RUN cd ${BUILD_BASE}/tools/rollup-http/echo-dapp && \
cargo build --release
cargo build --target riscv64gc-unknown-linux-gnu --release

# pack tools (deb)
# ------------------------------------------------------------------------------
Expand All @@ -114,7 +150,7 @@ RUN mkdir -p ${STAGING_DEBIAN} ${STAGING_SBIN} ${STAGING_BIN} ${STAGING_BASE}/et
COPY control ${STAGING_DEBIAN}/control

COPY --from=rust-builder ${BUILD_BASE}/tools/rollup-http/rollup-init/rollup-init ${STAGING_SBIN}
COPY --from=http-server-builder ${BUILD_BASE}/tools/rollup-http/rollup-http-server/target/release/rollup-http-server ${STAGING_BIN}
COPY --from=echo-dapp-builder ${BUILD_BASE}/tools/rollup-http/echo-dapp/target/release/echo-dapp ${STAGING_BIN}
COPY --from=http-server-builder ${BUILD_BASE}/tools/rollup-http/rollup-http-server/target/riscv64gc-unknown-linux-gnu/release/rollup-http-server ${STAGING_BIN}
COPY --from=echo-dapp-builder ${BUILD_BASE}/tools/rollup-http/echo-dapp/target/riscv64gc-unknown-linux-gnu/release/echo-dapp ${STAGING_BIN}

RUN dpkg-deb -Zxz --root-owner-group --build ${STAGING_BASE} ${BUILD_BASE}/${TOOLS_DEB}
46 changes: 30 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ PATCH := 0
LABEL :=
VERSION := $(MAJOR).$(MINOR).$(PATCH)$(LABEL)

TOOLS_DEB := machine-emulator-tools-v$(VERSION).deb
TOOLS_IMAGE := cartesi/machine-emulator-tools:$(VERSION)
TOOLS_ROOTFS := rootfs-tools-v$(VERSION).ext2
TOOLS_DEB := machine-emulator-tools-v$(VERSION).deb
TOOLS_IMAGE := cartesi/machine-emulator-tools:$(VERSION)
TOOLS_ROOTFS := rootfs-tools-v$(VERSION).ext2

IMAGE_KERNEL_VERSION ?= v0.19.1
LINUX_VERSION ?= 6.5.9-ctsi-1
Expand All @@ -31,17 +31,15 @@ LINUX_HEADERS_URLPATH := https://github.com/cartesi/image-kernel/releases/downlo
all: fs

build: control
@if ! (docker image inspect "$(TOOLS_IMAGE)" >/dev/null 2>&1) || [[ "$(force)" == "true" ]]; then \
docker buildx build --platform=linux/riscv64 --load \
--build-arg TOOLS_DEB=$(TOOLS_DEB) \
--build-arg IMAGE_KERNEL_VERSION=$(IMAGE_KERNEL_VERSION) \
--build-arg LINUX_VERSION=$(LINUX_VERSION) \
--build-arg LINUX_HEADERS_URLPATH=$(LINUX_HEADERS_URLPATH) \
-t $(TOOLS_IMAGE) \
-f Dockerfile \
. ; \
fi
@$(MAKE) copy
@docker buildx build --platform=linux/riscv64 --load \
--build-arg TOOLS_DEB=$(TOOLS_DEB) \
--build-arg IMAGE_KERNEL_VERSION=$(IMAGE_KERNEL_VERSION) \
--build-arg LINUX_VERSION=$(LINUX_VERSION) \
--build-arg LINUX_HEADERS_URLPATH=$(LINUX_HEADERS_URLPATH) \
-t $(TOOLS_IMAGE) \
-f Dockerfile \
. ; \
$(MAKE) copy

copy:
@ID=`docker create --platform=linux/riscv64 $(TOOLS_IMAGE)` && \
Expand Down Expand Up @@ -78,6 +76,20 @@ setup:
setup-required:
@echo 'riscv64 buildx setup required:' `docker buildx ls | grep -q riscv64 && echo no || echo yes`

build-rust-builder-env:
docker build --target rust-builder -t $(TOOLS_IMAGE)-rust-builder -f Dockerfile .

rust-builder-env:
@docker run --hostname rust-builder -it --rm \
-e USER=$$(id -u -n) \
-e GROUP=$$(id -g -n) \
-e UID=$$(id -u) \
-e GID=$$(id -g) \
-v `pwd`:/opt/cartesi/machine-emulator-tools \
-w /opt/cartesi/machine-emulator-tools \
$(TOOLS_IMAGE)-rust-builder /bin/bash


help:
@echo 'available commands:'
@echo ' deb - build machine-emulator-tools .deb package'
Expand All @@ -91,7 +103,9 @@ help:
clean-image:
@(docker rmi $(TOOLS_IMAGE) > /dev/null 2>&1 || true)

distclean clean: clean-image
rm -f $(TOOLS_DEB) control rootfs*
clean:
@rm -f $(TOOLS_DEB) control rootfs*

distclean: clean clean-image

.PHONY: build fs deb env setup setup-required help distclean
5 changes: 5 additions & 0 deletions rollup-http/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.riscv64gc-unknown-linux-gnu]
linker = "riscv64-linux-gnu-gcc-12"

[net]
git-fetch-with-cli = true
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ CONTAINER_BASE="${CONTAINER_BASE:-/opt/cartesi/tools}"

docker run --rm -e USER=`id -u -n` -e GROUP=`id -g -n` -e UID=`id -u` -e GID=`id -g` -v `pwd`/../:$CONTAINER_BASE \
--env CC=riscv64-cartesi-linux-gnu-gcc --env CXX=riscv64-cartesi-linux-gnu-g++ --env CFLAGS="-march=$RISCV_ARCH -mabi=$RISCV_ABI" \
--env CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER=riscv64-cartesi-linux-gnu-gcc \
-w $CONTAINER_BASE/echo-dapp $TOOLCHAIN_IMAGE:$TOOLCHAIN_TAG \
cargo build --target riscv64gc-unknown-linux-gnu --release
7 changes: 0 additions & 7 deletions rollup-http/echo-dapp/environment.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ CONTAINER_BASE="${CONTAINER_BASE:-/opt/cartesi/tools}"

docker run --rm -e USER=`id -u -n` -e GROUP=`id -g -n` -e UID=`id -u` -e GID=`id -g` -v `pwd`/../:$CONTAINER_BASE \
--env CC=riscv64-cartesi-linux-gnu-gcc --env CXX=riscv64-cartesi-linux-gnu-g++ --env CFLAGS="-march=$RISCV_ARCH -mabi=$RISCV_ABI" \
--env CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER=riscv64-cartesi-linux-gnu-gcc \
-w $CONTAINER_BASE/rollup-http-server $TOOLCHAIN_IMAGE:$TOOLCHAIN_TAG \
cargo build --target riscv64gc-unknown-linux-gnu --release
7 changes: 0 additions & 7 deletions rollup-http/rollup-http-server/environment.sh

This file was deleted.

0 comments on commit e86adc9

Please sign in to comment.