Skip to content

Commit

Permalink
refactor: cross-compile sys-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vfusco committed Dec 7, 2023
1 parent 855f1ca commit ed68b5f
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 140 deletions.
33 changes: 1 addition & 32 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Build
on: [push]
env:
TOOLCHAIN_IMAGE: ${{ github.repository_owner }}/toolchain
TOOLCHAIN_TAG: 0.16.0
jobs:
tools:
runs-on: ubuntu-latest-8-cores
Expand Down Expand Up @@ -100,41 +97,13 @@ jobs:
${{ env.TOOLS_ROOTFS }}
${{ env.TOOLS_ROOTFS }}.sha512
build:
test-rollup-http-server:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build yield tool
run: cd sys-utils/yield && make toolchain-exec CONTAINER_COMMAND="/usr/bin/make yield.toolchain"

- name: Build ioctl-echo-loop tool
run: cd sys-utils/ioctl-echo-loop && make toolchain-exec CONTAINER_COMMAND="/usr/bin/make ioctl-echo-loop.toolchain"

- name: Build rollup tool
run: cd sys-utils/rollup && make toolchain-exec CONTAINER_COMMAND="/usr/bin/make rollup.toolchain"

- name: Build rollup http server
run: cd rollup-http/rollup-http-server && ./build.sh

- name: Build echo dapp
run: cd rollup-http/echo-dapp && ./build.sh

- name: Test rollup-http-server and echo-dapp client
run: |
cd rollup-http/rollup-http-server
Expand Down
84 changes: 33 additions & 51 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,59 @@
# limitations under the License.
#

FROM --platform=linux/riscv64 riscv64/ubuntu:22.04 as tools-env
FROM ubuntu:22.04 as tools-env
ARG IMAGE_KERNEL_VERSION=v0.19.1
ARG LINUX_VERSION=6.5.9-ctsi-1
ARG LINUX_HEADERS_URLPATH=https://github.com/cartesi/image-kernel/releases/download/${IMAGE_KERNEL_VERSION}/linux-libc-dev-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb
ARG LINUX_HEADERS_URLPATH=https://github.com/cartesi/image-kernel/releases/download/${IMAGE_KERNEL_VERSION}/linux-libc-dev-riscv64-cross-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb
ARG BUILD_BASE=/opt/cartesi

# Install dependencies
# ------------------------------------------------------------------------------
ENV LINUX_HEADERS_FILEPATH=/tmp/linux-libc-dev-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb
ENV LINUX_HEADERS_FILEPATH=/tmp/linux-libc-dev-riscv64-cross-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb

RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
apt-get upgrade -y && \
apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
git \
rsync \
wget \
crossbuild-essential-riscv64 \
gcc-12-riscv64-linux-gnu \
g++-12-riscv64-linux-gnu \
&& \
wget -O ${LINUX_HEADERS_FILEPATH} ${LINUX_HEADERS_URLPATH} && \
echo "aace918b830ee2d682c329b3361a3458ebc42ba5193ccb821fd0b2071ac821b3 ${LINUX_HEADERS_FILEPATH}" | sha256sum --check && \
echo "efdb2243d9b6828e90c826be0f178110f0cc590cb00e8fa588cb20723126c2a4 ${LINUX_HEADERS_FILEPATH}" | sha256sum --check && \
apt-get install -y --no-install-recommends ${LINUX_HEADERS_FILEPATH} && \
mkdir -p ${BUILD_BASE} && \
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}

#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"
ENV TOOLCHAIN_PREFIX="riscv64-linux-gnu-"
ENV TOOLCHAIN_SUFFIX="-12"

FROM tools-env as builder
COPY sys-utils/ ${BUILD_BASE}/tools/sys-utils/
COPY --chown=developer:developer sys-utils/ ${BUILD_BASE}/tools/sys-utils/

# build C/C++ tools
# ------------------------------------------------------------------------------
FROM builder as c-builder
RUN make -C ${BUILD_BASE}/tools/sys-utils/xhalt/ CROSS_COMPILE="" xhalt.toolchain
RUN make -C ${BUILD_BASE}/tools/sys-utils/yield/ CROSS_COMPILE="" yield.toolchain
RUN make -C ${BUILD_BASE}/tools/sys-utils/ioctl-echo-loop/ CROSS_COMPILE="" ioctl-echo-loop.toolchain
RUN make -C ${BUILD_BASE}/tools/sys-utils/rollup/ CROSS_COMPILE="" rollup.toolchain
USER developer
RUN make -C ${BUILD_BASE}/tools/sys-utils/ -j$(nproc) all

