commit:     3de1dd6e7da09404b32449eed5f19af12d72b9e3
Author:     James Le Cuirot <chewi <AT> gentoo <DOT> org>
AuthorDate: Sat Jan 17 23:16:53 2026 +0000
Commit:     James Le Cuirot <chewi <AT> gentoo <DOT> org>
CommitDate: Mon Jan 19 22:54:16 2026 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=3de1dd6e

meson.eclass: Add meson_add_machine_file and meson_add_native_file helpers

Sometimes it is necessary to give Meson configuration beyond what the
eclass currently writes to the .ini files. Multiple native and cross
files can be specified, and Meson will use them all. The only rule is
that you cannot repeat a section within the same file.

The given argument is used to form the filename for the .ini file. It is
important for the filename to be unique because timestamp changes will
trigger a reconfigure.

meson_add_machine_file uses either --native-file or --cross-file depending on
whether we're cross-compiling (including multilib) or not.

Signed-off-by: James Le Cuirot <chewi <AT> gentoo.org>

 eclass/meson.eclass | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/eclass/meson.eclass b/eclass/meson.eclass
index d43eccaa5cc7..e07e4b879832 100644
--- a/eclass/meson.eclass
+++ b/eclass/meson.eclass
@@ -76,8 +76,8 @@ BDEPEND=">=dev-build/meson-1.2.3
 # @VARIABLE: emesonargs
 # @DEFAULT_UNSET
 # @DESCRIPTION:
-# Optional meson arguments as Bash array; this should be defined before
-# calling meson_src_configure.
+# Optional meson arguments as Bash array; this should be defined before calling
+# meson_src_configure, meson_add_machine_file, or meson_add_native_file.
 
 # @VARIABLE: MYMESONARGS
 # @DEFAULT_UNSET
@@ -271,6 +271,33 @@ _meson_create_native_file() {
        echo "${fn}"
 }
 
+# @FUNCTION: meson_add_machine_file
+# @USAGE: <name> < <data>
+# @DESCRIPTION:
+# Appends --native-file or --cross-file to emesonargs with the data given via
+# standard input. Assumes emesonargs has already been defined as an array.
+meson_add_machine_file() {
+       local file=${T}/meson.${CHOST}.${ABI}.${1}.ini
+       cat > "${file}" || die
+
+       if tc-is-cross-compiler || [[ ${ABI} != "${DEFAULT_ABI}" ]]; then
+               emesonargs+=( --cross-file "${file}" )
+       else
+               emesonargs+=( --native-file "${file}" )
+       fi
+}
+
+# @FUNCTION: meson_add_native_file
+# @USAGE: <name> < <data>
+# @DESCRIPTION:
+# Appends --native-file (never --cross-file) to emesonargs with the data given
+# via standard input. Assumes emesonargs has already been defined as an array.
+meson_add_native_file() {
+       # Ensure the filename is unique by including the target tuple and ABI.
+       local name=${CHOST}.${ABI}.${1} CHOST=${CBUILD} ABI=${DEFAULT_ABI}
+       meson_add_machine_file "${name}"
+}
+
 # @FUNCTION: meson_use
 # @USAGE: <USE flag> [option name]
 # @DESCRIPTION:

Reply via email to