diff mbox series

[2/2] rust-common.bbclass: filter out incorrect compiler flags in wrapper

Message ID 20251120121947.3473848-3-skandigraun@gmail.com
State New
Headers show
Series -fcanon-file-prefix and cc-rs compiler flag mixing | expand

Commit Message

Gyorgy Sarvari Nov. 20, 2025, 12:19 p.m. UTC
This patch is a workaround for https://bugzilla.yoctoproject.org/show_bug.cgi?id=15976

cc-rs crate is used by a number of projects, including rust's bootstrap also
to invoke the systems c/c++ compiler.

A few updates ago it has changed the way CFLAGS/CXXFLAGS are handled: it now
merges them with HOST_C*FLAGS and TARGET_C*FLAGS.

This is a problem when a recipe is cross compiled, but it has a build dependency
which uses this crate to be built. In this case the C*FLAGS variable contains
target flags, while the HOST_C*FLAGS contains host-specific flags, and when the
two are mixed, the output is not what one expects.

This change tries to filter out the incorrect flags:
- If the wrapper is invoked as a c or c++ compiler wrapper,
- then determines if it compiles for host or for target
- Depending on the on above, it considers the TARGET_*FLAGS or HOST*FLAGS
  correct, and the C*FLAGS variable content incorrect.
- It subtracts the correct set from the incorrect set, and drops the
  remainder from the compiler flags. (Which might be often an empty list, so
  the flags are frequently unchanged)

Signed-off-by: Gyorgy Sarvari <skandigraun@gmail.com>
---
 meta/classes-recipe/rust-common.bbclass | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
diff mbox series

Patch

diff --git a/meta/classes-recipe/rust-common.bbclass b/meta/classes-recipe/rust-common.bbclass
index 31331c7a26..c61143a56c 100644
--- a/meta/classes-recipe/rust-common.bbclass
+++ b/meta/classes-recipe/rust-common.bbclass
@@ -148,6 +148,29 @@  create_wrapper_rust () {
 
 	binary = orig_binary.split()[0]
 	args = orig_binary.split() + sys.argv[1:]
+
+	# The following is trying to be a workaround for
+	# https://bugzilla.yoctoproject.org/show_bug.cgi?id=15976
+	# cc-rs crate passes both host AND target flags at the same time
+	# to this script, in case a recipe is cross-compiled, but a crate
+	# is a build dependency, and is compiled for the host.
+	# This tries to filter out the inappropriate flags.
+	if sys.argv[0].endswith("-cc"):
+	    script_type = "C"
+	elif sys.argv[0].endswith("-cxx"):
+	    script_type = "CXX"
+	else:
+	    script_type = "other"
+
+	flags_to_remove = []
+	if script_type != "other":
+	    host_or_target = "HOST" if "build-rust-" in sys.argv[0] else "TARGET"
+	    incorrect_flags = os.getenv("%sFLAGS" % script_type).split()
+	    correct_flags = os.getenv("%s_%sFLAGS" % (host_or_target, script_type)).split()
+	    flags_to_remove = set(incorrect_flags) - set(correct_flags)
+
+	args = list(filter(lambda flag: flag not in flags_to_remove, args))
+
 	if extras:
 	    args.append(extras)
 	os.execvp(binary, args)