summaryrefslogtreecommitdiff
path: root/eclass/cargo.eclass
diff options
context:
space:
mode:
authorV3n3RiX <venerix@koprulu.sector>2024-08-07 12:37:21 +0100
committerV3n3RiX <venerix@koprulu.sector>2024-08-07 12:37:21 +0100
commitb8c7370a682e4e29cda623222d17a790c01c3642 (patch)
treef6caa14689bd00a5760eadaa381ff41e50ef3c1b /eclass/cargo.eclass
parent8a4997a7e2d1e36c089d4d76935b5a902d98d3d0 (diff)
gentoo auto-resync : 07:08:2024 - 12:37:20
Diffstat (limited to 'eclass/cargo.eclass')
-rw-r--r--eclass/cargo.eclass93
1 files changed, 65 insertions, 28 deletions
diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index 7db34efb4e17..c360c2a6c419 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -109,7 +109,7 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
#
# If you enable CARGO_OPTIONAL, you have to set BDEPEND on virtual/rust
# for your package and call at least cargo_gen_config manually before using
-# other src_functions of this eclass.
+# other src_functions or cargo_env of this eclass.
# Note that cargo_gen_config is automatically called by cargo_src_unpack.
# @ECLASS_VARIABLE: myfeatures
@@ -248,7 +248,7 @@ cargo_crate_uris() {
# @FUNCTION: cargo_gen_config
# @DESCRIPTION:
-# Generate the $CARGO_HOME/config necessary to use our local registry and settings.
+# Generate the $CARGO_HOME/config.toml necessary to use our local registry and settings.
# Cargo can also be configured through environment variables in addition to the TOML syntax below.
# For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
# can also be used to define the value.
@@ -259,9 +259,22 @@ cargo_crate_uris() {
cargo_gen_config() {
debug-print-function ${FUNCNAME} "$@"
+ # The default linker is "cc" so override by setting linker to CC in the
+ # RUSTFLAGS. The given linker cannot include any arguments, so split these
+ # into link-args along with LDFLAGS. Also include external RUSTFLAGS.
+ # Note that as of Rust 1.80, the build host RUSTFLAGS are ignored when
+ # cross-compiling unless you use the unstable host-config feature available
+ # with USE=nightly. There is no simple way around this.
+ tc-export_build_env
+ local LD_A=( $(tc-getBUILD_CC) ${BUILD_LDFLAGS} )
+ local MY_BUILD_RUSTFLAGS="-C strip=none -C linker=${LD_A[0]}"
+ [[ ${#LD_A[@]} -gt 1 ]] && MY_BUILD_RUSTFLAGS+="$(printf -- ' -C link-arg=%s' "${LD_A[@]:1}")"
+ MY_BUILD_RUSTFLAGS+=" ${RUSTFLAGS} ${BUILD_RUSTFLAGS}"
+ tc-is-cross-compiler || MY_BUILD_RUSTFLAGS+=" ${TARGET_RUSTFLAGS}"
+
mkdir -p "${ECARGO_HOME}" || die
- cat > "${ECARGO_HOME}/config" <<- _EOF_ || die "Failed to create cargo config"
+ cat > "${ECARGO_HOME}/config.toml" <<- _EOF_ || die "Failed to create cargo config"
[source.gentoo]
directory = "${ECARGO_VENDOR}"
@@ -273,6 +286,7 @@ cargo_gen_config() {
offline = true
[build]
+ rustflags = "${MY_BUILD_RUSTFLAGS}"
jobs = $(makeopts_jobs)
incremental = false
@@ -523,36 +537,65 @@ cargo_src_configure() {
[[ ${ECARGO_ARGS[@]} ]] && einfo "Configured with: ${ECARGO_ARGS[@]}"
}
-# @FUNCTION: cargo_src_compile
+# @FUNCTION: cargo_env
+# @USAGE: Command with its arguments
# @DESCRIPTION:
-# Build the package using cargo build.
-cargo_src_compile() {
- debug-print-function ${FUNCNAME} "$@"
-
+# Run the given command under an environment needed for performing tasks with
+# Cargo such as building. RUSTFLAGS is used for both the build and target host.
+# BUILD_RUSTFLAGS and TARGET_RUSTFLAGS are used for just the build host and
+# target host respectively. Ensure these are set consistently between Cargo
+# invocations, otherwise rebuilds will occur.
+cargo_env() {
[[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
+ # Shadow flag variables so that filtering below remains local.
+ local flag
+ for flag in $(all-flag-vars); do
+ local -x "${flag}=${!flag}"
+ done
+
+ # Rust extensions are incompatible with C/C++ LTO compiler see e.g.
+ # https://bugs.gentoo.org/910220
filter-lto
+
tc-export AR CC CXX PKG_CONFIG
+ # Set vars for cc-rs crate.
+ local -x \
+ HOST_AR=$(tc-getBUILD_AR)
+ HOST_CC=$(tc-getBUILD_CC)
+ HOST_CXX=$(tc-getBUILD_CXX)
+ HOST_CFLAGS=${BUILD_CFLAGS}
+ HOST_CXXFLAGS=${BUILD_CXXFLAGS}
+
if tc-is-cross-compiler; then
- export CARGO_BUILD_TARGET=$(rust_abi)
+ local -x CARGO_BUILD_TARGET=$(rust_abi)
local TRIPLE=${CARGO_BUILD_TARGET//-/_}
- export CARGO_TARGET_"${TRIPLE^^}"_LINKER=$(tc-getCC)
-
- # Set vars for cc-rs crate.
- tc-export_build_env
- export \
- HOST_AR=$(tc-getBUILD_AR)
- HOST_CC=$(tc-getBUILD_CC)
- HOST_CXX=$(tc-getBUILD_CXX)
- HOST_CFLAGS=${BUILD_CFLAGS}
- HOST_CXXFLAGS=${BUILD_CXXFLAGS}
+ local TRIPLE=${TRIPLE^^} LD_A=( $(tc-getCC) ${LDFLAGS} )
+ local -x CARGO_TARGET_"${TRIPLE}"_RUSTFLAGS="-C strip=none -C linker=${LD_A[0]}"
+ [[ ${#LD_A[@]} -gt 1 ]] && local CARGO_TARGET_"${TRIPLE}"_RUSTFLAGS+="$(printf -- ' -C link-arg=%s' "${LD_A[@]:1}")"
+ local CARGO_TARGET_"${TRIPLE}"_RUSTFLAGS+=" ${RUSTFLAGS} ${TARGET_RUSTFLAGS}"
fi
+ (
+ # These variables will override the above, even if empty, so unset them
+ # locally. Do this in a subshell so that they remain set afterwards.
+ unset CARGO_BUILD_RUSTFLAGS CARGO_ENCODED_RUSTFLAGS RUSTFLAGS
+
+ "${@}"
+ )
+}
+
+# @FUNCTION: cargo_src_compile
+# @DESCRIPTION:
+# Build the package using cargo build.
+cargo_src_compile() {
+ debug-print-function ${FUNCNAME} "$@"
+
set -- cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
einfo "${@}"
- "${@}" || die "cargo build failed"
+ cargo_env "${@}" || die "cargo build failed"
}
# @FUNCTION: cargo_src_install
@@ -564,16 +607,13 @@ cargo_src_compile() {
cargo_src_install() {
debug-print-function ${FUNCNAME} "$@"
- [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
- die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
-
set -- cargo install $(has --path ${@} || echo --path ./) \
--root "${ED}/usr" \
${GIT_CRATES[@]:+--frozen} \
$(usex debug --debug "") \
${ECARGO_ARGS[@]} "$@"
einfo "${@}"
- "${@}" || die "cargo install failed"
+ cargo_env "${@}" || die "cargo install failed"
rm -f "${ED}/usr/.crates.toml" || die
rm -f "${ED}/usr/.crates2.json" || die
@@ -585,12 +625,9 @@ cargo_src_install() {
cargo_src_test() {
debug-print-function ${FUNCNAME} "$@"
- [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
- die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
-
set -- cargo test $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
einfo "${@}"
- "${@}" || die "cargo test failed"
+ cargo_env "${@}" || die "cargo test failed"
}
fi