1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000, 2001, 2003 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
41 static const char *munge (const char *);
43 /* Given a filename, quote characters in that filename which are
44 significant to Make. Note that it's not possible to quote all such
45 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
46 not properly handled. It isn't possible to get this right in any
47 current version of Make. (??? Still true? Old comment referred to
51 munge (const char *filename)
57 for (p = filename, len = 0; *p; p++, len++)
63 /* GNU make uses a weird quoting scheme for white space.
64 A space or tab preceded by 2N+1 backslashes represents
65 N backslashes followed by space; a space or tab
66 preceded by 2N backslashes represents N backslashes at
67 the end of a file name; and backslashes in other
68 contexts should not be doubled. */
69 for (q = p - 1; filename <= q && *q == '\\'; q--)
75 /* '$' is quoted by doubling it. */
81 /* Now we know how big to make the buffer. */
82 buffer = xmalloc (len + 1);
84 for (p = filename, dst = buffer; *p; p++, dst++)
90 for (q = p - 1; filename <= q && *q == '\\'; q--)
109 /* Public routines. */
114 struct deps *d = xmalloc (sizeof (struct deps));
116 /* Allocate space for the vectors only if we need it. */
130 deps_free (struct deps *d)
136 for (i = 0; i < d->ntargets; i++)
137 free ((void *) d->targetv[i]);
143 for (i = 0; i < d->ndeps; i++)
144 free ((void *) d->depv[i]);
151 /* Adds a target T. We make a copy, so it need not be a permanent
152 string. QUOTE is true if the string should be quoted. */
154 deps_add_target (struct deps *d, const char *t, int quote)
156 if (d->ntargets == d->targets_size)
158 d->targets_size = d->targets_size * 2 + 4;
159 d->targetv = xrealloc (d->targetv,
160 d->targets_size * sizeof (const char *));
164 t = munge (t); /* Also makes permanent copy. */
168 d->targetv[d->ntargets++] = t;
171 /* Sets the default target if none has been given already. An empty
172 string as the default target in interpreted as stdin. The string
173 is quoted for MAKE. */
175 deps_add_default_target (cpp_reader *pfile, const char *tgt)
177 struct deps *d = pfile->deps;
179 /* Only if we have no targets. */
184 deps_add_target (d, "-", 1);
187 #ifndef TARGET_OBJECT_SUFFIX
188 # define TARGET_OBJECT_SUFFIX ".o"
190 const char *start = lbasename (tgt);
195 if (NULL == CPP_OPTION (pfile, obj_ext))
196 obj_ext = TARGET_OBJECT_SUFFIX;
197 else if (CPP_OPTION (pfile, obj_ext)[0] != '.')
199 char *t = alloca (strlen (CPP_OPTION (pfile, obj_ext)) + 2);
201 strcpy (&t[1], CPP_OPTION (pfile, obj_ext));
205 obj_ext = CPP_OPTION (pfile, obj_ext);
207 o = (char *) alloca (strlen (start) + strlen (obj_ext) + 1);
211 suffix = strrchr (o, '.');
213 suffix = o + strlen (o);
214 strcpy (suffix, obj_ext);
216 deps_add_target (d, o, 1);
221 deps_add_dep (struct deps *d, const char *t)
223 t = munge (t); /* Also makes permanent copy. */
225 if (d->ndeps == d->deps_size)
227 d->deps_size = d->deps_size * 2 + 8;
228 d->depv = xrealloc (d->depv, d->deps_size * sizeof (const char *));
230 d->depv[d->ndeps++] = t;
234 deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
236 unsigned int size, i, column;
239 if (colmax && colmax < 34)
242 for (i = 0; i < d->ntargets; i++)
244 size = strlen (d->targetv[i]);
246 if (colmax && column > colmax)
248 fputs (" \\\n ", fp);
256 fputs (d->targetv[i], fp);
263 for (i = 0; i < d->ndeps; i++)
265 size = strlen (d->depv[i]);
267 if (colmax && column > colmax)
269 fputs (" \\\n ", fp);
277 fputs (d->depv[i], fp);
283 deps_phony_targets (const struct deps *d, FILE *fp)
287 for (i = 1; i < d->ndeps; i++)
290 fputs (d->depv[i], fp);
296 /* Write out a deps buffer to a file, in a form that can be read back
297 with deps_restore. Returns nonzero on error, in which case the
298 error number will be in errno. */
301 deps_save (struct deps *deps, FILE *f)
305 /* The cppreader structure contains makefile dependences. Write out this
308 /* The number of dependences. */
309 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
311 /* The length of each dependence followed by the string. */
312 for (i = 0; i < deps->ndeps; i++)
314 size_t num_to_write = strlen (deps->depv[i]);
315 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
317 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
324 /* Read back dependency information written with deps_save into
325 the deps buffer. The third argument may be NULL, in which case
326 the dependency information is just skipped, or it may be a filename,
327 in which case that filename is skipped. */
330 deps_restore (struct deps *deps, FILE *fd, const char *self)
332 unsigned int i, count;
334 size_t buf_size = 512;
335 char *buf = xmalloc (buf_size);
337 /* Number of dependences. */
338 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
341 /* The length of each dependence string, followed by the string. */
342 for (i = 0; i < count; i++)
344 /* Read in # bytes in string. */
345 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
347 if (buf_size < num_to_read + 1)
349 buf_size = num_to_read + 1 + 127;
350 buf = xrealloc (buf, buf_size);
352 if (fread (buf, 1, num_to_read, fd) != num_to_read)
354 buf[num_to_read] = '\0';
356 /* Generate makefile dependencies from .pch if -nopch-deps. */
357 if (self != NULL && strcmp (buf, self) != 0)
358 deps_add_dep (deps, buf);