p1689r5: initial support
commit024f135a1e9b8f8e102960357cae6e99e1dbe6eb
authorBen Boeckel <ben.boeckel@kitware.com>
Fri, 1 Sep 2023 13:04:02 +0000 (1 09:04 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 19 Sep 2023 21:32:23 +0000 (19 17:32 -0400)
treee03784e8f1a657a82139f213aa03f199f7d0cec9
parent1e44764bb381bf9594fcefdc95678a872adc1927
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>
43 files changed:
gcc/c-family/c-opts.cc
gcc/c-family/c.opt
gcc/cp/module.cc
gcc/doc/invoke.texi
gcc/gcc.cc
gcc/json.h
gcc/testsuite/g++.dg/modules/depflags-f-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-f.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fi.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fj-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fj-MF-share.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fj.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fjo-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fjo.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fo-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-fo.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-j-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-j.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-jo-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-jo.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-o-MD.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/depflags-o.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/modules.exp
gcc/testsuite/g++.dg/modules/p1689-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-1.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-2.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-3.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-4.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-5.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-file-default.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-file-default.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-target-default.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/p1689-target-default.exp.ddi [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/test-p1689.py [new file with mode: 0644]
gcc/testsuite/lib/modules.exp [new file with mode: 0644]
libcpp/include/cpplib.h
libcpp/include/mkdeps.h
libcpp/init.cc
libcpp/mkdeps.cc