* New config.sub and config.guess
[make.git] / remake.c
bloba2b2127226c39068c41db44e3094d54bccbf01dd
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 static int update_file PARAMS ((struct file *file, unsigned int depth));
62 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
63 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
64 static int touch_file PARAMS ((struct file *file));
65 static void remake_file PARAMS ((struct file *file));
66 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
67 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
70 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
71 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
72 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
73 be disabled for them unless they were also command-line targets, and we
74 should only make one goal at a time and return as soon as one goal whose
75 `changed' member is nonzero is successfully made. */
77 int
78 update_goal_chain (goals, makefiles)
79 register struct dep *goals;
80 int makefiles;
82 int t = touch_flag, q = question_flag, n = just_print_flag;
83 unsigned int j = job_slots;
84 int status = -1;
86 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
87 : file_mtime (file))
89 /* Duplicate the chain so we can remove things from it. */
91 goals = copy_dep_chain (goals);
94 /* Clear the `changed' flag of each goal in the chain.
95 We will use the flag below to notice when any commands
96 have actually been run for a target. When no commands
97 have been run, we give an "up to date" diagnostic. */
99 struct dep *g;
100 for (g = goals; g != 0; g = g->next)
101 g->changed = 0;
104 /* All files start with the considered bit 0, so the global value is 1. */
105 considered = 1;
107 /* Update all the goals until they are all finished. */
109 while (goals != 0)
111 register struct dep *g, *lastgoal;
113 /* Start jobs that are waiting for the load to go down. */
115 start_waiting_jobs ();
117 /* Wait for a child to die. */
119 reap_children (1, 0);
121 lastgoal = 0;
122 g = goals;
123 while (g != 0)
125 /* Iterate over all double-colon entries for this file. */
126 struct file *file;
127 int stop = 0, any_not_updated = 0;
129 for (file = g->file->double_colon ? g->file->double_colon : g->file;
130 file != NULL;
131 file = file->prev)
133 unsigned int ocommands_started;
134 int x;
135 check_renamed (file);
136 if (makefiles)
138 if (file->cmd_target)
140 touch_flag = t;
141 question_flag = q;
142 just_print_flag = n;
144 else
145 touch_flag = question_flag = just_print_flag = 0;
148 /* Save the old value of `commands_started' so we can compare
149 later. It will be incremented when any commands are
150 actually run. */
151 ocommands_started = commands_started;
153 x = update_file (file, makefiles ? 1 : 0);
154 check_renamed (file);
156 /* Set the goal's `changed' flag if any commands were started
157 by calling update_file above. We check this flag below to
158 decide when to give an "up to date" diagnostic. */
159 g->changed += commands_started - ocommands_started;
161 /* If we updated a file and STATUS was not already 1, set it to
162 1 if updating failed, or to 0 if updating succeeded. Leave
163 STATUS as it is if no updating was done. */
165 stop = 0;
166 if ((x != 0 || file->updated) && status < 1)
168 if (file->update_status != 0)
170 /* Updating failed, or -q triggered. The STATUS value
171 tells our caller which. */
172 status = file->update_status;
173 /* If -q just triggered, stop immediately. It doesn't
174 matter how much more we run, since we already know
175 the answer to return. */
176 stop = (!keep_going_flag && !question_flag
177 && !makefiles);
179 else
181 FILE_TIMESTAMP mtime = MTIME (file);
182 check_renamed (file);
184 if (file->updated && g->changed &&
185 mtime != file->mtime_before_update)
187 /* Updating was done. If this is a makefile and
188 just_print_flag or question_flag is set (meaning
189 -n or -q was given and this file was specified
190 as a command-line target), don't change STATUS.
191 If STATUS is changed, we will get re-exec'd, and
192 enter an infinite loop. */
193 if (!makefiles
194 || (!just_print_flag && !question_flag))
195 status = 0;
196 if (makefiles && file->dontcare)
197 /* This is a default makefile; stop remaking. */
198 stop = 1;
203 /* Keep track if any double-colon entry is not finished.
204 When they are all finished, the goal is finished. */
205 any_not_updated |= !file->updated;
207 if (stop)
208 break;
211 /* Reset FILE since it is null at the end of the loop. */
212 file = g->file;
214 if (stop || !any_not_updated)
216 /* If we have found nothing whatever to do for the goal,
217 print a message saying nothing needs doing. */
219 if (!makefiles
220 /* If the update_status is zero, we updated successfully
221 or not at all. G->changed will have been set above if
222 any commands were actually started for this goal. */
223 && file->update_status == 0 && !g->changed
224 /* Never give a message under -s or -q. */
225 && !silent_flag && !question_flag)
226 message (1, ((file->phony || file->cmds == 0)
227 ? _("Nothing to be done for `%s'.")
228 : _("`%s' is up to date.")),
229 file->name);
231 /* This goal is finished. Remove it from the chain. */
232 if (lastgoal == 0)
233 goals = g->next;
234 else
235 lastgoal->next = g->next;
237 /* Free the storage. */
238 free ((char *) g);
240 g = lastgoal == 0 ? goals : lastgoal->next;
242 if (stop)
243 break;
245 else
247 lastgoal = g;
248 g = g->next;
252 /* If we reached the end of the dependency graph toggle the considered
253 flag for the next pass. */
254 if (g == 0)
255 considered = !considered;
258 if (makefiles)
260 touch_flag = t;
261 question_flag = q;
262 just_print_flag = n;
263 job_slots = j;
265 return status;
268 /* If FILE is not up to date, execute the commands for it.
269 Return 0 if successful, 1 if unsuccessful;
270 but with some flag settings, just call `exit' if unsuccessful.
272 DEPTH is the depth in recursions of this function.
273 We increment it during the consideration of our dependencies,
274 then decrement it again after finding out whether this file
275 is out of date.
277 If there are multiple double-colon entries for FILE,
278 each is considered in turn. */
280 static int
281 update_file (file, depth)
282 struct file *file;
283 unsigned int depth;
285 register int status = 0;
286 register struct file *f;
288 f = file->double_colon ? file->double_colon : file;
290 /* Prune the dependency graph: if we've already been here on _this_
291 pass through the dependency graph, we don't have to go any further.
292 We won't reap_children until we start the next pass, so no state
293 change is possible below here until then. */
294 if (f->considered == considered)
296 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
297 return f->command_state == cs_finished ? f->update_status : 0;
300 /* This loop runs until we start commands for a double colon rule, or until
301 the chain is exhausted. */
302 for (; f != 0; f = f->prev)
304 f->considered = considered;
306 status |= update_file_1 (f, depth);
307 check_renamed (f);
309 if (status != 0 && !keep_going_flag)
310 break;
312 if (f->command_state == cs_running
313 || f->command_state == cs_deps_running)
315 /* Don't run the other :: rules for this
316 file until this rule is finished. */
317 status = 0;
318 break;
322 /* Process the remaining rules in the double colon chain so they're marked
323 considered. Start their prerequisites, too. */
324 for (; f != 0 ; f = f->prev)
326 struct dep *d;
328 f->considered = considered;
330 for (d = f->deps; d != 0; d = d->next)
331 status |= update_file (d->file, depth + 1);
334 return status;
337 /* Consider a single `struct file' and update it as appropriate. */
339 static int
340 update_file_1 (file, depth)
341 struct file *file;
342 unsigned int depth;
344 register FILE_TIMESTAMP this_mtime;
345 int noexist, must_make, deps_changed;
346 int dep_status = 0;
347 register struct dep *d, *lastd;
348 int running = 0;
350 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
352 if (file->updated)
354 if (file->update_status > 0)
356 DBF (DB_VERBOSE,
357 _("Recently tried and failed to update file `%s'.\n"));
358 return file->update_status;
361 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
362 return 0;
365 switch (file->command_state)
367 case cs_not_started:
368 case cs_deps_running:
369 break;
370 case cs_running:
371 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
372 return 0;
373 case cs_finished:
374 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
375 return file->update_status;
376 default:
377 abort ();
380 ++depth;
382 /* Notice recursive update of the same file. */
383 start_updating (file);
385 /* Looking at the file's modtime beforehand allows the possibility
386 that its name may be changed by a VPATH search, and thus it may
387 not need an implicit rule. If this were not done, the file
388 might get implicit commands that apply to its initial name, only
389 to have that name replaced with another found by VPATH search. */
391 this_mtime = file_mtime (file);
392 check_renamed (file);
393 noexist = this_mtime == (FILE_TIMESTAMP) -1;
394 if (noexist)
395 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
397 must_make = noexist;
399 /* If file was specified as a target with no commands,
400 come up with some default commands. */
402 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
404 if (try_implicit_rule (file, depth))
405 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
406 else
407 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
408 file->tried_implicit = 1;
410 if (file->cmds == 0 && !file->is_target
411 && default_file != 0 && default_file->cmds != 0)
413 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
414 file->cmds = default_file->cmds;
417 /* Update all non-intermediate files we depend on, if necessary,
418 and see whether any of them is more recent than this file. */
420 lastd = 0;
421 d = file->deps;
422 while (d != 0)
424 FILE_TIMESTAMP mtime;
426 check_renamed (d->file);
428 mtime = file_mtime (d->file);
429 check_renamed (d->file);
431 if (is_updating (d->file))
433 error (NILF, _("Circular %s <- %s dependency dropped."),
434 file->name, d->file->name);
435 /* We cannot free D here because our the caller will still have
436 a reference to it when we were called recursively via
437 check_dep below. */
438 if (lastd == 0)
439 file->deps = d->next;
440 else
441 lastd->next = d->next;
442 d = d->next;
443 continue;
446 d->file->parent = file;
447 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
448 check_renamed (d->file);
451 register struct file *f = d->file;
452 if (f->double_colon)
453 f = f->double_colon;
456 running |= (f->command_state == cs_running
457 || f->command_state == cs_deps_running);
458 f = f->prev;
460 while (f != 0);
463 if (dep_status != 0 && !keep_going_flag)
464 break;
466 if (!running)
467 d->changed = file_mtime (d->file) != mtime;
469 lastd = d;
470 d = d->next;
473 /* Now we know whether this target needs updating.
474 If it does, update all the intermediate files we depend on. */
476 if (must_make)
478 for (d = file->deps; d != 0; d = d->next)
479 if (d->file->intermediate)
481 FILE_TIMESTAMP mtime = file_mtime (d->file);
482 check_renamed (d->file);
483 d->file->parent = file;
484 dep_status |= update_file (d->file, depth);
485 check_renamed (d->file);
488 register struct file *f = d->file;
489 if (f->double_colon)
490 f = f->double_colon;
493 running |= (f->command_state == cs_running
494 || f->command_state == cs_deps_running);
495 f = f->prev;
497 while (f != 0);
500 if (dep_status != 0 && !keep_going_flag)
501 break;
503 if (!running)
504 d->changed = ((file->phony && file->cmds != 0)
505 || file_mtime (d->file) != mtime);
509 finish_updating (file);
511 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
513 if (running)
515 set_command_state (file, cs_deps_running);
516 --depth;
517 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
518 return 0;
521 /* If any dependency failed, give up now. */
523 if (dep_status != 0)
525 file->update_status = dep_status;
526 notice_finished_file (file);
528 depth--;
530 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
532 if (depth == 0 && keep_going_flag
533 && !just_print_flag && !question_flag)
534 error (NILF,
535 _("Target `%s' not remade because of errors."), file->name);
537 return dep_status;
540 if (file->command_state == cs_deps_running)
541 /* The commands for some deps were running on the last iteration, but
542 they have finished now. Reset the command_state to not_started to
543 simplify later bookkeeping. It is important that we do this only
544 when the prior state was cs_deps_running, because that prior state
545 was definitely propagated to FILE's also_make's by set_command_state
546 (called above), but in another state an also_make may have
547 independently changed to finished state, and we would confuse that
548 file's bookkeeping (updated, but not_started is bogus state). */
549 set_command_state (file, cs_not_started);
551 /* Now record which dependencies are more
552 recent than this file, so we can define $?. */
554 deps_changed = 0;
555 for (d = file->deps; d != 0; d = d->next)
557 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
558 check_renamed (d->file);
560 #if 1 /* %%% In version 4, remove this code completely to
561 implement not remaking deps if their deps are newer
562 than their parents. */
563 if (d_mtime == (FILE_TIMESTAMP) -1 && !d->file->intermediate)
564 /* We must remake if this dep does not
565 exist and is not intermediate. */
566 must_make = 1;
567 #endif
569 /* Set DEPS_CHANGED if this dep actually changed. */
570 deps_changed |= d->changed;
572 /* Set D->changed if either this dep actually changed,
573 or its dependent, FILE, is older or does not exist. */
574 d->changed |= noexist || d_mtime > this_mtime;
576 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
578 const char *fmt = 0;
580 if (d_mtime == (FILE_TIMESTAMP) -1)
582 if (ISDB (DB_BASIC))
583 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
585 else if (d->changed)
587 if (ISDB (DB_BASIC))
588 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
590 else if (ISDB (DB_VERBOSE))
591 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
593 if (fmt)
595 print_spaces (depth);
596 printf (fmt, dep_name (d), file->name);
597 fflush (stdout);
602 /* Here depth returns to the value it had when we were called. */
603 depth--;
605 if (file->double_colon && file->deps == 0)
607 must_make = 1;
608 DBF (DB_BASIC,
609 _("Target `%s' is double-colon and has no prerequisites.\n"));
611 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
613 must_make = 0;
614 DBF (DB_VERBOSE,
615 _("No commands for `%s' and no prerequisites actually changed.\n"));
618 if (!must_make)
620 if (ISDB (DB_VERBOSE))
622 print_spaces (depth);
623 printf (_("No need to remake target `%s'"), file->name);
624 if (!streq (file->name, file->hname))
625 printf (_("; using VPATH name `%s'"), file->hname);
626 puts (".");
627 fflush (stdout);
630 notice_finished_file (file);
632 /* Since we don't need to remake the file, convert it to use the
633 VPATH filename if we found one. hfile will be either the
634 local name if no VPATH or the VPATH name if one was found. */
636 while (file)
638 file->name = file->hname;
639 file = file->prev;
642 return 0;
645 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
647 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
648 VPATH. */
649 if (!streq(file->name, file->hname))
651 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
652 file->ignore_vpath = 1;
655 /* Now, take appropriate actions to remake the file. */
656 remake_file (file);
658 if (file->command_state != cs_finished)
660 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
661 return 0;
664 switch (file->update_status)
666 case 2:
667 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
668 break;
669 case 0:
670 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
671 break;
672 case 1:
673 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
674 break;
675 default:
676 assert (file->update_status >= 0 && file->update_status <= 2);
677 break;
680 file->updated = 1;
681 return file->update_status;
684 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
685 files listed in its `also_make' member. Under -t, this function also
686 touches FILE.
688 On return, FILE->update_status will no longer be -1 if it was. */
690 void
691 notice_finished_file (file)
692 register struct file *file;
694 struct dep *d;
695 int ran = file->command_state == cs_running;
697 file->command_state = cs_finished;
698 file->updated = 1;
700 if (touch_flag
701 /* The update status will be:
702 -1 if this target was not remade;
703 0 if 0 or more commands (+ or ${MAKE}) were run and won;
704 1 if some commands were run and lost.
705 We touch the target if it has commands which either were not run
706 or won when they ran (i.e. status is 0). */
707 && file->update_status == 0)
709 if (file->cmds != 0 && file->cmds->any_recurse)
711 /* If all the command lines were recursive,
712 we don't want to do the touching. */
713 unsigned int i;
714 for (i = 0; i < file->cmds->ncommand_lines; ++i)
715 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
716 goto have_nonrecursing;
718 else
720 have_nonrecursing:
721 if (file->phony)
722 file->update_status = 0;
723 else
724 /* Should set file's modification date and do nothing else. */
725 file->update_status = touch_file (file);
729 if (file->mtime_before_update == 0)
730 file->mtime_before_update = file->last_mtime;
732 if (ran && !file->phony)
734 struct file *f;
735 int i = 0;
737 /* If -n or -q and all the commands are recursive, we ran them so
738 really check the target's mtime again. Otherwise, assume the target
739 would have been updated. */
741 if (question_flag || just_print_flag)
743 for (i = file->cmds->ncommand_lines; i > 0; --i)
744 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
745 break;
748 /* If there were no commands at all, it's always new. */
750 else if (file->is_target && file->cmds == 0)
751 i = 1;
753 file->last_mtime = i == 0 ? 0 : NEW_MTIME;
755 /* Propagate the change of modification time to all the double-colon
756 entries for this file. */
757 for (f = file->double_colon; f != 0; f = f->next)
758 f->last_mtime = file->last_mtime;
761 if (ran && file->update_status != -1)
762 /* We actually tried to update FILE, which has
763 updated its also_make's as well (if it worked).
764 If it didn't work, it wouldn't work again for them.
765 So mark them as updated with the same status. */
766 for (d = file->also_make; d != 0; d = d->next)
768 d->file->command_state = cs_finished;
769 d->file->updated = 1;
770 d->file->update_status = file->update_status;
772 if (ran && !d->file->phony)
773 /* Fetch the new modification time.
774 We do this instead of just invalidating the cached time
775 so that a vpath_search can happen. Otherwise, it would
776 never be done because the target is already updated. */
777 (void) f_mtime (d->file, 0);
779 else if (file->update_status == -1)
780 /* Nothing was done for FILE, but it needed nothing done.
781 So mark it now as "succeeded". */
782 file->update_status = 0;
785 /* Check whether another file (whose mtime is THIS_MTIME)
786 needs updating on account of a dependency which is file FILE.
787 If it does, store 1 in *MUST_MAKE_PTR.
788 In the process, update any non-intermediate files
789 that FILE depends on (including FILE itself).
790 Return nonzero if any updating failed. */
792 static int
793 check_dep (file, depth, this_mtime, must_make_ptr)
794 struct file *file;
795 unsigned int depth;
796 FILE_TIMESTAMP this_mtime;
797 int *must_make_ptr;
799 register struct dep *d;
800 int dep_status = 0;
802 ++depth;
803 start_updating (file);
805 if (!file->intermediate)
806 /* If this is a non-intermediate file, update it and record
807 whether it is newer than THIS_MTIME. */
809 FILE_TIMESTAMP mtime;
810 dep_status = update_file (file, depth);
811 check_renamed (file);
812 mtime = file_mtime (file);
813 check_renamed (file);
814 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
815 *must_make_ptr = 1;
817 else
819 /* FILE is an intermediate file. */
820 FILE_TIMESTAMP mtime;
822 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
824 if (try_implicit_rule (file, depth))
825 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
826 else
827 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
828 file->tried_implicit = 1;
830 if (file->cmds == 0 && !file->is_target
831 && default_file != 0 && default_file->cmds != 0)
833 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
834 file->cmds = default_file->cmds;
837 /* If the intermediate file actually exists
838 and is newer, then we should remake from it. */
839 check_renamed (file);
840 mtime = file_mtime (file);
841 check_renamed (file);
842 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
843 *must_make_ptr = 1;
844 /* Otherwise, update all non-intermediate files we depend on,
845 if necessary, and see whether any of them is more
846 recent than the file on whose behalf we are checking. */
847 else
849 register struct dep *lastd;
851 lastd = 0;
852 d = file->deps;
853 while (d != 0)
855 if (is_updating (d->file))
857 error (NILF, _("Circular %s <- %s dependency dropped."),
858 file->name, d->file->name);
859 if (lastd == 0)
861 file->deps = d->next;
862 free ((char *) d);
863 d = file->deps;
865 else
867 lastd->next = d->next;
868 free ((char *) d);
869 d = lastd->next;
871 continue;
874 d->file->parent = file;
875 dep_status |= check_dep (d->file, depth, this_mtime,
876 must_make_ptr);
877 check_renamed (d->file);
878 if (dep_status != 0 && !keep_going_flag)
879 break;
881 if (d->file->command_state == cs_running
882 || d->file->command_state == cs_deps_running)
883 /* Record that some of FILE's deps are still being made.
884 This tells the upper levels to wait on processing it until
885 the commands are finished. */
886 set_command_state (file, cs_deps_running);
888 lastd = d;
889 d = d->next;
894 finish_updating (file);
895 return dep_status;
898 /* Touch FILE. Return zero if successful, one if not. */
900 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
902 static int
903 touch_file (file)
904 register struct file *file;
906 if (!silent_flag)
907 message (0, "touch %s", file->name);
909 #ifndef NO_ARCHIVES
910 if (ar_name (file->name))
911 return ar_touch (file->name);
912 else
913 #endif
915 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
917 if (fd < 0)
918 TOUCH_ERROR ("touch: open: ");
919 else
921 struct stat statbuf;
922 char buf;
923 int status;
926 status = fstat (fd, &statbuf);
927 while (status < 0 && EINTR_SET);
929 if (status < 0)
930 TOUCH_ERROR ("touch: fstat: ");
931 /* Rewrite character 0 same as it already is. */
932 if (read (fd, &buf, 1) < 0)
933 TOUCH_ERROR ("touch: read: ");
934 if (lseek (fd, 0L, 0) < 0L)
935 TOUCH_ERROR ("touch: lseek: ");
936 if (write (fd, &buf, 1) < 0)
937 TOUCH_ERROR ("touch: write: ");
938 /* If file length was 0, we just
939 changed it, so change it back. */
940 if (statbuf.st_size == 0)
942 (void) close (fd);
943 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
944 if (fd < 0)
945 TOUCH_ERROR ("touch: open: ");
947 (void) close (fd);
951 return 0;
954 /* Having checked and updated the dependencies of FILE,
955 do whatever is appropriate to remake FILE itself.
956 Return the status from executing FILE's commands. */
958 static void
959 remake_file (file)
960 struct file *file;
962 if (file->cmds == 0)
964 if (file->phony)
965 /* Phony target. Pretend it succeeded. */
966 file->update_status = 0;
967 else if (file->is_target)
968 /* This is a nonexistent target file we cannot make.
969 Pretend it was successfully remade. */
970 file->update_status = 0;
971 else
973 const char *msg_noparent
974 = _("%sNo rule to make target `%s'%s");
975 const char *msg_parent
976 = _("%sNo rule to make target `%s', needed by `%s'%s");
978 /* This is a dependency file we cannot remake. Fail. */
979 if (!keep_going_flag && !file->dontcare)
981 if (file->parent == 0)
982 fatal (NILF, msg_noparent, "", file->name, "");
984 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
987 if (!file->dontcare)
989 if (file->parent == 0)
990 error (NILF, msg_noparent, "*** ", file->name, ".");
991 else
992 error (NILF, msg_parent, "*** ",
993 file->name, file->parent->name, ".");
995 file->update_status = 2;
998 else
1000 chop_commands (file->cmds);
1002 /* The normal case: start some commands. */
1003 if (!touch_flag || file->cmds->any_recurse)
1005 execute_file_commands (file);
1006 return;
1009 /* This tells notice_finished_file it is ok to touch the file. */
1010 file->update_status = 0;
1013 /* This does the touching under -t. */
1014 notice_finished_file (file);
1017 /* Return the mtime of a file, given a `struct file'.
1018 Caches the time in the struct file to avoid excess stat calls.
1020 If the file is not found, and SEARCH is nonzero, VPATH searching and
1021 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1022 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1023 FILE. */
1025 FILE_TIMESTAMP
1026 f_mtime (file, search)
1027 register struct file *file;
1028 int search;
1030 FILE_TIMESTAMP mtime;
1032 /* File's mtime is not known; must get it from the system. */
1034 #ifndef NO_ARCHIVES
1035 if (ar_name (file->name))
1037 /* This file is an archive-member reference. */
1039 char *arname, *memname;
1040 struct file *arfile;
1041 int arname_used = 0;
1043 /* Find the archive's name. */
1044 ar_parse_name (file->name, &arname, &memname);
1046 /* Find the modification time of the archive itself.
1047 Also allow for its name to be changed via VPATH search. */
1048 arfile = lookup_file (arname);
1049 if (arfile == 0)
1051 arfile = enter_file (arname);
1052 arname_used = 1;
1054 mtime = f_mtime (arfile, search);
1055 check_renamed (arfile);
1056 if (search && strcmp (arfile->hname, arname))
1058 /* The archive's name has changed.
1059 Change the archive-member reference accordingly. */
1061 char *name;
1062 unsigned int arlen, memlen;
1064 if (!arname_used)
1066 free (arname);
1067 arname_used = 1;
1070 arname = arfile->hname;
1071 arlen = strlen (arname);
1072 memlen = strlen (memname);
1074 /* free (file->name); */
1076 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1077 bcopy (arname, name, arlen);
1078 name[arlen] = '(';
1079 bcopy (memname, name + arlen + 1, memlen);
1080 name[arlen + 1 + memlen] = ')';
1081 name[arlen + 1 + memlen + 1] = '\0';
1083 /* If the archive was found with GPATH, make the change permanent;
1084 otherwise defer it until later. */
1085 if (arfile->name == arfile->hname)
1086 rename_file (file, name);
1087 else
1088 rehash_file (file, name);
1089 check_renamed (file);
1092 if (!arname_used)
1093 free (arname);
1094 free (memname);
1096 if (mtime == (FILE_TIMESTAMP) -1)
1097 /* The archive doesn't exist, so it's members don't exist either. */
1098 return (FILE_TIMESTAMP) -1;
1100 mtime = ar_member_date (file->hname);
1102 else
1103 #endif
1105 mtime = name_mtime (file->name);
1107 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1109 /* If name_mtime failed, search VPATH. */
1110 char *name = file->name;
1111 if (vpath_search (&name, &mtime)
1112 /* Last resort, is it a library (-lxxx)? */
1113 || (name[0] == '-' && name[1] == 'l'
1114 && library_search (&name, &mtime)))
1116 if (mtime != 0)
1117 /* vpath_search and library_search store zero in MTIME
1118 if they didn't need to do a stat call for their work. */
1119 file->last_mtime = mtime;
1121 /* If we found it in VPATH, see if it's in GPATH too; if so,
1122 change the name right now; if not, defer until after the
1123 dependencies are updated. */
1124 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1126 rename_file (file, name);
1127 check_renamed (file);
1128 return file_mtime (file);
1131 rehash_file (file, name);
1132 check_renamed (file);
1133 mtime = name_mtime (name);
1139 /* Files can have bogus timestamps that nothing newly made will be
1140 "newer" than. Updating their dependents could just result in loops.
1141 So notify the user of the anomaly with a warning.
1143 We only need to do this once, for now. */
1145 static FILE_TIMESTAMP now = 0;
1146 if (!clock_skew_detected
1147 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1148 && !file->updated)
1150 /* This file's time appears to be in the future.
1151 Update our concept of the present, and compare again. */
1153 now = file_timestamp_now ();
1155 #ifdef WINDOWS32
1157 * FAT filesystems round time to nearest even second(!). Just
1158 * allow for any file (NTFS or FAT) to perhaps suffer from this
1159 * braindamage.
1161 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1162 #else
1163 #ifdef __MSDOS__
1164 /* Scrupulous testing indicates that some Windows
1165 filesystems can set file times up to 3 sec into the future! */
1166 if (mtime > now + 3)
1167 #else
1168 if (mtime > now)
1169 #endif
1170 #endif
1172 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1173 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1175 file_timestamp_sprintf (mtimebuf, mtime);
1176 file_timestamp_sprintf (nowbuf, now);
1177 error (NILF, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1178 file->name, mtimebuf, nowbuf);
1179 clock_skew_detected = 1;
1184 /* Store the mtime into all the entries for this file. */
1185 if (file->double_colon)
1186 file = file->double_colon;
1190 /* If this file is not implicit but it is intermediate then it was
1191 made so by the .INTERMEDIATE target. If this file has never
1192 been built by us but was found now, it existed before make
1193 started. So, turn off the intermediate bit so make doesn't
1194 delete it, since it didn't create it. */
1195 if (mtime != (FILE_TIMESTAMP)-1 && file->command_state == cs_not_started
1196 && !file->tried_implicit && file->intermediate)
1197 file->intermediate = 0;
1199 file->last_mtime = mtime;
1200 file = file->prev;
1202 while (file != 0);
1204 return mtime;
1208 /* Return the mtime of the file or archive-member reference NAME. */
1210 static FILE_TIMESTAMP
1211 name_mtime (name)
1212 register char *name;
1214 struct stat st;
1216 if (stat (name, &st) < 0)
1217 return (FILE_TIMESTAMP) -1;
1219 return FILE_TIMESTAMP_STAT_MODTIME (st);
1223 /* Search for a library file specified as -lLIBNAME, searching for a
1224 suitable library file in the system library directories and the VPATH
1225 directories. */
1227 static int
1228 library_search (lib, mtime_ptr)
1229 char **lib;
1230 FILE_TIMESTAMP *mtime_ptr;
1232 static char *dirs[] =
1234 #ifndef _AMIGA
1235 "/lib",
1236 "/usr/lib",
1237 #endif
1238 #if defined(WINDOWS32) && !defined(LIBDIR)
1240 * This is completely up to the user at product install time. Just define
1241 * a placeholder.
1243 #define LIBDIR "."
1244 #endif
1245 LIBDIR, /* Defined by configuration. */
1249 static char *libpatterns = NULL;
1251 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1252 FILE_TIMESTAMP mtime;
1254 /* Loop variables for the libpatterns value. */
1255 char *p, *p2;
1256 unsigned int len;
1258 char *file, **dp;
1260 /* If we don't have libpatterns, get it. */
1261 if (!libpatterns)
1263 int save = warn_undefined_variables_flag;
1264 warn_undefined_variables_flag = 0;
1266 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1268 warn_undefined_variables_flag = save;
1271 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1272 p2 = libpatterns;
1273 while ((p = find_next_token (&p2, &len)) != 0)
1275 static char *buf = NULL;
1276 static int buflen = 0;
1277 static int libdir_maxlen = -1;
1278 char *libbuf = variable_expand ("");
1280 /* Expand the pattern using LIBNAME as a replacement. */
1282 char c = p[len];
1283 char *p3, *p4;
1285 p[len] = '\0';
1286 p3 = find_percent (p);
1287 if (!p3)
1289 /* Give a warning if there is no pattern, then remove the
1290 pattern so it's ignored next time. */
1291 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1292 for (; len; --len, ++p)
1293 *p = ' ';
1294 *p = c;
1295 continue;
1297 p4 = variable_buffer_output (libbuf, p, p3-p);
1298 p4 = variable_buffer_output (p4, libname, strlen (libname));
1299 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1300 p[len] = c;
1303 /* Look first for `libNAME.a' in the current directory. */
1304 mtime = name_mtime (libbuf);
1305 if (mtime != (FILE_TIMESTAMP) -1)
1307 *lib = xstrdup (libbuf);
1308 if (mtime_ptr != 0)
1309 *mtime_ptr = mtime;
1310 return 1;
1313 /* Now try VPATH search on that. */
1315 file = libbuf;
1316 if (vpath_search (&file, mtime_ptr))
1318 *lib = file;
1319 return 1;
1322 /* Now try the standard set of directories. */
1324 if (!buflen)
1326 for (dp = dirs; *dp != 0; ++dp)
1328 int l = strlen (*dp);
1329 if (l > libdir_maxlen)
1330 libdir_maxlen = l;
1332 buflen = strlen (libbuf);
1333 buf = xmalloc(libdir_maxlen + buflen + 2);
1335 else if (buflen < strlen (libbuf))
1337 buflen = strlen (libbuf);
1338 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1341 for (dp = dirs; *dp != 0; ++dp)
1343 sprintf (buf, "%s/%s", *dp, libbuf);
1344 mtime = name_mtime (buf);
1345 if (mtime != (FILE_TIMESTAMP) -1)
1347 *lib = xstrdup (buf);
1348 if (mtime_ptr != 0)
1349 *mtime_ptr = mtime;
1350 return 1;
1355 return 0;