Try using POSIX::getcwd to find the working directory wherever it exists.
[make.git] / remake.c
blob750a80c2c4121bc98e17190fafbd361a7fd3b0cb
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 = (question_flag && !keep_going_flag
180 && !rebuilding_makefiles);
182 else
184 FILE_TIMESTAMP mtime = MTIME (file);
185 check_renamed (file);
187 if (file->updated && g->changed &&
188 mtime != file->mtime_before_update)
190 /* Updating was done. If this is a makefile and
191 just_print_flag or question_flag is set (meaning
192 -n or -q was given and this file was specified
193 as a command-line target), don't change STATUS.
194 If STATUS is changed, we will get re-exec'd, and
195 enter an infinite loop. */
196 if (!rebuilding_makefiles
197 || (!just_print_flag && !question_flag))
198 status = 0;
199 if (rebuilding_makefiles && file->dontcare)
200 /* This is a default makefile; stop remaking. */
201 stop = 1;
206 /* Keep track if any double-colon entry is not finished.
207 When they are all finished, the goal is finished. */
208 any_not_updated |= !file->updated;
210 if (stop)
211 break;
214 /* Reset FILE since it is null at the end of the loop. */
215 file = g->file;
217 if (stop || !any_not_updated)
219 /* If we have found nothing whatever to do for the goal,
220 print a message saying nothing needs doing. */
222 if (!rebuilding_makefiles
223 /* If the update_status is zero, we updated successfully
224 or not at all. G->changed will have been set above if
225 any commands were actually started for this goal. */
226 && file->update_status == 0 && !g->changed
227 /* Never give a message under -s or -q. */
228 && !silent_flag && !question_flag)
229 message (1, ((file->phony || file->cmds == 0)
230 ? _("Nothing to be done for `%s'.")
231 : _("`%s' is up to date.")),
232 file->name);
234 /* This goal is finished. Remove it from the chain. */
235 if (lastgoal == 0)
236 goals = g->next;
237 else
238 lastgoal->next = g->next;
240 /* Free the storage. */
241 free ((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 we got an error, don't bother with double_colon etc. */
311 if (status != 0 && !keep_going_flag)
312 return status;
314 if (f->command_state == cs_running
315 || f->command_state == cs_deps_running)
317 /* Don't run the other :: rules for this
318 file until this rule is finished. */
319 status = 0;
320 break;
324 /* Process the remaining rules in the double colon chain so they're marked
325 considered. Start their prerequisites, too. */
326 if (file->double_colon)
327 for (; f != 0 ; f = f->prev)
329 struct dep *d;
331 f->considered = considered;
333 for (d = f->deps; d != 0; d = d->next)
334 status |= update_file (d->file, depth + 1);
337 return status;
340 /* Show a message stating the target failed to build. */
342 static void
343 complain (const struct file *file)
345 const char *msg_noparent
346 = _("%sNo rule to make target `%s'%s");
347 const char *msg_parent
348 = _("%sNo rule to make target `%s', needed by `%s'%s");
350 if (!keep_going_flag)
352 if (file->parent == 0)
353 fatal (NILF, msg_noparent, "", file->name, "");
355 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
358 if (file->parent == 0)
359 error (NILF, msg_noparent, "*** ", file->name, ".");
360 else
361 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
364 /* Consider a single `struct file' and update it as appropriate. */
366 static int
367 update_file_1 (struct file *file, unsigned int depth)
369 register FILE_TIMESTAMP this_mtime;
370 int noexist, must_make, deps_changed;
371 int dep_status = 0;
372 register struct dep *d, *lastd;
373 int running = 0;
375 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
377 if (file->updated)
379 if (file->update_status > 0)
381 DBF (DB_VERBOSE,
382 _("Recently tried and failed to update file `%s'.\n"));
384 /* If the file we tried to make is marked dontcare then no message
385 was printed about it when it failed during the makefile rebuild.
386 If we're trying to build it again in the normal rebuild, print a
387 message now. */
388 if (file->dontcare && !rebuilding_makefiles)
390 file->dontcare = 0;
391 complain (file);
394 return file->update_status;
397 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
398 return 0;
401 switch (file->command_state)
403 case cs_not_started:
404 case cs_deps_running:
405 break;
406 case cs_running:
407 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
408 return 0;
409 case cs_finished:
410 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
411 return file->update_status;
412 default:
413 abort ();
416 ++depth;
418 /* Notice recursive update of the same file. */
419 start_updating (file);
421 /* Looking at the file's modtime beforehand allows the possibility
422 that its name may be changed by a VPATH search, and thus it may
423 not need an implicit rule. If this were not done, the file
424 might get implicit commands that apply to its initial name, only
425 to have that name replaced with another found by VPATH search. */
427 this_mtime = file_mtime (file);
428 check_renamed (file);
429 noexist = this_mtime == NONEXISTENT_MTIME;
430 if (noexist)
431 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
432 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
433 && file->low_resolution_time)
435 /* Avoid spurious rebuilds due to low resolution time stamps. */
436 int ns = FILE_TIMESTAMP_NS (this_mtime);
437 if (ns != 0)
438 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
439 file->name);
440 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
443 must_make = noexist;
445 /* If file was specified as a target with no commands,
446 come up with some default commands. */
448 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
450 if (try_implicit_rule (file, depth))
451 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
452 else
453 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
454 file->tried_implicit = 1;
456 if (file->cmds == 0 && !file->is_target
457 && default_file != 0 && default_file->cmds != 0)
459 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
460 file->cmds = default_file->cmds;
463 /* Update all non-intermediate files we depend on, if necessary,
464 and see whether any of them is more recent than this file. */
466 lastd = 0;
467 d = file->deps;
468 while (d != 0)
470 FILE_TIMESTAMP mtime;
471 int maybe_make;
472 int dontcare = 0;
474 check_renamed (d->file);
476 mtime = file_mtime (d->file);
477 check_renamed (d->file);
479 if (is_updating (d->file))
481 error (NILF, _("Circular %s <- %s dependency dropped."),
482 file->name, d->file->name);
483 /* We cannot free D here because our the caller will still have
484 a reference to it when we were called recursively via
485 check_dep below. */
486 if (lastd == 0)
487 file->deps = d->next;
488 else
489 lastd->next = d->next;
490 d = d->next;
491 continue;
494 d->file->parent = file;
495 maybe_make = must_make;
497 /* Inherit dontcare flag from our parent. */
498 if (rebuilding_makefiles)
500 dontcare = d->file->dontcare;
501 d->file->dontcare = file->dontcare;
505 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
507 /* Restore original dontcare flag. */
508 if (rebuilding_makefiles)
509 d->file->dontcare = dontcare;
511 if (! d->ignore_mtime)
512 must_make = maybe_make;
514 check_renamed (d->file);
517 register struct file *f = d->file;
518 if (f->double_colon)
519 f = f->double_colon;
522 running |= (f->command_state == cs_running
523 || f->command_state == cs_deps_running);
524 f = f->prev;
526 while (f != 0);
529 if (dep_status != 0 && !keep_going_flag)
530 break;
532 if (!running)
533 d->changed = file_mtime (d->file) != mtime;
535 lastd = d;
536 d = d->next;
539 /* Now we know whether this target needs updating.
540 If it does, update all the intermediate files we depend on. */
542 if (must_make || always_make_flag)
544 for (d = file->deps; d != 0; d = d->next)
545 if (d->file->intermediate)
547 int dontcare = 0;
549 FILE_TIMESTAMP mtime = file_mtime (d->file);
550 check_renamed (d->file);
551 d->file->parent = file;
553 /* Inherit dontcare flag from our parent. */
554 if (rebuilding_makefiles)
556 dontcare = d->file->dontcare;
557 d->file->dontcare = file->dontcare;
561 dep_status |= update_file (d->file, depth);
563 /* Restore original dontcare flag. */
564 if (rebuilding_makefiles)
565 d->file->dontcare = dontcare;
567 check_renamed (d->file);
570 register struct file *f = d->file;
571 if (f->double_colon)
572 f = f->double_colon;
575 running |= (f->command_state == cs_running
576 || f->command_state == cs_deps_running);
577 f = f->prev;
579 while (f != 0);
582 if (dep_status != 0 && !keep_going_flag)
583 break;
585 if (!running)
586 d->changed = ((file->phony && file->cmds != 0)
587 || file_mtime (d->file) != mtime);
591 finish_updating (file);
593 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
595 if (running)
597 set_command_state (file, cs_deps_running);
598 --depth;
599 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
600 return 0;
603 /* If any dependency failed, give up now. */
605 if (dep_status != 0)
607 file->update_status = dep_status;
608 notice_finished_file (file);
610 --depth;
612 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
614 if (depth == 0 && keep_going_flag
615 && !just_print_flag && !question_flag)
616 error (NILF,
617 _("Target `%s' not remade because of errors."), file->name);
619 return dep_status;
622 if (file->command_state == cs_deps_running)
623 /* The commands for some deps were running on the last iteration, but
624 they have finished now. Reset the command_state to not_started to
625 simplify later bookkeeping. It is important that we do this only
626 when the prior state was cs_deps_running, because that prior state
627 was definitely propagated to FILE's also_make's by set_command_state
628 (called above), but in another state an also_make may have
629 independently changed to finished state, and we would confuse that
630 file's bookkeeping (updated, but not_started is bogus state). */
631 set_command_state (file, cs_not_started);
633 /* Now record which prerequisites are more
634 recent than this file, so we can define $?. */
636 deps_changed = 0;
637 for (d = file->deps; d != 0; d = d->next)
639 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
640 check_renamed (d->file);
642 if (! d->ignore_mtime)
644 #if 1
645 /* %%% In version 4, remove this code completely to
646 implement not remaking deps if their deps are newer
647 than their parents. */
648 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
649 /* We must remake if this dep does not
650 exist and is not intermediate. */
651 must_make = 1;
652 #endif
654 /* Set DEPS_CHANGED if this dep actually changed. */
655 deps_changed |= d->changed;
658 /* Set D->changed if either this dep actually changed,
659 or its dependent, FILE, is older or does not exist. */
660 d->changed |= noexist || d_mtime > this_mtime;
662 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
664 const char *fmt = 0;
666 if (d->ignore_mtime)
668 if (ISDB (DB_VERBOSE))
669 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
671 else if (d_mtime == NONEXISTENT_MTIME)
673 if (ISDB (DB_BASIC))
674 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
676 else if (d->changed)
678 if (ISDB (DB_BASIC))
679 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
681 else if (ISDB (DB_VERBOSE))
682 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
684 if (fmt)
686 print_spaces (depth);
687 printf (fmt, dep_name (d), file->name);
688 fflush (stdout);
693 /* Here depth returns to the value it had when we were called. */
694 depth--;
696 if (file->double_colon && file->deps == 0)
698 must_make = 1;
699 DBF (DB_BASIC,
700 _("Target `%s' is double-colon and has no prerequisites.\n"));
702 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
703 && !always_make_flag)
705 must_make = 0;
706 DBF (DB_VERBOSE,
707 _("No commands for `%s' and no prerequisites actually changed.\n"));
709 else if (!must_make && file->cmds != 0 && always_make_flag)
711 must_make = 1;
712 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
715 if (!must_make)
717 if (ISDB (DB_VERBOSE))
719 print_spaces (depth);
720 printf (_("No need to remake target `%s'"), file->name);
721 if (!streq (file->name, file->hname))
722 printf (_("; using VPATH name `%s'"), file->hname);
723 puts (".");
724 fflush (stdout);
727 notice_finished_file (file);
729 /* Since we don't need to remake the file, convert it to use the
730 VPATH filename if we found one. hfile will be either the
731 local name if no VPATH or the VPATH name if one was found. */
733 while (file)
735 file->name = file->hname;
736 file = file->prev;
739 return 0;
742 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
744 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
745 VPATH. */
746 if (!streq(file->name, file->hname))
748 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
749 file->ignore_vpath = 1;
752 /* Now, take appropriate actions to remake the file. */
753 remake_file (file);
755 if (file->command_state != cs_finished)
757 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
758 return 0;
761 switch (file->update_status)
763 case 2:
764 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
765 break;
766 case 0:
767 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
768 break;
769 case 1:
770 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
771 break;
772 default:
773 assert (file->update_status >= 0 && file->update_status <= 2);
774 break;
777 file->updated = 1;
778 return file->update_status;
781 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
782 files listed in its `also_make' member. Under -t, this function also
783 touches FILE.
785 On return, FILE->update_status will no longer be -1 if it was. */
787 void
788 notice_finished_file (struct file *file)
790 struct dep *d;
791 int ran = file->command_state == cs_running;
792 int touched = 0;
794 file->command_state = cs_finished;
795 file->updated = 1;
797 if (touch_flag
798 /* The update status will be:
799 -1 if this target was not remade;
800 0 if 0 or more commands (+ or ${MAKE}) were run and won;
801 1 if some commands were run and lost.
802 We touch the target if it has commands which either were not run
803 or won when they ran (i.e. status is 0). */
804 && file->update_status == 0)
806 if (file->cmds != 0 && file->cmds->any_recurse)
808 /* If all the command lines were recursive,
809 we don't want to do the touching. */
810 unsigned int i;
811 for (i = 0; i < file->cmds->ncommand_lines; ++i)
812 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
813 goto have_nonrecursing;
815 else
817 have_nonrecursing:
818 if (file->phony)
819 file->update_status = 0;
820 /* According to POSIX, -t doesn't affect targets with no cmds. */
821 else if (file->cmds != 0)
823 /* Should set file's modification date and do nothing else. */
824 file->update_status = touch_file (file);
826 /* Pretend we ran a real touch command, to suppress the
827 "`foo' is up to date" message. */
828 commands_started++;
830 /* Request for the timestamp to be updated (and distributed
831 to the double-colon entries). Simply setting ran=1 would
832 almost have done the trick, but messes up with the also_make
833 updating logic below. */
834 touched = 1;
839 if (file->mtime_before_update == UNKNOWN_MTIME)
840 file->mtime_before_update = file->last_mtime;
842 if ((ran && !file->phony) || touched)
844 struct file *f;
845 int i = 0;
847 /* If -n, -t, or -q and all the commands are recursive, we ran them so
848 really check the target's mtime again. Otherwise, assume the target
849 would have been updated. */
851 if (question_flag || just_print_flag || touch_flag)
853 for (i = file->cmds->ncommand_lines; i > 0; --i)
854 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
855 break;
858 /* If there were no commands at all, it's always new. */
860 else if (file->is_target && file->cmds == 0)
861 i = 1;
863 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
865 /* Propagate the change of modification time to all the double-colon
866 entries for this file. */
867 for (f = file->double_colon; f != 0; f = f->prev)
868 f->last_mtime = file->last_mtime;
871 if (ran && file->update_status != -1)
872 /* We actually tried to update FILE, which has
873 updated its also_make's as well (if it worked).
874 If it didn't work, it wouldn't work again for them.
875 So mark them as updated with the same status. */
876 for (d = file->also_make; d != 0; d = d->next)
878 d->file->command_state = cs_finished;
879 d->file->updated = 1;
880 d->file->update_status = file->update_status;
882 if (ran && !d->file->phony)
883 /* Fetch the new modification time.
884 We do this instead of just invalidating the cached time
885 so that a vpath_search can happen. Otherwise, it would
886 never be done because the target is already updated. */
887 (void) f_mtime (d->file, 0);
889 else if (file->update_status == -1)
890 /* Nothing was done for FILE, but it needed nothing done.
891 So mark it now as "succeeded". */
892 file->update_status = 0;
895 /* Check whether another file (whose mtime is THIS_MTIME)
896 needs updating on account of a dependency which is file FILE.
897 If it does, store 1 in *MUST_MAKE_PTR.
898 In the process, update any non-intermediate files
899 that FILE depends on (including FILE itself).
900 Return nonzero if any updating failed. */
902 static int
903 check_dep (struct file *file, unsigned int depth,
904 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
906 struct dep *d;
907 int dep_status = 0;
909 ++depth;
910 start_updating (file);
912 if (file->phony || !file->intermediate)
914 /* If this is a non-intermediate file, update it and record
915 whether it is newer than THIS_MTIME. */
916 FILE_TIMESTAMP mtime;
917 dep_status = update_file (file, depth);
918 check_renamed (file);
919 mtime = file_mtime (file);
920 check_renamed (file);
921 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
922 *must_make_ptr = 1;
924 else
926 /* FILE is an intermediate file. */
927 FILE_TIMESTAMP mtime;
929 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
931 if (try_implicit_rule (file, depth))
932 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
933 else
934 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
935 file->tried_implicit = 1;
937 if (file->cmds == 0 && !file->is_target
938 && default_file != 0 && default_file->cmds != 0)
940 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
941 file->cmds = default_file->cmds;
944 /* If the intermediate file actually exists
945 and is newer, then we should remake from it. */
946 check_renamed (file);
947 mtime = file_mtime (file);
948 check_renamed (file);
949 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
950 *must_make_ptr = 1;
951 /* Otherwise, update all non-intermediate files we depend on,
952 if necessary, and see whether any of them is more
953 recent than the file on whose behalf we are checking. */
954 else
956 struct dep *lastd;
958 lastd = 0;
959 d = file->deps;
960 while (d != 0)
962 int maybe_make;
964 if (is_updating (d->file))
966 error (NILF, _("Circular %s <- %s dependency dropped."),
967 file->name, d->file->name);
968 if (lastd == 0)
970 file->deps = d->next;
971 free ((char *) d);
972 d = file->deps;
974 else
976 lastd->next = d->next;
977 free ((char *) d);
978 d = lastd->next;
980 continue;
983 d->file->parent = file;
984 maybe_make = *must_make_ptr;
985 dep_status |= check_dep (d->file, depth, this_mtime,
986 &maybe_make);
987 if (! d->ignore_mtime)
988 *must_make_ptr = maybe_make;
989 check_renamed (d->file);
990 if (dep_status != 0 && !keep_going_flag)
991 break;
993 if (d->file->command_state == cs_running
994 || d->file->command_state == cs_deps_running)
995 /* Record that some of FILE's deps are still being made.
996 This tells the upper levels to wait on processing it until
997 the commands are finished. */
998 set_command_state (file, cs_deps_running);
1000 lastd = d;
1001 d = d->next;
1006 finish_updating (file);
1007 return dep_status;
1010 /* Touch FILE. Return zero if successful, one if not. */
1012 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1014 static int
1015 touch_file (struct file *file)
1017 if (!silent_flag)
1018 message (0, "touch %s", file->name);
1020 #ifndef NO_ARCHIVES
1021 if (ar_name (file->name))
1022 return ar_touch (file->name);
1023 else
1024 #endif
1026 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1028 if (fd < 0)
1029 TOUCH_ERROR ("touch: open: ");
1030 else
1032 struct stat statbuf;
1033 char buf;
1034 int e;
1036 EINTRLOOP (e, fstat (fd, &statbuf));
1037 if (e < 0)
1038 TOUCH_ERROR ("touch: fstat: ");
1039 /* Rewrite character 0 same as it already is. */
1040 if (read (fd, &buf, 1) < 0)
1041 TOUCH_ERROR ("touch: read: ");
1042 if (lseek (fd, 0L, 0) < 0L)
1043 TOUCH_ERROR ("touch: lseek: ");
1044 if (write (fd, &buf, 1) < 0)
1045 TOUCH_ERROR ("touch: write: ");
1046 /* If file length was 0, we just
1047 changed it, so change it back. */
1048 if (statbuf.st_size == 0)
1050 (void) close (fd);
1051 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1052 if (fd < 0)
1053 TOUCH_ERROR ("touch: open: ");
1055 (void) close (fd);
1059 return 0;
1062 /* Having checked and updated the dependencies of FILE,
1063 do whatever is appropriate to remake FILE itself.
1064 Return the status from executing FILE's commands. */
1066 static void
1067 remake_file (struct file *file)
1069 if (file->cmds == 0)
1071 if (file->phony)
1072 /* Phony target. Pretend it succeeded. */
1073 file->update_status = 0;
1074 else if (file->is_target)
1075 /* This is a nonexistent target file we cannot make.
1076 Pretend it was successfully remade. */
1077 file->update_status = 0;
1078 else
1080 /* This is a dependency file we cannot remake. Fail. */
1081 if (!rebuilding_makefiles || !file->dontcare)
1082 complain (file);
1083 file->update_status = 2;
1086 else
1088 chop_commands (file->cmds);
1090 /* The normal case: start some commands. */
1091 if (!touch_flag || file->cmds->any_recurse)
1093 execute_file_commands (file);
1094 return;
1097 /* This tells notice_finished_file it is ok to touch the file. */
1098 file->update_status = 0;
1101 /* This does the touching under -t. */
1102 notice_finished_file (file);
1105 /* Return the mtime of a file, given a `struct file'.
1106 Caches the time in the struct file to avoid excess stat calls.
1108 If the file is not found, and SEARCH is nonzero, VPATH searching and
1109 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1110 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1111 FILE. */
1113 FILE_TIMESTAMP
1114 f_mtime (struct file *file, int search)
1116 FILE_TIMESTAMP mtime;
1118 /* File's mtime is not known; must get it from the system. */
1120 #ifndef NO_ARCHIVES
1121 if (ar_name (file->name))
1123 /* This file is an archive-member reference. */
1125 char *arname, *memname;
1126 struct file *arfile;
1127 int arname_used = 0;
1128 time_t member_date;
1130 /* Find the archive's name. */
1131 ar_parse_name (file->name, &arname, &memname);
1133 /* Find the modification time of the archive itself.
1134 Also allow for its name to be changed via VPATH search. */
1135 arfile = lookup_file (arname);
1136 if (arfile == 0)
1138 arfile = enter_file (arname);
1139 arname_used = 1;
1141 mtime = f_mtime (arfile, search);
1142 check_renamed (arfile);
1143 if (search && strcmp (arfile->hname, arname))
1145 /* The archive's name has changed.
1146 Change the archive-member reference accordingly. */
1148 char *name;
1149 unsigned int arlen, memlen;
1151 if (!arname_used)
1153 free (arname);
1154 arname_used = 1;
1157 arname = arfile->hname;
1158 arlen = strlen (arname);
1159 memlen = strlen (memname);
1161 /* free (file->name); */
1163 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1164 bcopy (arname, name, arlen);
1165 name[arlen] = '(';
1166 bcopy (memname, name + arlen + 1, memlen);
1167 name[arlen + 1 + memlen] = ')';
1168 name[arlen + 1 + memlen + 1] = '\0';
1170 /* If the archive was found with GPATH, make the change permanent;
1171 otherwise defer it until later. */
1172 if (arfile->name == arfile->hname)
1173 rename_file (file, name);
1174 else
1175 rehash_file (file, name);
1176 check_renamed (file);
1179 if (!arname_used)
1180 free (arname);
1181 free (memname);
1183 file->low_resolution_time = 1;
1185 if (mtime == NONEXISTENT_MTIME)
1186 /* The archive doesn't exist, so its members don't exist either. */
1187 return NONEXISTENT_MTIME;
1189 member_date = ar_member_date (file->hname);
1190 mtime = (member_date == (time_t) -1
1191 ? NONEXISTENT_MTIME
1192 : file_timestamp_cons (file->hname, member_date, 0));
1194 else
1195 #endif
1197 mtime = name_mtime (file->name);
1199 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1201 /* If name_mtime failed, search VPATH. */
1202 char *name = file->name;
1203 if (vpath_search (&name, &mtime)
1204 /* Last resort, is it a library (-lxxx)? */
1205 || (name[0] == '-' && name[1] == 'l'
1206 && library_search (&name, &mtime)))
1208 if (mtime != UNKNOWN_MTIME)
1209 /* vpath_search and library_search store UNKNOWN_MTIME
1210 if they didn't need to do a stat call for their work. */
1211 file->last_mtime = mtime;
1213 /* If we found it in VPATH, see if it's in GPATH too; if so,
1214 change the name right now; if not, defer until after the
1215 dependencies are updated. */
1216 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1218 rename_file (file, name);
1219 check_renamed (file);
1220 return file_mtime (file);
1223 rehash_file (file, name);
1224 check_renamed (file);
1225 mtime = name_mtime (name);
1231 /* Files can have bogus timestamps that nothing newly made will be
1232 "newer" than. Updating their dependents could just result in loops.
1233 So notify the user of the anomaly with a warning.
1235 We only need to do this once, for now. */
1237 if (!clock_skew_detected
1238 && mtime != NONEXISTENT_MTIME
1239 && !file->updated)
1241 static FILE_TIMESTAMP adjusted_now;
1243 FILE_TIMESTAMP adjusted_mtime = mtime;
1245 #if defined(WINDOWS32) || defined(__MSDOS__)
1246 /* Experimentation has shown that FAT filesystems can set file times
1247 up to 3 seconds into the future! Play it safe. */
1249 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1251 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1252 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1253 adjusted_mtime -= adjustment;
1254 #elif defined(__EMX__)
1255 /* FAT filesystems round time to the nearest even second!
1256 Allow for any file (NTFS or FAT) to perhaps suffer from this
1257 brain damage. */
1258 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1259 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1260 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1261 : 0);
1262 #endif
1264 /* If the file's time appears to be in the future, update our
1265 concept of the present and try once more. */
1266 if (adjusted_now < adjusted_mtime)
1268 int resolution;
1269 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1270 adjusted_now = now + (resolution - 1);
1271 if (adjusted_now < adjusted_mtime)
1273 #ifdef NO_FLOAT
1274 error (NILF, _("Warning: File `%s' has modification time in the future"),
1275 file->name);
1276 #else
1277 double from_now =
1278 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1279 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1280 / 1e9));
1281 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1282 file->name, from_now);
1283 #endif
1284 clock_skew_detected = 1;
1290 /* Store the mtime into all the entries for this file. */
1291 if (file->double_colon)
1292 file = file->double_colon;
1296 /* If this file is not implicit but it is intermediate then it was
1297 made so by the .INTERMEDIATE target. If this file has never
1298 been built by us but was found now, it existed before make
1299 started. So, turn off the intermediate bit so make doesn't
1300 delete it, since it didn't create it. */
1301 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1302 && file->command_state == cs_not_started
1303 && !file->tried_implicit && file->intermediate)
1304 file->intermediate = 0;
1306 file->last_mtime = mtime;
1307 file = file->prev;
1309 while (file != 0);
1311 return mtime;
1315 /* Return the mtime of the file or archive-member reference NAME. */
1317 /* First, we check with stat(). If the file does not exist, then we return
1318 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1319 examine each indirection of the symlink and find the newest mtime.
1320 This causes one duplicate stat() when -L is being used, but the code is
1321 much cleaner. */
1323 static FILE_TIMESTAMP
1324 name_mtime (char *name)
1326 FILE_TIMESTAMP mtime;
1327 struct stat st;
1328 int e;
1330 EINTRLOOP (e, stat (name, &st));
1331 if (e == 0)
1332 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1333 else if (errno == ENOENT || errno == ENOTDIR)
1334 mtime = NONEXISTENT_MTIME;
1335 else
1337 perror_with_name ("stat: ", name);
1338 return NONEXISTENT_MTIME;
1341 /* If we get here we either found it, or it doesn't exist.
1342 If it doesn't exist see if we can use a symlink mtime instead. */
1344 #ifdef MAKE_SYMLINKS
1345 #ifndef S_ISLNK
1346 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1347 #endif
1348 if (check_symlink_flag)
1350 PATH_VAR (lpath);
1352 /* Check each symbolic link segment (if any). Find the latest mtime
1353 amongst all of them (and the target file of course).
1354 Note that we have already successfully dereferenced all the links
1355 above. So, if we run into any error trying to lstat(), or
1356 readlink(), or whatever, something bizarre-o happened. Just give up
1357 and use whatever mtime we've already computed at that point. */
1358 strcpy (lpath, name);
1359 while (1)
1361 FILE_TIMESTAMP ltime;
1362 PATH_VAR (lbuf);
1363 long llen;
1364 char *p;
1366 EINTRLOOP (e, lstat (lpath, &st));
1367 if (e)
1369 /* Just take what we have so far. */
1370 if (errno != ENOENT && errno != ENOTDIR)
1371 perror_with_name ("lstat: ", lpath);
1372 break;
1375 /* If this is not a symlink, we're done (we started with the real
1376 file's mtime so we don't need to test it again). */
1377 if (!S_ISLNK (st.st_mode))
1378 break;
1380 /* If this mtime is newer than what we had, keep the new one. */
1381 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1382 if (ltime > mtime)
1383 mtime = ltime;
1385 /* Set up to check the file pointed to by this link. */
1386 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1387 if (llen < 0)
1389 /* Eh? Just take what we have. */
1390 perror_with_name ("readlink: ", lpath);
1391 break;
1393 lbuf[llen] = '\0';
1395 /* If the target is fully-qualified or the source is just a
1396 filename, then the new path is the target. Otherwise it's the
1397 source directory plus the target. */
1398 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1399 strcpy (lpath, lbuf);
1400 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1401 /* Eh? Path too long! Again, just go with what we have. */
1402 break;
1403 else
1404 /* Create the next step in the symlink chain. */
1405 strcpy (p+1, lbuf);
1408 #endif
1410 return mtime;
1414 /* Search for a library file specified as -lLIBNAME, searching for a
1415 suitable library file in the system library directories and the VPATH
1416 directories. */
1418 static int
1419 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1421 static char *dirs[] =
1423 #ifndef _AMIGA
1424 "/lib",
1425 "/usr/lib",
1426 #endif
1427 #if defined(WINDOWS32) && !defined(LIBDIR)
1429 * This is completely up to the user at product install time. Just define
1430 * a placeholder.
1432 #define LIBDIR "."
1433 #endif
1434 LIBDIR, /* Defined by configuration. */
1438 static char *libpatterns = NULL;
1440 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1441 FILE_TIMESTAMP mtime;
1443 /* Loop variables for the libpatterns value. */
1444 char *p, *p2;
1445 unsigned int len;
1447 char *file, **dp;
1449 /* If we don't have libpatterns, get it. */
1450 if (!libpatterns)
1452 int save = warn_undefined_variables_flag;
1453 warn_undefined_variables_flag = 0;
1455 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1457 warn_undefined_variables_flag = save;
1460 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1461 p2 = libpatterns;
1462 while ((p = find_next_token (&p2, &len)) != 0)
1464 static char *buf = NULL;
1465 static unsigned int buflen = 0;
1466 static int libdir_maxlen = -1;
1467 char *libbuf = variable_expand ("");
1469 /* Expand the pattern using LIBNAME as a replacement. */
1471 char c = p[len];
1472 char *p3, *p4;
1474 p[len] = '\0';
1475 p3 = find_percent (p);
1476 if (!p3)
1478 /* Give a warning if there is no pattern, then remove the
1479 pattern so it's ignored next time. */
1480 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1481 for (; len; --len, ++p)
1482 *p = ' ';
1483 *p = c;
1484 continue;
1486 p4 = variable_buffer_output (libbuf, p, p3-p);
1487 p4 = variable_buffer_output (p4, libname, strlen (libname));
1488 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1489 p[len] = c;
1492 /* Look first for `libNAME.a' in the current directory. */
1493 mtime = name_mtime (libbuf);
1494 if (mtime != NONEXISTENT_MTIME)
1496 *lib = xstrdup (libbuf);
1497 if (mtime_ptr != 0)
1498 *mtime_ptr = mtime;
1499 return 1;
1502 /* Now try VPATH search on that. */
1504 file = libbuf;
1505 if (vpath_search (&file, mtime_ptr))
1507 *lib = file;
1508 return 1;
1511 /* Now try the standard set of directories. */
1513 if (!buflen)
1515 for (dp = dirs; *dp != 0; ++dp)
1517 int l = strlen (*dp);
1518 if (l > libdir_maxlen)
1519 libdir_maxlen = l;
1521 buflen = strlen (libbuf);
1522 buf = xmalloc(libdir_maxlen + buflen + 2);
1524 else if (buflen < strlen (libbuf))
1526 buflen = strlen (libbuf);
1527 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1530 for (dp = dirs; *dp != 0; ++dp)
1532 sprintf (buf, "%s/%s", *dp, libbuf);
1533 mtime = name_mtime (buf);
1534 if (mtime != NONEXISTENT_MTIME)
1536 *lib = xstrdup (buf);
1537 if (mtime_ptr != 0)
1538 *mtime_ptr = mtime;
1539 return 1;
1544 return 0;