Skip to contentSkip to conversation

package/pkg-cargo: add HOST_* variables for target build

RFC PATCH In review Sebastien LAVEZE 3 participants 4 messages Feb 10 – Feb 11

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 package/pkg-cargo: add HOST_* variables for target build

From: Sebastien Laveze <[email protected]>

Cargo builds both host (build.rs) and target in the same build stage.

Host-side build scripts (build.rs) using cc-rs resolve tools/flags through HOST_* before falling back to unprefixed variables. Without HOST_* set, they can incorrectly pick target compiler/flags and fail in mixed-bitness setups (e.g. 64-bit host with 32-bit target).

https://docs.rs/cc/1.2.55/cc/#external-configuration-via-environment-variables

---
 package/pkg-cargo.mk | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
package/pkg-cargo.mk +13 -1
index 47a6353f25..a3c7436a1b 100644
@@ -51,7 +51,19 @@ PKG_CARGO_ENV = \
 	CARGO_TARGET_APPLIES_TO_HOST="false" \
 	CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
 	CARGO_HOST_RUSTFLAGS="$(addprefix -C link-args=,$(HOST_LDFLAGS))" \
-	CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
+	CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc \
+	HOST_AR="$(HOSTAR)" \
+	HOST_AS="$(HOSTAS)" \
+	HOST_CC="$(HOSTCC)" \
+	HOST_CXX="$(HOSTCXX)" \
+	HOST_CPP="$(HOSTCPP)" \
+	HOST_LD="$(HOSTLD)" \
+	HOST_NM="$(HOSTNM)" \
+	HOST_RANLIB="$(HOSTRANLIB)" \
+	HOST_PKG_CONFIG_SYSROOT_DIR="/" \
+	HOST_PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
+	HOST_PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 \
+	HOST_PKG_CONFIG_LIBDIR="$(HOST_DIR)/lib/pkgconfig:$(HOST_DIR)/share/pkgconfig"
 
 # We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
 # as we are unable to select a build profile using the environment.
Signature · 6 lines
2.51.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

Hi Sebastien,

Quoted 8 lines · Sebastien LAVEZE

From: Sebastien Laveze <[email protected]>

Cargo builds both host (build.rs) and target in the same build stage.

Host-side build scripts (build.rs) using cc-rs resolve tools/flags through HOST_* before falling back to unprefixed variables. Without HOST_* set, they can incorrectly pick target compiler/flags and fail in mixed-bitness setups (e.g. 64-bit host with 32-bit target).

This sounds sensible.

Do you have a defconfig that reproduces the problem?

Quoted 8 lines

https://docs.rs/cc/1.2.55/cc/#external-configuration-via-environment-variables

---
  package/pkg-cargo.mk | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)

[...]

Best,
Marcus
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#2 lore raw

Hi Marcus,

Quoted 11 lines · Marcus Hoffmann
Quoted 7 lines

Host-side build scripts (build.rs) using cc-rs resolve tools/flags through
HOST_* before falling back to unprefixed variables. Without HOST_* set, they
can incorrectly pick target compiler/flags and fail in mixed- bitness setups
(e.g. 64-bit host with 32-bit target).

This sounds sensible.

Do you have a defconfig that reproduces the problem?

I'm based on an imx6ull_evk and had no issue with cargo build until I started integrating some build.rs logic with some openssl-sys deps.

However I was wondering what's the best approach for fixing this. Injecting TARGET_CONFIGURE_OPTS in the cargo target build doesn't seem ideal because cc-rs will fallback to the non prefixed version making the error more cumbersome (using target cc instead of host cc).

For example:
warning: [email protected]: arm-buildroot-linux-gnueabihf- gcc.br_real: error: unrecognized command-line option '-m64'
error: failed to run custom build command for openssl-sys v0.9.111

To avoid possible misuse I would also remove TARGET_CONFIGURE_OPTS in the target build. The good news is that buildroot (package/Makefile.in) already has TARGET_* definitions which perfectly match cc-rs requirements along with some HOST_* but not all of them.

Quoted 3 lines

Best,
Marcus

Thx,
Sebastien
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#3 lore raw
In reply to Sebastien LAVEZE
Quoted 11 lines · Sebastien LAVEZE

From: Sebastien Laveze <[email protected]>

Cargo builds both host (build.rs) and target in the same build stage.

Host-side build scripts (build.rs) using cc-rs resolve tools/flags through HOST_* before falling back to unprefixed variables. Without HOST_* set, they can incorrectly pick target compiler/flags and fail in mixed-bitness setups (e.g. 64-bit host with 32-bit target).

https://docs.rs/cc/1.2.55/cc/#external-configuration-via-environment-variables

Hmm, it looks like these docs are from a specific cargo crate, not cargo itself, doesn't cargo let us set host specific configurations like this?:
https://doc.rust-lang.org/cargo/reference/unstable.html#host-config

Quoted 11 lines
---
 package/pkg-cargo.mk | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
package/pkg-cargo.mk +0 -0
index 47a6353f25..a3c7436a1b 100644

package/pkg-cargo.mk PKG_CARGO_ENV = \ · line 53

Quoted 3 lines
package/pkg-cargo.mk +0 -0
@@ -51,7 +51,19 @@ PKG_CARGO_ENV = \
        CARGO_TARGET_APPLIES_TO_HOST="false" \
        CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
        CARGO_HOST_RUSTFLAGS="$(addprefix -C link-args=,$(HOST_LDFLAGS))" \

Can we not use the CARGO_HOST_ prefixed env variables instead?

Quoted 20 lines
package/pkg-cargo.mk +13 -2
-       CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
+       CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc \
+       HOST_AR="$(HOSTAR)" \
+       HOST_AS="$(HOSTAS)" \
+       HOST_CC="$(HOSTCC)" \
+       HOST_CXX="$(HOSTCXX)" \
+       HOST_CPP="$(HOSTCPP)" \
+       HOST_LD="$(HOSTLD)" \
+       HOST_NM="$(HOSTNM)" \
+       HOST_RANLIB="$(HOSTRANLIB)" \
+       HOST_PKG_CONFIG_SYSROOT_DIR="/" \
+       HOST_PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
+       HOST_PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 \
+       HOST_PKG_CONFIG_LIBDIR="$(HOST_DIR)/lib/pkgconfig:$(HOST_DIR)/share/pkgconfig"
 
 # We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
 # as we are unable to select a build profile using the environment.
--

2.51.0

Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot