Skip to contentSkip to conversation

Support cross compiling with clang

PATCH Superseded Andrew Jones 3 participants 13 messages Sep 4, 2024 – Sep 11, 2024

Subsystems (6)
KVM Kbuild RISC-V s390 +2 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Support cross compiling with clang

Modify configure to allow --cc=clang and a cross-prefix to be specified together (as well as --cflags). This allows compiling with clang, but using cross binutils for everything else, including the linker. So far tested on riscv 32- and 64-bit and aarch64 (with some hacks to the code to get it to compile - which is why there's no gitlab-ci patch for aarch64 in this series). I suspect it should work for other architectures too.

v2:
- fix building with clang and --config-efi by suppressing a warning
- added riscv clang efi build to CI
- picked up Thomas's tags

Andrew Jones (4):
riscv: Drop mstrict-align
Makefile: Prepare for clang EFI builds
configure: Support cross compiling with clang
riscv: gitlab-ci: Add clang build tests

 .gitlab-ci.yml | 43 +++++++++++++++++++++++++++++++++++++++++++
 Makefile       |  2 ++
 configure      | 11 ++++++++---
 riscv/Makefile |  2 +-
 4 files changed, 54 insertions(+), 4 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/4 riscv: Drop mstrict-align

The spec says unaligned accesses are supported, so this isn't required and clang doesn't support it. A platform might have slow unaligned accesses, but kvm-unit-tests isn't about speed anyway.

---
 riscv/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
riscv/Makefile +1 -1
index 179a373dbacf..2ee7c5bb5ad8 100644
@@ -76,7 +76,7 @@ LDFLAGS += -melf32lriscv
 endif
 CFLAGS += -DCONFIG_RELOC
 CFLAGS += -mcmodel=medany
-CFLAGS += -mstrict-align
+#CFLAGS += -mstrict-align
 CFLAGS += -std=gnu99
 CFLAGS += -ffreestanding
 CFLAGS += -O2
Signature · 1 line
2.46.0
#2 lore raw

Re: [kvm-unit-tests PATCH v2 1/4] riscv: Drop mstrict-align

Quoted 15 lines · Andrew Jones

The spec says unaligned accesses are supported, so this isn't required and clang doesn't support it. A platform might have slow unaligned accesses, but kvm-unit-tests isn't about speed anyway.

---
 riscv/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
riscv/Makefile +0 -0
index 179a373dbacf..2ee7c5bb5ad8 100644

riscv/Makefile LDFLAGS += -melf32lriscv · line 79

Quoted 5 lines
riscv/Makefile +1 -1
@@ -76,7 +76,7 @@ LDFLAGS += -melf32lriscv
 endif
 CFLAGS += -DCONFIG_RELOC
 CFLAGS += -mcmodel=medany
-CFLAGS += -mstrict-align
+#CFLAGS += -mstrict-align

Just remove the line?

Or put a comment there instead to explain.

Thanks,
Nick

#3 lore raw

Re: [kvm-unit-tests PATCH v2 1/4] riscv: Drop mstrict-align

Quoted 24 lines · Nicholas Piggin
Quoted 15 lines · Andrew Jones

The spec says unaligned accesses are supported, so this isn't required and clang doesn't support it. A platform might have slow unaligned accesses, but kvm-unit-tests isn't about speed anyway.

---
 riscv/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
riscv/Makefile +0 -0
index 179a373dbacf..2ee7c5bb5ad8 100644

riscv/Makefile LDFLAGS += -melf32lriscv · line 79

Quoted 5 lines
riscv/Makefile +1 -1
@@ -76,7 +76,7 @@ LDFLAGS += -melf32lriscv
 endif
 CFLAGS += -DCONFIG_RELOC
 CFLAGS += -mcmodel=medany
-CFLAGS += -mstrict-align
+#CFLAGS += -mstrict-align

Just remove the line?

Quoted 1 line

Or put a comment there instead to explain.

I'll add a comment.

Thanks,
drew

#4 lore raw

PATCH v2 2/4 Makefile: Prepare for clang EFI builds

In reply to Andrew Jones

clang complains about GNU extensions such as variable sized types not being at the end of structs unless -Wno-gnu is used. We may eventually want -Wno-gnu, but for now let's just handle the warnings as they come. Add -Wno-gnu-variable-sized-type-not-at-end to avoid the warning issued for the initrd_dev_path struct.

---
 Makefile | 2 ++
 1 file changed, 2 insertions(+)
Makefile +2 -0
index 3d51cb726120..7471f7285b78 100644
@@ -50,6 +50,8 @@ EFI_CFLAGS += -fshort-wchar
 # EFI applications use PIC as they are loaded to dynamic addresses, not a fixed
 # starting address
 EFI_CFLAGS += -fPIC
+# Avoid error with the initrd_dev_path struct
+EFI_CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
 # Create shared library
 EFI_LDFLAGS := -Bsymbolic -shared -nostdlib
 endif
Signature · 1 line
2.46.0
Reviewed-by on v2 2/4
#5 lore raw

Re: [kvm-unit-tests PATCH v2 2/4] Makefile: Prepare for clang EFI builds

Quoted 16 lines · Andrew Jones

clang complains about GNU extensions such as variable sized types not being at the end of structs unless -Wno-gnu is used. We may eventually want -Wno-gnu, but for now let's just handle the warnings as they come. Add -Wno-gnu-variable-sized-type-not-at-end to avoid the warning issued for the initrd_dev_path struct.

---
  Makefile | 2 ++
  1 file changed, 2 insertions(+)
Makefile +0 -0
index 3d51cb726120..7471f7285b78 100644

Makefile EFI_CFLAGS += -fshort-wchar · line 57

Quoted 8 lines
Makefile +2 -0
@@ -50,6 +50,8 @@ EFI_CFLAGS += -fshort-wchar
  # EFI applications use PIC as they are loaded to dynamic addresses, not a fixed
  # starting address
  EFI_CFLAGS += -fPIC
+# Avoid error with the initrd_dev_path struct
+EFI_CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
  # Create shared library
  EFI_LDFLAGS := -Bsymbolic -shared -nostdlib
  endif
Reviewed-by on v2 2/4
#6 lore raw

Re: [kvm-unit-tests PATCH v2 2/4] Makefile: Prepare for clang EFI builds

In reply to Andrew Jones
Quoted 5 lines · Andrew Jones

clang complains about GNU extensions such as variable sized types not being at the end of structs unless -Wno-gnu is used. We may eventually want -Wno-gnu, but for now let's just handle the warnings as they come. Add -Wno-gnu-variable-sized-type-not-at-end to avoid the warning issued for the initrd_dev_path struct.

You could also make a variant of struct efi_vendor_dev_path with no vendordata just for initrd_dev_path?

It's taken from Linux or some efi upstream though so maybe it's annoying to make such changes here. Okay in that case since it's limited to EFI.

Quoted 19 lines
---
 Makefile | 2 ++
 1 file changed, 2 insertions(+)
Makefile +2 -0
index 3d51cb726120..7471f7285b78 100644
@@ -50,6 +50,8 @@ EFI_CFLAGS += -fshort-wchar
 # EFI applications use PIC as they are loaded to dynamic addresses, not a fixed
 # starting address
 EFI_CFLAGS += -fPIC
+# Avoid error with the initrd_dev_path struct
+EFI_CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
 # Create shared library
 EFI_LDFLAGS := -Bsymbolic -shared -nostdlib
 endif
#7 lore raw

Re: [kvm-unit-tests PATCH v2 2/4] Makefile: Prepare for clang EFI builds

Quoted 7 lines · Nicholas Piggin
Quoted 5 lines · Andrew Jones

clang complains about GNU extensions such as variable sized types not being at the end of structs unless -Wno-gnu is used. We may eventually want -Wno-gnu, but for now let's just handle the warnings as they come. Add -Wno-gnu-variable-sized-type-not-at-end to avoid the warning issued for the initrd_dev_path struct.

Quoted 5 lines

You could also make a variant of struct efi_vendor_dev_path with no vendordata just for initrd_dev_path?

It's taken from Linux or some efi upstream though so maybe it's annoying to make such changes here. Okay in that case since it's limited to EFI.

I should have mentioned in the commit message that efi_vendor_dev_path is a direct import of the Linux code and that we'd rather keep it that way. I can do that for v2.

Quoted 2 lines

Thanks,
drew

Quoted 27 lines
Quoted 21 lines
---
 Makefile | 2 ++
 1 file changed, 2 insertions(+)
Makefile +2 -0
index 3d51cb726120..7471f7285b78 100644
@@ -50,6 +50,8 @@ EFI_CFLAGS += -fshort-wchar
 # EFI applications use PIC as they are loaded to dynamic addresses, not a fixed
 # starting address
 EFI_CFLAGS += -fPIC
+# Avoid error with the initrd_dev_path struct
+EFI_CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
 # Create shared library
 EFI_LDFLAGS := -Bsymbolic -shared -nostdlib
 endif
Signature · 3 lines
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#8 lore raw

PATCH v2 3/4 configure: Support cross compiling with clang

In reply to Andrew Jones

When a user specifies the compiler with --cc assume it's already fully named, even if the user also specifies a cross-prefix. This allows clang to be selected for the compiler, which doesn't use prefixes, but also still provide a cross prefix for binutils. If a user needs a prefix on the compiler that they specify with --cc, then they'll just have to specify it with the prefix prepended.

Also ensure user provided cflags are used when testing the compiler, since the flags may drastically change behavior, such as the --target flag for clang.

With these changes it's possible to cross compile for riscv with clang after configuring with

 ./configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' \
             --cross-prefix=riscv64-linux-gnu-
---
 configure | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
configure +8 -3
index 27ae9cc89657..337af07374df 100755
@@ -130,6 +130,7 @@ while [[ "$1" = -* ]]; do
 	    ;;
 	--cc)
 	    cc="$arg"
