[pbs-devel] [PATCH proxmox-backup] fix static build

Fabian Grünbichler f.gruenbichler at proxmox.com
Mon Jun 23 08:17:05 CEST 2025


`cargo rustc` only passes the flags (like `target-feature` in this case) for
the final invocation, not for any dependency compilation.

unfortunately, switching to `cargo build` is not straight-forward:
- during a package build, $CARGO is the cargo wrapper which only honors
  RUSTFLAGS in its `prepare-debian` invocation
- rustflags in cargo's config.toml are global/per target
- the unstable override that would allow setting them per profile is broken
- and it would only work for the final invocation anyway, just like `cargo rustc`

as a stop-gap measure, let's duplicate and adapt the generated config.toml, and
select it explicitly when doing the static compilation as part of the package
build. manual `make proxmox-backup-client-static` can still just pass RUSTFLAGS
via the environment..

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
verified that this passes the right --target-feature to the build, and that
code guarded under that feature with an intentionally introduced compilation
error trips up the static client build..

sed is not my strongest suite, so feel free to make that part in d/rules
nicer..

we could also put some of the variables into a shared file source by Makefile
and d/rules, maybe..

 Makefile     | 14 +++++++++-----
 debian/rules | 13 +++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 1ae8da6f5..f14b4d8ce 100644
--- a/Makefile
+++ b/Makefile
@@ -38,19 +38,23 @@ SUBCRATES != cargo metadata --no-deps --format-version=1 \
 	| grep "$$PWD/" \
 	| sed -e "s!.*$$PWD/!!g" -e 's/\#.*$$//g' -e 's/)$$//g'
 
+# sync with debian/rules!
 STATIC_TARGET_DIR := target/static-build
-
 ifeq ($(BUILD_MODE), release)
 CARGO_BUILD_ARGS += --release --target $(DEB_HOST_RUST_TYPE)
+CARGO_STATIC_CONFIG ?= --config debian/cargo_home/config.static.toml
+CARGO_STATIC_BUILD_ARGS += $(CARGO_STATIC_CONFIG) --release --target $(DEB_HOST_RUST_TYPE) --target-dir $(STATIC_TARGET_DIR)
 COMPILEDIR := target/$(DEB_HOST_RUST_TYPE)/release
 STATIC_COMPILEDIR := $(STATIC_TARGET_DIR)/$(DEB_HOST_RUST_TYPE)/release
 else
 CARGO_BUILD_ARGS += --target $(DEB_HOST_RUST_TYPE)
+CARGO_STATIC_BUILD_ARGS += --target $(DEB_HOST_RUST_TYPE) --target-dir $(STATIC_TARGET_DIR)
 COMPILEDIR := target/$(DEB_HOST_RUST_TYPE)/debug
 STATIC_COMPILEDIR := $(STATIC_TARGET_DIR)/$(DEB_HOST_RUST_TYPE)/debug
 endif
 
 STATIC_RUSTC_FLAGS := -C target-feature=+crt-static -L $(STATIC_COMPILEDIR)/deps-stubs/
+# end sync with debian/rules
 
 ifeq ($(valgrind), yes)
 CARGO_BUILD_ARGS += --features valgrind
@@ -209,11 +213,11 @@ $(STATIC_BINS) &:
 	mkdir -p $(STATIC_COMPILEDIR)/deps-stubs/ && \
           echo '!<arch>' > $(STATIC_COMPILEDIR)/deps-stubs/libsystemd.a # workaround for to greedy linkage and proxmox-systemd
 	OPENSSL_STATIC=1 \
-	$(CARGO) rustc $(CARGO_BUILD_ARGS) --package pxar-bin --bin pxar \
-	  --target-dir $(STATIC_TARGET_DIR) -- $(STATIC_RUSTC_FLAGS)
+	RUSTFLAGS="$(STATIC_RUSTC_FLAGS)" \
+	$(CARGO) build $(CARGO_STATIC_BUILD_ARGS) --package pxar-bin --bin pxar
 	OPENSSL_STATIC=1 \
-	$(CARGO) rustc $(CARGO_BUILD_ARGS) --package proxmox-backup-client --bin proxmox-backup-client \
-	  --target-dir $(STATIC_TARGET_DIR) -- $(STATIC_RUSTC_FLAGS)
+	RUSTFLAGS="$(STATIC_RUSTC_FLAGS)" \
+	$(CARGO) build $(CARGO_STATIC_BUILD_ARGS) --package proxmox-backup-client --bin proxmox-backup-client
 
 .PHONY: lint
 lint:
diff --git a/debian/rules b/debian/rules
index 70cec4754..6f1773eaf 100755
--- a/debian/rules
+++ b/debian/rules
@@ -7,6 +7,16 @@ include /usr/share/dpkg/pkg-info.mk
 include /usr/share/rustc/architecture.mk
 
 export BUILD_MODE=release
+export CARGO_STATIC_CONFIG=--config debian/cargo_home/config.static.toml
+
+# sync with Makefile!
+STATIC_TARGET_DIR := target/static-build
+ifeq ($(BUILD_MODE), release)
+STATIC_COMPILEDIR := $(STATIC_TARGET_DIR)/$(DEB_HOST_RUST_TYPE)/release
+else
+STATIC_COMPILEDIR := $(STATIC_TARGET_DIR)/$(DEB_HOST_RUST_TYPE)/debug
+endif
+# end sync with Makefile!
 
 export CARGO=/usr/share/cargo/bin/cargo
 
@@ -28,6 +38,9 @@ override_dh_auto_configure:
 	@perl -ne 'if (/^version\s*=\s*"(\d+(?:\.\d+)+)"/) { my $$v_cargo = $$1; my $$v_deb = "$(DEB_VERSION_UPSTREAM)"; \
 	    die "ERROR: d/changelog <-> Cargo.toml version mismatch: $$v_cargo != $$v_deb\n" if $$v_cargo ne $$v_deb; exit(0); }' Cargo.toml
 	$(CARGO) prepare-debian $(CURDIR)/debian/cargo_registry --link-from-system
+	# add a new config for static building, sync with Makefile!
+	cp debian/cargo_home/config.toml debian/cargo_home/config.static.toml
+	sed -ie 's!^\(rustflags = .*\)\]$$!\1, "-C", "target-feature=+crt-static", "-L", "$(STATIC_COMPILEDIR)/deps-stubs/"\]!' debian/cargo_home/config.static.toml
 	# `cargo build` and `cargo install` have different config precedence, symlink
 	# the wrapper config into a place where `build` picks it up as well..
 	# https://doc.rust-lang.org/cargo/commands/cargo-install.html#configuration-discovery
-- 
2.39.5





More information about the pbs-devel mailing list