Skip to content

Commit 607dfad

Browse files
Merge pull request #19 from KarlK90/feature/cpio-build-target
Add cross compilation and cpio archive targets
2 parents 046d9c4 + 1bb00d9 commit 607dfad

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

Makefile

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,59 @@ Currently supported root filesystems are:
2525
mount options
2626
* nfsroot
2727
* 9pfs with virtio transport (for QEMU)
28+
29+
Cross compilation with cross.rs
30+
-------------------------------
31+
32+
The project includes a Makefile for easy cross-compilation.
33+
34+
### Prerequisites
35+
36+
Before building, ensure you have the following tools installed:
37+
38+
- [`cross`](https://github.com/cross-rs/cross) - Cross-compilation tool for Rust
39+
- [`rustup`](https://rustup.rs/) - Rust toolchain manager
40+
- [`podman`](https://podman.io/) or [`docker`](https://www.docker.com/) - Container runtime for cross
41+
- `cpio` - For creating cpio/initramfs archives
42+
- `gzip` - For compressing cpio/initramfs archives
43+
- A `nightly` Rust toolchain
44+
45+
You can verify all dependencies are installed by running:
46+
47+
```bash
48+
make check-toolchain
49+
```
50+
51+
### Usage
52+
53+
To see all available make targets and options:
54+
55+
```bash
56+
make help
57+
```
58+
59+
Common build commands:
60+
61+
```bash
62+
# Build binaries and CPIO archives for all default targets
63+
make all
64+
65+
# Build for a specific target only
66+
make aarch64-unknown-linux-musl-build
67+
68+
# Create CPIO archive for a specific target
69+
make aarch64-unknown-linux-musl-cpio
70+
71+
# Build with minimal profile (smaller binary size)
72+
make MINIMAL_BUILD=1 all
73+
74+
# Use Docker instead of Podman
75+
make CROSS_CONTAINER_ENGINE=docker all
76+
```
77+
78+
Default target architectures:
79+
- `aarch64-unknown-linux-musl`
80+
- `arm-unknown-linux-musleabihf`
81+
- `x86_64-unknown-linux-musl`
82+
83+
Build artifacts are placed in `target/<arch>/<profile>/` directories.

0 commit comments

Comments
 (0)