Have the test driver check for the new format of the time skew error
[make.git] / remake.c
blob72c4d78a4447be79219343cc364a0bdaf456c5d3
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 (goals, makefiles)
83 register struct dep *goals;
84 int makefiles;
86 int t = touch_flag, q = question_flag, n = just_print_flag;
87 unsigned int j = job_slots;
88 int status = -1;
90 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
91 : file_mtime (file))
93 /* Duplicate the chain so we can remove things from it. */
95 goals = copy_dep_chain (goals);
98 /* Clear the `changed' flag of each goal in the chain.
99 We will use the flag below to notice when any commands
100 have actually been run for a target. When no commands
101 have been run, we give an "up to date" diagnostic. */
103 struct dep *g;
104 for (g = goals; g != 0; g = g->next)
105 g->changed = 0;
108 /* All files start with the considered bit 0, so the global value is 1. */
109 considered = 1;
111 /* Update all the goals until they are all finished. */
113 while (goals != 0)
115 register struct dep *g, *lastgoal;
117 /* Start jobs that are waiting for the load to go down. */
119 start_waiting_jobs ();
121 /* Wait for a child to die. */
123 reap_children (1, 0);
125 lastgoal = 0;
126 g = goals;
127 while (g != 0)
129 /* Iterate over all double-colon entries for this file. */
130 struct file *file;
131 int stop = 0, any_not_updated = 0;
133 for (file = g->file->double_colon ? g->file->double_colon : g->file;
134 file != NULL;
135 file = file->prev)
137 unsigned int ocommands_started;
138 int x;
139 check_renamed (file);
140 if (makefiles)
142 if (file->cmd_target)
144 touch_flag = t;
145 question_flag = q;
146 just_print_flag = n;
148 else
149 touch_flag = question_flag = just_print_flag = 0;
152 /* Save the old value of `commands_started' so we can compare
153 later. It will be incremented when any commands are
154 actually run. */
155 ocommands_started = commands_started;
157 x = update_file (file, makefiles ? 1 : 0);
158 check_renamed (file);
160 /* Set the goal's `changed' flag if any commands were started
161 by calling update_file above. We check this flag below to
162 decide when to give an "up to date" diagnostic. */
163 g->changed += commands_started - ocommands_started;
165 /* If we updated a file and STATUS was not already 1, set it to
166 1 if updating failed, or to 0 if updating succeeded. Leave
167 STATUS as it is if no updating was done. */
169 stop = 0;
170 if ((x != 0 || file->updated) && status < 1)
172 if (file->update_status != 0)
174 /* Updating failed, or -q triggered. The STATUS value
175 tells our caller which. */
176 status = file->update_status;
177 /* If -q just triggered, stop immediately. It doesn't
178 matter how much more we run, since we already know
179 the answer to return. */
180 stop = (!keep_going_flag && !question_flag
181 && !makefiles);
183 else
185 FILE_TIMESTAMP mtime = MTIME (file);
186 check_renamed (file);
188 if (file->updated && g->changed &&
189 mtime != file->mtime_before_update)
191 /* Updating was done. If this is a makefile and
192 just_print_flag or question_flag is set (meaning
193 -n or -q was given and this file was specified
194 as a command-line target), don't change STATUS.
195 If STATUS is changed, we will get re-exec'd, and
196 enter an infinite loop. */
197 if (!makefiles
198 || (!just_print_flag && !question_flag))
199 status = 0;
200 if (makefiles && file->dontcare)
201 /* This is a default makefile; stop remaking. */
202 stop = 1;
207 /* Keep track if any double-colon entry is not finished.
208 When they are all finished, the goal is finished. */
209 any_not_updated |= !file->updated;
211 if (stop)
212 break;
215 /* Reset FILE since it is null at the end of the loop. */
216 file = g->file;
218 if (stop || !any_not_updated)
220 /* If we have found nothing whatever to do for the goal,
221 print a message saying nothing needs doing. */
223 if (!makefiles
224 /* If the update_status is zero, we updated successfully
225 or not at all. G->changed will have been set above if
226 any commands were actually started for this goal. */
227 && file->update_status == 0 && !g->changed
228 /* Never give a message under -s or -q. */
229 && !silent_flag && !question_flag)
230 message (1, ((file->phony || file->cmds == 0)
231 ? _("Nothing to be done for `%s'.")
232 : _("`%s' is up to date.")),
233 file->name);
235 /* This goal is finished. Remove it from the chain. */
236 if (lastgoal == 0)
237 goals = g->next;
238 else
239 lastgoal->next = g->next;
241 /* Free the storage. */
242 free ((char *) g);
244 g = lastgoal == 0 ? goals : lastgoal->next;
246 if (stop)
247 break;
249 else
251 lastgoal = g;
252 g = g->next;
256 /* If we reached the end of the dependency graph toggle the considered
257 flag for the next pass. */
258 if (g == 0)
259 considered = !considered;
262 if (makefiles)
264 touch_flag = t;
265 question_flag = q;
266 just_print_flag = n;
267 job_slots = j;
269 return status;
272 /* If FILE is not up to date, execute the commands for it.
273 Return 0 if successful, 1 if unsuccessful;
274 but with some flag settings, just call `exit' if unsuccessful.
276 DEPTH is the depth in recursions of this function.
277 We increment it during the consideration of our dependencies,
278 then decrement it again after finding out whether this file
279 is out of date.
281 If there are multiple double-colon entries for FILE,
282 each is considered in turn. */
284 static int
285 update_file (file, depth)
286 struct file *file;
287 unsigned int depth;
289 register int status = 0;
290 register struct file *f;
292 f = file->double_colon ? file->double_colon : file;
294 /* Prune the dependency graph: if we've already been here on _this_
295 pass through the dependency graph, we don't have to go any further.
296 We won't reap_children until we start the next pass, so no state
297 change is possible below here until then. */
298 if (f->considered == considered)
300 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
301 return f->command_state == cs_finished ? f->update_status : 0;
304 /* This loop runs until we start commands for a double colon rule, or until
305 the chain is exhausted. */
306 for (; f != 0; f = f->prev)
308 f->considered = considered;
310 status |= update_file_1 (f, depth);
311 check_renamed (f);
313 if (status != 0 && !keep_going_flag)
314 break;
316 if (f->command_state == cs_running
317 || f->command_state == cs_deps_running)
319 /* Don't run the other :: rules for this
320 file until this rule is finished. */
321 status = 0;
322 break;
326 /* Process the remaining rules in the double colon chain so they're marked
327 considered. Start their prerequisites, too. */
328 for (; f != 0 ; f = f->prev)
330 struct dep *d;
332 f->considered = considered;
334 for (d = f->deps; d != 0; d = d->next)
335 status |= update_file (d->file, depth + 1);
338 return status;
341 /* Consider a single `struct file' and update it as appropriate. */
343 static int
344 update_file_1 (file, depth)
345 struct file *file;
346 unsigned int depth;
348 register FILE_TIMESTAMP this_mtime;
349 int noexist, must_make, deps_changed;
350 int dep_status = 0;
351 register struct dep *d, *lastd;
352 int running = 0;
354 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
356 if (file->updated)
358 if (file->update_status > 0)
360 DBF (DB_VERBOSE,
361 _("Recently tried and failed to update file `%s'.\n"));
362 return file->update_status;
365 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
366 return 0;
369 switch (file->command_state)
371 case cs_not_started:
372 case cs_deps_running:
373 break;
374 case cs_running:
375 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
376 return 0;
377 case cs_finished:
378 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
379 return file->update_status;
380 default:
381 abort ();
384 ++depth;
386 /* Notice recursive update of the same file. */
387 start_updating (file);
389 /* Looking at the file's modtime beforehand allows the possibility
390 that its name may be changed by a VPATH search, and thus it may
391 not need an implicit rule. If this were not done, the file
392 might get implicit commands that apply to its initial name, only
393 to have that name replaced with another found by VPATH search. */
395 this_mtime = file_mtime (file);
396 check_renamed (file);
397 noexist = this_mtime == NONEXISTENT_MTIME;
398 if (noexist)
399 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
400 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
401 && file->low_resolution_time)
403 /* Avoid spurious rebuilds due to low resolution time stamps. */
404 int ns = FILE_TIMESTAMP_NS (this_mtime);
405 if (ns != 0)
406 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
407 file->name);
408 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
411 must_make = noexist;
413 /* If file was specified as a target with no commands,
414 come up with some default commands. */
416 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
418 if (try_implicit_rule (file, depth))
419 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
420 else
421 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
422 file->tried_implicit = 1;
424 if (file->cmds == 0 && !file->is_target
425 && default_file != 0 && default_file->cmds != 0)
427 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
428 file->cmds = default_file->cmds;
431 /* Update all non-intermediate files we depend on, if necessary,
432 and see whether any of them is more recent than this file. */
434 lastd = 0;
435 d = file->deps;
436 while (d != 0)
438 FILE_TIMESTAMP mtime;
439 int maybe_make;
441 check_renamed (d->file);
443 mtime = file_mtime (d->file);
444 check_renamed (d->file);
446 if (is_updating (d->file))
448 error (NILF, _("Circular %s <- %s dependency dropped."),
449 file->name, d->file->name);
450 /* We cannot free D here because our the caller will still have
451 a reference to it when we were called recursively via
452 check_dep below. */
453 if (lastd == 0)
454 file->deps = d->next;
455 else
456 lastd->next = d->next;
457 d = d->next;
458 continue;
461 d->file->parent = file;
462 maybe_make = must_make;
463 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
464 if (! d->ignore_mtime)
465 must_make = maybe_make;
467 check_renamed (d->file);
470 register struct file *f = d->file;
471 if (f->double_colon)
472 f = f->double_colon;
475 running |= (f->command_state == cs_running
476 || f->command_state == cs_deps_running);
477 f = f->prev;
479 while (f != 0);
482 if (dep_status != 0 && !keep_going_flag)
483 break;
485 if (!running)
486 d->changed = file_mtime (d->file) != mtime;
488 lastd = d;
489 d = d->next;
492 /* Now we know whether this target needs updating.
493 If it does, update all the intermediate files we depend on. */
495 if (must_make || always_make_flag)
497 for (d = file->deps; d != 0; d = d->next)
498 if (d->file->intermediate)
500 FILE_TIMESTAMP mtime = file_mtime (d->file);
501 check_renamed (d->file);
502 d->file->parent = file;
503 dep_status |= update_file (d->file, depth);
504 check_renamed (d->file);
507 register struct file *f = d->file;
508 if (f->double_colon)
509 f = f->double_colon;
512 running |= (f->command_state == cs_running
513 || f->command_state == cs_deps_running);
514 f = f->prev;
516 while (f != 0);
519 if (dep_status != 0 && !keep_going_flag)
520 break;
522 if (!running)
523 d->changed = ((file->phony && file->cmds != 0)
524 || file_mtime (d->file) != mtime);
528 finish_updating (file);
530 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
532 if (running)
534 set_command_state (file, cs_deps_running);
535 --depth;
536 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
537 return 0;
540 /* If any dependency failed, give up now. */
542 if (dep_status != 0)
544 file->update_status = dep_status;
545 notice_finished_file (file);
547 --depth;
549 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
551 if (depth == 0 && keep_going_flag
552 && !just_print_flag && !question_flag)
553 error (NILF,
554 _("Target `%s' not remade because of errors."), file->name);
556 return dep_status;
559 if (file->command_state == cs_deps_running)
560 /* The commands for some deps were running on the last iteration, but
561 they have finished now. Reset the command_state to not_started to
562 simplify later bookkeeping. It is important that we do this only
563 when the prior state was cs_deps_running, because that prior state
564 was definitely propagated to FILE's also_make's by set_command_state
565 (called above), but in another state an also_make may have
566 independently changed to finished state, and we would confuse that
567 file's bookkeeping (updated, but not_started is bogus state). */
568 set_command_state (file, cs_not_started);
570 /* Now record which prerequisites are more
571 recent than this file, so we can define $?. */
573 deps_changed = 0;
574 for (d = file->deps; d != 0; d = d->next)
576 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
577 check_renamed (d->file);
579 if (! d->ignore_mtime)
581 #if 1
582 /* %%% In version 4, remove this code completely to
583 implement not remaking deps if their deps are newer
584 than their parents. */
585 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
586 /* We must remake if this dep does not
587 exist and is not intermediate. */
588 must_make = 1;
589 #endif
591 /* Set DEPS_CHANGED if this dep actually changed. */
592 deps_changed |= d->changed;
595 /* Set D->changed if either this dep actually changed,
596 or its dependent, FILE, is older or does not exist. */
597 d->changed |= noexist || d_mtime > this_mtime;
599 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
601 const char *fmt = 0;
603 if (d->ignore_mtime)
605 if (ISDB (DB_VERBOSE))
606 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
608 else if (d_mtime == NONEXISTENT_MTIME)
610 if (ISDB (DB_BASIC))
611 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
613 else if (d->changed)
615 if (ISDB (DB_BASIC))
616 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
618 else if (ISDB (DB_VERBOSE))
619 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
621 if (fmt)
623 print_spaces (depth);
624 printf (fmt, dep_name (d), file->name);
625 fflush (stdout);
630 /* Here depth returns to the value it had when we were called. */
631 depth--;
633 if (file->double_colon && file->deps == 0)
635 must_make = 1;
636 DBF (DB_BASIC,
637 _("Target `%s' is double-colon and has no prerequisites.\n"));
639 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
640 && !always_make_flag)
642 must_make = 0;
643 DBF (DB_VERBOSE,
644 _("No commands for `%s' and no prerequisites actually changed.\n"));
646 else if (!must_make && file->cmds != 0 && always_make_flag)
648 must_make = 1;
649 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
652 if (!must_make)
654 if (ISDB (DB_VERBOSE))
656 print_spaces (depth);
657 printf (_("No need to remake target `%s'"), file->name);
658 if (!streq (file->name, file->hname))
659 printf (_("; using VPATH name `%s'"), file->hname);
660 puts (".");
661 fflush (stdout);
664 notice_finished_file (file);
666 /* Since we don't need to remake the file, convert it to use the
667 VPATH filename if we found one. hfile will be either the
668 local name if no VPATH or the VPATH name if one was found. */
670 while (file)
672 file->name = file->hname;
673 file = file->prev;
676 return 0;
679 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
681 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
682 VPATH. */
683 if (!streq(file->name, file->hname))
685 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
686 file->ignore_vpath = 1;
689 /* Now, take appropriate actions to remake the file. */
690 remake_file (file);
692 if (file->command_state != cs_finished)
694 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
695 return 0;
698 switch (file->update_status)
700 case 2:
701 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
702 break;
703 case 0:
704 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
705 break;
706 case 1:
707 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
708 break;
709 default:
710 assert (file->update_status >= 0 && file->update_status <= 2);
711 break;
714 file->updated = 1;
715 return file->update_status;
718 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
719 files listed in its `also_make' member. Under -t, this function also
720 touches FILE.
722 On return, FILE->update_status will no longer be -1 if it was. */
724 void
725 notice_finished_file (file)
726 register struct file *file;
728 struct dep *d;
729 int ran = file->command_state == cs_running;
730 int touched = 0;
732 file->command_state = cs_finished;
733 file->updated = 1;
735 if (touch_flag
736 /* The update status will be:
737 -1 if this target was not remade;
738 0 if 0 or more commands (+ or ${MAKE}) were run and won;
739 1 if some commands were run and lost.
740 We touch the target if it has commands which either were not run
741 or won when they ran (i.e. status is 0). */
742 && file->update_status == 0)
744 if (file->cmds != 0 && file->cmds->any_recurse)
746 /* If all the command lines were recursive,
747 we don't want to do the touching. */
748 unsigned int i;
749 for (i = 0; i < file->cmds->ncommand_lines; ++i)
750 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
751 goto have_nonrecursing;
753 else
755 have_nonrecursing:
756 if (file->phony)
757 file->update_status = 0;
758 else
760 /* Should set file's modification date and do nothing else. */
761 file->update_status = touch_file (file);
763 /* Pretend we ran a real touch command, to suppress the
764 "`foo' is up to date" message. */
765 commands_started++;
767 /* Request for the timestamp to be updated (and distributed
768 to the double-colon entries). Simply setting ran=1 would
769 almost have done the trick, but messes up with the also_make
770 updating logic below. */
771 touched = 1;
776 if (file->mtime_before_update == UNKNOWN_MTIME)
777 file->mtime_before_update = file->last_mtime;
779 if ((ran && !file->phony) || touched)
781 struct file *f;
782 int i = 0;
784 /* If -n, -t, or -q and all the commands are recursive, we ran them so
785 really check the target's mtime again. Otherwise, assume the target
786 would have been updated. */
788 if (question_flag || just_print_flag || touch_flag)
790 for (i = file->cmds->ncommand_lines; i > 0; --i)
791 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
792 break;
795 /* If there were no commands at all, it's always new. */
797 else if (file->is_target && file->cmds == 0)
798 i = 1;
800 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
802 /* Propagate the change of modification time to all the double-colon
803 entries for this file. */
804 for (f = file->double_colon; f != 0; f = f->prev)
805 f->last_mtime = file->last_mtime;
808 if (ran && file->update_status != -1)
809 /* We actually tried to update FILE, which has
810 updated its also_make's as well (if it worked).
811 If it didn't work, it wouldn't work again for them.
812 So mark them as updated with the same status. */
813 for (d = file->also_make; d != 0; d = d->next)
815 d->file->command_state = cs_finished;
816 d->file->updated = 1;
817 d->file->update_status = file->update_status;
819 if (ran && !d->file->phony)
820 /* Fetch the new modification time.
821 We do this instead of just invalidating the cached time
822 so that a vpath_search can happen. Otherwise, it would
823 never be done because the target is already updated. */
824 (void) f_mtime (d->file, 0);
826 else if (file->update_status == -1)
827 /* Nothing was done for FILE, but it needed nothing done.
828 So mark it now as "succeeded". */
829 file->update_status = 0;
832 /* Check whether another file (whose mtime is THIS_MTIME)
833 needs updating on account of a dependency which is file FILE.
834 If it does, store 1 in *MUST_MAKE_PTR.
835 In the process, update any non-intermediate files
836 that FILE depends on (including FILE itself).
837 Return nonzero if any updating failed. */
839 static int
840 check_dep (file, depth, this_mtime, must_make_ptr)
841 struct file *file;
842 unsigned int depth;
843 FILE_TIMESTAMP this_mtime;
844 int *must_make_ptr;
846 struct dep *d;
847 int dep_status = 0;
849 ++depth;
850 start_updating (file);
852 if (!file->intermediate)
853 /* If this is a non-intermediate file, update it and record
854 whether it is newer than THIS_MTIME. */
856 FILE_TIMESTAMP mtime;
857 dep_status = update_file (file, depth);
858 check_renamed (file);
859 mtime = file_mtime (file);
860 check_renamed (file);
861 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
862 *must_make_ptr = 1;
864 else
866 /* FILE is an intermediate file. */
867 FILE_TIMESTAMP mtime;
869 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
871 if (try_implicit_rule (file, depth))
872 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
873 else
874 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
875 file->tried_implicit = 1;
877 if (file->cmds == 0 && !file->is_target
878 && default_file != 0 && default_file->cmds != 0)
880 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
881 file->cmds = default_file->cmds;
884 /* If the intermediate file actually exists
885 and is newer, then we should remake from it. */
886 check_renamed (file);
887 mtime = file_mtime (file);
888 check_renamed (file);
889 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
890 *must_make_ptr = 1;
891 /* Otherwise, update all non-intermediate files we depend on,
892 if necessary, and see whether any of them is more
893 recent than the file on whose behalf we are checking. */
894 else
896 struct dep *lastd;
898 lastd = 0;
899 d = file->deps;
900 while (d != 0)
902 int maybe_make;
904 if (is_updating (d->file))
906 error (NILF, _("Circular %s <- %s dependency dropped."),
907 file->name, d->file->name);
908 if (lastd == 0)
910 file->deps = d->next;
911 free ((char *) d);
912 d = file->deps;
914 else
916 lastd->next = d->next;
917 free ((char *) d);
918 d = lastd->next;
920 continue;
923 d->file->parent = file;
924 maybe_make = *must_make_ptr;
925 dep_status |= check_dep (d->file, depth, this_mtime,
926 &maybe_make);
927 if (! d->ignore_mtime)
928 *must_make_ptr = maybe_make;
929 check_renamed (d->file);
930 if (dep_status != 0 && !keep_going_flag)
931 break;
933 if (d->file->command_state == cs_running
934 || d->file->command_state == cs_deps_running)
935 /* Record that some of FILE's deps are still being made.
936 This tells the upper levels to wait on processing it until
937 the commands are finished. */
938 set_command_state (file, cs_deps_running);
940 lastd = d;
941 d = d->next;
946 finish_updating (file);
947 return dep_status;
950 /* Touch FILE. Return zero if successful, one if not. */
952 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
954 static int
955 touch_file (file)
956 register struct file *file;
958 if (!silent_flag)
959 message (0, "touch %s", file->name);
961 #ifndef NO_ARCHIVES
962 if (ar_name (file->name))
963 return ar_touch (file->name);
964 else
965 #endif
967 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
969 if (fd < 0)
970 TOUCH_ERROR ("touch: open: ");
971 else
973 struct stat statbuf;
974 char buf;
976 if (fstat (fd, &statbuf) < 0)
977 TOUCH_ERROR ("touch: fstat: ");
978 /* Rewrite character 0 same as it already is. */
979 if (read (fd, &buf, 1) < 0)
980 TOUCH_ERROR ("touch: read: ");
981 if (lseek (fd, 0L, 0) < 0L)
982 TOUCH_ERROR ("touch: lseek: ");
983 if (write (fd, &buf, 1) < 0)
984 TOUCH_ERROR ("touch: write: ");
985 /* If file length was 0, we just
986 changed it, so change it back. */
987 if (statbuf.st_size == 0)
989 (void) close (fd);
990 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
991 if (fd < 0)
992 TOUCH_ERROR ("touch: open: ");
994 (void) close (fd);
998 return 0;
1001 /* Having checked and updated the dependencies of FILE,
1002 do whatever is appropriate to remake FILE itself.
1003 Return the status from executing FILE's commands. */
1005 static void
1006 remake_file (file)
1007 struct file *file;
1009 if (file->cmds == 0)
1011 if (file->phony)
1012 /* Phony target. Pretend it succeeded. */
1013 file->update_status = 0;
1014 else if (file->is_target)
1015 /* This is a nonexistent target file we cannot make.
1016 Pretend it was successfully remade. */
1017 file->update_status = 0;
1018 else
1020 const char *msg_noparent
1021 = _("%sNo rule to make target `%s'%s");
1022 const char *msg_parent
1023 = _("%sNo rule to make target `%s', needed by `%s'%s");
1025 /* This is a dependency file we cannot remake. Fail. */
1026 if (!keep_going_flag && !file->dontcare)
1028 if (file->parent == 0)
1029 fatal (NILF, msg_noparent, "", file->name, "");
1031 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
1034 if (!file->dontcare)
1036 if (file->parent == 0)
1037 error (NILF, msg_noparent, "*** ", file->name, ".");
1038 else
1039 error (NILF, msg_parent, "*** ",
1040 file->name, file->parent->name, ".");
1042 file->update_status = 2;
1045 else
1047 chop_commands (file->cmds);
1049 /* The normal case: start some commands. */
1050 if (!touch_flag || file->cmds->any_recurse)
1052 execute_file_commands (file);
1053 return;
1056 /* This tells notice_finished_file it is ok to touch the file. */
1057 file->update_status = 0;
1060 /* This does the touching under -t. */
1061 notice_finished_file (file);
1064 /* Return the mtime of a file, given a `struct file'.
1065 Caches the time in the struct file to avoid excess stat calls.
1067 If the file is not found, and SEARCH is nonzero, VPATH searching and
1068 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1069 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1070 FILE. */
1072 FILE_TIMESTAMP
1073 f_mtime (file, search)
1074 register struct file *file;
1075 int search;
1077 FILE_TIMESTAMP mtime;
1079 /* File's mtime is not known; must get it from the system. */
1081 #ifndef NO_ARCHIVES
1082 if (ar_name (file->name))
1084 /* This file is an archive-member reference. */
1086 char *arname, *memname;
1087 struct file *arfile;
1088 int arname_used = 0;
1089 time_t member_date;
1091 /* Find the archive's name. */
1092 ar_parse_name (file->name, &arname, &memname);
1094 /* Find the modification time of the archive itself.
1095 Also allow for its name to be changed via VPATH search. */
1096 arfile = lookup_file (arname);
1097 if (arfile == 0)
1099 arfile = enter_file (arname);
1100 arname_used = 1;
1102 mtime = f_mtime (arfile, search);
1103 check_renamed (arfile);
1104 if (search && strcmp (arfile->hname, arname))
1106 /* The archive's name has changed.
1107 Change the archive-member reference accordingly. */
1109 char *name;
1110 unsigned int arlen, memlen;
1112 if (!arname_used)
1114 free (arname);
1115 arname_used = 1;
1118 arname = arfile->hname;
1119 arlen = strlen (arname);
1120 memlen = strlen (memname);
1122 /* free (file->name); */
1124 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1125 bcopy (arname, name, arlen);
1126 name[arlen] = '(';
1127 bcopy (memname, name + arlen + 1, memlen);
1128 name[arlen + 1 + memlen] = ')';
1129 name[arlen + 1 + memlen + 1] = '\0';
1131 /* If the archive was found with GPATH, make the change permanent;
1132 otherwise defer it until later. */
1133 if (arfile->name == arfile->hname)
1134 rename_file (file, name);
1135 else
1136 rehash_file (file, name);
1137 check_renamed (file);
1140 if (!arname_used)
1141 free (arname);
1142 free (memname);
1144 file->low_resolution_time = 1;
1146 if (mtime == NONEXISTENT_MTIME)
1147 /* The archive doesn't exist, so its members don't exist either. */
1148 return NONEXISTENT_MTIME;
1150 member_date = ar_member_date (file->hname);
1151 mtime = (member_date == (time_t) -1
1152 ? NONEXISTENT_MTIME
1153 : file_timestamp_cons (file->hname, member_date, 0));
1155 else
1156 #endif
1158 mtime = name_mtime (file->name);
1160 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1162 /* If name_mtime failed, search VPATH. */
1163 char *name = file->name;
1164 if (vpath_search (&name, &mtime)
1165 /* Last resort, is it a library (-lxxx)? */
1166 || (name[0] == '-' && name[1] == 'l'
1167 && library_search (&name, &mtime)))
1169 if (mtime != UNKNOWN_MTIME)
1170 /* vpath_search and library_search store UNKNOWN_MTIME
1171 if they didn't need to do a stat call for their work. */
1172 file->last_mtime = mtime;
1174 /* If we found it in VPATH, see if it's in GPATH too; if so,
1175 change the name right now; if not, defer until after the
1176 dependencies are updated. */
1177 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1179 rename_file (file, name);
1180 check_renamed (file);
1181 return file_mtime (file);
1184 rehash_file (file, name);
1185 check_renamed (file);
1186 mtime = name_mtime (name);
1192 /* Files can have bogus timestamps that nothing newly made will be
1193 "newer" than. Updating their dependents could just result in loops.
1194 So notify the user of the anomaly with a warning.
1196 We only need to do this once, for now. */
1198 if (!clock_skew_detected
1199 && mtime != NONEXISTENT_MTIME
1200 && !file->updated)
1202 static FILE_TIMESTAMP adjusted_now;
1204 FILE_TIMESTAMP adjusted_mtime = mtime;
1206 #if defined(WINDOWS32) || defined(__MSDOS__)
1207 /* Experimentation has shown that FAT filesystems can set file times
1208 up to 3 seconds into the future! Play it safe. */
1210 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1212 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1213 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1214 adjusted_mtime -= adjustment;
1215 #endif
1217 /* If the file's time appears to be in the future, update our
1218 concept of the present and try once more. */
1219 if (adjusted_now < adjusted_mtime)
1221 int resolution;
1222 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1223 adjusted_now = now + (resolution - 1);
1224 if (adjusted_now < adjusted_mtime)
1226 #ifdef NO_FLOAT
1227 error (NILF, _("Warning: File `%s' has modification time in the future"),
1228 file->name);
1229 #else
1230 double from_now =
1231 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1232 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1233 / 1e9));
1234 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1235 file->name, from_now);
1236 #endif
1237 clock_skew_detected = 1;
1243 /* Store the mtime into all the entries for this file. */
1244 if (file->double_colon)
1245 file = file->double_colon;
1249 /* If this file is not implicit but it is intermediate then it was
1250 made so by the .INTERMEDIATE target. If this file has never
1251 been built by us but was found now, it existed before make
1252 started. So, turn off the intermediate bit so make doesn't
1253 delete it, since it didn't create it. */
1254 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1255 && file->command_state == cs_not_started
1256 && !file->tried_implicit && file->intermediate)
1257 file->intermediate = 0;
1259 file->last_mtime = mtime;
1260 file = file->prev;
1262 while (file != 0);
1264 return mtime;
1268 /* Return the mtime of the file or archive-member reference NAME. */
1270 static FILE_TIMESTAMP
1271 name_mtime (name)
1272 register char *name;
1274 struct stat st;
1276 if (stat (name, &st) != 0)
1278 if (errno != ENOENT && errno != ENOTDIR)
1279 perror_with_name ("stat:", name);
1280 return NONEXISTENT_MTIME;
1283 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1287 /* Search for a library file specified as -lLIBNAME, searching for a
1288 suitable library file in the system library directories and the VPATH
1289 directories. */
1291 static int
1292 library_search (lib, mtime_ptr)
1293 char **lib;
1294 FILE_TIMESTAMP *mtime_ptr;
1296 static char *dirs[] =
1298 #ifndef _AMIGA
1299 "/lib",
1300 "/usr/lib",
1301 #endif
1302 #if defined(WINDOWS32) && !defined(LIBDIR)
1304 * This is completely up to the user at product install time. Just define
1305 * a placeholder.
1307 #define LIBDIR "."
1308 #endif
1309 LIBDIR, /* Defined by configuration. */
1313 static char *libpatterns = NULL;
1315 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1316 FILE_TIMESTAMP mtime;
1318 /* Loop variables for the libpatterns value. */
1319 char *p, *p2;
1320 unsigned int len;
1322 char *file, **dp;
1324 /* If we don't have libpatterns, get it. */
1325 if (!libpatterns)
1327 int save = warn_undefined_variables_flag;
1328 warn_undefined_variables_flag = 0;
1330 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1332 warn_undefined_variables_flag = save;
1335 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1336 p2 = libpatterns;
1337 while ((p = find_next_token (&p2, &len)) != 0)
1339 static char *buf = NULL;
1340 static int buflen = 0;
1341 static int libdir_maxlen = -1;
1342 char *libbuf = variable_expand ("");
1344 /* Expand the pattern using LIBNAME as a replacement. */
1346 char c = p[len];
1347 char *p3, *p4;
1349 p[len] = '\0';
1350 p3 = find_percent (p);
1351 if (!p3)
1353 /* Give a warning if there is no pattern, then remove the
1354 pattern so it's ignored next time. */
1355 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1356 for (; len; --len, ++p)
1357 *p = ' ';
1358 *p = c;
1359 continue;
1361 p4 = variable_buffer_output (libbuf, p, p3-p);
1362 p4 = variable_buffer_output (p4, libname, strlen (libname));
1363 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1364 p[len] = c;
1367 /* Look first for `libNAME.a' in the current directory. */
1368 mtime = name_mtime (libbuf);
1369 if (mtime != NONEXISTENT_MTIME)
1371 *lib = xstrdup (libbuf);
1372 if (mtime_ptr != 0)
1373 *mtime_ptr = mtime;
1374 return 1;
1377 /* Now try VPATH search on that. */
1379 file = libbuf;
1380 if (vpath_search (&file, mtime_ptr))
1382 *lib = file;
1383 return 1;
1386 /* Now try the standard set of directories. */
1388 if (!buflen)
1390 for (dp = dirs; *dp != 0; ++dp)
1392 int l = strlen (*dp);
1393 if (l > libdir_maxlen)
1394 libdir_maxlen = l;
1396 buflen = strlen (libbuf);
1397 buf = xmalloc(libdir_maxlen + buflen + 2);
1399 else if (buflen < strlen (libbuf))
1401 buflen = strlen (libbuf);
1402 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1405 for (dp = dirs; *dp != 0; ++dp)
1407 sprintf (buf, "%s/%s", *dp, libbuf);
1408 mtime = name_mtime (buf);
1409 if (mtime != NONEXISTENT_MTIME)
1411 *lib = xstrdup (buf);
1412 if (mtime_ptr != 0)
1413 *mtime_ptr = mtime;
1414 return 1;
1419 return 0;