Move the copyright info to the end of the NEWS file, otherwise automake's
[make.git] / remake.c
blob8a943d6906b388cd3d518d7809c05d439ca08c6f
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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 PARAMS ((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 PARAMS ((struct file *file, unsigned int depth));
64 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
65 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
66 static int touch_file PARAMS ((struct file *file));
67 static void remake_file PARAMS ((struct file *file));
68 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
69 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
72 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
73 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
75 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
76 and -n should be disabled for them unless they were also command-line
77 targets, and we should only make one goal at a time and return as soon as
78 one goal whose `changed' member is nonzero is successfully made. */
80 int
81 update_goal_chain (struct dep *goals)
83 int t = touch_flag, q = question_flag, n = just_print_flag;
84 unsigned int j = job_slots;
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 ((char *) 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;
265 job_slots = j;
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 /* If we got an error, don't bother with double_colon etc. */
310 if (status != 0 && !keep_going_flag)
311 return status;
313 if (f->command_state == cs_running
314 || f->command_state == cs_deps_running)
316 /* Don't run the other :: rules for this
317 file until this rule is finished. */
318 status = 0;
319 break;
323 /* Process the remaining rules in the double colon chain so they're marked
324 considered. Start their prerequisites, too. */
325 if (file->double_colon)
326 for (; f != 0 ; f = f->prev)
328 struct dep *d;
330 f->considered = considered;
332 for (d = f->deps; d != 0; d = d->next)
333 status |= update_file (d->file, depth + 1);
336 return status;
339 /* Show a message stating the target failed to build. */
341 static void
342 complain (const struct file *file)
344 const char *msg_noparent
345 = _("%sNo rule to make target `%s'%s");
346 const char *msg_parent
347 = _("%sNo rule to make target `%s', needed by `%s'%s");
349 if (!keep_going_flag)
351 if (file->parent == 0)
352 fatal (NILF, msg_noparent, "", file->name, "");
354 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
357 if (file->parent == 0)
358 error (NILF, msg_noparent, "*** ", file->name, ".");
359 else
360 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
363 /* Consider a single `struct file' and update it as appropriate. */
365 static int
366 update_file_1 (struct file *file, unsigned int depth)
368 register FILE_TIMESTAMP this_mtime;
369 int noexist, must_make, deps_changed;
370 int dep_status = 0;
371 register struct dep *d, *lastd;
372 int running = 0;
374 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
376 if (file->updated)
378 if (file->update_status > 0)
380 DBF (DB_VERBOSE,
381 _("Recently tried and failed to update file `%s'.\n"));
383 /* If the file we tried to make is marked dontcare then no message
384 was printed about it when it failed during the makefile rebuild.
385 If we're trying to build it again in the normal rebuild, print a
386 message now. */
387 if (file->dontcare && !rebuilding_makefiles)
389 file->dontcare = 0;
390 complain (file);
393 return file->update_status;
396 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
397 return 0;
400 switch (file->command_state)
402 case cs_not_started:
403 case cs_deps_running:
404 break;
405 case cs_running:
406 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
407 return 0;
408 case cs_finished:
409 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
410 return file->update_status;
411 default:
412 abort ();
415 ++depth;
417 /* Notice recursive update of the same file. */
418 start_updating (file);
420 /* Looking at the file's modtime beforehand allows the possibility
421 that its name may be changed by a VPATH search, and thus it may
422 not need an implicit rule. If this were not done, the file
423 might get implicit commands that apply to its initial name, only
424 to have that name replaced with another found by VPATH search. */
426 this_mtime = file_mtime (file);
427 check_renamed (file);
428 noexist = this_mtime == NONEXISTENT_MTIME;
429 if (noexist)
430 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
431 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
432 && file->low_resolution_time)
434 /* Avoid spurious rebuilds due to low resolution time stamps. */
435 int ns = FILE_TIMESTAMP_NS (this_mtime);
436 if (ns != 0)
437 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
438 file->name);
439 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
442 must_make = noexist;
444 /* If file was specified as a target with no commands,
445 come up with some default commands. */
447 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
449 if (try_implicit_rule (file, depth))
450 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
451 else
452 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
453 file->tried_implicit = 1;
455 if (file->cmds == 0 && !file->is_target
456 && default_file != 0 && default_file->cmds != 0)
458 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
459 file->cmds = default_file->cmds;
462 /* Update all non-intermediate files we depend on, if necessary,
463 and see whether any of them is more recent than this file. */
465 lastd = 0;
466 d = file->deps;
467 while (d != 0)
469 FILE_TIMESTAMP mtime;
470 int maybe_make;
471 int dontcare = 0;
473 check_renamed (d->file);
475 mtime = file_mtime (d->file);
476 check_renamed (d->file);
478 if (is_updating (d->file))
480 error (NILF, _("Circular %s <- %s dependency dropped."),
481 file->name, d->file->name);
482 /* We cannot free D here because our the caller will still have
483 a reference to it when we were called recursively via
484 check_dep below. */
485 if (lastd == 0)
486 file->deps = d->next;
487 else
488 lastd->next = d->next;
489 d = d->next;
490 continue;
493 d->file->parent = file;
494 maybe_make = must_make;
496 /* Inherit dontcare flag from our parent. */
497 if (rebuilding_makefiles)
499 dontcare = d->file->dontcare;
500 d->file->dontcare = file->dontcare;
504 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
506 /* Restore original dontcare flag. */
507 if (rebuilding_makefiles)
508 d->file->dontcare = dontcare;
510 if (! d->ignore_mtime)
511 must_make = maybe_make;
513 check_renamed (d->file);
516 register struct file *f = d->file;
517 if (f->double_colon)
518 f = f->double_colon;
521 running |= (f->command_state == cs_running
522 || f->command_state == cs_deps_running);
523 f = f->prev;
525 while (f != 0);
528 if (dep_status != 0 && !keep_going_flag)
529 break;
531 if (!running)
532 /* The prereq is considered changed if the timestamp has changed while
533 it was built, OR it doesn't exist. */
534 d->changed = ((file_mtime (d->file) != mtime)
535 || (mtime == NONEXISTENT_MTIME));
537 lastd = d;
538 d = d->next;
541 /* Now we know whether this target needs updating.
542 If it does, update all the intermediate files we depend on. */
544 if (must_make || always_make_flag)
546 for (d = file->deps; d != 0; d = d->next)
547 if (d->file->intermediate)
549 int dontcare = 0;
551 FILE_TIMESTAMP mtime = file_mtime (d->file);
552 check_renamed (d->file);
553 d->file->parent = file;
555 /* Inherit dontcare flag from our parent. */
556 if (rebuilding_makefiles)
558 dontcare = d->file->dontcare;
559 d->file->dontcare = file->dontcare;
563 dep_status |= update_file (d->file, depth);
565 /* Restore original dontcare flag. */
566 if (rebuilding_makefiles)
567 d->file->dontcare = dontcare;
569 check_renamed (d->file);
572 register struct file *f = d->file;
573 if (f->double_colon)
574 f = f->double_colon;
577 running |= (f->command_state == cs_running
578 || f->command_state == cs_deps_running);
579 f = f->prev;
581 while (f != 0);
584 if (dep_status != 0 && !keep_going_flag)
585 break;
587 if (!running)
588 d->changed = ((file->phony && file->cmds != 0)
589 || file_mtime (d->file) != mtime);
593 finish_updating (file);
595 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
597 if (running)
599 set_command_state (file, cs_deps_running);
600 --depth;
601 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
602 return 0;
605 /* If any dependency failed, give up now. */
607 if (dep_status != 0)
609 file->update_status = dep_status;
610 notice_finished_file (file);
612 --depth;
614 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
616 if (depth == 0 && keep_going_flag
617 && !just_print_flag && !question_flag)
618 error (NILF,
619 _("Target `%s' not remade because of errors."), file->name);
621 return dep_status;
624 if (file->command_state == cs_deps_running)
625 /* The commands for some deps were running on the last iteration, but
626 they have finished now. Reset the command_state to not_started to
627 simplify later bookkeeping. It is important that we do this only
628 when the prior state was cs_deps_running, because that prior state
629 was definitely propagated to FILE's also_make's by set_command_state
630 (called above), but in another state an also_make may have
631 independently changed to finished state, and we would confuse that
632 file's bookkeeping (updated, but not_started is bogus state). */
633 set_command_state (file, cs_not_started);
635 /* Now record which prerequisites are more
636 recent than this file, so we can define $?. */
638 deps_changed = 0;
639 for (d = file->deps; d != 0; d = d->next)
641 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
642 check_renamed (d->file);
644 if (! d->ignore_mtime)
646 #if 1
647 /* %%% In version 4, remove this code completely to
648 implement not remaking deps if their deps are newer
649 than their parents. */
650 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
651 /* We must remake if this dep does not
652 exist and is not intermediate. */
653 must_make = 1;
654 #endif
656 /* Set DEPS_CHANGED if this dep actually changed. */
657 deps_changed |= d->changed;
660 /* Set D->changed if either this dep actually changed,
661 or its dependent, FILE, is older or does not exist. */
662 d->changed |= noexist || d_mtime > this_mtime;
664 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
666 const char *fmt = 0;
668 if (d->ignore_mtime)
670 if (ISDB (DB_VERBOSE))
671 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
673 else if (d_mtime == NONEXISTENT_MTIME)
675 if (ISDB (DB_BASIC))
676 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
678 else if (d->changed)
680 if (ISDB (DB_BASIC))
681 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
683 else if (ISDB (DB_VERBOSE))
684 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
686 if (fmt)
688 print_spaces (depth);
689 printf (fmt, dep_name (d), file->name);
690 fflush (stdout);
695 /* Here depth returns to the value it had when we were called. */
696 depth--;
698 if (file->double_colon && file->deps == 0)
700 must_make = 1;
701 DBF (DB_BASIC,
702 _("Target `%s' is double-colon and has no prerequisites.\n"));
704 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
705 && !always_make_flag)
707 must_make = 0;
708 DBF (DB_VERBOSE,
709 _("No commands for `%s' and no prerequisites actually changed.\n"));
711 else if (!must_make && file->cmds != 0 && always_make_flag)
713 must_make = 1;
714 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
717 if (!must_make)
719 if (ISDB (DB_VERBOSE))
721 print_spaces (depth);
722 printf (_("No need to remake target `%s'"), file->name);
723 if (!streq (file->name, file->hname))
724 printf (_("; using VPATH name `%s'"), file->hname);
725 puts (".");
726 fflush (stdout);
729 notice_finished_file (file);
731 /* Since we don't need to remake the file, convert it to use the
732 VPATH filename if we found one. hfile will be either the
733 local name if no VPATH or the VPATH name if one was found. */
735 while (file)
737 file->name = file->hname;
738 file = file->prev;
741 return 0;
744 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
746 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
747 VPATH. */
748 if (!streq(file->name, file->hname))
750 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
751 file->ignore_vpath = 1;
754 /* Now, take appropriate actions to remake the file. */
755 remake_file (file);
757 if (file->command_state != cs_finished)
759 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
760 return 0;
763 switch (file->update_status)
765 case 2:
766 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
767 break;
768 case 0:
769 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
770 break;
771 case 1:
772 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
773 break;
774 default:
775 assert (file->update_status >= 0 && file->update_status <= 2);
776 break;
779 file->updated = 1;
780 return file->update_status;
783 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
784 files listed in its `also_make' member. Under -t, this function also
785 touches FILE.
787 On return, FILE->update_status will no longer be -1 if it was. */
789 void
790 notice_finished_file (struct file *file)
792 struct dep *d;
793 int ran = file->command_state == cs_running;
794 int touched = 0;
796 file->command_state = cs_finished;
797 file->updated = 1;
799 if (touch_flag
800 /* The update status will be:
801 -1 if this target was not remade;
802 0 if 0 or more commands (+ or ${MAKE}) were run and won;
803 1 if some commands were run and lost.
804 We touch the target if it has commands which either were not run
805 or won when they ran (i.e. status is 0). */
806 && file->update_status == 0)
808 if (file->cmds != 0 && file->cmds->any_recurse)
810 /* If all the command lines were recursive,
811 we don't want to do the touching. */
812 unsigned int i;
813 for (i = 0; i < file->cmds->ncommand_lines; ++i)
814 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
815 goto have_nonrecursing;
817 else
819 have_nonrecursing:
820 if (file->phony)
821 file->update_status = 0;
822 /* According to POSIX, -t doesn't affect targets with no cmds. */
823 else if (file->cmds != 0)
825 /* Should set file's modification date and do nothing else. */
826 file->update_status = touch_file (file);
828 /* Pretend we ran a real touch command, to suppress the
829 "`foo' is up to date" message. */
830 commands_started++;
832 /* Request for the timestamp to be updated (and distributed
833 to the double-colon entries). Simply setting ran=1 would
834 almost have done the trick, but messes up with the also_make
835 updating logic below. */
836 touched = 1;
841 if (file->mtime_before_update == UNKNOWN_MTIME)
842 file->mtime_before_update = file->last_mtime;
844 if ((ran && !file->phony) || touched)
846 int i = 0;
848 /* If -n, -t, or -q and all the commands are recursive, we ran them so
849 really check the target's mtime again. Otherwise, assume the target
850 would have been updated. */
852 if (question_flag || just_print_flag || touch_flag)
854 for (i = file->cmds->ncommand_lines; i > 0; --i)
855 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
856 break;
859 /* If there were no commands at all, it's always new. */
861 else if (file->is_target && file->cmds == 0)
862 i = 1;
864 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
867 if (file->double_colon)
869 /* If this is a double colon rule and it is the last one to be
870 updated, propagate the change of modification time to all the
871 double-colon entries for this file.
873 We do it on the last update because it is important to handle
874 individual entries as separate rules with separate timestamps
875 while they are treated as targets and then as one rule with the
876 unified timestamp when they are considered as a prerequisite
877 of some target. */
879 struct file *f;
880 FILE_TIMESTAMP max_mtime = file->last_mtime;
882 /* Check that all rules were updated and at the same time find
883 the max timestamp. We assume UNKNOWN_MTIME is newer then
884 any other value. */
885 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
886 if (max_mtime != UNKNOWN_MTIME
887 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
888 max_mtime = f->last_mtime;
890 if (f == 0)
891 for (f = file->double_colon; f != 0; f = f->prev)
892 f->last_mtime = max_mtime;
895 if (ran && file->update_status != -1)
896 /* We actually tried to update FILE, which has
897 updated its also_make's as well (if it worked).
898 If it didn't work, it wouldn't work again for them.
899 So mark them as updated with the same status. */
900 for (d = file->also_make; d != 0; d = d->next)
902 d->file->command_state = cs_finished;
903 d->file->updated = 1;
904 d->file->update_status = file->update_status;
906 if (ran && !d->file->phony)
907 /* Fetch the new modification time.
908 We do this instead of just invalidating the cached time
909 so that a vpath_search can happen. Otherwise, it would
910 never be done because the target is already updated. */
911 (void) f_mtime (d->file, 0);
913 else if (file->update_status == -1)
914 /* Nothing was done for FILE, but it needed nothing done.
915 So mark it now as "succeeded". */
916 file->update_status = 0;
919 /* Check whether another file (whose mtime is THIS_MTIME)
920 needs updating on account of a dependency which is file FILE.
921 If it does, store 1 in *MUST_MAKE_PTR.
922 In the process, update any non-intermediate files
923 that FILE depends on (including FILE itself).
924 Return nonzero if any updating failed. */
926 static int
927 check_dep (struct file *file, unsigned int depth,
928 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
930 struct dep *d;
931 int dep_status = 0;
933 ++depth;
934 start_updating (file);
936 if (file->phony || !file->intermediate)
938 /* If this is a non-intermediate file, update it and record
939 whether it is newer than THIS_MTIME. */
940 FILE_TIMESTAMP mtime;
941 dep_status = update_file (file, depth);
942 check_renamed (file);
943 mtime = file_mtime (file);
944 check_renamed (file);
945 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
946 *must_make_ptr = 1;
948 else
950 /* FILE is an intermediate file. */
951 FILE_TIMESTAMP mtime;
953 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
955 if (try_implicit_rule (file, depth))
956 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
957 else
958 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
959 file->tried_implicit = 1;
961 if (file->cmds == 0 && !file->is_target
962 && default_file != 0 && default_file->cmds != 0)
964 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
965 file->cmds = default_file->cmds;
968 /* If the intermediate file actually exists
969 and is newer, then we should remake from it. */
970 check_renamed (file);
971 mtime = file_mtime (file);
972 check_renamed (file);
973 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
974 *must_make_ptr = 1;
975 /* Otherwise, update all non-intermediate files we depend on,
976 if necessary, and see whether any of them is more
977 recent than the file on whose behalf we are checking. */
978 else
980 struct dep *lastd;
982 lastd = 0;
983 d = file->deps;
984 while (d != 0)
986 int maybe_make;
988 if (is_updating (d->file))
990 error (NILF, _("Circular %s <- %s dependency dropped."),
991 file->name, d->file->name);
992 if (lastd == 0)
994 file->deps = d->next;
995 free ((char *) d);
996 d = file->deps;
998 else
1000 lastd->next = d->next;
1001 free ((char *) d);
1002 d = lastd->next;
1004 continue;
1007 d->file->parent = file;
1008 maybe_make = *must_make_ptr;
1009 dep_status |= check_dep (d->file, depth, this_mtime,
1010 &maybe_make);
1011 if (! d->ignore_mtime)
1012 *must_make_ptr = maybe_make;
1013 check_renamed (d->file);
1014 if (dep_status != 0 && !keep_going_flag)
1015 break;
1017 if (d->file->command_state == cs_running
1018 || d->file->command_state == cs_deps_running)
1019 /* Record that some of FILE's deps are still being made.
1020 This tells the upper levels to wait on processing it until
1021 the commands are finished. */
1022 set_command_state (file, cs_deps_running);
1024 lastd = d;
1025 d = d->next;
1030 finish_updating (file);
1031 return dep_status;
1034 /* Touch FILE. Return zero if successful, one if not. */
1036 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1038 static int
1039 touch_file (struct file *file)
1041 if (!silent_flag)
1042 message (0, "touch %s", file->name);
1044 #ifndef NO_ARCHIVES
1045 if (ar_name (file->name))
1046 return ar_touch (file->name);
1047 else
1048 #endif
1050 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1052 if (fd < 0)
1053 TOUCH_ERROR ("touch: open: ");
1054 else
1056 struct stat statbuf;
1057 char buf;
1058 int e;
1060 EINTRLOOP (e, fstat (fd, &statbuf));
1061 if (e < 0)
1062 TOUCH_ERROR ("touch: fstat: ");
1063 /* Rewrite character 0 same as it already is. */
1064 if (read (fd, &buf, 1) < 0)
1065 TOUCH_ERROR ("touch: read: ");
1066 if (lseek (fd, 0L, 0) < 0L)
1067 TOUCH_ERROR ("touch: lseek: ");
1068 if (write (fd, &buf, 1) < 0)
1069 TOUCH_ERROR ("touch: write: ");
1070 /* If file length was 0, we just
1071 changed it, so change it back. */
1072 if (statbuf.st_size == 0)
1074 (void) close (fd);
1075 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1076 if (fd < 0)
1077 TOUCH_ERROR ("touch: open: ");
1079 (void) close (fd);
1083 return 0;
1086 /* Having checked and updated the dependencies of FILE,
1087 do whatever is appropriate to remake FILE itself.
1088 Return the status from executing FILE's commands. */
1090 static void
1091 remake_file (struct file *file)
1093 if (file->cmds == 0)
1095 if (file->phony)
1096 /* Phony target. Pretend it succeeded. */
1097 file->update_status = 0;
1098 else if (file->is_target)
1099 /* This is a nonexistent target file we cannot make.
1100 Pretend it was successfully remade. */
1101 file->update_status = 0;
1102 else
1104 /* This is a dependency file we cannot remake. Fail. */
1105 if (!rebuilding_makefiles || !file->dontcare)
1106 complain (file);
1107 file->update_status = 2;
1110 else
1112 chop_commands (file->cmds);
1114 /* The normal case: start some commands. */
1115 if (!touch_flag || file->cmds->any_recurse)
1117 execute_file_commands (file);
1118 return;
1121 /* This tells notice_finished_file it is ok to touch the file. */
1122 file->update_status = 0;
1125 /* This does the touching under -t. */
1126 notice_finished_file (file);
1129 /* Return the mtime of a file, given a `struct file'.
1130 Caches the time in the struct file to avoid excess stat calls.
1132 If the file is not found, and SEARCH is nonzero, VPATH searching and
1133 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1134 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1135 FILE. */
1137 FILE_TIMESTAMP
1138 f_mtime (struct file *file, int search)
1140 FILE_TIMESTAMP mtime;
1142 /* File's mtime is not known; must get it from the system. */
1144 #ifndef NO_ARCHIVES
1145 if (ar_name (file->name))
1147 /* This file is an archive-member reference. */
1149 char *arname, *memname;
1150 struct file *arfile;
1151 int arname_used = 0;
1152 time_t member_date;
1154 /* Find the archive's name. */
1155 ar_parse_name (file->name, &arname, &memname);
1157 /* Find the modification time of the archive itself.
1158 Also allow for its name to be changed via VPATH search. */
1159 arfile = lookup_file (arname);
1160 if (arfile == 0)
1162 arfile = enter_file (arname);
1163 arname_used = 1;
1165 mtime = f_mtime (arfile, search);
1166 check_renamed (arfile);
1167 if (search && strcmp (arfile->hname, arname))
1169 /* The archive's name has changed.
1170 Change the archive-member reference accordingly. */
1172 char *name;
1173 unsigned int arlen, memlen;
1175 if (!arname_used)
1177 free (arname);
1178 arname_used = 1;
1181 arname = arfile->hname;
1182 arlen = strlen (arname);
1183 memlen = strlen (memname);
1185 /* free (file->name); */
1187 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1188 bcopy (arname, name, arlen);
1189 name[arlen] = '(';
1190 bcopy (memname, name + arlen + 1, memlen);
1191 name[arlen + 1 + memlen] = ')';
1192 name[arlen + 1 + memlen + 1] = '\0';
1194 /* If the archive was found with GPATH, make the change permanent;
1195 otherwise defer it until later. */
1196 if (arfile->name == arfile->hname)
1197 rename_file (file, name);
1198 else
1199 rehash_file (file, name);
1200 check_renamed (file);
1203 if (!arname_used)
1204 free (arname);
1205 free (memname);
1207 file->low_resolution_time = 1;
1209 if (mtime == NONEXISTENT_MTIME)
1210 /* The archive doesn't exist, so its members don't exist either. */
1211 return NONEXISTENT_MTIME;
1213 member_date = ar_member_date (file->hname);
1214 mtime = (member_date == (time_t) -1
1215 ? NONEXISTENT_MTIME
1216 : file_timestamp_cons (file->hname, member_date, 0));
1218 else
1219 #endif
1221 mtime = name_mtime (file->name);
1223 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1225 /* If name_mtime failed, search VPATH. */
1226 char *name = file->name;
1227 if (vpath_search (&name, &mtime)
1228 /* Last resort, is it a library (-lxxx)? */
1229 || (name[0] == '-' && name[1] == 'l'
1230 && library_search (&name, &mtime)))
1232 if (mtime != UNKNOWN_MTIME)
1233 /* vpath_search and library_search store UNKNOWN_MTIME
1234 if they didn't need to do a stat call for their work. */
1235 file->last_mtime = mtime;
1237 /* If we found it in VPATH, see if it's in GPATH too; if so,
1238 change the name right now; if not, defer until after the
1239 dependencies are updated. */
1240 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1242 rename_file (file, name);
1243 check_renamed (file);
1244 return file_mtime (file);
1247 rehash_file (file, name);
1248 check_renamed (file);
1249 /* If the result of a vpath search is -o or -W, preserve it.
1250 Otherwise, find the mtime of the resulting file. */
1251 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1252 mtime = name_mtime (name);
1257 /* Files can have bogus timestamps that nothing newly made will be
1258 "newer" than. Updating their dependents could just result in loops.
1259 So notify the user of the anomaly with a warning.
1261 We only need to do this once, for now. */
1263 if (!clock_skew_detected
1264 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1265 && !file->updated)
1267 static FILE_TIMESTAMP adjusted_now;
1269 FILE_TIMESTAMP adjusted_mtime = mtime;
1271 #if defined(WINDOWS32) || defined(__MSDOS__)
1272 /* Experimentation has shown that FAT filesystems can set file times
1273 up to 3 seconds into the future! Play it safe. */
1275 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1277 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1278 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1279 adjusted_mtime -= adjustment;
1280 #elif defined(__EMX__)
1281 /* FAT filesystems round time to the nearest even second!
1282 Allow for any file (NTFS or FAT) to perhaps suffer from this
1283 brain damage. */
1284 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1285 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1286 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1287 : 0);
1288 #endif
1290 /* If the file's time appears to be in the future, update our
1291 concept of the present and try once more. */
1292 if (adjusted_now < adjusted_mtime)
1294 int resolution;
1295 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1296 adjusted_now = now + (resolution - 1);
1297 if (adjusted_now < adjusted_mtime)
1299 #ifdef NO_FLOAT
1300 error (NILF, _("Warning: File `%s' has modification time in the future"),
1301 file->name);
1302 #else
1303 double from_now =
1304 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1305 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1306 / 1e9));
1307 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1308 file->name, from_now);
1309 #endif
1310 clock_skew_detected = 1;
1315 /* Store the mtime into all the entries for this file. */
1316 if (file->double_colon)
1317 file = file->double_colon;
1321 /* If this file is not implicit but it is intermediate then it was
1322 made so by the .INTERMEDIATE target. If this file has never
1323 been built by us but was found now, it existed before make
1324 started. So, turn off the intermediate bit so make doesn't
1325 delete it, since it didn't create it. */
1326 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1327 && file->command_state == cs_not_started
1328 && !file->tried_implicit && file->intermediate)
1329 file->intermediate = 0;
1331 file->last_mtime = mtime;
1332 file = file->prev;
1334 while (file != 0);
1336 return mtime;
1340 /* Return the mtime of the file or archive-member reference NAME. */
1342 /* First, we check with stat(). If the file does not exist, then we return
1343 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1344 examine each indirection of the symlink and find the newest mtime.
1345 This causes one duplicate stat() when -L is being used, but the code is
1346 much cleaner. */
1348 static FILE_TIMESTAMP
1349 name_mtime (char *name)
1351 FILE_TIMESTAMP mtime;
1352 struct stat st;
1353 int e;
1355 EINTRLOOP (e, stat (name, &st));
1356 if (e == 0)
1357 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1358 else if (errno == ENOENT || errno == ENOTDIR)
1359 mtime = NONEXISTENT_MTIME;
1360 else
1362 perror_with_name ("stat: ", name);
1363 return NONEXISTENT_MTIME;
1366 /* If we get here we either found it, or it doesn't exist.
1367 If it doesn't exist see if we can use a symlink mtime instead. */
1369 #ifdef MAKE_SYMLINKS
1370 #ifndef S_ISLNK
1371 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1372 #endif
1373 if (check_symlink_flag)
1375 PATH_VAR (lpath);
1377 /* Check each symbolic link segment (if any). Find the latest mtime
1378 amongst all of them (and the target file of course).
1379 Note that we have already successfully dereferenced all the links
1380 above. So, if we run into any error trying to lstat(), or
1381 readlink(), or whatever, something bizarre-o happened. Just give up
1382 and use whatever mtime we've already computed at that point. */
1383 strcpy (lpath, name);
1384 while (1)
1386 FILE_TIMESTAMP ltime;
1387 PATH_VAR (lbuf);
1388 long llen;
1389 char *p;
1391 EINTRLOOP (e, lstat (lpath, &st));
1392 if (e)
1394 /* Just take what we have so far. */
1395 if (errno != ENOENT && errno != ENOTDIR)
1396 perror_with_name ("lstat: ", lpath);
1397 break;
1400 /* If this is not a symlink, we're done (we started with the real
1401 file's mtime so we don't need to test it again). */
1402 if (!S_ISLNK (st.st_mode))
1403 break;
1405 /* If this mtime is newer than what we had, keep the new one. */
1406 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1407 if (ltime > mtime)
1408 mtime = ltime;
1410 /* Set up to check the file pointed to by this link. */
1411 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1412 if (llen < 0)
1414 /* Eh? Just take what we have. */
1415 perror_with_name ("readlink: ", lpath);
1416 break;
1418 lbuf[llen] = '\0';
1420 /* If the target is fully-qualified or the source is just a
1421 filename, then the new path is the target. Otherwise it's the
1422 source directory plus the target. */
1423 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1424 strcpy (lpath, lbuf);
1425 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1426 /* Eh? Path too long! Again, just go with what we have. */
1427 break;
1428 else
1429 /* Create the next step in the symlink chain. */
1430 strcpy (p+1, lbuf);
1433 #endif
1435 return mtime;
1439 /* Search for a library file specified as -lLIBNAME, searching for a
1440 suitable library file in the system library directories and the VPATH
1441 directories. */
1443 static int
1444 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1446 static char *dirs[] =
1448 #ifndef _AMIGA
1449 "/lib",
1450 "/usr/lib",
1451 #endif
1452 #if defined(WINDOWS32) && !defined(LIBDIR)
1454 * This is completely up to the user at product install time. Just define
1455 * a placeholder.
1457 #define LIBDIR "."
1458 #endif
1459 LIBDIR, /* Defined by configuration. */
1463 static char *libpatterns = NULL;
1465 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1466 FILE_TIMESTAMP mtime;
1468 /* Loop variables for the libpatterns value. */
1469 char *p, *p2;
1470 unsigned int len;
1472 char *file, **dp;
1474 /* If we don't have libpatterns, get it. */
1475 if (!libpatterns)
1477 int save = warn_undefined_variables_flag;
1478 warn_undefined_variables_flag = 0;
1480 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1482 warn_undefined_variables_flag = save;
1485 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1486 p2 = libpatterns;
1487 while ((p = find_next_token (&p2, &len)) != 0)
1489 static char *buf = NULL;
1490 static unsigned int buflen = 0;
1491 static int libdir_maxlen = -1;
1492 char *libbuf = variable_expand ("");
1494 /* Expand the pattern using LIBNAME as a replacement. */
1496 char c = p[len];
1497 char *p3, *p4;
1499 p[len] = '\0';
1500 p3 = find_percent (p);
1501 if (!p3)
1503 /* Give a warning if there is no pattern, then remove the
1504 pattern so it's ignored next time. */
1505 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1506 for (; len; --len, ++p)
1507 *p = ' ';
1508 *p = c;
1509 continue;
1511 p4 = variable_buffer_output (libbuf, p, p3-p);
1512 p4 = variable_buffer_output (p4, libname, strlen (libname));
1513 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1514 p[len] = c;
1517 /* Look first for `libNAME.a' in the current directory. */
1518 mtime = name_mtime (libbuf);
1519 if (mtime != NONEXISTENT_MTIME)
1521 *lib = xstrdup (libbuf);
1522 if (mtime_ptr != 0)
1523 *mtime_ptr = mtime;
1524 return 1;
1527 /* Now try VPATH search on that. */
1529 file = libbuf;
1530 if (vpath_search (&file, mtime_ptr))
1532 *lib = file;
1533 return 1;
1536 /* Now try the standard set of directories. */
1538 if (!buflen)
1540 for (dp = dirs; *dp != 0; ++dp)
1542 int l = strlen (*dp);
1543 if (l > libdir_maxlen)
1544 libdir_maxlen = l;
1546 buflen = strlen (libbuf);
1547 buf = xmalloc(libdir_maxlen + buflen + 2);
1549 else if (buflen < strlen (libbuf))
1551 buflen = strlen (libbuf);
1552 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1555 for (dp = dirs; *dp != 0; ++dp)
1557 sprintf (buf, "%s/%s", *dp, libbuf);
1558 mtime = name_mtime (buf);
1559 if (mtime != NONEXISTENT_MTIME)
1561 *lib = xstrdup (buf);
1562 if (mtime_ptr != 0)
1563 *mtime_ptr = mtime;
1564 return 1;
1569 return 0;