From a4699b9bc8c5d6d91b51d4232259e7ea549cbd2e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 16:48:19 +0200 Subject: [PATCH 1/2] rename size-optimized to size_optimized --- .github/workflows/rust.yml | 4 ++-- Cargo.toml | 2 +- example/Makefile | 10 +++++----- example/target.mk | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f4af981..c025406 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -52,6 +52,6 @@ jobs: - name: install rust targets run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update - - name: build example -- glibc size-optimized + - name: build example -- glibc size_optimized run: cd example && make clean - && make LIBC=gnu LINK=dynamic PROFILE=size-optimized CARGO="rustup run nightly cargo" LTO=1 + && make LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 diff --git a/Cargo.toml b/Cargo.toml index dd3d637..5e29900 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ missing_safety_doc = "allow" [package.metadata.docs.rs] all-features = true -[profile.size-optimized] +[profile.size_optimized] inherits = "release" opt-level = 'z' # Optimize for size lto = true # Enable link-time optimization diff --git a/example/Makefile b/example/Makefile index b238200..c69b9a7 100644 --- a/example/Makefile +++ b/example/Makefile @@ -11,8 +11,8 @@ ifeq (,$(filter dynamic static,$(_link_type))) 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") +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) @@ -43,7 +43,7 @@ _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) 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 -size-optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ +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 \ --profile=$(_rust_cli_profile) \ @@ -55,7 +55,7 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections -size-optimized_CFLAGS += -Oz \ +size_optimized_CFLAGS += -Oz \ -fwrapv -fomit-frame-pointer -fno-stack-protector\ -fno-unroll-loops \ -fno-unwind-tables -fno-asynchronous-unwind-tables \ @@ -74,7 +74,7 @@ debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic -size-optimized_RUSTFLAGS += -Zlocation-detail=none \ +size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ -C panic=abort diff --git a/example/target.mk b/example/target.mk index 12d98b1..2b74f03 100644 --- a/example/target.mk +++ b/example/target.mk @@ -45,7 +45,7 @@ help: @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 "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'" From 4d9c9e2ac4e08e1c86a63e5753cbb0b928410d35 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 17:21:08 +0200 Subject: [PATCH 2/2] clean up .PHONY and all --- .github/workflows/rust.yml | 8 ++++---- example/Makefile | 27 ++++++++++++--------------- example/target.mk | 6 +++++- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c025406..d0f1da3 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 LIBC=gnu LINK=dynamic PROFILE=release + run: cd example && make -r clean-all && make -r LIBC=gnu LINK=dynamic PROFILE=release - name: build example -- glibc debug - run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=debug + run: cd example && make -r clean-all && make -r LIBC=gnu LINK=dynamic PROFILE=debug build-size-gnu-unstable: runs-on: ubuntu-latest @@ -53,5 +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 LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 + run: cd example && make clean-all -r + && make -r LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 diff --git a/example/Makefile b/example/Makefile index c69b9a7..e2ac12e 100644 --- a/example/Makefile +++ b/example/Makefile @@ -34,6 +34,7 @@ FEATURES := ""#"env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) +CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) # TODO: check if override is needed _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) @@ -68,12 +69,19 @@ size_optimized_CFLAGS += -Oz \ -fno-ident \ -fno-exceptions \ $(_no_debug_cflags) + release_CFLAGS += -O2 \ $(_no_debug_cflags) debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic +_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 + size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ @@ -86,39 +94,28 @@ ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -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 - # 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 := $(_programs) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) -_run_programs := $(addprefix run_, $(_programs)) -# TODO: use cargo --target-dir to put rust binaries into same dir +.SUFFIXES: + +.PHONY: all build-rust all: $(_bins) -.PHONY: all clean sizes $(_run_programs) clean-c clean-rust build-rust help FORCE - $(_unstripped_bins): %_unstripped: src/%.c src/helpers.h build-rust $(CC) $< $(CFLAGS) -o $@ $(_bins): %: %_unstripped $(STRIP) $(STRIPFLAGS) $^ -o $@ -build-rust: FORCE +build-rust: # generate servicepoint header and library to link against $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) -FORCE: ; - #----- Begin Boilerplate endif \ No newline at end of file diff --git a/example/target.mk b/example/target.mk index 2b74f03..b7e743c 100644 --- a/example/target.mk +++ b/example/target.mk @@ -33,7 +33,7 @@ Makefile : ; # and the source directory containing only source files, `clean' is as trivial # as removing the target directories! # -.PHONY: clean clean-all +.PHONY: clean clean-all help clean: rm -rf $(OBJDIR) clean-all: @@ -50,3 +50,7 @@ help: @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' | -" + @echo "" + @echo "When building this project, each configuration will result in a separate output directory." + @echo "The target clean only removes the output directory of the specified configuration." + @echo "The target clean-all can be used to remove the builds of all configurations."