From 40b1dce975114c3b310ac76bfb1c5b4edc8d3328 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Sun, 19 Apr 2026 15:48:52 +0700 Subject: [PATCH] Add compiling and testing with Makefile --- .gitignore | 1 + .gitlab-ci.yml | 10 ++-- Cargo.lock | 32 +++++------ Makefile | 129 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 10 +++- check.sh | 6 +-- netstack/Cargo.toml | 2 +- 7 files changed, 164 insertions(+), 26 deletions(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index d1adf4b7b0..a5456eb414 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ +sysroot/ # Local settings folder for Visual Studio Code .vscode/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ecb2229972..45848a7f64 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,8 +7,7 @@ workflow: stages: - build - cross-build - # TODO? - # - test + - test fmt: stage: build @@ -16,8 +15,6 @@ fmt: - rustup component add rustfmt - CHECK_ONLY=1 ./fmt.sh -# TODO: unit tests - x86_64: stage: build script: @@ -38,3 +35,8 @@ riscv64gc: stage: cross-build script: - ./check.sh --arch=riscv64gc + +boot: + stage: test + script: + - timeout -s KILL 9m ./check.sh --test diff --git a/Cargo.lock b/Cargo.lock index f90cb5dba8..7a0062f898 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1311,6 +1311,22 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" +[[package]] +name = "netstack" +version = "0.1.0" +dependencies = [ + "anyhow", + "daemon", + "libredox", + "log", + "redox-log", + "redox-scheme", + "redox_event", + "redox_syscall 0.7.4", + "scheme-utils", + "smoltcp", +] + [[package]] name = "nom" version = "3.2.1" @@ -1841,22 +1857,6 @@ dependencies = [ "libredox", ] -[[package]] -name = "redox_netstack" -version = "0.1.0" -dependencies = [ - "anyhow", - "daemon", - "libredox", - "log", - "redox-log", - "redox-scheme", - "redox_event", - "redox_syscall 0.7.4", - "scheme-utils", - "smoltcp", -] - [[package]] name = "redox_syscall" version = "0.5.18" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..3a5f7a57e1 --- /dev/null +++ b/Makefile @@ -0,0 +1,129 @@ +TARGET ?= x86_64-unknown-redox +LINKER ?= $(shell redoxer env which $(shell redoxer env printenv LD)) +BOARD ?= +BUILD_TYPE ?= release +BUILD_FLAGS ?= --release +CARGO ?= redoxer +CARGO_HOST ?= env -u CARGO -u RUSTFLAGS cargo + +SRC_DIR ?= $(CURDIR) +BUILD_DIR ?= $(shell pwd) +DESTDIR ?= ./sysroot +SYSROOT ?= $(BUILD_DIR)/target/$(TARGET)/sysroot +TARGET_DIR = $(BUILD_DIR)/target/$(TARGET)/$(BUILD_TYPE) +BUILD_FLAGS += --target-dir $(BUILD_DIR)/target + +INITFS_BINS = init logd ramfs randd zerod \ + acpid fbbootlogd fbcond hwd inputd lived \ + pcid pcid-spawner rtcd vesad +INITFS_DRIVERS_BINS = nvmed virtio-blkd virtio-gpud +BASE_BINS = inputd pcid pcid-spawner redoxerd audiod dhcpd ipcd ptyd netstack +DRIVERS_BINS = e1000d ihdad ihdgd ixgbed rtl8139d rtl8168d \ + usbctl usbhidd usbhubd usbscsid virtio-netd xhcid + +ifneq (,$(filter i586-unknown-redox i686-unknown-redox x86_64-unknown-redox,$(TARGET))) + INITFS_BINS += ps2d + INITFS_DRIVERS_BINS += ahcid ided + DRIVERS_BINS += ac97d sb16d vboxd +endif + +ifeq ($(TARGET),aarch64-unknown-redox) + ifeq ($(BOARD),raspi3b) + INITFS_BINS += bcm2835-sdhcid + endif +endif + +INITFS_CARGO_ARGS = $(foreach bin,$(INITFS_BINS),-p $(bin)) +INITFS_DRIVERS_CARGO_ARGS = $(foreach bin,$(INITFS_DRIVERS_BINS),-p $(bin)) +BASE_CARGO_ARGS = $(foreach bin,$(BASE_BINS),-p $(bin)) +DRIVERS_CARGO_ARGS = $(foreach bin,$(DRIVERS_BINS),-p $(bin)) + +.PHONY: all initfs base install install-initfs install-base test + +all: initfs base +install: install-initfs install-base +initfs: $(TARGET_DIR)/initfs.img +base: $(TARGET_DIR)/bin.tag + +clean: + rm -rf $(SRC_DIR)/target $(SRC_DIR)/sysroot $(SYSROOT) $(TARGET_DIR) + +# test if booting +test: all + $(MAKE) install + REDOXER_QEMU_SMP=1 redoxer exec --folder ./sysroot/:/ true + +# test with interactive gui +test-gui: all + $(MAKE) install + REDOXER_QEMU_SMP=1 redoxer exec --gui --folder ./sysroot/:/ ion + +# ----------------------------------------------------------------------------- +# base-initfs +# ----------------------------------------------------------------------------- +$(SYSROOT)/bin/redoxfs: + redoxer pkg redoxfs + +$(TARGET_DIR)/initfs: $(SYSROOT)/bin/redoxfs + rm -rf "$@" "$@.partial" +# Copy config files + mkdir -p "$@.partial/lib/init.d" "$@.partial/lib/pcid.d" + cp "$(SRC_DIR)/init.initfs.d"/* "$@.partial/lib/init.d/" + cp "$(SRC_DIR)/drivers/initfs.toml" "$@.partial/lib/pcid.d/initfs.toml" +# Build daemons and drivers + CARGO_PROFILE_RELEASE_OPT_LEVEL=s CARGO_PROFILE_RELEASE_PANIC=abort \ + $(CARGO) build $(BUILD_FLAGS) \ + --manifest-path "$(SRC_DIR)/Cargo.toml" \ + $(INITFS_CARGO_ARGS) $(INITFS_DRIVERS_CARGO_ARGS) +# Distribute binaries + mkdir -pv "$@.partial/bin" "$@.partial/lib/drivers" + for bin in $(INITFS_BINS); do \ + cp -v "$(TARGET_DIR)/$$bin" "$@.partial/bin"; \ + done + for bin in $(INITFS_DRIVERS_BINS); do \ + cp -v "$(TARGET_DIR)/$$bin" "$@.partial/lib/drivers"; \ + done + cp "$(SYSROOT)/bin/redoxfs" "$@.partial/bin" + mv "$@.partial" "$@" + +$(TARGET_DIR)/bootstrap: + cd "$(SRC_DIR)/bootstrap" && $(CARGO) rustc $(BUILD_FLAGS) \ + -- -Ctarget-feature=+crt-static -Clinker="$(LINKER)" + +$(TARGET_DIR)/initfs.img: $(TARGET_DIR)/initfs $(TARGET_DIR)/bootstrap + $(CARGO_HOST) run --manifest-path "$(SRC_DIR)/initfs/tools/Cargo.toml" --bin redox-initfs-ar -- \ + "$(TARGET_DIR)/initfs" "$(TARGET_DIR)/bootstrap" -o "$@" + +install-initfs: $(TARGET_DIR)/initfs.img + @mkdir -pv "$(DESTDIR)/usr/lib/boot" + @cp -v "$<" "$(DESTDIR)/usr/lib/boot/initfs" + +# ----------------------------------------------------------------------------- +# base +# ----------------------------------------------------------------------------- +$(TARGET_DIR)/bin.tag: +# Build daemons and drivers + CARGO_PROFILE_RELEASE_OPT_LEVEL=s CARGO_PROFILE_RELEASE_PANIC=abort \ + $(CARGO) build $(BUILD_FLAGS) \ + --manifest-path "$(SRC_DIR)/Cargo.toml" \ + $(BASE_CARGO_ARGS) $(DRIVERS_CARGO_ARGS) + mv $(TARGET_DIR)/smolnetd $(TARGET_DIR)/netstack + touch "$@" + +install-base: $(TARGET_DIR)/bin.tag + @mkdir -pv "$(DESTDIR)/usr/bin" "$(DESTDIR)/usr/lib/drivers" + @mkdir -pv "$(DESTDIR)/usr/lib/init.d/" "$(DESTDIR)/usr/lib/pcid.d" +# Distribute binaries + @for bin in $(BASE_BINS); do \ + cp -v "$(TARGET_DIR)/$$bin" "$(DESTDIR)/usr/bin"; \ + done + @for bin in $(DRIVERS_BINS); do \ + cp -v "$(TARGET_DIR)/$$bin" "$(DESTDIR)/usr/lib/drivers"; \ + done + @mv -v $(DESTDIR)/usr/bin/netstack $(DESTDIR)/usr/bin/smolnetd +# Copy configurations + @cp -v "$(SRC_DIR)/init.d"/* "$(DESTDIR)/usr/lib/init.d/" + @find "$(SRC_DIR)/drivers" -maxdepth 3 -type f -name 'config.toml' | while read -r conf; do \ + driver=$$(basename "$$(dirname "$$conf")"); \ + cp -v "$$conf" "$(DESTDIR)/usr/lib/pcid.d/$$driver.toml"; \ + done diff --git a/README.md b/README.md index 0cdd324c7b..7f0f0e1aa3 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,12 @@ To learn how to do development with these system components inside the Redox bui ### How To Build -To build this system component you need to download the Redox build system, you can learn how to do it on the [Building Redox](https://doc.redox-os.org/book/podman-build.html) page. +It is recommended to build this system component via the Redox build system, you can learn how to do it on the [Building Redox](https://doc.redox-os.org/book/podman-build.html) page. -This is necessary because they only work with cross-compilation to a Redox virtual machine or real hardware, but you can do some testing from Linux. +To build and test outside the build system, [install redoxer](https://doc.redox-os.org/book/ci.html) then use `check.sh` script to build or test: +- `./check.sh` - Check build for x86_64 +- `./check.sh --arch=ARCH` - Check build for specific ARCH (`aarch64`, `i586`, `riscv64gc`) +- `./check.sh --all` - Check build for all ARCH +- `./check.sh --test` - Check the base system boots up on x86_64 + +You can also use `make install` to inspect the content on `./sysroot`, or `make test-gui` to test booting with orbital interactively. diff --git a/check.sh b/check.sh index 674097b507..61ce212bb7 100755 --- a/check.sh +++ b/check.sh @@ -41,7 +41,7 @@ SUPPORTED_TARGETS=( CURRENT_TARGET="${TARGET:-x86_64-unknown-redox}" CHECK_ALL=false -CMD_ACTION="check" +CMD_ACTION="all" while [[ $# -gt 0 ]]; do case "$1" in --all-target) @@ -74,9 +74,9 @@ run_redoxer() { redoxer toolchain || { echo -e "${RED}Fail: redoxer toolchain for: $target.${NC}" && exit 1; } echo "----------------------------------------" - echo "Running redoxer $CMD_ACTION for: $TARGET" + echo "Running make $CMD_ACTION for: $TARGET" - if redoxer "$CMD_ACTION"; then + if make "$CMD_ACTION"; then return 0 else echo -e "${RED}Fail: $CMD_ACTION $TARGET failed.${NC}" diff --git a/netstack/Cargo.toml b/netstack/Cargo.toml index 589eddc315..28fee44ba4 100644 --- a/netstack/Cargo.toml +++ b/netstack/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "redox_netstack" +name = "netstack" description = "Network stack daemons" version = "0.1.0" edition = "2021"