aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorBen Boeckel <ben.boeckel@kitware.com>2023-09-01 09:04:02 -0400
committerJason Merrill <jason@redhat.com>2023-09-19 17:32:23 -0400
commit024f135a1e9b8f8e102960357cae6e99e1dbe6eb (patch)
treee03784e8f1a657a82139f213aa03f199f7d0cec9 /gcc
parent1e44764bb381bf9594fcefdc95678a872adc1927 (diff)
downloadgcc-024f135a1e9b8f8e102960357cae6e99e1dbe6eb.zip
gcc-024f135a1e9b8f8e102960357cae6e99e1dbe6eb.tar.gz
gcc-024f135a1e9b8f8e102960357cae6e99e1dbe6eb.tar.bz2
p1689r5: initial support
This patch implements support for [P1689R5][] to communicate to a build system the C++20 module dependencies to build systems so that they may build `.gcm` files in the proper order. Support is communicated through the following three new flags: - `-fdeps-format=` specifies the format for the output. Currently named `p1689r5`. - `-fdeps-file=` specifies the path to the file to write the format to. - `-fdeps-target=` specifies the `.o` that will be written for the TU that is scanned. This is required so that the build system can correlate the dependency output with the actual compilation that will occur. CMake supports this format as of 17 Jun 2022 (to be part of 3.25.0) using an experimental feature selection (to allow for future usage evolution without committing to how it works today). While it remains experimental, docs may be found in CMake's documentation for experimental features. Future work may include using this format for Fortran module dependencies as well, however this is still pending work. [P1689R5]: https://isocpp.org/files/papers/P1689R5.html [cmake-experimental]: https://gitlab.kitware.com/cmake/cmake/-/blob/master/Help/dev/experimental.rst TODO: - header-unit information fields Header units (including the standard library headers) are 100% unsupported right now because the `-E` mechanism wants to import their BMIs. A new mode (i.e., something more workable than existing `-E` behavior) that mocks up header units as if they were imported purely from their path and content would be required. - non-utf8 paths The current standard says that paths that are not unambiguously represented using UTF-8 are not supported (because these cases are rare and the extra complication is not worth it at this time). Future versions of the format might have ways of encoding non-UTF-8 paths. For now, this patch just doesn't support non-UTF-8 paths (ignoring the "unambiguously representable in UTF-8" case). - figure out why junk gets placed at the end of the file Sometimes it seems like the file gets a lot of `NUL` bytes appended to it. It happens rarely and seems to be the result of some `ftruncate`-style call which results in extra padding in the contents. Noting it here as an observation at least. libcpp/ * include/cpplib.h: Add cpp_fdeps_format enum. (cpp_options): Add fdeps_format field (cpp_finish): Add structured dependency fdeps_stream parameter. * include/mkdeps.h (deps_add_module_target): Add flag for whether a module is exported or not. (fdeps_add_target): Add function. (deps_write_p1689r5): Add function. * init.cc (cpp_finish): Add new preprocessor parameter used for C++ module tracking. * mkdeps.cc (mkdeps): Implement P1689R5 output. gcc/ * doc/invoke.texi: Document -fdeps-format=, -fdeps-file=, and -fdeps-target= flags. * gcc.cc: add defaults for -fdeps-target= and -fdeps-file= when only -fdeps-format= is specified. * json.h: Add a TODO item to refactor out to share with `libcpp/mkdeps.cc`. gcc/c-family/ * c-opts.cc (c_common_handle_option): Add fdeps_file variable and -fdeps-format=, -fdeps-file=, and -fdeps-target= parsing. * c.opt: Add -fdeps-format=, -fdeps-file=, and -fdeps-target= flags. gcc/cp/ * module.cc (preprocessed_module): Pass whether the module is exported to dependency tracking. gcc/testsuite/ * g++.dg/modules/depflags-f-MD.C: New test. * g++.dg/modules/depflags-f.C: New test. * g++.dg/modules/depflags-fi.C: New test. * g++.dg/modules/depflags-fj-MD.C: New test. * g++.dg/modules/depflags-fj.C: New test. * g++.dg/modules/depflags-fjo-MD.C: New test. * g++.dg/modules/depflags-fjo.C: New test. * g++.dg/modules/depflags-fo-MD.C: New test. * g++.dg/modules/depflags-fo.C: New test. * g++.dg/modules/depflags-j-MD.C: New test. * g++.dg/modules/depflags-j.C: New test. * g++.dg/modules/depflags-jo-MD.C: New test. * g++.dg/modules/depflags-jo.C: New test. * g++.dg/modules/depflags-o-MD.C: New test. * g++.dg/modules/depflags-o.C: New test. * g++.dg/modules/p1689-1.C: New test. * g++.dg/modules/p1689-1.exp.ddi: New test expectation. * g++.dg/modules/p1689-2.C: New test. * g++.dg/modules/p1689-2.exp.ddi: New test expectation. * g++.dg/modules/p1689-3.C: New test. * g++.dg/modules/p1689-3.exp.ddi: New test expectation. * g++.dg/modules/p1689-4.C: New test. * g++.dg/modules/p1689-4.exp.ddi: New test expectation. * g++.dg/modules/p1689-5.C: New test. * g++.dg/modules/p1689-5.exp.ddi: New test expectation. * g++.dg/modules/modules.exp: Load new P1689 library routines. * g++.dg/modules/test-p1689.py: New tool for validating P1689 output. * lib/modules.exp: Support for validating P1689 outputs. Signed-off-by: Ben Boeckel <ben.boeckel@kitware.com> Reviewed-by: Jason Merrill <jason@redhat.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/c-family/c-opts.cc44
-rw-r--r--gcc/c-family/c.opt12
-rw-r--r--gcc/cp/module.cc3
-rw-r--r--gcc/doc/invoke.texi27
-rw-r--r--gcc/gcc.cc4
-rw-r--r--gcc/json.h3
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-f-MD.C2
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-f.C3
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fi.C4
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fj-MD.C3
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fj-MF-share.C6
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fj.C4
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fjo-MD.C4
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fjo.C5
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fo-MD.C3
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-fo.C4
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-j-MD.C2
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-j.C3
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-jo-MD.C3
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-jo.C4
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-o-MD.C2
-rw-r--r--gcc/testsuite/g++.dg/modules/depflags-o.C3
-rw-r--r--gcc/testsuite/g++.dg/modules/modules.exp1
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-1.C17
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-1.exp.ddi27
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-2.C15
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-2.exp.ddi16
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-3.C13
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-3.exp.ddi16
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-4.C13
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-4.exp.ddi14
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-5.C13
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-5.exp.ddi14
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-file-default.C16
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-file-default.exp.ddi27
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-target-default.C16
-rw-r--r--gcc/testsuite/g++.dg/modules/p1689-target-default.exp.ddi27
-rw-r--r--gcc/testsuite/g++.dg/modules/test-p1689.py222
-rw-r--r--gcc/testsuite/lib/modules.exp71
39 files changed, 683 insertions, 3 deletions
diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index d9f55f4..fe2d143 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -77,6 +77,9 @@ static bool verbose;
/* Dependency output file. */
static const char *deps_file;
+/* Structured dependency output file. */
+static const char *fdeps_file;
+
/* The prefix given by -iprefix, if any. */
static const char *iprefix;
@@ -361,6 +364,24 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
deps_file = arg;
break;
+ case OPT_fdeps_format_:
+ /* https://wg21.link/p1689r5 */
+ if (!strcmp (arg, "p1689r5"))
+ cpp_opts->deps.fdeps_format = FDEPS_FMT_P1689R5;
+ else
+ error ("%<-fdeps-format=%> unknown format %<%s%>", arg);
+ break;
+
+ case OPT_fdeps_file_:
+ deps_seen = true;
+ fdeps_file = arg;
+ break;
+
+ case OPT_fdeps_target_:
+ deps_seen = true;
+ defer_opt (code, arg);
+ break;
+
case OPT_MF:
deps_seen = true;
deps_file = arg;
@@ -1281,6 +1302,7 @@ void
c_common_finish (void)
{
FILE *deps_stream = NULL;
+ FILE *fdeps_stream = NULL;
/* Note that we write the dependencies even if there are errors. This is
useful for handling outdated generated headers that now trigger errors
@@ -1309,9 +1331,27 @@ c_common_finish (void)
locations with input_location, which would be incorrect now. */
override_libcpp_locations = false;
+ if (cpp_opts->deps.fdeps_format != FDEPS_FMT_NONE)
+ {
+ if (!fdeps_file)
+ fdeps_stream = out_stream;
+ else if (fdeps_file[0] == '-' && fdeps_file[1] == '\0')
+ fdeps_stream = stdout;
+ else
+ {
+ fdeps_stream = fopen (fdeps_file, "w");
+ if (!fdeps_stream)
+ fatal_error (input_location, "opening dependency file %s: %m",
+ fdeps_file);
+ }
+ if (fdeps_stream == deps_stream && fdeps_stream != stdout)
+ fatal_error (input_location, "%<-MF%> and %<-fdeps-file=%> cannot share an output file %s: %m",
+ fdeps_file);
+ }
+
/* For performance, avoid tearing down cpplib's internal structures
with cpp_destroy (). */
- cpp_finish (parse_in, deps_stream);
+ cpp_finish (parse_in, deps_stream, fdeps_stream);
if (deps_stream && deps_stream != out_stream && deps_stream != stdout
&& (ferror (deps_stream) || fclose (deps_stream)))
@@ -1383,6 +1423,8 @@ handle_deferred_opts (void)
if (opt->code == OPT_MT || opt->code == OPT_MQ)
deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
+ else if (opt->code == OPT_fdeps_target_)
+ fdeps_add_target (deps, opt->arg, true);
}
}
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 7348ad4..44b9c86 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -256,6 +256,18 @@ MT
C ObjC C++ ObjC++ Joined Separate MissingArgError(missing makefile target after %qs)
-MT <target> Add a target that does not require quoting.
+fdeps-format=
+C ObjC C++ ObjC++ NoDriverArg Joined MissingArgError(missing format after %qs)
+Structured format for output dependency information. Supported (\"p1689r5\").
+
+fdeps-file=
+C ObjC C++ ObjC++ NoDriverArg Joined MissingArgError(missing output path after %qs)
+File for output dependency information.
+
+fdeps-target=
+C ObjC C++ ObjC++ NoDriverArg Joined MissingArgError(missing path after %qs)
+-fdeps-target=obj.o Output file for the compile step.
+
P
C ObjC C++ ObjC++
Do not generate #line directives.
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index ea362bd..9df60d6 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -19834,7 +19834,8 @@ preprocessed_module (cpp_reader *reader)
&& (module->is_interface () || module->is_partition ()))
deps_add_module_target (deps, module->get_flatname (),
maybe_add_cmi_prefix (module->filename),
- module->is_header());
+ module->is_header (),
+ module->is_exported ());
else
deps_add_module_dep (deps, module->get_flatname ());
}
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 03d93e6..09b2b92 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -2830,6 +2830,33 @@ is @option{-fpermitted-flt-eval-methods=c11}. The default when in a GNU
dialect (@option{-std=gnu11} or similar) is
@option{-fpermitted-flt-eval-methods=ts-18661-3}.
+@opindex fdeps-
+The @samp{-fdeps-*} options are used to extract structured dependency
+information for a source. This involves determining what resources provided by
+other source files will be required to compile the source as well as what
+resources are provided by the source. This information can be used to add
+required dependencies between compilation rules of dependent sources based on
+their contents rather than requiring such information be reflected within the
+build tools as well.
+
+@opindex fdeps-file
+@item -fdeps-file=@var{file}
+Where to write structured dependency information.
+
+@opindex fdeps-format
+@item -fdeps-format=@var{format}
+The format to use for structured dependency information. @samp{p1689r5} is the
+only supported format right now. Note that when this argument is specified, the
+output of @samp{-MF} is stripped of some information (namely C++ modules) so
+that it does not use extended makefile syntax not understood by most tools.
+
+@opindex fdeps-target
+@item -fdeps-target=@var{file}
+Analogous to @option{-MT} but for structured dependency information. This
+indicates the target which will ultimately need any required resources and
+provide any resources extracted from the source that may be required by other
+sources.
+
@opindex fplan9-extensions
@item -fplan9-extensions
Accept some non-standard constructs used in Plan 9 code.
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 586fdb5..c6e600f 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -1236,7 +1236,9 @@ static const char *cpp_unique_options =
%{remap} %{%:debug-level-gt(2):-dD}\
%{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
%{H} %C %{D*&U*&A*} %{i*} %Z %i\
- %{E|M|MM:%W{o*}}";
+ %{E|M|MM:%W{o*}}\
+ %{fdeps-format=*:%{!fdeps-file=*:-fdeps-file=%:join(%{!o:%b.ddi}%{o*:%.ddi%*})}}\
+ %{fdeps-format=*:%{!fdeps-target=*:-fdeps-target=%:join(%{!o:%b.o}%{o*:%.o%*})}}";
/* This contains cpp options which are common with cc1_options and are passed
only when preprocessing only to avoid duplication. We pass the cc1 spec
diff --git a/gcc/json.h b/gcc/json.h
index 057119d..6cc58c8 100644
--- a/gcc/json.h
+++ b/gcc/json.h
@@ -30,6 +30,9 @@ along with GCC; see the file COPYING3. If not see
Supports creating a DOM-like tree of json::value *, and then dumping
json::value * to text. */
+/* TODO: `libcpp/mkdeps.cc` wants JSON writing support for p1689r5 output;
+ extract this code and move to libiberty. */
+
namespace json
{
diff --git a/gcc/testsuite/g++.dg/modules/depflags-f-MD.C b/gcc/testsuite/g++.dg/modules/depflags-f-MD.C
new file mode 100644
index 0000000..2bd980c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-f-MD.C
@@ -0,0 +1,2 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-format=p1689r5 }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-f.C b/gcc/testsuite/g++.dg/modules/depflags-f.C
new file mode 100644
index 0000000..789b18f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-f.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -fdeps-format=p1689r5 }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fi.C b/gcc/testsuite/g++.dg/modules/depflags-fi.C
new file mode 100644
index 0000000..d8e75ad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fi.C
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdeps-format=invalid }
+
+// { dg-prune-output "error: '-fdeps-format=' unknown format 'invalid'" }
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fj-MD.C b/gcc/testsuite/g++.dg/modules/depflags-fj-MD.C
new file mode 100644
index 0000000..3a94523
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fj-MD.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-format=p1689r5 }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fj-MF-share.C b/gcc/testsuite/g++.dg/modules/depflags-fj-MF-share.C
new file mode 100644
index 0000000..723014f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fj-MF-share.C
@@ -0,0 +1,6 @@
+// { dg-additional-options -MD }
+// { dg-additional-options "-MF depflags-3.ddi" }
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-format=p1689r5 }
+
+// { dg-prune-output "error: '-MF' and '-fdeps-file=' cannot share an output file" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fj.C b/gcc/testsuite/g++.dg/modules/depflags-fj.C
new file mode 100644
index 0000000..063e0ee
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fj.C
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-format=p1689r5 }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fjo-MD.C b/gcc/testsuite/g++.dg/modules/depflags-fjo-MD.C
new file mode 100644
index 0000000..84da337
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fjo-MD.C
@@ -0,0 +1,4 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-target=depflags-1.C }
+// { dg-additional-options -fdeps-format=p1689r5 }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fjo.C b/gcc/testsuite/g++.dg/modules/depflags-fjo.C
new file mode 100644
index 0000000..760d8f2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fjo.C
@@ -0,0 +1,5 @@
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-target=depflags-1.C }
+// { dg-additional-options -fdeps-format=p1689r5 }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fo-MD.C b/gcc/testsuite/g++.dg/modules/depflags-fo-MD.C
new file mode 100644
index 0000000..6da03f9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fo-MD.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=depflags-1.C }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-fo.C b/gcc/testsuite/g++.dg/modules/depflags-fo.C
new file mode 100644
index 0000000..5deb27a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-fo.C
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=depflags-1.C }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-j-MD.C b/gcc/testsuite/g++.dg/modules/depflags-j-MD.C
new file mode 100644
index 0000000..92a2af6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-j-MD.C
@@ -0,0 +1,2 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-j.C b/gcc/testsuite/g++.dg/modules/depflags-j.C
new file mode 100644
index 0000000..2eb5890
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-j.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-jo-MD.C b/gcc/testsuite/g++.dg/modules/depflags-jo-MD.C
new file mode 100644
index 0000000..ba2b21e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-jo-MD.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-target=depflags-1.C }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-jo.C b/gcc/testsuite/g++.dg/modules/depflags-jo.C
new file mode 100644
index 0000000..a015d35
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-jo.C
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdeps-file=depflags-3.ddi }
+// { dg-additional-options -fdeps-target=depflags-1.C }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-o-MD.C b/gcc/testsuite/g++.dg/modules/depflags-o-MD.C
new file mode 100644
index 0000000..1b67558
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-o-MD.C
@@ -0,0 +1,2 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdeps-target=depflags-1.C }
diff --git a/gcc/testsuite/g++.dg/modules/depflags-o.C b/gcc/testsuite/g++.dg/modules/depflags-o.C
new file mode 100644
index 0000000..38d6450
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/depflags-o.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -fdeps-target=depflags-1.C }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
diff --git a/gcc/testsuite/g++.dg/modules/modules.exp b/gcc/testsuite/g++.dg/modules/modules.exp
index dc302d3..b692954 100644
--- a/gcc/testsuite/g++.dg/modules/modules.exp
+++ b/gcc/testsuite/g++.dg/modules/modules.exp
@@ -28,6 +28,7 @@
# { dg-module-do [link|run] [xfail] [options] } # link [and run]
load_lib g++-dg.exp
+load_lib modules.exp
# If a testcase doesn't have special options, use these.
global DEFAULT_CXXFLAGS
diff --git a/gcc/testsuite/g++.dg/modules/p1689-1.C b/gcc/testsuite/g++.dg/modules/p1689-1.C
new file mode 100644
index 0000000..3c138c8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-1.C
@@ -0,0 +1,17 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-1.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=p1689-1.o }
+// { dg-additional-options -fdeps-file=p1689-1.ddi }
+
+// Export a module that uses modules, re-exports modules, and partitions.
+
+export module foo;
+export import foo:part1;
+import foo:part2;
+
+export import bar;
+
+// { dg-final { run-check-p1689-valid p1689-1.ddi p1689-1.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-1.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-1.exp.ddi
new file mode 100644
index 0000000..c5648ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-1.exp.ddi
@@ -0,0 +1,27 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-1.o",
+ "provides": [
+ {
+ "logical-name": "foo",
+ "is-interface": true
+ }
+ ],
+ "requires": [
+ "__P1689_unordered__",
+ {
+ "logical-name": "bar"
+ },
+ {
+ "logical-name": "foo:part1"
+ },
+ {
+ "logical-name": "foo:part2"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/p1689-2.C b/gcc/testsuite/g++.dg/modules/p1689-2.C
new file mode 100644
index 0000000..5d7fe52
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-2.C
@@ -0,0 +1,15 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-2.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=p1689-2.o }
+// { dg-additional-options -fdeps-file=p1689-2.ddi }
+
+// Export a module partition that uses modules.
+
+export module foo:part1;
+
+#include <iostream>
+
+// { dg-final { run-check-p1689-valid p1689-2.ddi p1689-2.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-2.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-2.exp.ddi
new file mode 100644
index 0000000..6901172
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-2.exp.ddi
@@ -0,0 +1,16 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-2.o",
+ "provides": [
+ {
+ "logical-name": "foo:part1",
+ "is-interface": true
+ }
+ ],
+ "requires": []
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/p1689-3.C b/gcc/testsuite/g++.dg/modules/p1689-3.C
new file mode 100644
index 0000000..6c0aced
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-3.C
@@ -0,0 +1,13 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-3.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=p1689-3.o }
+// { dg-additional-options -fdeps-file=p1689-3.ddi }
+
+// Provide a module partition.
+
+module foo:part2;
+
+// { dg-final { run-check-p1689-valid p1689-3.ddi p1689-3.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-3.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-3.exp.ddi
new file mode 100644
index 0000000..5a40bea
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-3.exp.ddi
@@ -0,0 +1,16 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-3.o",
+ "provides": [
+ {
+ "logical-name": "foo:part2",
+ "is-interface": false
+ }
+ ],
+ "requires": []
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/p1689-4.C b/gcc/testsuite/g++.dg/modules/p1689-4.C
new file mode 100644
index 0000000..922f015
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-4.C
@@ -0,0 +1,13 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-4.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=p1689-4.o }
+// { dg-additional-options -fdeps-file=p1689-4.ddi }
+
+// Module implementation unit.
+
+module foo;
+
+// { dg-final { run-check-p1689-valid p1689-4.ddi p1689-4.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-4.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-4.exp.ddi
new file mode 100644
index 0000000..b119f565
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-4.exp.ddi
@@ -0,0 +1,14 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-4.o",
+ "requires": []
+ {
+ "logical-name": "foo"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/p1689-5.C b/gcc/testsuite/g++.dg/modules/p1689-5.C
new file mode 100644
index 0000000..94908bf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-5.C
@@ -0,0 +1,13 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-5.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=p1689-5.o }
+// { dg-additional-options -fdeps-file=p1689-5.ddi }
+
+// Use modules, don't provide anything.
+
+import bar;
+
+// { dg-final { run-check-p1689-valid p1689-5.ddi p1689-5.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-5.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-5.exp.ddi
new file mode 100644
index 0000000..18704ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-5.exp.ddi
@@ -0,0 +1,14 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-5.o",
+ "requires": [
+ {
+ "logical-name": "bar"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/p1689-file-default.C b/gcc/testsuite/g++.dg/modules/p1689-file-default.C
new file mode 100644
index 0000000..4e1f5bb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-file-default.C
@@ -0,0 +1,16 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-file-default.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-target=p1689-file-default.o }
+
+// Scan without `-fdeps-file=`
+
+export module foo;
+export import foo:part1;
+import foo:part2;
+
+export import bar;
+
+// { dg-final { run-check-p1689-valid p1689-file-default.o.ddi p1689-file-default.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-file-default.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-file-default.exp.ddi
new file mode 100644
index 0000000..187eab6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-file-default.exp.ddi
@@ -0,0 +1,27 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-file-default.o",
+ "provides": [
+ {
+ "logical-name": "foo",
+ "is-interface": true
+ }
+ ],
+ "requires": [
+ "__P1689_unordered__",
+ {
+ "logical-name": "bar"
+ },
+ {
+ "logical-name": "foo:part1"
+ },
+ {
+ "logical-name": "foo:part2"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/p1689-target-default.C b/gcc/testsuite/g++.dg/modules/p1689-target-default.C
new file mode 100644
index 0000000..67088ed
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-target-default.C
@@ -0,0 +1,16 @@
+// { dg-additional-options -E }
+// { dg-additional-options "-MT p1689-target-default.ddi" }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdeps-format=p1689r5 }
+// { dg-additional-options -fdeps-file=p1689-target-default.ddi }
+
+// Scan without `-fdeps-target=`
+
+export module foo;
+export import foo:part1;
+import foo:part2;
+
+export import bar;
+
+// { dg-final { run-check-p1689-valid p1689-target-default.ddi p1689-target-default.exp.ddi } }
diff --git a/gcc/testsuite/g++.dg/modules/p1689-target-default.exp.ddi b/gcc/testsuite/g++.dg/modules/p1689-target-default.exp.ddi
new file mode 100644
index 0000000..dd3d9c4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/p1689-target-default.exp.ddi
@@ -0,0 +1,27 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-target-default.o",
+ "provides": [
+ {
+ "logical-name": "foo",
+ "is-interface": true
+ }
+ ],
+ "requires": [
+ "__P1689_unordered__",
+ {
+ "logical-name": "bar"
+ },
+ {
+ "logical-name": "foo:part1"
+ },
+ {
+ "logical-name": "foo:part2"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
diff --git a/gcc/testsuite/g++.dg/modules/test-p1689.py b/gcc/testsuite/g++.dg/modules/test-p1689.py
new file mode 100644
index 0000000..2f07cc3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/test-p1689.py
@@ -0,0 +1,222 @@
+import json
+
+
+# Parameters.
+ALL_ERRORS = False
+REPLACEMENTS = {}
+
+
+def _print_path(path):
+ '''Format a JSON path for output.'''
+ return '/'.join(path)
+
+
+def _report_error(msg):
+ '''Report an error.'''
+ full_msg = 'ERROR: ' + msg
+ if ALL_ERRORS:
+ print(full_msg)
+ else:
+ raise RuntimeError(full_msg)
+
+
+def _error_type_mismatch(path, actual, expect):
+ '''Report that there is a type mismatch.'''
+ _report_error('type mismatch at %s: actual: "%s" expect: "%s"' % (_print_path(path), actual, expect))
+
+
+def _error_unknown_type(path, typ):
+ '''Report that there is an unknown type in the JSON object.'''
+ _report_error('unknown type at %s: "%s"' % (_print_path(path), typ))
+
+
+def _error_length_mismatch(path, actual, expect):
+ '''Report a length mismatch in an object or array.'''
+ _report_error('length mismatch at %s: actual: "%s" expect: "%s"' % (_print_path(path), actual, expect))
+
+
+def _error_unexpect_value(path, actual, expect):
+ '''Report a value mismatch.'''
+ _report_error('value mismatch at %s: actual: "%s" expect: "%s"' % (_print_path(path), actual, expect))
+
+
+def _error_extra_key(path, key):
+ '''Report on a key that is unexpected.'''
+ _report_error('extra key at %s: "%s"' % (_print_path(path), key))
+
+
+def _error_missing_key(path, key):
+ '''Report on a key that is missing.'''
+ _report_error('extra key at %s: %s' % (_print_path(path), key))
+
+
+def _compare_object(path, actual, expect):
+ '''Compare a JSON object.'''
+ is_ok = True
+
+ if not len(actual) == len(expect):
+ _error_length_mismatch(path, len(actual), len(expect))
+ is_ok = False
+
+ for key in actual:
+ if key not in expect:
+ _error_extra_key(path, key)
+ is_ok = False
+ else:
+ sub_error = compare_json(path + [key], actual[key], expect[key])
+ if sub_error:
+ is_ok = False
+
+ for key in expect:
+ if key not in actual:
+ _error_missing_key(path, key)
+ is_ok = False
+
+ return is_ok
+
+
+def _compare_array(path, actual, expect):
+ '''Compare a JSON array.'''
+ is_ok = True
+
+ if not len(actual) == len(expect):
+ _error_length_mismatch(path, len(actual), len(expect))
+ is_ok = False
+
+ for (idx, (a, e)) in enumerate(zip(actual, expect)):
+ sub_error = compare_json(path + [str(idx)], a, e)
+ if sub_error:
+ is_ok = False
+
+ return is_ok
+
+
+def _make_replacements(value):
+ for (old, new) in REPLACEMENTS.values():
+ value = value.replace(old, new)
+ return value
+
+
+def _compare_string(path, actual, expect):
+ '''Compare a JSON string supporting replacements in the expected output.'''
+ expect = _make_replacements(expect)
+
+ if not actual == expect:
+ _error_unexpect_value(path, actual, expect)
+ return False
+ else:
+ print('%s is ok: %s' % (_print_path(path), actual))
+ return True
+
+
+def _compare_number(path, actual, expect):
+ '''Compare a JSON integer.'''
+ if not actual == expect:
+ _error_unexpect_value(path, actual, expect)
+ return False
+ else:
+ print('%s is ok: %s' % (_print_path(path), actual))
+ return True
+
+
+def _inspect_ordering(arr):
+ req_ordering = True
+
+ if not arr:
+ return arr, req_ordering
+
+ if arr[0] == '__P1689_unordered__':
+ arr.pop(0)
+ req_ordering = False
+
+ return arr, req_ordering
+
+
+def compare_json(path, actual, expect):
+ actual_type = type(actual)
+ expect_type = type(expect)
+
+ is_ok = True
+
+ if not actual_type == expect_type:
+ _error_type_mismatch(path, actual_type, expect_type)
+ is_ok = False
+ elif actual_type == dict:
+ is_ok = _compare_object(path, actual, expect)
+ elif actual_type == list:
+ expect, req_ordering = _inspect_ordering(expect)
+ if not req_ordering:
+ actual = set(actual)
+ expect = set(expect)
+ is_ok = _compare_array(path, actual, expect)
+ elif actual_type == str:
+ is_ok = _compare_string(path, actual, expect)
+ elif actual_type == float:
+ is_ok = _compare_number(path, actual, expect)
+ elif actual_type == int:
+ is_ok = _compare_number(path, actual, expect)
+ elif actual_type == bool:
+ is_ok = _compare_number(path, actual, expect)
+ elif actual_type == type(None):
+ pass
+ else:
+ _error_unknown_type(path, actual_type)
+ is_ok = False
+
+ return is_ok
+
+
+def validate_p1689(actual, expect):
+ '''Validate a P1689 file against an expected output file.
+
+ Returns `False` if it fails, `True` if they are the same.
+ '''
+ with open(actual, 'r') as fin:
+ actual_content = fin.read()
+ with open(expect, 'r') as fin:
+ expect_content = fin.read()
+
+ actual_json = json.loads(actual_content)
+ expect_json = json.loads(expect_content)
+
+ return compare_json([], actual_json, expect_json)
+
+
+if __name__ == '__main__':
+ import sys
+
+ actual = None
+ expect = None
+
+ # Parse arguments.
+ args = sys.argv[1:]
+ while args:
+ # Take an argument.
+ arg = args.pop(0)
+
+ # Parse out replacement expressions.
+ if arg == '-r' or arg == '--replace':
+ replacement = args.pop(0)
+ (key, value) = replacement.split('=', maxsplit=1)
+ REPLACEMENTS[key] = value
+ # Flag to change how errors are reported.
+ elif arg == '-A' or arg == '--all':
+ ALL_ERRORS = True
+ # Required arguments.
+ elif arg == '-a' or arg == '--actual':
+ actual = args.pop(0)
+ elif arg == '-e' or arg == '--expect':
+ expect = args.pop(0)
+
+ # Validate that we have the required arguments.
+ if actual is None:
+ raise RuntimeError('missing "actual" file')
+ if expect is None:
+ raise RuntimeError('missing "expect" file')
+
+ # Do the actual work.
+ is_ok = validate_p1689(actual, expect)
+
+ # Fail if errors are found.
+ if not is_ok:
+ sys.exit(1)
diff --git a/gcc/testsuite/lib/modules.exp b/gcc/testsuite/lib/modules.exp
new file mode 100644
index 0000000..d466a73
--- /dev/null
+++ b/gcc/testsuite/lib/modules.exp
@@ -0,0 +1,71 @@
+# Copyright (C) 1997-2022 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# <http://www.gnu.org/licenses/>.
+
+# Verify various kinds of gcov output: line counts, branch percentages,
+# and call return percentages. None of this is language-specific.
+
+load_lib "target-supports.exp"
+
+#
+# clean-p1689-file -- delete a working file the compiler creates for p1689
+#
+# TESTCASE is the name of the test.
+# SUFFIX is file suffix
+
+proc clean-p1689-file { testcase suffix } {
+ set basename [file tail $testcase]
+ set base [file rootname $basename]
+ remote_file host delete $base.$suffix
+}
+
+#
+# clean-p1689 -- delete the working files the compiler creates for p1689
+#
+# TESTCASE is the name of the test.
+#
+proc clean-p1689 { testcase } {
+ clean-p1689-file $testcase "d"
+ clean-p1689-file $testcase "ddi"
+}
+
+# Call by dg-final to check a P1689 dependency file
+
+proc run-check-p1689-valid { depfile template } {
+ global srcdir subdir
+ # Extract the test file name from the arguments.
+ set testcase [file rootname [file tail $depfile]]
+
+ verbose "Running P1689 validation for $testcase in $srcdir/$subdir" 2
+ set testcase [remote_download host $testcase]
+
+ set pytest_script "test-p1689.py"
+ if { ![check_effective_target_recent_python3] } {
+ unsupported "$pytest_script python3 is missing"
+ return
+ }
+
+ verbose "running script" 1
+ spawn -noecho python3 $srcdir/$subdir/$pytest_script --all --actual $depfile --expect $srcdir/$subdir/$template
+
+ expect {
+ -re "ERROR: (\[^\r\n\]*)" {
+ fail $expect_out(0,string)
+ exp_continue
+ }
+ }
+
+ clean-p1689 $testcase
+}