home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / GNUSRC.Z / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  12.6 KB  |  533 lines

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