summaryrefslogtreecommitdiffstats
path: root/src/kmk/commands.c
blob: 619839c9111011b42f2a37f4b73268735c1692e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/* Command processing for GNU Make.
Copyright (C) 1988-2016 Free Software Foundation, Inc.
This file is part of GNU Make.

GNU Make is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "makeint.h"
#include "filedef.h"
#include "dep.h"
#include "variable.h"
#include "job.h"
#include "commands.h"
#ifdef WINDOWS32
#include <windows.h>
#include "w32err.h"
# ifdef CONFIG_NEW_WIN_CHILDREN
#  include "w32/winchildren.h"
# endif
#endif
#ifdef CONFIG_WITH_LAZY_DEPS_VARS
# include <assert.h>
#endif

#if VMS
# define FILE_LIST_SEPARATOR (vms_comma_separator ? ',' : ' ')
#else
# define FILE_LIST_SEPARATOR ' '
#endif

#ifndef HAVE_UNISTD_H
int getpid ();
#endif

#ifndef CONFIG_WITH_STRCACHE2

static unsigned long
dep_hash_1 (const void *key)
{
  const struct dep *d = key;
  return_STRING_HASH_1 (dep_name (d));
}

static unsigned long
dep_hash_2 (const void *key)
{
  const struct dep *d = key;
  return_STRING_HASH_2 (dep_name (d));
}

static int
dep_hash_cmp (const void *x, const void *y)
{
  const struct dep *dx = x;
  const struct dep *dy = y;
  return strcmp (dep_name (dx), dep_name (dy));
}


#else  /* CONFIG_WITH_STRCACHE2 */

/* Exploit the fact that all names are in the string cache. This means equal
   names shall have the same storage and there is no need for hashing or
   comparing. Use the address as the first hash, avoiding any touching of
   the name, and the length as the second. */

static unsigned long
dep_hash_1 (const void *key)
{
  const char *name = dep_name ((struct dep const *) key);
  assert (strcache2_is_cached (&file_strcache, name));
  return (size_t) name / sizeof(void *);
}

static unsigned long
dep_hash_2 (const void *key)
{
  const char *name = dep_name ((struct dep const *) key);
  return strcache2_get_len (&file_strcache, name);
}

static int
dep_hash_cmp (const void *x, const void *y)
{
  struct dep *dx = (struct dep *) x;
  struct dep *dy = (struct dep *) y;
  const char *dxname = dep_name (dx);
  const char *dyname = dep_name (dy);
  int cmp = dxname == dyname ? 0 : 1;

  /* check preconds: both cached and the cache contains no duplicates. */
  assert (strcache2_is_cached (&file_strcache, dxname));
  assert (strcache2_is_cached (&file_strcache, dyname));
  assert (cmp == 0 || strcmp (dxname, dyname) != 0);

  /* If the names are the same but ignore_mtimes are not equal, one of these
     is an order-only prerequisite and one isn't.  That means that we should
     remove the one that isn't and keep the one that is.  */

  if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
    dx->ignore_mtime = dy->ignore_mtime = 0;

  return cmp;
}

#endif /* CONFIG_WITH_STRCACHE2 */

#ifdef CONFIG_WITH_LAZY_DEPS_VARS
/* Create as copy of DEPS without duplicates, similar to what
   set_file_variables does.  Used by func_deps.  */

struct dep *create_uniqute_deps_chain (struct dep *deps)
{
  struct dep *d;
  struct dep *head = NULL;
  struct dep **ppnext= &head;
  struct hash_table dep_hash;
  void **slot;

  hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);

  for (d = deps; d != 0; d = d->next)
    {
      if (d->need_2nd_expansion)
        continue;

      slot = hash_find_slot (&dep_hash, d);
      if (HASH_VACANT (*slot))
        {
          struct dep *n = alloc_dep();
          *n = *d;
          n->next = NULL;
          *ppnext = n;
          ppnext = &n->next;
          hash_insert_at (&dep_hash, n, slot);
        }
      else
        {
          /* Upgrade order only if a normal dep exists.
             Note! Elected not to upgrade the original, only the sanitized
                   list, need to check that out later. FIXME TODO */
          struct dep *d2 = (struct dep *)*slot;
          if (d->ignore_mtime != d2->ignore_mtime)
            d->ignore_mtime = d2->ignore_mtime = 0;
        }
    }

  return head;
}
#endif /* CONFIG_WITH_LAZY_DEPS_VARS */

/* Set FILE's automatic variables up.  */

