* Fix PR/1791.
[make.git] / remake.c
blob1ea0df2884e09147b1ed84f401e8a776d6e875e1
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"));
400 must_make = noexist;
402 /* If file was specified as a target with no commands,
403 come up with some default commands. */
405 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
407 if (try_implicit_rule (file, depth))
408 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
409 else
410 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
411 file->tried_implicit = 1;
413 if (file->cmds == 0 && !file->is_target
414 && default_file != 0 && default_file->cmds != 0)
416 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
417 file->cmds = default_file->cmds;
420 /* Update all non-intermediate files we depend on, if necessary,
421 and see whether any of them is more recent than this file. */
423 lastd = 0;
424 d = file->deps;
425 while (d != 0)
427 FILE_TIMESTAMP mtime;
429 check_renamed (d->file);
431 mtime = file_mtime (d->file);
432 check_renamed (d->file);
434 if (is_updating (d->file))
436 error (NILF, _("Circular %s <- %s dependency dropped."),
437 file->name, d->file->name);
438 /* We cannot free D here because our the caller will still have
439 a reference to it when we were called recursively via
440 check_dep below. */
441 if (lastd == 0)
442 file->deps = d->next;
443 else
444 lastd->next = d->next;
445 d = d->next;
446 continue;
449 d->file->parent = file;
450 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
451 check_renamed (d->file);
454 register struct file *f = d->file;
455 if (f->double_colon)
456 f = f->double_colon;
459 running |= (f->command_state == cs_running
460 || f->command_state == cs_deps_running);
461 f = f->prev;
463 while (f != 0);
466 if (dep_status != 0 && !keep_going_flag)
467 break;
469 if (!running)
470 d->changed = file_mtime (d->file) != mtime;
472 lastd = d;
473 d = d->next;
476 /* Now we know whether this target needs updating.
477 If it does, update all the intermediate files we depend on. */
479 if (must_make)
481 for (d = file->deps; d != 0; d = d->next)
482 if (d->file->intermediate)
484 FILE_TIMESTAMP mtime = file_mtime (d->file);
485 check_renamed (d->file);
486 d->file->parent = file;
487 dep_status |= update_file (d->file, depth);
488 check_renamed (d->file);
491 register struct file *f = d->file;
492 if (f->double_colon)
493 f = f->double_colon;
496 running |= (f->command_state == cs_running
497 || f->command_state == cs_deps_running);
498 f = f->prev;
500 while (f != 0);
503 if (dep_status != 0 && !keep_going_flag)
504 break;
506 if (!running)
507 d->changed = ((file->phony && file->cmds != 0)
508 || file_mtime (d->file) != mtime);
512 finish_updating (file);
514 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
516 if (running)
518 set_command_state (file, cs_deps_running);
519 --depth;
520 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
521 return 0;
524 /* If any dependency failed, give up now. */
526 if (dep_status != 0)
528 file->update_status = dep_status;
529 notice_finished_file (file);
531 depth--;
533 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
535 if (depth == 0 && keep_going_flag
536 && !just_print_flag && !question_flag)
537 error (NILF,
538 _("Target `%s' not remade because of errors."), file->name);
540 return dep_status;
543 if (file->command_state == cs_deps_running)
544 /* The commands for some deps were running on the last iteration, but
545 they have finished now. Reset the command_state to not_started to
546 simplify later bookkeeping. It is important that we do this only
547 when the prior state was cs_deps_running, because that prior state
548 was definitely propagated to FILE's also_make's by set_command_state
549 (called above), but in another state an also_make may have
550 independently changed to finished state, and we would confuse that
551 file's bookkeeping (updated, but not_started is bogus state). */
552 set_command_state (file, cs_not_started);
554 /* Now record which dependencies are more
555 recent than this file, so we can define $?. */
557 deps_changed = 0;
558 for (d = file->deps; d != 0; d = d->next)
560 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
561 check_renamed (d->file);
563 #if 1 /* %%% In version 4, remove this code completely to
564 implement not remaking deps if their deps are newer
565 than their parents. */
566 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
567 /* We must remake if this dep does not
568 exist and is not intermediate. */
569 must_make = 1;
570 #endif
572 /* Set DEPS_CHANGED if this dep actually changed. */
573 deps_changed |= d->changed;
575 /* Set D->changed if either this dep actually changed,
576 or its dependent, FILE, is older or does not exist. */
577 d->changed |= noexist || d_mtime > this_mtime;
579 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
581 const char *fmt = 0;
583 if (d_mtime == NONEXISTENT_MTIME)
585 if (ISDB (DB_BASIC))
586 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
588 else if (d->changed)
590 if (ISDB (DB_BASIC))
591 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
593 else if (ISDB (DB_VERBOSE))
594 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
596 if (fmt)
598 print_spaces (depth);
599 printf (fmt, dep_name (d), file->name);
600 fflush (stdout);
605 /* Here depth returns to the value it had when we were called. */
606 depth--;
608 if (file->double_colon && file->deps == 0)
610 must_make = 1;
611 DBF (DB_BASIC,
612 _("Target `%s' is double-colon and has no prerequisites.\n"));
614 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
616 must_make = 0;
617 DBF (DB_VERBOSE,
618 _("No commands for `%s' and no prerequisites actually changed.\n"));
621 if (!must_make)
623 if (ISDB (DB_VERBOSE))
625 print_spaces (depth);
626 printf (_("No need to remake target `%s'"), file->name);
627 if (!streq (file->name, file->hname))
628 printf (_("; using VPATH name `%s'"), file->hname);
629 puts (".");
630 fflush (stdout);
633 notice_finished_file (file);
635 /* Since we don't need to remake the file, convert it to use the
636 VPATH filename if we found one. hfile will be either the
637 local name if no VPATH or the VPATH name if one was found. */
639 while (file)
641 file->name = file->hname;
642 file = file->prev;
645 return 0;
648 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
650 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
651 VPATH. */
652 if (!streq(file->name, file->hname))
654 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
655 file->ignore_vpath = 1;
658 /* Now, take appropriate actions to remake the file. */
659 remake_file (file);
661 if (file->command_state != cs_finished)
663 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
664 return 0;
667 switch (file->update_status)
669 case 2:
670 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
671 break;
672 case 0:
673 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
674 break;
675 case 1:
676 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
677 break;
678 default:
679 assert (file->update_status >= 0 && file->update_status <= 2);
680 break;
683 file->updated = 1;
684 return file->update_status;
687 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
688 files listed in its `also_make' member. Under -t, this function also
689 touches FILE.
691 On return, FILE->update_status will no longer be -1 if it was. */
693 void
694 notice_finished_file (file)
695 register struct file *file;
697 struct dep *d;
698 int ran = file->command_state == cs_running;
700 file->command_state = cs_finished;
701 file->updated = 1;
703 if (touch_flag
704 /* The update status will be:
705 -1 if this target was not remade;
706 0 if 0 or more commands (+ or ${MAKE}) were run and won;
707 1 if some commands were run and lost.
708 We touch the target if it has commands which either were not run
709 or won when they ran (i.e. status is 0). */
710 && file->update_status == 0)
712 if (file->cmds != 0 && file->cmds->any_recurse)
714 /* If all the command lines were recursive,
715 we don't want to do the touching. */
716 unsigned int i;
717 for (i = 0; i < file->cmds->ncommand_lines; ++i)
718 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
719 goto have_nonrecursing;
721 else
723 have_nonrecursing:
724 if (file->phony)
725 file->update_status = 0;
726 else
727 /* Should set file's modification date and do nothing else. */
728 file->update_status = touch_file (file);
732 if (file->mtime_before_update == UNKNOWN_MTIME)
733 file->mtime_before_update = file->last_mtime;
735 if (ran && !file->phony)
737 struct file *f;
738 int i = 0;
740 /* If -n or -q and all the commands are recursive, we ran them so
741 really check the target's mtime again. Otherwise, assume the target
742 would have been updated. */
744 if (question_flag || just_print_flag)
746 for (i = file->cmds->ncommand_lines; i > 0; --i)
747 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
748 break;
751 /* If there were no commands at all, it's always new. */
753 else if (file->is_target && file->cmds == 0)
754 i = 1;
756 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
758 /* Propagate the change of modification time to all the double-colon
759 entries for this file. */
760 for (f = file->double_colon; f != 0; f = f->next)
761 f->last_mtime = file->last_mtime;
764 if (ran && file->update_status != -1)
765 /* We actually tried to update FILE, which has
766 updated its also_make's as well (if it worked).
767 If it didn't work, it wouldn't work again for them.
768 So mark them as updated with the same status. */
769 for (d = file->also_make; d != 0; d = d->next)
771 d->file->command_state = cs_finished;
772 d->file->updated = 1;
773 d->file->update_status = file->update_status;
775 if (ran && !d->file->phony)
776 /* Fetch the new modification time.
777 We do this instead of just invalidating the cached time
778 so that a vpath_search can happen. Otherwise, it would
779 never be done because the target is already updated. */
780 (void) f_mtime (d->file, 0);
782 else if (file->update_status == -1)
783 /* Nothing was done for FILE, but it needed nothing done.
784 So mark it now as "succeeded". */
785 file->update_status = 0;
788 /* Check whether another file (whose mtime is THIS_MTIME)
789 needs updating on account of a dependency which is file FILE.
790 If it does, store 1 in *MUST_MAKE_PTR.
791 In the process, update any non-intermediate files
792 that FILE depends on (including FILE itself).
793 Return nonzero if any updating failed. */
795 static int
796 check_dep (file, depth, this_mtime, must_make_ptr)
797 struct file *file;
798 unsigned int depth;
799 FILE_TIMESTAMP this_mtime;
800 int *must_make_ptr;
802 register struct dep *d;
803 int dep_status = 0;
805 ++depth;
806 start_updating (file);
808 if (!file->intermediate)
809 /* If this is a non-intermediate file, update it and record
810 whether it is newer than THIS_MTIME. */
812 FILE_TIMESTAMP mtime;
813 dep_status = update_file (file, depth);
814 check_renamed (file);
815 mtime = file_mtime (file);
816 check_renamed (file);
817 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
818 *must_make_ptr = 1;
820 else
822 /* FILE is an intermediate file. */
823 FILE_TIMESTAMP mtime;
825 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
827 if (try_implicit_rule (file, depth))
828 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
829 else
830 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
831 file->tried_implicit = 1;
833 if (file->cmds == 0 && !file->is_target
834 && default_file != 0 && default_file->cmds != 0)
836 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
837 file->cmds = default_file->cmds;
840 /* If the intermediate file actually exists
841 and is newer, then we should remake from it. */
842 check_renamed (file);
843 mtime = file_mtime (file);
844 check_renamed (file);
845 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
846 *must_make_ptr = 1;
847 /* Otherwise, update all non-intermediate files we depend on,
848 if necessary, and see whether any of them is more
849 recent than the file on whose behalf we are checking. */
850 else
852 register struct dep *lastd;
854 lastd = 0;
855 d = file->deps;
856 while (d != 0)
858 if (is_updating (d->file))
860 error (NILF, _("Circular %s <- %s dependency dropped."),
861 file->name, d->file->name);
862 if (lastd == 0)
864 file->deps = d->next;
865 free ((char *) d);
866 d = file->deps;
868 else
870 lastd->next = d->next;
871 free ((char *) d);
872 d = lastd->next;
874 continue;
877 d->file->parent = file;
878 dep_status |= check_dep (d->file, depth, this_mtime,
879 must_make_ptr);
880 check_renamed (d->file);
881 if (dep_status != 0 && !keep_going_flag)
882 break;
884 if (d->file->command_state == cs_running
885 || d->file->command_state == cs_deps_running)
886 /* Record that some of FILE's deps are still being made.
887 This tells the upper levels to wait on processing it until
888 the commands are finished. */
889 set_command_state (file, cs_deps_running);
891 lastd = d;
892 d = d->next;
897 finish_updating (file);
898 return dep_status;
901 /* Touch FILE. Return zero if successful, one if not. */
903 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
905 static int
906 touch_file (file)
907 register struct file *file;
909 if (!silent_flag)
910 message (0, "touch %s", file->name);
912 #ifndef NO_ARCHIVES
913 if (ar_name (file->name))
914 return ar_touch (file->name);
915 else
916 #endif
918 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
920 if (fd < 0)
921 TOUCH_ERROR ("touch: open: ");
922 else
924 struct stat statbuf;
925 char buf;
926 int status;
929 status = fstat (fd, &statbuf);
930 while (status < 0 && EINTR_SET);
932 if (status < 0)
933 TOUCH_ERROR ("touch: fstat: ");
934 /* Rewrite character 0 same as it already is. */
935 if (read (fd, &buf, 1) < 0)
936 TOUCH_ERROR ("touch: read: ");
937 if (lseek (fd, 0L, 0) < 0L)
938 TOUCH_ERROR ("touch: lseek: ");
939 if (write (fd, &buf, 1) < 0)
940 TOUCH_ERROR ("touch: write: ");
941 /* If file length was 0, we just
942 changed it, so change it back. */
943 if (statbuf.st_size == 0)
945 (void) close (fd);
946 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
947 if (fd < 0)
948 TOUCH_ERROR ("touch: open: ");
950 (void) close (fd);
954 return 0;
957 /* Having checked and updated the dependencies of FILE,
958 do whatever is appropriate to remake FILE itself.
959 Return the status from executing FILE's commands. */
961 static void
962 remake_file (file)
963 struct file *file;
965 if (file->cmds == 0)
967 if (file->phony)
968 /* Phony target. Pretend it succeeded. */
969 file->update_status = 0;
970 else if (file->is_target)
971 /* This is a nonexistent target file we cannot make.
972 Pretend it was successfully remade. */
973 file->update_status = 0;
974 else
976 const char *msg_noparent
977 = _("%sNo rule to make target `%s'%s");
978 const char *msg_parent
979 = _("%sNo rule to make target `%s', needed by `%s'%s");
981 /* This is a dependency file we cannot remake. Fail. */
982 if (!keep_going_flag && !file->dontcare)
984 if (file->parent == 0)
985 fatal (NILF, msg_noparent, "", file->name, "");
987 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
990 if (!file->dontcare)
992 if (file->parent == 0)
993 error (NILF, msg_noparent, "*** ", file->name, ".");
994 else
995 error (NILF, msg_parent, "*** ",
996 file->name, file->parent->name, ".");
998 file->update_status = 2;
1001 else
1003 chop_commands (file->cmds);
1005 /* The normal case: start some commands. */
1006 if (!touch_flag || file->cmds->any_recurse)
1008 execute_file_commands (file);
1009 return;
1012 /* This tells notice_finished_file it is ok to touch the file. */
1013 file->update_status = 0;
1016 /* This does the touching under -t. */
1017 notice_finished_file (file);
1020 /* Return the mtime of a file, given a `struct file'.
1021 Caches the time in the struct file to avoid excess stat calls.
1023 If the file is not found, and SEARCH is nonzero, VPATH searching and
1024 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1025 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1026 FILE. */
1028 FILE_TIMESTAMP
1029 f_mtime (file, search)
1030 register struct file *file;
1031 int search;
1033 FILE_TIMESTAMP mtime;
1035 /* File's mtime is not known; must get it from the system. */
1037 #ifndef NO_ARCHIVES
1038 if (ar_name (file->name))
1040 /* This file is an archive-member reference. */
1042 char *arname, *memname;
1043 struct file *arfile;
1044 time_t memtime;
1045 int arname_used = 0;
1046 time_t member_date;
1048 /* Find the archive's name. */
1049 ar_parse_name (file->name, &arname, &memname);
1051 /* Find the modification time of the archive itself.
1052 Also allow for its name to be changed via VPATH search. */
1053 arfile = lookup_file (arname);
1054 if (arfile == 0)
1056 arfile = enter_file (arname);
1057 arname_used = 1;
1059 mtime = f_mtime (arfile, search);
1060 check_renamed (arfile);
1061 if (search && strcmp (arfile->hname, arname))
1063 /* The archive's name has changed.
1064 Change the archive-member reference accordingly. */
1066 char *name;
1067 unsigned int arlen, memlen;
1069 if (!arname_used)
1071 free (arname);
1072 arname_used = 1;
1075 arname = arfile->hname;
1076 arlen = strlen (arname);
1077 memlen = strlen (memname);
1079 /* free (file->name); */
1081 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1082 bcopy (arname, name, arlen);
1083 name[arlen] = '(';
1084 bcopy (memname, name + arlen + 1, memlen);
1085 name[arlen + 1 + memlen] = ')';
1086 name[arlen + 1 + memlen + 1] = '\0';
1088 /* If the archive was found with GPATH, make the change permanent;
1089 otherwise defer it until later. */
1090 if (arfile->name == arfile->hname)
1091 rename_file (file, name);
1092 else
1093 rehash_file (file, name);
1094 check_renamed (file);
1097 if (!arname_used)
1098 free (arname);
1099 free (memname);
1101 if (mtime == NONEXISTENT_MTIME)
1102 /* The archive doesn't exist, so its members don't exist either. */
1103 return NONEXISTENT_MTIME;
1105 member_date = ar_member_date (file->hname);
1106 mtime = (member_date == (time_t) -1
1107 ? NONEXISTENT_MTIME
1108 : file_timestamp_cons (file->hname, member_date, 0));
1110 else
1111 #endif
1113 mtime = name_mtime (file->name);
1115 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1117 /* If name_mtime failed, search VPATH. */
1118 char *name = file->name;
1119 if (vpath_search (&name, &mtime)
1120 /* Last resort, is it a library (-lxxx)? */
1121 || (name[0] == '-' && name[1] == 'l'
1122 && library_search (&name, &mtime)))
1124 if (mtime != UNKNOWN_MTIME)
1125 /* vpath_search and library_search store UNKNOWN_MTIME
1126 if they didn't need to do a stat call for their work. */
1127 file->last_mtime = mtime;
1129 /* If we found it in VPATH, see if it's in GPATH too; if so,
1130 change the name right now; if not, defer until after the
1131 dependencies are updated. */
1132 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1134 rename_file (file, name);
1135 check_renamed (file);
1136 return file_mtime (file);
1139 rehash_file (file, name);
1140 check_renamed (file);
1141 mtime = name_mtime (name);
1147 /* Files can have bogus timestamps that nothing newly made will be
1148 "newer" than. Updating their dependents could just result in loops.
1149 So notify the user of the anomaly with a warning.
1151 We only need to do this once, for now. */
1153 if (!clock_skew_detected
1154 && mtime != NONEXISTENT_MTIME
1155 && !file->updated)
1157 static FILE_TIMESTAMP now;
1159 FILE_TIMESTAMP adjusted_mtime = mtime;
1161 #if defined(WINDOWS32) || defined(__MSDOS__)
1162 FILE_TIMESTAMP adjustment;
1163 #ifdef WINDOWS32
1164 /* FAT filesystems round time to the nearest even second!
1165 Allow for any file (NTFS or FAT) to perhaps suffer from this
1166 brain damage. */
1167 adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1168 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1169 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1170 : 0);
1171 #else
1172 /* On DJGPP under Windows 98 and Windows NT, FAT filesystems can
1173 set file times up to 3 seconds into the future! The bug doesn't
1174 occur in plain DOS or in Windows 95, but we play it safe. */
1175 adjustment = (FILE_TIMESTAMP) 3 << FILE_TIMESTAMP_LO_BITS;
1176 #endif
1177 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1178 adjusted_mtime -= adjustment;
1179 #endif
1181 /* If the file's time appears to be in the future, udpate our
1182 concept of the present and try once more. */
1183 if (now < adjusted_mtime
1184 && (now = file_timestamp_now ()) < adjusted_mtime)
1186 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1187 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1189 file_timestamp_sprintf (mtimebuf, mtime);
1190 file_timestamp_sprintf (nowbuf, now);
1191 error (NILF, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1192 file->name, mtimebuf, nowbuf);
1193 clock_skew_detected = 1;
1198 /* Store the mtime into all the entries for this file. */
1199 if (file->double_colon)
1200 file = file->double_colon;
1204 /* If this file is not implicit but it is intermediate then it was
1205 made so by the .INTERMEDIATE target. If this file has never
1206 been built by us but was found now, it existed before make
1207 started. So, turn off the intermediate bit so make doesn't
1208 delete it, since it didn't create it. */
1209 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1210 && file->command_state == cs_not_started
1211 && !file->tried_implicit && file->intermediate)
1212 file->intermediate = 0;
1214 file->last_mtime = mtime;
1215 file = file->prev;
1217 while (file != 0);
1219 return mtime;
1223 /* Return the mtime of the file or archive-member reference NAME. */
1225 static FILE_TIMESTAMP
1226 name_mtime (name)
1227 register char *name;
1229 struct stat st;
1231 while (stat (name, &st) != 0)
1232 if (errno != EINTR)
1234 if (errno != ENOENT && errno != ENOTDIR)
1235 perror_with_name ("stat:", name);
1236 return NONEXISTENT_MTIME;
1239 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1243 /* Search for a library file specified as -lLIBNAME, searching for a
1244 suitable library file in the system library directories and the VPATH
1245 directories. */
1247 static int
1248 library_search (lib, mtime_ptr)
1249 char **lib;
1250 FILE_TIMESTAMP *mtime_ptr;
1252 static char *dirs[] =
1254 #ifndef _AMIGA
1255 "/lib",
1256 "/usr/lib",
1257 #endif
1258 #if defined(WINDOWS32) && !defined(LIBDIR)
1260 * This is completely up to the user at product install time. Just define
1261 * a placeholder.
1263 #define LIBDIR "."
1264 #endif
1265 LIBDIR, /* Defined by configuration. */
1269 static char *libpatterns = NULL;
1271 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1272 FILE_TIMESTAMP mtime;
1274 /* Loop variables for the libpatterns value. */
1275 char *p, *p2;
1276 unsigned int len;
1278 char *file, **dp;
1280 /* If we don't have libpatterns, get it. */
1281 if (!libpatterns)
1283 int save = warn_undefined_variables_flag;
1284 warn_undefined_variables_flag = 0;
1286 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1288 warn_undefined_variables_flag = save;
1291 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1292 p2 = libpatterns;
1293 while ((p = find_next_token (&p2, &len)) != 0)
1295 static char *buf = NULL;
1296 static int buflen = 0;
1297 static int libdir_maxlen = -1;
1298 char *libbuf = variable_expand ("");
1300 /* Expand the pattern using LIBNAME as a replacement. */
1302 char c = p[len];
1303 char *p3, *p4;
1305 p[len] = '\0';
1306 p3 = find_percent (p);
1307 if (!p3)
1309 /* Give a warning if there is no pattern, then remove the
1310 pattern so it's ignored next time. */
1311 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1312 for (; len; --len, ++p)
1313 *p = ' ';
1314 *p = c;
1315 continue;
1317 p4 = variable_buffer_output (libbuf, p, p3-p);
1318 p4 = variable_buffer_output (p4, libname, strlen (libname));
1319 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1320 p[len] = c;
1323 /* Look first for `libNAME.a' in the current directory. */
1324 mtime = name_mtime (libbuf);
1325 if (mtime != NONEXISTENT_MTIME)
1327 *lib = xstrdup (libbuf);
1328 if (mtime_ptr != 0)
1329 *mtime_ptr = mtime;
1330 return 1;
1333 /* Now try VPATH search on that. */
1335 file = libbuf;
1336 if (vpath_search (&file, mtime_ptr))
1338 *lib = file;
1339 return 1;
1342 /* Now try the standard set of directories. */
1344 if (!buflen)
1346 for (dp = dirs; *dp != 0; ++dp)
1348 int l = strlen (*dp);
1349 if (l > libdir_maxlen)
1350 libdir_maxlen = l;
1352 buflen = strlen (libbuf);
1353 buf = xmalloc(libdir_maxlen + buflen + 2);
1355 else if (buflen < strlen (libbuf))
1357 buflen = strlen (libbuf);
1358 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1361 for (dp = dirs; *dp != 0; ++dp)
1363 sprintf (buf, "%s/%s", *dp, libbuf);
1364 mtime = name_mtime (buf);
1365 if (mtime != NONEXISTENT_MTIME)
1367 *lib = xstrdup (buf);
1368 if (mtime_ptr != 0)
1369 *mtime_ptr = mtime;
1370 return 1;
1375 return 0;