cpplib: Implement -MP for phony targets [was re: -MT]

Neil Booth neil@daikokuya.demon.co.uk
Thu Jan 4 04:31:00 GMT 2001


Zack Weinberg wrote:-

> I should warn you that the current semantics of deps_add_target are
> wrong for -MT.  It tries to quote dollar signs, spaces, etc. in its
> argument, which means you can't write something like
> 
>   -MT '$(objpfx)foo.o $(objpfx)foo.os $(objpfx)foo.op'
> 
> and have it do what you expect.  [Glibc needs to do just that - it's
> not a theoretical problem.]

OK, this is fixed in the patch below, with doc updates.  We accept one
or more -MT options, whose arguments go unquoted.

> We do want to do the quoting when we deduce the target from the object
> file name - you created a deps_add_default_target, maybe you could
> move the call to munge() in there.

That implementation would give memory leak issues; so I've just made
an extra QUOTE argument to deps_add_target, which continues to make
its own copy of strings, quoted depending upon the QUOTE boolean.

Unfortunately, the patch is munged with one implementing -MP.
Apologies for that; I should have done them separately but a make
bootstrap + make check takes so long now I'd be sitting around for
hours, and I didn't make an intermediate diff.

-MP prints the Phony (dummy) targets for the dependencies other than
the main file.  I've changed Zack's original code to add a blank line
before each one; just for clarity.  Example:-

bash-2.04$ ./cpp0 -MM /tmp/test.c -MP
test.o: /tmp/test.c /tmp/test.h

/tmp/test.h:
bash-2.04$

As requested in a private mail by Tom Tromey, I've updated gcj to use
the same option.  Tom, I couldn't find anything in libjava using -MA,
so I've only updated the handler.  Is this part OK to commit?

I'm running a Linux bootstrap at present.

Tom / Zack, the behaviour of the default target is as follows:

bash-2.04$ ./cpp0 -MM /tmp/test.c
test.o: /tmp/test.c /tmp/test.h

Shouldn't we keep the path, as in

/tmp/test.o: /tmp/test.c /tmp/test.h

It seems to me we should, but I'm not an expert on these issues.  If
so, I can fix that with a follow-up patch.

Neil.

	* cpp.texi: Update for -MP.  Clarify behaviour of -MT.
	* cppinit.c (initialize_dependency_output):  Update.
	(cpp_finish): Output dummy targets for -MP.
	(OPT_MP): New.
	(cpp_handle_option): Handle -MP.  Don't quote -MT options.
	* cpplib.h (struct cpp_options): Add deps_phony_targets.
	* gcc.c (cpp_options): Update to handle -MP.
	* mkdeps.c (deps_add_target, deps_add_default_target): Update
	to quote only the default target.
	(deps_phony_targets): Insert a preceding newline.  Rename from
	deps_dummy_targets for consistency.
	* mkdeps.h: Update

	* java/lang.c: Change -MA to -MP.

Index: gcc/cpp.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpp.texi,v
retrieving revision 1.38
diff -u -p -r1.38 cpp.texi
--- cpp.texi	2001/01/04 10:25:53	1.38
+++ cpp.texi	2001/01/04 12:08:10
@@ -3476,14 +3476,38 @@ files into a single dependency file suit
 Like @samp{-MD} except mention only user header files, not system
 header files.
 
+@item -MP
+@findex -MP
+This option instructs CPP to add a phony target for each dependency
+other than the main file, causing each to depend on nothing.  These
+dummy rules work around errors MAKE gives if you remove header files
+without updating the Makefile to match.
+
+This is typical output:-
+
+@smallexample
+/tmp/test.o: /tmp/test.c /tmp/test.h
+
+/tmp/test.h:
+@end smallexample
+
 @item -MT @var{target}
 @findex -MT
 By default CPP uses the base file name and appends the object suffix,
 normally ``.o'', to it to obtain the name of the target for dependency
-generation.  With @samp{-MT} you can specify one or more of your own
-targets; doing so overrides the default.
+generation.  With @samp{-MT} you can specify a target yourself,
+overriding the default one.
+
+If you want multiple targets, you can specify them as a single argument
+to @samp{-MT}, or use multiple @samp{-MT} options.
+
+The targets you specify are output in the order they appear on the
+command line, and, unlike the default target, are not quoted for MAKE.
+This allows you to do things like, for example,
 
-The targets are output in the order they appear on the command line.
+@smallexample
+-MT '$(objpfx)foo.o $(objpfx)foo.os $(objpfx)foo.op'
+@end smallexample
 
 @item -H
 @findex -H
