commit:     869a67efcaedbf7d08fe16057466f58ac1f21a84
Author:     Matoro Mahri <matoro_gentoo <AT> matoro <DOT> tk>
AuthorDate: Fri Feb  9 15:48:17 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Feb 10 07:49:17 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=869a67ef

dev-lang/rust: crossdev support

This allows Rust to be cross-compiled with crossdev.  This is the last
item for bug 680652, closing it.  Note that this is for a foreign CHOST,
i.e., CBUILD != CHOST == CTARGET.  It does not cover the scenario for
CBUILD == CHOST != CTARGET(s), which is tracked separately in bug
679878.

A number of changes were required in order to make this work:

* Force USE=system-bootstrap, since we cannot specify the appropriate
  boostrap URL based on CBUILD in SRC_URI
* LLVm is compiled twice as part of the build, once for host and once
  for target.  However, the rust build uses the same settings from
  config.toml for both builds.  Therefore we cannot override flags nor
  default-linker and must let rust choose them for us.
* Set the appropriate build/host variables which correspond to
  CBUILD/CHOST.  This works as expected.
* Set PKG_CONFIG and OPENSSL_*_DIR variables; cargo needs these for some
  reason.
* Enforce that LLVM_TARGETS is set correctly for both host and target
  arches.  This uses the new llvm_tuple_to_target function, introduced
  in https://github.com/gentoo/gentoo/pull/33996
* Lastly a small patch to rust source is needed, to tell it to link with
  system libz.  It's unclear why this scenario was excluded under
  cross-compile conditions in upstream rust.

See: https://paste.sr.ht/~kchibisov/682321e0fd4a3ece4a4b7b71591896f5cd3cdb22
Bug: https://bugs.gentoo.org/679878
Closes: https://bugs.gentoo.org/680652
Signed-off-by: Matoro Mahri <matoro_gentoo <AT> matoro.tk>
Closes: https://github.com/gentoo/gentoo/pull/35246
Signed-off-by: Sam James <sam <AT> gentoo.org>

 .../rust/files/1.74.1-cross-compile-libz.patch     | 19 +++++++++
 dev-lang/rust/rust-1.74.1.ebuild                   | 46 ++++++++++++++++------
 dev-lang/rust/rust-1.75.0.ebuild                   | 46 ++++++++++++++++------
 3 files changed, 89 insertions(+), 22 deletions(-)

