From c7d40b828b2c861ea590f3f6271264215de64b17 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 14:45:59 +0200 Subject: [PATCH] per-config build output using VPATH based on https://make.mad-scientist.net/papers/multi-architecture-builds/ --- .github/workflows/rust.yml | 7 +-- .gitignore | 3 +- example/Makefile | 96 ++++++++++++++++++-------------------- example/target.mk | 52 +++++++++++++++++++++ 4 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 example/target.mk diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e9947d5..f4af981 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -36,9 +36,9 @@ jobs: run: output=$(git status --porcelain) && [ -z "$output" ] - name: build example -- glibc release - run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=release + run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=release - name: build example -- glibc debug - run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=debug + run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=debug build-size-gnu-unstable: runs-on: ubuntu-latest @@ -53,4 +53,5 @@ jobs: run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update - name: build example -- glibc size-optimized - run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=size-optimized CARGO="rustup run nightly cargo" LTO=1 + run: cd example && make clean + && make LIBC=gnu LINK=dynamic PROFILE=size-optimized CARGO="rustup run nightly cargo" LTO=1 diff --git a/.gitignore b/.gitignore index f48287d..6f84fcd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ out .direnv .envrc result -mutants.* \ No newline at end of file +mutants.* +_out_* \ No newline at end of file diff --git a/example/Makefile b/example/Makefile index c2e75bb..b238200 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,33 +1,48 @@ +# based on https://make.mad-scientist.net/papers/multi-architecture-builds/ + +_libc := $(if $(LIBC),$(LIBC),gnu) +ifeq (,$(filter gnu musl,$(_libc))) + _link_type := $(error "LIBC has to be set to one of: gnu, musl") +endif + +_link_type := $(if $(LINK),$(LINK),dynamic) +ifeq (,$(filter dynamic static,$(_link_type))) + _link_type := $(error "LINK has to be set to one of: dynamic, static") +endif + +_profile := $(if $(PROFILE),$(PROFILE),release) +ifeq (,$(filter release debug size-optimized,$(_profile))) + _profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized") +endif + +ARCH ?= $(shell uname -m) +RUST_TARGET := $(ARCH)-unknown-linux-$(_libc) + +#----- Begin Boilerplate +ifeq (,$(filter _out_%,$(notdir $(CURDIR)))) + include target.mk +else +#----- End Boilerplate + +VPATH = $(SRCDIR) + CARGO ?= cargo STRIP ?= strip CC ?= gcc -RUST_ARCH ?= x86_64 - FEATURES := ""#"env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) -export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include # TODO: check if override is needed -override _libc := $(if $(MUSL),musl,gnu) -override _profile := $(if $(PROFILE),$(PROFILE),release) -override _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) -override _link_type := $(if $(LINK),$(LINK),dynamic) +_rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) -# TODO: make LINK=static fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a - -_all_profiles := release debug size-optimized -ifeq (,$(filter $(_all_profiles),$(_profile))) - _profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized") -endif +# TODO: `make LINK=static` fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag STATIC_LINK_LIBS += -lservicepoint_binding_c -RUST_TARGET := $(RUST_ARCH)-unknown-linux-$(_libc) - size-optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ -Zbuild-std-features="panic_immediate_abort" CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ @@ -35,7 +50,8 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --no-default-features \ --features=$(FEATURES) \ --target=$(RUST_TARGET) \ - $($(_profile)_CARGOFLAGS) + $($(_profile)_CARGOFLAGS) \ + --target-dir=cargo CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections @@ -53,7 +69,7 @@ size-optimized_CFLAGS += -Oz \ -fno-exceptions \ $(_no_debug_cflags) release_CFLAGS += -O2 \ - $(_no_debug_cflags) + $(_no_debug_cflags) debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic @@ -70,16 +86,19 @@ ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) +CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) +_servicepoint_cflags := -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR) + +CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) ifeq ($(LTO), 1) CFLAGS += -flto endif -RUST_TARGET_DIR := $(REPO_ROOT)/target/$(RUST_TARGET)/$(_profile) - -_c_src := $(wildcard ./src/*.c) +# TODO: wildcard does not work with VPATH +_c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ + src/moving_line.c src/random_stuff.c src/wiping_clear.c _programs := $(basename $(notdir $(_c_src))) -_bins := $(addprefix out/, $(_programs)) +_bins := $(_programs) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) _run_programs := $(addprefix run_, $(_programs)) @@ -87,42 +106,19 @@ _run_programs := $(addprefix run_, $(_programs)) all: $(_bins) -clean: clean-c clean-rust - -clean-c: - rm -r out || true - -clean-rust: - rm $(SERVICEPOINT_HEADER_OUT)/servicepoint.h || true - cargo clean - .PHONY: all clean sizes $(_run_programs) clean-c clean-rust build-rust help FORCE -$(_unstripped_bins): out/%_unstripped: src/%.c src/helpers.h build-rust - mkdir -p out || true - $(CC) $< \ - -I $(SERVICEPOINT_HEADER_OUT) \ - -L $(RUST_TARGET_DIR) \ - $(CFLAGS) \ - -o $@ +$(_unstripped_bins): %_unstripped: src/%.c src/helpers.h build-rust + $(CC) $< $(CFLAGS) -o $@ -$(_bins): out/%: out/%_unstripped +$(_bins): %: %_unstripped $(STRIP) $(STRIPFLAGS) $^ -o $@ build-rust: FORCE - mkdir -p $(SERVICEPOINT_HEADER_OUT) || true # generate servicepoint header and library to link against $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) FORCE: ; -help: - @echo "You have the choice of the following parameters:" - @echo "" - @echo "Variable | Description | Default | Values" - @echo "---------+-----------------------------------+-----------+---------------------------" - @echo "CARGO | which cargo binary to use | 'cargo' | 'rustup run nightly cargo'" - @echo "CC | which C compiler to use | 'gcc' | 'musl-gcc'" - @echo "STRIP | which strip command to use | 'strip' | -" - @echo "MUSL | if set, use musl instead of glibc | unset | '1'" - @echo "PROFILE | Compilation profile | 'release' | 'debug' or 'size-optimized'" +#----- Begin Boilerplate +endif \ No newline at end of file diff --git a/example/target.mk b/example/target.mk new file mode 100644 index 0000000..12d98b1 --- /dev/null +++ b/example/target.mk @@ -0,0 +1,52 @@ +# Based on https://make.mad-scientist.net/papers/multi-architecture-builds/ + +.SUFFIXES: + +OBJDIR := _out_$(RUST_TARGET)-$(_link_type)-$(_profile) + +# Define the rules to build in the target subdirectories. +# +MAKETARGET = $(MAKE) --no-print-directory -C $@ -f $(CURDIR)/Makefile \ + SRCDIR=$(CURDIR) $(MAKECMDGOALS) + +.PHONY: $(OBJDIR) +$(OBJDIR): + +@[ -d "$@" ] || mkdir -p "$@" + +@$(MAKETARGET) + +# These rules keep make from trying to use the match-anything rule below to +# rebuild the makefiles--ouch! Obviously, if you don't follow my convention +# of using a `.mk' suffix on all non-standard makefiles you'll need to change +# the pattern rule. +# +Makefile : ; +%.mk :: ; + +# Anything we don't know how to build will use this rule. The command is a +# do-nothing command, but the prerequisites ensure that the appropriate +# recursive invocations of make will occur. +# +% :: $(OBJDIR) ; + +# The clean rule is best handled from the source directory: since we're +# rigorous about keeping the target directories containing only target files +# and the source directory containing only source files, `clean' is as trivial +# as removing the target directories! +# +.PHONY: clean clean-all +clean: + rm -rf $(OBJDIR) +clean-all: + rm -rf _out_* + +help: + @echo "You have the choice of the following parameters:" + @echo "" + @echo "Variable | Description | Default | Values" + @echo "---------+----------------------+-----------+---------------------------" + @echo "LIBC | libc to link against | 'gnu' | 'gnu' or 'musl'" + @echo "PROFILE | Optimization profile | 'release' | 'debug' or 'size-optimized'" + @echo "LINK | | 'dynamic' | 'dynamic' or 'static'" + @echo "CARGO | cargo binary to use | 'cargo' | 'rustup run nightly cargo'" + @echo "CC | C compiler to use | 'gcc' | 'musl-gcc'" + @echo "STRIP | strip command to use | 'strip' | -"