]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/mkdeps.c
directives.c (cpp_handle_deferred_pragma): Save, clear and restore cb.line_change.
[thirdparty/gcc.git] / libcpp / mkdeps.c
CommitLineData
49e6c08e 1/* Dependency generator for Makefile fragments.
0c20a65f 2 Copyright (C) 2000, 2001, 2003 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
17Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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;
82 }
83 }
84
85 /* Now we know how big to make the buffer. */
dd3b81b4 86 buffer = xmalloc (len + 1);
49e6c08e
ZW
87
88 for (p = filename, dst = buffer; *p; p++, dst++)
89 {
90 switch (*p)
91 {
92 case ' ':
93 case '\t':
e23c0ba3 94 for (q = p - 1; filename <= q && *q == '\\'; q--)
49e6c08e
ZW
95 *dst++ = '\\';
96 *dst++ = '\\';
97 break;
98
99 case '$':
100 *dst++ = '$';
101 break;
102
103 default:
104 /* nothing */;
105 }
106 *dst = *p;
107 }
108
109 *dst = '\0';
110 return buffer;
111}
112
c6e83800
ZW
113/* If T begins with any of the partial pathnames listed in d->vpathv,
114 then advance T to point beyond that pathname. */
115static const char *
116apply_vpath (struct deps *d, const char *t)
49e6c08e 117{
c6e83800
ZW
118 if (d->vpathv)
119 {
120 unsigned int i;
121 for (i = 0; i < d->nvpaths; i++)
122 {
123 if (!strncmp (d->vpathv[i], t, d->vpathlv[i]))
124 {
125 const char *p = t + d->vpathlv[i];
126 if (!IS_DIR_SEPARATOR (*p))
127 goto not_this_one;
128
129 /* Do not simplify $(vpath)/../whatever. ??? Might not
130 be necessary. */
131 if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
132 goto not_this_one;
133
134 /* found a match */
135 t = t + d->vpathlv[i] + 1;
136 break;
137 }
138 not_this_one:;
139 }
140 }
49e6c08e 141
c6e83800
ZW
142 /* Remove leading ./ in any case. */
143 while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
144 t += 2;
49e6c08e 145
c6e83800
ZW
146 return t;
147}
49e6c08e 148
c6e83800 149/* Public routines. */
49e6c08e 150
c6e83800
ZW
151struct deps *
152deps_init (void)
153{
154 return xcalloc (sizeof (struct deps), 1);
49e6c08e
ZW
155}
156
157void
0c20a65f 158deps_free (struct deps *d)
49e6c08e
ZW
159{
160 unsigned int i;
05bccae2 161
f7114e17
NB
162 if (d->targetv)
163 {
164 for (i = 0; i < d->ntargets; i++)
fad205ff 165 free ((void *) d->targetv[i]);
f7114e17
NB
166 free (d->targetv);
167 }
05bccae2 168
f7114e17
NB
169 if (d->depv)
170 {
171 for (i = 0; i < d->ndeps; i++)
fad205ff 172 free ((void *) d->depv[i]);
f7114e17
NB
173 free (d->depv);
174 }
49e6c08e 175
c6e83800
ZW
176 if (d->vpathv)
177 {
178 for (i = 0; i < d->nvpaths; i++)
179 free ((void *) d->vpathv[i]);
180 free (d->vpathv);
181 free (d->vpathlv);
182 }
183
49e6c08e
ZW
184 free (d);
185}
186
a5a4ce3c
NB
187/* Adds a target T. We make a copy, so it need not be a permanent
188 string. QUOTE is true if the string should be quoted. */
49e6c08e 189void
0c20a65f 190deps_add_target (struct deps *d, const char *t, int quote)
49e6c08e 191{
49e6c08e
ZW
192 if (d->ntargets == d->targets_size)
193 {
f7114e17 194 d->targets_size = d->targets_size * 2 + 4;
703ad42b 195 d->targetv = xrealloc (d->targetv,
49e6c08e
ZW
196 d->targets_size * sizeof (const char *));
197 }
05bccae2 198
c6e83800 199 t = apply_vpath (d, t);
a5a4ce3c
NB
200 if (quote)
201 t = munge (t); /* Also makes permanent copy. */
202 else
203 t = xstrdup (t);
204
49e6c08e
ZW
205 d->targetv[d->ntargets++] = t;
206}
207
03b9ab42 208/* Sets the default target if none has been given already. An empty
a5a4ce3c
NB
209 string as the default target in interpreted as stdin. The string
210 is quoted for MAKE. */
49e6c08e 211void
0c20a65f 212deps_add_default_target (struct deps *d, const char *tgt)
49e6c08e 213{
03b9ab42
NB
214 /* Only if we have no targets. */
215 if (d->ntargets)
216 return;
49e6c08e 217
03b9ab42 218 if (tgt[0] == '\0')
a5a4ce3c 219 deps_add_target (d, "-", 1);
49e6c08e 220 else
03b9ab42 221 {
45936a85
DD
222#ifndef TARGET_OBJECT_SUFFIX
223# define TARGET_OBJECT_SUFFIX ".o"
03b9ab42 224#endif
0821bff7 225 const char *start = lbasename (tgt);
703ad42b 226 char *o = alloca (strlen (start) + strlen (TARGET_OBJECT_SUFFIX) + 1);
48ce6bbb 227 char *suffix;
03b9ab42 228
48ce6bbb 229 strcpy (o, start);
0c20a65f 230
48ce6bbb
NS
231 suffix = strrchr (o, '.');
232 if (!suffix)
233 suffix = o + strlen (o);
45936a85 234 strcpy (suffix, TARGET_OBJECT_SUFFIX);
0c20a65f 235
a5a4ce3c 236 deps_add_target (d, o, 1);
03b9ab42 237 }
49e6c08e
ZW
238}
239
240void
0c20a65f 241deps_add_dep (struct deps *d, const char *t)
49e6c08e 242{
c6e83800 243 t = munge (apply_vpath (d, t)); /* Also makes permanent copy. */
49e6c08e
ZW
244
245 if (d->ndeps == d->deps_size)
246 {
fa6f74f6 247 d->deps_size = d->deps_size * 2 + 8;
703ad42b 248 d->depv = xrealloc (d->depv, d->deps_size * sizeof (const char *));
49e6c08e
ZW
249 }
250 d->depv[d->ndeps++] = t;
251}
252
c6e83800
ZW
253void
254deps_add_vpath (struct deps *d, const char *vpath)
255{
256 const char *elem, *p;
257 char *copy;
258 size_t len;
259
260 for (elem = vpath; *elem; elem = p)
261 {
262 for (p = elem; *p && *p != ':'; p++);
263 len = p - elem;
264 copy = xmalloc (len + 1);
265 memcpy (copy, elem, len);
266 copy[len] = '\0';
267 if (*p == ':')
268 p++;
269
270 if (d->nvpaths == d->vpaths_size)
271 {
272 d->vpaths_size = d->vpaths_size * 2 + 8;
273 d->vpathv = xrealloc (d->vpathv,
274 d->vpaths_size * sizeof (const char *));
275 d->vpathlv = xrealloc (d->vpathlv, d->vpaths_size * sizeof (size_t));
276 }
277 d->vpathv[d->nvpaths] = copy;
278 d->vpathlv[d->nvpaths] = len;
279 d->nvpaths++;
280 }
281}
282
49e6c08e 283void
0c20a65f 284deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
49e6c08e
ZW
285{
286 unsigned int size, i, column;
287
288 column = 0;
289 if (colmax && colmax < 34)
290 colmax = 34;
291
292 for (i = 0; i < d->ntargets; i++)
293 {
294 size = strlen (d->targetv[i]);
295 column += size;
296 if (colmax && column > colmax)
297 {
298 fputs (" \\\n ", fp);
299 column = 1 + size;
300 }
301 if (i)
302 {
303 putc (' ', fp);
304 column++;
305 }
306 fputs (d->targetv[i], fp);
307 }
308
309 putc (':', fp);
310 putc (' ', fp);
311 column += 2;
312
313 for (i = 0; i < d->ndeps; i++)
314 {
315 size = strlen (d->depv[i]);
316 column += size;
317 if (colmax && column > colmax)
318 {
319 fputs (" \\\n ", fp);
320 column = 1 + size;
321 }
322 if (i)
323 {
324 putc (' ', fp);
325 column++;
326 }
327 fputs (d->depv[i], fp);
328 }
329 putc ('\n', fp);
330}
0c20a65f 331
49e6c08e 332void
0c20a65f 333deps_phony_targets (const struct deps *d, FILE *fp)
49e6c08e 334{
05bccae2 335 unsigned int i;
49e6c08e
ZW
336
337 for (i = 1; i < d->ndeps; i++)
338 {
a5a4ce3c 339 putc ('\n', fp);
49e6c08e
ZW
340 fputs (d->depv[i], fp);
341 putc (':', fp);
342 putc ('\n', fp);
343 }
344}
17211ab5
GK
345
346/* Write out a deps buffer to a file, in a form that can be read back
347 with deps_restore. Returns nonzero on error, in which case the
348 error number will be in errno. */
349
350int
0c20a65f 351deps_save (struct deps *deps, FILE *f)
17211ab5
GK
352{
353 unsigned int i;
354
355 /* The cppreader structure contains makefile dependences. Write out this
356 structure. */
357
358 /* The number of dependences. */
359 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
360 return -1;
361 /* The length of each dependence followed by the string. */
362 for (i = 0; i < deps->ndeps; i++)
363 {
364 size_t num_to_write = strlen (deps->depv[i]);
365 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
366 return -1;
367 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
368 return -1;
369 }
370
371 return 0;
372}
373
374/* Read back dependency information written with deps_save into
375 the deps buffer. The third argument may be NULL, in which case
376 the dependency information is just skipped, or it may be a filename,
377 in which case that filename is skipped. */
378
379int
0c20a65f 380deps_restore (struct deps *deps, FILE *fd, const char *self)
17211ab5
GK
381{
382 unsigned int i, count;
383 size_t num_to_read;
384 size_t buf_size = 512;
703ad42b 385 char *buf = xmalloc (buf_size);
17211ab5
GK
386
387 /* Number of dependences. */
388 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
389 return -1;
390
391 /* The length of each dependence string, followed by the string. */
392 for (i = 0; i < count; i++)
393 {
394 /* Read in # bytes in string. */
395 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
396 return -1;
397 if (buf_size < num_to_read + 1)
398 {
399 buf_size = num_to_read + 1 + 127;
400 buf = xrealloc (buf, buf_size);
401 }
402 if (fread (buf, 1, num_to_read, fd) != num_to_read)
403 return -1;
404 buf[num_to_read] = '\0';
405
0c20a65f 406 /* Generate makefile dependencies from .pch if -nopch-deps. */
17211ab5
GK
407 if (self != NULL && strcmp (buf, self) != 0)
408 deps_add_dep (deps, buf);
409 }
410
411 free (buf);
412 return 0;
413}