Skip to content

Commit ebaab20

Browse files
committed
fix: build when target_arch==build_arch
In #82 we fixed the build when the target arch is the same as the build arch. A side effect of the fix is that the rust build-scripts (which are intended to run on the build host) are now built with the target compiler and cflags. This is fine in most cases but it breaks when the host system is older than the system we are building. For example, we might build for a newer glibc that will be available on the target but when we run the build script on the host, this version does not exist which leads to the error described in #83. Another version of the same problem is that we may try to use some build flags that are available on the compiler for the target (`aarch64-poky-linux-gcc`) but not on the host compiler (`gcc`) because the host compiler is older than the one we are using for the target. This patch fixes the problem by using two unstable features of cargo: - [`UNSTABLE_TARGET_APPLIES_TO_HOST`](https://doc.rust-lang.org/cargo/reference/unstable.html#target-applies-to-host) allows us to pass the `CARGO_TARGET_APPLIES_TO_HOST=false` setting so that the target build settings are not used when building for the host. - [`UNSTABLE_HOST_CONFIG`](https://doc.rust-lang.org/cargo/reference/unstable.html#host-config) allows us to set build flags for the host in a `[host]` section of the `Cargo.toml` file. The `__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS` environment variable is required to use these unstable features on a stable rust. This patch works with stable rust and does not require unstable. The inspiration for this solution [comes from buildroot](https://github.com/buildroot/buildroot/blob/25d865996d1d9753fe7d4dfe39cf18c7e9f91224/package/pkg-cargo.mk#L26-L47). fixes #83
1 parent d56c37a commit ebaab20

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

classes/cargo.bbclass

+17-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ create_cargo_config() {
6464
echo > ${CARGO_HOME}/config
6565
echo "[target.${RUST_TARGET}]" >> ${CARGO_HOME}/config
6666
echo "linker = '${WRAPPER_DIR}/linker-wrapper.sh'" >> ${CARGO_HOME}/config
67+
68+
# (Rust unstable) - See do_compile below.
69+
echo "[host]" >> ${CARGO_HOME}/config
70+
echo "linker = '${WRAPPER_DIR}/linker-native-wrapper.sh'" >> ${CARGO_HOME}/config
6771
fi
6872

6973
echo >> ${CARGO_HOME}/config
@@ -116,12 +120,23 @@ cargo_do_compile() {
116120
export TARGET_CXX="${WRAPPER_DIR}/cxx-wrapper.sh"
117121
export CC="${WRAPPER_DIR}/cc-native-wrapper.sh"
118122
export CXX="${WRAPPER_DIR}/cxx-native-wrapper.sh"
119-
export TARGET_LD="${WRAPPER_DIR}/ld-wrapper.sh"
120-
export LD="${WRAPPER_DIR}/ld-native-wrapper.sh"
121123
export PKG_CONFIG_ALLOW_CROSS="1"
122124
export LDFLAGS=""
123125
export RUSTFLAGS="${RUSTFLAGS}"
124126
export SSH_AUTH_SOCK="${SSH_AUTH_SOCK}"
127+
# When RUST_BUILD == RUST_TARGET, we need to use an unstable Rust feature
128+
# to specify different build options for the target and the Host.
129+
# The Host configuration is set in do_configure() above.
130+
if [ "${RUST_BUILD}" = "${RUST_TARGET}" ]; then
131+
export __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly"
132+
export CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true"
133+
export CARGO_UNSTABLE_HOST_CONFIG="true"
134+
export CARGO_TARGET_APPLIES_TO_HOST="false"
135+
136+
# Make sure the cc crate does not use CFLAGS when building for the host.
137+
export HOST_CFLAGS=""
138+
export HOST_CXXFLAGS=""
139+
fi
125140
bbnote "which rustc:" `which rustc`
126141
bbnote "rustc --version" `rustc --version`
127142
bbnote "which cargo:" `which cargo`

0 commit comments

Comments
 (0)