On 11/17/25 4:55 PM, Jakub Jelinek wrote:
On Mon, Nov 17, 2025 at 04:48:30PM +0530, Jason Merrill wrote:
If this would be solely for GCC testsuite and nothing else, my preference
would be to do this in DejaGNU with // { dg-compile-std-module
But if the intent is also users compiling simple C++ sources, then that
doesn't work.

Indeed, it's intended more for users.

I wonder if expansion of --compile-std-module into
-xc++-system-header bits/stdc++.h -xc++-system-module bits/std.cc 
-xc++-system-module bits/std.compat.cc -xnone
won't cause surprising result for users which are using -x <language>.
E.g. if their C++ source is *.c and they compile with
g++ -xc++ --compile-std-module foo.c

Do people put other options between -x and their source files?

I'm afraid they do.  Especially when compiling multiple files in one command, 
they put
other options in between.

I suppose we
could insert the new options before any user-specified -x (or source file)
rather than at the position of the ---compile-std-module.

That would work too, sure.

This revision does that, adds docs, and also makes it work with -S -o so it will work on godbolt.

From c7c1f32a477c42de3fd9308e90d09b6a617893df Mon Sep 17 00:00:00 2001
From: Jason Merrill <[email protected]>
Date: Tue, 11 Nov 2025 15:58:01 +0530
Subject: [PATCH] driver/c++: add --compile-std-module
To: [email protected]

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/doc/invoke.texi |  9 +++++++++
 gcc/c-family/c.opt  |  4 ++++
 gcc/cp/lang-specs.h | 18 ++++++++++++++++++
 gcc/cp/g++spec.cc   | 37 +++++++++++++++++++++++++++++++++++++
 gcc/gcc.cc          | 11 +++++++++--
 5 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 6b3ba12e7dd..35e6a66ee44 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -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.
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..6fddb58862c 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,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)
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index eae7f07d962..9caaeb32f13 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -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;
 }
 
@@ -4994,7 +4996,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;
 	}
@@ -8980,6 +8983,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++;
-- 
2.51.0

Reply via email to