+	    cc_selected=yes
 	    ;;
 	--cflags)
 	    cflags="$arg"
@@ -200,6 +201,10 @@ while [[ "$1" = -* ]]; do
     esac
 done
 
+if [ -z "$cc_selected" ] && [ "$cross_prefix" ]; then
+    cc="$cross_prefix$cc"
+fi
+
 if [ -z "$efi" ] || [ "$efi" = "n" ]; then
     [ "$efi_direct" = "y" ] && efi_direct=
 fi
@@ -370,7 +375,7 @@ fi
 cat << EOF > lib-test.c
 __UINT32_TYPE__
 EOF
-u32_long=$("$cross_prefix$cc" -E lib-test.c | grep -v '^#' | grep -q long && echo yes)
+u32_long=$("$cc" $cflags -E lib-test.c | grep -v '^#' | grep -q long && echo yes)
 rm -f lib-test.c
 
 # check if slash can be used for division
@@ -379,7 +384,7 @@ if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then
 foo:
     movl (8 / 2), %eax
 EOF
-  wa_divide=$("$cross_prefix$cc" -c lib-test.S >/dev/null 2>&1 || echo yes)
+  wa_divide=$("$cc" $cflags -c lib-test.S >/dev/null 2>&1 || echo yes)
   rm -f lib-test.{o,S}
 fi
 
