Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 4 commits into from

Conversation

arichardson
Copy link
Member

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 #65517

@arichardson arichardson requested review from a team as code owners September 22, 2023 22:32
@llvmbot llvmbot added libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. libc++abi libc++abi C++ Runtime Library. Not libc++. libunwind labels Sep 22, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 22, 2023

@llvm/pr-subscribers-libcxx
@llvm/pr-subscribers-libcxxabi

@llvm/pr-subscribers-libunwind

Changes

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 #65517


Full diff: https://github.com/llvm/llvm-project/pull/67201.diff

8 Files Affected:

  • (modified) libcxx/test/configs/llvm-libc++-shared.cfg.in (+2-2)
  • (modified) libcxx/test/configs/llvm-libc++-static.cfg.in (+2-2)
  • (modified) libcxxabi/test/configs/llvm-libc++abi-merged.cfg.in (+2-2)
  • (modified) libcxxabi/test/configs/llvm-libc++abi-shared.cfg.in (+2-2)
  • (modified) libcxxabi/test/configs/llvm-libc++abi-static.cfg.in (+2-2)
  • (modified) libunwind/test/configs/llvm-libunwind-merged.cfg.in (+2-2)
  • (modified) libunwind/test/configs/llvm-libunwind-shared.cfg.in (+2-2)
  • (modified) libunwind/test/configs/llvm-libunwind-static.cfg.in (+2-2)
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 -- '

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why all configurations shouldn't be doing this?

@arichardson
Copy link
Member Author

Is there a reason why all configurations shouldn't be doing this?

I think it would be good if all of them did it, the reason I didn't is that I limited it to the ones I can test locally. Seeing as there is probably CI coverage for all of the configs, should I try updating the remaining ones?

@ldionne
Copy link
Member

ldionne commented Sep 25, 2023

I think it would be good if all of them did it, the reason I didn't is that I limited it to the ones I can test locally. Seeing as there is probably CI coverage for all of the configs, should I try updating the remaining ones?

Yes, I think it makes sense for all of them to use that variable. Can you confirm that this is basically empty all the time except if you're using a custom CMake toolchain file?

@arichardson
Copy link
Member Author

I think it would be good if all of them did it, the reason I didn't is that I limited it to the ones I can test locally. Seeing as there is probably CI coverage for all of the configs, should I try updating the remaining ones?

Yes, I think it makes sense for all of them to use that variable. Can you confirm that this is basically empty all the time except if you're using a custom CMake toolchain file?

Grepping through the CMake source shows that the variable could have an effect on the tests when targeting Android and Windows, but for other targets (Linux,macOS) it is a set to " ". I guess one option could be to guard it using if "@CMAKE_CROSSCOMPILING@":?

