From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 3ADE0159C9B for ; Wed, 7 Aug 2024 16:03:28 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 902582BC08A; Wed, 7 Aug 2024 16:02:46 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 467AE2BC086 for ; Wed, 7 Aug 2024 16:02:46 +0000 (UTC) From: James Le Cuirot To: gentoo-dev Cc: rust@gentoo.org, James Le Cuirot 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 Message-ID: <20240807160221.1035675-3-chewi@gentoo.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807160221.1035675-1-chewi@gentoo.org> References: <20240807160221.1035675-1-chewi@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Archives-Salt: 0611cc54-e76a-4bb9-a979-5dc3a2c14f8a X-Archives-Hash: e44fc65487b08c2279e0048f285954ca 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 --- 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