Skip to content

Commit 8fb174f

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. 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. During review of this PR, it was decided to drop the `cargo_home/config` file completely and use only environment variables. 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 7ac87ec commit 8fb174f

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

classes/cargo_bin.bbclass

+18-29
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,6 @@ CARGO_BUILD_FLAGS = "\
5151
${EXTRA_CARGO_FLAGS} \
5252
"
5353

54-
create_cargo_config() {
55-
if [ "${RUST_BUILD}" != "${RUST_TARGET}" ]; then
56-
echo > ${CARGO_HOME}/config
57-
echo "[target.${RUST_BUILD}]" >> ${CARGO_HOME}/config
58-
echo "linker = '${WRAPPER_DIR}/linker-native-wrapper.sh'" >> ${CARGO_HOME}/config
59-
60-
echo >> ${CARGO_HOME}/config
61-
echo "[target.${RUST_TARGET}]" >> ${CARGO_HOME}/config
62-
echo "linker = '${WRAPPER_DIR}/linker-wrapper.sh'" >> ${CARGO_HOME}/config
63-
else
64-
echo > ${CARGO_HOME}/config
65-
echo "[target.${RUST_TARGET}]" >> ${CARGO_HOME}/config
66-
echo "linker = '${WRAPPER_DIR}/linker-wrapper.sh'" >> ${CARGO_HOME}/config
67-
fi
68-
69-
echo >> ${CARGO_HOME}/config
70-
echo "[build]" >> ${CARGO_HOME}/config
71-
echo "rustflags = ['-C', 'rpath']" >> ${CARGO_HOME}/config
72-
73-
echo >> ${CARGO_HOME}/config
74-
echo "[profile.release]" >> ${CARGO_HOME}/config
75-
echo "debug = true" >> ${CARGO_HOME}/config
76-
}
77-
7854
cargo_bin_do_configure() {
7955
mkdir -p "${B}"
8056
mkdir -p "${CARGO_HOME}"
@@ -106,22 +82,35 @@ cargo_bin_do_configure() {
10682
echo "#!/bin/sh" >"${WRAPPER_DIR}/linker-native-wrapper.sh"
10783
echo "${BUILD_CC} ${BUILD_LDFLAGS} \"\$@\"" >>"${WRAPPER_DIR}/linker-native-wrapper.sh"
10884
chmod +x "${WRAPPER_DIR}/linker-native-wrapper.sh"
109-
110-
# Create our global config in CARGO_HOME
111-
create_cargo_config
11285
}
11386

11487
cargo_bin_do_compile() {
11588
export TARGET_CC="${WRAPPER_DIR}/cc-wrapper.sh"
11689
export TARGET_CXX="${WRAPPER_DIR}/cxx-wrapper.sh"
11790
export CC="${WRAPPER_DIR}/cc-native-wrapper.sh"
11891
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"
12192
export PKG_CONFIG_ALLOW_CROSS="1"
12293
export LDFLAGS=""
12394
export RUSTFLAGS="${RUSTFLAGS}"
12495
export SSH_AUTH_SOCK="${SSH_AUTH_SOCK}"
96+
97+
# This "DO_NOT_USE_THIS" option of cargo is currently the only way to
98+
# configure a different linker for host and target builds when RUST_BUILD ==
99+
# RUST_TARGET.
100+
export __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly"
101+
export CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true"
102+
export CARGO_UNSTABLE_HOST_CONFIG="true"
103+
export CARGO_TARGET_APPLIES_TO_HOST="false"
104+
export CARGO_TARGET_${@rust_target(d, 'BUILD').replace('-','_')}_LINKER="${WRAPPER_DIR}/linker-wrapper.sh"
105+
export CARGO_HOST_LINKER="${WRAPPER_DIR}/linker-native-wrapper.sh"
106+
export CARGO_BUILD_FLAGS="-C rpath"
107+
export CARGO_PROFILE_RELEASE_DEBUG="true"
108+
109+
# The CC crate defaults to using CFLAGS when compiling everything. We can
110+
# give it custom flags for compiling on the host.
111+
export HOST_CXXFLAGS=""
112+
export HOST_CFLAGS=""
113+
125114
bbnote "which rustc:" `which rustc`
126115
bbnote "rustc --version" `rustc --version`
127116
bbnote "which cargo:" `which cargo`

0 commit comments

Comments
 (0)