1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000, 2001, 2003, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Zack Weinberg, Mar 2000
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
29 /* Keep this structure local to this file, so clients don't find it
30 easy to start making assumptions. */
34 unsigned int ntargets; /* number of slots actually occupied */
35 unsigned int targets_size; /* amt of allocated space - in words */
39 unsigned int deps_size;
44 unsigned int vpaths_size;
47 static const char *munge (const char *);
49 /* Given a filename, quote characters in that filename which are
50 significant to Make. Note that it's not possible to quote all such
51 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
52 not properly handled. It isn't possible to get this right in any
53 current version of Make. (??? Still true? Old comment referred to
57 munge (const char *filename)
63 for (p = filename, len = 0; *p; p++, len++)
69 /* GNU make uses a weird quoting scheme for white space.
70 A space or tab preceded by 2N+1 backslashes represents
71 N backslashes followed by space; a space or tab
72 preceded by 2N backslashes represents N backslashes at
73 the end of a file name; and backslashes in other
74 contexts should not be doubled. */
75 for (q = p - 1; filename <= q && *q == '\\'; q--)
81 /* '$' is quoted by doubling it. */
86 /* '#' is quoted with a backslash. */
92 /* Now we know how big to make the buffer. */
93 buffer = XNEWVEC (char, len + 1);
95 for (p = filename, dst = buffer; *p; p++, dst++)
101 for (q = p - 1; filename <= q && *q == '\\'; q--)
124 /* If T begins with any of the partial pathnames listed in d->vpathv,
125 then advance T to point beyond that pathname. */
127 apply_vpath (struct deps *d, const char *t)
132 for (i = 0; i < d->nvpaths; i++)
134 if (!strncmp (d->vpathv[i], t, d->vpathlv[i]))
136 const char *p = t + d->vpathlv[i];
137 if (!IS_DIR_SEPARATOR (*p))
140 /* Do not simplify $(vpath)/../whatever. ??? Might not
142 if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
146 t = t + d->vpathlv[i] + 1;
153 /* Remove leading ./ in any case. */
154 while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
157 /* If we removed a leading ./, then also remove any /s after the
159 while (IS_DIR_SEPARATOR (t[0]))
166 /* Public routines. */
171 return XCNEW (struct deps);
175 deps_free (struct deps *d)
181 for (i = 0; i < d->ntargets; i++)
182 free ((void *) d->targetv[i]);
188 for (i = 0; i < d->ndeps; i++)
189 free ((void *) d->depv[i]);
195 for (i = 0; i < d->nvpaths; i++)
196 free ((void *) d->vpathv[i]);
204 /* Adds a target T. We make a copy, so it need not be a permanent
205 string. QUOTE is true if the string should be quoted. */
207 deps_add_target (struct deps *d, const char *t, int quote)
209 if (d->ntargets == d->targets_size)
211 d->targets_size = d->targets_size * 2 + 4;
212 d->targetv = XRESIZEVEC (const char *, d->targetv, d->targets_size);
215 t = apply_vpath (d, t);
217 t = munge (t); /* Also makes permanent copy. */
221 d->targetv[d->ntargets++] = t;
224 /* Sets the default target if none has been given already. An empty
225 string as the default target in interpreted as stdin. The string
226 is quoted for MAKE. */
228 deps_add_default_target (cpp_reader *pfile, const char *tgt)
230 struct deps *d = pfile->deps;
232 /* Only if we have no targets. */
237 deps_add_target (d, "-", 1);
240 #ifndef TARGET_OBJECT_SUFFIX
241 # define TARGET_OBJECT_SUFFIX ".o"
243 const char *start = lbasename (tgt);
249 if (NULL == CPP_OPTION (pfile, obj_ext))
250 obj_ext = TARGET_OBJECT_SUFFIX;
251 else if (CPP_OPTION (pfile, obj_ext)[0] != '.')
253 char *t = alloca (strlen (CPP_OPTION (pfile, obj_ext)) + 2);
255 strcpy (&t[1], CPP_OPTION (pfile, obj_ext));
259 obj_ext = CPP_OPTION (pfile, obj_ext);
261 o = (char *) alloca (strlen (start) + strlen (obj_ext) + 1);
264 suffix = strrchr (o, '.');
266 suffix = o + strlen (o);
267 strcpy (suffix, obj_ext);
269 deps_add_target (d, o, 1);
274 deps_add_dep (struct deps *d, const char *t)
276 t = munge (apply_vpath (d, t)); /* Also makes permanent copy. */
278 if (d->ndeps == d->deps_size)
280 d->deps_size = d->deps_size * 2 + 8;
281 d->depv = XRESIZEVEC (const char *, d->depv, d->deps_size);
283 d->depv[d->ndeps++] = t;
287 deps_add_vpath (struct deps *d, const char *vpath)
289 const char *elem, *p;
293 for (elem = vpath; *elem; elem = p)
295 for (p = elem; *p && *p != ':'; p++);
297 copy = XNEWVEC (char, len + 1);
298 memcpy (copy, elem, len);
303 if (d->nvpaths == d->vpaths_size)
305 d->vpaths_size = d->vpaths_size * 2 + 8;
306 d->vpathv = XRESIZEVEC (const char *, d->vpathv, d->vpaths_size);
307 d->vpathlv = XRESIZEVEC (size_t, d->vpathlv, d->vpaths_size);
309 d->vpathv[d->nvpaths] = copy;
310 d->vpathlv[d->nvpaths] = len;
316 deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
318 unsigned int size, i, column;
321 if (colmax && colmax < 34)
324 for (i = 0; i < d->ntargets; i++)
326 size = strlen (d->targetv[i]);
330 if (colmax && column > colmax)
332 fputs (" \\\n ", fp);
341 fputs (d->targetv[i], fp);
347 for (i = 0; i < d->ndeps; i++)
349 size = strlen (d->depv[i]);
351 if (colmax && column > colmax)
353 fputs (" \\\n ", fp);
361 fputs (d->depv[i], fp);
367 deps_phony_targets (const struct deps *d, FILE *fp)
371 for (i = 1; i < d->ndeps; i++)
374 fputs (d->depv[i], fp);
380 /* Write out a deps buffer to a file, in a form that can be read back
381 with deps_restore. Returns nonzero on error, in which case the
382 error number will be in errno. */
385 deps_save (struct deps *deps, FILE *f)
389 /* The cppreader structure contains makefile dependences. Write out this
392 /* The number of dependences. */
393 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
395 /* The length of each dependence followed by the string. */
396 for (i = 0; i < deps->ndeps; i++)
398 size_t num_to_write = strlen (deps->depv[i]);
399 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
401 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
408 /* Read back dependency information written with deps_save into
409 the deps buffer. The third argument may be NULL, in which case
410 the dependency information is just skipped, or it may be a filename,
411 in which case that filename is skipped. */
414 deps_restore (struct deps *deps, FILE *fd, const char *self)
416 unsigned int i, count;
418 size_t buf_size = 512;
419 char *buf = XNEWVEC (char, buf_size);
421 /* Number of dependences. */
422 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
425 /* The length of each dependence string, followed by the string. */
426 for (i = 0; i < count; i++)
428 /* Read in # bytes in string. */
429 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
431 if (buf_size < num_to_read + 1)
433 buf_size = num_to_read + 1 + 127;
434 buf = XRESIZEVEC (char, buf, buf_size);
436 if (fread (buf, 1, num_to_read, fd) != num_to_read)
438 buf[num_to_read] = '\0';
440 /* Generate makefile dependencies from .pch if -nopch-deps. */
441 if (self != NULL && strcmp (buf, self) != 0)
442 deps_add_dep (deps, buf);