From 5bd5a493a2eef0392f93daa59cf2c10396f12930 Mon Sep 17 00:00:00 2001
From: Jonas Danielsson <jonas@threetimestwo.org>
Date: Tue, 20 Oct 2020 13:21:51 +0200
Subject: [PATCH] Add '-g' opt for graph output of modified targets

A graph visualisation can be helpful in order to get a better
understanding of the workings of a GNU Make based project.
Particularly if you are debugging why something keeps rebuilding each
pass or if you are very new to a projekt.

All the information that can be gleaned from this graph output is
already present in the output of --print-database. However we strive to
present it more succinct. Only the targets that were modified in the
goal is present and the relationship between targets and prerequisite
is hopefully more clear.

The output from the goal grahps is stored in files called [goal].dot
in the starting directory.

The hope is also that this can be paired with already existing viewers
and explorers, since the dot language is well known (29 years!).

An example (rebuilding GNU Make):
  $ touch src/file.c
  $ AM_MAKEFLAGS="-g" make -g
  Making all in lib
  make[1]: Entering directory '/home/jonas/sandbox/mymake/lib'
  /home/jonas/sandbox/mymake/./make -g all-recursive
  make[2]: Entering directory '/home/jonas/sandbox/mymake/lib'
  make[3]: Entering directory '/home/jonas/sandbox/mymake/lib'
  make[3]: Nothing to be done for 'all-am'.
  make[3]: Leaving directory '/home/jonas/sandbox/mymake/lib'
  make[2]: Writing dependency graph to '/home/jonas/sandbox/mymake/lib/all-recursive.dot'
  make[2]: Leaving directory '/home/jonas/sandbox/mymake/lib'
  make[1]: Writing dependency graph to '/home/jonas/sandbox/mymake/lib/all.dot'
  make[1]: Leaving directory '/home/jonas/sandbox/mymake/lib'
  Making all in po
  make[1]: Entering directory '/home/jonas/sandbox/mymake/po'
  make[1]: Nothing to be done for 'all'.
  make[1]: Leaving directory '/home/jonas/sandbox/mymake/po'
  Making all in doc
  make[1]: Entering directory '/home/jonas/sandbox/mymake/doc'
  make[1]: Nothing to be done for 'all'.
  make[1]: Leaving directory '/home/jonas/sandbox/mymake/doc'
  make[1]: Entering directory '/home/jonas/sandbox/mymake'

  [...]
  [gcc building]
  [...]

  make[1]: Writing dependency graph to '/home/jonas/sandbox/mymake/all-am.dot'
  make[1]: Leaving directory '/home/jonas/sandbox/mymake'
  make: Writing dependency graph to '/home/jonas/sandbox/mymake/all.dot'

  $ cat all-am.dot
  digraph all-am {
  "make" -> "src/file.o"
  "all-am" -> "make"
  "all-am"
  }

  $ cat all.dot
  digraph all {
  "all" -> "all-recursive" [label="forced: PHONY prerequisite"]
  "all"
  }
---
 doc/make.1    |   6 +++
 doc/make.texi |   9 ++++
 src/file.c    | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/filedef.h |   2 +
 src/main.c    |   5 +++
 src/remake.c  |   4 ++
 6 files changed, 148 insertions(+)

diff --git a/doc/make.1 b/doc/make.1
index dbd023f..a9d56df 100644
--- a/doc/make.1
+++ b/doc/make.1
@@ -160,6 +160,12 @@ Use
 .I file
 as a makefile.
 .TP 0.5i
+\fB\-g\fR, \fB\-\-output\-graph\fR
+Output build dependency graph for updated make goals. The graph is
+written to a file called
+.I [goal].dot
+and is represented using the Graphvis (dot) syntax. Only targets that
+have been updated is present in the graph.
 \fB\-i\fR, \fB\-\-ignore\-errors\fR
 Ignore all errors in commands executed to remake files.
 .TP 0.5i
diff --git a/doc/make.texi b/doc/make.texi
index 21573c0..bf7db47 100644
--- a/doc/make.texi
+++ b/doc/make.texi
@@ -8925,6 +8925,15 @@ been defined, but before any makefiles are read.
 Read the file named @var{file} as a makefile.
 @xref{Makefiles, ,Writing Makefiles}.
 
+@item -g
+@cindex @code{-g}
+@itemx --output-graph
+@cindex @code{--output-graph}
+Output build dependency graph for updated make goals. The graph is
+written to a file called @code{[goal].dot} and is represented using
+the Graphvis (dot) syntax. Only targets that have been updated is
+present in the graph.
+
 @item -h
 @cindex @code{-h}
 @itemx --help
diff --git a/src/file.c b/src/file.c
index a979ca5..1d6c4e0 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1199,4 +1199,126 @@ init_hash_files (void)
   hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
 }
 
