1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000, 2001 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! */
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;
42 static const char *munge PARAMS ((const char *));
44 /* Given a filename, quote characters in that filename which are
45 significant to Make. Note that it's not possible to quote all such
46 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
47 not properly handled. It isn't possible to get this right in any
48 current version of Make. (??? Still true? Old comment referred to
59 for (p = filename, len = 0; *p; p++, len++)
65 /* GNU make uses a weird quoting scheme for white space.
66 A space or tab preceded by 2N+1 backslashes represents
67 N backslashes followed by space; a space or tab
68 preceded by 2N backslashes represents N backslashes at
69 the end of a file name; and backslashes in other
70 contexts should not be doubled. */
71 for (q = p - 1; filename <= q && *q == '\\'; q--)
77 /* '$' is quoted by doubling it. */
83 /* Now we know how big to make the buffer. */
84 buffer = xmalloc (len + 1);
86 for (p = filename, dst = buffer; *p; p++, dst++)
92 for (q = p - 1; filename <= q && *q == '\\'; q--)
111 /* Public routines. */
116 struct deps *d = (struct deps *) xmalloc (sizeof (struct deps));
118 /* Allocate space for the vectors only if we need it. */
139 for (i = 0; i < d->ntargets; i++)
140 free ((PTR) d->targetv[i]);
146 for (i = 0; i < d->ndeps; i++)
147 free ((PTR) d->depv[i]);
154 /* Adds a target T. We make a copy, so it need not be a permanent
155 string. QUOTE is true if the string should be quoted. */
157 deps_add_target (d, t, quote)
162 if (d->ntargets == d->targets_size)
164 d->targets_size = d->targets_size * 2 + 4;
165 d->targetv = (const char **) xrealloc (d->targetv,
166 d->targets_size * sizeof (const char *));
170 t = munge (t); /* Also makes permanent copy. */
174 d->targetv[d->ntargets++] = t;
177 /* Sets the default target if none has been given already. An empty
178 string as the default target in interpreted as stdin. The string
179 is quoted for MAKE. */
181 deps_add_default_target (pfile, tgt)
185 /* Only if we have no targets. */
186 if (pfile->deps->ntargets)
190 deps_add_target (pfile->deps, "-", 1);
193 #ifndef TARGET_OBJECT_SUFFIX
194 # define TARGET_OBJECT_SUFFIX ".o"
196 char *start = lbasename (tgt);
201 if (NULL == CPP_OPTION (pfile, obj_ext))
202 obj_ext = TARGET_OBJECT_SUFFIX;
203 else if (CPP_OPTION (pfile, obj_ext)[0] != '.')
205 char *t = alloca (strlen (CPP_OPTION (pfile, obj_ext)) + 2);
207 strcpy (&t[1], CPP_OPTION (pfile, obj_ext));
211 obj_ext = CPP_OPTION (pfile, obj_ext);
213 o = (char *) alloca (strlen (start) + strlen (obj_ext) + 1);
217 suffix = strrchr (o, '.');
219 suffix = o + strlen (o);
220 strcpy (suffix, obj_ext);
222 deps_add_target (pfile->deps, o, 1);
231 t = munge (t); /* Also makes permanent copy. */
233 if (d->ndeps == d->deps_size)
235 d->deps_size = d->deps_size * 2 + 8;
236 d->depv = (const char **)
237 xrealloc (d->depv, d->deps_size * sizeof (const char *));
239 d->depv[d->ndeps++] = t;
243 deps_write (d, fp, colmax)
244 const struct deps *d;
248 unsigned int size, i, column;
251 if (colmax && colmax < 34)
254 for (i = 0; i < d->ntargets; i++)
256 size = strlen (d->targetv[i]);
258 if (colmax && column > colmax)
260 fputs (" \\\n ", fp);
268 fputs (d->targetv[i], fp);
275 for (i = 0; i < d->ndeps; i++)
277 size = strlen (d->depv[i]);
279 if (colmax && column > colmax)
281 fputs (" \\\n ", fp);
289 fputs (d->depv[i], fp);
295 deps_phony_targets (d, fp)
296 const struct deps *d;
301 for (i = 1; i < d->ndeps; i++)
304 fputs (d->depv[i], fp);