Install Greg McGary's patches to port the id-utils hashing functions to
[make/kirr.git] / remake.c
blob66c6d311d140cf119b4c427fead9b0e432578e1b
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "dep.h"
26 #include "variable.h"
27 #include "debug.h"
29 #include <assert.h>
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #else
34 #include <sys/file.h>
35 #endif
37 #ifdef VMS
38 #include <starlet.h>
39 #endif
40 #ifdef WINDOWS32
41 #include <io.h>
42 #endif
44 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
47 /* The test for circular dependencies is based on the 'updating' bit in
48 `struct file'. However, double colon targets have seperate `struct
49 file's; make sure we always use the base of the double colon chain. */
51 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 1)
53 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating = 0)
55 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
56 ->updating)
59 /* Incremented when a command is started (under -n, when one would be). */
60 unsigned int commands_started = 0;
62 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
63 static unsigned int considered;
65 static int update_file PARAMS ((struct file *file, unsigned int depth));
66 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
67 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
68 static int touch_file PARAMS ((struct file *file));
69 static void remake_file PARAMS ((struct file *file));
70 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
71 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
74 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
75 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
76 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
77 be disabled for them unless they were also command-line targets, and we
78 should only make one goal at a time and return as soon as one goal whose
79 `changed' member is nonzero is successfully made. */
81 int
82 update_goal_chain (goals, makefiles)
83 register struct dep *goals;
84 int makefiles;
86 int t = touch_flag, q = question_flag, n = just_print_flag;
87 unsigned int j = job_slots;
88 int status = -1;
90 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
91 : file_mtime (file))
93 /* Duplicate the chain so we can remove things from it. */
95 goals = copy_dep_chain (goals);
98 /* Clear the `changed' flag of each goal in the chain.
99 We will use the flag below to notice when any commands
100 have actually been run for a target. When no commands
101 have been run, we give an "up to date" diagnostic. */
103 struct dep *g;
104 for (g = goals; g != 0; g = g->next)
105 g->changed = 0;
108 /* All files start with the considered bit 0, so the global value is 1. */
109 considered = 1;
111 /* Update all the goals until they are all finished. */
113 while (goals != 0)
115 register struct dep *g, *lastgoal;
117 /* Start jobs that are waiting for the load to go down. */
119 start_waiting_jobs ();
121 /* Wait for a child to die. */
123 reap_children (1, 0);
125 lastgoal = 0;
126 g = goals;
127 while (g != 0)
129 /* Iterate over all double-colon entries for this file. */
130 struct file *file;
131 int stop = 0, any_not_updated = 0;
133 for (file = g->file->double_colon ? g->file->double_colon : g->file;
134 file != NULL;
135 file = file->prev)
137 unsigned int ocommands_started;
138 int x;
139 check_renamed (file);
140 if (makefiles)
142 if (file->cmd_target)
144 touch_flag = t;
145 question_flag = q;
146 just_print_flag = n;
148 else
149 touch_flag = question_flag = just_print_flag = 0;
152 /* Save the old value of `commands_started' so we can compare
153 later. It will be incremented when any commands are
154 actually run. */
155 ocommands_started = commands_started;
157 x = update_file (file, makefiles ? 1 : 0);
158 check_renamed (file);
160 /* Set the goal's `changed' flag if any commands were started
161 by calling update_file above. We check this flag below to
162 decide when to give an "up to date" diagnostic. */
163 g->changed += commands_started - ocommands_started;
165 /* If we updated a file and STATUS was not already 1, set it to
166 1 if updating failed, or to 0 if updating succeeded. Leave
167 STATUS as it is if no updating was done. */
169 stop = 0;
170 if ((x != 0 || file->updated) && status < 1)
172 if (file->update_status != 0)
174 /* Updating failed, or -q triggered. The STATUS value
175 tells our caller which. */
176 status = file->update_status;
177 /* If -q just triggered, stop immediately. It doesn't
178 matter how much more we run, since we already know
179 the answer to return. */
180 stop = (!keep_going_flag && !question_flag
181 && !makefiles);
183 else
185 FILE_TIMESTAMP mtime = MTIME (file);
186 check_renamed (file);
188 if (file->updated && g->changed &&
189 mtime != file->mtime_before_update)
191 /* Updating was done. If this is a makefile and
192 just_print_flag or question_flag is set (meaning
193 -n or -q was given and this file was specified
194 as a command-line target), don't change STATUS.
195 If STATUS is changed, we will get re-exec'd, and
196 enter an infinite loop. */
197 if (!makefiles
198 || (!just_print_flag && !question_flag))
199 status = 0;
200 if (makefiles && file->dontcare)
201 /* This is a default makefile; stop remaking. */
202 stop = 1;
207 /* Keep track if any double-colon entry is not finished.
208 When they are all finished, the goal is finished. */
209 any_not_updated |= !file->updated;
211 if (stop)
212 break;
215 /* Reset FILE since it is null at the end of the loop. */
216 file = g->file;
218 if (stop || !any_not_updated)
220 /* If we have found nothing whatever to do for the goal,
221 print a message saying nothing needs doing. */
223 if (!makefiles
224 /* If the update_status is zero, we updated successfully
225 or not at all. G->changed will have been set above if
226 any commands were actually started for this goal. */
227 && file->update_status == 0 && !g->changed
228 /* Never give a message under -s or -q. */
229 && !silent_flag && !question_flag)
230 message (1, ((file->phony || file->cmds == 0)
231 ? _("Nothing to be done for `%s'.")
232 : _("`%s' is up to date.")),
233 file->name);
235 /* This goal is finished. Remove it from the chain. */
236 if (lastgoal == 0)
237 goals = g->next;
238 else
239 lastgoal->next = g->next;
241 /* Free the storage. */
242 free ((char *) g);
244 g = lastgoal == 0 ? goals : lastgoal->next;
246 if (stop)
247 break;
249 else
251 lastgoal = g;
252 g = g->next;
256 /* If we reached the end of the dependency graph toggle the considered
257 flag for the next pass. */
258 if (g == 0)
259 considered = !considered;
262 if (makefiles)
264 touch_flag = t;
265 question_flag = q;
266 just_print_flag = n;
267 job_slots = j;
269 return status;
272 /* If FILE is not up to date, execute the commands for it.
273 Return 0 if successful, 1 if unsuccessful;
274 but with some flag settings, just call `exit' if unsuccessful.
276 DEPTH is the depth in recursions of this function.
277 We increment it during the consideration of our dependencies,
278 then decrement it again after finding out whether this file
279 is out of date.
281 If there are multiple double-colon entries for FILE,
282 each is considered in turn. */
284 static int
285 update_file (file, depth)
286 struct file *file;
287 unsigned int depth;
289 register int status = 0;
290 register struct file *f;
292 f = file->double_colon ? file->double_colon : file;
294 /* Prune the dependency graph: if we've already been here on _this_
295 pass through the dependency graph, we don't have to go any further.
296 We won't reap_children until we start the next pass, so no state
297 change is possible below here until then. */
298 if (f->considered == considered)
300 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
301 return f->command_state == cs_finished ? f->update_status : 0;
304 /* This loop runs until we start commands for a double colon rule, or until
305 the chain is exhausted. */
306 for (; f != 0; f = f->prev)
308 f->considered = considered;
310 status |= update_file_1 (f, depth);
311 check_renamed (f);
313 if (status != 0 && !keep_going_flag)
314 break;
316 if (f->command_state == cs_running
317 || f->command_state == cs_deps_running)
319 /* Don't run the other :: rules for this
320 file until this rule is finished. */
321 status = 0;
322 break;
326 /* Process the remaining rules in the double colon chain so they're marked
327 considered. Start their prerequisites, too. */
328 for (; f != 0 ; f = f->prev)
330 struct dep *d;
332 f->considered = considered;
334 for (d = f->deps; d != 0; d = d->next)
335 status |= update_file (d->file, depth + 1);
338 return status;
341 /* Consider a single `struct file' and update it as appropriate. */
343 static int
344 update_file_1 (file, depth)
345 struct file *file;
346 unsigned int depth;
348 register FILE_TIMESTAMP this_mtime;
349 int noexist, must_make, deps_changed;
350 int dep_status = 0;
351 register struct dep *d, *lastd;
352 int running = 0;
353 int maybe_make;
355 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
357 if (file->updated)
359 if (file->update_status > 0)
361 DBF (DB_VERBOSE,
362 _("Recently tried and failed to update file `%s'.\n"));
363 return file->update_status;
366 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
367 return 0;
370 switch (file->command_state)
372 case cs_not_started:
373 case cs_deps_running:
374 break;
375 case cs_running:
376 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
377 return 0;
378 case cs_finished:
379 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
380 return file->update_status;
381 default:
382 abort ();
385 ++depth;
387 /* Notice recursive update of the same file. */
388 start_updating (file);
390 /* Looking at the file's modtime beforehand allows the possibility
391 that its name may be changed by a VPATH search, and thus it may
392 not need an implicit rule. If this were not done, the file
393 might get implicit commands that apply to its initial name, only
394 to have that name replaced with another found by VPATH search. */
396 this_mtime = file_mtime (file);
397 check_renamed (file);
398 noexist = this_mtime == NONEXISTENT_MTIME;
399 if (noexist)
400 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
401 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
402 && file->low_resolution_time)
404 /* Avoid spurious rebuilds due to low resolution time stamps. */
405 int ns = FILE_TIMESTAMP_NS (this_mtime);
406 if (ns != 0)
407 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
408 file->name);
409 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
412 must_make = noexist;
414 /* If file was specified as a target with no commands,
415 come up with some default commands. */
417 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
419 if (try_implicit_rule (file, depth))
420 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
421 else
422 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
423 file->tried_implicit = 1;
425 if (file->cmds == 0 && !file->is_target
426 && default_file != 0 && default_file->cmds != 0)
428 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
429 file->cmds = default_file->cmds;
432 /* Update all non-intermediate files we depend on, if necessary,
433 and see whether any of them is more recent than this file. */
435 lastd = 0;
436 d = file->deps;
437 while (d != 0)
439 FILE_TIMESTAMP mtime;
441 check_renamed (d->file);
443 mtime = file_mtime (d->file);
444 check_renamed (d->file);
446 if (is_updating (d->file))
448 error (NILF, _("Circular %s <- %s dependency dropped."),
449 file->name, d->file->name);
450 /* We cannot free D here because our the caller will still have
451 a reference to it when we were called recursively via
452 check_dep below. */
453 if (lastd == 0)
454 file->deps = d->next;
455 else
456 lastd->next = d->next;
457 d = d->next;
458 continue;
461 d->file->parent = file;
462 maybe_make = must_make;
463 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
464 if (! d->ignore_mtime)
465 must_make = maybe_make;
467 check_renamed (d->file);
470 register struct file *f = d->file;
471 if (f->double_colon)
472 f = f->double_colon;
475 running |= (f->command_state == cs_running
476 || f->command_state == cs_deps_running);
477 f = f->prev;
479 while (f != 0);
482 if (dep_status != 0 && !keep_going_flag)
483 break;
485 if (!running)
486 d->changed = file_mtime (d->file) != mtime;
488 lastd = d;
489 d = d->next;
492 /* Now we know whether this target needs updating.
493 If it does, update all the intermediate files we depend on. */
495 if (must_make)
497 for (d = file->deps; d != 0; d = d->next)
498 if (d->file->intermediate)
500 FILE_TIMESTAMP mtime = file_mtime (d->file);
501 check_renamed (d->file);
502 d->file->parent = file;
503 dep_status |= update_file (d->file, depth);
504 check_renamed (d->file);
507 register struct file *f = d->file;
508 if (f->double_colon)
509 f = f->double_colon;
512 running |= (f->command_state == cs_running
513 || f->command_state == cs_deps_running);
514 f = f->prev;
516 while (f != 0);
519 if (dep_status != 0 && !keep_going_flag)
520 break;
522 if (!running)
523 d->changed = ((file->phony && file->cmds != 0)
524 || file_mtime (d->file) != mtime);
528 finish_updating (file);
530 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
532 if (running)
534 set_command_state (file, cs_deps_running);
535 --depth;
536 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
537 return 0;
540 /* If any dependency failed, give up now. */
542 if (dep_status != 0)
544 file->update_status = dep_status;
545 notice_finished_file (file);
547 depth--;
549 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
551 if (depth == 0 && keep_going_flag
552 && !just_print_flag && !question_flag)
553 error (NILF,
554 _("Target `%s' not remade because of errors."), file->name);
556 return dep_status;
559 if (file->command_state == cs_deps_running)
560 /* The commands for some deps were running on the last iteration, but
561 they have finished now. Reset the command_state to not_started to
562 simplify later bookkeeping. It is important that we do this only
563 when the prior state was cs_deps_running, because that prior state
564 was definitely propagated to FILE's also_make's by set_command_state
565 (called above), but in another state an also_make may have
566 independently changed to finished state, and we would confuse that
567 file's bookkeeping (updated, but not_started is bogus state). */
568 set_command_state (file, cs_not_started);
570 /* Now record which dependencies are more
571 recent than this file, so we can define $?. */
573 deps_changed = 0;
574 for (d = file->deps; d != 0; d = d->next)
576 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
577 check_renamed (d->file);
579 if (! d->ignore_mtime)
581 #if 1
582 /* %%% In version 4, remove this code completely to
583 implement not remaking deps if their deps are newer
584 than their parents. */
585 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
586 /* We must remake if this dep does not
587 exist and is not intermediate. */
588 must_make = 1;
589 #endif
591 /* Set DEPS_CHANGED if this dep actually changed. */
592 deps_changed |= d->changed;
595 /* Set D->changed if either this dep actually changed,
596 or its dependent, FILE, is older or does not exist. */
597 d->changed |= noexist || d_mtime > this_mtime;
599 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
601 const char *fmt = 0;
603 if (d->ignore_mtime)
605 if (ISDB (DB_VERBOSE))
606 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
608 else if (d_mtime == NONEXISTENT_MTIME)
610 if (ISDB (DB_BASIC))
611 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
613 else if (d->changed)
615 if (ISDB (DB_BASIC))
616 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
618 else if (ISDB (DB_VERBOSE))
619 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
621 if (fmt)
623 print_spaces (depth);
624 printf (fmt, dep_name (d), file->name);
625 fflush (stdout);
630 /* Here depth returns to the value it had when we were called. */
631 depth--;
633 if (file->double_colon && file->deps == 0)
635 must_make = 1;
636 DBF (DB_BASIC,
637 _("Target `%s' is double-colon and has no prerequisites.\n"));
639 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
641 must_make = 0;
642 DBF (DB_VERBOSE,
643 _("No commands for `%s' and no prerequisites actually changed.\n"));
646 if (!must_make)
648 if (ISDB (DB_VERBOSE))
650 print_spaces (depth);
651 printf (_("No need to remake target `%s'"), file->name);
652 if (!streq (file->name, file->hname))
653 printf (_("; using VPATH name `%s'"), file->hname);
654 puts (".");
655 fflush (stdout);
658 notice_finished_file (file);
660 /* Since we don't need to remake the file, convert it to use the
661 VPATH filename if we found one. hfile will be either the
662 local name if no VPATH or the VPATH name if one was found. */
664 while (file)
666 file->name = file->hname;
667 file = file->prev;
670 return 0;
673 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
675 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
676 VPATH. */
677 if (!streq(file->name, file->hname))
679 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
680 file->ignore_vpath = 1;
683 /* Now, take appropriate actions to remake the file. */
684 remake_file (file);
686 if (file->command_state != cs_finished)
688 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
689 return 0;
692 switch (file->update_status)
694 case 2:
695 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
696 break;
697 case 0:
698 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
699 break;
700 case 1:
701 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
702 break;
703 default:
704 assert (file->update_status >= 0 && file->update_status <= 2);
705 break;
708 file->updated = 1;
709 return file->update_status;
712 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
713 files listed in its `also_make' member. Under -t, this function also
714 touches FILE.
716 On return, FILE->update_status will no longer be -1 if it was. */
718 void
719 notice_finished_file (file)
720 register struct file *file;
722 struct dep *d;
723 int ran = file->command_state == cs_running;
724 int touched = 0;
726 file->command_state = cs_finished;
727 file->updated = 1;
729 if (touch_flag
730 /* The update status will be:
731 -1 if this target was not remade;
732 0 if 0 or more commands (+ or ${MAKE}) were run and won;
733 1 if some commands were run and lost.
734 We touch the target if it has commands which either were not run
735 or won when they ran (i.e. status is 0). */
736 && file->update_status == 0)
738 if (file->cmds != 0 && file->cmds->any_recurse)
740 /* If all the command lines were recursive,
741 we don't want to do the touching. */
742 unsigned int i;
743 for (i = 0; i < file->cmds->ncommand_lines; ++i)
744 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
745 goto have_nonrecursing;
747 else
749 have_nonrecursing:
750 if (file->phony)
751 file->update_status = 0;
752 else
754 /* Should set file's modification date and do nothing else. */
755 file->update_status = touch_file (file);
757 /* Pretend we ran a real touch command, to suppress the
758 "`foo' is up to date" message. */
759 commands_started++;
761 /* Request for the timestamp to be updated (and distributed
762 to the double-colon entries). Simply setting ran=1 would
763 almost have done the trick, but messes up with the also_make
764 updating logic below. */
765 touched = 1;
770 if (file->mtime_before_update == UNKNOWN_MTIME)
771 file->mtime_before_update = file->last_mtime;
773 if ((ran && !file->phony) || touched)
775 struct file *f;
776 int i = 0;
778 /* If -n, -t, or -q and all the commands are recursive, we ran them so
779 really check the target's mtime again. Otherwise, assume the target
780 would have been updated. */
782 if (question_flag || just_print_flag || touch_flag)
784 for (i = file->cmds->ncommand_lines; i > 0; --i)
785 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
786 break;
789 /* If there were no commands at all, it's always new. */
791 else if (file->is_target && file->cmds == 0)
792 i = 1;
794 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
796 /* Propagate the change of modification time to all the double-colon
797 entries for this file. */
798 for (f = file->double_colon; f != 0; f = f->prev)
799 f->last_mtime = file->last_mtime;
802 if (ran && file->update_status != -1)
803 /* We actually tried to update FILE, which has
804 updated its also_make's as well (if it worked).
805 If it didn't work, it wouldn't work again for them.
806 So mark them as updated with the same status. */
807 for (d = file->also_make; d != 0; d = d->next)
809 d->file->command_state = cs_finished;
810 d->file->updated = 1;
811 d->file->update_status = file->update_status;
813 if (ran && !d->file->phony)
814 /* Fetch the new modification time.
815 We do this instead of just invalidating the cached time
816 so that a vpath_search can happen. Otherwise, it would
817 never be done because the target is already updated. */
818 (void) f_mtime (d->file, 0);
820 else if (file->update_status == -1)
821 /* Nothing was done for FILE, but it needed nothing done.
822 So mark it now as "succeeded". */
823 file->update_status = 0;
826 /* Check whether another file (whose mtime is THIS_MTIME)
827 needs updating on account of a dependency which is file FILE.
828 If it does, store 1 in *MUST_MAKE_PTR.
829 In the process, update any non-intermediate files
830 that FILE depends on (including FILE itself).
831 Return nonzero if any updating failed. */
833 static int
834 check_dep (file, depth, this_mtime, must_make_ptr)
835 struct file *file;
836 unsigned int depth;
837 FILE_TIMESTAMP this_mtime;
838 int *must_make_ptr;
840 struct dep *d;
841 int dep_status = 0;
842 int maybe_make;
844 ++depth;
845 start_updating (file);
847 if (!file->intermediate)
848 /* If this is a non-intermediate file, update it and record
849 whether it is newer than THIS_MTIME. */
851 FILE_TIMESTAMP mtime;
852 dep_status = update_file (file, depth);
853 check_renamed (file);
854 mtime = file_mtime (file);
855 check_renamed (file);
856 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
857 *must_make_ptr = 1;
859 else
861 /* FILE is an intermediate file. */
862 FILE_TIMESTAMP mtime;
864 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
866 if (try_implicit_rule (file, depth))
867 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
868 else
869 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
870 file->tried_implicit = 1;
872 if (file->cmds == 0 && !file->is_target
873 && default_file != 0 && default_file->cmds != 0)
875 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
876 file->cmds = default_file->cmds;
879 /* If the intermediate file actually exists
880 and is newer, then we should remake from it. */
881 check_renamed (file);
882 mtime = file_mtime (file);
883 check_renamed (file);
884 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
885 *must_make_ptr = 1;
886 /* Otherwise, update all non-intermediate files we depend on,
887 if necessary, and see whether any of them is more
888 recent than the file on whose behalf we are checking. */
889 else
891 register struct dep *lastd;
893 lastd = 0;
894 d = file->deps;
895 while (d != 0)
897 if (is_updating (d->file))
899 error (NILF, _("Circular %s <- %s dependency dropped."),
900 file->name, d->file->name);
901 if (lastd == 0)
903 file->deps = d->next;
904 free ((char *) d);
905 d = file->deps;
907 else
909 lastd->next = d->next;
910 free ((char *) d);
911 d = lastd->next;
913 continue;
916 d->file->parent = file;
917 maybe_make = *must_make_ptr;
918 dep_status |= check_dep (d->file, depth, this_mtime,
919 &maybe_make);
920 if (! d->ignore_mtime)
921 *must_make_ptr = maybe_make;
922 check_renamed (d->file);
923 if (dep_status != 0 && !keep_going_flag)
924 break;
926 if (d->file->command_state == cs_running
927 || d->file->command_state == cs_deps_running)
928 /* Record that some of FILE's deps are still being made.
929 This tells the upper levels to wait on processing it until
930 the commands are finished. */
931 set_command_state (file, cs_deps_running);
933 lastd = d;
934 d = d->next;
939 finish_updating (file);
940 return dep_status;
943 /* Touch FILE. Return zero if successful, one if not. */
945 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
947 static int
948 touch_file (file)
949 register struct file *file;
951 if (!silent_flag)
952 message (0, "touch %s", file->name);
954 #ifndef NO_ARCHIVES
955 if (ar_name (file->name))
956 return ar_touch (file->name);
957 else
958 #endif
960 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
962 if (fd < 0)
963 TOUCH_ERROR ("touch: open: ");
964 else
966 struct stat statbuf;
967 char buf;
969 if (fstat (fd, &statbuf) < 0)
970 TOUCH_ERROR ("touch: fstat: ");
971 /* Rewrite character 0 same as it already is. */
972 if (read (fd, &buf, 1) < 0)
973 TOUCH_ERROR ("touch: read: ");
974 if (lseek (fd, 0L, 0) < 0L)
975 TOUCH_ERROR ("touch: lseek: ");
976 if (write (fd, &buf, 1) < 0)
977 TOUCH_ERROR ("touch: write: ");
978 /* If file length was 0, we just
979 changed it, so change it back. */
980 if (statbuf.st_size == 0)
982 (void) close (fd);
983 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
984 if (fd < 0)
985 TOUCH_ERROR ("touch: open: ");
987 (void) close (fd);
991 return 0;
994 /* Having checked and updated the dependencies of FILE,
995 do whatever is appropriate to remake FILE itself.
996 Return the status from executing FILE's commands. */
998 static void
999 remake_file (file)
1000 struct file *file;
1002 if (file->cmds == 0)
1004 if (file->phony)
1005 /* Phony target. Pretend it succeeded. */
1006 file->update_status = 0;
1007 else if (file->is_target)
1008 /* This is a nonexistent target file we cannot make.
1009 Pretend it was successfully remade. */
1010 file->update_status = 0;
1011 else
1013 const char *msg_noparent
1014 = _("%sNo rule to make target `%s'%s");
1015 const char *msg_parent
1016 = _("%sNo rule to make target `%s', needed by `%s'%s");
1018 /* This is a dependency file we cannot remake. Fail. */
1019 if (!keep_going_flag && !file->dontcare)
1021 if (file->parent == 0)
1022 fatal (NILF, msg_noparent, "", file->name, "");
1024 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
1027 if (!file->dontcare)
1029 if (file->parent == 0)
1030 error (NILF, msg_noparent, "*** ", file->name, ".");
1031 else
1032 error (NILF, msg_parent, "*** ",
1033 file->name, file->parent->name, ".");
1035 file->update_status = 2;
1038 else
1040 chop_commands (file->cmds);
1042 /* The normal case: start some commands. */
1043 if (!touch_flag || file->cmds->any_recurse)
1045 execute_file_commands (file);
1046 return;
1049 /* This tells notice_finished_file it is ok to touch the file. */
1050 file->update_status = 0;
1053 /* This does the touching under -t. */
1054 notice_finished_file (file);
1057 /* Return the mtime of a file, given a `struct file'.
1058 Caches the time in the struct file to avoid excess stat calls.
1060 If the file is not found, and SEARCH is nonzero, VPATH searching and
1061 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1062 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1063 FILE. */
1065 FILE_TIMESTAMP
1066 f_mtime (file, search)
1067 register struct file *file;
1068 int search;
1070 FILE_TIMESTAMP mtime;
1072 /* File's mtime is not known; must get it from the system. */
1074 #ifndef NO_ARCHIVES
1075 if (ar_name (file->name))
1077 /* This file is an archive-member reference. */
1079 char *arname, *memname;
1080 struct file *arfile;
1081 int arname_used = 0;
1082 time_t member_date;
1084 /* Find the archive's name. */
1085 ar_parse_name (file->name, &arname, &memname);
1087 /* Find the modification time of the archive itself.
1088 Also allow for its name to be changed via VPATH search. */
1089 arfile = lookup_file (arname);
1090 if (arfile == 0)
1092 arfile = enter_file (arname);
1093 arname_used = 1;
1095 mtime = f_mtime (arfile, search);
1096 check_renamed (arfile);
1097 if (search && strcmp (arfile->hname, arname))
1099 /* The archive's name has changed.
1100 Change the archive-member reference accordingly. */
1102 char *name;
1103 unsigned int arlen, memlen;
1105 if (!arname_used)
1107 free (arname);
1108 arname_used = 1;
1111 arname = arfile->hname;
1112 arlen = strlen (arname);
1113 memlen = strlen (memname);
1115 /* free (file->name); */
1117 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1118 bcopy (arname, name, arlen);
1119 name[arlen] = '(';
1120 bcopy (memname, name + arlen + 1, memlen);
1121 name[arlen + 1 + memlen] = ')';
1122 name[arlen + 1 + memlen + 1] = '\0';
1124 /* If the archive was found with GPATH, make the change permanent;
1125 otherwise defer it until later. */
1126 if (arfile->name == arfile->hname)
1127 rename_file (file, name);
1128 else
1129 rehash_file (file, name);
1130 check_renamed (file);
1133 if (!arname_used)
1134 free (arname);
1135 free (memname);
1137 file->low_resolution_time = 1;
1139 if (mtime == NONEXISTENT_MTIME)
1140 /* The archive doesn't exist, so its members don't exist either. */
1141 return NONEXISTENT_MTIME;
1143 member_date = ar_member_date (file->hname);
1144 mtime = (member_date == (time_t) -1
1145 ? NONEXISTENT_MTIME
1146 : file_timestamp_cons (file->hname, member_date, 0));
1148 else
1149 #endif
1151 mtime = name_mtime (file->name);
1153 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1155 /* If name_mtime failed, search VPATH. */
1156 char *name = file->name;
1157 if (vpath_search (&name, &mtime)
1158 /* Last resort, is it a library (-lxxx)? */
1159 || (name[0] == '-' && name[1] == 'l'
1160 && library_search (&name, &mtime)))
1162 if (mtime != UNKNOWN_MTIME)
1163 /* vpath_search and library_search store UNKNOWN_MTIME
1164 if they didn't need to do a stat call for their work. */
1165 file->last_mtime = mtime;
1167 /* If we found it in VPATH, see if it's in GPATH too; if so,
1168 change the name right now; if not, defer until after the
1169 dependencies are updated. */
1170 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1172 rename_file (file, name);
1173 check_renamed (file);
1174 return file_mtime (file);
1177 rehash_file (file, name);
1178 check_renamed (file);
1179 mtime = name_mtime (name);
1185 /* Files can have bogus timestamps that nothing newly made will be
1186 "newer" than. Updating their dependents could just result in loops.
1187 So notify the user of the anomaly with a warning.
1189 We only need to do this once, for now. */
1191 if (!clock_skew_detected
1192 && mtime != NONEXISTENT_MTIME
1193 && !file->updated)
1195 static FILE_TIMESTAMP adjusted_now;
1197 FILE_TIMESTAMP adjusted_mtime = mtime;
1199 #if defined(WINDOWS32) || defined(__MSDOS__)
1200 /* Experimentation has shown that FAT filesystems can set file times
1201 up to 3 seconds into the future! Play it safe. */
1203 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1205 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1206 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1207 adjusted_mtime -= adjustment;
1208 #endif
1210 /* If the file's time appears to be in the future, update our
1211 concept of the present and try once more. */
1212 if (adjusted_now < adjusted_mtime)
1214 int resolution;
1215 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1216 adjusted_now = now + (resolution - 1);
1217 if (adjusted_now < adjusted_mtime)
1219 #ifdef NO_FLOAT
1220 error (NILF, _("Warning: File `%s' has modification time in the future"),
1221 file->name);
1222 #else
1223 double from_now =
1224 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1225 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1226 / 1e9));
1227 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1228 file->name, from_now);
1229 #endif
1230 clock_skew_detected = 1;
1236 /* Store the mtime into all the entries for this file. */
1237 if (file->double_colon)
1238 file = file->double_colon;
1242 /* If this file is not implicit but it is intermediate then it was
1243 made so by the .INTERMEDIATE target. If this file has never
1244 been built by us but was found now, it existed before make
1245 started. So, turn off the intermediate bit so make doesn't
1246 delete it, since it didn't create it. */
1247 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1248 && file->command_state == cs_not_started
1249 && !file->tried_implicit && file->intermediate)
1250 file->intermediate = 0;
1252 file->last_mtime = mtime;
1253 file = file->prev;
1255 while (file != 0);
1257 return mtime;
1261 /* Return the mtime of the file or archive-member reference NAME. */
1263 static FILE_TIMESTAMP
1264 name_mtime (name)
1265 register char *name;
1267 struct stat st;
1269 if (stat (name, &st) != 0)
1271 if (errno != ENOENT && errno != ENOTDIR)
1272 perror_with_name ("stat:", name);
1273 return NONEXISTENT_MTIME;
1276 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1280 /* Search for a library file specified as -lLIBNAME, searching for a
1281 suitable library file in the system library directories and the VPATH
1282 directories. */
1284 static int
1285 library_search (lib, mtime_ptr)
1286 char **lib;
1287 FILE_TIMESTAMP *mtime_ptr;
1289 static char *dirs[] =
1291 #ifndef _AMIGA
1292 "/lib",
1293 "/usr/lib",
1294 #endif
1295 #if defined(WINDOWS32) && !defined(LIBDIR)
1297 * This is completely up to the user at product install time. Just define
1298 * a placeholder.
1300 #define LIBDIR "."
1301 #endif
1302 LIBDIR, /* Defined by configuration. */
1306 static char *libpatterns = NULL;
1308 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1309 FILE_TIMESTAMP mtime;
1311 /* Loop variables for the libpatterns value. */
1312 char *p, *p2;
1313 unsigned int len;
1315 char *file, **dp;
1317 /* If we don't have libpatterns, get it. */
1318 if (!libpatterns)
1320 int save = warn_undefined_variables_flag;
1321 warn_undefined_variables_flag = 0;
1323 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1325 warn_undefined_variables_flag = save;
1328 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1329 p2 = libpatterns;
1330 while ((p = find_next_token (&p2, &len)) != 0)
1332 static char *buf = NULL;
1333 static int buflen = 0;
1334 static int libdir_maxlen = -1;
1335 char *libbuf = variable_expand ("");
1337 /* Expand the pattern using LIBNAME as a replacement. */
1339 char c = p[len];
1340 char *p3, *p4;
1342 p[len] = '\0';
1343 p3 = find_percent (p);
1344 if (!p3)
1346 /* Give a warning if there is no pattern, then remove the
1347 pattern so it's ignored next time. */
1348 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1349 for (; len; --len, ++p)
1350 *p = ' ';
1351 *p = c;
1352 continue;
1354 p4 = variable_buffer_output (libbuf, p, p3-p);
1355 p4 = variable_buffer_output (p4, libname, strlen (libname));
1356 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1357 p[len] = c;
1360 /* Look first for `libNAME.a' in the current directory. */
1361 mtime = name_mtime (libbuf);
1362 if (mtime != NONEXISTENT_MTIME)
1364 *lib = xstrdup (libbuf);
1365 if (mtime_ptr != 0)
1366 *mtime_ptr = mtime;
1367 return 1;
1370 /* Now try VPATH search on that. */
1372 file = libbuf;
1373 if (vpath_search (&file, mtime_ptr))
1375 *lib = file;
1376 return 1;
1379 /* Now try the standard set of directories. */
1381 if (!buflen)
1383 for (dp = dirs; *dp != 0; ++dp)
1385 int l = strlen (*dp);
1386 if (l > libdir_maxlen)
1387 libdir_maxlen = l;
1389 buflen = strlen (libbuf);
1390 buf = xmalloc(libdir_maxlen + buflen + 2);
1392 else if (buflen < strlen (libbuf))
1394 buflen = strlen (libbuf);
1395 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1398 for (dp = dirs; *dp != 0; ++dp)
1400 sprintf (buf, "%s/%s", *dp, libbuf);
1401 mtime = name_mtime (buf);
1402 if (mtime != NONEXISTENT_MTIME)
1404 *lib = xstrdup (buf);
1405 if (mtime_ptr != 0)
1406 *mtime_ptr = mtime;
1407 return 1;
1412 return 0;