Tested x86_64-pc-linux-gnu.  Any opinions?  Bikeshedding of the flag name?

-- 8< --

For simple testcases that want to use the std module, it would be useful to
have a reasonably short way to request building the binary module form
before the testcase.  So with this patch users can write

  g++ -std=c++20 -fmodules --compile-std-module mytest.C

gcc/c-family/ChangeLog:

        * c.opt: Add --compile-std-module.

gcc/cp/ChangeLog:

        * lang-specs.h: Add @c++-system-module.
        * g++spec.cc (lang_specific_driver): Handle --compile-std-module.

gcc/ChangeLog:

        * opts-common.cc (generate_canonical_option): Handle
        OPT_SPECIAL_input_file.
---
 gcc/c-family/c.opt  |  4 ++++
 gcc/cp/lang-specs.h | 18 ++++++++++++++++++
 gcc/cp/g++spec.cc   | 27 +++++++++++++++++++++++++++
 gcc/opts-common.cc  |  6 ++++++
 4 files changed, 55 insertions(+)

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 0d97b0d13ce..8da089780c4 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -45,6 +45,10 @@ C ObjC C++ ObjC++ Separate Alias(A) 
MissingArgError(assertion missing after %qs)
 -assert=
 C ObjC C++ ObjC++ Joined Alias(A) MissingArgError(assertion missing after %qs)
 
+-compile-std-module
+C++ ObjC++ Driver
+--compile-std-module   Compile module units for <bits/stdc++.h>, std, and 
std.compat.
+
 -comments
 C ObjC C++ ObjC++ Alias(C)
 
diff --git a/gcc/cp/lang-specs.h b/gcc/cp/lang-specs.h
index a67ce4c22a5..90ad03c0812 100644
--- a/gcc/cp/lang-specs.h
+++ b/gcc/cp/lang-specs.h
@@ -105,6 +105,24 @@ along with GCC; see the file COPYING3.  If not see
       "                 %{!o*:--output-pch %w%i.gch}%W{o*:--output-pch 
%w%*}}}}%{!S:%V}}"
       "}}}",
      CPLUSPLUS_CPP_SPEC, 0, 0},
+  /* Just for implementing --compile-std-module.  */
+  {"@c++-system-module",
+      "%{E|M|MM:cc1plus -E %(cpp_options) %2 %(cpp_debug_options)"
+      "  -fsearch-include-path=system}"
+      "%{!E:%{!M:%{!MM:"
+      "  %{save-temps*|no-integrated-cpp:cc1plus -E"
+      "    -fsearch-include-path=system"
+      "           %(cpp_options) %2 -o %{save-temps*:%b.ii} 
%{!save-temps*:%g.ii} \n}"
+      "  cc1plus %{save-temps*|no-integrated-cpp:-fpreprocessed"
+      "           %{save-temps*:%b.ii} %{!save-temps*:%g.ii}}"
+      "  %{!save-temps*:%{!no-integrated-cpp:%(cpp_unique_options)"
+      "    -fsearch-include-path=system}}"
+      "  %(cc1_options) %2"
+      "  %{!fsyntax-only:"
+      "    %{fmodule-only:%{!S:-o %g.s%V}}"
+      "    %{!fmodule-only:%(invoke_as)}}"
+      "}}}",
+      CPLUSPLUS_CPP_SPEC, 0, 0},
   {"@c++",
       "%{E|M|MM:cc1plus -E %(cpp_options) %2 %(cpp_debug_options)}"
       "%{!E:%{!M:%{!MM:"
diff --git a/gcc/cp/g++spec.cc b/gcc/cp/g++spec.cc
index 165a91d45cd..0753a15efc3 100644
--- a/gcc/cp/g++spec.cc
+++ b/gcc/cp/g++spec.cc
@@ -131,6 +131,9 @@ lang_specific_driver (struct cl_decoded_option 
**in_decoded_options,
   /* By default, we don't add -lstdc++exp.  */
   bool need_experimental = false;
 
+  /* Whether to also compile module std.  */
+  bool std_module = false;
+
   /* True if we saw -static.  */
   int static_link = 0;
 
@@ -246,6 +249,10 @@ lang_specific_driver (struct cl_decoded_option 
**in_decoded_options,
          which_library = (stdcxxlib_kind) decoded_options[i].value;
          break;
 
+       case OPT__compile_std_module:
+         std_module = true;
+         break;
+
        case OPT_SPECIAL_input_file:
          {
            int len;
@@ -302,6 +309,7 @@ lang_specific_driver (struct cl_decoded_option 
**in_decoded_options,
 
   /* Add one for shared_libgcc or extra static library.  */
   num_args = (argc + added + need_math + need_experimental
+             + (std_module * 5)
              + (library > 0) * 4 + 1);
   /* For libc++, on most platforms, the ABI library (usually called libc++abi)
      is provided as a separate DSO, which we must also append.
@@ -337,6 +345,25 @@ lang_specific_driver (struct cl_decoded_option 
**in_decoded_options,
          saw_libc = &decoded_options[i];
        }
 
+      /* For --compile-std-module, also compile
+        bits/stdc++.h bits/std.cc bits/std.compat.cc.  */
+      if (decoded_options[i].opt_index == OPT__compile_std_module)
+       {
+         generate_option (OPT_x, "c++-system-header", 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         generate_option (OPT_SPECIAL_input_file, "bits/stdc++.h", 1,
+                          CL_DRIVER, &new_decoded_options[j++]);
+         generate_option (OPT_x, "c++-system-module", 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         generate_option (OPT_SPECIAL_input_file, "bits/std.cc", 1,
+                          CL_DRIVER, &new_decoded_options[j++]);
+         generate_option (OPT_SPECIAL_input_file, "bits/std.compat.cc", 1,
+                          CL_DRIVER, &new_decoded_options[j++]);
+         generate_option (OPT_x, "none", 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         --j;
+       }
+
       /* Wrap foo.[chi] files in a language specification to
         force the gcc compiler driver to run cc1plus on them.  */
       if (args[i] & LANGSPEC)
diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc
index 379402e68a6..ba6d5464c98 100644
--- a/gcc/opts-common.cc
+++ b/gcc/opts-common.cc
@@ -412,6 +412,12 @@ generate_canonical_option (size_t opt_index, const char 
*arg,
          decoded->canonical_option[1] = arg;
          decoded->canonical_option_num_elements = 2;
        }
+      else if (opt_index == OPT_SPECIAL_input_file)
+       {
+         decoded->canonical_option[0] = arg;
+         decoded->canonical_option[1] = NULL;
+         decoded->canonical_option_num_elements = 1;
+       }
       else
        {
          gcc_assert (option->flags & CL_JOINED);

base-commit: aaa625a51cff750b40bc98f6555adc3f3f5f297b
-- 
2.51.0

Reply via email to