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

  1. /* Basic dependency engine for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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. #include <assert.h>
  25.  
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #else
  29. #include <sys/file.h>
  30. #endif
  31.  
  32. extern int try_implicit_rule ();
  33.  
  34.  
  35. /* Incremented when a command is started (under -n, when one would be).  */
  36. unsigned int commands_started = 0;
  37.  
  38. static int update_file (), update_file_1 (), check_dep (), touch_file ();
  39. static void remake_file ();
  40. static time_t name_mtime ();
  41. static int library_search ();
  42. extern time_t f_mtime ();
  43.  
  44. /* Remake all the goals in the `struct dep' chain GOALS.  Return -1 if nothing
  45.    was done, 0 if all goals were updated successfully, or 1 if a goal failed.
  46.    If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
  47.    be disabled for them unless they were also command-line targets, and we
  48.    should only make one goal at a time and return as soon as one goal whose
  49.    `changed' member is nonzero is successfully made.  */
  50.  
  51. int
  52. update_goal_chain (goals, makefiles)
  53.      register struct dep *goals;
  54.      int makefiles;
  55. {
  56.   int t = touch_flag, q = question_flag, n = just_print_flag;
  57.   unsigned int j = job_slots;
  58.   int status = -1;
  59.  
  60. #define    MTIME(file) (makefiles ? file_mtime_no_search (file) \
  61.              : file_mtime (file))
  62.  
  63.   /* Duplicate the chain so we can remove things from it.  */
  64.  
  65.   goals = copy_dep_chain (goals);
  66.  
  67.   {
  68.     /* Clear the `changed' flag of each goal in the chain.
  69.        We will use the flag below to notice when any commands
  70.        have actually been run for a target.  When no commands
  71.        have been run, we give an "up to date" diagnostic.  */
  72.  
  73.     struct dep *g;
  74.     for (g = goals; g != 0; g = g->next)
  75.       g->changed = 0;
  76.   }
  77.  
  78.   if (makefiles)
  79.     /* Only run one job at a time.  */
  80.     job_slots = 1;
  81.  
  82.   /* Update all the goals until they are all finished.  */
  83.  
  84.   while (goals != 0)
  85.     {
  86.       register struct dep *g, *lastgoal;
  87.  
  88.       /* Start jobs that are waiting for the load to go down.  */
  89.  
  90.       start_waiting_jobs ();
  91.  
  92.       /* Wait for a child to die.  */
  93.  
  94.       reap_children (1, 0);
  95.  
  96.       lastgoal = 0;
  97.       g = goals;
  98.       while (g != 0)
  99.     {
  100.       /* Iterate over all double-colon entries for this file.  */
  101.       struct file *file = g->file;
  102.       int stop, any_not_updated = 0;
  103.       for (file = g->file->double_colon ? g->file->double_colon : g->file;
  104.            file != NULL;
  105.            file = file->prev)
  106.         {
  107.           unsigned int ocommands_started;
  108.           int x;
  109.           time_t mtime = MTIME (file);
  110.           check_renamed (file);
  111.  
  112.           if (makefiles)
  113.         {
  114.           if (file->cmd_target)
  115.             {
  116.               touch_flag = t;
  117.               question_flag = q;
  118.               just_print_flag = n;
  119.             }
  120.           else
  121.             touch_flag = question_flag = just_print_flag = 0;
  122.         }
  123.  
  124.           /* Save the old value of `commands_started' so we can compare
  125.          later.  It will be incremented when any commands are
  126.          actually run.  */
  127.           ocommands_started = commands_started;
  128.  
  129.           x = update_file (file, makefiles ? 1 : 0);
  130.           check_renamed (file);
  131.  
  132.           /* Set the goal's `changed' flag if any commands were started
  133.          by calling update_file above.  We check this flag below to
  134.          decide when to give an "up to date" diagnostic.  */
  135.           g->changed += commands_started - ocommands_started;
  136.         
  137.           stop = 0;
  138.           if (x != 0 || file->updated)
  139.         {
  140.           /* If STATUS was not already 1, set it to 1 if
  141.              updating failed, or to 0 if updating succeeded.
  142.              Leave STATUS as it is if no updating was done.  */
  143.  
  144.           if (status < 1)
  145.             {
  146.               if (file->update_status != 0)
  147.             {
  148.               /* Updating failed, or -q triggered.
  149.                  The STATUS value tells our caller which.  */
  150.               status = file->update_status;
  151.               /* If -q just triggered, stop immediately.
  152.                  It doesn't matter how much more we run,
  153.                  since we already know the answer to return.  */
  154.               stop = (!keep_going_flag && !question_flag
  155.                   && !makefiles);
  156.             }
  157.               else if (MTIME (file) != mtime)
  158.             {
  159.               /* Updating was done.  If this is a makefile and
  160.                  just_print_flag or question_flag is set
  161.                  (meaning -n or -q was given and this file was
  162.                  specified as a command-line target), don't
  163.                  change STATUS.  If STATUS is changed, we will
  164.                  get re-exec'd, and fall into an infinite loop.  */
  165.               if (!makefiles
  166.                   || (!just_print_flag && !question_flag))
  167.                 status = 0;
  168.               if (makefiles && file->dontcare)
  169.                 /* This is a default makefile.  Stop remaking.  */
  170.                 stop = 1;
  171.             }
  172.             }
  173.         }
  174.  
  175.           /* Keep track if any double-colon entry is not finished.
  176.                  When they are all finished, the goal is finished.  */
  177.           any_not_updated |= !file->updated;
  178.  
  179.           if (stop)
  180.         break;
  181.         }
  182.  
  183.       /* Reset FILE since it is null at the end of the loop.  */
  184.       file = g->file;
  185.  
  186.       if (stop || !any_not_updated)
  187.         {
  188.           /* If we have found nothing whatever to do for the goal,
  189.          print a message saying nothing needs doing.  */
  190.  
  191.           if (!makefiles
  192.           /* If the update_status is zero, we updated successfully
  193.              or not at all.  G->changed will have been set above if
  194.              any commands were actually started for this goal.  */
  195.           && file->update_status == 0 && !g->changed
  196.           /* Never give a message under -s or -q.  */
  197. #if NeXT || NeXT_PDO
  198.                   && !(next_flag & NEXT_QUIET_FLAG)
  199. #endif
  200.           && !silent_flag && !question_flag)
  201.         {
  202.           if (file->phony || file->cmds == 0)
  203.             message ("Nothing to be done for `%s'.",
  204.                  file->name);
  205.           else
  206.             message ("`%s' is up to date.", file->name);
  207.           fflush (stdout);
  208.         }
  209.  
  210.           /* This goal is finished.  Remove it from the chain.  */
  211.           if (lastgoal == 0)
  212.         goals = g->next;
  213.           else
  214.         lastgoal->next = g->next;
  215.  
  216.           /* Free the storage.  */
  217.           free ((char *) g);
  218.  
  219.           g = lastgoal == 0 ? goals : lastgoal->next;
  220.  
  221.           if (stop)
  222.         break;
  223.         }
  224.       else
  225.         {
  226.           lastgoal = g;
  227.           g = g->next;
  228.         }
  229.     }
  230.     }
  231.  
  232.   if (makefiles)
  233.     {
  234.       touch_flag = t;
  235.       question_flag = q;
  236.       just_print_flag = n;
  237.       job_slots = j;
  238.     }
  239.  
  240.   return status;
  241. }
  242.  
  243. /* If FILE is not up to date, execute the commands for it.
  244.    Return 0 if successful, 1 if unsuccessful;
  245.    but with some flag settings, just call `exit' if unsuccessful.
  246.  
  247.    DEPTH is the depth in recursions of this function.
  248.    We increment it during the consideration of our dependencies,
  249.    then decrement it again after finding out whether this file
  250.    is out of date.
  251.  
  252.    If there are multiple double-colon entries for FILE,
  253.    each is considered in turn.  */
  254.  
  255. static int
  256. update_file (file, depth)
  257.      struct file *file;
  258.      unsigned int depth;
  259. {
  260.   register int status = 0;
  261.   register struct file *f;
  262.  
  263.   for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
  264.     {
  265.       status |= update_file_1 (f, depth);
  266.       check_renamed (f);
  267.  
  268.       if (status != 0 && !keep_going_flag)
  269.     return status;
  270.  
  271.       switch (f->command_state)
  272.     {
  273.     case cs_finished:
  274.       /* The file is done being remade.  */
  275.       break;
  276.  
  277.     case cs_running:
  278.     case cs_deps_running:
  279.       /* Don't run the other :: rules for this
  280.          file until this rule is finished.  */
  281.       return 0;
  282.  
  283.     default:
  284.       assert (f->command_state == cs_running);
  285.       break;
  286.     }
  287.     }
  288.  
  289.   return status;
  290. }
  291.  
  292. /* Consider a single `struct file' and update it as appropriate.  */
  293.  
  294. static int
  295. update_file_1 (file, depth)
  296.      struct file *file;
  297.      unsigned int depth;
  298. {
  299.   register time_t this_mtime;
  300.   int noexist, must_make, deps_changed;
  301.   int dep_status = 0;
  302.   register struct dep *d, *lastd;
  303.   int running = 0;
  304.  
  305.   DEBUGPR ("Considering target file `%s'.\n");
  306.  
  307.   if (file->updated)
  308.     {
  309.       if (file->update_status > 0)
  310.     {
  311.       DEBUGPR ("Recently tried and failed to update file `%s'.\n");
  312.       return file->update_status;
  313.     }
  314.  
  315.       DEBUGPR ("File `%s' was considered already.\n");
  316.       return 0;
  317.     }
  318.  
  319.   switch (file->command_state)
  320.     {
  321.     case cs_not_started:
  322.     case cs_deps_running:
  323.       break;
  324.     case cs_running:
  325.       DEBUGPR ("Still updating file `%s'.\n");
  326.       return 0;
  327.     case cs_finished:
  328.       DEBUGPR ("Finished updating file `%s'.\n");
  329.       return file->update_status;
  330.     default:
  331.       abort ();
  332.     }
  333.  
  334.   ++depth;
  335.  
  336.   /* Notice recursive update of the same file.  */
  337.   file->updating = 1;
  338.  
  339.   /* Looking at the file's modtime beforehand allows the possibility
  340.      that its name may be changed by a VPATH search, and thus it may
  341.      not need an implicit rule.  If this were not done, the file
  342.      might get implicit commands that apply to its initial name, only
  343.      to have that name replaced with another found by VPATH search.  */
  344.  
  345.   this_mtime = file_mtime (file);
  346.   check_renamed (file);
  347.   noexist = this_mtime == (time_t) -1;
  348.   if (noexist)
  349.     DEBUGPR ("File `%s' does not exist.\n");
  350.  
  351.   must_make = noexist;
  352.  
  353.   /* If file was specified as a target with no commands,
  354.      come up with some default commands.  */
  355.  
  356.   if (!file->phony && file->cmds == 0 && !file->tried_implicit)
  357.     {
  358.       if (try_implicit_rule (file, depth))
  359.     DEBUGPR ("Found an implicit rule for `%s'.\n");
  360.       else
  361.     DEBUGPR ("No implicit rule found for `%s'.\n");
  362.       file->tried_implicit = 1;
  363.     }
  364.   if (file->cmds == 0 && !file->is_target
  365.       && default_file != 0 && default_file->cmds != 0)
  366.     {
  367.       DEBUGPR ("Using default commands for `%s'.\n");
  368.       file->cmds = default_file->cmds;
  369.     }
  370.  
  371.   /* Update all non-intermediate files we depend on, if necessary,
  372.      and see whether any of them is more recent than this file.  */
  373.  
  374.   lastd = 0;
  375.   d = file->deps;
  376.   while (d != 0)
  377.     {
  378.       time_t mtime;
  379.  
  380.       check_renamed (d->file);
  381.  
  382.       mtime = file_mtime (d->file);
  383.       check_renamed (d->file);
  384.  
  385.       if (d->file->updating)
  386.     {
  387. #if NeXT || NeXT_PDO
  388.          if (!(next_flag & NEXT_QUIET_FLAG))
  389.             error ("Circular %s <- %s dependency dropped.",
  390.                    file->name, d->file->name);
  391. #else
  392.             error ("Circular %s <- %s dependency dropped.",
  393.                    file->name, d->file->name);
  394. #endif
  395.       if (lastd == 0)
  396.         {
  397.           file->deps = d->next;
  398.           free ((char *) d);
  399.           d = file->deps;
  400.         }
  401.       else
  402.         {
  403.           lastd->next = d->next;
  404.           free ((char *) d);
  405.           d = lastd->next;
  406.         }
  407.       continue;
  408.     }
  409.  
  410.       d->file->parent = file;
  411.       dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
  412.       check_renamed (d->file);
  413.  
  414.       {
  415.     register struct file *f = d->file;
  416.     if (f->double_colon)
  417.       f = f->double_colon;
  418.     do
  419.       {
  420.         running |= (f->command_state == cs_running
  421.             || f->command_state == cs_deps_running);
  422.         f = f->prev;
  423.       }
  424.     while (f != 0);
  425.       }
  426.  
  427.       if (dep_status != 0 && !keep_going_flag)
  428.     break;
  429.  
  430.       if (!running)
  431.     d->changed = file_mtime (d->file) != mtime;
  432.  
  433.       lastd = d;
  434.       d = d->next;
  435.     }
  436.  
  437.   /* Now we know whether this target needs updating.
  438.      If it does, update all the intermediate files we depend on.  */
  439.  
  440.   if (must_make)
  441.     {
  442.       for (d = file->deps; d != 0; d = d->next)
  443.     if (d->file->intermediate)
  444.       {
  445.         time_t mtime = file_mtime (d->file);
  446.         check_renamed (d->file);
  447.         d->file->parent = file;
  448.         dep_status |= update_file (d->file, depth);
  449.         check_renamed (d->file);
  450.  
  451.         {
  452.           register struct file *f = d->file;
  453.           if (f->double_colon)
  454.         f = f->double_colon;
  455.           do
  456.         {
  457.           running |= (f->command_state == cs_running
  458.                   || f->command_state == cs_deps_running);
  459.           f = f->prev;
  460.         }
  461.           while (f != 0);
  462.         }
  463.  
  464.         if (dep_status != 0 && !keep_going_flag)
  465.           break;
  466.  
  467.         if (!running)
  468.           d->changed = ((file->phony && file->cmds != 0)
  469.                 || file_mtime (d->file) != mtime);
  470.       }
  471.     }
  472.  
  473.   file->updating = 0;
  474.  
  475.   DEBUGPR ("Finished dependencies of target file `%s'.\n");
  476.  
  477.   if (running)
  478.     {
  479.       set_command_state (file, cs_deps_running);
  480.       --depth;
  481.       DEBUGPR ("The dependencies of `%s' are being made.\n");
  482.       return 0;
  483.     }
  484.  
  485.   /* If any dependency failed, give up now.  */
  486.  
  487.   if (dep_status != 0)
  488.     {
  489.       file->update_status = dep_status;
  490.       notice_finished_file (file);
  491.  
  492.       depth--;
  493.  
  494.       DEBUGPR ("Giving up on target file `%s'.\n");
  495.  
  496.       if (depth == 0 && keep_going_flag
  497.       && !just_print_flag && !question_flag)
  498.     error ("Target `%s' not remade because of errors.", file->name);
  499.  
  500.       return dep_status;
  501.     }
  502.  
  503.   if (file->command_state == cs_deps_running)
  504.     /* The commands for some deps were running on the last iteration, but
  505.        they have finished now.  Reset the command_state to not_started to
  506.        simplify later bookkeeping.  It is important that we do this only
  507.        when the prior state was cs_deps_running, because that prior state
  508.        was definitely propagated to FILE's also_make's by set_command_state
  509.        (called above), but in another state an also_make may have
  510.        independently changed to finished state, and we would confuse that
  511.        file's bookkeeping (updated, but not_started is bogus state).  */
  512.     set_command_state (file, cs_not_started);
  513.  
  514.   /* Now record which dependencies are more
  515.      recent than this file, so we can define $?.  */
  516.  
  517.   deps_changed = 0;
  518.   for (d = file->deps; d != 0; d = d->next)
  519.     {
  520.       time_t d_mtime = file_mtime (d->file);
  521.       check_renamed (d->file);
  522.  
  523. #if 1    /* %%% In version 4, remove this code completely to
  524.        implement not remaking deps if their deps are newer
  525.        than their parents.  */
  526.       if (d_mtime == (time_t) -1 && !d->file->intermediate)
  527.     /* We must remake if this dep does not
  528.        exist and is not intermediate.  */
  529.     must_make = 1;
  530. #endif
  531.  
  532.       /* Set DEPS_CHANGED if this dep actually changed.  */
  533.       deps_changed |= d->changed;
  534.  
  535.       /* Set D->changed if either this dep actually changed,
  536.      or its dependent, FILE, is older or does not exist.  */
  537.       d->changed |= noexist || d_mtime > this_mtime;
  538.  
  539.       if (debug_flag && !noexist)
  540.     {
  541.       print_spaces (depth);
  542.       if (d_mtime == (time_t) -1)
  543.         printf ("Dependency `%s' does not exist.\n", dep_name (d));
  544.       else
  545.         printf ("Dependency `%s' is %s than dependent `%s'.\n",
  546.             dep_name (d), d->changed ? "newer" : "older", file->name);
  547.       fflush (stdout);
  548.     }
  549.     }
  550.  
  551.   /* Here depth returns to the value it had when we were called.  */
  552.   depth--;
  553.  
  554.   if (file->double_colon && file->deps == 0)
  555.     {
  556.       must_make = 1;
  557.       DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
  558.     }
  559.   else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
  560.     {
  561.       must_make = 0;
  562.       DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
  563.     }
  564.  
  565.   if (!must_make)
  566.     {
  567.       DEBUGPR ("No need to remake target `%s'.\n");
  568.       notice_finished_file (file);
  569.       return 0;
  570.     }
  571.  
  572.   DEBUGPR ("Must remake target `%s'.\n");
  573.  
  574.   /* Now, take appropriate actions to remake the file.  */
  575.   remake_file (file);
  576.  
  577.   if (file->command_state != cs_finished)
  578.     {
  579.       DEBUGPR ("Commands of `%s' are being run.\n");
  580.       return 0;
  581.     }
  582.  
  583.   switch (file->update_status)
  584.     {
  585.     case 2:
  586.       DEBUGPR ("Failed to remake target file `%s'.\n");
  587.       break;
  588.     case 0:
  589.       DEBUGPR ("Successfully remade target file `%s'.\n");
  590.       break;
  591.     case 1:
  592.       DEBUGPR ("Target file `%s' needs remade under -q.\n");
  593.       break;
  594.     default:
  595.       assert (file->update_status >= 0 && file->update_status <= 2);
  596.       break;
  597.     }
  598.  
  599.   file->updated = 1;
  600.   return file->update_status;
  601. }
  602.  
  603. /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
  604.    files listed in its `also_make' member.  Under -t, this function also
  605.    touches FILE.
  606.  
  607.    On return, FILE->update_status will no longer be -1 if it was.  */
  608.  
  609. void
  610. notice_finished_file (file)
  611.      register struct file *file;
  612. {
  613.   struct dep *d;
  614.   int ran = file->command_state == cs_running;
  615.  
  616.   file->command_state = cs_finished;
  617.   file->updated = 1;
  618.  
  619.   if (touch_flag
  620.       /* The update status will be:
  621.          -1    if this target was not remade;
  622.         0    if 0 or more commands (+ or ${MAKE}) were run and won;
  623.         1    if some commands were run and lost.
  624.      We touch the target if it has commands which either were not run
  625.      or won when they ran (i.e. status is 0).  */
  626.       && file->update_status == 0)
  627.     {
  628.       if (file->cmds != 0 && file->cmds->any_recurse)
  629.     {
  630.       /* If all the command lines were recursive,
  631.          we don't want to do the touching.  */
  632.       unsigned int i;
  633.       for (i = 0; i < file->cmds->ncommand_lines; ++i)
  634.         if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
  635.           goto have_nonrecursing;
  636.     }
  637.       else
  638.     {
  639.     have_nonrecursing:
  640.       if (file->phony)
  641.         file->update_status = 0;
  642.       else
  643.         /* Should set file's modification date and do nothing else.  */
  644.         file->update_status = touch_file (file);
  645.     }
  646.     }
  647.  
  648.   if (ran && !file->phony)
  649.     {
  650.       struct file *f;
  651.  
  652.       if (just_print_flag || question_flag
  653.       || (file->is_target && file->cmds == 0))
  654.     file->last_mtime = NEW_MTIME;
  655.       else
  656.     file->last_mtime = 0;
  657.  
  658.       /* Propagate the change of modification time to all the double-colon
  659.      entries for this file.  */
  660.       for (f = file->double_colon; f != 0; f = f->next)
  661.     f->last_mtime = file->last_mtime;
  662.     }
  663.  
  664.   if (ran && file->update_status != -1)
  665.     /* We actually tried to update FILE, which has
  666.        updated its also_make's as well (if it worked).
  667.        If it didn't work, it wouldn't work again for them.
  668.        So mark them as updated with the same status.  */
  669.     for (d = file->also_make; d != 0; d = d->next)
  670.       {
  671.     d->file->command_state = cs_finished;
  672.     d->file->updated = 1;
  673.     d->file->update_status = file->update_status;
  674.  
  675.     if (ran && !d->file->phony)
  676.       /* Fetch the new modification time.
  677.          We do this instead of just invalidating the cached time
  678.          so that a vpath_search can happen.  Otherwise, it would
  679.          never be done because the target is already updated.  */
  680.       (void) f_mtime (d->file, 0);
  681.       }
  682.   else if (file->update_status == -1)
  683.     /* Nothing was done for FILE, but it needed nothing done.
  684.        So mark it now as "succeeded".  */
  685.     file->update_status = 0;
  686. }
  687.  
  688. /* Check whether another file (whose mtime is THIS_MTIME)
  689.    needs updating on account of a dependency which is file FILE.
  690.    If it does, store 1 in *MUST_MAKE_PTR.
  691.    In the process, update any non-intermediate files
  692.    that FILE depends on (including FILE itself).
  693.    Return nonzero if any updating failed.  */
  694.  
  695. static int
  696. check_dep (file, depth, this_mtime, must_make_ptr)
  697.      struct file *file;
  698.      unsigned int depth;
  699.      time_t this_mtime;
  700.      int *must_make_ptr;
  701. {
  702.   register struct dep *d;
  703.   int dep_status = 0;
  704.  
  705.   ++depth;
  706.   file->updating = 1;
  707.  
  708.   if (!file->intermediate)
  709.     /* If this is a non-intermediate file, update it and record
  710.        whether it is newer than THIS_MTIME.  */
  711.     {
  712.       time_t mtime;
  713.       dep_status = update_file (file, depth);
  714.       check_renamed (file);
  715.       mtime = file_mtime (file);
  716.       check_renamed (file);
  717.       if (mtime == (time_t) -1 || mtime > this_mtime)
  718.     *must_make_ptr = 1;
  719.     }
  720.   else
  721.     {
  722.       /* FILE is an intermediate file.
  723.      Update all non-intermediate files we depend on, if necessary,
  724.      and see whether any of them is more recent than the file
  725.      on whose behalf we are checking.  */
  726.       register struct dep *lastd;
  727.       lastd = 0;
  728.       d = file->deps;
  729.       while (d != 0)
  730.     {
  731.       if (d->file->updating)
  732.         {
  733. #if NeXT || NeXT_PDO
  734.              if (!(next_flag & NEXT_QUIET_FLAG))
  735.                 error ("Circular %s <- %s dependency dropped.",
  736.                        file->name, d->file->name);
  737. #else
  738.              error ("Circular %s <- %s dependency dropped.",
  739.                     file->name, d->file->name);
  740. #endif
  741.           if (lastd == 0)
  742.         {
  743.           file->deps = d->next;
  744.           free ((char *) d);
  745.           d = file->deps;
  746.         }
  747.           else
  748.         {
  749.           lastd->next = d->next;
  750.           free ((char *) d);
  751.           d = lastd->next;
  752.         }
  753.           continue;
  754.         }
  755.  
  756.       d->file->parent = file;
  757.       dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
  758.       check_renamed (d->file);
  759.       if (dep_status != 0 && !keep_going_flag)
  760.         break;
  761.  
  762.       if (d->file->command_state == cs_running
  763.           || d->file->command_state == cs_deps_running)
  764.         /* Record that some of FILE's dependencies are still being made.
  765.            This tells the upper levels to wait on processing it until
  766.            the commands are finished.  */
  767.         set_command_state (file, cs_deps_running);
  768.  
  769.       lastd = d;
  770.       d = d->next;
  771.     }
  772.     }
  773.  
  774.   file->updating = 0;
  775.   return dep_status;
  776. }
  777.  
  778. /* Touch FILE.  Return zero if successful, one if not.  */
  779.  
  780. #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
  781.  
  782. static int
  783. touch_file (file)
  784.      register struct file *file;
  785. {
  786.   if (!silent_flag)
  787.     {
  788.       printf ("touch %s\n", file->name);
  789.       fflush (stdout);
  790.     }
  791.  
  792. #ifndef    NO_ARCHIVES
  793.   if (ar_name (file->name))
  794.     return ar_touch (file->name);
  795.   else
  796. #endif
  797.     {
  798.       int fd = open (file->name, O_RDWR | O_CREAT, 0666);
  799.  
  800.       if (fd < 0)
  801.     TOUCH_ERROR ("touch: open: ");
  802.       else
  803.     {
  804.       struct stat statbuf;
  805.       char buf;
  806.       int status;
  807.  
  808. #ifdef EINTR
  809.       do
  810. #endif
  811.         status = fstat (fd, &statbuf);
  812. #ifdef EINTR
  813.       while (status < 0 && errno == EINTR);
  814. #endif
  815.       if (status < 0)
  816.         TOUCH_ERROR ("touch: fstat: ");
  817.       /* Rewrite character 0 same as it already is.  */
  818.       if (read (fd, &buf, 1) < 0)
  819.         TOUCH_ERROR ("touch: read: ");
  820.       if (lseek (fd, 0L, 0) < 0L)
  821.         TOUCH_ERROR ("touch: lseek: ");
  822.       if (write (fd, &buf, 1) < 0)
  823.         TOUCH_ERROR ("touch: write: ");
  824.       /* If file length was 0, we just
  825.          changed it, so change it back.  */
  826.       if (statbuf.st_size == 0)
  827.         {
  828.           (void) close (fd);
  829.           fd = open (file->name, O_RDWR | O_TRUNC, 0666);
  830.           if (fd < 0)
  831.         TOUCH_ERROR ("touch: open: ");
  832.         }
  833.       (void) close (fd);
  834.     }
  835.     }
  836.  
  837.   return 0;
  838. }
  839.  
  840. /* Having checked and updated the dependencies of FILE,
  841.    do whatever is appropriate to remake FILE itself.
  842.    Return the status from executing FILE's commands.  */
  843.  
  844. static void
  845. remake_file (file)
  846.      struct file *file;
  847. {
  848.   if (file->cmds == 0)
  849.     {
  850.       if (file->phony)
  851.     /* Phony target.  Pretend it succeeded.  */
  852.     file->update_status = 0;
  853.       else if (file->is_target)
  854.     /* This is a nonexistent target file we cannot make.
  855.        Pretend it was successfully remade.  */
  856.     file->update_status = 0;
  857.       else
  858.     {
  859.       /* This is a dependency file we cannot remake.  Fail.  */
  860.          static const char msg_noparent[]
  861.             = "%sNo rule to make target `%s'%s";
  862.            static const char msg_parent[]
  863.               = "%sNo rule to make target `%s', needed by `%s'%s";
  864. #if NeXT || NeXT_PDO
  865.       char *name = file->name;
  866.       if ((next_flag & NEXT_VPATH_FLAG) && general_vpath_search(&name)) {
  867.               free(name);
  868.           file->update_status = 0;
  869.       } else
  870. #endif    /* NeXT || NeXT_PDO */
  871.       if (keep_going_flag || file->dontcare)
  872.         {
  873.           if (!file->dontcare)
  874.         {
  875.           if (file->parent == 0)
  876.             error (msg_noparent, "*** ", file->name, ".");
  877.           else
  878.             error (msg_parent, "*** ",
  879.                file->name, file->parent->name, ".");
  880.         }
  881.            file->update_status = 2;
  882.         }
  883.       else
  884.         {
  885.           if (file->parent == 0)
  886.         fatal (msg_noparent, "", file->name, "");
  887.           else
  888.         fatal (msg_parent, "", file->name, file->parent->name, "");
  889.         }
  890.     }
  891.     }
  892.   else
  893.     {
  894.       chop_commands (file->cmds);
  895.  
  896.       if (!touch_flag || file->cmds->any_recurse)
  897.     {
  898.       execute_file_commands (file);
  899.       return;
  900.     }
  901.       else
  902.     /* This tells notice_finished_file it is ok to touch the file.  */
  903.     file->update_status = 0;
  904.     }
  905.  
  906.   /* This does the touching under -t.  */
  907.   notice_finished_file (file);
  908. }
  909.  
  910. /* Return the mtime of a file, given a `struct file'.
  911.    Caches the time in the struct file to avoid excess stat calls.
  912.  
  913.    If the file is not found, and SEARCH is nonzero, VPATH searching and
  914.    replacement is done.  If that fails, a library (-lLIBNAME) is tried and
  915.    the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
  916.    FILE.  */
  917.  
  918. time_t
  919. f_mtime (file, search)
  920.      register struct file *file;
  921.      int search;
  922. {
  923.   time_t mtime;
  924.  
  925.   /* File's mtime is not known; must get it from the system.  */
  926.  
  927. #ifndef    NO_ARCHIVES
  928.   if (ar_name (file->name))
  929.     {
  930.       /* This file is an archive-member reference.  */
  931.  
  932.       char *arname, *memname;
  933.       struct file *arfile;
  934.       int arname_used = 0;
  935.  
  936.       /* Find the archive's name.  */
  937.       ar_parse_name (file->name, &arname, &memname);
  938.  
  939.       /* Find the modification time of the archive itself.
  940.      Also allow for its name to be changed via VPATH search.  */
  941.       arfile = lookup_file (arname);
  942.       if (arfile == 0)
  943.     {
  944.       arfile = enter_file (arname);
  945.       arname_used = 1;
  946.     }
  947.       mtime = f_mtime (arfile, search);
  948.       check_renamed (arfile);
  949.       if (search && strcmp (arfile->name, arname))
  950.     {
  951.       /* The archive's name has changed.
  952.          Change the archive-member reference accordingly.  */
  953.  
  954.       unsigned int arlen, memlen;
  955.  
  956.       if (!arname_used)
  957.         {
  958.           free (arname);
  959.           arname_used = 1;
  960.         }
  961.  
  962.       arname = arfile->name;
  963.       arlen = strlen (arname);
  964.       memlen = strlen (memname);
  965.  
  966.       free (file->name);
  967.  
  968.       file->name = (char *) xmalloc (arlen + 1 + memlen + 2);
  969.       bcopy (arname, file->name, arlen);
  970.       file->name[arlen] = '(';
  971.       bcopy (memname, file->name + arlen + 1, memlen);
  972.       file->name[arlen + 1 + memlen] = ')';
  973.       file->name[arlen + 1 + memlen + 1] = '\0';
  974.     }
  975.  
  976.       if (!arname_used)
  977.     free (arname);
  978.       free (memname);
  979.  
  980.       if (mtime == (time_t) -1)
  981.     /* The archive doesn't exist, so it's members don't exist either.  */
  982.     return (time_t) -1;
  983.  
  984.       mtime = ar_member_date (file->name);
  985.     }
  986.   else
  987. #endif
  988.     {
  989.       mtime = name_mtime (file->name);
  990.  
  991.       if (mtime == (time_t) -1 && search)
  992.     {
  993.       /* If name_mtime failed, search VPATH.  */
  994.       char *name = file->name;
  995.       if (vpath_search (&name, &mtime)
  996.           /* Last resort, is it a library (-lxxx)?  */
  997.           || (name[0] == '-' && name[1] == 'l'
  998.           && library_search (&name, &mtime)))
  999.         {
  1000.           if (mtime != 0)
  1001.         /* vpath_search and library_search store zero in MTIME
  1002.            if they didn't need to do a stat call for their work.  */
  1003.         file->last_mtime = mtime;
  1004.           rename_file (file, name);
  1005.           check_renamed (file);
  1006.           return file_mtime (file);
  1007.         }
  1008.     }
  1009.     }
  1010.  
  1011.   /* Store the mtime into all the entries for this file.  */
  1012.   if (file->double_colon)
  1013.     file = file->double_colon;
  1014.   do
  1015.     {
  1016.       file->last_mtime = mtime;
  1017.       file = file->prev;
  1018.     } while (file != 0);
  1019.  
  1020.   return mtime;
  1021. }
  1022.  
  1023.  
  1024. /* Return the mtime of the file or archive-member reference NAME.  */
  1025.  
  1026. static time_t
  1027. name_mtime (name)
  1028.      register char *name;
  1029. {
  1030.   struct stat st;
  1031.  
  1032.   if (safe_stat (name, &st) < 0)
  1033.     return (time_t) -1;
  1034.  
  1035.   return (time_t) st.st_mtime;
  1036. }
  1037.  
  1038.  
  1039. /* Search for a library file specified as -lLIBNAME, searching for a
  1040.    suitable library file in the system library directories and the VPATH
  1041.    directories.  */
  1042.  
  1043. static int
  1044. library_search (lib, mtime_ptr)
  1045.      char **lib;
  1046.      time_t *mtime_ptr;
  1047. {
  1048.   static char *dirs[] =
  1049.     {
  1050.       "/lib",
  1051.       "/usr/lib",
  1052.       LIBDIR,            /* Defined by configuration.  */
  1053.       0
  1054.     };
  1055.  
  1056.   char *libname = &(*lib)[2];    /* Name without the `-l'.  */
  1057.   time_t mtime;
  1058.  
  1059.   /* Buffer to construct possible names in.  */
  1060.   char *buf = xmalloc (sizeof (LIBDIR) + 8 + strlen (libname) + 4 + 2 + 1);
  1061.   char *file, **dp;
  1062.  
  1063.   /* Look first for `libNAME.a' in the current directory.  */
  1064.  
  1065.   sprintf (buf, "lib%s.a", libname);
  1066.   mtime = name_mtime (buf);
  1067.   if (mtime != (time_t) -1)
  1068.     {
  1069.       *lib = buf;
  1070.       if (mtime_ptr != 0)
  1071.     *mtime_ptr = mtime;
  1072.       return 1;
  1073.     }
  1074.  
  1075.   /* Now try VPATH search on that.  */
  1076.  
  1077.   file = buf;
  1078.   if (vpath_search (&file, mtime_ptr))
  1079.     {
  1080.       free (buf);
  1081.       *lib = file;
  1082.       return 1;
  1083.     }
  1084.  
  1085.   /* Now try the standard set of directories.  */
  1086.  
  1087.   for (dp = dirs; *dp != 0; ++dp)
  1088.     {
  1089.       sprintf (buf, "%s/lib%s.a", *dp, libname);
  1090.       mtime = name_mtime (buf);
  1091.       if (mtime != (time_t) -1)
  1092.     {
  1093.       *lib = buf;
  1094.       if (mtime_ptr != 0)
  1095.         *mtime_ptr = mtime;
  1096.       return 1;
  1097.     }
  1098.     }
  1099.  
  1100.   free (buf);
  1101.   return 0;
  1102. }
  1103.