Modules/Compiler/ARMCC.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/ARMClang.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -march=${CMAKE_SYSTEM_ARCH}")
Modules/Compiler/ARMClang.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Modules/Compiler/Bruce-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -D__CLASSIC_C__")
Modules/Compiler/Fujitsu.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/GHS-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " ")
Modules/Compiler/GHS-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " ")
Modules/Compiler/GNU.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/IAR.cmake:    string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/IAR.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " --silent")
Modules/Compiler/IAR.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -S")
Modules/Compiler/Intel-ISPC.cmake:string(APPEND CMAKE_ISPC_FLAGS_INIT " ")
Modules/Compiler/Intel.cmake:    string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/IntelLLVM.cmake:    string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/LCC.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/NVIDIA-CUDA.cmake:  string(APPEND CMAKE_CUDA_FLAGS_INIT " ")
Modules/Compiler/OpenWatcom.cmake:  string(APPEND CMAKE_${type}_LINKER_FLAGS_INIT " opt map")
Modules/Compiler/OpenWatcom.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " -w3")
Modules/Compiler/PGI.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/PGI.cmake:    string(APPEND CMAKE_${lang}_FLAGS_INIT " -Bdynamic")
Modules/Compiler/PathScale.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/SunPro-ASM.cmake:string(APPEND CMAKE_ASM_FLAGS_INIT " ")
Modules/Compiler/SunPro-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " ")
Modules/Compiler/SunPro-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " ")
Modules/Compiler/Tasking.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Compiler/TinyCC-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " ")
Modules/Compiler/XL-ASM.cmake:string(APPEND CMAKE_ASM_FLAGS_INIT " -qthreaded -qhalt=e -qsourcetype=assembler")
Modules/Compiler/XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qthreaded")
Modules/Compiler/XL-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -qthreaded")
Modules/Platform/AIX-XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/AIX-XL-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -qhalt=s")
Modules/Platform/ARTOS-GNU-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -DARTOS -Xp -+")
Modules/Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libstdc++")
Modules/Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libc++")
Modules/Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libc++")
Modules/Platform/Android-Common.cmake:    string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_ANDROID_ABI_INIT_CFLAGS}")
Modules/Platform/Android-Common.cmake:      string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " ${_ANDROID_ABI_INIT_LDFLAGS}")
Modules/Platform/Android-Common.cmake:    string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${_ANDROID_ABI_INIT_EXE_LDFLAGS}")
Modules/Platform/Android-Common.cmake:      string(APPEND CMAKE_${lang}_FLAGS_INIT " -fexceptions")
Modules/Platform/Android-Common.cmake:      string(APPEND CMAKE_${lang}_FLAGS_INIT " -fno-exceptions")
Modules/Platform/Android-Common.cmake:      string(APPEND CMAKE_${lang}_FLAGS_INIT " -frtti")
Modules/Platform/Android-Common.cmake:      string(APPEND CMAKE_${lang}_FLAGS_INIT " -fno-rtti")
Modules/Platform/Apple-XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/Apple-XL-CXX.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/BlueGeneP-dynamic-XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/BlueGeneP-dynamic-XL-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -qhalt=s")
Modules/Platform/BlueGeneP-static-XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/BlueGeneP-static-XL-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -qhalt=s")
Modules/Platform/BlueGeneQ-dynamic-XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/BlueGeneQ-dynamic-XL-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -qhalt=s")
Modules/Platform/BlueGeneQ-static-XL-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -qhalt=e")
Modules/Platform/BlueGeneQ-static-XL-CXX.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -qhalt=s")
Modules/Platform/CYGWIN-GNU.cmake:string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -Wl,--enable-auto-import")
Modules/Platform/DOS-OpenWatcom.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system dos")
Modules/Platform/DOS-OpenWatcom.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system dos4g")
Modules/Platform/DOS-OpenWatcom.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -bt=dos")
Modules/Platform/DOS-OpenWatcom.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -bt=dos -xs")
Modules/Platform/HP-UX-HP.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
Modules/Platform/Linux-OpenWatcom.cmake:string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system linux opt noextension")
Modules/Platform/Linux-OpenWatcom.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -bt=linux")
Modules/Platform/Linux-OpenWatcom.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -bt=linux -xs")
Modules/Platform/OS2-OpenWatcom.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system os2")
Modules/Platform/OS2-OpenWatcom.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system os2v2")
Modules/Platform/OS2-OpenWatcom.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -bt=os2")
Modules/Platform/OS2-OpenWatcom.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -bt=os2 -xs")
Modules/Platform/OSF1.cmake:  set (CMAKE_C_FLAGS_INIT "")
Modules/Platform/OSF1.cmake:  set (CMAKE_CXX_FLAGS_INIT "")
Modules/Platform/SerenityOS-GNU.cmake:set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--hash-style=gnu,-z,relro,-z,now,-z,noexecstack,-z,separate-code,-z,max-page-size=0x1000")
Modules/Platform/Windows-Embarcadero.cmake:  string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " ${_tM} -lS:1048576 -lSc:4098 -lH:1048576 -lHc:8192 ")
Modules/Platform/Windows-Embarcadero.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_tM}")
Modules/Platform/Windows-IntelLLVM.cmake:# Save original CMAKE_${t}_LINKER_FLAGS_INIT
Modules/Platform/Windows-IntelLLVM.cmake:  set(_saved_cmake_${t}_linker_flags_init ${CMAKE_${t}_LINKER_FLAGS_INIT})
Modules/Platform/Windows-IntelLLVM.cmake:  set(CMAKE_${t}_LINKER_FLAGS_INIT "")
Modules/Platform/Windows-IntelLLVM.cmake:  foreach(flag ${CMAKE_${t}_LINKER_FLAGS_INIT})
Modules/Platform/Windows-IntelLLVM.cmake:  set(CMAKE_${t}_LINKER_FLAGS_INIT "")
Modules/Platform/Windows-IntelLLVM.cmake:  list(APPEND CMAKE_${t}_LINKER_FLAGS_INIT
Modules/Platform/Windows-MSVC.cmake:    string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " /NODEFAULTLIB:libc.lib /NODEFAULTLIB:oldnames.lib")
Modules/Platform/Windows-MSVC.cmake:  string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " ${_MACHINE_ARCH_FLAG}")
Modules/Platform/Windows-MSVC.cmake:  string(APPEND CMAKE_STATIC_LINKER_FLAGS_INIT " /machine:ARM64X")
Modules/Platform/Windows-MSVC.cmake:  string(APPEND CMAKE_STATIC_LINKER_FLAGS_INIT " ${_MACHINE_ARCH_FLAG}")
Modules/Platform/Windows-MSVC.cmake:      string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_PLATFORM_DEFINES}${_PLATFORM_DEFINES_${lang}} -fms-extensions -fms-compatibility -D_WINDOWS${_Wall}${_FLAGS_${lang}}")
Modules/Platform/Windows-MSVC.cmake:      string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_PLATFORM_DEFINES}${_PLATFORM_DEFINES_${lang}} /D_WINDOWS${_W3}${_FLAGS_${lang}}")
Modules/Platform/Windows-MSVC.cmake:  if(NOT CMAKE_RC_FLAGS_INIT)
Modules/Platform/Windows-MSVC.cmake:    string(APPEND CMAKE_RC_FLAGS_INIT " ${fixed_flags}")
Modules/Platform/Windows-NVIDIA-CUDA.cmake:string(APPEND CMAKE_CUDA_FLAGS_INIT " ${PLATFORM_DEFINES_CUDA} -D_WINDOWS -Xcompiler=\"${_W3}${_FLAGS_CXX}\"")
Modules/Platform/Windows-OpenWatcom.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -bt=nt -dWIN32 ${_br_bm}")
Modules/Platform/Windows-OpenWatcom.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -bt=nt -xs -dWIN32 ${_br_bm}")
Modules/Platform/Windows-df.cmake:set (CMAKE_EXE_LINKER_FLAGS_INIT " /INCREMENTAL:YES")
Modules/Platform/Windows3x-OpenWatcom.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system windows")
Modules/Platform/Windows3x-OpenWatcom.cmake:  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " system win386")
Modules/Platform/Windows3x-OpenWatcom.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -bt=windows")
Modules/Platform/Windows3x-OpenWatcom.cmake:string(APPEND CMAKE_CXX_FLAGS_INIT " -bt=windows -xs")

