Home | History | Annotate | Download | only in make-3.81
      1 /* Basic dependency engine for GNU Make.
      2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
      3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
      4 Foundation, Inc.
      5 This file is part of GNU Make.
      6 
      7 GNU Make is free software; you can redistribute it and/or modify it under the
      8 terms of the GNU General Public License as published by the Free Software
      9 Foundation; either version 2, or (at your option) any later version.
     10 
     11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
     12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     13 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     14 
     15 You should have received a copy of the GNU General Public License along with
     16 GNU Make; see the file COPYING.  If not, write to the Free Software
     17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
     18 
     19 #include "make.h"
     20 #include "filedef.h"
     21 #include "job.h"
     22 #include "commands.h"
     23 #include "dep.h"
     24 #include "variable.h"
     25 #include "debug.h"
     26 
     27 #include <assert.h>
     28 
     29 #ifdef HAVE_FCNTL_H
     30 #include <fcntl.h>
     31 #else
     32 #include <sys/file.h>
     33 #endif
     34 
     35 #ifdef VMS
     36 #include <starlet.h>
     37 #endif
     38 #ifdef WINDOWS32
     39 #include <io.h>
     40 #endif
     41 
     42 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
     43 
     44 
     45 /* The test for circular dependencies is based on the 'updating' bit in
     46    `struct file'.  However, double colon targets have seperate `struct
     47    file's; make sure we always use the base of the double colon chain. */
     48 
     49 #define start_updating(_f)  (((_f)->double_colon ? (_f)->double_colon : (_f))\
     50                              ->updating = 1)
     51 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
     52                              ->updating = 0)
     53 #define is_updating(_f)     (((_f)->double_colon ? (_f)->double_colon : (_f))\
     54                              ->updating)
     55 
     56 
     57 /* Incremented when a command is started (under -n, when one would be).  */
     58 unsigned int commands_started = 0;
     59 
     60 /* Current value for pruning the scan of the goal chain (toggle 0/1).  */
     61 static unsigned int considered;
     62 
     63 static int update_file PARAMS ((struct file *file, unsigned int depth));
     64 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
     65 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
     66 static int touch_file PARAMS ((struct file *file));
     67 static void remake_file PARAMS ((struct file *file));
     68 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
     69 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
     70 
     71 
     72 /* Remake all the goals in the `struct dep' chain GOALS.  Return -1 if nothing
     74    was done, 0 if all goals were updated successfully, or 1 if a goal failed.
     75 
     76    If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
     77    and -n should be disabled for them unless they were also command-line
     78    targets, and we should only make one goal at a time and return as soon as
     79    one goal whose `changed' member is nonzero is successfully made.  */
     80 
     81 int
     82 update_goal_chain (struct dep *goals)
     83 {
     84   int t = touch_flag, q = question_flag, n = just_print_flag;
     85   unsigned int j = job_slots;
     86   int status = -1;
     87 
     88 #define	MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
     89 		     : file_mtime (file))
     90 
     91   /* Duplicate the chain so we can remove things from it.  */
     92 
     93   goals = copy_dep_chain (goals);
     94 
     95   {
     96     /* Clear the `changed' flag of each goal in the chain.
     97        We will use the flag below to notice when any commands
     98        have actually been run for a target.  When no commands
     99        have been run, we give an "up to date" diagnostic.  */
    100 
    101     struct dep *g;
    102     for (g = goals; g != 0; g = g->next)
    103       g->changed = 0;
    104   }
    105 
    106   /* All files start with the considered bit 0, so the global value is 1.  */
    107   considered = 1;
    108 
    109   /* Update all the goals until they are all finished.  */
    110 
    111   while (goals != 0)
    112     {
    113       register struct dep *g, *lastgoal;
    114 
    115       /* Start jobs that are waiting for the load to go down.  */
    116 
    117       start_waiting_jobs ();
    118 
    119       /* Wait for a child to die.  */
    120 
    121       reap_children (1, 0);
    122 
    123       lastgoal = 0;
    124       g = goals;
    125       while (g != 0)
    126 	{
    127 	  /* Iterate over all double-colon entries for this file.  */
    128 	  struct file *file;
    129 	  int stop = 0, any_not_updated = 0;
    130 
    131 	  for (file = g->file->double_colon ? g->file->double_colon : g->file;
    132 	       file != NULL;
    133 	       file = file->prev)
    134 	    {
    135 	      unsigned int ocommands_started;
    136 	      int x;
    137 	      check_renamed (file);
    138 	      if (rebuilding_makefiles)
    139 		{
    140 		  if (file->cmd_target)
    141 		    {
    142 		      touch_flag = t;
    143 		      question_flag = q;
    144 		      just_print_flag = n;
    145 		    }
    146 		  else
    147 		    touch_flag = question_flag = just_print_flag = 0;
    148 		}
    149 
    150 	      /* Save the old value of `commands_started' so we can compare
    151 		 later.  It will be incremented when any commands are
    152 		 actually run.  */
    153 	      ocommands_started = commands_started;
    154 
    155 	      x = update_file (file, rebuilding_makefiles ? 1 : 0);
    156 	      check_renamed (file);
    157 
    158 	      /* Set the goal's `changed' flag if any commands were started
    159 		 by calling update_file above.  We check this flag below to
    160 		 decide when to give an "up to date" diagnostic.  */
    161               if (commands_started > ocommands_started)
    162                 g->changed = 1;
    163 
    164               /* If we updated a file and STATUS was not already 1, set it to
    165                  1 if updating failed, or to 0 if updating succeeded.  Leave
    166                  STATUS as it is if no updating was done.  */
    167 
    168 	      stop = 0;
    169 	      if ((x != 0 || file->updated) && status < 1)
    170                 {
    171                   if (file->update_status != 0)
    172                     {
    173                       /* Updating failed, or -q triggered.  The STATUS value
    174                          tells our caller which.  */
    175                       status = file->update_status;
    176                       /* If -q just triggered, stop immediately.  It doesn't
    177                          matter how much more we run, since we already know
    178                          the answer to return.  */
    179                       stop = (question_flag && !keep_going_flag
    180                               && !rebuilding_makefiles);
    181                     }
    182                   else
    183                     {
    184                       FILE_TIMESTAMP mtime = MTIME (file);
    185                       check_renamed (file);
    186 
    187                       if (file->updated && g->changed &&
    188                            mtime != file->mtime_before_update)
    189                         {
    190                           /* Updating was done.  If this is a makefile and
    191                              just_print_flag or question_flag is set (meaning
    192                              -n or -q was given and this file was specified
    193                              as a command-line target), don't change STATUS.
    194                              If STATUS is changed, we will get re-exec'd, and
    195                              enter an infinite loop.  */
    196                           if (!rebuilding_makefiles
    197                               || (!just_print_flag && !question_flag))
    198                             status = 0;
    199                           if (rebuilding_makefiles && file->dontcare)
    200                             /* This is a default makefile; stop remaking.  */
    201                             stop = 1;
    202                         }
    203                     }
    204                 }
    205 
    206 	      /* Keep track if any double-colon entry is not finished.
    207                  When they are all finished, the goal is finished.  */
    208 	      any_not_updated |= !file->updated;
    209 
    210 	      if (stop)
    211 		break;
    212 	    }
    213 
    214 	  /* Reset FILE since it is null at the end of the loop.  */
    215 	  file = g->file;
    216 
    217 	  if (stop || !any_not_updated)
    218 	    {
    219 	      /* If we have found nothing whatever to do for the goal,
    220 		 print a message saying nothing needs doing.  */
    221 
    222 	      if (!rebuilding_makefiles
    223 		  /* If the update_status is zero, we updated successfully
    224 		     or not at all.  G->changed will have been set above if
    225 		     any commands were actually started for this goal.  */
    226 		  && file->update_status == 0 && !g->changed
    227 		  /* Never give a message under -s or -q.  */
    228 		  && !silent_flag && !question_flag)
    229 		message (1, ((file->phony || file->cmds == 0)
    230 			     ? _("Nothing to be done for `%s'.")
    231 			     : _("`%s' is up to date.")),
    232 			 file->name);
    233 
    234 	      /* This goal is finished.  Remove it from the chain.  */
    235 	      if (lastgoal == 0)
    236 		goals = g->next;
    237 	      else
    238 		lastgoal->next = g->next;
    239 
    240 	      /* Free the storage.  */
    241 	      free ((char *) g);
    242 
    243 	      g = lastgoal == 0 ? goals : lastgoal->next;
    244 
    245 	      if (stop)
    246 		break;
    247 	    }
    248 	  else
    249 	    {
    250 	      lastgoal = g;
    251 	      g = g->next;
    252 	    }
    253 	}
    254 
    255       /* If we reached the end of the dependency graph toggle the considered
    256          flag for the next pass.  */
    257       if (g == 0)
    258         considered = !considered;
    259     }
    260 
    261   if (rebuilding_makefiles)
    262     {
    263       touch_flag = t;
    264       question_flag = q;
    265       just_print_flag = n;
    266       job_slots = j;
    267     }
    268 
    269   return status;
    270 }
    271 
    272 /* If FILE is not up to date, execute the commands for it.
    274    Return 0 if successful, 1 if unsuccessful;
    275    but with some flag settings, just call `exit' if unsuccessful.
    276 
    277    DEPTH is the depth in recursions of this function.
    278    We increment it during the consideration of our dependencies,
    279    then decrement it again after finding out whether this file
    280    is out of date.
    281 
    282    If there are multiple double-colon entries for FILE,
    283    each is considered in turn.  */
    284 
    285 static int
    286 update_file (struct file *file, unsigned int depth)
    287 {
    288   register int status = 0;
    289   register struct file *f;
    290 
    291   f = file->double_colon ? file->double_colon : file;
    292 
    293   /* Prune the dependency graph: if we've already been here on _this_
    294      pass through the dependency graph, we don't have to go any further.
    295      We won't reap_children until we start the next pass, so no state
    296      change is possible below here until then.  */
    297   if (f->considered == considered)
    298     {
    299       DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
    300       return f->command_state == cs_finished ? f->update_status : 0;
    301     }
    302 
    303   /* This loop runs until we start commands for a double colon rule, or until
    304      the chain is exhausted. */
    305   for (; f != 0; f = f->prev)
    306     {
    307       f->considered = considered;
    308 
    309       status |= update_file_1 (f, depth);
    310       check_renamed (f);
    311 
    312       /* Clean up any alloca() used during the update.  */
    313       alloca (0);
    314 
    315       /* If we got an error, don't bother with double_colon etc.  */
    316       if (status != 0 && !keep_going_flag)
    317 	return status;
    318 
    319       if (f->command_state == cs_running
    320           || f->command_state == cs_deps_running)
    321         {
    322 	  /* Don't run the other :: rules for this
    323 	     file until this rule is finished.  */
    324           status = 0;
    325           break;
    326         }
    327     }
    328 
    329   /* Process the remaining rules in the double colon chain so they're marked
    330      considered.  Start their prerequisites, too.  */
    331   if (file->double_colon)
    332     for (; f != 0 ; f = f->prev)
    333       {
    334         struct dep *d;
    335 
    336         f->considered = considered;
    337 
    338         for (d = f->deps; d != 0; d = d->next)
    339           status |= update_file (d->file, depth + 1);
    340       }
    341 
    342   return status;
    343 }
    344 
    345 /* Show a message stating the target failed to build.  */
    347 
    348 static void
    349 complain (const struct file *file)
    350 {
    351   const char *msg_noparent
    352     = _("%sNo rule to make target `%s'%s");
    353   const char *msg_parent
    354     = _("%sNo rule to make target `%s', needed by `%s'%s");
    355 
    356   if (!keep_going_flag)
    357     {
    358       if (file->parent == 0)
    359         fatal (NILF, msg_noparent, "", file->name, "");
    360 
    361       fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
    362     }
    363 
    364   if (file->parent == 0)
    365     error (NILF, msg_noparent, "*** ", file->name, ".");
    366   else
    367     error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
    368 }
    369 
    370 /* Consider a single `struct file' and update it as appropriate.  */
    371 
    372 static int
    373 update_file_1 (struct file *file, unsigned int depth)
    374 {
    375   register FILE_TIMESTAMP this_mtime;
    376   int noexist, must_make, deps_changed;
    377   int dep_status = 0;
    378   register struct dep *d, *lastd;
    379   int running = 0;
    380 
    381   DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
    382 
    383   if (file->updated)
    384     {
    385       if (file->update_status > 0)
    386 	{
    387 	  DBF (DB_VERBOSE,
    388                _("Recently tried and failed to update file `%s'.\n"));
    389 
    390           /* If the file we tried to make is marked dontcare then no message
    391              was printed about it when it failed during the makefile rebuild.
    392              If we're trying to build it again in the normal rebuild, print a
    393              message now.  */
    394           if (file->dontcare && !rebuilding_makefiles)
    395             {
    396               file->dontcare = 0;
    397               complain (file);
    398             }
    399 
    400 	  return file->update_status;
    401 	}
    402 
    403       DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
    404       return 0;
    405     }
    406 
    407   switch (file->command_state)
    408     {
    409     case cs_not_started:
    410     case cs_deps_running:
    411       break;
    412     case cs_running:
    413       DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
    414       return 0;
    415     case cs_finished:
    416       DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
    417       return file->update_status;
    418     default:
    419       abort ();
    420     }
    421 
    422   ++depth;
    423 
    424   /* Notice recursive update of the same file.  */
    425   start_updating (file);
    426 
    427   /* Looking at the file's modtime beforehand allows the possibility
    428      that its name may be changed by a VPATH search, and thus it may
    429      not need an implicit rule.  If this were not done, the file
    430      might get implicit commands that apply to its initial name, only
    431      to have that name replaced with another found by VPATH search.  */
    432 
    433   this_mtime = file_mtime (file);
    434   check_renamed (file);
    435   noexist = this_mtime == NONEXISTENT_MTIME;
    436   if (noexist)
    437     DBF (DB_BASIC, _("File `%s' does not exist.\n"));
    438   else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
    439 	   && file->low_resolution_time)
    440     {
    441       /* Avoid spurious rebuilds due to low resolution time stamps.  */
    442       int ns = FILE_TIMESTAMP_NS (this_mtime);
    443       if (ns != 0)
    444 	error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
    445 	       file->name);
    446       this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
    447     }
    448 
    449   must_make = noexist;
    450 
    451   /* If file was specified as a target with no commands,
    452      come up with some default commands.  */
    453 
    454   if (!file->phony && file->cmds == 0 && !file->tried_implicit)
    455     {
    456       if (try_implicit_rule (file, depth))
    457 	DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
    458       else
    459 	DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
    460       file->tried_implicit = 1;
    461     }
    462   if (file->cmds == 0 && !file->is_target
    463       && default_file != 0 && default_file->cmds != 0)
    464     {
    465       DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
    466       file->cmds = default_file->cmds;
    467     }
    468 
    469   /* Update all non-intermediate files we depend on, if necessary,
    470      and see whether any of them is more recent than this file.  */
    471 
    472   lastd = 0;
    473   d = file->deps;
    474   while (d != 0)
    475     {
    476       FILE_TIMESTAMP mtime;
    477       int maybe_make;
    478       int dontcare = 0;
    479 
    480       check_renamed (d->file);
    481 
    482       mtime = file_mtime (d->file);
    483       check_renamed (d->file);
    484 
    485       if (is_updating (d->file))
    486 	{
    487 	  error (NILF, _("Circular %s <- %s dependency dropped."),
    488 		 file->name, d->file->name);
    489 	  /* We cannot free D here because our the caller will still have
    490 	     a reference to it when we were called recursively via
    491 	     check_dep below.  */
    492 	  if (lastd == 0)
    493 	    file->deps = d->next;
    494 	  else
    495 	    lastd->next = d->next;
    496 	  d = d->next;
    497 	  continue;
    498 	}
    499 
    500       d->file->parent = file;
    501       maybe_make = must_make;
    502 
    503       /* Inherit dontcare flag from our parent. */
    504       if (rebuilding_makefiles)
    505         {
    506           dontcare = d->file->dontcare;
    507           d->file->dontcare = file->dontcare;
    508         }
    509 
    510 
    511       dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
    512 
    513       /* Restore original dontcare flag. */
    514       if (rebuilding_makefiles)
    515         d->file->dontcare = dontcare;
    516 
    517       if (! d->ignore_mtime)
    518         must_make = maybe_make;
    519 
    520       check_renamed (d->file);
    521 
    522       {
    523 	register struct file *f = d->file;
    524 	if (f->double_colon)
    525 	  f = f->double_colon;
    526 	do
    527 	  {
    528 	    running |= (f->command_state == cs_running
    529 			|| f->command_state == cs_deps_running);
    530 	    f = f->prev;
    531 	  }
    532 	while (f != 0);
    533       }
    534 
    535       if (dep_status != 0 && !keep_going_flag)
    536 	break;
    537 
    538       if (!running)
    539         /* The prereq is considered changed if the timestamp has changed while
    540            it was built, OR it doesn't exist.
    541 	   This causes the Linux kernel build to break.  We'll defer this
    542 	   fix until GNU make 3.82 to give them time to update.  */
    543 	d->changed = ((file_mtime (d->file) != mtime)
    544                       /* || (mtime == NONEXISTENT_MTIME) */);
    545 
    546       lastd = d;
    547       d = d->next;
    548     }
    549 
    550   /* Now we know whether this target needs updating.
    551      If it does, update all the intermediate files we depend on.  */
    552 
    553   if (must_make || always_make_flag)
    554     {
    555       for (d = file->deps; d != 0; d = d->next)
    556 	if (d->file->intermediate)
    557 	  {
    558             int dontcare = 0;
    559 
    560 	    FILE_TIMESTAMP mtime = file_mtime (d->file);
    561 	    check_renamed (d->file);
    562 	    d->file->parent = file;
    563 
    564             /* Inherit dontcare flag from our parent. */
    565             if (rebuilding_makefiles)
    566               {
    567                 dontcare = d->file->dontcare;
    568                 d->file->dontcare = file->dontcare;
    569               }
    570 
    571 
    572 	    dep_status |= update_file (d->file, depth);
    573 
    574             /* Restore original dontcare flag. */
    575             if (rebuilding_makefiles)
    576               d->file->dontcare = dontcare;
    577 
    578 	    check_renamed (d->file);
    579 
    580 	    {
    581 	      register struct file *f = d->file;
    582 	      if (f->double_colon)
    583 		f = f->double_colon;
    584 	      do
    585 		{
    586 		  running |= (f->command_state == cs_running
    587 			      || f->command_state == cs_deps_running);
    588 		  f = f->prev;
    589 		}
    590 	      while (f != 0);
    591 	    }
    592 
    593 	    if (dep_status != 0 && !keep_going_flag)
    594 	      break;
    595 
    596 	    if (!running)
    597 	      d->changed = ((file->phony && file->cmds != 0)
    598 			    || file_mtime (d->file) != mtime);
    599 	  }
    600     }
    601 
    602   finish_updating (file);
    603 
    604   DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
    605 
    606   if (running)
    607     {
    608       set_command_state (file, cs_deps_running);
    609       --depth;
    610       DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
    611       return 0;
    612     }
    613 
    614   /* If any dependency failed, give up now.  */
    615 
    616   if (dep_status != 0)
    617     {
    618       file->update_status = dep_status;
    619       notice_finished_file (file);
    620 
    621       --depth;
    622 
    623       DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
    624 
    625       if (depth == 0 && keep_going_flag
    626 	  && !just_print_flag && !question_flag)
    627 	error (NILF,
    628                _("Target `%s' not remade because of errors."), file->name);
    629 
    630       return dep_status;
    631     }
    632 
    633   if (file->command_state == cs_deps_running)
    634     /* The commands for some deps were running on the last iteration, but
    635        they have finished now.  Reset the command_state to not_started to
    636        simplify later bookkeeping.  It is important that we do this only
    637        when the prior state was cs_deps_running, because that prior state
    638        was definitely propagated to FILE's also_make's by set_command_state
    639        (called above), but in another state an also_make may have
    640        independently changed to finished state, and we would confuse that
    641        file's bookkeeping (updated, but not_started is bogus state).  */
    642     set_command_state (file, cs_not_started);
    643 
    644   /* Now record which prerequisites are more
    645      recent than this file, so we can define $?.  */
    646 
    647   deps_changed = 0;
    648   for (d = file->deps; d != 0; d = d->next)
    649     {
    650       FILE_TIMESTAMP d_mtime = file_mtime (d->file);
    651       check_renamed (d->file);
    652 
    653       if (! d->ignore_mtime)
    654         {
    655 #if 1
    656           /* %%% In version 4, remove this code completely to
    657 	   implement not remaking deps if their deps are newer
    658 	   than their parents.  */
    659           if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
    660             /* We must remake if this dep does not
    661                exist and is not intermediate.  */
    662             must_make = 1;
    663 #endif
    664 
    665           /* Set DEPS_CHANGED if this dep actually changed.  */
    666           deps_changed |= d->changed;
    667         }
    668 
    669       /* Set D->changed if either this dep actually changed,
    670 	 or its dependent, FILE, is older or does not exist.  */
    671       d->changed |= noexist || d_mtime > this_mtime;
    672 
    673       if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
    674 	{
    675           const char *fmt = 0;
    676 
    677           if (d->ignore_mtime)
    678             {
    679               if (ISDB (DB_VERBOSE))
    680                 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
    681             }
    682           else if (d_mtime == NONEXISTENT_MTIME)
    683             {
    684               if (ISDB (DB_BASIC))
    685                 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
    686             }
    687 	  else if (d->changed)
    688             {
    689               if (ISDB (DB_BASIC))
    690                 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
    691             }
    692           else if (ISDB (DB_VERBOSE))
    693             fmt = _("Prerequisite `%s' is older than target `%s'.\n");
    694 
    695           if (fmt)
    696             {
    697               print_spaces (depth);
    698               printf (fmt, dep_name (d), file->name);
    699               fflush (stdout);
    700             }
    701 	}
    702     }
    703 
    704   /* Here depth returns to the value it had when we were called.  */
    705   depth--;
    706 
    707   if (file->double_colon && file->deps == 0)
    708     {
    709       must_make = 1;
    710       DBF (DB_BASIC,
    711            _("Target `%s' is double-colon and has no prerequisites.\n"));
    712     }
    713   else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
    714            && !always_make_flag)
    715     {
    716       must_make = 0;
    717       DBF (DB_VERBOSE,
    718            _("No commands for `%s' and no prerequisites actually changed.\n"));
    719     }
    720   else if (!must_make && file->cmds != 0 && always_make_flag)
    721     {
    722       must_make = 1;
    723       DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
    724     }
    725 
    726   if (!must_make)
    727     {
    728       if (ISDB (DB_VERBOSE))
    729         {
    730           print_spaces (depth);
    731           printf (_("No need to remake target `%s'"), file->name);
    732           if (!streq (file->name, file->hname))
    733               printf (_("; using VPATH name `%s'"), file->hname);
    734           puts (".");
    735           fflush (stdout);
    736         }
    737 
    738       notice_finished_file (file);
    739 
    740       /* Since we don't need to remake the file, convert it to use the
    741          VPATH filename if we found one.  hfile will be either the
    742          local name if no VPATH or the VPATH name if one was found.  */
    743 
    744       while (file)
    745         {
    746           file->name = file->hname;
    747           file = file->prev;
    748         }
    749 
    750       return 0;
    751     }
    752 
    753   DBF (DB_BASIC, _("Must remake target `%s'.\n"));
    754 
    755   /* It needs to be remade.  If it's VPATH and not reset via GPATH, toss the
    756      VPATH.  */
    757   if (!streq(file->name, file->hname))
    758     {
    759       DB (DB_BASIC, (_("  Ignoring VPATH name `%s'.\n"), file->hname));
    760       file->ignore_vpath = 1;
    761     }
    762 
    763   /* Now, take appropriate actions to remake the file.  */
    764   remake_file (file);
    765 
    766   if (file->command_state != cs_finished)
    767     {
    768       DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
    769       return 0;
    770     }
    771 
    772   switch (file->update_status)
    773     {
    774     case 2:
    775       DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
    776       break;
    777     case 0:
    778       DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
    779       break;
    780     case 1:
    781       DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
    782       break;
    783     default:
    784       assert (file->update_status >= 0 && file->update_status <= 2);
    785       break;
    786     }
    787 
    788   file->updated = 1;
    789   return file->update_status;
    790 }
    791 
    792 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
    794    files listed in its `also_make' member.  Under -t, this function also
    795    touches FILE.
    796 
    797    On return, FILE->update_status will no longer be -1 if it was.  */
    798 
    799 void
    800 notice_finished_file (struct file *file)
    801 {
    802   struct dep *d;
    803   int ran = file->command_state == cs_running;
    804   int touched = 0;
    805 
    806   file->command_state = cs_finished;
    807   file->updated = 1;
    808 
    809   if (touch_flag
    810       /* The update status will be:
    811 	 	-1	if this target was not remade;
    812 		0	if 0 or more commands (+ or ${MAKE}) were run and won;
    813 		1	if some commands were run and lost.
    814 	 We touch the target if it has commands which either were not run
    815 	 or won when they ran (i.e. status is 0).  */
    816       && file->update_status == 0)
    817     {
    818       if (file->cmds != 0 && file->cmds->any_recurse)
    819 	{
    820 	  /* If all the command lines were recursive,
    821 	     we don't want to do the touching.  */
    822 	  unsigned int i;
    823 	  for (i = 0; i < file->cmds->ncommand_lines; ++i)
    824 	    if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
    825 	      goto have_nonrecursing;
    826 	}
    827       else
    828 	{
    829 	have_nonrecursing:
    830 	  if (file->phony)
    831 	    file->update_status = 0;
    832           /* According to POSIX, -t doesn't affect targets with no cmds.  */
    833 	  else if (file->cmds != 0)
    834             {
    835               /* Should set file's modification date and do nothing else.  */
    836               file->update_status = touch_file (file);
    837 
    838               /* Pretend we ran a real touch command, to suppress the
    839                  "`foo' is up to date" message.  */
    840               commands_started++;
    841 
    842               /* Request for the timestamp to be updated (and distributed
    843                  to the double-colon entries). Simply setting ran=1 would
    844                  almost have done the trick, but messes up with the also_make
    845                  updating logic below.  */
    846               touched = 1;
    847             }
    848 	}
    849     }
    850 
    851   if (file->mtime_before_update == UNKNOWN_MTIME)
    852     file->mtime_before_update = file->last_mtime;
    853 
    854   if ((ran && !file->phony) || touched)
    855     {
    856       int i = 0;
    857 
    858       /* If -n, -t, or -q and all the commands are recursive, we ran them so
    859          really check the target's mtime again.  Otherwise, assume the target
    860          would have been updated. */
    861 
    862       if (question_flag || just_print_flag || touch_flag)
    863         {
    864           for (i = file->cmds->ncommand_lines; i > 0; --i)
    865             if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
    866               break;
    867         }
    868 
    869       /* If there were no commands at all, it's always new. */
    870 
    871       else if (file->is_target && file->cmds == 0)
    872 	i = 1;
    873 
    874       file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
    875     }
    876 
    877   if (file->double_colon)
    878     {
    879       /* If this is a double colon rule and it is the last one to be
    880          updated, propagate the change of modification time to all the
    881          double-colon entries for this file.
    882 
    883          We do it on the last update because it is important to handle
    884          individual entries as separate rules with separate timestamps
    885          while they are treated as targets and then as one rule with the
    886          unified timestamp when they are considered as a prerequisite
    887          of some target.  */
    888 
    889       struct file *f;
    890       FILE_TIMESTAMP max_mtime = file->last_mtime;
    891 
    892       /* Check that all rules were updated and at the same time find
    893          the max timestamp.  We assume UNKNOWN_MTIME is newer then
    894          any other value.  */
    895       for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
    896         if (max_mtime != UNKNOWN_MTIME
    897             && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
    898           max_mtime = f->last_mtime;
    899 
    900       if (f == 0)
    901         for (f = file->double_colon; f != 0; f = f->prev)
    902           f->last_mtime = max_mtime;
    903     }
    904 
    905   if (ran && file->update_status != -1)
    906     /* We actually tried to update FILE, which has
    907        updated its also_make's as well (if it worked).
    908        If it didn't work, it wouldn't work again for them.
    909        So mark them as updated with the same status.  */
    910     for (d = file->also_make; d != 0; d = d->next)
    911       {
    912 	d->file->command_state = cs_finished;
    913 	d->file->updated = 1;
    914 	d->file->update_status = file->update_status;
    915 
    916 	if (ran && !d->file->phony)
    917 	  /* Fetch the new modification time.
    918 	     We do this instead of just invalidating the cached time
    919 	     so that a vpath_search can happen.  Otherwise, it would
    920 	     never be done because the target is already updated.  */
    921 	  (void) f_mtime (d->file, 0);
    922       }
    923   else if (file->update_status == -1)
    924     /* Nothing was done for FILE, but it needed nothing done.
    925        So mark it now as "succeeded".  */
    926     file->update_status = 0;
    927 }
    928 
    929 /* Check whether another file (whose mtime is THIS_MTIME)
    931    needs updating on account of a dependency which is file FILE.
    932    If it does, store 1 in *MUST_MAKE_PTR.
    933    In the process, update any non-intermediate files
    934    that FILE depends on (including FILE itself).
    935    Return nonzero if any updating failed.  */
    936 
    937 static int
    938 check_dep (struct file *file, unsigned int depth,
    939            FILE_TIMESTAMP this_mtime, int *must_make_ptr)
    940 {
    941   struct dep *d;
    942   int dep_status = 0;
    943 
    944   ++depth;
    945   start_updating (file);
    946 
    947   if (file->phony || !file->intermediate)
    948     {
    949       /* If this is a non-intermediate file, update it and record
    950          whether it is newer than THIS_MTIME.  */
    951       FILE_TIMESTAMP mtime;
    952       dep_status = update_file (file, depth);
    953       check_renamed (file);
    954       mtime = file_mtime (file);
    955       check_renamed (file);
    956       if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
    957 	*must_make_ptr = 1;
    958     }
    959   else
    960     {
    961       /* FILE is an intermediate file.  */
    962       FILE_TIMESTAMP mtime;
    963 
    964       if (!file->phony && file->cmds == 0 && !file->tried_implicit)
    965 	{
    966 	  if (try_implicit_rule (file, depth))
    967 	    DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
    968 	  else
    969 	    DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
    970 	  file->tried_implicit = 1;
    971 	}
    972       if (file->cmds == 0 && !file->is_target
    973 	  && default_file != 0 && default_file->cmds != 0)
    974 	{
    975 	  DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
    976 	  file->cmds = default_file->cmds;
    977 	}
    978 
    979       /* If the intermediate file actually exists
    980 	 and is newer, then we should remake from it.  */
    981       check_renamed (file);
    982       mtime = file_mtime (file);
    983       check_renamed (file);
    984       if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
    985 	*must_make_ptr = 1;
    986 	  /* Otherwise, update all non-intermediate files we depend on,
    987 	     if necessary, and see whether any of them is more
    988 	     recent than the file on whose behalf we are checking.  */
    989       else
    990 	{
    991 	  struct dep *lastd;
    992 
    993 	  lastd = 0;
    994 	  d = file->deps;
    995 	  while (d != 0)
    996 	    {
    997               int maybe_make;
    998 
    999 	      if (is_updating (d->file))
   1000 		{
   1001 		  error (NILF, _("Circular %s <- %s dependency dropped."),
   1002 			 file->name, d->file->name);
   1003 		  if (lastd == 0)
   1004 		    {
   1005 		      file->deps = d->next;
   1006                       free_dep (d);
   1007 		      d = file->deps;
   1008 		    }
   1009 		  else
   1010 		    {
   1011 		      lastd->next = d->next;
   1012                       free_dep (d);
   1013 		      d = lastd->next;
   1014 		    }
   1015 		  continue;
   1016 		}
   1017 
   1018 	      d->file->parent = file;
   1019               maybe_make = *must_make_ptr;
   1020 	      dep_status |= check_dep (d->file, depth, this_mtime,
   1021                                        &maybe_make);
   1022               if (! d->ignore_mtime)
   1023                 *must_make_ptr = maybe_make;
   1024 	      check_renamed (d->file);
   1025 	      if (dep_status != 0 && !keep_going_flag)
   1026 		break;
   1027 
   1028 	      if (d->file->command_state == cs_running
   1029 		  || d->file->command_state == cs_deps_running)
   1030 		/* Record that some of FILE's deps are still being made.
   1031 		   This tells the upper levels to wait on processing it until
   1032 		   the commands are finished.  */
   1033 		set_command_state (file, cs_deps_running);
   1034 
   1035 	      lastd = d;
   1036 	      d = d->next;
   1037 	    }
   1038 	}
   1039     }
   1040 
   1041   finish_updating (file);
   1042   return dep_status;
   1043 }
   1044 
   1045 /* Touch FILE.  Return zero if successful, one if not.  */
   1047 
   1048 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
   1049 
   1050 static int
   1051 touch_file (struct file *file)
   1052 {
   1053   if (!silent_flag)
   1054     message (0, "touch %s", file->name);
   1055 
   1056 #ifndef	NO_ARCHIVES
   1057   if (ar_name (file->name))
   1058     return ar_touch (file->name);
   1059   else
   1060 #endif
   1061     {
   1062       int fd = open (file->name, O_RDWR | O_CREAT, 0666);
   1063 
   1064       if (fd < 0)
   1065 	TOUCH_ERROR ("touch: open: ");
   1066       else
   1067 	{
   1068 	  struct stat statbuf;
   1069 	  char buf;
   1070           int e;
   1071 
   1072           EINTRLOOP (e, fstat (fd, &statbuf));
   1073 	  if (e < 0)
   1074 	    TOUCH_ERROR ("touch: fstat: ");
   1075 	  /* Rewrite character 0 same as it already is.  */
   1076 	  if (read (fd, &buf, 1) < 0)
   1077 	    TOUCH_ERROR ("touch: read: ");
   1078 	  if (lseek (fd, 0L, 0) < 0L)
   1079 	    TOUCH_ERROR ("touch: lseek: ");
   1080 	  if (write (fd, &buf, 1) < 0)
   1081 	    TOUCH_ERROR ("touch: write: ");
   1082 	  /* If file length was 0, we just
   1083 	     changed it, so change it back.  */
   1084 	  if (statbuf.st_size == 0)
   1085 	    {
   1086 	      (void) close (fd);
   1087 	      fd = open (file->name, O_RDWR | O_TRUNC, 0666);
   1088 	      if (fd < 0)
   1089 		TOUCH_ERROR ("touch: open: ");
   1090 	    }
   1091 	  (void) close (fd);
   1092 	}
   1093     }
   1094 
   1095   return 0;
   1096 }
   1097 
   1098 /* Having checked and updated the dependencies of FILE,
   1100    do whatever is appropriate to remake FILE itself.
   1101    Return the status from executing FILE's commands.  */
   1102 
   1103 static void
   1104 remake_file (struct file *file)
   1105 {
   1106   if (file->cmds == 0)
   1107     {
   1108       if (file->phony)
   1109 	/* Phony target.  Pretend it succeeded.  */
   1110 	file->update_status = 0;
   1111       else if (file->is_target)
   1112 	/* This is a nonexistent target file we cannot make.
   1113 	   Pretend it was successfully remade.  */
   1114 	file->update_status = 0;
   1115       else
   1116         {
   1117           /* This is a dependency file we cannot remake.  Fail.  */
   1118           if (!rebuilding_makefiles || !file->dontcare)
   1119             complain (file);
   1120           file->update_status = 2;
   1121         }
   1122     }
   1123   else
   1124     {
   1125       chop_commands (file->cmds);
   1126 
   1127       /* The normal case: start some commands.  */
   1128       if (!touch_flag || file->cmds->any_recurse)
   1129 	{
   1130 	  execute_file_commands (file);
   1131 	  return;
   1132 	}
   1133 
   1134       /* This tells notice_finished_file it is ok to touch the file.  */
   1135       file->update_status = 0;
   1136     }
   1137 
   1138   /* This does the touching under -t.  */
   1139   notice_finished_file (file);
   1140 }
   1141 
   1142 /* Return the mtime of a file, given a `struct file'.
   1144    Caches the time in the struct file to avoid excess stat calls.
   1145 
   1146    If the file is not found, and SEARCH is nonzero, VPATH searching and
   1147    replacement is done.  If that fails, a library (-lLIBNAME) is tried and
   1148    the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
   1149    FILE.  */
   1150 
   1151 FILE_TIMESTAMP
   1152 f_mtime (struct file *file, int search)
   1153 {
   1154   FILE_TIMESTAMP mtime;
   1155 
   1156   /* File's mtime is not known; must get it from the system.  */
   1157 
   1158 #ifndef	NO_ARCHIVES
   1159   if (ar_name (file->name))
   1160     {
   1161       /* This file is an archive-member reference.  */
   1162 
   1163       char *arname, *memname;
   1164       struct file *arfile;
   1165       int arname_used = 0;
   1166       time_t member_date;
   1167 
   1168       /* Find the archive's name.  */
   1169       ar_parse_name (file->name, &arname, &memname);
   1170 
   1171       /* Find the modification time of the archive itself.
   1172 	 Also allow for its name to be changed via VPATH search.  */
   1173       arfile = lookup_file (arname);
   1174       if (arfile == 0)
   1175 	{
   1176 	  arfile = enter_file (arname);
   1177 	  arname_used = 1;
   1178 	}
   1179       mtime = f_mtime (arfile, search);
   1180       check_renamed (arfile);
   1181       if (search && strcmp (arfile->hname, arname))
   1182 	{
   1183 	  /* The archive's name has changed.
   1184 	     Change the archive-member reference accordingly.  */
   1185 
   1186           char *name;
   1187 	  unsigned int arlen, memlen;
   1188 
   1189 	  if (!arname_used)
   1190 	    {
   1191 	      free (arname);
   1192 	      arname_used = 1;
   1193 	    }
   1194 
   1195 	  arname = arfile->hname;
   1196 	  arlen = strlen (arname);
   1197 	  memlen = strlen (memname);
   1198 
   1199 	  /* free (file->name); */
   1200 
   1201 	  name = (char *) xmalloc (arlen + 1 + memlen + 2);
   1202 	  bcopy (arname, name, arlen);
   1203 	  name[arlen] = '(';
   1204 	  bcopy (memname, name + arlen + 1, memlen);
   1205 	  name[arlen + 1 + memlen] = ')';
   1206 	  name[arlen + 1 + memlen + 1] = '\0';
   1207 
   1208           /* If the archive was found with GPATH, make the change permanent;
   1209              otherwise defer it until later.  */
   1210           if (arfile->name == arfile->hname)
   1211             rename_file (file, name);
   1212           else
   1213             rehash_file (file, name);
   1214           check_renamed (file);
   1215 	}
   1216 
   1217       if (!arname_used)
   1218 	free (arname);
   1219       free (memname);
   1220 
   1221       file->low_resolution_time = 1;
   1222 
   1223       if (mtime == NONEXISTENT_MTIME)
   1224 	/* The archive doesn't exist, so its members don't exist either.  */
   1225 	return NONEXISTENT_MTIME;
   1226 
   1227       member_date = ar_member_date (file->hname);
   1228       mtime = (member_date == (time_t) -1
   1229                ? NONEXISTENT_MTIME
   1230                : file_timestamp_cons (file->hname, member_date, 0));
   1231     }
   1232   else
   1233 #endif
   1234     {
   1235       mtime = name_mtime (file->name);
   1236 
   1237       if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
   1238 	{
   1239 	  /* If name_mtime failed, search VPATH.  */
   1240 	  char *name = file->name;
   1241 	  if (vpath_search (&name, &mtime)
   1242 	      /* Last resort, is it a library (-lxxx)?  */
   1243 	      || (name[0] == '-' && name[1] == 'l'
   1244 		  && library_search (&name, &mtime)))
   1245 	    {
   1246 	      if (mtime != UNKNOWN_MTIME)
   1247 		/* vpath_search and library_search store UNKNOWN_MTIME
   1248 		   if they didn't need to do a stat call for their work.  */
   1249 		file->last_mtime = mtime;
   1250 
   1251               /* If we found it in VPATH, see if it's in GPATH too; if so,
   1252                  change the name right now; if not, defer until after the
   1253                  dependencies are updated. */
   1254               if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
   1255                 {
   1256                   rename_file (file, name);
   1257                   check_renamed (file);
   1258                   return file_mtime (file);
   1259                 }
   1260 
   1261 	      rehash_file (file, name);
   1262 	      check_renamed (file);
   1263               /* If the result of a vpath search is -o or -W, preserve it.
   1264                  Otherwise, find the mtime of the resulting file.  */
   1265               if (mtime != OLD_MTIME && mtime != NEW_MTIME)
   1266                 mtime = name_mtime (name);
   1267 	    }
   1268 	}
   1269     }
   1270 
   1271   /* Files can have bogus timestamps that nothing newly made will be
   1272      "newer" than.  Updating their dependents could just result in loops.
   1273      So notify the user of the anomaly with a warning.
   1274 
   1275      We only need to do this once, for now. */
   1276 
   1277   if (!clock_skew_detected
   1278       && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
   1279       && !file->updated)
   1280     {
   1281       static FILE_TIMESTAMP adjusted_now;
   1282 
   1283       FILE_TIMESTAMP adjusted_mtime = mtime;
   1284 
   1285 #if defined(WINDOWS32) || defined(__MSDOS__)
   1286       /* Experimentation has shown that FAT filesystems can set file times
   1287          up to 3 seconds into the future!  Play it safe.  */
   1288 
   1289 #define FAT_ADJ_OFFSET  (FILE_TIMESTAMP) 3
   1290 
   1291       FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
   1292       if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
   1293         adjusted_mtime -= adjustment;
   1294 #elif defined(__EMX__)
   1295       /* FAT filesystems round time to the nearest even second!
   1296          Allow for any file (NTFS or FAT) to perhaps suffer from this
   1297          brain damage.  */
   1298       FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
   1299                      && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
   1300                     ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
   1301                     : 0);
   1302 #endif
   1303 
   1304       /* If the file's time appears to be in the future, update our
   1305          concept of the present and try once more.  */
   1306       if (adjusted_now < adjusted_mtime)
   1307         {
   1308           int resolution;
   1309           FILE_TIMESTAMP now = file_timestamp_now (&resolution);
   1310           adjusted_now = now + (resolution - 1);
   1311           if (adjusted_now < adjusted_mtime)
   1312             {
   1313 #ifdef NO_FLOAT
   1314               error (NILF, _("Warning: File `%s' has modification time in the future"),
   1315                      file->name);
   1316 #else
   1317               double from_now =
   1318                 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
   1319                  + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
   1320                     / 1e9));
   1321               error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
   1322                      file->name, from_now);
   1323 #endif
   1324               clock_skew_detected = 1;
   1325             }
   1326         }
   1327     }
   1328 
   1329   /* Store the mtime into all the entries for this file.  */
   1330   if (file->double_colon)
   1331     file = file->double_colon;
   1332 
   1333   do
   1334     {
   1335       /* If this file is not implicit but it is intermediate then it was
   1336 	 made so by the .INTERMEDIATE target.  If this file has never
   1337 	 been built by us but was found now, it existed before make
   1338 	 started.  So, turn off the intermediate bit so make doesn't
   1339 	 delete it, since it didn't create it.  */
   1340       if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
   1341 	  && file->command_state == cs_not_started
   1342 	  && !file->tried_implicit && file->intermediate)
   1343 	file->intermediate = 0;
   1344 
   1345       file->last_mtime = mtime;
   1346       file = file->prev;
   1347     }
   1348   while (file != 0);
   1349 
   1350   return mtime;
   1351 }
   1352 
   1353 
   1354 /* Return the mtime of the file or archive-member reference NAME.  */
   1355 
   1356 /* First, we check with stat().  If the file does not exist, then we return
   1357    NONEXISTENT_MTIME.  If it does, and the symlink check flag is set, then
   1358    examine each indirection of the symlink and find the newest mtime.
   1359    This causes one duplicate stat() when -L is being used, but the code is
   1360    much cleaner.  */
   1361 
   1362 static FILE_TIMESTAMP
   1363 name_mtime (char *name)
   1364 {
   1365   FILE_TIMESTAMP mtime;
   1366   struct stat st;
   1367   int e;
   1368 
   1369   EINTRLOOP (e, stat (name, &st));
   1370   if (e == 0)
   1371     mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
   1372   else if (errno == ENOENT || errno == ENOTDIR)
   1373     mtime = NONEXISTENT_MTIME;
   1374   else
   1375     {
   1376       perror_with_name ("stat: ", name);
   1377       return NONEXISTENT_MTIME;
   1378     }
   1379 
   1380   /* If we get here we either found it, or it doesn't exist.
   1381      If it doesn't exist see if we can use a symlink mtime instead.  */
   1382 
   1383 #ifdef MAKE_SYMLINKS
   1384 #ifndef S_ISLNK
   1385 # define S_ISLNK(_m)     (((_m)&S_IFMT)==S_IFLNK)
   1386 #endif
   1387   if (check_symlink_flag)
   1388     {
   1389       PATH_VAR (lpath);
   1390 
   1391       /* Check each symbolic link segment (if any).  Find the latest mtime
   1392          amongst all of them (and the target file of course).
   1393          Note that we have already successfully dereferenced all the links
   1394          above.  So, if we run into any error trying to lstat(), or
   1395          readlink(), or whatever, something bizarre-o happened.  Just give up
   1396          and use whatever mtime we've already computed at that point.  */
   1397       strcpy (lpath, name);
   1398       while (1)
   1399         {
   1400           FILE_TIMESTAMP ltime;
   1401           PATH_VAR (lbuf);
   1402           long llen;
   1403           char *p;
   1404 
   1405           EINTRLOOP (e, lstat (lpath, &st));
   1406           if (e)
   1407             {
   1408               /* Just take what we have so far.  */
   1409               if (errno != ENOENT && errno != ENOTDIR)
   1410                 perror_with_name ("lstat: ", lpath);
   1411               break;
   1412             }
   1413 
   1414           /* If this is not a symlink, we're done (we started with the real
   1415              file's mtime so we don't need to test it again).  */
   1416           if (!S_ISLNK (st.st_mode))
   1417             break;
   1418 
   1419           /* If this mtime is newer than what we had, keep the new one.  */
   1420           ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
   1421           if (ltime > mtime)
   1422             mtime = ltime;
   1423 
   1424           /* Set up to check the file pointed to by this link.  */
   1425           EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
   1426           if (llen < 0)
   1427             {
   1428               /* Eh?  Just take what we have.  */
   1429               perror_with_name ("readlink: ", lpath);
   1430               break;
   1431             }
   1432           lbuf[llen] = '\0';
   1433 
   1434           /* If the target is fully-qualified or the source is just a
   1435              filename, then the new path is the target.  Otherwise it's the
   1436              source directory plus the target.  */
   1437           if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
   1438             strcpy (lpath, lbuf);
   1439           else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
   1440             /* Eh?  Path too long!  Again, just go with what we have.  */
   1441             break;
   1442           else
   1443             /* Create the next step in the symlink chain.  */
   1444             strcpy (p+1, lbuf);
   1445         }
   1446     }
   1447 #endif
   1448 
   1449   return mtime;
   1450 }
   1451 
   1452 
   1453 /* Search for a library file specified as -lLIBNAME, searching for a
   1454    suitable library file in the system library directories and the VPATH
   1455    directories.  */
   1456 
   1457 static int
   1458 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
   1459 {
   1460   static char *dirs[] =
   1461     {
   1462 #ifndef _AMIGA
   1463       "/lib",
   1464       "/usr/lib",
   1465 #endif
   1466 #if defined(WINDOWS32) && !defined(LIBDIR)
   1467 /*
   1468  * This is completely up to the user at product install time. Just define
   1469  * a placeholder.
   1470  */
   1471 #define LIBDIR "."
   1472 #endif
   1473       LIBDIR,			/* Defined by configuration.  */
   1474       0
   1475     };
   1476 
   1477   static char *libpatterns = NULL;
   1478 
   1479   char *libname = &(*lib)[2];	/* Name without the `-l'.  */
   1480   FILE_TIMESTAMP mtime;
   1481 
   1482   /* Loop variables for the libpatterns value.  */
   1483   char *p, *p2;
   1484   unsigned int len;
   1485 
   1486   char *file, **dp;
   1487 
   1488   /* If we don't have libpatterns, get it.  */
   1489   if (!libpatterns)
   1490     {
   1491       int save = warn_undefined_variables_flag;
   1492       warn_undefined_variables_flag = 0;
   1493 
   1494       libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
   1495 
   1496       warn_undefined_variables_flag = save;
   1497     }
   1498 
   1499   /* Loop through all the patterns in .LIBPATTERNS, and search on each one.  */
   1500   p2 = libpatterns;
   1501   while ((p = find_next_token (&p2, &len)) != 0)
   1502     {
   1503       static char *buf = NULL;
   1504       static unsigned int buflen = 0;
   1505       static int libdir_maxlen = -1;
   1506       char *libbuf = variable_expand ("");
   1507 
   1508       /* Expand the pattern using LIBNAME as a replacement.  */
   1509       {
   1510 	char c = p[len];
   1511 	char *p3, *p4;
   1512 
   1513 	p[len] = '\0';
   1514 	p3 = find_percent (p);
   1515 	if (!p3)
   1516 	  {
   1517 	    /* Give a warning if there is no pattern, then remove the
   1518 	       pattern so it's ignored next time.  */
   1519 	    error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
   1520 	    for (; len; --len, ++p)
   1521 	      *p = ' ';
   1522 	    *p = c;
   1523 	    continue;
   1524 	  }
   1525 	p4 = variable_buffer_output (libbuf, p, p3-p);
   1526 	p4 = variable_buffer_output (p4, libname, strlen (libname));
   1527 	p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
   1528 	p[len] = c;
   1529       }
   1530 
   1531       /* Look first for `libNAME.a' in the current directory.  */
   1532       mtime = name_mtime (libbuf);
   1533       if (mtime != NONEXISTENT_MTIME)
   1534 	{
   1535 	  *lib = xstrdup (libbuf);
   1536 	  if (mtime_ptr != 0)
   1537 	    *mtime_ptr = mtime;
   1538 	  return 1;
   1539 	}
   1540 
   1541       /* Now try VPATH search on that.  */
   1542 
   1543       file = libbuf;
   1544       if (vpath_search (&file, mtime_ptr))
   1545 	{
   1546 	  *lib = file;
   1547 	  return 1;
   1548 	}
   1549 
   1550       /* Now try the standard set of directories.  */
   1551 
   1552       if (!buflen)
   1553 	{
   1554 	  for (dp = dirs; *dp != 0; ++dp)
   1555 	    {
   1556 	      int l = strlen (*dp);
   1557 	      if (l > libdir_maxlen)
   1558 		libdir_maxlen = l;
   1559 	    }
   1560 	  buflen = strlen (libbuf);
   1561 	  buf = xmalloc(libdir_maxlen + buflen + 2);
   1562 	}
   1563       else if (buflen < strlen (libbuf))
   1564 	{
   1565 	  buflen = strlen (libbuf);
   1566 	  buf = xrealloc (buf, libdir_maxlen + buflen + 2);
   1567 	}
   1568 
   1569       for (dp = dirs; *dp != 0; ++dp)
   1570 	{
   1571 	  sprintf (buf, "%s/%s", *dp, libbuf);
   1572 	  mtime = name_mtime (buf);
   1573 	  if (mtime != NONEXISTENT_MTIME)
   1574 	    {
   1575 	      *lib = xstrdup (buf);
   1576 	      if (mtime_ptr != 0)
   1577 		*mtime_ptr = mtime;
   1578 	      return 1;
   1579 	    }
   1580 	}
   1581     }
   1582 
   1583   return 0;
   1584 }
   1585