Updates and fixes for CVS builds.
[make/kirr.git] / remake.c
blobae5f37cc362666cad77070ef7f2beff62a4d5c54
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 /* The test for circular dependencies is based on the 'updating' bit in
47 `struct file'. However, double colon targets have seperate `struct
48 file's; make sure we always use the base of the double colon chain. */
50 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
51 ->updating = 1)
52 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
53 ->updating = 0)
54 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
55 ->updating)
58 /* Incremented when a command is started (under -n, when one would be). */
59 unsigned int commands_started = 0;
61 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
62 static unsigned int considered;
64 static int update_file PARAMS ((struct file *file, unsigned int depth));
65 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
66 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
67 static int touch_file PARAMS ((struct file *file));
68 static void remake_file PARAMS ((struct file *file));
69 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
70 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
73 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
74 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
75 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
76 be disabled for them unless they were also command-line targets, and we
77 should only make one goal at a time and return as soon as one goal whose
78 `changed' member is nonzero is successfully made. */
80 int
81 update_goal_chain (goals, makefiles)
82 register struct dep *goals;
83 int makefiles;
85 int t = touch_flag, q = question_flag, n = just_print_flag;
86 unsigned int j = job_slots;
87 int status = -1;
89 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
90 : file_mtime (file))
92 /* Duplicate the chain so we can remove things from it. */
94 goals = copy_dep_chain (goals);
97 /* Clear the `changed' flag of each goal in the chain.
98 We will use the flag below to notice when any commands
99 have actually been run for a target. When no commands
100 have been run, we give an "up to date" diagnostic. */
102 struct dep *g;
103 for (g = goals; g != 0; g = g->next)
104 g->changed = 0;
107 /* All files start with the considered bit 0, so the global value is 1. */
108 considered = 1;
110 /* Update all the goals until they are all finished. */
112 while (goals != 0)
114 register struct dep *g, *lastgoal;
116 /* Start jobs that are waiting for the load to go down. */
118 start_waiting_jobs ();
120 /* Wait for a child to die. */
122 reap_children (1, 0);
124 lastgoal = 0;
125 g = goals;
126 while (g != 0)
128 /* Iterate over all double-colon entries for this file. */
129 struct file *file;
130 int stop = 0, any_not_updated = 0;
132 for (file = g->file->double_colon ? g->file->double_colon : g->file;
133 file != NULL;
134 file = file->prev)
136 unsigned int ocommands_started;
137 int x;
138 check_renamed (file);
139 if (makefiles)
141 if (file->cmd_target)
143 touch_flag = t;
144 question_flag = q;
145 just_print_flag = n;
147 else
148 touch_flag = question_flag = just_print_flag = 0;
151 /* Save the old value of `commands_started' so we can compare
152 later. It will be incremented when any commands are
153 actually run. */
154 ocommands_started = commands_started;
156 x = update_file (file, makefiles ? 1 : 0);
157 check_renamed (file);
159 /* Set the goal's `changed' flag if any commands were started
160 by calling update_file above. We check this flag below to
161 decide when to give an "up to date" diagnostic. */
162 g->changed += commands_started - ocommands_started;
164 /* If we updated a file and STATUS was not already 1, set it to
165 1 if updating failed, or to 0 if updating succeeded. Leave
166 STATUS as it is if no updating was done. */
168 stop = 0;
169 if ((x != 0 || file->updated) && status < 1)
171 if (file->update_status != 0)
173 /* Updating failed, or -q triggered. The STATUS value
174 tells our caller which. */
175 status = file->update_status;
176 /* If -q just triggered, stop immediately. It doesn't
177 matter how much more we run, since we already know
178 the answer to return. */
179 stop = (!keep_going_flag && !question_flag
180 && !makefiles);
182 else
184 FILE_TIMESTAMP mtime = MTIME (file);
185 check_renamed (file);
187 if (file->updated && g->changed &&
188 mtime != file->mtime_before_update)
190 /* Updating was done. If this is a makefile and
191 just_print_flag or question_flag is set (meaning
192 -n or -q was given and this file was specified
193 as a command-line target), don't change STATUS.
194 If STATUS is changed, we will get re-exec'd, and
195 enter an infinite loop. */
196 if (!makefiles
197 || (!just_print_flag && !question_flag))
198 status = 0;
199 if (makefiles && file->dontcare)
200 /* This is a default makefile; stop remaking. */
201 stop = 1;
206 /* Keep track if any double-colon entry is not finished.
207 When they are all finished, the goal is finished. */
208 any_not_updated |= !file->updated;
210 if (stop)
211 break;
214 /* Reset FILE since it is null at the end of the loop. */
215 file = g->file;
217 if (stop || !any_not_updated)
219 /* If we have found nothing whatever to do for the goal,
220 print a message saying nothing needs doing. */
222 if (!makefiles
223 /* If the update_status is zero, we updated successfully
224 or not at all. G->changed will have been set above if
225 any commands were actually started for this goal. */
226 && file->update_status == 0 && !g->changed
227 /* Never give a message under -s or -q. */
228 && !silent_flag && !question_flag)
229 message (1, ((file->phony || file->cmds == 0)
230 ? _("Nothing to be done for `%s'.")
231 : _("`%s' is up to date.")),
232 file->name);
234 /* This goal is finished. Remove it from the chain. */
235 if (lastgoal == 0)
236 goals = g->next;
237 else
238 lastgoal->next = g->next;
240 /* Free the storage. */
241 free ((char *) g);
243 g = lastgoal == 0 ? goals : lastgoal->next;
245 if (stop)
246 break;
248 else
250 lastgoal = g;
251 g = g->next;
255 /* If we reached the end of the dependency graph toggle the considered
256 flag for the next pass. */
257 if (g == 0)
258 considered = !considered;
261 if (makefiles)
263 touch_flag = t;
264 question_flag = q;
265 just_print_flag = n;
266 job_slots = j;
268 return status;
271 /* If FILE is not up to date, execute the commands for it.
272 Return 0 if successful, 1 if unsuccessful;
273 but with some flag settings, just call `exit' if unsuccessful.
275 DEPTH is the depth in recursions of this function.
276 We increment it during the consideration of our dependencies,
277 then decrement it again after finding out whether this file
278 is out of date.
280 If there are multiple double-colon entries for FILE,
281 each is considered in turn. */
283 static int
284 update_file (file, depth)
285 struct file *file;
286 unsigned int depth;
288 register int status = 0;
289 register struct file *f;
291 f = file->double_colon ? file->double_colon : file;
293 /* Prune the dependency graph: if we've already been here on _this_
294 pass through the dependency graph, we don't have to go any further.
295 We won't reap_children until we start the next pass, so no state
296 change is possible below here until then. */
297 if (f->considered == considered)
299 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
300 return f->command_state == cs_finished ? f->update_status : 0;
303 /* This loop runs until we start commands for a double colon rule, or until
304 the chain is exhausted. */
305 for (; f != 0; f = f->prev)
307 f->considered = considered;
309 status |= update_file_1 (f, depth);
310 check_renamed (f);
312 if (status != 0 && !keep_going_flag)
313 break;
315 if (f->command_state == cs_running
316 || f->command_state == cs_deps_running)
318 /* Don't run the other :: rules for this
319 file until this rule is finished. */
320 status = 0;
321 break;
325 /* Process the remaining rules in the double colon chain so they're marked
326 considered. Start their prerequisites, too. */
327 for (; f != 0 ; f = f->prev)
329 struct dep *d;
331 f->considered = considered;
333 for (d = f->deps; d != 0; d = d->next)
334 status |= update_file (d->file, depth + 1);
337 return status;
340 /* Consider a single `struct file' and update it as appropriate. */
342 static int
343 update_file_1 (file, depth)
344 struct file *file;
345 unsigned int depth;
347 register FILE_TIMESTAMP this_mtime;
348 int noexist, must_make, deps_changed;
349 int dep_status = 0;
350 register struct dep *d, *lastd;
351 int running = 0;
353 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
355 if (file->updated)
357 if (file->update_status > 0)
359 DBF (DB_VERBOSE,
360 _("Recently tried and failed to update file `%s'.\n"));
361 return file->update_status;
364 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
365 return 0;
368 switch (file->command_state)
370 case cs_not_started:
371 case cs_deps_running:
372 break;
373 case cs_running:
374 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
375 return 0;
376 case cs_finished:
377 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
378 return file->update_status;
379 default:
380 abort ();
383 ++depth;
385 /* Notice recursive update of the same file. */
386 start_updating (file);
388 /* Looking at the file's modtime beforehand allows the possibility
389 that its name may be changed by a VPATH search, and thus it may
390 not need an implicit rule. If this were not done, the file
391 might get implicit commands that apply to its initial name, only
392 to have that name replaced with another found by VPATH search. */
394 this_mtime = file_mtime (file);
395 check_renamed (file);
396 noexist = this_mtime == NONEXISTENT_MTIME;
397 if (noexist)
398 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
399 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
400 && file->low_resolution_time)
402 /* Avoid spurious rebuilds due to low resolution time stamps. */
403 int ns = FILE_TIMESTAMP_NS (this_mtime);
404 if (ns != 0)
405 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
406 file->name);
407 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
410 must_make = noexist;
412 /* If file was specified as a target with no commands,
413 come up with some default commands. */
415 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
417 if (try_implicit_rule (file, depth))
418 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
419 else
420 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
421 file->tried_implicit = 1;
423 if (file->cmds == 0 && !file->is_target
424 && default_file != 0 && default_file->cmds != 0)
426 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
427 file->cmds = default_file->cmds;
430 /* Update all non-intermediate files we depend on, if necessary,
431 and see whether any of them is more recent than this file. */
433 lastd = 0;
434 d = file->deps;
435 while (d != 0)
437 FILE_TIMESTAMP mtime;
439 check_renamed (d->file);
441 mtime = file_mtime (d->file);
442 check_renamed (d->file);
444 if (is_updating (d->file))
446 error (NILF, _("Circular %s <- %s dependency dropped."),
447 file->name, d->file->name);
448 /* We cannot free D here because our the caller will still have
449 a reference to it when we were called recursively via
450 check_dep below. */
451 if (lastd == 0)
452 file->deps = d->next;
453 else
454 lastd->next = d->next;
455 d = d->next;
456 continue;
459 d->file->parent = file;
460 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
461 check_renamed (d->file);
464 register struct file *f = d->file;
465 if (f->double_colon)
466 f = f->double_colon;
469 running |= (f->command_state == cs_running
470 || f->command_state == cs_deps_running);
471 f = f->prev;
473 while (f != 0);
476 if (dep_status != 0 && !keep_going_flag)
477 break;
479 if (!running)
480 d->changed = file_mtime (d->file) != mtime;
482 lastd = d;
483 d = d->next;
486 /* Now we know whether this target needs updating.
487 If it does, update all the intermediate files we depend on. */
489 if (must_make)
491 for (d = file->deps; d != 0; d = d->next)
492 if (d->file->intermediate)
494 FILE_TIMESTAMP mtime = file_mtime (d->file);
495 check_renamed (d->file);
496 d->file->parent = file;
497 dep_status |= update_file (d->file, depth);
498 check_renamed (d->file);
501 register struct file *f = d->file;
502 if (f->double_colon)
503 f = f->double_colon;
506 running |= (f->command_state == cs_running
507 || f->command_state == cs_deps_running);
508 f = f->prev;
510 while (f != 0);
513 if (dep_status != 0 && !keep_going_flag)
514 break;
516 if (!running)
517 d->changed = ((file->phony && file->cmds != 0)
518 || file_mtime (d->file) != mtime);
522 finish_updating (file);
524 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
526 if (running)
528 set_command_state (file, cs_deps_running);
529 --depth;
530 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
531 return 0;
534 /* If any dependency failed, give up now. */
536 if (dep_status != 0)
538 file->update_status = dep_status;
539 notice_finished_file (file);
541 depth--;
543 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
545 if (depth == 0 && keep_going_flag
546 && !just_print_flag && !question_flag)
547 error (NILF,
548 _("Target `%s' not remade because of errors."), file->name);
550 return dep_status;
553 if (file->command_state == cs_deps_running)
554 /* The commands for some deps were running on the last iteration, but
555 they have finished now. Reset the command_state to not_started to
556 simplify later bookkeeping. It is important that we do this only
557 when the prior state was cs_deps_running, because that prior state
558 was definitely propagated to FILE's also_make's by set_command_state
559 (called above), but in another state an also_make may have
560 independently changed to finished state, and we would confuse that
561 file's bookkeeping (updated, but not_started is bogus state). */
562 set_command_state (file, cs_not_started);
564 /* Now record which dependencies are more
565 recent than this file, so we can define $?. */
567 deps_changed = 0;
568 for (d = file->deps; d != 0; d = d->next)
570 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
571 check_renamed (d->file);
573 #if 1 /* %%% In version 4, remove this code completely to
574 implement not remaking deps if their deps are newer
575 than their parents. */
576 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
577 /* We must remake if this dep does not
578 exist and is not intermediate. */
579 must_make = 1;
580 #endif
582 /* Set DEPS_CHANGED if this dep actually changed. */
583 deps_changed |= d->changed;
585 /* Set D->changed if either this dep actually changed,
586 or its dependent, FILE, is older or does not exist. */
587 d->changed |= noexist || d_mtime > this_mtime;
589 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
591 const char *fmt = 0;
593 if (d_mtime == NONEXISTENT_MTIME)
595 if (ISDB (DB_BASIC))
596 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
598 else if (d->changed)
600 if (ISDB (DB_BASIC))
601 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
603 else if (ISDB (DB_VERBOSE))
604 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
606 if (fmt)
608 print_spaces (depth);
609 printf (fmt, dep_name (d), file->name);
610 fflush (stdout);
615 /* Here depth returns to the value it had when we were called. */
616 depth--;
618 if (file->double_colon && file->deps == 0)
620 must_make = 1;
621 DBF (DB_BASIC,
622 _("Target `%s' is double-colon and has no prerequisites.\n"));
624 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
626 must_make = 0;
627 DBF (DB_VERBOSE,
628 _("No commands for `%s' and no prerequisites actually changed.\n"));
631 if (!must_make)
633 if (ISDB (DB_VERBOSE))
635 print_spaces (depth);
636 printf (_("No need to remake target `%s'"), file->name);
637 if (!streq (file->name, file->hname))
638 printf (_("; using VPATH name `%s'"), file->hname);
639 puts (".");
640 fflush (stdout);
643 notice_finished_file (file);
645 /* Since we don't need to remake the file, convert it to use the
646 VPATH filename if we found one. hfile will be either the
647 local name if no VPATH or the VPATH name if one was found. */
649 while (file)
651 file->name = file->hname;
652 file = file->prev;
655 return 0;
658 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
660 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
661 VPATH. */
662 if (!streq(file->name, file->hname))
664 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
665 file->ignore_vpath = 1;
668 /* Now, take appropriate actions to remake the file. */
669 remake_file (file);
671 if (file->command_state != cs_finished)
673 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
674 return 0;
677 switch (file->update_status)
679 case 2:
680 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
681 break;
682 case 0:
683 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
684 break;
685 case 1:
686 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
687 break;
688 default:
689 assert (file->update_status >= 0 && file->update_status <= 2);
690 break;
693 file->updated = 1;
694 return file->update_status;
697 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
698 files listed in its `also_make' member. Under -t, this function also
699 touches FILE.
701 On return, FILE->update_status will no longer be -1 if it was. */
703 void
704 notice_finished_file (file)
705 register struct file *file;
707 struct dep *d;
708 int ran = file->command_state == cs_running;
710 file->command_state = cs_finished;
711 file->updated = 1;
713 if (touch_flag
714 /* The update status will be:
715 -1 if this target was not remade;
716 0 if 0 or more commands (+ or ${MAKE}) were run and won;
717 1 if some commands were run and lost.
718 We touch the target if it has commands which either were not run
719 or won when they ran (i.e. status is 0). */
720 && file->update_status == 0)
722 if (file->cmds != 0 && file->cmds->any_recurse)
724 /* If all the command lines were recursive,
725 we don't want to do the touching. */
726 unsigned int i;
727 for (i = 0; i < file->cmds->ncommand_lines; ++i)
728 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
729 goto have_nonrecursing;
731 else
733 have_nonrecursing:
734 if (file->phony)
735 file->update_status = 0;
736 else
737 /* Should set file's modification date and do nothing else. */
738 file->update_status = touch_file (file);
742 if (file->mtime_before_update == UNKNOWN_MTIME)
743 file->mtime_before_update = file->last_mtime;
745 if (ran && !file->phony)
747 struct file *f;
748 int i = 0;
750 /* If -n or -q and all the commands are recursive, we ran them so
751 really check the target's mtime again. Otherwise, assume the target
752 would have been updated. */
754 if (question_flag || just_print_flag)
756 for (i = file->cmds->ncommand_lines; i > 0; --i)
757 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
758 break;
761 /* If there were no commands at all, it's always new. */
763 else if (file->is_target && file->cmds == 0)
764 i = 1;
766 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
768 /* Propagate the change of modification time to all the double-colon
769 entries for this file. */
770 for (f = file->double_colon; f != 0; f = f->prev)
771 f->last_mtime = file->last_mtime;
774 if (ran && file->update_status != -1)
775 /* We actually tried to update FILE, which has
776 updated its also_make's as well (if it worked).
777 If it didn't work, it wouldn't work again for them.
778 So mark them as updated with the same status. */
779 for (d = file->also_make; d != 0; d = d->next)
781 d->file->command_state = cs_finished;
782 d->file->updated = 1;
783 d->file->update_status = file->update_status;
785 if (ran && !d->file->phony)
786 /* Fetch the new modification time.
787 We do this instead of just invalidating the cached time
788 so that a vpath_search can happen. Otherwise, it would
789 never be done because the target is already updated. */
790 (void) f_mtime (d->file, 0);
792 else if (file->update_status == -1)
793 /* Nothing was done for FILE, but it needed nothing done.
794 So mark it now as "succeeded". */
795 file->update_status = 0;
798 /* Check whether another file (whose mtime is THIS_MTIME)
799 needs updating on account of a dependency which is file FILE.
800 If it does, store 1 in *MUST_MAKE_PTR.
801 In the process, update any non-intermediate files
802 that FILE depends on (including FILE itself).
803 Return nonzero if any updating failed. */
805 static int
806 check_dep (file, depth, this_mtime, must_make_ptr)
807 struct file *file;
808 unsigned int depth;
809 FILE_TIMESTAMP this_mtime;
810 int *must_make_ptr;
812 register struct dep *d;
813 int dep_status = 0;
815 ++depth;
816 start_updating (file);
818 if (!file->intermediate)
819 /* If this is a non-intermediate file, update it and record
820 whether it is newer than THIS_MTIME. */
822 FILE_TIMESTAMP mtime;
823 dep_status = update_file (file, depth);
824 check_renamed (file);
825 mtime = file_mtime (file);
826 check_renamed (file);
827 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
828 *must_make_ptr = 1;
830 else
832 /* FILE is an intermediate file. */
833 FILE_TIMESTAMP mtime;
835 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
837 if (try_implicit_rule (file, depth))
838 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
839 else
840 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
841 file->tried_implicit = 1;
843 if (file->cmds == 0 && !file->is_target
844 && default_file != 0 && default_file->cmds != 0)
846 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
847 file->cmds = default_file->cmds;
850 /* If the intermediate file actually exists
851 and is newer, then we should remake from it. */
852 check_renamed (file);
853 mtime = file_mtime (file);
854 check_renamed (file);
855 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
856 *must_make_ptr = 1;
857 /* Otherwise, update all non-intermediate files we depend on,
858 if necessary, and see whether any of them is more
859 recent than the file on whose behalf we are checking. */
860 else
862 register struct dep *lastd;
864 lastd = 0;
865 d = file->deps;
866 while (d != 0)
868 if (is_updating (d->file))
870 error (NILF, _("Circular %s <- %s dependency dropped."),
871 file->name, d->file->name);
872 if (lastd == 0)
874 file->deps = d->next;
875 free ((char *) d);
876 d = file->deps;
878 else
880 lastd->next = d->next;
881 free ((char *) d);
882 d = lastd->next;
884 continue;
887 d->file->parent = file;
888 dep_status |= check_dep (d->file, depth, this_mtime,
889 must_make_ptr);
890 check_renamed (d->file);
891 if (dep_status != 0 && !keep_going_flag)
892 break;
894 if (d->file->command_state == cs_running
895 || d->file->command_state == cs_deps_running)
896 /* Record that some of FILE's deps are still being made.
897 This tells the upper levels to wait on processing it until
898 the commands are finished. */
899 set_command_state (file, cs_deps_running);
901 lastd = d;
902 d = d->next;
907 finish_updating (file);
908 return dep_status;
911 /* Touch FILE. Return zero if successful, one if not. */
913 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
915 static int
916 touch_file (file)
917 register struct file *file;
919 if (!silent_flag)
920 message (0, "touch %s", file->name);
922 #ifndef NO_ARCHIVES
923 if (ar_name (file->name))
924 return ar_touch (file->name);
925 else
926 #endif
928 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
930 if (fd < 0)
931 TOUCH_ERROR ("touch: open: ");
932 else
934 struct stat statbuf;
935 char buf;
937 if (fstat (fd, &statbuf) < 0)
938 TOUCH_ERROR ("touch: fstat: ");
939 /* Rewrite character 0 same as it already is. */
940 if (read (fd, &buf, 1) < 0)
941 TOUCH_ERROR ("touch: read: ");
942 if (lseek (fd, 0L, 0) < 0L)
943 TOUCH_ERROR ("touch: lseek: ");
944 if (write (fd, &buf, 1) < 0)
945 TOUCH_ERROR ("touch: write: ");
946 /* If file length was 0, we just
947 changed it, so change it back. */
948 if (statbuf.st_size == 0)
950 (void) close (fd);
951 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
952 if (fd < 0)
953 TOUCH_ERROR ("touch: open: ");
955 (void) close (fd);
959 return 0;
962 /* Having checked and updated the dependencies of FILE,
963 do whatever is appropriate to remake FILE itself.
964 Return the status from executing FILE's commands. */
966 static void
967 remake_file (file)
968 struct file *file;
970 if (file->cmds == 0)
972 if (file->phony)
973 /* Phony target. Pretend it succeeded. */
974 file->update_status = 0;
975 else if (file->is_target)
976 /* This is a nonexistent target file we cannot make.
977 Pretend it was successfully remade. */
978 file->update_status = 0;
979 else
981 const char *msg_noparent
982 = _("%sNo rule to make target `%s'%s");
983 const char *msg_parent
984 = _("%sNo rule to make target `%s', needed by `%s'%s");
986 /* This is a dependency file we cannot remake. Fail. */
987 if (!keep_going_flag && !file->dontcare)
989 if (file->parent == 0)
990 fatal (NILF, msg_noparent, "", file->name, "");
992 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
995 if (!file->dontcare)
997 if (file->parent == 0)
998 error (NILF, msg_noparent, "*** ", file->name, ".");
999 else
1000 error (NILF, msg_parent, "*** ",
1001 file->name, file->parent->name, ".");
1003 file->update_status = 2;
1006 else
1008 chop_commands (file->cmds);
1010 /* The normal case: start some commands. */
1011 if (!touch_flag || file->cmds->any_recurse)
1013 execute_file_commands (file);
1014 return;
1017 /* This tells notice_finished_file it is ok to touch the file. */
1018 file->update_status = 0;
1021 /* This does the touching under -t. */
1022 notice_finished_file (file);
1025 /* Return the mtime of a file, given a `struct file'.
1026 Caches the time in the struct file to avoid excess stat calls.
1028 If the file is not found, and SEARCH is nonzero, VPATH searching and
1029 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1030 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1031 FILE. */
1033 FILE_TIMESTAMP
1034 f_mtime (file, search)
1035 register struct file *file;
1036 int search;
1038 FILE_TIMESTAMP mtime;
1040 /* File's mtime is not known; must get it from the system. */
1042 #ifndef NO_ARCHIVES
1043 if (ar_name (file->name))
1045 /* This file is an archive-member reference. */
1047 char *arname, *memname;
1048 struct file *arfile;
1049 int arname_used = 0;
1050 time_t member_date;
1052 /* Find the archive's name. */
1053 ar_parse_name (file->name, &arname, &memname);
1055 /* Find the modification time of the archive itself.
1056 Also allow for its name to be changed via VPATH search. */
1057 arfile = lookup_file (arname);
1058 if (arfile == 0)
1060 arfile = enter_file (arname);
1061 arname_used = 1;
1063 mtime = f_mtime (arfile, search);
1064 check_renamed (arfile);
1065 if (search && strcmp (arfile->hname, arname))
1067 /* The archive's name has changed.
1068 Change the archive-member reference accordingly. */
1070 char *name;
1071 unsigned int arlen, memlen;
1073 if (!arname_used)
1075 free (arname);
1076 arname_used = 1;
1079 arname = arfile->hname;
1080 arlen = strlen (arname);
1081 memlen = strlen (memname);
1083 /* free (file->name); */
1085 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1086 bcopy (arname, name, arlen);
1087 name[arlen] = '(';
1088 bcopy (memname, name + arlen + 1, memlen);
1089 name[arlen + 1 + memlen] = ')';
1090 name[arlen + 1 + memlen + 1] = '\0';
1092 /* If the archive was found with GPATH, make the change permanent;
1093 otherwise defer it until later. */
1094 if (arfile->name == arfile->hname)
1095 rename_file (file, name);
1096 else
1097 rehash_file (file, name);
1098 check_renamed (file);
1101 if (!arname_used)
1102 free (arname);
1103 free (memname);
1105 file->low_resolution_time = 1;
1107 if (mtime == NONEXISTENT_MTIME)
1108 /* The archive doesn't exist, so its members don't exist either. */
1109 return NONEXISTENT_MTIME;
1111 member_date = ar_member_date (file->hname);
1112 mtime = (member_date == (time_t) -1
1113 ? NONEXISTENT_MTIME
1114 : file_timestamp_cons (file->hname, member_date, 0));
1116 else
1117 #endif
1119 mtime = name_mtime (file->name);
1121 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1123 /* If name_mtime failed, search VPATH. */
1124 char *name = file->name;
1125 if (vpath_search (&name, &mtime)
1126 /* Last resort, is it a library (-lxxx)? */
1127 || (name[0] == '-' && name[1] == 'l'
1128 && library_search (&name, &mtime)))
1130 if (mtime != UNKNOWN_MTIME)
1131 /* vpath_search and library_search store UNKNOWN_MTIME
1132 if they didn't need to do a stat call for their work. */
1133 file->last_mtime = mtime;
1135 /* If we found it in VPATH, see if it's in GPATH too; if so,
1136 change the name right now; if not, defer until after the
1137 dependencies are updated. */
1138 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1140 rename_file (file, name);
1141 check_renamed (file);
1142 return file_mtime (file);
1145 rehash_file (file, name);
1146 check_renamed (file);
1147 mtime = name_mtime (name);
1153 /* Files can have bogus timestamps that nothing newly made will be
1154 "newer" than. Updating their dependents could just result in loops.
1155 So notify the user of the anomaly with a warning.
1157 We only need to do this once, for now. */
1159 if (!clock_skew_detected
1160 && mtime != NONEXISTENT_MTIME
1161 && !file->updated)
1163 static FILE_TIMESTAMP adjusted_now;
1165 FILE_TIMESTAMP adjusted_mtime = mtime;
1167 #if defined(WINDOWS32) || defined(__MSDOS__)
1168 /* Experimentation has shown that FAT filesystems can set file times
1169 up to 3 seconds into the future! Play it safe. */
1171 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1173 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1174 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1175 adjusted_mtime -= adjustment;
1176 #endif
1178 /* If the file's time appears to be in the future, update our
1179 concept of the present and try once more. */
1180 if (adjusted_now < adjusted_mtime)
1182 int resolution;
1183 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1184 adjusted_now = now + (resolution - 1);
1185 if (adjusted_now < adjusted_mtime)
1187 #ifdef NO_FLOAT
1188 error (NILF, _("Warning: File `%s' has modification time in the future"),
1189 file->name);
1190 #else
1191 double from_now =
1192 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1193 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1194 / 1e9));
1195 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1196 file->name, from_now);
1197 #endif
1198 clock_skew_detected = 1;
1204 /* Store the mtime into all the entries for this file. */
1205 if (file->double_colon)
1206 file = file->double_colon;
1210 /* If this file is not implicit but it is intermediate then it was
1211 made so by the .INTERMEDIATE target. If this file has never
1212 been built by us but was found now, it existed before make
1213 started. So, turn off the intermediate bit so make doesn't
1214 delete it, since it didn't create it. */
1215 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1216 && file->command_state == cs_not_started
1217 && !file->tried_implicit && file->intermediate)
1218 file->intermediate = 0;
1220 file->last_mtime = mtime;
1221 file = file->prev;
1223 while (file != 0);
1225 return mtime;
1229 /* Return the mtime of the file or archive-member reference NAME. */
1231 static FILE_TIMESTAMP
1232 name_mtime (name)
1233 register char *name;
1235 struct stat st;
1237 if (stat (name, &st) != 0)
1239 if (errno != ENOENT && errno != ENOTDIR)
1240 perror_with_name ("stat:", name);
1241 return NONEXISTENT_MTIME;
1244 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1248 /* Search for a library file specified as -lLIBNAME, searching for a
1249 suitable library file in the system library directories and the VPATH
1250 directories. */
1252 static int
1253 library_search (lib, mtime_ptr)
1254 char **lib;
1255 FILE_TIMESTAMP *mtime_ptr;
1257 static char *dirs[] =
1259 #ifndef _AMIGA
1260 "/lib",
1261 "/usr/lib",
1262 #endif
1263 #if defined(WINDOWS32) && !defined(LIBDIR)
1265 * This is completely up to the user at product install time. Just define
1266 * a placeholder.
1268 #define LIBDIR "."
1269 #endif
1270 LIBDIR, /* Defined by configuration. */
1274 static char *libpatterns = NULL;
1276 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1277 FILE_TIMESTAMP mtime;
1279 /* Loop variables for the libpatterns value. */
1280 char *p, *p2;
1281 unsigned int len;
1283 char *file, **dp;
1285 /* If we don't have libpatterns, get it. */
1286 if (!libpatterns)
1288 int save = warn_undefined_variables_flag;
1289 warn_undefined_variables_flag = 0;
1291 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1293 warn_undefined_variables_flag = save;
1296 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1297 p2 = libpatterns;
1298 while ((p = find_next_token (&p2, &len)) != 0)
1300 static char *buf = NULL;
1301 static int buflen = 0;
1302 static int libdir_maxlen = -1;
1303 char *libbuf = variable_expand ("");
1305 /* Expand the pattern using LIBNAME as a replacement. */
1307 char c = p[len];
1308 char *p3, *p4;
1310 p[len] = '\0';
1311 p3 = find_percent (p);
1312 if (!p3)
1314 /* Give a warning if there is no pattern, then remove the
1315 pattern so it's ignored next time. */
1316 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1317 for (; len; --len, ++p)
1318 *p = ' ';
1319 *p = c;
1320 continue;
1322 p4 = variable_buffer_output (libbuf, p, p3-p);
1323 p4 = variable_buffer_output (p4, libname, strlen (libname));
1324 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1325 p[len] = c;
1328 /* Look first for `libNAME.a' in the current directory. */
1329 mtime = name_mtime (libbuf);
1330 if (mtime != NONEXISTENT_MTIME)
1332 *lib = xstrdup (libbuf);
1333 if (mtime_ptr != 0)
1334 *mtime_ptr = mtime;
1335 return 1;
1338 /* Now try VPATH search on that. */
1340 file = libbuf;
1341 if (vpath_search (&file, mtime_ptr))
1343 *lib = file;
1344 return 1;
1347 /* Now try the standard set of directories. */
1349 if (!buflen)
1351 for (dp = dirs; *dp != 0; ++dp)
1353 int l = strlen (*dp);
1354 if (l > libdir_maxlen)
1355 libdir_maxlen = l;
1357 buflen = strlen (libbuf);
1358 buf = xmalloc(libdir_maxlen + buflen + 2);
1360 else if (buflen < strlen (libbuf))
1362 buflen = strlen (libbuf);
1363 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1366 for (dp = dirs; *dp != 0; ++dp)
1368 sprintf (buf, "%s/%s", *dp, libbuf);
1369 mtime = name_mtime (buf);
1370 if (mtime != NONEXISTENT_MTIME)
1372 *lib = xstrdup (buf);
1373 if (mtime_ptr != 0)
1374 *mtime_ptr = mtime;
1375 return 1;
1380 return 0;