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