size optimized examples build in flake, remove unstable compiler flags from makefile
Some checks failed
Rust / build-gnu-apt (pull_request) Failing after 4m38s
Rust / build-size-gnu-unstable (pull_request) Failing after 57s

dynamically linked bin is 16k
This commit is contained in:
Vinzenz Schroeter 2025-06-02 23:10:03 +02:00
parent d98aec63b0
commit 579a68c77a
3 changed files with 84 additions and 28 deletions

View file

@ -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
endif

13
nix-build-all.sh Executable file
View file

@ -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

View file

@ -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;
}
)
)
)