Documentation and tests for order-only prerequisites.
[make/kirr.git] / remake.c
blobfcefdc771feaed0ef14eb2db1d6afe55d30bf579
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;
352 int maybe_make;
354 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
356 if (file->updated)
358 if (file->update_status > 0)
360 DBF (DB_VERBOSE,
361 _("Recently tried and failed to update file `%s'.\n"));
362 return file->update_status;
365 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
366 return 0;
369 switch (file->command_state)
371 case cs_not_started:
372 case cs_deps_running:
373 break;
374 case cs_running:
375 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
376 return 0;
377 case cs_finished:
378 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
379 return file->update_status;
380 default:
381 abort ();
384 ++depth;
386 /* Notice recursive update of the same file. */
387 start_updating (file);
389 /* Looking at the file's modtime beforehand allows the possibility
390 that its name may be changed by a VPATH search, and thus it may
391 not need an implicit rule. If this were not done, the file
392 might get implicit commands that apply to its initial name, only
393 to have that name replaced with another found by VPATH search. */
395 this_mtime = file_mtime (file);
396 check_renamed (file);
397 noexist = this_mtime == NONEXISTENT_MTIME;
398 if (noexist)
399 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
400 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
401 && file->low_resolution_time)
403 /* Avoid spurious rebuilds due to low resolution time stamps. */
404 int ns = FILE_TIMESTAMP_NS (this_mtime);
405 if (ns != 0)
406 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
407 file->name);
408 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
411 must_make = noexist;
413 /* If file was specified as a target with no commands,
414 come up with some default commands. */
416 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
418 if (try_implicit_rule (file, depth))
419 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
420 else
421 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
422 file->tried_implicit = 1;
424 if (file->cmds == 0 && !file->is_target
425 && default_file != 0 && default_file->cmds != 0)
427 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
428 file->cmds = default_file->cmds;
431 /* Update all non-intermediate files we depend on, if necessary,
432 and see whether any of them is more recent than this file. */
434 lastd = 0;
435 d = file->deps;
436 while (d != 0)
438 FILE_TIMESTAMP mtime;
440 check_renamed (d->file);
442 mtime = file_mtime (d->file);
443 check_renamed (d->file);
445 if (is_updating (d->file))
447 error (NILF, _("Circular %s <- %s dependency dropped."),
448 file->name, d->file->name);
449 /* We cannot free D here because our the caller will still have
450 a reference to it when we were called recursively via
451 check_dep below. */
452 if (lastd == 0)
453 file->deps = d->next;
454 else
455 lastd->next = d->next;
456 d = d->next;
457 continue;
460 d->file->parent = file;
461 maybe_make = must_make;
462 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
463 if (! d->ignore_mtime)
464 must_make = maybe_make;
466 check_renamed (d->file);
469 register struct file *f = d->file;
470 if (f->double_colon)
471 f = f->double_colon;
474 running |= (f->command_state == cs_running
475 || f->command_state == cs_deps_running);
476 f = f->prev;
478 while (f != 0);
481 if (dep_status != 0 && !keep_going_flag)
482 break;
484 if (!running)
485 d->changed = file_mtime (d->file) != mtime;
487 lastd = d;
488 d = d->next;
491 /* Now we know whether this target needs updating.
492 If it does, update all the intermediate files we depend on. */
494 if (must_make)
496 for (d = file->deps; d != 0; d = d->next)
497 if (d->file->intermediate)
499 FILE_TIMESTAMP mtime = file_mtime (d->file);
500 check_renamed (d->file);
501 d->file->parent = file;
502 dep_status |= update_file (d->file, depth);
503 check_renamed (d->file);
506 register struct file *f = d->file;
507 if (f->double_colon)
508 f = f->double_colon;
511 running |= (f->command_state == cs_running
512 || f->command_state == cs_deps_running);
513 f = f->prev;
515 while (f != 0);
518 if (dep_status != 0 && !keep_going_flag)
519 break;
521 if (!running)
522 d->changed = ((file->phony && file->cmds != 0)
523 || file_mtime (d->file) != mtime);
527 finish_updating (file);
529 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
531 if (running)
533 set_command_state (file, cs_deps_running);
534 --depth;
535 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
536 return 0;
539 /* If any dependency failed, give up now. */
541 if (dep_status != 0)
543 file->update_status = dep_status;
544 notice_finished_file (file);
546 depth--;
548 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
550 if (depth == 0 && keep_going_flag
551 && !just_print_flag && !question_flag)
552 error (NILF,
553 _("Target `%s' not remade because of errors."), file->name);
555 return dep_status;
558 if (file->command_state == cs_deps_running)
559 /* The commands for some deps were running on the last iteration, but
560 they have finished now. Reset the command_state to not_started to
561 simplify later bookkeeping. It is important that we do this only
562 when the prior state was cs_deps_running, because that prior state
563 was definitely propagated to FILE's also_make's by set_command_state
564 (called above), but in another state an also_make may have
565 independently changed to finished state, and we would confuse that
566 file's bookkeeping (updated, but not_started is bogus state). */
567 set_command_state (file, cs_not_started);
569 /* Now record which dependencies are more
570 recent than this file, so we can define $?. */
572 deps_changed = 0;
573 for (d = file->deps; d != 0; d = d->next)
575 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
576 check_renamed (d->file);
578 if (! d->ignore_mtime)
580 #if 1
581 /* %%% In version 4, remove this code completely to
582 implement not remaking deps if their deps are newer
583 than their parents. */
584 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
585 /* We must remake if this dep does not
586 exist and is not intermediate. */
587 must_make = 1;
588 #endif
590 /* Set DEPS_CHANGED if this dep actually changed. */
591 deps_changed |= d->changed;
594 /* Set D->changed if either this dep actually changed,
595 or its dependent, FILE, is older or does not exist. */
596 d->changed |= noexist || d_mtime > this_mtime;
598 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
600 const char *fmt = 0;
602 if (d->ignore_mtime)
604 if (ISDB (DB_VERBOSE))
605 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
607 else if (d_mtime == NONEXISTENT_MTIME)
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;
723 int touched = 0;
725 file->command_state = cs_finished;
726 file->updated = 1;
728 if (touch_flag
729 /* The update status will be:
730 -1 if this target was not remade;
731 0 if 0 or more commands (+ or ${MAKE}) were run and won;
732 1 if some commands were run and lost.
733 We touch the target if it has commands which either were not run
734 or won when they ran (i.e. status is 0). */
735 && file->update_status == 0)
737 if (file->cmds != 0 && file->cmds->any_recurse)
739 /* If all the command lines were recursive,
740 we don't want to do the touching. */
741 unsigned int i;
742 for (i = 0; i < file->cmds->ncommand_lines; ++i)
743 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
744 goto have_nonrecursing;
746 else
748 have_nonrecursing:
749 if (file->phony)
750 file->update_status = 0;
751 else
753 /* Should set file's modification date and do nothing else. */
754 file->update_status = touch_file (file);
756 /* Pretend we ran a real touch command, to suppress the
757 "`foo' is up to date" message. */
758 commands_started++;
760 /* Request for the timestamp to be updated (and distributed
761 to the double-colon entries). Simply setting ran=1 would
762 almost have done the trick, but messes up with the also_make
763 updating logic below. */
764 touched = 1;
769 if (file->mtime_before_update == UNKNOWN_MTIME)
770 file->mtime_before_update = file->last_mtime;
772 if ((ran && !file->phony) || touched)
774 struct file *f;
775 int i = 0;
777 /* If -n, -t, or -q and all the commands are recursive, we ran them so
778 really check the target's mtime again. Otherwise, assume the target
779 would have been updated. */
781 if (question_flag || just_print_flag || touch_flag)
783 for (i = file->cmds->ncommand_lines; i > 0; --i)
784 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
785 break;
788 /* If there were no commands at all, it's always new. */
790 else if (file->is_target && file->cmds == 0)
791 i = 1;
793 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
795 /* Propagate the change of modification time to all the double-colon
796 entries for this file. */
797 for (f = file->double_colon; f != 0; f = f->prev)
798 f->last_mtime = file->last_mtime;
801 if (ran && file->update_status != -1)
802 /* We actually tried to update FILE, which has
803 updated its also_make's as well (if it worked).
804 If it didn't work, it wouldn't work again for them.
805 So mark them as updated with the same status. */
806 for (d = file->also_make; d != 0; d = d->next)
808 d->file->command_state = cs_finished;
809 d->file->updated = 1;
810 d->file->update_status = file->update_status;
812 if (ran && !d->file->phony)
813 /* Fetch the new modification time.
814 We do this instead of just invalidating the cached time
815 so that a vpath_search can happen. Otherwise, it would
816 never be done because the target is already updated. */
817 (void) f_mtime (d->file, 0);
819 else if (file->update_status == -1)
820 /* Nothing was done for FILE, but it needed nothing done.
821 So mark it now as "succeeded". */
822 file->update_status = 0;
825 /* Check whether another file (whose mtime is THIS_MTIME)
826 needs updating on account of a dependency which is file FILE.
827 If it does, store 1 in *MUST_MAKE_PTR.
828 In the process, update any non-intermediate files
829 that FILE depends on (including FILE itself).
830 Return nonzero if any updating failed. */
832 static int
833 check_dep (file, depth, this_mtime, must_make_ptr)
834 struct file *file;
835 unsigned int depth;
836 FILE_TIMESTAMP this_mtime;
837 int *must_make_ptr;
839 struct dep *d;
840 int dep_status = 0;
841 int maybe_make;
843 ++depth;
844 start_updating (file);
846 if (!file->intermediate)
847 /* If this is a non-intermediate file, update it and record
848 whether it is newer than THIS_MTIME. */
850 FILE_TIMESTAMP mtime;
851 dep_status = update_file (file, depth);
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;
858 else
860 /* FILE is an intermediate file. */
861 FILE_TIMESTAMP mtime;
863 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
865 if (try_implicit_rule (file, depth))
866 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
867 else
868 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
869 file->tried_implicit = 1;
871 if (file->cmds == 0 && !file->is_target
872 && default_file != 0 && default_file->cmds != 0)
874 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
875 file->cmds = default_file->cmds;
878 /* If the intermediate file actually exists
879 and is newer, then we should remake from it. */
880 check_renamed (file);
881 mtime = file_mtime (file);
882 check_renamed (file);
883 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
884 *must_make_ptr = 1;
885 /* Otherwise, update all non-intermediate files we depend on,
886 if necessary, and see whether any of them is more
887 recent than the file on whose behalf we are checking. */
888 else
890 register struct dep *lastd;
892 lastd = 0;
893 d = file->deps;
894 while (d != 0)
896 if (is_updating (d->file))
898 error (NILF, _("Circular %s <- %s dependency dropped."),
899 file->name, d->file->name);
900 if (lastd == 0)
902 file->deps = d->next;
903 free ((char *) d);
904 d = file->deps;
906 else
908 lastd->next = d->next;
909 free ((char *) d);
910 d = lastd->next;
912 continue;
915 d->file->parent = file;
916 maybe_make = *must_make_ptr;
917 dep_status |= check_dep (d->file, depth, this_mtime,
918 &maybe_make);
919 if (! d->ignore_mtime)
920 *must_make_ptr = maybe_make;
921 check_renamed (d->file);
922 if (dep_status != 0 && !keep_going_flag)
923 break;
925 if (d->file->command_state == cs_running
926 || d->file->command_state == cs_deps_running)
927 /* Record that some of FILE's deps are still being made.
928 This tells the upper levels to wait on processing it until
929 the commands are finished. */
930 set_command_state (file, cs_deps_running);
932 lastd = d;
933 d = d->next;
938 finish_updating (file);
939 return dep_status;
942 /* Touch FILE. Return zero if successful, one if not. */
944 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
946 static int
947 touch_file (file)
948 register struct file *file;
950 if (!silent_flag)
951 message (0, "touch %s", file->name);
953 #ifndef NO_ARCHIVES
954 if (ar_name (file->name))
955 return ar_touch (file->name);
956 else
957 #endif
959 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
961 if (fd < 0)
962 TOUCH_ERROR ("touch: open: ");
963 else
965 struct stat statbuf;
966 char buf;
968 if (fstat (fd, &statbuf) < 0)
969 TOUCH_ERROR ("touch: fstat: ");
970 /* Rewrite character 0 same as it already is. */
971 if (read (fd, &buf, 1) < 0)
972 TOUCH_ERROR ("touch: read: ");
973 if (lseek (fd, 0L, 0) < 0L)
974 TOUCH_ERROR ("touch: lseek: ");
975 if (write (fd, &buf, 1) < 0)
976 TOUCH_ERROR ("touch: write: ");
977 /* If file length was 0, we just
978 changed it, so change it back. */
979 if (statbuf.st_size == 0)
981 (void) close (fd);
982 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
983 if (fd < 0)
984 TOUCH_ERROR ("touch: open: ");
986 (void) close (fd);
990 return 0;
993 /* Having checked and updated the dependencies of FILE,
994 do whatever is appropriate to remake FILE itself.
995 Return the status from executing FILE's commands. */
997 static void
998 remake_file (file)
999 struct file *file;
1001 if (file->cmds == 0)
1003 if (file->phony)
1004 /* Phony target. Pretend it succeeded. */
1005 file->update_status = 0;
1006 else if (file->is_target)
1007 /* This is a nonexistent target file we cannot make.
1008 Pretend it was successfully remade. */
1009 file->update_status = 0;
1010 else
1012 const char *msg_noparent
1013 = _("%sNo rule to make target `%s'%s");
1014 const char *msg_parent
1015 = _("%sNo rule to make target `%s', needed by `%s'%s");
1017 /* This is a dependency file we cannot remake. Fail. */
1018 if (!keep_going_flag && !file->dontcare)
1020 if (file->parent == 0)
1021 fatal (NILF, msg_noparent, "", file->name, "");
1023 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
1026 if (!file->dontcare)
1028 if (file->parent == 0)
1029 error (NILF, msg_noparent, "*** ", file->name, ".");
1030 else
1031 error (NILF, msg_parent, "*** ",
1032 file->name, file->parent->name, ".");
1034 file->update_status = 2;
1037 else
1039 chop_commands (file->cmds);
1041 /* The normal case: start some commands. */
1042 if (!touch_flag || file->cmds->any_recurse)
1044 execute_file_commands (file);
1045 return;
1048 /* This tells notice_finished_file it is ok to touch the file. */
1049 file->update_status = 0;
1052 /* This does the touching under -t. */
1053 notice_finished_file (file);
1056 /* Return the mtime of a file, given a `struct file'.
1057 Caches the time in the struct file to avoid excess stat calls.
1059 If the file is not found, and SEARCH is nonzero, VPATH searching and
1060 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1061 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1062 FILE. */
1064 FILE_TIMESTAMP
1065 f_mtime (file, search)
1066 register struct file *file;
1067 int search;
1069 FILE_TIMESTAMP mtime;
1071 /* File's mtime is not known; must get it from the system. */
1073 #ifndef NO_ARCHIVES
1074 if (ar_name (file->name))
1076 /* This file is an archive-member reference. */
1078 char *arname, *memname;
1079 struct file *arfile;
1080 int arname_used = 0;
1081 time_t member_date;
1083 /* Find the archive's name. */
1084 ar_parse_name (file->name, &arname, &memname);
1086 /* Find the modification time of the archive itself.
1087 Also allow for its name to be changed via VPATH search. */
1088 arfile = lookup_file (arname);
1089 if (arfile == 0)
1091 arfile = enter_file (arname);
1092 arname_used = 1;
1094 mtime = f_mtime (arfile, search);
1095 check_renamed (arfile);
1096 if (search && strcmp (arfile->hname, arname))
1098 /* The archive's name has changed.
1099 Change the archive-member reference accordingly. */
1101 char *name;
1102 unsigned int arlen, memlen;
1104 if (!arname_used)
1106 free (arname);
1107 arname_used = 1;
1110 arname = arfile->hname;
1111 arlen = strlen (arname);
1112 memlen = strlen (memname);
1114 /* free (file->name); */
1116 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1117 bcopy (arname, name, arlen);
1118 name[arlen] = '(';
1119 bcopy (memname, name + arlen + 1, memlen);
1120 name[arlen + 1 + memlen] = ')';
1121 name[arlen + 1 + memlen + 1] = '\0';
1123 /* If the archive was found with GPATH, make the change permanent;
1124 otherwise defer it until later. */
1125 if (arfile->name == arfile->hname)
1126 rename_file (file, name);
1127 else
1128 rehash_file (file, name);
1129 check_renamed (file);
1132 if (!arname_used)
1133 free (arname);
1134 free (memname);
1136 file->low_resolution_time = 1;
1138 if (mtime == NONEXISTENT_MTIME)
1139 /* The archive doesn't exist, so its members don't exist either. */
1140 return NONEXISTENT_MTIME;
1142 member_date = ar_member_date (file->hname);
1143 mtime = (member_date == (time_t) -1
1144 ? NONEXISTENT_MTIME
1145 : file_timestamp_cons (file->hname, member_date, 0));
1147 else
1148 #endif
1150 mtime = name_mtime (file->name);
1152 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1154 /* If name_mtime failed, search VPATH. */
1155 char *name = file->name;
1156 if (vpath_search (&name, &mtime)
1157 /* Last resort, is it a library (-lxxx)? */
1158 || (name[0] == '-' && name[1] == 'l'
1159 && library_search (&name, &mtime)))
1161 if (mtime != UNKNOWN_MTIME)
1162 /* vpath_search and library_search store UNKNOWN_MTIME
1163 if they didn't need to do a stat call for their work. */
1164 file->last_mtime = mtime;
1166 /* If we found it in VPATH, see if it's in GPATH too; if so,
1167 change the name right now; if not, defer until after the
1168 dependencies are updated. */
1169 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1171 rename_file (file, name);
1172 check_renamed (file);
1173 return file_mtime (file);
1176 rehash_file (file, name);
1177 check_renamed (file);
1178 mtime = name_mtime (name);
1184 /* Files can have bogus timestamps that nothing newly made will be
1185 "newer" than. Updating their dependents could just result in loops.
1186 So notify the user of the anomaly with a warning.
1188 We only need to do this once, for now. */
1190 if (!clock_skew_detected
1191 && mtime != NONEXISTENT_MTIME
1192 && !file->updated)
1194 static FILE_TIMESTAMP adjusted_now;
1196 FILE_TIMESTAMP adjusted_mtime = mtime;
1198 #if defined(WINDOWS32) || defined(__MSDOS__)
1199 /* Experimentation has shown that FAT filesystems can set file times
1200 up to 3 seconds into the future! Play it safe. */
1202 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1204 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1205 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1206 adjusted_mtime -= adjustment;
1207 #endif
1209 /* If the file's time appears to be in the future, update our
1210 concept of the present and try once more. */
1211 if (adjusted_now < adjusted_mtime)
1213 int resolution;
1214 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1215 adjusted_now = now + (resolution - 1);
1216 if (adjusted_now < adjusted_mtime)
1218 #ifdef NO_FLOAT
1219 error (NILF, _("Warning: File `%s' has modification time in the future"),
1220 file->name);
1221 #else
1222 double from_now =
1223 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1224 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1225 / 1e9));
1226 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1227 file->name, from_now);
1228 #endif
1229 clock_skew_detected = 1;
1235 /* Store the mtime into all the entries for this file. */
1236 if (file->double_colon)
1237 file = file->double_colon;
1241 /* If this file is not implicit but it is intermediate then it was
1242 made so by the .INTERMEDIATE target. If this file has never
1243 been built by us but was found now, it existed before make
1244 started. So, turn off the intermediate bit so make doesn't
1245 delete it, since it didn't create it. */
1246 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1247 && file->command_state == cs_not_started
1248 && !file->tried_implicit && file->intermediate)
1249 file->intermediate = 0;
1251 file->last_mtime = mtime;
1252 file = file->prev;
1254 while (file != 0);
1256 return mtime;
1260 /* Return the mtime of the file or archive-member reference NAME. */
1262 static FILE_TIMESTAMP
1263 name_mtime (name)
1264 register char *name;
1266 struct stat st;
1268 if (stat (name, &st) != 0)
1270 if (errno != ENOENT && errno != ENOTDIR)
1271 perror_with_name ("stat:", name);
1272 return NONEXISTENT_MTIME;
1275 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1279 /* Search for a library file specified as -lLIBNAME, searching for a
1280 suitable library file in the system library directories and the VPATH
1281 directories. */
1283 static int
1284 library_search (lib, mtime_ptr)
1285 char **lib;
1286 FILE_TIMESTAMP *mtime_ptr;
1288 static char *dirs[] =
1290 #ifndef _AMIGA
1291 "/lib",
1292 "/usr/lib",
1293 #endif
1294 #if defined(WINDOWS32) && !defined(LIBDIR)
1296 * This is completely up to the user at product install time. Just define
1297 * a placeholder.
1299 #define LIBDIR "."
1300 #endif
1301 LIBDIR, /* Defined by configuration. */
1305 static char *libpatterns = NULL;
1307 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1308 FILE_TIMESTAMP mtime;
1310 /* Loop variables for the libpatterns value. */
1311 char *p, *p2;
1312 unsigned int len;
1314 char *file, **dp;
1316 /* If we don't have libpatterns, get it. */
1317 if (!libpatterns)
1319 int save = warn_undefined_variables_flag;
1320 warn_undefined_variables_flag = 0;
1322 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1324 warn_undefined_variables_flag = save;
1327 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1328 p2 = libpatterns;
1329 while ((p = find_next_token (&p2, &len)) != 0)
1331 static char *buf = NULL;
1332 static int buflen = 0;
1333 static int libdir_maxlen = -1;
1334 char *libbuf = variable_expand ("");
1336 /* Expand the pattern using LIBNAME as a replacement. */
1338 char c = p[len];
1339 char *p3, *p4;
1341 p[len] = '\0';
1342 p3 = find_percent (p);
1343 if (!p3)
1345 /* Give a warning if there is no pattern, then remove the
1346 pattern so it's ignored next time. */
1347 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1348 for (; len; --len, ++p)
1349 *p = ' ';
1350 *p = c;
1351 continue;
1353 p4 = variable_buffer_output (libbuf, p, p3-p);
1354 p4 = variable_buffer_output (p4, libname, strlen (libname));
1355 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1356 p[len] = c;
1359 /* Look first for `libNAME.a' in the current directory. */
1360 mtime = name_mtime (libbuf);
1361 if (mtime != NONEXISTENT_MTIME)
1363 *lib = xstrdup (libbuf);
1364 if (mtime_ptr != 0)
1365 *mtime_ptr = mtime;
1366 return 1;
1369 /* Now try VPATH search on that. */
1371 file = libbuf;
1372 if (vpath_search (&file, mtime_ptr))
1374 *lib = file;
1375 return 1;
1378 /* Now try the standard set of directories. */
1380 if (!buflen)
1382 for (dp = dirs; *dp != 0; ++dp)
1384 int l = strlen (*dp);
1385 if (l > libdir_maxlen)
1386 libdir_maxlen = l;
1388 buflen = strlen (libbuf);
1389 buf = xmalloc(libdir_maxlen + buflen + 2);
1391 else if (buflen < strlen (libbuf))
1393 buflen = strlen (libbuf);
1394 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1397 for (dp = dirs; *dp != 0; ++dp)
1399 sprintf (buf, "%s/%s", *dp, libbuf);
1400 mtime = name_mtime (buf);
1401 if (mtime != NONEXISTENT_MTIME)
1403 *lib = xstrdup (buf);
1404 if (mtime_ptr != 0)
1405 *mtime_ptr = mtime;
1406 return 1;
1411 return 0;