~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/3rdparty/qmake/commands.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Command processing for GNU Make.
 
2
Copyright (C) 1988,89,91,92,93,94,95,96,97 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
 
6
it under the terms of the GNU General Public License as published by
 
7
the Free Software Foundation; either version 2, or (at your option)
 
8
any later version.
 
9
 
 
10
GNU Make is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with GNU Make; see the file COPYING.  If not, write to
 
17
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
Boston, MA 02111-1307, USA.  */
 
19
 
 
20
#include "make.h"
 
21
#include "dep.h"
 
22
#include "filedef.h"
 
23
#include "variable.h"
 
24
#include "job.h"
 
25
#include "commands.h"
 
26
 
 
27
extern int remote_kill PARAMS ((int id, int sig));
 
28
 
 
29
#ifndef HAVE_UNISTD_H
 
30
extern int getpid ();
 
31
#endif
 
32
 
 
33
/* Set FILE's automatic variables up.  */
 
34
 
 
35
static void
 
36
set_file_variables (file)
 
37
     register struct file *file;
 
38
{
 
39
  register char *p;
 
40
  char *at, *percent, *star, *less;
 
41
 
 
42
#ifndef NO_ARCHIVES
 
43
  /* If the target is an archive member `lib(member)',
 
44
     then $@ is `lib' and $% is `member'.  */
 
45
 
 
46
  if (ar_name (file->name))
 
47
    {
 
48
      unsigned int len;
 
49
      p = index (file->name, '(');
 
50
      at = (char *) alloca (p - file->name + 1);
 
51
      bcopy (file->name, at, p - file->name);
 
52
      at[p - file->name] = '\0';
 
53
      len = strlen (p + 1);
 
54
      percent = (char *) alloca (len);
 
55
      bcopy (p + 1, percent, len - 1);
 
56
      percent[len - 1] = '\0';
 
57
    }
 
58
  else
 
59
#endif  /* NO_ARCHIVES.  */
 
60
    {
 
61
      at = file->name;
 
62
      percent = "";
 
63
    }
 
64
 
 
65
  /* $* is the stem from an implicit or static pattern rule.  */
 
66
  if (file->stem == 0)
 
67
    {
 
68
      /* In Unix make, $* is set to the target name with
 
69
         any suffix in the .SUFFIXES list stripped off for
 
70
         explicit rules.  We store this in the `stem' member.  */
 
71
      register struct dep *d;
 
72
      char *name;
 
73
      unsigned int len;
 
74
 
 
75
#ifndef NO_ARCHIVES
 
76
      if (ar_name (file->name))
 
77
        {
 
78
          name = index (file->name, '(') + 1;
 
79
          len = strlen (name) - 1;
 
80
        }
 
81
      else
 
82
#endif
 
83
        {
 
84
          name = file->name;
 
85
          len = strlen (name);
 
86
        }
 
87
 
 
88
      for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
 
89
        {
 
90
          unsigned int slen = strlen (dep_name (d));
 
91
          if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
 
92
            {
 
93
              file->stem = savestring (name, len - slen);
 
94
              break;
 
95
            }
 
96
        }
 
97
      if (d == 0)
 
98
        file->stem = "";
 
99
    }
 
100
  star = file->stem;
 
101
 
 
102
  /* $< is the first dependency.  */
 
103
  less = file->deps != 0 ? dep_name (file->deps) : "";
 
104
 
 
105
  if (file->cmds == default_file->cmds)
 
106
    /* This file got its commands from .DEFAULT.
 
107
       In this case $< is the same as $@.  */
 
108
    less = at;
 
109
 
 
110
#define DEFINE_VARIABLE(name, len, value) \
 
111
  (void) define_variable_for_file (name, len, value, o_automatic, 0, file)
 
112
 
 
113
  /* Define the variables.  */
 
114
 
 
115
  DEFINE_VARIABLE ("<", 1, less);
 
116
  DEFINE_VARIABLE ("*", 1, star);
 
117
  DEFINE_VARIABLE ("@", 1, at);
 
118
  DEFINE_VARIABLE ("%", 1, percent);
 
119
 
 
120
  /* Compute the values for $^, $+, and $?.  */
 
121
 
 
122
  {
 
123
    register unsigned int qmark_len, plus_len;
 
124
    char *caret_value, *plus_value;
 
125
    register char *cp;
 
126
    char *qmark_value;
 
127
    register char *qp;
 
128
    register struct dep *d;
 
129
    unsigned int len;
 
130
 
 
131
    /* Compute first the value for $+, which is supposed to contain
 
132
       duplicate dependencies as they were listed in the makefile.  */
 
133
 
 
134
    plus_len = 0;
 
135
    for (d = file->deps; d != 0; d = d->next)
 
136
      plus_len += strlen (dep_name (d)) + 1;
 
137
 
 
138
    len = plus_len == 0 ? 1 : plus_len;
 
139
    cp = plus_value = (char *) alloca (len);
 
140
 
 
141
    qmark_len = plus_len;       /* Will be this or less.  */
 
142
    for (d = file->deps; d != 0; d = d->next)
 
143
      {
 
144
        char *c = dep_name (d);
 
145
 
 
146
#ifndef NO_ARCHIVES
 
147
        if (ar_name (c))
 
148
          {
 
149
            c = index (c, '(') + 1;
 
150
            len = strlen (c) - 1;
 
151
          }
 
152
        else
 
153
#endif
 
154
          len = strlen (c);
 
155
 
 
156
        bcopy (c, cp, len);
 
157
        cp += len;
 
158
#if VMS
 
159
        *cp++ = ',';
 
160
#else
 
161
        *cp++ = ' ';
 
162
#endif
 
163
        if (! d->changed)
 
164
          qmark_len -= len + 1; /* Don't space in $? for this one.  */
 
165
      }
 
166
 
 
167
    /* Kill the last space and define the variable.  */
 
168
 
 
169
    cp[cp > plus_value ? -1 : 0] = '\0';
 
170
    DEFINE_VARIABLE ("+", 1, plus_value);
 
171
 
 
172
    /* Make sure that no dependencies are repeated.  This does not
 
173
       really matter for the purpose of updating targets, but it
 
174
       might make some names be listed twice for $^ and $?.  */
 
175
 
 
176
    uniquize_deps (file->deps);
 
177
 
 
178
    /* Compute the values for $^ and $?.  */
 
179
 
 
180
    cp = caret_value = plus_value; /* Reuse the buffer; it's big enough.  */
 
181
    len = qmark_len == 0 ? 1 : qmark_len;
 
182
    qp = qmark_value = (char *) alloca (len);
 
183
 
 
184
    for (d = file->deps; d != 0; d = d->next)
 
185
      {
 
186
        char *c = dep_name (d);
 
187
 
 
188
#ifndef NO_ARCHIVES
 
189
        if (ar_name (c))
 
190
          {
 
191
            c = index (c, '(') + 1;
 
192
            len = strlen (c) - 1;
 
193
          }
 
194
        else
 
195
#endif
 
196
          len = strlen (c);
 
197
 
 
198
        bcopy (c, cp, len);
 
199
        cp += len;
 
200
#if VMS
 
201
        *cp++ = ',';
 
202
#else
 
203
        *cp++ = ' ';
 
204
#endif
 
205
        if (d->changed)
 
206
          {
 
207
            bcopy (c, qp, len);
 
208
            qp += len;
 
209
#if VMS
 
210
            *qp++ = ',';
 
211
#else
 
212
            *qp++ = ' ';
 
213
#endif
 
214
          }
 
215
      }
 
216
 
 
217
    /* Kill the last spaces and define the variables.  */
 
218
 
 
219
    cp[cp > caret_value ? -1 : 0] = '\0';
 
220
    DEFINE_VARIABLE ("^", 1, caret_value);
 
221
 
 
222
    qp[qp > qmark_value ? -1 : 0] = '\0';
 
223
    DEFINE_VARIABLE ("?", 1, qmark_value);
 
224
  }
 
225
 
 
226
#undef  DEFINE_VARIABLE
 
227
}
 
