* Release 3.77.92.
[make.git] / remake.c
blob74c4a9acb97f9212ecd4870d864687fb5b50cac6
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 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"
27 #include <assert.h>
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #else
32 #include <sys/file.h>
33 #endif
35 #ifdef VMS
36 #include <starlet.h>
37 #endif
38 #ifdef WINDOWS32
39 #include <io.h>
40 #endif
42 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
45 /* Incremented when a command is started (under -n, when one would be). */
46 unsigned int commands_started = 0;
48 static int update_file PARAMS ((struct file *file, unsigned int depth));
49 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
50 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
51 static int touch_file PARAMS ((struct file *file));
52 static void remake_file PARAMS ((struct file *file));
53 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
54 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
57 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
58 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
59 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
60 be disabled for them unless they were also command-line targets, and we
61 should only make one goal at a time and return as soon as one goal whose
62 `changed' member is nonzero is successfully made. */
64 /* We need to know this "lower down" for correct error handling. */
65 static int updating_makefiles = 0;
67 int
68 update_goal_chain (goals, makefiles)
69 register struct dep *goals;
70 int makefiles;
72 int t = touch_flag, q = question_flag, n = just_print_flag;
73 unsigned int j = job_slots;
74 int status = -1;
76 updating_makefiles = makefiles;
78 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
79 : file_mtime (file))
81 /* Duplicate the chain so we can remove things from it. */
83 goals = copy_dep_chain (goals);
86 /* Clear the `changed' flag of each goal in the chain.
87 We will use the flag below to notice when any commands
88 have actually been run for a target. When no commands
89 have been run, we give an "up to date" diagnostic. */
91 struct dep *g;
92 for (g = goals; g != 0; g = g->next)
93 g->changed = 0;
96 #if 0
97 /* Only run one job at a time when building makefiles.
98 No one seems to know why this was done, and no one can think of a good
99 reason to do it. Hopefully an obvious one won't appear as soon as we
100 release the next version :-/. */
101 if (makefiles)
102 job_slots = 1;
103 #endif
105 /* All files start with the considered bit 0, so the global value is 1. */
106 considered = 1;
108 /* Update all the goals until they are all finished. */
110 while (goals != 0)
112 register struct dep *g, *lastgoal;
114 /* Start jobs that are waiting for the load to go down. */
116 start_waiting_jobs ();
118 /* Wait for a child to die. */
120 reap_children (1, 0);
122 lastgoal = 0;
123 g = goals;
124 while (g != 0)
126 /* Iterate over all double-colon entries for this file. */
127 struct file *file;
128 int stop = 0, any_not_updated = 0;
130 for (file = g->file->double_colon ? g->file->double_colon : g->file;
131 file != NULL;
132 file = file->prev)
134 unsigned int ocommands_started;
135 int x;
136 check_renamed (file);
137 if (makefiles)
139 if (file->cmd_target)
141 touch_flag = t;
142 question_flag = q;
143 just_print_flag = n;
145 else
146 touch_flag = question_flag = just_print_flag = 0;
149 /* Save the old value of `commands_started' so we can compare
150 later. It will be incremented when any commands are
151 actually run. */
152 ocommands_started = commands_started;
154 x = update_file (file, makefiles ? 1 : 0);
155 check_renamed (file);
157 /* Set the goal's `changed' flag if any commands were started
158 by calling update_file above. We check this flag below to
159 decide when to give an "up to date" diagnostic. */
160 g->changed += commands_started - ocommands_started;
162 stop = 0;
163 if (x != 0 || file->updated)
165 /* If STATUS was not already 1, set it to 1 if
166 updating failed, or to 0 if updating succeeded.
167 Leave STATUS as it is if no updating was done. */
169 if (status < 1)
171 if (file->update_status != 0)
173 /* Updating failed, or -q triggered.
174 The STATUS value tells our caller which. */
175 status = file->update_status;
176 /* If -q just triggered, stop immediately.
177 It doesn't matter how much more we run,
178 since we already know the answer to return. */
179 stop = (!keep_going_flag && !question_flag
180 && !makefiles);
182 else if (file->updated && g->changed &&
183 file->last_mtime != file->mtime_before_update)
185 /* Updating was done. If this is a makefile and
186 just_print_flag or question_flag is set
187 (meaning -n or -q was given and this file was
188 specified as a command-line target), don't
189 change STATUS. If STATUS is changed, we will
190 get re-exec'd, and fall into an infinite loop. */
191 if (!makefiles
192 || (!just_print_flag && !question_flag))
193 status = 0;
194 if (makefiles && file->dontcare)
195 /* This is a default makefile. Stop remaking. */
196 stop = 1;
201 /* Keep track if any double-colon entry is not finished.
202 When they are all finished, the goal is finished. */
203 any_not_updated |= !file->updated;
205 if (stop)
206 break;
209 /* Reset FILE since it is null at the end of the loop. */
210 file = g->file;
212 if (stop || !any_not_updated)
214 /* If we have found nothing whatever to do for the goal,
215 print a message saying nothing needs doing. */
217 if (!makefiles
218 /* If the update_status is zero, we updated successfully
219 or not at all. G->changed will have been set above if
220 any commands were actually started for this goal. */
221 && file->update_status == 0 && !g->changed
222 /* Never give a message under -s or -q. */
223 && !silent_flag && !question_flag)
224 message (1, ((file->phony || file->cmds == 0)
225 ? _("Nothing to be done for `%s'.")
226 : _("`%s' is up to date.")),
227 file->name);
229 /* This goal is finished. Remove it from the chain. */
230 if (lastgoal == 0)
231 goals = g->next;
232 else
233 lastgoal->next = g->next;
235 /* Free the storage. */
236 free ((char *) g);
238 g = lastgoal == 0 ? goals : lastgoal->next;
240 if (stop)
241 break;
243 else
245 lastgoal = g;
246 g = g->next;
250 /* If we reached the end of the dependency graph toggle the considered
251 flag for the next pass. */
252 if (g == 0)
253 considered = !considered;
256 if (makefiles)
258 touch_flag = t;
259 question_flag = q;
260 just_print_flag = n;
261 job_slots = j;
263 return status;
266 /* Generate an error/fatal message if no rules are available for the target.
268 static void
269 no_rule_error(file)
270 struct file *file;
272 static const char msg_noparent[]
273 = _("%sNo rule to make target `%s'%s");
274 static const char msg_parent[]
275 = _("%sNo rule to make target `%s', needed by `%s'%s");
277 if (keep_going_flag || file->dontcare)
279 /* If the previous attempt was made while we were creating
280 makefiles, but we aren't anymore, print an error now. */
281 if (!file->dontcare
282 || (file->mfile_status && !updating_makefiles))
284 if (file->parent == 0)
285 error (NILF, msg_noparent, "*** ", file->name, ".");
286 else
287 error (NILF, msg_parent, "*** ",
288 file->name, file->parent->name, ".");
290 file->update_status = 2;
291 file->mfile_status = updating_makefiles;
293 else if (file->parent == 0)
294 fatal (NILF, msg_noparent, "", file->name, "");
295 else
296 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
299 /* If FILE is not up to date, execute the commands for it.
300 Return 0 if successful, 1 if unsuccessful;
301 but with some flag settings, just call `exit' if unsuccessful.
303 DEPTH is the depth in recursions of this function.
304 We increment it during the consideration of our dependencies,
305 then decrement it again after finding out whether this file
306 is out of date.
308 If there are multiple double-colon entries for FILE,
309 each is considered in turn. */
311 static int
312 update_file (file, depth)
313 struct file *file;
314 unsigned int depth;
316 register int status = 0;
317 register struct file *f;
319 /* Prune the dependency graph: if we've already been here on _this_ pass
320 through the dependency graph, we don't have to go any further. We won't
321 reap_children until we start the next pass, so no state change is
322 possible below here until then. */
323 if (file->considered == considered)
325 DEBUGPR (_("Pruning file `%s'.\n"));
326 return 0;
328 file->considered = considered;
330 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
332 status |= update_file_1 (f, depth);
333 check_renamed (f);
335 if (status != 0 && !keep_going_flag)
336 return status;
338 switch (f->command_state)
340 case cs_finished:
341 /* The file is done being remade. */
342 break;
344 case cs_running:
345 case cs_deps_running:
346 /* Don't run the other :: rules for this
347 file until this rule is finished. */
348 return 0;
350 default:
351 assert (f->command_state == cs_running);
352 break;
356 return status;
359 /* Consider a single `struct file' and update it as appropriate. */
361 static int
362 update_file_1 (file, depth)
363 struct file *file;
364 unsigned int depth;
366 register FILE_TIMESTAMP this_mtime;
367 int noexist, must_make, deps_changed;
368 int dep_status = 0;
369 register struct dep *d, *lastd;
370 int running = 0;
372 DEBUGPR (_("Considering target file `%s'.\n"));
374 if (file->updated)
376 if (file->update_status > 0)
378 DEBUGPR (_("Recently tried and failed to update file `%s'.\n"));
379 no_rule_error(file);
380 return file->update_status;
383 DEBUGPR (_("File `%s' was considered already.\n"));
384 return 0;
387 switch (file->command_state)
389 case cs_not_started:
390 case cs_deps_running:
391 break;
392 case cs_running:
393 DEBUGPR (_("Still updating file `%s'.\n"));
394 return 0;
395 case cs_finished:
396 DEBUGPR (_("Finished updating file `%s'.\n"));
397 return file->update_status;
398 default:
399 abort ();
402 ++depth;
404 /* Notice recursive update of the same file. */
405 file->updating = 1;
407 /* Looking at the file's modtime beforehand allows the possibility
408 that its name may be changed by a VPATH search, and thus it may
409 not need an implicit rule. If this were not done, the file
410 might get implicit commands that apply to its initial name, only
411 to have that name replaced with another found by VPATH search. */
413 this_mtime = file_mtime (file);
414 check_renamed (file);
415 noexist = this_mtime == (FILE_TIMESTAMP) -1;
416 if (noexist)
417 DEBUGPR (_("File `%s' does not exist.\n"));
419 must_make = noexist;
421 /* If file was specified as a target with no commands,
422 come up with some default commands. */
424 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
426 if (try_implicit_rule (file, depth))
427 DEBUGPR (_("Found an implicit rule for `%s'.\n"));
428 else
429 DEBUGPR (_("No implicit rule found for `%s'.\n"));
430 file->tried_implicit = 1;
432 if (file->cmds == 0 && !file->is_target
433 && default_file != 0 && default_file->cmds != 0)
435 DEBUGPR (_("Using default commands for `%s'.\n"));
436 file->cmds = default_file->cmds;
439 /* Update all non-intermediate files we depend on, if necessary,
440 and see whether any of them is more recent than this file. */
442 lastd = 0;
443 d = file->deps;
444 while (d != 0)
446 FILE_TIMESTAMP mtime;
448 check_renamed (d->file);
450 mtime = file_mtime (d->file);
451 check_renamed (d->file);
453 if (d->file->updating)
455 error (NILF, _("Circular %s <- %s dependency dropped."),
456 file->name, d->file->name);
457 /* We cannot free D here because our the caller will still have
458 a reference to it when we were called recursively via
459 check_dep below. */
460 if (lastd == 0)
461 file->deps = d->next;
462 else
463 lastd->next = d->next;
464 d = d->next;
465 continue;
468 d->file->parent = file;
469 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
470 check_renamed (d->file);
473 register struct file *f = d->file;
474 if (f->double_colon)
475 f = f->double_colon;
478 running |= (f->command_state == cs_running
479 || f->command_state == cs_deps_running);
480 f = f->prev;
482 while (f != 0);
485 if (dep_status != 0 && !keep_going_flag)
486 break;
488 if (!running)
489 d->changed = file_mtime (d->file) != mtime;
491 lastd = d;
492 d = d->next;
495 /* Now we know whether this target needs updating.
496 If it does, update all the intermediate files we depend on. */
498 if (must_make)
500 for (d = file->deps; d != 0; d = d->next)
501 if (d->file->intermediate)
503 FILE_TIMESTAMP mtime = file_mtime (d->file);
504 check_renamed (d->file);
505 d->file->parent = file;
506 dep_status |= update_file (d->file, depth);
507 check_renamed (d->file);
510 register struct file *f = d->file;
511 if (f->double_colon)
512 f = f->double_colon;
515 running |= (f->command_state == cs_running
516 || f->command_state == cs_deps_running);
517 f = f->prev;
519 while (f != 0);
522 if (dep_status != 0 && !keep_going_flag)
523 break;
525 if (!running)
526 d->changed = ((file->phony && file->cmds != 0)
527 || file_mtime (d->file) != mtime);
531 file->updating = 0;
533 DEBUGPR (_("Finished dependencies of target file `%s'.\n"));
535 if (running)
537 set_command_state (file, cs_deps_running);
538 --depth;
539 DEBUGPR (_("The dependencies of `%s' are being made.\n"));
540 return 0;
543 /* If any dependency failed, give up now. */
545 if (dep_status != 0)
547 file->update_status = dep_status;
548 notice_finished_file (file);
550 depth--;
552 DEBUGPR (_("Giving up on target file `%s'.\n"));
554 if (depth == 0 && keep_going_flag
555 && !just_print_flag && !question_flag)
556 error (NILF, _("Target `%s' not remade because of errors."), file->name);
558 return dep_status;
561 if (file->command_state == cs_deps_running)
562 /* The commands for some deps were running on the last iteration, but
563 they have finished now. Reset the command_state to not_started to
564 simplify later bookkeeping. It is important that we do this only
565 when the prior state was cs_deps_running, because that prior state
566 was definitely propagated to FILE's also_make's by set_command_state
567 (called above), but in another state an also_make may have
568 independently changed to finished state, and we would confuse that
569 file's bookkeeping (updated, but not_started is bogus state). */
570 set_command_state (file, cs_not_started);
572 /* Now record which dependencies are more
573 recent than this file, so we can define $?. */
575 deps_changed = 0;
576 for (d = file->deps; d != 0; d = d->next)
578 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
579 check_renamed (d->file);
581 #if 1 /* %%% 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 == (FILE_TIMESTAMP) -1 && !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;
593 /* Set D->changed if either this dep actually changed,
594 or its dependent, FILE, is older or does not exist. */
595 d->changed |= noexist || d_mtime > this_mtime;
597 if (debug_flag && !noexist)
599 print_spaces (depth);
600 if (d_mtime == (FILE_TIMESTAMP) -1)
601 printf (_("Dependency `%s' does not exist.\n"), dep_name (d));
602 else
603 printf (_("Dependency `%s' is %s than dependent `%s'.\n"),
604 dep_name (d), d->changed ? _("newer") : _("older"), file->name);
605 fflush (stdout);
609 /* Here depth returns to the value it had when we were called. */
610 depth--;
612 if (file->double_colon && file->deps == 0)
614 must_make = 1;
615 DEBUGPR (_("Target `%s' is double-colon and has no dependencies.\n"));
617 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
619 must_make = 0;
620 DEBUGPR (_("No commands for `%s' and no dependencies actually changed.\n"));
623 if (!must_make)
625 if (debug_flag)
627 print_spaces(depth);
628 printf(_("No need to remake target `%s'"), file->name);
629 if (!streq(file->name, file->hname))
630 printf(_("; using VPATH name `%s'"), file->hname);
631 printf(".\n");
632 fflush(stdout);
635 notice_finished_file (file);
637 /* Since we don't need to remake the file, convert it to use the
638 VPATH filename if we found one. hfile will be either the
639 local name if no VPATH or the VPATH name if one was found. */
641 while (file)
643 file->name = file->hname;
644 file = file->prev;
647 return 0;
650 DEBUGPR (_("Must remake target `%s'.\n"));
652 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
653 VPATH. */
654 if (!streq(file->name, file->hname))
656 if (debug_flag)
658 print_spaces (depth);
659 printf(_(" Ignoring VPATH name `%s'.\n"), file->hname);
660 fflush(stdout);
662 file->ignore_vpath = 1;
665 /* Now, take appropriate actions to remake the file. */
666 remake_file (file);
668 if (file->command_state != cs_finished)
670 DEBUGPR (_("Commands of `%s' are being run.\n"));
671 return 0;
674 switch (file->update_status)
676 case 2:
677 DEBUGPR (_("Failed to remake target file `%s'.\n"));
678 break;
679 case 0:
680 DEBUGPR (_("Successfully remade target file `%s'.\n"));
681 break;
682 case 1:
683 DEBUGPR (_("Target file `%s' needs remade under -q.\n"));
684 break;
685 default:
686 assert (file->update_status >= 0 && file->update_status <= 2);
687 break;
690 file->updated = 1;
691 return file->update_status;
694 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
695 files listed in its `also_make' member. Under -t, this function also
696 touches FILE.
698 On return, FILE->update_status will no longer be -1 if it was. */
700 void
701 notice_finished_file (file)
702 register struct file *file;
704 struct dep *d;
705 int ran = file->command_state == cs_running;
707 file->command_state = cs_finished;
708 file->updated = 1;
710 if (touch_flag
711 /* The update status will be:
712 -1 if this target was not remade;
713 0 if 0 or more commands (+ or ${MAKE}) were run and won;
714 1 if some commands were run and lost.
715 We touch the target if it has commands which either were not run
716 or won when they ran (i.e. status is 0). */
717 && file->update_status == 0)
719 if (file->cmds != 0 && file->cmds->any_recurse)
721 /* If all the command lines were recursive,
722 we don't want to do the touching. */
723 unsigned int i;
724 for (i = 0; i < file->cmds->ncommand_lines; ++i)
725 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
726 goto have_nonrecursing;
728 else
730 have_nonrecursing:
731 if (file->phony)
732 file->update_status = 0;
733 else
734 /* Should set file's modification date and do nothing else. */
735 file->update_status = touch_file (file);
739 if (ran && !file->phony)
741 struct file *f;
743 assert(file->mtime_before_update == 0);
744 file->mtime_before_update = file->last_mtime;
746 if (just_print_flag || question_flag
747 || (file->is_target && file->cmds == 0))
748 file->last_mtime = NEW_MTIME;
749 else
750 file->last_mtime = 0;
752 /* Propagate the change of modification time to all the double-colon
753 entries for this file. */
754 for (f = file->double_colon; f != 0; f = f->next)
755 f->last_mtime = file->last_mtime;
758 if (ran && file->update_status != -1)
759 /* We actually tried to update FILE, which has
760 updated its also_make's as well (if it worked).
761 If it didn't work, it wouldn't work again for them.
762 So mark them as updated with the same status. */
763 for (d = file->also_make; d != 0; d = d->next)
765 d->file->command_state = cs_finished;
766 d->file->updated = 1;
767 d->file->update_status = file->update_status;
769 if (ran && !d->file->phony)
770 /* Fetch the new modification time.
771 We do this instead of just invalidating the cached time
772 so that a vpath_search can happen. Otherwise, it would
773 never be done because the target is already updated. */
774 (void) f_mtime (d->file, 0);
776 else if (file->update_status == -1)
777 /* Nothing was done for FILE, but it needed nothing done.
778 So mark it now as "succeeded". */
779 file->update_status = 0;
782 /* Check whether another file (whose mtime is THIS_MTIME)
783 needs updating on account of a dependency which is file FILE.
784 If it does, store 1 in *MUST_MAKE_PTR.
785 In the process, update any non-intermediate files
786 that FILE depends on (including FILE itself).
787 Return nonzero if any updating failed. */
789 static int
790 check_dep (file, depth, this_mtime, must_make_ptr)
791 struct file *file;
792 unsigned int depth;
793 FILE_TIMESTAMP this_mtime;
794 int *must_make_ptr;
796 register struct dep *d;
797 int dep_status = 0;
799 ++depth;
800 file->updating = 1;
802 if (!file->intermediate)
803 /* If this is a non-intermediate file, update it and record
804 whether it is newer than THIS_MTIME. */
806 FILE_TIMESTAMP mtime;
807 dep_status = update_file (file, depth);
808 check_renamed (file);
809 mtime = file_mtime (file);
810 check_renamed (file);
811 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
812 *must_make_ptr = 1;
814 else
816 /* FILE is an intermediate file. */
817 FILE_TIMESTAMP mtime;
819 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
821 if (try_implicit_rule (file, depth))
822 DEBUGPR (_("Found an implicit rule for `%s'.\n"));
823 else
824 DEBUGPR (_("No implicit rule found for `%s'.\n"));
825 file->tried_implicit = 1;
827 if (file->cmds == 0 && !file->is_target
828 && default_file != 0 && default_file->cmds != 0)
830 DEBUGPR (_("Using default commands for `%s'.\n"));
831 file->cmds = default_file->cmds;
834 /* If the intermediate file actually exists
835 and is newer, then we should remake from it. */
836 check_renamed (file);
837 mtime = file_mtime (file);
838 check_renamed (file);
839 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
840 *must_make_ptr = 1;
841 /* Otherwise, update all non-intermediate files we depend on,
842 if necessary, and see whether any of them is more
843 recent than the file on whose behalf we are checking. */
844 else
846 register struct dep *lastd;
848 lastd = 0;
849 d = file->deps;
850 while (d != 0)
852 if (d->file->updating)
854 error (NILF, _("Circular %s <- %s dependency dropped."),
855 file->name, d->file->name);
856 if (lastd == 0)
858 file->deps = d->next;
859 free ((char *) d);
860 d = file->deps;
862 else
864 lastd->next = d->next;
865 free ((char *) d);
866 d = lastd->next;
868 continue;
871 d->file->parent = file;
872 dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
873 check_renamed (d->file);
874 if (dep_status != 0 && !keep_going_flag)
875 break;
877 if (d->file->command_state == cs_running
878 || d->file->command_state == cs_deps_running)
879 /* Record that some of FILE's deps are still being made.
880 This tells the upper levels to wait on processing it until
881 the commands are finished. */
882 set_command_state (file, cs_deps_running);
884 lastd = d;
885 d = d->next;
890 file->updating = 0;
891 return dep_status;
894 /* Touch FILE. Return zero if successful, one if not. */
896 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
898 static int
899 touch_file (file)
900 register struct file *file;
902 if (!silent_flag)
903 message (0, "touch %s", file->name);
905 #ifndef NO_ARCHIVES
906 if (ar_name (file->name))
907 return ar_touch (file->name);
908 else
909 #endif
911 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
913 if (fd < 0)
914 TOUCH_ERROR ("touch: open: ");
915 else
917 struct stat statbuf;
918 char buf;
919 int status;
922 status = fstat (fd, &statbuf);
923 while (status < 0 && EINTR_SET);
925 if (status < 0)
926 TOUCH_ERROR ("touch: fstat: ");
927 /* Rewrite character 0 same as it already is. */
928 if (read (fd, &buf, 1) < 0)
929 TOUCH_ERROR ("touch: read: ");
930 if (lseek (fd, 0L, 0) < 0L)
931 TOUCH_ERROR ("touch: lseek: ");
932 if (write (fd, &buf, 1) < 0)
933 TOUCH_ERROR ("touch: write: ");
934 /* If file length was 0, we just
935 changed it, so change it back. */
936 if (statbuf.st_size == 0)
938 (void) close (fd);
939 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
940 if (fd < 0)
941 TOUCH_ERROR ("touch: open: ");
943 (void) close (fd);
947 return 0;
950 /* Having checked and updated the dependencies of FILE,
951 do whatever is appropriate to remake FILE itself.
952 Return the status from executing FILE's commands. */
954 static void
955 remake_file (file)
956 struct file *file;
958 if (file->cmds == 0)
960 if (file->phony)
961 /* Phony target. Pretend it succeeded. */
962 file->update_status = 0;
963 else if (file->is_target)
964 /* This is a nonexistent target file we cannot make.
965 Pretend it was successfully remade. */
966 file->update_status = 0;
967 else
968 no_rule_error(file);
970 else
972 chop_commands (file->cmds);
974 if (!touch_flag || file->cmds->any_recurse)
976 execute_file_commands (file);
977 return;
979 else
980 /* This tells notice_finished_file it is ok to touch the file. */
981 file->update_status = 0;
984 /* This does the touching under -t. */
985 notice_finished_file (file);
988 /* Return the mtime of a file, given a `struct file'.
989 Caches the time in the struct file to avoid excess stat calls.
991 If the file is not found, and SEARCH is nonzero, VPATH searching and
992 replacement is done. If that fails, a library (-lLIBNAME) is tried and
993 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
994 FILE. */
996 FILE_TIMESTAMP
997 f_mtime (file, search)
998 register struct file *file;
999 int search;
1001 FILE_TIMESTAMP mtime;
1003 /* File's mtime is not known; must get it from the system. */
1005 #ifndef NO_ARCHIVES
1006 if (ar_name (file->name))
1008 /* This file is an archive-member reference. */
1010 char *arname, *memname;
1011 struct file *arfile;
1012 int arname_used = 0;
1014 /* Find the archive's name. */
1015 ar_parse_name (file->name, &arname, &memname);
1017 /* Find the modification time of the archive itself.
1018 Also allow for its name to be changed via VPATH search. */
1019 arfile = lookup_file (arname);
1020 if (arfile == 0)
1022 arfile = enter_file (arname);
1023 arname_used = 1;
1025 mtime = f_mtime (arfile, search);
1026 check_renamed (arfile);
1027 if (search && strcmp (arfile->hname, arname))
1029 /* The archive's name has changed.
1030 Change the archive-member reference accordingly. */
1032 char *name;
1033 unsigned int arlen, memlen;
1035 if (!arname_used)
1037 free (arname);
1038 arname_used = 1;
1041 arname = arfile->hname;
1042 arlen = strlen (arname);
1043 memlen = strlen (memname);
1045 /* free (file->name); */
1047 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1048 bcopy (arname, name, arlen);
1049 name[arlen] = '(';
1050 bcopy (memname, name + arlen + 1, memlen);
1051 name[arlen + 1 + memlen] = ')';
1052 name[arlen + 1 + memlen + 1] = '\0';
1054 /* If the archive was found with GPATH, make the change permanent;
1055 otherwise defer it until later. */
1056 if (arfile->name == arfile->hname)
1057 rename_file (file, name);
1058 else
1059 rehash_file (file, name);
1060 check_renamed (file);
1063 if (!arname_used)
1064 free (arname);
1065 free (memname);
1067 if (mtime == (FILE_TIMESTAMP) -1)
1068 /* The archive doesn't exist, so it's members don't exist either. */
1069 return (FILE_TIMESTAMP) -1;
1071 mtime = ar_member_date (file->hname);
1073 else
1074 #endif
1076 mtime = name_mtime (file->name);
1078 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1080 /* If name_mtime failed, search VPATH. */
1081 char *name = file->name;
1082 if (vpath_search (&name, &mtime)
1083 /* Last resort, is it a library (-lxxx)? */
1084 || (name[0] == '-' && name[1] == 'l'
1085 && library_search (&name, &mtime)))
1087 if (mtime != 0)
1088 /* vpath_search and library_search store zero in MTIME
1089 if they didn't need to do a stat call for their work. */
1090 file->last_mtime = mtime;
1092 /* If we found it in VPATH, see if it's in GPATH too; if so,
1093 change the name right now; if not, defer until after the
1094 dependencies are updated. */
1095 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1097 rename_file (file, name);
1098 check_renamed (file);
1099 return file_mtime (file);
1102 rehash_file (file, name);
1103 check_renamed (file);
1104 mtime = name_mtime (name);
1110 /* Files can have bogus timestamps that nothing newly made will be
1111 "newer" than. Updating their dependents could just result in loops.
1112 So notify the user of the anomaly with a warning.
1114 We only need to do this once, for now. */
1116 static FILE_TIMESTAMP now = 0;
1117 if (!clock_skew_detected
1118 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1119 && !file->updated)
1121 /* This file's time appears to be in the future.
1122 Update our concept of the present, and compare again. */
1124 now = file_timestamp_now ();
1126 #ifdef WINDOWS32
1128 * FAT filesystems round time to nearest even second(!). Just
1129 * allow for any file (NTFS or FAT) to perhaps suffer from this
1130 * braindamage.
1132 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1133 #else
1134 #ifdef __MSDOS__
1135 /* Scrupulous testing indicates that some Windows
1136 filesystems can set file times up to 3 sec into the future! */
1137 if (mtime > now + 3)
1138 #else
1139 if (mtime > now)
1140 #endif
1141 #endif
1143 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1144 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1146 file_timestamp_sprintf (mtimebuf, mtime);
1147 file_timestamp_sprintf (nowbuf, now);
1148 error (NILF, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1149 file->name, mtimebuf, nowbuf);
1150 clock_skew_detected = 1;
1155 /* Store the mtime into all the entries for this file. */
1156 if (file->double_colon)
1157 file = file->double_colon;
1161 /* If this file is not implicit but it is intermediate then it was
1162 made so by the .INTERMEDIATE target. If this file has never
1163 been built by us but was found now, it existed before make
1164 started. So, turn off the intermediate bit so make doesn't
1165 delete it, since it didn't create it. */
1166 if (mtime != (FILE_TIMESTAMP)-1 && file->command_state == cs_not_started
1167 && !file->tried_implicit && file->intermediate)
1168 file->intermediate = 0;
1170 file->last_mtime = mtime;
1171 file = file->prev;
1173 while (file != 0);
1175 return mtime;
1179 /* Return the mtime of the file or archive-member reference NAME. */
1181 static FILE_TIMESTAMP
1182 name_mtime (name)
1183 register char *name;
1185 struct stat st;
1187 if (stat (name, &st) < 0)
1188 return (FILE_TIMESTAMP) -1;
1190 return FILE_TIMESTAMP_STAT_MODTIME (st);
1194 /* Search for a library file specified as -lLIBNAME, searching for a
1195 suitable library file in the system library directories and the VPATH
1196 directories. */
1198 static int
1199 library_search (lib, mtime_ptr)
1200 char **lib;
1201 FILE_TIMESTAMP *mtime_ptr;
1203 static char *dirs[] =
1205 #ifndef _AMIGA
1206 "/lib",
1207 "/usr/lib",
1208 #endif
1209 #if defined(WINDOWS32) && !defined(LIBDIR)
1211 * This is completely up to the user at product install time. Just define
1212 * a placeholder.
1214 #define LIBDIR "."
1215 #endif
1216 LIBDIR, /* Defined by configuration. */
1220 static char *libpatterns = NULL;
1222 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1223 FILE_TIMESTAMP mtime;
1225 /* Loop variables for the libpatterns value. */
1226 char *p, *p2;
1227 int len;
1229 char *file, **dp;
1231 /* If we don't have libpatterns, get it. */
1232 if (!libpatterns)
1234 int save = warn_undefined_variables_flag;
1235 warn_undefined_variables_flag = 0;
1237 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1239 warn_undefined_variables_flag = save;
1242 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1243 p2 = libpatterns;
1244 while ((p = find_next_token (&p2, &len)) != 0)
1246 static char *buf = NULL;
1247 static int buflen = 0;
1248 static int libdir_maxlen = -1;
1249 char *libbuf = variable_expand ("");
1251 /* Expand the pattern using LIBNAME as a replacement. */
1253 char c = p[len];
1254 char *p3, *p4;
1256 p[len] = '\0';
1257 p3 = find_percent (p);
1258 if (!p3)
1260 /* Give a warning if there is no pattern, then remove the
1261 pattern so it's ignored next time. */
1262 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1263 for (; len; --len, ++p)
1264 *p = ' ';
1265 *p = c;
1266 continue;
1268 p4 = variable_buffer_output (libbuf, p, p3-p);
1269 p4 = variable_buffer_output (p4, libname, strlen (libname));
1270 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1271 p[len] = c;
1274 /* Look first for `libNAME.a' in the current directory. */
1275 mtime = name_mtime (libbuf);
1276 if (mtime != (FILE_TIMESTAMP) -1)
1278 *lib = xstrdup (libbuf);
1279 if (mtime_ptr != 0)
1280 *mtime_ptr = mtime;
1281 return 1;
1284 /* Now try VPATH search on that. */
1286 file = libbuf;
1287 if (vpath_search (&file, mtime_ptr))
1289 *lib = file;
1290 return 1;
1293 /* Now try the standard set of directories. */
1295 if (!buflen)
1297 for (dp = dirs; *dp != 0; ++dp)
1299 int l = strlen (*dp);
1300 if (l > libdir_maxlen)
1301 libdir_maxlen = l;
1303 buflen = strlen (libbuf);
1304 buf = xmalloc(libdir_maxlen + buflen + 2);
1306 else if (buflen < strlen (libbuf))
1308 buflen = strlen (libbuf);
1309 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1312 for (dp = dirs; *dp != 0; ++dp)
1314 sprintf (buf, "%s/%s", *dp, libbuf);
1315 mtime = name_mtime (buf);
1316 if (mtime != (FILE_TIMESTAMP) -1)
1318 *lib = xstrdup (buf);
1319 if (mtime_ptr != 0)
1320 *mtime_ptr = mtime;
1321 return 1;
1326 return 0;