[libc++] Fix modules builds when features are removed

When some headers are not available because we removed features like
localization or threads, the compiler should not try to include these
headers when building modules. To avoid that from happening, add a
requires-declaration that is never satisfied when the configuration
in use doesn't support a header.

rdar://93777687

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

NOKEYCHECK=True
GitOrigin-RevId: 0e9a01dcac99ccf599738cef394b840b126d5cc9
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 8f2a66f..3f89fee 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -618,7 +618,6 @@
   map
   math.h
   memory
-  module.modulemap
   mutex
   new
   numbers
@@ -669,9 +668,17 @@
   wctype.h
   )
 
-configure_file("__config_site.in" "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}/__config_site" @ONLY)
+foreach(feature LIBCXX_ENABLE_FILESYSTEM LIBCXX_ENABLE_LOCALIZATION LIBCXX_ENABLE_THREADS LIBCXX_ENABLE_WIDE_CHARACTERS)
+  if (NOT ${${feature}})
+    set(requires_${feature} "requires LIBCXX_CONFIGURED_WITHOUT_SUPPORT_FOR_THIS_HEADER")
+  endif()
+endforeach()
 
-set(_all_includes "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}/__config_site")
+configure_file("__config_site.in" "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}/__config_site" @ONLY)
+configure_file("module.modulemap.in" "${LIBCXX_GENERATED_INCLUDE_DIR}/module.modulemap" @ONLY)
+
+set(_all_includes "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}/__config_site"
+                  "${LIBCXX_GENERATED_INCLUDE_DIR}/module.modulemap")
 foreach(f ${files})
   set(src "${CMAKE_CURRENT_SOURCE_DIR}/${f}")
   set(dst "${LIBCXX_GENERATED_INCLUDE_DIR}/${f}")
diff --git a/include/csignal b/include/csignal
index 81ca890..c1b58f8 100644
--- a/include/csignal
+++ b/include/csignal
@@ -41,7 +41,10 @@
 
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
-#include <signal.h>
+
+#if __has_include(<signal.h>)
+# include <signal.h>
+#endif
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
diff --git a/include/module.modulemap b/include/module.modulemap.in
similarity index 96%
rename from include/module.modulemap
rename to include/module.modulemap.in
index 0194612..b89983b 100644
--- a/include/module.modulemap
+++ b/include/module.modulemap.in
@@ -38,6 +38,7 @@
     // <iso646.h> provided by compiler.
     // <limits.h> provided by compiler or C library.
     module locale_h {
+      @requires_LIBCXX_ENABLE_LOCALIZATION@
       header "locale.h"
       export *
     }
@@ -50,6 +51,7 @@
       export *
     }
     module stdatomic_h {
+      @requires_LIBCXX_ENABLE_THREADS@
       requires cplusplus23
       header "stdatomic.h"
       export *
@@ -90,11 +92,13 @@
     }
     // <time.h> provided by C library.
     module wchar_h {
+      @requires_LIBCXX_ENABLE_WIDE_CHARACTERS@
       // <wchar.h>'s __need_* macros require textual inclusion.
       textual header "wchar.h"
       export *
     }
     module wctype_h {
+      @requires_LIBCXX_ENABLE_WIDE_CHARACTERS@
       header "wctype.h"
       export *
     }
@@ -155,6 +159,7 @@
       export *
     }
     module clocale {
+      @requires_LIBCXX_ENABLE_LOCALIZATION@
       header "clocale"
       export *
     }
@@ -215,11 +220,13 @@
       export *
     }
     module cwchar {
+      @requires_LIBCXX_ENABLE_WIDE_CHARACTERS@
       header "cwchar"
       export depr.stdio_h
       export *
     }
     module cwctype {
+      @requires_LIBCXX_ENABLE_WIDE_CHARACTERS@
       header "cwctype"
       export *
     }
@@ -382,6 +389,7 @@
     export *
   }
   module barrier {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "barrier"
     export *
   }
@@ -425,7 +433,11 @@
       module duration              { private header "__chrono/duration.h" }
       module file_clock            { private header "__chrono/file_clock.h" }
       module hh_mm_ss              { private header "__chrono/hh_mm_ss.h" }
