From d0e1e2a9b2d602a2f9e1a1ea84f2a30a80c5c254 Mon Sep 17 00:00:00 2001 From: Victor Fusco <1221933+vfusco@users.noreply.github.com> Date: Mon, 4 Dec 2023 19:26:04 -0300 Subject: [PATCH] feat!: cross compile rust application binaries --- Dockerfile | 76 ++++++++++++++----- Makefile | 46 +++++++---- rollup-http/.cargo/config.toml | 5 ++ .../{build.sh => build-with-toolchain.sh} | 1 + rollup-http/echo-dapp/environment.sh | 7 -- .../{build.sh => build-with-toolchain.sh} | 1 + rollup-http/rollup-http-server/environment.sh | 7 -- 7 files changed, 93 insertions(+), 50 deletions(-) create mode 100644 rollup-http/.cargo/config.toml rename rollup-http/echo-dapp/{build.sh => build-with-toolchain.sh} (87%) delete mode 100644 rollup-http/echo-dapp/environment.sh rename rollup-http/rollup-http-server/{build.sh => build-with-toolchain.sh} (87%) delete mode 100644 rollup-http/rollup-http-server/environment.sh diff --git a/Dockerfile b/Dockerfile index 21fbd3df..a41bbb3d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 && \ @@ -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.0 +ARG LINUX_VERSION=6.5.9-ctsi-1 +ARG BUILD_BASE=/opt/cartesi +ENV LINUX_HEADERS_FILEPATH=/tmp/linux-headers-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.tar.xz -# 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-headers-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.tar.xz && \ + echo "fcac2e1b6c5e27dfd9980871495102dedf494539631e4daf3f6508c2d82f7e0e ${LINUX_HEADERS_FILEPATH}" | sha256sum --check && \ + tar -xJf ${LINUX_HEADERS_FILEPATH} -C / && \ + 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 -I/opt/riscv/kernel/work/linux-headers/include" + +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) # ------------------------------------------------------------------------------ @@ -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} diff --git a/Makefile b/Makefile index e03c1eb4..e87b64a8 100644 --- a/Makefile +++ b/Makefile @@ -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.0 LINUX_VERSION ?= 6.5.9-ctsi-1 @@ -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)` && \ @@ -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' @@ -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 diff --git a/rollup-http/.cargo/config.toml b/rollup-http/.cargo/config.toml new file mode 100644 index 00000000..b7f2f7bf --- /dev/null +++ b/rollup-http/.cargo/config.toml @@ -0,0 +1,5 @@ +[target.riscv64gc-unknown-linux-gnu] +linker = "riscv64-linux-gnu-gcc-12" + +[net] +git-fetch-with-cli = true diff --git a/rollup-http/echo-dapp/build.sh b/rollup-http/echo-dapp/build-with-toolchain.sh similarity index 87% rename from rollup-http/echo-dapp/build.sh rename to rollup-http/echo-dapp/build-with-toolchain.sh index 709a1028..d984e3ae 100755 --- a/rollup-http/echo-dapp/build.sh +++ b/rollup-http/echo-dapp/build-with-toolchain.sh @@ -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 diff --git a/rollup-http/echo-dapp/environment.sh b/rollup-http/echo-dapp/environment.sh deleted file mode 100644 index eaee9fed..00000000 --- a/rollup-http/echo-dapp/environment.sh +++ /dev/null @@ -1,7 +0,0 @@ -#! /bin/sh - -export CC=riscv64-cartesi-linux-gnu-gcc -export CXX=riscv64-cartesi-linux-gnu-g++ -export RISCV_ARCH=rv64gc -export RISCV_ABI=lp64d -export CFLAGS="-march=$RISCV_ARCH -mabi=$RISCV_ABI" diff --git a/rollup-http/rollup-http-server/build.sh b/rollup-http/rollup-http-server/build-with-toolchain.sh similarity index 87% rename from rollup-http/rollup-http-server/build.sh rename to rollup-http/rollup-http-server/build-with-toolchain.sh index 5f7cbe65..4308d8f9 100755 --- a/rollup-http/rollup-http-server/build.sh +++ b/rollup-http/rollup-http-server/build-with-toolchain.sh @@ -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 diff --git a/rollup-http/rollup-http-server/environment.sh b/rollup-http/rollup-http-server/environment.sh deleted file mode 100644 index eaee9fed..00000000 --- a/rollup-http/rollup-http-server/environment.sh +++ /dev/null @@ -1,7 +0,0 @@ -#! /bin/sh - -export CC=riscv64-cartesi-linux-gnu-gcc -export CXX=riscv64-cartesi-linux-gnu-g++ -export RISCV_ARCH=rv64gc -export RISCV_ABI=lp64d -export CFLAGS="-march=$RISCV_ARCH -mabi=$RISCV_ABI"