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! */
27 /* Keep this structure local to this file, so clients don't find it
28 easy to start making assumptions. */
32 unsigned int ntargets; /* number of slots actually occupied */
33 unsigned int targets_size; /* amt of allocated space - in words */
37 unsigned int deps_size;
40 static const char *munge (const char *);
42 /* Given a filename, quote characters in that filename which are
43 significant to Make. Note that it's not possible to quote all such
44 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
45 not properly handled. It isn't possible to get this right in any
46 current version of Make. (??? Still true? Old comment referred to
50 munge (const char *filename)
56 for (p = filename, len = 0; *p; p++, len++)
62 /* GNU make uses a weird quoting scheme for white space.
63 A space or tab preceded by 2N+1 backslashes represents
64 N backslashes followed by space; a space or tab
65 preceded by 2N backslashes represents N backslashes at
66 the end of a file name; and backslashes in other
67 contexts should not be doubled. */
68 for (q = p - 1; filename <= q && *q == '\\'; q--)
74 /* '$' is quoted by doubling it. */
80 /* Now we know how big to make the buffer. */
81 buffer = xmalloc (len + 1);
83 for (p = filename, dst = buffer; *p; p++, dst++)
89 for (q = p - 1; filename <= q && *q == '\\'; q--)
108 /* Public routines. */
113 struct deps *d = (struct deps *) xmalloc (sizeof (struct deps));
115 /* Allocate space for the vectors only if we need it. */
129 deps_free (struct deps *d)
135 for (i = 0; i < d->ntargets; i++)
136 free ((void *) d->targetv[i]);
142 for (i = 0; i < d->ndeps; i++)
143 free ((void *) d->depv[i]);
150 /* Adds a target T. We make a copy, so it need not be a permanent
151 string. QUOTE is true if the string should be quoted. */
153 deps_add_target (struct deps *d, const char *t, int quote)
155 if (d->ntargets == d->targets_size)
157 d->targets_size = d->targets_size * 2 + 4;
158 d->targetv = (const char **) xrealloc (d->targetv,
159 d->targets_size * sizeof (const char *));
163 t = munge (t); /* Also makes permanent copy. */
167 d->targetv[d->ntargets++] = t;
170 /* Sets the default target if none has been given already. An empty
171 string as the default target in interpreted as stdin. The string
172 is quoted for MAKE. */
174 deps_add_default_target (struct deps *d, const char *tgt)
176 /* Only if we have no targets. */
181 deps_add_target (d, "-", 1);
184 #ifndef TARGET_OBJECT_SUFFIX
185 # define TARGET_OBJECT_SUFFIX ".o"
187 const char *start = lbasename (tgt);
188 char *o = (char *) alloca (strlen (start) + strlen (TARGET_OBJECT_SUFFIX) + 1);
193 suffix = strrchr (o, '.');
195 suffix = o + strlen (o);
196 strcpy (suffix, TARGET_OBJECT_SUFFIX);
198 deps_add_target (d, o, 1);
203 deps_add_dep (struct deps *d, const char *t)
205 t = munge (t); /* Also makes permanent copy. */
207 if (d->ndeps == d->deps_size)
209 d->deps_size = d->deps_size * 2 + 8;
210 d->depv = (const char **)
211 xrealloc (d->depv, d->deps_size * sizeof (const char *));
213 d->depv[d->ndeps++] = t;
217 deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
219 unsigned int size, i, column;
222 if (colmax && colmax < 34)
225 for (i = 0; i < d->ntargets; i++)
227 size = strlen (d->targetv[i]);
229 if (colmax && column > colmax)
231 fputs (" \\\n ", fp);
239 fputs (d->targetv[i], fp);
246 for (i = 0; i < d->ndeps; i++)
248 size = strlen (d->depv[i]);
250 if (colmax && column > colmax)
252 fputs (" \\\n ", fp);
260 fputs (d->depv[i], fp);
266 deps_phony_targets (const struct deps *d, FILE *fp)
270 for (i = 1; i < d->ndeps; i++)
273 fputs (d->depv[i], fp);
279 /* Write out a deps buffer to a file, in a form that can be read back
280 with deps_restore. Returns nonzero on error, in which case the
281 error number will be in errno. */
284 deps_save (struct deps *deps, FILE *f)
288 /* The cppreader structure contains makefile dependences. Write out this
291 /* The number of dependences. */
292 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
294 /* The length of each dependence followed by the string. */
295 for (i = 0; i < deps->ndeps; i++)
297 size_t num_to_write = strlen (deps->depv[i]);
298 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
300 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
307 /* Read back dependency information written with deps_save into
308 the deps buffer. The third argument may be NULL, in which case
309 the dependency information is just skipped, or it may be a filename,
310 in which case that filename is skipped. */
313 deps_restore (struct deps *deps, FILE *fd, const char *self)
315 unsigned int i, count;
317 size_t buf_size = 512;
318 char *buf = (char *) xmalloc (buf_size);
320 /* Number of dependences. */
321 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
324 /* The length of each dependence string, followed by the string. */
325 for (i = 0; i < count; i++)
327 /* Read in # bytes in string. */
328 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
330 if (buf_size < num_to_read + 1)
332 buf_size = num_to_read + 1 + 127;
333 buf = xrealloc (buf, buf_size);
335 if (fread (buf, 1, num_to_read, fd) != num_to_read)
337 buf[num_to_read] = '\0';
339 /* Generate makefile dependencies from .pch if -nopch-deps. */
340 if (self != NULL && strcmp (buf, self) != 0)
341 deps_add_dep (deps, buf);