228
 
 
229
/* Chop CMDS up into individual command lines if necessary.
 
230
   Also set the `lines_flag' and `any_recurse' members.  */
 
231
 
 
232
void
 
233
chop_commands (cmds)
 
234
     register struct commands *cmds;
 
235
{
 
236
  if (cmds != 0 && cmds->command_lines == 0)
 
237
    {
 
238
      /* Chop CMDS->commands up into lines in CMDS->command_lines.
 
239
         Also set the corresponding CMDS->lines_flags elements,
 
240
         and the CMDS->any_recurse flag.  */
 
241
      register char *p;
 
242
      unsigned int nlines, idx;
 
243
      char **lines;
 
244
 
 
245
      nlines = 5;
 
246
      lines = (char **) xmalloc (5 * sizeof (char *));
 
247
      idx = 0;
 
248
      p = cmds->commands;
 
249
      while (*p != '\0')
 
250
        {
 
251
          char *end = p;
 
252
        find_end:;
 
253
          end = index (end, '\n');
 
254
          if (end == 0)
 
255
            end = p + strlen (p);
 
256
          else if (end > p && end[-1] == '\\')
 
257
            {
 
258
              int backslash = 1;
 
259
              register char *b;
 
260
              for (b = end - 2; b >= p && *b == '\\'; --b)
 
261
                backslash = !backslash;
 
262
              if (backslash)
 
263
                {
 
264
                  ++end;
 
265
                  goto find_end;
 
266
                }
 
267
            }
 
268
 
 
269
          if (idx == nlines)
 
270
            {
 
271
              nlines += 2;
 
272
              lines = (char **) xrealloc ((char *) lines,
 
273
                                          nlines * sizeof (char *));
 
274
            }
 
275
          lines[idx++] = savestring (p, end - p);
 
276
          p = end;
 
277
          if (*p != '\0')
 
278
            ++p;
 
279
        }
 
280
 
 
281
      if (idx != nlines)
 
282
        {
 
283
          nlines = idx;
 
284
          lines = (char **) xrealloc ((char *) lines,
 
285
                                      nlines * sizeof (char *));
 
286
        }
 
287
 
 
288
      cmds->ncommand_lines = nlines;
 
289
      cmds->command_lines = lines;
 
290
 
 
291
      cmds->any_recurse = 0;
 
292
      cmds->lines_flags = (char *) xmalloc (nlines);
 
293
      for (idx = 0; idx < nlines; ++idx)
 
294
        {
 
295
          int flags = 0;
 
296
 
 
297
          for (p = lines[idx];
 
298
               isblank (*p) || *p == '-' || *p == '@' || *p == '+';
 
299
               ++p)
 
300
            switch (*p)
 
301
              {
 
302
              case '+':
 
303
                flags |= COMMANDS_RECURSE;
 
304
                break;
 
305
              case '@':
 
306
                flags |= COMMANDS_SILENT;
 
307
                break;
 
308
              case '-':
 
309
                flags |= COMMANDS_NOERROR;
 
310
                break;
 
311
              }
 
312
          if (!(flags & COMMANDS_RECURSE))
 
313
            {
 
314
              unsigned int len = strlen (p);
 
315
              if (sindex (p, len, "$(MAKE)", 7) != 0
 
316
                  || sindex (p, len, "${MAKE}", 7) != 0)
 
317
                flags |= COMMANDS_RECURSE;
 
318
            }
 
319
 
 
320
          cmds->lines_flags[idx] = flags;
 
321
          cmds->any_recurse |= flags & COMMANDS_RECURSE;
 
322
        }
 
323
    }
 
324
}
 
