public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: James Le Cuirot <chewi@gentoo.org>
To: gentoo-dev <gentoo-dev@lists.gentoo.org>
Cc: rust@gentoo.org, James Le Cuirot <chewi@gentoo.org>
Subject: [gentoo-dev] [PATCH 2/2] cargo.eclass: Preserve project-specific [build] flags by reading config
Date: Wed,  7 Aug 2024 16:52:12 +0100	[thread overview]
Message-ID: <20240807160221.1035675-3-chewi@gentoo.org> (raw)
In-Reply-To: <20240807160221.1035675-1-chewi@gentoo.org>

The flags we set an a target-specific environment variable override any
generic [build] flags set by the project, requiring ebuilds to set these
themselves, which is undesirable. Work around this by using tomlq to
read the flags from the config files checked by Cargo and prepending
them to our environment variable.

Signed-off-by: James Le Cuirot <chewi@gentoo.org>
---
 eclass/cargo.eclass | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index 6d4cf1b425eb..dea8c49e4585 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -36,7 +36,8 @@ esac
 
 inherit flag-o-matic multiprocessing rust-toolchain toolchain-funcs
 
-[[ ! ${CARGO_OPTIONAL} ]] && BDEPEND="${RUST_DEPEND}"
+# app-misc/yq is needed for tomlq.
+[[ ! ${CARGO_OPTIONAL} ]] && BDEPEND="${RUST_DEPEND} app-misc/yq"
 
 IUSE="${IUSE} debug"
 
@@ -566,21 +567,26 @@ cargo_env() {
 	# It has been common for users and ebuilds to set RUSTFLAGS, which would
 	# have overridden whatever a project sets anyway, so the least-worst option
 	# is to include those RUSTFLAGS in target-specific config here, which will
-	# merge with any the project sets. Only flags in generic [build] config set
-	# by the project will be lost, and ebuilds will need to add those to
-	# RUSTFLAGS themselves if they are important.
-	#
-	# We could potentially inspect a project's generic [build] config and
-	# reapply those flags ourselves, but that would require a proper toml parser
-	# like tomlq, it might lead to confusion where projects also have
-	# target-specific config, and converting arrays to strings may not work
-	# well. Nightly features to inspect the config might help here in future.
+	# merge with any the project sets. Flags in generic [build] config set by
+	# the project would then be overridden, but we preserve them by reading them
+	# from config files and prepending them to RUSTFLAGS.
 	#
 	# As of Rust 1.80, it is not possible to set separate flags for the build
 	# host and the target host when cross-compiling. The flags given are applied
 	# to the target host only with no flags being applied to the build host. The
 	# nightly host-config feature will improve this situation later.
-	#
+
+	# Iterate over all the config.toml files that Cargo will read except for our
+	# own, prepending any build.rustflags to our environment variable so that
+	# they don't get overridden by our other flags. Skip any flags with an
+	# explicit space in them because we cannot handle those via our variable.
+	local DIR=${PWD}
+	while true; do
+		RUSTFLAGS="$(tomlq -r '.build.rustflags | if type == "array" then map(select(contains(" ") | not)) else [.] end | join(" ")' "${DIR}"/.cargo/config.toml 2>/dev/null) ${RUSTFLAGS# }"
+		[[ -d ${DIR} ]] || break
+		DIR=${DIR%/*}
+	done
+
 	# 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.
-- 
2.45.2



  parent reply	other threads:[~2024-08-07 16:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-07 15:52 [gentoo-dev] [PATCH 0/2] cargo.eclass: Change RUSTFLAGS approach following recent build failures James Le Cuirot
2024-08-07 15:52 ` [gentoo-dev] [PATCH 1/2] " James Le Cuirot
2024-08-07 15:52 ` James Le Cuirot [this message]
2024-08-07 17:39   ` [gentoo-dev] [PATCH 2/2] cargo.eclass: Preserve project-specific [build] flags by reading config Michał Górny
2024-08-07 20:19     ` James Le Cuirot
2024-08-07 22:42       ` Ionen Wolkens
2024-08-08  2:17       ` Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240807160221.1035675-3-chewi@gentoo.org \
    --to=chewi@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    --cc=rust@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox