1 /* Command processing for GNU Make.
2 Copyright (C) 1988-2022 Free Software Foundation, Inc.
3 This file is part of GNU Make.
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
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.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <https://www.gnu.org/licenses/>. */
30 # define FILE_LIST_SEPARATOR (vms_comma_separator ? ',' : ' ')
32 # define FILE_LIST_SEPARATOR ' '
37 dep_hash_1 (const void *key)
39 const struct dep *d = key;
40 return_STRING_HASH_1 (dep_name (d));
44 dep_hash_2 (const void *key)
46 const struct dep *d = key;
47 return_STRING_HASH_2 (dep_name (d));
51 dep_hash_cmp (const void *x, const void *y)
53 const struct dep *dx = x;
54 const struct dep *dy = y;
55 return strcmp (dep_name (dx), dep_name (dy));
58 /* Set FILE's automatic variables up.
60 * If STEM is 0, then set FILE->STEM and $* to the target name with any
61 * suffix in the .SUFFIXES list stripped off. */
64 set_file_variables (struct file *file, const char *stem)
67 const char *at, *percent, *star, *less;
70 /* If the target is an archive member 'lib(member)',
71 then $@ is 'lib' and $% is 'member'. */
73 if (ar_name (file->name))
79 cp = strchr (file->name, '(');
80 p = alloca (cp - file->name + 1);
81 memcpy (p, file->name, cp - file->name);
82 p[cp - file->name] = '\0';
84 len = strlen (cp + 1);
86 memcpy (p, cp + 1, len - 1);
91 #endif /* NO_ARCHIVES. */
97 /* $* is the stem from an implicit or static pattern rule. */
100 /* In Unix make, $* is set to the target name with
101 any suffix in the .SUFFIXES list stripped off for
102 explicit rules. We store this in the 'stem' member. */
107 if (ar_name (file->name))
109 name = strchr (file->name, '(') + 1;
110 len = strlen (name) - 1;
119 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
121 const char *dn = dep_name (d);
122 size_t slen = strlen (dn);
123 if (len > slen && memcmp (dn, name + (len - slen), slen) == 0)
125 file->stem = stem = strcache_add_len (name, len - slen);
130 file->stem = stem = "";
134 /* $< is the first not order-only dependency. */
136 for (d = file->deps; d != 0; d = d->next)
137 if (!d->ignore_mtime && !d->ignore_automatic_vars && !d->need_2nd_expansion)
143 if (file->cmds != 0 && file->cmds == default_file->cmds)
144 /* This file got its commands from .DEFAULT.
145 In this case $< is the same as $@. */
148 #define DEFINE_VARIABLE(name, len, value) \
149 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
151 /* Define the variables. */
153 DEFINE_VARIABLE ("<", 1, less);
154 DEFINE_VARIABLE ("*", 1, star);
155 DEFINE_VARIABLE ("@", 1, at);
156 DEFINE_VARIABLE ("%", 1, percent);
158 /* Compute the values for $^, $+, $?, and $|. */
161 static char *plus_value=0, *bar_value=0, *qmark_value=0;
162 static size_t plus_max=0, bar_max=0, qmark_max=0;
164 size_t qmark_len, plus_len, bar_len;
171 struct hash_table dep_hash;
174 /* Compute first the value for $+, which is supposed to contain
175 duplicate dependencies as they were listed in the makefile. */
179 for (d = file->deps; d != 0; d = d->next)
181 if (!d->need_2nd_expansion && !d->ignore_automatic_vars)
184 bar_len += strlen (dep_name (d)) + 1;
186 plus_len += strlen (dep_name (d)) + 1;
196 if (plus_len > plus_max)
197 plus_value = xrealloc (plus_value, plus_max = plus_len);
201 qmark_len = plus_len + 1; /* Will be this or less. */
202 for (d = file->deps; d != 0; d = d->next)
203 if (! d->ignore_mtime && ! d->need_2nd_expansion && ! d->ignore_automatic_vars)
205 const char *c = dep_name (d);
210 c = strchr (c, '(') + 1;
211 len = strlen (c) - 1;
217 cp = mempcpy (cp, c, len);
218 *cp++ = FILE_LIST_SEPARATOR;
219 if (! (d->changed || always_make_flag))
220 qmark_len -= len + 1; /* Don't space in $? for this one. */
223 /* Kill the last space and define the variable. */
225 cp[cp > plus_value ? -1 : 0] = '\0';
226 DEFINE_VARIABLE ("+", 1, plus_value);
228 /* Compute the values for $^, $?, and $|. */
230 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
232 if (qmark_len > qmark_max)
233 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
236 if (bar_len > bar_max)
237 bar_value = xrealloc (bar_value, bar_max = bar_len);
240 /* Make sure that no dependencies are repeated in $^, $?, and $|. It
241 would be natural to combine the next two loops but we can't do it
242 because of a situation where we have two dep entries, the first
243 is order-only and the second is normal (see below). */
245 hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
247 for (d = file->deps; d != 0; d = d->next)
249 if (d->need_2nd_expansion || d->ignore_automatic_vars)
252 slot = hash_find_slot (&dep_hash, d);
253 if (HASH_VACANT (*slot))
254 hash_insert_at (&dep_hash, d, slot);
257 /* Check if the two prerequisites have different ignore_mtime.
258 If so then we need to "upgrade" one that is order-only. */
260 struct dep* hd = (struct dep*) *slot;
262 if (d->ignore_mtime != hd->ignore_mtime)
263 d->ignore_mtime = hd->ignore_mtime = 0;
267 for (d = file->deps; d != 0; d = d->next)
271 if (d->need_2nd_expansion || d->ignore_automatic_vars || hash_find_item (&dep_hash, d) != d)
278 c = strchr (c, '(') + 1;
279 len = strlen (c) - 1;
287 bp = mempcpy (bp, c, len);
288 *bp++ = FILE_LIST_SEPARATOR;
292 cp = mempcpy (cp, c, len);
293 *cp++ = FILE_LIST_SEPARATOR;
294 if (d->changed || always_make_flag)
296 qp = mempcpy (qp, c, len);
297 *qp++ = FILE_LIST_SEPARATOR;
302 hash_free (&dep_hash, 0);
304 /* Kill the last spaces and define the variables. */
306 cp[cp > caret_value ? -1 : 0] = '\0';
307 DEFINE_VARIABLE ("^", 1, caret_value);
309 qp[qp > qmark_value ? -1 : 0] = '\0';
310 DEFINE_VARIABLE ("?", 1, qmark_value);
312 bp[bp > bar_value ? -1 : 0] = '\0';
313 DEFINE_VARIABLE ("|", 1, bar_value);
316 #undef DEFINE_VARIABLE
319 /* Chop CMDS up into individual command lines if necessary.
320 Also set the 'lines_flags' and 'any_recurse' members. */
323 chop_commands (struct commands *cmds)
325 unsigned short nlines;
329 /* If we don't have any commands, or we already parsed them, never mind. */
330 if (!cmds || cmds->command_lines != NULL)
333 /* Chop CMDS->commands up into lines in CMDS->command_lines. */
337 size_t l = strlen (cmds->commands);
340 lines = xmalloc (nlines * sizeof (char *));
341 lines[0] = xstrdup (cmds->commands);
343 /* Strip the trailing newline. */
344 if (l > 0 && lines[0][l-1] == '\n')
345 lines[0][l-1] = '\0';
349 const char *p = cmds->commands;
353 lines = xmalloc (max * sizeof (char *));
358 end = strchr (end, '\n');
360 end = p + strlen (p);
361 else if (end > p && end[-1] == '\\')
367 for (b = end - 2; b >= p && *b == '\\'; --b)
368 backslash = !backslash;
377 if (nlines == USHRT_MAX)
378 ON (fatal, &cmds->fileinfo,
379 _("Recipe has too many lines (limit %hu)"), nlines);
384 lines = xrealloc (lines, max * sizeof (char *));
387 lines[nlines++] = xstrndup (p, (size_t) (end - p));
394 /* Finally, set the corresponding CMDS->lines_flags elements and the
395 CMDS->any_recurse flag. */
397 cmds->ncommand_lines = nlines;
398 cmds->command_lines = lines;
400 cmds->any_recurse = 0;
401 cmds->lines_flags = xmalloc (nlines);
403 for (i = 0; i < nlines; ++i)
405 unsigned char flags = 0;
406 const char *p = lines[i];
408 while (ISBLANK (*p) || *p == '-' || *p == '@' || *p == '+')
412 flags |= COMMANDS_RECURSE;
415 flags |= COMMANDS_SILENT;
418 flags |= COMMANDS_NOERROR;
422 /* If no explicit '+' was given, look for MAKE variable references. */
423 if (! ANY_SET (flags, COMMANDS_RECURSE)
424 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
425 flags |= COMMANDS_RECURSE;
427 cmds->lines_flags[i] = flags;
428 cmds->any_recurse |= ANY_SET (flags, COMMANDS_RECURSE) ? 1 : 0;
432 /* Execute the commands to remake FILE. If they are currently executing,
433 return or have already finished executing, just return. Otherwise,
434 fork off a child process to run the first command line in the sequence. */
437 execute_file_commands (struct file *file)
441 /* Don't go through all the preparations if
442 the commands are nothing but whitespace. */
444 for (p = file->cmds->commands; *p != '\0'; ++p)
445 if (!ISSPACE (*p) && *p != '-' && *p != '@' && *p != '+')
449 /* If there are no commands, assume everything worked. */
450 set_command_state (file, cs_running);
451 file->update_status = us_success;
452 notice_finished_file (file);
456 /* First set the automatic variables according to this file. */
458 initialize_file_variables (file, 0);
460 set_file_variables (file, file->stem);
462 /* Some systems don't support overwriting a loaded object so if this one
463 unload it before remaking. Keep its name in .LOADED: it will be rebuilt
464 and loaded again. If rebuilding or loading again fail, then we'll exit
465 anyway and it won't matter. */
466 if (file->loaded && unload_file (file->name) == 0)
472 /* Start the commands running. */
476 /* This is set while we are inside fatal_error_signal,
477 so things can avoid nonreentrant operations. */
479 volatile sig_atomic_t handling_fatal_signal = 0;
481 /* Handle fatal signals. */
484 fatal_error_signal (int sig)
487 extern int dos_status, dos_command_running;
489 if (dos_command_running)
491 /* That was the child who got the signal, not us. */
492 dos_status |= (sig << 8);
495 remove_intermediates (1);
497 #else /* not __MSDOS__ */
499 remove_intermediates (1);
501 fputs (_("*** Break.\n"), stderr);
504 #else /* not Amiga */
506 extern HANDLE main_thread;
508 /* Windows creates a separate thread for handling Ctrl+C, so we need
509 to suspend the main thread, or else we will have race conditions
510 when both threads call reap_children. */
513 DWORD susp_count = SuspendThread (main_thread);
516 fprintf (stderr, "SuspendThread: suspend count = %lu\n", susp_count);
517 else if (susp_count == (DWORD)-1)
519 DWORD ierr = GetLastError ();
521 fprintf (stderr, "SuspendThread: error %lu: %s\n",
522 ierr, map_windows32_error_to_string (ierr));
526 handling_fatal_signal = 1;
528 /* Set the handling for this signal to the default.
529 It is blocked now while we run this handler. */
530 signal (sig, SIG_DFL);
532 temp_stdin_unlink ();
536 /* A termination signal won't be sent to the entire
537 process group, but it means we want to kill the children. */
542 for (c = children; c != 0; c = c->next)
543 if (!c->remote && c->pid > 0)
544 (void) kill (c->pid, SIGTERM);
547 /* If we got a signal that means the user
548 wanted to kill make, remove pending targets. */
550 if (sig == SIGTERM || sig == SIGINT
561 /* Remote children won't automatically get signals sent
562 to the process group, so we must send them. */
563 for (c = children; c != 0; c = c->next)
564 if (c->remote && c->pid > 0)
565 (void) remote_kill (c->pid, sig);
567 for (c = children; c != 0; c = c->next)
568 delete_child_targets (c);
570 /* Clean up the children. We don't just use the call below because
571 we don't want to print the "Waiting for children" message. */
572 while (job_slots_used > 0)
573 reap_children (1, 0);
576 /* Wait for our children to die. */
577 while (job_slots_used > 0)
578 reap_children (1, 1);
580 /* Delete any non-precious intermediate files that were made. */
582 remove_intermediates (1);
586 /* We don't want to send ourselves SIGQUIT, because it will
587 cause a core dump. Just exit instead. */
593 CloseHandle (main_thread);
594 /* Cannot call W32_kill with a pid (it needs a handle). The exit
595 status of 130 emulates what happens in Bash. */
598 /* Signal the same code; this time it will really be fatal. The signal
599 will be unblocked when we return and arrive then to kill us. */
600 if (kill (make_pid (), sig) < 0)
601 pfatal_with_name ("kill");
602 #endif /* not WINDOWS32 */
603 #endif /* not Amiga */
604 #endif /* not __MSDOS__ */
607 /* Delete FILE unless it's precious or not actually a file (phony),
608 and it has changed on disk since we last stat'd it. */
611 delete_target (struct file *file, const char *on_behalf_of)
616 if (file->precious || file->phony)
620 if (ar_name (file->name))
622 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
624 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
625 if (ar_member_date (file->name) != file_date)
629 _("*** [%s] Archive member '%s' may be bogus; not deleted"),
630 on_behalf_of, file->name);
633 _("*** Archive member '%s' may be bogus; not deleted"),
640 EINTRLOOP (e, stat (file->name, &st));
642 && S_ISREG (st.st_mode)
643 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
647 _("*** [%s] Deleting file '%s'"), on_behalf_of, file->name);
649 OS (error, NILF, _("*** Deleting file '%s'"), file->name);
650 if (unlink (file->name) < 0
651 && errno != ENOENT) /* It disappeared; so what. */
652 perror_with_name ("unlink: ", file->name);
657 /* Delete all non-precious targets of CHILD unless they were already deleted.
658 Set the flag in CHILD to say they've been deleted. */
661 delete_child_targets (struct child *child)
665 if (child->deleted || child->pid < 0)
668 /* Delete the target file if it changed. */
669 delete_target (child->file, NULL);
671 /* Also remove any non-precious targets listed in the 'also_make' member. */
672 for (d = child->file->also_make; d != 0; d = d->next)
673 delete_target (d->file, child->file->name);
678 /* Print out the commands in CMDS. */
681 print_commands (const struct commands *cmds)
685 fputs (_("# recipe to execute"), stdout);
687 if (cmds->fileinfo.filenm == 0)
688 puts (_(" (built-in):"));
690 printf (_(" (from '%s', line %lu):\n"),
691 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
699 /* Print one full logical recipe line: find a non-escaped newline. */
700 for (end = s, bs = 0; *end != '\0'; ++end)
702 if (*end == '\n' && !bs)
705 bs = *end == '\\' ? !bs : 0;
708 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
710 s = end + (end[0] == '\n');