+static const char *
+get_edge_label (const struct file *file)
+{
+  if (file->last_mtime == NONEXISTENT_MTIME)
+    {
+      if (file->phony)
+        return "[label=\"forced: PHONY prerequisite\"]";
+
+      return "[label=\"forced: non-existent file\"]";
+    }
+  return "";
+}
+
+static int
+add_edge (const struct file *parent, const struct file *child, FILE *fp)
+{
+  return fprintf (fp, "\"%s\" -> \"%s\" %s\n",
+                  parent->name, child->name, get_edge_label (child));
+}
+
+static enum update_status
+output_graph_node (const void *item, FILE *fp)
+{
+  int n;
+  int dep_updated;
+  const struct file *file = item;
+
+  if (!file->updated)
+    return us_none;
+
+  if (file->deps)
+    {
+      struct dep *d;
+      dep_updated = 0;
+      for (d = file->deps; d != 0; d = d->next)
+        {
+            enum update_status status = output_graph_node (d->file, fp);
+            if (status == us_failed)
+              return us_failed;
+            if (status == us_success)
+              {
+                dep_updated = 1;
+                if (add_edge (file, d->file, fp) < 0)
+                  return us_failed;
+              }
+        }
+    }
+
+  /* We only want to include the files that actually changed during
+     the updating of this goal. But we make an exception for non-existent
+     files, as they are a clue as to why something was rebuilt.
+
+     And we also make exception if the file has dependencies that have updated.
+  */
+
+ if (!file->deps || dep_updated == 0)
+  {
+    if (file->last_mtime != NONEXISTENT_MTIME)
+      if (file->last_mtime == file->mtime_before_update) {
+        return us_none;
+      }
+  }
+
+  /* Special case for single target goals */
+  if (!file->parent && !file->deps)
+  {
+      n = fprintf (fp, "\"%s\"\n", file->name);
+      if (n < 0)
+        return us_failed;
+   }
+
+  return us_success;
+}
+
+
+/* Traverse the dependency graph for a goal and output it in the
+   Graphviz (dot) syntax.
+
+   Only targets that have been updated (last_mtime != mtime_before_update)
+   will be included in the graph.
+*/
+
+void
+output_graph_file (const char *goal)
+{
+  FILE *fp;
+  struct file *root;
+  char dot_fname[PATH_MAX + FILENAME_MAX + 1];
+
+  /* Make sure we always save the graph file in the starting directory */
+  sprintf (dot_fname, "%s/%s.dot", starting_directory, goal);
+  OS (message, 1, "Writing dependency graph to '%s'", dot_fname);
+
+  fp = fopen (dot_fname, "w");
+  if (!fp)
+    {
+      OS (error, NILF, _("Failed to open to graph file: %s"), strerror (errno));
+      goto out;
+    }
+  if (fprintf (fp, "strict digraph \"%s\" {\n", goal) < 0)
+    {
+      OS (error, NILF, _("Failed to write to graph file: %s"), strerror (errno));
+      goto out;
+    }
+
+  root = lookup_file (goal);
+  if (!root)
+    goto out;
+
+  if (output_graph_node (root, fp) == us_failed)
+    {
+      OS (error, NILF, _("Failed to write to graph file: %s"), strerror (errno));
+      goto out;
+    }
+
+  if (fprintf (fp, "}\n") < 0)
+    OS (error, NILF, _("Failed to write to graph file: %s"), strerror (errno));
+
+out:
+  fclose (fp);
+}
+
 /* EOF */
diff --git a/src/filedef.h b/src/filedef.h
index 972f853..8c73bb9 100644
--- a/src/filedef.h
+++ b/src/filedef.h
@@ -110,6 +110,7 @@ struct file
 
 extern struct file *default_file;
 
+extern int output_graph_flag;
 
 struct file *lookup_file (const char *name);
 struct file *enter_file (const char *name);
@@ -129,6 +130,7 @@ void print_prereqs (const struct dep *deps);
 void print_file_data_base (void);
 int try_implicit_rule (struct file *file, unsigned int depth);
 int stemlen_compare (const void *v1, const void *v2);
+void output_graph_file (const char *goal);
 
 #if FILE_TIMESTAMP_HI_RES
 # define FILE_TIMESTAMP_STAT_MODTIME(fname, st) \
diff --git a/src/main.c b/src/main.c
index c79a9f6..76fc6d9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -239,6 +239,8 @@ int print_version_flag = 0;
 
 static struct stringlist *makefiles = 0;
 
+int output_graph_flag = 0;
+
 /* Size of the stack when we started.  */
 
 #ifdef SET_STACK_SIZE
@@ -350,6 +352,8 @@ static const char *const usage[] =
   -f FILE, --file=FILE, --makefile=FILE\n\
                               Read FILE as a makefile.\n"),
     N_("\
+  -g, --output-graph          Output (dot) graph of modified targets for each goal.\n"),
+    N_("\
   -h, --help                  Print this message and exit.\n"),
     N_("\
   -i, --ignore-errors         Ignore errors from recipes.\n"),
@@ -418,6 +422,7 @@ static const struct command_switch switches[] =
     { 'd', flag, &debug_flag, 1, 1, 0, 0, 0, 0 },
     { 'e', flag, &env_overrides, 1, 1, 0, 0, 0, "environment-overrides", },
     { 'E', strlist, &eval_strings, 1, 0, 0, 0, 0, "eval" },
+    { 'g', flag, &output_graph_flag, 0, 0, 0, 0, 0, "output-graph" },
     { 'h', flag, &print_usage_flag, 0, 0, 0, 0, 0, "help" },
     { 'i', flag, &ignore_errors_flag, 1, 1, 0, 0, 0, "ignore-errors" },
     { 'k', flag, &keep_going_flag, 1, 1, 0, 0, &default_keep_going_flag,
diff --git a/src/remake.c b/src/remake.c
index fb237c5..ac22ec1 100644
--- a/src/remake.c
+++ b/src/remake.c
@@ -233,6 +233,10 @@ update_goal_chain (struct goaldep *goaldeps)
               else
                 lastgoal->next = g->next;
 
+              /* Output graph if goal has changed and if requested */
+              if (g->changed && output_graph_flag)
+                output_graph_file (dep_name(g));
+
               /* Free the storage.  */
               free (g);
 
-- 
2.25.1

