[libunwind] Add a from-scratch config for running libunwind tests

Running tests for libunwind is a lot simpler than running tests for
libc++, so a simple Lit config file is sufficient. The benefit is that
we disentangle the libunwind test configuration from the libc++ and
libc++abi test configuration. The setup was too complicated, which led
to some bugs (notably we were running against the system libunwind on
Apple platforms).

Differential Revision: https://reviews.llvm.org/D111664

GitOrigin-RevId: 6fd55bba61bb4df98f9485b8d3cfc9e956d992b8
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7a8bfae..59d489b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -89,9 +89,9 @@
     "Additional compiler flags for test programs.")
 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/lit.site.cfg.in" CACHE STRING
   "The path to the Lit testing configuration to use when running the tests.
-   If a relative path is provided, it is assumed to be relative to '<monorepo>/libcxx/test/configs'.")
+   If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
 if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
-  set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_LIBCXX_PATH}/test/configs/${LIBUNWIND_TEST_CONFIG}")
+  set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
 endif()
 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
     "A list of parameters to run the Lit test suite with.")
diff --git a/test/configs/llvm-libunwind-shared.cfg.in b/test/configs/llvm-libunwind-shared.cfg.in
new file mode 100644
index 0000000..5ba7852
--- /dev/null
+++ b/test/configs/llvm-libunwind-shared.cfg.in
@@ -0,0 +1,64 @@
+@AUTO_GEN_COMMENT@
+
+@SERIALIZED_LIT_PARAMS@
+
+#
+# Configuration file for running the libunwind tests against the shared library.
+#
+# This file is a lot simpler than the ones for libc++ and libc++abi because
+# while libunwind is written in C++, it doesn't use the C++ Standard Library
+# so we don't need to set that up to run the tests correctly.
+#
+
+import os, site
+site.addsitedir(os.path.join('@LIBUNWIND_LIBCXX_PATH@', 'utils'))
+import libcxx.test.format
+
+# Basic configuration of the test suite
+config.name = os.path.basename('@LIBUNWIND_TEST_CONFIG@')
+config.test_source_root = os.path.join('@LIBUNWIND_SOURCE_DIR@', 'test')
+config.test_format = libcxx.test.format.CxxStandardLibraryTest()
+config.recursiveExpansionLimit = 10
+config.test_exec_root = '@CMAKE_BINARY_DIR@'
+
+compile_flags = []
+link_flags = []
+if @LIBUNWIND_USES_ARM_EHABI@:
+    config.available_features.add('libunwind-arm-ehabi')
+
+if not @LIBUNWIND_ENABLE_THREADS@:
+    compile_flags.append('-D_LIBUNWIND_HAS_NO_THREADS')
+    config.available_features.add('libunwind-no-threads')
+
+if @LIBUNWIND_ENABLE_CET@:
+    compile_flags.append('-fcf-protection=full')
+
+if '@CMAKE_SYSTEM_NAME@' == 'Linux':
+    link_flags.append('-Wl,--export-dynamic')
+
+# Stack unwinding tests need unwinding tables and these are not generated by default on all targets.
+compile_flags.append('-funwind-tables')
+
+config.substitutions.append(('%{cxx}', '@CMAKE_CXX_COMPILER@'))
+config.substitutions.append(('%{flags}',
+    '-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
+))
+config.substitutions.append(('%{compile_flags}',
+    '-nostdinc++ -I {}/include {}'.format('@LIBUNWIND_SOURCE_DIR@', ' '.join(compile_flags))
+))
+config.substitutions.append(('%{link_flags}',
+    '-nodefaultlibs -L {0}/lib -Wl,-rpath,{0}/lib -lunwind -ldl -lc {1}'.format('@CMAKE_BINARY_DIR@', ' '.join(link_flags))
+))
+config.substitutions.append(('%{exec}',
+    '{} {}/utils/run.py --execdir %T -- '.format(sys.executable, '@LIBUNWIND_LIBCXX_PATH@')
+))
+
+import os, site
+site.addsitedir(os.path.join('@LIBUNWIND_LIBCXX_PATH@', 'utils'))
+import libcxx.test.params, libcxx.test.newconfig, libcxx.test.newconfig
+libcxx.test.newconfig.configure(
+    libcxx.test.params.DEFAULT_PARAMETERS,
+    libcxx.test.features.DEFAULT_FEATURES,
+    config,
+    lit_config
+)
diff --git a/test/configs/llvm-libunwind-static.cfg.in b/test/configs/llvm-libunwind-static.cfg.in
new file mode 100644
index 0000000..6d3f4f0
--- /dev/null
+++ b/test/configs/llvm-libunwind-static.cfg.in
@@ -0,0 +1,66 @@
+@AUTO_GEN_COMMENT@
+
+@SERIALIZED_LIT_PARAMS@
+
+#
+# Configuration file for running the libunwind tests against the static library.
+#
+# This file is a lot simpler than the ones for libc++ and libc++abi because
+# while libunwind is written in C++, it doesn't use the C++ Standard Library
+# so we don't need to set that up to run the tests correctly.
+#
+
+import os, site
+site.addsitedir(os.path.join('@LIBUNWIND_LIBCXX_PATH@', 'utils'))
+import libcxx.test.format
+
+# Basic configuration of the test suite
+config.name = os.path.basename('@LIBUNWIND_TEST_CONFIG@')
+config.test_source_root = os.path.join('@LIBUNWIND_SOURCE_DIR@', 'test')
+config.test_format = libcxx.test.format.CxxStandardLibraryTest()
+config.recursiveExpansionLimit = 10
+config.test_exec_root = '@CMAKE_BINARY_DIR@'
+
+compile_flags = []
+link_flags = []
+if @LIBUNWIND_USES_ARM_EHABI@:
+    config.available_features.add('libunwind-arm-ehabi')
+
+if not @LIBUNWIND_ENABLE_THREADS@:
+    compile_flags.append('-D_LIBUNWIND_HAS_NO_THREADS')
+    config.available_features.add('libunwind-no-threads')
+else:
+    link_flags.append('-lpthread')
+
+if @LIBUNWIND_ENABLE_CET@:
+    compile_flags.append('-fcf-protection=full')
+
+if '@CMAKE_SYSTEM_NAME@' == 'Linux':
+    link_flags.append('-Wl,--export-dynamic')
+
+# Stack unwinding tests need unwinding tables and these are not generated by default on all targets.
+compile_flags.append('-funwind-tables')
+
+config.substitutions.append(('%{cxx}', '@CMAKE_CXX_COMPILER@'))
+config.substitutions.append(('%{flags}',
+    '-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
+))
+config.substitutions.append(('%{compile_flags}',
+    '-nostdinc++ -I {}/include {}'.format('@LIBUNWIND_SOURCE_DIR@', ' '.join(compile_flags))
+))
+config.substitutions.append(('%{link_flags}',
+    '-nodefaultlibs {}/lib/libunwind.a -ldl -lc {}'.format('@CMAKE_BINARY_DIR@', ' '.join(link_flags))
+))
+config.substitutions.append(('%{exec}',
+    '{} {}/utils/run.py --execdir %T -- '.format(sys.executable, '@LIBUNWIND_LIBCXX_PATH@')
+))
+
+import os, site
+site.addsitedir(os.path.join('@LIBUNWIND_LIBCXX_PATH@', 'utils'))
+import libcxx.test.params, libcxx.test.newconfig, libcxx.test.newconfig
+libcxx.test.newconfig.configure(
+    libcxx.test.params.DEFAULT_PARAMETERS,
+    libcxx.test.features.DEFAULT_FEATURES,
+    config,
+    lit_config
+)
diff --git a/test/forceunwind.pass.cpp b/test/forceunwind.pass.cpp
index a3191f8..1206632 100644
--- a/test/forceunwind.pass.cpp
+++ b/test/forceunwind.pass.cpp
@@ -12,9 +12,6 @@
 // TODO: Investigate these failures
 // XFAIL: asan, tsan, ubsan
 
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
-
 // Basic test for _Unwind_ForcedUnwind.
 // See libcxxabi/test/forced_unwind* tests too.
 
