home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / make-3.71-src.lha / src / amiga / make-3.71 / remake.c < prev    next >
C/C++ Source or Header  |  1994-06-22  |  27KB  |  1,056 lines

  1. /* Basic dependency engine for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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 "commands.h"
  21. #include "job.h"
  22. #include "dep.h"
  23. #include "file.h"
  24.  
  25. #ifdef HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #else
  28. #include <sys/file.h>
  29. #endif
  30.  
  31. extern int try_implicit_rule ();
  32.  
  33.  
  34. /* Incremented when a command is started (under -n, when one would be).  */
  35. unsigned int commands_started = 0;
  36.  
  37. static int update_file (), update_file_1 (), check_dep (), touch_file ();
  38. static void remake_file ();
  39. static time_t name_mtime ();
  40. static int library_search ();
  41. extern time_t f_mtime ();
  42.  
  43. /* Remake all the goals in the `struct dep' chain GOALS.  Return -1 if nothing
  44.    was done, 0 if all goals were updated successfully, or 1 if a goal failed.
  45.    If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
  46.    be disabled for them unless they were also command-line targets, and we
  47.    should only make one goal at a time and return as soon as one goal whose
  48.    `changed' member is nonzero is successfully made.  */
  49.  
  50. int
  51. update_goal_chain (goals, makefiles)
  52.      register struct dep *goals;
  53.      int makefiles;
  54. {
  55.   int t = touch_flag, q = question_flag, n = just_print_flag;
  56.   unsigned int j = job_slots;
  57.   int status = -1;
  58.  
  59. #define    MTIME(file) (makefiles ? file_mtime_no_search (file) \
  60.              : file_mtime (file))
  61.  
  62.   /* Duplicate the chain so we can remove things from it.  */
  63.  
  64.   goals = copy_dep_chain (goals);
  65.  
  66.   {
  67.     /* Clear the `changed' flag of each goal in the chain.
  68.        We will use the flag below to notice when any commands
  69.        have actually been run for a target.  When no commands
  70.        have been run, we give an "up to date" diagnostic.  */
  71.  
  72.     struct dep *g;
  73.     for (g = goals; g != 0; g = g->next)
  74.       g->changed = 0;
  75.   }
  76.  
  77.   if (makefiles)
  78.     /* Only run one job at a time.  */
  79.     job_slots = 1;
  80.  
  81.   /* Update all the goals until they are all finished.  */
  82.  
  83.   while (goals != 0)
  84.     {
  85.       register struct dep *g, *lastgoal;
  86.  
  87.       /* Start jobs that are waiting for the load to go down.  */
  88.  
  89.       start_waiting_jobs ();
  90.  
  91.       /* Wait for a child to die.  */
  92.  
  93.       reap_children (1, 0);
  94.  
  95.       lastgoal = 0;
  96.       g = goals;
  97.       while (g != 0)
  98.     {
  99.       /* Iterate over all double-colon entries for this file.  */
  100.       struct file *file = g->file;
  101.       int stop, any_not_updated = 0;
  102.       for (file = g->file->double_colon ? g->file->double_colon : g->file;
  103.            file != NULL;
  104.            file = file->prev)
  105.         {
  106.           unsigned int ocommands_started;
  107.           int x;
  108.           time_t mtime = MTIME (file);
  109.           check_renamed (file);
  110.  
  111.           if (makefiles)
  112.         {
  113.           if (file->cmd_target)
  114.             {
  115.               touch_flag = t;
  116.               question_flag = q;
  117.               just_print_flag = n;
  118.             }
  119.           else
  120.             touch_flag = question_flag = just_print_flag = 0;
  121.         }
  122.  
  123.           /* Save the old value of `commands_started' so we can compare
  124.          later.  It will be incremented when any commands are
  125.          actually run.  */
  126.           ocommands_started = commands_started;
  127.  
  128.           x = update_file (file, makefiles ? 1 : 0);
  129.           check_renamed (file);
  130.  
  131.           /* Set the goal's `changed' flag if any commands were started
  132.          by calling update_file above.  We check this flag below to
  133.          decide when to give an "up to date" diagnostic.  */
  134.           g->changed += commands_started - ocommands_started;
  135.         
  136.           stop = 0;
  137.           if (x != 0 || file->updated)
  138.         {
  139.           /* If STATUS was not already 1, set it to 1 if
  140.              updating failed, or to 0 if updating succeeded.
  141.              Leave STATUS as it is if no updating was done.  */
  142.  
  143.           if (status < 1)
  144.             {
  145.               if (file->update_status != 0)
  146.             {
  147.               /* Updating failed, or -q triggered.
  148.                  The STATUS value tells our caller which.  */
  149.               status = file->update_status;
  150.               /* If -q just triggered, stop immediately.
  151.                  It doesn't matter how much more we run,
  152.                  since we already know the answer to return.  */
  153.               stop = (!keep_going_flag && !question_flag
  154.                   && !makefiles);
  155.             }
  156.               else if (MTIME (file) != mtime)
  157.             {
  158.               /* Updating was done.  If this is a makefile and
  159.                  just_print_flag or question_flag is set
  160.                  (meaning -n or -q was given and this file was
  161.                  specified as a command-line target), don't
  162.                  change STATUS.  If STATUS is changed, we will
  163.                  get re-exec'd, and fall into an infinite loop.  */
  164.               if (!makefiles
  165.                   || (!just_print_flag && !question_flag))
  166.                 status = 0;
  167.               if (makefiles && file->dontcare)
  168.                 /* This is a default makefile.  Stop remaking.  */
  169.                 stop = 1;
  170.             }
  171.             }
  172.         }
  173.  
  174.           /* Keep track if any double-colon entry is not finished.
  175.                  When they are all finished, the goal is finished.  */
  176.           any_not_updated |= !file->updated;
  177.  
  178.           if (stop)
  179.         break;
  180.         }
  181.  
  182.       /* Reset FILE since it is null at the end of the loop.  */
  183.       file = g->file;
  184.  
  185.       if (stop || !any_not_updated)
  186.         {
  187.           /* If we have found nothing whatever to do for the goal,
  188.          print a message saying nothing needs doing.  */
  189.  
  190.           if (!makefiles
  191.           /* If the update_status is zero, we updated successfully
  192.              or not at all.  G->changed will have been set above if
  193.              any commands were actually started for this goal.  */
  194.           && file->update_status == 0 && !g->changed
  195.           /* Never give a message under -s or -q.  */
  196.           && !silent_flag && !question_flag)
  197.         {
  198.           if (file->phony || file->cmds == 0)
  199.             message ("Nothing to be done for `%s'.",
  200.                  file->name);
  201.           else
  202.             message ("`%s' is up to date.", file->name);
  203.           fflush (stdout);
  204.         }
  205.  
  206.           /* This goal is finished.  Remove it from the chain.  */
  207.           if (lastgoal == 0)
  208.         goals = g->next;
  209.           else
  210.         lastgoal->next = g->next;
  211.  
  212.           /* Free the storage.  */
  213.           free ((char *) g);
  214.  
  215.           g = lastgoal == 0 ? goals : lastgoal->next;
  216.  
  217.           if (stop)
  218.         break;
  219.         }
  220.       else
  221.         {
  222.           lastgoal = g;
  223.           g = g->next;
  224.         }
  225.     }
  226.     }
  227.  
  228.   if (makefiles)
  229.     {
  230.       touch_flag = t;
  231.       question_flag = q;
  232.       just_print_flag = n;
  233.       job_slots = j;
  234.     }
  235.  
  236.   return status;
  237. }
  238.  
  239. /* If FILE is not up to date, execute the commands for it.
  240.    Return 0 if successful, 1 if unsuccessful;
  241.    but with some flag settings, just call `exit' if unsuccessful.
  242.  
  243.    DEPTH is the depth in recursions of this function.
  244.    We increment it during the consideration of our dependencies,
  245.    then decrement it again after finding out whether this file
  246.    is out of date.
  247.  
  248.    If there are multiple double-colon entries for FILE,
  249.    each is considered in turn.  */
  250.  
  251. static int
  252. update_file (file, depth)
  253.      struct file *file;
  254.      unsigned int depth;
  255. {
  256.   register int status = 0;
  257.   register struct file *f;
  258.  
  259.   for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
  260.     {
  261.       status |= update_file_1 (f, depth);
  262.       check_renamed (f);
  263.  
  264.       if (status != 0 && !keep_going_flag)
  265.     return status;
  266.  
  267.       switch (f->command_state)
  268.     {
  269.     case cs_finished:
  270.       /* The file is done being remade.  */
  271.       break;
  272.  
  273.     case cs_running:
  274.     case cs_deps_running:
  275.       /* Don't run the other :: rules for this
  276.          file until this rule is finished.  */
  277.       return 0;
  278.  
  279.     default:
  280.       error ("internal error: `%s' command_state == %d in update_file",
  281.          f->name, (int) f->command_state);
  282.       abort ();
  283.       break;
  284.     }
  285.     }
  286.  
  287.   return status;
  288. }
  289.  
  290. /* Consider a single `struct file' and update it as appropriate.  */
  291.  
  292. static int
  293. update_file_1 (file, depth)
  294.      struct file *file;
  295.      unsigned int depth;
  296. {
  297.   register time_t t