blob: 89156cde14d4a22fa98fc9a42c0bfe9a910a4998 [file] [log] [blame]
Zack Weinberg49e6c082000-03-04 19:42:04 +00001/* Dependency generator for Makefile fragments.
Richard Sandiford500f3ed2013-01-14 18:13:59 +00002 Copyright (C) 2000-2013 Free Software Foundation, Inc.
Zack Weinberg49e6c082000-03-04 19:42:04 +00003 Contributed by Zack Weinberg, Mar 2000
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +02007Free Software Foundation; either version 3, or (at your option) any
Zack Weinberg49e6c082000-03-04 19:42:04 +00008later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020016along with this program; see the file COPYING3. If not see
17<http://www.gnu.org/licenses/>.
Zack Weinberg49e6c082000-03-04 19:42:04 +000018
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! */
22
23#include "config.h"
24#include "system.h"
25#include "mkdeps.h"
26
Neil Booth03b9ab42001-01-04 10:25:55 +000027/* Keep this structure local to this file, so clients don't find it
28 easy to start making assumptions. */
29struct deps
30{
31 const char **targetv;
32 unsigned int ntargets; /* number of slots actually occupied */
33 unsigned int targets_size; /* amt of allocated space - in words */
34
35 const char **depv;
36 unsigned int ndeps;
37 unsigned int deps_size;
Zack Weinbergc6e83802004-06-05 20:58:06 +000038
39 const char **vpathv;
40 size_t *vpathlv;
41 unsigned int nvpaths;
42 unsigned int vpaths_size;
Neil Booth03b9ab42001-01-04 10:25:55 +000043};
44
Andreas Jaeger0c20a652003-07-06 11:56:09 +020045static const char *munge (const char *);
Zack Weinberg49e6c082000-03-04 19:42:04 +000046
Zack Weinberg49e6c082000-03-04 19:42:04 +000047/* Given a filename, quote characters in that filename which are
48 significant to Make. Note that it's not possible to quote all such
49 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
50 not properly handled. It isn't possible to get this right in any
51 current version of Make. (??? Still true? Old comment referred to
52 3.76.1.) */
Andreas Jaeger0c20a652003-07-06 11:56:09 +020053
Zack Weinberg49e6c082000-03-04 19:42:04 +000054static const char *
Andreas Jaeger0c20a652003-07-06 11:56:09 +020055munge (const char *filename)
Zack Weinberg49e6c082000-03-04 19:42:04 +000056{
57 int len;
58 const char *p, *q;
59 char *dst, *buffer;
60
61 for (p = filename, len = 0; *p; p++, len++)
62 {
63 switch (*p)
64 {
65 case ' ':
66 case '\t':
67 /* GNU make uses a weird quoting scheme for white space.
68 A space or tab preceded by 2N+1 backslashes represents
69 N backslashes followed by space; a space or tab
70 preceded by 2N backslashes represents N backslashes at
71 the end of a file name; and backslashes in other
72 contexts should not be doubled. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +000073 for (q = p - 1; filename <= q && *q == '\\'; q--)
Zack Weinberg49e6c082000-03-04 19:42:04 +000074 len++;
75 len++;
76 break;
77
78 case '$':
Neil Bootha5a4ce32001-01-05 07:50:24 +000079 /* '$' is quoted by doubling it. */
Zack Weinberg49e6c082000-03-04 19:42:04 +000080 len++;
81 break;
Markus Milleder830465c2008-03-06 19:08:40 +010082
83 case '#':
84 /* '#' is quoted with a backslash. */
85 len++;
86 break;
Zack Weinberg49e6c082000-03-04 19:42:04 +000087 }
88 }
89
90 /* Now we know how big to make the buffer. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000091 buffer = XNEWVEC (char, len + 1);
Zack Weinberg49e6c082000-03-04 19:42:04 +000092
93 for (p = filename, dst = buffer; *p; p++, dst++)
94 {
95 switch (*p)
96 {
97 case ' ':
98 case '\t':
Zack Weinberge23c0ba2000-03-07 23:11:06 +000099 for (q = p - 1; filename <= q && *q == '\\'; q--)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000100 *dst++ = '\\';
101 *dst++ = '\\';
102 break;
103
104 case '$':
105 *dst++ = '$';
106 break;
107
Markus Milleder830465c2008-03-06 19:08:40 +0100108 case '#':
109 *dst++ = '\\';
110 break;
111
Zack Weinberg49e6c082000-03-04 19:42:04 +0000112 default:
113 /* nothing */;
114 }
115 *dst = *p;
116 }
117
118 *dst = '\0';
119 return buffer;
120}
121
Zack Weinbergc6e83802004-06-05 20:58:06 +0000122/* If T begins with any of the partial pathnames listed in d->vpathv,
123 then advance T to point beyond that pathname. */
124static const char *
125apply_vpath (struct deps *d, const char *t)
126{
127 if (d->vpathv)
128 {
129 unsigned int i;
130 for (i = 0; i < d->nvpaths; i++)
131 {
Kai Tietz44898002011-03-25 20:11:26 +0100132 if (!filename_ncmp (d->vpathv[i], t, d->vpathlv[i]))
Zack Weinbergc6e83802004-06-05 20:58:06 +0000133 {
134 const char *p = t + d->vpathlv[i];
135 if (!IS_DIR_SEPARATOR (*p))
136 goto not_this_one;
137
138 /* Do not simplify $(vpath)/../whatever. ??? Might not
139 be necessary. */
140 if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
141 goto not_this_one;
142
143 /* found a match */
144 t = t + d->vpathlv[i] + 1;
145 break;
146 }
147 not_this_one:;
148 }
149 }
150
151 /* Remove leading ./ in any case. */
152 while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
Tom Tromey67e64432007-01-30 15:50:00 +0000153 {
154 t += 2;
155 /* If we removed a leading ./, then also remove any /s after the
156 first. */
157 while (IS_DIR_SEPARATOR (t[0]))
158 ++t;
159 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000160
161 return t;
162}
163
Zack Weinberg49e6c082000-03-04 19:42:04 +0000164/* Public routines. */
165
166struct deps *
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200167deps_init (void)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000168{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000169 return XCNEW (struct deps);
Zack Weinberg49e6c082000-03-04 19:42:04 +0000170}
171
172void
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200173deps_free (struct deps *d)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000174{
175 unsigned int i;
Richard Kenner05bccae2000-03-07 11:41:32 +0000176
Neil Boothf7114e12001-01-06 00:15:29 +0000177 if (d->targetv)
178 {
179 for (i = 0; i < d->ntargets; i++)
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +0000180 free ((void *) d->targetv[i]);
Neil Boothf7114e12001-01-06 00:15:29 +0000181 free (d->targetv);
182 }
Richard Kenner05bccae2000-03-07 11:41:32 +0000183
Neil Boothf7114e12001-01-06 00:15:29 +0000184 if (d->depv)
185 {
186 for (i = 0; i < d->ndeps; i++)
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +0000187 free ((void *) d->depv[i]);
Neil Boothf7114e12001-01-06 00:15:29 +0000188 free (d->depv);
189 }
Zack Weinberg49e6c082000-03-04 19:42:04 +0000190
Zack Weinbergc6e83802004-06-05 20:58:06 +0000191 if (d->vpathv)
192 {
193 for (i = 0; i < d->nvpaths; i++)
194 free ((void *) d->vpathv[i]);
195 free (d->vpathv);
196 free (d->vpathlv);
197 }
198
Zack Weinberg49e6c082000-03-04 19:42:04 +0000199 free (d);
200}
201
Neil Bootha5a4ce32001-01-05 07:50:24 +0000202/* Adds a target T. We make a copy, so it need not be a permanent
203 string. QUOTE is true if the string should be quoted. */
Zack Weinberg49e6c082000-03-04 19:42:04 +0000204void
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200205deps_add_target (struct deps *d, const char *t, int quote)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000206{
Zack Weinberg49e6c082000-03-04 19:42:04 +0000207 if (d->ntargets == d->targets_size)
208 {
Neil Boothf7114e12001-01-06 00:15:29 +0000209 d->targets_size = d->targets_size * 2 + 4;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000210 d->targetv = XRESIZEVEC (const char *, d->targetv, d->targets_size);
Zack Weinberg49e6c082000-03-04 19:42:04 +0000211 }
Richard Kenner05bccae2000-03-07 11:41:32 +0000212
Zack Weinbergc6e83802004-06-05 20:58:06 +0000213 t = apply_vpath (d, t);
Neil Bootha5a4ce32001-01-05 07:50:24 +0000214 if (quote)
215 t = munge (t); /* Also makes permanent copy. */
216 else
217 t = xstrdup (t);
218
Zack Weinberg49e6c082000-03-04 19:42:04 +0000219 d->targetv[d->ntargets++] = t;
220}
221
Neil Booth03b9ab42001-01-04 10:25:55 +0000222/* Sets the default target if none has been given already. An empty
Neil Bootha5a4ce32001-01-05 07:50:24 +0000223 string as the default target in interpreted as stdin. The string
224 is quoted for MAKE. */
Zack Weinberg49e6c082000-03-04 19:42:04 +0000225void
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200226deps_add_default_target (struct deps *d, const char *tgt)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000227{
Neil Booth03b9ab42001-01-04 10:25:55 +0000228 /* Only if we have no targets. */
229 if (d->ntargets)
230 return;
Zack Weinberg49e6c082000-03-04 19:42:04 +0000231
Neil Booth03b9ab42001-01-04 10:25:55 +0000232 if (tgt[0] == '\0')
Neil Bootha5a4ce32001-01-05 07:50:24 +0000233 deps_add_target (d, "-", 1);
Zack Weinberg49e6c082000-03-04 19:42:04 +0000234 else
Neil Booth03b9ab42001-01-04 10:25:55 +0000235 {
DJ Delorie45936a82001-04-19 16:28:05 -0400236#ifndef TARGET_OBJECT_SUFFIX
237# define TARGET_OBJECT_SUFFIX ".o"
Neil Booth03b9ab42001-01-04 10:25:55 +0000238#endif
Andrew Cagney0821bff2001-08-03 15:42:25 +0000239 const char *start = lbasename (tgt);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000240 char *o = (char *) alloca (strlen (start)
241 + strlen (TARGET_OBJECT_SUFFIX) + 1);
Nathan Sidwell48ce6bb2001-02-12 14:06:22 +0000242 char *suffix;
Neil Booth03b9ab42001-01-04 10:25:55 +0000243
Nathan Sidwell48ce6bb2001-02-12 14:06:22 +0000244 strcpy (o, start);
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200245
Nathan Sidwell48ce6bb2001-02-12 14:06:22 +0000246 suffix = strrchr (o, '.');
247 if (!suffix)
248 suffix = o + strlen (o);
DJ Delorie45936a82001-04-19 16:28:05 -0400249 strcpy (suffix, TARGET_OBJECT_SUFFIX);
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200250
Neil Bootha5a4ce32001-01-05 07:50:24 +0000251 deps_add_target (d, o, 1);
Neil Booth03b9ab42001-01-04 10:25:55 +0000252 }
Zack Weinberg49e6c082000-03-04 19:42:04 +0000253}
254
255void
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200256deps_add_dep (struct deps *d, const char *t)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000257{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000258 t = munge (apply_vpath (d, t)); /* Also makes permanent copy. */
Zack Weinberg49e6c082000-03-04 19:42:04 +0000259
260 if (d->ndeps == d->deps_size)
261 {
Neil Boothfa6f74f2001-01-06 11:08:49 +0000262 d->deps_size = d->deps_size * 2 + 8;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000263 d->depv = XRESIZEVEC (const char *, d->depv, d->deps_size);
Zack Weinberg49e6c082000-03-04 19:42:04 +0000264 }
265 d->depv[d->ndeps++] = t;
266}
267
268void
Zack Weinbergc6e83802004-06-05 20:58:06 +0000269deps_add_vpath (struct deps *d, const char *vpath)
270{
271 const char *elem, *p;
272 char *copy;
273 size_t len;
274
275 for (elem = vpath; *elem; elem = p)
276 {
277 for (p = elem; *p && *p != ':'; p++);
278 len = p - elem;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000279 copy = XNEWVEC (char, len + 1);
Zack Weinbergc6e83802004-06-05 20:58:06 +0000280 memcpy (copy, elem, len);
281 copy[len] = '\0';
282 if (*p == ':')
283 p++;
284
285 if (d->nvpaths == d->vpaths_size)
286 {
287 d->vpaths_size = d->vpaths_size * 2 + 8;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000288 d->vpathv = XRESIZEVEC (const char *, d->vpathv, d->vpaths_size);
289 d->vpathlv = XRESIZEVEC (size_t, d->vpathlv, d->vpaths_size);
Zack Weinbergc6e83802004-06-05 20:58:06 +0000290 }
291 d->vpathv[d->nvpaths] = copy;
292 d->vpathlv[d->nvpaths] = len;
293 d->nvpaths++;
294 }
295}
296
297void
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200298deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000299{
300 unsigned int size, i, column;
301
302 column = 0;
303 if (colmax && colmax < 34)
304 colmax = 34;
305
306 for (i = 0; i < d->ntargets; i++)
307 {
308 size = strlen (d->targetv[i]);
309 column += size;
Zack Weinberg49e6c082000-03-04 19:42:04 +0000310 if (i)
311 {
Ralf Wildenhuesd482a072008-02-27 21:42:23 +0000312 if (colmax && column > colmax)
313 {
314 fputs (" \\\n ", fp);
315 column = 1 + size;
316 }
317 else
318 {
319 putc (' ', fp);
320 column++;
321 }
Zack Weinberg49e6c082000-03-04 19:42:04 +0000322 }
323 fputs (d->targetv[i], fp);
324 }
325
326 putc (':', fp);
Ralf Wildenhuesd482a072008-02-27 21:42:23 +0000327 column++;
Zack Weinberg49e6c082000-03-04 19:42:04 +0000328
329 for (i = 0; i < d->ndeps; i++)
330 {
331 size = strlen (d->depv[i]);
332 column += size;
333 if (colmax && column > colmax)
334 {
335 fputs (" \\\n ", fp);
336 column = 1 + size;
337 }
Ralf Wildenhuesd482a072008-02-27 21:42:23 +0000338 else
Zack Weinberg49e6c082000-03-04 19:42:04 +0000339 {
340 putc (' ', fp);
341 column++;
342 }
343 fputs (d->depv[i], fp);
344 }
345 putc ('\n', fp);
346}
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200347
Zack Weinberg49e6c082000-03-04 19:42:04 +0000348void
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200349deps_phony_targets (const struct deps *d, FILE *fp)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000350{
Richard Kenner05bccae2000-03-07 11:41:32 +0000351 unsigned int i;
Zack Weinberg49e6c082000-03-04 19:42:04 +0000352
353 for (i = 1; i < d->ndeps; i++)
354 {
Neil Bootha5a4ce32001-01-05 07:50:24 +0000355 putc ('\n', fp);
Zack Weinberg49e6c082000-03-04 19:42:04 +0000356 fputs (d->depv[i], fp);
357 putc (':', fp);
358 putc ('\n', fp);
359 }
360}
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000361
362/* Write out a deps buffer to a file, in a form that can be read back
363 with deps_restore. Returns nonzero on error, in which case the
364 error number will be in errno. */
365
366int
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200367deps_save (struct deps *deps, FILE *f)
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000368{
369 unsigned int i;
370
371 /* The cppreader structure contains makefile dependences. Write out this
372 structure. */
373
374 /* The number of dependences. */
375 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
376 return -1;
377 /* The length of each dependence followed by the string. */
378 for (i = 0; i < deps->ndeps; i++)
379 {
380 size_t num_to_write = strlen (deps->depv[i]);
381 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
382 return -1;
383 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
384 return -1;
385 }
386
387 return 0;
388}
389
390/* Read back dependency information written with deps_save into
391 the deps buffer. The third argument may be NULL, in which case
392 the dependency information is just skipped, or it may be a filename,
393 in which case that filename is skipped. */
394
395int
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200396deps_restore (struct deps *deps, FILE *fd, const char *self)
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000397{
398 unsigned int i, count;
399 size_t num_to_read;
400 size_t buf_size = 512;
Tobias Burnus55e7f902012-10-15 22:08:57 +0200401 char *buf;
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000402
403 /* Number of dependences. */
404 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
405 return -1;
406
Tobias Burnus55e7f902012-10-15 22:08:57 +0200407 buf = XNEWVEC (char, buf_size);
408
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000409 /* The length of each dependence string, followed by the string. */
410 for (i = 0; i < count; i++)
411 {
412 /* Read in # bytes in string. */
413 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
Tobias Burnus55e7f902012-10-15 22:08:57 +0200414 {
415 free (buf);
416 return -1;
417 }
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000418 if (buf_size < num_to_read + 1)
419 {
420 buf_size = num_to_read + 1 + 127;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000421 buf = XRESIZEVEC (char, buf, buf_size);
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000422 }
423 if (fread (buf, 1, num_to_read, fd) != num_to_read)
Tobias Burnus55e7f902012-10-15 22:08:57 +0200424 {
425 free (buf);
426 return -1;
427 }
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000428 buf[num_to_read] = '\0';
429
Andreas Jaeger0c20a652003-07-06 11:56:09 +0200430 /* Generate makefile dependencies from .pch if -nopch-deps. */
Kai Tietz44898002011-03-25 20:11:26 +0100431 if (self != NULL && filename_cmp (buf, self) != 0)
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000432 deps_add_dep (deps, buf);
433 }
434
435 free (buf);
436 return 0;
437}