void
#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
set_file_variables (struct file *file, int called_early)
#else
set_file_variables (struct file *file)
#endif
{
  struct dep *d;
  const char *at, *percent, *star, *less;
#ifdef CONFIG_WITH_STRCACHE2
  const char *org_stem = file->stem;
#endif

#ifndef NO_ARCHIVES
  /* If the target is an archive member 'lib(member)',
     then $@ is 'lib' and $% is 'member'.  */

  if (ar_name (file->name))
    {
      unsigned int len;
      const char *cp;
      char *p;

      cp = strchr (file->name, '(');
      p = alloca (cp - file->name + 1);
      memcpy (p, file->name, cp - file->name);
      p[cp - file->name] = '\0';
      at = p;
      len = strlen (cp + 1);
      p = alloca (len);
      memcpy (p, cp + 1, len - 1);
      p[len - 1] = '\0';
      percent = p;
    }
  else
#endif  /* NO_ARCHIVES.  */
    {
      at = file->name;
      percent = "";
    }

  /* $* is the stem from an implicit or static pattern rule.  */
  if (file->stem == 0)
    {
      /* In Unix make, $* is set to the target name with
         any suffix in the .SUFFIXES list stripped off for
         explicit rules.  We store this in the 'stem' member.  */
      const char *name;
      unsigned int len;

#ifndef NO_ARCHIVES
      if (ar_name (file->name))
        {
          name = strchr (file->name, '(') + 1;
          len = strlen (name) - 1;
        }
      else
#endif
	{
          name = file->name;
#ifndef CONFIG_WITH_STRCACHE2
          len = strlen (name);
#else
          len = strcache2_get_len (&file_strcache, name);
#endif
        }

#ifndef CONFIG_WITH_STRCACHE2
      for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
        {
          unsigned int slen = strlen (dep_name (d));
#else
      for (d = enter_file (suffixes_strcached)->deps; d ; d = d->next)
        {
          unsigned int slen = strcache2_get_len (&file_strcache, dep_name (d));
#endif
          if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
            {
              file->stem = strcache_add_len (name, len - slen);
              break;
            }
        }
      if (d == 0)
        file->stem = "";
    }
  star = file->stem;

  /* $< is the first not order-only dependency.  */
  less = "";
  for (d = file->deps; d != 0; d = d->next)
    if (!d->ignore_mtime)
      {
        if (!d->need_2nd_expansion)
          less = dep_name (d);
        break;
      }

  if (file->cmds == default_file->cmds)
    /* This file got its commands from .DEFAULT.
       In this case $< is the same as $@.  */
    less = at;

#define DEFINE_VARIABLE(name, len, value) \
  (void) define_variable_for_file (name,len,value,o_automatic,0,file)

  /* Define the variables.  */

#ifndef CONFIG_WITH_RDONLY_VARIABLE_VALUE
  DEFINE_VARIABLE ("<", 1, less);
  DEFINE_VARIABLE ("*", 1, star);
  DEFINE_VARIABLE ("@", 1, at);
  DEFINE_VARIABLE ("%", 1, percent);
#else  /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
# define DEFINE_VARIABLE_RO_VAL(name, len, value, value_len) \
  define_variable_in_set((name), (len), (value), (value_len), -1, \
        (o_automatic), 0, (file)->variables->set, NILF)

  if (*less == '\0')
    DEFINE_VARIABLE_RO_VAL ("<", 1, "", 0);
  else if (less != at || at == file->name)
    DEFINE_VARIABLE_RO_VAL ("<", 1, less, strcache_get_len (less));
  else
    DEFINE_VARIABLE ("<", 1, less);

  if (*star == '\0')
    DEFINE_VARIABLE_RO_VAL ("*", 1, "", 0);
  else if (file->stem != org_stem)
    DEFINE_VARIABLE_RO_VAL ("*", 1, star, strcache_get_len (star));
  else
    DEFINE_VARIABLE ("*", 1, star);

  if (at == file->name)
    DEFINE_VARIABLE_RO_VAL ("@", 1, at, strcache_get_len (at));
  else
    DEFINE_VARIABLE ("@", 1, at);

  if (*percent == '\0')
    DEFINE_VARIABLE_RO_VAL ("%", 1, "", 0);
  else
    DEFINE_VARIABLE ("%", 1, percent);
#endif /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */

#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
  /* The $^, $+, $? and $| variables should not be set if we're called
     early by a .MUST_MAKE invocation or $(commands ).  */
  if (called_early)
    return;
#endif

  /* Compute the values for $^, $+, $?, and $|.  */
#ifdef CONFIG_WITH_LAZY_DEPS_VARS
  /* Lazy doesn't work for double colon rules with multiple files with
     commands, nor for files that has been thru rehash_file() (vpath).  */
  if (   (   file->double_colon
          && (   file->double_colon != file
             || file->last != file))
      || file->name != file->hname) /* XXX: Rehashed files should be fixable! */
#endif
  {
    static char *plus_value=0, *bar_value=0, *qmark_value=0;
    static unsigned int plus_max=0, bar_max=0, qmark_max=0;

    unsigned int qmark_len, plus_len, bar_len;
    char *cp;
    char *caret_value;
    char *qp;
    char *bp;
    unsigned int len;

    struct hash_table dep_hash;
    void **slot;

    /* Compute first the value for $+, which is supposed to contain
       duplicate dependencies as they were listed in the makefile.  */

    plus_len = 0;
    bar_len = 0;
    for (d = file->deps; d != 0; d = d->next)
      {
        if (!d->need_2nd_expansion)
          {
            if (d->ignore_mtime)
#ifndef CONFIG_WITH_STRCACHE2
              bar_len += strlen (dep_name (d)) + 1;
#else
              bar_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
#endif
            else
#ifndef CONFIG_WITH_STRCACHE2
              plus_len += strlen (dep_name (d)) + 1;
#else
              plus_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
#endif
          }
      }

    if (bar_len == 0)
      bar_len++;
    if (plus_len == 0)
      plus_len++;

    if (plus_len > plus_max)
      plus_value = xrealloc (plus_value, plus_max = plus_len);

    cp = plus_value;

    qmark_len = plus_len + 1;   /* Will be this or less.  */
    for (d = file->deps; d != 0; d = d->next)
      if (! d->ignore_mtime && ! d->need_2nd_expansion)
        {
          const char *c = dep_name (d);

#ifndef NO_ARCHIVES
          if (ar_name (c))
            {
              c = strchr (c, '(') + 1;
              len = strlen (c) - 1;
            }
          else
#endif
#ifndef CONFIG_WITH_STRCACHE2
            len = strlen (c);
#else
            len = strcache2_get_len (&file_strcache, c);
#endif

          memcpy (cp, c, len);
          cp += len;
          *cp++ = FILE_LIST_SEPARATOR;
          if (! (d->changed || always_make_flag))
            qmark_len -= len + 1;       /* Don't space in $? for this one.  */
        }

    /* Kill the last space and define the variable.  */

    cp[cp > plus_value ? -1 : 0] = '\0';
    DEFINE_VARIABLE ("+", 1, plus_value);

    /* Compute the values for $^, $?, and $|.  */

    cp = caret_value = plus_value; /* Reuse the buffer; it's big enough.  */

    if (qmark_len > qmark_max)
      qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
    qp = qmark_value;

    if (bar_len > bar_max)
      bar_value = xrealloc (bar_value, bar_max = bar_len);
    bp = bar_value;

    /* Make sure that no dependencies are repeated in $^, $?, and $|.  It
       would be natural to combine the next two loops but we can't do it
       because of a situation where we have two dep entries, the first
       is order-only and the second is normal (see below).  */

    hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);

    for (d = file->deps; d != 0; d = d->next)
      {
        if (d->need_2nd_expansion)
          continue;

        slot = hash_find_slot (&dep_hash, d);
        if (HASH_VACANT (*slot))
          hash_insert_at (&dep_hash, d, slot);
        else
          {
            /* Check if the two prerequisites have different ignore_mtime.
               If so then we need to "upgrade" one that is order-only.  */

            struct dep* hd = (struct dep*) *slot;

            if (d->ignore_mtime != hd->ignore_mtime)
              d->ignore_mtime = hd->ignore_mtime = 0;
          }
      }

    for (d = file->deps; d != 0; d = d->next)
      {
        const char *c;

        if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
          continue;

        c = dep_name (d);
#ifndef NO_ARCHIVES
        if (ar_name (c))
          {
            c = strchr (c, '(') + 1;
            len = strlen (c) - 1;
          }
        else
#endif
#ifndef CONFIG_WITH_STRCACHE2
          len = strlen (c);
#else
          len = strcache2_get_len (&file_strcache, c);
#endif

        if (d->ignore_mtime)
          {
            memcpy (bp, c, len);
            bp += len;
            *bp++ = FILE_LIST_SEPARATOR;
          }
        else
          {
            memcpy (cp, c, len);
            cp += len;
            *cp++ = FILE_LIST_SEPARATOR;
            if (d->changed || always_make_flag)
              {
                memcpy (qp, c, len);
                qp += len;
                *qp++ = FILE_LIST_SEPARATOR;
              }
          }
      }

    hash_free (&dep_hash, 0);

    /* Kill the last spaces and define the variables.  */

    cp[cp > caret_value ? -1 : 0] = '\0';
    DEFINE_VARIABLE ("^", 1, caret_value);

    qp[qp > qmark_value ? -1 : 0] = '\0';
    DEFINE_VARIABLE ("?", 1, qmark_value);

    bp[bp > bar_value ? -1 : 0] = '\0';
    DEFINE_VARIABLE ("|", 1, bar_value);
  }
#undef  DEFINE_VARIABLE
}

