1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000, 2001, 2003, 2007 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 2, 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; if not, write to the Free Software
17 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 /* Keep this structure local to this file, so clients don't find it
29 easy to start making assumptions. */
33 unsigned int ntargets; /* number of slots actually occupied */
34 unsigned int targets_size; /* amt of allocated space - in words */
38 unsigned int deps_size;
43 unsigned int vpaths_size;
46 static const char *munge (const char *);
48 /* Given a filename, quote characters in that filename which are
49 significant to Make. Note that it's not possible to quote all such
50 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
51 not properly handled. It isn't possible to get this right in any
52 current version of Make. (??? Still true? Old comment referred to
56 munge (const char *filename)
62 for (p = filename, len = 0; *p; p++, len++)
68 /* GNU make uses a weird quoting scheme for white space.
69 A space or tab preceded by 2N+1 backslashes represents
70 N backslashes followed by space; a space or tab
71 preceded by 2N backslashes represents N backslashes at
72 the end of a file name; and backslashes in other
73 contexts should not be doubled. */
74 for (q = p - 1; filename <= q && *q == '\\'; q--)
80 /* '$' is quoted by doubling it. */
86 /* Now we know how big to make the buffer. */
87 buffer = XNEWVEC (char, len + 1);
89 for (p = filename, dst = buffer; *p; p++, dst++)
95 for (q = p - 1; filename <= q && *q == '\\'; q--)
114 /* If T begins with any of the partial pathnames listed in d->vpathv,
115 then advance T to point beyond that pathname. */
117 apply_vpath (struct deps *d, const char *t)
122 for (i = 0; i < d->nvpaths; i++)
124 if (!strncmp (d->vpathv[i], t, d->vpathlv[i]))
126 const char *p = t + d->vpathlv[i];
127 if (!IS_DIR_SEPARATOR (*p))
130 /* Do not simplify $(vpath)/../whatever. ??? Might not
132 if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
136 t = t + d->vpathlv[i] + 1;
143 /* Remove leading ./ in any case. */
144 while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
147 /* If we removed a leading ./, then also remove any /s after the
149 while (IS_DIR_SEPARATOR (t[0]))
156 /* Public routines. */
161 return XCNEW (struct deps);
165 deps_free (struct deps *d)
171 for (i = 0; i < d->ntargets; i++)
172 free ((void *) d->targetv[i]);
178 for (i = 0; i < d->ndeps; i++)
179 free ((void *) d->depv[i]);
185 for (i = 0; i < d->nvpaths; i++)
186 free ((void *) d->vpathv[i]);
194 /* Adds a target T. We make a copy, so it need not be a permanent
195 string. QUOTE is true if the string should be quoted. */
197 deps_add_target (struct deps *d, const char *t, int quote)
199 if (d->ntargets == d->targets_size)
201 d->targets_size = d->targets_size * 2 + 4;
202 d->targetv = XRESIZEVEC (const char *, d->targetv, d->targets_size);
205 t = apply_vpath (d, t);
207 t = munge (t); /* Also makes permanent copy. */
211 d->targetv[d->ntargets++] = t;
214 /* Sets the default target if none has been given already. An empty
215 string as the default target in interpreted as stdin. The string
216 is quoted for MAKE. */
218 deps_add_default_target (cpp_reader *pfile, const char *tgt)
220 struct deps *d = pfile->deps;
222 /* Only if we have no targets. */
227 deps_add_target (d, "-", 1);
230 #ifndef TARGET_OBJECT_SUFFIX
231 # define TARGET_OBJECT_SUFFIX ".o"
233 const char *start = lbasename (tgt);
238 if (NULL == CPP_OPTION (pfile, obj_ext))
239 obj_ext = TARGET_OBJECT_SUFFIX;
240 else if (CPP_OPTION (pfile, obj_ext)[0] != '.')
242 char *t = alloca (strlen (CPP_OPTION (pfile, obj_ext)) + 2);
244 strcpy (&t[1], CPP_OPTION (pfile, obj_ext));
248 obj_ext = CPP_OPTION (pfile, obj_ext);
250 o = (char *) alloca (strlen (start) + strlen (obj_ext) + 1);
254 suffix = strrchr (o, '.');
256 suffix = o + strlen (o);
257 strcpy (suffix, obj_ext);
259 deps_add_target (d, o, 1);
264 deps_add_dep (struct deps *d, const char *t)
266 t = munge (apply_vpath (d, t)); /* Also makes permanent copy. */
268 if (d->ndeps == d->deps_size)
270 d->deps_size = d->deps_size * 2 + 8;
271 d->depv = XRESIZEVEC (const char *, d->depv, d->deps_size);
273 d->depv[d->ndeps++] = t;
277 deps_add_vpath (struct deps *d, const char *vpath)
279 const char *elem, *p;
283 for (elem = vpath; *elem; elem = p)
285 for (p = elem; *p && *p != ':'; p++);
287 copy = XNEWVEC (char, len + 1);
288 memcpy (copy, elem, len);
293 if (d->nvpaths == d->vpaths_size)
295 d->vpaths_size = d->vpaths_size * 2 + 8;
296 d->vpathv = XRESIZEVEC (const char *, d->vpathv, d->vpaths_size);
297 d->vpathlv = XRESIZEVEC (size_t, d->vpathlv, d->vpaths_size);
299 d->vpathv[d->nvpaths] = copy;
300 d->vpathlv[d->nvpaths] = len;
306 deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
308 unsigned int size, i, column;
311 if (colmax && colmax < 34)
314 for (i = 0; i < d->ntargets; i++)
316 size = strlen (d->targetv[i]);
318 if (colmax && column > colmax)
320 fputs (" \\\n ", fp);
328 fputs (d->targetv[i], fp);
335 for (i = 0; i < d->ndeps; i++)
337 size = strlen (d->depv[i]);
339 if (colmax && column > colmax)
341 fputs (" \\\n ", fp);
349 fputs (d->depv[i], fp);
355 deps_phony_targets (const struct deps *d, FILE *fp)
359 for (i = 1; i < d->ndeps; i++)
362 fputs (d->depv[i], fp);
368 /* Write out a deps buffer to a file, in a form that can be read back
369 with deps_restore. Returns nonzero on error, in which case the
370 error number will be in errno. */
373 deps_save (struct deps *deps, FILE *f)
377 /* The cppreader structure contains makefile dependences. Write out this
380 /* The number of dependences. */
381 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
383 /* The length of each dependence followed by the string. */
384 for (i = 0; i < deps->ndeps; i++)
386 size_t num_to_write = strlen (deps->depv[i]);
387 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
389 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
396 /* Read back dependency information written with deps_save into
397 the deps buffer. The third argument may be NULL, in which case
398 the dependency information is just skipped, or it may be a filename,
399 in which case that filename is skipped. */
402 deps_restore (struct deps *deps, FILE *fd, const char *self)
404 unsigned int i, count;
406 size_t buf_size = 512;
407 char *buf = XNEWVEC (char, buf_size);
409 /* Number of dependences. */
410 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
413 /* The length of each dependence string, followed by the string. */
414 for (i = 0; i < count; i++)
416 /* Read in # bytes in string. */
417 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
419 if (buf_size < num_to_read + 1)
421 buf_size = num_to_read + 1 + 127;
422 buf = XRESIZEVEC (char, buf, buf_size);
424 if (fread (buf, 1, num_to_read, fd) != num_to_read)
426 buf[num_to_read] = '\0';
428 /* Generate makefile dependencies from .pch if -nopch-deps. */
429 if (self != NULL && strcmp (buf, self) != 0)
430 deps_add_dep (deps, buf);