all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup] fix static build
Date: Mon, 23 Jun 2025 08:17:05 +0200	[thread overview]
Message-ID: <20250623061705.79482-1-f.gruenbichler@proxmox.com> (raw)

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



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel

             reply	other threads:[~2025-06-23  6:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23  6:17 Fabian Grünbichler [this message]
2025-06-23  7:50 ` Fabian Grünbichler
2025-06-23 11:41   ` Fabian Grünbichler
2025-06-30 13:56     ` [pbs-devel] applied: " Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250623061705.79482-1-f.gruenbichler@proxmox.com \
    --to=f.gruenbichler@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal