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.
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. */
82 update_goal_chain (goals
, makefiles
)
83 register struct dep
*goals
;
86 int t
= touch_flag
, q
= question_flag
, n
= just_print_flag
;
87 unsigned int j
= job_slots
;
90 #define MTIME(file) (makefiles ? file_mtime_no_search (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. */
104 for (g
= goals
; g
!= 0; g
= g
->next
)
108 /* All files start with the considered bit 0, so the global value is 1. */
111 /* Update all the goals until they are all finished. */
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);
129 /* Iterate over all double-colon entries for this file. */
131 int stop
= 0, any_not_updated
= 0;
133 for (file
= g
->file
->double_colon
? g
->file
->double_colon
: g
->file
;
137 unsigned int ocommands_started
;
139 check_renamed (file
);
142 if (file
->cmd_target
)
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
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. */
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
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. */
198 || (!just_print_flag
&& !question_flag
))
200 if (makefiles
&& file
->dontcare
)
201 /* This is a default makefile; stop remaking. */
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
;
215 /* Reset FILE since it is null at the end of the loop. */
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. */
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.")),
235 /* This goal is finished. Remove it from the chain. */
239 lastgoal
->next
= g
->next
;
241 /* Free the storage. */
244 g
= lastgoal
== 0 ? goals
: lastgoal
->next
;
256 /* If we reached the end of the dependency graph toggle the considered
257 flag for the next pass. */
259 considered
= !considered
;
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
281 If there are multiple double-colon entries for FILE,
282 each is considered in turn. */
285 update_file (file
, 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
);
313 if (status
!= 0 && !keep_going_flag
)
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. */
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
)
332 f
->considered
= considered
;
334 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
335 status
|= update_file (d
->file
, depth
+ 1);
341 /* Consider a single `struct file' and update it as appropriate. */
344 update_file_1 (file
, depth
)
348 register FILE_TIMESTAMP this_mtime
;
349 int noexist
, must_make
, deps_changed
;
351 register struct dep
*d
, *lastd
;
354 DBF (DB_VERBOSE
, _("Considering target file `%s'.\n"));
358 if (file
->update_status
> 0)
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"));
369 switch (file
->command_state
)
372 case cs_deps_running
:
375 DBF (DB_VERBOSE
, _("Still updating file `%s'.\n"));
378 DBF (DB_VERBOSE
, _("Finished updating file `%s'.\n"));
379 return file
->update_status
;
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
;
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
);
406 error (NILF
, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
408 this_mtime
+= FILE_TIMESTAMPS_PER_S
- 1 - ns
;
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"));
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. */
438 FILE_TIMESTAMP mtime
;
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
454 file
->deps
= d
->next
;
456 lastd
->next
= d
->next
;
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
;
475 running
|= (f
->command_state
== cs_running
476 || f
->command_state
== cs_deps_running
);
482 if (dep_status
!= 0 && !keep_going_flag
)
486 d
->changed
= file_mtime (d
->file
) != mtime
;
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
;
512 running
|= (f
->command_state
== cs_running
513 || f
->command_state
== cs_deps_running
);
519 if (dep_status
!= 0 && !keep_going_flag
)
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"));
534 set_command_state (file
, cs_deps_running
);
536 DBF (DB_VERBOSE
, _("The prerequisites of `%s' are being made.\n"));
540 /* If any dependency failed, give up now. */
544 file
->update_status
= dep_status
;
545 notice_finished_file (file
);
549 DBF (DB_VERBOSE
, _("Giving up on target file `%s'.\n"));
551 if (depth
== 0 && keep_going_flag
552 && !just_print_flag
&& !question_flag
)
554 _("Target `%s' not remade because of errors."), file
->name
);
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 $?. */
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
)
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. */
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
))
605 if (ISDB (DB_VERBOSE
))
606 fmt
= _("Prerequisite `%s' is order-only for target `%s'.\n");
608 else if (d_mtime
== NONEXISTENT_MTIME
)
611 fmt
= _("Prerequisite `%s' of target `%s' does not exist.\n");
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");
623 print_spaces (depth
);
624 printf (fmt
, dep_name (d
), file
->name
);
630 /* Here depth returns to the value it had when we were called. */
633 if (file
->double_colon
&& file
->deps
== 0)
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
)
644 _("No commands for `%s' and no prerequisites actually changed.\n"));
646 else if (!must_make
&& file
->cmds
!= 0 && always_make_flag
)
649 DBF (DB_VERBOSE
, _("Making `%s' due to always-make flag.\n"));
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
);
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. */
672 file
->name
= file
->hname
;
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
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. */
692 if (file
->command_state
!= cs_finished
)
694 DBF (DB_VERBOSE
, _("Commands of `%s' are being run.\n"));
698 switch (file
->update_status
)
701 DBF (DB_BASIC
, _("Failed to remake target file `%s'.\n"));
704 DBF (DB_BASIC
, _("Successfully remade target file `%s'.\n"));
707 DBF (DB_BASIC
, _("Target file `%s' needs remade under -q.\n"));
710 assert (file
->update_status
>= 0 && file
->update_status
<= 2);
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
722 On return, FILE->update_status will no longer be -1 if it was. */
725 notice_finished_file (file
)
726 register struct file
*file
;
729 int ran
= file
->command_state
== cs_running
;
732 file
->command_state
= cs_finished
;
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. */
749 for (i
= 0; i
< file
->cmds
->ncommand_lines
; ++i
)
750 if (!(file
->cmds
->lines_flags
[i
] & COMMANDS_RECURSE
))
751 goto have_nonrecursing
;
757 file
->update_status
= 0;
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. */
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. */
776 if (file
->mtime_before_update
== UNKNOWN_MTIME
)
777 file
->mtime_before_update
= file
->last_mtime
;
779 if ((ran
&& !file
->phony
) || touched
)
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
))
795 /* If there were no commands at all, it's always new. */
797 else if (file
->is_target
&& file
->cmds
== 0)
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. */
840 check_dep (file
, depth
, this_mtime
, must_make_ptr
)
843 FILE_TIMESTAMP this_mtime
;
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
)
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"));
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
)
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. */
904 if (is_updating (d
->file
))
906 error (NILF
, _("Circular %s <- %s dependency dropped."),
907 file
->name
, d
->file
->name
);
910 file
->deps
= d
->next
;
916 lastd
->next
= d
->next
;
923 d
->file
->parent
= file
;
924 maybe_make
= *must_make_ptr
;
925 dep_status
|= check_dep (d
->file
, depth
, this_mtime
,
927 if (! d
->ignore_mtime
)
928 *must_make_ptr
= maybe_make
;
929 check_renamed (d
->file
);
930 if (dep_status
!= 0 && !keep_going_flag
)
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
);
946 finish_updating (file
);
950 /* Touch FILE. Return zero if successful, one if not. */
952 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
956 register struct file
*file
;
959 message (0, "touch %s", file
->name
);
962 if (ar_name (file
->name
))
963 return ar_touch (file
->name
);
967 int fd
= open (file
->name
, O_RDWR
| O_CREAT
, 0666);
970 TOUCH_ERROR ("touch: open: ");
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)
990 fd
= open (file
->name
, O_RDWR
| O_TRUNC
, 0666);
992 TOUCH_ERROR ("touch: open: ");
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. */
1009 if (file
->cmds
== 0)
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;
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
, ".");
1039 error (NILF
, msg_parent
, "*** ",
1040 file
->name
, file
->parent
->name
, ".");
1042 file
->update_status
= 2;
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
);
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
1073 f_mtime (file
, search
)
1074 register struct file
*file
;
1077 FILE_TIMESTAMP mtime
;
1079 /* File's mtime is not known; must get it from the system. */
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;
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
);
1099 arfile
= enter_file (arname
);
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. */
1110 unsigned int arlen
, memlen
;
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
);
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
);
1136 rehash_file (file
, name
);
1137 check_renamed (file
);
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
1153 : file_timestamp_cons (file
->hname
, member_date
, 0));
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
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
;
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
)
1222 FILE_TIMESTAMP now
= file_timestamp_now (&resolution
);
1223 adjusted_now
= now
+ (resolution
- 1);
1224 if (adjusted_now
< adjusted_mtime
)
1227 error (NILF
, _("Warning: File `%s' has modification time in the future"),
1231 (FILE_TIMESTAMP_S (mtime
) - FILE_TIMESTAMP_S (now
)
1232 + ((FILE_TIMESTAMP_NS (mtime
) - FILE_TIMESTAMP_NS (now
))
1234 error (NILF
, _("Warning: File `%s' has modification time %.2g s in the future"),
1235 file
->name
, from_now
);
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
;
1268 /* Return the mtime of the file or archive-member reference NAME. */
1270 static FILE_TIMESTAMP
1272 register char *name
;
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
1292 library_search (lib
, mtime_ptr
)
1294 FILE_TIMESTAMP
*mtime_ptr
;
1296 static char *dirs
[] =
1302 #if defined(WINDOWS32) && !defined(LIBDIR)
1304 * This is completely up to the user at product install time. Just define
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. */
1324 /* If we don't have libpatterns, get it. */
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. */
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. */
1350 p3
= find_percent (p
);
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
)
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
));
1367 /* Look first for `libNAME.a' in the current directory. */
1368 mtime
= name_mtime (libbuf
);
1369 if (mtime
!= NONEXISTENT_MTIME
)
1371 *lib
= xstrdup (libbuf
);
1377 /* Now try VPATH search on that. */
1380 if (vpath_search (&file
, mtime_ptr
))
1386 /* Now try the standard set of directories. */
1390 for (dp
= dirs
; *dp
!= 0; ++dp
)
1392 int l
= strlen (*dp
);
1393 if (l
> libdir_maxlen
)
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
);