[libc++] Allow specifying custom Lit config files

Before this patch, the libc++ test suite first loads lit.site.cfg
(generated by CMake), and then lit.cfg. It's also possible to load
lit.cfg before lit.site.cfg and to point to a custom lit.site.cfg
file using '--param=libcxx_site_config'. However, in that case, lit.cfg
still relies on the site configuration filling up the 'config' object
like the default lit.site.cfg file does, which isn't flexible enough.

This commit simplifies the setup by having just a single Lit site config
file per CMake configuration, and always loading exactly that config file.
However, the config file to use can be selected when setting up CMake via
the LIBCXX_TEST_CONFIG setting. Furthermore, the site configs are entirely
standalone, which means that a new site config can be added that doesn't
need to conform what's expected by config.py.

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

Cr-Mirrored-From: https://chromium.googlesource.com/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 0c66af970c806d65d9335c7272610c82c2388e31
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index b68f59f..d4b84b8 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -103,11 +103,12 @@
 endif()
 
 if (LIBCXX_INCLUDE_TESTS)
-  include(AddLLVM) # for configure_lit_site_cfg and add_lit_testsuit
+  include(AddLLVM) # for configure_lit_site_cfg and add_lit_testsuite
 
   configure_lit_site_cfg(
-    ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
-    ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg)
+    "${LIBCXX_TEST_CONFIG}"
+    ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
+    MAIN_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py")
 
   add_custom_target(check-cxx-deps
     DEPENDS cxx ${LIBCXX_TEST_DEPS}
diff --git a/test/lit.cfg b/test/lit.cfg
deleted file mode 100644
index ce98c63..0000000
--- a/test/lit.cfg
+++ /dev/null
@@ -1,64 +0,0 @@
-# -*- Python -*- vim: set ft=python ts=4 sw=4 expandtab tw=79:
-# Configuration file for the 'lit' test runner.
-import os
-import site
-
-site.addsitedir(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'utils'))
-
-
-# Tell pylint that we know config and lit_config exist somewhere.
-if 'PYLINT_IMPORT' in os.environ:
-    config = object()
-    lit_config = object()
-
-# name: The name of this test suite.
-config.name = 'libc++'
-
-# suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.pass.cpp', '.fail.cpp', '.sh.cpp', '.pass.mm']
-
-# test_source_root: The root path where tests are located.
-config.test_source_root = os.path.dirname(__file__)
-
-# Allow expanding substitutions that are based on other substitutions
-config.recursiveExpansionLimit = 10
-
-loaded_site_cfg = getattr(config, 'loaded_site_config', False)
-if not loaded_site_cfg:
-    import libcxx.test.config
-    libcxx.test.config.loadSiteConfig(lit_config, config, 'libcxx_site_config',
-                                      'LIBCXX_SITE_CONFIG')
-
-# Infer the test_exec_root from the libcxx_object root.
-obj_root = getattr(config, 'libcxx_obj_root', None)
-
-# Check that the test exec root is known.
-if obj_root is None:
-    obj_root = getattr(config, 'libcxx_obj_root', None)
-    if obj_root is None:
-        import tempfile
-        obj_root = tempfile.mkdtemp(prefix='libcxx-testsuite-')
-        lit_config.warning('Creating temporary directory for object root: %s' %
-                           obj_root)
-
-if not config.test_exec_root:
-    config.test_exec_root = os.path.join(obj_root, 'test')
-
-cfg_variant = getattr(config, 'configuration_variant', 'libcxx')
-if cfg_variant:
-    lit_config.note('Using configuration variant: %s' % cfg_variant)
-
-# Load the Configuration class from the module name <cfg_variant>.test.config.
-config_module_name = '.'.join([cfg_variant, 'test', 'config'])
-config_module = __import__(config_module_name, fromlist=['Configuration'])
-
-configuration = config_module.Configuration(lit_config, config)
-configuration.configure()
-configuration.print_config_info()
-if lit_config.params.get('use_old_format', False):
-    lit_config.note("Using the old libc++ testing format")
-    config.test_format = configuration.get_test_format()
-else:
-    lit_config.note("Using the new libc++ testing format")
-    import libcxx.test.newformat
-    config.test_format = libcxx.test.newformat.CxxStandardLibraryTest()
diff --git a/test/lit.cfg.py b/test/lit.cfg.py
new file mode 100644
index 0000000..647464a
--- /dev/null
+++ b/test/lit.cfg.py
@@ -0,0 +1,10 @@
+# All the Lit configuration is handled in the site configs -- this file is only
+# left as a canary to catch invocations of Lit that do not go through llvm-lit.
+#
+# Invocations that go through llvm-lit will automatically use the right Lit
+# site configuration inside the build directory.
+
+lit_config.fatal(
+    "You seem to be running Lit directly -- you should be running Lit through "
+    "<build>/bin/llvm-lit, which will ensure that the right Lit configuration "
+    "file is used.")
diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in
index f00a255..8692217 100644
--- a/test/lit.site.cfg.in
+++ b/test/lit.site.cfg.in
@@ -1,4 +1,8 @@
 @AUTO_GEN_COMMENT@
+
+import os
+import site
+
 config.cxx_under_test           = "@LIBCXX_COMPILER@"
 config.project_obj_root         = "@CMAKE_BINARY_DIR@"
 config.libcxx_src_root          = "@LIBCXX_SOURCE_DIR@"
@@ -39,6 +43,32 @@
 # Code signing
 config.llvm_codesign_identity   = "@LLVM_CODESIGNING_IDENTITY@"
 
-# Let the main config do the real work.
-config.loaded_site_config = True
-lit_config.load_config(config, "@LIBCXX_SOURCE_DIR@/test/lit.cfg")
+site.addsitedir(os.path.join(config.libcxx_src_root, 'utils'))
+
+# name: The name of this test suite.
+config.name = 'libc++'
+
+# suffixes: A list of file extensions to treat as test files.
+config.suffixes = ['.pass.cpp', '.fail.cpp', '.sh.cpp', '.pass.mm']
+
+# test_source_root: The root path where tests are located.
+config.test_source_root = os.path.join(config.libcxx_src_root, 'test')
+
+# Allow expanding substitutions that are based on other substitutions
+config.recursiveExpansionLimit = 10
+
+# Infer the test_exec_root from the libcxx_object root.
+config.test_exec_root = os.path.join(config.libcxx_obj_root, 'test')
+
+lit_config.note('Using configuration variant: {}'.format(config.configuration_variant))
+import libcxx.test.config
+configuration = libcxx.test.config.Configuration(lit_config, config)
+configuration.configure()
+configuration.print_config_info()
+if lit_config.params.get('use_old_format', False):
+    lit_config.note("Using the old libc++ testing format")
+    config.test_format = configuration.get_test_format()
+else:
+    lit_config.note("Using the new libc++ testing format")
+    import libcxx.test.newformat
+    config.test_format = libcxx.test.newformat.CxxStandardLibraryTest()