325
 
 
326
/* Execute the commands to remake FILE.  If they are currently executing,
 
327
   return or have already finished executing, just return.  Otherwise,
 
328
   fork off a child process to run the first command line in the sequence.  */
 
329
 
 
330
void
 
331
execute_file_commands (file)
 
332
     struct file *file;
 
333
{
 
334
  register char *p;
 
335
 
 
336
  /* Don't go through all the preparations if
 
337
     the commands are nothing but whitespace.  */
 
338
 
 
339
  for (p = file->cmds->commands; *p != '\0'; ++p)
 
340
    if (!isspace (*p) && *p != '-' && *p != '@')
 
341
      break;
 
342
  if (*p == '\0')
 
343
    {
 
344
      /* We are all out of commands.
 
345
         If we have gotten this far, all the previous commands
 
346
         have run successfully, so we have winning update status.  */
 
347
      set_command_state (file, cs_running);
 
348
      file->update_status = 0;
 
349
      notice_finished_file (file);
 
350
      return;
 
351
    }
 
352
 
 
353
  /* First set the automatic variables according to this file.  */
 
354
 
 
355
  initialize_file_variables (file);
 
356
 
 
357
  set_file_variables (file);
 
358
 
 
359
  /* Start the commands running.  */
 
360
  new_job (file);
 
361
}
 
