[gcc r11-5133] preprocessor: Update mkdeps for modules

Nathan Sidwell nathan@gcc.gnu.org
Wed Nov 18 16:46:02 GMT 2020


https://gcc.gnu.org/g:db87f19ae3cfc126fb39616515b57dea4df02e6d

commit r11-5133-gdb87f19ae3cfc126fb39616515b57dea4df02e6d
Author: Nathan Sidwell <nathan@acm.org>
Date:   Wed Nov 18 06:44:38 2020 -0800

    preprocessor: Update mkdeps for modules
    
    This is slightly different to the original patch I posted.  This adds
    separate module target and dependency functions (rather than a single
    bi-modal function).
    
            libcpp/
            * include/cpplib.h (struct cpp_options): Add modules to
            dep-options.
            * include/mkdeps.h (deps_add_module_target): Declare.
            (deps_add_module_dep): Declare.
            * mkdeps.c (class mkdeps): Add modules, module_name, cmi_name,
            is_header_unit fields.  Adjust cdtors.
            (deps_add_module_target, deps_add_module_dep): New.
            (make_write): Write module dependencies, if enabled.

Diff:
---
 libcpp/include/cpplib.h |  3 ++
 libcpp/include/mkdeps.h |  7 ++++
 libcpp/mkdeps.c         | 87 ++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index d2324266d39..75d4d0a9f2f 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -528,6 +528,9 @@ struct cpp_options
        one.  */
     bool phony_targets;
 
+    /* Generate dependency info for modules.  */
+    bool modules;
+
     /* If true, no dependency is generated on the main file.  */
     bool ignore_main_file;
 
diff --git a/libcpp/include/mkdeps.h b/libcpp/include/mkdeps.h
index 593b718aaeb..9f10327eec3 100644
--- a/libcpp/include/mkdeps.h
+++ b/libcpp/include/mkdeps.h
@@ -51,6 +51,13 @@ extern void deps_add_target (class mkdeps *, const char *, int);
    string as the default target is interpreted as stdin.  */
 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);
+
+/* Adds a module dependency.  The module name is copied.  */
+extern void deps_add_module_dep (struct mkdeps *, const char *module);
+
 /* 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
diff --git a/libcpp/mkdeps.c b/libcpp/mkdeps.c
index a989ed355fa..4a8e101b912 100644
--- a/libcpp/mkdeps.c
+++ b/libcpp/mkdeps.c
@@ -81,7 +81,7 @@ public:
   };
 
   mkdeps ()
-    : quote_lwm (0)
+    : module_name (NULL), cmi_name (NULL), is_header_unit (false), quote_lwm (0)
   {
   }
   ~mkdeps ()
@@ -94,14 +94,22 @@ public:
       free (const_cast <char *> (deps[i]));
     for (i = vpath.size (); i--;)
       XDELETEVEC (vpath[i].str);
+    for (i = modules.size (); i--;)
+      XDELETEVEC (modules[i]);
+    XDELETEVEC (module_name);
+    free (const_cast <char *> (cmi_name));
   }
 
 public:
   vec<const char *> targets;
   vec<const char *> deps;
   vec<velt> vpath;
+  vec<const char *> modules;
 
 public:
+  const char *module_name;
+  const char *cmi_name;
+  bool is_header_unit;
   unsigned short quote_lwm;
 };
 
@@ -313,6 +321,28 @@ deps_add_vpath (class mkdeps *d, const char *vpath)
     }
 }
 
+/* Add a new module target (there can only be one).  M is the module
+   name.   */
+
+void
+deps_add_module_target (struct mkdeps *d, const char *m,
+			const char *cmi, bool is_header_unit)
+{
+  gcc_assert (!d->module_name);
+  
+  d->module_name = xstrdup (m);
+  d->is_header_unit = is_header_unit;
+  d->cmi_name = xstrdup (cmi);
+}
+
+/* Add a new module dependency.  M is the module name.  */
+
+void
+deps_add_module_dep (struct mkdeps *d, const char *m)
+{
+  d->modules.push (xstrdup (m));
+}
+
 /* Write NAME, with a leading space to FP, a Makefile.  Advance COL as
    appropriate, wrap at COLMAX, returning new column number.  Iff
    QUOTE apply quoting.  Append TRAIL.  */
@@ -369,6 +399,8 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
   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)
+	column = make_write_name (d->cmi_name, fp, column, colmax);
       fputs (":", fp);
       column++;
       make_write_vec (d->deps, fp, column, colmax);
@@ -377,6 +409,59 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
 	for (unsigned i = 1; i < d->deps.size (); i++)
 	  fprintf (fp, "%s:\n", munge (d->deps[i]));
     }
+
+  if (!CPP_OPTION (pfile, deps.modules))
+    return;
+
+  if (d->modules.size ())
+    {
+      column = make_write_vec (d->targets, fp, 0, colmax, d->quote_lwm);
+      if (d->cmi_name)
+	column = make_write_name (d->cmi_name, fp, column, colmax);
+      fputs (":", fp);
+      column++;
+      column = make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
+      fputs ("\n", fp);
+    }
+
+  if (d->module_name)
+    {
+      if (d->cmi_name)
+	{
+	  /* module-name : cmi-name */
+	  column = make_write_name (d->module_name, fp, 0, colmax,
+				    true, ".c++m");
+	  fputs (":", fp);
+	  column++;
+	  column = make_write_name (d->cmi_name, fp, column, colmax);
+	  fputs ("\n", fp);
+
+	  column = fprintf (fp, ".PHONY:");
+	  column = make_write_name (d->module_name, fp, column, colmax,
+				    true, ".c++m");
+	  fputs ("\n", fp);
+	}
+
+      if (d->cmi_name && !d->is_header_unit)
+	{
+	  /* An order-only dependency.
+	      cmi-name :| first-target
+	     We can probably drop this this in favour of Make-4.3's grouped
+	      targets '&:'  */
+	  column = make_write_name (d->cmi_name, fp, 0, colmax);
+	  fputs (":|", fp);
+	  column++;
+	  column = make_write_name (d->targets[0], fp, column, colmax);
+	  fputs ("\n", fp);
+	}
+    }
+  
+  if (d->modules.size ())
+    {
+      column = fprintf (fp, "CXX_IMPORTS +=");
+      make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
+      fputs ("\n", fp);
+    }
 }
 
 /* Write out dependencies according to the selected format (which is


More information about the Gcc-cvs mailing list