Index: gcc/cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.128
diff -u -p -r1.128 cppinit.c
--- cppinit.c	2001/01/04 10:25:53	1.128
+++ cppinit.c	2001/01/04 12:08:15
@@ -760,7 +760,8 @@ initialize_dependency_output (pfile)
       s = strchr (spec, ' ');
       if (s)
 	{
-	  deps_add_target (pfile->deps, s + 1);
+	  /* Let the caller perform MAKE quoting.  */
+	  deps_add_target (pfile->deps, s + 1, 0);
 	  output_file = (char *) xmalloc (s - spec + 1);
 	  memcpy (output_file, spec, s - spec);
 	  output_file[s - spec] = 0;
@@ -1018,6 +1019,10 @@ cpp_finish (pfile)
       if (deps_stream)
 	{
 	  deps_write (pfile->deps, deps_stream, 72);
+
+	  if (CPP_OPTION (pfile, deps_phony_targets))
+	    deps_phony_targets (pfile->deps, deps_stream);
+
 	  if (CPP_OPTION (pfile, deps_file))
 	    {
 	      if (ferror (deps_stream) || fclose (deps_stream) != 0)
@@ -1077,6 +1082,7 @@ new_pending_directive (pend, text, handl
   DEF_OPT("MG",                       0,      OPT_MG)                         \
   DEF_OPT("MM",                       0,      OPT_MM)                         \
   DEF_OPT("MMD",                      no_fil, OPT_MMD)                        \
+  DEF_OPT("MP",                       0,      OPT_MP)                         \
   DEF_OPT("MT",                       no_tgt, OPT_MT)                         \
   DEF_OPT("P",                        0,      OPT_P)                          \
   DEF_OPT("U",                        no_mac, OPT_U)                          \
@@ -1496,11 +1502,15 @@ cpp_handle_option (pfile, argc, argv)
 	      CPP_OPTION (pfile, no_output) = 1;
 	  break;
 
+	case OPT_MP:
+	  CPP_OPTION (pfile, deps_phony_targets) = 1;
+	  break;
+
 	case OPT_MT:
 	  /* Add a target.  */
 	  if (! pfile->deps)
 	    pfile->deps = deps_init ();
-	  deps_add_target (pfile->deps, arg);
+	  deps_add_target (pfile->deps, arg, 0);
 	  break;
 
 	case OPT_A:
Index: gcc/cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.154
diff -u -p -r1.154 cpplib.h
--- cpplib.h	2001/01/04 10:25:54	1.154
+++ cpplib.h	2001/01/04 12:08:21
@@ -329,6 +329,9 @@ struct cpp_options
      #include <...> as well.  */
   unsigned char print_deps;
 
+  /* Nonzero if phony targets are created for each header.  */
+  unsigned char deps_phony_targets;
+
   /* Nonzero if missing .h files in -M output are assumed to be
      generated files and not errors.  */
   unsigned char print_deps_missing_files;
@@ -570,7 +573,7 @@ struct cpp_reader
   cpp_token date;
   cpp_token time;
 
-  /* Buffer of -M output.  */
+  /* Opaque handle to the dependencies of mkdeps.c.  Used by -M etc.  */
   struct deps *deps;
 
   /* Obstack holding all macro hash nodes.  This never shrinks.
Index: gcc/gcc.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/gcc.c,v
retrieving revision 1.191
diff -u -p -r1.191 gcc.c
--- gcc.c	2001/01/04 10:25:54	1.191
+++ gcc.c	2001/01/04 12:08:35
@@ -584,7 +584,7 @@ static const char *cpp_options =
 "%{C:%{!E:%eGNU C does not support -C without using -E}}\
  %{std*} %{nostdinc*}\
  %{C} %{v} %{I*} %{P} %{$} %I\
- %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{MT}\
+ %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{MP} %{MT}\
  %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2 -D__GNUC_PATCHLEVEL__=%v3}\
  %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
  %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
Index: gcc/mkdeps.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/mkdeps.c,v
retrieving revision 1.8
diff -u -p -r1.8 mkdeps.c
--- mkdeps.c	2001/01/04 10:25:54	1.8
+++ mkdeps.c	2001/01/04 12:08:39
@@ -73,8 +73,7 @@ munge (filename)
 	  break;
 
 	case '$':
-	  /* '$' is quoted by doubling it. This can mishandle things
-	     like "$(" but there's no easy fix.  */
+	  /* '$' is quoted by doubling it.  */
 	  len++;
 	  break;
 	}
@@ -172,13 +171,14 @@ deps_free (d)
   free (d);
 }
 
+/* Adds a target T.  We make a copy, so it need not be a permanent
+   string.  QUOTE is true if the string should be quoted.  */
 void
-deps_add_target (d, t)
+deps_add_target (d, t, quote)
      struct deps *d;
      const char *t;
+     int quote;
 {
-  t = munge (t);  /* Also makes permanent copy.  */
-
   if (d->ntargets == d->targets_size)
     {
       d->targets_size *= 2;
@@ -186,11 +186,17 @@ deps_add_target (d, t)
 			     d->targets_size * sizeof (const char *));
     }
 
+  if (quote)
+    t = munge (t);  /* Also makes permanent copy.  */
+  else
+    t = xstrdup (t);
+
   d->targetv[d->ntargets++] = t;
 }
 
 /* Sets the default target if none has been given already.  An empty
-   string as the default target in interpreted as stdin.  */
+   string as the default target in interpreted as stdin.  The string
+   is quoted for MAKE.  */
 void
 deps_add_default_target (d, tgt)
      struct deps *d;
@@ -203,7 +209,7 @@ deps_add_default_target (d, tgt)
     return;
 
   if (tgt[0] == '\0')
-    deps_add_target (d, "-");
+    deps_add_target (d, "-", 1);
   else
     {
       tgt = base_name (tgt);
@@ -220,7 +226,7 @@ deps_add_default_target (d, tgt)
 	strcpy (suffix, OBJECT_SUFFIX);
       else
 	strcat (o, OBJECT_SUFFIX);
-      deps_add_target (d, o);
+      deps_add_target (d, o, 1);
     }
 }
 
