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
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! */
27 /* Keep this structure local to this file, so clients don't find it
28 easy to start making assumptions. */
32 unsigned int ntargets
; /* number of slots actually occupied */
33 unsigned int targets_size
; /* amt of allocated space - in words */
37 unsigned int deps_size
;
42 unsigned int vpaths_size
;
45 static const char *munge (const char *);
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
55 munge (const char *filename
)
61 for (p
= filename
, len
= 0; *p
; p
++, len
++)
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. */
73 for (q
= p
- 1; filename
<= q
&& *q
== '\\'; q
--)
79 /* '$' is quoted by doubling it. */
85 /* Now we know how big to make the buffer. */
86 buffer
= xmalloc (len
+ 1);
88 for (p
= filename
, dst
= buffer
; *p
; p
++, dst
++)
94 for (q
= p
- 1; filename
<= q
&& *q
== '\\'; q
--)
113 /* If T begins with any of the partial pathnames listed in d->vpathv,
114 then advance T to point beyond that pathname. */
116 apply_vpath (struct deps
*d
, const char *t
)
121 for (i
= 0; i
< d
->nvpaths
; i
++)
123 if (!strncmp (d
->vpathv
[i
], t
, d
->vpathlv
[i
]))
125 const char *p
= t
+ d
->vpathlv
[i
];
126 if (!IS_DIR_SEPARATOR (*p
))
129 /* Do not simplify $(vpath)/../whatever. ??? Might not
131 if (p
[1] == '.' && p
[2] == '.' && IS_DIR_SEPARATOR (p
[3]))
135 t
= t
+ d
->vpathlv
[i
] + 1;
142 /* Remove leading ./ in any case. */
143 while (t
[0] == '.' && IS_DIR_SEPARATOR (t
[1]))
149 /* Public routines. */
154 return xcalloc (sizeof (struct deps
), 1);
158 deps_free (struct deps
*d
)
164 for (i
= 0; i
< d
->ntargets
; i
++)
165 free ((void *) d
->targetv
[i
]);
171 for (i
= 0; i
< d
->ndeps
; i
++)
172 free ((void *) d
->depv
[i
]);
178 for (i
= 0; i
< d
->nvpaths
; i
++)
179 free ((void *) d
->vpathv
[i
]);
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. */
190 deps_add_target (struct deps
*d
, const char *t
, int quote
)
192 if (d
->ntargets
== d
->targets_size
)
194 d
->targets_size
= d
->targets_size
* 2 + 4;
195 d
->targetv
= xrealloc (d
->targetv
,
196 d
->targets_size
* sizeof (const char *));
199 t
= apply_vpath (d
, t
);
201 t
= munge (t
); /* Also makes permanent copy. */
205 d
->targetv
[d
->ntargets
++] = t
;
208 /* Sets the default target if none has been given already. An empty
209 string as the default target in interpreted as stdin. The string
210 is quoted for MAKE. */
212 deps_add_default_target (struct deps
*d
, const char *tgt
)
214 /* Only if we have no targets. */
219 deps_add_target (d
, "-", 1);
222 #ifndef TARGET_OBJECT_SUFFIX
223 # define TARGET_OBJECT_SUFFIX ".o"
225 const char *start
= lbasename (tgt
);
226 char *o
= alloca (strlen (start
) + strlen (TARGET_OBJECT_SUFFIX
) + 1);
231 suffix
= strrchr (o
, '.');
233 suffix
= o
+ strlen (o
);
234 strcpy (suffix
, TARGET_OBJECT_SUFFIX
);
236 deps_add_target (d
, o
, 1);
241 deps_add_dep (struct deps
*d
, const char *t
)
243 t
= munge (apply_vpath (d
, t
)); /* Also makes permanent copy. */
245 if (d
->ndeps
== d
->deps_size
)
247 d
->deps_size
= d
->deps_size
* 2 + 8;
248 d
->depv
= xrealloc (d
->depv
, d
->deps_size
* sizeof (const char *));
250 d
->depv
[d
->ndeps
++] = t
;
254 deps_add_vpath (struct deps
*d
, const char *vpath
)
256 const char *elem
, *p
;
260 for (elem
= vpath
; *elem
; elem
= p
)
262 for (p
= elem
; *p
&& *p
!= ':'; p
++);
264 copy
= xmalloc (len
+ 1);
265 memcpy (copy
, elem
, len
);
270 if (d
->nvpaths
== d
->vpaths_size
)
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));
277 d
->vpathv
[d
->nvpaths
] = copy
;
278 d
->vpathlv
[d
->nvpaths
] = len
;
284 deps_write (const struct deps
*d
, FILE *fp
, unsigned int colmax
)
286 unsigned int size
, i
, column
;
289 if (colmax
&& colmax
< 34)
292 for (i
= 0; i
< d
->ntargets
; i
++)
294 size
= strlen (d
->targetv
[i
]);
296 if (colmax
&& column
> colmax
)
298 fputs (" \\\n ", fp
);
306 fputs (d
->targetv
[i
], fp
);
313 for (i
= 0; i
< d
->ndeps
; i
++)
315 size
= strlen (d
->depv
[i
]);
317 if (colmax
&& column
> colmax
)
319 fputs (" \\\n ", fp
);
327 fputs (d
->depv
[i
], fp
);
333 deps_phony_targets (const struct deps
*d
, FILE *fp
)
337 for (i
= 1; i
< d
->ndeps
; i
++)
340 fputs (d
->depv
[i
], fp
);
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. */
351 deps_save (struct deps
*deps
, FILE *f
)
355 /* The cppreader structure contains makefile dependences. Write out this
358 /* The number of dependences. */
359 if (fwrite (&deps
->ndeps
, sizeof (deps
->ndeps
), 1, f
) != 1)
361 /* The length of each dependence followed by the string. */
362 for (i
= 0; i
< deps
->ndeps
; i
++)
364 size_t num_to_write
= strlen (deps
->depv
[i
]);
365 if (fwrite (&num_to_write
, sizeof (size_t), 1, f
) != 1)
367 if (fwrite (deps
->depv
[i
], num_to_write
, 1, f
) != 1)
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. */
380 deps_restore (struct deps
*deps
, FILE *fd
, const char *self
)
382 unsigned int i
, count
;
384 size_t buf_size
= 512;
385 char *buf
= xmalloc (buf_size
);
387 /* Number of dependences. */
388 if (fread (&count
, 1, sizeof (count
), fd
) != sizeof (count
))
391 /* The length of each dependence string, followed by the string. */
392 for (i
= 0; i
< count
; i
++)
394 /* Read in # bytes in string. */
395 if (fread (&num_to_read
, 1, sizeof (size_t), fd
) != sizeof (size_t))
397 if (buf_size
< num_to_read
+ 1)
399 buf_size
= num_to_read
+ 1 + 127;
400 buf
= xrealloc (buf
, buf_size
);
402 if (fread (buf
, 1, num_to_read
, fd
) != num_to_read
)
404 buf
[num_to_read
] = '\0';
406 /* Generate makefile dependencies from .pch if -nopch-deps. */
407 if (self
!= NULL
&& strcmp (buf
, self
) != 0)
408 deps_add_dep (deps
, buf
);