https://gcc.gnu.org/g:3ad2e2d707c3d6b0c6bd8c3ef0df4f7aaee1c3c2
commit r16-5581-g3ad2e2d707c3d6b0c6bd8c3ef0df4f7aaee1c3c2 Author: Jason Merrill <[email protected]> Date: Tue Nov 11 15:58:01 2025 +0530 driver/c++: add --compile-std-module 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. Diff: --- 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 +++++++-- gcc/testsuite/g++.dg/modules/compile-std1.C | 11 +++++++++ gcc/testsuite/g++.dg/modules/modules.exp | 3 ++- 7 files changed, 90 insertions(+), 3 deletions(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 284b9f4e802f..06723c2b3b62 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 0d97b0d13ce5..8da089780c4e 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 a67ce4c22a5d..90ad03c08128 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 165a91d45cd0..6fddb58862cc 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 9cc83d9ea346..fa46387e59a6 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; } @@ -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 index 000000000000..a7972be33c1b --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/compile-std1.C @@ -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; +} diff --git a/gcc/testsuite/g++.dg/modules/modules.exp b/gcc/testsuite/g++.dg/modules/modules.exp index 73b5de1397fa..3f2b8f2d5764 100644 --- a/gcc/testsuite/g++.dg/modules/modules.exp +++ b/gcc/testsuite/g++.dg/modules/modules.exp @@ -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] }
