]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libcpp: add .c++-header-unit target
authorJason Merrill <jason@redhat.com>
Wed, 5 Jun 2024 01:15:02 +0000 (21:15 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 18 Nov 2024 08:18:17 +0000 (09:18 +0100)
The dependency output for header unit modules is based on the absolute
pathname of the header file, but that's not something that a makefile can
portably refer to.  This patch adds a .c++-header-unit target based on the
header name relative to an element of the include path.

libcpp/ChangeLog:

* internal.h (_cpp_get_file_dir): Declare.
* files.cc (_cpp_get_file_dir): New fn.
* mkdeps.cc (make_write): Use it.

gcc/testsuite/ChangeLog:

* g++.dg/modules/dep-4.H: New test.

gcc/testsuite/g++.dg/modules/dep-4.H [new file with mode: 0644]
libcpp/files.cc
libcpp/internal.h
libcpp/mkdeps.cc

diff --git a/gcc/testsuite/g++.dg/modules/dep-4.H b/gcc/testsuite/g++.dg/modules/dep-4.H
new file mode 100644 (file)
index 0000000..070fa5a
--- /dev/null
@@ -0,0 +1,7 @@
+// { dg-do preprocess }
+// { dg-additional-options "-fmodules -M" }
+
+inline void f() { }
+
+// { dg-final { scan-file dep-4.i {dep-4\.H\.c\+\+-header-unit:} } }
+// { dg-final { scan-file-not dep-4.i {inline} } }
index 840dffccce428abc2aac5cc4de64e996121d9b42..1cbce4947ee19b4351cae9d53d569b6d1d13582c 100644 (file)
@@ -2336,6 +2336,13 @@ _cpp_get_file_stat (_cpp_file *file)
   return &file->st;
 }
 
+/* Return the directory where FILE was found.  */
+struct cpp_dir *
+_cpp_get_file_dir (_cpp_file *file)
+{
+  return file->dir;
+}
+
 /* Set the include chain for "" to QUOTE, for <> to BRACKET.  If
    QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the
    directory of the including file.
index d91acd64ba3917844a6e4e344871ede77ebf0475..ad4e5909cfae9b3cc36b42d6b481795b52d716f3 100644 (file)
@@ -786,6 +786,7 @@ extern bool _cpp_save_file_entries (cpp_reader *pfile, FILE *f);
 extern bool _cpp_read_file_entries (cpp_reader *, FILE *);
 extern const char *_cpp_get_file_name (_cpp_file *);
 extern struct stat *_cpp_get_file_stat (_cpp_file *);
+extern struct cpp_dir *_cpp_get_file_dir (_cpp_file *);
 extern bool _cpp_has_header (cpp_reader *, const char *, int,
                             enum include_type);
 
index a31890a386a9c977b1319f42cc0d6c7647f7ec68..b37d33013bef85745cac05538bd7738b733e2b08 100644 (file)
@@ -463,6 +463,19 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
          /* module-name : cmi-name */
          column = make_write_name (d->module_name, fp, 0, colmax,
                                    true, ".c++-module");
+         const char *module_basename = nullptr;
+         if (d->is_header_unit)
+           {
+             /* Also emit a target for the include name, so for #include
+                <iostream> you'd make iostream.c++-header-unit, regardless of
+                what actual directory iostream lives in.  We reconstruct the
+                include name by skipping the directory where we found it.  */
+             auto *dir = _cpp_get_file_dir (pfile->main_file);
+             gcc_assert (!strncmp (d->module_name, dir->name, dir->len));
+             module_basename = (d->module_name + dir->len + 1);
+             column = make_write_name (module_basename, fp, column, colmax,
+                                       true, ".c++-header-unit");
+           }
          fputs (":", fp);
          column++;
          column = make_write_name (d->cmi_name, fp, column, colmax);
@@ -471,6 +484,9 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
          column = fprintf (fp, ".PHONY:");
          column = make_write_name (d->module_name, fp, column, colmax,
                                    true, ".c++-module");
+         if (module_basename)
+           column = make_write_name (module_basename, fp, column, colmax,
+                                     true, ".c++-header-unit");
          fputs ("\n", fp);
        }