diff --git a/test/frameheadercache_test.pass.cpp b/test/frameheadercache_test.pass.cpp
index dd1d3dd..a50b2b6 100644
--- a/test/frameheadercache_test.pass.cpp
+++ b/test/frameheadercache_test.pass.cpp
@@ -1,9 +1,6 @@
 // TODO: Investigate these failures
 // XFAIL: asan, tsan, ubsan
 
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
-
 // The other libunwind tests don't test internal interfaces, so the include path
 // is a little wonky.
 #include "../src/config.h"
diff --git a/test/libunwind_01.pass.cpp b/test/libunwind_01.pass.cpp
index aceb8be..6c1e1bb 100644
--- a/test/libunwind_01.pass.cpp
+++ b/test/libunwind_01.pass.cpp
@@ -1,11 +1,8 @@
 // TODO: Investigate these failures
 // XFAIL: asan, tsan, ubsan
 
-// TODO: Investigate these failures on x86_64 macOS
-// XFAIL: target=x86_64-apple-darwin{{.+}}
-
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
+// TODO: Investigate these failures on x86_64 macOS back deployment
+// UNSUPPORTED: target=x86_64-apple-darwin{{.+}}
 
 #include <libunwind.h>
 #include <stdlib.h>
diff --git a/test/libunwind_02.pass.cpp b/test/libunwind_02.pass.cpp
index 64d8d58..6eea7a3 100644
--- a/test/libunwind_02.pass.cpp
+++ b/test/libunwind_02.pass.cpp
@@ -1,9 +1,6 @@
 // TODO: Investigate these failures
 // XFAIL: asan, tsan, ubsan
 
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
-
 #include <assert.h>
 #include <stdlib.h>
 #include <unwind.h>
diff --git a/test/signal_frame.pass.cpp b/test/signal_frame.pass.cpp
index fea1897..cfd4f48 100644
--- a/test/signal_frame.pass.cpp
+++ b/test/signal_frame.pass.cpp
@@ -15,9 +15,6 @@
 // TODO: Investigate this failure on macOS
 // XFAIL: target={{.+}}-apple-darwin{{.+}}
 
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
-
 // UNSUPPORTED: libunwind-arm-ehabi
 
 #include <assert.h>
diff --git a/test/signal_unwind.pass.cpp b/test/signal_unwind.pass.cpp
index 5468c7f..12d9589 100644
--- a/test/signal_unwind.pass.cpp
+++ b/test/signal_unwind.pass.cpp
@@ -13,9 +13,6 @@
 // TODO: Investigate these failures
 // XFAIL: asan, tsan, ubsan
 
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
-
 #include <assert.h>
 #include <dlfcn.h>
 #include <signal.h>
diff --git a/test/unwind_leaffunction.pass.cpp b/test/unwind_leaffunction.pass.cpp
index 8fc3433..4037455 100644
--- a/test/unwind_leaffunction.pass.cpp
+++ b/test/unwind_leaffunction.pass.cpp
@@ -13,9 +13,6 @@
 // TODO: Investigate these failures
 // XFAIL: asan, tsan, ubsan
 
-// TODO: Investigate this failure
-// XFAIL: 32bits-on-64bits
-
 #include <assert.h>
 #include <dlfcn.h>
 #include <signal.h>