]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/mkdeps.c
re PR pch/13675 (#including a precompiled header more than once in the same unit...
[thirdparty/gcc.git] / libcpp / mkdeps.c
CommitLineData
49e6c08e 1/* Dependency generator for Makefile fragments.
d482a073 2 Copyright (C) 2000, 2001, 2003, 2007, 2008 Free Software Foundation, Inc.
49e6c08e
ZW
3 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
7Free Software Foundation; either version 2, or (at your option) any
8later 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
16along with this program; if not, write to the Free Software
200031d1 17Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
49e6c08e
ZW
18
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
03b9ab42
NB
27/* 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;
c6e83800
ZW
38
39 const char **vpathv;
40 size_t *vpathlv;
41 unsigned int nvpaths;
42 unsigned int vpaths_size;
03b9ab42
NB
43};
44
0c20a65f 45static const char *munge (const char *);
49e6c08e 46
49e6c08e
ZW
47/* 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.) */
0c20a65f 53
49e6c08e 54static const char *
0c20a65f 55munge (const char *filename)
49e6c08e
ZW
56{
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. */
e23c0ba3 73 for (q = p - 1; filename <= q && *q == '\\'; q--)
49e6c08e
ZW
74 len++;
75 len++;
76 break;
77
78 case '$':
a5a4ce3c 79 /* '$' is quoted by doubling it. */
49e6c08e
ZW
80 len++;
81 break;
830465c6
MM
82
83 case '#':
84 /* '#' is quoted with a backslash. */
85 len++;
86 break;
49e6c08e
ZW
87 }
88 }
89
90 /* Now we know how big to make the buffer. */
c3f829c1 91 buffer = XNEWVEC (char, len + 1);
49e6c08e
ZW
92
93 for (p = filename, dst = buffer; *p; p++, dst++)
94 {
95 switch (*p)
96 {
97 case ' ':
98 case '\t':
e23c0ba3 99 for (q = p - 1; filename <= q && *q == '\\'; q--)
49e6c08e
ZW
100 *dst++ = '\\';
101 *dst++ = '\\';
102 break;
103
104 case '$':
105 *dst++ = '$';
106 break;
107
830465c6
MM
108 case '#':
109 *dst++ = '\\';
110 break;
111
49e6c08e
ZW
112 default:
113 /* nothing */;
114 }
115 *dst = *p;
116 }
117
118 *dst = '\0';
119 return buffer;
120}
121
c6e83800
ZW
122/* 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)
49e6c08e 126{
c6e83800
ZW
127 if (d->vpathv)
128 {
129 unsigned int i;
130 for (i = 0; i < d->nvpaths; i++)
131 {
132 if (!strncmp (d->vpathv[i], t, d->vpathlv[i]))
133 {
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 }
49e6c08e 150
c6e83800
ZW
151 /* Remove leading ./ in any case. */
152 while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
67e64439
TT
153 {
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 }
49e6c08e 160
c6e83800
ZW
161 return t;
162}
49e6c08e 163
c6e83800 164/* Public routines. */
49e6c08e 165
c6e83800
ZW
166struct deps *
167deps_init (void)
168{
c3f829c1 169 return XCNEW (struct deps);
49e6c08e
ZW
170}
171
172void
0c20a65f 173deps_free (struct deps *d)
49e6c08e
ZW
174{
175 unsigned int i;
05bccae2 176
f7114e17
NB
177 if (d->targetv)
178 {
179 for (i = 0; i < d->ntargets; i++)
fad205ff 180 free ((void *) d->targetv[i]);
f7114e17
NB
181 free (d->targetv);
182 }
05bccae2 183
f7114e17
NB
184 if (d->depv)
185 {
186 for (i = 0; i < d->ndeps; i++)
fad205ff 187 free ((void *) d->depv[i]);
f7114e17
NB
188 free (d->depv);
189 }
49e6c08e 190
c6e83800
ZW
191 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
49e6c08e
ZW
199 free (d);
200}
201
a5a4ce3c
NB
202/* 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. */
49e6c08e 204void
0c20a65f 205deps_add_target (struct deps *d, const char *t, int quote)
49e6c08e 206{
49e6c08e
ZW
207 if (d->ntargets == d->targets_size)
208 {
f7114e17 209 d->targets_size = d->targets_size * 2 + 4;
c3f829c1 210 d->targetv = XRESIZEVEC (const char *, d->targetv, d->targets_size);
49e6c08e 211 }
05bccae2 212
c6e83800 213 t = apply_vpath (d, t);
a5a4ce3c
NB
214 if (quote)
215 t = munge (t); /* Also makes permanent copy. */
216 else
217 t = xstrdup (t);
218
49e6c08e
ZW
219 d->targetv[d->ntargets++] = t;
220}
221
03b9ab42 222/* Sets the default target if none has been given already. An empty
a5a4ce3c
NB
223 string as the default target in interpreted as stdin. The string
224 is quoted for MAKE. */
49e6c08e 225void
0c20a65f 226deps_add_default_target (struct deps *d, const char *tgt)
49e6c08e 227{
03b9ab42
NB
228 /* Only if we have no targets. */
229 if (d->ntargets)
230 return;
49e6c08e 231
03b9ab42 232 if (tgt[0] == '\0')
a5a4ce3c 233 deps_add_target (d, "-", 1);
49e6c08e 234 else
03b9ab42 235 {
45936a85
DD
236#ifndef TARGET_OBJECT_SUFFIX
237# define TARGET_OBJECT_SUFFIX ".o"
03b9ab42 238#endif
0821bff7 239 const char *start = lbasename (tgt);
c3f829c1
GDR
240 char *o = (char *) alloca (strlen (start)
241 + strlen (TARGET_OBJECT_SUFFIX) + 1);
48ce6bbb 242 char *suffix;
03b9ab42 243
48ce6bbb 244 strcpy (o, start);
0c20a65f 245
48ce6bbb
NS
246 suffix = strrchr (o, '.');
247 if (!suffix)
248 suffix = o + strlen (o);
45936a85 249 strcpy (suffix, TARGET_OBJECT_SUFFIX);
0c20a65f 250
a5a4ce3c 251 deps_add_target (d, o, 1);
03b9ab42 252 }
49e6c08e
ZW
253}
254
255void
0c20a65f 256deps_add_dep (struct deps *d, const char *t)
49e6c08e 257{
c6e83800 258 t = munge (apply_vpath (d, t)); /* Also makes permanent copy. */
49e6c08e
ZW
259
260 if (d->ndeps == d->deps_size)
261 {
fa6f74f6 262 d->deps_size = d->deps_size * 2 + 8;
c3f829c1 263 d->depv = XRESIZEVEC (const char *, d->depv, d->deps_size);
49e6c08e
ZW
264 }
265 d->depv[d->ndeps++] = t;
266}
267
c6e83800
ZW
268void
269deps_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;
c3f829c1 279 copy = XNEWVEC (char, len + 1);
c6e83800
ZW
280 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;
c3f829c1
GDR
288 d->vpathv = XRESIZEVEC (const char *, d->vpathv, d->vpaths_size);
289 d->vpathlv = XRESIZEVEC (size_t, d->vpathlv, d->vpaths_size);
c6e83800
ZW
290 }
291 d->vpathv[d->nvpaths] = copy;
292 d->vpathlv[d->nvpaths] = len;
293 d->nvpaths++;
294 }
295}
296
49e6c08e 297void
0c20a65f 298deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
49e6c08e
ZW
299{
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;
49e6c08e
ZW
310 if (i)
311 {
d482a073
RW
312 if (colmax && column > colmax)
313 {
314 fputs (" \\\n ", fp);
315 column = 1 + size;
316 }
317 else
318 {
319 putc (' ', fp);
320 column++;
321 }
49e6c08e
ZW
322 }
323 fputs (d->targetv[i], fp);
324 }
325
326 putc (':', fp);
d482a073 327 column++;
49e6c08e
ZW
328
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 }
d482a073 338 else
49e6c08e
ZW
339 {
340 putc (' ', fp);
341 column++;
342 }
343 fputs (d->depv[i], fp);
344 }
345 putc ('\n', fp);
346}
0c20a65f 347
49e6c08e 348void
0c20a65f 349deps_phony_targets (const struct deps *d, FILE *fp)
49e6c08e 350{
05bccae2 351 unsigned int i;
49e6c08e
ZW
352
353 for (i = 1; i < d->ndeps; i++)
354 {
a5a4ce3c 355 putc ('\n', fp);
49e6c08e
ZW
356 fputs (d->depv[i], fp);
357 putc (':', fp);
358 putc ('\n', fp);
359 }
360}
17211ab5
GK
361
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
0c20a65f 367deps_save (struct deps *deps, FILE *f)
17211ab5
GK
368{
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
0c20a65f 396deps_restore (struct deps *deps, FILE *fd, const char *self)
17211ab5
GK
397{
398 unsigned int i, count;
399 size_t num_to_read;
400 size_t buf_size = 512;
c3f829c1 401 char *buf = XNEWVEC (char, buf_size);
17211ab5
GK
402
403 /* Number of dependences. */
404 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
405 return -1;
406
407 /* The length of each dependence string, followed by the string. */
408 for (i = 0; i < count; i++)
409 {
410 /* Read in # bytes in string. */
411 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
412 return -1;
413 if (buf_size < num_to_read + 1)
414 {
415 buf_size = num_to_read + 1 + 127;
c3f829c1 416 buf = XRESIZEVEC (char, buf, buf_size);
17211ab5
GK
417 }
418 if (fread (buf, 1, num_to_read, fd) != num_to_read)
419 return -1;
420 buf[num_to_read] = '\0';
421
0c20a65f 422 /* Generate makefile dependencies from .pch if -nopch-deps. */
17211ab5
GK
423 if (self != NULL && strcmp (buf, self) != 0)
424 deps_add_dep (deps, buf);
425 }
426
427 free (buf);
428 return 0;
429}