[libc++] Serialize Lit parameters to make them available to from-scratch configs

Before this patch, Lit parameters that were set as a result of CMake
options were not made available to from-scratch configs. This patch
serializes those parameters into the generated lit config file so that
they are available to all configs.

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

GitOrigin-RevId: 58a230455bbc680fc12641d9231dce5dfb907e91
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a5a739b..24c769c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -24,6 +24,22 @@
     "Executor to use when running tests.")
 
 set(AUTO_GEN_COMMENT "## Autogenerated by libunwind configuration.\n# Do not edit!")
+set(SERIALIZED_LIT_PARAMS "# Lit parameters serialized here for llvm-lit to pick them up\n")
+
+macro(serialize_lit_param param value)
+  string(APPEND SERIALIZED_LIT_PARAMS "config.${param} = ${value}\n")
+endmacro()
+
+serialize_lit_param(enable_experimental False)
+
+if (LLVM_USE_SANITIZER)
+  serialize_lit_param(use_sanitizer "\"${LLVM_USE_SANITIZER}\"")
+endif()
+
+if (TARGET_TRIPLE)
+  serialize_lit_param(target_triple "\"${TARGET_TRIPLE}\"")
+endif()
+
 configure_lit_site_cfg(
   "${LIBUNWIND_TEST_CONFIG}"
   ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
diff --git a/test/libunwind/test/config.py b/test/libunwind/test/config.py
index b3f12bf..18919c2 100644
--- a/test/libunwind/test/config.py
+++ b/test/libunwind/test/config.py
@@ -45,7 +45,7 @@
         self.cxx.compile_flags += ['-funwind-tables']
         # Make symbols available in the tests.
         triple = self.get_lit_conf('target_triple', None)
-        if 'linux' in triple:
+        if triple is not None and 'linux' in triple:
             self.cxx.link_flags += ['-Wl,--export-dynamic']
         if not self.get_lit_bool('enable_threads', True):
             self.cxx.compile_flags += ['-D_LIBUNWIND_HAS_NO_THREADS']
diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in
index 51b4631..6f0c339 100644
--- a/test/lit.site.cfg.in
+++ b/test/lit.site.cfg.in
@@ -1,5 +1,7 @@
 @AUTO_GEN_COMMENT@
 
+@SERIALIZED_LIT_PARAMS@
+
 import os
 import site
 
@@ -14,7 +16,6 @@
 config.llvm_unwinder            = True
 config.builtins_library         = "@LIBUNWIND_BUILTINS_LIBRARY@"
 config.enable_threads           = @LIBUNWIND_ENABLE_THREADS@
-config.use_sanitizer            = "@LLVM_USE_SANITIZER@"
 config.enable_32bit             = @LIBUNWIND_BUILD_32_BITS@
 config.target_info              = "@LIBUNWIND_TARGET_INFO@"
 config.test_linker_flags        = "@LIBUNWIND_TEST_LINKER_FLAGS@"
@@ -24,12 +25,9 @@
 config.enable_shared            = @LIBCXX_ENABLE_SHARED@
 config.arm_ehabi                = @LIBUNWIND_USES_ARM_EHABI@
 config.host_triple              = "@LLVM_HOST_TRIPLE@"
-if "@TARGET_TRIPLE@":
-    config.target_triple        = "@TARGET_TRIPLE@"
 config.sysroot                  = "@LIBUNWIND_SYSROOT@"
 config.gcc_toolchain            = "@LIBUNWIND_GCC_TOOLCHAIN@"
 config.cxx_ext_threads          = @LIBUNWIND_BUILD_EXTERNAL_THREAD_LIBRARY@
-config.enable_experimental      = False
 
 site.addsitedir(os.path.join(config.libunwind_src_root, 'test'))
 site.addsitedir(os.path.join(config.libcxx_src_root, 'utils'))