/* Chop CMDS up into individual command lines if necessary.
   Also set the 'lines_flags' and 'any_recurse' members.  */

void
chop_commands (struct commands *cmds)
{
  unsigned int nlines, idx;
  char **lines;

  /* If we don't have any commands,
     or we already parsed them, never mind.  */

  if (!cmds || cmds->command_lines != 0)
    return;

  /* Chop CMDS->commands up into lines in CMDS->command_lines.  */

  if (one_shell)
    {
      int l = strlen (cmds->commands);

      nlines = 1;
      lines = xmalloc (nlines * sizeof (char *));
      lines[0] = xstrdup (cmds->commands);

      /* Strip the trailing newline.  */
      if (l > 0 && lines[0][l-1] == '\n')
        lines[0][l-1] = '\0';
    }
  else
    {
      const char *p;

      nlines = 5;
      lines = xmalloc (nlines * sizeof (char *));
      idx = 0;
      p = cmds->commands;
      while (*p != '\0')
        {
          const char *end = p;
        find_end:;
          end = strchr (end, '\n');
          if (end == 0)
            end = p + strlen (p);
          else if (end > p && end[-1] == '\\')
            {
              int backslash = 1;
              const char *b;
              for (b = end - 2; b >= p && *b == '\\'; --b)
                backslash = !backslash;
              if (backslash)
                {
                  ++end;
                  goto find_end;
                }
            }

          if (idx == nlines)
            {
              nlines += 2;
              lines = xrealloc (lines, nlines * sizeof (char *));
            }
          lines[idx++] = xstrndup (p, end - p);
          p = end;
          if (*p != '\0')
            ++p;
        }

      if (idx != nlines)
        {
          nlines = idx;
          lines = xrealloc (lines, nlines * sizeof (char *));
        }
    }

  /* Finally, set the corresponding CMDS->lines_flags elements and the
     CMDS->any_recurse flag.  */

  if (nlines > USHRT_MAX)
    ON (fatal, &cmds->fileinfo, _("Recipe has too many lines (%ud)"), nlines);

  cmds->ncommand_lines = nlines;
  cmds->command_lines = lines;

  cmds->any_recurse = 0;
#ifndef CONFIG_WITH_COMMANDS_FUNC
  cmds->lines_flags = xmalloc (nlines);
#else
  cmds->lines_flags = xmalloc (nlines * sizeof (cmds->lines_flags[0]));
#endif

  for (idx = 0; idx < nlines; ++idx)
    {
      unsigned char flags = 0;
      const char *p = lines[idx];

      while (ISBLANK (*p) || *p == '-' || *p == '@' || *p == '+' IF_WITH_COMMANDS_FUNC(|| *p == '%'))
        switch (*(p++))
          {
          case '+':
            flags |= COMMANDS_RECURSE;
            break;
          case '@':
            flags |= COMMANDS_SILENT;
            break;
          case '-':
            flags |= COMMANDS_NOERROR;
            break;
#ifdef CONFIG_WITH_COMMANDS_FUNC
          case '%':
            flags |= COMMAND_GETTER_SKIP_IT;
            break;
#endif
          }

      /* If no explicit '+' was given, look for MAKE variable references.  */
      if (!(flags & COMMANDS_RECURSE)
#ifndef KMK
          && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
#else
          && (strstr (p, "$(KMK)") != 0 || strstr (p, "${KMK}") != 0 ||
              strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
#endif
        flags |= COMMANDS_RECURSE;

#ifdef CONFIG_WITH_KMK_BUILTIN
      /* check if kmk builtin command */
      if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
        flags |= COMMANDS_KMK_BUILTIN;
#endif

      cmds->lines_flags[idx] = flags;
      cmds->any_recurse |= flags & COMMANDS_RECURSE ? 1 : 0;
    }
}

#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
/* This is for saving memory in func_commands. */
void
free_chopped_commands (struct commands *cmds)
{
  if (   cmds
      && cmds->command_lines != 0
      && cmds->refs == 0)
    {
      unsigned idx = cmds->ncommand_lines;
      while (idx-- > 0)
        free (cmds->command_lines[idx]);
      free (cmds->command_lines);
      free (cmds->lines_flags);
      cmds->command_lines = 0;
      cmds->lines_flags = 0;
      cmds->ncommand_lines = 0;
    }
}

#endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
/* Execute the commands to remake FILE.  If they are currently executing,
   return or have already finished executing, just return.  Otherwise,
   fork off a child process to run the first command line in the sequence.  */

void
execute_file_commands (struct file *file)
{
  const char *p;

  /* Don't go through all the preparations if
     the commands are nothing but whitespace.  */

  for (p = file->cmds->commands; *p != '\0'; ++p)
    if (!ISSPACE (*p) && *p != '-' && *p != '@' && *p != '+')
      break;
  if (*p == '\0')
    {
      /* If there are no commands, assume everything worked.  */
#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
      file->command_flags |= COMMANDS_NO_COMMANDS;
#endif
      set_command_state (file, cs_running);
      file->update_status = us_success;
      notice_finished_file (file);
      return;
    }

  /* First set the automatic variables according to this file.  */

  initialize_file_variables (file, 0);

#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
  set_file_variables (file, 0 /* final call */);
#else
  set_file_variables (file);
#endif

  /* If this is a loaded dynamic object, unload it before remaking.
     Some systems don't support overwriting a loaded object.  */
  if (file->loaded)
    unload_file (file->name);

  /* Start the commands running.  */
  new_job (file);
}

/* This is set while we are inside fatal_error_signal,
   so things can avoid nonreentrant operations.  */

int handling_fatal_signal = 0;

/* Handle fatal signals.  */

RETSIGTYPE
fatal_error_signal (int sig)
{
#ifdef __MSDOS__
  extern int dos_status, dos_command_running;

  if (dos_command_running)
    {
      /* That was the child who got the signal, not us.  */
      dos_status |= (sig << 8);
      return;
    }
  remove_intermediates (1);
  exit (EXIT_FAILURE);
#else /* not __MSDOS__ */
#ifdef _AMIGA
  remove_intermediates (1);
  if (sig == SIGINT)
     fputs (_("*** Break.\n"), stderr);

  exit (10);
#else /* not Amiga */
#if defined (WINDOWS32) && !defined (CONFIG_NEW_WIN32_CTRL_EVENT)
  extern HANDLE main_thread;

  /* Windows creates a sperate thread for handling Ctrl+C, so we need
     to suspend the main thread, or else we will have race conditions
     when both threads call reap_children.  */
  if (main_thread)
    {
      DWORD susp_count = SuspendThread (main_thread);

      if (susp_count != 0)
        fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
      else if (susp_count == (DWORD)-1)
        {
          DWORD ierr = GetLastError ();

          fprintf (stderr, "SuspendThread: error %ld: %s\n",
                   ierr, map_windows32_error_to_string (ierr));
        }
    }
#endif
  handling_fatal_signal = 1;

  /* Set the handling for this signal to the default.
     It is blocked now while we run this handler.  */
  signal (sig, SIG_DFL);

  /* A termination signal won't be sent to the entire
     process group, but it means we want to kill the children.  */

  if (sig == SIGTERM)
    {
      struct child *c;
      for (c = children; c != 0; c = c->next)
        if (!c->remote)
# if defined (CONFIG_NEW_WIN_CHILDREN) && defined (WINDOWS32)
          MkWinChildKill (c->pid, SIGTERM, c);
# else
          (void) kill (c->pid, SIGTERM);
# endif
    }

  /* If we got a signal that means the user
     wanted to kill make, remove pending targets.  */

  if (sig == SIGTERM || sig == SIGINT
#ifdef SIGHUP
    || sig == SIGHUP
#endif
#ifdef SIGQUIT
    || sig == SIGQUIT
#endif
    )
    {
      struct child *c;

      /* Remote children won't automatically get signals sent
         to the process group, so we must send them.  */
      for (c = children; c != 0; c = c->next)
        if (c->remote)
          (void) remote_kill (c->pid, sig);

      for (c = children; c != 0; c = c->next)
        delete_child_targets (c);

      /* Clean up the children.  We don't just use the call below because
         we don't want to print the "Waiting for children" message.  */
      while (job_slots_used > 0)
        reap_children (1, 0);
    }
  else
    /* Wait for our children to die.  */
    while (job_slots_used > 0)
      reap_children (1, 1);

  /* Delete any non-precious intermediate files that were made.  */

  remove_intermediates (1);
#ifdef SIGQUIT
  if (sig == SIGQUIT)
    /* We don't want to send ourselves SIGQUIT, because it will
       cause a core dump.  Just exit instead.  */
    exit (MAKE_TROUBLE);
#endif

#ifdef WINDOWS32
# ifndef CONFIG_NEW_WIN32_CTRL_EVENT
  if (main_thread)
    CloseHandle (main_thread);
# endif /* !CONFIG_NEW_WIN32_CTRL_EVENT */
  /* Cannot call W32_kill with a pid (it needs a handle).  The exit
     status of 130 emulates what happens in Bash.  */
  exit (130);
#else
  /* Signal the same code; this time it will really be fatal.  The signal
     will be unblocked when we return and arrive then to kill us.  */
  if (kill (getpid (), sig) < 0)
    pfatal_with_name ("kill");
#endif /* not WINDOWS32 */
#endif /* not Amiga */
#endif /* not __MSDOS__  */
}

/* Delete FILE unless it's precious or not actually a file (phony),
   and it has changed on disk since we last stat'd it.  */

static void
delete_target (struct file *file, const char *on_behalf_of)
{
  struct stat st;
  int e;

  if (file->precious || file->phony)
    return;
#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
  assert (!file->multi_maybe);
#endif

#ifndef NO_ARCHIVES
  if (ar_name (file->name))
    {
      time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
                          ? (time_t) -1
                          : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
      if (ar_member_date (file->name) != file_date)
        {
          if (on_behalf_of)
            OSS (error, NILF,
                 _("*** [%s] Archive member '%s' may be bogus; not deleted"),
                 on_behalf_of, file->name);
          else
            OS (error, NILF,
                _("*** Archive member '%s' may be bogus; not deleted"),
                file->name);
        }
      return;
    }
#endif

  EINTRLOOP (e, stat (file->name, &st));
  if (e == 0
      && S_ISREG (st.st_mode)
      && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
    {
      if (on_behalf_of)
        OSS (error, NILF,
             _("*** [%s] Deleting file '%s'"), on_behalf_of, file->name);
      else
        OS (error, NILF, _("*** Deleting file '%s'"), file->name);
      if (unlink (file->name) < 0
          && errno != ENOENT)   /* It disappeared; so what.  */
        perror_with_name ("unlink: ", file->name);
    }
}


/* Delete all non-precious targets of CHILD unless they were already deleted.
   Set the flag in CHILD to say they've been deleted.  */

void
delete_child_targets (struct child *child)
{
  struct dep *d;

  if (child->deleted)
    return;

  /* Delete the target file if it changed.  */
  delete_target (child->file, NULL);

  /* Also remove any non-precious targets listed in the 'also_make' member.  */
  for (d = child->file->also_make; d != 0; d = d->next)
    delete_target (d->file, child->file->name);

#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
  /* Also remove any multi target siblings, except for the 'maybe' ones (we
     handle that here) and precious ones (delete_target deals with that).
     Note that CHILD is always the multi target head (see remake.c).  */
  if (child->file == child->file->multi_head)
    {
      struct file *f2;
      for (f2 = child->file->multi_next; f2; f2 = f2->multi_next)
        if (!f2->multi_maybe)
          delete_target (f2, child->file->name);
    }
#endif

  child->deleted = 1;
}

/* Print out the commands in CMDS.  */

void
print_commands (const struct commands *cmds)
{
  const char *s;

  fputs (_("#  recipe to execute"), stdout);

  if (cmds->fileinfo.filenm == 0)
    puts (_(" (built-in):"));
  else
    printf (_(" (from '%s', line %lu):\n"),
            cmds->fileinfo.filenm, cmds->fileinfo.lineno);

  s = cmds->commands;
  while (*s != '\0')
    {
      const char *end;
      int bs;

      /* Print one full logical recipe line: find a non-escaped newline.  */
      for (end = s, bs = 0; *end != '\0'; ++end)
        {
          if (*end == '\n' && !bs)
            break;

          bs = *end == '\\' ? !bs : 0;
        }

      printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);

      s = end + (end[0] == '\n');
    }
}