- Fix Savannah bug #19108
[make.git] / remake.c
blob513cb5603529814324c9a137c8d422e46e6fda32
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 unsigned int j = job_slots;
86 int status = -1;
88 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
89 : file_mtime (file))
91 /* Duplicate the chain so we can remove things from it. */
93 goals = copy_dep_chain (goals);
96 /* Clear the `changed' flag of each goal in the chain.
97 We will use the flag below to notice when any commands
98 have actually been run for a target. When no commands
99 have been run, we give an "up to date" diagnostic. */
101 struct dep *g;
102 for (g = goals; g != 0; g = g->next)
103 g->changed = 0;
106 /* All files start with the considered bit 0, so the global value is 1. */
107 considered = 1;
109 /* Update all the goals until they are all finished. */
111 while (goals != 0)
113 register struct dep *g, *lastgoal;
115 /* Start jobs that are waiting for the load to go down. */
117 start_waiting_jobs ();
119 /* Wait for a child to die. */
121 reap_children (1, 0);
123 lastgoal = 0;
124 g = goals;
125 while (g != 0)
127 /* Iterate over all double-colon entries for this file. */
128 struct file *file;
129 int stop = 0, any_not_updated = 0;
131 for (file = g->file->double_colon ? g->file->double_colon : g->file;
132 file != NULL;
133 file = file->prev)
135 unsigned int ocommands_started;
136 int x;
137 check_renamed (file);
138 if (rebuilding_makefiles)
140 if (file->cmd_target)
142 touch_flag = t;
143 question_flag = q;
144 just_print_flag = n;
146 else
147 touch_flag = question_flag = just_print_flag = 0;
150 /* Save the old value of `commands_started' so we can compare
151 later. It will be incremented when any commands are
152 actually run. */
153 ocommands_started = commands_started;
155 x = update_file (file, rebuilding_makefiles ? 1 : 0);
156 check_renamed (file);
158 /* Set the goal's `changed' flag if any commands were started
159 by calling update_file above. We check this flag below to
160 decide when to give an "up to date" diagnostic. */
161 if (commands_started > ocommands_started)
162 g->changed = 1;
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. */
168 stop = 0;
169 if ((x != 0 || file->updated) && status < 1)
171 if (file->update_status != 0)
173 /* Updating failed, or -q triggered. The STATUS value
174 tells our caller which. */
175 status = file->update_status;
176 /* If -q just triggered, stop immediately. It doesn't
177 matter how much more we run, since we already know
178 the answer to return. */
179 stop = (question_flag && !keep_going_flag
180 && !rebuilding_makefiles);
182 else
184 FILE_TIMESTAMP mtime = MTIME (file);
185 check_renamed (file);
187 if (file->updated && g->changed &&
188 mtime != file->mtime_before_update)
190 /* Updating was done. If this is a makefile and
191 just_print_flag or question_flag is set (meaning
192 -n or -q was given and this file was specified
193 as a command-line target), don't change STATUS.
194 If STATUS is changed, we will get re-exec'd, and
195 enter an infinite loop. */
196 if (!rebuilding_makefiles
197 || (!just_print_flag && !question_flag))
198 status = 0;
199 if (rebuilding_makefiles && file->dontcare)
200 /* This is a default makefile; stop remaking. */
201 stop = 1;
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;
210 if (stop)
211 break;
214 /* Reset FILE since it is null at the end of the loop. */
215 file = g->file;
217 if (stop || !any_not_updated)
219 /* If we have found nothing whatever to do for the goal,
220 print a message saying nothing needs doing. */
222 if (!rebuilding_makefiles
223 /* If the update_status is zero, we updated successfully
224 or not at all. G->changed will have been set above if
225 any commands were actually started for this goal. */
226 && file->update_status == 0 && !g->changed
227 /* Never give a message under -s or -q. */
228 && !silent_flag && !question_flag)
229 message (1, ((file->phony || file->cmds == 0)
230 ? _("Nothing to be done for `%s'.")
231 : _("`%s' is up to date.")),
232 file->name);
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;
240 /* Free the storage. */
241 free (g);
243 g = lastgoal == 0 ? goals : lastgoal->next;
245 if (stop)
246 break;
248 else
250 lastgoal = g;
251 g = g->next;
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;
261 if (rebuilding_makefiles)
263 touch_flag = t;
264 question_flag = q;
265 just_print_flag = n;
266 job_slots = j;
269 return status;
272 /* If FILE is not up to date, execute the commands for it.
273 Return 0 if successful, 1 if unsuccessful;
274 but with some flag settings, just call `exit' if unsuccessful.
276 DEPTH is the depth in recursions of this function.
277 We increment it during the consideration of our dependencies,
278 then decrement it again after finding out whether this file
279 is out of date.
281 If there are multiple double-colon entries for FILE,
282 each is considered in turn. */
284 static int
285 update_file (struct file *file, unsigned int depth)
287 register int status = 0;
288 register struct file *f;
290 f = file->double_colon ? file->double_colon : file;
292 /* Prune the dependency graph: if we've already been here on _this_
293 pass through the dependency graph, we don't have to go any further.
294 We won't reap_children until we start the next pass, so no state
295 change is possible below here until then. */
296 if (f->considered == considered)
298 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
299 return f->command_state == cs_finished ? f->update_status : 0;
302 /* This loop runs until we start commands for a double colon rule, or until
303 the chain is exhausted. */
304 for (; f != 0; f = f->prev)
306 f->considered = considered;
308 status |= update_file_1 (f, depth);
309 check_renamed (f);
311 /* Clean up any alloca() used during the update. */
312 alloca (0);
314 /* If we got an error, don't bother with double_colon etc. */
315 if (status != 0 && !keep_going_flag)
316 return status;
318 if (f->command_state == cs_running
319 || f->command_state == cs_deps_running)
321 /* Don't run the other :: rules for this
322 file until this rule is finished. */
323 status = 0;
324 break;
328 /* Process the remaining rules in the double colon chain so they're marked
329 considered. Start their prerequisites, too. */
330 if (file->double_colon)
331 for (; f != 0 ; f = f->prev)
333 struct dep *d;
335 f->considered = considered;
337 for (d = f->deps; d != 0; d = d->next)
338 status |= update_file (d->file, depth + 1);
341 return status;
344 /* Show a message stating the target failed to build. */
346 static void
347 complain (const struct file *file)
349 const char *msg_noparent
350 = _("%sNo rule to make target `%s'%s");
351 const char *msg_parent
352 = _("%sNo rule to make target `%s', needed by `%s'%s");
354 if (!keep_going_flag)
356 if (file->parent == 0)
357 fatal (NILF, msg_noparent, "", file->name, "");
359 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
362 if (file->parent == 0)
363 error (NILF, msg_noparent, "*** ", file->name, ".");
364 else
365 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
368 /* Consider a single `struct file' and update it as appropriate. */
370 static int
371 update_file_1 (struct file *file, unsigned int depth)
373 FILE_TIMESTAMP this_mtime;
374 int noexist, must_make, deps_changed;
375 int dep_status = 0;
376 struct dep *d, *ad;
377 struct dep amake;
378 int running = 0;
380 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
382 if (file->updated)
384 if (file->update_status > 0)
386 DBF (DB_VERBOSE,
387 _("Recently tried and failed to update file `%s'.\n"));
389 /* If the file we tried to make is marked dontcare then no message
390 was printed about it when it failed during the makefile rebuild.
391 If we're trying to build it again in the normal rebuild, print a
392 message now. */
393 if (file->dontcare && !rebuilding_makefiles)
395 file->dontcare = 0;
396 complain (file);
399 return file->update_status;
402 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
403 return 0;
406 switch (file->command_state)
408 case cs_not_started:
409 case cs_deps_running:
410 break;
411 case cs_running:
412 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
413 return 0;
414 case cs_finished:
415 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
416 return file->update_status;
417 default:
418 abort ();
421 ++depth;
423 /* Notice recursive update of the same file. */
424 start_updating (file);
426 /* Looking at the file's modtime beforehand allows the possibility
427 that its name may be changed by a VPATH search, and thus it may
428 not need an implicit rule. If this were not done, the file
429 might get implicit commands that apply to its initial name, only
430 to have that name replaced with another found by VPATH search. */
432 this_mtime = file_mtime (file);
433 check_renamed (file);
434 noexist = this_mtime == NONEXISTENT_MTIME;
435 if (noexist)
436 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
437 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
438 && file->low_resolution_time)
440 /* Avoid spurious rebuilds due to low resolution time stamps. */
441 int ns = FILE_TIMESTAMP_NS (this_mtime);
442 if (ns != 0)
443 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
444 file->name);
445 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
448 must_make = noexist;
450 /* If file was specified as a target with no commands,
451 come up with some default commands. */
453 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
455 if (try_implicit_rule (file, depth))
456 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
457 else
458 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
459 file->tried_implicit = 1;
461 if (file->cmds == 0 && !file->is_target
462 && default_file != 0 && default_file->cmds != 0)
464 DBF (DB_IMPLICIT, _("Using default recipe for `%s'.\n"));
465 file->cmds = default_file->cmds;
468 /* Update all non-intermediate files we depend on, if necessary, and see
469 whether any of them is more recent than this file. We need to walk our
470 deps, AND the deps of any also_make targets to ensure everything happens
471 in the correct order. */
473 amake.file = file;
474 amake.next = file->also_make;
475 ad = &amake;
476 while (ad)
478 struct dep *lastd = 0;
480 /* Find the deps we're scanning */
481 d = ad->file->deps;
482 ad = ad->next;
484 while (d)
486 FILE_TIMESTAMP mtime;
487 int maybe_make;
488 int dontcare = 0;
490 check_renamed (d->file);
492 mtime = file_mtime (d->file);
493 check_renamed (d->file);
495 if (is_updating (d->file))
497 error (NILF, _("Circular %s <- %s dependency dropped."),
498 file->name, d->file->name);
499 /* We cannot free D here because our the caller will still have
500 a reference to it when we were called recursively via
501 check_dep below. */
502 if (lastd == 0)
503 file->deps = d->next;
504 else
505 lastd->next = d->next;
506 d = d->next;
507 continue;
510 d->file->parent = file;
511 maybe_make = must_make;
513 /* Inherit dontcare flag from our parent. */
514 if (rebuilding_makefiles)
516 dontcare = d->file->dontcare;
517 d->file->dontcare = file->dontcare;
520 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
522 /* Restore original dontcare flag. */
523 if (rebuilding_makefiles)
524 d->file->dontcare = dontcare;
526 if (! d->ignore_mtime)
527 must_make = maybe_make;
529 check_renamed (d->file);
532 register struct file *f = d->file;
533 if (f->double_colon)
534 f = f->double_colon;
537 running |= (f->command_state == cs_running
538 || f->command_state == cs_deps_running);
539 f = f->prev;
541 while (f != 0);
544 if (dep_status != 0 && !keep_going_flag)
545 break;
547 if (!running)
548 /* The prereq is considered changed if the timestamp has changed while
549 it was built, OR it doesn't exist. */
550 d->changed = ((file_mtime (d->file) != mtime)
551 || (mtime == NONEXISTENT_MTIME));
553 lastd = d;
554 d = d->next;
558 /* Now we know whether this target needs updating.
559 If it does, update all the intermediate files we depend on. */
561 if (must_make || always_make_flag)
563 for (d = file->deps; d != 0; d = d->next)
564 if (d->file->intermediate)
566 int dontcare = 0;
568 FILE_TIMESTAMP mtime = file_mtime (d->file);
569 check_renamed (d->file);
570 d->file->parent = file;
572 /* Inherit dontcare flag from our parent. */
573 if (rebuilding_makefiles)
575 dontcare = d->file->dontcare;
576 d->file->dontcare = file->dontcare;
580 dep_status |= update_file (d->file, depth);
582 /* Restore original dontcare flag. */
583 if (rebuilding_makefiles)
584 d->file->dontcare = dontcare;
586 check_renamed (d->file);
589 register struct file *f = d->file;
590 if (f->double_colon)
591 f = f->double_colon;
594 running |= (f->command_state == cs_running
595 || f->command_state == cs_deps_running);
596 f = f->prev;
598 while (f != 0);
601 if (dep_status != 0 && !keep_going_flag)
602 break;
604 if (!running)
605 d->changed = ((file->phony && file->cmds != 0)
606 || file_mtime (d->file) != mtime);
610 finish_updating (file);
612 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
614 if (running)
616 set_command_state (file, cs_deps_running);
617 --depth;
618 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
619 return 0;
622 /* If any dependency failed, give up now. */
624 if (dep_status != 0)
626 file->update_status = dep_status;
627 notice_finished_file (file);
629 --depth;
631 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
633 if (depth == 0 && keep_going_flag
634 && !just_print_flag && !question_flag)
635 error (NILF,
636 _("Target `%s' not remade because of errors."), file->name);
638 return dep_status;
641 if (file->command_state == cs_deps_running)
642 /* The commands for some deps were running on the last iteration, but
643 they have finished now. Reset the command_state to not_started to
644 simplify later bookkeeping. It is important that we do this only
645 when the prior state was cs_deps_running, because that prior state
646 was definitely propagated to FILE's also_make's by set_command_state
647 (called above), but in another state an also_make may have
648 independently changed to finished state, and we would confuse that
649 file's bookkeeping (updated, but not_started is bogus state). */
650 set_command_state (file, cs_not_started);
652 /* Now record which prerequisites are more
653 recent than this file, so we can define $?. */
655 deps_changed = 0;
656 for (d = file->deps; d != 0; d = d->next)
658 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
659 check_renamed (d->file);
661 if (! d->ignore_mtime)
663 #if 1
664 /* %%% In version 4, remove this code completely to
665 implement not remaking deps if their deps are newer
666 than their parents. */
667 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
668 /* We must remake if this dep does not
669 exist and is not intermediate. */
670 must_make = 1;
671 #endif
673 /* Set DEPS_CHANGED if this dep actually changed. */
674 deps_changed |= d->changed;
677 /* Set D->changed if either this dep actually changed,
678 or its dependent, FILE, is older or does not exist. */
679 d->changed |= noexist || d_mtime > this_mtime;
681 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
683 const char *fmt = 0;
685 if (d->ignore_mtime)
687 if (ISDB (DB_VERBOSE))
688 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
690 else if (d_mtime == NONEXISTENT_MTIME)
692 if (ISDB (DB_BASIC))
693 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
695 else if (d->changed)
697 if (ISDB (DB_BASIC))
698 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
700 else if (ISDB (DB_VERBOSE))
701 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
703 if (fmt)
705 print_spaces (depth);
706 printf (fmt, dep_name (d), file->name);
707 fflush (stdout);
712 /* Here depth returns to the value it had when we were called. */
713 depth--;
715 if (file->double_colon && file->deps == 0)
717 must_make = 1;
718 DBF (DB_BASIC,
719 _("Target `%s' is double-colon and has no prerequisites.\n"));
721 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
722 && !always_make_flag)
724 must_make = 0;
725 DBF (DB_VERBOSE,
726 _("No recipe for `%s' and no prerequisites actually changed.\n"));
728 else if (!must_make && file->cmds != 0 && always_make_flag)
730 must_make = 1;
731 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
734 if (!must_make)
736 if (ISDB (DB_VERBOSE))
738 print_spaces (depth);
739 printf (_("No need to remake target `%s'"), file->name);
740 if (!streq (file->name, file->hname))
741 printf (_("; using VPATH name `%s'"), file->hname);
742 puts (".");
743 fflush (stdout);
746 notice_finished_file (file);
748 /* Since we don't need to remake the file, convert it to use the
749 VPATH filename if we found one. hfile will be either the
750 local name if no VPATH or the VPATH name if one was found. */
752 while (file)
754 file->name = file->hname;
755 file = file->prev;
758 return 0;
761 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
763 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
764 VPATH. */
765 if (!streq(file->name, file->hname))
767 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
768 file->ignore_vpath = 1;
771 /* Now, take appropriate actions to remake the file. */
772 remake_file (file);
774 if (file->command_state != cs_finished)
776 DBF (DB_VERBOSE, _("Recipe of `%s' is being run.\n"));
777 return 0;
780 switch (file->update_status)
782 case 2:
783 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
784 break;
785 case 0:
786 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
787 break;
788 case 1:
789 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
790 break;
791 default:
792 assert (file->update_status >= 0 && file->update_status <= 2);
793 break;
796 file->updated = 1;
797 return file->update_status;
800 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
801 files listed in its `also_make' member. Under -t, this function also
802 touches FILE.
804 On return, FILE->update_status will no longer be -1 if it was. */
806 void
807 notice_finished_file (struct file *file)
809 struct dep *d;
810 int ran = file->command_state == cs_running;
811 int touched = 0;
813 file->command_state = cs_finished;
814 file->updated = 1;
816 if (touch_flag
817 /* The update status will be:
818 -1 if this target was not remade;
819 0 if 0 or more commands (+ or ${MAKE}) were run and won;
820 1 if some commands were run and lost.
821 We touch the target if it has commands which either were not run
822 or won when they ran (i.e. status is 0). */
823 && file->update_status == 0)
825 if (file->cmds != 0 && file->cmds->any_recurse)
827 /* If all the command lines were recursive,
828 we don't want to do the touching. */
829 unsigned int i;
830 for (i = 0; i < file->cmds->ncommand_lines; ++i)
831 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
832 goto have_nonrecursing;
834 else
836 have_nonrecursing:
837 if (file->phony)
838 file->update_status = 0;
839 /* According to POSIX, -t doesn't affect targets with no cmds. */
840 else if (file->cmds != 0)
842 /* Should set file's modification date and do nothing else. */
843 file->update_status = touch_file (file);
845 /* Pretend we ran a real touch command, to suppress the
846 "`foo' is up to date" message. */
847 commands_started++;
849 /* Request for the timestamp to be updated (and distributed
850 to the double-colon entries). Simply setting ran=1 would
851 almost have done the trick, but messes up with the also_make
852 updating logic below. */
853 touched = 1;
858 if (file->mtime_before_update == UNKNOWN_MTIME)
859 file->mtime_before_update = file->last_mtime;
861 if ((ran && !file->phony) || touched)
863 int i = 0;
865 /* If -n, -t, or -q and all the commands are recursive, we ran them so
866 really check the target's mtime again. Otherwise, assume the target
867 would have been updated. */
869 if (question_flag || just_print_flag || touch_flag)
871 for (i = file->cmds->ncommand_lines; i > 0; --i)
872 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
873 break;
876 /* If there were no commands at all, it's always new. */
878 else if (file->is_target && file->cmds == 0)
879 i = 1;
881 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
884 if (file->double_colon)
886 /* If this is a double colon rule and it is the last one to be
887 updated, propagate the change of modification time to all the
888 double-colon entries for this file.
890 We do it on the last update because it is important to handle
891 individual entries as separate rules with separate timestamps
892 while they are treated as targets and then as one rule with the
893 unified timestamp when they are considered as a prerequisite
894 of some target. */
896 struct file *f;
897 FILE_TIMESTAMP max_mtime = file->last_mtime;
899 /* Check that all rules were updated and at the same time find
900 the max timestamp. We assume UNKNOWN_MTIME is newer then
901 any other value. */
902 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
903 if (max_mtime != UNKNOWN_MTIME
904 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
905 max_mtime = f->last_mtime;
907 if (f == 0)
908 for (f = file->double_colon; f != 0; f = f->prev)
909 f->last_mtime = max_mtime;
912 if (ran && file->update_status != -1)
913 /* We actually tried to update FILE, which has
914 updated its also_make's as well (if it worked).
915 If it didn't work, it wouldn't work again for them.
916 So mark them as updated with the same status. */
917 for (d = file->also_make; d != 0; d = d->next)
919 d->file->command_state = cs_finished;
920 d->file->updated = 1;
921 d->file->update_status = file->update_status;
923 if (ran && !d->file->phony)
924 /* Fetch the new modification time.
925 We do this instead of just invalidating the cached time
926 so that a vpath_search can happen. Otherwise, it would
927 never be done because the target is already updated. */
928 f_mtime (d->file, 0);
930 else if (file->update_status == -1)
931 /* Nothing was done for FILE, but it needed nothing done.
932 So mark it now as "succeeded". */
933 file->update_status = 0;
936 /* Check whether another file (whose mtime is THIS_MTIME) needs updating on
937 account of a dependency which is file FILE. If it does, store 1 in
938 *MUST_MAKE_PTR. In the process, update any non-intermediate files that
939 FILE depends on (including FILE itself). Return nonzero if any updating
940 failed. */
942 static int
943 check_dep (struct file *file, unsigned int depth,
944 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
946 struct dep *d;
947 int dep_status = 0;
949 ++depth;
950 start_updating (file);
952 if (file->phony || !file->intermediate)
954 /* If this is a non-intermediate file, update it and record whether it
955 is newer than THIS_MTIME. */
956 FILE_TIMESTAMP mtime;
957 dep_status = update_file (file, depth);
958 check_renamed (file);
959 mtime = file_mtime (file);
960 check_renamed (file);
961 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
962 *must_make_ptr = 1;
964 else
966 /* FILE is an intermediate file. */
967 FILE_TIMESTAMP mtime;
969 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
971 if (try_implicit_rule (file, depth))
972 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
973 else
974 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
975 file->tried_implicit = 1;
977 if (file->cmds == 0 && !file->is_target
978 && default_file != 0 && default_file->cmds != 0)
980 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
981 file->cmds = default_file->cmds;
984 check_renamed (file);
985 mtime = file_mtime (file);
986 check_renamed (file);
987 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
988 /* If the intermediate file actually exists and is newer, then we
989 should remake from it. */
990 *must_make_ptr = 1;
991 else
993 /* Otherwise, update all non-intermediate files we depend on, if
994 necessary, and see whether any of them is more recent than the
995 file on whose behalf we are checking. */
996 struct dep *ld;
997 int deps_running = 0;
999 /* If this target is not running, set it's state so that we check it
1000 fresh. It could be it was checked as part of an order-only
1001 prerequisite and so wasn't rebuilt then, but should be now. */
1002 if (file->command_state != cs_running)
1003 set_command_state (file, cs_not_started);
1005 ld = 0;
1006 d = file->deps;
1007 while (d != 0)
1009 int maybe_make;
1011 if (is_updating (d->file))
1013 error (NILF, _("Circular %s <- %s dependency dropped."),
1014 file->name, d->file->name);
1015 if (ld == 0)
1017 file->deps = d->next;
1018 free_dep (d);
1019 d = file->deps;
1021 else
1023 ld->next = d->next;
1024 free_dep (d);
1025 d = ld->next;
1027 continue;
1030 d->file->parent = file;
1031 maybe_make = *must_make_ptr;
1032 dep_status |= check_dep (d->file, depth, this_mtime,
1033 &maybe_make);
1034 if (! d->ignore_mtime)
1035 *must_make_ptr = maybe_make;
1036 check_renamed (d->file);
1037 if (dep_status != 0 && !keep_going_flag)
1038 break;
1040 if (d->file->command_state == cs_running
1041 || d->file->command_state == cs_deps_running)
1042 deps_running = 1;
1044 ld = d;
1045 d = d->next;
1048 if (deps_running)
1049 /* Record that some of FILE's deps are still being made.
1050 This tells the upper levels to wait on processing it until the
1051 commands are finished. */
1052 set_command_state (file, cs_deps_running);
1056 finish_updating (file);
1057 return dep_status;
1060 /* Touch FILE. Return zero if successful, one if not. */
1062 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1064 static int
1065 touch_file (struct file *file)
1067 if (!silent_flag)
1068 message (0, "touch %s", file->name);
1070 #ifndef NO_ARCHIVES
1071 if (ar_name (file->name))
1072 return ar_touch (file->name);
1073 else
1074 #endif
1076 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1078 if (fd < 0)
1079 TOUCH_ERROR ("touch: open: ");
1080 else
1082 struct stat statbuf;
1083 char buf = 'x';
1084 int e;
1086 EINTRLOOP (e, fstat (fd, &statbuf));
1087 if (e < 0)
1088 TOUCH_ERROR ("touch: fstat: ");
1089 /* Rewrite character 0 same as it already is. */
1090 if (read (fd, &buf, 1) < 0)
1091 TOUCH_ERROR ("touch: read: ");
1092 if (lseek (fd, 0L, 0) < 0L)
1093 TOUCH_ERROR ("touch: lseek: ");
1094 if (write (fd, &buf, 1) < 0)
1095 TOUCH_ERROR ("touch: write: ");
1096 /* If file length was 0, we just
1097 changed it, so change it back. */
1098 if (statbuf.st_size == 0)
1100 (void) close (fd);
1101 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1102 if (fd < 0)
1103 TOUCH_ERROR ("touch: open: ");
1105 (void) close (fd);
1109 return 0;
1112 /* Having checked and updated the dependencies of FILE,
1113 do whatever is appropriate to remake FILE itself.
1114 Return the status from executing FILE's commands. */
1116 static void
1117 remake_file (struct file *file)
1119 if (file->cmds == 0)
1121 if (file->phony)
1122 /* Phony target. Pretend it succeeded. */
1123 file->update_status = 0;
1124 else if (file->is_target)
1125 /* This is a nonexistent target file we cannot make.
1126 Pretend it was successfully remade. */
1127 file->update_status = 0;
1128 else
1130 /* This is a dependency file we cannot remake. Fail. */
1131 if (!rebuilding_makefiles || !file->dontcare)
1132 complain (file);
1133 file->update_status = 2;
1136 else
1138 chop_commands (file->cmds);
1140 /* The normal case: start some commands. */
1141 if (!touch_flag || file->cmds->any_recurse)
1143 execute_file_commands (file);
1144 return;
1147 /* This tells notice_finished_file it is ok to touch the file. */
1148 file->update_status = 0;
1151 /* This does the touching under -t. */
1152 notice_finished_file (file);
1155 /* Return the mtime of a file, given a `struct file'.
1156 Caches the time in the struct file to avoid excess stat calls.
1158 If the file is not found, and SEARCH is nonzero, VPATH searching and
1159 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1160 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1161 FILE. */
1163 FILE_TIMESTAMP
1164 f_mtime (struct file *file, int search)
1166 FILE_TIMESTAMP mtime;
1168 /* File's mtime is not known; must get it from the system. */
1170 #ifndef NO_ARCHIVES
1171 if (ar_name (file->name))
1173 /* This file is an archive-member reference. */
1175 char *arname, *memname;
1176 struct file *arfile;
1177 time_t member_date;
1179 /* Find the archive's name. */
1180 ar_parse_name (file->name, &arname, &memname);
1182 /* Find the modification time of the archive itself.
1183 Also allow for its name to be changed via VPATH search. */
1184 arfile = lookup_file (arname);
1185 if (arfile == 0)
1186 arfile = enter_file (strcache_add (arname));
1187 mtime = f_mtime (arfile, search);
1188 check_renamed (arfile);
1189 if (search && strcmp (arfile->hname, arname))
1191 /* The archive's name has changed.
1192 Change the archive-member reference accordingly. */
1194 char *name;
1195 unsigned int arlen, memlen;
1197 arlen = strlen (arfile->hname);
1198 memlen = strlen (memname);
1200 name = xmalloc (arlen + 1 + memlen + 2);
1201 memcpy (name, arfile->hname, arlen);
1202 name[arlen] = '(';
1203 memcpy (name + arlen + 1, memname, memlen);
1204 name[arlen + 1 + memlen] = ')';
1205 name[arlen + 1 + memlen + 1] = '\0';
1207 /* If the archive was found with GPATH, make the change permanent;
1208 otherwise defer it until later. */
1209 if (arfile->name == arfile->hname)
1210 rename_file (file, name);
1211 else
1212 rehash_file (file, name);
1213 check_renamed (file);
1216 free (arname);
1218 file->low_resolution_time = 1;
1220 if (mtime == NONEXISTENT_MTIME)
1221 /* The archive doesn't exist, so its members don't exist either. */
1222 return NONEXISTENT_MTIME;
1224 member_date = ar_member_date (file->hname);
1225 mtime = (member_date == (time_t) -1
1226 ? NONEXISTENT_MTIME
1227 : file_timestamp_cons (file->hname, member_date, 0));
1229 else
1230 #endif
1232 mtime = name_mtime (file->name);
1234 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1236 /* If name_mtime failed, search VPATH. */
1237 const char *name = vpath_search (file->name, &mtime);
1238 if (name
1239 /* Last resort, is it a library (-lxxx)? */
1240 || (file->name[0] == '-' && file->name[1] == 'l'
1241 && (name = library_search (file->name, &mtime)) != 0))
1243 if (mtime != UNKNOWN_MTIME)
1244 /* vpath_search and library_search store UNKNOWN_MTIME
1245 if they didn't need to do a stat call for their work. */
1246 file->last_mtime = mtime;
1248 /* If we found it in VPATH, see if it's in GPATH too; if so,
1249 change the name right now; if not, defer until after the
1250 dependencies are updated. */
1251 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1253 rename_file (file, name);
1254 check_renamed (file);
1255 return file_mtime (file);
1258 rehash_file (file, name);
1259 check_renamed (file);
1260 /* If the result of a vpath search is -o or -W, preserve it.
1261 Otherwise, find the mtime of the resulting file. */
1262 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1263 mtime = name_mtime (name);
1268 /* Files can have bogus timestamps that nothing newly made will be
1269 "newer" than. Updating their dependents could just result in loops.
1270 So notify the user of the anomaly with a warning.
1272 We only need to do this once, for now. */
1274 if (!clock_skew_detected
1275 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1276 && !file->updated)
1278 static FILE_TIMESTAMP adjusted_now;
1280 FILE_TIMESTAMP adjusted_mtime = mtime;
1282 #if defined(WINDOWS32) || defined(__MSDOS__)
1283 /* Experimentation has shown that FAT filesystems can set file times
1284 up to 3 seconds into the future! Play it safe. */
1286 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1288 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1289 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1290 adjusted_mtime -= adjustment;
1291 #elif defined(__EMX__)
1292 /* FAT filesystems round time to the nearest even second!
1293 Allow for any file (NTFS or FAT) to perhaps suffer from this
1294 brain damage. */
1295 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1296 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1297 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1298 : 0);
1299 #endif
1301 /* If the file's time appears to be in the future, update our
1302 concept of the present and try once more. */
1303 if (adjusted_now < adjusted_mtime)
1305 int resolution;
1306 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1307 adjusted_now = now + (resolution - 1);
1308 if (adjusted_now < adjusted_mtime)
1310 #ifdef NO_FLOAT
1311 error (NILF, _("Warning: File `%s' has modification time in the future"),
1312 file->name);
1313 #else
1314 double from_now =
1315 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1316 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1317 / 1e9));
1318 char from_now_string[100];
1320 if (from_now >= 99 && from_now <= ULONG_MAX)
1321 sprintf (from_now_string, "%lu", (unsigned long) from_now);
1322 else
1323 sprintf (from_now_string, "%.2g", from_now);
1324 error (NILF, _("Warning: File `%s' has modification time %s s in the future"),
1325 file->name, from_now_string);
1326 #endif
1327 clock_skew_detected = 1;
1332 /* Store the mtime into all the entries for this file. */
1333 if (file->double_colon)
1334 file = file->double_colon;
1338 /* If this file is not implicit but it is intermediate then it was
1339 made so by the .INTERMEDIATE target. If this file has never
1340 been built by us but was found now, it existed before make
1341 started. So, turn off the intermediate bit so make doesn't
1342 delete it, since it didn't create it. */
1343 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1344 && file->command_state == cs_not_started
1345 && !file->tried_implicit && file->intermediate)
1346 file->intermediate = 0;
1348 file->last_mtime = mtime;
1349 file = file->prev;
1351 while (file != 0);
1353 return mtime;
1357 /* Return the mtime of the file or archive-member reference NAME. */
1359 /* First, we check with stat(). If the file does not exist, then we return
1360 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1361 examine each indirection of the symlink and find the newest mtime.
1362 This causes one duplicate stat() when -L is being used, but the code is
1363 much cleaner. */
1365 static FILE_TIMESTAMP
1366 name_mtime (const char *name)
1368 FILE_TIMESTAMP mtime;
1369 struct stat st;
1370 int e;
1372 EINTRLOOP (e, stat (name, &st));
1373 if (e == 0)
1374 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1375 else if (errno == ENOENT || errno == ENOTDIR)
1376 mtime = NONEXISTENT_MTIME;
1377 else
1379 perror_with_name ("stat: ", name);
1380 return NONEXISTENT_MTIME;
1383 /* If we get here we either found it, or it doesn't exist.
1384 If it doesn't exist see if we can use a symlink mtime instead. */
1386 #ifdef MAKE_SYMLINKS
1387 #ifndef S_ISLNK
1388 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1389 #endif
1390 if (check_symlink_flag)
1392 PATH_VAR (lpath);
1394 /* Check each symbolic link segment (if any). Find the latest mtime
1395 amongst all of them (and the target file of course).
1396 Note that we have already successfully dereferenced all the links
1397 above. So, if we run into any error trying to lstat(), or
1398 readlink(), or whatever, something bizarre-o happened. Just give up
1399 and use whatever mtime we've already computed at that point. */
1400 strcpy (lpath, name);
1401 while (1)
1403 FILE_TIMESTAMP ltime;
1404 PATH_VAR (lbuf);
1405 long llen;
1406 char *p;
1408 EINTRLOOP (e, lstat (lpath, &st));
1409 if (e)
1411 /* Just take what we have so far. */
1412 if (errno != ENOENT && errno != ENOTDIR)
1413 perror_with_name ("lstat: ", lpath);
1414 break;
1417 /* If this is not a symlink, we're done (we started with the real
1418 file's mtime so we don't need to test it again). */
1419 if (!S_ISLNK (st.st_mode))
1420 break;
1422 /* If this mtime is newer than what we had, keep the new one. */
1423 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1424 if (ltime > mtime)
1425 mtime = ltime;
1427 /* Set up to check the file pointed to by this link. */
1428 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1429 if (llen < 0)
1431 /* Eh? Just take what we have. */
1432 perror_with_name ("readlink: ", lpath);
1433 break;
1435 lbuf[llen] = '\0';
1437 /* If the target is fully-qualified or the source is just a
1438 filename, then the new path is the target. Otherwise it's the
1439 source directory plus the target. */
1440 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1441 strcpy (lpath, lbuf);
1442 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1443 /* Eh? Path too long! Again, just go with what we have. */
1444 break;
1445 else
1446 /* Create the next step in the symlink chain. */
1447 strcpy (p+1, lbuf);
1450 #endif
1452 return mtime;
1456 /* Search for a library file specified as -lLIBNAME, searching for a
1457 suitable library file in the system library directories and the VPATH
1458 directories. */
1460 static const char *
1461 library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
1463 static char *dirs[] =
1465 #ifndef _AMIGA
1466 "/lib",
1467 "/usr/lib",
1468 #endif
1469 #if defined(WINDOWS32) && !defined(LIBDIR)
1471 * This is completely up to the user at product install time. Just define
1472 * a placeholder.
1474 #define LIBDIR "."
1475 #endif
1476 LIBDIR, /* Defined by configuration. */
1480 const char *file = 0;
1481 char *libpatterns;
1482 FILE_TIMESTAMP mtime;
1484 /* Loop variables for the libpatterns value. */
1485 char *p;
1486 const char *p2;
1487 unsigned int len;
1488 unsigned int liblen;
1490 char **dp;
1492 libpatterns = xstrdup (variable_expand ("$(.LIBPATTERNS)"));
1494 /* Skip the '-l'. */
1495 lib += 2;
1496 liblen = strlen (lib);
1498 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1499 p2 = libpatterns;
1500 while ((p = find_next_token (&p2, &len)) != 0)
1502 static char *buf = NULL;
1503 static unsigned int buflen = 0;
1504 static int libdir_maxlen = -1;
1505 char *libbuf = variable_expand ("");
1507 /* Expand the pattern using LIB as a replacement. */
1509 char c = p[len];
1510 char *p3, *p4;
1512 p[len] = '\0';
1513 p3 = find_percent (p);
1514 if (!p3)
1516 /* Give a warning if there is no pattern. */
1517 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1518 p[len] = c;
1519 continue;
1521 p4 = variable_buffer_output (libbuf, p, p3-p);
1522 p4 = variable_buffer_output (p4, lib, liblen);
1523 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1524 p[len] = c;
1527 /* Look first for `libNAME.a' in the current directory. */
1528 mtime = name_mtime (libbuf);
1529 if (mtime != NONEXISTENT_MTIME)
1531 if (mtime_ptr != 0)
1532 *mtime_ptr = mtime;
1533 file = strcache_add (libbuf);
1534 goto fini;
1537 /* Now try VPATH search on that. */
1540 file = vpath_search (libbuf, mtime_ptr);
1541 if (file)
1542 goto fini;
1545 /* Now try the standard set of directories. */
1547 if (!buflen)
1549 for (dp = dirs; *dp != 0; ++dp)
1551 int l = strlen (*dp);
1552 if (l > libdir_maxlen)
1553 libdir_maxlen = l;
1555 buflen = strlen (libbuf);
1556 buf = xmalloc(libdir_maxlen + buflen + 2);
1558 else if (buflen < strlen (libbuf))
1560 buflen = strlen (libbuf);
1561 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1564 for (dp = dirs; *dp != 0; ++dp)
1566 sprintf (buf, "%s/%s", *dp, libbuf);
1567 mtime = name_mtime (buf);
1568 if (mtime != NONEXISTENT_MTIME)
1570 if (mtime_ptr != 0)
1571 *mtime_ptr = mtime;
1572 file = strcache_add (buf);
1573 goto fini;
1578 fini:
1579 free (libpatterns);
1580 return file;