* regclass.c (choose_hard_reg_mode): Add third argument.
[official-gcc.git] / gcc / mkdeps.c
blob71aab3d9fc55ee32bcf425a611f1a57d0d849a0d
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
8 later version.
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! */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "mkdeps.h"
29 /* Keep this structure local to this file, so clients don't find it
30 easy to start making assumptions. */
31 struct deps
33 const char **targetv;
34 unsigned int ntargets; /* number of slots actually occupied */
35 unsigned int targets_size; /* amt of allocated space - in words */
37 const char **depv;
38 unsigned int ndeps;
39 unsigned int deps_size;
42 static const char *munge (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
49 3.76.1.) */
51 static const char *
52 munge (const char *filename)
54 int len;
55 const char *p, *q;
56 char *dst, *buffer;
58 for (p = filename, len = 0; *p; p++, len++)
60 switch (*p)
62 case ' ':
63 case '\t':
64 /* GNU make uses a weird quoting scheme for white space.
65 A space or tab preceded by 2N+1 backslashes represents
66 N backslashes followed by space; a space or tab
67 preceded by 2N backslashes represents N backslashes at
68 the end of a file name; and backslashes in other
69 contexts should not be doubled. */
70 for (q = p - 1; filename <= q && *q == '\\'; q--)
71 len++;
72 len++;
73 break;
75 case '$':
76 /* '$' is quoted by doubling it. */
77 len++;
78 break;
82 /* Now we know how big to make the buffer. */
83 buffer = xmalloc (len + 1);
85 for (p = filename, dst = buffer; *p; p++, dst++)
87 switch (*p)
89 case ' ':
90 case '\t':
91 for (q = p - 1; filename <= q && *q == '\\'; q--)
92 *dst++ = '\\';
93 *dst++ = '\\';
94 break;
96 case '$':
97 *dst++ = '$';
98 break;
100 default:
101 /* nothing */;
103 *dst = *p;
106 *dst = '\0';
107 return buffer;
110 /* Public routines. */
112 struct deps *
113 deps_init (void)
115 struct deps *d = (struct deps *) xmalloc (sizeof (struct deps));
117 /* Allocate space for the vectors only if we need it. */
119 d->targetv = 0;
120 d->depv = 0;
122 d->ntargets = 0;
123 d->targets_size = 0;
124 d->ndeps = 0;
125 d->deps_size = 0;
127 return d;
130 void
131 deps_free (struct deps *d)
133 unsigned int i;
135 if (d->targetv)
137 for (i = 0; i < d->ntargets; i++)
138 free ((void *) d->targetv[i]);
139 free (d->targetv);
142 if (d->depv)
144 for (i = 0; i < d->ndeps; i++)
145 free ((void *) d->depv[i]);
146 free (d->depv);
149 free (d);
152 /* Adds a target T. We make a copy, so it need not be a permanent
153 string. QUOTE is true if the string should be quoted. */
154 void
155 deps_add_target (struct deps *d, const char *t, int quote)
157 if (d->ntargets == d->targets_size)
159 d->targets_size = d->targets_size * 2 + 4;
160 d->targetv = (const char **) xrealloc (d->targetv,
161 d->targets_size * sizeof (const char *));
164 if (quote)
165 t = munge (t); /* Also makes permanent copy. */
166 else
167 t = xstrdup (t);
169 d->targetv[d->ntargets++] = t;
172 /* Sets the default target if none has been given already. An empty
173 string as the default target in interpreted as stdin. The string
174 is quoted for MAKE. */
175 void
176 deps_add_default_target (struct deps *d, const char *tgt)
178 /* Only if we have no targets. */
179 if (d->ntargets)
180 return;
182 if (tgt[0] == '\0')
183 deps_add_target (d, "-", 1);
184 else
186 #ifndef TARGET_OBJECT_SUFFIX
187 # define TARGET_OBJECT_SUFFIX ".o"
188 #endif
189 const char *start = lbasename (tgt);
190 char *o = (char *) alloca (strlen (start) + strlen (TARGET_OBJECT_SUFFIX) + 1);
191 char *suffix;
193 strcpy (o, start);
195 suffix = strrchr (o, '.');
196 if (!suffix)
197 suffix = o + strlen (o);
198 strcpy (suffix, TARGET_OBJECT_SUFFIX);
200 deps_add_target (d, o, 1);
204 void
205 deps_add_dep (struct deps *d, const char *t)
207 t = munge (t); /* Also makes permanent copy. */
209 if (d->ndeps == d->deps_size)
211 d->deps_size = d->deps_size * 2 + 8;
212 d->depv = (const char **)
213 xrealloc (d->depv, d->deps_size * sizeof (const char *));
215 d->depv[d->ndeps++] = t;
218 void
219 deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
221 unsigned int size, i, column;
223 column = 0;
224 if (colmax && colmax < 34)
225 colmax = 34;
227 for (i = 0; i < d->ntargets; i++)
229 size = strlen (d->targetv[i]);
230 column += size;
231 if (colmax && column > colmax)
233 fputs (" \\\n ", fp);
234 column = 1 + size;
236 if (i)
238 putc (' ', fp);
239 column++;
241 fputs (d->targetv[i], fp);
244 putc (':', fp);
245 putc (' ', fp);
246 column += 2;
248 for (i = 0; i < d->ndeps; i++)
250 size = strlen (d->depv[i]);
251 column += size;
252 if (colmax && column > colmax)
254 fputs (" \\\n ", fp);
255 column = 1 + size;
257 if (i)
259 putc (' ', fp);
260 column++;
262 fputs (d->depv[i], fp);
264 putc ('\n', fp);
267 void
268 deps_phony_targets (const struct deps *d, FILE *fp)
270 unsigned int i;
272 for (i = 1; i < d->ndeps; i++)
274 putc ('\n', fp);
275 fputs (d->depv[i], fp);
276 putc (':', fp);
277 putc ('\n', fp);
281 /* Write out a deps buffer to a file, in a form that can be read back
282 with deps_restore. Returns nonzero on error, in which case the
283 error number will be in errno. */
286 deps_save (struct deps *deps, FILE *f)
288 unsigned int i;
290 /* The cppreader structure contains makefile dependences. Write out this
291 structure. */
293 /* The number of dependences. */
294 if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
295 return -1;
296 /* The length of each dependence followed by the string. */
297 for (i = 0; i < deps->ndeps; i++)
299 size_t num_to_write = strlen (deps->depv[i]);
300 if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
301 return -1;
302 if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
303 return -1;
306 return 0;
309 /* Read back dependency information written with deps_save into
310 the deps buffer. The third argument may be NULL, in which case
311 the dependency information is just skipped, or it may be a filename,
312 in which case that filename is skipped. */
315 deps_restore (struct deps *deps, FILE *fd, const char *self)
317 unsigned int i, count;
318 size_t num_to_read;
319 size_t buf_size = 512;
320 char *buf = (char *) xmalloc (buf_size);
322 /* Number of dependences. */
323 if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
324 return -1;
326 /* The length of each dependence string, followed by the string. */
327 for (i = 0; i < count; i++)
329 /* Read in # bytes in string. */
330 if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
331 return -1;
332 if (buf_size < num_to_read + 1)
334 buf_size = num_to_read + 1 + 127;
335 buf = xrealloc (buf, buf_size);
337 if (fread (buf, 1, num_to_read, fd) != num_to_read)
338 return -1;
339 buf[num_to_read] = '\0';
341 /* Generate makefile dependencies from .pch if -nopch-deps. */
342 if (self != NULL && strcmp (buf, self) != 0)
343 deps_add_dep (deps, buf);
346 free (buf);
347 return 0;