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! */
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
;
40 static const char *munge
PARAMS ((const char *));
42 /* Given a filename, quote characters in that filename which are
43 significant to Make. Note that it's not possible to quote all such
44 characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
45 not properly handled. It isn't possible to get this right in any
46 current version of Make. (??? Still true? Old comment referred to
57 for (p
= filename
, len
= 0; *p
; p
++, len
++)
63 /* GNU make uses a weird quoting scheme for white space.
64 A space or tab preceded by 2N+1 backslashes represents
65 N backslashes followed by space; a space or tab
66 preceded by 2N backslashes represents N backslashes at
67 the end of a file name; and backslashes in other
68 contexts should not be doubled. */
69 for (q
= p
- 1; filename
<= q
&& *q
== '\\'; q
--)
75 /* '$' is quoted by doubling it. */
81 /* Now we know how big to make the buffer. */
82 buffer
= xmalloc (len
+ 1);
84 for (p
= filename
, dst
= buffer
; *p
; p
++, dst
++)
90 for (q
= p
- 1; filename
<= q
&& *q
== '\\'; q
--)
109 /* Public routines. */
114 struct deps
*d
= (struct deps
*) xmalloc (sizeof (struct deps
));
116 /* Allocate space for the vectors only if we need it. */
137 for (i
= 0; i
< d
->ntargets
; i
++)
138 free ((PTR
) d
->targetv
[i
]);
144 for (i
= 0; i
< d
->ndeps
; i
++)
145 free ((PTR
) d
->depv
[i
]);
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. */
155 deps_add_target (d
, t
, quote
)
160 if (d
->ntargets
== d
->targets_size
)
162 d
->targets_size
= d
->targets_size
* 2 + 4;
163 d
->targetv
= (const char **) xrealloc (d
->targetv
,
164 d
->targets_size
* sizeof (const char *));
168 t
= munge (t
); /* Also makes permanent copy. */
172 d
->targetv
[d
->ntargets
++] = t
;
175 /* Sets the default target if none has been given already. An empty
176 string as the default target in interpreted as stdin. The string
177 is quoted for MAKE. */
179 deps_add_default_target (d
, tgt
)
183 /* Only if we have no targets. */
188 deps_add_target (d
, "-", 1);
191 #ifndef TARGET_OBJECT_SUFFIX
192 # define TARGET_OBJECT_SUFFIX ".o"
194 const char *start
= lbasename (tgt
);
195 char *o
= (char *) alloca (strlen (start
) + strlen (TARGET_OBJECT_SUFFIX
) + 1);
200 suffix
= strrchr (o
, '.');
202 suffix
= o
+ strlen (o
);
203 strcpy (suffix
, TARGET_OBJECT_SUFFIX
);
205 deps_add_target (d
, o
, 1);
214 t
= munge (t
); /* Also makes permanent copy. */
216 if (d
->ndeps
== d
->deps_size
)
218 d
->deps_size
= d
->deps_size
* 2 + 8;
219 d
->depv
= (const char **)
220 xrealloc (d
->depv
, d
->deps_size
* sizeof (const char *));
222 d
->depv
[d
->ndeps
++] = t
;
226 deps_write (d
, fp
, colmax
)
227 const struct deps
*d
;
231 unsigned int size
, i
, column
;
234 if (colmax
&& colmax
< 34)
237 for (i
= 0; i
< d
->ntargets
; i
++)
239 size
= strlen (d
->targetv
[i
]);
241 if (colmax
&& column
> colmax
)
243 fputs (" \\\n ", fp
);
251 fputs (d
->targetv
[i
], fp
);
258 for (i
= 0; i
< d
->ndeps
; i
++)
260 size
= strlen (d
->depv
[i
]);
262 if (colmax
&& column
> colmax
)
264 fputs (" \\\n ", fp
);
272 fputs (d
->depv
[i
], fp
);
278 deps_phony_targets (d
, fp
)
279 const struct deps
*d
;
284 for (i
= 1; i
< d
->ndeps
; i
++)
287 fputs (d
->depv
[i
], fp
);