@@ -1,3 +1,8 @@
+2022-10-27 Ben Boeckel <ben.boeckel@kitware.com>
+
+ * doc/invoke.texi: Document -fdeps-format=, -fdep-file=, and
+ -fdep-output= flags.
+
2022-10-26 David Faust <david.faust@oracle.com>
* config/bpf/bpf.cc: Support __builtin_preserve_field_info.
@@ -1,3 +1,9 @@
+2022-10-27 Ben Boeckel <ben.boeckel@kitware.com>
+
+ * c-opts.cc (c_common_handle_option): Add fdeps_file variable and
+ -fdeps-format=, -fdep-file=, and -fdep-output= parsing.
+ * c.opt: Add -fdeps-format=, -fdep-file=, and -fdep-output= flags.
+
2022-10-26 Marek Polacek <polacek@redhat.com>
PR c++/106393
@@ -77,6 +77,9 @@ static bool verbose;
/* Dependency output file. */
static const char *deps_file;
+/* Enhanced dependency output file. */
+static const char *fdeps_file;
+
/* The prefix given by -iprefix, if any. */
static const char *iprefix;
@@ -360,6 +363,23 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
deps_file = arg;
break;
+ case OPT_fdep_format_:
+ if (!strcmp (arg, "p1689r5"))
+ cpp_opts->deps.format = DEPS_FMT_P1689R5;
+ else
+ error ("%<-fdep-format=%> unknown format %<%s%>", arg);
+ break;
+
+ case OPT_fdep_file_:
+ deps_seen = true;
+ fdeps_file = arg;
+ break;
+
+ case OPT_fdep_output_:
+ deps_seen = true;
+ defer_opt (code, arg);
+ break;
+
case OPT_MF:
deps_seen = true;
deps_file = arg;
@@ -1271,6 +1291,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
@@ -1299,9 +1320,24 @@ c_common_finish (void)
locations with input_location, which would be incorrect now. */
override_libcpp_locations = false;
+ if (cpp_opts->deps.format != DEPS_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);
+ }
+ }
+
/* 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)))
@@ -1373,6 +1409,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_fdep_output_)
+ deps_add_output (deps, opt->arg, true);
}
}
@@ -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.
+fdep-format=
+C ObjC C++ ObjC++ NoDriverArg Joined MissingArgError(missing format after %qs)
+Format for output dependency information. Supported (\"p1689r5\").
+
+fdep-file=
+C ObjC C++ ObjC++ NoDriverArg Joined MissingArgError(missing output path after %qs)
+File for output dependency information.
+
+fdep-output=
+C ObjC C++ ObjC++ NoDriverArg Joined MissingArgError(missing path after %qs)
+-fdep-output=obj.o Output file for the compile step.
+
P
C ObjC C++ ObjC++
Do not generate #line directives.
@@ -1,3 +1,8 @@
+2022-10-27 Ben Boeckel <ben.boeckel@kitware.com>
+
+ * module.cc (preprocessed_module): Pass whether the module is
+ exported to dependency tracking.
+
2022-10-26 Marek Polacek <polacek@redhat.com>
PR c++/106393
@@ -19794,7 +19794,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 ());
}
@@ -2786,6 +2786,21 @@ 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}.
+@item -fdep-file=@var{file}
+@opindex fdep-file
+Where to write structured dependency information.
+
+@item -fdep-format=@var{format}
+@opindex fdep-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.
+
+@item -fdep-output=@var{file}
+@opindex fdep-output
+Analogous to @option{-MT} but for structured dependency information.
+
@item -fplan9-extensions
@opindex fplan9-extensions
Accept some non-standard constructs used in Plan 9 code.
@@ -1,3 +1,10 @@
+2022-10-27 Ben Boeckel <ben.boeckel@kitware.com>
+
+ * g++.dg/modules/depflags*: New tests.
+ * g++.dg/modules/p1689*: New tests.
+ * g++.dg/modules/test-p1689.py: New tool for validating P1689 output.
+ * lib/modules.exp: Support for validating P1689 outputs.
+
2022-10-26 David Malcolm <dmalcolm@redhat.com>
* gcc.dg/analyzer/fd-3.c (test_5): Expect "opened here" message
new file mode 100644
@@ -0,0 +1,2 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-format=p1689r5 }
new file mode 100644
@@ -0,0 +1 @@
+// { dg-additional-options -fdep-format=p1689r5 }
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-additional-options -fdep-format=invalid }
+
+// { dg-prune-output "error: '-fdep-format=' unknown format 'invalid'" }
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-file=depflags-3.json }
+// { dg-additional-options -fdep-format=p1689r5 }
new file mode 100644
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdep-file=depflags-3.json }
+// { dg-additional-options -fdep-format=p1689r5 }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
new file mode 100644
@@ -0,0 +1,4 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-file=depflags-3.json }
+// { dg-additional-options -fdep-output=depflags-1.C }
+// { dg-additional-options -fdep-format=p1689r5 }
new file mode 100644
@@ -0,0 +1,5 @@
+// { dg-additional-options -fdep-file=depflags-3.json }
+// { dg-additional-options -fdep-output=depflags-1.C }
+// { dg-additional-options -fdep-format=p1689r5 }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=depflags-1.C }
new file mode 100644
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=depflags-1.C }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
new file mode 100644
@@ -0,0 +1,2 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-file=depflags-3.json }
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-additional-options -fdep-file=depflags-3.json }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-file=depflags-3.json }
+// { dg-additional-options -fdep-output=depflags-1.C }
new file mode 100644
@@ -0,0 +1,4 @@
+// { dg-additional-options -fdep-file=depflags-3.json }
+// { dg-additional-options -fdep-output=depflags-1.C }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
new file mode 100644
@@ -0,0 +1,2 @@
+// { dg-additional-options -MD }
+// { dg-additional-options -fdep-output=depflags-1.C }
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-additional-options -fdep-output=depflags-1.C }
+
+// { dg-prune-output "error: to generate dependencies you must specify either '-M' or '-MM'" }
@@ -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
@@ -237,6 +238,13 @@ proc cleanup_module_files { files } {
}
}
+# delete the specified set of dep files
+proc cleanup_dep_files { files } {
+ foreach file $files {
+ file_on_host delete $file
+ }
+}
+
global testdir
set testdir $srcdir/$subdir
proc srcdir {} {
@@ -310,6 +318,7 @@ foreach src [lsort [find $srcdir/$subdir {*_a.[CHX}]] {
set std_list [module-init $src]
foreach std $std_list {
set mod_files {}
+ set dep_files {}
global module_do
set module_do {"compile" "P"}
set asm_list {}
@@ -346,6 +355,8 @@ foreach src [lsort [find $srcdir/$subdir {*_a.[CHX}]] {
set mod_files [find $DEFAULT_REPO *.gcm]
}
cleanup_module_files $mod_files
+
+ cleanup_dep_files $dep_files
}
}
}
new file mode 100644
@@ -0,0 +1,18 @@
+// { dg-additional-options -E }
+// { dg-additional-options -MT }
+// { dg-additional-options p1689-1.json }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=p1689-1.o }
+// { dg-additional-options -fdep-file=p1689-1.json }
+
+// 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.json p1689-1.exp.json } }
new file mode 100644
@@ -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
+}
new file mode 100644
@@ -0,0 +1,16 @@
+// { dg-additional-options -E }
+// { dg-additional-options -MT }
+// { dg-additional-options p1689-2.json }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=p1689-2.o }
+// { dg-additional-options -fdep-file=p1689-2.json }
+
+// Export a module partition that uses modules.
+
+export module foo:part1;
+
+#include <iostream>
+
+// { dg-final { run-check-p1689-valid p1689-2.json p1689-2.exp.json } }
new file mode 100644
@@ -0,0 +1,16 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-2.o",
+ "provides": [
+ {
+ "logical-name": "foo:part1",
+ "is-interface": true
+ }
+ ],
+ "requires": []
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
new file mode 100644
@@ -0,0 +1,14 @@
+// { dg-additional-options -E }
+// { dg-additional-options -MT }
+// { dg-additional-options p1689-3.json }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=p1689-3.o }
+// { dg-additional-options -fdep-file=p1689-3.json }
+
+// Provide a module partition.
+
+module foo:part2;
+
+// { dg-final { run-check-p1689-valid p1689-3.json p1689-3.exp.json } }
new file mode 100644
@@ -0,0 +1,16 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-3.o",
+ "provides": [
+ {
+ "logical-name": "foo:part2",
+ "is-interface": false
+ }
+ ],
+ "requires": []
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
new file mode 100644
@@ -0,0 +1,14 @@
+// { dg-additional-options -E }
+// { dg-additional-options -MT }
+// { dg-additional-options p1689-4.json }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=p1689-4.o }
+// { dg-additional-options -fdep-file=p1689-4.json }
+
+// Module implementation unit.
+
+module foo;
+
+// { dg-final { run-check-p1689-valid p1689-4.json p1689-4.exp.json } }
new file mode 100644
@@ -0,0 +1,14 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-4.o",
+ "requires": []
+ {
+ "logical-name": "foo"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
new file mode 100644
@@ -0,0 +1,14 @@
+// { dg-additional-options -E }
+// { dg-additional-options -MT }
+// { dg-additional-options p1689-5.json }
+// { dg-additional-options -MD }
+// { dg-additional-options -fmodules-ts }
+// { dg-additional-options -fdep-format=p1689r5 }
+// { dg-additional-options -fdep-output=p1689-5.o }
+// { dg-additional-options -fdep-file=p1689-5.json }
+
+// Use modules, don't provide anything.
+
+import bar;
+
+// { dg-final { run-check-p1689-valid p1689-5.json p1689-5.exp.json } }
new file mode 100644
@@ -0,0 +1,14 @@
+{
+ "rules": [
+ {
+ "primary-output": "p1689-5.o",
+ "requires": [
+ {
+ "logical-name": "bar"
+ }
+ ]
+ }
+ ],
+ "version": 0,
+ "revision": 0
+}
new file mode 100644
@@ -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)
new file mode 100644
@@ -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 "json"
+}
+
+# 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
+}
@@ -1,3 +1,14 @@
+2022-10-27 Ben Boeckel <ben.boeckel@kitware.com>
+
+ * include/cpplib.h: Add cpp_deps_format enum.
+ * include/cpplib.h (cpp_options): Add format field.
+ * include/cpplib.h (cpp_finish): Add dependency stream parameter.
+ * include/mkdeps.h (deps_add_module_target): Add new preprocessor
+ parameter used for C++ module tracking.
+ * init.cc (cpp_finish): Add new preprocessor parameter used for C++
+ module tracking.
+ * mkdeps.cc (mkdeps): Implement P1689R5 output.
+
2022-10-27 Ben Boeckel <ben.boeckel@kitware.com>
* include/charset.cc: Add `_cpp_valid_utf8_str` which determines
@@ -302,6 +302,9 @@ typedef CPPCHAR_SIGNED_T cppchar_signed_t;
/* Style of header dependencies to generate. */
enum cpp_deps_style { DEPS_NONE = 0, DEPS_USER, DEPS_SYSTEM };
+/* Format of header dependencies to generate. */
+enum cpp_deps_format { DEPS_FMT_NONE = 0, DEPS_FMT_P1689R5 };
+
/* The possible normalization levels, from most restrictive to least. */
enum cpp_normalize_level {
/* In NFKC. */
@@ -589,6 +592,9 @@ struct cpp_options
/* Style of header dependencies to generate. */
enum cpp_deps_style style;
+ /* Format of header dependencies to generate. */
+ enum cpp_deps_format format;
+
/* Assume missing files are generated files. */
bool missing_files;
@@ -1112,9 +1118,9 @@ extern void cpp_post_options (cpp_reader *);
extern void cpp_init_iconv (cpp_reader *);
/* Call this to finish preprocessing. If you requested dependency
- generation, pass an open stream to write the information to,
- otherwise NULL. It is your responsibility to close the stream. */
-extern void cpp_finish (cpp_reader *, FILE *deps_stream);
+ generation, pass open stream(s) to write the information to,
+ otherwise NULL. It is your responsibility to close the stream(s). */
+extern void cpp_finish (cpp_reader *, FILE *deps_stream, FILE *fdeps_stream = NULL);
/* Call this to release the handle at the end of preprocessing. Any
use of the handle after this function returns is invalid. */
@@ -53,20 +53,29 @@ extern void deps_add_default_target (class mkdeps *, const char *);
/* Adds a module target. The module name and cmi name are copied. */
extern void deps_add_module_target (struct mkdeps *, const char *module,
- const char *cmi, bool is_header);
+ const char *cmi, bool is_header,
+ bool is_exported);
/* Adds a module dependency. The module name is copied. */
extern void deps_add_module_dep (struct mkdeps *, const char *module);
+/* Add an output. */
+extern void deps_add_output (struct mkdeps *, const char *, bool);
+
/* Add a dependency (appears on the right side of the colon) to the
deps list. Dependencies will be printed in the order that they
were entered with this function. By convention, the first
dependency entered should be the primary source file. */
extern void deps_add_dep (class mkdeps *, const char *);
-/* Write out a deps buffer to a specified file. The last argument
- is the number of columns to word-wrap at (0 means don't wrap). */
-extern void deps_write (const cpp_reader *, FILE *, unsigned int);
+/* Write out a deps buffer to a specified file. The third argument
+ is the number of columns to word-wrap at (0 means don't wrap).
+ The last argument indicates whether to output extra information
+ (namely modules). */
+extern void deps_write (const struct cpp_reader *, FILE *, unsigned int);
+
+/* Write out a deps buffer to a specified file in P1689R5 format. */
+extern void deps_write_p1689r5 (const struct mkdeps *, FILE *);
/* Write out a deps buffer to a file, in a form that can be read back
with deps_restore. Returns nonzero on error, in which case the
@@ -855,7 +855,7 @@ read_original_directory (cpp_reader *pfile)
Maybe it should also reset state, such that you could call
cpp_start_read with a new filename to restart processing. */
void
-cpp_finish (cpp_reader *pfile, FILE *deps_stream)
+cpp_finish (struct cpp_reader *pfile, FILE *deps_stream, FILE *fdeps_stream)
{
/* Warn about unused macros before popping the final buffer. */
if (CPP_OPTION (pfile, warn_unused_macros))
@@ -869,8 +869,15 @@ cpp_finish (cpp_reader *pfile, FILE *deps_stream)
while (pfile->buffer)
_cpp_pop_buffer (pfile);
- if (deps_stream)
- deps_write (pfile, deps_stream, 72);
+ cpp_deps_format deps_format = CPP_OPTION (pfile, deps.format);
+ if (deps_format == DEPS_FMT_P1689R5 && fdeps_stream)
+ deps_write_p1689r5 (pfile->deps, fdeps_stream);
+
+ if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
+ && deps_stream)
+ {
+ deps_write (pfile, deps_stream, 72);
+ }
/* Report on headers that could use multiple include guards. */
if (CPP_OPTION (pfile, print_include_names))
@@ -81,7 +81,8 @@ public:
};
mkdeps ()
- : module_name (NULL), cmi_name (NULL), is_header_unit (false), quote_lwm (0)
+ : primary_output (NULL), module_name (NULL), cmi_name (NULL)
+ , is_header_unit (false), is_exported (false), quote_lwm (0)
{
}
~mkdeps ()
@@ -90,6 +91,9 @@ public:
for (i = targets.size (); i--;)
free (const_cast <char *> (targets[i]));
+ free (const_cast <char *> (primary_output));
+ for (i = outputs.size (); i--;)
+ free (const_cast <char *> (outputs[i]));
for (i = deps.size (); i--;)
free (const_cast <char *> (deps[i]));
for (i = vpath.size (); i--;)
@@ -103,6 +107,8 @@ public:
public:
vec<const char *> targets;
vec<const char *> deps;
+ const char * primary_output;
+ vec<const char *> outputs;
vec<velt> vpath;
vec<const char *> modules;
@@ -110,6 +116,7 @@ public:
const char *module_name;
const char *cmi_name;
bool is_header_unit;
+ bool is_exported;
unsigned short quote_lwm;
};
@@ -288,6 +295,21 @@ deps_add_default_target (class mkdeps *d, const char *tgt)
}
}
+/* Adds an output O. We make a copy, so it need not be a permanent
+ string. */
+void
+deps_add_output (struct mkdeps *d, const char *o, bool is_primary)
+{
+ o = apply_vpath (d, o);
+ if (is_primary)
+ {
+ if (d->primary_output)
+ d->outputs.push (d->primary_output);
+ d->primary_output = xstrdup (o);
+ } else
+ d->outputs.push (xstrdup (o));
+}
+
void
deps_add_dep (class mkdeps *d, const char *t)
{
@@ -325,12 +347,13 @@ deps_add_vpath (class mkdeps *d, const char *vpath)
void
deps_add_module_target (struct mkdeps *d, const char *m,
- const char *cmi, bool is_header_unit)
+ const char *cmi, bool is_header_unit, bool is_exported)
{
gcc_assert (!d->module_name);
d->module_name = xstrdup (m);
d->is_header_unit = is_header_unit;
+ d->is_exported = is_exported;
d->cmi_name = xstrdup (cmi);
}
@@ -395,10 +418,15 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
if (colmax && colmax < 34)
colmax = 34;
+ /* Write out C++ modules information if no other `-fdeps-format=`
+ * option is given. */
+ cpp_deps_format deps_format = CPP_OPTION (pfile, deps.format);
+ bool write_make_modules_deps = deps_format == DEPS_FMT_NONE;
+
if (d->deps.size ())
{
column = make_write_vec (d->targets, fp, 0, colmax, d->quote_lwm);
- if (CPP_OPTION (pfile, deps.modules) && d->cmi_name)
+ if (write_make_modules_deps && CPP_OPTION (pfile, deps.modules) && d->cmi_name)
column = make_write_name (d->cmi_name, fp, column, colmax);
fputs (":", fp);
column++;
@@ -412,7 +440,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
if (!CPP_OPTION (pfile, deps.modules))
return;
- if (d->modules.size ())
+ if (write_make_modules_deps && d->modules.size ())
{
column = make_write_vec (d->targets, fp, 0, colmax, d->quote_lwm);
if (d->cmi_name)
@@ -423,7 +451,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
fputs ("\n", fp);
}
- if (d->module_name)
+ if (write_make_modules_deps && d->module_name)
{
if (d->cmi_name)
{
@@ -455,7 +483,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
}
}
- if (d->modules.size ())
+ if (write_make_modules_deps && d->modules.size ())
{
column = fprintf (fp, "CXX_IMPORTS +=");
make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
@@ -468,11 +496,118 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
/* Really we should be opening fp here. */
void
-deps_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
+deps_write (const struct cpp_reader *pfile, FILE *fp, unsigned int colmax)
{
make_write (pfile, fp, colmax);
}
+static void
+p1689r5_write_filepath (const char *name, FILE *fp)
+{
+ if (_cpp_valid_utf8_str (name))
+ {
+ fputc ('"', fp);
+ for (const char* c = name; *c; c++)
+ {
+ // Escape control characters.
+ if (ISCNTRL (*c))
+ fprintf (fp, "\\u%04x", *c);
+ // JSON escape characters.
+ else if (*c == '"' || *c == '\\')
+ {
+ fputc ('\\', fp);
+ fputc (*c, fp);
+ }
+ // Everything else.
+ else
+ fputc (*c, fp);
+ }
+ fputc ('"', fp);
+ }
+ else
+ {
+ // TODO: print an error
+ }
+}
+
+static void
+p1689r5_write_vec (const mkdeps::vec<const char *> &vec, FILE *fp)
+{
+ for (unsigned ix = 0; ix != vec.size (); ix++)
+ {
+ p1689r5_write_filepath (vec[ix], fp);
+ if (ix < vec.size () - 1)
+ fputc (',', fp);
+ fputc ('\n', fp);
+ }
+}
+
+void
+deps_write_p1689r5 (const struct mkdeps *d, FILE *fp)
+{
+ fputs ("{\n", fp);
+
+ fputs ("\"rules\": [\n", fp);
+ fputs ("{\n", fp);
+
+ if (d->primary_output)
+ {
+ fputs ("\"primary-output\": ", fp);
+ p1689r5_write_filepath (d->primary_output, fp);
+ fputs (",\n", fp);
+ }
+
+ if (d->outputs.size ())
+ {
+ fputs ("\"outputs\": [\n", fp);
+ p1689r5_write_vec (d->outputs, fp);
+ fputs ("],\n", fp);
+ }
+
+ if (d->module_name)
+ {
+ fputs ("\"provides\": [\n", fp);
+ fputs ("{\n", fp);
+
+ fputs ("\"logical-name\": ", fp);
+ p1689r5_write_filepath (d->module_name, fp);
+ fputs (",\n", fp);
+
+ fprintf (fp, "\"is-interface\": %s\n", d->is_exported ? "true" : "false");
+
+ // TODO: header-unit information
+
+ fputs ("}\n", fp);
+ fputs ("],\n", fp);
+ }
+
+ fputs ("\"requires\": [\n", fp);
+ for (size_t i = 0; i < d->modules.size (); i++)
+ {
+ if (i != 0)
+ fputs (",\n", fp);
+ fputs ("{\n", fp);
+
+ fputs ("\"logical-name\": ", fp);
+ p1689r5_write_filepath (d->modules[i], fp);
+ fputs ("\n", fp);
+
+ // TODO: header-unit information
+
+ fputs ("}\n", fp);
+ }
+ fputs ("]\n", fp);
+
+ fputs ("}\n", fp);
+
+ fputs ("],\n", fp);
+
+ fputs ("\"version\": 0,\n", fp);
+ fputs ("\"revision\": 0\n", fp);
+
+ fputs ("}\n", fp);
+}
+
/* Write out a deps buffer to a file, in a form that can be read back
with deps_restore. Returns nonzero on error, in which case the
error number will be in errno. */