# build rust tools
# ------------------------------------------------------------------------------
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

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}
FROM tools-env as rust-env
ENV PATH="/home/developer/.cargo/bin:${PATH}"

USER developer
RUN \
cd && \

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 && \
Expand All @@ -88,14 +77,7 @@ RUN \
--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"

FROM rust-env as rust-builder
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
Expand Down Expand Up @@ -131,24 +113,24 @@ RUN cd ${BUILD_BASE}/tools/rollup-http/echo-dapp && \

# pack tools (deb)
# ------------------------------------------------------------------------------
FROM c-builder as packer
FROM tools-env as packer
ARG TOOLS_DEB=machine-emulator-tools.deb
ARG STAGING_BASE=${BUILD_BASE}/_install
ARG STAGING_DEBIAN=${STAGING_BASE}/DEBIAN
ARG STAGING_SBIN=${STAGING_BASE}/usr/sbin
ARG STAGING_BIN=${STAGING_BASE}/usr/bin

RUN mkdir -p ${STAGING_DEBIAN} ${STAGING_SBIN} ${STAGING_BIN} ${STAGING_BASE}/etc && \
echo "cartesi-machine" > ${staging_base}/etc/hostname && \
cp ${BUILD_BASE}/tools/sys-utils/system-init/system-init ${STAGING_SBIN} && \
cp ${BUILD_BASE}/tools/sys-utils/xhalt/xhalt ${STAGING_SBIN} && \
cp ${BUILD_BASE}/tools/sys-utils/yield/yield ${STAGING_SBIN} && \
cp ${BUILD_BASE}/tools/sys-utils/rollup/rollup ${STAGING_SBIN} && \
cp ${BUILD_BASE}/tools/sys-utils/ioctl-echo-loop/ioctl-echo-loop ${STAGING_BIN} && \
cp ${BUILD_BASE}/tools/sys-utils/misc/* ${STAGING_BIN}
echo "cartesi-machine" > ${staging_base}/etc/hostname

COPY control ${STAGING_DEBIAN}/control

COPY --from=builder ${BUILD_BASE}/tools/sys-utils/cartesi-init/cartesi-init ${STAGING_SBIN}
COPY --from=c-builder ${BUILD_BASE}/tools/sys-utils/xhalt/xhalt ${STAGING_SBIN}
COPY --from=c-builder ${BUILD_BASE}/tools/sys-utils/yield/yield ${STAGING_SBIN}
COPY --from=c-builder ${BUILD_BASE}/tools/sys-utils/rollup/rollup ${STAGING_SBIN}
COPY --from=c-builder ${BUILD_BASE}/tools/sys-utils/ioctl-echo-loop/ioctl-echo-loop ${STAGING_BIN}
COPY --from=c-builder ${BUILD_BASE}/tools/sys-utils/misc/* ${STAGING_BIN}
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/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}
Expand Down
43 changes: 30 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ TOOLS_ROOTFS := rootfs-tools-v$(VERSION).ext2

IMAGE_KERNEL_VERSION ?= v0.19.1
LINUX_VERSION ?= 6.5.9-ctsi-1
LINUX_HEADERS_URLPATH := https://github.com/cartesi/image-kernel/releases/download/${IMAGE_KERNEL_VERSION}/linux-libc-dev-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb
LINUX_HEADERS_URLPATH := https://github.com/cartesi/image-kernel/releases/download/${IMAGE_KERNEL_VERSION}/linux-libc-dev-riscv64-cross-${LINUX_VERSION}-${IMAGE_KERNEL_VERSION}.deb

all: fs

build: control
@docker buildx build --platform=linux/riscv64 --load \
@docker buildx build --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)` && \
docker cp $$ID:/opt/cartesi/$(TOOLS_DEB) . && \
@ID=`docker create $(TOOLS_IMAGE)` && \
docker cp $$ID:/opt/cartesi/$(TOOLS_DEB) . && \
docker rm $$ID

$(TOOLS_DEB) deb: build
Expand Down Expand Up @@ -76,9 +76,23 @@ setup:
setup-required:
@echo 'riscv64 buildx setup required:' `docker buildx ls | grep -q riscv64 && echo no || echo yes`

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

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

tools-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)-tools-env /bin/bash


rust-builder-env:
@docker run --hostname rust-builder -it --rm \
-e USER=$$(id -u -n) \
Expand All @@ -89,6 +103,17 @@ rust-builder-env:
-w /opt/cartesi/machine-emulator-tools \
$(TOOLS_IMAGE)-rust-builder /bin/bash

clean-image:
@(docker rmi $(TOOLS_IMAGE) > /dev/null 2>&1 || true)

clean:
@rm -f $(TOOLS_DEB) control rootfs*
@$(MAKE) -C sys-utils clean

distclean: clean clean-image

sys-utils:
@$(MAKE) -C sys-utils

help:
@echo 'available commands:'
Expand All @@ -100,12 +125,4 @@ help:
@echo ' env - print useful Makefile variables as a KEY=VALUE list'
@echo ' clean - remove the generated artifacts'

clean-image:
@(docker rmi $(TOOLS_IMAGE) > /dev/null 2>&1 || true)

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

distclean: clean clean-image

.PHONY: build fs deb env setup setup-required help distclean
36 changes: 36 additions & 0 deletions sys-utils/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


UTILITIES := xhalt yield rollup ioctl-echo-loop
UTILITIES_WITH_TOOLCHAIN := $(addsuffix -with-toolchain,$(UTILITIES))

all: $(UTILITIES)

$(UTILITIES):
@$(MAKE) -C $@

with-toolchain:
@$(MAKE) $(UTILITIES_WITH_TOOLCHAIN)

$(UTILITIES_WITH_TOOLCHAIN):
@$(MAKE) -C $@ with-toolchain

clean:
@$(foreach utility,$(UTILITIES),$(MAKE) -C $(utility) clean;)


.PHONY: all with-toolchain $(UTILITIES) $(UTILITIES_WITH_TOOLCHAIN)
24 changes: 13 additions & 11 deletions sys-utils/ioctl-echo-loop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ TOOLCHAIN_TAG ?= 0.16.0
RISCV_ARCH ?= rv64gc
RISCV_ABI ?= lp64d

CROSS_COMPILE = riscv64-cartesi-linux-gnu-
RVCC = $(CROSS_COMPILE)gcc
RVCXX = $(CROSS_COMPILE)g++
RVCOPY = $(CROSS_COMPILE)objcopy
RVDUMP = $(CROSS_COMPILE)objdump
STRIP = $(CROSS_COMPILE)strip
TOOLCHAIN_PREFIX ?= riscv64-cartesi-linux-gnu-
TOOLCHAIN_SUFFIX ?=

RVCC = $(TOOLCHAIN_PREFIX)gcc$(TOOLCHAIN_SUFFIX)
RVCXX = $(TOOLCHAIN_PREFIX)g++$(TOOLCHAIN_SUFFIX)
RVCOPY = $(TOOLCHAIN_PREFIX)objcopy$(TOOLCHAIN_SUFFIX)
RVDUMP = $(TOOLCHAIN_PREFIX)objdump$(TOOLCHAIN_SUFFIX)
STRIP = $(TOOLCHAIN_PREFIX)strip
CFLAGS :=-Wall -pedantic -O2 -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI)

CONTAINER_MAKE := /usr/bin/make
Expand All @@ -35,17 +37,17 @@ KERNEL_HEADERS_PATH := /opt/riscv/usr/include

all: ioctl-echo-loop

ioctl-echo-loop: ioctl-echo-loop.c
ioctl-echo-loop.with-toolchain with-toolchain:
$(MAKE) toolchain-exec CONTAINER_COMMAND="$(CONTAINER_MAKE) $@.toolchain"

extra.ext2: ioctl-echo-loop
extra.ext2.with-toolchain:
$(MAKE) toolchain-exec CONTAINER_COMMAND="$(CONTAINER_MAKE) $@.toolchain"

ioctl-echo-loop.toolchain:
ioctl-echo-loop: ioctl-echo-loop.c
$(RVCC) $(CFLAGS) -o ioctl-echo-loop ioctl-echo-loop.c
$(STRIP) ioctl-echo-loop

extra.ext2.toolchain:
extra.ext2: ioctl-echo-loop
mkdir -m 0755 ./extra
cp ./ioctl-echo-loop ./extra/ioctl-echo-loop
xgenext2fs -i 512 -b 8192 -d extra $(basename $@)
Expand All @@ -72,6 +74,6 @@ toolchain-env:
$(TOOLCHAIN_IMAGE):$(TOOLCHAIN_TAG)

clean:
\rm -rf ioctl-echo-loop extra.ext2 extra
@rm -rf ioctl-echo-loop extra.ext2 extra

.PHONY: toolchain-exec toolchain-env
Loading

0 comments on commit ed68b5f

Please sign in to comment.