-      module high_resolution_clock { private header "__chrono/high_resolution_clock.h" }
+      module high_resolution_clock {
+        private header "__chrono/high_resolution_clock.h"
+        export steady_clock
+        export system_clock
+      }
       module literals              { private header "__chrono/literals.h" }
       module month                 { private header "__chrono/month.h" }
       module month_weekday         { private header "__chrono/month_weekday.h" }
@@ -441,6 +453,7 @@
     }
   }
   module codecvt {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "codecvt"
     export *
   }
@@ -528,6 +541,7 @@
     export *
   }
   module filesystem {
+    @requires_LIBCXX_ENABLE_FILESYSTEM@
     header "filesystem"
     export *
 
@@ -588,6 +602,7 @@
     export *
   }
   module fstream {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "fstream"
     export *
   }
@@ -626,6 +641,7 @@
     }
   }
   module future {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "future"
     export *
   }
@@ -634,10 +650,12 @@
     export *
   }
   module iomanip {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "iomanip"
     export *
   }
   module ios {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "ios"
     export iosfwd
     export *
@@ -651,6 +669,7 @@
     export *
   }
   module iostream {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "iostream"
     export ios
     export streambuf
@@ -659,6 +678,7 @@
     export *
   }
   module istream {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "istream"
     // FIXME: should re-export ios, streambuf?
     export *
@@ -711,6 +731,7 @@
     }
   }
   module latch {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "latch"
     export *
   }
@@ -724,6 +745,7 @@
     export *
   }
   module locale {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "locale"
     export *
   }
@@ -761,6 +783,7 @@
     }
   }
   module mutex {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "mutex"
     export *
   }
@@ -797,6 +820,7 @@
     export *
   }
   module ostream {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "ostream"
     // FIXME: should re-export ios, streambuf?
     export *
@@ -909,6 +933,7 @@
     export *
   }
   module regex {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "regex"
     export initializer_list
     export *
@@ -918,6 +943,7 @@
     export *
   }
   module semaphore {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "semaphore"
     export *
   }
@@ -927,6 +953,7 @@
     export *
   }
   module shared_mutex {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "shared_mutex"
     export version
   }
@@ -937,6 +964,7 @@
     module span_fwd { private header "__fwd/span.h" }
   }
   module sstream {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "sstream"
     // FIXME: should re-export istream, ostream, ios, streambuf, string?
     export *
@@ -951,6 +979,7 @@
     export *
   }
   module streambuf {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "streambuf"
     export *
   }
@@ -969,6 +998,7 @@
     module string_view_fwd { private header "__fwd/string_view.h" }
   }
   module strstream {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
     header "strstream"
     export *
   }
@@ -977,6 +1007,7 @@
     export *
   }
   module thread {
+    @requires_LIBCXX_ENABLE_THREADS@
     header "thread"
     export *
 
@@ -1118,7 +1149,6 @@
     export *
   }
 
-  // __config not modularised due to a bug in Clang
   // FIXME: These should be private.
   module __assert            {         header "__assert"            export * }
   module __availability      { private header "__availability"      export * }
@@ -1127,12 +1157,18 @@
   module __debug             {         header "__debug"             export * }
   module __errc              { private header "__errc"              export * }
   module __hash_table        {         header "__hash_table"        export * }
-  module __locale            { private header "__locale"            export * }
+  module __locale            {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
+    private header "__locale" export *
+  }
   module __mbstate_t         { private header "__mbstate_t.h"       export * }
   module __mutex_base        { private header "__mutex_base"        export * }
   module __node_handle       { private header "__node_handle"       export * }
   module __split_buffer      { private header "__split_buffer"      export * }
-  module __std_stream        { private header "__std_stream"        export * }
+  module __std_stream        {
+    @requires_LIBCXX_ENABLE_LOCALIZATION@
+    private header "__std_stream" export *
+  }
   module __string            { private header "__string"            export * }
   module __threading_support {         header "__threading_support" export * }
   module __tree              {         header "__tree"              export * }
@@ -1184,6 +1220,7 @@
       export *
     }
     module regex {
+      @requires_LIBCXX_ENABLE_LOCALIZATION@
       header "experimental/regex"
       export *
     }