@@ -442,7 +447,7 @@ ARCH=$arch
 ARCH_NAME=$arch_name
 ARCH_LIBDIR=$arch_libdir
 PROCESSOR=$processor
-CC=$cross_prefix$cc
+CC=$cc
 CFLAGS=$cflags
 LD=$cross_prefix$ld
 OBJCOPY=$cross_prefix$objcopy
Signature · 1 line
2.46.0
Reviewed-by on v2 3/4
#9 lore raw

Re: [kvm-unit-tests PATCH v2 3/4] configure: Support cross compiling with clang

Quoted 6 lines · Andrew Jones

When a user specifies the compiler with --cc assume it's already fully named, even if the user also specifies a cross-prefix. This allows clang to be selected for the compiler, which doesn't use prefixes, but also still provide a cross prefix for binutils. If a user needs a prefix on the compiler that they specify with --cc, then they'll just have to specify it with the prefix prepended.

Makes sense.

Quoted 3 lines

Also ensure user provided cflags are used when testing the compiler, since the flags may drastically change behavior, such as the --target flag for clang.

Could be a separate patch but no big deal.

Quoted 6 lines

With these changes it's possible to cross compile for riscv with clang after configuring with

 ./configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' \
             --cross-prefix=riscv64-linux-gnu-

Nice. Perhaps add a recipe to README?

