More copyright/license updates.
[make.git] / remake.c
blobaaed489f683eb7af8910025213649a829cffccf5
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, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18 #include "make.h"
19 #include "filedef.h"
20 #include "job.h"
21 #include "commands.h"
22 #include "dep.h"
23 #include "variable.h"
24 #include "debug.h"
26 #include <assert.h>
28 #ifdef HAVE_FCNTL_H
29 #include <fcntl.h>
30 #else
31 #include <sys/file.h>
32 #endif
34 #ifdef VMS
35 #include <starlet.h>
36 #endif
37 #ifdef WINDOWS32
38 #include <io.h>
39 #endif
41 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
44 /* The test for circular dependencies is based on the 'updating' bit in
45 `struct file'. However, double colon targets have seperate `struct
46 file's; make sure we always use the base of the double colon chain. */
48 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
49 ->updating = 1)
50 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
51 ->updating = 0)
52 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
53 ->updating)
56 /* Incremented when a command is started (under -n, when one would be). */
57 unsigned int commands_started = 0;
59 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
60 static unsigned int considered;
62 static int update_file PARAMS ((struct file *file, unsigned int depth));
63 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
64 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
65 static int touch_file PARAMS ((struct file *file));
66 static void remake_file PARAMS ((struct file *file));
67 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
68 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
71 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
72 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
74 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
75 and -n should be disabled for them unless they were also command-line
76 targets, and we should only make one goal at a time and return as soon as
77 one goal whose `changed' member is nonzero is successfully made. */
79 int
80 update_goal_chain (struct dep *goals)
82 int t = touch_flag, q = question_flag, n = just_print_flag;
83 unsigned int j = job_slots;
84 int status = -1;
86 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
87 : file_mtime (file))
89 /* Duplicate the chain so we can remove things from it. */
91 goals = copy_dep_chain (goals);
94 /* Clear the `changed' flag of each goal in the chain.
95 We will use the flag below to notice when any commands
96 have actually been run for a target. When no commands
97 have been run, we give an "up to date" diagnostic. */
99 struct dep *g;
100 for (g = goals; g != 0; g = g->next)
101 g->changed = 0;
104 /* All files start with the considered bit 0, so the global value is 1. */
105 considered = 1;
107 /* Update all the goals until they are all finished. */
109 while (goals != 0)
111 register struct dep *g, *lastgoal;
113 /* Start jobs that are waiting for the load to go down. */
115 start_waiting_jobs ();
117 /* Wait for a child to die. */
119 reap_children (1, 0);
121 lastgoal = 0;
122 g = goals;
123 while (g != 0)
125 /* Iterate over all double-colon entries for this file. */
126 struct file *file;
127 int stop = 0, any_not_updated = 0;
129 for (file = g->file->double_colon ? g->file->double_colon : g->file;
130 file != NULL;
131 file = file->prev)
133 unsigned int ocommands_started;
134 int x;
135 check_renamed (file);
136 if (rebuilding_makefiles)
138 if (file->cmd_target)
140 touch_flag = t;
141 question_flag = q;
142 just_print_flag = n;
144 else
145 touch_flag = question_flag = just_print_flag = 0;
148 /* Save the old value of `commands_started' so we can compare
149 later. It will be incremented when any commands are
150 actually run. */
151 ocommands_started = commands_started;
153 x = update_file (file, rebuilding_makefiles ? 1 : 0);
154 check_renamed (file);
156 /* Set the goal's `changed' flag if any commands were started
157 by calling update_file above. We check this flag below to
158 decide when to give an "up to date" diagnostic. */
159 if (commands_started > ocommands_started)
160 g->changed = 1;
162 /* If we updated a file and STATUS was not already 1, set it to
163 1 if updating failed, or to 0 if updating succeeded. Leave
164 STATUS as it is if no updating was done. */
166 stop = 0;
167 if ((x != 0 || file->updated) && status < 1)
169 if (file->update_status != 0)
171 /* Updating failed, or -q triggered. The STATUS value
172 tells our caller which. */
173 status = file->update_status;
174 /* If -q just triggered, stop immediately. It doesn't
175 matter how much more we run, since we already know
176 the answer to return. */
177 stop = (question_flag && !keep_going_flag
178 && !rebuilding_makefiles);
180 else
182 FILE_TIMESTAMP mtime = MTIME (file);
183 check_renamed (file);
185 if (file->updated && g->changed &&
186 mtime != file->mtime_before_update)
188 /* Updating was done. If this is a makefile and
189 just_print_flag or question_flag is set (meaning
190 -n or -q was given and this file was specified
191 as a command-line target), don't change STATUS.
192 If STATUS is changed, we will get re-exec'd, and
193 enter an infinite loop. */
194 if (!rebuilding_makefiles
195 || (!just_print_flag && !question_flag))
196 status = 0;
197 if (rebuilding_makefiles && file->dontcare)
198 /* This is a default makefile; stop remaking. */
199 stop = 1;
204 /* Keep track if any double-colon entry is not finished.
205 When they are all finished, the goal is finished. */
206 any_not_updated |= !file->updated;
208 if (stop)
209 break;
212 /* Reset FILE since it is null at the end of the loop. */
213 file = g->file;
215 if (stop || !any_not_updated)
217 /* If we have found nothing whatever to do for the goal,
218 print a message saying nothing needs doing. */
220 if (!rebuilding_makefiles
221 /* If the update_status is zero, we updated successfully
222 or not at all. G->changed will have been set above if
223 any commands were actually started for this goal. */
224 && file->update_status == 0 && !g->changed
225 /* Never give a message under -s or -q. */
226 && !silent_flag && !question_flag)
227 message (1, ((file->phony || file->cmds == 0)
228 ? _("Nothing to be done for `%s'.")
229 : _("`%s' is up to date.")),
230 file->name);
232 /* This goal is finished. Remove it from the chain. */
233 if (lastgoal == 0)
234 goals = g->next;
235 else
236 lastgoal->next = g->next;
238 /* Free the storage. */
239 free ((char *) g);
241 g = lastgoal == 0 ? goals : lastgoal->next;
243 if (stop)
244 break;
246 else
248 lastgoal = g;
249 g = g->next;
253 /* If we reached the end of the dependency graph toggle the considered
254 flag for the next pass. */
255 if (g == 0)
256 considered = !considered;
259 if (rebuilding_makefiles)
261 touch_flag = t;
262 question_flag = q;
263 just_print_flag = n;
264 job_slots = j;
266 return status;
269 /* If FILE is not up to date, execute the commands for it.
270 Return 0 if successful, 1 if unsuccessful;
271 but with some flag settings, just call `exit' if unsuccessful.
273 DEPTH is the depth in recursions of this function.
274 We increment it during the consideration of our dependencies,
275 then decrement it again after finding out whether this file
276 is out of date.
278 If there are multiple double-colon entries for FILE,
279 each is considered in turn. */
281 static int
282 update_file (struct file *file, unsigned int depth)
284 register int status = 0;
285 register struct file *f;
287 f = file->double_colon ? file->double_colon : file;
289 /* Prune the dependency graph: if we've already been here on _this_
290 pass through the dependency graph, we don't have to go any further.
291 We won't reap_children until we start the next pass, so no state
292 change is possible below here until then. */
293 if (f->considered == considered)
295 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
296 return f->command_state == cs_finished ? f->update_status : 0;
299 /* This loop runs until we start commands for a double colon rule, or until
300 the chain is exhausted. */
301 for (; f != 0; f = f->prev)
303 f->considered = considered;
305 status |= update_file_1 (f, depth);
306 check_renamed (f);
308 /* If we got an error, don't bother with double_colon etc. */
309 if (status != 0 && !keep_going_flag)
310 return status;
312 if (f->command_state == cs_running
313 || f->command_state == cs_deps_running)
315 /* Don't run the other :: rules for this
316 file until this rule is finished. */
317 status = 0;
318 break;
322 /* Process the remaining rules in the double colon chain so they're marked
323 considered. Start their prerequisites, too. */
324 if (file->double_colon)
325 for (; f != 0 ; f = f->prev)
327 struct dep *d;
329 f->considered = considered;
331 for (d = f->deps; d != 0; d = d->next)
332 status |= update_file (d->file, depth + 1);
335 return status;
338 /* Show a message stating the target failed to build. */
340 static void
341 complain (const struct file *file)
343 const char *msg_noparent
344 = _("%sNo rule to make target `%s'%s");
345 const char *msg_parent
346 = _("%sNo rule to make target `%s', needed by `%s'%s");
348 if (!keep_going_flag)
350 if (file->parent == 0)
351 fatal (NILF, msg_noparent, "", file->name, "");
353 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
356 if (file->parent == 0)
357 error (NILF, msg_noparent, "*** ", file->name, ".");
358 else
359 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
362 /* Consider a single `struct file' and update it as appropriate. */
364 static int
365 update_file_1 (struct file *file, unsigned int depth)
367 register FILE_TIMESTAMP this_mtime;
368 int noexist, must_make, deps_changed;
369 int dep_status = 0;
370 register struct dep *d, *lastd;
371 int running = 0;
373 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
375 if (file->updated)
377 if (file->update_status > 0)
379 DBF (DB_VERBOSE,
380 _("Recently tried and failed to update file `%s'.\n"));
382 /* If the file we tried to make is marked dontcare then no message
383 was printed about it when it failed during the makefile rebuild.
384 If we're trying to build it again in the normal rebuild, print a
385 message now. */
386 if (file->dontcare && !rebuilding_makefiles)
388 file->dontcare = 0;
389 complain (file);
392 return file->update_status;
395 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
396 return 0;
399 switch (file->command_state)
401 case cs_not_started:
402 case cs_deps_running:
403 break;
404 case cs_running:
405 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
406 return 0;
407 case cs_finished:
408 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
409 return file->update_status;
410 default:
411 abort ();
414 ++depth;
416 /* Notice recursive update of the same file. */
417 start_updating (file);
419 /* Looking at the file's modtime beforehand allows the possibility
420 that its name may be changed by a VPATH search, and thus it may
421 not need an implicit rule. If this were not done, the file
422 might get implicit commands that apply to its initial name, only
423 to have that name replaced with another found by VPATH search. */
425 this_mtime = file_mtime (file);
426 check_renamed (file);
427 noexist = this_mtime == NONEXISTENT_MTIME;
428 if (noexist)
429 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
430 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
431 && file->low_resolution_time)
433 /* Avoid spurious rebuilds due to low resolution time stamps. */
434 int ns = FILE_TIMESTAMP_NS (this_mtime);
435 if (ns != 0)
436 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
437 file->name);
438 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
441 must_make = noexist;
443 /* If file was specified as a target with no commands,
444 come up with some default commands. */
446 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
448 if (try_implicit_rule (file, depth))
449 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
450 else
451 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
452 file->tried_implicit = 1;
454 if (file->cmds == 0 && !file->is_target
455 && default_file != 0 && default_file->cmds != 0)
457 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
458 file->cmds = default_file->cmds;
461 /* Update all non-intermediate files we depend on, if necessary,
462 and see whether any of them is more recent than this file. */
464 lastd = 0;
465 d = file->deps;
466 while (d != 0)
468 FILE_TIMESTAMP mtime;
469 int maybe_make;
470 int dontcare = 0;
472 check_renamed (d->file);
474 mtime = file_mtime (d->file);
475 check_renamed (d->file);
477 if (is_updating (d->file))
479 error (NILF, _("Circular %s <- %s dependency dropped."),
480 file->name, d->file->name);
481 /* We cannot free D here because our the caller will still have
482 a reference to it when we were called recursively via
483 check_dep below. */
484 if (lastd == 0)
485 file->deps = d->next;
486 else
487 lastd->next = d->next;
488 d = d->next;
489 continue;
492 d->file->parent = file;
493 maybe_make = must_make;
495 /* Inherit dontcare flag from our parent. */
496 if (rebuilding_makefiles)
498 dontcare = d->file->dontcare;
499 d->file->dontcare = file->dontcare;
503 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
505 /* Restore original dontcare flag. */
506 if (rebuilding_makefiles)
507 d->file->dontcare = dontcare;
509 if (! d->ignore_mtime)
510 must_make = maybe_make;
512 check_renamed (d->file);
515 register struct file *f = d->file;
516 if (f->double_colon)
517 f = f->double_colon;
520 running |= (f->command_state == cs_running
521 || f->command_state == cs_deps_running);
522 f = f->prev;
524 while (f != 0);
527 if (dep_status != 0 && !keep_going_flag)
528 break;
530 if (!running)
531 /* The prereq is considered changed if the timestamp has changed while
532 it was built, OR it doesn't exist. */
533 d->changed = ((file_mtime (d->file) != mtime)
534 || (mtime == NONEXISTENT_MTIME));
536 lastd = d;
537 d = d->next;
540 /* Now we know whether this target needs updating.
541 If it does, update all the intermediate files we depend on. */
543 if (must_make || always_make_flag)
545 for (d = file->deps; d != 0; d = d->next)
546 if (d->file->intermediate)
548 int dontcare = 0;
550 FILE_TIMESTAMP mtime = file_mtime (d->file);
551 check_renamed (d->file);
552 d->file->parent = file;
554 /* Inherit dontcare flag from our parent. */
555 if (rebuilding_makefiles)
557 dontcare = d->file->dontcare;
558 d->file->dontcare = file->dontcare;
562 dep_status |= update_file (d->file, depth);
564 /* Restore original dontcare flag. */
565 if (rebuilding_makefiles)
566 d->file->dontcare = dontcare;
568 check_renamed (d->file);
571 register struct file *f = d->file;
572 if (f->double_colon)
573 f = f->double_colon;
576 running |= (f->command_state == cs_running
577 || f->command_state == cs_deps_running);
578 f = f->prev;
580 while (f != 0);
583 if (dep_status != 0 && !keep_going_flag)
584 break;
586 if (!running)
587 d->changed = ((file->phony && file->cmds != 0)
588 || file_mtime (d->file) != mtime);
592 finish_updating (file);
594 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
596 if (running)
598 set_command_state (file, cs_deps_running);
599 --depth;
600 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
601 return 0;
604 /* If any dependency failed, give up now. */
606 if (dep_status != 0)
608 file->update_status = dep_status;
609 notice_finished_file (file);
611 --depth;
613 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
615 if (depth == 0 && keep_going_flag
616 && !just_print_flag && !question_flag)
617 error (NILF,
618 _("Target `%s' not remade because of errors."), file->name);
620 return dep_status;
623 if (file->command_state == cs_deps_running)
624 /* The commands for some deps were running on the last iteration, but
625 they have finished now. Reset the command_state to not_started to
626 simplify later bookkeeping. It is important that we do this only
627 when the prior state was cs_deps_running, because that prior state
628 was definitely propagated to FILE's also_make's by set_command_state
629 (called above), but in another state an also_make may have
630 independently changed to finished state, and we would confuse that
631 file's bookkeeping (updated, but not_started is bogus state). */
632 set_command_state (file, cs_not_started);
634 /* Now record which prerequisites are more
635 recent than this file, so we can define $?. */
637 deps_changed = 0;
638 for (d = file->deps; d != 0; d = d->next)
640 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
641 check_renamed (d->file);
643 if (! d->ignore_mtime)
645 #if 1
646 /* %%% In version 4, remove this code completely to
647 implement not remaking deps if their deps are newer
648 than their parents. */
649 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
650 /* We must remake if this dep does not
651 exist and is not intermediate. */
652 must_make = 1;
653 #endif
655 /* Set DEPS_CHANGED if this dep actually changed. */
656 deps_changed |= d->changed;
659 /* Set D->changed if either this dep actually changed,
660 or its dependent, FILE, is older or does not exist. */
661 d->changed |= noexist || d_mtime > this_mtime;
663 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
665 const char *fmt = 0;
667 if (d->ignore_mtime)
669 if (ISDB (DB_VERBOSE))
670 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
672 else if (d_mtime == NONEXISTENT_MTIME)
674 if (ISDB (DB_BASIC))
675 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
677 else if (d->changed)
679 if (ISDB (DB_BASIC))
680 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
682 else if (ISDB (DB_VERBOSE))
683 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
685 if (fmt)
687 print_spaces (depth);
688 printf (fmt, dep_name (d), file->name);
689 fflush (stdout);
694 /* Here depth returns to the value it had when we were called. */
695 depth--;
697 if (file->double_colon && file->deps == 0)
699 must_make = 1;
700 DBF (DB_BASIC,
701 _("Target `%s' is double-colon and has no prerequisites.\n"));
703 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
704 && !always_make_flag)
706 must_make = 0;
707 DBF (DB_VERBOSE,
708 _("No commands for `%s' and no prerequisites actually changed.\n"));
710 else if (!must_make && file->cmds != 0 && always_make_flag)
712 must_make = 1;
713 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
716 if (!must_make)
718 if (ISDB (DB_VERBOSE))
720 print_spaces (depth);
721 printf (_("No need to remake target `%s'"), file->name);
722 if (!streq (file->name, file->hname))
723 printf (_("; using VPATH name `%s'"), file->hname);
724 puts (".");
725 fflush (stdout);
728 notice_finished_file (file);
730 /* Since we don't need to remake the file, convert it to use the
731 VPATH filename if we found one. hfile will be either the
732 local name if no VPATH or the VPATH name if one was found. */
734 while (file)
736 file->name = file->hname;
737 file = file->prev;
740 return 0;
743 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
745 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
746 VPATH. */
747 if (!streq(file->name, file->hname))
749 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
750 file->ignore_vpath = 1;
753 /* Now, take appropriate actions to remake the file. */
754 remake_file (file);
756 if (file->command_state != cs_finished)
758 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
759 return 0;
762 switch (file->update_status)
764 case 2:
765 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
766 break;
767 case 0:
768 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
769 break;
770 case 1:
771 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
772 break;
773 default:
774 assert (file->update_status >= 0 && file->update_status <= 2);
775 break;
778 file->updated = 1;
779 return file->update_status;
782 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
783 files listed in its `also_make' member. Under -t, this function also
784 touches FILE.
786 On return, FILE->update_status will no longer be -1 if it was. */
788 void
789 notice_finished_file (struct file *file)
791 struct dep *d;
792 int ran = file->command_state == cs_running;
793 int touched = 0;
795 file->command_state = cs_finished;
796 file->updated = 1;
798 if (touch_flag
799 /* The update status will be:
800 -1 if this target was not remade;
801 0 if 0 or more commands (+ or ${MAKE}) were run and won;
802 1 if some commands were run and lost.
803 We touch the target if it has commands which either were not run
804 or won when they ran (i.e. status is 0). */
805 && file->update_status == 0)
807 if (file->cmds != 0 && file->cmds->any_recurse)
809 /* If all the command lines were recursive,
810 we don't want to do the touching. */
811 unsigned int i;
812 for (i = 0; i < file->cmds->ncommand_lines; ++i)
813 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
814 goto have_nonrecursing;
816 else
818 have_nonrecursing:
819 if (file->phony)
820 file->update_status = 0;
821 /* According to POSIX, -t doesn't affect targets with no cmds. */
822 else if (file->cmds != 0)
824 /* Should set file's modification date and do nothing else. */
825 file->update_status = touch_file (file);
827 /* Pretend we ran a real touch command, to suppress the
828 "`foo' is up to date" message. */
829 commands_started++;
831 /* Request for the timestamp to be updated (and distributed
832 to the double-colon entries). Simply setting ran=1 would
833 almost have done the trick, but messes up with the also_make
834 updating logic below. */
835 touched = 1;
840 if (file->mtime_before_update == UNKNOWN_MTIME)
841 file->mtime_before_update = file->last_mtime;
843 if ((ran && !file->phony) || touched)
845 int i = 0;
847 /* If -n, -t, or -q and all the commands are recursive, we ran them so
848 really check the target's mtime again. Otherwise, assume the target
849 would have been updated. */
851 if (question_flag || just_print_flag || touch_flag)
853 for (i = file->cmds->ncommand_lines; i > 0; --i)
854 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
855 break;
858 /* If there were no commands at all, it's always new. */
860 else if (file->is_target && file->cmds == 0)
861 i = 1;
863 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
866 if (file->double_colon)
868 /* If this is a double colon rule and it is the last one to be
869 updated, propagate the change of modification time to all the
870 double-colon entries for this file.
872 We do it on the last update because it is important to handle
873 individual entries as separate rules with separate timestamps
874 while they are treated as targets and then as one rule with the
875 unified timestamp when they are considered as a prerequisite
876 of some target. */
878 struct file *f;
879 FILE_TIMESTAMP max_mtime = file->last_mtime;
881 /* Check that all rules were updated and at the same time find
882 the max timestamp. We assume UNKNOWN_MTIME is newer then
883 any other value. */
884 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
885 if (max_mtime != UNKNOWN_MTIME
886 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
887 max_mtime = f->last_mtime;
889 if (f == 0)
890 for (f = file->double_colon; f != 0; f = f->prev)
891 f->last_mtime = max_mtime;
894 if (ran && file->update_status != -1)
895 /* We actually tried to update FILE, which has
896 updated its also_make's as well (if it worked).
897 If it didn't work, it wouldn't work again for them.
898 So mark them as updated with the same status. */
899 for (d = file->also_make; d != 0; d = d->next)
901 d->file->command_state = cs_finished;
902 d->file->updated = 1;
903 d->file->update_status = file->update_status;
905 if (ran && !d->file->phony)
906 /* Fetch the new modification time.
907 We do this instead of just invalidating the cached time
908 so that a vpath_search can happen. Otherwise, it would
909 never be done because the target is already updated. */
910 (void) f_mtime (d->file, 0);
912 else if (file->update_status == -1)
913 /* Nothing was done for FILE, but it needed nothing done.
914 So mark it now as "succeeded". */
915 file->update_status = 0;
918 /* Check whether another file (whose mtime is THIS_MTIME)
919 needs updating on account of a dependency which is file FILE.
920 If it does, store 1 in *MUST_MAKE_PTR.
921 In the process, update any non-intermediate files
922 that FILE depends on (including FILE itself).
923 Return nonzero if any updating failed. */
925 static int
926 check_dep (struct file *file, unsigned int depth,
927 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
929 struct dep *d;
930 int dep_status = 0;
932 ++depth;
933 start_updating (file);
935 if (file->phony || !file->intermediate)
937 /* If this is a non-intermediate file, update it and record
938 whether it is newer than THIS_MTIME. */
939 FILE_TIMESTAMP mtime;
940 dep_status = update_file (file, depth);
941 check_renamed (file);
942 mtime = file_mtime (file);
943 check_renamed (file);
944 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
945 *must_make_ptr = 1;
947 else
949 /* FILE is an intermediate file. */
950 FILE_TIMESTAMP mtime;
952 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
954 if (try_implicit_rule (file, depth))
955 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
956 else
957 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
958 file->tried_implicit = 1;
960 if (file->cmds == 0 && !file->is_target
961 && default_file != 0 && default_file->cmds != 0)
963 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
964 file->cmds = default_file->cmds;
967 /* If the intermediate file actually exists
968 and is newer, then we should remake from it. */
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;
974 /* Otherwise, update all non-intermediate files we depend on,
975 if necessary, and see whether any of them is more
976 recent than the file on whose behalf we are checking. */
977 else
979 struct dep *lastd;
981 lastd = 0;
982 d = file->deps;
983 while (d != 0)
985 int maybe_make;
987 if (is_updating (d->file))
989 error (NILF, _("Circular %s <- %s dependency dropped."),
990 file->name, d->file->name);
991 if (lastd == 0)
993 file->deps = d->next;
994 free ((char *) d);
995 d = file->deps;
997 else
999 lastd->next = d->next;
1000 free ((char *) d);
1001 d = lastd->next;
1003 continue;
1006 d->file->parent = file;
1007 maybe_make = *must_make_ptr;
1008 dep_status |= check_dep (d->file, depth, this_mtime,
1009 &maybe_make);
1010 if (! d->ignore_mtime)
1011 *must_make_ptr = maybe_make;
1012 check_renamed (d->file);
1013 if (dep_status != 0 && !keep_going_flag)
1014 break;
1016 if (d->file->command_state == cs_running
1017 || d->file->command_state == cs_deps_running)
1018 /* Record that some of FILE's deps are still being made.
1019 This tells the upper levels to wait on processing it until
1020 the commands are finished. */
1021 set_command_state (file, cs_deps_running);
1023 lastd = d;
1024 d = d->next;
1029 finish_updating (file);
1030 return dep_status;
1033 /* Touch FILE. Return zero if successful, one if not. */
1035 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1037 static int
1038 touch_file (struct file *file)
1040 if (!silent_flag)
1041 message (0, "touch %s", file->name);
1043 #ifndef NO_ARCHIVES
1044 if (ar_name (file->name))
1045 return ar_touch (file->name);
1046 else
1047 #endif
1049 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1051 if (fd < 0)
1052 TOUCH_ERROR ("touch: open: ");
1053 else
1055 struct stat statbuf;
1056 char buf;
1057 int e;
1059 EINTRLOOP (e, fstat (fd, &statbuf));
1060 if (e < 0)
1061 TOUCH_ERROR ("touch: fstat: ");
1062 /* Rewrite character 0 same as it already is. */
1063 if (read (fd, &buf, 1) < 0)
1064 TOUCH_ERROR ("touch: read: ");
1065 if (lseek (fd, 0L, 0) < 0L)
1066 TOUCH_ERROR ("touch: lseek: ");
1067 if (write (fd, &buf, 1) < 0)
1068 TOUCH_ERROR ("touch: write: ");
1069 /* If file length was 0, we just
1070 changed it, so change it back. */
1071 if (statbuf.st_size == 0)
1073 (void) close (fd);
1074 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1075 if (fd < 0)
1076 TOUCH_ERROR ("touch: open: ");
1078 (void) close (fd);
1082 return 0;
1085 /* Having checked and updated the dependencies of FILE,
1086 do whatever is appropriate to remake FILE itself.
1087 Return the status from executing FILE's commands. */
1089 static void
1090 remake_file (struct file *file)
1092 if (file->cmds == 0)
1094 if (file->phony)
1095 /* Phony target. Pretend it succeeded. */
1096 file->update_status = 0;
1097 else if (file->is_target)
1098 /* This is a nonexistent target file we cannot make.
1099 Pretend it was successfully remade. */
1100 file->update_status = 0;
1101 else
1103 /* This is a dependency file we cannot remake. Fail. */
1104 if (!rebuilding_makefiles || !file->dontcare)
1105 complain (file);
1106 file->update_status = 2;
1109 else
1111 chop_commands (file->cmds);
1113 /* The normal case: start some commands. */
1114 if (!touch_flag || file->cmds->any_recurse)
1116 execute_file_commands (file);
1117 return;
1120 /* This tells notice_finished_file it is ok to touch the file. */
1121 file->update_status = 0;
1124 /* This does the touching under -t. */
1125 notice_finished_file (file);
1128 /* Return the mtime of a file, given a `struct file'.
1129 Caches the time in the struct file to avoid excess stat calls.
1131 If the file is not found, and SEARCH is nonzero, VPATH searching and
1132 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1133 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1134 FILE. */
1136 FILE_TIMESTAMP
1137 f_mtime (struct file *file, int search)
1139 FILE_TIMESTAMP mtime;
1141 /* File's mtime is not known; must get it from the system. */
1143 #ifndef NO_ARCHIVES
1144 if (ar_name (file->name))
1146 /* This file is an archive-member reference. */
1148 char *arname, *memname;
1149 struct file *arfile;
1150 int arname_used = 0;
1151 time_t member_date;
1153 /* Find the archive's name. */
1154 ar_parse_name (file->name, &arname, &memname);
1156 /* Find the modification time of the archive itself.
1157 Also allow for its name to be changed via VPATH search. */
1158 arfile = lookup_file (arname);
1159 if (arfile == 0)
1161 arfile = enter_file (arname);
1162 arname_used = 1;
1164 mtime = f_mtime (arfile, search);
1165 check_renamed (arfile);
1166 if (search && strcmp (arfile->hname, arname))
1168 /* The archive's name has changed.
1169 Change the archive-member reference accordingly. */
1171 char *name;
1172 unsigned int arlen, memlen;
1174 if (!arname_used)
1176 free (arname);
1177 arname_used = 1;
1180 arname = arfile->hname;
1181 arlen = strlen (arname);
1182 memlen = strlen (memname);
1184 /* free (file->name); */
1186 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1187 bcopy (arname, name, arlen);
1188 name[arlen] = '(';
1189 bcopy (memname, name + arlen + 1, memlen);
1190 name[arlen + 1 + memlen] = ')';
1191 name[arlen + 1 + memlen + 1] = '\0';
1193 /* If the archive was found with GPATH, make the change permanent;
1194 otherwise defer it until later. */
1195 if (arfile->name == arfile->hname)
1196 rename_file (file, name);
1197 else
1198 rehash_file (file, name);
1199 check_renamed (file);
1202 if (!arname_used)
1203 free (arname);
1204 free (memname);
1206 file->low_resolution_time = 1;
1208 if (mtime == NONEXISTENT_MTIME)
1209 /* The archive doesn't exist, so its members don't exist either. */
1210 return NONEXISTENT_MTIME;
1212 member_date = ar_member_date (file->hname);
1213 mtime = (member_date == (time_t) -1
1214 ? NONEXISTENT_MTIME
1215 : file_timestamp_cons (file->hname, member_date, 0));
1217 else
1218 #endif
1220 mtime = name_mtime (file->name);
1222 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1224 /* If name_mtime failed, search VPATH. */
1225 char *name = file->name;
1226 if (vpath_search (&name, &mtime)
1227 /* Last resort, is it a library (-lxxx)? */
1228 || (name[0] == '-' && name[1] == 'l'
1229 && library_search (&name, &mtime)))
1231 if (mtime != UNKNOWN_MTIME)
1232 /* vpath_search and library_search store UNKNOWN_MTIME
1233 if they didn't need to do a stat call for their work. */
1234 file->last_mtime = mtime;
1236 /* If we found it in VPATH, see if it's in GPATH too; if so,
1237 change the name right now; if not, defer until after the
1238 dependencies are updated. */
1239 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1241 rename_file (file, name);
1242 check_renamed (file);
1243 return file_mtime (file);
1246 rehash_file (file, name);
1247 check_renamed (file);
1248 /* If the result of a vpath search is -o or -W, preserve it.
1249 Otherwise, find the mtime of the resulting file. */
1250 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1251 mtime = name_mtime (name);
1256 /* Files can have bogus timestamps that nothing newly made will be
1257 "newer" than. Updating their dependents could just result in loops.
1258 So notify the user of the anomaly with a warning.
1260 We only need to do this once, for now. */
1262 if (!clock_skew_detected
1263 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1264 && !file->updated)
1266 static FILE_TIMESTAMP adjusted_now;
1268 FILE_TIMESTAMP adjusted_mtime = mtime;
1270 #if defined(WINDOWS32) || defined(__MSDOS__)
1271 /* Experimentation has shown that FAT filesystems can set file times
1272 up to 3 seconds into the future! Play it safe. */
1274 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1276 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1277 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1278 adjusted_mtime -= adjustment;
1279 #elif defined(__EMX__)
1280 /* FAT filesystems round time to the nearest even second!
1281 Allow for any file (NTFS or FAT) to perhaps suffer from this
1282 brain damage. */
1283 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1284 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1285 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1286 : 0);
1287 #endif
1289 /* If the file's time appears to be in the future, update our
1290 concept of the present and try once more. */
1291 if (adjusted_now < adjusted_mtime)
1293 int resolution;
1294 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1295 adjusted_now = now + (resolution - 1);
1296 if (adjusted_now < adjusted_mtime)
1298 #ifdef NO_FLOAT
1299 error (NILF, _("Warning: File `%s' has modification time in the future"),
1300 file->name);
1301 #else
1302 double from_now =
1303 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1304 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1305 / 1e9));
1306 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1307 file->name, from_now);
1308 #endif
1309 clock_skew_detected = 1;
1314 /* Store the mtime into all the entries for this file. */
1315 if (file->double_colon)
1316 file = file->double_colon;
1320 /* If this file is not implicit but it is intermediate then it was
1321 made so by the .INTERMEDIATE target. If this file has never
1322 been built by us but was found now, it existed before make
1323 started. So, turn off the intermediate bit so make doesn't
1324 delete it, since it didn't create it. */
1325 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1326 && file->command_state == cs_not_started
1327 && !file->tried_implicit && file->intermediate)
1328 file->intermediate = 0;
1330 file->last_mtime = mtime;
1331 file = file->prev;
1333 while (file != 0);
1335 return mtime;
1339 /* Return the mtime of the file or archive-member reference NAME. */
1341 /* First, we check with stat(). If the file does not exist, then we return
1342 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1343 examine each indirection of the symlink and find the newest mtime.
1344 This causes one duplicate stat() when -L is being used, but the code is
1345 much cleaner. */
1347 static FILE_TIMESTAMP
1348 name_mtime (char *name)
1350 FILE_TIMESTAMP mtime;
1351 struct stat st;
1352 int e;
1354 EINTRLOOP (e, stat (name, &st));
1355 if (e == 0)
1356 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1357 else if (errno == ENOENT || errno == ENOTDIR)
1358 mtime = NONEXISTENT_MTIME;
1359 else
1361 perror_with_name ("stat: ", name);
1362 return NONEXISTENT_MTIME;
1365 /* If we get here we either found it, or it doesn't exist.
1366 If it doesn't exist see if we can use a symlink mtime instead. */
1368 #ifdef MAKE_SYMLINKS
1369 #ifndef S_ISLNK
1370 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1371 #endif
1372 if (check_symlink_flag)
1374 PATH_VAR (lpath);
1376 /* Check each symbolic link segment (if any). Find the latest mtime
1377 amongst all of them (and the target file of course).
1378 Note that we have already successfully dereferenced all the links
1379 above. So, if we run into any error trying to lstat(), or
1380 readlink(), or whatever, something bizarre-o happened. Just give up
1381 and use whatever mtime we've already computed at that point. */
1382 strcpy (lpath, name);
1383 while (1)
1385 FILE_TIMESTAMP ltime;
1386 PATH_VAR (lbuf);
1387 long llen;
1388 char *p;
1390 EINTRLOOP (e, lstat (lpath, &st));
1391 if (e)
1393 /* Just take what we have so far. */
1394 if (errno != ENOENT && errno != ENOTDIR)
1395 perror_with_name ("lstat: ", lpath);
1396 break;
1399 /* If this is not a symlink, we're done (we started with the real
1400 file's mtime so we don't need to test it again). */
1401 if (!S_ISLNK (st.st_mode))
1402 break;
1404 /* If this mtime is newer than what we had, keep the new one. */
1405 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1406 if (ltime > mtime)
1407 mtime = ltime;
1409 /* Set up to check the file pointed to by this link. */
1410 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1411 if (llen < 0)
1413 /* Eh? Just take what we have. */
1414 perror_with_name ("readlink: ", lpath);
1415 break;
1417 lbuf[llen] = '\0';
1419 /* If the target is fully-qualified or the source is just a
1420 filename, then the new path is the target. Otherwise it's the
1421 source directory plus the target. */
1422 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1423 strcpy (lpath, lbuf);
1424 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1425 /* Eh? Path too long! Again, just go with what we have. */
1426 break;
1427 else
1428 /* Create the next step in the symlink chain. */
1429 strcpy (p+1, lbuf);
1432 #endif
1434 return mtime;
1438 /* Search for a library file specified as -lLIBNAME, searching for a
1439 suitable library file in the system library directories and the VPATH
1440 directories. */
1442 static int
1443 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1445 static char *dirs[] =
1447 #ifndef _AMIGA
1448 "/lib",
1449 "/usr/lib",
1450 #endif
1451 #if defined(WINDOWS32) && !defined(LIBDIR)
1453 * This is completely up to the user at product install time. Just define
1454 * a placeholder.
1456 #define LIBDIR "."
1457 #endif
1458 LIBDIR, /* Defined by configuration. */
1462 static char *libpatterns = NULL;
1464 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1465 FILE_TIMESTAMP mtime;
1467 /* Loop variables for the libpatterns value. */
1468 char *p, *p2;
1469 unsigned int len;
1471 char *file, **dp;
1473 /* If we don't have libpatterns, get it. */
1474 if (!libpatterns)
1476 int save = warn_undefined_variables_flag;
1477 warn_undefined_variables_flag = 0;
1479 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1481 warn_undefined_variables_flag = save;
1484 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1485 p2 = libpatterns;
1486 while ((p = find_next_token (&p2, &len)) != 0)
1488 static char *buf = NULL;
1489 static unsigned int buflen = 0;
1490 static int libdir_maxlen = -1;
1491 char *libbuf = variable_expand ("");
1493 /* Expand the pattern using LIBNAME as a replacement. */
1495 char c = p[len];
1496 char *p3, *p4;
1498 p[len] = '\0';
1499 p3 = find_percent (p);
1500 if (!p3)
1502 /* Give a warning if there is no pattern, then remove the
1503 pattern so it's ignored next time. */
1504 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1505 for (; len; --len, ++p)
1506 *p = ' ';
1507 *p = c;
1508 continue;
1510 p4 = variable_buffer_output (libbuf, p, p3-p);
1511 p4 = variable_buffer_output (p4, libname, strlen (libname));
1512 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1513 p[len] = c;
1516 /* Look first for `libNAME.a' in the current directory. */
1517 mtime = name_mtime (libbuf);
1518 if (mtime != NONEXISTENT_MTIME)
1520 *lib = xstrdup (libbuf);
1521 if (mtime_ptr != 0)
1522 *mtime_ptr = mtime;
1523 return 1;
1526 /* Now try VPATH search on that. */
1528 file = libbuf;
1529 if (vpath_search (&file, mtime_ptr))
1531 *lib = file;
1532 return 1;
1535 /* Now try the standard set of directories. */
1537 if (!buflen)
1539 for (dp = dirs; *dp != 0; ++dp)
1541 int l = strlen (*dp);
1542 if (l > libdir_maxlen)
1543 libdir_maxlen = l;
1545 buflen = strlen (libbuf);
1546 buf = xmalloc(libdir_maxlen + buflen + 2);
1548 else if (buflen < strlen (libbuf))
1550 buflen = strlen (libbuf);
1551 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1554 for (dp = dirs; *dp != 0; ++dp)
1556 sprintf (buf, "%s/%s", *dp, libbuf);
1557 mtime = name_mtime (buf);
1558 if (mtime != NONEXISTENT_MTIME)
1560 *lib = xstrdup (buf);
1561 if (mtime_ptr != 0)
1562 *mtime_ptr = mtime;
1563 return 1;
1568 return 0;