- Update manual description for pattern rule search algorithm
[make.git] / remake.c
bloba70cbeeb21ab8fa29e282e56031c848f93378225
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
20 #include "filedef.h"
21 #include "job.h"
22 #include "commands.h"
23 #include "dep.h"
24 #include "variable.h"
25 #include "debug.h"
27 #include <assert.h>
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #else
32 #include <sys/file.h>
33 #endif
35 #ifdef VMS
36 #include <starlet.h>
37 #endif
38 #ifdef WINDOWS32
39 #include <io.h>
40 #endif
42 extern int try_implicit_rule (struct file *file, unsigned int depth);
45 /* The test for circular dependencies is based on the 'updating' bit in
46 `struct file'. However, double colon targets have seperate `struct
47 file's; make sure we always use the base of the double colon chain. */
49 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
50 ->updating = 1)
51 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 0)
53 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating)
57 /* Incremented when a command is started (under -n, when one would be). */
58 unsigned int commands_started = 0;
60 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
61 static unsigned int considered;
63 static int update_file (struct file *file, unsigned int depth);
64 static int update_file_1 (struct file *file, unsigned int depth);
65 static int check_dep (struct file *file, unsigned int depth,
66 FILE_TIMESTAMP this_mtime, int *must_make_ptr);
67 static int touch_file (struct file *file);
68 static void remake_file (struct file *file);
69 static FILE_TIMESTAMP name_mtime (const char *name);
70 static const char *library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr);
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.
76 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
77 and -n should be disabled for them unless they were also command-line
78 targets, and we should only make one goal at a time and return as soon as
79 one goal whose `changed' member is nonzero is successfully made. */
81 int
82 update_goal_chain (struct dep *goals)
84 int t = touch_flag, q = question_flag, n = just_print_flag;
85 int status = -1;
87 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
88 : file_mtime (file))
90 /* Duplicate the chain so we can remove things from it. */
92 goals = copy_dep_chain (goals);
95 /* Clear the `changed' flag of each goal in the chain.
96 We will use the flag below to notice when any commands
97 have actually been run for a target. When no commands
98 have been run, we give an "up to date" diagnostic. */
100 struct dep *g;
101 for (g = goals; g != 0; g = g->next)
102 g->changed = 0;
105 /* All files start with the considered bit 0, so the global value is 1. */
106 considered = 1;
108 /* Update all the goals until they are all finished. */
110 while (goals != 0)
112 register struct dep *g, *lastgoal;
114 /* Start jobs that are waiting for the load to go down. */
116 start_waiting_jobs ();
118 /* Wait for a child to die. */
120 reap_children (1, 0);
122 lastgoal = 0;
123 g = goals;
124 while (g != 0)
126 /* Iterate over all double-colon entries for this file. */
127 struct file *file;
128 int stop = 0, any_not_updated = 0;
130 for (file = g->file->double_colon ? g->file->double_colon : g->file;
131 file != NULL;
132 file = file->prev)
134 unsigned int ocommands_started;
135 int x;
136 check_renamed (file);
137 if (rebuilding_makefiles)
139 if (file->cmd_target)
141 touch_flag = t;
142 question_flag = q;
143 just_print_flag = n;
145 else
146 touch_flag = question_flag = just_print_flag = 0;
149 /* Save the old value of `commands_started' so we can compare
150 later. It will be incremented when any commands are
151 actually run. */
152 ocommands_started = commands_started;
154 x = update_file (file, rebuilding_makefiles ? 1 : 0);
155 check_renamed (file);
157 /* Set the goal's `changed' flag if any commands were started
158 by calling update_file above. We check this flag below to
159 decide when to give an "up to date" diagnostic. */
160 if (commands_started > ocommands_started)
161 g->changed = 1;
163 /* If we updated a file and STATUS was not already 1, set it to
164 1 if updating failed, or to 0 if updating succeeded. Leave
165 STATUS as it is if no updating was done. */
167 stop = 0;
168 if ((x != 0 || file->updated) && status < 1)
170 if (file->update_status != 0)
172 /* Updating failed, or -q triggered. The STATUS value
173 tells our caller which. */
174 status = file->update_status;
175 /* If -q just triggered, stop immediately. It doesn't
176 matter how much more we run, since we already know
177 the answer to return. */
178 stop = (question_flag && !keep_going_flag
179 && !rebuilding_makefiles);
181 else
183 FILE_TIMESTAMP mtime = MTIME (file);
184 check_renamed (file);
186 if (file->updated && g->changed &&
187 mtime != file->mtime_before_update)
189 /* Updating was done. If this is a makefile and
190 just_print_flag or question_flag is set (meaning
191 -n or -q was given and this file was specified
192 as a command-line target), don't change STATUS.
193 If STATUS is changed, we will get re-exec'd, and
194 enter an infinite loop. */
195 if (!rebuilding_makefiles
196 || (!just_print_flag && !question_flag))
197 status = 0;
198 if (rebuilding_makefiles && file->dontcare)
199 /* This is a default makefile; stop remaking. */
200 stop = 1;
205 /* Keep track if any double-colon entry is not finished.
206 When they are all finished, the goal is finished. */
207 any_not_updated |= !file->updated;
209 if (stop)
210 break;
213 /* Reset FILE since it is null at the end of the loop. */
214 file = g->file;
216 if (stop || !any_not_updated)
218 /* If we have found nothing whatever to do for the goal,
219 print a message saying nothing needs doing. */
221 if (!rebuilding_makefiles
222 /* If the update_status is zero, we updated successfully
223 or not at all. G->changed will have been set above if
224 any commands were actually started for this goal. */
225 && file->update_status == 0 && !g->changed
226 /* Never give a message under -s or -q. */
227 && !silent_flag && !question_flag)
228 message (1, ((file->phony || file->cmds == 0)
229 ? _("Nothing to be done for `%s'.")
230 : _("`%s' is up to date.")),
231 file->name);
233 /* This goal is finished. Remove it from the chain. */
234 if (lastgoal == 0)
235 goals = g->next;
236 else
237 lastgoal->next = g->next;
239 /* Free the storage. */
240 free (g);
242 g = lastgoal == 0 ? goals : lastgoal->next;
244 if (stop)
245 break;
247 else
249 lastgoal = g;
250 g = g->next;
254 /* If we reached the end of the dependency graph toggle the considered
255 flag for the next pass. */
256 if (g == 0)
257 considered = !considered;
260 if (rebuilding_makefiles)
262 touch_flag = t;
263 question_flag = q;
264 just_print_flag = n;
267 return status;
270 /* If FILE is not up to date, execute the commands for it.
271 Return 0 if successful, 1 if unsuccessful;
272 but with some flag settings, just call `exit' if unsuccessful.
274 DEPTH is the depth in recursions of this function.
275 We increment it during the consideration of our dependencies,
276 then decrement it again after finding out whether this file
277 is out of date.
279 If there are multiple double-colon entries for FILE,
280 each is considered in turn. */
282 static int
283 update_file (struct file *file, unsigned int depth)
285 register int status = 0;
286 register struct file *f;
288 f = file->double_colon ? file->double_colon : file;
290 /* Prune the dependency graph: if we've already been here on _this_
291 pass through the dependency graph, we don't have to go any further.
292 We won't reap_children until we start the next pass, so no state
293 change is possible below here until then. */
294 if (f->considered == considered)
296 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
297 return f->command_state == cs_finished ? f->update_status : 0;
300 /* This loop runs until we start commands for a double colon rule, or until
301 the chain is exhausted. */
302 for (; f != 0; f = f->prev)
304 f->considered = considered;
306 status |= update_file_1 (f, depth);
307 check_renamed (f);
309 /* Clean up any alloca() used during the update. */
310 alloca (0);
312 /* If we got an error, don't bother with double_colon etc. */
313 if (status != 0 && !keep_going_flag)
314 return status;
316 if (f->command_state == cs_running
317 || f->command_state == cs_deps_running)
319 /* Don't run the other :: rules for this
320 file until this rule is finished. */
321 status = 0;
322 break;
326 /* Process the remaining rules in the double colon chain so they're marked
327 considered. Start their prerequisites, too. */
328 if (file->double_colon)
329 for (; f != 0 ; f = f->prev)
331 struct dep *d;
333 f->considered = considered;
335 for (d = f->deps; d != 0; d = d->next)
336 status |= update_file (d->file, depth + 1);
339 return status;
342 /* Show a message stating the target failed to build. */
344 static void
345 complain (const struct file *file)
347 const char *msg_noparent
348 = _("%sNo rule to make target `%s'%s");
349 const char *msg_parent
350 = _("%sNo rule to make target `%s', needed by `%s'%s");
352 if (!keep_going_flag)
354 if (file->parent == 0)
355 fatal (NILF, msg_noparent, "", file->name, "");
357 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
360 if (file->parent == 0)
361 error (NILF, msg_noparent, "*** ", file->name, ".");
362 else
363 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
366 /* Consider a single `struct file' and update it as appropriate. */
368 static int
369 update_file_1 (struct file *file, unsigned int depth)
371 FILE_TIMESTAMP this_mtime;
372 int noexist, must_make, deps_changed;
373 int dep_status = 0;
374 struct file *ofile;
375 struct dep *d, *ad;
376 struct dep amake;
377 int running = 0;
379 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
381 if (file->updated)
383 if (file->update_status > 0)
385 DBF (DB_VERBOSE,
386 _("Recently tried and failed to update file `%s'.\n"));
388 /* If the file we tried to make is marked dontcare then no message
389 was printed about it when it failed during the makefile rebuild.
390 If we're trying to build it again in the normal rebuild, print a
391 message now. */
392 if (file->dontcare && !rebuilding_makefiles)
394 file->dontcare = 0;
395 complain (file);
398 return file->update_status;
401 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
402 return 0;
405 switch (file->command_state)
407 case cs_not_started:
408 case cs_deps_running:
409 break;
410 case cs_running:
411 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
412 return 0;
413 case cs_finished:
414 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
415 return file->update_status;
416 default:
417 abort ();
420 ++depth;
422 /* Notice recursive update of the same file. */
423 start_updating (file);
425 /* We might change file if we find a different one via vpath;
426 remember this one to turn off updating. */
427 ofile = file;
429 /* Looking at the file's modtime beforehand allows the possibility
430 that its name may be changed by a VPATH search, and thus it may
431 not need an implicit rule. If this were not done, the file
432 might get implicit commands that apply to its initial name, only
433 to have that name replaced with another found by VPATH search. */
435 this_mtime = file_mtime (file);
436 check_renamed (file);
437 noexist = this_mtime == NONEXISTENT_MTIME;
438 if (noexist)
439 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
440 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
441 && file->low_resolution_time)
443 /* Avoid spurious rebuilds due to low resolution time stamps. */
444 int ns = FILE_TIMESTAMP_NS (this_mtime);
445 if (ns != 0)
446 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
447 file->name);
448 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
451 must_make = noexist;
453 /* If file was specified as a target with no commands,
454 come up with some default commands. */
456 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
458 if (try_implicit_rule (file, depth))
459 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
460 else
461 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
462 file->tried_implicit = 1;
464 if (file->cmds == 0 && !file->is_target
465 && default_file != 0 && default_file->cmds != 0)
467 DBF (DB_IMPLICIT, _("Using default recipe for `%s'.\n"));
468 file->cmds = default_file->cmds;
471 /* Update all non-intermediate files we depend on, if necessary, and see
472 whether any of them is more recent than this file. We need to walk our
473 deps, AND the deps of any also_make targets to ensure everything happens
474 in the correct order. */
476 amake.file = file;
477 amake.next = file->also_make;
478 ad = &amake;
479 while (ad)
481 struct dep *lastd = 0;
483 /* Find the deps we're scanning */
484 d = ad->file->deps;
485 ad = ad->next;
487 while (d)
489 FILE_TIMESTAMP mtime;
490 int maybe_make;
491 int dontcare = 0;
493 check_renamed (d->file);
495 mtime = file_mtime (d->file);
496 check_renamed (d->file);
498 if (is_updating (d->file))
500 error (NILF, _("Circular %s <- %s dependency dropped."),
501 file->name, d->file->name);
502 /* We cannot free D here because our the caller will still have
503 a reference to it when we were called recursively via
504 check_dep below. */
505 if (lastd == 0)
506 file->deps = d->next;
507 else
508 lastd->next = d->next;
509 d = d->next;
510 continue;
513 d->file->parent = file;
514 maybe_make = must_make;
516 /* Inherit dontcare flag from our parent. */
517 if (rebuilding_makefiles)
519 dontcare = d->file->dontcare;
520 d->file->dontcare = file->dontcare;
523 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
525 /* Restore original dontcare flag. */
526 if (rebuilding_makefiles)
527 d->file->dontcare = dontcare;
529 if (! d->ignore_mtime)
530 must_make = maybe_make;
532 check_renamed (d->file);
535 register struct file *f = d->file;
536 if (f->double_colon)
537 f = f->double_colon;
540 running |= (f->command_state == cs_running
541 || f->command_state == cs_deps_running);
542 f = f->prev;
544 while (f != 0);
547 if (dep_status != 0 && !keep_going_flag)
548 break;
550 if (!running)
551 /* The prereq is considered changed if the timestamp has changed while
552 it was built, OR it doesn't exist. */
553 d->changed = ((file_mtime (d->file) != mtime)
554 || (mtime == NONEXISTENT_MTIME));
556 lastd = d;
557 d = d->next;
561 /* Now we know whether this target needs updating.
562 If it does, update all the intermediate files we depend on. */
564 if (must_make || always_make_flag)
566 for (d = file->deps; d != 0; d = d->next)
567 if (d->file->intermediate)
569 int dontcare = 0;
571 FILE_TIMESTAMP mtime = file_mtime (d->file);
572 check_renamed (d->file);
573 d->file->parent = file;
575 /* Inherit dontcare flag from our parent. */
576 if (rebuilding_makefiles)
578 dontcare = d->file->dontcare;
579 d->file->dontcare = file->dontcare;
583 dep_status |= update_file (d->file, depth);
585 /* Restore original dontcare flag. */
586 if (rebuilding_makefiles)
587 d->file->dontcare = dontcare;
589 check_renamed (d->file);
592 register struct file *f = d->file;
593 if (f->double_colon)
594 f = f->double_colon;
597 running |= (f->command_state == cs_running
598 || f->command_state == cs_deps_running);
599 f = f->prev;
601 while (f != 0);
604 if (dep_status != 0 && !keep_going_flag)
605 break;
607 if (!running)
608 d->changed = ((file->phony && file->cmds != 0)
609 || file_mtime (d->file) != mtime);
613 finish_updating (file);
614 finish_updating (ofile);
616 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
618 if (running)
620 set_command_state (file, cs_deps_running);
621 --depth;
622 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
623 return 0;
626 /* If any dependency failed, give up now. */
628 if (dep_status != 0)
630 file->update_status = dep_status;
631 notice_finished_file (file);
633 --depth;
635 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
637 if (depth == 0 && keep_going_flag
638 && !just_print_flag && !question_flag)
639 error (NILF,
640 _("Target `%s' not remade because of errors."), file->name);
642 return dep_status;
645 if (file->command_state == cs_deps_running)
646 /* The commands for some deps were running on the last iteration, but
647 they have finished now. Reset the command_state to not_started to
648 simplify later bookkeeping. It is important that we do this only
649 when the prior state was cs_deps_running, because that prior state
650 was definitely propagated to FILE's also_make's by set_command_state
651 (called above), but in another state an also_make may have
652 independently changed to finished state, and we would confuse that
653 file's bookkeeping (updated, but not_started is bogus state). */
654 set_command_state (file, cs_not_started);
656 /* Now record which prerequisites are more
657 recent than this file, so we can define $?. */
659 deps_changed = 0;
660 for (d = file->deps; d != 0; d = d->next)
662 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
663 check_renamed (d->file);
665 if (! d->ignore_mtime)
667 #if 1
668 /* %%% In version 4, remove this code completely to
669 implement not remaking deps if their deps are newer
670 than their parents. */
671 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
672 /* We must remake if this dep does not
673 exist and is not intermediate. */
674 must_make = 1;
675 #endif
677 /* Set DEPS_CHANGED if this dep actually changed. */
678 deps_changed |= d->changed;
681 /* Set D->changed if either this dep actually changed,
682 or its dependent, FILE, is older or does not exist. */
683 d->changed |= noexist || d_mtime > this_mtime;
685 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
687 const char *fmt = 0;
689 if (d->ignore_mtime)
691 if (ISDB (DB_VERBOSE))
692 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
694 else if (d_mtime == NONEXISTENT_MTIME)
696 if (ISDB (DB_BASIC))
697 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
699 else if (d->changed)
701 if (ISDB (DB_BASIC))
702 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
704 else if (ISDB (DB_VERBOSE))
705 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
707 if (fmt)
709 print_spaces (depth);
710 printf (fmt, dep_name (d), file->name);
711 fflush (stdout);
716 /* Here depth returns to the value it had when we were called. */
717 depth--;
719 if (file->double_colon && file->deps == 0)
721 must_make = 1;
722 DBF (DB_BASIC,
723 _("Target `%s' is double-colon and has no prerequisites.\n"));
725 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
726 && !always_make_flag)
728 must_make = 0;
729 DBF (DB_VERBOSE,
730 _("No recipe for `%s' and no prerequisites actually changed.\n"));
732 else if (!must_make && file->cmds != 0 && always_make_flag)
734 must_make = 1;
735 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
738 if (!must_make)
740 if (ISDB (DB_VERBOSE))
742 print_spaces (depth);
743 printf (_("No need to remake target `%s'"), file->name);
744 if (!streq (file->name, file->hname))
745 printf (_("; using VPATH name `%s'"), file->hname);
746 puts (".");
747 fflush (stdout);
750 notice_finished_file (file);
752 /* Since we don't need to remake the file, convert it to use the
753 VPATH filename if we found one. hfile will be either the
754 local name if no VPATH or the VPATH name if one was found. */
756 while (file)
758 file->name = file->hname;
759 file = file->prev;
762 return 0;
765 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
767 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
768 VPATH. */
769 if (!streq(file->name, file->hname))
771 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
772 file->ignore_vpath = 1;
775 /* Now, take appropriate actions to remake the file. */
776 remake_file (file);
778 if (file->command_state != cs_finished)
780 DBF (DB_VERBOSE, _("Recipe of `%s' is being run.\n"));
781 return 0;
784 switch (file->update_status)
786 case 2:
787 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
788 break;
789 case 0:
790 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
791 break;
792 case 1:
793 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
794 break;
795 default:
796 assert (file->update_status >= 0 && file->update_status <= 2);
797 break;
800 file->updated = 1;
801 return file->update_status;
804 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
805 files listed in its `also_make' member. Under -t, this function also
806 touches FILE.
808 On return, FILE->update_status will no longer be -1 if it was. */
810 void
811 notice_finished_file (struct file *file)
813 struct dep *d;
814 int ran = file->command_state == cs_running;
815 int touched = 0;
817 file->command_state = cs_finished;
818 file->updated = 1;
820 if (touch_flag
821 /* The update status will be:
822 -1 if this target was not remade;
823 0 if 0 or more commands (+ or ${MAKE}) were run and won;
824 1 if some commands were run and lost.
825 We touch the target if it has commands which either were not run
826 or won when they ran (i.e. status is 0). */
827 && file->update_status == 0)
829 if (file->cmds != 0 && file->cmds->any_recurse)
831 /* If all the command lines were recursive,
832 we don't want to do the touching. */
833 unsigned int i;
834 for (i = 0; i < file->cmds->ncommand_lines; ++i)
835 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
836 goto have_nonrecursing;
838 else
840 have_nonrecursing:
841 if (file->phony)
842 file->update_status = 0;
843 /* According to POSIX, -t doesn't affect targets with no cmds. */
844 else if (file->cmds != 0)
846 /* Should set file's modification date and do nothing else. */
847 file->update_status = touch_file (file);
849 /* Pretend we ran a real touch command, to suppress the
850 "`foo' is up to date" message. */
851 commands_started++;
853 /* Request for the timestamp to be updated (and distributed
854 to the double-colon entries). Simply setting ran=1 would
855 almost have done the trick, but messes up with the also_make
856 updating logic below. */
857 touched = 1;
862 if (file->mtime_before_update == UNKNOWN_MTIME)
863 file->mtime_before_update = file->last_mtime;
865 if ((ran && !file->phony) || touched)
867 int i = 0;
869 /* If -n, -t, or -q and all the commands are recursive, we ran them so
870 really check the target's mtime again. Otherwise, assume the target
871 would have been updated. */
873 if ((question_flag || just_print_flag || touch_flag) && file->cmds)
875 for (i = file->cmds->ncommand_lines; i > 0; --i)
876 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
877 break;
880 /* If there were no commands at all, it's always new. */
882 else if (file->is_target && file->cmds == 0)
883 i = 1;
885 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
888 if (file->double_colon)
890 /* If this is a double colon rule and it is the last one to be
891 updated, propagate the change of modification time to all the
892 double-colon entries for this file.
894 We do it on the last update because it is important to handle
895 individual entries as separate rules with separate timestamps
896 while they are treated as targets and then as one rule with the
897 unified timestamp when they are considered as a prerequisite
898 of some target. */
900 struct file *f;
901 FILE_TIMESTAMP max_mtime = file->last_mtime;
903 /* Check that all rules were updated and at the same time find
904 the max timestamp. We assume UNKNOWN_MTIME is newer then
905 any other value. */
906 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
907 if (max_mtime != UNKNOWN_MTIME
908 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
909 max_mtime = f->last_mtime;
911 if (f == 0)
912 for (f = file->double_colon; f != 0; f = f->prev)
913 f->last_mtime = max_mtime;
916 if (ran && file->update_status != -1)
917 /* We actually tried to update FILE, which has
918 updated its also_make's as well (if it worked).
919 If it didn't work, it wouldn't work again for them.
920 So mark them as updated with the same status. */
921 for (d = file->also_make; d != 0; d = d->next)
923 d->file->command_state = cs_finished;
924 d->file->updated = 1;
925 d->file->update_status = file->update_status;
927 if (ran && !d->file->phony)
928 /* Fetch the new modification time.
929 We do this instead of just invalidating the cached time
930 so that a vpath_search can happen. Otherwise, it would
931 never be done because the target is already updated. */
932 f_mtime (d->file, 0);
934 else if (file->update_status == -1)
935 /* Nothing was done for FILE, but it needed nothing done.
936 So mark it now as "succeeded". */
937 file->update_status = 0;
940 /* Check whether another file (whose mtime is THIS_MTIME) needs updating on
941 account of a dependency which is file FILE. If it does, store 1 in
942 *MUST_MAKE_PTR. In the process, update any non-intermediate files that
943 FILE depends on (including FILE itself). Return nonzero if any updating
944 failed. */
946 static int
947 check_dep (struct file *file, unsigned int depth,
948 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
950 struct file *ofile;
951 struct dep *d;
952 int dep_status = 0;
954 ++depth;
955 start_updating (file);
957 /* We might change file if we find a different one via vpath;
958 remember this one to turn off updating. */
959 ofile = file;
961 if (file->phony || !file->intermediate)
963 /* If this is a non-intermediate file, update it and record whether it
964 is newer than THIS_MTIME. */
965 FILE_TIMESTAMP mtime;
966 dep_status = update_file (file, depth);
967 check_renamed (file);
968 mtime = file_mtime (file);
969 check_renamed (file);
970 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
971 *must_make_ptr = 1;
973 else
975 /* FILE is an intermediate file. */
976 FILE_TIMESTAMP mtime;
978 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
980 if (try_implicit_rule (file, depth))
981 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
982 else
983 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
984 file->tried_implicit = 1;
986 if (file->cmds == 0 && !file->is_target
987 && default_file != 0 && default_file->cmds != 0)
989 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
990 file->cmds = default_file->cmds;
993 check_renamed (file);
994 mtime = file_mtime (file);
995 check_renamed (file);
996 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
997 /* If the intermediate file actually exists and is newer, then we
998 should remake from it. */
999 *must_make_ptr = 1;
1000 else
1002 /* Otherwise, update all non-intermediate files we depend on, if
1003 necessary, and see whether any of them is more recent than the
1004 file on whose behalf we are checking. */
1005 struct dep *ld;
1006 int deps_running = 0;
1008 /* If this target is not running, set it's state so that we check it
1009 fresh. It could be it was checked as part of an order-only
1010 prerequisite and so wasn't rebuilt then, but should be now. */
1011 if (file->command_state != cs_running)
1012 set_command_state (file, cs_not_started);
1014 ld = 0;
1015 d = file->deps;
1016 while (d != 0)
1018 int maybe_make;
1020 if (is_updating (d->file))
1022 error (NILF, _("Circular %s <- %s dependency dropped."),
1023 file->name, d->file->name);
1024 if (ld == 0)
1026 file->deps = d->next;
1027 free_dep (d);
1028 d = file->deps;
1030 else
1032 ld->next = d->next;
1033 free_dep (d);
1034 d = ld->next;
1036 continue;
1039 d->file->parent = file;
1040 maybe_make = *must_make_ptr;
1041 dep_status |= check_dep (d->file, depth, this_mtime,
1042 &maybe_make);
1043 if (! d->ignore_mtime)
1044 *must_make_ptr = maybe_make;
1045 check_renamed (d->file);
1046 if (dep_status != 0 && !keep_going_flag)
1047 break;
1049 if (d->file->command_state == cs_running
1050 || d->file->command_state == cs_deps_running)
1051 deps_running = 1;
1053 ld = d;
1054 d = d->next;
1057 if (deps_running)
1058 /* Record that some of FILE's deps are still being made.
1059 This tells the upper levels to wait on processing it until the
1060 commands are finished. */
1061 set_command_state (file, cs_deps_running);
1065 finish_updating (file);
1066 finish_updating (ofile);
1068 return dep_status;
1071 /* Touch FILE. Return zero if successful, one if not. */
1073 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1075 static int
1076 touch_file (struct file *file)
1078 if (!silent_flag)
1079 message (0, "touch %s", file->name);
1081 #ifndef NO_ARCHIVES
1082 if (ar_name (file->name))
1083 return ar_touch (file->name);
1084 else
1085 #endif
1087 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1089 if (fd < 0)
1090 TOUCH_ERROR ("touch: open: ");
1091 else
1093 struct stat statbuf;
1094 char buf = 'x';
1095 int e;
1097 EINTRLOOP (e, fstat (fd, &statbuf));
1098 if (e < 0)
1099 TOUCH_ERROR ("touch: fstat: ");
1100 /* Rewrite character 0 same as it already is. */
1101 if (read (fd, &buf, 1) < 0)
1102 TOUCH_ERROR ("touch: read: ");
1103 if (lseek (fd, 0L, 0) < 0L)
1104 TOUCH_ERROR ("touch: lseek: ");
1105 if (write (fd, &buf, 1) < 0)
1106 TOUCH_ERROR ("touch: write: ");
1107 /* If file length was 0, we just
1108 changed it, so change it back. */
1109 if (statbuf.st_size == 0)
1111 (void) close (fd);
1112 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1113 if (fd < 0)
1114 TOUCH_ERROR ("touch: open: ");
1116 (void) close (fd);
1120 return 0;
1123 /* Having checked and updated the dependencies of FILE,
1124 do whatever is appropriate to remake FILE itself.
1125 Return the status from executing FILE's commands. */
1127 static void
1128 remake_file (struct file *file)
1130 if (file->cmds == 0)
1132 if (file->phony)
1133 /* Phony target. Pretend it succeeded. */
1134 file->update_status = 0;
1135 else if (file->is_target)
1136 /* This is a nonexistent target file we cannot make.
1137 Pretend it was successfully remade. */
1138 file->update_status = 0;
1139 else
1141 /* This is a dependency file we cannot remake. Fail. */
1142 if (!rebuilding_makefiles || !file->dontcare)
1143 complain (file);
1144 file->update_status = 2;
1147 else
1149 chop_commands (file->cmds);
1151 /* The normal case: start some commands. */
1152 if (!touch_flag || file->cmds->any_recurse)
1154 execute_file_commands (file);
1155 return;
1158 /* This tells notice_finished_file it is ok to touch the file. */
1159 file->update_status = 0;
1162 /* This does the touching under -t. */
1163 notice_finished_file (file);
1166 /* Return the mtime of a file, given a `struct file'.
1167 Caches the time in the struct file to avoid excess stat calls.
1169 If the file is not found, and SEARCH is nonzero, VPATH searching and
1170 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1171 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1172 FILE. */
1174 FILE_TIMESTAMP
1175 f_mtime (struct file *file, int search)
1177 FILE_TIMESTAMP mtime;
1179 /* File's mtime is not known; must get it from the system. */
1181 #ifndef NO_ARCHIVES
1182 if (ar_name (file->name))
1184 /* This file is an archive-member reference. */
1186 char *arname, *memname;
1187 struct file *arfile;
1188 time_t member_date;
1190 /* Find the archive's name. */
1191 ar_parse_name (file->name, &arname, &memname);
1193 /* Find the modification time of the archive itself.
1194 Also allow for its name to be changed via VPATH search. */
1195 arfile = lookup_file (arname);
1196 if (arfile == 0)
1197 arfile = enter_file (strcache_add (arname));
1198 mtime = f_mtime (arfile, search);
1199 check_renamed (arfile);
1200 if (search && strcmp (arfile->hname, arname))
1202 /* The archive's name has changed.
1203 Change the archive-member reference accordingly. */
1205 char *name;
1206 unsigned int arlen, memlen;
1208 arlen = strlen (arfile->hname);
1209 memlen = strlen (memname);
1211 name = xmalloc (arlen + 1 + memlen + 2);
1212 memcpy (name, arfile->hname, arlen);
1213 name[arlen] = '(';
1214 memcpy (name + arlen + 1, memname, memlen);
1215 name[arlen + 1 + memlen] = ')';
1216 name[arlen + 1 + memlen + 1] = '\0';
1218 /* If the archive was found with GPATH, make the change permanent;
1219 otherwise defer it until later. */
1220 if (arfile->name == arfile->hname)
1221 rename_file (file, name);
1222 else
1223 rehash_file (file, name);
1224 check_renamed (file);
1227 free (arname);
1229 file->low_resolution_time = 1;
1231 if (mtime == NONEXISTENT_MTIME)
1232 /* The archive doesn't exist, so its members don't exist either. */
1233 return NONEXISTENT_MTIME;
1235 member_date = ar_member_date (file->hname);
1236 mtime = (member_date == (time_t) -1
1237 ? NONEXISTENT_MTIME
1238 : file_timestamp_cons (file->hname, member_date, 0));
1240 else
1241 #endif
1243 mtime = name_mtime (file->name);
1245 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1247 /* If name_mtime failed, search VPATH. */
1248 const char *name = vpath_search (file->name, &mtime);
1249 if (name
1250 /* Last resort, is it a library (-lxxx)? */
1251 || (file->name[0] == '-' && file->name[1] == 'l'
1252 && (name = library_search (file->name, &mtime)) != 0))
1254 if (mtime != UNKNOWN_MTIME)
1255 /* vpath_search and library_search store UNKNOWN_MTIME
1256 if they didn't need to do a stat call for their work. */
1257 file->last_mtime = mtime;
1259 /* If we found it in VPATH, see if it's in GPATH too; if so,
1260 change the name right now; if not, defer until after the
1261 dependencies are updated. */
1262 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1264 rename_file (file, name);
1265 check_renamed (file);
1266 return file_mtime (file);
1269 rehash_file (file, name);
1270 check_renamed (file);
1271 /* If the result of a vpath search is -o or -W, preserve it.
1272 Otherwise, find the mtime of the resulting file. */
1273 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1274 mtime = name_mtime (name);
1279 /* Files can have bogus timestamps that nothing newly made will be
1280 "newer" than. Updating their dependents could just result in loops.
1281 So notify the user of the anomaly with a warning.
1283 We only need to do this once, for now. */
1285 if (!clock_skew_detected
1286 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1287 && !file->updated)
1289 static FILE_TIMESTAMP adjusted_now;
1291 FILE_TIMESTAMP adjusted_mtime = mtime;
1293 #if defined(WINDOWS32) || defined(__MSDOS__)
1294 /* Experimentation has shown that FAT filesystems can set file times
1295 up to 3 seconds into the future! Play it safe. */
1297 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1299 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1300 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1301 adjusted_mtime -= adjustment;
1302 #elif defined(__EMX__)
1303 /* FAT filesystems round time to the nearest even second!
1304 Allow for any file (NTFS or FAT) to perhaps suffer from this
1305 brain damage. */
1306 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1307 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1308 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1309 : 0);
1310 #endif
1312 /* If the file's time appears to be in the future, update our
1313 concept of the present and try once more. */
1314 if (adjusted_now < adjusted_mtime)
1316 int resolution;
1317 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1318 adjusted_now = now + (resolution - 1);
1319 if (adjusted_now < adjusted_mtime)
1321 #ifdef NO_FLOAT
1322 error (NILF, _("Warning: File `%s' has modification time in the future"),
1323 file->name);
1324 #else
1325 double from_now =
1326 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1327 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1328 / 1e9));
1329 char from_now_string[100];
1331 if (from_now >= 99 && from_now <= ULONG_MAX)
1332 sprintf (from_now_string, "%lu", (unsigned long) from_now);
1333 else
1334 sprintf (from_now_string, "%.2g", from_now);
1335 error (NILF, _("Warning: File `%s' has modification time %s s in the future"),
1336 file->name, from_now_string);
1337 #endif
1338 clock_skew_detected = 1;
1343 /* Store the mtime into all the entries for this file. */
1344 if (file->double_colon)
1345 file = file->double_colon;
1349 /* If this file is not implicit but it is intermediate then it was
1350 made so by the .INTERMEDIATE target. If this file has never
1351 been built by us but was found now, it existed before make
1352 started. So, turn off the intermediate bit so make doesn't
1353 delete it, since it didn't create it. */
1354 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1355 && file->command_state == cs_not_started
1356 && !file->tried_implicit && file->intermediate)
1357 file->intermediate = 0;
1359 file->last_mtime = mtime;
1360 file = file->prev;
1362 while (file != 0);
1364 return mtime;
1368 /* Return the mtime of the file or archive-member reference NAME. */
1370 /* First, we check with stat(). If the file does not exist, then we return
1371 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1372 examine each indirection of the symlink and find the newest mtime.
1373 This causes one duplicate stat() when -L is being used, but the code is
1374 much cleaner. */
1376 static FILE_TIMESTAMP
1377 name_mtime (const char *name)
1379 FILE_TIMESTAMP mtime;
1380 struct stat st;
1381 int e;
1383 EINTRLOOP (e, stat (name, &st));
1384 if (e == 0)
1385 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1386 else if (errno == ENOENT || errno == ENOTDIR)
1387 mtime = NONEXISTENT_MTIME;
1388 else
1390 perror_with_name ("stat: ", name);
1391 return NONEXISTENT_MTIME;
1394 /* If we get here we either found it, or it doesn't exist.
1395 If it doesn't exist see if we can use a symlink mtime instead. */
1397 #ifdef MAKE_SYMLINKS
1398 #ifndef S_ISLNK
1399 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1400 #endif
1401 if (check_symlink_flag)
1403 PATH_VAR (lpath);
1405 /* Check each symbolic link segment (if any). Find the latest mtime
1406 amongst all of them (and the target file of course).
1407 Note that we have already successfully dereferenced all the links
1408 above. So, if we run into any error trying to lstat(), or
1409 readlink(), or whatever, something bizarre-o happened. Just give up
1410 and use whatever mtime we've already computed at that point. */
1411 strcpy (lpath, name);
1412 while (1)
1414 FILE_TIMESTAMP ltime;
1415 PATH_VAR (lbuf);
1416 long llen;
1417 char *p;
1419 EINTRLOOP (e, lstat (lpath, &st));
1420 if (e)
1422 /* Just take what we have so far. */
1423 if (errno != ENOENT && errno != ENOTDIR)
1424 perror_with_name ("lstat: ", lpath);
1425 break;
1428 /* If this is not a symlink, we're done (we started with the real
1429 file's mtime so we don't need to test it again). */
1430 if (!S_ISLNK (st.st_mode))
1431 break;
1433 /* If this mtime is newer than what we had, keep the new one. */
1434 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1435 if (ltime > mtime)
1436 mtime = ltime;
1438 /* Set up to check the file pointed to by this link. */
1439 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1440 if (llen < 0)
1442 /* Eh? Just take what we have. */
1443 perror_with_name ("readlink: ", lpath);
1444 break;
1446 lbuf[llen] = '\0';
1448 /* If the target is fully-qualified or the source is just a
1449 filename, then the new path is the target. Otherwise it's the
1450 source directory plus the target. */
1451 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1452 strcpy (lpath, lbuf);
1453 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1454 /* Eh? Path too long! Again, just go with what we have. */
1455 break;
1456 else
1457 /* Create the next step in the symlink chain. */
1458 strcpy (p+1, lbuf);
1461 #endif
1463 return mtime;
1467 /* Search for a library file specified as -lLIBNAME, searching for a
1468 suitable library file in the system library directories and the VPATH
1469 directories. */
1471 static const char *
1472 library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
1474 static char *dirs[] =
1476 #ifndef _AMIGA
1477 "/lib",
1478 "/usr/lib",
1479 #endif
1480 #if defined(WINDOWS32) && !defined(LIBDIR)
1482 * This is completely up to the user at product install time. Just define
1483 * a placeholder.
1485 #define LIBDIR "."
1486 #endif
1487 LIBDIR, /* Defined by configuration. */
1491 const char *file = 0;
1492 char *libpatterns;
1493 FILE_TIMESTAMP mtime;
1495 /* Loop variables for the libpatterns value. */
1496 char *p;
1497 const char *p2;
1498 unsigned int len;
1499 unsigned int liblen;
1501 char **dp;
1503 libpatterns = xstrdup (variable_expand ("$(.LIBPATTERNS)"));
1505 /* Skip the '-l'. */
1506 lib += 2;
1507 liblen = strlen (lib);
1509 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1510 p2 = libpatterns;
1511 while ((p = find_next_token (&p2, &len)) != 0)
1513 static char *buf = NULL;
1514 static unsigned int buflen = 0;
1515 static int libdir_maxlen = -1;
1516 char *libbuf = variable_expand ("");
1518 /* Expand the pattern using LIB as a replacement. */
1520 char c = p[len];
1521 char *p3, *p4;
1523 p[len] = '\0';
1524 p3 = find_percent (p);
1525 if (!p3)
1527 /* Give a warning if there is no pattern. */
1528 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1529 p[len] = c;
1530 continue;
1532 p4 = variable_buffer_output (libbuf, p, p3-p);
1533 p4 = variable_buffer_output (p4, lib, liblen);
1534 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1535 p[len] = c;
1538 /* Look first for `libNAME.a' in the current directory. */
1539 mtime = name_mtime (libbuf);
1540 if (mtime != NONEXISTENT_MTIME)
1542 if (mtime_ptr != 0)
1543 *mtime_ptr = mtime;
1544 file = strcache_add (libbuf);
1545 goto fini;
1548 /* Now try VPATH search on that. */
1551 file = vpath_search (libbuf, mtime_ptr);
1552 if (file)
1553 goto fini;
1556 /* Now try the standard set of directories. */
1558 if (!buflen)
1560 for (dp = dirs; *dp != 0; ++dp)
1562 int l = strlen (*dp);
1563 if (l > libdir_maxlen)
1564 libdir_maxlen = l;
1566 buflen = strlen (libbuf);
1567 buf = xmalloc(libdir_maxlen + buflen + 2);
1569 else if (buflen < strlen (libbuf))
1571 buflen = strlen (libbuf);
1572 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1575 for (dp = dirs; *dp != 0; ++dp)
1577 sprintf (buf, "%s/%s", *dp, libbuf);
1578 mtime = name_mtime (buf);
1579 if (mtime != NONEXISTENT_MTIME)
1581 if (mtime_ptr != 0)
1582 *mtime_ptr = mtime;
1583 file = strcache_add (buf);
1584 goto fini;
1589 fini:
1590 free (libpatterns);
1591 return file;