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