|
| 1 | +# SPDX-FileCopyrightText: 2025 The rsinit Authors |
| 2 | +# SPDX-License-Identifier: GPL-2.0-only |
| 3 | + |
| 4 | +CARGO_RUNNER ?= cross |
| 5 | +CROSS_CONTAINER_ENGINE = podman |
| 6 | +MINIMAL_BUILD ?= 0 |
| 7 | + |
| 8 | +ifneq ($(MINIMAL_BUILD),0) |
| 9 | + TARGET_PROFILE := minimal |
| 10 | + RUSTFLAGS := -Zfmt-debug=none -Zlocation-detail=none |
| 11 | + CARGO_FLAGS := -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort |
| 12 | +else |
| 13 | + TARGET_PROFILE ?= release |
| 14 | + RUSTFLAGS ?= |
| 15 | + CARGO_FLAGS ?= |
| 16 | +endif |
| 17 | + |
| 18 | +default-target-list := aarch64-unknown-linux-musl arm-unknown-linux-musleabihf x86_64-unknown-linux-musl |
| 19 | + |
| 20 | +all: check-toolchain build cpio |
| 21 | +build: $(addsuffix -build,$(default-target-list)) |
| 22 | +cpio: $(addsuffix -cpio,$(default-target-list)) |
| 23 | + |
| 24 | +help: |
| 25 | + @echo "rsinit cross compilation Makefile" |
| 26 | + @echo "" |
| 27 | + @echo "Available targets:" |
| 28 | + @echo " help - Show this help message" |
| 29 | + @echo " check-toolchain - Verify all required CLI tools are installed" |
| 30 | + @echo " all - Build and create CPIO archives for all default targets" |
| 31 | + @echo " build - Build binaries for all default targets" |
| 32 | + @echo " cpio - Create CPIO archives for all default targets" |
| 33 | + @echo " <target>-build - Build for a specific target (e.g., aarch64-unknown-linux-musl-build)" |
| 34 | + @echo " <target>-cpio - Create CPIO archive for a specific target" |
| 35 | + @echo "" |
| 36 | + @echo "Default targets: $(default-target-list)" |
| 37 | + @echo "" |
| 38 | + @echo "Configuration variables:" |
| 39 | + @echo " CARGO_RUNNER - Cargo wrapper for cross-compilation (default: $(CARGO_RUNNER))" |
| 40 | + @echo " CROSS_CONTAINER_ENGINE - Container engine for cross (default: $(CROSS_CONTAINER_ENGINE))" |
| 41 | + @echo " MINIMAL_BUILD - Enable minimal build profile (default: $(MINIMAL_BUILD))" |
| 42 | + @echo " TARGET_PROFILE - Build profile to use (current: $(TARGET_PROFILE))" |
| 43 | + @echo "" |
| 44 | + @echo "Examples:" |
| 45 | + @echo " make check-toolchain - Verify dependencies" |
| 46 | + @echo " make all - Build everything" |
| 47 | + @echo " make aarch64-unknown-linux-musl-build - Build for aarch64 only" |
| 48 | + @echo " make MINIMAL_BUILD=1 all - Build with minimal profile" |
| 49 | + @echo " make CROSS_CONTAINER_ENGINE=docker all - Use Docker instead of Podman" |
| 50 | + |
| 51 | +check-toolchain: |
| 52 | + @echo "Checking for required CLI tools..." |
| 53 | + @command -v $(CARGO_RUNNER) >/dev/null 2>&1 || { echo "Error: $(CARGO_RUNNER) is not installed. Please install it to continue."; exit 1; } |
| 54 | + @command -v rustup >/dev/null 2>&1 || { echo "Error: rustup is not installed. Please install it from https://rustup.rs/"; exit 1; } |
| 55 | + @command -v find >/dev/null 2>&1 || { echo "Error: find is not installed."; exit 1; } |
| 56 | + @command -v cpio >/dev/null 2>&1 || { echo "Error: cpio is not installed. Please install it using your package manager."; exit 1; } |
| 57 | + @command -v gzip >/dev/null 2>&1 || { echo "Error: gzip is not installed."; exit 1; } |
| 58 | + @command -v $(CROSS_CONTAINER_ENGINE) >/dev/null 2>&1 || { echo "Error: $(CROSS_CONTAINER_ENGINE) is not installed. Please install podman or set CROSS_CONTAINER_ENGINE to docker."; exit 1; } |
| 59 | + @rustup toolchain list | grep -q nightly || { echo "Error: nightly toolchain is not installed. Run: rustup toolchain install nightly"; exit 1; } |
| 60 | + @echo "All required tools are available!" |
| 61 | + |
| 62 | +%-build: check-toolchain |
| 63 | + CROSS_CONTAINER_ENGINE=$(CROSS_CONTAINER_ENGINE) RUSTFLAGS="$(RUSTFLAGS)" $(CARGO_RUNNER) +nightly build --target $* $(if $(TARGET_PROFILE),--profile $(TARGET_PROFILE)) $(CARGO_FLAGS) |
| 64 | + |
| 65 | +%-cpio: T=target/$*/$(if $(TARGET_PROFILE),$(TARGET_PROFILE),debug) |
| 66 | +%-cpio: %-build |
| 67 | + cd $T && find init | cpio --create --format=newc > init-$*.cpio |
| 68 | + cd $T && gzip --keep --best --force init-$*.cpio |
| 69 | + |
| 70 | +clean: |
| 71 | + cargo clean |
| 72 | + |
| 73 | +.PHONY: all build clean cpio check-toolchain help |
0 commit comments