|
| 1 | +# List of packages to compile for any architecture |
| 2 | +PACKAGES_ANYARCH=\ |
| 3 | +cartesi-machine-linux-image \ |
| 4 | +cartesi-machine-rootfs-image \ |
| 5 | +cartesi-machine-emulator \ |
| 6 | +xgenext2fs |
| 7 | + |
| 8 | +# List of packages to compile for riscv64 |
| 9 | +PACKAGES_RISCV64=\ |
| 10 | +cartesi-machine-guest-linux-headers \ |
| 11 | +cartesi-machine-guest-tools |
| 12 | + |
| 13 | +# Target architecture to compile packages |
| 14 | +TARGET_ARCH?=$(shell uname -m) |
| 15 | + |
| 16 | +# Docker base image used for building packages |
| 17 | +BASE_IMAGE=alpine:3.21 |
| 18 | +IMAGE=cartesi/apk-builder-$(TARGET_ARCH) |
| 19 | + |
| 20 | +# Repository path to save built packages |
| 21 | +REPO_PATH=$(abspath ../cdn)/apk |
| 22 | +REPO_NAME=stable |
| 23 | +KEY_NAME=cartesi-apk-key |
| 24 | + |
| 25 | +# Package list to build |
| 26 | +PACKAGES=$(PACKAGES_ANYARCH) |
| 27 | +ifeq ($(TARGET_ARCH),riscv64) |
| 28 | +PACKAGES+=$(PACKAGES_RISCV64) |
| 29 | +endif |
| 30 | + |
| 31 | +all: ## Generate a key (if needed) and build all packages |
| 32 | + @$(MAKE) --no-print-directory image |
| 33 | + @$(MAKE) --no-print-directory key |
| 34 | + @$(MAKE) --no-print-directory packages-all |
| 35 | + |
| 36 | +packages-all: ## Build packages for all architectures (x86_64/aarch64/riscv64) |
| 37 | + @$(MAKE) --no-print-directory image TARGET_ARCH=x86_64 |
| 38 | + @$(MAKE) --no-print-directory packages TARGET_ARCH=x86_64 |
| 39 | + @$(MAKE) --no-print-directory image TARGET_ARCH=aarch64 |
| 40 | + @$(MAKE) --no-print-directory packages TARGET_ARCH=aarch64 |
| 41 | + @$(MAKE) --no-print-directory image TARGET_ARCH=riscv64 |
| 42 | + @$(MAKE) --no-print-directory packages TARGET_ARCH=riscv64 |
| 43 | + |
| 44 | +packages: $(patsubst %,%.apk,$(PACKAGES)) ## Build packages for given TARGET_ARCH |
| 45 | + |
| 46 | +%.apk: ## Build a package for given TARGET_ARCH |
| 47 | + @$(MAKE) --no-print-directory exec COMMAND="\ |
| 48 | + cd $* && \ |
| 49 | + export SOURCE_DATE_EPOCH=\\\`stat -c %Y APKBUILD\\\` && \ |
| 50 | + abuild -rF && \ |
| 51 | + chown -R $(shell id -u):$(shell id -g) /root/packages/work" |
| 52 | + |
| 53 | +key: ## Generate package signature key |
| 54 | + echo "NOTICE: Generating new key!" |
| 55 | + @mkdir -p $(REPO_PATH)/keys key |
| 56 | + docker run --platform=linux/$(TARGET_ARCH) \ |
| 57 | + --volume ./key:/root/.abuild \ |
| 58 | + --volume $(REPO_PATH):/apk \ |
| 59 | + --rm $(IMAGE) \ |
| 60 | + ash -c "\ |
| 61 | + abuild-keygen -n && \ |
| 62 | + mv /root/.abuild/*.rsa.pub /root/.abuild/$(KEY_NAME).rsa.pub && \ |
| 63 | + mv /root/.abuild/*.rsa /root/.abuild/$(KEY_NAME).rsa && \ |
| 64 | + cp /root/.abuild/$(KEY_NAME).rsa.pub /apk/keys/$(KEY_NAME).rsa.pub && \ |
| 65 | + chown -R $(shell id -u):$(shell id -g) /root/.abuild /apk/keys" |
| 66 | + |
| 67 | +shell: ## Spawn an image shell for given TARGET_ARCH |
| 68 | + @$(MAKE) --no-print-directory exec DOCKER_FLAGS="-it" COMMAND="ash" |
| 69 | + |
| 70 | +exec: ## Execute a COMMAND inside an image for given TARGET_ARCH |
| 71 | + docker run --platform=linux/$(TARGET_ARCH) \ |
| 72 | + --volume ./key:/key \ |
| 73 | + --volume $(REPO_PATH)/$(REPO_NAME):/root/packages/work \ |
| 74 | + --volume .:/work \ |
| 75 | + --workdir /work \ |
| 76 | + $(DOCKER_FLAGS) --rm $(IMAGE) \ |
| 77 | + ash -c "\ |
| 78 | + cp /key/*.rsa.pub /etc/apk/keys/ && \ |
| 79 | + cp -a /key /root/.abuild && \ |
| 80 | + chown -R root:root /root/.abuild && \ |
| 81 | + $(COMMAND)" |
| 82 | + |
| 83 | +image: ## Build Docker image for building packages for given TARGET_ARCH |
| 84 | + docker build --platform=linux/$(TARGET_ARCH) \ |
| 85 | + --build-arg=BASE_IMAGE=$(BASE_IMAGE) \ |
| 86 | + --tag=$(IMAGE) \ |
| 87 | + --progress=plain \ |
| 88 | + --file Dockerfile . |
| 89 | + |
| 90 | +test: ## Test built packages for all architectures (x86_64/aarch64/riscv64) |
| 91 | + @$(MAKE) --no-print-directory test-packages TARGET_ARCH=x86_64 |
| 92 | + @$(MAKE) --no-print-directory test-packages TARGET_ARCH=aarch64 |
| 93 | + @$(MAKE) --no-print-directory test-packages TARGET_ARCH=riscv64 |
| 94 | + |
| 95 | +ifeq ($(TARGET_ARCH),riscv64) |
| 96 | +test-packages: |
| 97 | + @$(MAKE) --no-print-directory exec COMMAND="\ |
| 98 | + apk add $(PACKAGES) && \ |
| 99 | + rollup --help && \ |
| 100 | + cartesi-machine" |
| 101 | +else |
| 102 | +test-packages: ## Test built packages for given TARGET_ARCH |
| 103 | + @$(MAKE) --no-print-directory exec COMMAND="\ |
| 104 | + apk add $(PACKAGES) && \ |
| 105 | + cartesi-machine" |
| 106 | +endif |
| 107 | + |
| 108 | +distclean: ## Remove everything from APK repository directory |
| 109 | + rm -rf $(REPO_PATH)/$(REPO_NAME) |
| 110 | + |
| 111 | +help: ## Show this help |
| 112 | + @sed \ |
| 113 | + -e '/^[a-zA-Z0-9_\-]*:.*##/!d' \ |
| 114 | + -e 's/:.*##\s*/:/' \ |
| 115 | + -e 's/^\(.\+\):\(.*\)/$(shell tput setaf 6)\1$(shell tput sgr0):\2/' \ |
| 116 | + $(MAKEFILE_LIST) | column -c2 -t -s : |
| 117 | + |
| 118 | +.PHONY: all packages-all packages shell exec image test test-packages distclean help |
0 commit comments