362
 
 
363
/* This is set while we are inside fatal_error_signal,
 
364
   so things can avoid nonreentrant operations.  */
 
365
 
 
366
int handling_fatal_signal = 0;
 
367
 
 
368
/* Handle fatal signals.  */
 
369
 
 
370
RETSIGTYPE
 
371
fatal_error_signal (sig)
 
372
     int sig;
 
373
{
 
374
#ifdef __MSDOS__
 
375
  extern int dos_status, dos_command_running;
 
376
 
 
377
  if (dos_command_running)
 
378
    {
 
379
      /* That was the child who got the signal, not us.  */
 
380
      dos_status |= (sig << 8);
 
381
      return;
 
382
    }
 
383
  remove_intermediates (1);
 
384
  exit (EXIT_FAILURE);
 
385
#else /* not __MSDOS__ */
 
386
#ifdef _AMIGA
 
387
  remove_intermediates (1);
 
388
  if (sig == SIGINT)
 
389
     fputs (_("*** Break.\n"), stderr);
 
390
 
 
391
  exit (10);
 
392
#else /* not Amiga */
 
393
  handling_fatal_signal = 1;
 
394
 
 
395
  /* Set the handling for this signal to the default.
 
396
     It is blocked now while we run this handler.  */
 
397
  signal (sig, SIG_DFL);
 
398
 
 
399
  /* A termination signal won't be sent to the entire
 
400
     process group, but it means we want to kill the children.  */
 
401
 
 
402
  if (sig == SIGTERM)
 
403
    {
 
404
      register struct child *c;
 
405
      for (c = children; c != 0; c = c->next)
 
406
        if (!c->remote)
 
407
          (void) kill (c->pid, SIGTERM);
 
408
    }
 
409
 
 
410
  /* If we got a signal that means the user
 
411
     wanted to kill make, remove pending targets.  */
 
412
 
 
413
  if (sig == SIGTERM || sig == SIGINT
 
414
#ifdef SIGHUP
 
415
    || sig == SIGHUP
 
416
#endif
 
417
#ifdef SIGQUIT
 
418
    || sig == SIGQUIT
 
419
#endif
 
420
    )
 
421
    {
 
422
      register struct child *c;
 
423
 
 
424
      /* Remote children won't automatically get signals sent
 
425
         to the process group, so we must send them.  */
 
426
      for (c = children; c != 0; c = c->next)
 
427
        if (c->remote)
 
428
          (void) remote_kill (c->pid, sig);
 
429
 
 
430
      for (c = children; c != 0; c = c->next)
 
431
        delete_child_targets (c);
 
432
 
 
433
      /* Clean up the children.  We don't just use the call below because
 
434
         we don't want to print the "Waiting for children" message.  */
 
435
      while (job_slots_used > 0)
 
436
        reap_children (1, 0);
 
437
    }
 
438
  else
 
439
    /* Wait for our children to die.  */
 
440
    while (job_slots_used > 0)
 
441
      reap_children (1, 1);
 
442
 
 
443
  /* Delete any non-precious intermediate files that were made.  */
 
444
 
 
445
  remove_intermediates (1);
 
446
 
 
447
#ifdef SIGQUIT
 
448
  if (sig == SIGQUIT)
 
449
    /* We don't want to send ourselves SIGQUIT, because it will
 
450
       cause a core dump.  Just exit instead.  */
 
451
    exit (EXIT_FAILURE);
 
452
#endif
 
453
 
 
454
  /* Signal the same code; this time it will really be fatal.  The signal
 
455
     will be unblocked when we return and arrive then to kill us.  */
 
456
  if (kill (getpid (), sig) < 0)
 
457
    pfatal_with_name ("kill");
 
458
#endif /* not Amiga */
 
459
#endif /* not __MSDOS__  */
 
460
}
 
