Fix a bug where a variable could be used without being initialized in W32.
[make.git] / remake.c
blob184e9101fbb916245bd9603ff5decc554cc47fec
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 if (commands_started > ocommands_started)
163 g->changed = 1;
165 /* If we updated a file and STATUS was not already 1, set it to
166 1 if updating failed, or to 0 if updating succeeded. Leave
167 STATUS as it is if no updating was done. */
169 stop = 0;
170 if ((x != 0 || file->updated) && status < 1)
172 if (file->update_status != 0)
174 /* Updating failed, or -q triggered. The STATUS value
175 tells our caller which. */
176 status = file->update_status;
177 /* If -q just triggered, stop immediately. It doesn't
178 matter how much more we run, since we already know
179 the answer to return. */
180 stop = (question_flag && !keep_going_flag
181 && !rebuilding_makefiles);
183 else
185 FILE_TIMESTAMP mtime = MTIME (file);
186 check_renamed (file);
188 if (file->updated && g->changed &&
189 mtime != file->mtime_before_update)
191 /* Updating was done. If this is a makefile and
192 just_print_flag or question_flag is set (meaning
193 -n or -q was given and this file was specified
194 as a command-line target), don't change STATUS.
195 If STATUS is changed, we will get re-exec'd, and
196 enter an infinite loop. */
197 if (!rebuilding_makefiles
198 || (!just_print_flag && !question_flag))
199 status = 0;
200 if (rebuilding_makefiles && file->dontcare)
201 /* This is a default makefile; stop remaking. */
202 stop = 1;
207 /* Keep track if any double-colon entry is not finished.
208 When they are all finished, the goal is finished. */
209 any_not_updated |= !file->updated;
211 if (stop)
212 break;
215 /* Reset FILE since it is null at the end of the loop. */
216 file = g->file;
218 if (stop || !any_not_updated)
220 /* If we have found nothing whatever to do for the goal,
221 print a message saying nothing needs doing. */
223 if (!rebuilding_makefiles
224 /* If the update_status is zero, we updated successfully
225 or not at all. G->changed will have been set above if
226 any commands were actually started for this goal. */
227 && file->update_status == 0 && !g->changed
228 /* Never give a message under -s or -q. */
229 && !silent_flag && !question_flag)
230 message (1, ((file->phony || file->cmds == 0)
231 ? _("Nothing to be done for `%s'.")
232 : _("`%s' is up to date.")),
233 file->name);
235 /* This goal is finished. Remove it from the chain. */
236 if (lastgoal == 0)
237 goals = g->next;
238 else
239 lastgoal->next = g->next;
241 /* Free the storage. */
242 free ((char *) g);
244 g = lastgoal == 0 ? goals : lastgoal->next;
246 if (stop)
247 break;
249 else
251 lastgoal = g;
252 g = g->next;
256 /* If we reached the end of the dependency graph toggle the considered
257 flag for the next pass. */
258 if (g == 0)
259 considered = !considered;
262 if (rebuilding_makefiles)
264 touch_flag = t;
265 question_flag = q;
266 just_print_flag = n;
267 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 /* If we got an error, don't bother with double_colon etc. */
312 if (status != 0 && !keep_going_flag)
313 return status;
315 if (f->command_state == cs_running
316 || f->command_state == cs_deps_running)
318 /* Don't run the other :: rules for this
319 file until this rule is finished. */
320 status = 0;
321 break;
325 /* Process the remaining rules in the double colon chain so they're marked
326 considered. Start their prerequisites, too. */
327 if (file->double_colon)
328 for (; f != 0 ; f = f->prev)
330 struct dep *d;
332 f->considered = considered;
334 for (d = f->deps; d != 0; d = d->next)
335 status |= update_file (d->file, depth + 1);
338 return status;
341 /* Show a message stating the target failed to build. */
343 static void
344 complain (const struct file *file)
346 const char *msg_noparent
347 = _("%sNo rule to make target `%s'%s");
348 const char *msg_parent
349 = _("%sNo rule to make target `%s', needed by `%s'%s");
351 if (!keep_going_flag)
353 if (file->parent == 0)
354 fatal (NILF, msg_noparent, "", file->name, "");
356 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
359 if (file->parent == 0)
360 error (NILF, msg_noparent, "*** ", file->name, ".");
361 else
362 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
365 /* Consider a single `struct file' and update it as appropriate. */
367 static int
368 update_file_1 (struct file *file, unsigned int depth)
370 register FILE_TIMESTAMP this_mtime;
371 int noexist, must_make, deps_changed;
372 int dep_status = 0;
373 register struct dep *d, *lastd;
374 int running = 0;
376 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
378 if (file->updated)
380 if (file->update_status > 0)
382 DBF (DB_VERBOSE,
383 _("Recently tried and failed to update file `%s'.\n"));
385 /* If the file we tried to make is marked dontcare then no message
386 was printed about it when it failed during the makefile rebuild.
387 If we're trying to build it again in the normal rebuild, print a
388 message now. */
389 if (file->dontcare && !rebuilding_makefiles)
391 file->dontcare = 0;
392 complain (file);
395 return file->update_status;
398 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
399 return 0;
402 switch (file->command_state)
404 case cs_not_started:
405 case cs_deps_running:
406 break;
407 case cs_running:
408 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
409 return 0;
410 case cs_finished:
411 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
412 return file->update_status;
413 default:
414 abort ();
417 ++depth;
419 /* Notice recursive update of the same file. */
420 start_updating (file);
422 /* Looking at the file's modtime beforehand allows the possibility
423 that its name may be changed by a VPATH search, and thus it may
424 not need an implicit rule. If this were not done, the file
425 might get implicit commands that apply to its initial name, only
426 to have that name replaced with another found by VPATH search. */
428 this_mtime = file_mtime (file);
429 check_renamed (file);
430 noexist = this_mtime == NONEXISTENT_MTIME;
431 if (noexist)
432 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
433 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
434 && file->low_resolution_time)
436 /* Avoid spurious rebuilds due to low resolution time stamps. */
437 int ns = FILE_TIMESTAMP_NS (this_mtime);
438 if (ns != 0)
439 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
440 file->name);
441 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
444 must_make = noexist;
446 /* If file was specified as a target with no commands,
447 come up with some default commands. */
449 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
451 if (try_implicit_rule (file, depth))
452 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
453 else
454 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
455 file->tried_implicit = 1;
457 if (file->cmds == 0 && !file->is_target
458 && default_file != 0 && default_file->cmds != 0)
460 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
461 file->cmds = default_file->cmds;
464 /* Update all non-intermediate files we depend on, if necessary,
465 and see whether any of them is more recent than this file. */
467 lastd = 0;
468 d = file->deps;
469 while (d != 0)
471 FILE_TIMESTAMP mtime;
472 int maybe_make;
473 int dontcare = 0;
475 check_renamed (d->file);
477 mtime = file_mtime (d->file);
478 check_renamed (d->file);
480 if (is_updating (d->file))
482 error (NILF, _("Circular %s <- %s dependency dropped."),
483 file->name, d->file->name);
484 /* We cannot free D here because our the caller will still have
485 a reference to it when we were called recursively via
486 check_dep below. */
487 if (lastd == 0)
488 file->deps = d->next;
489 else
490 lastd->next = d->next;
491 d = d->next;
492 continue;
495 d->file->parent = file;
496 maybe_make = must_make;
498 /* Inherit dontcare flag from our parent. */
499 if (rebuilding_makefiles)
501 dontcare = d->file->dontcare;
502 d->file->dontcare = file->dontcare;
506 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
508 /* Restore original dontcare flag. */
509 if (rebuilding_makefiles)
510 d->file->dontcare = dontcare;
512 if (! d->ignore_mtime)
513 must_make = maybe_make;
515 check_renamed (d->file);
518 register struct file *f = d->file;
519 if (f->double_colon)
520 f = f->double_colon;
523 running |= (f->command_state == cs_running
524 || f->command_state == cs_deps_running);
525 f = f->prev;
527 while (f != 0);
530 if (dep_status != 0 && !keep_going_flag)
531 break;
533 if (!running)
534 /* The prereq is considered changed if the timestamp has changed while
535 it was built, OR it doesn't exist. */
536 d->changed = ((file_mtime (d->file) != mtime)
537 || (mtime == NONEXISTENT_MTIME));
539 lastd = d;
540 d = d->next;
543 /* Now we know whether this target needs updating.
544 If it does, update all the intermediate files we depend on. */
546 if (must_make || always_make_flag)
548 for (d = file->deps; d != 0; d = d->next)
549 if (d->file->intermediate)
551 int dontcare = 0;
553 FILE_TIMESTAMP mtime = file_mtime (d->file);
554 check_renamed (d->file);
555 d->file->parent = file;
557 /* Inherit dontcare flag from our parent. */
558 if (rebuilding_makefiles)
560 dontcare = d->file->dontcare;
561 d->file->dontcare = file->dontcare;
565 dep_status |= update_file (d->file, depth);
567 /* Restore original dontcare flag. */
568 if (rebuilding_makefiles)
569 d->file->dontcare = dontcare;
571 check_renamed (d->file);
574 register struct file *f = d->file;
575 if (f->double_colon)
576 f = f->double_colon;
579 running |= (f->command_state == cs_running
580 || f->command_state == cs_deps_running);
581 f = f->prev;
583 while (f != 0);
586 if (dep_status != 0 && !keep_going_flag)
587 break;
589 if (!running)
590 d->changed = ((file->phony && file->cmds != 0)
591 || file_mtime (d->file) != mtime);
595 finish_updating (file);
597 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
599 if (running)
601 set_command_state (file, cs_deps_running);
602 --depth;
603 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
604 return 0;
607 /* If any dependency failed, give up now. */
609 if (dep_status != 0)
611 file->update_status = dep_status;
612 notice_finished_file (file);
614 --depth;
616 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
618 if (depth == 0 && keep_going_flag
619 && !just_print_flag && !question_flag)
620 error (NILF,
621 _("Target `%s' not remade because of errors."), file->name);
623 return dep_status;
626 if (file->command_state == cs_deps_running)
627 /* The commands for some deps were running on the last iteration, but
628 they have finished now. Reset the command_state to not_started to
629 simplify later bookkeeping. It is important that we do this only
630 when the prior state was cs_deps_running, because that prior state
631 was definitely propagated to FILE's also_make's by set_command_state
632 (called above), but in another state an also_make may have
633 independently changed to finished state, and we would confuse that
634 file's bookkeeping (updated, but not_started is bogus state). */
635 set_command_state (file, cs_not_started);
637 /* Now record which prerequisites are more
638 recent than this file, so we can define $?. */
640 deps_changed = 0;
641 for (d = file->deps; d != 0; d = d->next)
643 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
644 check_renamed (d->file);
646 if (! d->ignore_mtime)
648 #if 1
649 /* %%% In version 4, remove this code completely to
650 implement not remaking deps if their deps are newer
651 than their parents. */
652 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
653 /* We must remake if this dep does not
654 exist and is not intermediate. */
655 must_make = 1;
656 #endif
658 /* Set DEPS_CHANGED if this dep actually changed. */
659 deps_changed |= d->changed;
662 /* Set D->changed if either this dep actually changed,
663 or its dependent, FILE, is older or does not exist. */
664 d->changed |= noexist || d_mtime > this_mtime;
666 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
668 const char *fmt = 0;
670 if (d->ignore_mtime)
672 if (ISDB (DB_VERBOSE))
673 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
675 else if (d_mtime == NONEXISTENT_MTIME)
677 if (ISDB (DB_BASIC))
678 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
680 else if (d->changed)
682 if (ISDB (DB_BASIC))
683 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
685 else if (ISDB (DB_VERBOSE))
686 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
688 if (fmt)
690 print_spaces (depth);
691 printf (fmt, dep_name (d), file->name);
692 fflush (stdout);
697 /* Here depth returns to the value it had when we were called. */
698 depth--;
700 if (file->double_colon && file->deps == 0)
702 must_make = 1;
703 DBF (DB_BASIC,
704 _("Target `%s' is double-colon and has no prerequisites.\n"));
706 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
707 && !always_make_flag)
709 must_make = 0;
710 DBF (DB_VERBOSE,
711 _("No commands for `%s' and no prerequisites actually changed.\n"));
713 else if (!must_make && file->cmds != 0 && always_make_flag)
715 must_make = 1;
716 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
719 if (!must_make)
721 if (ISDB (DB_VERBOSE))
723 print_spaces (depth);
724 printf (_("No need to remake target `%s'"), file->name);
725 if (!streq (file->name, file->hname))
726 printf (_("; using VPATH name `%s'"), file->hname);
727 puts (".");
728 fflush (stdout);
731 notice_finished_file (file);
733 /* Since we don't need to remake the file, convert it to use the
734 VPATH filename if we found one. hfile will be either the
735 local name if no VPATH or the VPATH name if one was found. */
737 while (file)
739 file->name = file->hname;
740 file = file->prev;
743 return 0;
746 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
748 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
749 VPATH. */
750 if (!streq(file->name, file->hname))
752 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
753 file->ignore_vpath = 1;
756 /* Now, take appropriate actions to remake the file. */
757 remake_file (file);
759 if (file->command_state != cs_finished)
761 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
762 return 0;
765 switch (file->update_status)
767 case 2:
768 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
769 break;
770 case 0:
771 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
772 break;
773 case 1:
774 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
775 break;
776 default:
777 assert (file->update_status >= 0 && file->update_status <= 2);
778 break;
781 file->updated = 1;
782 return file->update_status;
785 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
786 files listed in its `also_make' member. Under -t, this function also
787 touches FILE.
789 On return, FILE->update_status will no longer be -1 if it was. */
791 void
792 notice_finished_file (struct file *file)
794 struct dep *d;
795 int ran = file->command_state == cs_running;
796 int touched = 0;
798 file->command_state = cs_finished;
799 file->updated = 1;
801 if (touch_flag
802 /* The update status will be:
803 -1 if this target was not remade;
804 0 if 0 or more commands (+ or ${MAKE}) were run and won;
805 1 if some commands were run and lost.
806 We touch the target if it has commands which either were not run
807 or won when they ran (i.e. status is 0). */
808 && file->update_status == 0)
810 if (file->cmds != 0 && file->cmds->any_recurse)
812 /* If all the command lines were recursive,
813 we don't want to do the touching. */
814 unsigned int i;
815 for (i = 0; i < file->cmds->ncommand_lines; ++i)
816 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
817 goto have_nonrecursing;
819 else
821 have_nonrecursing:
822 if (file->phony)
823 file->update_status = 0;
824 /* According to POSIX, -t doesn't affect targets with no cmds. */
825 else if (file->cmds != 0)
827 /* Should set file's modification date and do nothing else. */
828 file->update_status = touch_file (file);
830 /* Pretend we ran a real touch command, to suppress the
831 "`foo' is up to date" message. */
832 commands_started++;
834 /* Request for the timestamp to be updated (and distributed
835 to the double-colon entries). Simply setting ran=1 would
836 almost have done the trick, but messes up with the also_make
837 updating logic below. */
838 touched = 1;
843 if (file->mtime_before_update == UNKNOWN_MTIME)
844 file->mtime_before_update = file->last_mtime;
846 if ((ran && !file->phony) || touched)
848 int i = 0;
850 /* If -n, -t, or -q and all the commands are recursive, we ran them so
851 really check the target's mtime again. Otherwise, assume the target
852 would have been updated. */
854 if (question_flag || just_print_flag || touch_flag)
856 for (i = file->cmds->ncommand_lines; i > 0; --i)
857 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
858 break;
861 /* If there were no commands at all, it's always new. */
863 else if (file->is_target && file->cmds == 0)
864 i = 1;
866 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
869 if (file->double_colon)
871 /* If this is a double colon rule and it is the last one to be
872 updated, propagate the change of modification time to all the
873 double-colon entries for this file.
875 We do it on the last update because it is important to handle
876 individual entries as separate rules with separate timestamps
877 while they are treated as targets and then as one rule with the
878 unified timestamp when they are considered as a prerequisite
879 of some target. */
881 struct file *f;
882 FILE_TIMESTAMP max_mtime = file->last_mtime;
884 /* Check that all rules were updated and at the same time find
885 the max timestamp. We assume UNKNOWN_MTIME is newer then
886 any other value. */
887 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
888 if (max_mtime != UNKNOWN_MTIME
889 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
890 max_mtime = f->last_mtime;
892 if (f == 0)
893 for (f = file->double_colon; f != 0; f = f->prev)
894 f->last_mtime = max_mtime;
897 if (ran && file->update_status != -1)
898 /* We actually tried to update FILE, which has
899 updated its also_make's as well (if it worked).
900 If it didn't work, it wouldn't work again for them.
901 So mark them as updated with the same status. */
902 for (d = file->also_make; d != 0; d = d->next)
904 d->file->command_state = cs_finished;
905 d->file->updated = 1;
906 d->file->update_status = file->update_status;
908 if (ran && !d->file->phony)
909 /* Fetch the new modification time.
910 We do this instead of just invalidating the cached time
911 so that a vpath_search can happen. Otherwise, it would
912 never be done because the target is already updated. */
913 (void) f_mtime (d->file, 0);
915 else if (file->update_status == -1)
916 /* Nothing was done for FILE, but it needed nothing done.
917 So mark it now as "succeeded". */
918 file->update_status = 0;
921 /* Check whether another file (whose mtime is THIS_MTIME)
922 needs updating on account of a dependency which is file FILE.
923 If it does, store 1 in *MUST_MAKE_PTR.
924 In the process, update any non-intermediate files
925 that FILE depends on (including FILE itself).
926 Return nonzero if any updating failed. */
928 static int
929 check_dep (struct file *file, unsigned int depth,
930 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
932 struct dep *d;
933 int dep_status = 0;
935 ++depth;
936 start_updating (file);
938 if (file->phony || !file->intermediate)
940 /* If this is a non-intermediate file, update it and record
941 whether it is newer than THIS_MTIME. */
942 FILE_TIMESTAMP mtime;
943 dep_status = update_file (file, depth);
944 check_renamed (file);
945 mtime = file_mtime (file);
946 check_renamed (file);
947 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
948 *must_make_ptr = 1;
950 else
952 /* FILE is an intermediate file. */
953 FILE_TIMESTAMP mtime;
955 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
957 if (try_implicit_rule (file, depth))
958 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
959 else
960 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
961 file->tried_implicit = 1;
963 if (file->cmds == 0 && !file->is_target
964 && default_file != 0 && default_file->cmds != 0)
966 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
967 file->cmds = default_file->cmds;
970 /* If the intermediate file actually exists
971 and is newer, then we should remake from it. */
972 check_renamed (file);
973 mtime = file_mtime (file);
974 check_renamed (file);
975 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
976 *must_make_ptr = 1;
977 /* Otherwise, update all non-intermediate files we depend on,
978 if necessary, and see whether any of them is more
979 recent than the file on whose behalf we are checking. */
980 else
982 struct dep *lastd;
984 lastd = 0;
985 d = file->deps;
986 while (d != 0)
988 int maybe_make;
990 if (is_updating (d->file))
992 error (NILF, _("Circular %s <- %s dependency dropped."),
993 file->name, d->file->name);
994 if (lastd == 0)
996 file->deps = d->next;
997 free ((char *) d);
998 d = file->deps;
1000 else
1002 lastd->next = d->next;
1003 free ((char *) d);
1004 d = lastd->next;
1006 continue;
1009 d->file->parent = file;
1010 maybe_make = *must_make_ptr;
1011 dep_status |= check_dep (d->file, depth, this_mtime,
1012 &maybe_make);
1013 if (! d->ignore_mtime)
1014 *must_make_ptr = maybe_make;
1015 check_renamed (d->file);
1016 if (dep_status != 0 && !keep_going_flag)
1017 break;
1019 if (d->file->command_state == cs_running
1020 || d->file->command_state == cs_deps_running)
1021 /* Record that some of FILE's deps are still being made.
1022 This tells the upper levels to wait on processing it until
1023 the commands are finished. */
1024 set_command_state (file, cs_deps_running);
1026 lastd = d;
1027 d = d->next;
1032 finish_updating (file);
1033 return dep_status;
1036 /* Touch FILE. Return zero if successful, one if not. */
1038 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1040 static int
1041 touch_file (struct file *file)
1043 if (!silent_flag)
1044 message (0, "touch %s", file->name);
1046 #ifndef NO_ARCHIVES
1047 if (ar_name (file->name))
1048 return ar_touch (file->name);
1049 else
1050 #endif
1052 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1054 if (fd < 0)
1055 TOUCH_ERROR ("touch: open: ");
1056 else
1058 struct stat statbuf;
1059 char buf;
1060 int e;
1062 EINTRLOOP (e, fstat (fd, &statbuf));
1063 if (e < 0)
1064 TOUCH_ERROR ("touch: fstat: ");
1065 /* Rewrite character 0 same as it already is. */
1066 if (read (fd, &buf, 1) < 0)
1067 TOUCH_ERROR ("touch: read: ");
1068 if (lseek (fd, 0L, 0) < 0L)
1069 TOUCH_ERROR ("touch: lseek: ");
1070 if (write (fd, &buf, 1) < 0)
1071 TOUCH_ERROR ("touch: write: ");
1072 /* If file length was 0, we just
1073 changed it, so change it back. */
1074 if (statbuf.st_size == 0)
1076 (void) close (fd);
1077 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1078 if (fd < 0)
1079 TOUCH_ERROR ("touch: open: ");
1081 (void) close (fd);
1085 return 0;
1088 /* Having checked and updated the dependencies of FILE,
1089 do whatever is appropriate to remake FILE itself.
1090 Return the status from executing FILE's commands. */
1092 static void
1093 remake_file (struct file *file)
1095 if (file->cmds == 0)
1097 if (file->phony)
1098 /* Phony target. Pretend it succeeded. */
1099 file->update_status = 0;
1100 else if (file->is_target)
1101 /* This is a nonexistent target file we cannot make.
1102 Pretend it was successfully remade. */
1103 file->update_status = 0;
1104 else
1106 /* This is a dependency file we cannot remake. Fail. */
1107 if (!rebuilding_makefiles || !file->dontcare)
1108 complain (file);
1109 file->update_status = 2;
1112 else
1114 chop_commands (file->cmds);
1116 /* The normal case: start some commands. */
1117 if (!touch_flag || file->cmds->any_recurse)
1119 execute_file_commands (file);
1120 return;
1123 /* This tells notice_finished_file it is ok to touch the file. */
1124 file->update_status = 0;
1127 /* This does the touching under -t. */
1128 notice_finished_file (file);
1131 /* Return the mtime of a file, given a `struct file'.
1132 Caches the time in the struct file to avoid excess stat calls.
1134 If the file is not found, and SEARCH is nonzero, VPATH searching and
1135 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1136 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1137 FILE. */
1139 FILE_TIMESTAMP
1140 f_mtime (struct file *file, int search)
1142 FILE_TIMESTAMP mtime;
1144 /* File's mtime is not known; must get it from the system. */
1146 #ifndef NO_ARCHIVES
1147 if (ar_name (file->name))
1149 /* This file is an archive-member reference. */
1151 char *arname, *memname;
1152 struct file *arfile;
1153 int arname_used = 0;
1154 time_t member_date;
1156 /* Find the archive's name. */
1157 ar_parse_name (file->name, &arname, &memname);
1159 /* Find the modification time of the archive itself.
1160 Also allow for its name to be changed via VPATH search. */
1161 arfile = lookup_file (arname);
1162 if (arfile == 0)
1164 arfile = enter_file (arname);
1165 arname_used = 1;
1167 mtime = f_mtime (arfile, search);
1168 check_renamed (arfile);
1169 if (search && strcmp (arfile->hname, arname))
1171 /* The archive's name has changed.
1172 Change the archive-member reference accordingly. */
1174 char *name;
1175 unsigned int arlen, memlen;
1177 if (!arname_used)
1179 free (arname);
1180 arname_used = 1;
1183 arname = arfile->hname;
1184 arlen = strlen (arname);
1185 memlen = strlen (memname);
1187 /* free (file->name); */
1189 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1190 bcopy (arname, name, arlen);
1191 name[arlen] = '(';
1192 bcopy (memname, name + arlen + 1, memlen);
1193 name[arlen + 1 + memlen] = ')';
1194 name[arlen + 1 + memlen + 1] = '\0';
1196 /* If the archive was found with GPATH, make the change permanent;
1197 otherwise defer it until later. */
1198 if (arfile->name == arfile->hname)
1199 rename_file (file, name);
1200 else
1201 rehash_file (file, name);
1202 check_renamed (file);
1205 if (!arname_used)
1206 free (arname);
1207 free (memname);
1209 file->low_resolution_time = 1;
1211 if (mtime == NONEXISTENT_MTIME)
1212 /* The archive doesn't exist, so its members don't exist either. */
1213 return NONEXISTENT_MTIME;
1215 member_date = ar_member_date (file->hname);
1216 mtime = (member_date == (time_t) -1
1217 ? NONEXISTENT_MTIME
1218 : file_timestamp_cons (file->hname, member_date, 0));
1220 else
1221 #endif
1223 mtime = name_mtime (file->name);
1225 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1227 /* If name_mtime failed, search VPATH. */
1228 char *name = file->name;
1229 if (vpath_search (&name, &mtime)
1230 /* Last resort, is it a library (-lxxx)? */
1231 || (name[0] == '-' && name[1] == 'l'
1232 && library_search (&name, &mtime)))
1234 if (mtime != UNKNOWN_MTIME)
1235 /* vpath_search and library_search store UNKNOWN_MTIME
1236 if they didn't need to do a stat call for their work. */
1237 file->last_mtime = mtime;
1239 /* If we found it in VPATH, see if it's in GPATH too; if so,
1240 change the name right now; if not, defer until after the
1241 dependencies are updated. */
1242 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1244 rename_file (file, name);
1245 check_renamed (file);
1246 return file_mtime (file);
1249 rehash_file (file, name);
1250 check_renamed (file);
1251 mtime = name_mtime (name);
1257 /* Files can have bogus timestamps that nothing newly made will be
1258 "newer" than. Updating their dependents could just result in loops.
1259 So notify the user of the anomaly with a warning.
1261 We only need to do this once, for now. */
1263 if (!clock_skew_detected
1264 && mtime != NONEXISTENT_MTIME
1265 && !file->updated)
1267 static FILE_TIMESTAMP adjusted_now;
1269 FILE_TIMESTAMP adjusted_mtime = mtime;
1271 #if defined(WINDOWS32) || defined(__MSDOS__)
1272 /* Experimentation has shown that FAT filesystems can set file times
1273 up to 3 seconds into the future! Play it safe. */
1275 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1277 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1278 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1279 adjusted_mtime -= adjustment;
1280 #elif defined(__EMX__)
1281 /* FAT filesystems round time to the nearest even second!
1282 Allow for any file (NTFS or FAT) to perhaps suffer from this
1283 brain damage. */
1284 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1285 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1286 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1287 : 0);
1288 #endif
1290 /* If the file's time appears to be in the future, update our
1291 concept of the present and try once more. */
1292 if (adjusted_now < adjusted_mtime)
1294 int resolution;
1295 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1296 adjusted_now = now + (resolution - 1);
1297 if (adjusted_now < adjusted_mtime)
1299 #ifdef NO_FLOAT
1300 error (NILF, _("Warning: File `%s' has modification time in the future"),
1301 file->name);
1302 #else
1303 double from_now =
1304 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1305 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1306 / 1e9));
1307 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1308 file->name, from_now);
1309 #endif
1310 clock_skew_detected = 1;
1316 /* Store the mtime into all the entries for this file. */
1317 if (file->double_colon)
1318 file = file->double_colon;
1322 /* If this file is not implicit but it is intermediate then it was
1323 made so by the .INTERMEDIATE target. If this file has never
1324 been built by us but was found now, it existed before make
1325 started. So, turn off the intermediate bit so make doesn't
1326 delete it, since it didn't create it. */
1327 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1328 && file->command_state == cs_not_started
1329 && !file->tried_implicit && file->intermediate)
1330 file->intermediate = 0;
1332 file->last_mtime = mtime;
1333 file = file->prev;
1335 while (file != 0);
1337 return mtime;
1341 /* Return the mtime of the file or archive-member reference NAME. */
1343 /* First, we check with stat(). If the file does not exist, then we return
1344 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1345 examine each indirection of the symlink and find the newest mtime.
1346 This causes one duplicate stat() when -L is being used, but the code is
1347 much cleaner. */
1349 static FILE_TIMESTAMP
1350 name_mtime (char *name)
1352 FILE_TIMESTAMP mtime;
1353 struct stat st;
1354 int e;
1356 EINTRLOOP (e, stat (name, &st));
1357 if (e == 0)
1358 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1359 else if (errno == ENOENT || errno == ENOTDIR)
1360 mtime = NONEXISTENT_MTIME;
1361 else
1363 perror_with_name ("stat: ", name);
1364 return NONEXISTENT_MTIME;
1367 /* If we get here we either found it, or it doesn't exist.
1368 If it doesn't exist see if we can use a symlink mtime instead. */
1370 #ifdef MAKE_SYMLINKS
1371 #ifndef S_ISLNK
1372 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1373 #endif
1374 if (check_symlink_flag)
1376 PATH_VAR (lpath);
1378 /* Check each symbolic link segment (if any). Find the latest mtime
1379 amongst all of them (and the target file of course).
1380 Note that we have already successfully dereferenced all the links
1381 above. So, if we run into any error trying to lstat(), or
1382 readlink(), or whatever, something bizarre-o happened. Just give up
1383 and use whatever mtime we've already computed at that point. */
1384 strcpy (lpath, name);
1385 while (1)
1387 FILE_TIMESTAMP ltime;
1388 PATH_VAR (lbuf);
1389 long llen;
1390 char *p;
1392 EINTRLOOP (e, lstat (lpath, &st));
1393 if (e)
1395 /* Just take what we have so far. */
1396 if (errno != ENOENT && errno != ENOTDIR)
1397 perror_with_name ("lstat: ", lpath);
1398 break;
1401 /* If this is not a symlink, we're done (we started with the real
1402 file's mtime so we don't need to test it again). */
1403 if (!S_ISLNK (st.st_mode))
1404 break;
1406 /* If this mtime is newer than what we had, keep the new one. */
1407 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1408 if (ltime > mtime)
1409 mtime = ltime;
1411 /* Set up to check the file pointed to by this link. */
1412 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1413 if (llen < 0)
1415 /* Eh? Just take what we have. */
1416 perror_with_name ("readlink: ", lpath);
1417 break;
1419 lbuf[llen] = '\0';
1421 /* If the target is fully-qualified or the source is just a
1422 filename, then the new path is the target. Otherwise it's the
1423 source directory plus the target. */
1424 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1425 strcpy (lpath, lbuf);
1426 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1427 /* Eh? Path too long! Again, just go with what we have. */
1428 break;
1429 else
1430 /* Create the next step in the symlink chain. */
1431 strcpy (p+1, lbuf);
1434 #endif
1436 return mtime;
1440 /* Search for a library file specified as -lLIBNAME, searching for a
1441 suitable library file in the system library directories and the VPATH
1442 directories. */
1444 static int
1445 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1447 static char *dirs[] =
1449 #ifndef _AMIGA
1450 "/lib",
1451 "/usr/lib",
1452 #endif
1453 #if defined(WINDOWS32) && !defined(LIBDIR)
1455 * This is completely up to the user at product install time. Just define
1456 * a placeholder.
1458 #define LIBDIR "."
1459 #endif
1460 LIBDIR, /* Defined by configuration. */
1464 static char *libpatterns = NULL;
1466 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1467 FILE_TIMESTAMP mtime;
1469 /* Loop variables for the libpatterns value. */
1470 char *p, *p2;
1471 unsigned int len;
1473 char *file, **dp;
1475 /* If we don't have libpatterns, get it. */
1476 if (!libpatterns)
1478 int save = warn_undefined_variables_flag;
1479 warn_undefined_variables_flag = 0;
1481 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1483 warn_undefined_variables_flag = save;
1486 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1487 p2 = libpatterns;
1488 while ((p = find_next_token (&p2, &len)) != 0)
1490 static char *buf = NULL;
1491 static unsigned int buflen = 0;
1492 static int libdir_maxlen = -1;
1493 char *libbuf = variable_expand ("");
1495 /* Expand the pattern using LIBNAME as a replacement. */
1497 char c = p[len];
1498 char *p3, *p4;
1500 p[len] = '\0';
1501 p3 = find_percent (p);
1502 if (!p3)
1504 /* Give a warning if there is no pattern, then remove the
1505 pattern so it's ignored next time. */
1506 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1507 for (; len; --len, ++p)
1508 *p = ' ';
1509 *p = c;
1510 continue;
1512 p4 = variable_buffer_output (libbuf, p, p3-p);
1513 p4 = variable_buffer_output (p4, libname, strlen (libname));
1514 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1515 p[len] = c;
1518 /* Look first for `libNAME.a' in the current directory. */
1519 mtime = name_mtime (libbuf);
1520 if (mtime != NONEXISTENT_MTIME)
1522 *lib = xstrdup (libbuf);
1523 if (mtime_ptr != 0)
1524 *mtime_ptr = mtime;
1525 return 1;
1528 /* Now try VPATH search on that. */
1530 file = libbuf;
1531 if (vpath_search (&file, mtime_ptr))
1533 *lib = file;
1534 return 1;
1537 /* Now try the standard set of directories. */
1539 if (!buflen)
1541 for (dp = dirs; *dp != 0; ++dp)
1543 int l = strlen (*dp);
1544 if (l > libdir_maxlen)
1545 libdir_maxlen = l;
1547 buflen = strlen (libbuf);
1548 buf = xmalloc(libdir_maxlen + buflen + 2);
1550 else if (buflen < strlen (libbuf))
1552 buflen = strlen (libbuf);
1553 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1556 for (dp = dirs; *dp != 0; ++dp)
1558 sprintf (buf, "%s/%s", *dp, libbuf);
1559 mtime = name_mtime (buf);
1560 if (mtime != NONEXISTENT_MTIME)
1562 *lib = xstrdup (buf);
1563 if (mtime_ptr != 0)
1564 *mtime_ptr = mtime;
1565 return 1;
1570 return 0;