Thanks,
Nick

Quoted 57 lines
---
 configure | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
configure +8 -3
index 27ae9cc89657..337af07374df 100755
@@ -130,6 +130,7 @@ while [[ "$1" = -* ]]; do
 	    ;;
 	--cc)
 	    cc="$arg"
+	    cc_selected=yes
 	    ;;
 	--cflags)
 	    cflags="$arg"
@@ -200,6 +201,10 @@ while [[ "$1" = -* ]]; do
     esac
 done
 
+if [ -z "$cc_selected" ] && [ "$cross_prefix" ]; then
+    cc="$cross_prefix$cc"
+fi
+
 if [ -z "$efi" ] || [ "$efi" = "n" ]; then
     [ "$efi_direct" = "y" ] && efi_direct=
 fi
@@ -370,7 +375,7 @@ fi
 cat << EOF > lib-test.c
 __UINT32_TYPE__
 EOF
-u32_long=$("$cross_prefix$cc" -E lib-test.c | grep -v '^#' | grep -q long && echo yes)
+u32_long=$("$cc" $cflags -E lib-test.c | grep -v '^#' | grep -q long && echo yes)
 rm -f lib-test.c
 
 # check if slash can be used for division
@@ -379,7 +384,7 @@ if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then
 foo:
     movl (8 / 2), %eax
 EOF
-  wa_divide=$("$cross_prefix$cc" -c lib-test.S >/dev/null 2>&1 || echo yes)
+  wa_divide=$("$cc" $cflags -c lib-test.S >/dev/null 2>&1 || echo yes)
   rm -f lib-test.{o,S}
 fi
 
@@ -442,7 +447,7 @@ ARCH=$arch
 ARCH_NAME=$arch_name
 ARCH_LIBDIR=$arch_libdir
 PROCESSOR=$processor
-CC=$cross_prefix$cc
+CC=$cc
 CFLAGS=$cflags
 LD=$cross_prefix$ld
 OBJCOPY=$cross_prefix$objcopy
#10 lore raw

Re: [kvm-unit-tests PATCH v2 3/4] configure: Support cross compiling with clang

Quoted 17 lines · Nicholas Piggin
Quoted 6 lines · Andrew Jones

When a user specifies the compiler with --cc assume it's already fully named, even if the user also specifies a cross-prefix. This allows clang to be selected for the compiler, which doesn't use prefixes, but also still provide a cross prefix for binutils. If a user needs a prefix on the compiler that they specify with --cc, then they'll just have to specify it with the prefix prepended.

Makes sense.

Quoted 3 lines

Also ensure user provided cflags are used when testing the compiler, since the flags may drastically change behavior, such as the --target flag for clang.

Could be a separate patch but no big deal.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

With these changes it's possible to cross compile for riscv with clang after configuring with

 ./configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' \
             --cross-prefix=riscv64-linux-gnu-

