Fix bug #2846.
[make.git] / remake.c
bloba13a9a436e5e27088c984b20367f19fe6c6082be
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.
76 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
77 be disabled for them unless they were also command-line targets, and we
78 should only make one goal at a time and return as soon as one goal whose
79 `changed' member is nonzero is successfully made. */
81 int
82 update_goal_chain (struct dep *goals, int makefiles)
84 int t = touch_flag, q = question_flag, n = just_print_flag;
85 unsigned int j = job_slots;
86 int status = -1;
88 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
89 : file_mtime (file))
91 /* Duplicate the chain so we can remove things from it. */
93 goals = copy_dep_chain (goals);
96 /* Clear the `changed' flag of each goal in the chain.
97 We will use the flag below to notice when any commands
98 have actually been run for a target. When no commands
99 have been run, we give an "up to date" diagnostic. */
101 struct dep *g;
102 for (g = goals; g != 0; g = g->next)
103 g->changed = 0;
106 /* All files start with the considered bit 0, so the global value is 1. */
107 considered = 1;
109 /* Update all the goals until they are all finished. */
111 while (goals != 0)
113 register struct dep *g, *lastgoal;
115 /* Start jobs that are waiting for the load to go down. */
117 start_waiting_jobs ();
119 /* Wait for a child to die. */
121 reap_children (1, 0);
123 lastgoal = 0;
124 g = goals;
125 while (g != 0)
127 /* Iterate over all double-colon entries for this file. */
128 struct file *file;
129 int stop = 0, any_not_updated = 0;
131 for (file = g->file->double_colon ? g->file->double_colon : g->file;
132 file != NULL;
133 file = file->prev)
135 unsigned int ocommands_started;
136 int x;
137 check_renamed (file);
138 if (makefiles)
140 if (file->cmd_target)
142 touch_flag = t;
143 question_flag = q;
144 just_print_flag = n;
146 else
147 touch_flag = question_flag = just_print_flag = 0;
150 /* Save the old value of `commands_started' so we can compare
151 later. It will be incremented when any commands are
152 actually run. */
153 ocommands_started = commands_started;
155 x = update_file (file, makefiles ? 1 : 0);
156 check_renamed (file);
158 /* Set the goal's `changed' flag if any commands were started
159 by calling update_file above. We check this flag below to
160 decide when to give an "up to date" diagnostic. */
161 g->changed += commands_started - ocommands_started;
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 = (!keep_going_flag && !question_flag
179 && !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 (!makefiles
196 || (!just_print_flag && !question_flag))
197 status = 0;
198 if (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 (!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 (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 (status != 0 && !keep_going_flag)
310 break;
312 if (f->command_state == cs_running
313 || f->command_state == cs_deps_running)
315 /* Don't run the other :: rules for this
316 file until this rule is finished. */
317 status = 0;
318 break;
322 /* Process the remaining rules in the double colon chain so they're marked
323 considered. Start their prerequisites, too. */
324 for (; f != 0 ; f = f->prev)
326 struct dep *d;
328 f->considered = considered;
330 for (d = f->deps; d != 0; d = d->next)
331 status |= update_file (d->file, depth + 1);
334 return status;
337 /* Consider a single `struct file' and update it as appropriate. */
339 static int
340 update_file_1 (struct file *file, unsigned int depth)
342 register FILE_TIMESTAMP this_mtime;
343 int noexist, must_make, deps_changed;
344 int dep_status = 0;
345 register struct dep *d, *lastd;
346 int running = 0;
348 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
350 if (file->updated)
352 if (file->update_status > 0)
354 DBF (DB_VERBOSE,
355 _("Recently tried and failed to update file `%s'.\n"));
356 return file->update_status;
359 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
360 return 0;
363 switch (file->command_state)
365 case cs_not_started:
366 case cs_deps_running:
367 break;
368 case cs_running:
369 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
370 return 0;
371 case cs_finished:
372 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
373 return file->update_status;
374 default:
375 abort ();
378 ++depth;
380 /* Notice recursive update of the same file. */
381 start_updating (file);
383 /* Looking at the file's modtime beforehand allows the possibility
384 that its name may be changed by a VPATH search, and thus it may
385 not need an implicit rule. If this were not done, the file
386 might get implicit commands that apply to its initial name, only
387 to have that name replaced with another found by VPATH search. */
389 this_mtime = file_mtime (file);
390 check_renamed (file);
391 noexist = this_mtime == NONEXISTENT_MTIME;
392 if (noexist)
393 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
394 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
395 && file->low_resolution_time)
397 /* Avoid spurious rebuilds due to low resolution time stamps. */
398 int ns = FILE_TIMESTAMP_NS (this_mtime);
399 if (ns != 0)
400 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
401 file->name);
402 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
405 must_make = noexist;
407 /* If file was specified as a target with no commands,
408 come up with some default commands. */
410 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
412 if (try_implicit_rule (file, depth))
413 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
414 else
415 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
416 file->tried_implicit = 1;
418 if (file->cmds == 0 && !file->is_target
419 && default_file != 0 && default_file->cmds != 0)
421 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
422 file->cmds = default_file->cmds;
425 /* Update all non-intermediate files we depend on, if necessary,
426 and see whether any of them is more recent than this file. */
428 lastd = 0;
429 d = file->deps;
430 while (d != 0)
432 FILE_TIMESTAMP mtime;
433 int maybe_make;
435 check_renamed (d->file);
437 mtime = file_mtime (d->file);
438 check_renamed (d->file);
440 if (is_updating (d->file))
442 error (NILF, _("Circular %s <- %s dependency dropped."),
443 file->name, d->file->name);
444 /* We cannot free D here because our the caller will still have
445 a reference to it when we were called recursively via
446 check_dep below. */
447 if (lastd == 0)
448 file->deps = d->next;
449 else
450 lastd->next = d->next;
451 d = d->next;
452 continue;
455 d->file->parent = file;
456 maybe_make = must_make;
457 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
458 if (! d->ignore_mtime)
459 must_make = maybe_make;
461 check_renamed (d->file);
464 register struct file *f = d->file;
465 if (f->double_colon)
466 f = f->double_colon;
469 running |= (f->command_state == cs_running
470 || f->command_state == cs_deps_running);
471 f = f->prev;
473 while (f != 0);
476 if (dep_status != 0 && !keep_going_flag)
477 break;
479 if (!running)
480 d->changed = file_mtime (d->file) != mtime;
482 lastd = d;
483 d = d->next;
486 /* Now we know whether this target needs updating.
487 If it does, update all the intermediate files we depend on. */
489 if (must_make || always_make_flag)
491 for (d = file->deps; d != 0; d = d->next)
492 if (d->file->intermediate)
494 FILE_TIMESTAMP mtime = file_mtime (d->file);
495 check_renamed (d->file);
496 d->file->parent = file;
497 dep_status |= update_file (d->file, depth);
498 check_renamed (d->file);
501 register struct file *f = d->file;
502 if (f->double_colon)
503 f = f->double_colon;
506 running |= (f->command_state == cs_running
507 || f->command_state == cs_deps_running);
508 f = f->prev;
510 while (f != 0);
513 if (dep_status != 0 && !keep_going_flag)
514 break;
516 if (!running)
517 d->changed = ((file->phony && file->cmds != 0)
518 || file_mtime (d->file) != mtime);
522 finish_updating (file);
524 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
526 if (running)
528 set_command_state (file, cs_deps_running);
529 --depth;
530 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
531 return 0;
534 /* If any dependency failed, give up now. */
536 if (dep_status != 0)
538 file->update_status = dep_status;
539 notice_finished_file (file);
541 --depth;
543 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
545 if (depth == 0 && keep_going_flag
546 && !just_print_flag && !question_flag)
547 error (NILF,
548 _("Target `%s' not remade because of errors."), file->name);
550 return dep_status;
553 if (file->command_state == cs_deps_running)
554 /* The commands for some deps were running on the last iteration, but
555 they have finished now. Reset the command_state to not_started to
556 simplify later bookkeeping. It is important that we do this only
557 when the prior state was cs_deps_running, because that prior state
558 was definitely propagated to FILE's also_make's by set_command_state
559 (called above), but in another state an also_make may have
560 independently changed to finished state, and we would confuse that
561 file's bookkeeping (updated, but not_started is bogus state). */
562 set_command_state (file, cs_not_started);
564 /* Now record which prerequisites are more
565 recent than this file, so we can define $?. */
567 deps_changed = 0;
568 for (d = file->deps; d != 0; d = d->next)
570 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
571 check_renamed (d->file);
573 if (! d->ignore_mtime)
575 #if 1
576 /* %%% In version 4, remove this code completely to
577 implement not remaking deps if their deps are newer
578 than their parents. */
579 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
580 /* We must remake if this dep does not
581 exist and is not intermediate. */
582 must_make = 1;
583 #endif
585 /* Set DEPS_CHANGED if this dep actually changed. */
586 deps_changed |= d->changed;
589 /* Set D->changed if either this dep actually changed,
590 or its dependent, FILE, is older or does not exist. */
591 d->changed |= noexist || d_mtime > this_mtime;
593 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
595 const char *fmt = 0;
597 if (d->ignore_mtime)
599 if (ISDB (DB_VERBOSE))
600 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
602 else if (d_mtime == NONEXISTENT_MTIME)
604 if (ISDB (DB_BASIC))
605 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
607 else if (d->changed)
609 if (ISDB (DB_BASIC))
610 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
612 else if (ISDB (DB_VERBOSE))
613 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
615 if (fmt)
617 print_spaces (depth);
618 printf (fmt, dep_name (d), file->name);
619 fflush (stdout);
624 /* Here depth returns to the value it had when we were called. */
625 depth--;
627 if (file->double_colon && file->deps == 0)
629 must_make = 1;
630 DBF (DB_BASIC,
631 _("Target `%s' is double-colon and has no prerequisites.\n"));
633 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
634 && !always_make_flag)
636 must_make = 0;
637 DBF (DB_VERBOSE,
638 _("No commands for `%s' and no prerequisites actually changed.\n"));
640 else if (!must_make && file->cmds != 0 && always_make_flag)
642 must_make = 1;
643 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
646 if (!must_make)
648 if (ISDB (DB_VERBOSE))
650 print_spaces (depth);
651 printf (_("No need to remake target `%s'"), file->name);
652 if (!streq (file->name, file->hname))
653 printf (_("; using VPATH name `%s'"), file->hname);
654 puts (".");
655 fflush (stdout);
658 notice_finished_file (file);
660 /* Since we don't need to remake the file, convert it to use the
661 VPATH filename if we found one. hfile will be either the
662 local name if no VPATH or the VPATH name if one was found. */
664 while (file)
666 file->name = file->hname;
667 file = file->prev;
670 return 0;
673 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
675 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
676 VPATH. */
677 if (!streq(file->name, file->hname))
679 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
680 file->ignore_vpath = 1;
683 /* Now, take appropriate actions to remake the file. */
684 remake_file (file);
686 if (file->command_state != cs_finished)
688 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
689 return 0;
692 switch (file->update_status)
694 case 2:
695 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
696 break;
697 case 0:
698 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
699 break;
700 case 1:
701 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
702 break;
703 default:
704 assert (file->update_status >= 0 && file->update_status <= 2);
705 break;
708 file->updated = 1;
709 return file->update_status;
712 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
713 files listed in its `also_make' member. Under -t, this function also
714 touches FILE.
716 On return, FILE->update_status will no longer be -1 if it was. */
718 void
719 notice_finished_file (struct file *file)
721 struct dep *d;
722 int ran = file->command_state == cs_running;
723 int touched = 0;
725 file->command_state = cs_finished;
726 file->updated = 1;
728 if (touch_flag
729 /* The update status will be:
730 -1 if this target was not remade;
731 0 if 0 or more commands (+ or ${MAKE}) were run and won;
732 1 if some commands were run and lost.
733 We touch the target if it has commands which either were not run
734 or won when they ran (i.e. status is 0). */
735 && file->update_status == 0)
737 if (file->cmds != 0 && file->cmds->any_recurse)
739 /* If all the command lines were recursive,
740 we don't want to do the touching. */
741 unsigned int i;
742 for (i = 0; i < file->cmds->ncommand_lines; ++i)
743 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
744 goto have_nonrecursing;
746 else
748 have_nonrecursing:
749 if (file->phony)
750 file->update_status = 0;
751 /* According to POSIX, -t doesn't affect targets with no cmds. */
752 else if (file->cmds != 0)
754 /* Should set file's modification date and do nothing else. */
755 file->update_status = touch_file (file);
757 /* Pretend we ran a real touch command, to suppress the
758 "`foo' is up to date" message. */
759 commands_started++;
761 /* Request for the timestamp to be updated (and distributed
762 to the double-colon entries). Simply setting ran=1 would
763 almost have done the trick, but messes up with the also_make
764 updating logic below. */
765 touched = 1;
770 if (file->mtime_before_update == UNKNOWN_MTIME)
771 file->mtime_before_update = file->last_mtime;
773 if ((ran && !file->phony) || touched)
775 struct file *f;
776 int i = 0;
778 /* If -n, -t, or -q and all the commands are recursive, we ran them so
779 really check the target's mtime again. Otherwise, assume the target
780 would have been updated. */
782 if (question_flag || just_print_flag || touch_flag)
784 for (i = file->cmds->ncommand_lines; i > 0; --i)
785 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
786 break;
789 /* If there were no commands at all, it's always new. */
791 else if (file->is_target && file->cmds == 0)
792 i = 1;
794 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
796 /* Propagate the change of modification time to all the double-colon
797 entries for this file. */
798 for (f = file->double_colon; f != 0; f = f->prev)
799 f->last_mtime = file->last_mtime;
802 if (ran && file->update_status != -1)
803 /* We actually tried to update FILE, which has
804 updated its also_make's as well (if it worked).
805 If it didn't work, it wouldn't work again for them.
806 So mark them as updated with the same status. */
807 for (d = file->also_make; d != 0; d = d->next)
809 d->file->command_state = cs_finished;
810 d->file->updated = 1;
811 d->file->update_status = file->update_status;
813 if (ran && !d->file->phony)
814 /* Fetch the new modification time.
815 We do this instead of just invalidating the cached time
816 so that a vpath_search can happen. Otherwise, it would
817 never be done because the target is already updated. */
818 (void) f_mtime (d->file, 0);
820 else if (file->update_status == -1)
821 /* Nothing was done for FILE, but it needed nothing done.
822 So mark it now as "succeeded". */
823 file->update_status = 0;
826 /* Check whether another file (whose mtime is THIS_MTIME)
827 needs updating on account of a dependency which is file FILE.
828 If it does, store 1 in *MUST_MAKE_PTR.
829 In the process, update any non-intermediate files
830 that FILE depends on (including FILE itself).
831 Return nonzero if any updating failed. */
833 static int
834 check_dep (struct file *file, unsigned int depth,
835 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
837 struct dep *d;
838 int dep_status = 0;
840 ++depth;
841 start_updating (file);
843 if (!file->intermediate)
844 /* If this is a non-intermediate file, update it and record
845 whether it is newer than THIS_MTIME. */
847 FILE_TIMESTAMP mtime;
848 dep_status = update_file (file, depth);
849 check_renamed (file);
850 mtime = file_mtime (file);
851 check_renamed (file);
852 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
853 *must_make_ptr = 1;
855 else
857 /* FILE is an intermediate file. */
858 FILE_TIMESTAMP mtime;
860 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
862 if (try_implicit_rule (file, depth))
863 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
864 else
865 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
866 file->tried_implicit = 1;
868 if (file->cmds == 0 && !file->is_target
869 && default_file != 0 && default_file->cmds != 0)
871 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
872 file->cmds = default_file->cmds;
875 /* If the intermediate file actually exists
876 and is newer, then we should remake from it. */
877 check_renamed (file);
878 mtime = file_mtime (file);
879 check_renamed (file);
880 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
881 *must_make_ptr = 1;
882 /* Otherwise, update all non-intermediate files we depend on,
883 if necessary, and see whether any of them is more
884 recent than the file on whose behalf we are checking. */
885 else
887 struct dep *lastd;
889 lastd = 0;
890 d = file->deps;
891 while (d != 0)
893 int maybe_make;
895 if (is_updating (d->file))
897 error (NILF, _("Circular %s <- %s dependency dropped."),
898 file->name, d->file->name);
899 if (lastd == 0)
901 file->deps = d->next;
902 free ((char *) d);
903 d = file->deps;
905 else
907 lastd->next = d->next;
908 free ((char *) d);
909 d = lastd->next;
911 continue;
914 d->file->parent = file;
915 maybe_make = *must_make_ptr;
916 dep_status |= check_dep (d->file, depth, this_mtime,
917 &maybe_make);
918 if (! d->ignore_mtime)
919 *must_make_ptr = maybe_make;
920 check_renamed (d->file);
921 if (dep_status != 0 && !keep_going_flag)
922 break;
924 if (d->file->command_state == cs_running
925 || d->file->command_state == cs_deps_running)
926 /* Record that some of FILE's deps are still being made.
927 This tells the upper levels to wait on processing it until
928 the commands are finished. */
929 set_command_state (file, cs_deps_running);
931 lastd = d;
932 d = d->next;
937 finish_updating (file);
938 return dep_status;
941 /* Touch FILE. Return zero if successful, one if not. */
943 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
945 static int
946 touch_file (struct file *file)
948 if (!silent_flag)
949 message (0, "touch %s", file->name);
951 #ifndef NO_ARCHIVES
952 if (ar_name (file->name))
953 return ar_touch (file->name);
954 else
955 #endif
957 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
959 if (fd < 0)
960 TOUCH_ERROR ("touch: open: ");
961 else
963 struct stat statbuf;
964 char buf;
965 int e;
967 EINTRLOOP (e, fstat (fd, &statbuf));
968 if (e < 0)
969 TOUCH_ERROR ("touch: fstat: ");
970 /* Rewrite character 0 same as it already is. */
971 if (read (fd, &buf, 1) < 0)
972 TOUCH_ERROR ("touch: read: ");
973 if (lseek (fd, 0L, 0) < 0L)
974 TOUCH_ERROR ("touch: lseek: ");
975 if (write (fd, &buf, 1) < 0)
976 TOUCH_ERROR ("touch: write: ");
977 /* If file length was 0, we just
978 changed it, so change it back. */
979 if (statbuf.st_size == 0)
981 (void) close (fd);
982 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
983 if (fd < 0)
984 TOUCH_ERROR ("touch: open: ");
986 (void) close (fd);
990 return 0;
993 /* Having checked and updated the dependencies of FILE,
994 do whatever is appropriate to remake FILE itself.
995 Return the status from executing FILE's commands. */
997 static void
998 remake_file (struct file *file)
1000 if (file->cmds == 0)
1002 if (file->phony)
1003 /* Phony target. Pretend it succeeded. */
1004 file->update_status = 0;
1005 else if (file->is_target)
1006 /* This is a nonexistent target file we cannot make.
1007 Pretend it was successfully remade. */
1008 file->update_status = 0;
1009 else
1011 const char *msg_noparent
1012 = _("%sNo rule to make target `%s'%s");
1013 const char *msg_parent
1014 = _("%sNo rule to make target `%s', needed by `%s'%s");
1016 /* This is a dependency file we cannot remake. Fail. */
1017 if (!keep_going_flag && !file->dontcare)
1019 if (file->parent == 0)
1020 fatal (NILF, msg_noparent, "", file->name, "");
1022 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
1025 if (!file->dontcare)
1027 if (file->parent == 0)
1028 error (NILF, msg_noparent, "*** ", file->name, ".");
1029 else
1030 error (NILF, msg_parent, "*** ",
1031 file->name, file->parent->name, ".");
1033 file->update_status = 2;
1036 else
1038 chop_commands (file->cmds);
1040 /* The normal case: start some commands. */
1041 if (!touch_flag || file->cmds->any_recurse)
1043 execute_file_commands (file);
1044 return;
1047 /* This tells notice_finished_file it is ok to touch the file. */
1048 file->update_status = 0;
1051 /* This does the touching under -t. */
1052 notice_finished_file (file);
1055 /* Return the mtime of a file, given a `struct file'.
1056 Caches the time in the struct file to avoid excess stat calls.
1058 If the file is not found, and SEARCH is nonzero, VPATH searching and
1059 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1060 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1061 FILE. */
1063 FILE_TIMESTAMP
1064 f_mtime (struct file *file, int search)
1066 FILE_TIMESTAMP mtime;
1068 /* File's mtime is not known; must get it from the system. */
1070 #ifndef NO_ARCHIVES
1071 if (ar_name (file->name))
1073 /* This file is an archive-member reference. */
1075 char *arname, *memname;
1076 struct file *arfile;
1077 int arname_used = 0;
1078 time_t member_date;
1080 /* Find the archive's name. */
1081 ar_parse_name (file->name, &arname, &memname);
1083 /* Find the modification time of the archive itself.
1084 Also allow for its name to be changed via VPATH search. */
1085 arfile = lookup_file (arname);
1086 if (arfile == 0)
1088 arfile = enter_file (arname);
1089 arname_used = 1;
1091 mtime = f_mtime (arfile, search);
1092 check_renamed (arfile);
1093 if (search && strcmp (arfile->hname, arname))
1095 /* The archive's name has changed.
1096 Change the archive-member reference accordingly. */
1098 char *name;
1099 unsigned int arlen, memlen;
1101 if (!arname_used)
1103 free (arname);
1104 arname_used = 1;
1107 arname = arfile->hname;
1108 arlen = strlen (arname);
1109 memlen = strlen (memname);
1111 /* free (file->name); */
1113 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1114 bcopy (arname, name, arlen);
1115 name[arlen] = '(';
1116 bcopy (memname, name + arlen + 1, memlen);
1117 name[arlen + 1 + memlen] = ')';
1118 name[arlen + 1 + memlen + 1] = '\0';
1120 /* If the archive was found with GPATH, make the change permanent;
1121 otherwise defer it until later. */
1122 if (arfile->name == arfile->hname)
1123 rename_file (file, name);
1124 else
1125 rehash_file (file, name);
1126 check_renamed (file);
1129 if (!arname_used)
1130 free (arname);
1131 free (memname);
1133 file->low_resolution_time = 1;
1135 if (mtime == NONEXISTENT_MTIME)
1136 /* The archive doesn't exist, so its members don't exist either. */
1137 return NONEXISTENT_MTIME;
1139 member_date = ar_member_date (file->hname);
1140 mtime = (member_date == (time_t) -1
1141 ? NONEXISTENT_MTIME
1142 : file_timestamp_cons (file->hname, member_date, 0));
1144 else
1145 #endif
1147 mtime = name_mtime (file->name);
1149 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1151 /* If name_mtime failed, search VPATH. */
1152 char *name = file->name;
1153 if (vpath_search (&name, &mtime)
1154 /* Last resort, is it a library (-lxxx)? */
1155 || (name[0] == '-' && name[1] == 'l'
1156 && library_search (&name, &mtime)))
1158 if (mtime != UNKNOWN_MTIME)
1159 /* vpath_search and library_search store UNKNOWN_MTIME
1160 if they didn't need to do a stat call for their work. */
1161 file->last_mtime = mtime;
1163 /* If we found it in VPATH, see if it's in GPATH too; if so,
1164 change the name right now; if not, defer until after the
1165 dependencies are updated. */
1166 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1168 rename_file (file, name);
1169 check_renamed (file);
1170 return file_mtime (file);
1173 rehash_file (file, name);
1174 check_renamed (file);
1175 mtime = name_mtime (name);
1181 /* Files can have bogus timestamps that nothing newly made will be
1182 "newer" than. Updating their dependents could just result in loops.
1183 So notify the user of the anomaly with a warning.
1185 We only need to do this once, for now. */
1187 if (!clock_skew_detected
1188 && mtime != NONEXISTENT_MTIME
1189 && !file->updated)
1191 static FILE_TIMESTAMP adjusted_now;
1193 FILE_TIMESTAMP adjusted_mtime = mtime;
1195 #if defined(WINDOWS32) || defined(__MSDOS__)
1196 /* Experimentation has shown that FAT filesystems can set file times
1197 up to 3 seconds into the future! Play it safe. */
1199 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1201 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1202 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1203 adjusted_mtime -= adjustment;
1204 #elif defined(__EMX__)
1205 /* FAT filesystems round time to the nearest even second!
1206 Allow for any file (NTFS or FAT) to perhaps suffer from this
1207 brain damage. */
1208 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1209 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1210 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1211 : 0);
1212 #endif
1214 /* If the file's time appears to be in the future, update our
1215 concept of the present and try once more. */
1216 if (adjusted_now < adjusted_mtime)
1218 int resolution;
1219 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1220 adjusted_now = now + (resolution - 1);
1221 if (adjusted_now < adjusted_mtime)
1223 #ifdef NO_FLOAT
1224 error (NILF, _("Warning: File `%s' has modification time in the future"),
1225 file->name);
1226 #else
1227 double from_now =
1228 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1229 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1230 / 1e9));
1231 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1232 file->name, from_now);
1233 #endif
1234 clock_skew_detected = 1;
1240 /* Store the mtime into all the entries for this file. */
1241 if (file->double_colon)
1242 file = file->double_colon;
1246 /* If this file is not implicit but it is intermediate then it was
1247 made so by the .INTERMEDIATE target. If this file has never
1248 been built by us but was found now, it existed before make
1249 started. So, turn off the intermediate bit so make doesn't
1250 delete it, since it didn't create it. */
1251 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1252 && file->command_state == cs_not_started
1253 && !file->tried_implicit && file->intermediate)
1254 file->intermediate = 0;
1256 file->last_mtime = mtime;
1257 file = file->prev;
1259 while (file != 0);
1261 return mtime;
1265 /* Return the mtime of the file or archive-member reference NAME. */
1267 static FILE_TIMESTAMP
1268 name_mtime (char *name)
1270 struct stat st;
1271 int e;
1273 EINTRLOOP (e, stat (name, &st));
1274 if (e != 0)
1276 if (errno != ENOENT && errno != ENOTDIR)
1277 perror_with_name ("stat:", name);
1278 return NONEXISTENT_MTIME;
1281 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1285 /* Search for a library file specified as -lLIBNAME, searching for a
1286 suitable library file in the system library directories and the VPATH
1287 directories. */
1289 static int
1290 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1292 static char *dirs[] =
1294 #ifndef _AMIGA
1295 "/lib",
1296 "/usr/lib",
1297 #endif
1298 #if defined(WINDOWS32) && !defined(LIBDIR)
1300 * This is completely up to the user at product install time. Just define
1301 * a placeholder.
1303 #define LIBDIR "."
1304 #endif
1305 LIBDIR, /* Defined by configuration. */
1309 static char *libpatterns = NULL;
1311 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1312 FILE_TIMESTAMP mtime;
1314 /* Loop variables for the libpatterns value. */
1315 char *p, *p2;
1316 unsigned int len;
1318 char *file, **dp;
1320 /* If we don't have libpatterns, get it. */
1321 if (!libpatterns)
1323 int save = warn_undefined_variables_flag;
1324 warn_undefined_variables_flag = 0;
1326 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1328 warn_undefined_variables_flag = save;
1331 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1332 p2 = libpatterns;
1333 while ((p = find_next_token (&p2, &len)) != 0)
1335 static char *buf = NULL;
1336 static int buflen = 0;
1337 static int libdir_maxlen = -1;
1338 char *libbuf = variable_expand ("");
1340 /* Expand the pattern using LIBNAME as a replacement. */
1342 char c = p[len];
1343 char *p3, *p4;
1345 p[len] = '\0';
1346 p3 = find_percent (p);
1347 if (!p3)
1349 /* Give a warning if there is no pattern, then remove the
1350 pattern so it's ignored next time. */
1351 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1352 for (; len; --len, ++p)
1353 *p = ' ';
1354 *p = c;
1355 continue;
1357 p4 = variable_buffer_output (libbuf, p, p3-p);
1358 p4 = variable_buffer_output (p4, libname, strlen (libname));
1359 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1360 p[len] = c;
1363 /* Look first for `libNAME.a' in the current directory. */
1364 mtime = name_mtime (libbuf);
1365 if (mtime != NONEXISTENT_MTIME)
1367 *lib = xstrdup (libbuf);
1368 if (mtime_ptr != 0)
1369 *mtime_ptr = mtime;
1370 return 1;
1373 /* Now try VPATH search on that. */
1375 file = libbuf;
1376 if (vpath_search (&file, mtime_ptr))
1378 *lib = file;
1379 return 1;
1382 /* Now try the standard set of directories. */
1384 if (!buflen)
1386 for (dp = dirs; *dp != 0; ++dp)
1388 int l = strlen (*dp);
1389 if (l > libdir_maxlen)
1390 libdir_maxlen = l;
1392 buflen = strlen (libbuf);
1393 buf = xmalloc(libdir_maxlen + buflen + 2);
1395 else if (buflen < strlen (libbuf))
1397 buflen = strlen (libbuf);
1398 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1401 for (dp = dirs; *dp != 0; ++dp)
1403 sprintf (buf, "%s/%s", *dp, libbuf);
1404 mtime = name_mtime (buf);
1405 if (mtime != NONEXISTENT_MTIME)
1407 *lib = xstrdup (buf);
1408 if (mtime_ptr != 0)
1409 *mtime_ptr = mtime;
1410 return 1;
1415 return 0;