[libunwind] [libunwind][libc++][libc++abi] Add cross-compilation flags to tests (PR #67201)

Alexander Richardson via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 22 15:32:40 PDT 2023


https://github.com/arichardson created https://github.com/llvm/llvm-project/pull/67201

There is currently only limited support for passing target-specific flags (e.g. -mabi= or -march=) to the testsuites if you don't have a compiler (or wrapper script) that defaults to the expected flags. However, some targets (e.g. RISC-V) may require additional flags beyond the basic --target= that is already added by the current infrastructure.

When cross-compiling with CMake, the recommended approach is to define a toolchain file that specifies the necessary flags needed to compile for a given target. There are two interesting variables here that we should also be passing when building the test binaries:
- CMAKE_CXX_FLAGS_INIT defines the flags we need for compilation jobs and will specify flags such as -mabi= and -march=
- CMAKE_EXE_LINKER_FLAGS_INIT defines the linker flags that are needed to cross-compile for a given target. When not cross-compiling these variables will generally be empty.

This patch only adds these flags to the default libc++ configurations, but if the clang-cl/gcc configurations could also benefit from this I'll update the patch.

This should be a possible alternative to https://reviews.llvm.org/D151056 and https://github.com/llvm/llvm-project/pull/65517

>From 72464ba171bc303f692f9a73012a4d1060ca0dda Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Wed, 6 Sep 2023 11:41:56 -0700
Subject: [PATCH] [libunwind][libc++][libc++abi] Add cross-compilation flags to
 tests

There is currently only limited support for passing target-specific
flags (e.g. -mabi= or -march=) to the testsuites if you don't have a
compiler (or wrapper script) that defaults to the expected flags.
However, some targets (e.g. RISC-V) may require additional flags beyond
the basic --target= that is already added by the current infrastructure.

When cross-compiling with CMake, the recommended approach is to define
a toolchain file that specifies the necessary flags needed to compile for
a given target. There are two interesting variables here that we should
also be passing when building the test binaries:
- CMAKE_CXX_FLAGS_INIT defines the flags we need for compilation jobs
   and will specify flags such as -mabi= and -march=
- CMAKE_EXE_LINKER_FLAGS_INIT defines the linker flags that are needed to
  cross-compile for a given target.
When not cross-compiling these variables will generally be empty.

This patch only adds these flags to the default libc++ configurations, but
if the clang-cl/gcc configurations could also benefit from this I'll update
the patch.
---
 libcxx/test/configs/llvm-libc++-shared.cfg.in       | 4 ++--
 libcxx/test/configs/llvm-libc++-static.cfg.in       | 4 ++--
 libcxxabi/test/configs/llvm-libc++abi-merged.cfg.in | 4 ++--
 libcxxabi/test/configs/llvm-libc++abi-shared.cfg.in | 4 ++--
 libcxxabi/test/configs/llvm-libc++abi-static.cfg.in | 4 ++--
 libunwind/test/configs/llvm-libunwind-merged.cfg.in | 4 ++--
 libunwind/test/configs/llvm-libunwind-shared.cfg.in | 4 ++--
 libunwind/test/configs/llvm-libunwind-static.cfg.in | 4 ++--
 8 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/libcxx/test/configs/llvm-libc++-shared.cfg.in b/libcxx/test/configs/llvm-libc++-shared.cfg.in
index 143b3b3feae1109..82e7129d8f166a0 100644
--- a/libcxx/test/configs/llvm-libc++-shared.cfg.in
+++ b/libcxx/test/configs/llvm-libc++-shared.cfg.in
@@ -7,10 +7,10 @@ config.substitutions.append(('%{flags}',
     '-pthread' + (' -isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else '')
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support'
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support'
 ))
 config.substitutions.append(('%{link_flags}',
-    '-nostdlib++ -L %{lib} -Wl,-rpath,%{lib} -lc++'
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -nostdlib++ -L %{lib} -Wl,-rpath,%{lib} -lc++'
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libcxx/test/configs/llvm-libc++-static.cfg.in b/libcxx/test/configs/llvm-libc++-static.cfg.in
index e866d4f52a0629d..3dc4af1c000d59f 100644
--- a/libcxx/test/configs/llvm-libc++-static.cfg.in
+++ b/libcxx/test/configs/llvm-libc++-static.cfg.in
@@ -7,10 +7,10 @@ config.substitutions.append(('%{flags}',
     '-pthread' + (' -isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else '')
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support'
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support'
 ))
 config.substitutions.append(('%{link_flags}',
-    '-nostdlib++ -L %{lib} -lc++ -lc++abi'
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -nostdlib++ -L %{lib} -lc++ -lc++abi'
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libcxxabi/test/configs/llvm-libc++abi-merged.cfg.in b/libcxxabi/test/configs/llvm-libc++abi-merged.cfg.in
index c6fa4301b459ce9..861fc26baccaaf7 100644
--- a/libcxxabi/test/configs/llvm-libc++abi-merged.cfg.in
+++ b/libcxxabi/test/configs/llvm-libc++abi-merged.cfg.in
@@ -6,11 +6,11 @@ config.substitutions.append(('%{flags}',
     '-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS ' +
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS ' +
     '-I %{libcxx}/test/support -I %{libcxx}/src'
 ))
 config.substitutions.append(('%{link_flags}',
-    '-nostdlib++ -L %{lib} -Wl,-rpath,%{lib} -lc++ -pthread'
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -nostdlib++ -L %{lib} -Wl,-rpath,%{lib} -lc++ -pthread'
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libcxxabi/test/configs/llvm-libc++abi-shared.cfg.in b/libcxxabi/test/configs/llvm-libc++abi-shared.cfg.in
index 6b69205da77c992..86f4e654140589b 100644
--- a/libcxxabi/test/configs/llvm-libc++abi-shared.cfg.in
+++ b/libcxxabi/test/configs/llvm-libc++abi-shared.cfg.in
@@ -7,10 +7,10 @@ config.substitutions.append(('%{flags}',
     '-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -I %{libcxx}/test/support -I %{libcxx}/src -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS'
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -I %{libcxx}/test/support -I %{libcxx}/src -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS'
 ))
 config.substitutions.append(('%{link_flags}',
-    '-nostdlib++ -L %{lib} -Wl,-rpath,%{lib} -lc++ -lc++abi -pthread'
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -nostdlib++ -L %{lib} -Wl,-rpath,%{lib} -lc++ -lc++abi -pthread'
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libcxxabi/test/configs/llvm-libc++abi-static.cfg.in b/libcxxabi/test/configs/llvm-libc++abi-static.cfg.in
index 352e2c39586e1ea..b0e312d1c73fdee 100644
--- a/libcxxabi/test/configs/llvm-libc++abi-static.cfg.in
+++ b/libcxxabi/test/configs/llvm-libc++abi-static.cfg.in
@@ -7,10 +7,10 @@ config.substitutions.append(('%{flags}',
     '-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -I %{libcxx}/test/support -I %{libcxx}/src -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS'
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -I %{libcxx}/test/support -I %{libcxx}/src -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS'
 ))
 config.substitutions.append(('%{link_flags}',
-    '-nostdlib++ -L %{lib} -lc++ -lc++abi -pthread'
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -nostdlib++ -L %{lib} -lc++ -lc++abi -pthread'
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libunwind/test/configs/llvm-libunwind-merged.cfg.in b/libunwind/test/configs/llvm-libunwind-merged.cfg.in
index 10650f7edf66a2f..19f312c87ba4050 100644
--- a/libunwind/test/configs/llvm-libunwind-merged.cfg.in
+++ b/libunwind/test/configs/llvm-libunwind-merged.cfg.in
@@ -25,10 +25,10 @@ config.substitutions.append(('%{flags}',
     '-isysroot {}'.format(local_sysroot) if local_sysroot else ''
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{{include}} {}'.format(' '.join(compile_flags))
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{{include}} {}'.format(' '.join(compile_flags))
 ))
 config.substitutions.append(('%{link_flags}',
-    '-L %{{lib}} -Wl,-rpath,%{{lib}} -lc++ {}'.format(' '.join(link_flags))
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -L %{{lib}} -Wl,-rpath,%{{lib}} -lc++ {}'.format(' '.join(link_flags))
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libunwind/test/configs/llvm-libunwind-shared.cfg.in b/libunwind/test/configs/llvm-libunwind-shared.cfg.in
index 97185e57234ff7f..44ee6ab4d906765 100644
--- a/libunwind/test/configs/llvm-libunwind-shared.cfg.in
+++ b/libunwind/test/configs/llvm-libunwind-shared.cfg.in
@@ -24,10 +24,10 @@ config.substitutions.append(('%{flags}',
     '-isysroot {}'.format(local_sysroot) if local_sysroot else ''
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{{include}} {}'.format(' '.join(compile_flags))
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{{include}} {}'.format(' '.join(compile_flags))
 ))
 config.substitutions.append(('%{link_flags}',
-    '-L %{{lib}} -Wl,-rpath,%{{lib}} -lunwind {}'.format(' '.join(link_flags))
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ -L %{{lib}} -Wl,-rpath,%{{lib}} -lunwind {}'.format(' '.join(link_flags))
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '
diff --git a/libunwind/test/configs/llvm-libunwind-static.cfg.in b/libunwind/test/configs/llvm-libunwind-static.cfg.in
index fc6a18d057f3884..763b6d4fe1d4618 100644
--- a/libunwind/test/configs/llvm-libunwind-static.cfg.in
+++ b/libunwind/test/configs/llvm-libunwind-static.cfg.in
@@ -27,10 +27,10 @@ config.substitutions.append(('%{flags}',
     '-isysroot {}'.format(local_sysroot) if local_sysroot else ''
 ))
 config.substitutions.append(('%{compile_flags}',
-    '-nostdinc++ -I %{{include}} {}'.format(' '.join(compile_flags))
+    '@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{{include}} {}'.format(' '.join(compile_flags))
 ))
 config.substitutions.append(('%{link_flags}',
-    '%{{lib}}/libunwind.a {}'.format(' '.join(link_flags))
+    '@CMAKE_EXE_LINKER_FLAGS_INIT@ %{{lib}}/libunwind.a {}'.format(' '.join(link_flags))
 ))
 config.substitutions.append(('%{exec}',
     '%{executor} --execdir %T -- '



More information about the cfe-commits mailing list