]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
driver/c++: add --compile-std-module
authorJason Merrill <jason@redhat.com>
Tue, 11 Nov 2025 10:28:01 +0000 (15:58 +0530)
committerJason Merrill <jason@redhat.com>
Tue, 25 Nov 2025 10:23:23 +0000 (15:53 +0530)
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

I expect this to be particularly useful on godbolt.org, where currently
building a modules testcase requires messing with cmake.  One complication
there is that just adding std.cc to the command line arguments hits the
"cannot specify -o with -S with multiple files" error, so I avoid counting
these added inputs as "multiple files"; in that situation each compile will
output to the same target file, with the user-specified input last so it's
the one that actually ends up in the target after the command completes.

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:

* doc/invoke.texi: Document --compile-std-module.
* gcc.cc (struct infile): Add artificial field.
(add_infile): Set it.
(driver::prepare_infiles): Check it.

gcc/testsuite/ChangeLog:

* g++.dg/modules/compile-std1.C: New test.
* g++.dg/modules/modules.exp: Only run it once.

gcc/c-family/c.opt
gcc/cp/g++spec.cc
gcc/cp/lang-specs.h
gcc/doc/invoke.texi
gcc/gcc.cc
gcc/testsuite/g++.dg/modules/compile-std1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/modules.exp

index 0d97b0d13ce5bb00c14ccccdee8ece4046248bf8..8da089780c4e8b0fbacc58b2487d1c870902e7a8 100644 (file)
@@ -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)
 
index 165a91d45cd00f78dd87200127eedf3af5f60bf5..6fddb58862cc56698e5dbe658bacc17407cad652 100644 (file)
@@ -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,35 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
          saw_libc = &decoded_options[i];
        }
 
+      /* Insert --compile-std-module options before any -x or source files.  */
+      size_t opt = decoded_options[i].opt_index;
+      if (std_module
+         && (opt == OPT__compile_std_module
+             || opt == OPT_SPECIAL_input_file
+             || opt == OPT_x))
+       {
+         generate_option (OPT_x, "c++-system-header", 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         generate_option_input_file ("bits/stdc++.h",
+                                     &new_decoded_options[j]);
+         /* Tell process_command that this file was added by the driver.  */
+         new_decoded_options[j++].mask = CL_DRIVER;
+         generate_option (OPT_x, "c++-system-module", 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         generate_option_input_file ("bits/std.cc",
+                                     &new_decoded_options[j]);
+         new_decoded_options[j++].mask = CL_DRIVER;
+         generate_option_input_file ("bits/std.compat.cc",
+                                     &new_decoded_options[j]);
+         new_decoded_options[j++].mask = CL_DRIVER;
+         generate_option (OPT_x, "none", 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         new_decoded_options[j] = decoded_options[i];
+         std_module = false;
+       }
+      if (opt == OPT__compile_std_module)
+       --j;
+
       /* Wrap foo.[chi] files in a language specification to
         force the gcc compiler driver to run cc1plus on them.  */
       if (args[i] & LANGSPEC)
index a67ce4c22a5d38e025b4a14b17085d365c8c517b..90ad03c08128e1f6ad63de0d1ae0abdef533a4c1 100644 (file)
@@ -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:"
index 284b9f4e802f997ea593b73fc7b0d12c275bbf82..06723c2b3b6236290d9d3535a46b261eee515f1e 100644 (file)
@@ -3022,6 +3022,15 @@ Here is a list of options that are @emph{only} for compiling C++ programs:
 
 @table @gcctabopt
 
+@opindex compile-std-module
+@item --compile-std-module
+Build the compiled module interfaces (@pxref{C++ Modules}) for the
+@samp{<bits/stdc++.h>} header unit and the @samp{std} and
+@samp{std.compat} modules before compiling any source files explicitly
+specified on the command line.  This is intended to be useful for
+building simple programs that use @samp{import std;} with a single
+command.
+
 @opindex fabi-version
 @item -fabi-version=@var{n}
 Use version @var{n} of the C++ ABI@.  The default is version 0.
index 9cc83d9ea346c8e0f06c6eb7ac4e2e895b9d8591..fa46387e59a68abc2e569b73c56b222dba8890fc 100644 (file)
@@ -3623,6 +3623,7 @@ struct infile
   struct compiler *incompiler;
   bool compiled;
   bool preprocessed;
+  bool artificial;
 };
 
 /* Also a vector of input files specified.  */
@@ -3826,10 +3827,11 @@ alloc_infile (void)
    infiles.  */
 
 static void
-add_infile (const char *name, const char *language)
+add_infile (const char *name, const char *language, bool art = false)
 {
   alloc_infile ();
   infiles[n_infiles].name = name;
+  infiles[n_infiles].artificial = art;
   infiles[n_infiles++].language = language;
 }
 
@@ -4998,7 +5000,8 @@ process_command (unsigned int decoded_options_count,
 #ifdef HAVE_TARGET_OBJECT_SUFFIX
          arg = convert_filename (arg, 0, access (arg, F_OK));
 #endif
-         add_infile (arg, spec_lang);
+         add_infile (arg, spec_lang,
+                     decoded_options[j].mask == CL_DRIVER);
 
          continue;
        }
@@ -8984,6 +8987,10 @@ driver::prepare_infiles ()
       if (lang_n_infiles > 0 && compiler != input_file_compiler
          && infiles[i].language && infiles[i].language[0] != '*')
        infiles[i].incompiler = compiler;
+      else if (infiles[i].artificial)
+       /* Leave lang_n_infiles alone so files added by the driver don't
+          interfere with -c -o.  */
+       infiles[i].incompiler = compiler;
       else if (compiler)
        {
          lang_n_infiles++;
diff --git a/gcc/testsuite/g++.dg/modules/compile-std1.C b/gcc/testsuite/g++.dg/modules/compile-std1.C
new file mode 100644 (file)
index 0000000..a7972be
--- /dev/null
@@ -0,0 +1,11 @@
+// { dg-additional-options "-fmodules --compile-std-module -g -O" }
+// { dg-do compile { target c++20 } }
+
+import <bits/stdc++.h>;
+import std;
+import std.compat;
+
+void f()
+{
+  std::string s;
+}
index 73b5de1397fa66010139f149ea05c7642025eb43..3f2b8f2d57647339ef87e1a32c8875bb3e984c1e 100644 (file)
@@ -267,7 +267,8 @@ proc module-init { src } {
     set extra_tool_flags {}
 
     if { [llength $option_list]
-        && [string match "*xtreme*" $src] } {
+        && ( [string match "*xtreme*" $src]
+             || [string match "*compile-std*" $src] ) } {
        # Only run the xtreme tests once.
        set option_list [lrange [lsort $option_list] end end]
     }