461
 
 
462
/* Delete FILE unless it's precious or not actually a file (phony),
 
463
   and it has changed on disk since we last stat'd it.  */
 
464
 
 
465
static void
 
466
delete_target (file, on_behalf_of)
 
467
     struct file *file;
 
468
     char *on_behalf_of;
 
469
{
 
470
  struct stat st;
 
471
 
 
472
  if (file->precious || file->phony)
 
473
    return;
 
474
 
 
475
#ifndef NO_ARCHIVES
 
476
  if (ar_name (file->name))
 
477
    {
 
478
      if (ar_member_date (file->name) != FILE_TIMESTAMP_S (file->last_mtime))
 
479
        {
 
480
          if (on_behalf_of)
 
481
            error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
 
482
                   on_behalf_of, file->name);
 
483
          else
 
484
            error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
 
485
                   file->name);
 
486
        }
 
487
      return;
 
488
    }
 
489
#endif
 
490
 
 
491
  if (stat (file->name, &st) == 0
 
492
      && S_ISREG (st.st_mode)
 
493
      && FILE_TIMESTAMP_STAT_MODTIME (st) != file->last_mtime)
 
494
    {
 
495
      if (on_behalf_of)
 
496
        error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
 
497
      else
 
498
        error (NILF, _("*** Deleting file `%s'"), file->name);
 
499
      if (unlink (file->name) < 0
 
500
          && errno != ENOENT)   /* It disappeared; so what.  */
 
501
        perror_with_name ("unlink: ", file->name);
 
502
    }
 
503
}
 
504
 
 
505
 
 
506
/* Delete all non-precious targets of CHILD unless they were already deleted.
 
507
   Set the flag in CHILD to say they've been deleted.  */
 
508
 
 
509
void
 
510
delete_child_targets (child)
 
511
     struct child *child;
 
512
{
 
513
  struct dep *d;
 
514
 
 
515
  if (child->deleted)
 
516
    return;
 
517
 
 
518
  /* Delete the target file if it changed.  */
 
519
  delete_target (child->file, (char *) 0);
 
520
 
 
521
  /* Also remove any non-precious targets listed in the `also_make' member.  */
 
522
  for (d = child->file->also_make; d != 0; d = d->next)
 
523
    delete_target (d->file, child->file->name);
 
524
 
 
525
  child->deleted = 1;
 
526
}
 
527
 
 
528
/* Print out the commands in CMDS.  */
 
529
 
 
530
void
 
531
print_commands (cmds)
 
532
     register struct commands *cmds;
 
533
{
 
534
  register char *s;
 
535
 
 
536
  fputs (_("#  commands to execute"), stdout);
 
537
 
 
538
  if (cmds->fileinfo.filenm == 0)
 
539
    puts (_(" (built-in):"));
 
540
  else
 
541
    printf (_(" (from `%s', line %lu):\n"),
 
542
            cmds->fileinfo.filenm, cmds->fileinfo.lineno);
 
543
 
 
544
  s = cmds->commands;
 
545
  while (*s != '\0')
 
546
    {
 
547
      char *end;
 
548
 
 
549
      while (isspace (*s))
 
550
        ++s;
 
551
 
 
552
      end = index (s, '\n');
 
553
      if (end == 0)
 
554
        end = s + strlen (s);
 
555
 
 
556
      printf ("\t%.*s\n", (int) (end - s), s);
 
557
 
 
558
      s = end;
 
559
    }
 
560
}