Fix for bug #1276: Handle SHELL according to POSIX requirements.
[make/kirr.git] / remake.c
blob70acad3361d5d0ede502b91b3f570194b3e491cc
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "dep.h"
26 #include "variable.h"
27 #include "debug.h"
29 #include <assert.h>
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #else
34 #include <sys/file.h>
35 #endif
37 #ifdef VMS
38 #include <starlet.h>
39 #endif
40 #ifdef WINDOWS32
41 #include <io.h>
42 #endif
44 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
47 /* The test for circular dependencies is based on the 'updating' bit in
48 `struct file'. However, double colon targets have seperate `struct
49 file's; make sure we always use the base of the double colon chain. */
51 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 1)
53 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating = 0)
55 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
56 ->updating)
59 /* Incremented when a command is started (under -n, when one would be). */
60 unsigned int commands_started = 0;
62 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
63 static unsigned int considered;
65 static int update_file PARAMS ((struct file *file, unsigned int depth));
66 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
67 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
68 static int touch_file PARAMS ((struct file *file));
69 static void remake_file PARAMS ((struct file *file));
70 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
71 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
74 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
75 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
77 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
78 and -n should be disabled for them unless they were also command-line
79 targets, and we should only make one goal at a time and return as soon as
80 one goal whose `changed' member is nonzero is successfully made. */
82 int
83 update_goal_chain (struct dep *goals)
85 int t = touch_flag, q = question_flag, n = just_print_flag;
86 unsigned int j = job_slots;
87 int status = -1;
89 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
90 : file_mtime (file))
92 /* Duplicate the chain so we can remove things from it. */
94 goals = copy_dep_chain (goals);
97 /* Clear the `changed' flag of each goal in the chain.
98 We will use the flag below to notice when any commands
99 have actually been run for a target. When no commands
100 have been run, we give an "up to date" diagnostic. */
102 struct dep *g;
103 for (g = goals; g != 0; g = g->next)
104 g->changed = 0;
107 /* All files start with the considered bit 0, so the global value is 1. */
108 considered = 1;
110 /* Update all the goals until they are all finished. */
112 while (goals != 0)
114 register struct dep *g, *lastgoal;
116 /* Start jobs that are waiting for the load to go down. */
118 start_waiting_jobs ();
120 /* Wait for a child to die. */
122 reap_children (1, 0);
124 lastgoal = 0;
125 g = goals;
126 while (g != 0)
128 /* Iterate over all double-colon entries for this file. */
129 struct file *file;
130 int stop = 0, any_not_updated = 0;
132 for (file = g->file->double_colon ? g->file->double_colon : g->file;
133 file != NULL;
134 file = file->prev)
136 unsigned int ocommands_started;
137 int x;
138 check_renamed (file);
139 if (rebuilding_makefiles)
141 if (file->cmd_target)
143 touch_flag = t;
144 question_flag = q;
145 just_print_flag = n;
147 else
148 touch_flag = question_flag = just_print_flag = 0;
151 /* Save the old value of `commands_started' so we can compare
152 later. It will be incremented when any commands are
153 actually run. */
154 ocommands_started = commands_started;
156 x = update_file (file, rebuilding_makefiles ? 1 : 0);
157 check_renamed (file);
159 /* Set the goal's `changed' flag if any commands were started
160 by calling update_file above. We check this flag below to
161 decide when to give an "up to date" diagnostic. */
162 g->changed += commands_started - ocommands_started;
164 /* If we updated a file and STATUS was not already 1, set it to
165 1 if updating failed, or to 0 if updating succeeded. Leave
166 STATUS as it is if no updating was done. */
168 stop = 0;
169 if ((x != 0 || file->updated) && status < 1)
171 if (file->update_status != 0)
173 /* Updating failed, or -q triggered. The STATUS value
174 tells our caller which. */
175 status = file->update_status;
176 /* If -q just triggered, stop immediately. It doesn't
177 matter how much more we run, since we already know
178 the answer to return. */
179 stop = (!keep_going_flag && !question_flag
180 && !rebuilding_makefiles);
182 else
184 FILE_TIMESTAMP mtime = MTIME (file);
185 check_renamed (file);
187 if (file->updated && g->changed &&
188 mtime != file->mtime_before_update)
190 /* Updating was done. If this is a makefile and
191 just_print_flag or question_flag is set (meaning
192 -n or -q was given and this file was specified
193 as a command-line target), don't change STATUS.
194 If STATUS is changed, we will get re-exec'd, and
195 enter an infinite loop. */
196 if (!rebuilding_makefiles
197 || (!just_print_flag && !question_flag))
198 status = 0;
199 if (rebuilding_makefiles && file->dontcare)
200 /* This is a default makefile; stop remaking. */
201 stop = 1;
206 /* Keep track if any double-colon entry is not finished.
207 When they are all finished, the goal is finished. */
208 any_not_updated |= !file->updated;
210 if (stop)
211 break;
214 /* Reset FILE since it is null at the end of the loop. */
215 file = g->file;
217 if (stop || !any_not_updated)
219 /* If we have found nothing whatever to do for the goal,
220 print a message saying nothing needs doing. */
222 if (!rebuilding_makefiles
223 /* If the update_status is zero, we updated successfully
224 or not at all. G->changed will have been set above if
225 any commands were actually started for this goal. */
226 && file->update_status == 0 && !g->changed
227 /* Never give a message under -s or -q. */
228 && !silent_flag && !question_flag)
229 message (1, ((file->phony || file->cmds == 0)
230 ? _("Nothing to be done for `%s'.")
231 : _("`%s' is up to date.")),
232 file->name);
234 /* This goal is finished. Remove it from the chain. */
235 if (lastgoal == 0)
236 goals = g->next;
237 else
238 lastgoal->next = g->next;
240 /* Free the storage. */
241 free ((char *) g);
243 g = lastgoal == 0 ? goals : lastgoal->next;
245 if (stop)
246 break;
248 else
250 lastgoal = g;
251 g = g->next;
255 /* If we reached the end of the dependency graph toggle the considered
256 flag for the next pass. */
257 if (g == 0)
258 considered = !considered;
261 if (rebuilding_makefiles)
263 touch_flag = t;
264 question_flag = q;
265 just_print_flag = n;
266 job_slots = j;
268 return status;
271 /* If FILE is not up to date, execute the commands for it.
272 Return 0 if successful, 1 if unsuccessful;
273 but with some flag settings, just call `exit' if unsuccessful.
275 DEPTH is the depth in recursions of this function.
276 We increment it during the consideration of our dependencies,
277 then decrement it again after finding out whether this file
278 is out of date.
280 If there are multiple double-colon entries for FILE,
281 each is considered in turn. */
283 static int
284 update_file (struct file *file, unsigned int depth)
286 register int status = 0;
287 register struct file *f;
289 f = file->double_colon ? file->double_colon : file;
291 /* Prune the dependency graph: if we've already been here on _this_
292 pass through the dependency graph, we don't have to go any further.
293 We won't reap_children until we start the next pass, so no state
294 change is possible below here until then. */
295 if (f->considered == considered)
297 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
298 return f->command_state == cs_finished ? f->update_status : 0;
301 /* This loop runs until we start commands for a double colon rule, or until
302 the chain is exhausted. */
303 for (; f != 0; f = f->prev)
305 f->considered = considered;
307 status |= update_file_1 (f, depth);
308 check_renamed (f);
310 if (status != 0 && !keep_going_flag)
311 break;
313 if (f->command_state == cs_running
314 || f->command_state == cs_deps_running)
316 /* Don't run the other :: rules for this
317 file until this rule is finished. */
318 status = 0;
319 break;
323 /* Process the remaining rules in the double colon chain so they're marked
324 considered. Start their prerequisites, too. */
325 if (file->double_colon)
326 for (; f != 0 ; f = f->prev)
328 struct dep *d;
330 f->considered = considered;
332 for (d = f->deps; d != 0; d = d->next)
333 status |= update_file (d->file, depth + 1);
336 return status;
339 /* Show a message stating the target failed to build. */
341 static void
342 complain (const struct file *file)
344 const char *msg_noparent
345 = _("%sNo rule to make target `%s'%s");
346 const char *msg_parent
347 = _("%sNo rule to make target `%s', needed by `%s'%s");
349 if (!keep_going_flag)
351 if (file->parent == 0)
352 fatal (NILF, msg_noparent, "", file->name, "");
354 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
357 if (file->parent == 0)
358 error (NILF, msg_noparent, "*** ", file->name, ".");
359 else
360 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
363 /* Consider a single `struct file' and update it as appropriate. */
365 static int
366 update_file_1 (struct file *file, unsigned int depth)
368 register FILE_TIMESTAMP this_mtime;
369 int noexist, must_make, deps_changed;
370 int dep_status = 0;
371 register struct dep *d, *lastd;
372 int running = 0;
374 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
376 if (file->updated)
378 if (file->update_status > 0)
380 DBF (DB_VERBOSE,
381 _("Recently tried and failed to update file `%s'.\n"));
383 /* If the file we tried to make is marked dontcare then no message
384 was printed about it when it failed during the makefile rebuild.
385 If we're trying to build it again in the normal rebuild, print a
386 message now. */
387 if (file->dontcare && !rebuilding_makefiles)
389 file->dontcare = 0;
390 complain (file);
393 return file->update_status;
396 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
397 return 0;
400 switch (file->command_state)
402 case cs_not_started:
403 case cs_deps_running:
404 break;
405 case cs_running:
406 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
407 return 0;
408 case cs_finished:
409 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
410 return file->update_status;
411 default:
412 abort ();
415 ++depth;
417 /* Notice recursive update of the same file. */
418 start_updating (file);
420 /* Looking at the file's modtime beforehand allows the possibility
421 that its name may be changed by a VPATH search, and thus it may
422 not need an implicit rule. If this were not done, the file
423 might get implicit commands that apply to its initial name, only
424 to have that name replaced with another found by VPATH search. */
426 this_mtime = file_mtime (file);
427 check_renamed (file);
428 noexist = this_mtime == NONEXISTENT_MTIME;
429 if (noexist)
430 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
431 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
432 && file->low_resolution_time)
434 /* Avoid spurious rebuilds due to low resolution time stamps. */
435 int ns = FILE_TIMESTAMP_NS (this_mtime);
436 if (ns != 0)
437 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
438 file->name);
439 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
442 must_make = noexist;
444 /* If file was specified as a target with no commands,
445 come up with some default commands. */
447 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
449 if (try_implicit_rule (file, depth))
450 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
451 else
452 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
453 file->tried_implicit = 1;
455 if (file->cmds == 0 && !file->is_target
456 && default_file != 0 && default_file->cmds != 0)
458 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
459 file->cmds = default_file->cmds;
462 /* Update all non-intermediate files we depend on, if necessary,
463 and see whether any of them is more recent than this file. */
465 lastd = 0;
466 d = file->deps;
467 while (d != 0)
469 FILE_TIMESTAMP mtime;
470 int maybe_make;
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 d->changed = file_mtime (d->file) != mtime;
534 lastd = d;
535 d = d->next;
538 /* Now we know whether this target needs updating.
539 If it does, update all the intermediate files we depend on. */
541 if (must_make || always_make_flag)
543 for (d = file->deps; d != 0; d = d->next)
544 if (d->file->intermediate)
546 int dontcare = 0;
548 FILE_TIMESTAMP mtime = file_mtime (d->file);
549 check_renamed (d->file);
550 d->file->parent = file;
552 /* Inherit dontcare flag from our parent. */
553 if (rebuilding_makefiles)
555 dontcare = d->file->dontcare;
556 d->file->dontcare = file->dontcare;
560 dep_status |= update_file (d->file, depth);
562 /* Restore original dontcare flag. */
563 if (rebuilding_makefiles)
564 d->file->dontcare = dontcare;
566 check_renamed (d->file);
569 register struct file *f = d->file;
570 if (f->double_colon)
571 f = f->double_colon;
574 running |= (f->command_state == cs_running
575 || f->command_state == cs_deps_running);
576 f = f->prev;
578 while (f != 0);
581 if (dep_status != 0 && !keep_going_flag)
582 break;
584 if (!running)
585 d->changed = ((file->phony && file->cmds != 0)
586 || file_mtime (d->file) != mtime);
590 finish_updating (file);
592 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
594 if (running)
596 set_command_state (file, cs_deps_running);
597 --depth;
598 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
599 return 0;
602 /* If any dependency failed, give up now. */
604 if (dep_status != 0)
606 file->update_status = dep_status;
607 notice_finished_file (file);
609 --depth;
611 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
613 if (depth == 0 && keep_going_flag
614 && !just_print_flag && !question_flag)
615 error (NILF,
616 _("Target `%s' not remade because of errors."), file->name);
618 return dep_status;
621 if (file->command_state == cs_deps_running)
622 /* The commands for some deps were running on the last iteration, but
623 they have finished now. Reset the command_state to not_started to
624 simplify later bookkeeping. It is important that we do this only
625 when the prior state was cs_deps_running, because that prior state
626 was definitely propagated to FILE's also_make's by set_command_state
627 (called above), but in another state an also_make may have
628 independently changed to finished state, and we would confuse that
629 file's bookkeeping (updated, but not_started is bogus state). */
630 set_command_state (file, cs_not_started);
632 /* Now record which prerequisites are more
633 recent than this file, so we can define $?. */
635 deps_changed = 0;
636 for (d = file->deps; d != 0; d = d->next)
638 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
639 check_renamed (d->file);
641 if (! d->ignore_mtime)
643 #if 1
644 /* %%% In version 4, remove this code completely to
645 implement not remaking deps if their deps are newer
646 than their parents. */
647 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
648 /* We must remake if this dep does not
649 exist and is not intermediate. */
650 must_make = 1;
651 #endif
653 /* Set DEPS_CHANGED if this dep actually changed. */
654 deps_changed |= d->changed;
657 /* Set D->changed if either this dep actually changed,
658 or its dependent, FILE, is older or does not exist. */
659 d->changed |= noexist || d_mtime > this_mtime;
661 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
663 const char *fmt = 0;
665 if (d->ignore_mtime)
667 if (ISDB (DB_VERBOSE))
668 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
670 else if (d_mtime == NONEXISTENT_MTIME)
672 if (ISDB (DB_BASIC))
673 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
675 else if (d->changed)
677 if (ISDB (DB_BASIC))
678 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
680 else if (ISDB (DB_VERBOSE))
681 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
683 if (fmt)
685 print_spaces (depth);
686 printf (fmt, dep_name (d), file->name);
687 fflush (stdout);
692 /* Here depth returns to the value it had when we were called. */
693 depth--;
695 if (file->double_colon && file->deps == 0)
697 must_make = 1;
698 DBF (DB_BASIC,
699 _("Target `%s' is double-colon and has no prerequisites.\n"));
701 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
702 && !always_make_flag)
704 must_make = 0;
705 DBF (DB_VERBOSE,
706 _("No commands for `%s' and no prerequisites actually changed.\n"));
708 else if (!must_make && file->cmds != 0 && always_make_flag)
710 must_make = 1;
711 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
714 if (!must_make)
716 if (ISDB (DB_VERBOSE))
718 print_spaces (depth);
719 printf (_("No need to remake target `%s'"), file->name);
720 if (!streq (file->name, file->hname))
721 printf (_("; using VPATH name `%s'"), file->hname);
722 puts (".");
723 fflush (stdout);
726 notice_finished_file (file);
728 /* Since we don't need to remake the file, convert it to use the
729 VPATH filename if we found one. hfile will be either the
730 local name if no VPATH or the VPATH name if one was found. */
732 while (file)
734 file->name = file->hname;
735 file = file->prev;
738 return 0;
741 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
743 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
744 VPATH. */
745 if (!streq(file->name, file->hname))
747 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
748 file->ignore_vpath = 1;
751 /* Now, take appropriate actions to remake the file. */
752 remake_file (file);
754 if (file->command_state != cs_finished)
756 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
757 return 0;
760 switch (file->update_status)
762 case 2:
763 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
764 break;
765 case 0:
766 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
767 break;
768 case 1:
769 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
770 break;
771 default:
772 assert (file->update_status >= 0 && file->update_status <= 2);
773 break;
776 file->updated = 1;
777 return file->update_status;
780 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
781 files listed in its `also_make' member. Under -t, this function also
782 touches FILE.
784 On return, FILE->update_status will no longer be -1 if it was. */
786 void
787 notice_finished_file (struct file *file)
789 struct dep *d;
790 int ran = file->command_state == cs_running;
791 int touched = 0;
793 file->command_state = cs_finished;
794 file->updated = 1;
796 if (touch_flag
797 /* The update status will be:
798 -1 if this target was not remade;
799 0 if 0 or more commands (+ or ${MAKE}) were run and won;
800 1 if some commands were run and lost.
801 We touch the target if it has commands which either were not run
802 or won when they ran (i.e. status is 0). */
803 && file->update_status == 0)
805 if (file->cmds != 0 && file->cmds->any_recurse)
807 /* If all the command lines were recursive,
808 we don't want to do the touching. */
809 unsigned int i;
810 for (i = 0; i < file->cmds->ncommand_lines; ++i)
811 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
812 goto have_nonrecursing;
814 else
816 have_nonrecursing:
817 if (file->phony)
818 file->update_status = 0;
819 /* According to POSIX, -t doesn't affect targets with no cmds. */
820 else if (file->cmds != 0)
822 /* Should set file's modification date and do nothing else. */
823 file->update_status = touch_file (file);
825 /* Pretend we ran a real touch command, to suppress the
826 "`foo' is up to date" message. */
827 commands_started++;
829 /* Request for the timestamp to be updated (and distributed
830 to the double-colon entries). Simply setting ran=1 would
831 almost have done the trick, but messes up with the also_make
832 updating logic below. */
833 touched = 1;
838 if (file->mtime_before_update == UNKNOWN_MTIME)
839 file->mtime_before_update = file->last_mtime;
841 if ((ran && !file->phony) || touched)
843 struct file *f;
844 int i = 0;
846 /* If -n, -t, or -q and all the commands are recursive, we ran them so
847 really check the target's mtime again. Otherwise, assume the target
848 would have been updated. */
850 if (question_flag || just_print_flag || touch_flag)
852 for (i = file->cmds->ncommand_lines; i > 0; --i)
853 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
854 break;
857 /* If there were no commands at all, it's always new. */
859 else if (file->is_target && file->cmds == 0)
860 i = 1;
862 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
864 /* Propagate the change of modification time to all the double-colon
865 entries for this file. */
866 for (f = file->double_colon; f != 0; f = f->prev)
867 f->last_mtime = file->last_mtime;
870 if (ran && file->update_status != -1)
871 /* We actually tried to update FILE, which has
872 updated its also_make's as well (if it worked).
873 If it didn't work, it wouldn't work again for them.
874 So mark them as updated with the same status. */
875 for (d = file->also_make; d != 0; d = d->next)
877 d->file->command_state = cs_finished;
878 d->file->updated = 1;
879 d->file->update_status = file->update_status;
881 if (ran && !d->file->phony)
882 /* Fetch the new modification time.
883 We do this instead of just invalidating the cached time
884 so that a vpath_search can happen. Otherwise, it would
885 never be done because the target is already updated. */
886 (void) f_mtime (d->file, 0);
888 else if (file->update_status == -1)
889 /* Nothing was done for FILE, but it needed nothing done.
890 So mark it now as "succeeded". */
891 file->update_status = 0;
894 /* Check whether another file (whose mtime is THIS_MTIME)
895 needs updating on account of a dependency which is file FILE.
896 If it does, store 1 in *MUST_MAKE_PTR.
897 In the process, update any non-intermediate files
898 that FILE depends on (including FILE itself).
899 Return nonzero if any updating failed. */
901 static int
902 check_dep (struct file *file, unsigned int depth,
903 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
905 struct dep *d;
906 int dep_status = 0;
908 ++depth;
909 start_updating (file);
911 if (!file->intermediate)
912 /* If this is a non-intermediate file, update it and record
913 whether it is newer than THIS_MTIME. */
915 FILE_TIMESTAMP mtime;
916 dep_status = update_file (file, depth);
917 check_renamed (file);
918 mtime = file_mtime (file);
919 check_renamed (file);
920 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
921 *must_make_ptr = 1;
923 else
925 /* FILE is an intermediate file. */
926 FILE_TIMESTAMP mtime;
928 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
930 if (try_implicit_rule (file, depth))
931 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
932 else
933 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
934 file->tried_implicit = 1;
936 if (file->cmds == 0 && !file->is_target
937 && default_file != 0 && default_file->cmds != 0)
939 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
940 file->cmds = default_file->cmds;
943 /* If the intermediate file actually exists
944 and is newer, then we should remake from it. */
945 check_renamed (file);
946 mtime = file_mtime (file);
947 check_renamed (file);
948 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
949 *must_make_ptr = 1;
950 /* Otherwise, update all non-intermediate files we depend on,
951 if necessary, and see whether any of them is more
952 recent than the file on whose behalf we are checking. */
953 else
955 struct dep *lastd;
957 lastd = 0;
958 d = file->deps;
959 while (d != 0)
961 int maybe_make;
963 if (is_updating (d->file))
965 error (NILF, _("Circular %s <- %s dependency dropped."),
966 file->name, d->file->name);
967 if (lastd == 0)
969 file->deps = d->next;
970 free ((char *) d);
971 d = file->deps;
973 else
975 lastd->next = d->next;
976 free ((char *) d);
977 d = lastd->next;
979 continue;
982 d->file->parent = file;
983 maybe_make = *must_make_ptr;
984 dep_status |= check_dep (d->file, depth, this_mtime,
985 &maybe_make);
986 if (! d->ignore_mtime)
987 *must_make_ptr = maybe_make;
988 check_renamed (d->file);
989 if (dep_status != 0 && !keep_going_flag)
990 break;
992 if (d->file->command_state == cs_running
993 || d->file->command_state == cs_deps_running)
994 /* Record that some of FILE's deps are still being made.
995 This tells the upper levels to wait on processing it until
996 the commands are finished. */
997 set_command_state (file, cs_deps_running);
999 lastd = d;
1000 d = d->next;
1005 finish_updating (file);
1006 return dep_status;
1009 /* Touch FILE. Return zero if successful, one if not. */
1011 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1013 static int
1014 touch_file (struct file *file)
1016 if (!silent_flag)
1017 message (0, "touch %s", file->name);
1019 #ifndef NO_ARCHIVES
1020 if (ar_name (file->name))
1021 return ar_touch (file->name);
1022 else
1023 #endif
1025 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1027 if (fd < 0)
1028 TOUCH_ERROR ("touch: open: ");
1029 else
1031 struct stat statbuf;
1032 char buf;
1033 int e;
1035 EINTRLOOP (e, fstat (fd, &statbuf));
1036 if (e < 0)
1037 TOUCH_ERROR ("touch: fstat: ");
1038 /* Rewrite character 0 same as it already is. */
1039 if (read (fd, &buf, 1) < 0)
1040 TOUCH_ERROR ("touch: read: ");
1041 if (lseek (fd, 0L, 0) < 0L)
1042 TOUCH_ERROR ("touch: lseek: ");
1043 if (write (fd, &buf, 1) < 0)
1044 TOUCH_ERROR ("touch: write: ");
1045 /* If file length was 0, we just
1046 changed it, so change it back. */
1047 if (statbuf.st_size == 0)
1049 (void) close (fd);
1050 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1051 if (fd < 0)
1052 TOUCH_ERROR ("touch: open: ");
1054 (void) close (fd);
1058 return 0;
1061 /* Having checked and updated the dependencies of FILE,
1062 do whatever is appropriate to remake FILE itself.
1063 Return the status from executing FILE's commands. */
1065 static void
1066 remake_file (struct file *file)
1068 if (file->cmds == 0)
1070 if (file->phony)
1071 /* Phony target. Pretend it succeeded. */
1072 file->update_status = 0;
1073 else if (file->is_target)
1074 /* This is a nonexistent target file we cannot make.
1075 Pretend it was successfully remade. */
1076 file->update_status = 0;
1077 else
1079 /* This is a dependency file we cannot remake. Fail. */
1080 if (!rebuilding_makefiles || !file->dontcare)
1081 complain (file);
1082 file->update_status = 2;
1085 else
1087 chop_commands (file->cmds);
1089 /* The normal case: start some commands. */
1090 if (!touch_flag || file->cmds->any_recurse)
1092 execute_file_commands (file);
1093 return;
1096 /* This tells notice_finished_file it is ok to touch the file. */
1097 file->update_status = 0;
1100 /* This does the touching under -t. */
1101 notice_finished_file (file);
1104 /* Return the mtime of a file, given a `struct file'.
1105 Caches the time in the struct file to avoid excess stat calls.
1107 If the file is not found, and SEARCH is nonzero, VPATH searching and
1108 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1109 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1110 FILE. */
1112 FILE_TIMESTAMP
1113 f_mtime (struct file *file, int search)
1115 FILE_TIMESTAMP mtime;
1117 /* File's mtime is not known; must get it from the system. */
1119 #ifndef NO_ARCHIVES
1120 if (ar_name (file->name))
1122 /* This file is an archive-member reference. */
1124 char *arname, *memname;
1125 struct file *arfile;
1126 int arname_used = 0;
1127 time_t member_date;
1129 /* Find the archive's name. */
1130 ar_parse_name (file->name, &arname, &memname);
1132 /* Find the modification time of the archive itself.
1133 Also allow for its name to be changed via VPATH search. */
1134 arfile = lookup_file (arname);
1135 if (arfile == 0)
1137 arfile = enter_file (arname);
1138 arname_used = 1;
1140 mtime = f_mtime (arfile, search);
1141 check_renamed (arfile);
1142 if (search && strcmp (arfile->hname, arname))
1144 /* The archive's name has changed.
1145 Change the archive-member reference accordingly. */
1147 char *name;
1148 unsigned int arlen, memlen;
1150 if (!arname_used)
1152 free (arname);
1153 arname_used = 1;
1156 arname = arfile->hname;
1157 arlen = strlen (arname);
1158 memlen = strlen (memname);
1160 /* free (file->name); */
1162 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1163 bcopy (arname, name, arlen);
1164 name[arlen] = '(';
1165 bcopy (memname, name + arlen + 1, memlen);
1166 name[arlen + 1 + memlen] = ')';
1167 name[arlen + 1 + memlen + 1] = '\0';
1169 /* If the archive was found with GPATH, make the change permanent;
1170 otherwise defer it until later. */
1171 if (arfile->name == arfile->hname)
1172 rename_file (file, name);
1173 else
1174 rehash_file (file, name);
1175 check_renamed (file);
1178 if (!arname_used)
1179 free (arname);
1180 free (memname);
1182 file->low_resolution_time = 1;
1184 if (mtime == NONEXISTENT_MTIME)
1185 /* The archive doesn't exist, so its members don't exist either. */
1186 return NONEXISTENT_MTIME;
1188 member_date = ar_member_date (file->hname);
1189 mtime = (member_date == (time_t) -1
1190 ? NONEXISTENT_MTIME
1191 : file_timestamp_cons (file->hname, member_date, 0));
1193 else
1194 #endif
1196 mtime = name_mtime (file->name);
1198 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1200 /* If name_mtime failed, search VPATH. */
1201 char *name = file->name;
1202 if (vpath_search (&name, &mtime)
1203 /* Last resort, is it a library (-lxxx)? */
1204 || (name[0] == '-' && name[1] == 'l'
1205 && library_search (&name, &mtime)))
1207 if (mtime != UNKNOWN_MTIME)
1208 /* vpath_search and library_search store UNKNOWN_MTIME
1209 if they didn't need to do a stat call for their work. */
1210 file->last_mtime = mtime;
1212 /* If we found it in VPATH, see if it's in GPATH too; if so,
1213 change the name right now; if not, defer until after the
1214 dependencies are updated. */
1215 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1217 rename_file (file, name);
1218 check_renamed (file);
1219 return file_mtime (file);
1222 rehash_file (file, name);
1223 check_renamed (file);
1224 mtime = name_mtime (name);
1230 /* Files can have bogus timestamps that nothing newly made will be
1231 "newer" than. Updating their dependents could just result in loops.
1232 So notify the user of the anomaly with a warning.
1234 We only need to do this once, for now. */
1236 if (!clock_skew_detected
1237 && mtime != NONEXISTENT_MTIME
1238 && !file->updated)
1240 static FILE_TIMESTAMP adjusted_now;
1242 FILE_TIMESTAMP adjusted_mtime = mtime;
1244 #if defined(WINDOWS32) || defined(__MSDOS__)
1245 /* Experimentation has shown that FAT filesystems can set file times
1246 up to 3 seconds into the future! Play it safe. */
1248 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1250 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1251 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1252 adjusted_mtime -= adjustment;
1253 #elif defined(__EMX__)
1254 /* FAT filesystems round time to the nearest even second!
1255 Allow for any file (NTFS or FAT) to perhaps suffer from this
1256 brain damage. */
1257 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1258 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1259 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1260 : 0);
1261 #endif
1263 /* If the file's time appears to be in the future, update our
1264 concept of the present and try once more. */
1265 if (adjusted_now < adjusted_mtime)
1267 int resolution;
1268 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1269 adjusted_now = now + (resolution - 1);
1270 if (adjusted_now < adjusted_mtime)
1272 #ifdef NO_FLOAT
1273 error (NILF, _("Warning: File `%s' has modification time in the future"),
1274 file->name);
1275 #else
1276 double from_now =
1277 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1278 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1279 / 1e9));
1280 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1281 file->name, from_now);
1282 #endif
1283 clock_skew_detected = 1;
1289 /* Store the mtime into all the entries for this file. */
1290 if (file->double_colon)
1291 file = file->double_colon;
1295 /* If this file is not implicit but it is intermediate then it was
1296 made so by the .INTERMEDIATE target. If this file has never
1297 been built by us but was found now, it existed before make
1298 started. So, turn off the intermediate bit so make doesn't
1299 delete it, since it didn't create it. */
1300 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1301 && file->command_state == cs_not_started
1302 && !file->tried_implicit && file->intermediate)
1303 file->intermediate = 0;
1305 file->last_mtime = mtime;
1306 file = file->prev;
1308 while (file != 0);
1310 return mtime;
1314 /* Return the mtime of the file or archive-member reference NAME. */
1316 static FILE_TIMESTAMP
1317 name_mtime (char *name)
1319 struct stat st;
1320 int e;
1322 EINTRLOOP (e, stat (name, &st));
1323 if (e != 0)
1325 if (errno != ENOENT && errno != ENOTDIR)
1326 perror_with_name ("stat:", name);
1327 return NONEXISTENT_MTIME;
1330 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1334 /* Search for a library file specified as -lLIBNAME, searching for a
1335 suitable library file in the system library directories and the VPATH
1336 directories. */
1338 static int
1339 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1341 static char *dirs[] =
1343 #ifndef _AMIGA
1344 "/lib",
1345 "/usr/lib",
1346 #endif
1347 #if defined(WINDOWS32) && !defined(LIBDIR)
1349 * This is completely up to the user at product install time. Just define
1350 * a placeholder.
1352 #define LIBDIR "."
1353 #endif
1354 LIBDIR, /* Defined by configuration. */
1358 static char *libpatterns = NULL;
1360 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1361 FILE_TIMESTAMP mtime;
1363 /* Loop variables for the libpatterns value. */
1364 char *p, *p2;
1365 unsigned int len;
1367 char *file, **dp;
1369 /* If we don't have libpatterns, get it. */
1370 if (!libpatterns)
1372 int save = warn_undefined_variables_flag;
1373 warn_undefined_variables_flag = 0;
1375 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1377 warn_undefined_variables_flag = save;
1380 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1381 p2 = libpatterns;
1382 while ((p = find_next_token (&p2, &len)) != 0)
1384 static char *buf = NULL;
1385 static unsigned int buflen = 0;
1386 static int libdir_maxlen = -1;
1387 char *libbuf = variable_expand ("");
1389 /* Expand the pattern using LIBNAME as a replacement. */
1391 char c = p[len];
1392 char *p3, *p4;
1394 p[len] = '\0';
1395 p3 = find_percent (p);
1396 if (!p3)
1398 /* Give a warning if there is no pattern, then remove the
1399 pattern so it's ignored next time. */
1400 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1401 for (; len; --len, ++p)
1402 *p = ' ';
1403 *p = c;
1404 continue;
1406 p4 = variable_buffer_output (libbuf, p, p3-p);
1407 p4 = variable_buffer_output (p4, libname, strlen (libname));
1408 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1409 p[len] = c;
1412 /* Look first for `libNAME.a' in the current directory. */
1413 mtime = name_mtime (libbuf);
1414 if (mtime != NONEXISTENT_MTIME)
1416 *lib = xstrdup (libbuf);
1417 if (mtime_ptr != 0)
1418 *mtime_ptr = mtime;
1419 return 1;
1422 /* Now try VPATH search on that. */
1424 file = libbuf;
1425 if (vpath_search (&file, mtime_ptr))
1427 *lib = file;
1428 return 1;
1431 /* Now try the standard set of directories. */
1433 if (!buflen)
1435 for (dp = dirs; *dp != 0; ++dp)
1437 int l = strlen (*dp);
1438 if (l > libdir_maxlen)
1439 libdir_maxlen = l;
1441 buflen = strlen (libbuf);
1442 buf = xmalloc(libdir_maxlen + buflen + 2);
1444 else if (buflen < strlen (libbuf))
1446 buflen = strlen (libbuf);
1447 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1450 for (dp = dirs; *dp != 0; ++dp)
1452 sprintf (buf, "%s/%s", *dp, libbuf);
1453 mtime = name_mtime (buf);
1454 if (mtime != NONEXISTENT_MTIME)
1456 *lib = xstrdup (buf);
1457 if (mtime_ptr != 0)
1458 *mtime_ptr = mtime;
1459 return 1;
1464 return 0;