Nice. Perhaps add a recipe to README?

Sure.

Quoted 2 lines

Thanks,
drew

#11 lore raw

PATCH v2 4/4 riscv: gitlab-ci: Add clang build tests

In reply to Andrew Jones

Test building 32 and 64-bit with clang. Throw a test of in- and out- of-tree building in too by swapping which is done to which (32-bit vs. 64-bit) with respect to the gcc build tests.

---
 .gitlab-ci.yml | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
.gitlab-ci.yml +43 -0
index 67a9a15733f1..b7ad99870e5a 100644
@@ -176,6 +176,49 @@ build-riscv64-efi:
       | tee results.txt
  - grep -q PASS results.txt && ! grep -q FAIL results.txt
 
+build-riscv32-clang:
+ extends: .intree_template
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu clang
+ - ./configure --arch=riscv32 --cc=clang --cflags='--target=riscv32' --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\nMARCHID=0\nMIMPID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - grep -q PASS results.txt && ! grep -q FAIL results.txt
+
+build-riscv64-clang:
+ extends: .outoftree_template
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu clang
+ - mkdir build
+ - cd build
+ - ../configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\nMARCHID=0\nMIMPID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - grep -q PASS results.txt && ! grep -q FAIL results.txt
+
+build-riscv64-clang-efi:
+ extends: .intree_template
+ script:
+ - dnf install -y edk2-riscv64 qemu-system-riscv gcc-riscv64-linux-gnu clang
+ - cp /usr/share/edk2/riscv/RISCV_VIRT_CODE.fd .
+ - truncate -s 32M RISCV_VIRT_CODE.fd
+ - ./configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' --cross-prefix=riscv64-linux-gnu- --enable-efi
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\nMARCHID=0\nMIMPID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - grep -q PASS results.txt && ! grep -q FAIL results.txt
+
 build-s390x:
  extends: .outoftree_template
  script:
Signature · 1 line
2.46.0
Acked-by on v2 4/4
#12 lore raw

Re: [kvm-unit-tests PATCH v2 4/4] riscv: gitlab-ci: Add clang build tests

Quoted 4 lines · Andrew Jones

Test building 32 and 64-bit with clang. Throw a test of in- and out- of-tree building in too by swapping which is done to which (32-bit vs. 64-bit) with respect to the gcc build tests.

Quoted 60 lines
---
 .gitlab-ci.yml | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
.gitlab-ci.yml +43 -0
index 67a9a15733f1..b7ad99870e5a 100644
@@ -176,6 +176,49 @@ build-riscv64-efi:
       | tee results.txt
  - grep -q PASS results.txt && ! grep -q FAIL results.txt
 
+build-riscv32-clang:
+ extends: .intree_template
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu clang
+ - ./configure --arch=riscv32 --cc=clang --cflags='--target=riscv32' --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\nMARCHID=0\nMIMPID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - grep -q PASS results.txt && ! grep -q FAIL results.txt
+
+build-riscv64-clang:
+ extends: .outoftree_template
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu clang
+ - mkdir build
+ - cd build
+ - ../configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\nMARCHID=0\nMIMPID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - grep -q PASS results.txt && ! grep -q FAIL results.txt
+
+build-riscv64-clang-efi:
+ extends: .intree_template
+ script:
+ - dnf install -y edk2-riscv64 qemu-system-riscv gcc-riscv64-linux-gnu clang
+ - cp /usr/share/edk2/riscv/RISCV_VIRT_CODE.fd .
+ - truncate -s 32M RISCV_VIRT_CODE.fd
+ - ./configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' --cross-prefix=riscv64-linux-gnu- --enable-efi
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\nMARCHID=0\nMIMPID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - grep -q PASS results.txt && ! grep -q FAIL results.txt
+
 build-s390x:
  extends: .outoftree_template
  script: