[runtimes] Revert the libc++ __config_site change

This is a massive revert of the following commits (from most revent to oldest):

	2b9b7b5775a1d8fcd7aa5abaa8fc0bc303434f1a.
	529ac33197f6408952ae995075ac5e2dc5287e81
	28270234f1478047e35879f4ba8838b47edfcc14
	69c2087283cf7b17ca75f69daebf4ffc158b754a
	b5aa67446e01bd277727b05710a42e69ac41e74b
	5d796645d6c8cadeb003715c33e231a8ba05b6de

After checking-in the __config_site change, a lot of things started breaking
due to widespread reliance on various aspects of libc++'s build, notably the
fact that we can include the headers from the source tree, but also reliance
on various "internal" CMake variables used by the runtimes build and compiler-rt.

These were unintended consequences of the change, and after two days, we
still haven't restored all the bots to being green. Instead, now that I
understand what specific areas this will blow up in, I should be able to
chop up the patch into smaller ones that are easier to digest.

See https://reviews.llvm.org/D89041 for more details on this adventure.

GitOrigin-RevId: 48e4b0fd3a3d68cc9774699964cf4c6c2be38cf3
diff --git a/CMakeLists.txt b/CMakeLists.txt
index eb19646..015a359 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -397,11 +397,9 @@
 # Configure System
 #===============================================================================
 
-# TODO: Other projects rely on the location where we generate the libc++ headers
-#       internally. We should get rid of these dependencies.
 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
   set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++)
-  set(LIBCXX_GENERATED_INCLUDE_DIR "${LLVM_BINARY_DIR}/include/c++/v1")
+  set(LIBCXX_HEADER_DIR ${LLVM_BINARY_DIR})
   set(LIBCXX_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++)
   if(LIBCXX_LIBDIR_SUBDIR)
     string(APPEND LIBCXX_LIBRARY_DIR /${LIBCXX_LIBDIR_SUBDIR})
@@ -409,11 +407,11 @@
   endif()
 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
   set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
-  set(LIBCXX_GENERATED_INCLUDE_DIR  "${LLVM_BINARY_DIR}/include/c++/v1")
+  set(LIBCXX_HEADER_DIR  ${LLVM_BINARY_DIR})
   set(LIBCXX_INSTALL_LIBRARY_DIR lib${LIBCXX_LIBDIR_SUFFIX})
 else()
   set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
-  set(LIBCXX_GENERATED_INCLUDE_DIR  "${CMAKE_BINARY_DIR}/include/c++/v1")
+  set(LIBCXX_HEADER_DIR  ${CMAKE_BINARY_DIR})
   set(LIBCXX_INSTALL_LIBRARY_DIR lib${LIBCXX_LIBDIR_SUFFIX})
 endif()
 
diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt
index 12acffc..42d25c2 100644
--- a/benchmarks/CMakeLists.txt
+++ b/benchmarks/CMakeLists.txt
@@ -8,7 +8,7 @@
 set(BENCHMARK_LIBCXX_COMPILE_FLAGS
     -Wno-unused-command-line-argument
     -nostdinc++
-    -isystem "${LIBCXX_GENERATED_INCLUDE_DIR}"
+    -isystem ${LIBCXX_SOURCE_DIR}/include
     -L${LIBCXX_LIBRARY_DIR}
     -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
     ${SANITIZER_FLAGS}
@@ -18,6 +18,7 @@
           -L${LIBCXX_CXX_ABI_LIBRARY_PATH}
           -Wl,-rpath,${LIBCXX_CXX_ABI_LIBRARY_PATH})
 endif()
+list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS -include "${LIBCXX_BINARY_DIR}/__config_site")
 split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
 
 ExternalProject_Add(google-benchmark-libcxx
diff --git a/cmake/Modules/HandleLibCXXABI.cmake b/cmake/Modules/HandleLibCXXABI.cmake
index 6f50853..c5aa267 100644
--- a/cmake/Modules/HandleLibCXXABI.cmake
+++ b/cmake/Modules/HandleLibCXXABI.cmake
@@ -52,10 +52,8 @@
             COMMENT "Copying C++ ABI header ${fpath}...")
         list(APPEND abilib_headers "${dst}")
 
-        if (LIBCXX_GENERATED_INCLUDE_DIR) # always true
-          # TODO: libc++ shouldn't be responsible for copying the libc++abi
-          #       headers into the right location.
-          set(dst "${LIBCXX_GENERATED_INCLUDE_DIR}/include/c++/v1/${dstdir}/${fpath}")
+        if (LIBCXX_HEADER_DIR)
+          set(dst "${LIBCXX_HEADER_DIR}/include/c++/v1/${dstdir}/${fpath}")
           add_custom_command(OUTPUT ${dst}
               DEPENDS ${src}
               COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
diff --git a/docs/TestingLibcxx.rst b/docs/TestingLibcxx.rst
index ec017e2..d42becf 100644
--- a/docs/TestingLibcxx.rst
+++ b/docs/TestingLibcxx.rst
@@ -26,8 +26,8 @@
 
 After building libc++, you can run parts of the libc++ test suite by simply
 running ``llvm-lit`` on a specified test or directory. If you're unsure
-whether the required targets have been built, you can use the `check-cxx-deps`
-target to build them. For example:
+whether the required libraries have been built, you can use the
+`check-cxx-deps` target. For example:
 
 .. code-block:: bash
 
@@ -37,12 +37,6 @@
   $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test
   $ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic
 
-In the default configuration, the tests are built against headers that form a
-fake installation root of libc++. This installation root has to be updated when
-changes are made to the headers, so you should re-run the `check-cxx-deps` target
-before running the tests manually with `lit` when you make any sort of change,
-including to the headers.
-
 Sometimes you'll want to change the way LIT is running the tests. Custom options
 can be specified using the `--param=<name>=<val>` flag. The most common option
 you'll want to change is the standard dialect (ie -std=c++XX). By default the
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index a63b565..7c97db4 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -2,7 +2,6 @@
   __bit_reference
   __bsd_locale_defaults.h
   __bsd_locale_fallbacks.h
-  __config
   __errc
   __debug
   __functional_03
@@ -185,28 +184,62 @@
     )
 endif()
 
-if (LIBCXX_GENERATED_INCLUDE_DIR) # Always true
-  configure_file("__config_site.in" "${LIBCXX_GENERATED_INCLUDE_DIR}/__config_site" @ONLY)
+configure_file("__config_site.in"
+               "${LIBCXX_BINARY_DIR}/__config_site"
+               @ONLY)
 
-  set(_all_includes "${LIBCXX_GENERATED_INCLUDE_DIR}/__config_site")
+# Generate a custom __config header. The new header is created
+# by prepending __config_site to the current __config header.
+add_custom_command(OUTPUT ${LIBCXX_BINARY_DIR}/__generated_config
+  COMMAND ${Python3_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/cat_files.py
+    ${LIBCXX_BINARY_DIR}/__config_site
+    ${LIBCXX_SOURCE_DIR}/include/__config
+    -o ${LIBCXX_BINARY_DIR}/__generated_config
+  DEPENDS ${LIBCXX_SOURCE_DIR}/include/__config
+          ${LIBCXX_BINARY_DIR}/__config_site
+)
+# Add a target that executes the generation commands.
+add_custom_target(cxx-generated-config ALL
+  DEPENDS ${LIBCXX_BINARY_DIR}/__generated_config)
+
+if(LIBCXX_HEADER_DIR)
+  set(output_dir ${LIBCXX_HEADER_DIR}/include/c++/v1)
+
+  set(out_files)
   foreach(f ${files})
-    set(src "${CMAKE_CURRENT_SOURCE_DIR}/${f}")
-    set(dst "${LIBCXX_GENERATED_INCLUDE_DIR}/${f}")
+    set(src ${CMAKE_CURRENT_SOURCE_DIR}/${f})
+    set(dst ${output_dir}/${f})
     add_custom_command(OUTPUT ${dst}
       DEPENDS ${src}
       COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
       COMMENT "Copying CXX header ${f}")
-    list(APPEND _all_includes "${dst}")
+    list(APPEND out_files ${dst})
   endforeach()
-  add_custom_target(generate-cxx-headers DEPENDS ${_all_includes})
+
+  # Copy the generated header as __config into build directory.
+  set(src ${LIBCXX_BINARY_DIR}/__generated_config)
+  set(dst ${output_dir}/__config)
+  add_custom_command(OUTPUT ${dst}
+      DEPENDS ${src} cxx-generated-config
+      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
+      COMMENT "Copying CXX __config")
+  list(APPEND out_files ${dst})
+  add_custom_target(generate-cxx-headers DEPENDS ${out_files})
 
   add_library(cxx-headers INTERFACE)
   add_dependencies(cxx-headers generate-cxx-headers ${LIBCXX_CXX_ABI_HEADER_TARGET})
   # TODO: Use target_include_directories once we figure out why that breaks the runtimes build
   if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
-    target_compile_options(cxx-headers INTERFACE /I "${LIBCXX_GENERATED_INCLUDE_DIR}")
+    target_compile_options(cxx-headers INTERFACE /I "${output_dir}")
   else()
-    target_compile_options(cxx-headers INTERFACE -I "${LIBCXX_GENERATED_INCLUDE_DIR}")
+    target_compile_options(cxx-headers INTERFACE -I "${output_dir}")
+  endif()
+
+  # Make sure the generated __config_site header is included when we build the library.
+  if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
+    target_compile_options(cxx-headers INTERFACE /FI "${LIBCXX_BINARY_DIR}/__config_site")
+  else()
+    target_compile_options(cxx-headers INTERFACE -include "${LIBCXX_BINARY_DIR}/__config_site")
   endif()
 else()
   add_library(cxx-headers INTERFACE)
@@ -222,15 +255,16 @@
     )
   endforeach()
 
-  # Install the generated __config_site.
-  install(FILES ${LIBCXX_GENERATED_INCLUDE_DIR}/__config_site
+  # Install the generated header as __config.
+  install(FILES ${LIBCXX_BINARY_DIR}/__generated_config
     DESTINATION ${LIBCXX_INSTALL_HEADER_PREFIX}include/c++/v1
     PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
+    RENAME __config
     COMPONENT cxx-headers)
 
   if (NOT CMAKE_CONFIGURATION_TYPES)
     add_custom_target(install-cxx-headers
-                      DEPENDS cxx-headers
+                      DEPENDS cxx-headers cxx-generated-config
                       COMMAND "${CMAKE_COMMAND}"
                               -DCMAKE_INSTALL_COMPONENT=cxx-headers
                               -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
diff --git a/include/__config b/include/__config
index f2ebd4d..7cdcd9a 100644
--- a/include/__config
+++ b/include/__config
@@ -10,8 +10,6 @@
 #ifndef _LIBCPP_CONFIG
 #define _LIBCPP_CONFIG
 
-#include <__config_site>
-
 #if defined(_MSC_VER) && !defined(__clang__)
 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #    define _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
diff --git a/test/configs/legacy.cfg.in b/test/configs/legacy.cfg.in
index 148ac3f..f0a4e8a 100644
--- a/test/configs/legacy.cfg.in
+++ b/test/configs/legacy.cfg.in
@@ -3,7 +3,6 @@
 import os
 import site
 
-config.cxx_headers              = "@LIBCXX_GENERATED_INCLUDE_DIR@"
 config.cxx_under_test           = "@CMAKE_CXX_COMPILER@"
 config.project_obj_root         = "@CMAKE_BINARY_DIR@"
 config.libcxx_src_root          = "@LIBCXX_SOURCE_DIR@"
diff --git a/utils/libcxx/test/config.py b/utils/libcxx/test/config.py
index c5050d1..0d21aa1 100644
--- a/utils/libcxx/test/config.py
+++ b/utils/libcxx/test/config.py
@@ -331,6 +331,7 @@
 
     def configure_compile_flags_header_includes(self):
         support_path = os.path.join(self.libcxx_src_root, 'test', 'support')
+        self.configure_config_site_header()
         if self.cxx_stdlib_under_test != 'libstdc++' and \
            not self.target_info.is_windows():
             self.cxx.compile_flags += [
@@ -347,12 +348,16 @@
                                          'set_windows_crt_report_mode.h')
             ]
         cxx_headers = self.get_lit_conf('cxx_headers')
-        if cxx_headers is None and self.cxx_stdlib_under_test != 'libc++':
+        if cxx_headers == '' or (cxx_headers is None
+                                 and self.cxx_stdlib_under_test != 'libc++'):
             self.lit_config.note('using the system cxx headers')
             return
         self.cxx.compile_flags += ['-nostdinc++']
+        if cxx_headers is None:
+            cxx_headers = os.path.join(self.libcxx_src_root, 'include')
         if not os.path.isdir(cxx_headers):
-            self.lit_config.fatal("cxx_headers='{}' is not a directory.".format(cxx_headers))
+            self.lit_config.fatal("cxx_headers='%s' is not a directory."
+                                  % cxx_headers)
         self.cxx.compile_flags += ['-I' + cxx_headers]
         if self.libcxx_obj_root is not None:
             cxxabi_headers = os.path.join(self.libcxx_obj_root, 'include',
@@ -360,6 +365,16 @@
             if os.path.isdir(cxxabi_headers):
                 self.cxx.compile_flags += ['-I' + cxxabi_headers]
 
+    def configure_config_site_header(self):
+        # Check for a possible __config_site in the build directory. We
+        # use this if it exists.
+        if self.libcxx_obj_root is None:
+            return
+        config_site_header = os.path.join(self.libcxx_obj_root, '__config_site')
+        if not os.path.isfile(config_site_header):
+            return
+        self.cxx.compile_flags += ['-include', config_site_header]
+
     def configure_link_flags(self):
         # Configure library path
         self.configure_link_flags_cxx_library_path()