source: trunk/third/gmake/remake.c @ 15972

Revision 15972, 38.4 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15971, which included commits to RCS files with non-trunk default branches.
Line 
1/* Basic dependency engine for GNU Make.
2Copyright (C) 1988,89,90,91,92,93,94,95,96,97,99 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10GNU Make is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Make; see the file COPYING.  If not, write to
17the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, MA 02111-1307, USA.  */
19
20#include "make.h"
21#include "filedef.h"
22#include "job.h"
23#include "commands.h"
24#include "dep.h"
25#include "variable.h"
26#include "debug.h"
27
28#include <assert.h>
29
30#ifdef HAVE_FCNTL_H
31#include <fcntl.h>
32#else
33#include <sys/file.h>
34#endif
35
36#ifdef VMS
37#include <starlet.h>
38#endif
39#ifdef WINDOWS32
40#include <io.h>
41#endif
42
43extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
44
45
46/* The test for circular dependencies is based on the 'updating' bit in
47   `struct file'.  However, double colon targets have seperate `struct
48   file's; make sure we always use the base of the double colon chain. */
49
50#define start_updating(_f)  (((_f)->double_colon ? (_f)->double_colon : (_f))\
51                             ->updating = 1)
52#define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
53                             ->updating = 0)
54#define is_updating(_f)     (((_f)->double_colon ? (_f)->double_colon : (_f))\
55                             ->updating)
56
57
58/* Incremented when a command is started (under -n, when one would be).  */
59unsigned int commands_started = 0;
60
61/* Current value for pruning the scan of the goal chain (toggle 0/1).  */
62static unsigned int considered;
63
64static int update_file PARAMS ((struct file *file, unsigned int depth));
65static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
66static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
67static int touch_file PARAMS ((struct file *file));
68static void remake_file PARAMS ((struct file *file));
69static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
70static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
71
72
73/* 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   If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
76   be disabled for them unless they were also command-line targets, and we
77   should only make one goal at a time and return as soon as one goal whose
78   `changed' member is nonzero is successfully made.  */
79
80int
81update_goal_chain (goals, makefiles)
82     register struct dep *goals;
83     int makefiles;
84{
85  int t = touch_flag, q = question_flag, n = just_print_flag;
86  unsigned int j = job_slots;
87  int status = -1;
88
89#define MTIME(file) (makefiles ? file_mtime_no_search (file) \
90                     : file_mtime (file))
91
92  /* Duplicate the chain so we can remove things from it.  */
93
94  goals = copy_dep_chain (goals);
95
96  {
97    /* Clear the `changed' flag of each goal in the chain.
98       We will use the flag below to notice when any commands
99       have actually been run for a target.  When no commands
100       have been run, we give an "up to date" diagnostic.  */
101
102    struct dep *g;
103    for (g = goals; g != 0; g = g->next)
104      g->changed = 0;
105  }
106
107  /* All files start with the considered bit 0, so the global value is 1.  */
108  considered = 1;
109
110  /* Update all the goals until they are all finished.  */
111
112  while (goals != 0)
113    {
114      register struct dep *g, *lastgoal;
115
116      /* Start jobs that are waiting for the load to go down.  */
117
118      start_waiting_jobs ();
119
120      /* Wait for a child to die.  */
121
122      reap_children (1, 0);
123
124      lastgoal = 0;
125      g = goals;
126      while (g != 0)
127        {
128          /* Iterate over all double-colon entries for this file.  */
129          struct file *file;
130          int stop = 0, any_not_updated = 0;
131
132          for (file = g->file->double_colon ? g->file->double_colon : g->file;
133               file != NULL;
134               file = file->prev)
135            {
136              unsigned int ocommands_started;
137              int x;
138              check_renamed (file);
139              if (makefiles)
140                {
141                  if (file->cmd_target)
142                    {
143                      touch_flag = t;
144                      question_flag = q;
145                      just_print_flag = n;
146                    }
147                  else
148                    touch_flag = question_flag = just_print_flag = 0;
149                }
150
151              /* Save the old value of `commands_started' so we can compare
152                 later.  It will be incremented when any commands are
153                 actually run.  */
154              ocommands_started = commands_started;
155
156              x = update_file (file, makefiles ? 1 : 0);
157              check_renamed (file);
158
159              /* Set the goal's `changed' flag if any commands were started
160                 by calling update_file above.  We check this flag below to
161                 decide when to give an "up to date" diagnostic.  */
162              g->changed += commands_started - ocommands_started;
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 = (!keep_going_flag && !question_flag
180                              && !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 (!makefiles
197                              || (!just_print_flag && !question_flag))
198                            status = 0;
199                          if (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 (!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 (makefiles)
262    {
263      touch_flag = t;
264      question_flag = q;
265      just_print_flag = n;
266      job_slots = j;
267    }
268  return status;
269}
270
271/* If FILE is not up to date, execute the commands for it.
272   Return 0 if successful, 1 if unsuccessful;
273   but with some flag settings, just call `exit' if unsuccessful.
274
275   DEPTH is the depth in recursions of this function.
276   We increment it during the consideration of our dependencies,
277   then decrement it again after finding out whether this file
278   is out of date.
279
280   If there are multiple double-colon entries for FILE,
281   each is considered in turn.  */
282
283static int
284update_file (file, depth)
285     struct file *file;
286     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      if (status != 0 && !keep_going_flag)
313        break;
314
315      if (f->command_state == cs_running
316          || f->command_state == cs_deps_running)
317        {
318          /* Don't run the other :: rules for this
319             file until this rule is finished.  */
320          status = 0;
321          break;
322        }
323    }
324
325  /* Process the remaining rules in the double colon chain so they're marked
326     considered.  Start their prerequisites, too.  */
327  for (; f != 0 ; f = f->prev)
328    {
329      struct dep *d;
330
331      f->considered = considered;
332
333      for (d = f->deps; d != 0; d = d->next)
334        status |= update_file (d->file, depth + 1);
335    }
336
337  return status;
338}
339
340/* Consider a single `struct file' and update it as appropriate.  */
341
342static int
343update_file_1 (file, depth)
344     struct file *file;
345     unsigned int depth;
346{
347  register FILE_TIMESTAMP this_mtime;
348  int noexist, must_make, deps_changed;
349  int dep_status = 0;
350  register struct dep *d, *lastd;
351  int running = 0;
352
353  DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
354
355  if (file->updated)
356    {
357      if (file->update_status > 0)
358        {
359          DBF (DB_VERBOSE,
360               _("Recently tried and failed to update file `%s'.\n"));
361          return file->update_status;
362        }
363
364      DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
365      return 0;
366    }
367
368  switch (file->command_state)
369    {
370    case cs_not_started:
371    case cs_deps_running:
372      break;
373    case cs_running:
374      DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
375      return 0;
376    case cs_finished:
377      DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
378      return file->update_status;
379    default:
380      abort ();
381    }
382
383  ++depth;
384
385  /* Notice recursive update of the same file.  */
386  start_updating (file);
387
388  /* Looking at the file's modtime beforehand allows the possibility
389     that its name may be changed by a VPATH search, and thus it may
390     not need an implicit rule.  If this were not done, the file
391     might get implicit commands that apply to its initial name, only
392     to have that name replaced with another found by VPATH search.  */
393
394  this_mtime = file_mtime (file);
395  check_renamed (file);
396  noexist = this_mtime == NONEXISTENT_MTIME;
397  if (noexist)
398    DBF (DB_BASIC, _("File `%s' does not exist.\n"));
399
400  must_make = noexist;
401
402  /* If file was specified as a target with no commands,
403     come up with some default commands.  */
404
405  if (!file->phony && file->cmds == 0 && !file->tried_implicit)
406    {
407      if (try_implicit_rule (file, depth))
408        DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
409      else
410        DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
411      file->tried_implicit = 1;
412    }
413  if (file->cmds == 0 && !file->is_target
414      && default_file != 0 && default_file->cmds != 0)
415    {
416      DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
417      file->cmds = default_file->cmds;
418    }
419
420  /* Update all non-intermediate files we depend on, if necessary,
421     and see whether any of them is more recent than this file.  */
422
423  lastd = 0;
424  d = file->deps;
425  while (d != 0)
426    {
427      FILE_TIMESTAMP mtime;
428
429      check_renamed (d->file);
430
431      mtime = file_mtime (d->file);
432      check_renamed (d->file);
433
434      if (is_updating (d->file))
435        {
436          error (NILF, _("Circular %s <- %s dependency dropped."),
437                 file->name, d->file->name);
438          /* We cannot free D here because our the caller will still have
439             a reference to it when we were called recursively via
440             check_dep below.  */
441          if (lastd == 0)
442            file->deps = d->next;
443          else
444            lastd->next = d->next;
445          d = d->next;
446          continue;
447        }
448
449      d->file->parent = file;
450      dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
451      check_renamed (d->file);
452
453      {
454        register struct file *f = d->file;
455        if (f->double_colon)
456          f = f->double_colon;
457        do
458          {
459            running |= (f->command_state == cs_running
460                        || f->command_state == cs_deps_running);
461            f = f->prev;
462          }
463        while (f != 0);
464      }
465
466      if (dep_status != 0 && !keep_going_flag)
467        break;
468
469      if (!running)
470        d->changed = file_mtime (d->file) != mtime;
471
472      lastd = d;
473      d = d->next;
474    }
475
476  /* Now we know whether this target needs updating.
477     If it does, update all the intermediate files we depend on.  */
478
479  if (must_make)
480    {
481      for (d = file->deps; d != 0; d = d->next)
482        if (d->file->intermediate)
483          {
484            FILE_TIMESTAMP mtime = file_mtime (d->file);
485            check_renamed (d->file);
486            d->file->parent = file;
487            dep_status |= update_file (d->file, depth);
488            check_renamed (d->file);
489
490            {
491              register struct file *f = d->file;
492              if (f->double_colon)
493                f = f->double_colon;
494              do
495                {
496                  running |= (f->command_state == cs_running
497                              || f->command_state == cs_deps_running);
498                  f = f->prev;
499                }
500              while (f != 0);
501            }
502
503            if (dep_status != 0 && !keep_going_flag)
504              break;
505
506            if (!running)
507              d->changed = ((file->phony && file->cmds != 0)
508                            || file_mtime (d->file) != mtime);
509          }
510    }
511
512  finish_updating (file);
513
514  DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
515
516  if (running)
517    {
518      set_command_state (file, cs_deps_running);
519      --depth;
520      DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
521      return 0;
522    }
523
524  /* If any dependency failed, give up now.  */
525
526  if (dep_status != 0)
527    {
528      file->update_status = dep_status;
529      notice_finished_file (file);
530
531      depth--;
532
533      DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
534
535      if (depth == 0 && keep_going_flag
536          && !just_print_flag && !question_flag)
537        error (NILF,
538               _("Target `%s' not remade because of errors."), file->name);
539
540      return dep_status;
541    }
542
543  if (file->command_state == cs_deps_running)
544    /* The commands for some deps were running on the last iteration, but
545       they have finished now.  Reset the command_state to not_started to
546       simplify later bookkeeping.  It is important that we do this only
547       when the prior state was cs_deps_running, because that prior state
548       was definitely propagated to FILE's also_make's by set_command_state
549       (called above), but in another state an also_make may have
550       independently changed to finished state, and we would confuse that
551       file's bookkeeping (updated, but not_started is bogus state).  */
552    set_command_state (file, cs_not_started);
553
554  /* Now record which dependencies are more
555     recent than this file, so we can define $?.  */
556
557  deps_changed = 0;
558  for (d = file->deps; d != 0; d = d->next)
559    {
560      FILE_TIMESTAMP d_mtime = file_mtime (d->file);
561      check_renamed (d->file);
562
563#if 1   /* %%% In version 4, remove this code completely to
564           implement not remaking deps if their deps are newer
565           than their parents.  */
566      if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
567        /* We must remake if this dep does not
568           exist and is not intermediate.  */
569        must_make = 1;
570#endif
571
572      /* Set DEPS_CHANGED if this dep actually changed.  */
573      deps_changed |= d->changed;
574
575      /* Set D->changed if either this dep actually changed,
576         or its dependent, FILE, is older or does not exist.  */
577      d->changed |= noexist || d_mtime > this_mtime;
578
579      if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
580        {
581          const char *fmt = 0;
582
583          if (d_mtime == NONEXISTENT_MTIME)
584            {
585              if (ISDB (DB_BASIC))
586                fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
587            }
588          else if (d->changed)
589            {
590              if (ISDB (DB_BASIC))
591                fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
592            }
593          else if (ISDB (DB_VERBOSE))
594            fmt = _("Prerequisite `%s' is older than target `%s'.\n");
595
596          if (fmt)
597            {
598              print_spaces (depth);
599              printf (fmt, dep_name (d), file->name);
600              fflush (stdout);
601            }
602        }
603    }
604
605  /* Here depth returns to the value it had when we were called.  */
606  depth--;
607
608  if (file->double_colon && file->deps == 0)
609    {
610      must_make = 1;
611      DBF (DB_BASIC,
612           _("Target `%s' is double-colon and has no prerequisites.\n"));
613    }
614  else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
615    {
616      must_make = 0;
617      DBF (DB_VERBOSE,
618           _("No commands for `%s' and no prerequisites actually changed.\n"));
619    }
620
621  if (!must_make)
622    {
623      if (ISDB (DB_VERBOSE))
624        {
625          print_spaces (depth);
626          printf (_("No need to remake target `%s'"), file->name);
627          if (!streq (file->name, file->hname))
628              printf (_("; using VPATH name `%s'"), file->hname);
629          puts (".");
630          fflush (stdout);
631        }
632
633      notice_finished_file (file);
634
635      /* Since we don't need to remake the file, convert it to use the
636         VPATH filename if we found one.  hfile will be either the
637         local name if no VPATH or the VPATH name if one was found.  */
638
639      while (file)
640        {
641          file->name = file->hname;
642          file = file->prev;
643        }
644
645      return 0;
646    }
647
648  DBF (DB_BASIC, _("Must remake target `%s'.\n"));
649
650  /* It needs to be remade.  If it's VPATH and not reset via GPATH, toss the
651     VPATH.  */
652  if (!streq(file->name, file->hname))
653    {
654      DB (DB_BASIC, (_("  Ignoring VPATH name `%s'.\n"), file->hname));
655      file->ignore_vpath = 1;
656    }
657
658  /* Now, take appropriate actions to remake the file.  */
659  remake_file (file);
660
661  if (file->command_state != cs_finished)
662    {
663      DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
664      return 0;
665    }
666
667  switch (file->update_status)
668    {
669    case 2:
670      DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
671      break;
672    case 0:
673      DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
674      break;
675    case 1:
676      DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
677      break;
678    default:
679      assert (file->update_status >= 0 && file->update_status <= 2);
680      break;
681    }
682
683  file->updated = 1;
684  return file->update_status;
685}
686
687/* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
688   files listed in its `also_make' member.  Under -t, this function also
689   touches FILE.
690
691   On return, FILE->update_status will no longer be -1 if it was.  */
692
693void
694notice_finished_file (file)
695     register struct file *file;
696{
697  struct dep *d;
698  int ran = file->command_state == cs_running;
699
700  file->command_state = cs_finished;
701  file->updated = 1;
702
703  if (touch_flag
704      /* The update status will be:
705                -1      if this target was not remade;
706                0       if 0 or more commands (+ or ${MAKE}) were run and won;
707                1       if some commands were run and lost.
708         We touch the target if it has commands which either were not run
709         or won when they ran (i.e. status is 0).  */
710      && file->update_status == 0)
711    {
712      if (file->cmds != 0 && file->cmds->any_recurse)
713        {
714          /* If all the command lines were recursive,
715             we don't want to do the touching.  */
716          unsigned int i;
717          for (i = 0; i < file->cmds->ncommand_lines; ++i)
718            if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
719              goto have_nonrecursing;
720        }
721      else
722        {
723        have_nonrecursing:
724          if (file->phony)
725            file->update_status = 0;
726          else
727            /* Should set file's modification date and do nothing else.  */
728            file->update_status = touch_file (file);
729        }
730    }
731
732  if (file->mtime_before_update == UNKNOWN_MTIME)
733    file->mtime_before_update = file->last_mtime;
734
735  if (ran && !file->phony)
736    {
737      struct file *f;
738      int i = 0;
739
740      /* If -n or -q and all the commands are recursive, we ran them so
741         really check the target's mtime again.  Otherwise, assume the target
742         would have been updated. */
743
744      if (question_flag || just_print_flag)
745        {
746          for (i = file->cmds->ncommand_lines; i > 0; --i)
747            if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
748              break;
749        }
750
751      /* If there were no commands at all, it's always new. */
752
753      else if (file->is_target && file->cmds == 0)
754        i = 1;
755
756      file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
757
758      /* Propagate the change of modification time to all the double-colon
759         entries for this file.  */
760      for (f = file->double_colon; f != 0; f = f->next)
761        f->last_mtime = file->last_mtime;
762    }
763
764  if (ran && file->update_status != -1)
765    /* We actually tried to update FILE, which has
766       updated its also_make's as well (if it worked).
767       If it didn't work, it wouldn't work again for them.
768       So mark them as updated with the same status.  */
769    for (d = file->also_make; d != 0; d = d->next)
770      {
771        d->file->command_state = cs_finished;
772        d->file->updated = 1;
773        d->file->update_status = file->update_status;
774
775        if (ran && !d->file->phony)
776          /* Fetch the new modification time.
777             We do this instead of just invalidating the cached time
778             so that a vpath_search can happen.  Otherwise, it would
779             never be done because the target is already updated.  */
780          (void) f_mtime (d->file, 0);
781      }
782  else if (file->update_status == -1)
783    /* Nothing was done for FILE, but it needed nothing done.
784       So mark it now as "succeeded".  */
785    file->update_status = 0;
786}
787
788/* Check whether another file (whose mtime is THIS_MTIME)
789   needs updating on account of a dependency which is file FILE.
790   If it does, store 1 in *MUST_MAKE_PTR.
791   In the process, update any non-intermediate files
792   that FILE depends on (including FILE itself).
793   Return nonzero if any updating failed.  */
794
795static int
796check_dep (file, depth, this_mtime, must_make_ptr)
797     struct file *file;
798     unsigned int depth;
799     FILE_TIMESTAMP this_mtime;
800     int *must_make_ptr;
801{
802  register struct dep *d;
803  int dep_status = 0;
804
805  ++depth;
806  start_updating (file);
807
808  if (!file->intermediate)
809    /* If this is a non-intermediate file, update it and record
810       whether it is newer than THIS_MTIME.  */
811    {
812      FILE_TIMESTAMP mtime;
813      dep_status = update_file (file, depth);
814      check_renamed (file);
815      mtime = file_mtime (file);
816      check_renamed (file);
817      if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
818        *must_make_ptr = 1;
819    }
820  else
821    {
822      /* FILE is an intermediate file.  */
823      FILE_TIMESTAMP mtime;
824
825      if (!file->phony && file->cmds == 0 && !file->tried_implicit)
826        {
827          if (try_implicit_rule (file, depth))
828            DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
829          else
830            DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
831          file->tried_implicit = 1;
832        }
833      if (file->cmds == 0 && !file->is_target
834          && default_file != 0 && default_file->cmds != 0)
835        {
836          DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
837          file->cmds = default_file->cmds;
838        }
839
840      /* If the intermediate file actually exists
841         and is newer, then we should remake from it.  */
842      check_renamed (file);
843      mtime = file_mtime (file);
844      check_renamed (file);
845      if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
846        *must_make_ptr = 1;
847          /* Otherwise, update all non-intermediate files we depend on,
848             if necessary, and see whether any of them is more
849             recent than the file on whose behalf we are checking.  */
850      else
851        {
852          register struct dep *lastd;
853
854          lastd = 0;
855          d = file->deps;
856          while (d != 0)
857            {
858              if (is_updating (d->file))
859                {
860                  error (NILF, _("Circular %s <- %s dependency dropped."),
861                         file->name, d->file->name);
862                  if (lastd == 0)
863                    {
864                      file->deps = d->next;
865                      free ((char *) d);
866                      d = file->deps;
867                    }
868                  else
869                    {
870                      lastd->next = d->next;
871                      free ((char *) d);
872                      d = lastd->next;
873                    }
874                  continue;
875                }
876
877              d->file->parent = file;
878              dep_status |= check_dep (d->file, depth, this_mtime,
879                                       must_make_ptr);
880              check_renamed (d->file);
881              if (dep_status != 0 && !keep_going_flag)
882                break;
883
884              if (d->file->command_state == cs_running
885                  || d->file->command_state == cs_deps_running)
886                /* Record that some of FILE's deps are still being made.
887                   This tells the upper levels to wait on processing it until
888                   the commands are finished.  */
889                set_command_state (file, cs_deps_running);
890
891              lastd = d;
892              d = d->next;
893            }
894        }
895    }
896
897  finish_updating (file);
898  return dep_status;
899}
900
901/* Touch FILE.  Return zero if successful, one if not.  */
902
903#define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
904
905static int
906touch_file (file)
907     register struct file *file;
908{
909  if (!silent_flag)
910    message (0, "touch %s", file->name);
911
912#ifndef NO_ARCHIVES
913  if (ar_name (file->name))
914    return ar_touch (file->name);
915  else
916#endif
917    {
918      int fd = open (file->name, O_RDWR | O_CREAT, 0666);
919
920      if (fd < 0)
921        TOUCH_ERROR ("touch: open: ");
922      else
923        {
924          struct stat statbuf;
925          char buf;
926          int status;
927
928          do
929            status = fstat (fd, &statbuf);
930          while (status < 0 && EINTR_SET);
931
932          if (status < 0)
933            TOUCH_ERROR ("touch: fstat: ");
934          /* Rewrite character 0 same as it already is.  */
935          if (read (fd, &buf, 1) < 0)
936            TOUCH_ERROR ("touch: read: ");
937          if (lseek (fd, 0L, 0) < 0L)
938            TOUCH_ERROR ("touch: lseek: ");
939          if (write (fd, &buf, 1) < 0)
940            TOUCH_ERROR ("touch: write: ");
941          /* If file length was 0, we just
942             changed it, so change it back.  */
943          if (statbuf.st_size == 0)
944            {
945              (void) close (fd);
946              fd = open (file->name, O_RDWR | O_TRUNC, 0666);
947              if (fd < 0)
948                TOUCH_ERROR ("touch: open: ");
949            }
950          (void) close (fd);
951        }
952    }
953
954  return 0;
955}
956
957/* Having checked and updated the dependencies of FILE,
958   do whatever is appropriate to remake FILE itself.
959   Return the status from executing FILE's commands.  */
960
961static void
962remake_file (file)
963     struct file *file;
964{
965  if (file->cmds == 0)
966    {
967      if (file->phony)
968        /* Phony target.  Pretend it succeeded.  */
969        file->update_status = 0;
970      else if (file->is_target)
971        /* This is a nonexistent target file we cannot make.
972           Pretend it was successfully remade.  */
973        file->update_status = 0;
974      else
975        {
976          const char *msg_noparent
977            = _("%sNo rule to make target `%s'%s");
978          const char *msg_parent
979            = _("%sNo rule to make target `%s', needed by `%s'%s");
980
981          /* This is a dependency file we cannot remake.  Fail.  */
982          if (!keep_going_flag && !file->dontcare)
983            {
984              if (file->parent == 0)
985                fatal (NILF, msg_noparent, "", file->name, "");
986
987              fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
988            }
989
990          if (!file->dontcare)
991            {
992              if (file->parent == 0)
993                error (NILF, msg_noparent, "*** ", file->name, ".");
994              else
995                error (NILF, msg_parent, "*** ",
996                       file->name, file->parent->name, ".");
997            }
998          file->update_status = 2;
999        }
1000    }
1001  else
1002    {
1003      chop_commands (file->cmds);
1004
1005      /* The normal case: start some commands.  */
1006      if (!touch_flag || file->cmds->any_recurse)
1007        {
1008          execute_file_commands (file);
1009          return;
1010        }
1011
1012      /* This tells notice_finished_file it is ok to touch the file.  */
1013      file->update_status = 0;
1014    }
1015
1016  /* This does the touching under -t.  */
1017  notice_finished_file (file);
1018}
1019
1020/* Return the mtime of a file, given a `struct file'.
1021   Caches the time in the struct file to avoid excess stat calls.
1022
1023   If the file is not found, and SEARCH is nonzero, VPATH searching and
1024   replacement is done.  If that fails, a library (-lLIBNAME) is tried and
1025   the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1026   FILE.  */
1027
1028FILE_TIMESTAMP
1029f_mtime (file, search)
1030     register struct file *file;
1031     int search;
1032{
1033  FILE_TIMESTAMP mtime;
1034
1035  /* File's mtime is not known; must get it from the system.  */
1036
1037#ifndef NO_ARCHIVES
1038  if (ar_name (file->name))
1039    {
1040      /* This file is an archive-member reference.  */
1041
1042      char *arname, *memname;
1043      struct file *arfile;
1044      time_t memtime;
1045      int arname_used = 0;
1046      time_t member_date;
1047
1048      /* Find the archive's name.  */
1049      ar_parse_name (file->name, &arname, &memname);
1050
1051      /* Find the modification time of the archive itself.
1052         Also allow for its name to be changed via VPATH search.  */
1053      arfile = lookup_file (arname);
1054      if (arfile == 0)
1055        {
1056          arfile = enter_file (arname);
1057          arname_used = 1;
1058        }
1059      mtime = f_mtime (arfile, search);
1060      check_renamed (arfile);
1061      if (search && strcmp (arfile->hname, arname))
1062        {
1063          /* The archive's name has changed.
1064             Change the archive-member reference accordingly.  */
1065
1066          char *name;
1067          unsigned int arlen, memlen;
1068
1069          if (!arname_used)
1070            {
1071              free (arname);
1072              arname_used = 1;
1073            }
1074
1075          arname = arfile->hname;
1076          arlen = strlen (arname);
1077          memlen = strlen (memname);
1078
1079          /* free (file->name); */
1080
1081          name = (char *) xmalloc (arlen + 1 + memlen + 2);
1082          bcopy (arname, name, arlen);
1083          name[arlen] = '(';
1084          bcopy (memname, name + arlen + 1, memlen);
1085          name[arlen + 1 + memlen] = ')';
1086          name[arlen + 1 + memlen + 1] = '\0';
1087
1088          /* If the archive was found with GPATH, make the change permanent;
1089             otherwise defer it until later.  */
1090          if (arfile->name == arfile->hname)
1091            rename_file (file, name);
1092          else
1093            rehash_file (file, name);
1094          check_renamed (file);
1095        }
1096
1097      if (!arname_used)
1098        free (arname);
1099      free (memname);
1100
1101      if (mtime == NONEXISTENT_MTIME)
1102        /* The archive doesn't exist, so its members don't exist either.  */
1103        return NONEXISTENT_MTIME;
1104
1105      member_date = ar_member_date (file->hname);
1106      mtime = (member_date == (time_t) -1
1107               ? NONEXISTENT_MTIME
1108               : file_timestamp_cons (file->hname, member_date, 0));
1109    }
1110  else
1111#endif
1112    {
1113      mtime = name_mtime (file->name);
1114
1115      if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1116        {
1117          /* If name_mtime failed, search VPATH.  */
1118          char *name = file->name;
1119          if (vpath_search (&name, &mtime)
1120              /* Last resort, is it a library (-lxxx)?  */
1121              || (name[0] == '-' && name[1] == 'l'
1122                  && library_search (&name, &mtime)))
1123            {
1124              if (mtime != UNKNOWN_MTIME)
1125                /* vpath_search and library_search store UNKNOWN_MTIME
1126                   if they didn't need to do a stat call for their work.  */
1127                file->last_mtime = mtime;
1128
1129              /* If we found it in VPATH, see if it's in GPATH too; if so,
1130                 change the name right now; if not, defer until after the
1131                 dependencies are updated. */
1132              if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1133                {
1134                  rename_file (file, name);
1135                  check_renamed (file);
1136                  return file_mtime (file);
1137                }
1138
1139              rehash_file (file, name);
1140              check_renamed (file);
1141              mtime = name_mtime (name);
1142            }
1143        }
1144    }
1145
1146  {
1147    /* Files can have bogus timestamps that nothing newly made will be
1148       "newer" than.  Updating their dependents could just result in loops.
1149       So notify the user of the anomaly with a warning.
1150
1151       We only need to do this once, for now. */
1152
1153    if (!clock_skew_detected
1154        && mtime != NONEXISTENT_MTIME
1155        && !file->updated)
1156      {
1157        static FILE_TIMESTAMP now;
1158
1159        FILE_TIMESTAMP adjusted_mtime = mtime;
1160
1161#if defined(WINDOWS32) || defined(__MSDOS__)
1162        FILE_TIMESTAMP adjustment;
1163#ifdef WINDOWS32
1164        /* FAT filesystems round time to the nearest even second!
1165           Allow for any file (NTFS or FAT) to perhaps suffer from this
1166           brain damage.  */
1167        adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1168                       && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1169                      ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1170                      : 0);
1171#else
1172        /* On DJGPP under Windows 98 and Windows NT, FAT filesystems can
1173           set file times up to 3 seconds into the future!  The bug doesn't
1174           occur in plain DOS or in Windows 95, but we play it safe.  */
1175        adjustment = (FILE_TIMESTAMP) 3 << FILE_TIMESTAMP_LO_BITS;
1176#endif
1177        if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1178          adjusted_mtime -= adjustment;
1179#endif
1180
1181        /* If the file's time appears to be in the future, udpate our
1182           concept of the present and try once more.  */
1183        if (now < adjusted_mtime
1184            && (now = file_timestamp_now ()) < adjusted_mtime)
1185          {
1186            char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1187            char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1188
1189            file_timestamp_sprintf (mtimebuf, mtime);
1190            file_timestamp_sprintf (nowbuf, now);
1191            error (NILF, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1192                   file->name, mtimebuf, nowbuf);
1193            clock_skew_detected = 1;
1194          }
1195      }
1196  }
1197
1198  /* Store the mtime into all the entries for this file.  */
1199  if (file->double_colon)
1200    file = file->double_colon;
1201
1202  do
1203    {
1204      /* If this file is not implicit but it is intermediate then it was
1205         made so by the .INTERMEDIATE target.  If this file has never
1206         been built by us but was found now, it existed before make
1207         started.  So, turn off the intermediate bit so make doesn't
1208         delete it, since it didn't create it.  */
1209      if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1210          && file->command_state == cs_not_started
1211          && !file->tried_implicit && file->intermediate)
1212        file->intermediate = 0;
1213
1214      file->last_mtime = mtime;
1215      file = file->prev;
1216    }
1217  while (file != 0);
1218
1219  return mtime;
1220}
1221
1222
1223/* Return the mtime of the file or archive-member reference NAME.  */
1224
1225static FILE_TIMESTAMP
1226name_mtime (name)
1227     register char *name;
1228{
1229  struct stat st;
1230
1231 while (stat (name, &st) != 0)
1232   if (errno != EINTR)
1233     {
1234       if (errno != ENOENT && errno != ENOTDIR)
1235         perror_with_name ("stat:", name);
1236       return NONEXISTENT_MTIME;
1237     }
1238
1239  return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1240}
1241
1242
1243/* Search for a library file specified as -lLIBNAME, searching for a
1244   suitable library file in the system library directories and the VPATH
1245   directories.  */
1246
1247static int
1248library_search (lib, mtime_ptr)
1249     char **lib;
1250     FILE_TIMESTAMP *mtime_ptr;
1251{
1252  static char *dirs[] =
1253    {
1254#ifndef _AMIGA
1255      "/lib",
1256      "/usr/lib",
1257#endif
1258#if defined(WINDOWS32) && !defined(LIBDIR)
1259/*
1260 * This is completely up to the user at product install time. Just define
1261 * a placeholder.
1262 */
1263#define LIBDIR "."
1264#endif
1265      LIBDIR,                   /* Defined by configuration.  */
1266      0
1267    };
1268
1269  static char *libpatterns = NULL;
1270
1271  char *libname = &(*lib)[2];   /* Name without the `-l'.  */
1272  FILE_TIMESTAMP mtime;
1273
1274  /* Loop variables for the libpatterns value.  */
1275  char *p, *p2;
1276  unsigned int len;
1277
1278  char *file, **dp;
1279
1280  /* If we don't have libpatterns, get it.  */
1281  if (!libpatterns)
1282    {
1283      int save = warn_undefined_variables_flag;
1284      warn_undefined_variables_flag = 0;
1285
1286      libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1287
1288      warn_undefined_variables_flag = save;
1289    }
1290
1291  /* Loop through all the patterns in .LIBPATTERNS, and search on each one.  */
1292  p2 = libpatterns;
1293  while ((p = find_next_token (&p2, &len)) != 0)
1294    {
1295      static char *buf = NULL;
1296      static int buflen = 0;
1297      static int libdir_maxlen = -1;
1298      char *libbuf = variable_expand ("");
1299
1300      /* Expand the pattern using LIBNAME as a replacement.  */
1301      {
1302        char c = p[len];
1303        char *p3, *p4;
1304
1305        p[len] = '\0';
1306        p3 = find_percent (p);
1307        if (!p3)
1308          {
1309            /* Give a warning if there is no pattern, then remove the
1310               pattern so it's ignored next time.  */
1311            error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1312            for (; len; --len, ++p)
1313              *p = ' ';
1314            *p = c;
1315            continue;
1316          }
1317        p4 = variable_buffer_output (libbuf, p, p3-p);
1318        p4 = variable_buffer_output (p4, libname, strlen (libname));
1319        p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1320        p[len] = c;
1321      }
1322
1323      /* Look first for `libNAME.a' in the current directory.  */
1324      mtime = name_mtime (libbuf);
1325      if (mtime != NONEXISTENT_MTIME)
1326        {
1327          *lib = xstrdup (libbuf);
1328          if (mtime_ptr != 0)
1329            *mtime_ptr = mtime;
1330          return 1;
1331        }
1332
1333      /* Now try VPATH search on that.  */
1334
1335      file = libbuf;
1336      if (vpath_search (&file, mtime_ptr))
1337        {
1338          *lib = file;
1339          return 1;
1340        }
1341
1342      /* Now try the standard set of directories.  */
1343
1344      if (!buflen)
1345        {
1346          for (dp = dirs; *dp != 0; ++dp)
1347            {
1348              int l = strlen (*dp);
1349              if (l > libdir_maxlen)
1350                libdir_maxlen = l;
1351            }
1352          buflen = strlen (libbuf);
1353          buf = xmalloc(libdir_maxlen + buflen + 2);
1354        }
1355      else if (buflen < strlen (libbuf))
1356        {
1357          buflen = strlen (libbuf);
1358          buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1359        }
1360
1361      for (dp = dirs; *dp != 0; ++dp)
1362        {
1363          sprintf (buf, "%s/%s", *dp, libbuf);
1364          mtime = name_mtime (buf);
1365          if (mtime != NONEXISTENT_MTIME)
1366            {
1367              *lib = xstrdup (buf);
1368              if (mtime_ptr != 0)
1369                *mtime_ptr = mtime;
1370              return 1;
1371            }
1372        }
1373    }
1374
1375  return 0;
1376}
Note: See TracBrowser for help on using the repository browser.