Support cross compiling with clang
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.
Andrew Jones (3):
riscv: Drop mstrict-align
configure: Support cross compiling with clang
riscv: gitlab-ci: Add clang build tests
.gitlab-ci.yml | 28 ++++++++++++++++++++++++++++ configure | 11 ++++++++--- riscv/Makefile | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-)
Signature · 1 line
2.46.0
PATCH 1/3 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
@@ -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
PATCH 2/3 configure: Support cross compiling with clang
In reply to Andrew JonesWhen 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
@@ -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
PATCH 3/3 riscv: gitlab-ci: Add clang build tests
In reply to Andrew JonesTest 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 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
.gitlab-ci.yml +28 -0
@@ -176,6 +176,34 @@ build-riscv64-efi: | 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-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-s390x: extends: .outoftree_template script:
Signature · 1 line
2.46.0
Re: [kvm-unit-tests PATCH 2/3] configure: Support cross compiling with clang
In reply to Andrew JonesQuoted 14 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.
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
Quoted 7 lines
./configure --arch=riscv64 --cc=clang --cflags='--target=riscv64' \ --cross-prefix=riscv64-linux-gnu-Signed-off-by Andrew Jones <[email protected]>--- configure | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
Re: [kvm-unit-tests PATCH 3/3] riscv: gitlab-ci: Add clang build tests
In reply to Andrew JonesQuoted 8 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.
Signed-off-by Andrew Jones <[email protected]>--- .gitlab-ci.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
Re: [kvm-unit-tests PATCH 1/3] riscv: Drop mstrict-align
In reply to Andrew JonesQuoted 14 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.
Signed-off-by Andrew Jones <[email protected]>--- riscv/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)riscv/Makefile +0
-0
riscv/Makefile LDFLAGS += -melf32lriscv · line 82
Quoted 8 lines
riscv/Makefile +1
-1@@ -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
Quoted 12 lines · Andrew Jones
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.
Andrew Jones (3):
riscv: Drop mstrict-align
configure: Support cross compiling with clang
riscv: gitlab-ci: Add clang build tests
Quoted 7 lines
.gitlab-ci.yml | 28 ++++++++++++++++++++++++++++ configure | 11 ++++++++--- riscv/Makefile | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-)Signature · 1 line
2.46.0
When compiling with clang and --config-efi I hit
lib/efi.c:342:29: error: field 'vendor' with variable sized type 'struct efi_vendor_dev_path' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
342 | struct efi_vendor_dev_path vendor;
| ^1 error generated.
Our efi code is exactly the same as the Linux code, but Linux avoids that warning with -Wno-gnu. We could also add that to EFI_CFLAGS, but I think I'll just add -Wno-gnu-variable-sized-type-not-at-end since I like seeing these things as they come.
I'll send a v2 with a Makefile patch added.
Thanks,
drew