@ldionne
Copy link
Member

ldionne commented Sep 25, 2023

You could add it to all the .cfg.in files (I think we agree that it's what makes sense in theory) and then see whether the flags change in the configurations tested in the CI.

@arichardson
Copy link
Member Author

Updated all configs, hopefully CI agrees that this is correct.

@arichardson arichardson force-pushed the libcxx-xcompile branch 2 times, most recently from 6fba1ce to ba9765d Compare October 9, 2023 17:31
@ldionne
Copy link
Member

ldionne commented Oct 10, 2023

This would look good to me, but we seem to be failing the Windows tests!

@arichardson
Copy link
Member Author

This would look good to me, but we seem to be failing the Windows tests!

Unfortunately I can't see what's going wrong there and I don't have an easy way of reproducing it. I'll try to add some debugging code to this PR in the coming days.

@ldionne
Copy link
Member

ldionne commented Oct 14, 2023

You could try this:

diff --git a/libcxx/utils/libcxx/test/params.py b/libcxx/utils/libcxx/test/params.py
index 456794b9b1cc..37ea60b5e495 100644
--- a/libcxx/utils/libcxx/test/params.py
+++ b/libcxx/utils/libcxx/test/params.py
@@ -113,7 +113,7 @@ DEFAULT_PARAMETERS = [
         type=str,
         help="The version of the standard to compile the test suite with.",
         default=lambda cfg: next(
-            s for s in reversed(_allStandards) if getStdFlag(cfg, s)
+            s for s in reversed(_allStandards) if True
         ),
         actions=lambda std: [
             AddFeature(std),

Then I think it would fail later and probably in a way that provides more information.

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.
@@ -45,10 +45,10 @@ config.substitutions.append(('%{flags}',
'-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
))
config.substitutions.append(('%{compile_flags}',
'-nostdinc++ -I %{include} -I %{libcxx}/test/support'
'@CMAKE_CXX_FLAGS_INIT@ -nostdinc++ -I %{include} -I %{libcxx}/test/support'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense. The flags you use to build the library may not be the flags our users use when using it.

I think the test suite should handle its flags manually, and we should find another route to get you what you want here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original approach was to add a target flag (#65517) but @ldionne wasn't keen on that approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking here was that CMAKE_CXX_FLAGS_INIT are kind of like flags specified in a toolchain file -- they represent flags that literally any program compiled for target X needs to be passed. In that sense, they would be required for both the library and the test suite.

For instance, @arichardson don't you need to pass -mabi= or -march= to both the library and the test suite? I am still thinking that the proper way to actually do this would be to create .cfg.in config files that pass the target flags you need. Or if you need to specify various values for e.g. -mabi, you can create a Lit parameter that is local to that configuration like we do in libcxx/test/configs/apple-libc++-backdeployment.cfg.in. It feels like I've suggested that in the past though, so maybe there's a reason why you didn't go down that road?

@EricWF
Copy link
Member

EricWF commented Oct 20, 2023

We also don't want to make our test suite flags beholden to changes CMake chooses to make.

@EricWF
Copy link
Member

EricWF commented Oct 20, 2023

I'll take another pass at this on the weekend so I can provide some concrete direction.

@arichardson
Copy link
Member Author

Debugging shows that the reason this is failing on the clang-cl bots is that the CMAKE_CXX_FLAGS_INIT are using the MSVC syntax but we pass --driver-mode=g++ explicitly:

lit: C:\ws\src\libcxx\utils\libcxx\test\format.py:55: note: Command ["'C:/Program Files/LLVM/bin/clang-cl.exe' -xc++ nul -Werror -fsyntax-only --driver-mode=g++   /DWIN32 /D_WINDOWS /EHsc -fms-runtime-lib=dll -nostdinc++ -I C:/ws/src/build/clang-cl-dll/include/c++/v1 -I C:/ws/src/build/clang-cl-dll/include/c++/v1 -I C:/ws/src/libcxx/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX  -std=c++03"] failed with code 1: clang-cl: error: no such file or directory: '/DWIN32'
--
  | clang-cl: error: no such file or directory: '/D_WINDOWS'
  | clang-cl: error: no such file or directory: '/EHsc'

@arichardson
Copy link
Member Author

Debugging shows that the reason this is failing on the clang-cl bots is that the CMAKE_CXX_FLAGS_INIT are using the MSVC syntax but we pass --driver-mode=g++ explicitly:

lit: C:\ws\src\libcxx\utils\libcxx\test\format.py:55: note: Command ["'C:/Program Files/LLVM/bin/clang-cl.exe' -xc++ nul -Werror -fsyntax-only --driver-mode=g++   /DWIN32 /D_WINDOWS /EHsc -fms-runtime-lib=dll -nostdinc++ -I C:/ws/src/build/clang-cl-dll/include/c++/v1 -I C:/ws/src/build/clang-cl-dll/include/c++/v1 -I C:/ws/src/libcxx/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX  -std=c++03"] failed with code 1: clang-cl: error: no such file or directory: '/DWIN32'
--
  | clang-cl: error: no such file or directory: '/D_WINDOWS'
  | clang-cl: error: no such file or directory: '/EHsc'

I am less convinced now that this is the right solution since the CMake flags refer to the library build rather than the testing configuration, so maybe an explicit test suite parameter as in #65517 is the cleaner solution?

@ldionne
Copy link
Member

ldionne commented Nov 23, 2023

Closing since it seems we don't want to pursue this anymore, since @CMAKE_CXX_FLAGS_INIT@ are flags for the library build, not necessarily for the tests to use.

@ldionne ldionne closed this Nov 23, 2023
@arichardson arichardson deleted the libcxx-xcompile branch November 27, 2023 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++abi libc++abi C++ Runtime Library. Not libc++. libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. libunwind
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants