1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97,99 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
42 extern int try_implicit_rule
PARAMS ((struct file
*file
, unsigned int depth
));
45 /* Incremented when a command is started (under -n, when one would be). */
46 unsigned int commands_started
= 0;
48 static int update_file
PARAMS ((struct file
*file
, unsigned int depth
));
49 static int update_file_1
PARAMS ((struct file
*file
, unsigned int depth
));
50 static int check_dep
PARAMS ((struct file
*file
, unsigned int depth
, FILE_TIMESTAMP this_mtime
, int *must_make_ptr
));
51 static int touch_file
PARAMS ((struct file
*file
));
52 static void remake_file
PARAMS ((struct file
*file
));
53 static FILE_TIMESTAMP name_mtime
PARAMS ((char *name
));
54 static int library_search
PARAMS ((char **lib
, FILE_TIMESTAMP
*mtime_ptr
));
57 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
58 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
59 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
60 be disabled for them unless they were also command-line targets, and we
61 should only make one goal at a time and return as soon as one goal whose
62 `changed' member is nonzero is successfully made. */
64 /* We need to know this "lower down" for correct error handling. */
65 static int updating_makefiles
= 0;
68 update_goal_chain (goals
, makefiles
)
69 register struct dep
*goals
;
72 int t
= touch_flag
, q
= question_flag
, n
= just_print_flag
;
73 unsigned int j
= job_slots
;
76 updating_makefiles
= makefiles
;
78 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
81 /* Duplicate the chain so we can remove things from it. */
83 goals
= copy_dep_chain (goals
);
86 /* Clear the `changed' flag of each goal in the chain.
87 We will use the flag below to notice when any commands
88 have actually been run for a target. When no commands
89 have been run, we give an "up to date" diagnostic. */
92 for (g
= goals
; g
!= 0; g
= g
->next
)
96 /* All files start with the considered bit 0, so the global value is 1. */
99 /* Update all the goals until they are all finished. */
103 register struct dep
*g
, *lastgoal
;
105 /* Start jobs that are waiting for the load to go down. */
107 start_waiting_jobs ();
109 /* Wait for a child to die. */
111 reap_children (1, 0);
117 /* Iterate over all double-colon entries for this file. */
119 int stop
= 0, any_not_updated
= 0;
121 for (file
= g
->file
->double_colon
? g
->file
->double_colon
: g
->file
;
125 unsigned int ocommands_started
;
127 check_renamed (file
);
130 if (file
->cmd_target
)
137 touch_flag
= question_flag
= just_print_flag
= 0;
140 /* Save the old value of `commands_started' so we can compare
141 later. It will be incremented when any commands are
143 ocommands_started
= commands_started
;
145 x
= update_file (file
, makefiles
? 1 : 0);
146 check_renamed (file
);
148 /* Set the goal's `changed' flag if any commands were started
149 by calling update_file above. We check this flag below to
150 decide when to give an "up to date" diagnostic. */
151 g
->changed
+= commands_started
- ocommands_started
;
153 /* If we updated a file and STATUS was not already 1, set it to
154 1 if updating failed, or to 0 if updating succeeded. Leave
155 STATUS as it is if no updating was done. */
158 if ((x
!= 0 || file
->updated
) && status
< 1)
160 if (file
->update_status
!= 0)
162 /* Updating failed, or -q triggered. The STATUS value
163 tells our caller which. */
164 status
= file
->update_status
;
165 /* If -q just triggered, stop immediately. It doesn't
166 matter how much more we run, since we already know
167 the answer to return. */
168 stop
= (!keep_going_flag
&& !question_flag
173 FILE_TIMESTAMP mtime
= MTIME (file
);
174 check_renamed (file
);
176 if (file
->updated
&& g
->changed
&&
177 mtime
!= file
->mtime_before_update
)
179 /* Updating was done. If this is a makefile and
180 just_print_flag or question_flag is set (meaning
181 -n or -q was given and this file was specified
182 as a command-line target), don't change STATUS.
183 If STATUS is changed, we will get re-exec'd, and
184 enter an infinite loop. */
186 || (!just_print_flag
&& !question_flag
))
188 if (makefiles
&& file
->dontcare
)
189 /* This is a default makefile; stop remaking. */
195 /* Keep track if any double-colon entry is not finished.
196 When they are all finished, the goal is finished. */
197 any_not_updated
|= !file
->updated
;
203 /* Reset FILE since it is null at the end of the loop. */
206 if (stop
|| !any_not_updated
)
208 /* If we have found nothing whatever to do for the goal,
209 print a message saying nothing needs doing. */
212 /* If the update_status is zero, we updated successfully
213 or not at all. G->changed will have been set above if
214 any commands were actually started for this goal. */
215 && file
->update_status
== 0 && !g
->changed
216 /* Never give a message under -s or -q. */
217 && !silent_flag
&& !question_flag
)
218 message (1, ((file
->phony
|| file
->cmds
== 0)
219 ? _("Nothing to be done for `%s'.")
220 : _("`%s' is up to date.")),
223 /* This goal is finished. Remove it from the chain. */
227 lastgoal
->next
= g
->next
;
229 /* Free the storage. */
232 g
= lastgoal
== 0 ? goals
: lastgoal
->next
;
244 /* If we reached the end of the dependency graph toggle the considered
245 flag for the next pass. */
247 considered
= !considered
;
260 /* Generate an error/fatal message if no rules are available for the target.
266 static const char msg_noparent
[]
267 = _("%sNo rule to make target `%s'%s");
268 static const char msg_parent
[]
269 = _("%sNo rule to make target `%s', needed by `%s'%s");
271 if (keep_going_flag
|| file
->dontcare
)
273 /* If the previous attempt was made while we were creating
274 makefiles, but we aren't anymore, print an error now. */
276 || (file
->mfile_status
&& !updating_makefiles
))
278 if (file
->parent
== 0)
279 error (NILF
, msg_noparent
, "*** ", file
->name
, ".");
281 error (NILF
, msg_parent
, "*** ",
282 file
->name
, file
->parent
->name
, ".");
284 file
->update_status
= 2;
285 file
->mfile_status
= updating_makefiles
;
287 else if (file
->parent
== 0)
288 fatal (NILF
, msg_noparent
, "", file
->name
, "");
290 fatal (NILF
, msg_parent
, "", file
->name
, file
->parent
->name
, "");
293 /* If FILE is not up to date, execute the commands for it.
294 Return 0 if successful, 1 if unsuccessful;
295 but with some flag settings, just call `exit' if unsuccessful.
297 DEPTH is the depth in recursions of this function.
298 We increment it during the consideration of our dependencies,
299 then decrement it again after finding out whether this file
302 If there are multiple double-colon entries for FILE,
303 each is considered in turn. */
306 update_file (file
, depth
)
310 register int status
= 0;
311 register struct file
*f
;
313 for (f
= file
->double_colon
? file
->double_colon
: file
; f
!= 0; f
= f
->prev
)
315 /* Prune the dependency graph: if we've already been here on _this_
316 pass through the dependency graph, we don't have to go any further.
317 We won't reap_children until we start the next pass, so no state
318 change is possible below here until then. */
319 if (f
->considered
== considered
)
321 DEBUGPR (_("Pruning file `%s'.\n"));
324 f
->considered
= considered
;
326 status
|= update_file_1 (f
, depth
);
329 if (status
!= 0 && !keep_going_flag
)
332 switch (f
->command_state
)
335 /* The file is done being remade. */
339 case cs_deps_running
:
340 /* Don't run the other :: rules for this
341 file until this rule is finished. */
345 assert (f
->command_state
== cs_running
);
353 /* Consider a single `struct file' and update it as appropriate. */
356 update_file_1 (file
, depth
)
360 register FILE_TIMESTAMP this_mtime
;
361 int noexist
, must_make
, deps_changed
;
363 register struct dep
*d
, *lastd
;
366 DEBUGPR (_("Considering target file `%s'.\n"));
370 if (file
->update_status
> 0)
372 DEBUGPR (_("Recently tried and failed to update file `%s'.\n"));
374 return file
->update_status
;
377 DEBUGPR (_("File `%s' was considered already.\n"));
381 switch (file
->command_state
)
384 case cs_deps_running
:
387 DEBUGPR (_("Still updating file `%s'.\n"));
390 DEBUGPR (_("Finished updating file `%s'.\n"));
391 return file
->update_status
;
398 /* Notice recursive update of the same file. */
401 /* Looking at the file's modtime beforehand allows the possibility
402 that its name may be changed by a VPATH search, and thus it may
403 not need an implicit rule. If this were not done, the file
404 might get implicit commands that apply to its initial name, only
405 to have that name replaced with another found by VPATH search. */
407 this_mtime
= file_mtime (file
);
408 check_renamed (file
);
409 noexist
= this_mtime
== (FILE_TIMESTAMP
) -1;
411 DEBUGPR (_("File `%s' does not exist.\n"));
415 /* If file was specified as a target with no commands,
416 come up with some default commands. */
418 if (!file
->phony
&& file
->cmds
== 0 && !file
->tried_implicit
)
420 if (try_implicit_rule (file
, depth
))
421 DEBUGPR (_("Found an implicit rule for `%s'.\n"));
423 DEBUGPR (_("No implicit rule found for `%s'.\n"));
424 file
->tried_implicit
= 1;
426 if (file
->cmds
== 0 && !file
->is_target
427 && default_file
!= 0 && default_file
->cmds
!= 0)
429 DEBUGPR (_("Using default commands for `%s'.\n"));
430 file
->cmds
= default_file
->cmds
;
433 /* Update all non-intermediate files we depend on, if necessary,
434 and see whether any of them is more recent than this file. */
440 FILE_TIMESTAMP mtime
;
442 check_renamed (d
->file
);
444 mtime
= file_mtime (d
->file
);
445 check_renamed (d
->file
);
447 if (d
->file
->updating
)
449 error (NILF
, _("Circular %s <- %s dependency dropped."),
450 file
->name
, d
->file
->name
);
451 /* We cannot free D here because our the caller will still have
452 a reference to it when we were called recursively via
455 file
->deps
= d
->next
;
457 lastd
->next
= d
->next
;
462 d
->file
->parent
= file
;
463 dep_status
|= check_dep (d
->file
, depth
, this_mtime
, &must_make
);
464 check_renamed (d
->file
);
467 register struct file
*f
= d
->file
;
472 running
|= (f
->command_state
== cs_running
473 || f
->command_state
== cs_deps_running
);
479 if (dep_status
!= 0 && !keep_going_flag
)
483 d
->changed
= file_mtime (d
->file
) != mtime
;
489 /* Now we know whether this target needs updating.
490 If it does, update all the intermediate files we depend on. */
494 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
495 if (d
->file
->intermediate
)
497 FILE_TIMESTAMP mtime
= file_mtime (d
->file
);
498 check_renamed (d
->file
);
499 d
->file
->parent
= file
;
500 dep_status
|= update_file (d
->file
, depth
);
501 check_renamed (d
->file
);
504 register struct file
*f
= d
->file
;
509 running
|= (f
->command_state
== cs_running
510 || f
->command_state
== cs_deps_running
);
516 if (dep_status
!= 0 && !keep_going_flag
)
520 d
->changed
= ((file
->phony
&& file
->cmds
!= 0)
521 || file_mtime (d
->file
) != mtime
);
527 DEBUGPR (_("Finished prerequisites of target file `%s'.\n"));
531 set_command_state (file
, cs_deps_running
);
533 DEBUGPR (_("The prerequisites of `%s' are being made.\n"));
537 /* If any dependency failed, give up now. */
541 file
->update_status
= dep_status
;
542 notice_finished_file (file
);
546 DEBUGPR (_("Giving up on target file `%s'.\n"));
548 if (depth
== 0 && keep_going_flag
549 && !just_print_flag
&& !question_flag
)
551 _("Target `%s' not remade because of errors."), file
->name
);
556 if (file
->command_state
== cs_deps_running
)
557 /* The commands for some deps were running on the last iteration, but
558 they have finished now. Reset the command_state to not_started to
559 simplify later bookkeeping. It is important that we do this only
560 when the prior state was cs_deps_running, because that prior state
561 was definitely propagated to FILE's also_make's by set_command_state
562 (called above), but in another state an also_make may have
563 independently changed to finished state, and we would confuse that
564 file's bookkeeping (updated, but not_started is bogus state). */
565 set_command_state (file
, cs_not_started
);
567 /* Now record which dependencies are more
568 recent than this file, so we can define $?. */
571 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
573 FILE_TIMESTAMP d_mtime
= file_mtime (d
->file
);
574 check_renamed (d
->file
);
576 #if 1 /* %%% 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
== (FILE_TIMESTAMP
) -1 && !d
->file
->intermediate
)
580 /* We must remake if this dep does not
581 exist and is not intermediate. */
585 /* Set DEPS_CHANGED if this dep actually changed. */
586 deps_changed
|= d
->changed
;
588 /* Set D->changed if either this dep actually changed,
589 or its dependent, FILE, is older or does not exist. */
590 d
->changed
|= noexist
|| d_mtime
> this_mtime
;
592 if (debug_flag
&& !noexist
)
594 print_spaces (depth
);
595 if (d_mtime
== (FILE_TIMESTAMP
) -1)
596 printf (_("Prerequisite `%s' does not exist.\n"), dep_name (d
));
598 printf (_("Prerequisite `%s' is %s than target `%s'.\n"),
599 dep_name (d
), d
->changed
? _("newer") : _("older"), file
->name
);
604 /* Here depth returns to the value it had when we were called. */
607 if (file
->double_colon
&& file
->deps
== 0)
610 DEBUGPR (_("Target `%s' is double-colon and has no prerequisites.\n"));
612 else if (!noexist
&& file
->is_target
&& !deps_changed
&& file
->cmds
== 0)
615 DEBUGPR (_("No commands for `%s' and no prerequisites actually changed.\n"));
623 printf(_("No need to remake target `%s'"), file
->name
);
624 if (!streq(file
->name
, file
->hname
))
625 printf(_("; using VPATH name `%s'"), file
->hname
);
630 notice_finished_file (file
);
632 /* Since we don't need to remake the file, convert it to use the
633 VPATH filename if we found one. hfile will be either the
634 local name if no VPATH or the VPATH name if one was found. */
638 file
->name
= file
->hname
;
645 DEBUGPR (_("Must remake target `%s'.\n"));
647 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
649 if (!streq(file
->name
, file
->hname
))
653 print_spaces (depth
);
654 printf(_(" Ignoring VPATH name `%s'.\n"), file
->hname
);
657 file
->ignore_vpath
= 1;
660 /* Now, take appropriate actions to remake the file. */
663 if (file
->command_state
!= cs_finished
)
665 DEBUGPR (_("Commands of `%s' are being run.\n"));
669 switch (file
->update_status
)
672 DEBUGPR (_("Failed to remake target file `%s'.\n"));
675 DEBUGPR (_("Successfully remade target file `%s'.\n"));
678 DEBUGPR (_("Target file `%s' needs remade under -q.\n"));
681 assert (file
->update_status
>= 0 && file
->update_status
<= 2);
686 return file
->update_status
;
689 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
690 files listed in its `also_make' member. Under -t, this function also
693 On return, FILE->update_status will no longer be -1 if it was. */
696 notice_finished_file (file
)
697 register struct file
*file
;
700 int ran
= file
->command_state
== cs_running
;
702 file
->command_state
= cs_finished
;
706 /* The update status will be:
707 -1 if this target was not remade;
708 0 if 0 or more commands (+ or ${MAKE}) were run and won;
709 1 if some commands were run and lost.
710 We touch the target if it has commands which either were not run
711 or won when they ran (i.e. status is 0). */
712 && file
->update_status
== 0)
714 if (file
->cmds
!= 0 && file
->cmds
->any_recurse
)
716 /* If all the command lines were recursive,
717 we don't want to do the touching. */
719 for (i
= 0; i
< file
->cmds
->ncommand_lines
; ++i
)
720 if (!(file
->cmds
->lines_flags
[i
] & COMMANDS_RECURSE
))
721 goto have_nonrecursing
;
727 file
->update_status
= 0;
729 /* Should set file's modification date and do nothing else. */
730 file
->update_status
= touch_file (file
);
734 if (file
->mtime_before_update
== 0)
735 file
->mtime_before_update
= file
->last_mtime
;
737 if (ran
&& !file
->phony
)
742 /* If -n or -q and all the commands are recursive, we ran them so
743 really check the target's mtime again. Otherwise, assume the target
744 would have been updated. */
746 if (question_flag
|| just_print_flag
)
747 for (i
= file
->cmds
->ncommand_lines
; i
> 0; --i
)
748 if (! (file
->cmds
->lines_flags
[i
-1] & COMMANDS_RECURSE
))
751 /* If there were no commands at all, it's always new. */
753 else if (file
->is_target
&& file
->cmds
== 0)
756 file
->last_mtime
= i
== 0 ? 0 : NEW_MTIME
;
758 /* Propagate the change of modification time to all the double-colon
759 entries for this file. */
760 for (f
= file
->double_colon
; f
!= 0; f
= f
->next
)
761 f
->last_mtime
= file
->last_mtime
;
764 if (ran
&& file
->update_status
!= -1)
765 /* We actually tried to update FILE, which has
766 updated its also_make's as well (if it worked).
767 If it didn't work, it wouldn't work again for them.
768 So mark them as updated with the same status. */
769 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
771 d
->file
->command_state
= cs_finished
;
772 d
->file
->updated
= 1;
773 d
->file
->update_status
= file
->update_status
;
775 if (ran
&& !d
->file
->phony
)
776 /* Fetch the new modification time.
777 We do this instead of just invalidating the cached time
778 so that a vpath_search can happen. Otherwise, it would
779 never be done because the target is already updated. */
780 (void) f_mtime (d
->file
, 0);
782 else if (file
->update_status
== -1)
783 /* Nothing was done for FILE, but it needed nothing done.
784 So mark it now as "succeeded". */
785 file
->update_status
= 0;
788 /* Check whether another file (whose mtime is THIS_MTIME)
789 needs updating on account of a dependency which is file FILE.
790 If it does, store 1 in *MUST_MAKE_PTR.
791 In the process, update any non-intermediate files
792 that FILE depends on (including FILE itself).
793 Return nonzero if any updating failed. */
796 check_dep (file
, depth
, this_mtime
, must_make_ptr
)
799 FILE_TIMESTAMP this_mtime
;
802 register struct dep
*d
;
808 if (!file
->intermediate
)
809 /* If this is a non-intermediate file, update it and record
810 whether it is newer than THIS_MTIME. */
812 FILE_TIMESTAMP mtime
;
813 dep_status
= update_file (file
, depth
);
814 check_renamed (file
);
815 mtime
= file_mtime (file
);
816 check_renamed (file
);
817 if (mtime
== (FILE_TIMESTAMP
) -1 || mtime
> this_mtime
)
822 /* FILE is an intermediate file. */
823 FILE_TIMESTAMP mtime
;
825 if (!file
->phony
&& file
->cmds
== 0 && !file
->tried_implicit
)
827 if (try_implicit_rule (file
, depth
))
828 DEBUGPR (_("Found an implicit rule for `%s'.\n"));
830 DEBUGPR (_("No implicit rule found for `%s'.\n"));
831 file
->tried_implicit
= 1;
833 if (file
->cmds
== 0 && !file
->is_target
834 && default_file
!= 0 && default_file
->cmds
!= 0)
836 DEBUGPR (_("Using default commands for `%s'.\n"));
837 file
->cmds
= default_file
->cmds
;
840 /* If the intermediate file actually exists
841 and is newer, then we should remake from it. */
842 check_renamed (file
);
843 mtime
= file_mtime (file
);
844 check_renamed (file
);
845 if (mtime
!= (FILE_TIMESTAMP
) -1 && mtime
> this_mtime
)
847 /* Otherwise, update all non-intermediate files we depend on,
848 if necessary, and see whether any of them is more
849 recent than the file on whose behalf we are checking. */
852 register struct dep
*lastd
;
858 if (d
->file
->updating
)
860 error (NILF
, _("Circular %s <- %s dependency dropped."),
861 file
->name
, d
->file
->name
);
864 file
->deps
= d
->next
;
870 lastd
->next
= d
->next
;
877 d
->file
->parent
= file
;
878 dep_status
|= check_dep (d
->file
, depth
, this_mtime
,
880 check_renamed (d
->file
);
881 if (dep_status
!= 0 && !keep_going_flag
)
884 if (d
->file
->command_state
== cs_running
885 || d
->file
->command_state
== cs_deps_running
)
886 /* Record that some of FILE's deps are still being made.
887 This tells the upper levels to wait on processing it until
888 the commands are finished. */
889 set_command_state (file
, cs_deps_running
);
901 /* Touch FILE. Return zero if successful, one if not. */
903 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
907 register struct file
*file
;
910 message (0, "touch %s", file
->name
);
913 if (ar_name (file
->name
))
914 return ar_touch (file
->name
);
918 int fd
= open (file
->name
, O_RDWR
| O_CREAT
, 0666);
921 TOUCH_ERROR ("touch: open: ");
929 status
= fstat (fd
, &statbuf
);
930 while (status
< 0 && EINTR_SET
);
933 TOUCH_ERROR ("touch: fstat: ");
934 /* Rewrite character 0 same as it already is. */
935 if (read (fd
, &buf
, 1) < 0)
936 TOUCH_ERROR ("touch: read: ");
937 if (lseek (fd
, 0L, 0) < 0L)
938 TOUCH_ERROR ("touch: lseek: ");
939 if (write (fd
, &buf
, 1) < 0)
940 TOUCH_ERROR ("touch: write: ");
941 /* If file length was 0, we just
942 changed it, so change it back. */
943 if (statbuf
.st_size
== 0)
946 fd
= open (file
->name
, O_RDWR
| O_TRUNC
, 0666);
948 TOUCH_ERROR ("touch: open: ");
957 /* Having checked and updated the dependencies of FILE,
958 do whatever is appropriate to remake FILE itself.
959 Return the status from executing FILE's commands. */
968 /* Phony target. Pretend it succeeded. */
969 file
->update_status
= 0;
970 else if (file
->is_target
)
971 /* This is a nonexistent target file we cannot make.
972 Pretend it was successfully remade. */
973 file
->update_status
= 0;
975 no_rule_error (file
);
979 chop_commands (file
->cmds
);
981 /* The normal case: start some commands. */
982 if (!touch_flag
|| file
->cmds
->any_recurse
)
984 execute_file_commands (file
);
988 /* This tells notice_finished_file it is ok to touch the file. */
989 file
->update_status
= 0;
992 /* This does the touching under -t. */
993 notice_finished_file (file
);
996 /* Return the mtime of a file, given a `struct file'.
997 Caches the time in the struct file to avoid excess stat calls.
999 If the file is not found, and SEARCH is nonzero, VPATH searching and
1000 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1001 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1005 f_mtime (file
, search
)
1006 register struct file
*file
;
1009 FILE_TIMESTAMP mtime
;
1011 /* File's mtime is not known; must get it from the system. */
1014 if (ar_name (file
->name
))
1016 /* This file is an archive-member reference. */
1018 char *arname
, *memname
;
1019 struct file
*arfile
;
1020 int arname_used
= 0;
1022 /* Find the archive's name. */
1023 ar_parse_name (file
->name
, &arname
, &memname
);
1025 /* Find the modification time of the archive itself.
1026 Also allow for its name to be changed via VPATH search. */
1027 arfile
= lookup_file (arname
);
1030 arfile
= enter_file (arname
);
1033 mtime
= f_mtime (arfile
, search
);
1034 check_renamed (arfile
);
1035 if (search
&& strcmp (arfile
->hname
, arname
))
1037 /* The archive's name has changed.
1038 Change the archive-member reference accordingly. */
1041 unsigned int arlen
, memlen
;
1049 arname
= arfile
->hname
;
1050 arlen
= strlen (arname
);
1051 memlen
= strlen (memname
);
1053 /* free (file->name); */
1055 name
= (char *) xmalloc (arlen
+ 1 + memlen
+ 2);
1056 bcopy (arname
, name
, arlen
);
1058 bcopy (memname
, name
+ arlen
+ 1, memlen
);
1059 name
[arlen
+ 1 + memlen
] = ')';
1060 name
[arlen
+ 1 + memlen
+ 1] = '\0';
1062 /* If the archive was found with GPATH, make the change permanent;
1063 otherwise defer it until later. */
1064 if (arfile
->name
== arfile
->hname
)
1065 rename_file (file
, name
);
1067 rehash_file (file
, name
);
1068 check_renamed (file
);
1075 if (mtime
== (FILE_TIMESTAMP
) -1)
1076 /* The archive doesn't exist, so it's members don't exist either. */
1077 return (FILE_TIMESTAMP
) -1;
1079 mtime
= ar_member_date (file
->hname
);
1084 mtime
= name_mtime (file
->name
);
1086 if (mtime
== (FILE_TIMESTAMP
) -1 && search
&& !file
->ignore_vpath
)
1088 /* If name_mtime failed, search VPATH. */
1089 char *name
= file
->name
;
1090 if (vpath_search (&name
, &mtime
)
1091 /* Last resort, is it a library (-lxxx)? */
1092 || (name
[0] == '-' && name
[1] == 'l'
1093 && library_search (&name
, &mtime
)))
1096 /* vpath_search and library_search store zero in MTIME
1097 if they didn't need to do a stat call for their work. */
1098 file
->last_mtime
= mtime
;
1100 /* If we found it in VPATH, see if it's in GPATH too; if so,
1101 change the name right now; if not, defer until after the
1102 dependencies are updated. */
1103 if (gpath_search (name
, strlen(name
) - strlen(file
->name
) - 1))
1105 rename_file (file
, name
);
1106 check_renamed (file
);
1107 return file_mtime (file
);
1110 rehash_file (file
, name
);
1111 check_renamed (file
);
1112 mtime
= name_mtime (name
);
1118 /* Files can have bogus timestamps that nothing newly made will be
1119 "newer" than. Updating their dependents could just result in loops.
1120 So notify the user of the anomaly with a warning.
1122 We only need to do this once, for now. */
1124 static FILE_TIMESTAMP now
= 0;
1125 if (!clock_skew_detected
1126 && mtime
!= (FILE_TIMESTAMP
)-1 && mtime
> now
1129 /* This file's time appears to be in the future.
1130 Update our concept of the present, and compare again. */
1132 now
= file_timestamp_now ();
1136 * FAT filesystems round time to nearest even second(!). Just
1137 * allow for any file (NTFS or FAT) to perhaps suffer from this
1140 if (mtime
> now
&& (((mtime
% 2) == 0) && ((mtime
-1) > now
)))
1143 /* Scrupulous testing indicates that some Windows
1144 filesystems can set file times up to 3 sec into the future! */
1145 if (mtime
> now
+ 3)
1151 char mtimebuf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
1152 char nowbuf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
1154 file_timestamp_sprintf (mtimebuf
, mtime
);
1155 file_timestamp_sprintf (nowbuf
, now
);
1156 error (NILF
, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1157 file
->name
, mtimebuf
, nowbuf
);
1158 clock_skew_detected
= 1;
1163 /* Store the mtime into all the entries for this file. */
1164 if (file
->double_colon
)
1165 file
= file
->double_colon
;
1169 /* If this file is not implicit but it is intermediate then it was
1170 made so by the .INTERMEDIATE target. If this file has never
1171 been built by us but was found now, it existed before make
1172 started. So, turn off the intermediate bit so make doesn't
1173 delete it, since it didn't create it. */
1174 if (mtime
!= (FILE_TIMESTAMP
)-1 && file
->command_state
== cs_not_started
1175 && !file
->tried_implicit
&& file
->intermediate
)
1176 file
->intermediate
= 0;
1178 file
->last_mtime
= mtime
;
1187 /* Return the mtime of the file or archive-member reference NAME. */
1189 static FILE_TIMESTAMP
1191 register char *name
;
1195 if (stat (name
, &st
) < 0)
1196 return (FILE_TIMESTAMP
) -1;
1198 return FILE_TIMESTAMP_STAT_MODTIME (st
);
1202 /* Search for a library file specified as -lLIBNAME, searching for a
1203 suitable library file in the system library directories and the VPATH
1207 library_search (lib
, mtime_ptr
)
1209 FILE_TIMESTAMP
*mtime_ptr
;
1211 static char *dirs
[] =
1217 #if defined(WINDOWS32) && !defined(LIBDIR)
1219 * This is completely up to the user at product install time. Just define
1224 LIBDIR
, /* Defined by configuration. */
1228 static char *libpatterns
= NULL
;
1230 char *libname
= &(*lib
)[2]; /* Name without the `-l'. */
1231 FILE_TIMESTAMP mtime
;
1233 /* Loop variables for the libpatterns value. */
1239 /* If we don't have libpatterns, get it. */
1242 int save
= warn_undefined_variables_flag
;
1243 warn_undefined_variables_flag
= 0;
1245 libpatterns
= xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1247 warn_undefined_variables_flag
= save
;
1250 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1252 while ((p
= find_next_token (&p2
, &len
)) != 0)
1254 static char *buf
= NULL
;
1255 static int buflen
= 0;
1256 static int libdir_maxlen
= -1;
1257 char *libbuf
= variable_expand ("");
1259 /* Expand the pattern using LIBNAME as a replacement. */
1265 p3
= find_percent (p
);
1268 /* Give a warning if there is no pattern, then remove the
1269 pattern so it's ignored next time. */
1270 error (NILF
, _(".LIBPATTERNS element `%s' is not a pattern"), p
);
1271 for (; len
; --len
, ++p
)
1276 p4
= variable_buffer_output (libbuf
, p
, p3
-p
);
1277 p4
= variable_buffer_output (p4
, libname
, strlen (libname
));
1278 p4
= variable_buffer_output (p4
, p3
+1, len
- (p3
-p
));
1282 /* Look first for `libNAME.a' in the current directory. */
1283 mtime
= name_mtime (libbuf
);
1284 if (mtime
!= (FILE_TIMESTAMP
) -1)
1286 *lib
= xstrdup (libbuf
);
1292 /* Now try VPATH search on that. */
1295 if (vpath_search (&file
, mtime_ptr
))
1301 /* Now try the standard set of directories. */
1305 for (dp
= dirs
; *dp
!= 0; ++dp
)
1307 int l
= strlen (*dp
);
1308 if (l
> libdir_maxlen
)
1311 buflen
= strlen (libbuf
);
1312 buf
= xmalloc(libdir_maxlen
+ buflen
+ 2);
1314 else if (buflen
< strlen (libbuf
))
1316 buflen
= strlen (libbuf
);
1317 buf
= xrealloc (buf
, libdir_maxlen
+ buflen
+ 2);
1320 for (dp
= dirs
; *dp
!= 0; ++dp
)
1322 sprintf (buf
, "%s/%s", *dp
, libbuf
);
1323 mtime
= name_mtime (buf
);
1324 if (mtime
!= (FILE_TIMESTAMP
) -1)
1326 *lib
= xstrdup (buf
);