1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Contributed by Zack Weinberg, Mar 2000
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
28 /* Not set up to just include std::vector et al, here's a simple
31 /* Keep this structure local to this file, so clients don't find it
32 easy to start making assumptions. */
36 /* T has trivial cctor & dtor. */
47 : ary (NULL
), num (0), alloc (0)
55 unsigned size () const
59 const T
&operator[] (unsigned ix
) const
63 T
&operator[] (unsigned ix
)
67 void push (const T
&elt
)
71 alloc
= alloc
? alloc
* 2 : 16;
72 ary
= XRESIZEVEC (T
, ary
, alloc
);
84 : module_name (NULL
), cmi_name (NULL
), is_header_unit (false), quote_lwm (0)
91 for (i
= targets
.size (); i
--;)
92 free (const_cast <char *> (targets
[i
]));
93 for (i
= deps
.size (); i
--;)
94 free (const_cast <char *> (deps
[i
]));
95 for (i
= vpath
.size (); i
--;)
96 XDELETEVEC (vpath
[i
].str
);
97 for (i
= modules
.size (); i
--;)
98 XDELETEVEC (modules
[i
]);
99 XDELETEVEC (module_name
);
100 free (const_cast <char *> (cmi_name
));
104 vec
<const char *> targets
;
105 vec
<const char *> deps
;
107 vec
<const char *> modules
;
110 const char *module_name
;
111 const char *cmi_name
;
113 unsigned short quote_lwm
;
116 /* Apply Make quoting to STR, TRAIL. Note that it's not possible to
117 quote all such characters - e.g. \n, %, *, ?, [, \ (in some
118 contexts), and ~ are not properly handled. It isn't possible to
119 get this right in any current version of Make. (??? Still true?
120 Old comment referred to 3.76.1.) */
123 munge (const char *str
, const char *trail
= nullptr)
125 static unsigned alloc
;
129 for (; str
; str
= trail
, trail
= nullptr)
131 unsigned slashes
= 0;
133 for (const char *probe
= str
; (c
= *probe
++);)
135 if (alloc
< dst
+ 4 + slashes
)
137 alloc
= alloc
* 2 + 32;
138 buf
= XRESIZEVEC (char, buf
, alloc
);
153 /* GNU make uses a weird quoting scheme for white space.
154 A space or tab preceded by 2N+1 backslashes
155 represents N backslashes followed by space; a space
156 or tab preceded by 2N backslashes represents N
157 backslashes at the end of a file name; and
158 backslashes in other contexts should not be
182 /* If T begins with any of the partial pathnames listed in d->vpathv,
183 then advance T to point beyond that pathname. */
185 apply_vpath (class mkdeps
*d
, const char *t
)
187 if (unsigned len
= d
->vpath
.size ())
188 for (unsigned i
= len
; i
--;)
190 if (!filename_ncmp (d
->vpath
[i
].str
, t
, d
->vpath
[i
].len
))
192 const char *p
= t
+ d
->vpath
[i
].len
;
193 if (!IS_DIR_SEPARATOR (*p
))
196 /* Do not simplify $(vpath)/../whatever. ??? Might not
198 if (p
[1] == '.' && p
[2] == '.' && IS_DIR_SEPARATOR (p
[3]))
202 t
= t
+ d
->vpath
[i
].len
+ 1;
208 /* Remove leading ./ in any case. */
209 while (t
[0] == '.' && IS_DIR_SEPARATOR (t
[1]))
212 /* If we removed a leading ./, then also remove any /s after the
214 while (IS_DIR_SEPARATOR (t
[0]))
221 /* Public routines. */
226 return new mkdeps ();
230 deps_free (class mkdeps
*d
)
235 /* Adds a target T. We make a copy, so it need not be a permanent
236 string. QUOTE is true if the string should be quoted. */
238 deps_add_target (class mkdeps
*d
, const char *t
, int quote
)
240 t
= xstrdup (apply_vpath (d
, t
));
244 /* Sometimes unquoted items are added after quoted ones.
245 Swap out the lowest quoted. */
246 if (d
->quote_lwm
!= d
->targets
.size ())
248 const char *lowest
= d
->targets
[d
->quote_lwm
];
249 d
->targets
[d
->quote_lwm
] = t
;
258 /* Sets the default target if none has been given already. An empty
259 string as the default target in interpreted as stdin. The string
260 is quoted for MAKE. */
262 deps_add_default_target (class mkdeps
*d
, const char *tgt
)
264 /* Only if we have no targets. */
265 if (d
->targets
.size ())
269 d
->targets
.push (xstrdup ("-"));
272 #ifndef TARGET_OBJECT_SUFFIX
273 # define TARGET_OBJECT_SUFFIX ".o"
275 const char *start
= lbasename (tgt
);
276 char *o
= (char *) alloca (strlen (start
)
277 + strlen (TARGET_OBJECT_SUFFIX
) + 1);
282 suffix
= strrchr (o
, '.');
284 suffix
= o
+ strlen (o
);
285 strcpy (suffix
, TARGET_OBJECT_SUFFIX
);
287 deps_add_target (d
, o
, 1);
292 deps_add_dep (class mkdeps
*d
, const char *t
)
296 t
= apply_vpath (d
, t
);
298 d
->deps
.push (xstrdup (t
));
302 deps_add_vpath (class mkdeps
*d
, const char *vpath
)
304 const char *elem
, *p
;
306 for (elem
= vpath
; *elem
; elem
= p
)
308 for (p
= elem
; *p
&& *p
!= ':'; p
++)
312 char *str
= XNEWVEC (char, elt
.len
+ 1);
314 memcpy (str
, elem
, elt
.len
);
323 /* Add a new module target (there can only be one). M is the module
327 deps_add_module_target (struct mkdeps
*d
, const char *m
,
328 const char *cmi
, bool is_header_unit
)
330 gcc_assert (!d
->module_name
);
332 d
->module_name
= xstrdup (m
);
333 d
->is_header_unit
= is_header_unit
;
334 d
->cmi_name
= xstrdup (cmi
);
337 /* Add a new module dependency. M is the module name. */
340 deps_add_module_dep (struct mkdeps
*d
, const char *m
)
342 d
->modules
.push (xstrdup (m
));
345 /* Write NAME, with a leading space to FP, a Makefile. Advance COL as
346 appropriate, wrap at COLMAX, returning new column number. Iff
347 QUOTE apply quoting. Append TRAIL. */
350 make_write_name (const char *name
, FILE *fp
, unsigned col
, unsigned colmax
,
351 bool quote
= true, const char *trail
= NULL
)
354 name
= munge (name
, trail
);
355 unsigned size
= strlen (name
);
359 if (colmax
&& col
+ size
> colmax
)
374 /* Write all the names in VEC via make_write_name. */
377 make_write_vec (const mkdeps::vec
<const char *> &vec
, FILE *fp
,
378 unsigned col
, unsigned colmax
, unsigned quote_lwm
= 0,
379 const char *trail
= NULL
)
381 for (unsigned ix
= 0; ix
!= vec
.size (); ix
++)
382 col
= make_write_name (vec
[ix
], fp
, col
, colmax
, ix
>= quote_lwm
, trail
);
386 /* Write the dependencies to a Makefile. If PHONY is true, add
387 .PHONY targets for all the dependencies too. */
390 make_write (const cpp_reader
*pfile
, FILE *fp
, unsigned int colmax
)
392 const mkdeps
*d
= pfile
->deps
;
395 if (colmax
&& colmax
< 34)
400 column
= make_write_vec (d
->targets
, fp
, 0, colmax
, d
->quote_lwm
);
401 if (CPP_OPTION (pfile
, deps
.modules
) && d
->cmi_name
)
402 column
= make_write_name (d
->cmi_name
, fp
, column
, colmax
);
405 make_write_vec (d
->deps
, fp
, column
, colmax
);
407 if (CPP_OPTION (pfile
, deps
.phony_targets
))
408 for (unsigned i
= 1; i
< d
->deps
.size (); i
++)
409 fprintf (fp
, "%s:\n", munge (d
->deps
[i
]));
412 if (!CPP_OPTION (pfile
, deps
.modules
))
415 if (d
->modules
.size ())
417 column
= make_write_vec (d
->targets
, fp
, 0, colmax
, d
->quote_lwm
);
419 column
= make_write_name (d
->cmi_name
, fp
, column
, colmax
);
422 column
= make_write_vec (d
->modules
, fp
, column
, colmax
, 0, ".c++m");
430 /* module-name : cmi-name */
431 column
= make_write_name (d
->module_name
, fp
, 0, colmax
,
435 column
= make_write_name (d
->cmi_name
, fp
, column
, colmax
);
438 column
= fprintf (fp
, ".PHONY:");
439 column
= make_write_name (d
->module_name
, fp
, column
, colmax
,
444 if (d
->cmi_name
&& !d
->is_header_unit
)
446 /* An order-only dependency.
447 cmi-name :| first-target
448 We can probably drop this this in favour of Make-4.3's grouped
450 column
= make_write_name (d
->cmi_name
, fp
, 0, colmax
);
453 column
= make_write_name (d
->targets
[0], fp
, column
, colmax
);
458 if (d
->modules
.size ())
460 column
= fprintf (fp
, "CXX_IMPORTS +=");
461 make_write_vec (d
->modules
, fp
, column
, colmax
, 0, ".c++m");
466 /* Write out dependencies according to the selected format (which is
467 only Make at the moment). */
468 /* Really we should be opening fp here. */
471 deps_write (const cpp_reader
*pfile
, FILE *fp
, unsigned int colmax
)
473 make_write (pfile
, fp
, colmax
);
476 /* Write out a deps buffer to a file, in a form that can be read back
477 with deps_restore. Returns nonzero on error, in which case the
478 error number will be in errno. */
481 deps_save (class mkdeps
*deps
, FILE *f
)
486 /* The cppreader structure contains makefile dependences. Write out this
489 /* The number of dependences. */
490 size
= deps
->deps
.size ();
491 if (fwrite (&size
, sizeof (size
), 1, f
) != 1)
494 /* The length of each dependence followed by the string. */
495 for (i
= 0; i
< deps
->deps
.size (); i
++)
497 size
= strlen (deps
->deps
[i
]);
498 if (fwrite (&size
, sizeof (size
), 1, f
) != 1)
500 if (fwrite (deps
->deps
[i
], size
, 1, f
) != 1)
507 /* Read back dependency information written with deps_save into
508 the deps sizefer. The third argument may be NULL, in which case
509 the dependency information is just skipped, or it may be a filename,
510 in which case that filename is skipped. */
513 deps_restore (class mkdeps
*deps
, FILE *fd
, const char *self
)
519 /* Number of dependences. */
520 if (fread (&size
, sizeof (size
), 1, fd
) != 1)
523 /* The length of each dependence string, followed by the string. */
524 for (unsigned i
= size
; i
--;)
526 /* Read in # bytes in string. */
527 if (fread (&size
, sizeof (size
), 1, fd
) != 1)
530 if (size
>= buf_size
)
532 buf_size
= size
+ 512;
533 buf
= XRESIZEVEC (char, buf
, buf_size
);
535 if (fread (buf
, 1, size
, fd
) != size
)
542 /* Generate makefile dependencies from .pch if -nopch-deps. */
543 if (self
!= NULL
&& filename_cmp (buf
, self
) != 0)
544 deps_add_dep (deps
, buf
);