diff --git a/dev-lang/rust/files/1.74.1-cross-compile-libz.patch 
b/dev-lang/rust/files/1.74.1-cross-compile-libz.patch
new file mode 100644
index 000000000000..f2a881b46134
--- /dev/null
+++ b/dev-lang/rust/files/1.74.1-cross-compile-libz.patch
@@ -0,0 +1,19 @@
+https://paste.sr.ht/~kchibisov/682321e0fd4a3ece4a4b7b71591896f5cd3cdb22
+https://github.com/gentoo/gentoo/pull/35246#discussion_r1484525497
+
+diff --git a/compiler/rustc_llvm/build.rs b/compiler/rustc_llvm/build.rs
+index f606fa483ca..8a2e1c40e8b 100644
+--- a/compiler/rustc_llvm/build.rs
++++ b/compiler/rustc_llvm/build.rs
+@@ -237,10 +237,7 @@ fn main() {
+     // of llvm-config, not the target that we're attempting to link.
+     let mut cmd = Command::new(&llvm_config);
+     cmd.arg(llvm_link_arg).arg("--libs");
+-
+-    if !is_crossed {
+-        cmd.arg("--system-libs");
+-    }
++    cmd.arg("--system-libs");
+ 
+     if (target.starts_with("arm") && !target.contains("freebsd"))
+         || target.starts_with("mips-")

diff --git a/dev-lang/rust/rust-1.74.1.ebuild b/dev-lang/rust/rust-1.74.1.ebuild
index 956ce4016716..b37b1fd78ff5 100644
--- a/dev-lang/rust/rust-1.74.1.ebuild
+++ b/dev-lang/rust/rust-1.74.1.ebuild
@@ -163,6 +163,7 @@ RESTRICT="test"
 VERIFY_SIG_OPENPGP_KEY_PATH=/usr/share/openpgp-keys/rust.asc
 
 PATCHES=(
+       "${FILESDIR}"/1.74.1-cross-compile-libz.patch
        #"${FILESDIR}"/1.72.0-bump-libc-deps-to-0.2.146.patch  # pending refresh
        "${FILESDIR}"/1.70.0-ignore-broken-and-non-applicable-tests.patch
        "${FILESDIR}"/1.62.1-musl-dynamic-linking.patch
@@ -184,7 +185,7 @@ bootstrap_rust_version_check() {
        [[ ${MERGE_TYPE} == binary ]] && return
        local rustc_wanted="$(ver_cut 1).$(($(ver_cut 2) - 1))"
        local rustc_toonew="$(ver_cut 1).$(($(ver_cut 2) + 1))"
-       local rustc_version=( $(eselect --brief rust show 2>/dev/null) )
+       local rustc_version=( $(eselect --brief --root="${BROOT}" rust show 
2>/dev/null) )
        rustc_version=${rustc_version[0]#rust-bin-}
        rustc_version=${rustc_version#rust-}
 
@@ -256,6 +257,18 @@ pkg_setup() {
        python-any-r1_pkg_setup
 
        export LIBGIT2_NO_PKG_CONFIG=1 #749381
+       if tc-is-cross-compiler; then
+               export PKG_CONFIG_ALLOW_CROSS=1
+               export PKG_CONFIG_PATH="${ROOT}/usr/$(get_libdir)/pkgconfig"
+               export OPENSSL_INCLUDE_DIR="${ROOT}/usr/include"
+               export OPENSSL_LIB_DIR="${ROOT}/usr/$(get_libdir)"
+
+               use system-bootstrap || die "USE=system-bootstrap is required 
when cross-compiling"
+               use system-llvm && die "USE=system-llvm not allowed when 
cross-compiling"
+               local cross_llvm_target="$(llvm_tuple_to_target "${CBUILD}")"
+               use "llvm_targets_${cross_llvm_target}" || \
+                       die "Must enable LLVM_TARGETS=${cross_llvm_target} 
matching CBUILD=${CBUILD} when cross-compiling"
+       fi
 
        use system-bootstrap && bootstrap_rust_version_check
 
@@ -298,7 +311,7 @@ src_prepare() {
        if ! use system-bootstrap; then
                has_version sys-devel/gcc || esetup_unwind_hack
                local rust_stage0_root="${WORKDIR}"/rust-stage0
-               local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi)"
+               local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi 
"${CBUILD}")"
 
                "${WORKDIR}/${rust_stage0}"/install.sh --disable-ldconfig \
                        --without=rust-docs-json-preview,rust-docs 
--destdir="${rust_stage0_root}" --prefix=/ || die
@@ -347,6 +360,8 @@ src_configure() {
        [[ -d ${rust_stage0_root} ]] || die "${rust_stage0_root} is not a 
directory"
 
        rust_target="$(rust_abi)"
+       rust_build="$(rust_abi "${CBUILD}")"
+       rust_host="$(rust_abi "${CHOST}")"
 
        local cm_btype="$(usex debug DEBUG RELEASE)"
        cat <<- _EOF_ > "${S}"/config.toml
@@ -378,17 +393,24 @@ src_configure() {
                enable-warnings = false
                [llvm.build-config]
                CMAKE_VERBOSE_MAKEFILE = "ON"
-               CMAKE_C_FLAGS_${cm_btype} = "${CFLAGS}"
-               CMAKE_CXX_FLAGS_${cm_btype} = "${CXXFLAGS}"
-               CMAKE_EXE_LINKER_FLAGS_${cm_btype} = "${LDFLAGS}"
-               CMAKE_MODULE_LINKER_FLAGS_${cm_btype} = "${LDFLAGS}"
-               CMAKE_SHARED_LINKER_FLAGS_${cm_btype} = "${LDFLAGS}"
-               CMAKE_STATIC_LINKER_FLAGS_${cm_btype} = "${ARFLAGS}"
+               $(if ! tc-is-cross-compiler; then
+                       # When cross-compiling, LLVM is compiled twice, once 
for host and
+                       # once for target.  Unfortunately, this build 
configuration applies
+                       # to both, which means any flags applicable to one 
target but not
+                       # the other will break.  Conditionally disable 
respecting user
+                       # flags when cross-compiling.
+                       echo "CMAKE_C_FLAGS_${cm_btype} = \"${CFLAGS}\""
+                       echo "CMAKE_CXX_FLAGS_${cm_btype} = \"${CXXFLAGS}\""
+                       echo "CMAKE_EXE_LINKER_FLAGS_${cm_btype} = 
\"${LDFLAGS}\""
+                       echo "CMAKE_MODULE_LINKER_FLAGS_${cm_btype} = 
\"${LDFLAGS}\""
+                       echo "CMAKE_SHARED_LINKER_FLAGS_${cm_btype} = 
\"${LDFLAGS}\""
+                       echo "CMAKE_STATIC_LINKER_FLAGS_${cm_btype} = 
\"${ARFLAGS}\""
+               fi)
                [build]
                build-stage = 2
                test-stage = 2
-               build = "${rust_target}"
-               host = ["${rust_target}"]
+               build = "${rust_build}"
+               host = ["${rust_host}"]
                target = [${rust_targets}]
                cargo = "${rust_stage0_root}/bin/cargo"
                rustc = "${rust_stage0_root}/bin/rustc"
@@ -426,7 +448,9 @@ src_configure() {
                debuginfo-level-tests = 0
                backtrace = true
                incremental = false
-               default-linker = "$(tc-getCC)"
+               $(if ! tc-is-cross-compiler; then
+                       echo "default-linker = \"$(tc-getCC)\""
+               fi)
                parallel-compiler = $(toml_usex parallel-compiler)
                channel = "$(usex nightly nightly stable)"
                description = "gentoo"

diff --git a/dev-lang/rust/rust-1.75.0.ebuild b/dev-lang/rust/rust-1.75.0.ebuild
index 0f5a1abd1f6e..1c9761e003fe 100644
--- a/dev-lang/rust/rust-1.75.0.ebuild
+++ b/dev-lang/rust/rust-1.75.0.ebuild
@@ -163,6 +163,7 @@ RESTRICT="test"
 VERIFY_SIG_OPENPGP_KEY_PATH=/usr/share/openpgp-keys/rust.asc
 
 PATCHES=(
+       "${FILESDIR}"/1.74.1-cross-compile-libz.patch
        #"${FILESDIR}"/1.72.0-bump-libc-deps-to-0.2.146.patch  # pending refresh
        "${FILESDIR}"/1.70.0-ignore-broken-and-non-applicable-tests.patch
        "${FILESDIR}"/1.67.0-doc-wasm.patch
@@ -186,7 +187,7 @@ bootstrap_rust_version_check() {
        [[ ${MERGE_TYPE} == binary ]] && return
        local rustc_wanted="$(ver_cut 1).$(($(ver_cut 2) - 1))"
        local rustc_toonew="$(ver_cut 1).$(($(ver_cut 2) + 1))"
-       local rustc_version=( $(eselect --brief rust show 2>/dev/null) )
+       local rustc_version=( $(eselect --brief --root="${BROOT}" rust show 
2>/dev/null) )
        rustc_version=${rustc_version[0]#rust-bin-}
        rustc_version=${rustc_version#rust-}
 
@@ -258,6 +259,18 @@ pkg_setup() {
        python-any-r1_pkg_setup
 
        export LIBGIT2_NO_PKG_CONFIG=1 #749381
+       if tc-is-cross-compiler; then
+               export PKG_CONFIG_ALLOW_CROSS=1
+               export PKG_CONFIG_PATH="${ROOT}/usr/$(get_libdir)/pkgconfig"
+               export OPENSSL_INCLUDE_DIR="${ROOT}/usr/include"
+               export OPENSSL_LIB_DIR="${ROOT}/usr/$(get_libdir)"
+
+               use system-bootstrap || die "USE=system-bootstrap is required 
when cross-compiling"
+               use system-llvm && die "USE=system-llvm not allowed when 
cross-compiling"
+               local cross_llvm_target="$(llvm_tuple_to_target "${CBUILD}")"
+               use "llvm_targets_${cross_llvm_target}" || \
+                       die "Must enable LLVM_TARGETS=${cross_llvm_target} 
matching CBUILD=${CBUILD} when cross-compiling"
+       fi
 
        use system-bootstrap && bootstrap_rust_version_check
 
@@ -300,7 +313,7 @@ src_prepare() {
        if ! use system-bootstrap; then
                has_version sys-devel/gcc || esetup_unwind_hack
                local rust_stage0_root="${WORKDIR}"/rust-stage0
-               local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi)"
+               local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi 
"${CBUILD}")"
 
                "${WORKDIR}/${rust_stage0}"/install.sh --disable-ldconfig \
                        --without=rust-docs-json-preview,rust-docs 
--destdir="${rust_stage0_root}" --prefix=/ || die
@@ -349,6 +362,8 @@ src_configure() {
        [[ -d ${rust_stage0_root} ]] || die "${rust_stage0_root} is not a 
directory"
 
        rust_target="$(rust_abi)"
+       rust_build="$(rust_abi "${CBUILD}")"
+       rust_host="$(rust_abi "${CHOST}")"
 
        local cm_btype="$(usex debug DEBUG RELEASE)"
        cat <<- _EOF_ > "${S}"/config.toml
@@ -380,17 +395,24 @@ src_configure() {
                enable-warnings = false
                [llvm.build-config]
                CMAKE_VERBOSE_MAKEFILE = "ON"
-               CMAKE_C_FLAGS_${cm_btype} = "${CFLAGS}"
-               CMAKE_CXX_FLAGS_${cm_btype} = "${CXXFLAGS}"
-               CMAKE_EXE_LINKER_FLAGS_${cm_btype} = "${LDFLAGS}"
-               CMAKE_MODULE_LINKER_FLAGS_${cm_btype} = "${LDFLAGS}"
-               CMAKE_SHARED_LINKER_FLAGS_${cm_btype} = "${LDFLAGS}"
-               CMAKE_STATIC_LINKER_FLAGS_${cm_btype} = "${ARFLAGS}"
+               $(if ! tc-is-cross-compiler; then
+                       # When cross-compiling, LLVM is compiled twice, once 
for host and
+                       # once for target.  Unfortunately, this build 
configuration applies
+                       # to both, which means any flags applicable to one 
target but not
+                       # the other will break.  Conditionally disable 
respecting user
+                       # flags when cross-compiling.
+                       echo "CMAKE_C_FLAGS_${cm_btype} = \"${CFLAGS}\""
+                       echo "CMAKE_CXX_FLAGS_${cm_btype} = \"${CXXFLAGS}\""
+                       echo "CMAKE_EXE_LINKER_FLAGS_${cm_btype} = 
\"${LDFLAGS}\""
+                       echo "CMAKE_MODULE_LINKER_FLAGS_${cm_btype} = 
\"${LDFLAGS}\""
+                       echo "CMAKE_SHARED_LINKER_FLAGS_${cm_btype} = 
\"${LDFLAGS}\""
+                       echo "CMAKE_STATIC_LINKER_FLAGS_${cm_btype} = 
\"${ARFLAGS}\""
+               fi)
                [build]
                build-stage = 2
                test-stage = 2
-               build = "${rust_target}"
-               host = ["${rust_target}"]
+               build = "${rust_build}"
+               host = ["${rust_host}"]
                target = [${rust_targets}]
                cargo = "${rust_stage0_root}/bin/cargo"
                rustc = "${rust_stage0_root}/bin/rustc"
@@ -428,7 +450,9 @@ src_configure() {
                debuginfo-level-tests = 0
                backtrace = true
                incremental = false
-               default-linker = "$(tc-getCC)"
+               $(if ! tc-is-cross-compiler; then
+                       echo "default-linker = \"$(tc-getCC)\""
+               fi)
                parallel-compiler = $(toml_usex parallel-compiler)
                channel = "$(usex nightly nightly stable)"
                description = "gentoo"

Reply via email to