Skip to contentSkip to conversation

Makefile: Use CFLAGS in cc-option

PATCH In review Andrew Jones 3 participants 4 messages Mar 7, 2025 – Apr 8, 2025

Subsystems (5)
KVM Kbuild s390 KVM arm64 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Makefile: Use CFLAGS in cc-option

When cross compiling with clang we need to specify the target in CFLAGS and cc-option will fail to recognize target-specific options without it. Add CFLAGS to the CC invocation in cc-option.

The introduction of the realmode_bits variable is necessary to avoid make failing to build x86 due to CFLAGS referencing itself.

---
v2:
- Fixed x86 builds with the realmode_bits variable

 Makefile            | 2 +-
 x86/Makefile.common | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
Makefile +1 -1
index 78352fced9d4..9dc5d2234e2a 100644
@@ -21,7 +21,7 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
 
 # cc-option
 # Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
-cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
+cc-option = $(shell if $(CC) $(CFLAGS) -Werror $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
 libcflat := lib/libcflat.a
x86/Makefile.common +2 -1
index 0b7f35c8de85..e97464912e28 100644
@@ -98,6 +98,7 @@ tests-common = $(TEST_DIR)/vmexit.$(exe) $(TEST_DIR)/tsc.$(exe) \
 ifneq ($(CONFIG_EFI),y)
 tests-common += $(TEST_DIR)/realmode.$(exe) \
 		$(TEST_DIR)/la57.$(exe)
+realmode_bits := $(if $(call cc-option,-m16,""),16,32)
 endif
 
 test_cases: $(tests-common) $(tests)
@@ -108,7 +109,7 @@ $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o
 	$(LD) -m elf_i386 -nostdlib -o $@ \
 	      -T $(SRCDIR)/$(TEST_DIR)/realmode.lds $^
 
-$(TEST_DIR)/realmode.o: bits = $(if $(call cc-option,-m16,""),16,32)
+$(TEST_DIR)/realmode.o: bits = $(realmode_bits)
 
 $(TEST_DIR)/access_test.$(bin): $(TEST_DIR)/access.o
 
Signature · 1 line
2.48.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 42 lines · Andrew Jones

When cross compiling with clang we need to specify the target in CFLAGS and cc-option will fail to recognize target-specific options without it. Add CFLAGS to the CC invocation in cc-option.

The introduction of the realmode_bits variable is necessary to avoid make failing to build x86 due to CFLAGS referencing itself.

---
v2:
- Fixed x86 builds with the realmode_bits variable

  Makefile            | 2 +-
  x86/Makefile.common | 3 ++-
  2 files changed, 3 insertions(+), 2 deletions(-)
Makefile +1 -1
index 78352fced9d4..9dc5d2234e2a 100644
@@ -21,7 +21,7 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
 
  # cc-option
  # Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
-cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
+cc-option = $(shell if $(CC) $(CFLAGS) -Werror $(1) -S -o /dev/null -xc /dev/null \
                > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
  libcflat := lib/libcflat.a
x86/Makefile.common +1 -0
index 0b7f35c8de85..e97464912e28 100644
@@ -98,6 +98,7 @@ tests-common = $(TEST_DIR)/vmexit.$(exe) $(TEST_DIR)/tsc.$(exe) \
  ifneq ($(CONFIG_EFI),y)
  tests-common += $(TEST_DIR)/realmode.$(exe) \
  		$(TEST_DIR)/la57.$(exe)
+realmode_bits := $(if $(call cc-option,-m16,""),16,32)
  endif
 
  test_cases: $(tests-common) $(tests)

x86/Makefile.common $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o · line 115

Quoted 8 lines
x86/Makefile.common +1 -1
@@ -108,7 +109,7 @@ $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o
  	$(LD) -m elf_i386 -nostdlib -o $@ \
  	      -T $(SRCDIR)/$(TEST_DIR)/realmode.lds $^
 
-$(TEST_DIR)/realmode.o: bits = $(if $(call cc-option,-m16,""),16,32)
+$(TEST_DIR)/realmode.o: bits = $(realmode_bits)
 
  $(TEST_DIR)/access_test.$(bin): $(TEST_DIR)/access.o
 
Tested-by on v2 1/1
#2 lore raw
In reply to Andrew Jones

Hi Drew,

Thank you for debugging this. I tested the patch by compiling the MTE test from Vladimir with clang and it works now:

Thanks,
Alex

Quoted 53 lines · Andrew Jones

When cross compiling with clang we need to specify the target in CFLAGS and cc-option will fail to recognize target-specific options without it. Add CFLAGS to the CC invocation in cc-option.

The introduction of the realmode_bits variable is necessary to avoid make failing to build x86 due to CFLAGS referencing itself.

---
v2:
- Fixed x86 builds with the realmode_bits variable

 Makefile            | 2 +-
 x86/Makefile.common | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
Makefile +1 -1
index 78352fced9d4..9dc5d2234e2a 100644
@@ -21,7 +21,7 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
 
 # cc-option
 # Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
-cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
+cc-option = $(shell if $(CC) $(CFLAGS) -Werror $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
 libcflat := lib/libcflat.a
x86/Makefile.common +2 -1
index 0b7f35c8de85..e97464912e28 100644
@@ -98,6 +98,7 @@ tests-common = $(TEST_DIR)/vmexit.$(exe) $(TEST_DIR)/tsc.$(exe) \
 ifneq ($(CONFIG_EFI),y)
 tests-common += $(TEST_DIR)/realmode.$(exe) \
 		$(TEST_DIR)/la57.$(exe)
+realmode_bits := $(if $(call cc-option,-m16,""),16,32)
 endif
 
 test_cases: $(tests-common) $(tests)
@@ -108,7 +109,7 @@ $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o
 	$(LD) -m elf_i386 -nostdlib -o $@ \
 	      -T $(SRCDIR)/$(TEST_DIR)/realmode.lds $^
 
-$(TEST_DIR)/realmode.o: bits = $(if $(call cc-option,-m16,""),16,32)
+$(TEST_DIR)/realmode.o: bits = $(realmode_bits)
 
 $(TEST_DIR)/access_test.$(bin): $(TEST_DIR)/access.o
 
Signature · 1 line
2.48.1
#3 lore raw
In reply to Andrew Jones
Quoted 53 lines · Andrew Jones

When cross compiling with clang we need to specify the target in CFLAGS and cc-option will fail to recognize target-specific options without it. Add CFLAGS to the CC invocation in cc-option.

The introduction of the realmode_bits variable is necessary to avoid make failing to build x86 due to CFLAGS referencing itself.

---
v2:
- Fixed x86 builds with the realmode_bits variable

 Makefile            | 2 +-
 x86/Makefile.common | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
Makefile +1 -1
index 78352fced9d4..9dc5d2234e2a 100644
@@ -21,7 +21,7 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
 
 # cc-option
 # Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
-cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
+cc-option = $(shell if $(CC) $(CFLAGS) -Werror $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
 libcflat := lib/libcflat.a
x86/Makefile.common +2 -1
index 0b7f35c8de85..e97464912e28 100644
@@ -98,6 +98,7 @@ tests-common = $(TEST_DIR)/vmexit.$(exe) $(TEST_DIR)/tsc.$(exe) \
 ifneq ($(CONFIG_EFI),y)
 tests-common += $(TEST_DIR)/realmode.$(exe) \
 		$(TEST_DIR)/la57.$(exe)
+realmode_bits := $(if $(call cc-option,-m16,""),16,32)
 endif
 
 test_cases: $(tests-common) $(tests)
@@ -108,7 +109,7 @@ $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o
 	$(LD) -m elf_i386 -nostdlib -o $@ \
 	      -T $(SRCDIR)/$(TEST_DIR)/realmode.lds $^
 
-$(TEST_DIR)/realmode.o: bits = $(if $(call cc-option,-m16,""),16,32)
+$(TEST_DIR)/realmode.o: bits = $(realmode_bits)
 
 $(TEST_DIR)/access_test.$(bin): $(TEST_DIR)/access.o
 
Signature · 1 line
2.48.1

Merged through arm/queue.

Thanks,
drew