VirtualBox

source: kBuild/vendor/gnumake/current/commands.c

Last change on this file was 3138, checked in by bird, 7 years ago

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

  • Property svn:eol-style set to native
File size: 18.1 KB
Line 
1/* Command processing for GNU Make.
2Copyright (C) 1988-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this 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
28#if VMS
29# define FILE_LIST_SEPARATOR (vms_comma_separator ? ',' : ' ')
30#else
31# define FILE_LIST_SEPARATOR ' '
32#endif
33
34#ifndef HAVE_UNISTD_H
35int getpid ();
36#endif
37
38
39
40static unsigned long
41dep_hash_1 (const void *key)
42{
43 const struct dep *d = key;
44 return_STRING_HASH_1 (dep_name (d));
45}
46
47static unsigned long
48dep_hash_2 (const void *key)
49{
50 const struct dep *d = key;
51 return_STRING_HASH_2 (dep_name (d));
52}
53
54static int
55dep_hash_cmp (const void *x, const void *y)
56{
57 const struct dep *dx = x;
58 const struct dep *dy = y;
59 return strcmp (dep_name (dx), dep_name (dy));
60}
61
62/* Set FILE's automatic variables up. */
63
64void
65set_file_variables (struct file *file)
66{
67 struct dep *d;
68 const char *at, *percent, *star, *less;
69
70#ifndef NO_ARCHIVES
71 /* If the target is an archive member 'lib(member)',
72 then $@ is 'lib' and $% is 'member'. */
73
74 if (ar_name (file->name))
75 {
76 unsigned int len;
77 const char *cp;
78 char *p;
79
80 cp = strchr (file->name, '(');
81 p = alloca (cp - file->name + 1);
82 memcpy (p, file->name, cp - file->name);
83 p[cp - file->name] = '\0';
84 at = p;
85 len = strlen (cp + 1);
86 p = alloca (len);
87 memcpy (p, cp + 1, len - 1);
88 p[len - 1] = '\0';
89 percent = p;
90 }
91 else
92#endif /* NO_ARCHIVES. */
93 {
94 at = file->name;
95 percent = "";
96 }
97
98 /* $* is the stem from an implicit or static pattern rule. */
99 if (file->stem == 0)
100 {
101 /* In Unix make, $* is set to the target name with
102 any suffix in the .SUFFIXES list stripped off for
103 explicit rules. We store this in the 'stem' member. */
104 const char *name;
105 unsigned int len;
106
107#ifndef NO_ARCHIVES
108 if (ar_name (file->name))
109 {
110 name = strchr (file->name, '(') + 1;
111 len = strlen (name) - 1;
112 }
113 else
114#endif
115 {
116 name = file->name;
117 len = strlen (name);
118 }
119
120 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
121 {
122 unsigned int slen = strlen (dep_name (d));
123 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
124 {
125 file->stem = strcache_add_len (name, len - slen);
126 break;
127 }
128 }
129 if (d == 0)
130 file->stem = "";
131 }
132 star = file->stem;
133
134 /* $< is the first not order-only dependency. */
135 less = "";
136 for (d = file->deps; d != 0; d = d->next)
137 if (!d->ignore_mtime)
138 {
139 if (!d->need_2nd_expansion)
140 less = dep_name (d);
141 break;
142 }
143
144 if (file->cmds == default_file->cmds)
145 /* This file got its commands from .DEFAULT.
146 In this case $< is the same as $@. */
147 less = at;
148
149#define DEFINE_VARIABLE(name, len, value) \
150 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
151
152 /* Define the variables. */
153
154 DEFINE_VARIABLE ("<", 1, less);
155 DEFINE_VARIABLE ("*", 1, star);
156 DEFINE_VARIABLE ("@", 1, at);
157 DEFINE_VARIABLE ("%", 1, percent);
158
159 /* Compute the values for $^, $+, $?, and $|. */
160
161 {
162 static char *plus_value=0, *bar_value=0, *qmark_value=0;
163 static unsigned int plus_max=0, bar_max=0, qmark_max=0;
164
165 unsigned int qmark_len, plus_len, bar_len;
166 char *cp;
167 char *caret_value;
168 char *qp;
169 char *bp;
170 unsigned int len;
171
172 struct hash_table dep_hash;
173 void **slot;
174
175 /* Compute first the value for $+, which is supposed to contain
176 duplicate dependencies as they were listed in the makefile. */
177
178 plus_len = 0;
179 bar_len = 0;
180 for (d = file->deps; d != 0; d = d->next)
181 {
182 if (!d->need_2nd_expansion)
183 {
184 if (d->ignore_mtime)
185 bar_len += strlen (dep_name (d)) + 1;
186 else
187 plus_len += strlen (dep_name (d)) + 1;
188 }
189 }
190
191 if (bar_len == 0)
192 bar_len++;
193
194 if (plus_len == 0)
195 plus_len++;
196
197 if (plus_len > plus_max)
198 plus_value = xrealloc (plus_value, plus_max = plus_len);
199
200 cp = plus_value;
201
202 qmark_len = plus_len + 1; /* Will be this or less. */
203 for (d = file->deps; d != 0; d = d->next)
204 if (! d->ignore_mtime && ! d->need_2nd_expansion)
205 {
206 const char *c = dep_name (d);
207
208#ifndef NO_ARCHIVES
209 if (ar_name (c))
210 {
211 c = strchr (c, '(') + 1;
212 len = strlen (c) - 1;
213 }
214 else
215#endif
216 len = strlen (c);
217
218 memcpy (cp, c, len);
219 cp += len;
220 *cp++ = FILE_LIST_SEPARATOR;
221 if (! (d->changed || always_make_flag))
222 qmark_len -= len + 1; /* Don't space in $? for this one. */
223 }
224
225 /* Kill the last space and define the variable. */
226
227 cp[cp > plus_value ? -1 : 0] = '\0';
228 DEFINE_VARIABLE ("+", 1, plus_value);
229
230 /* Compute the values for $^, $?, and $|. */
231
232 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
233
234 if (qmark_len > qmark_max)
235 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
236 qp = qmark_value;
237
238 if (bar_len > bar_max)
239 bar_value = xrealloc (bar_value, bar_max = bar_len);
240 bp = bar_value;
241
242 /* Make sure that no dependencies are repeated in $^, $?, and $|. It
243 would be natural to combine the next two loops but we can't do it
244 because of a situation where we have two dep entries, the first
245 is order-only and the second is normal (see below). */
246
247 hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
248
249 for (d = file->deps; d != 0; d = d->next)
250 {
251 if (d->need_2nd_expansion)
252 continue;
253
254 slot = hash_find_slot (&dep_hash, d);
255 if (HASH_VACANT (*slot))
256 hash_insert_at (&dep_hash, d, slot);
257 else
258 {
259 /* Check if the two prerequisites have different ignore_mtime.
260 If so then we need to "upgrade" one that is order-only. */
261
262 struct dep* hd = (struct dep*) *slot;
263
264 if (d->ignore_mtime != hd->ignore_mtime)
265 d->ignore_mtime = hd->ignore_mtime = 0;
266 }
267 }
268
269 for (d = file->deps; d != 0; d = d->next)
270 {
271 const char *c;
272
273 if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
274 continue;
275
276 c = dep_name (d);
277#ifndef NO_ARCHIVES
278 if (ar_name (c))
279 {
280 c = strchr (c, '(') + 1;
281 len = strlen (c) - 1;
282 }
283 else
284#endif
285 len = strlen (c);
286
287 if (d->ignore_mtime)
288 {
289 memcpy (bp, c, len);
290 bp += len;
291 *bp++ = FILE_LIST_SEPARATOR;
292 }
293 else
294 {
295 memcpy (cp, c, len);
296 cp += len;
297 *cp++ = FILE_LIST_SEPARATOR;
298 if (d->changed || always_make_flag)
299 {
300 memcpy (qp, c, len);
301 qp += len;
302 *qp++ = FILE_LIST_SEPARATOR;
303 }
304 }
305 }
306
307 hash_free (&dep_hash, 0);
308
309 /* Kill the last spaces and define the variables. */
310
311 cp[cp > caret_value ? -1 : 0] = '\0';
312 DEFINE_VARIABLE ("^", 1, caret_value);
313
314 qp[qp > qmark_value ? -1 : 0] = '\0';
315 DEFINE_VARIABLE ("?", 1, qmark_value);
316
317 bp[bp > bar_value ? -1 : 0] = '\0';
318 DEFINE_VARIABLE ("|", 1, bar_value);
319 }
320
321#undef DEFINE_VARIABLE
322}
323
324
325/* Chop CMDS up into individual command lines if necessary.
326 Also set the 'lines_flags' and 'any_recurse' members. */
327
328void
329chop_commands (struct commands *cmds)
330{
331 unsigned int nlines, idx;
332 char **lines;
333
334 /* If we don't have any commands,
335 or we already parsed them, never mind. */
336
337 if (!cmds || cmds->command_lines != 0)
338 return;
339
340 /* Chop CMDS->commands up into lines in CMDS->command_lines. */
341
342 if (one_shell)
343 {
344 int l = strlen (cmds->commands);
345
346 nlines = 1;
347 lines = xmalloc (nlines * sizeof (char *));
348 lines[0] = xstrdup (cmds->commands);
349
350 /* Strip the trailing newline. */
351 if (l > 0 && lines[0][l-1] == '\n')
352 lines[0][l-1] = '\0';
353 }
354 else
355 {
356 const char *p;
357
358 nlines = 5;
359 lines = xmalloc (nlines * sizeof (char *));
360 idx = 0;
361 p = cmds->commands;
362 while (*p != '\0')
363 {
364 const char *end = p;
365 find_end:;
366 end = strchr (end, '\n');
367 if (end == 0)
368 end = p + strlen (p);
369 else if (end > p && end[-1] == '\\')
370 {
371 int backslash = 1;
372 const char *b;
373 for (b = end - 2; b >= p && *b == '\\'; --b)
374 backslash = !backslash;
375 if (backslash)
376 {
377 ++end;
378 goto find_end;
379 }
380 }
381
382 if (idx == nlines)
383 {
384 nlines += 2;
385 lines = xrealloc (lines, nlines * sizeof (char *));
386 }
387 lines[idx++] = xstrndup (p, end - p);
388 p = end;
389 if (*p != '\0')
390 ++p;
391 }
392
393 if (idx != nlines)
394 {
395 nlines = idx;
396 lines = xrealloc (lines, nlines * sizeof (char *));
397 }
398 }
399
400 /* Finally, set the corresponding CMDS->lines_flags elements and the
401 CMDS->any_recurse flag. */
402
403 if (nlines > USHRT_MAX)
404 ON (fatal, &cmds->fileinfo, _("Recipe has too many lines (%ud)"), nlines);
405
406 cmds->ncommand_lines = nlines;
407 cmds->command_lines = lines;
408
409 cmds->any_recurse = 0;
410 cmds->lines_flags = xmalloc (nlines);
411
412 for (idx = 0; idx < nlines; ++idx)
413 {
414 unsigned char flags = 0;
415 const char *p = lines[idx];
416
417 while (ISBLANK (*p) || *p == '-' || *p == '@' || *p == '+')
418 switch (*(p++))
419 {
420 case '+':
421 flags |= COMMANDS_RECURSE;
422 break;
423 case '@':
424 flags |= COMMANDS_SILENT;
425 break;
426 case '-':
427 flags |= COMMANDS_NOERROR;
428 break;
429 }
430
431 /* If no explicit '+' was given, look for MAKE variable references. */
432 if (!(flags & COMMANDS_RECURSE)
433 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
434 flags |= COMMANDS_RECURSE;
435
436 cmds->lines_flags[idx] = flags;
437 cmds->any_recurse |= flags & COMMANDS_RECURSE ? 1 : 0;
438 }
439}
440
441
442/* Execute the commands to remake FILE. If they are currently executing,
443 return or have already finished executing, just return. Otherwise,
444 fork off a child process to run the first command line in the sequence. */
445
446void
447execute_file_commands (struct file *file)
448{
449 const char *p;
450
451 /* Don't go through all the preparations if
452 the commands are nothing but whitespace. */
453
454 for (p = file->cmds->commands; *p != '\0'; ++p)
455 if (!ISSPACE (*p) && *p != '-' && *p != '@' && *p != '+')
456 break;
457 if (*p == '\0')
458 {
459 /* If there are no commands, assume everything worked. */
460 set_command_state (file, cs_running);
461 file->update_status = us_success;
462 notice_finished_file (file);
463 return;
464 }
465
466 /* First set the automatic variables according to this file. */
467
468 initialize_file_variables (file, 0);
469
470 set_file_variables (file);
471
472 /* If this is a loaded dynamic object, unload it before remaking.
473 Some systems don't support overwriting a loaded object. */
474 if (file->loaded)
475 unload_file (file->name);
476
477 /* Start the commands running. */
478 new_job (file);
479}
480
481
482/* This is set while we are inside fatal_error_signal,
483 so things can avoid nonreentrant operations. */
484
485int handling_fatal_signal = 0;
486
487/* Handle fatal signals. */
488
489RETSIGTYPE
490fatal_error_signal (int sig)
491{
492#ifdef __MSDOS__
493 extern int dos_status, dos_command_running;
494
495 if (dos_command_running)
496 {
497 /* That was the child who got the signal, not us. */
498 dos_status |= (sig << 8);
499 return;
500 }
501 remove_intermediates (1);
502 exit (EXIT_FAILURE);
503#else /* not __MSDOS__ */
504#ifdef _AMIGA
505 remove_intermediates (1);
506 if (sig == SIGINT)
507 fputs (_("*** Break.\n"), stderr);
508
509 exit (10);
510#else /* not Amiga */
511#ifdef WINDOWS32
512 extern HANDLE main_thread;
513
514 /* Windows creates a sperate thread for handling Ctrl+C, so we need
515 to suspend the main thread, or else we will have race conditions
516 when both threads call reap_children. */
517 if (main_thread)
518 {
519 DWORD susp_count = SuspendThread (main_thread);
520
521 if (susp_count != 0)
522 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
523 else if (susp_count == (DWORD)-1)
524 {
525 DWORD ierr = GetLastError ();
526
527 fprintf (stderr, "SuspendThread: error %ld: %s\n",
528 ierr, map_windows32_error_to_string (ierr));
529 }
530 }
531#endif
532 handling_fatal_signal = 1;
533
534 /* Set the handling for this signal to the default.
535 It is blocked now while we run this handler. */
536 signal (sig, SIG_DFL);
537
538 /* A termination signal won't be sent to the entire
539 process group, but it means we want to kill the children. */
540
541 if (sig == SIGTERM)
542 {
543 struct child *c;
544 for (c = children; c != 0; c = c->next)
545 if (!c->remote)
546 (void) kill (c->pid, SIGTERM);
547 }
548
549 /* If we got a signal that means the user
550 wanted to kill make, remove pending targets. */
551
552 if (sig == SIGTERM || sig == SIGINT
553#ifdef SIGHUP
554 || sig == SIGHUP
555#endif
556#ifdef SIGQUIT
557 || sig == SIGQUIT
558#endif
559 )
560 {
561 struct child *c;
562
563 /* Remote children won't automatically get signals sent
564 to the process group, so we must send them. */
565 for (c = children; c != 0; c = c->next)
566 if (c->remote)
567 (void) remote_kill (c->pid, sig);
568
569 for (c = children; c != 0; c = c->next)
570 delete_child_targets (c);
571
572 /* Clean up the children. We don't just use the call below because
573 we don't want to print the "Waiting for children" message. */
574 while (job_slots_used > 0)
575 reap_children (1, 0);
576 }
577 else
578 /* Wait for our children to die. */
579 while (job_slots_used > 0)
580 reap_children (1, 1);
581
582 /* Delete any non-precious intermediate files that were made. */
583
584 remove_intermediates (1);
585
586#ifdef SIGQUIT
587 if (sig == SIGQUIT)
588 /* We don't want to send ourselves SIGQUIT, because it will
589 cause a core dump. Just exit instead. */
590 exit (MAKE_TROUBLE);
591#endif
592
593#ifdef WINDOWS32
594 if (main_thread)
595 CloseHandle (main_thread);
596 /* Cannot call W32_kill with a pid (it needs a handle). The exit
597 status of 130 emulates what happens in Bash. */
598 exit (130);
599#else
600 /* Signal the same code; this time it will really be fatal. The signal
601 will be unblocked when we return and arrive then to kill us. */
602 if (kill (getpid (), sig) < 0)
603 pfatal_with_name ("kill");
604#endif /* not WINDOWS32 */
605#endif /* not Amiga */
606#endif /* not __MSDOS__ */
607}
608
609
610/* Delete FILE unless it's precious or not actually a file (phony),
611 and it has changed on disk since we last stat'd it. */
612
613static void
614delete_target (struct file *file, const char *on_behalf_of)
615{
616 struct stat st;
617 int e;
618
619 if (file->precious || file->phony)
620 return;
621
622#ifndef NO_ARCHIVES
623 if (ar_name (file->name))
624 {
625 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
626 ? (time_t) -1
627 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
628 if (ar_member_date (file->name) != file_date)
629 {
630 if (on_behalf_of)
631 OSS (error, NILF,
632 _("*** [%s] Archive member '%s' may be bogus; not deleted"),
633 on_behalf_of, file->name);
634 else
635 OS (error, NILF,
636 _("*** Archive member '%s' may be bogus; not deleted"),
637 file->name);
638 }
639 return;
640 }
641#endif
642
643 EINTRLOOP (e, stat (file->name, &st));
644 if (e == 0
645 && S_ISREG (st.st_mode)
646 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
647 {
648 if (on_behalf_of)
649 OSS (error, NILF,
650 _("*** [%s] Deleting file '%s'"), on_behalf_of, file->name);
651 else
652 OS (error, NILF, _("*** Deleting file '%s'"), file->name);
653 if (unlink (file->name) < 0
654 && errno != ENOENT) /* It disappeared; so what. */
655 perror_with_name ("unlink: ", file->name);
656 }
657}
658
659
660/* Delete all non-precious targets of CHILD unless they were already deleted.
661 Set the flag in CHILD to say they've been deleted. */
662
663void
664delete_child_targets (struct child *child)
665{
666 struct dep *d;
667
668 if (child->deleted)
669 return;
670
671 /* Delete the target file if it changed. */
672 delete_target (child->file, NULL);
673
674 /* Also remove any non-precious targets listed in the 'also_make' member. */
675 for (d = child->file->also_make; d != 0; d = d->next)
676 delete_target (d->file, child->file->name);
677
678 child->deleted = 1;
679}
680
681
682/* Print out the commands in CMDS. */
683
684void
685print_commands (const struct commands *cmds)
686{
687 const char *s;
688
689 fputs (_("# recipe to execute"), stdout);
690
691 if (cmds->fileinfo.filenm == 0)
692 puts (_(" (built-in):"));
693 else
694 printf (_(" (from '%s', line %lu):\n"),
695 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
696
697 s = cmds->commands;
698 while (*s != '\0')
699 {
700 const char *end;
701 int bs;
702
703 /* Print one full logical recipe line: find a non-escaped newline. */
704 for (end = s, bs = 0; *end != '\0'; ++end)
705 {
706 if (*end == '\n' && !bs)
707 break;
708
709 bs = *end == '\\' ? !bs : 0;
710 }
711
712 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
713
714 s = end + (end[0] == '\n');
715 }
716}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette