When using gcc without cross compiling, i.e., `CROSS_COMPILE` unset or empty, the selftests build defaults to the host architecture, i.e., it uses plain gcc. However, when compiling with clang an unset `ARCH` variable in combination with an unset `CROSS_COMPILE` variable, i.e., compiling for the host architecture, leads to compilation failures since `lib.mk` can not determine the clang target triple. In this case, the following error message is displayed for each subsystem that does not set `ARCH` in its own Makefile before including `lib.mk` (lines wrapped at 75 chrs):
make[1]: Entering directory '/mnt/build/linux/tools/testing/selftests/ sysctl' ../lib.mk:33: *** Specify CROSS_COMPILE or add '--target=' option to lib.mk. Stop. make[1]: Leaving directory '/mnt/build/linux/tools/testing/selftests/ sysctl'
Align the behavior for gcc and clang builds by interpreting unset `ARCH` and `CROSS_COMPILE` variables in `LLVM` builds as a sign that the user wants to build for the host architecture.
This preserves the property that setting the `ARCH` variable to an unknown value will trigger an error that complains about insufficient information.
RFC since I am not entirely sure if this behavior is in fact known and intended, and whether the way to obtain the host target triple is sufficiently general. (The flag was introduced in llvm-8 with [1], it will be an error for older clang versions, however, currently 13.0.1 is the minimal version required to build the kernel. For some clang binaries it prints the 'unknown' instead of the 'linux' version of the target, e.g., mips [2]). An alternative could be to simply do:
ARCH ?= $(shell uname -m)
before using it to select the target. Possibly with some post processing, but at that point we would likely be replicating `scripts/subarch.include`.
Also unsure if it needs a 'Fixes: 795285ef2425 ("selftests: Fix clang cross compilation")'. Furthermore, this change might make it possible to remove the explicit setting of `ARCH` from the few subsystem Makefiles that do it.
Would be happy to get some feedback on those points. If it looks OK I can also send it as a patch.
Link: https://reviews.llvm.org/D50755 [1] Link: https://godbolt.org/z/r7Gn9bvv1 [2] Signed-off-by: Valentin Obst kernel@valentinobst.de --- tools/testing/selftests/lib.mk | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/lib.mk b/tools/testing/selftests/lib.mk index aa646e0661f3..a8f0442a36bc 100644 --- a/tools/testing/selftests/lib.mk +++ b/tools/testing/selftests/lib.mk @@ -7,6 +7,8 @@ else ifneq ($(filter -%,$(LLVM)),) LLVM_SUFFIX := $(LLVM) endif
+CLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) + CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu CLANG_TARGET_FLAGS_hexagon := hexagon-linux-musl @@ -18,7 +20,13 @@ CLANG_TARGET_FLAGS_riscv := riscv64-linux-gnu CLANG_TARGET_FLAGS_s390 := s390x-linux-gnu CLANG_TARGET_FLAGS_x86 := x86_64-linux-gnu CLANG_TARGET_FLAGS_x86_64 := x86_64-linux-gnu -CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH)) + +# Default to host architecture if ARCH is not explicitly given. +ifeq ($(ARCH),) +CLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple) +else +CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH)) +endif
ifeq ($(CROSS_COMPILE),) ifeq ($(CLANG_TARGET_FLAGS),) @@ -30,7 +38,7 @@ else CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%)) endif # CROSS_COMPILE
-CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) $(CLANG_FLAGS) -fintegrated-as +CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as else CC := $(CROSS_COMPILE)gcc endif # LLVM
--- base-commit: d206a76d7d2726f3b096037f2079ce0bd3ba329b change-id: 20240303-selftests-libmk-llvm-rfc-5fe3cfa9f094
Best regards, -- Valentin Obst kernel@valentinobst.de