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)
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. */
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))\
53 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
55 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
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. */
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
;
89 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (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. */
103 for (g
= goals
; g
!= 0; g
= g
->next
)
107 /* All files start with the considered bit 0, so the global value is 1. */
110 /* Update all the goals until they are all finished. */
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);
128 /* Iterate over all double-colon entries for this file. */
130 int stop
= 0, any_not_updated
= 0;
132 for (file
= g
->file
->double_colon
? g
->file
->double_colon
: g
->file
;
136 unsigned int ocommands_started
;
138 check_renamed (file
);
139 if (rebuilding_makefiles
)
141 if (file
->cmd_target
)
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
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. */
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
);
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
))
199 if (rebuilding_makefiles
&& file
->dontcare
)
200 /* This is a default makefile; stop remaking. */
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
;
214 /* Reset FILE since it is null at the end of the loop. */
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.")),
234 /* This goal is finished. Remove it from the chain. */
238 lastgoal
->next
= g
->next
;
240 /* Free the storage. */
243 g
= lastgoal
== 0 ? goals
: lastgoal
->next
;
255 /* If we reached the end of the dependency graph toggle the considered
256 flag for the next pass. */
258 considered
= !considered
;
261 if (rebuilding_makefiles
)
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
280 If there are multiple double-colon entries for FILE,
281 each is considered in turn. */
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
);
310 if (status
!= 0 && !keep_going_flag
)
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. */
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
)
330 f
->considered
= considered
;
332 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
333 status
|= update_file (d
->file
, depth
+ 1);
339 /* Show a message stating the target failed to build. */
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
, ".");
360 error (NILF
, msg_parent
, "*** ", file
->name
, file
->parent
->name
, ".");
363 /* Consider a single `struct file' and update it as appropriate. */
366 update_file_1 (struct file
*file
, unsigned int depth
)
368 register FILE_TIMESTAMP this_mtime
;
369 int noexist
, must_make
, deps_changed
;
371 register struct dep
*d
, *lastd
;
374 DBF (DB_VERBOSE
, _("Considering target file `%s'.\n"));
378 if (file
->update_status
> 0)
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
387 if (file
->dontcare
&& !rebuilding_makefiles
)
393 return file
->update_status
;
396 DBF (DB_VERBOSE
, _("File `%s' was considered already.\n"));
400 switch (file
->command_state
)
403 case cs_deps_running
:
406 DBF (DB_VERBOSE
, _("Still updating file `%s'.\n"));
409 DBF (DB_VERBOSE
, _("Finished updating file `%s'.\n"));
410 return file
->update_status
;
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
;
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
);
437 error (NILF
, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
439 this_mtime
+= FILE_TIMESTAMPS_PER_S
- 1 - ns
;
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"));
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. */
469 FILE_TIMESTAMP mtime
;
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
486 file
->deps
= d
->next
;
488 lastd
->next
= d
->next
;
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
;
521 running
|= (f
->command_state
== cs_running
522 || f
->command_state
== cs_deps_running
);
528 if (dep_status
!= 0 && !keep_going_flag
)
532 d
->changed
= file_mtime (d
->file
) != mtime
;
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
)
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
;
574 running
|= (f
->command_state
== cs_running
575 || f
->command_state
== cs_deps_running
);
581 if (dep_status
!= 0 && !keep_going_flag
)
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"));
596 set_command_state (file
, cs_deps_running
);
598 DBF (DB_VERBOSE
, _("The prerequisites of `%s' are being made.\n"));
602 /* If any dependency failed, give up now. */
606 file
->update_status
= dep_status
;
607 notice_finished_file (file
);
611 DBF (DB_VERBOSE
, _("Giving up on target file `%s'.\n"));
613 if (depth
== 0 && keep_going_flag
614 && !just_print_flag
&& !question_flag
)
616 _("Target `%s' not remade because of errors."), file
->name
);
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 $?. */
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
)
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. */
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
))
667 if (ISDB (DB_VERBOSE
))
668 fmt
= _("Prerequisite `%s' is order-only for target `%s'.\n");
670 else if (d_mtime
== NONEXISTENT_MTIME
)
673 fmt
= _("Prerequisite `%s' of target `%s' does not exist.\n");
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");
685 print_spaces (depth
);
686 printf (fmt
, dep_name (d
), file
->name
);
692 /* Here depth returns to the value it had when we were called. */
695 if (file
->double_colon
&& file
->deps
== 0)
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
)
706 _("No commands for `%s' and no prerequisites actually changed.\n"));
708 else if (!must_make
&& file
->cmds
!= 0 && always_make_flag
)
711 DBF (DB_VERBOSE
, _("Making `%s' due to always-make flag.\n"));
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
);
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. */
734 file
->name
= file
->hname
;
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
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. */
754 if (file
->command_state
!= cs_finished
)
756 DBF (DB_VERBOSE
, _("Commands of `%s' are being run.\n"));
760 switch (file
->update_status
)
763 DBF (DB_BASIC
, _("Failed to remake target file `%s'.\n"));
766 DBF (DB_BASIC
, _("Successfully remade target file `%s'.\n"));
769 DBF (DB_BASIC
, _("Target file `%s' needs remade under -q.\n"));
772 assert (file
->update_status
>= 0 && file
->update_status
<= 2);
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
784 On return, FILE->update_status will no longer be -1 if it was. */
787 notice_finished_file (struct file
*file
)
790 int ran
= file
->command_state
== cs_running
;
793 file
->command_state
= cs_finished
;
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. */
810 for (i
= 0; i
< file
->cmds
->ncommand_lines
; ++i
)
811 if (!(file
->cmds
->lines_flags
[i
] & COMMANDS_RECURSE
))
812 goto have_nonrecursing
;
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. */
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. */
838 if (file
->mtime_before_update
== UNKNOWN_MTIME
)
839 file
->mtime_before_update
= file
->last_mtime
;
841 if ((ran
&& !file
->phony
) || touched
)
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
))
857 /* If there were no commands at all, it's always new. */
859 else if (file
->is_target
&& file
->cmds
== 0)
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. */
902 check_dep (struct file
*file
, unsigned int depth
,
903 FILE_TIMESTAMP this_mtime
, int *must_make_ptr
)
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
)
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"));
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
)
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. */
963 if (is_updating (d
->file
))
965 error (NILF
, _("Circular %s <- %s dependency dropped."),
966 file
->name
, d
->file
->name
);
969 file
->deps
= d
->next
;
975 lastd
->next
= d
->next
;
982 d
->file
->parent
= file
;
983 maybe_make
= *must_make_ptr
;
984 dep_status
|= check_dep (d
->file
, depth
, this_mtime
,
986 if (! d
->ignore_mtime
)
987 *must_make_ptr
= maybe_make
;
988 check_renamed (d
->file
);
989 if (dep_status
!= 0 && !keep_going_flag
)
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
);
1005 finish_updating (file
);
1009 /* Touch FILE. Return zero if successful, one if not. */
1011 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1014 touch_file (struct file
*file
)
1017 message (0, "touch %s", file
->name
);
1020 if (ar_name (file
->name
))
1021 return ar_touch (file
->name
);
1025 int fd
= open (file
->name
, O_RDWR
| O_CREAT
, 0666);
1028 TOUCH_ERROR ("touch: open: ");
1031 struct stat statbuf
;
1035 EINTRLOOP (e
, fstat (fd
, &statbuf
));
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)
1050 fd
= open (file
->name
, O_RDWR
| O_TRUNC
, 0666);
1052 TOUCH_ERROR ("touch: open: ");
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. */
1066 remake_file (struct file
*file
)
1068 if (file
->cmds
== 0)
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;
1079 /* This is a dependency file we cannot remake. Fail. */
1080 if (!rebuilding_makefiles
|| !file
->dontcare
)
1082 file
->update_status
= 2;
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
);
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
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. */
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;
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
);
1137 arfile
= enter_file (arname
);
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. */
1148 unsigned int arlen
, memlen
;
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
);
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
);
1174 rehash_file (file
, name
);
1175 check_renamed (file
);
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
1191 : file_timestamp_cons (file
->hname
, member_date
, 0));
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
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
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
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
)
1268 FILE_TIMESTAMP now
= file_timestamp_now (&resolution
);
1269 adjusted_now
= now
+ (resolution
- 1);
1270 if (adjusted_now
< adjusted_mtime
)
1273 error (NILF
, _("Warning: File `%s' has modification time in the future"),
1277 (FILE_TIMESTAMP_S (mtime
) - FILE_TIMESTAMP_S (now
)
1278 + ((FILE_TIMESTAMP_NS (mtime
) - FILE_TIMESTAMP_NS (now
))
1280 error (NILF
, _("Warning: File `%s' has modification time %.2g s in the future"),
1281 file
->name
, from_now
);
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
;
1314 /* Return the mtime of the file or archive-member reference NAME. */
1316 static FILE_TIMESTAMP
1317 name_mtime (char *name
)
1322 EINTRLOOP (e
, stat (name
, &st
));
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
1339 library_search (char **lib
, FILE_TIMESTAMP
*mtime_ptr
)
1341 static char *dirs
[] =
1347 #if defined(WINDOWS32) && !defined(LIBDIR)
1349 * This is completely up to the user at product install time. Just define
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. */
1369 /* If we don't have libpatterns, get it. */
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. */
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. */
1395 p3
= find_percent (p
);
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
)
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
));
1412 /* Look first for `libNAME.a' in the current directory. */
1413 mtime
= name_mtime (libbuf
);
1414 if (mtime
!= NONEXISTENT_MTIME
)
1416 *lib
= xstrdup (libbuf
);
1422 /* Now try VPATH search on that. */
1425 if (vpath_search (&file
, mtime_ptr
))
1431 /* Now try the standard set of directories. */
1435 for (dp
= dirs
; *dp
!= 0; ++dp
)
1437 int l
= strlen (*dp
);
1438 if (l
> libdir_maxlen
)
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
);