[PATCH v3 1/2] kbuild: Introduce CONFIG_CROSS_COMPILING

From: Nathan Chancellor

Date: Wed Nov 25 2026 - 01:43:48 EST


There are times where decisions may want to be made within Kconfig when
cross compiling. Introduce a small script to call '$(HOSTCC) -v' and
'$(CC) -v' and compare the first part of the target triples to see if
they match.

This check is cheap but approximate, as Kconfig does not have all the
information available such as endianness and bit width for the target to
pass along to compiler. For example, a user building for x86 with a
compiler that can build both with '-m32' / '-m64' may have
CONFIG_CROSS_COMPILING set depending on the default value of the
toolchain and what's being targeted. Should this become problematic in
the future, the logic can be expanded to cover these corner cases but
for now, keep the implementation simple and cover the majority of cases.
Leave some informational prints to make it easy to debug future reports
with

$ make ... run-command KBUILD_RUN_COMMAND='$(srctree)/scripts/check-cross-compiling.sh "$(HOSTCC)" "$(CC) $(CLANG_FLAGS)"'

Cc: stable@xxxxxxxxxxxxxxx # dependency of future change
Suggested-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Link: https://lore.kernel.org/CAHk-=wgmkoWHWwaQ6nsGoCiBp0hrpJuiwmoVbQ=Efun8JyUNKw@xxxxxxxxxxxxxx/
Signed-off-by: Nathan Chancellor <nathan@xxxxxxxxxx>
---
MAINTAINERS | 1 +
scripts/Kconfig.toolchain | 3 +++
scripts/check-cross-compiling.sh | 26 ++++++++++++++++++++++++++
3 files changed, 30 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..fb76f9e82f65 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14454,6 +14454,7 @@ F: scripts/Kbuild*
F: scripts/Makefile*
F: scripts/bash-completion/
F: scripts/basic/
+F: scripts/check-cross-compiling.sh
F: scripts/clang-tools/
F: scripts/container
F: scripts/dummy-tools/
diff --git a/scripts/Kconfig.toolchain b/scripts/Kconfig.toolchain
index 2acbd40a38f1..ac2e3de50ac0 100644
--- a/scripts/Kconfig.toolchain
+++ b/scripts/Kconfig.toolchain
@@ -32,6 +32,9 @@ config CLANG_VERSION
default $(cc-version) if CC_IS_CLANG
default 0

+config CROSS_COMPILING
+ def_bool $(success,$(srctree)/scripts/check-cross-compiling.sh "$(HOSTCC)" "$(CC) $(CLANG_FLAGS)")
+
config AS_IS_GNU
def_bool $(success,test "$(as-name)" = GNU)

diff --git a/scripts/check-cross-compiling.sh b/scripts/check-cross-compiling.sh
new file mode 100755
index 000000000000..4c1e6a061b3b
--- /dev/null
+++ b/scripts/check-cross-compiling.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+HOSTCC="$1"
+CC="$2"
+
+get_target() {
+ "$@" -v 2>&1 | grep -o 'Target: \([^-]\+\)'
+}
+
+if ! hostcc_target=$(get_target $HOSTCC); then
+ echo >&2 "$0: Could not get target for HOSTCC!"
+ exit 1
+fi
+
+if ! cc_target=$(get_target $CC); then
+ echo >&2 "$0: Could not get target for CC!"
+ exit 1
+fi
+
+echo "HOSTCC: '$hostcc_target'"
+echo "CC: '$cc_target'"
+
+if [ "$hostcc_target" = "$cc_target" ]; then
+ exit 1
+fi

--
2.56.1