Fix some bugs in variable pattern substitution (e.g. $(VAR:A=B)),
[make.git] / remake.c
blobb0c76dc0fdd06a29c86049a65701c1c6c72a7de1
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
3 2002 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
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "dep.h"
26 #include "variable.h"
27 #include "debug.h"
29 #include <assert.h>
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #else
34 #include <sys/file.h>
35 #endif
37 #ifdef VMS
38 #include <starlet.h>
39 #endif
40 #ifdef WINDOWS32
41 #include <io.h>
42 #endif
44 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
47 /* The test for circular dependencies is based on the 'updating' bit in
48 `struct file'. However, double colon targets have seperate `struct
49 file's; make sure we always use the base of the double colon chain. */
51 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 1)
53 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating = 0)
55 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
56 ->updating)
59 /* Incremented when a command is started (under -n, when one would be). */
60 unsigned int commands_started = 0;
62 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
63 static unsigned int considered;
65 static int update_file PARAMS ((struct file *file, unsigned int depth));
66 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
67 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
68 static int touch_file PARAMS ((struct file *file));
69 static void remake_file PARAMS ((struct file *file));
70 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
71 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
74 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
75 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
77 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
78 and -n should be disabled for them unless they were also command-line
79 targets, and we should only make one goal at a time and return as soon as
80 one goal whose `changed' member is nonzero is successfully made. */
82 int
83 update_goal_chain (struct dep *goals)
85 int t = touch_flag, q = question_flag, n = just_print_flag;
86 unsigned int j = job_slots;
87 int status = -1;
89 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
90 : file_mtime (file))
92 /* Duplicate the chain so we can remove things from it. */
94 goals = copy_dep_chain (goals);
97 /* Clear the `changed' flag of each goal in the chain.
98 We will use the flag below to notice when any commands
99 have actually been run for a target. When no commands
100 have been run, we give an "up to date" diagnostic. */
102 struct dep *g;
103 for (g = goals; g != 0; g = g->next)
104 g->changed = 0;
107 /* All files start with the considered bit 0, so the global value is 1. */
108 considered = 1;
110 /* Update all the goals until they are all finished. */
112 while (goals != 0)
114 register struct dep *g, *lastgoal;
116 /* Start jobs that are waiting for the load to go down. */
118 start_waiting_jobs ();
120 /* Wait for a child to die. */
122 reap_children (1, 0);
124 lastgoal = 0;
125 g = goals;
126 while (g != 0)
128 /* Iterate over all double-colon entries for this file. */
129 struct file *file;
130 int stop = 0, any_not_updated = 0;
132 for (file = g->file->double_colon ? g->file->double_colon : g->file;
133 file != NULL;
134 file = file->prev)
136 unsigned int ocommands_started;
137 int x;
138 check_renamed (file);
139 if (rebuilding_makefiles)
141 if (file->cmd_target)
143 touch_flag = t;
144 question_flag = q;
145 just_print_flag = n;
147 else
148 touch_flag = question_flag = just_print_flag = 0;
151 /* Save the old value of `commands_started' so we can compare
152 later. It will be incremented when any commands are
153 actually run. */
154 ocommands_started = commands_started;
156 x = update_file (file, rebuilding_makefiles ? 1 : 0);
157 check_renamed (file);
159 /* Set the goal's `changed' flag if any commands were started
160 by calling update_file above. We check this flag below to
161 decide when to give an "up to date" diagnostic. */
162 g->changed += commands_started - ocommands_started;
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 = (!keep_going_flag && !question_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 ((char *) 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;
268 return status;
271 /* If FILE is not up to date, execute the commands for it.
272 Return 0 if successful, 1 if unsuccessful;
273 but with some flag settings, just call `exit' if unsuccessful.
275 DEPTH is the depth in recursions of this function.
276 We increment it during the consideration of our dependencies,
277 then decrement it again after finding out whether this file
278 is out of date.
280 If there are multiple double-colon entries for FILE,
281 each is considered in turn. */
283 static int
284 update_file (struct file *file, unsigned int depth)
286 register int status = 0;
287 register struct file *f;
289 f = file->double_colon ? file->double_colon : file;
291 /* Prune the dependency graph: if we've already been here on _this_
292 pass through the dependency graph, we don't have to go any further.
293 We won't reap_children until we start the next pass, so no state
294 change is possible below here until then. */
295 if (f->considered == considered)
297 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
298 return f->command_state == cs_finished ? f->update_status : 0;
301 /* This loop runs until we start commands for a double colon rule, or until
302 the chain is exhausted. */
303 for (; f != 0; f = f->prev)
305 f->considered = considered;
307 status |= update_file_1 (f, depth);
308 check_renamed (f);
310 if (status != 0 && !keep_going_flag)
311 break;
313 if (f->command_state == cs_running
314 || f->command_state == cs_deps_running)
316 /* Don't run the other :: rules for this
317 file until this rule is finished. */
318 status = 0;
319 break;
323 /* Process the remaining rules in the double colon chain so they're marked
324 considered. Start their prerequisites, too. */
325 if (file->double_colon)
326 for (; f != 0 ; f = f->prev)
328 struct dep *d;
330 f->considered = considered;
332 for (d = f->deps; d != 0; d = d->next)
333 status |= update_file (d->file, depth + 1);
336 return status;
339 /* Show a message stating the target failed to build. */
341 static void
342 complain (const struct file *file)
344 const char *msg_noparent
345 = _("%sNo rule to make target `%s'%s");
346 const char *msg_parent
347 = _("%sNo rule to make target `%s', needed by `%s'%s");
349 if (!keep_going_flag)
351 if (file->parent == 0)
352 fatal (NILF, msg_noparent, "", file->name, "");
354 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
357 if (file->parent == 0)
358 error (NILF, msg_noparent, "*** ", file->name, ".");
359 else
360 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
363 /* Consider a single `struct file' and update it as appropriate. */
365 static int
366 update_file_1 (struct file *file, unsigned int depth)
368 register FILE_TIMESTAMP this_mtime;
369 int noexist, must_make, deps_changed;
370 int dep_status = 0;
371 register struct dep *d, *lastd;
372 int running = 0;
374 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
376 if (file->updated)
378 if (file->update_status > 0)
380 DBF (DB_VERBOSE,
381 _("Recently tried and failed to update file `%s'.\n"));
383 /* If the file we tried to make is marked dontcare then no message
384 was printed about it when it failed during the makefile rebuild.
385 If we're trying to build it again in the normal rebuild, print a
386 message now. */
387 if (file->dontcare && !rebuilding_makefiles)
389 file->dontcare = 0;
390 complain (file);
393 return file->update_status;
396 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
397 return 0;
400 switch (file->command_state)
402 case cs_not_started:
403 case cs_deps_running:
404 break;
405 case cs_running:
406 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
407 return 0;
408 case cs_finished:
409 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
410 return file->update_status;
411 default:
412 abort ();
415 ++depth;
417 /* Notice recursive update of the same file. */
418 start_updating (file);
420 /* Looking at the file's modtime beforehand allows the possibility
421 that its name may be changed by a VPATH search, and thus it may
422 not need an implicit rule. If this were not done, the file
423 might get implicit commands that apply to its initial name, only
424 to have that name replaced with another found by VPATH search. */
426 this_mtime = file_mtime (file);
427 check_renamed (file);
428 noexist = this_mtime == NONEXISTENT_MTIME;
429 if (noexist)
430 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
431 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
432 && file->low_resolution_time)
434 /* Avoid spurious rebuilds due to low resolution time stamps. */
435 int ns = FILE_TIMESTAMP_NS (this_mtime);
436 if (ns != 0)
437 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
438 file->name);
439 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
442 must_make = noexist;
444 /* If file was specified as a target with no commands,
445 come up with some default commands. */
447 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
449 if (try_implicit_rule (file, depth))
450 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
451 else
452 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
453 file->tried_implicit = 1;
455 if (file->cmds == 0 && !file->is_target
456 && default_file != 0 && default_file->cmds != 0)
458 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
459 file->cmds = default_file->cmds;
462 /* Update all non-intermediate files we depend on, if necessary,
463 and see whether any of them is more recent than this file. */
465 lastd = 0;
466 d = file->deps;
467 while (d != 0)
469 FILE_TIMESTAMP mtime;
470 int maybe_make;
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;
494 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
495 if (! d->ignore_mtime)
496 must_make = maybe_make;
498 check_renamed (d->file);
501 register struct file *f = d->file;
502 if (f->double_colon)
503 f = f->double_colon;
506 running |= (f->command_state == cs_running
507 || f->command_state == cs_deps_running);
508 f = f->prev;
510 while (f != 0);
513 if (dep_status != 0 && !keep_going_flag)
514 break;
516 if (!running)
517 d->changed = file_mtime (d->file) != mtime;
519 lastd = d;
520 d = d->next;
523 /* Now we know whether this target needs updating.
524 If it does, update all the intermediate files we depend on. */
526 if (must_make || always_make_flag)
528 for (d = file->deps; d != 0; d = d->next)
529 if (d->file->intermediate)
531 FILE_TIMESTAMP mtime = file_mtime (d->file);
532 check_renamed (d->file);
533 d->file->parent = file;
534 dep_status |= update_file (d->file, depth);
535 check_renamed (d->file);
538 register struct file *f = d->file;
539 if (f->double_colon)
540 f = f->double_colon;
543 running |= (f->command_state == cs_running
544 || f->command_state == cs_deps_running);
545 f = f->prev;
547 while (f != 0);
550 if (dep_status != 0 && !keep_going_flag)
551 break;
553 if (!running)
554 d->changed = ((file->phony && file->cmds != 0)
555 || file_mtime (d->file) != mtime);
559 finish_updating (file);
561 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
563 if (running)
565 set_command_state (file, cs_deps_running);
566 --depth;
567 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
568 return 0;
571 /* If any dependency failed, give up now. */
573 if (dep_status != 0)
575 file->update_status = dep_status;
576 notice_finished_file (file);
578 --depth;
580 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
582 if (depth == 0 && keep_going_flag
583 && !just_print_flag && !question_flag)
584 error (NILF,
585 _("Target `%s' not remade because of errors."), file->name);
587 return dep_status;
590 if (file->command_state == cs_deps_running)
591 /* The commands for some deps were running on the last iteration, but
592 they have finished now. Reset the command_state to not_started to
593 simplify later bookkeeping. It is important that we do this only
594 when the prior state was cs_deps_running, because that prior state
595 was definitely propagated to FILE's also_make's by set_command_state
596 (called above), but in another state an also_make may have
597 independently changed to finished state, and we would confuse that
598 file's bookkeeping (updated, but not_started is bogus state). */
599 set_command_state (file, cs_not_started);
601 /* Now record which prerequisites are more
602 recent than this file, so we can define $?. */
604 deps_changed = 0;
605 for (d = file->deps; d != 0; d = d->next)
607 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
608 check_renamed (d->file);
610 if (! d->ignore_mtime)
612 #if 1
613 /* %%% In version 4, remove this code completely to
614 implement not remaking deps if their deps are newer
615 than their parents. */
616 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
617 /* We must remake if this dep does not
618 exist and is not intermediate. */
619 must_make = 1;
620 #endif
622 /* Set DEPS_CHANGED if this dep actually changed. */
623 deps_changed |= d->changed;
626 /* Set D->changed if either this dep actually changed,
627 or its dependent, FILE, is older or does not exist. */
628 d->changed |= noexist || d_mtime > this_mtime;
630 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
632 const char *fmt = 0;
634 if (d->ignore_mtime)
636 if (ISDB (DB_VERBOSE))
637 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
639 else if (d_mtime == NONEXISTENT_MTIME)
641 if (ISDB (DB_BASIC))
642 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
644 else if (d->changed)
646 if (ISDB (DB_BASIC))
647 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
649 else if (ISDB (DB_VERBOSE))
650 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
652 if (fmt)
654 print_spaces (depth);
655 printf (fmt, dep_name (d), file->name);
656 fflush (stdout);
661 /* Here depth returns to the value it had when we were called. */
662 depth--;
664 if (file->double_colon && file->deps == 0)
666 must_make = 1;
667 DBF (DB_BASIC,
668 _("Target `%s' is double-colon and has no prerequisites.\n"));
670 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
671 && !always_make_flag)
673 must_make = 0;
674 DBF (DB_VERBOSE,
675 _("No commands for `%s' and no prerequisites actually changed.\n"));
677 else if (!must_make && file->cmds != 0 && always_make_flag)
679 must_make = 1;
680 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
683 if (!must_make)
685 if (ISDB (DB_VERBOSE))
687 print_spaces (depth);
688 printf (_("No need to remake target `%s'"), file->name);
689 if (!streq (file->name, file->hname))
690 printf (_("; using VPATH name `%s'"), file->hname);
691 puts (".");
692 fflush (stdout);
695 notice_finished_file (file);
697 /* Since we don't need to remake the file, convert it to use the
698 VPATH filename if we found one. hfile will be either the
699 local name if no VPATH or the VPATH name if one was found. */
701 while (file)
703 file->name = file->hname;
704 file = file->prev;
707 return 0;
710 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
712 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
713 VPATH. */
714 if (!streq(file->name, file->hname))
716 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
717 file->ignore_vpath = 1;
720 /* Now, take appropriate actions to remake the file. */
721 remake_file (file);
723 if (file->command_state != cs_finished)
725 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
726 return 0;
729 switch (file->update_status)
731 case 2:
732 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
733 break;
734 case 0:
735 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
736 break;
737 case 1:
738 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
739 break;
740 default:
741 assert (file->update_status >= 0 && file->update_status <= 2);
742 break;
745 file->updated = 1;
746 return file->update_status;
749 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
750 files listed in its `also_make' member. Under -t, this function also
751 touches FILE.
753 On return, FILE->update_status will no longer be -1 if it was. */
755 void
756 notice_finished_file (struct file *file)
758 struct dep *d;
759 int ran = file->command_state == cs_running;
760 int touched = 0;
762 file->command_state = cs_finished;
763 file->updated = 1;
765 if (touch_flag
766 /* The update status will be:
767 -1 if this target was not remade;
768 0 if 0 or more commands (+ or ${MAKE}) were run and won;
769 1 if some commands were run and lost.
770 We touch the target if it has commands which either were not run
771 or won when they ran (i.e. status is 0). */
772 && file->update_status == 0)
774 if (file->cmds != 0 && file->cmds->any_recurse)
776 /* If all the command lines were recursive,
777 we don't want to do the touching. */
778 unsigned int i;
779 for (i = 0; i < file->cmds->ncommand_lines; ++i)
780 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
781 goto have_nonrecursing;
783 else
785 have_nonrecursing:
786 if (file->phony)
787 file->update_status = 0;
788 /* According to POSIX, -t doesn't affect targets with no cmds. */
789 else if (file->cmds != 0)
791 /* Should set file's modification date and do nothing else. */
792 file->update_status = touch_file (file);
794 /* Pretend we ran a real touch command, to suppress the
795 "`foo' is up to date" message. */
796 commands_started++;
798 /* Request for the timestamp to be updated (and distributed
799 to the double-colon entries). Simply setting ran=1 would
800 almost have done the trick, but messes up with the also_make
801 updating logic below. */
802 touched = 1;
807 if (file->mtime_before_update == UNKNOWN_MTIME)
808 file->mtime_before_update = file->last_mtime;
810 if ((ran && !file->phony) || touched)
812 struct file *f;
813 int i = 0;
815 /* If -n, -t, or -q and all the commands are recursive, we ran them so
816 really check the target's mtime again. Otherwise, assume the target
817 would have been updated. */
819 if (question_flag || just_print_flag || touch_flag)
821 for (i = file->cmds->ncommand_lines; i > 0; --i)
822 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
823 break;
826 /* If there were no commands at all, it's always new. */
828 else if (file->is_target && file->cmds == 0)
829 i = 1;
831 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
833 /* Propagate the change of modification time to all the double-colon
834 entries for this file. */
835 for (f = file->double_colon; f != 0; f = f->prev)
836 f->last_mtime = file->last_mtime;
839 if (ran && file->update_status != -1)
840 /* We actually tried to update FILE, which has
841 updated its also_make's as well (if it worked).
842 If it didn't work, it wouldn't work again for them.
843 So mark them as updated with the same status. */
844 for (d = file->also_make; d != 0; d = d->next)
846 d->file->command_state = cs_finished;
847 d->file->updated = 1;
848 d->file->update_status = file->update_status;
850 if (ran && !d->file->phony)
851 /* Fetch the new modification time.
852 We do this instead of just invalidating the cached time
853 so that a vpath_search can happen. Otherwise, it would
854 never be done because the target is already updated. */
855 (void) f_mtime (d->file, 0);
857 else if (file->update_status == -1)
858 /* Nothing was done for FILE, but it needed nothing done.
859 So mark it now as "succeeded". */
860 file->update_status = 0;
863 /* Check whether another file (whose mtime is THIS_MTIME)
864 needs updating on account of a dependency which is file FILE.
865 If it does, store 1 in *MUST_MAKE_PTR.
866 In the process, update any non-intermediate files
867 that FILE depends on (including FILE itself).
868 Return nonzero if any updating failed. */
870 static int
871 check_dep (struct file *file, unsigned int depth,
872 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
874 struct dep *d;
875 int dep_status = 0;
877 ++depth;
878 start_updating (file);
880 if (!file->intermediate)
881 /* If this is a non-intermediate file, update it and record
882 whether it is newer than THIS_MTIME. */
884 FILE_TIMESTAMP mtime;
885 dep_status = update_file (file, depth);
886 check_renamed (file);
887 mtime = file_mtime (file);
888 check_renamed (file);
889 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
890 *must_make_ptr = 1;
892 else
894 /* FILE is an intermediate file. */
895 FILE_TIMESTAMP mtime;
897 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
899 if (try_implicit_rule (file, depth))
900 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
901 else
902 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
903 file->tried_implicit = 1;
905 if (file->cmds == 0 && !file->is_target
906 && default_file != 0 && default_file->cmds != 0)
908 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
909 file->cmds = default_file->cmds;
912 /* If the intermediate file actually exists
913 and is newer, then we should remake from it. */
914 check_renamed (file);
915 mtime = file_mtime (file);
916 check_renamed (file);
917 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
918 *must_make_ptr = 1;
919 /* Otherwise, update all non-intermediate files we depend on,
920 if necessary, and see whether any of them is more
921 recent than the file on whose behalf we are checking. */
922 else
924 struct dep *lastd;
926 lastd = 0;
927 d = file->deps;
928 while (d != 0)
930 int maybe_make;
932 if (is_updating (d->file))
934 error (NILF, _("Circular %s <- %s dependency dropped."),
935 file->name, d->file->name);
936 if (lastd == 0)
938 file->deps = d->next;
939 free ((char *) d);
940 d = file->deps;
942 else
944 lastd->next = d->next;
945 free ((char *) d);
946 d = lastd->next;
948 continue;
951 d->file->parent = file;
952 maybe_make = *must_make_ptr;
953 dep_status |= check_dep (d->file, depth, this_mtime,
954 &maybe_make);
955 if (! d->ignore_mtime)
956 *must_make_ptr = maybe_make;
957 check_renamed (d->file);
958 if (dep_status != 0 && !keep_going_flag)
959 break;
961 if (d->file->command_state == cs_running
962 || d->file->command_state == cs_deps_running)
963 /* Record that some of FILE's deps are still being made.
964 This tells the upper levels to wait on processing it until
965 the commands are finished. */
966 set_command_state (file, cs_deps_running);
968 lastd = d;
969 d = d->next;
974 finish_updating (file);
975 return dep_status;
978 /* Touch FILE. Return zero if successful, one if not. */
980 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
982 static int
983 touch_file (struct file *file)
985 if (!silent_flag)
986 message (0, "touch %s", file->name);
988 #ifndef NO_ARCHIVES
989 if (ar_name (file->name))
990 return ar_touch (file->name);
991 else
992 #endif
994 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
996 if (fd < 0)
997 TOUCH_ERROR ("touch: open: ");
998 else
1000 struct stat statbuf;
1001 char buf;
1002 int e;
1004 EINTRLOOP (e, fstat (fd, &statbuf));
1005 if (e < 0)
1006 TOUCH_ERROR ("touch: fstat: ");
1007 /* Rewrite character 0 same as it already is. */
1008 if (read (fd, &buf, 1) < 0)
1009 TOUCH_ERROR ("touch: read: ");
1010 if (lseek (fd, 0L, 0) < 0L)
1011 TOUCH_ERROR ("touch: lseek: ");
1012 if (write (fd, &buf, 1) < 0)
1013 TOUCH_ERROR ("touch: write: ");
1014 /* If file length was 0, we just
1015 changed it, so change it back. */
1016 if (statbuf.st_size == 0)
1018 (void) close (fd);
1019 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1020 if (fd < 0)
1021 TOUCH_ERROR ("touch: open: ");
1023 (void) close (fd);
1027 return 0;
1030 /* Having checked and updated the dependencies of FILE,
1031 do whatever is appropriate to remake FILE itself.
1032 Return the status from executing FILE's commands. */
1034 static void
1035 remake_file (struct file *file)
1037 if (file->cmds == 0)
1039 if (file->phony)
1040 /* Phony target. Pretend it succeeded. */
1041 file->update_status = 0;
1042 else if (file->is_target)
1043 /* This is a nonexistent target file we cannot make.
1044 Pretend it was successfully remade. */
1045 file->update_status = 0;
1046 else
1048 /* This is a dependency file we cannot remake. Fail. */
1049 if (!rebuilding_makefiles || !file->dontcare)
1050 complain (file);
1051 file->update_status = 2;
1054 else
1056 chop_commands (file->cmds);
1058 /* The normal case: start some commands. */
1059 if (!touch_flag || file->cmds->any_recurse)
1061 execute_file_commands (file);
1062 return;
1065 /* This tells notice_finished_file it is ok to touch the file. */
1066 file->update_status = 0;
1069 /* This does the touching under -t. */
1070 notice_finished_file (file);
1073 /* Return the mtime of a file, given a `struct file'.
1074 Caches the time in the struct file to avoid excess stat calls.
1076 If the file is not found, and SEARCH is nonzero, VPATH searching and
1077 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1078 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1079 FILE. */
1081 FILE_TIMESTAMP
1082 f_mtime (struct file *file, int search)
1084 FILE_TIMESTAMP mtime;
1086 /* File's mtime is not known; must get it from the system. */
1088 #ifndef NO_ARCHIVES
1089 if (ar_name (file->name))
1091 /* This file is an archive-member reference. */
1093 char *arname, *memname;
1094 struct file *arfile;
1095 int arname_used = 0;
1096 time_t member_date;
1098 /* Find the archive's name. */
1099 ar_parse_name (file->name, &arname, &memname);
1101 /* Find the modification time of the archive itself.
1102 Also allow for its name to be changed via VPATH search. */
1103 arfile = lookup_file (arname);
1104 if (arfile == 0)
1106 arfile = enter_file (arname);
1107 arname_used = 1;
1109 mtime = f_mtime (arfile, search);
1110 check_renamed (arfile);
1111 if (search && strcmp (arfile->hname, arname))
1113 /* The archive's name has changed.
1114 Change the archive-member reference accordingly. */
1116 char *name;
1117 unsigned int arlen, memlen;
1119 if (!arname_used)
1121 free (arname);
1122 arname_used = 1;
1125 arname = arfile->hname;
1126 arlen = strlen (arname);
1127 memlen = strlen (memname);
1129 /* free (file->name); */
1131 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1132 bcopy (arname, name, arlen);
1133 name[arlen] = '(';
1134 bcopy (memname, name + arlen + 1, memlen);
1135 name[arlen + 1 + memlen] = ')';
1136 name[arlen + 1 + memlen + 1] = '\0';
1138 /* If the archive was found with GPATH, make the change permanent;
1139 otherwise defer it until later. */
1140 if (arfile->name == arfile->hname)
1141 rename_file (file, name);
1142 else
1143 rehash_file (file, name);
1144 check_renamed (file);
1147 if (!arname_used)
1148 free (arname);
1149 free (memname);
1151 file->low_resolution_time = 1;
1153 if (mtime == NONEXISTENT_MTIME)
1154 /* The archive doesn't exist, so its members don't exist either. */
1155 return NONEXISTENT_MTIME;
1157 member_date = ar_member_date (file->hname);
1158 mtime = (member_date == (time_t) -1
1159 ? NONEXISTENT_MTIME
1160 : file_timestamp_cons (file->hname, member_date, 0));
1162 else
1163 #endif
1165 mtime = name_mtime (file->name);
1167 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1169 /* If name_mtime failed, search VPATH. */
1170 char *name = file->name;
1171 if (vpath_search (&name, &mtime)
1172 /* Last resort, is it a library (-lxxx)? */
1173 || (name[0] == '-' && name[1] == 'l'
1174 && library_search (&name, &mtime)))
1176 if (mtime != UNKNOWN_MTIME)
1177 /* vpath_search and library_search store UNKNOWN_MTIME
1178 if they didn't need to do a stat call for their work. */
1179 file->last_mtime = mtime;
1181 /* If we found it in VPATH, see if it's in GPATH too; if so,
1182 change the name right now; if not, defer until after the
1183 dependencies are updated. */
1184 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1186 rename_file (file, name);
1187 check_renamed (file);
1188 return file_mtime (file);
1191 rehash_file (file, name);
1192 check_renamed (file);
1193 mtime = name_mtime (name);
1199 /* Files can have bogus timestamps that nothing newly made will be
1200 "newer" than. Updating their dependents could just result in loops.
1201 So notify the user of the anomaly with a warning.
1203 We only need to do this once, for now. */
1205 if (!clock_skew_detected
1206 && mtime != NONEXISTENT_MTIME
1207 && !file->updated)
1209 static FILE_TIMESTAMP adjusted_now;
1211 FILE_TIMESTAMP adjusted_mtime = mtime;
1213 #if defined(WINDOWS32) || defined(__MSDOS__)
1214 /* Experimentation has shown that FAT filesystems can set file times
1215 up to 3 seconds into the future! Play it safe. */
1217 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1219 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1220 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1221 adjusted_mtime -= adjustment;
1222 #elif defined(__EMX__)
1223 /* FAT filesystems round time to the nearest even second!
1224 Allow for any file (NTFS or FAT) to perhaps suffer from this
1225 brain damage. */
1226 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1227 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1228 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1229 : 0);
1230 #endif
1232 /* If the file's time appears to be in the future, update our
1233 concept of the present and try once more. */
1234 if (adjusted_now < adjusted_mtime)
1236 int resolution;
1237 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1238 adjusted_now = now + (resolution - 1);
1239 if (adjusted_now < adjusted_mtime)
1241 #ifdef NO_FLOAT
1242 error (NILF, _("Warning: File `%s' has modification time in the future"),
1243 file->name);
1244 #else
1245 double from_now =
1246 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1247 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1248 / 1e9));
1249 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1250 file->name, from_now);
1251 #endif
1252 clock_skew_detected = 1;
1258 /* Store the mtime into all the entries for this file. */
1259 if (file->double_colon)
1260 file = file->double_colon;
1264 /* If this file is not implicit but it is intermediate then it was
1265 made so by the .INTERMEDIATE target. If this file has never
1266 been built by us but was found now, it existed before make
1267 started. So, turn off the intermediate bit so make doesn't
1268 delete it, since it didn't create it. */
1269 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1270 && file->command_state == cs_not_started
1271 && !file->tried_implicit && file->intermediate)
1272 file->intermediate = 0;
1274 file->last_mtime = mtime;
1275 file = file->prev;
1277 while (file != 0);
1279 return mtime;
1283 /* Return the mtime of the file or archive-member reference NAME. */
1285 static FILE_TIMESTAMP
1286 name_mtime (char *name)
1288 struct stat st;
1289 int e;
1291 EINTRLOOP (e, stat (name, &st));
1292 if (e != 0)
1294 if (errno != ENOENT && errno != ENOTDIR)
1295 perror_with_name ("stat:", name);
1296 return NONEXISTENT_MTIME;
1299 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1303 /* Search for a library file specified as -lLIBNAME, searching for a
1304 suitable library file in the system library directories and the VPATH
1305 directories. */
1307 static int
1308 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1310 static char *dirs[] =
1312 #ifndef _AMIGA
1313 "/lib",
1314 "/usr/lib",
1315 #endif
1316 #if defined(WINDOWS32) && !defined(LIBDIR)
1318 * This is completely up to the user at product install time. Just define
1319 * a placeholder.
1321 #define LIBDIR "."
1322 #endif
1323 LIBDIR, /* Defined by configuration. */
1327 static char *libpatterns = NULL;
1329 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1330 FILE_TIMESTAMP mtime;
1332 /* Loop variables for the libpatterns value. */
1333 char *p, *p2;
1334 unsigned int len;
1336 char *file, **dp;
1338 /* If we don't have libpatterns, get it. */
1339 if (!libpatterns)
1341 int save = warn_undefined_variables_flag;
1342 warn_undefined_variables_flag = 0;
1344 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1346 warn_undefined_variables_flag = save;
1349 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1350 p2 = libpatterns;
1351 while ((p = find_next_token (&p2, &len)) != 0)
1353 static char *buf = NULL;
1354 static unsigned int buflen = 0;
1355 static int libdir_maxlen = -1;
1356 char *libbuf = variable_expand ("");
1358 /* Expand the pattern using LIBNAME as a replacement. */
1360 char c = p[len];
1361 char *p3, *p4;
1363 p[len] = '\0';
1364 p3 = find_percent (p);
1365 if (!p3)
1367 /* Give a warning if there is no pattern, then remove the
1368 pattern so it's ignored next time. */
1369 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1370 for (; len; --len, ++p)
1371 *p = ' ';
1372 *p = c;
1373 continue;
1375 p4 = variable_buffer_output (libbuf, p, p3-p);
1376 p4 = variable_buffer_output (p4, libname, strlen (libname));
1377 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1378 p[len] = c;
1381 /* Look first for `libNAME.a' in the current directory. */
1382 mtime = name_mtime (libbuf);
1383 if (mtime != NONEXISTENT_MTIME)
1385 *lib = xstrdup (libbuf);
1386 if (mtime_ptr != 0)
1387 *mtime_ptr = mtime;
1388 return 1;
1391 /* Now try VPATH search on that. */
1393 file = libbuf;
1394 if (vpath_search (&file, mtime_ptr))
1396 *lib = file;
1397 return 1;
1400 /* Now try the standard set of directories. */
1402 if (!buflen)
1404 for (dp = dirs; *dp != 0; ++dp)
1406 int l = strlen (*dp);
1407 if (l > libdir_maxlen)
1408 libdir_maxlen = l;
1410 buflen = strlen (libbuf);
1411 buf = xmalloc(libdir_maxlen + buflen + 2);
1413 else if (buflen < strlen (libbuf))
1415 buflen = strlen (libbuf);
1416 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1419 for (dp = dirs; *dp != 0; ++dp)
1421 sprintf (buf, "%s/%s", *dp, libbuf);
1422 mtime = name_mtime (buf);
1423 if (mtime != NONEXISTENT_MTIME)
1425 *lib = xstrdup (buf);
1426 if (mtime_ptr != 0)
1427 *mtime_ptr = mtime;
1428 return 1;
1433 return 0;