* Various changes and fixes. See ChangeLog.
[make.git] / remake.c
blobaf559ac542fbb5c708b223722df7f71203ae9d26
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)
8 any later version.
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. */
20 #include "make.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "dep.h"
25 #include "variable.h"
26 #include "debug.h"
28 #include <assert.h>
30 #ifdef HAVE_FCNTL_H
31 #include <fcntl.h>
32 #else
33 #include <sys/file.h>
34 #endif
36 #ifdef VMS
37 #include <starlet.h>
38 #endif
39 #ifdef WINDOWS32
40 #include <io.h>
41 #endif
43 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
46 /* Incremented when a command is started (under -n, when one would be). */
47 unsigned int commands_started = 0;
49 static int update_file PARAMS ((struct file *file, unsigned int depth));
50 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
51 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
52 static int touch_file PARAMS ((struct file *file));
53 static void remake_file PARAMS ((struct file *file));
54 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
55 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
58 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
59 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
60 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
61 be disabled for them unless they were also command-line targets, and we
62 should only make one goal at a time and return as soon as one goal whose
63 `changed' member is nonzero is successfully made. */
65 /* We need to know this "lower down" for correct error handling. */
66 static int updating_makefiles = 0;
68 int
69 update_goal_chain (goals, makefiles)
70 register struct dep *goals;
71 int makefiles;
73 int t = touch_flag, q = question_flag, n = just_print_flag;
74 unsigned int j = job_slots;
75 int status = -1;
77 updating_makefiles = makefiles;
79 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
80 : file_mtime (file))
82 /* Duplicate the chain so we can remove things from it. */
84 goals = copy_dep_chain (goals);
87 /* Clear the `changed' flag of each goal in the chain.
88 We will use the flag below to notice when any commands
89 have actually been run for a target. When no commands
90 have been run, we give an "up to date" diagnostic. */
92 struct dep *g;
93 for (g = goals; g != 0; g = g->next)
94 g->changed = 0;
97 /* All files start with the considered bit 0, so the global value is 1. */
98 considered = 1;
100 /* Update all the goals until they are all finished. */
102 while (goals != 0)
104 register struct dep *g, *lastgoal;
106 /* Start jobs that are waiting for the load to go down. */
108 start_waiting_jobs ();
110 /* Wait for a child to die. */
112 reap_children (1, 0);
114 lastgoal = 0;
115 g = goals;
116 while (g != 0)
118 /* Iterate over all double-colon entries for this file. */
119 struct file *file;
120 int stop = 0, any_not_updated = 0;
122 for (file = g->file->double_colon ? g->file->double_colon : g->file;
123 file != NULL;
124 file = file->prev)
126 unsigned int ocommands_started;
127 int x;
128 check_renamed (file);
129 if (makefiles)
131 if (file->cmd_target)
133 touch_flag = t;
134 question_flag = q;
135 just_print_flag = n;
137 else
138 touch_flag = question_flag = just_print_flag = 0;
141 /* Save the old value of `commands_started' so we can compare
142 later. It will be incremented when any commands are
143 actually run. */
144 ocommands_started = commands_started;
146 x = update_file (file, makefiles ? 1 : 0);
147 check_renamed (file);
149 /* Set the goal's `changed' flag if any commands were started
150 by calling update_file above. We check this flag below to
151 decide when to give an "up to date" diagnostic. */
152 g->changed += commands_started - ocommands_started;
154 /* If we updated a file and STATUS was not already 1, set it to
155 1 if updating failed, or to 0 if updating succeeded. Leave
156 STATUS as it is if no updating was done. */
158 stop = 0;
159 if ((x != 0 || file->updated) && status < 1)
161 if (file->update_status != 0)
163 /* Updating failed, or -q triggered. The STATUS value
164 tells our caller which. */
165 status = file->update_status;
166 /* If -q just triggered, stop immediately. It doesn't
167 matter how much more we run, since we already know
168 the answer to return. */
169 stop = (!keep_going_flag && !question_flag
170 && !makefiles);
172 else
174 FILE_TIMESTAMP mtime = MTIME (file);
175 check_renamed (file);
177 if (file->updated && g->changed &&
178 mtime != file->mtime_before_update)
180 /* Updating was done. If this is a makefile and
181 just_print_flag or question_flag is set (meaning
182 -n or -q was given and this file was specified
183 as a command-line target), don't change STATUS.
184 If STATUS is changed, we will get re-exec'd, and
185 enter an infinite loop. */
186 if (!makefiles
187 || (!just_print_flag && !question_flag))
188 status = 0;
189 if (makefiles && file->dontcare)
190 /* This is a default makefile; stop remaking. */
191 stop = 1;
196 /* Keep track if any double-colon entry is not finished.
197 When they are all finished, the goal is finished. */
198 any_not_updated |= !file->updated;
200 if (stop)
201 break;
204 /* Reset FILE since it is null at the end of the loop. */
205 file = g->file;
207 if (stop || !any_not_updated)
209 /* If we have found nothing whatever to do for the goal,
210 print a message saying nothing needs doing. */
212 if (!makefiles
213 /* If the update_status is zero, we updated successfully
214 or not at all. G->changed will have been set above if
215 any commands were actually started for this goal. */
216 && file->update_status == 0 && !g->changed
217 /* Never give a message under -s or -q. */
218 && !silent_flag && !question_flag)
219 message (1, ((file->phony || file->cmds == 0)
220 ? _("Nothing to be done for `%s'.")
221 : _("`%s' is up to date.")),
222 file->name);
224 /* This goal is finished. Remove it from the chain. */
225 if (lastgoal == 0)
226 goals = g->next;
227 else
228 lastgoal->next = g->next;
230 /* Free the storage. */
231 free ((char *) g);
233 g = lastgoal == 0 ? goals : lastgoal->next;
235 if (stop)
236 break;
238 else
240 lastgoal = g;
241 g = g->next;
245 /* If we reached the end of the dependency graph toggle the considered
246 flag for the next pass. */
247 if (g == 0)
248 considered = !considered;
251 if (makefiles)
253 touch_flag = t;
254 question_flag = q;
255 just_print_flag = n;
256 job_slots = j;
258 return status;
261 /* Generate an error/fatal message if no rules are available for the target.
263 static void
264 no_rule_error(file)
265 struct file *file;
267 static const char msg_noparent[]
268 = _("%sNo rule to make target `%s'%s");
269 static const char msg_parent[]
270 = _("%sNo rule to make target `%s', needed by `%s'%s");
272 if (keep_going_flag || file->dontcare)
274 /* If the previous attempt was made while we were creating
275 makefiles, but we aren't anymore, print an error now. */
276 if (!file->dontcare
277 || (file->mfile_status && !updating_makefiles))
279 if (file->parent == 0)
280 error (NILF, msg_noparent, "*** ", file->name, ".");
281 else
282 error (NILF, msg_parent, "*** ",
283 file->name, file->parent->name, ".");
285 file->update_status = 2;
286 file->mfile_status = updating_makefiles;
288 else if (file->parent == 0)
289 fatal (NILF, msg_noparent, "", file->name, "");
290 else
291 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
294 /* If FILE is not up to date, execute the commands for it.
295 Return 0 if successful, 1 if unsuccessful;
296 but with some flag settings, just call `exit' if unsuccessful.
298 DEPTH is the depth in recursions of this function.
299 We increment it during the consideration of our dependencies,
300 then decrement it again after finding out whether this file
301 is out of date.
303 If there are multiple double-colon entries for FILE,
304 each is considered in turn. */
306 static int
307 update_file (file, depth)
308 struct file *file;
309 unsigned int depth;
311 register int status = 0;
312 register struct file *f;
314 f = file->double_colon ? file->double_colon : file;
316 /* Prune the dependency graph: if we've already been here on _this_
317 pass through the dependency graph, we don't have to go any further.
318 We won't reap_children until we start the next pass, so no state
319 change is possible below here until then. */
320 if (f->considered == considered)
322 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
323 return 0;
326 /* This loop runs until we start a double colon rule, or until the
327 chain is exhausted. */
328 for (; f != 0; f = f->prev)
330 f->considered = considered;
332 status |= update_file_1 (f, depth);
333 check_renamed (f);
335 if (status != 0 && !keep_going_flag)
336 break;
338 if (f->command_state == cs_running
339 || f->command_state == cs_deps_running)
341 /* Don't run the other :: rules for this
342 file until this rule is finished. */
343 status = 0;
344 break;
348 /* Process the remaining rules in the double colon chain so they're marked
349 considered. Start their prerequisites, too. */
350 for (; f != 0 ; f = f->prev)
352 struct dep *d;
354 f->considered = considered;
356 for (d = f->deps; d != 0; d = d->next)
357 status |= update_file (d->file, depth + 1);
360 return status;
363 /* Consider a single `struct file' and update it as appropriate. */
365 static int
366 update_file_1 (file, depth)
367 struct file *file;
368 unsigned int depth;
370 register FILE_TIMESTAMP this_mtime;
371 int noexist, must_make, deps_changed;
372 int dep_status = 0;
373 register struct dep *d, *lastd;
374 int running = 0;
376 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
378 if (file->updated)
380 if (file->update_status > 0)
382 DBF (DB_VERBOSE,
383 _("Recently tried and failed to update file `%s'.\n"));
384 no_rule_error(file);
385 return file->update_status;
388 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
389 return 0;
392 switch (file->command_state)
394 case cs_not_started:
395 case cs_deps_running:
396 break;
397 case cs_running:
398 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
399 return 0;
400 case cs_finished:
401 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
402 return file->update_status;
403 default:
404 abort ();
407 ++depth;
409 /* Notice recursive update of the same file. */
410 file->updating = 1;
412 /* Looking at the file's modtime beforehand allows the possibility
413 that its name may be changed by a VPATH search, and thus it may
414 not need an implicit rule. If this were not done, the file
415 might get implicit commands that apply to its initial name, only
416 to have that name replaced with another found by VPATH search. */
418 this_mtime = file_mtime (file);
419 check_renamed (file);
420 noexist = this_mtime == (FILE_TIMESTAMP) -1;
421 if (noexist)
422 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
424 must_make = noexist;
426 /* If file was specified as a target with no commands,
427 come up with some default commands. */
429 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
431 if (try_implicit_rule (file, depth))
432 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
433 else
434 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
435 file->tried_implicit = 1;
437 if (file->cmds == 0 && !file->is_target
438 && default_file != 0 && default_file->cmds != 0)
440 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
441 file->cmds = default_file->cmds;
444 /* Update all non-intermediate files we depend on, if necessary,
445 and see whether any of them is more recent than this file. */
447 lastd = 0;
448 d = file->deps;
449 while (d != 0)
451 FILE_TIMESTAMP mtime;
453 check_renamed (d->file);
455 mtime = file_mtime (d->file);
456 check_renamed (d->file);
458 if (d->file->updating)
460 error (NILF, _("Circular %s <- %s dependency dropped."),
461 file->name, d->file->name);
462 /* We cannot free D here because our the caller will still have
463 a reference to it when we were called recursively via
464 check_dep below. */
465 if (lastd == 0)
466 file->deps = d->next;
467 else
468 lastd->next = d->next;
469 d = d->next;
470 continue;
473 d->file->parent = file;
474 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
475 check_renamed (d->file);
478 register struct file *f = d->file;
479 if (f->double_colon)
480 f = f->double_colon;
483 running |= (f->command_state == cs_running
484 || f->command_state == cs_deps_running);
485 f = f->prev;
487 while (f != 0);
490 if (dep_status != 0 && !keep_going_flag)
491 break;
493 if (!running)
494 d->changed = file_mtime (d->file) != mtime;
496 lastd = d;
497 d = d->next;
500 /* Now we know whether this target needs updating.
501 If it does, update all the intermediate files we depend on. */
503 if (must_make)
505 for (d = file->deps; d != 0; d = d->next)
506 if (d->file->intermediate)
508 FILE_TIMESTAMP mtime = file_mtime (d->file);
509 check_renamed (d->file);
510 d->file->parent = file;
511 dep_status |= update_file (d->file, depth);
512 check_renamed (d->file);
515 register struct file *f = d->file;
516 if (f->double_colon)
517 f = f->double_colon;
520 running |= (f->command_state == cs_running
521 || f->command_state == cs_deps_running);
522 f = f->prev;
524 while (f != 0);
527 if (dep_status != 0 && !keep_going_flag)
528 break;
530 if (!running)
531 d->changed = ((file->phony && file->cmds != 0)
532 || file_mtime (d->file) != mtime);
536 file->updating = 0;
538 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
540 if (running)
542 set_command_state (file, cs_deps_running);
543 --depth;
544 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
545 return 0;
548 /* If any dependency failed, give up now. */
550 if (dep_status != 0)
552 file->update_status = dep_status;
553 notice_finished_file (file);
555 depth--;
557 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
559 if (depth == 0 && keep_going_flag
560 && !just_print_flag && !question_flag)
561 error (NILF,
562 _("Target `%s' not remade because of errors."), file->name);
564 return dep_status;
567 if (file->command_state == cs_deps_running)
568 /* The commands for some deps were running on the last iteration, but
569 they have finished now. Reset the command_state to not_started to
570 simplify later bookkeeping. It is important that we do this only
571 when the prior state was cs_deps_running, because that prior state
572 was definitely propagated to FILE's also_make's by set_command_state
573 (called above), but in another state an also_make may have
574 independently changed to finished state, and we would confuse that
575 file's bookkeeping (updated, but not_started is bogus state). */
576 set_command_state (file, cs_not_started);
578 /* Now record which dependencies are more
579 recent than this file, so we can define $?. */
581 deps_changed = 0;
582 for (d = file->deps; d != 0; d = d->next)
584 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
585 check_renamed (d->file);
587 #if 1 /* %%% In version 4, remove this code completely to
588 implement not remaking deps if their deps are newer
589 than their parents. */
590 if (d_mtime == (FILE_TIMESTAMP) -1 && !d->file->intermediate)
591 /* We must remake if this dep does not
592 exist and is not intermediate. */
593 must_make = 1;
594 #endif
596 /* Set DEPS_CHANGED if this dep actually changed. */
597 deps_changed |= d->changed;
599 /* Set D->changed if either this dep actually changed,
600 or its dependent, FILE, is older or does not exist. */
601 d->changed |= noexist || d_mtime > this_mtime;
603 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
605 const char *fmt = 0;
607 if (d_mtime == (FILE_TIMESTAMP) -1)
609 if (ISDB (DB_BASIC))
610 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
612 else if (d->changed)
614 if (ISDB (DB_BASIC))
615 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
617 else if (ISDB (DB_VERBOSE))
618 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
620 if (fmt)
622 print_spaces (depth);
623 printf (fmt, dep_name (d), file->name);
624 fflush (stdout);
629 /* Here depth returns to the value it had when we were called. */
630 depth--;
632 if (file->double_colon && file->deps == 0)
634 must_make = 1;
635 DBF (DB_BASIC,
636 _("Target `%s' is double-colon and has no prerequisites.\n"));
638 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
640 must_make = 0;
641 DBF (DB_VERBOSE,
642 _("No commands for `%s' and no prerequisites actually changed.\n"));
645 if (!must_make)
647 if (ISDB (DB_VERBOSE))
649 print_spaces (depth);
650 printf (_("No need to remake target `%s'"), file->name);
651 if (!streq (file->name, file->hname))
652 printf (_("; using VPATH name `%s'"), file->hname);
653 puts (".");
654 fflush (stdout);
657 notice_finished_file (file);
659 /* Since we don't need to remake the file, convert it to use the
660 VPATH filename if we found one. hfile will be either the
661 local name if no VPATH or the VPATH name if one was found. */
663 while (file)
665 file->name = file->hname;
666 file = file->prev;
669 return 0;
672 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
674 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
675 VPATH. */
676 if (!streq(file->name, file->hname))
678 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
679 file->ignore_vpath = 1;
682 /* Now, take appropriate actions to remake the file. */
683 remake_file (file);
685 if (file->command_state != cs_finished)
687 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
688 return 0;
691 switch (file->update_status)
693 case 2:
694 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
695 break;
696 case 0:
697 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
698 break;
699 case 1:
700 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
701 break;
702 default:
703 assert (file->update_status >= 0 && file->update_status <= 2);
704 break;
707 file->updated = 1;
708 return file->update_status;
711 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
712 files listed in its `also_make' member. Under -t, this function also
713 touches FILE.
715 On return, FILE->update_status will no longer be -1 if it was. */
717 void
718 notice_finished_file (file)
719 register struct file *file;
721 struct dep *d;
722 int ran = file->command_state == cs_running;
724 file->command_state = cs_finished;
725 file->updated = 1;
727 if (touch_flag
728 /* The update status will be:
729 -1 if this target was not remade;
730 0 if 0 or more commands (+ or ${MAKE}) were run and won;
731 1 if some commands were run and lost.
732 We touch the target if it has commands which either were not run
733 or won when they ran (i.e. status is 0). */
734 && file->update_status == 0)
736 if (file->cmds != 0 && file->cmds->any_recurse)
738 /* If all the command lines were recursive,
739 we don't want to do the touching. */
740 unsigned int i;
741 for (i = 0; i < file->cmds->ncommand_lines; ++i)
742 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
743 goto have_nonrecursing;
745 else
747 have_nonrecursing:
748 if (file->phony)
749 file->update_status = 0;
750 else
751 /* Should set file's modification date and do nothing else. */
752 file->update_status = touch_file (file);
756 if (file->mtime_before_update == 0)
757 file->mtime_before_update = file->last_mtime;
759 if (ran && !file->phony)
761 struct file *f;
762 int i = 0;
764 /* If -n or -q and all the commands are recursive, we ran them so
765 really check the target's mtime again. Otherwise, assume the target
766 would have been updated. */
768 if (question_flag || just_print_flag)
770 for (i = file->cmds->ncommand_lines; i > 0; --i)
771 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
772 break;
775 /* If there were no commands at all, it's always new. */
777 else if (file->is_target && file->cmds == 0)
778 i = 1;
780 file->last_mtime = i == 0 ? 0 : NEW_MTIME;
782 /* Propagate the change of modification time to all the double-colon
783 entries for this file. */
784 for (f = file->double_colon; f != 0; f = f->next)
785 f->last_mtime = file->last_mtime;
788 if (ran && file->update_status != -1)
789 /* We actually tried to update FILE, which has
790 updated its also_make's as well (if it worked).
791 If it didn't work, it wouldn't work again for them.
792 So mark them as updated with the same status. */
793 for (d = file->also_make; d != 0; d = d->next)
795 d->file->command_state = cs_finished;
796 d->file->updated = 1;
797 d->file->update_status = file->update_status;
799 if (ran && !d->file->phony)
800 /* Fetch the new modification time.
801 We do this instead of just invalidating the cached time
802 so that a vpath_search can happen. Otherwise, it would
803 never be done because the target is already updated. */
804 (void) f_mtime (d->file, 0);
806 else if (file->update_status == -1)
807 /* Nothing was done for FILE, but it needed nothing done.
808 So mark it now as "succeeded". */
809 file->update_status = 0;
812 /* Check whether another file (whose mtime is THIS_MTIME)
813 needs updating on account of a dependency which is file FILE.
814 If it does, store 1 in *MUST_MAKE_PTR.
815 In the process, update any non-intermediate files
816 that FILE depends on (including FILE itself).
817 Return nonzero if any updating failed. */
819 static int
820 check_dep (file, depth, this_mtime, must_make_ptr)
821 struct file *file;
822 unsigned int depth;
823 FILE_TIMESTAMP this_mtime;
824 int *must_make_ptr;
826 register struct dep *d;
827 int dep_status = 0;
829 ++depth;
830 file->updating = 1;
832 if (!file->intermediate)
833 /* If this is a non-intermediate file, update it and record
834 whether it is newer than THIS_MTIME. */
836 FILE_TIMESTAMP mtime;
837 dep_status = update_file (file, depth);
838 check_renamed (file);
839 mtime = file_mtime (file);
840 check_renamed (file);
841 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
842 *must_make_ptr = 1;
844 else
846 /* FILE is an intermediate file. */
847 FILE_TIMESTAMP mtime;
849 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
851 if (try_implicit_rule (file, depth))
852 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
853 else
854 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
855 file->tried_implicit = 1;
857 if (file->cmds == 0 && !file->is_target
858 && default_file != 0 && default_file->cmds != 0)
860 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
861 file->cmds = default_file->cmds;
864 /* If the intermediate file actually exists
865 and is newer, then we should remake from it. */
866 check_renamed (file);
867 mtime = file_mtime (file);
868 check_renamed (file);
869 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
870 *must_make_ptr = 1;
871 /* Otherwise, update all non-intermediate files we depend on,
872 if necessary, and see whether any of them is more
873 recent than the file on whose behalf we are checking. */
874 else
876 register struct dep *lastd;
878 lastd = 0;
879 d = file->deps;
880 while (d != 0)
882 if (d->file->updating)
884 error (NILF, _("Circular %s <- %s dependency dropped."),
885 file->name, d->file->name);
886 if (lastd == 0)
888 file->deps = d->next;
889 free ((char *) d);
890 d = file->deps;
892 else
894 lastd->next = d->next;
895 free ((char *) d);
896 d = lastd->next;
898 continue;
901 d->file->parent = file;
902 dep_status |= check_dep (d->file, depth, this_mtime,
903 must_make_ptr);
904 check_renamed (d->file);
905 if (dep_status != 0 && !keep_going_flag)
906 break;
908 if (d->file->command_state == cs_running
909 || d->file->command_state == cs_deps_running)
910 /* Record that some of FILE's deps are still being made.
911 This tells the upper levels to wait on processing it until
912 the commands are finished. */
913 set_command_state (file, cs_deps_running);
915 lastd = d;
916 d = d->next;
921 file->updating = 0;
922 return dep_status;
925 /* Touch FILE. Return zero if successful, one if not. */
927 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
929 static int
930 touch_file (file)
931 register struct file *file;
933 if (!silent_flag)
934 message (0, "touch %s", file->name);
936 #ifndef NO_ARCHIVES
937 if (ar_name (file->name))
938 return ar_touch (file->name);
939 else
940 #endif
942 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
944 if (fd < 0)
945 TOUCH_ERROR ("touch: open: ");
946 else
948 struct stat statbuf;
949 char buf;
950 int status;
953 status = fstat (fd, &statbuf);
954 while (status < 0 && EINTR_SET);
956 if (status < 0)
957 TOUCH_ERROR ("touch: fstat: ");
958 /* Rewrite character 0 same as it already is. */
959 if (read (fd, &buf, 1) < 0)
960 TOUCH_ERROR ("touch: read: ");
961 if (lseek (fd, 0L, 0) < 0L)
962 TOUCH_ERROR ("touch: lseek: ");
963 if (write (fd, &buf, 1) < 0)
964 TOUCH_ERROR ("touch: write: ");
965 /* If file length was 0, we just
966 changed it, so change it back. */
967 if (statbuf.st_size == 0)
969 (void) close (fd);
970 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
971 if (fd < 0)
972 TOUCH_ERROR ("touch: open: ");
974 (void) close (fd);
978 return 0;
981 /* Having checked and updated the dependencies of FILE,
982 do whatever is appropriate to remake FILE itself.
983 Return the status from executing FILE's commands. */
985 static void
986 remake_file (file)
987 struct file *file;
989 if (file->cmds == 0)
991 if (file->phony)
992 /* Phony target. Pretend it succeeded. */
993 file->update_status = 0;
994 else if (file->is_target)
995 /* This is a nonexistent target file we cannot make.
996 Pretend it was successfully remade. */
997 file->update_status = 0;
998 else
999 no_rule_error (file);
1001 else
1003 chop_commands (file->cmds);
1005 /* The normal case: start some commands. */
1006 if (!touch_flag || file->cmds->any_recurse)
1008 execute_file_commands (file);
1009 return;
1012 /* This tells notice_finished_file it is ok to touch the file. */
1013 file->update_status = 0;
1016 /* This does the touching under -t. */
1017 notice_finished_file (file);
1020 /* Return the mtime of a file, given a `struct file'.
1021 Caches the time in the struct file to avoid excess stat calls.
1023 If the file is not found, and SEARCH is nonzero, VPATH searching and
1024 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1025 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1026 FILE. */
1028 FILE_TIMESTAMP
1029 f_mtime (file, search)
1030 register struct file *file;
1031 int search;
1033 FILE_TIMESTAMP mtime;
1035 /* File's mtime is not known; must get it from the system. */
1037 #ifndef NO_ARCHIVES
1038 if (ar_name (file->name))
1040 /* This file is an archive-member reference. */
1042 char *arname, *memname;
1043 struct file *arfile;
1044 int arname_used = 0;
1046 /* Find the archive's name. */
1047 ar_parse_name (file->name, &arname, &memname);
1049 /* Find the modification time of the archive itself.
1050 Also allow for its name to be changed via VPATH search. */
1051 arfile = lookup_file (arname);
1052 if (arfile == 0)
1054 arfile = enter_file (arname);
1055 arname_used = 1;
1057 mtime = f_mtime (arfile, search);
1058 check_renamed (arfile);
1059 if (search && strcmp (arfile->hname, arname))
1061 /* The archive's name has changed.
1062 Change the archive-member reference accordingly. */
1064 char *name;
1065 unsigned int arlen, memlen;
1067 if (!arname_used)
1069 free (arname);
1070 arname_used = 1;
1073 arname = arfile->hname;
1074 arlen = strlen (arname);
1075 memlen = strlen (memname);
1077 /* free (file->name); */
1079 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1080 bcopy (arname, name, arlen);
1081 name[arlen] = '(';
1082 bcopy (memname, name + arlen + 1, memlen);
1083 name[arlen + 1 + memlen] = ')';
1084 name[arlen + 1 + memlen + 1] = '\0';
1086 /* If the archive was found with GPATH, make the change permanent;
1087 otherwise defer it until later. */
1088 if (arfile->name == arfile->hname)
1089 rename_file (file, name);
1090 else
1091 rehash_file (file, name);
1092 check_renamed (file);
1095 if (!arname_used)
1096 free (arname);
1097 free (memname);
1099 if (mtime == (FILE_TIMESTAMP) -1)
1100 /* The archive doesn't exist, so it's members don't exist either. */
1101 return (FILE_TIMESTAMP) -1;
1103 mtime = ar_member_date (file->hname);
1105 else
1106 #endif
1108 mtime = name_mtime (file->name);
1110 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1112 /* If name_mtime failed, search VPATH. */
1113 char *name = file->name;
1114 if (vpath_search (&name, &mtime)
1115 /* Last resort, is it a library (-lxxx)? */
1116 || (name[0] == '-' && name[1] == 'l'
1117 && library_search (&name, &mtime)))
1119 if (mtime != 0)
1120 /* vpath_search and library_search store zero in MTIME
1121 if they didn't need to do a stat call for their work. */
1122 file->last_mtime = mtime;
1124 /* If we found it in VPATH, see if it's in GPATH too; if so,
1125 change the name right now; if not, defer until after the
1126 dependencies are updated. */
1127 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1129 rename_file (file, name);
1130 check_renamed (file);
1131 return file_mtime (file);
1134 rehash_file (file, name);
1135 check_renamed (file);
1136 mtime = name_mtime (name);
1142 /* Files can have bogus timestamps that nothing newly made will be
1143 "newer" than. Updating their dependents could just result in loops.
1144 So notify the user of the anomaly with a warning.
1146 We only need to do this once, for now. */
1148 static FILE_TIMESTAMP now = 0;
1149 if (!clock_skew_detected
1150 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1151 && !file->updated)
1153 /* This file's time appears to be in the future.
1154 Update our concept of the present, and compare again. */
1156 now = file_timestamp_now ();
1158 #ifdef WINDOWS32
1160 * FAT filesystems round time to nearest even second(!). Just
1161 * allow for any file (NTFS or FAT) to perhaps suffer from this
1162 * braindamage.
1164 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1165 #else
1166 #ifdef __MSDOS__
1167 /* Scrupulous testing indicates that some Windows
1168 filesystems can set file times up to 3 sec into the future! */
1169 if (mtime > now + 3)
1170 #else
1171 if (mtime > now)
1172 #endif
1173 #endif
1175 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1176 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1178 file_timestamp_sprintf (mtimebuf, mtime);
1179 file_timestamp_sprintf (nowbuf, now);
1180 error (NILF, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1181 file->name, mtimebuf, nowbuf);
1182 clock_skew_detected = 1;
1187 /* Store the mtime into all the entries for this file. */
1188 if (file->double_colon)
1189 file = file->double_colon;
1193 /* If this file is not implicit but it is intermediate then it was
1194 made so by the .INTERMEDIATE target. If this file has never
1195 been built by us but was found now, it existed before make
1196 started. So, turn off the intermediate bit so make doesn't
1197 delete it, since it didn't create it. */
1198 if (mtime != (FILE_TIMESTAMP)-1 && file->command_state == cs_not_started
1199 && !file->tried_implicit && file->intermediate)
1200 file->intermediate = 0;
1202 file->last_mtime = mtime;
1203 file = file->prev;
1205 while (file != 0);
1207 return mtime;
1211 /* Return the mtime of the file or archive-member reference NAME. */
1213 static FILE_TIMESTAMP
1214 name_mtime (name)
1215 register char *name;
1217 struct stat st;
1219 if (stat (name, &st) < 0)
1220 return (FILE_TIMESTAMP) -1;
1222 return FILE_TIMESTAMP_STAT_MODTIME (st);
1226 /* Search for a library file specified as -lLIBNAME, searching for a
1227 suitable library file in the system library directories and the VPATH
1228 directories. */
1230 static int
1231 library_search (lib, mtime_ptr)
1232 char **lib;
1233 FILE_TIMESTAMP *mtime_ptr;
1235 static char *dirs[] =
1237 #ifndef _AMIGA
1238 "/lib",
1239 "/usr/lib",
1240 #endif
1241 #if defined(WINDOWS32) && !defined(LIBDIR)
1243 * This is completely up to the user at product install time. Just define
1244 * a placeholder.
1246 #define LIBDIR "."
1247 #endif
1248 LIBDIR, /* Defined by configuration. */
1252 static char *libpatterns = NULL;
1254 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1255 FILE_TIMESTAMP mtime;
1257 /* Loop variables for the libpatterns value. */
1258 char *p, *p2;
1259 unsigned int len;
1261 char *file, **dp;
1263 /* If we don't have libpatterns, get it. */
1264 if (!libpatterns)
1266 int save = warn_undefined_variables_flag;
1267 warn_undefined_variables_flag = 0;
1269 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1271 warn_undefined_variables_flag = save;
1274 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1275 p2 = libpatterns;
1276 while ((p = find_next_token (&p2, &len)) != 0)
1278 static char *buf = NULL;
1279 static int buflen = 0;
1280 static int libdir_maxlen = -1;
1281 char *libbuf = variable_expand ("");
1283 /* Expand the pattern using LIBNAME as a replacement. */
1285 char c = p[len];
1286 char *p3, *p4;
1288 p[len] = '\0';
1289 p3 = find_percent (p);
1290 if (!p3)
1292 /* Give a warning if there is no pattern, then remove the
1293 pattern so it's ignored next time. */
1294 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1295 for (; len; --len, ++p)
1296 *p = ' ';
1297 *p = c;
1298 continue;
1300 p4 = variable_buffer_output (libbuf, p, p3-p);
1301 p4 = variable_buffer_output (p4, libname, strlen (libname));
1302 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1303 p[len] = c;
1306 /* Look first for `libNAME.a' in the current directory. */
1307 mtime = name_mtime (libbuf);
1308 if (mtime != (FILE_TIMESTAMP) -1)
1310 *lib = xstrdup (libbuf);
1311 if (mtime_ptr != 0)
1312 *mtime_ptr = mtime;
1313 return 1;
1316 /* Now try VPATH search on that. */
1318 file = libbuf;
1319 if (vpath_search (&file, mtime_ptr))
1321 *lib = file;
1322 return 1;
1325 /* Now try the standard set of directories. */
1327 if (!buflen)
1329 for (dp = dirs; *dp != 0; ++dp)
1331 int l = strlen (*dp);
1332 if (l > libdir_maxlen)
1333 libdir_maxlen = l;
1335 buflen = strlen (libbuf);
1336 buf = xmalloc(libdir_maxlen + buflen + 2);
1338 else if (buflen < strlen (libbuf))
1340 buflen = strlen (libbuf);
1341 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1344 for (dp = dirs; *dp != 0; ++dp)
1346 sprintf (buf, "%s/%s", *dp, libbuf);
1347 mtime = name_mtime (buf);
1348 if (mtime != (FILE_TIMESTAMP) -1)
1350 *lib = xstrdup (buf);
1351 if (mtime_ptr != 0)
1352 *mtime_ptr = mtime;
1353 return 1;
1358 return 0;