Changes from Ralf Wildenhues.
[make.git] / remake.c
blob4a4fb731c8fdecfe992e0f9e46b2ed9c1f10c15f
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 file *ofile;
377 struct dep *d, *ad;
378 struct dep amake;
379 int running = 0;
381 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
383 if (file->updated)
385 if (file->update_status > 0)
387 DBF (DB_VERBOSE,
388 _("Recently tried and failed to update file `%s'.\n"));
390 /* If the file we tried to make is marked dontcare then no message
391 was printed about it when it failed during the makefile rebuild.
392 If we're trying to build it again in the normal rebuild, print a
393 message now. */
394 if (file->dontcare && !rebuilding_makefiles)
396 file->dontcare = 0;
397 complain (file);
400 return file->update_status;
403 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
404 return 0;
407 switch (file->command_state)
409 case cs_not_started:
410 case cs_deps_running:
411 break;
412 case cs_running:
413 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
414 return 0;
415 case cs_finished:
416 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
417 return file->update_status;
418 default:
419 abort ();
422 ++depth;
424 /* Notice recursive update of the same file. */
425 start_updating (file);
427 /* We might change file if we find a different one via vpath;
428 remember this one to turn off updating. */
429 ofile = file;
431 /* Looking at the file's modtime beforehand allows the possibility
432 that its name may be changed by a VPATH search, and thus it may
433 not need an implicit rule. If this were not done, the file
434 might get implicit commands that apply to its initial name, only
435 to have that name replaced with another found by VPATH search. */
437 this_mtime = file_mtime (file);
438 check_renamed (file);
439 noexist = this_mtime == NONEXISTENT_MTIME;
440 if (noexist)
441 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
442 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
443 && file->low_resolution_time)
445 /* Avoid spurious rebuilds due to low resolution time stamps. */
446 int ns = FILE_TIMESTAMP_NS (this_mtime);
447 if (ns != 0)
448 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
449 file->name);
450 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
453 must_make = noexist;
455 /* If file was specified as a target with no commands,
456 come up with some default commands. */
458 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
460 if (try_implicit_rule (file, depth))
461 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
462 else
463 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
464 file->tried_implicit = 1;
466 if (file->cmds == 0 && !file->is_target
467 && default_file != 0 && default_file->cmds != 0)
469 DBF (DB_IMPLICIT, _("Using default recipe for `%s'.\n"));
470 file->cmds = default_file->cmds;
473 /* Update all non-intermediate files we depend on, if necessary, and see
474 whether any of them is more recent than this file. We need to walk our
475 deps, AND the deps of any also_make targets to ensure everything happens
476 in the correct order. */
478 amake.file = file;
479 amake.next = file->also_make;
480 ad = &amake;
481 while (ad)
483 struct dep *lastd = 0;
485 /* Find the deps we're scanning */
486 d = ad->file->deps;
487 ad = ad->next;
489 while (d)
491 FILE_TIMESTAMP mtime;
492 int maybe_make;
493 int dontcare = 0;
495 check_renamed (d->file);
497 mtime = file_mtime (d->file);
498 check_renamed (d->file);
500 if (is_updating (d->file))
502 error (NILF, _("Circular %s <- %s dependency dropped."),
503 file->name, d->file->name);
504 /* We cannot free D here because our the caller will still have
505 a reference to it when we were called recursively via
506 check_dep below. */
507 if (lastd == 0)
508 file->deps = d->next;
509 else
510 lastd->next = d->next;
511 d = d->next;
512 continue;
515 d->file->parent = file;
516 maybe_make = must_make;
518 /* Inherit dontcare flag from our parent. */
519 if (rebuilding_makefiles)
521 dontcare = d->file->dontcare;
522 d->file->dontcare = file->dontcare;
525 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
527 /* Restore original dontcare flag. */
528 if (rebuilding_makefiles)
529 d->file->dontcare = dontcare;
531 if (! d->ignore_mtime)
532 must_make = maybe_make;
534 check_renamed (d->file);
537 register struct file *f = d->file;
538 if (f->double_colon)
539 f = f->double_colon;
542 running |= (f->command_state == cs_running
543 || f->command_state == cs_deps_running);
544 f = f->prev;
546 while (f != 0);
549 if (dep_status != 0 && !keep_going_flag)
550 break;
552 if (!running)
553 /* The prereq is considered changed if the timestamp has changed while
554 it was built, OR it doesn't exist. */
555 d->changed = ((file_mtime (d->file) != mtime)
556 || (mtime == NONEXISTENT_MTIME));
558 lastd = d;
559 d = d->next;
563 /* Now we know whether this target needs updating.
564 If it does, update all the intermediate files we depend on. */
566 if (must_make || always_make_flag)
568 for (d = file->deps; d != 0; d = d->next)
569 if (d->file->intermediate)
571 int dontcare = 0;
573 FILE_TIMESTAMP mtime = file_mtime (d->file);
574 check_renamed (d->file);
575 d->file->parent = file;
577 /* Inherit dontcare flag from our parent. */
578 if (rebuilding_makefiles)
580 dontcare = d->file->dontcare;
581 d->file->dontcare = file->dontcare;
585 dep_status |= update_file (d->file, depth);
587 /* Restore original dontcare flag. */
588 if (rebuilding_makefiles)
589 d->file->dontcare = dontcare;
591 check_renamed (d->file);
594 register struct file *f = d->file;
595 if (f->double_colon)
596 f = f->double_colon;
599 running |= (f->command_state == cs_running
600 || f->command_state == cs_deps_running);
601 f = f->prev;
603 while (f != 0);
606 if (dep_status != 0 && !keep_going_flag)
607 break;
609 if (!running)
610 d->changed = ((file->phony && file->cmds != 0)
611 || file_mtime (d->file) != mtime);
615 finish_updating (file);
616 finish_updating (ofile);
618 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
620 if (running)
622 set_command_state (file, cs_deps_running);
623 --depth;
624 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
625 return 0;
628 /* If any dependency failed, give up now. */
630 if (dep_status != 0)
632 file->update_status = dep_status;
633 notice_finished_file (file);
635 --depth;
637 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
639 if (depth == 0 && keep_going_flag
640 && !just_print_flag && !question_flag)
641 error (NILF,
642 _("Target `%s' not remade because of errors."), file->name);
644 return dep_status;
647 if (file->command_state == cs_deps_running)
648 /* The commands for some deps were running on the last iteration, but
649 they have finished now. Reset the command_state to not_started to
650 simplify later bookkeeping. It is important that we do this only
651 when the prior state was cs_deps_running, because that prior state
652 was definitely propagated to FILE's also_make's by set_command_state
653 (called above), but in another state an also_make may have
654 independently changed to finished state, and we would confuse that
655 file's bookkeeping (updated, but not_started is bogus state). */
656 set_command_state (file, cs_not_started);
658 /* Now record which prerequisites are more
659 recent than this file, so we can define $?. */
661 deps_changed = 0;
662 for (d = file->deps; d != 0; d = d->next)
664 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
665 check_renamed (d->file);
667 if (! d->ignore_mtime)
669 #if 1
670 /* %%% In version 4, remove this code completely to
671 implement not remaking deps if their deps are newer
672 than their parents. */
673 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
674 /* We must remake if this dep does not
675 exist and is not intermediate. */
676 must_make = 1;
677 #endif
679 /* Set DEPS_CHANGED if this dep actually changed. */
680 deps_changed |= d->changed;
683 /* Set D->changed if either this dep actually changed,
684 or its dependent, FILE, is older or does not exist. */
685 d->changed |= noexist || d_mtime > this_mtime;
687 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
689 const char *fmt = 0;
691 if (d->ignore_mtime)
693 if (ISDB (DB_VERBOSE))
694 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
696 else if (d_mtime == NONEXISTENT_MTIME)
698 if (ISDB (DB_BASIC))
699 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
701 else if (d->changed)
703 if (ISDB (DB_BASIC))
704 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
706 else if (ISDB (DB_VERBOSE))
707 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
709 if (fmt)
711 print_spaces (depth);
712 printf (fmt, dep_name (d), file->name);
713 fflush (stdout);
718 /* Here depth returns to the value it had when we were called. */
719 depth--;
721 if (file->double_colon && file->deps == 0)
723 must_make = 1;
724 DBF (DB_BASIC,
725 _("Target `%s' is double-colon and has no prerequisites.\n"));
727 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
728 && !always_make_flag)
730 must_make = 0;
731 DBF (DB_VERBOSE,
732 _("No recipe for `%s' and no prerequisites actually changed.\n"));
734 else if (!must_make && file->cmds != 0 && always_make_flag)
736 must_make = 1;
737 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
740 if (!must_make)
742 if (ISDB (DB_VERBOSE))
744 print_spaces (depth);
745 printf (_("No need to remake target `%s'"), file->name);
746 if (!streq (file->name, file->hname))
747 printf (_("; using VPATH name `%s'"), file->hname);
748 puts (".");
749 fflush (stdout);
752 notice_finished_file (file);
754 /* Since we don't need to remake the file, convert it to use the
755 VPATH filename if we found one. hfile will be either the
756 local name if no VPATH or the VPATH name if one was found. */
758 while (file)
760 file->name = file->hname;
761 file = file->prev;
764 return 0;
767 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
769 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
770 VPATH. */
771 if (!streq(file->name, file->hname))
773 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
774 file->ignore_vpath = 1;
777 /* Now, take appropriate actions to remake the file. */
778 remake_file (file);
780 if (file->command_state != cs_finished)
782 DBF (DB_VERBOSE, _("Recipe of `%s' is being run.\n"));
783 return 0;
786 switch (file->update_status)
788 case 2:
789 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
790 break;
791 case 0:
792 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
793 break;
794 case 1:
795 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
796 break;
797 default:
798 assert (file->update_status >= 0 && file->update_status <= 2);
799 break;
802 file->updated = 1;
803 return file->update_status;
806 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
807 files listed in its `also_make' member. Under -t, this function also
808 touches FILE.
810 On return, FILE->update_status will no longer be -1 if it was. */
812 void
813 notice_finished_file (struct file *file)
815 struct dep *d;
816 int ran = file->command_state == cs_running;
817 int touched = 0;
819 file->command_state = cs_finished;
820 file->updated = 1;
822 if (touch_flag
823 /* The update status will be:
824 -1 if this target was not remade;
825 0 if 0 or more commands (+ or ${MAKE}) were run and won;
826 1 if some commands were run and lost.
827 We touch the target if it has commands which either were not run
828 or won when they ran (i.e. status is 0). */
829 && file->update_status == 0)
831 if (file->cmds != 0 && file->cmds->any_recurse)
833 /* If all the command lines were recursive,
834 we don't want to do the touching. */
835 unsigned int i;
836 for (i = 0; i < file->cmds->ncommand_lines; ++i)
837 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
838 goto have_nonrecursing;
840 else
842 have_nonrecursing:
843 if (file->phony)
844 file->update_status = 0;
845 /* According to POSIX, -t doesn't affect targets with no cmds. */
846 else if (file->cmds != 0)
848 /* Should set file's modification date and do nothing else. */
849 file->update_status = touch_file (file);
851 /* Pretend we ran a real touch command, to suppress the
852 "`foo' is up to date" message. */
853 commands_started++;
855 /* Request for the timestamp to be updated (and distributed
856 to the double-colon entries). Simply setting ran=1 would
857 almost have done the trick, but messes up with the also_make
858 updating logic below. */
859 touched = 1;
864 if (file->mtime_before_update == UNKNOWN_MTIME)
865 file->mtime_before_update = file->last_mtime;
867 if ((ran && !file->phony) || touched)
869 int i = 0;
871 /* If -n, -t, or -q and all the commands are recursive, we ran them so
872 really check the target's mtime again. Otherwise, assume the target
873 would have been updated. */
875 if (question_flag || just_print_flag || touch_flag)
877 for (i = file->cmds->ncommand_lines; i > 0; --i)
878 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
879 break;
882 /* If there were no commands at all, it's always new. */
884 else if (file->is_target && file->cmds == 0)
885 i = 1;
887 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
890 if (file->double_colon)
892 /* If this is a double colon rule and it is the last one to be
893 updated, propagate the change of modification time to all the
894 double-colon entries for this file.
896 We do it on the last update because it is important to handle
897 individual entries as separate rules with separate timestamps
898 while they are treated as targets and then as one rule with the
899 unified timestamp when they are considered as a prerequisite
900 of some target. */
902 struct file *f;
903 FILE_TIMESTAMP max_mtime = file->last_mtime;
905 /* Check that all rules were updated and at the same time find
906 the max timestamp. We assume UNKNOWN_MTIME is newer then
907 any other value. */
908 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
909 if (max_mtime != UNKNOWN_MTIME
910 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
911 max_mtime = f->last_mtime;
913 if (f == 0)
914 for (f = file->double_colon; f != 0; f = f->prev)
915 f->last_mtime = max_mtime;
918 if (ran && file->update_status != -1)
919 /* We actually tried to update FILE, which has
920 updated its also_make's as well (if it worked).
921 If it didn't work, it wouldn't work again for them.
922 So mark them as updated with the same status. */
923 for (d = file->also_make; d != 0; d = d->next)
925 d->file->command_state = cs_finished;
926 d->file->updated = 1;
927 d->file->update_status = file->update_status;
929 if (ran && !d->file->phony)
930 /* Fetch the new modification time.
931 We do this instead of just invalidating the cached time
932 so that a vpath_search can happen. Otherwise, it would
933 never be done because the target is already updated. */
934 f_mtime (d->file, 0);
936 else if (file->update_status == -1)
937 /* Nothing was done for FILE, but it needed nothing done.
938 So mark it now as "succeeded". */
939 file->update_status = 0;
942 /* Check whether another file (whose mtime is THIS_MTIME) needs updating on
943 account of a dependency which is file FILE. If it does, store 1 in
944 *MUST_MAKE_PTR. In the process, update any non-intermediate files that
945 FILE depends on (including FILE itself). Return nonzero if any updating
946 failed. */
948 static int
949 check_dep (struct file *file, unsigned int depth,
950 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
952 struct file *ofile;
953 struct dep *d;
954 int dep_status = 0;
956 ++depth;
957 start_updating (file);
959 /* We might change file if we find a different one via vpath;
960 remember this one to turn off updating. */
961 ofile = file;
963 if (file->phony || !file->intermediate)
965 /* If this is a non-intermediate file, update it and record whether it
966 is newer than THIS_MTIME. */
967 FILE_TIMESTAMP mtime;
968 dep_status = update_file (file, depth);
969 check_renamed (file);
970 mtime = file_mtime (file);
971 check_renamed (file);
972 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
973 *must_make_ptr = 1;
975 else
977 /* FILE is an intermediate file. */
978 FILE_TIMESTAMP mtime;
980 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
982 if (try_implicit_rule (file, depth))
983 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
984 else
985 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
986 file->tried_implicit = 1;
988 if (file->cmds == 0 && !file->is_target
989 && default_file != 0 && default_file->cmds != 0)
991 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
992 file->cmds = default_file->cmds;
995 check_renamed (file);
996 mtime = file_mtime (file);
997 check_renamed (file);
998 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
999 /* If the intermediate file actually exists and is newer, then we
1000 should remake from it. */
1001 *must_make_ptr = 1;
1002 else
1004 /* Otherwise, update all non-intermediate files we depend on, if
1005 necessary, and see whether any of them is more recent than the
1006 file on whose behalf we are checking. */
1007 struct dep *ld;
1008 int deps_running = 0;
1010 /* If this target is not running, set it's state so that we check it
1011 fresh. It could be it was checked as part of an order-only
1012 prerequisite and so wasn't rebuilt then, but should be now. */
1013 if (file->command_state != cs_running)
1014 set_command_state (file, cs_not_started);
1016 ld = 0;
1017 d = file->deps;
1018 while (d != 0)
1020 int maybe_make;
1022 if (is_updating (d->file))
1024 error (NILF, _("Circular %s <- %s dependency dropped."),
1025 file->name, d->file->name);
1026 if (ld == 0)
1028 file->deps = d->next;
1029 free_dep (d);
1030 d = file->deps;
1032 else
1034 ld->next = d->next;
1035 free_dep (d);
1036 d = ld->next;
1038 continue;
1041 d->file->parent = file;
1042 maybe_make = *must_make_ptr;
1043 dep_status |= check_dep (d->file, depth, this_mtime,
1044 &maybe_make);
1045 if (! d->ignore_mtime)
1046 *must_make_ptr = maybe_make;
1047 check_renamed (d->file);
1048 if (dep_status != 0 && !keep_going_flag)
1049 break;
1051 if (d->file->command_state == cs_running
1052 || d->file->command_state == cs_deps_running)
1053 deps_running = 1;
1055 ld = d;
1056 d = d->next;
1059 if (deps_running)
1060 /* Record that some of FILE's deps are still being made.
1061 This tells the upper levels to wait on processing it until the
1062 commands are finished. */
1063 set_command_state (file, cs_deps_running);
1067 finish_updating (file);
1068 finish_updating (ofile);
1070 return dep_status;
1073 /* Touch FILE. Return zero if successful, one if not. */
1075 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1077 static int
1078 touch_file (struct file *file)
1080 if (!silent_flag)
1081 message (0, "touch %s", file->name);
1083 #ifndef NO_ARCHIVES
1084 if (ar_name (file->name))
1085 return ar_touch (file->name);
1086 else
1087 #endif
1089 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1091 if (fd < 0)
1092 TOUCH_ERROR ("touch: open: ");
1093 else
1095 struct stat statbuf;
1096 char buf = 'x';
1097 int e;
1099 EINTRLOOP (e, fstat (fd, &statbuf));
1100 if (e < 0)
1101 TOUCH_ERROR ("touch: fstat: ");
1102 /* Rewrite character 0 same as it already is. */
1103 if (read (fd, &buf, 1) < 0)
1104 TOUCH_ERROR ("touch: read: ");
1105 if (lseek (fd, 0L, 0) < 0L)
1106 TOUCH_ERROR ("touch: lseek: ");
1107 if (write (fd, &buf, 1) < 0)
1108 TOUCH_ERROR ("touch: write: ");
1109 /* If file length was 0, we just
1110 changed it, so change it back. */
1111 if (statbuf.st_size == 0)
1113 (void) close (fd);
1114 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1115 if (fd < 0)
1116 TOUCH_ERROR ("touch: open: ");
1118 (void) close (fd);
1122 return 0;
1125 /* Having checked and updated the dependencies of FILE,
1126 do whatever is appropriate to remake FILE itself.
1127 Return the status from executing FILE's commands. */
1129 static void
1130 remake_file (struct file *file)
1132 if (file->cmds == 0)
1134 if (file->phony)
1135 /* Phony target. Pretend it succeeded. */
1136 file->update_status = 0;
1137 else if (file->is_target)
1138 /* This is a nonexistent target file we cannot make.
1139 Pretend it was successfully remade. */
1140 file->update_status = 0;
1141 else
1143 /* This is a dependency file we cannot remake. Fail. */
1144 if (!rebuilding_makefiles || !file->dontcare)
1145 complain (file);
1146 file->update_status = 2;
1149 else
1151 chop_commands (file->cmds);
1153 /* The normal case: start some commands. */
1154 if (!touch_flag || file->cmds->any_recurse)
1156 execute_file_commands (file);
1157 return;
1160 /* This tells notice_finished_file it is ok to touch the file. */
1161 file->update_status = 0;
1164 /* This does the touching under -t. */
1165 notice_finished_file (file);
1168 /* Return the mtime of a file, given a `struct file'.
1169 Caches the time in the struct file to avoid excess stat calls.
1171 If the file is not found, and SEARCH is nonzero, VPATH searching and
1172 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1173 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1174 FILE. */
1176 FILE_TIMESTAMP
1177 f_mtime (struct file *file, int search)
1179 FILE_TIMESTAMP mtime;
1181 /* File's mtime is not known; must get it from the system. */
1183 #ifndef NO_ARCHIVES
1184 if (ar_name (file->name))
1186 /* This file is an archive-member reference. */
1188 char *arname, *memname;
1189 struct file *arfile;
1190 time_t member_date;
1192 /* Find the archive's name. */
1193 ar_parse_name (file->name, &arname, &memname);
1195 /* Find the modification time of the archive itself.
1196 Also allow for its name to be changed via VPATH search. */
1197 arfile = lookup_file (arname);
1198 if (arfile == 0)
1199 arfile = enter_file (strcache_add (arname));
1200 mtime = f_mtime (arfile, search);
1201 check_renamed (arfile);
1202 if (search && strcmp (arfile->hname, arname))
1204 /* The archive's name has changed.
1205 Change the archive-member reference accordingly. */
1207 char *name;
1208 unsigned int arlen, memlen;
1210 arlen = strlen (arfile->hname);
1211 memlen = strlen (memname);
1213 name = xmalloc (arlen + 1 + memlen + 2);
1214 memcpy (name, arfile->hname, arlen);
1215 name[arlen] = '(';
1216 memcpy (name + arlen + 1, memname, memlen);
1217 name[arlen + 1 + memlen] = ')';
1218 name[arlen + 1 + memlen + 1] = '\0';
1220 /* If the archive was found with GPATH, make the change permanent;
1221 otherwise defer it until later. */
1222 if (arfile->name == arfile->hname)
1223 rename_file (file, name);
1224 else
1225 rehash_file (file, name);
1226 check_renamed (file);
1229 free (arname);
1231 file->low_resolution_time = 1;
1233 if (mtime == NONEXISTENT_MTIME)
1234 /* The archive doesn't exist, so its members don't exist either. */
1235 return NONEXISTENT_MTIME;
1237 member_date = ar_member_date (file->hname);
1238 mtime = (member_date == (time_t) -1
1239 ? NONEXISTENT_MTIME
1240 : file_timestamp_cons (file->hname, member_date, 0));
1242 else
1243 #endif
1245 mtime = name_mtime (file->name);
1247 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1249 /* If name_mtime failed, search VPATH. */
1250 const char *name = vpath_search (file->name, &mtime);
1251 if (name
1252 /* Last resort, is it a library (-lxxx)? */
1253 || (file->name[0] == '-' && file->name[1] == 'l'
1254 && (name = library_search (file->name, &mtime)) != 0))
1256 if (mtime != UNKNOWN_MTIME)
1257 /* vpath_search and library_search store UNKNOWN_MTIME
1258 if they didn't need to do a stat call for their work. */
1259 file->last_mtime = mtime;
1261 /* If we found it in VPATH, see if it's in GPATH too; if so,
1262 change the name right now; if not, defer until after the
1263 dependencies are updated. */
1264 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1266 rename_file (file, name);
1267 check_renamed (file);
1268 return file_mtime (file);
1271 rehash_file (file, name);
1272 check_renamed (file);
1273 /* If the result of a vpath search is -o or -W, preserve it.
1274 Otherwise, find the mtime of the resulting file. */
1275 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1276 mtime = name_mtime (name);
1281 /* Files can have bogus timestamps that nothing newly made will be
1282 "newer" than. Updating their dependents could just result in loops.
1283 So notify the user of the anomaly with a warning.
1285 We only need to do this once, for now. */
1287 if (!clock_skew_detected
1288 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1289 && !file->updated)
1291 static FILE_TIMESTAMP adjusted_now;
1293 FILE_TIMESTAMP adjusted_mtime = mtime;
1295 #if defined(WINDOWS32) || defined(__MSDOS__)
1296 /* Experimentation has shown that FAT filesystems can set file times
1297 up to 3 seconds into the future! Play it safe. */
1299 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1301 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1302 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1303 adjusted_mtime -= adjustment;
1304 #elif defined(__EMX__)
1305 /* FAT filesystems round time to the nearest even second!
1306 Allow for any file (NTFS or FAT) to perhaps suffer from this
1307 brain damage. */
1308 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1309 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1310 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1311 : 0);
1312 #endif
1314 /* If the file's time appears to be in the future, update our
1315 concept of the present and try once more. */
1316 if (adjusted_now < adjusted_mtime)
1318 int resolution;
1319 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1320 adjusted_now = now + (resolution - 1);
1321 if (adjusted_now < adjusted_mtime)
1323 #ifdef NO_FLOAT
1324 error (NILF, _("Warning: File `%s' has modification time in the future"),
1325 file->name);
1326 #else
1327 double from_now =
1328 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1329 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1330 / 1e9));
1331 char from_now_string[100];
1333 if (from_now >= 99 && from_now <= ULONG_MAX)
1334 sprintf (from_now_string, "%lu", (unsigned long) from_now);
1335 else
1336 sprintf (from_now_string, "%.2g", from_now);
1337 error (NILF, _("Warning: File `%s' has modification time %s s in the future"),
1338 file->name, from_now_string);
1339 #endif
1340 clock_skew_detected = 1;
1345 /* Store the mtime into all the entries for this file. */
1346 if (file->double_colon)
1347 file = file->double_colon;
1351 /* If this file is not implicit but it is intermediate then it was
1352 made so by the .INTERMEDIATE target. If this file has never
1353 been built by us but was found now, it existed before make
1354 started. So, turn off the intermediate bit so make doesn't
1355 delete it, since it didn't create it. */
1356 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1357 && file->command_state == cs_not_started
1358 && !file->tried_implicit && file->intermediate)
1359 file->intermediate = 0;
1361 file->last_mtime = mtime;
1362 file = file->prev;
1364 while (file != 0);
1366 return mtime;
1370 /* Return the mtime of the file or archive-member reference NAME. */
1372 /* First, we check with stat(). If the file does not exist, then we return
1373 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1374 examine each indirection of the symlink and find the newest mtime.
1375 This causes one duplicate stat() when -L is being used, but the code is
1376 much cleaner. */
1378 static FILE_TIMESTAMP
1379 name_mtime (const char *name)
1381 FILE_TIMESTAMP mtime;
1382 struct stat st;
1383 int e;
1385 EINTRLOOP (e, stat (name, &st));
1386 if (e == 0)
1387 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1388 else if (errno == ENOENT || errno == ENOTDIR)
1389 mtime = NONEXISTENT_MTIME;
1390 else
1392 perror_with_name ("stat: ", name);
1393 return NONEXISTENT_MTIME;
1396 /* If we get here we either found it, or it doesn't exist.
1397 If it doesn't exist see if we can use a symlink mtime instead. */
1399 #ifdef MAKE_SYMLINKS
1400 #ifndef S_ISLNK
1401 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1402 #endif
1403 if (check_symlink_flag)
1405 PATH_VAR (lpath);
1407 /* Check each symbolic link segment (if any). Find the latest mtime
1408 amongst all of them (and the target file of course).
1409 Note that we have already successfully dereferenced all the links
1410 above. So, if we run into any error trying to lstat(), or
1411 readlink(), or whatever, something bizarre-o happened. Just give up
1412 and use whatever mtime we've already computed at that point. */
1413 strcpy (lpath, name);
1414 while (1)
1416 FILE_TIMESTAMP ltime;
1417 PATH_VAR (lbuf);
1418 long llen;
1419 char *p;
1421 EINTRLOOP (e, lstat (lpath, &st));
1422 if (e)
1424 /* Just take what we have so far. */
1425 if (errno != ENOENT && errno != ENOTDIR)
1426 perror_with_name ("lstat: ", lpath);
1427 break;
1430 /* If this is not a symlink, we're done (we started with the real
1431 file's mtime so we don't need to test it again). */
1432 if (!S_ISLNK (st.st_mode))
1433 break;
1435 /* If this mtime is newer than what we had, keep the new one. */
1436 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1437 if (ltime > mtime)
1438 mtime = ltime;
1440 /* Set up to check the file pointed to by this link. */
1441 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1442 if (llen < 0)
1444 /* Eh? Just take what we have. */
1445 perror_with_name ("readlink: ", lpath);
1446 break;
1448 lbuf[llen] = '\0';
1450 /* If the target is fully-qualified or the source is just a
1451 filename, then the new path is the target. Otherwise it's the
1452 source directory plus the target. */
1453 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1454 strcpy (lpath, lbuf);
1455 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1456 /* Eh? Path too long! Again, just go with what we have. */
1457 break;
1458 else
1459 /* Create the next step in the symlink chain. */
1460 strcpy (p+1, lbuf);
1463 #endif
1465 return mtime;
1469 /* Search for a library file specified as -lLIBNAME, searching for a
1470 suitable library file in the system library directories and the VPATH
1471 directories. */
1473 static const char *
1474 library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
1476 static char *dirs[] =
1478 #ifndef _AMIGA
1479 "/lib",
1480 "/usr/lib",
1481 #endif
1482 #if defined(WINDOWS32) && !defined(LIBDIR)
1484 * This is completely up to the user at product install time. Just define
1485 * a placeholder.
1487 #define LIBDIR "."
1488 #endif
1489 LIBDIR, /* Defined by configuration. */
1493 const char *file = 0;
1494 char *libpatterns;
1495 FILE_TIMESTAMP mtime;
1497 /* Loop variables for the libpatterns value. */
1498 char *p;
1499 const char *p2;
1500 unsigned int len;
1501 unsigned int liblen;
1503 char **dp;
1505 libpatterns = xstrdup (variable_expand ("$(.LIBPATTERNS)"));
1507 /* Skip the '-l'. */
1508 lib += 2;
1509 liblen = strlen (lib);
1511 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1512 p2 = libpatterns;
1513 while ((p = find_next_token (&p2, &len)) != 0)
1515 static char *buf = NULL;
1516 static unsigned int buflen = 0;
1517 static int libdir_maxlen = -1;
1518 char *libbuf = variable_expand ("");
1520 /* Expand the pattern using LIB as a replacement. */
1522 char c = p[len];
1523 char *p3, *p4;
1525 p[len] = '\0';
1526 p3 = find_percent (p);
1527 if (!p3)
1529 /* Give a warning if there is no pattern. */
1530 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1531 p[len] = c;
1532 continue;
1534 p4 = variable_buffer_output (libbuf, p, p3-p);
1535 p4 = variable_buffer_output (p4, lib, liblen);
1536 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1537 p[len] = c;
1540 /* Look first for `libNAME.a' in the current directory. */
1541 mtime = name_mtime (libbuf);
1542 if (mtime != NONEXISTENT_MTIME)
1544 if (mtime_ptr != 0)
1545 *mtime_ptr = mtime;
1546 file = strcache_add (libbuf);
1547 goto fini;
1550 /* Now try VPATH search on that. */
1553 file = vpath_search (libbuf, mtime_ptr);
1554 if (file)
1555 goto fini;
1558 /* Now try the standard set of directories. */
1560 if (!buflen)
1562 for (dp = dirs; *dp != 0; ++dp)
1564 int l = strlen (*dp);
1565 if (l > libdir_maxlen)
1566 libdir_maxlen = l;
1568 buflen = strlen (libbuf);
1569 buf = xmalloc(libdir_maxlen + buflen + 2);
1571 else if (buflen < strlen (libbuf))
1573 buflen = strlen (libbuf);
1574 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1577 for (dp = dirs; *dp != 0; ++dp)
1579 sprintf (buf, "%s/%s", *dp, libbuf);
1580 mtime = name_mtime (buf);
1581 if (mtime != NONEXISTENT_MTIME)
1583 if (mtime_ptr != 0)
1584 *mtime_ptr = mtime;
1585 file = strcache_add (buf);
1586 goto fini;
1591 fini:
1592 free (libpatterns);
1593 return file;