From 579a68c77ad38b1fd989021878c2e9cb264adb9a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 2 Jun 2025 23:10:03 +0200 Subject: [PATCH] size optimized examples build in flake, remove unstable compiler flags from makefile dynamically linked bin is 16k --- example/Makefile | 8 ++--- nix-build-all.sh | 13 +++++++ packages.nix | 91 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 84 insertions(+), 28 deletions(-) create mode 100755 nix-build-all.sh diff --git a/example/Makefile b/example/Makefile index 1617552..5659216 100644 --- a/example/Makefile +++ b/example/Makefile @@ -39,16 +39,12 @@ CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) _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" \ - -Zbuild-std-features="panic_immediate_abort" CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --profile=$(_rust_cli_profile) \ --no-default-features \ --features=$(FEATURES) \ --target=$(RUST_TARGET) \ - $($(_profile)_CARGOFLAGS) \ --target-dir=cargo CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie @@ -73,7 +69,7 @@ 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) +_servicepoint_cflags := $(pkg-config --libs servicepoint --cflags) CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) ifeq ($(LTO), 1) CFLAGS += -flto @@ -120,4 +116,4 @@ build-rust: $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) #----- Begin Boilerplate -endif \ No newline at end of file +endif diff --git a/nix-build-all.sh b/nix-build-all.sh new file mode 100755 index 0000000..059297b --- /dev/null +++ b/nix-build-all.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e +set -x + +nix build .#servicepoint-binding-c + +nix build .#servicepoint-binding-c-stable-release +nix build .#servicepoint-binding-c-nightly-release +nix build .#servicepoint-binding-c-nightly-size + +nix build .#all-examples +nix build .#all-examples-size diff --git a/packages.nix b/packages.nix index 1716250..f973020 100644 --- a/packages.nix +++ b/packages.nix @@ -8,12 +8,9 @@ let version = "0.15.0"; mkServicepoint = - { - rustPlatform, - buildType, - }: + rustPlatform: rustPlatform.buildRustPackage (finalAttrs: { - inherit buildType version; + inherit version; pname = "servicepoint-binding-c"; src = lib.filter { @@ -28,14 +25,15 @@ let ]; }; cargoLock.lockFile = ./Cargo.lock; + useFetchCargoVendor = true; meta = { description = "C bindings for the servicepoint crate."; homepage = "https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c"; license = lib.licenses.gpl3Plus; pkgConfigModules = [ "servicepoint" ]; }; - nativeBuildInputs = with pkgs; [ pkg-config ]; - buildInputs = with pkgs; [ xz ]; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ pkgs.xz ]; preBuild = '' mkdir -p include @@ -56,16 +54,17 @@ let pkgs.stdenv.mkDerivation { inherit version; pname = "servicepoint-c-example-${name}"; - - nativeBuildInputs = with pkgs; [ pkg-config ]; - buildInputs = [ servicepointBinding ]; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ + servicepointBinding + pkgs.xz + ]; src = ./example/src; - - #dontUnpack = true; buildPhase = '' + set -e + set -x mkdir -p $out/bin - - gcc ${name}.c $(pkg-config --libs servicepoint --cflags) -o $out/bin/${name} + gcc ${name}.c $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) $EXTRA_CFLAGS -o $out/bin/${name} ''; }; rustPlatform-stable = pkgs.rustPlatform; @@ -83,21 +82,29 @@ let ]; in rec { - servicepoint-binding-c-release = mkServicepoint { - buildType = "release"; - rustPlatform = rustPlatform-stable; - }; - servicepoint-binding-c-size-optimized = mkServicepoint { + servicepoint-binding-c-stable-release = mkServicepoint rustPlatform-stable; + servicepoint-binding-c-nightly-release = mkServicepoint rustPlatform-unstable; + servicepoint-binding-c-nightly-size = servicepoint-binding-c-nightly-release // { buildType = "size_optimized"; - rustPlatform = rustPlatform-unstable; + buildNoDefaultFeatures = true; + # TODO: do these override the nix flags? + #CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; + # TODO: those override the nix flags + # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; + # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; }; - servicepoint-binding-c = servicepoint-binding-c-release; + servicepoint-binding-c = servicepoint-binding-c-stable-release; all-examples = pkgs.symlinkJoin { name = "servicepoint-all-examples"; - paths = builtins.map (e: selfPkgs.${e}) examples; + paths = builtins.map (e: selfPkgs."${e}") examples; + }; + all-examples-size = pkgs.symlinkJoin { + name = "servicepoint-all-examples-size"; + paths = builtins.map (e: selfPkgs."${e}-size") examples; }; } +# construct one package per example // (lib.genAttrs examples ( name: mkExample { @@ -105,3 +112,43 @@ rec { servicepointBinding = selfPkgs.servicepoint-binding-c; } )) +# construct another pakage per example, but with a different name and compiler options +// (lib.mapAttrs' + ( + name: value: + lib.nameValuePair ("${name}-size") ( + value + // { + EXTRA_CFLAGS = builtins.toString [ + "-Oz" + "-fwrapv" + "-fomit-frame-pointer" + "-fno-stack-protector" + "-fno-unroll-loops" + "-fno-unwind-tables" + "-fno-asynchronous-unwind-tables" + "-fmerge-all-constants" + "-fvisibility=hidden" + "-Bsymbolic" + "-fno-ident" + "-fno-exceptions" + "-ffunction-sections" + "-fdata-sections" + "-Wl,-z,norelro" + "-Wl,--hash-style=gnu" + "-Wl,--gc-sections" + "-Wl,--exclude-libs,ALL" + ]; + } + ) + ) + ( + lib.genAttrs examples ( + name: + mkExample { + inherit name; + servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; + } + ) + ) +)