1 | /* Command processing for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
---|
4 | 2010 Free Software Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
10 | version.
|
---|
11 |
|
---|
12 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License along with
|
---|
17 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "make.h"
|
---|
20 | #include "dep.h"
|
---|
21 | #include "filedef.h"
|
---|
22 | #include "variable.h"
|
---|
23 | #include "job.h"
|
---|
24 | #include "commands.h"
|
---|
25 | #ifdef WINDOWS32
|
---|
26 | #include <windows.h>
|
---|
27 | #include "w32err.h"
|
---|
28 | #endif
|
---|
29 | #ifdef CONFIG_WITH_LAZY_DEPS_VARS
|
---|
30 | # include <assert.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #if VMS
|
---|
34 | # define FILE_LIST_SEPARATOR ','
|
---|
35 | #else
|
---|
36 | # define FILE_LIST_SEPARATOR ' '
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | int remote_kill (int id, int sig);
|
---|
40 |
|
---|
41 | #ifndef HAVE_UNISTD_H
|
---|
42 | int getpid ();
|
---|
43 | #endif
|
---|
44 | |
---|
45 |
|
---|
46 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
47 |
|
---|
48 | static unsigned long
|
---|
49 | dep_hash_1 (const void *key)
|
---|
50 | {
|
---|
51 | const struct dep *d = key;
|
---|
52 | return_STRING_HASH_1 (dep_name (d));
|
---|
53 | }
|
---|
54 |
|
---|
55 | static unsigned long
|
---|
56 | dep_hash_2 (const void *key)
|
---|
57 | {
|
---|
58 | const struct dep *d = key;
|
---|
59 | return_STRING_HASH_2 (dep_name (d));
|
---|
60 | }
|
---|
61 |
|
---|
62 | static int
|
---|
63 | dep_hash_cmp (const void *x, const void *y)
|
---|
64 | {
|
---|
65 | const struct dep *dx = x;
|
---|
66 | const struct dep *dy = y;
|
---|
67 | return strcmp (dep_name (dx), dep_name (dy));
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | #else /* CONFIG_WITH_STRCACHE2 */
|
---|
72 |
|
---|
73 | /* Exploit the fact that all names are in the string cache. This means equal
|
---|
74 | names shall have the same storage and there is no need for hashing or
|
---|
75 | comparing. Use the address as the first hash, avoiding any touching of
|
---|
76 | the name, and the length as the second. */
|
---|
77 |
|
---|
78 | static unsigned long
|
---|
79 | dep_hash_1 (const void *key)
|
---|
80 | {
|
---|
81 | const char *name = dep_name ((struct dep const *) key);
|
---|
82 | assert (strcache2_is_cached (&file_strcache, name));
|
---|
83 | return (size_t) name / sizeof(void *);
|
---|
84 | }
|
---|
85 |
|
---|
86 | static unsigned long
|
---|
87 | dep_hash_2 (const void *key)
|
---|
88 | {
|
---|
89 | const char *name = dep_name ((struct dep const *) key);
|
---|
90 | return strcache2_get_len (&file_strcache, name);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static int
|
---|
94 | dep_hash_cmp (const void *x, const void *y)
|
---|
95 | {
|
---|
96 | struct dep *dx = (struct dep *) x;
|
---|
97 | struct dep *dy = (struct dep *) y;
|
---|
98 | const char *dxname = dep_name (dx);
|
---|
99 | const char *dyname = dep_name (dy);
|
---|
100 | int cmp = dxname == dyname ? 0 : 1;
|
---|
101 |
|
---|
102 | /* check preconds: both cached and the cache contains no duplicates. */
|
---|
103 | assert (strcache2_is_cached (&file_strcache, dxname));
|
---|
104 | assert (strcache2_is_cached (&file_strcache, dyname));
|
---|
105 | assert (cmp == 0 || strcmp (dxname, dyname) != 0);
|
---|
106 |
|
---|
107 | /* If the names are the same but ignore_mtimes are not equal, one of these
|
---|
108 | is an order-only prerequisite and one isn't. That means that we should
|
---|
109 | remove the one that isn't and keep the one that is. */
|
---|
110 |
|
---|
111 | if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
|
---|
112 | dx->ignore_mtime = dy->ignore_mtime = 0;
|
---|
113 |
|
---|
114 | return cmp;
|
---|
115 | }
|
---|
116 |
|
---|
117 | #endif /* CONFIG_WITH_STRCACHE2 */
|
---|
118 |
|
---|
119 | #ifdef CONFIG_WITH_LAZY_DEPS_VARS
|
---|
120 | /* Create as copy of DEPS without duplicates, similar to what
|
---|
121 | set_file_variables does. Used by func_deps. */
|
---|
122 |
|
---|
123 | struct dep *create_uniqute_deps_chain (struct dep *deps)
|
---|
124 | {
|
---|
125 | struct dep *d;
|
---|
126 | struct dep *head = NULL;
|
---|
127 | struct dep **ppnext= &head;
|
---|
128 | struct hash_table dep_hash;
|
---|
129 | void **slot;
|
---|
130 |
|
---|
131 | hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
|
---|
132 |
|
---|
133 | for (d = deps; d != 0; d = d->next)
|
---|
134 | {
|
---|
135 | if (d->need_2nd_expansion)
|
---|
136 | continue;
|
---|
137 |
|
---|
138 | slot = hash_find_slot (&dep_hash, d);
|
---|
139 | if (HASH_VACANT (*slot))
|
---|
140 | {
|
---|
141 | struct dep *n = alloc_dep();
|
---|
142 | *n = *d;
|
---|
143 | n->next = NULL;
|
---|
144 | *ppnext = n;
|
---|
145 | ppnext = &n->next;
|
---|
146 | hash_insert_at (&dep_hash, n, slot);
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | /* Upgrade order only if a normal dep exists.
|
---|
151 | Note! Elected not to upgrade the original, only the sanitized
|
---|
152 | list, need to check that out later. FIXME TODO */
|
---|
153 | struct dep *d2 = (struct dep *)*slot;
|
---|
154 | if (d->ignore_mtime != d2->ignore_mtime)
|
---|
155 | d->ignore_mtime = d2->ignore_mtime = 0;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | return head;
|
---|
160 | }
|
---|
161 | #endif /* CONFIG_WITH_LAZY_DEPS_VARS */
|
---|
162 |
|
---|
163 | /* Set FILE's automatic variables up. */
|
---|
164 |
|
---|
165 | void
|
---|
166 | #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
|
---|
167 | set_file_variables (struct file *file, int called_early)
|
---|
168 | #else
|
---|
169 | set_file_variables (struct file *file)
|
---|
170 | #endif
|
---|
171 | {
|
---|
172 | struct dep *d;
|
---|
173 | const char *at, *percent, *star, *less;
|
---|
174 | #ifdef CONFIG_WITH_STRCACHE2
|
---|
175 | const char *org_stem = file->stem;
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | #ifndef NO_ARCHIVES
|
---|
179 | /* If the target is an archive member `lib(member)',
|
---|
180 | then $@ is `lib' and $% is `member'. */
|
---|
181 |
|
---|
182 | if (ar_name (file->name))
|
---|
183 | {
|
---|
184 | unsigned int len;
|
---|
185 | const char *cp;
|
---|
186 | char *p;
|
---|
187 |
|
---|
188 | cp = strchr (file->name, '(');
|
---|
189 | p = alloca (cp - file->name + 1);
|
---|
190 | memcpy (p, file->name, cp - file->name);
|
---|
191 | p[cp - file->name] = '\0';
|
---|
192 | at = p;
|
---|
193 | len = strlen (cp + 1);
|
---|
194 | p = alloca (len);
|
---|
195 | memcpy (p, cp + 1, len - 1);
|
---|
196 | p[len - 1] = '\0';
|
---|
197 | percent = p;
|
---|
198 | }
|
---|
199 | else
|
---|
200 | #endif /* NO_ARCHIVES. */
|
---|
201 | {
|
---|
202 | at = file->name;
|
---|
203 | percent = "";
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* $* is the stem from an implicit or static pattern rule. */
|
---|
207 | if (file->stem == 0)
|
---|
208 | {
|
---|
209 | /* In Unix make, $* is set to the target name with
|
---|
210 | any suffix in the .SUFFIXES list stripped off for
|
---|
211 | explicit rules. We store this in the `stem' member. */
|
---|
212 | const char *name;
|
---|
213 | unsigned int len;
|
---|
214 |
|
---|
215 | #ifndef NO_ARCHIVES
|
---|
216 | if (ar_name (file->name))
|
---|
217 | {
|
---|
218 | name = strchr (file->name, '(') + 1;
|
---|
219 | len = strlen (name) - 1;
|
---|
220 | }
|
---|
221 | else
|
---|
222 | #endif
|
---|
223 | {
|
---|
224 | name = file->name;
|
---|
225 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
226 | len = strlen (name);
|
---|
227 | #else
|
---|
228 | len = strcache2_get_len (&file_strcache, name);
|
---|
229 | #endif
|
---|
230 | }
|
---|
231 |
|
---|
232 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
233 | for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
|
---|
234 | {
|
---|
235 | unsigned int slen = strlen (dep_name (d));
|
---|
236 | #else
|
---|
237 | for (d = enter_file (suffixes_strcached)->deps; d ; d = d->next)
|
---|
238 | {
|
---|
239 | unsigned int slen = strcache2_get_len (&file_strcache, dep_name (d));
|
---|
240 | #endif
|
---|
241 | if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
|
---|
242 | {
|
---|
243 | file->stem = strcache_add_len (name, len - slen);
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | if (d == 0)
|
---|
248 | file->stem = "";
|
---|
249 | }
|
---|
250 | star = file->stem;
|
---|
251 |
|
---|
252 | /* $< is the first not order-only dependency. */
|
---|
253 | less = "";
|
---|
254 | for (d = file->deps; d != 0; d = d->next)
|
---|
255 | if (!d->ignore_mtime)
|
---|
256 | {
|
---|
257 | if (!d->need_2nd_expansion)
|
---|
258 | less = dep_name (d);
|
---|
259 | break;
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (file->cmds == default_file->cmds)
|
---|
263 | /* This file got its commands from .DEFAULT.
|
---|
264 | In this case $< is the same as $@. */
|
---|
265 | less = at;
|
---|
266 |
|
---|
267 | #define DEFINE_VARIABLE(name, len, value) \
|
---|
268 | (void) define_variable_for_file (name,len,value,o_automatic,0,file)
|
---|
269 |
|
---|
270 | /* Define the variables. */
|
---|
271 |
|
---|
272 | #ifndef CONFIG_WITH_RDONLY_VARIABLE_VALUE
|
---|
273 | DEFINE_VARIABLE ("<", 1, less);
|
---|
274 | DEFINE_VARIABLE ("*", 1, star);
|
---|
275 | DEFINE_VARIABLE ("@", 1, at);
|
---|
276 | DEFINE_VARIABLE ("%", 1, percent);
|
---|
277 | #else /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
|
---|
278 | # define DEFINE_VARIABLE_RO_VAL(name, len, value, value_len) \
|
---|
279 | define_variable_in_set((name), (len), (value), (value_len), -1, \
|
---|
280 | (o_automatic), 0, (file)->variables->set, NILF)
|
---|
281 |
|
---|
282 | if (*less == '\0')
|
---|
283 | DEFINE_VARIABLE_RO_VAL ("<", 1, "", 0);
|
---|
284 | else if (less != at || at == file->name)
|
---|
285 | DEFINE_VARIABLE_RO_VAL ("<", 1, less, strcache_get_len (less));
|
---|
286 | else
|
---|
287 | DEFINE_VARIABLE ("<", 1, less);
|
---|
288 |
|
---|
289 | if (*star == '\0')
|
---|
290 | DEFINE_VARIABLE_RO_VAL ("*", 1, "", 0);
|
---|
291 | else if (file->stem != org_stem)
|
---|
292 | DEFINE_VARIABLE_RO_VAL ("*", 1, star, strcache_get_len (star));
|
---|
293 | else
|
---|
294 | DEFINE_VARIABLE ("*", 1, star);
|
---|
295 |
|
---|
296 | if (at == file->name)
|
---|
297 | DEFINE_VARIABLE_RO_VAL ("@", 1, at, strcache_get_len (at));
|
---|
298 | else
|
---|
299 | DEFINE_VARIABLE ("@", 1, at);
|
---|
300 |
|
---|
301 | if (*percent == '\0')
|
---|
302 | DEFINE_VARIABLE_RO_VAL ("%", 1, "", 0);
|
---|
303 | else
|
---|
304 | DEFINE_VARIABLE ("%", 1, percent);
|
---|
305 | #endif /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
|
---|
306 |
|
---|
307 | #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
|
---|
308 | /* The $^, $+, $? and $| variables should not be set if we're called
|
---|
309 | early by a .MUST_MAKE invocation or $(commands ). */
|
---|
310 | if (called_early)
|
---|
311 | return;
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | /* Compute the values for $^, $+, $?, and $|. */
|
---|
315 | #ifdef CONFIG_WITH_LAZY_DEPS_VARS
|
---|
316 | /* Lazy doesn't work for double colon rules with multiple files with
|
---|
317 | commands, nor for files that has been thru rehash_file() (vpath). */
|
---|
318 | if ( ( file->double_colon
|
---|
319 | && ( file->double_colon != file
|
---|
320 | || file->last != file))
|
---|
321 | || file->name != file->hname) /* XXX: Rehashed files should be fixable! */
|
---|
322 | #endif
|
---|
323 | {
|
---|
324 | static char *plus_value=0, *bar_value=0, *qmark_value=0;
|
---|
325 | static unsigned int plus_max=0, bar_max=0, qmark_max=0;
|
---|
326 |
|
---|
327 | unsigned int qmark_len, plus_len, bar_len;
|
---|
328 | char *cp;
|
---|
329 | char *caret_value;
|
---|
330 | char *qp;
|
---|
331 | char *bp;
|
---|
332 | unsigned int len;
|
---|
333 |
|
---|
334 | struct hash_table dep_hash;
|
---|
335 | void **slot;
|
---|
336 |
|
---|
337 | /* Compute first the value for $+, which is supposed to contain
|
---|
338 | duplicate dependencies as they were listed in the makefile. */
|
---|
339 |
|
---|
340 | plus_len = 0;
|
---|
341 | bar_len = 0;
|
---|
342 | for (d = file->deps; d != 0; d = d->next)
|
---|
343 | {
|
---|
344 | if (!d->need_2nd_expansion)
|
---|
345 | {
|
---|
346 | if (d->ignore_mtime)
|
---|
347 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
348 | bar_len += strlen (dep_name (d)) + 1;
|
---|
349 | #else
|
---|
350 | bar_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
|
---|
351 | #endif
|
---|
352 | else
|
---|
353 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
354 | plus_len += strlen (dep_name (d)) + 1;
|
---|
355 | #else
|
---|
356 | plus_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
|
---|
357 | #endif
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | if (bar_len == 0)
|
---|
362 | bar_len++;
|
---|
363 | if (plus_len == 0)
|
---|
364 | plus_len++;
|
---|
365 |
|
---|
366 | if (plus_len > plus_max)
|
---|
367 | plus_value = xrealloc (plus_value, plus_max = plus_len);
|
---|
368 |
|
---|
369 | cp = plus_value;
|
---|
370 |
|
---|
371 | qmark_len = plus_len + 1; /* Will be this or less. */
|
---|
372 | for (d = file->deps; d != 0; d = d->next)
|
---|
373 | if (! d->ignore_mtime && ! d->need_2nd_expansion)
|
---|
374 | {
|
---|
375 | const char *c = dep_name (d);
|
---|
376 |
|
---|
377 | #ifndef NO_ARCHIVES
|
---|
378 | if (ar_name (c))
|
---|
379 | {
|
---|
380 | c = strchr (c, '(') + 1;
|
---|
381 | len = strlen (c) - 1;
|
---|
382 | }
|
---|
383 | else
|
---|
384 | #endif
|
---|
385 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
386 | len = strlen (c);
|
---|
387 | #else
|
---|
388 | len = strcache2_get_len (&file_strcache, c);
|
---|
389 | #endif
|
---|
390 |
|
---|
391 | memcpy (cp, c, len);
|
---|
392 | cp += len;
|
---|
393 | *cp++ = FILE_LIST_SEPARATOR;
|
---|
394 | if (! (d->changed || always_make_flag))
|
---|
395 | qmark_len -= len + 1; /* Don't space in $? for this one. */
|
---|
396 | }
|
---|
397 |
|
---|
398 | /* Kill the last space and define the variable. */
|
---|
399 |
|
---|
400 | cp[cp > plus_value ? -1 : 0] = '\0';
|
---|
401 | DEFINE_VARIABLE ("+", 1, plus_value);
|
---|
402 |
|
---|
403 | /* Compute the values for $^, $?, and $|. */
|
---|
404 |
|
---|
405 | cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
|
---|
406 |
|
---|
407 | if (qmark_len > qmark_max)
|
---|
408 | qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
|
---|
409 | qp = qmark_value;
|
---|
410 |
|
---|
411 | if (bar_len > bar_max)
|
---|
412 | bar_value = xrealloc (bar_value, bar_max = bar_len);
|
---|
413 | bp = bar_value;
|
---|
414 |
|
---|
415 | /* Make sure that no dependencies are repeated in $^, $?, and $|. It
|
---|
416 | would be natural to combine the next two loops but we can't do it
|
---|
417 | because of a situation where we have two dep entries, the first
|
---|
418 | is order-only and the second is normal (see below). */
|
---|
419 |
|
---|
420 | hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
|
---|
421 |
|
---|
422 | for (d = file->deps; d != 0; d = d->next)
|
---|
423 | {
|
---|
424 | if (d->need_2nd_expansion)
|
---|
425 | continue;
|
---|
426 |
|
---|
427 | slot = hash_find_slot (&dep_hash, d);
|
---|
428 | if (HASH_VACANT (*slot))
|
---|
429 | hash_insert_at (&dep_hash, d, slot);
|
---|
430 | else
|
---|
431 | {
|
---|
432 | /* Check if the two prerequisites have different ignore_mtime.
|
---|
433 | If so then we need to "upgrade" one that is order-only. */
|
---|
434 |
|
---|
435 | struct dep* hd = (struct dep*) *slot;
|
---|
436 |
|
---|
437 | if (d->ignore_mtime != hd->ignore_mtime)
|
---|
438 | d->ignore_mtime = hd->ignore_mtime = 0;
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | for (d = file->deps; d != 0; d = d->next)
|
---|
443 | {
|
---|
444 | const char *c;
|
---|
445 |
|
---|
446 | if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
|
---|
447 | continue;
|
---|
448 |
|
---|
449 | c = dep_name (d);
|
---|
450 | #ifndef NO_ARCHIVES
|
---|
451 | if (ar_name (c))
|
---|
452 | {
|
---|
453 | c = strchr (c, '(') + 1;
|
---|
454 | len = strlen (c) - 1;
|
---|
455 | }
|
---|
456 | else
|
---|
457 | #endif
|
---|
458 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
459 | len = strlen (c);
|
---|
460 | #else
|
---|
461 | len = strcache2_get_len (&file_strcache, c);
|
---|
462 | #endif
|
---|
463 |
|
---|
464 | if (d->ignore_mtime)
|
---|
465 | {
|
---|
466 | memcpy (bp, c, len);
|
---|
467 | bp += len;
|
---|
468 | *bp++ = FILE_LIST_SEPARATOR;
|
---|
469 | }
|
---|
470 | else
|
---|
471 | {
|
---|
472 | memcpy (cp, c, len);
|
---|
473 | cp += len;
|
---|
474 | *cp++ = FILE_LIST_SEPARATOR;
|
---|
475 | if (d->changed || always_make_flag)
|
---|
476 | {
|
---|
477 | memcpy (qp, c, len);
|
---|
478 | qp += len;
|
---|
479 | *qp++ = FILE_LIST_SEPARATOR;
|
---|
480 | }
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | hash_free (&dep_hash, 0);
|
---|
485 |
|
---|
486 | /* Kill the last spaces and define the variables. */
|
---|
487 |
|
---|
488 | cp[cp > caret_value ? -1 : 0] = '\0';
|
---|
489 | DEFINE_VARIABLE ("^", 1, caret_value);
|
---|
490 |
|
---|
491 | qp[qp > qmark_value ? -1 : 0] = '\0';
|
---|
492 | DEFINE_VARIABLE ("?", 1, qmark_value);
|
---|
493 |
|
---|
494 | bp[bp > bar_value ? -1 : 0] = '\0';
|
---|
495 | DEFINE_VARIABLE ("|", 1, bar_value);
|
---|
496 | }
|
---|
497 | #undef DEFINE_VARIABLE
|
---|
498 | }
|
---|
499 | |
---|
500 |
|
---|
501 | /* Chop CMDS up into individual command lines if necessary.
|
---|
502 | Also set the `lines_flags' and `any_recurse' members. */
|
---|
503 |
|
---|
504 | void
|
---|
505 | chop_commands (struct commands *cmds)
|
---|
506 | {
|
---|
507 | unsigned int nlines, idx;
|
---|
508 | char **lines;
|
---|
509 |
|
---|
510 | /* If we don't have any commands,
|
---|
511 | or we already parsed them, never mind. */
|
---|
512 |
|
---|
513 | if (!cmds || cmds->command_lines != 0)
|
---|
514 | return;
|
---|
515 |
|
---|
516 | /* Chop CMDS->commands up into lines in CMDS->command_lines. */
|
---|
517 |
|
---|
518 | if (one_shell)
|
---|
519 | {
|
---|
520 | int l = strlen (cmds->commands);
|
---|
521 |
|
---|
522 | nlines = 1;
|
---|
523 | lines = xmalloc (nlines * sizeof (char *));
|
---|
524 | lines[0] = xstrdup (cmds->commands);
|
---|
525 |
|
---|
526 | /* Strip the trailing newline. */
|
---|
527 | if (l > 0 && lines[0][l-1] == '\n')
|
---|
528 | lines[0][l-1] = '\0';
|
---|
529 | }
|
---|
530 | else
|
---|
531 | {
|
---|
532 | const char *p;
|
---|
533 |
|
---|
534 | nlines = 5;
|
---|
535 | lines = xmalloc (nlines * sizeof (char *));
|
---|
536 | idx = 0;
|
---|
537 | p = cmds->commands;
|
---|
538 | while (*p != '\0')
|
---|
539 | {
|
---|
540 | const char *end = p;
|
---|
541 | find_end:;
|
---|
542 | end = strchr (end, '\n');
|
---|
543 | if (end == 0)
|
---|
544 | end = p + strlen (p);
|
---|
545 | else if (end > p && end[-1] == '\\')
|
---|
546 | {
|
---|
547 | int backslash = 1;
|
---|
548 | const char *b;
|
---|
549 | for (b = end - 2; b >= p && *b == '\\'; --b)
|
---|
550 | backslash = !backslash;
|
---|
551 | if (backslash)
|
---|
552 | {
|
---|
553 | ++end;
|
---|
554 | goto find_end;
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (idx == nlines)
|
---|
559 | {
|
---|
560 | nlines += 2;
|
---|
561 | lines = xrealloc (lines, nlines * sizeof (char *));
|
---|
562 | }
|
---|
563 | lines[idx++] = xstrndup (p, end - p);
|
---|
564 | p = end;
|
---|
565 | if (*p != '\0')
|
---|
566 | ++p;
|
---|
567 | }
|
---|
568 |
|
---|
569 | if (idx != nlines)
|
---|
570 | {
|
---|
571 | nlines = idx;
|
---|
572 | lines = xrealloc (lines, nlines * sizeof (char *));
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | /* Finally, set the corresponding CMDS->lines_flags elements and the
|
---|
577 | CMDS->any_recurse flag. */
|
---|
578 |
|
---|
579 | cmds->ncommand_lines = nlines;
|
---|
580 | cmds->command_lines = lines;
|
---|
581 |
|
---|
582 | cmds->any_recurse = 0;
|
---|
583 | #ifndef CONFIG_WITH_COMMANDS_FUNC
|
---|
584 | cmds->lines_flags = xmalloc (nlines);
|
---|
585 | #else
|
---|
586 | cmds->lines_flags = xmalloc (nlines * sizeof (cmds->lines_flags[0]));
|
---|
587 | #endif
|
---|
588 |
|
---|
589 | for (idx = 0; idx < nlines; ++idx)
|
---|
590 | {
|
---|
591 | int flags = 0;
|
---|
592 | const char *p = lines[idx];
|
---|
593 |
|
---|
594 | while (isblank (*p) || *p == '-' || *p == '@' || *p == '+' IF_WITH_COMMANDS_FUNC(|| *p == '%'))
|
---|
595 | switch (*(p++))
|
---|
596 | {
|
---|
597 | case '+':
|
---|
598 | flags |= COMMANDS_RECURSE;
|
---|
599 | break;
|
---|
600 | case '@':
|
---|
601 | flags |= COMMANDS_SILENT;
|
---|
602 | break;
|
---|
603 | case '-':
|
---|
604 | flags |= COMMANDS_NOERROR;
|
---|
605 | break;
|
---|
606 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
607 | case '%':
|
---|
608 | flags |= COMMAND_GETTER_SKIP_IT;
|
---|
609 | break;
|
---|
610 | #endif
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* If no explicit '+' was given, look for MAKE variable references. */
|
---|
614 | if (!(flags & COMMANDS_RECURSE)
|
---|
615 | #ifndef KMK
|
---|
616 | && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
|
---|
617 | #else
|
---|
618 | && (strstr (p, "$(KMK)") != 0 || strstr (p, "${KMK}") != 0 ||
|
---|
619 | strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
|
---|
620 | #endif
|
---|
621 | flags |= COMMANDS_RECURSE;
|
---|
622 |
|
---|
623 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
624 | /* check if kmk builtin command */
|
---|
625 | if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
---|
626 | flags |= COMMANDS_KMK_BUILTIN;
|
---|
627 | #endif
|
---|
628 |
|
---|
629 | cmds->lines_flags[idx] = flags;
|
---|
630 | cmds->any_recurse |= flags & COMMANDS_RECURSE;
|
---|
631 | }
|
---|
632 | }
|
---|
633 | |
---|
634 |
|
---|
635 | #ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
|
---|
636 | /* This is for saving memory in func_commands. */
|
---|
637 | void
|
---|
638 | free_chopped_commands (struct commands *cmds)
|
---|
639 | {
|
---|
640 | if ( cmds
|
---|
641 | && cmds->command_lines != 0
|
---|
642 | && cmds->refs == 0)
|
---|
643 | {
|
---|
644 | unsigned idx = cmds->ncommand_lines;
|
---|
645 | while (idx-- > 0)
|
---|
646 | free (cmds->command_lines[idx]);
|
---|
647 | free (cmds->command_lines);
|
---|
648 | free (cmds->lines_flags);
|
---|
649 | cmds->command_lines = 0;
|
---|
650 | cmds->lines_flags = 0;
|
---|
651 | cmds->ncommand_lines = 0;
|
---|
652 | }
|
---|
653 | }
|
---|
654 | |
---|
655 |
|
---|
656 | #endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
|
---|
657 | /* Execute the commands to remake FILE. If they are currently executing,
|
---|
658 | return or have already finished executing, just return. Otherwise,
|
---|
659 | fork off a child process to run the first command line in the sequence. */
|
---|
660 |
|
---|
661 | void
|
---|
662 | execute_file_commands (struct file *file)
|
---|
663 | {
|
---|
664 | const char *p;
|
---|
665 |
|
---|
666 | /* Don't go through all the preparations if
|
---|
667 | the commands are nothing but whitespace. */
|
---|
668 |
|
---|
669 | for (p = file->cmds->commands; *p != '\0'; ++p)
|
---|
670 | if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
|
---|
671 | break;
|
---|
672 | if (*p == '\0')
|
---|
673 | {
|
---|
674 | /* If there are no commands, assume everything worked. */
|
---|
675 | #ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
---|
676 | file->command_flags |= COMMANDS_NO_COMMANDS;
|
---|
677 | #endif
|
---|
678 | set_command_state (file, cs_running);
|
---|
679 | file->update_status = 0;
|
---|
680 | notice_finished_file (file);
|
---|
681 | return;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* First set the automatic variables according to this file. */
|
---|
685 |
|
---|
686 | initialize_file_variables (file, 0);
|
---|
687 |
|
---|
688 | #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
|
---|
689 | set_file_variables (file, 0 /* final call */);
|
---|
690 | #else
|
---|
691 | set_file_variables (file);
|
---|
692 | #endif
|
---|
693 |
|
---|
694 | /* Start the commands running. */
|
---|
695 | new_job (file);
|
---|
696 | }
|
---|
697 | |
---|
698 |
|
---|
699 | /* This is set while we are inside fatal_error_signal,
|
---|
700 | so things can avoid nonreentrant operations. */
|
---|
701 |
|
---|
702 | int handling_fatal_signal = 0;
|
---|
703 |
|
---|
704 | /* Handle fatal signals. */
|
---|
705 |
|
---|
706 | RETSIGTYPE
|
---|
707 | fatal_error_signal (int sig)
|
---|
708 | {
|
---|
709 | #ifdef __MSDOS__
|
---|
710 | extern int dos_status, dos_command_running;
|
---|
711 |
|
---|
712 | if (dos_command_running)
|
---|
713 | {
|
---|
714 | /* That was the child who got the signal, not us. */
|
---|
715 | dos_status |= (sig << 8);
|
---|
716 | return;
|
---|
717 | }
|
---|
718 | remove_intermediates (1);
|
---|
719 | exit (EXIT_FAILURE);
|
---|
720 | #else /* not __MSDOS__ */
|
---|
721 | #ifdef _AMIGA
|
---|
722 | remove_intermediates (1);
|
---|
723 | if (sig == SIGINT)
|
---|
724 | fputs (_("*** Break.\n"), stderr);
|
---|
725 |
|
---|
726 | exit (10);
|
---|
727 | #else /* not Amiga */
|
---|
728 | #if defined (WINDOWS32) && !defined (CONFIG_NEW_WIN32_CTRL_EVENT)
|
---|
729 | extern HANDLE main_thread;
|
---|
730 |
|
---|
731 | /* Windows creates a sperate thread for handling Ctrl+C, so we need
|
---|
732 | to suspend the main thread, or else we will have race conditions
|
---|
733 | when both threads call reap_children. */
|
---|
734 | if (main_thread)
|
---|
735 | {
|
---|
736 | DWORD susp_count = SuspendThread (main_thread);
|
---|
737 |
|
---|
738 | if (susp_count != 0)
|
---|
739 | fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
|
---|
740 | else if (susp_count == (DWORD)-1)
|
---|
741 | {
|
---|
742 | DWORD ierr = GetLastError ();
|
---|
743 |
|
---|
744 | fprintf (stderr, "SuspendThread: error %ld: %s\n",
|
---|
745 | ierr, map_windows32_error_to_string (ierr));
|
---|
746 | }
|
---|
747 | }
|
---|
748 | #endif
|
---|
749 | handling_fatal_signal = 1;
|
---|
750 |
|
---|
751 | /* Set the handling for this signal to the default.
|
---|
752 | It is blocked now while we run this handler. */
|
---|
753 | signal (sig, SIG_DFL);
|
---|
754 |
|
---|
755 | /* A termination signal won't be sent to the entire
|
---|
756 | process group, but it means we want to kill the children. */
|
---|
757 |
|
---|
758 | if (sig == SIGTERM)
|
---|
759 | {
|
---|
760 | struct child *c;
|
---|
761 | for (c = children; c != 0; c = c->next)
|
---|
762 | if (!c->remote)
|
---|
763 | (void) kill (c->pid, SIGTERM);
|
---|
764 | }
|
---|
765 |
|
---|
766 | /* If we got a signal that means the user
|
---|
767 | wanted to kill make, remove pending targets. */
|
---|
768 |
|
---|
769 | if (sig == SIGTERM || sig == SIGINT
|
---|
770 | #ifdef SIGHUP
|
---|
771 | || sig == SIGHUP
|
---|
772 | #endif
|
---|
773 | #ifdef SIGQUIT
|
---|
774 | || sig == SIGQUIT
|
---|
775 | #endif
|
---|
776 | )
|
---|
777 | {
|
---|
778 | struct child *c;
|
---|
779 |
|
---|
780 | /* Remote children won't automatically get signals sent
|
---|
781 | to the process group, so we must send them. */
|
---|
782 | for (c = children; c != 0; c = c->next)
|
---|
783 | if (c->remote)
|
---|
784 | (void) remote_kill (c->pid, sig);
|
---|
785 |
|
---|
786 | for (c = children; c != 0; c = c->next)
|
---|
787 | delete_child_targets (c);
|
---|
788 |
|
---|
789 | /* Clean up the children. We don't just use the call below because
|
---|
790 | we don't want to print the "Waiting for children" message. */
|
---|
791 | while (job_slots_used > 0)
|
---|
792 | reap_children (1, 0);
|
---|
793 | }
|
---|
794 | else
|
---|
795 | /* Wait for our children to die. */
|
---|
796 | while (job_slots_used > 0)
|
---|
797 | reap_children (1, 1);
|
---|
798 |
|
---|
799 | /* Delete any non-precious intermediate files that were made. */
|
---|
800 |
|
---|
801 | remove_intermediates (1);
|
---|
802 | #ifdef SIGQUIT
|
---|
803 | if (sig == SIGQUIT)
|
---|
804 | /* We don't want to send ourselves SIGQUIT, because it will
|
---|
805 | cause a core dump. Just exit instead. */
|
---|
806 | exit (EXIT_FAILURE);
|
---|
807 | #endif
|
---|
808 |
|
---|
809 | #ifdef WINDOWS32
|
---|
810 | # ifndef CONFIG_NEW_WIN32_CTRL_EVENT
|
---|
811 | if (main_thread)
|
---|
812 | CloseHandle (main_thread);
|
---|
813 | # endif /* !CONFIG_NEW_WIN32_CTRL_EVENT */
|
---|
814 | /* Cannot call W32_kill with a pid (it needs a handle). The exit
|
---|
815 | status of 130 emulates what happens in Bash. */
|
---|
816 | exit (130);
|
---|
817 | #else
|
---|
818 | /* Signal the same code; this time it will really be fatal. The signal
|
---|
819 | will be unblocked when we return and arrive then to kill us. */
|
---|
820 | if (kill (getpid (), sig) < 0)
|
---|
821 | pfatal_with_name ("kill");
|
---|
822 | #endif /* not WINDOWS32 */
|
---|
823 | #endif /* not Amiga */
|
---|
824 | #endif /* not __MSDOS__ */
|
---|
825 | }
|
---|
826 | |
---|
827 |
|
---|
828 | /* Delete FILE unless it's precious or not actually a file (phony),
|
---|
829 | and it has changed on disk since we last stat'd it. */
|
---|
830 |
|
---|
831 | static void
|
---|
832 | delete_target (struct file *file, const char *on_behalf_of)
|
---|
833 | {
|
---|
834 | struct stat st;
|
---|
835 | int e;
|
---|
836 |
|
---|
837 | if (file->precious || file->phony)
|
---|
838 | return;
|
---|
839 | #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
|
---|
840 | assert (!file->multi_maybe);
|
---|
841 | #endif
|
---|
842 |
|
---|
843 | #ifndef NO_ARCHIVES
|
---|
844 | if (ar_name (file->name))
|
---|
845 | {
|
---|
846 | time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
|
---|
847 | ? (time_t) -1
|
---|
848 | : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
|
---|
849 | if (ar_member_date (file->name) != file_date)
|
---|
850 | {
|
---|
851 | if (on_behalf_of)
|
---|
852 | error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
|
---|
853 | on_behalf_of, file->name);
|
---|
854 | else
|
---|
855 | error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
|
---|
856 | file->name);
|
---|
857 | }
|
---|
858 | return;
|
---|
859 | }
|
---|
860 | #endif
|
---|
861 |
|
---|
862 | EINTRLOOP (e, stat (file->name, &st));
|
---|
863 | if (e == 0
|
---|
864 | && S_ISREG (st.st_mode)
|
---|
865 | && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
|
---|
866 | {
|
---|
867 | if (on_behalf_of)
|
---|
868 | error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
|
---|
869 | else
|
---|
870 | error (NILF, _("*** Deleting file `%s'"), file->name);
|
---|
871 | if (unlink (file->name) < 0
|
---|
872 | && errno != ENOENT) /* It disappeared; so what. */
|
---|
873 | perror_with_name ("unlink: ", file->name);
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | /* Delete all non-precious targets of CHILD unless they were already deleted.
|
---|
879 | Set the flag in CHILD to say they've been deleted. */
|
---|
880 |
|
---|
881 | void
|
---|
882 | delete_child_targets (struct child *child)
|
---|
883 | {
|
---|
884 | struct dep *d;
|
---|
885 |
|
---|
886 | if (child->deleted)
|
---|
887 | return;
|
---|
888 |
|
---|
889 | /* Delete the target file if it changed. */
|
---|
890 | delete_target (child->file, NULL);
|
---|
891 |
|
---|
892 | /* Also remove any non-precious targets listed in the `also_make' member. */
|
---|
893 | for (d = child->file->also_make; d != 0; d = d->next)
|
---|
894 | delete_target (d->file, child->file->name);
|
---|
895 |
|
---|
896 | #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
|
---|
897 | /* Also remove any multi target siblings, except for the 'maybe' ones (we
|
---|
898 | handle that here) and precious ones (delete_target deals with that).
|
---|
899 | Note that CHILD is always the multi target head (see remake.c). */
|
---|
900 | if (child->file == child->file->multi_head)
|
---|
901 | {
|
---|
902 | struct file *f2;
|
---|
903 | for (f2 = child->file->multi_next; f2; f2 = f2->multi_next)
|
---|
904 | if (!f2->multi_maybe)
|
---|
905 | delete_target (f2, child->file->name);
|
---|
906 | }
|
---|
907 | #endif
|
---|
908 |
|
---|
909 | child->deleted = 1;
|
---|
910 | }
|
---|
911 | |
---|
912 |
|
---|
913 | /* Print out the commands in CMDS. */
|
---|
914 |
|
---|
915 | void
|
---|
916 | print_commands (const struct commands *cmds)
|
---|
917 | {
|
---|
918 | const char *s;
|
---|
919 |
|
---|
920 | fputs (_("# recipe to execute"), stdout);
|
---|
921 |
|
---|
922 | if (cmds->fileinfo.filenm == 0)
|
---|
923 | puts (_(" (built-in):"));
|
---|
924 | else
|
---|
925 | printf (_(" (from `%s', line %lu):\n"),
|
---|
926 | cmds->fileinfo.filenm, cmds->fileinfo.lineno);
|
---|
927 |
|
---|
928 | s = cmds->commands;
|
---|
929 | while (*s != '\0')
|
---|
930 | {
|
---|
931 | const char *end;
|
---|
932 |
|
---|
933 | end = strchr (s, '\n');
|
---|
934 | if (end == 0)
|
---|
935 | end = s + strlen (s);
|
---|
936 |
|
---|
937 | printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
|
---|
938 |
|
---|
939 | s = end + (end[0] == '\n');
|
---|
940 | }
|
---|
941 | }
|
---|