Numerous updates to tests for issues found on Cygwin and Windows.
[make/kirr.git] / remake.c
blobda231b4a0a465e092321e54a8839af55d9cf0265
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
19 #include "make.h"
20 #include "filedef.h"
21 #include "job.h"
22 #include "commands.h"
23 #include "dep.h"
24 #include "variable.h"
25 #include "debug.h"
27 #include <assert.h>
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #else
32 #include <sys/file.h>
33 #endif
35 #ifdef VMS
36 #include <starlet.h>
37 #endif
38 #ifdef WINDOWS32
39 #include <io.h>
40 #endif
42 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
45 /* The test for circular dependencies is based on the 'updating' bit in
46 `struct file'. However, double colon targets have seperate `struct
47 file's; make sure we always use the base of the double colon chain. */
49 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
50 ->updating = 1)
51 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 0)
53 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating)
57 /* Incremented when a command is started (under -n, when one would be). */
58 unsigned int commands_started = 0;
60 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
61 static unsigned int considered;
63 static int update_file PARAMS ((struct file *file, unsigned int depth));
64 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
65 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
66 static int touch_file PARAMS ((struct file *file));
67 static void remake_file PARAMS ((struct file *file));
68 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
69 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
72 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
73 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
75 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
76 and -n should be disabled for them unless they were also command-line
77 targets, and we should only make one goal at a time and return as soon as
78 one goal whose `changed' member is nonzero is successfully made. */
80 int
81 update_goal_chain (struct dep *goals)
83 int t = touch_flag, q = question_flag, n = just_print_flag;
84 unsigned int j = job_slots;
85 int status = -1;
87 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
88 : file_mtime (file))
90 /* Duplicate the chain so we can remove things from it. */
92 goals = copy_dep_chain (goals);
95 /* Clear the `changed' flag of each goal in the chain.
96 We will use the flag below to notice when any commands
97 have actually been run for a target. When no commands
98 have been run, we give an "up to date" diagnostic. */
100 struct dep *g;
101 for (g = goals; g != 0; g = g->next)
102 g->changed = 0;
105 /* All files start with the considered bit 0, so the global value is 1. */
106 considered = 1;
108 /* Update all the goals until they are all finished. */
110 while (goals != 0)
112 register struct dep *g, *lastgoal;
114 /* Start jobs that are waiting for the load to go down. */
116 start_waiting_jobs ();
118 /* Wait for a child to die. */
120 reap_children (1, 0);
122 lastgoal = 0;
123 g = goals;
124 while (g != 0)
126 /* Iterate over all double-colon entries for this file. */
127 struct file *file;
128 int stop = 0, any_not_updated = 0;
130 for (file = g->file->double_colon ? g->file->double_colon : g->file;
131 file != NULL;
132 file = file->prev)
134 unsigned int ocommands_started;
135 int x;
136 check_renamed (file);
137 if (rebuilding_makefiles)
139 if (file->cmd_target)
141 touch_flag = t;
142 question_flag = q;
143 just_print_flag = n;
145 else
146 touch_flag = question_flag = just_print_flag = 0;
149 /* Save the old value of `commands_started' so we can compare
150 later. It will be incremented when any commands are
151 actually run. */
152 ocommands_started = commands_started;
154 x = update_file (file, rebuilding_makefiles ? 1 : 0);
155 check_renamed (file);
157 /* Set the goal's `changed' flag if any commands were started
158 by calling update_file above. We check this flag below to
159 decide when to give an "up to date" diagnostic. */
160 if (commands_started > ocommands_started)
161 g->changed = 1;
163 /* If we updated a file and STATUS was not already 1, set it to
164 1 if updating failed, or to 0 if updating succeeded. Leave
165 STATUS as it is if no updating was done. */
167 stop = 0;
168 if ((x != 0 || file->updated) && status < 1)
170 if (file->update_status != 0)
172 /* Updating failed, or -q triggered. The STATUS value
173 tells our caller which. */
174 status = file->update_status;
175 /* If -q just triggered, stop immediately. It doesn't
176 matter how much more we run, since we already know
177 the answer to return. */
178 stop = (question_flag && !keep_going_flag
179 && !rebuilding_makefiles);
181 else
183 FILE_TIMESTAMP mtime = MTIME (file);
184 check_renamed (file);
186 if (file->updated && g->changed &&
187 mtime != file->mtime_before_update)
189 /* Updating was done. If this is a makefile and
190 just_print_flag or question_flag is set (meaning
191 -n or -q was given and this file was specified
192 as a command-line target), don't change STATUS.
193 If STATUS is changed, we will get re-exec'd, and
194 enter an infinite loop. */
195 if (!rebuilding_makefiles
196 || (!just_print_flag && !question_flag))
197 status = 0;
198 if (rebuilding_makefiles && file->dontcare)
199 /* This is a default makefile; stop remaking. */
200 stop = 1;
205 /* Keep track if any double-colon entry is not finished.
206 When they are all finished, the goal is finished. */
207 any_not_updated |= !file->updated;
209 if (stop)
210 break;
213 /* Reset FILE since it is null at the end of the loop. */
214 file = g->file;
216 if (stop || !any_not_updated)
218 /* If we have found nothing whatever to do for the goal,
219 print a message saying nothing needs doing. */
221 if (!rebuilding_makefiles
222 /* If the update_status is zero, we updated successfully
223 or not at all. G->changed will have been set above if
224 any commands were actually started for this goal. */
225 && file->update_status == 0 && !g->changed
226 /* Never give a message under -s or -q. */
227 && !silent_flag && !question_flag)
228 message (1, ((file->phony || file->cmds == 0)
229 ? _("Nothing to be done for `%s'.")
230 : _("`%s' is up to date.")),
231 file->name);
233 /* This goal is finished. Remove it from the chain. */
234 if (lastgoal == 0)
235 goals = g->next;
236 else
237 lastgoal->next = g->next;
239 /* Free the storage. */
240 free ((char *) g);
242 g = lastgoal == 0 ? goals : lastgoal->next;
244 if (stop)
245 break;
247 else
249 lastgoal = g;
250 g = g->next;
254 /* If we reached the end of the dependency graph toggle the considered
255 flag for the next pass. */
256 if (g == 0)
257 considered = !considered;
260 if (rebuilding_makefiles)
262 touch_flag = t;
263 question_flag = q;
264 just_print_flag = n;
265 job_slots = j;
267 return status;
270 /* If FILE is not up to date, execute the commands for it.
271 Return 0 if successful, 1 if unsuccessful;
272 but with some flag settings, just call `exit' if unsuccessful.
274 DEPTH is the depth in recursions of this function.
275 We increment it during the consideration of our dependencies,
276 then decrement it again after finding out whether this file
277 is out of date.
279 If there are multiple double-colon entries for FILE,
280 each is considered in turn. */
282 static int
283 update_file (struct file *file, unsigned int depth)
285 register int status = 0;
286 register struct file *f;
288 f = file->double_colon ? file->double_colon : file;
290 /* Prune the dependency graph: if we've already been here on _this_
291 pass through the dependency graph, we don't have to go any further.
292 We won't reap_children until we start the next pass, so no state
293 change is possible below here until then. */
294 if (f->considered == considered)
296 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
297 return f->command_state == cs_finished ? f->update_status : 0;
300 /* This loop runs until we start commands for a double colon rule, or until
301 the chain is exhausted. */
302 for (; f != 0; f = f->prev)
304 f->considered = considered;
306 status |= update_file_1 (f, depth);
307 check_renamed (f);
309 /* If we got an error, don't bother with double_colon etc. */
310 if (status != 0 && !keep_going_flag)
311 return status;
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;
471 int dontcare = 0;
473 check_renamed (d->file);
475 mtime = file_mtime (d->file);
476 check_renamed (d->file);
478 if (is_updating (d->file))
480 error (NILF, _("Circular %s <- %s dependency dropped."),
481 file->name, d->file->name);
482 /* We cannot free D here because our the caller will still have
483 a reference to it when we were called recursively via
484 check_dep below. */
485 if (lastd == 0)
486 file->deps = d->next;
487 else
488 lastd->next = d->next;
489 d = d->next;
490 continue;
493 d->file->parent = file;
494 maybe_make = must_make;
496 /* Inherit dontcare flag from our parent. */
497 if (rebuilding_makefiles)
499 dontcare = d->file->dontcare;
500 d->file->dontcare = file->dontcare;
504 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
506 /* Restore original dontcare flag. */
507 if (rebuilding_makefiles)
508 d->file->dontcare = dontcare;
510 if (! d->ignore_mtime)
511 must_make = maybe_make;
513 check_renamed (d->file);
516 register struct file *f = d->file;
517 if (f->double_colon)
518 f = f->double_colon;
521 running |= (f->command_state == cs_running
522 || f->command_state == cs_deps_running);
523 f = f->prev;
525 while (f != 0);
528 if (dep_status != 0 && !keep_going_flag)
529 break;
531 if (!running)
532 /* The prereq is considered changed if the timestamp has changed while
533 it was built, OR it doesn't exist.
534 This causes the Linux kernel build to break. We'll defer this
535 fix until GNU make 3.82 to give them time to update. */
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 /* If the result of a vpath search is -o or -W, preserve it.
1252 Otherwise, find the mtime of the resulting file. */
1253 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1254 mtime = name_mtime (name);
1259 /* Files can have bogus timestamps that nothing newly made will be
1260 "newer" than. Updating their dependents could just result in loops.
1261 So notify the user of the anomaly with a warning.
1263 We only need to do this once, for now. */
1265 if (!clock_skew_detected
1266 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1267 && !file->updated)
1269 static FILE_TIMESTAMP adjusted_now;
1271 FILE_TIMESTAMP adjusted_mtime = mtime;
1273 #if defined(WINDOWS32) || defined(__MSDOS__)
1274 /* Experimentation has shown that FAT filesystems can set file times
1275 up to 3 seconds into the future! Play it safe. */
1277 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1279 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1280 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1281 adjusted_mtime -= adjustment;
1282 #elif defined(__EMX__)
1283 /* FAT filesystems round time to the nearest even second!
1284 Allow for any file (NTFS or FAT) to perhaps suffer from this
1285 brain damage. */
1286 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1287 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1288 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1289 : 0);
1290 #endif
1292 /* If the file's time appears to be in the future, update our
1293 concept of the present and try once more. */
1294 if (adjusted_now < adjusted_mtime)
1296 int resolution;
1297 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1298 adjusted_now = now + (resolution - 1);
1299 if (adjusted_now < adjusted_mtime)
1301 #ifdef NO_FLOAT
1302 error (NILF, _("Warning: File `%s' has modification time in the future"),
1303 file->name);
1304 #else
1305 double from_now =
1306 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1307 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1308 / 1e9));
1309 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1310 file->name, from_now);
1311 #endif
1312 clock_skew_detected = 1;
1317 /* Store the mtime into all the entries for this file. */
1318 if (file->double_colon)
1319 file = file->double_colon;
1323 /* If this file is not implicit but it is intermediate then it was
1324 made so by the .INTERMEDIATE target. If this file has never
1325 been built by us but was found now, it existed before make
1326 started. So, turn off the intermediate bit so make doesn't
1327 delete it, since it didn't create it. */
1328 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1329 && file->command_state == cs_not_started
1330 && !file->tried_implicit && file->intermediate)
1331 file->intermediate = 0;
1333 file->last_mtime = mtime;
1334 file = file->prev;
1336 while (file != 0);
1338 return mtime;
1342 /* Return the mtime of the file or archive-member reference NAME. */
1344 /* First, we check with stat(). If the file does not exist, then we return
1345 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1346 examine each indirection of the symlink and find the newest mtime.
1347 This causes one duplicate stat() when -L is being used, but the code is
1348 much cleaner. */
1350 static FILE_TIMESTAMP
1351 name_mtime (char *name)
1353 FILE_TIMESTAMP mtime;
1354 struct stat st;
1355 int e;
1357 EINTRLOOP (e, stat (name, &st));
1358 if (e == 0)
1359 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1360 else if (errno == ENOENT || errno == ENOTDIR)
1361 mtime = NONEXISTENT_MTIME;
1362 else
1364 perror_with_name ("stat: ", name);
1365 return NONEXISTENT_MTIME;
1368 /* If we get here we either found it, or it doesn't exist.
1369 If it doesn't exist see if we can use a symlink mtime instead. */
1371 #ifdef MAKE_SYMLINKS
1372 #ifndef S_ISLNK
1373 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1374 #endif
1375 if (check_symlink_flag)
1377 PATH_VAR (lpath);
1379 /* Check each symbolic link segment (if any). Find the latest mtime
1380 amongst all of them (and the target file of course).
1381 Note that we have already successfully dereferenced all the links
1382 above. So, if we run into any error trying to lstat(), or
1383 readlink(), or whatever, something bizarre-o happened. Just give up
1384 and use whatever mtime we've already computed at that point. */
1385 strcpy (lpath, name);
1386 while (1)
1388 FILE_TIMESTAMP ltime;
1389 PATH_VAR (lbuf);
1390 long llen;
1391 char *p;
1393 EINTRLOOP (e, lstat (lpath, &st));
1394 if (e)
1396 /* Just take what we have so far. */
1397 if (errno != ENOENT && errno != ENOTDIR)
1398 perror_with_name ("lstat: ", lpath);
1399 break;
1402 /* If this is not a symlink, we're done (we started with the real
1403 file's mtime so we don't need to test it again). */
1404 if (!S_ISLNK (st.st_mode))
1405 break;
1407 /* If this mtime is newer than what we had, keep the new one. */
1408 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1409 if (ltime > mtime)
1410 mtime = ltime;
1412 /* Set up to check the file pointed to by this link. */
1413 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1414 if (llen < 0)
1416 /* Eh? Just take what we have. */
1417 perror_with_name ("readlink: ", lpath);
1418 break;
1420 lbuf[llen] = '\0';
1422 /* If the target is fully-qualified or the source is just a
1423 filename, then the new path is the target. Otherwise it's the
1424 source directory plus the target. */
1425 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1426 strcpy (lpath, lbuf);
1427 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1428 /* Eh? Path too long! Again, just go with what we have. */
1429 break;
1430 else
1431 /* Create the next step in the symlink chain. */
1432 strcpy (p+1, lbuf);
1435 #endif
1437 return mtime;
1441 /* Search for a library file specified as -lLIBNAME, searching for a
1442 suitable library file in the system library directories and the VPATH
1443 directories. */
1445 static int
1446 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1448 static char *dirs[] =
1450 #ifndef _AMIGA
1451 "/lib",
1452 "/usr/lib",
1453 #endif
1454 #if defined(WINDOWS32) && !defined(LIBDIR)
1456 * This is completely up to the user at product install time. Just define
1457 * a placeholder.
1459 #define LIBDIR "."
1460 #endif
1461 LIBDIR, /* Defined by configuration. */
1465 static char *libpatterns = NULL;
1467 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1468 FILE_TIMESTAMP mtime;
1470 /* Loop variables for the libpatterns value. */
1471 char *p, *p2;
1472 unsigned int len;
1474 char *file, **dp;
1476 /* If we don't have libpatterns, get it. */
1477 if (!libpatterns)
1479 int save = warn_undefined_variables_flag;
1480 warn_undefined_variables_flag = 0;
1482 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1484 warn_undefined_variables_flag = save;
1487 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1488 p2 = libpatterns;
1489 while ((p = find_next_token (&p2, &len)) != 0)
1491 static char *buf = NULL;
1492 static unsigned int buflen = 0;
1493 static int libdir_maxlen = -1;
1494 char *libbuf = variable_expand ("");
1496 /* Expand the pattern using LIBNAME as a replacement. */
1498 char c = p[len];
1499 char *p3, *p4;
1501 p[len] = '\0';
1502 p3 = find_percent (p);
1503 if (!p3)
1505 /* Give a warning if there is no pattern, then remove the
1506 pattern so it's ignored next time. */
1507 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1508 for (; len; --len, ++p)
1509 *p = ' ';
1510 *p = c;
1511 continue;
1513 p4 = variable_buffer_output (libbuf, p, p3-p);
1514 p4 = variable_buffer_output (p4, libname, strlen (libname));
1515 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1516 p[len] = c;
1519 /* Look first for `libNAME.a' in the current directory. */
1520 mtime = name_mtime (libbuf);
1521 if (mtime != NONEXISTENT_MTIME)
1523 *lib = xstrdup (libbuf);
1524 if (mtime_ptr != 0)
1525 *mtime_ptr = mtime;
1526 return 1;
1529 /* Now try VPATH search on that. */
1531 file = libbuf;
1532 if (vpath_search (&file, mtime_ptr))
1534 *lib = file;
1535 return 1;
1538 /* Now try the standard set of directories. */
1540 if (!buflen)
1542 for (dp = dirs; *dp != 0; ++dp)
1544 int l = strlen (*dp);
1545 if (l > libdir_maxlen)
1546 libdir_maxlen = l;
1548 buflen = strlen (libbuf);
1549 buf = xmalloc(libdir_maxlen + buflen + 2);
1551 else if (buflen < strlen (libbuf))
1553 buflen = strlen (libbuf);
1554 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1557 for (dp = dirs; *dp != 0; ++dp)
1559 sprintf (buf, "%s/%s", *dp, libbuf);
1560 mtime = name_mtime (buf);
1561 if (mtime != NONEXISTENT_MTIME)
1563 *lib = xstrdup (buf);
1564 if (mtime_ptr != 0)
1565 *mtime_ptr = mtime;
1566 return 1;
1571 return 0;