@@ -293,7 +299,7 @@ deps_write (d, fp, colmax)
 }
   
 void
-deps_dummy_targets (d, fp)
+deps_phony_targets (d, fp)
      const struct deps *d;
      FILE *fp;
 {
@@ -301,6 +307,7 @@ deps_dummy_targets (d, fp)
 
   for (i = 1; i < d->ndeps; i++)
     {
+      putc ('\n', fp);
       fputs (d->depv[i], fp);
       putc (':', fp);
       putc ('\n', fp);
Index: gcc/mkdeps.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/mkdeps.h,v
retrieving revision 1.2
diff -u -p -r1.2 mkdeps.h
--- mkdeps.h	2001/01/04 10:25:55	1.2
+++ mkdeps.h	2001/01/04 12:08:39
@@ -34,8 +34,9 @@ extern struct deps *deps_init	PARAMS ((v
 /* Destroy a deps buffer.  */
 extern void deps_free		PARAMS ((struct deps *));
 
-/* Add a target (appears on left side of the colon) to the deps list. */
-extern void deps_add_target	PARAMS ((struct deps *, const char *));
+/* Add a target (appears on left side of the colon) to the deps list.  Takes
+   a boolean indicating whether to quote the target for MAKE.  */
+extern void deps_add_target	PARAMS ((struct deps *, const char *, int));
 
 /* Sets the default target if none has been given already.  An empty
    string as the default target in interpreted as stdin.  */
@@ -56,6 +57,6 @@ extern void deps_write		PARAMS ((const s
    file, causing it to depend on nothing.  This is used to work around
    the intermediate-file deletion misfeature in Make, in some
    automatic dependency schemes.  */
-extern void deps_dummy_targets	PARAMS ((const struct deps *, FILE *));
+extern void deps_phony_targets	PARAMS ((const struct deps *, FILE *));
 
 #endif
Index: gcc/java/ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/ChangeLog,v
retrieving revision 1.599
diff -u -p -r1.599 ChangeLog
--- ChangeLog	2000/12/24 00:43:40	1.599
+++ ChangeLog	2001/01/04 12:09:25
@@ -1,3 +1,7 @@
+2001-01-04  Neil Booth  <neil@daikokuya.demon.co.uk>
+
+	* lang.c: Change -MA to -MP.
+
 2000-12-22  Bryce McKinlay  <bryce@albatross.co.nz>
 
 	Shorten primitive array allocation path:
Index: gcc/java/lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/lang.c,v
retrieving revision 1.52
diff -u -p -r1.52 lang.c
--- lang.c	2000/11/04 04:57:33	1.52
+++ lang.c	2001/01/04 12:09:28
@@ -1,5 +1,6 @@
 /* Java(TM) language-specific utility routines.
-   Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
+   Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -340,7 +341,7 @@ lang_decode_option (argc, argv)
       dependency_tracking |= DEPEND_ENABLE;
       return 1;
     }
-  else if (strcmp (p, "-MA") == 0)
+  else if (strcmp (p, "-MP") == 0)
     {
       jcf_dependency_print_dummies ();
       return 1;


More information about the Gcc-patches mailing list