1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
2 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 extern int try_implicit_rule ();
34 /* Incremented when a command is started (under -n, when one would be). */
35 unsigned int commands_started
= 0;
37 static int update_file (), update_file_1 (), check_dep (), touch_file ();
38 static void remake_file ();
39 static time_t name_mtime ();
40 static int library_search ();
41 extern time_t f_mtime ();
43 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
44 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
45 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
46 be disabled for them unless they were also command-line targets, and we
47 should only make one goal at a time and return as soon as one goal whose
48 `changed' member is nonzero is successfully made. */
51 update_goal_chain (goals
, makefiles
)
52 register struct dep
*goals
;
55 int t
= touch_flag
, q
= question_flag
, n
= just_print_flag
;
56 unsigned int j
= job_slots
;
59 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
62 /* Duplicate the chain so we can remove things from it. */
64 goals
= copy_dep_chain (goals
);
67 /* Clear the `changed' flag of each goal in the chain.
68 We will use the flag below to notice when any commands
69 have actually been run for a target. When no commands
70 have been run, we give an "up to date" diagnostic. */
73 for (g
= goals
; g
!= 0; g
= g
->next
)
78 /* Only run one job at a time. */
81 /* Update all the goals until they are all finished. */
85 register struct dep
*g
, *lastgoal
;
87 /* Start jobs that are waiting for the load to go down. */
89 start_waiting_jobs ();
91 /* Wait for a child to die. */
99 unsigned int ocommands_started
;
101 time_t mtime
= MTIME (g
->file
);
102 check_renamed (g
->file
);
106 if (g
->file
->cmd_target
)
113 touch_flag
= question_flag
= just_print_flag
= 0;
116 /* Save the old value of `commands_started' so we can compare later.
117 It will be incremented when any commands are actually run. */
118 ocommands_started
= commands_started
;
120 x
= update_file (g
->file
, makefiles
? 1 : 0);
121 check_renamed (g
->file
);
123 /* Set the goal's `changed' flag if any commands were started
124 by calling update_file above. We check this flag below to
125 decide when to give an "up to date" diagnostic. */
126 g
->changed
+= commands_started
- ocommands_started
;
128 if (x
!= 0 || g
->file
->updated
)
132 /* If STATUS was not already 1, set it to 1 if
133 updating failed, or to 0 if updating succeeded.
134 Leave STATUS as it is if no updating was done. */
138 if (g
->file
->update_status
!= 0)
140 /* Updating failed. */
142 stop
= !keep_going_flag
&& !makefiles
;
144 else if (MTIME (g
->file
) != mtime
)
146 /* Updating was done.
147 If this is a makefile and just_print_flag or
148 question_flag is set (meaning -n or -q was given
149 and this file was specified as a command-line target),
150 don't change STATUS. If STATUS is changed, we will
151 get re-exec'd, and fall into an infinite loop. */
152 if (!makefiles
|| (!just_print_flag
&& !question_flag
))
154 if (makefiles
&& g
->file
->dontcare
)
155 /* This is a default makefile. Stop remaking. */
160 if (stop
|| g
->file
->prev
== 0)
162 /* If we have found nothing whatever to do for the goal,
163 print a message saying nothing needs doing. */
166 /* If the update_status is zero, we updated successfully
167 or not at all. G->changed will have been set above if
168 any commands were actually started for this goal. */
169 && g
->file
->update_status
== 0 && !g
->changed
170 /* Never give a message under -s or -q. */
171 && !silent_flag
&& !question_flag
)
173 if (g
->file
->phony
|| g
->file
->cmds
== 0)
174 message ("Nothing to be done for `%s'.",
177 message ("`%s' is up to date.", g
->file
->name
);
181 /* This goal is finished. Remove it from the chain. */
185 lastgoal
->next
= g
->next
;
187 /* Free the storage. */
190 g
= lastgoal
== 0 ? goals
: lastgoal
->next
;
192 else if (g
->file
->updated
)
193 /* This instance of the target is done being updated.
194 Go to the next instance (:: rule).
195 update_file cycles through all instances, but under -j,
196 update_file can return while the file is running,
197 then reap_children can change its command state and
198 updated flag, leaving G->file done, but some of its
199 other instances needing work. */
200 g
->file
= g
->file
->prev
;
224 /* If FILE is not up to date, execute the commands for it.
225 Return 0 if successful, 1 if unsuccessful;
226 but with some flag settings, just call `exit' if unsuccessful.
228 DEPTH is the depth in recursions of this function.
229 We increment it during the consideration of our dependencies,
230 then decrement it again after finding out whether this file
233 If there are multiple double-colon entries for FILE,
234 each is considered in turn. */
237 update_file (file
, depth
)
241 register int status
= 0;
242 register struct file
*f
;
244 for (f
= file
; f
!= 0; f
= f
->prev
)
246 status
|= update_file_1 (f
, depth
);
249 if (status
!= 0 && !keep_going_flag
)
252 switch (f
->command_state
)
255 /* The file is done being remade. */
259 case cs_deps_running
:
260 /* Don't run the other :: rules for this
261 file until this rule is finished. */
265 error ("internal error: `%s' command_state == %d in update_file",
266 f
->name
, (int) f
->command_state
);
275 /* Consider a single `struct file' and update it as appropriate. */
278 update_file_1 (file
, depth
)
282 register time_t this_mtime
;
283 int noexist
, must_make
, deps_changed
;
285 register struct dep
*d
, *lastd
;
288 DEBUGPR ("Considering target file `%s'.\n");
292 if (file
->update_status
> 0)
294 DEBUGPR ("Recently tried and failed to update file `%s'.\n");
295 return file
->update_status
;
298 DEBUGPR ("File `%s' was considered already.\n");
302 switch (file
->command_state
)
305 case cs_deps_running
:
308 DEBUGPR ("Still updating file `%s'.\n");
311 DEBUGPR ("Finished updating file `%s'.\n");
312 return file
->update_status
;
319 /* Notice recursive update of the same file. */
322 /* Looking at the file's modtime beforehand allows the possibility
323 that its name may be changed by a VPATH search, and thus it may
324 not need an implicit rule. If this were not done, the file
325 might get implicit commands that apply to its initial name, only
326 to have that name replaced with another found by VPATH search. */
328 this_mtime
= file_mtime (file
);
329 check_renamed (file
);
330 noexist
= this_mtime
== (time_t) -1;
332 DEBUGPR ("File `%s' does not exist.\n");
336 /* If file was specified as a target with no commands,
337 come up with some default commands. */
339 if (!file
->phony
&& file
->cmds
== 0 && !file
->tried_implicit
)
341 if (try_implicit_rule (file
, depth
))
342 DEBUGPR ("Found an implicit rule for `%s'.\n");
345 DEBUGPR ("No implicit rule found for `%s'.\n");
347 && default_file
!= 0 && default_file
->cmds
!= 0)
349 DEBUGPR ("Using default commands for `%s'.\n");
350 file
->cmds
= default_file
->cmds
;
353 file
->tried_implicit
= 1;
356 /* Update all non-intermediate files we depend on, if necessary,
357 and see whether any of them is more recent than this file. */
365 check_renamed (d
->file
);
367 mtime
= file_mtime (d
->file
);
368 check_renamed (d
->file
);
370 if (d
->file
->updating
)
372 error ("Circular %s <- %s dependency dropped.",
373 file
->name
, d
->file
->name
);
376 file
->deps
= d
->next
;
382 lastd
->next
= d
->next
;
389 d
->file
->parent
= file
;
390 dep_status
|= check_dep (d
->file
, depth
, this_mtime
, &must_make
);
391 check_renamed (d
->file
);
394 register struct file
*f
= d
->file
;
397 running
|= (f
->command_state
== cs_running
398 || f
->command_state
== cs_deps_running
);
404 if (dep_status
!= 0 && !keep_going_flag
)
408 d
->changed
= file_mtime (d
->file
) != mtime
;
414 /* Now we know whether this target needs updating.
415 If it does, update all the intermediate files we depend on. */
419 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
420 if (d
->file
->intermediate
)
422 time_t mtime
= file_mtime (d
->file
);
423 check_renamed (d
->file
);
424 d
->file
->parent
= file
;
425 dep_status
|= update_file (d
->file
, depth
);
426 check_renamed (d
->file
);
429 register struct file
*f
= d
->file
;
432 running
|= (f
->command_state
== cs_running
433 || f
->command_state
== cs_deps_running
);
439 if (dep_status
!= 0 && !keep_going_flag
)
443 d
->changed
= ((file
->phony
&& file
->cmds
!= 0)
444 || file_mtime (d
->file
) != mtime
);
450 DEBUGPR ("Finished dependencies of target file `%s'.\n");
454 file
->command_state
= cs_deps_running
;
456 DEBUGPR ("The dependencies of `%s' are being made.\n");
460 /* If any dependency failed, give up now. */
464 file
->command_state
= cs_finished
;
465 file
->update_status
= dep_status
;
470 DEBUGPR ("Giving up on target file `%s'.\n");
472 if (depth
== 0 && keep_going_flag
473 && !just_print_flag
&& !question_flag
)
474 error ("Target `%s' not remade because of errors.", file
->name
);
479 file
->command_state
= cs_not_started
;
481 /* Now record which dependencies are more
482 recent than this file, so we can define $?. */
485 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
487 time_t d_mtime
= file_mtime (d
->file
);
488 check_renamed (d
->file
);
490 #if 1 /* %%% In version 4, remove this code completely to
491 implement not remaking deps if their deps are newer
492 than their parents. */
493 if (d_mtime
== (time_t) -1 && !d
->file
->intermediate
)
494 /* We must remake if this dep does not
495 exist and is not intermediate. */
499 /* Set DEPS_CHANGED if this dep actually changed. */
500 deps_changed
|= d
->changed
;
502 /* Set D->changed if either this dep actually changed,
503 or its dependent, FILE, is older or does not exist. */
504 d
->changed
|= noexist
|| d_mtime
> this_mtime
;
506 if (debug_flag
&& !noexist
)
508 print_spaces (depth
);
509 if (d_mtime
== (time_t) -1)
510 printf ("Dependency `%s' does not exist.\n", dep_name (d
));
512 printf ("Dependency `%s' is %s than dependent `%s'.\n",
513 dep_name (d
), d
->changed
? "newer" : "older", file
->name
);
518 /* Here depth returns to the value it had when we were called. */
521 if (file
->double_colon
&& file
->deps
== 0)
524 DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
526 else if (file
->is_target
&& !deps_changed
&& file
->cmds
== 0)
529 DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
534 DEBUGPR ("No need to remake target `%s'.\n");
535 file
->command_state
= cs_finished
;
536 file
->update_status
= 0;
541 DEBUGPR ("Must remake target `%s'.\n");
543 /* Now, take appropriate actions to remake the file. */
546 if (file
->command_state
!= cs_finished
)
548 DEBUGPR ("Commands of `%s' are being run.\n");
552 switch (file
->update_status
)
555 DEBUGPR ("Failed to remake target file `%s'.\n");
558 DEBUGPR ("Successfully remade target file `%s'.\n");
561 error ("internal error: `%s' update_status is -1 at cs_finished!",
565 error ("internal error: `%s' update_status invalid!", file
->name
);
570 return file
->update_status
;
573 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
574 files listed in its `also_make' member. Under -t, this function also
578 notice_finished_file (file
)
579 register struct file
*file
;
583 file
->command_state
= cs_finished
;
587 /* The update status will be:
588 -1 if no commands were run;
589 0 if some commands (+ or ${MAKE}) were run and won;
590 1 if some commands were run and lost.
591 The only time we don't want to touch the target is if
592 it had some recursive commands, and they lost. */
593 && file
->update_status
!= 1)
595 if (file
->cmds
!= 0 && file
->cmds
->any_recurse
)
597 /* If all the command lines were recursive,
598 we don't want to do the touching. */
600 for (i
= 0; i
< file
->cmds
->ncommand_lines
; ++i
)
601 if (!(file
->cmds
->lines_flags
[i
] & COMMANDS_RECURSE
))
602 goto have_nonrecursing
;
608 file
->update_status
= 0;
610 /* Should set file's modification date and do nothing else. */
611 file
->update_status
= touch_file (file
);
617 if (just_print_flag
|| question_flag
618 || (file
->is_target
&& file
->cmds
== 0))
619 file
->last_mtime
= time ((time_t *) 0);
621 file
->last_mtime
= 0;
624 if (file
->update_status
!= -1)
625 /* We actually tried to update FILE, which has
626 updated its also_make's as well (if it worked).
627 If it didn't work, it wouldn't work again for them.
628 So mark them as updated with the same status. */
629 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
631 d
->file
->command_state
= cs_finished
;
632 d
->file
->updated
= 1;
633 d
->file
->update_status
= file
->update_status
;
636 /* Fetch the new modification time.
637 We do this instead of just invalidating the cached time
638 so that a vpath_search can happen. Otherwise, it would
639 never be done because the target is already updated. */
640 (void) f_mtime (d
->file
, 0);
644 /* Check whether another file (whose mtime is THIS_MTIME)
645 needs updating on account of a dependency which is file FILE.
646 If it does, store 1 in *MUST_MAKE_PTR.
647 In the process, update any non-intermediate files
648 that FILE depends on (including FILE itself).
649 Return nonzero if any updating failed. */
652 check_dep (file
, depth
, this_mtime
, must_make_ptr
)
658 register struct dep
*d
;
664 if (!file
->intermediate
)
665 /* If this is a non-intermediate file, update it and record
666 whether it is newer than THIS_MTIME. */
669 dep_status
= update_file (file
, depth
);
670 check_renamed (file
);
671 mtime
= file_mtime (file
);
672 check_renamed (file
);
673 if (mtime
== (time_t) -1 || mtime
> this_mtime
)
678 /* FILE is an intermediate file.
679 Update all non-intermediate files we depend on, if necessary,
680 and see whether any of them is more recent than the file
681 on whose behalf we are checking. */
682 register struct dep
*lastd
;
687 if (d
->file
->updating
)
689 error ("Circular %s <- %s dependency dropped.",
690 file
->name
, d
->file
->name
);
693 file
->deps
= d
->next
;
699 lastd
->next
= d
->next
;
706 d
->file
->parent
= file
;
707 dep_status
|= check_dep (d
->file
, depth
, this_mtime
, must_make_ptr
);
708 check_renamed (d
->file
);
709 if (dep_status
!= 0 && !keep_going_flag
)
712 if (d
->file
->command_state
== cs_running
713 || d
->file
->command_state
== cs_deps_running
)
714 /* Record that some of FILE's dependencies are still being made.
715 This tells the upper levels to wait on processing it until
716 the commands are finished. */
717 file
->command_state
= cs_deps_running
;
728 /* Touch FILE. Return zero if successful, one if not. */
730 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
734 register struct file
*file
;
738 printf ("touch %s\n", file
->name
);
743 if (ar_name (file
->name
))
744 return ar_touch (file
->name
);
748 int fd
= open (file
->name
, O_RDWR
| O_CREAT
, 0666);
751 TOUCH_ERROR ("touch: open: ");
757 if (fstat (fd
, &statbuf
) < 0)
758 TOUCH_ERROR ("touch: fstat: ");
759 /* Rewrite character 0 same as it already is. */
760 if (read (fd
, &buf
, 1) < 0)
761 TOUCH_ERROR ("touch: read: ");
762 if (lseek (fd
, 0L, 0) < 0L)
763 TOUCH_ERROR ("touch: lseek: ");
764 if (write (fd
, &buf
, 1) < 0)
765 TOUCH_ERROR ("touch: write: ");
766 /* If file length was 0, we just
767 changed it, so change it back. */
768 if (statbuf
.st_size
== 0)
771 fd
= open (file
->name
, O_RDWR
| O_TRUNC
, 0666);
773 TOUCH_ERROR ("touch: open: ");
782 /* Having checked and updated the dependencies of FILE,
783 do whatever is appropriate to remake FILE itself.
784 Return the status from executing FILE's commands. */
793 /* Phony target. Pretend it succeeded. */
794 file
->update_status
= 0;
795 else if (file
->is_target
)
796 /* This is a nonexistent target file we cannot make.
797 Pretend it was successfully remade. */
798 file
->update_status
= 0;
801 /* This is a dependency file we cannot remake. Fail. */
802 static char noway
[] = "No rule to make target";
803 if (keep_going_flag
|| file
->dontcare
)
806 error ("*** %s `%s'.", noway
, file
->name
);
807 file
->update_status
= 1;
810 fatal ("%s `%s'", noway
, file
->name
);
815 chop_commands (file
->cmds
);
817 if (!touch_flag
|| file
->cmds
->any_recurse
)
819 execute_file_commands (file
);
824 /* This does the touching under -t. */
825 notice_finished_file (file
);
828 /* Return the mtime of a file, given a `struct file'.
829 Caches the time in the struct file to avoid excess stat calls.
831 If the file is not found, and SEARCH is nonzero, VPATH searching and
832 replacement is done. If that fails, a library (-lLIBNAME) is tried and
833 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
837 f_mtime (file
, search
)
838 register struct file
*file
;
843 /* File's mtime is not known; must get it from the system. */
846 if (ar_name (file
->name
))
848 /* This file is an archive-member reference. */
850 char *arname
, *memname
;
854 /* Find the archive's name. */
855 ar_parse_name (file
->name
, &arname
, &memname
);
857 /* Find the modification time of the archive itself.
858 Also allow for its name to be changed via VPATH search. */
859 arfile
= lookup_file (arname
);
862 arfile
= enter_file (arname
);
865 mtime
= f_mtime (arfile
, search
);
866 check_renamed (arfile
);
867 if (search
&& strcmp (arfile
->name
, arname
))
869 /* The archive's name has changed.
870 Change the archive-member reference accordingly. */
872 unsigned int arlen
, memlen
;
880 arname
= arfile
->name
;
881 arlen
= strlen (arname
);
882 memlen
= strlen (memname
);
886 file
->name
= (char *) xmalloc (arlen
+ 1 + memlen
+ 2);
887 bcopy (arname
, file
->name
, arlen
);
888 file
->name
[arlen
] = '(';
889 bcopy (memname
, file
->name
+ arlen
+ 1, memlen
);
890 file
->name
[arlen
+ 1 + memlen
] = ')';
891 file
->name
[arlen
+ 1 + memlen
+ 1] = '\0';
898 if (mtime
== (time_t) -1)
899 /* The archive doesn't exist, so it's members don't exist either. */
902 mtime
= ar_member_date (file
->name
);
907 mtime
= name_mtime (file
->name
);
909 if (mtime
== (time_t) -1 && search
)
911 /* If name_mtime failed, search VPATH. */
912 char *name
= file
->name
;
913 if (vpath_search (&name
, &mtime
)
914 /* Last resort, is it a library (-lxxx)? */
915 || (name
[0] == '-' && name
[1] == 'l'
916 && library_search (&name
, &mtime
)))
919 /* vpath_search and library_search store zero in MTIME
920 if they didn't need to do a stat call for their work. */
921 file
->last_mtime
= mtime
;
922 rename_file (file
, name
);
923 check_renamed (file
);
924 return file_mtime (file
);
929 /* Store the mtime into all the entries for this file. */
932 file
->last_mtime
= mtime
;
940 /* Return the mtime of the file or archive-member reference NAME. */
948 if (stat (name
, &st
) < 0)
951 return (time_t) st
.st_mtime
;
955 /* Search for a library file specified as -lLIBNAME, searching for a
956 suitable library file in the system library directories and the VPATH
960 library_search (lib
, mtime_ptr
)
964 static char *dirs
[] =
968 LIBDIR
, /* Defined by configuration. */
972 char *libname
= &(*lib
)[2]; /* Name without the `-l'. */
975 /* Buffer to construct possible names in. */
976 char *buf
= xmalloc (sizeof (LIBDIR
) + 8 + strlen (libname
) + 4 + 2 + 1);
979 /* Look first for `libNAME.a' in the current directory. */
981 sprintf (buf
, "lib%s.a", libname
);
982 mtime
= name_mtime (buf
);
983 if (mtime
!= (time_t) -1)
991 /* Now try VPATH search on that. */
994 if (vpath_search (&file
, mtime_ptr
))
1001 /* Now try the standard set of directories. */
1003 for (dp
= dirs
; *dp
!= 0; ++dp
)
1005 sprintf (buf
, "%s/lib%s.a", *dp
, libname
);
1006 mtime
= name_mtime (buf
);
1007 if (mtime
!= (time_t) -1)