1 /* Dependency generator for Makefile fragments.
2 Copyright (C) 2000, 2001 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! */
25 #include "coretypes.h"
29 /* Keep this structure local to this file, so clients don't find it
30 easy to start making assumptions. */
34 unsigned int ntargets
; /* number of slots actually occupied */
35 unsigned int targets_size
; /* amt of allocated space - in words */
39 unsigned int deps_size
;
42 static const char *munge
PARAMS ((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
59 for (p
= filename
, len
= 0; *p
; p
++, len
++)
65 /* GNU make uses a weird quoting scheme for white space.
66 A space or tab preceded by 2N+1 backslashes represents
67 N backslashes followed by space; a space or tab
68 preceded by 2N backslashes represents N backslashes at
69 the end of a file name; and backslashes in other
70 contexts should not be doubled. */
71 for (q
= p
- 1; filename
<= q
&& *q
== '\\'; q
--)
77 /* '$' is quoted by doubling it. */
83 /* Now we know how big to make the buffer. */
84 buffer
= xmalloc (len
+ 1);
86 for (p
= filename
, dst
= buffer
; *p
; p
++, dst
++)
92 for (q
= p
- 1; filename
<= q
&& *q
== '\\'; q
--)
111 /* Public routines. */
116 struct deps
*d
= (struct deps
*) xmalloc (sizeof (struct deps
));
118 /* Allocate space for the vectors only if we need it. */
139 for (i
= 0; i
< d
->ntargets
; i
++)
140 free ((PTR
) d
->targetv
[i
]);
146 for (i
= 0; i
< d
->ndeps
; i
++)
147 free ((PTR
) d
->depv
[i
]);
154 /* Adds a target T. We make a copy, so it need not be a permanent
155 string. QUOTE is true if the string should be quoted. */
157 deps_add_target (d
, t
, quote
)
162 if (d
->ntargets
== d
->targets_size
)
164 d
->targets_size
= d
->targets_size
* 2 + 4;
165 d
->targetv
= (const char **) xrealloc (d
->targetv
,
166 d
->targets_size
* sizeof (const char *));
170 t
= munge (t
); /* Also makes permanent copy. */
174 d
->targetv
[d
->ntargets
++] = t
;
177 /* Sets the default target if none has been given already. An empty
178 string as the default target in interpreted as stdin. The string
179 is quoted for MAKE. */
181 deps_add_default_target (d
, tgt
)
185 /* Only if we have no targets. */
190 deps_add_target (d
, "-", 1);
193 #ifndef TARGET_OBJECT_SUFFIX
194 # define TARGET_OBJECT_SUFFIX ".o"
196 const char *start
= lbasename (tgt
);
197 char *o
= (char *) alloca (strlen (start
) + strlen (TARGET_OBJECT_SUFFIX
) + 1);
202 suffix
= strrchr (o
, '.');
204 suffix
= o
+ strlen (o
);
205 strcpy (suffix
, TARGET_OBJECT_SUFFIX
);
207 deps_add_target (d
, o
, 1);
216 t
= munge (t
); /* Also makes permanent copy. */
218 if (d
->ndeps
== d
->deps_size
)
220 d
->deps_size
= d
->deps_size
* 2 + 8;
221 d
->depv
= (const char **)
222 xrealloc (d
->depv
, d
->deps_size
* sizeof (const char *));
224 d
->depv
[d
->ndeps
++] = t
;
228 deps_write (d
, fp
, colmax
)
229 const struct deps
*d
;
233 unsigned int size
, i
, column
;
236 if (colmax
&& colmax
< 34)
239 for (i
= 0; i
< d
->ntargets
; i
++)
241 size
= strlen (d
->targetv
[i
]);
243 if (colmax
&& column
> colmax
)
245 fputs (" \\\n ", fp
);
253 fputs (d
->targetv
[i
], fp
);
260 for (i
= 0; i
< d
->ndeps
; i
++)
262 size
= strlen (d
->depv
[i
]);
264 if (colmax
&& column
> colmax
)
266 fputs (" \\\n ", fp
);
274 fputs (d
->depv
[i
], fp
);
280 deps_phony_targets (d
, fp
)
281 const struct deps
*d
;
286 for (i
= 1; i
< d
->ndeps
; i
++)
289 fputs (d
->depv
[i
], fp
);