1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000-2022 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! */
29 #define untested() { fprintf (stderr, "@@#\n@@@:%s:%d:%s\n", __FILE__, __LINE__, __func__); }
31 /* Not set up to just include std::vector et al, here's a simple
34 /* Keep this structure local to this file, so clients don't find it
35 easy to start making assumptions. */
39 /* T has trivial cctor & dtor. */
50 : ary (NULL
), num (0), alloc (0)
58 unsigned size () const
62 const T
&operator[] (unsigned ix
) const
66 T
&operator[] (unsigned ix
)
70 void push (const T
&elt
)
74 alloc
= alloc
? alloc
* 2 : 16;
75 ary
= XRESIZEVEC (T
, ary
, alloc
);
87 : module_name (NULL
), cmi_name (NULL
), is_header_unit (false), quote_lwm (0)
94 for (i
= targets
.size (); i
--;)
95 free (const_cast <char *> (targets
[i
]));
96 for (i
= deps
.size (); i
--;)
97 free (const_cast <char *> (deps
[i
]));
98 for (i
= vpath
.size (); i
--;)
99 XDELETEVEC (vpath
[i
].str
);
100 for (i
= modules
.size (); i
--;)
101 XDELETEVEC (modules
[i
]);
102 XDELETEVEC (module_name
);
103 free (const_cast <char *> (cmi_name
));
107 vec
<const char *> targets
;
108 vec
<const char *> deps
;
110 vec
<const char *> modules
;
113 const char *module_name
;
114 const char *cmi_name
;
116 unsigned short quote_lwm
;
119 /* Apply Make quoting to STR, TRAIL. Note that it's not possible to
120 quote all such characters - e.g. \n, %, *, ?, [, \ (in some
121 contexts), and ~ are not properly handled. It isn't possible to
122 get this right in any current version of Make. (??? Still true?
123 Old comment referred to 3.76.1.) */
126 munge (const char *str
, const char *trail
= nullptr)
128 static unsigned alloc
;
132 for (; str
; str
= trail
, trail
= nullptr)
134 unsigned slashes
= 0;
136 for (const char *probe
= str
; (c
= *probe
++);)
138 if (alloc
< dst
+ 4 + slashes
)
140 alloc
= alloc
* 2 + 32;
141 buf
= XRESIZEVEC (char, buf
, alloc
);
156 /* GNU make uses a weird quoting scheme for white space.
157 A space or tab preceded by 2N+1 backslashes
158 represents N backslashes followed by space; a space
159 or tab preceded by 2N backslashes represents N
160 backslashes at the end of a file name; and
161 backslashes in other contexts should not be
185 /* If T begins with any of the partial pathnames listed in d->vpathv,
186 then advance T to point beyond that pathname. */
188 apply_vpath (class mkdeps
*d
, const char *t
)
190 if (unsigned len
= d
->vpath
.size ())
191 for (unsigned i
= len
; i
--;)
193 if (!filename_ncmp (d
->vpath
[i
].str
, t
, d
->vpath
[i
].len
))
195 const char *p
= t
+ d
->vpath
[i
].len
;
196 if (!IS_DIR_SEPARATOR (*p
))
199 /* Do not simplify $(vpath)/../whatever. ??? Might not
201 if (p
[1] == '.' && p
[2] == '.' && IS_DIR_SEPARATOR (p
[3]))
205 t
= t
+ d
->vpath
[i
].len
+ 1;
211 /* Remove leading ./ in any case. */
212 while (t
[0] == '.' && IS_DIR_SEPARATOR (t
[1]))
215 /* If we removed a leading ./, then also remove any /s after the
217 while (IS_DIR_SEPARATOR (t
[0]))
224 /* Public routines. */
229 return new mkdeps ();
233 deps_free (class mkdeps
*d
)
238 /* Adds a target T. We make a copy, so it need not be a permanent
239 string. QUOTE is true if the string should be quoted. */
241 deps_add_target (class mkdeps
*d
, const char *t
, int quote
)
243 t
= xstrdup (apply_vpath (d
, t
));
247 /* Sometimes unquoted items are added after quoted ones.
248 Swap out the lowest quoted. */
249 if (d
->quote_lwm
!= d
->targets
.size ())
251 const char *lowest
= d
->targets
[d
->quote_lwm
];
252 d
->targets
[d
->quote_lwm
] = t
;
261 /* Sets the default target if none has been given already. An empty
262 string as the default target in interpreted as stdin. The string
263 is quoted for MAKE. */
265 deps_add_default_target (const class cpp_reader
*pfile
, class mkdeps
*d
, const char *tgt
)
267 /* Only if we have no targets. */
268 if (d
->targets
.size ())
273 d
->targets
.push (xstrdup ("-"));
277 #ifndef TARGET_OBJECT_SUFFIX
278 # define TARGET_OBJECT_SUFFIX ".o"
280 const char *start
= lbasename (tgt
);
285 if (NULL
== CPP_OPTION (pfile
, obj_ext
))
287 obj_ext
= TARGET_OBJECT_SUFFIX
;
289 else if (CPP_OPTION (pfile
, obj_ext
)[0] != '.')
291 char *t
= (char*) alloca (strlen (CPP_OPTION (pfile
, obj_ext
)) + 2);
293 strcpy (&t
[1], CPP_OPTION (pfile
, obj_ext
));
298 obj_ext
= CPP_OPTION (pfile
, obj_ext
);
301 // if obj_ext != ".o":
302 // fprintf(stderr, "custom objext %s, deprecate this?\n", obj_ext);
304 char *o
= (char *) alloca (strlen (start
) + strlen (obj_ext
) + 1);
308 suffix
= strrchr (o
, '.');
309 if (!suffix
){ untested();
310 suffix
= o
+ strlen (o
);
313 strcpy (suffix
, obj_ext
);
315 deps_add_target (d
, o
, 1);
320 deps_add_dep (class mkdeps
*d
, const char *t
)
324 t
= apply_vpath (d
, t
);
326 d
->deps
.push (xstrdup (t
));
330 deps_add_vpath (class mkdeps
*d
, const char *vpath
)
332 const char *elem
, *p
;
334 for (elem
= vpath
; *elem
; elem
= p
)
336 for (p
= elem
; *p
&& *p
!= ':'; p
++)
340 char *str
= XNEWVEC (char, elt
.len
+ 1);
342 memcpy (str
, elem
, elt
.len
);
351 /* Add a new module target (there can only be one). M is the module
355 deps_add_module_target (struct mkdeps
*d
, const char *m
,
356 const char *cmi
, bool is_header_unit
)
358 gcc_assert (!d
->module_name
);
360 d
->module_name
= xstrdup (m
);
361 d
->is_header_unit
= is_header_unit
;
362 d
->cmi_name
= xstrdup (cmi
);
365 /* Add a new module dependency. M is the module name. */
368 deps_add_module_dep (struct mkdeps
*d
, const char *m
)
370 d
->modules
.push (xstrdup (m
));
373 /* Write NAME, with a leading space to FP, a Makefile. Advance COL as
374 appropriate, wrap at COLMAX, returning new column number. Iff
375 QUOTE apply quoting. Append TRAIL. */
378 make_write_name (const char *name
, FILE *fp
, unsigned col
, unsigned colmax
,
379 bool quote
= true, const char *trail
= NULL
)
382 name
= munge (name
, trail
);
383 unsigned size
= strlen (name
);
387 if (colmax
&& col
+ size
> colmax
)
402 /* Write all the names in VEC via make_write_name. */
405 make_write_vec (const mkdeps::vec
<const char *> &vec
, FILE *fp
,
406 unsigned col
, unsigned colmax
, unsigned quote_lwm
= 0,
407 const char *trail
= NULL
)
409 for (unsigned ix
= 0; ix
!= vec
.size (); ix
++)
410 col
= make_write_name (vec
[ix
], fp
, col
, colmax
, ix
>= quote_lwm
, trail
);
414 /* Write the dependencies to a Makefile. If PHONY is true, add
415 .PHONY targets for all the dependencies too. */
418 make_write (const cpp_reader
*pfile
, FILE *fp
, unsigned int colmax
)
420 const mkdeps
*d
= pfile
->deps
;
423 if (colmax
&& colmax
< 34)
428 column
= make_write_vec (d
->targets
, fp
, 0, colmax
, d
->quote_lwm
);
429 if (CPP_OPTION (pfile
, deps
.modules
) && d
->cmi_name
)
430 column
= make_write_name (d
->cmi_name
, fp
, column
, colmax
);
433 make_write_vec (d
->deps
, fp
, column
, colmax
);
435 if (CPP_OPTION (pfile
, deps
.phony_targets
))
436 for (unsigned i
= 1; i
< d
->deps
.size (); i
++)
437 fprintf (fp
, "%s:\n", munge (d
->deps
[i
]));
440 if (!CPP_OPTION (pfile
, deps
.modules
))
443 if (d
->modules
.size ())
445 column
= make_write_vec (d
->targets
, fp
, 0, colmax
, d
->quote_lwm
);
447 column
= make_write_name (d
->cmi_name
, fp
, column
, colmax
);
450 column
= make_write_vec (d
->modules
, fp
, column
, colmax
, 0, ".c++m");
458 /* module-name : cmi-name */
459 column
= make_write_name (d
->module_name
, fp
, 0, colmax
,
463 column
= make_write_name (d
->cmi_name
, fp
, column
, colmax
);
466 column
= fprintf (fp
, ".PHONY:");
467 column
= make_write_name (d
->module_name
, fp
, column
, colmax
,
472 if (d
->cmi_name
&& !d
->is_header_unit
)
474 /* An order-only dependency.
475 cmi-name :| first-target
476 We can probably drop this this in favour of Make-4.3's grouped
478 column
= make_write_name (d
->cmi_name
, fp
, 0, colmax
);
481 column
= make_write_name (d
->targets
[0], fp
, column
, colmax
);
486 if (d
->modules
.size ())
488 column
= fprintf (fp
, "CXX_IMPORTS +=");
489 make_write_vec (d
->modules
, fp
, column
, colmax
, 0, ".c++m");
494 /* Write out dependencies according to the selected format (which is
495 only Make at the moment). */
496 /* Really we should be opening fp here. */
499 deps_write (const cpp_reader
*pfile
, FILE *fp
, unsigned int colmax
)
501 make_write (pfile
, fp
, colmax
);
504 /* Write out a deps buffer to a file, in a form that can be read back
505 with deps_restore. Returns nonzero on error, in which case the
506 error number will be in errno. */
509 deps_save (class mkdeps
*deps
, FILE *f
)
514 /* The cppreader structure contains makefile dependences. Write out this
517 /* The number of dependences. */
518 size
= deps
->deps
.size ();
519 if (fwrite (&size
, sizeof (size
), 1, f
) != 1)
522 /* The length of each dependence followed by the string. */
523 for (i
= 0; i
< deps
->deps
.size (); i
++)
525 size
= strlen (deps
->deps
[i
]);
526 if (fwrite (&size
, sizeof (size
), 1, f
) != 1)
528 if (fwrite (deps
->deps
[i
], size
, 1, f
) != 1)
535 /* Read back dependency information written with deps_save into
536 the deps sizefer. The third argument may be NULL, in which case
537 the dependency information is just skipped, or it may be a filename,
538 in which case that filename is skipped. */
541 deps_restore (class mkdeps
*deps
, FILE *fd
, const char *self
)
547 /* Number of dependences. */
548 if (fread (&size
, sizeof (size
), 1, fd
) != 1)
551 /* The length of each dependence string, followed by the string. */
552 for (unsigned i
= size
; i
--;)
554 /* Read in # bytes in string. */
555 if (fread (&size
, sizeof (size
), 1, fd
) != 1)
558 if (size
>= buf_size
)
560 buf_size
= size
+ 512;
561 buf
= XRESIZEVEC (char, buf
, buf_size
);
563 if (fread (buf
, 1, size
, fd
) != size
)
570 /* Generate makefile dependencies from .pch if -nopch-deps. */
571 if (self
!= NULL
&& filename_cmp (buf
, self
) != 0)
572 deps_add_dep (deps
, buf
);