source: trunk/src/kmk/commands.c@ 1933

Last change on this file since 1933 was 1933, checked in by bird, 17 years ago

kmk: moved suffixes_strcached to strcache.c and global scope.

  • Property svn:eol-style set to native
File size: 17.9 KB
Line 
1/* Command processing for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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
30#if VMS
31# define FILE_LIST_SEPARATOR ','
32#else
33# define FILE_LIST_SEPARATOR ' '
34#endif
35
36int remote_kill (int id, int sig);
37
38#ifndef HAVE_UNISTD_H
39int getpid ();
40#endif
41
42
43/* Set FILE's automatic variables up. */
44
45void
46set_file_variables (struct file *file)
47{
48 const struct dep *d;
49 const char *at, *percent, *star, *less;
50
51#ifndef NO_ARCHIVES
52 /* If the target is an archive member `lib(member)',
53 then $@ is `lib' and $% is `member'. */
54
55 if (ar_name (file->name))
56 {
57 unsigned int len;
58 const char *cp;
59 char *p;
60
61 cp = strchr (file->name, '(');
62 p = alloca (cp - file->name + 1);
63 memcpy (p, file->name, cp - file->name);
64 p[cp - file->name] = '\0';
65 at = p;
66 len = strlen (cp + 1);
67 p = alloca (len);
68 memcpy (p, cp + 1, len - 1);
69 p[len - 1] = '\0';
70 percent = p;
71 }
72 else
73#endif /* NO_ARCHIVES. */
74 {
75 at = file->name;
76 percent = "";
77 }
78
79 /* $* is the stem from an implicit or static pattern rule. */
80 if (file->stem == 0)
81 {
82 /* In Unix make, $* is set to the target name with
83 any suffix in the .SUFFIXES list stripped off for
84 explicit rules. We store this in the `stem' member. */
85 const char *name;
86 unsigned int len;
87
88#ifndef NO_ARCHIVES
89 if (ar_name (file->name))
90 {
91 name = strchr (file->name, '(') + 1;
92 len = strlen (name) - 1;
93 }
94 else
95#endif
96 {
97 name = file->name;
98#ifndef CONFIG_WITH_STRCACHE2
99 len = strlen (name);
100#else
101 len = strcache2_get_len (&file_strcache, name);
102#endif
103 }
104
105#ifndef CONFIG_WITH_STRCACHE2
106 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
107 {
108 unsigned int slen = strlen (dep_name (d));
109#else
110 for (d = enter_file (suffixes_strcached)->deps; d ; d = d->next)
111 {
112 unsigned int slen = strcache2_get_len (&file_strcache, dep_name (d));
113#endif
114 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
115 {
116 file->stem = strcache_add_len (name, len - slen);
117 break;
118 }
119 }
120 if (d == 0)
121 file->stem = "";
122 }
123 star = file->stem;
124
125 /* $< is the first not order-only dependency. */
126 less = "";
127 for (d = file->deps; d != 0; d = d->next)
128 if (!d->ignore_mtime)
129 {
130 less = dep_name (d);
131 break;
132 }
133
134 if (file->cmds == default_file->cmds)
135 /* This file got its commands from .DEFAULT.
136 In this case $< is the same as $@. */
137 less = at;
138
139#define DEFINE_VARIABLE(name, len, value) \
140 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
141
142 /* Define the variables. */
143
144#ifndef CONFIG_WITH_RDONLY_VARIABLE_VALUE
145 DEFINE_VARIABLE ("<", 1, less);
146 DEFINE_VARIABLE ("*", 1, star);
147 DEFINE_VARIABLE ("@", 1, at);
148 DEFINE_VARIABLE ("%", 1, percent);
149#else /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
150# define DEFINE_VARIABLE_RO_VAL(name, len, value, value_len) \
151 define_variable_in_set((name), (len), (value), (value_len), -1, \
152 (o_automatic), 0, (file)->variables->set, NILF)
153
154 if (*less == '\0')
155 DEFINE_VARIABLE_RO_VAL ("<", 1, "", 0);
156 else if (less != at || at == file->name)
157 DEFINE_VARIABLE_RO_VAL ("<", 1, less, strcache_get_len (less));
158 else
159 DEFINE_VARIABLE ("<", 1, less);
160
161 if (*star == '\0')
162 DEFINE_VARIABLE_RO_VAL ("*", 1, "", 0);
163 else
164 DEFINE_VARIABLE_RO_VAL ("*", 1, star, strcache_get_len (star));
165
166 if (at == file->name)
167 DEFINE_VARIABLE_RO_VAL ("@", 1, at, strcache_get_len (at));
168 else
169 DEFINE_VARIABLE ("@", 1, at);
170
171 if (*percent == '\0')
172 DEFINE_VARIABLE_RO_VAL ("%", 1, "", 0);
173 else
174 DEFINE_VARIABLE ("%", 1, percent);
175#endif /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
176
177 /* Compute the values for $^, $+, $?, and $|. */
178
179 {
180 static char *plus_value=0, *bar_value=0, *qmark_value=0;
181 static unsigned int plus_max=0, bar_max=0, qmark_max=0;
182
183 unsigned int qmark_len, plus_len, bar_len;
184 char *cp;
185 char *caret_value;
186 char *qp;
187 char *bp;
188 unsigned int len;
189
190 /* Compute first the value for $+, which is supposed to contain
191 duplicate dependencies as they were listed in the makefile. */
192
193 plus_len = 0;
194 for (d = file->deps; d != 0; d = d->next)
195 if (! d->ignore_mtime)
196#ifndef CONFIG_WITH_STRCACHE2
197 plus_len += strlen (dep_name (d)) + 1;
198#else
199 plus_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
200#endif
201 if (plus_len == 0)
202 plus_len++;
203
204 if (plus_len > plus_max)
205 plus_value = xrealloc (plus_value, plus_max = plus_len);
206 cp = plus_value;
207
208 qmark_len = plus_len + 1; /* Will be this or less. */
209 for (d = file->deps; d != 0; d = d->next)
210 if (! d->ignore_mtime)
211 {
212 const char *c = dep_name (d);
213
214#ifndef NO_ARCHIVES
215 if (ar_name (c))
216 {
217 c = strchr (c, '(') + 1;
218 len = strlen (c) - 1;
219 }
220 else
221#endif
222#ifndef CONFIG_WITH_STRCACHE2
223 len = strlen (c);
224#else
225 len = strcache2_get_len (&file_strcache, c);
226#endif
227
228 memcpy (cp, c, len);
229 cp += len;
230 *cp++ = FILE_LIST_SEPARATOR;
231 if (! d->changed)
232 qmark_len -= len + 1; /* Don't space in $? for this one. */
233 }
234
235 /* Kill the last space and define the variable. */
236
237 cp[cp > plus_value ? -1 : 0] = '\0';
238 DEFINE_VARIABLE ("+", 1, plus_value);
239
240 /* Make sure that no dependencies are repeated. This does not
241 really matter for the purpose of updating targets, but it
242 might make some names be listed twice for $^ and $?. */
243
244 uniquize_deps (file->deps);
245
246 bar_len = 0;
247 for (d = file->deps; d != 0; d = d->next)
248 if (d->ignore_mtime)
249#ifndef CONFIG_WITH_STRCACHE2
250 bar_len += strlen (dep_name (d)) + 1;
251#else
252 bar_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
253#endif
254 if (bar_len == 0)
255 bar_len++;
256
257 /* Compute the values for $^, $?, and $|. */
258
259 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
260
261 if (qmark_len > qmark_max)
262 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
263 qp = qmark_value;
264
265 if (bar_len > bar_max)
266 bar_value = xrealloc (bar_value, bar_max = bar_len);
267 bp = bar_value;
268
269 for (d = file->deps; d != 0; d = d->next)
270 {
271 const char *c = dep_name (d);
272
273#ifndef NO_ARCHIVES
274 if (ar_name (c))
275 {
276 c = strchr (c, '(') + 1;
277 len = strlen (c) - 1;
278 }
279 else
280#endif
281#ifndef CONFIG_WITH_STRCACHE2
282 len = strlen (c);
283#else
284 len = strcache2_get_len (&file_strcache, c);
285#endif
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)
299 {
300 memcpy (qp, c, len);
301 qp += len;
302 *qp++ = FILE_LIST_SEPARATOR;
303 }
304 }
305 }
306
307 /* Kill the last spaces and define the variables. */
308
309 cp[cp > caret_value ? -1 : 0] = '\0';
310 DEFINE_VARIABLE ("^", 1, caret_value);
311
312 qp[qp > qmark_value ? -1 : 0] = '\0';
313 DEFINE_VARIABLE ("?", 1, qmark_value);
314
315 bp[bp > bar_value ? -1 : 0] = '\0';
316 DEFINE_VARIABLE ("|", 1, bar_value);
317 }
318
319#undef DEFINE_VARIABLE
320}
321
322
323/* Chop CMDS up into individual command lines if necessary.
324 Also set the `lines_flags' and `any_recurse' members. */
325
326void
327chop_commands (struct commands *cmds)
328{
329 const char *p;
330 unsigned int nlines, idx;
331 char **lines;
332
333 /* If we don't have any commands,
334 or we already parsed them, never mind. */
335
336 if (!cmds || cmds->command_lines != 0)
337 return;
338
339 /* Chop CMDS->commands up into lines in CMDS->command_lines.
340 Also set the corresponding CMDS->lines_flags elements,
341 and the CMDS->any_recurse flag. */
342
343 nlines = 5;
344 lines = xmalloc (5 * sizeof (char *));
345 idx = 0;
346 p = cmds->commands;
347 while (*p != '\0')
348 {
349 const char *end = p;
350 find_end:;
351 end = strchr (end, '\n');
352 if (end == 0)
353 end = p + strlen (p);
354 else if (end > p && end[-1] == '\\')
355 {
356 int backslash = 1;
357 const char *b;
358 for (b = end - 2; b >= p && *b == '\\'; --b)
359 backslash = !backslash;
360 if (backslash)
361 {
362 ++end;
363 goto find_end;
364 }
365 }
366
367 if (idx == nlines)
368 {
369 nlines += 2;
370 lines = xrealloc (lines, nlines * sizeof (char *));
371 }
372 lines[idx++] = savestring (p, end - p);
373 p = end;
374 if (*p != '\0')
375 ++p;
376 }
377
378 if (idx != nlines)
379 {
380 nlines = idx;
381 lines = xrealloc (lines, nlines * sizeof (char *));
382 }
383
384 cmds->ncommand_lines = nlines;
385 cmds->command_lines = lines;
386
387 cmds->any_recurse = 0;
388#ifdef CONFIG_WITH_COMMANDS_FUNC
389 cmds->lines_flags = xmalloc (nlines * sizeof (cmds->lines_flags[0]));
390#else
391 cmds->lines_flags = xmalloc (nlines);
392#endif
393 for (idx = 0; idx < nlines; ++idx)
394 {
395 int flags = 0;
396
397 for (p = lines[idx];
398#ifdef CONFIG_WITH_COMMANDS_FUNC
399 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+' || *p == '%';
400#else
401 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+';
402#endif
403 ++p)
404 switch (*p)
405 {
406 case '+':
407 flags |= COMMANDS_RECURSE;
408 break;
409 case '@':
410 flags |= COMMANDS_SILENT;
411 break;
412 case '-':
413 flags |= COMMANDS_NOERROR;
414 break;
415#ifdef CONFIG_WITH_COMMANDS_FUNC
416 case '%':
417 flags |= COMMAND_GETTER_SKIP_IT;
418 break;
419#endif
420 }
421
422 /* If no explicit '+' was given, look for MAKE variable references. */
423 if (!(flags & COMMANDS_RECURSE)
424#ifndef KMK
425 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
426#else
427 && (strstr (p, "$(KMK)") != 0 || strstr (p, "${KMK}") != 0 ||
428 strstr (p, "${MAKE}") != 0 || strstr (p, "${MAKE}") != 0))
429#endif
430 flags |= COMMANDS_RECURSE;
431
432#ifdef CONFIG_WITH_KMK_BUILTIN
433 /* check if kmk builtin command */
434 if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
435 flags |= COMMANDS_KMK_BUILTIN;
436#endif
437
438 cmds->lines_flags[idx] = flags;
439 cmds->any_recurse |= flags & COMMANDS_RECURSE;
440 }
441}
442
443
444/* Execute the commands to remake FILE. If they are currently executing,
445 return or have already finished executing, just return. Otherwise,
446 fork off a child process to run the first command line in the sequence. */
447
448void
449execute_file_commands (struct file *file)
450{
451 const char *p;
452
453 /* Don't go through all the preparations if
454 the commands are nothing but whitespace. */
455
456 for (p = file->cmds->commands; *p != '\0'; ++p)
457 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
458 break;
459 if (*p == '\0')
460 {
461 /* If there are no commands, assume everything worked. */
462#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
463 file->command_flags |= COMMANDS_NO_COMMANDS;
464#endif
465 set_command_state (file, cs_running);
466 file->update_status = 0;
467 notice_finished_file (file);
468 return;
469 }
470
471 /* First set the automatic variables according to this file. */
472
473 initialize_file_variables (file, 0);
474
475 set_file_variables (file);
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#if defined(WINDOWS32) && !defined(CONFIG_NEW_WIN32_CTRL_EVENT)
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#ifdef SIGQUIT
586 if (sig == SIGQUIT)
587 /* We don't want to send ourselves SIGQUIT, because it will
588 cause a core dump. Just exit instead. */
589 exit (EXIT_FAILURE);
590#endif
591
592#ifdef WINDOWS32
593#ifndef CONFIG_NEW_WIN32_CTRL_EVENT
594 if (main_thread)
595 CloseHandle (main_thread);
596#endif /* !CONFIG_NEW_WIN32_CTRL_EVENT */
597 /* Cannot call W32_kill with a pid (it needs a handle). The exit
598 status of 130 emulates what happens in Bash. */
599 exit (130);
600#else
601 /* Signal the same code; this time it will really be fatal. The signal
602 will be unblocked when we return and arrive then to kill us. */
603 if (kill (getpid (), sig) < 0)
604 pfatal_with_name ("kill");
605#endif /* not WINDOWS32 */
606#endif /* not Amiga */
607#endif /* not __MSDOS__ */
608}
609
610
611/* Delete FILE unless it's precious or not actually a file (phony),
612 and it has changed on disk since we last stat'd it. */
613
614static void
615delete_target (struct file *file, const char *on_behalf_of)
616{
617 struct stat st;
618 int e;
619
620 if (file->precious || file->phony)
621 return;
622
623#ifndef NO_ARCHIVES
624 if (ar_name (file->name))
625 {
626 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
627 ? (time_t) -1
628 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
629 if (ar_member_date (file->name) != file_date)
630 {
631 if (on_behalf_of)
632 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
633 on_behalf_of, file->name);
634 else
635 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
636 file->name);
637 }
638 return;
639 }
640#endif
641
642 EINTRLOOP (e, stat (file->name, &st));
643 if (e == 0
644 && S_ISREG (st.st_mode)
645 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
646 {
647 if (on_behalf_of)
648 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
649 else
650 error (NILF, _("*** Deleting file `%s'"), file->name);
651 if (unlink (file->name) < 0
652 && errno != ENOENT) /* It disappeared; so what. */
653 perror_with_name ("unlink: ", file->name);
654 }
655}
656
657
658/* Delete all non-precious targets of CHILD unless they were already deleted.
659 Set the flag in CHILD to say they've been deleted. */
660
661void
662delete_child_targets (struct child *child)
663{
664 struct dep *d;
665
666 if (child->deleted)
667 return;
668
669 /* Delete the target file if it changed. */
670 delete_target (child->file, NULL);
671
672 /* Also remove any non-precious targets listed in the `also_make' member. */
673 for (d = child->file->also_make; d != 0; d = d->next)
674 delete_target (d->file, child->file->name);
675
676 child->deleted = 1;
677}
678
679
680/* Print out the commands in CMDS. */
681
682void
683print_commands (const struct commands *cmds)
684{
685 const char *s;
686
687 fputs (_("# commands to execute"), stdout);
688
689 if (cmds->fileinfo.filenm == 0)
690 puts (_(" (built-in):"));
691 else
692 printf (_(" (from `%s', line %lu):\n"),
693 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
694
695 s = cmds->commands;
696 while (*s != '\0')
697 {
698 const char *end;
699
700 while (isspace ((unsigned char)*s))
701 ++s;
702
703 end = strchr (s, '\n');
704 if (end == 0)
705 end = s + strlen (s);
706
707 printf ("\t%.*s\n", (int) (end - s), s);
708
709 s = end;
710 }
711}
Note: See TracBrowser for help on using the repository browser.