If a file declared .INTERMEDIATE already exists before make starts, we
[make.git] / remake.c
blob674f7efe5bd48fcf1502dde7b8694a2c05b4de42
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 __MSDOS__
36 #include "variable.h"
37 #endif
39 #ifdef VMS
40 #include <starlet.h>
41 #endif
42 #ifdef WINDOWS32
43 #include <io.h>
44 #endif
46 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
49 /* Incremented when a command is started (under -n, when one would be). */
50 unsigned int commands_started = 0;
52 static int update_file PARAMS ((struct file *file, unsigned int depth));
53 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
54 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
55 static int touch_file PARAMS ((struct file *file));
56 static void remake_file PARAMS ((struct file *file));
57 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
58 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
61 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
62 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
63 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
64 be disabled for them unless they were also command-line targets, and we
65 should only make one goal at a time and return as soon as one goal whose
66 `changed' member is nonzero is successfully made. */
68 int
69 update_goal_chain (goals, makefiles)
70 register struct dep *goals;
71 int makefiles;
73 int t = touch_flag, q = question_flag, n = just_print_flag;
74 unsigned int j = job_slots;
75 int status = -1;
77 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
78 : file_mtime (file))
80 /* Duplicate the chain so we can remove things from it. */
82 goals = copy_dep_chain (goals);
85 /* Clear the `changed' flag of each goal in the chain.
86 We will use the flag below to notice when any commands
87 have actually been run for a target. When no commands
88 have been run, we give an "up to date" diagnostic. */
90 struct dep *g;
91 for (g = goals; g != 0; g = g->next)
92 g->changed = 0;
95 #if 0
96 /* Only run one job at a time when building makefiles.
97 No one seems to know why this was done, and no one can think of a good
98 reason to do it. Hopefully an obvious one won't appear as soon as we
99 release the next version :-/. */
100 if (makefiles)
101 job_slots = 1;
102 #endif
104 /* Update all the goals until they are all finished. */
106 while (goals != 0)
108 register struct dep *g, *lastgoal;
110 /* Start jobs that are waiting for the load to go down. */
112 start_waiting_jobs ();
114 /* Wait for a child to die. */
116 reap_children (1, 0);
118 lastgoal = 0;
119 g = goals;
120 while (g != 0)
122 /* Iterate over all double-colon entries for this file. */
123 struct file *file = g->file;
124 int stop = 0, any_not_updated = 0;
126 for (file = g->file->double_colon ? g->file->double_colon : g->file;
127 file != NULL;
128 file = file->prev)
130 unsigned int ocommands_started;
131 int x;
132 FILE_TIMESTAMP mtime = MTIME (file);
133 check_renamed (file);
134 if (makefiles)
136 if (file->cmd_target)
138 touch_flag = t;
139 question_flag = q;
140 just_print_flag = n;
142 else
143 touch_flag = question_flag = just_print_flag = 0;
146 /* Save the old value of `commands_started' so we can compare
147 later. It will be incremented when any commands are
148 actually run. */
149 ocommands_started = commands_started;
151 x = update_file (file, makefiles ? 1 : 0);
152 check_renamed (file);
154 /* Set the goal's `changed' flag if any commands were started
155 by calling update_file above. We check this flag below to
156 decide when to give an "up to date" diagnostic. */
157 g->changed += commands_started - ocommands_started;
159 stop = 0;
160 if (x != 0 || file->updated)
162 /* If STATUS was not already 1, set it to 1 if
163 updating failed, or to 0 if updating succeeded.
164 Leave STATUS as it is if no updating was done. */
166 if (status < 1)
168 if (file->update_status != 0)
170 /* Updating failed, or -q triggered.
171 The STATUS value tells our caller which. */
172 status = file->update_status;
173 /* If -q just triggered, stop immediately.
174 It doesn't matter how much more we run,
175 since we already know the answer to return. */
176 stop = (!keep_going_flag && !question_flag
177 && !makefiles);
179 else if (MTIME (file) != mtime)
181 /* Updating was done. If this is a makefile and
182 just_print_flag or question_flag is set
183 (meaning -n or -q was given and this file was
184 specified as a command-line target), don't
185 change STATUS. If STATUS is changed, we will
186 get re-exec'd, and fall into an infinite loop. */
187 if (!makefiles
188 || (!just_print_flag && !question_flag))
189 status = 0;
190 if (makefiles && file->dontcare)
191 /* This is a default makefile. Stop remaking. */
192 stop = 1;
197 /* Keep track if any double-colon entry is not finished.
198 When they are all finished, the goal is finished. */
199 any_not_updated |= !file->updated;
201 if (stop)
202 break;
205 /* Reset FILE since it is null at the end of the loop. */
206 file = g->file;
208 if (stop || !any_not_updated)
210 /* If we have found nothing whatever to do for the goal,
211 print a message saying nothing needs doing. */
213 if (!makefiles
214 /* If the update_status is zero, we updated successfully
215 or not at all. G->changed will have been set above if
216 any commands were actually started for this goal. */
217 && file->update_status == 0 && !g->changed
218 /* Never give a message under -s or -q. */
219 && !silent_flag && !question_flag)
220 message (1, ((file->phony || file->cmds == 0)
221 ? "Nothing to be done for `%s'."
222 : "`%s' is up to date."),
223 file->name);
225 /* This goal is finished. Remove it from the chain. */
226 if (lastgoal == 0)
227 goals = g->next;
228 else
229 lastgoal->next = g->next;
231 /* Free the storage. */
232 free ((char *) g);
234 g = lastgoal == 0 ? goals : lastgoal->next;
236 if (stop)
237 break;
239 else
241 lastgoal = g;
242 g = g->next;
247 if (makefiles)
249 touch_flag = t;
250 question_flag = q;
251 just_print_flag = n;
252 job_slots = j;
254 return status;
257 /* Generate an error/fatal message if no rules are available for the target.
259 static void
260 no_rule_error(file)
261 struct file *file;
263 static const char msg_noparent[]
264 = "%sNo rule to make target `%s'%s";
265 static const char msg_parent[]
266 = "%sNo rule to make target `%s', needed by `%s'%s";
267 if (keep_going_flag || file->dontcare)
269 if (!file->dontcare)
271 if (file->parent == 0)
272 error (NILF, msg_noparent, "*** ", file->name, ".");
273 else
274 error (NILF, msg_parent, "*** ",
275 file->name, file->parent->name, ".");
276 file->shownerror = 1;
278 file->update_status = 2;
280 else
282 if (file->parent == 0)
283 fatal (NILF, msg_noparent, "", file->name, "");
284 else
285 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
289 /* If FILE is not up to date, execute the commands for it.
290 Return 0 if successful, 1 if unsuccessful;
291 but with some flag settings, just call `exit' if unsuccessful.
293 DEPTH is the depth in recursions of this function.
294 We increment it during the consideration of our dependencies,
295 then decrement it again after finding out whether this file
296 is out of date.
298 If there are multiple double-colon entries for FILE,
299 each is considered in turn. */
301 static int
302 update_file (file, depth)
303 struct file *file;
304 unsigned int depth;
306 register int status = 0;
307 register struct file *f;
309 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
311 status |= update_file_1 (f, depth);
312 check_renamed (f);
314 if (status != 0 && !keep_going_flag)
315 return status;
317 switch (f->command_state)
319 case cs_finished:
320 /* The file is done being remade. */
321 break;
323 case cs_running:
324 case cs_deps_running:
325 /* Don't run the other :: rules for this
326 file until this rule is finished. */
327 return 0;
329 default:
330 assert (f->command_state == cs_running);
331 break;
335 return status;
338 /* Consider a single `struct file' and update it as appropriate. */
340 static int
341 update_file_1 (file, depth)
342 struct file *file;
343 unsigned int depth;
345 register FILE_TIMESTAMP this_mtime;
346 int noexist, must_make, deps_changed;
347 int dep_status = 0;
348 register struct dep *d, *lastd;
349 int running = 0;
351 DEBUGPR ("Considering target file `%s'.\n");
353 if (file->updated)
355 if (file->update_status > 0)
357 DEBUGPR ("Recently tried and failed to update file `%s'.\n");
358 if (!file->shownerror)
360 int dontcare = file->dontcare;
361 file->dontcare = 0;
362 no_rule_error(file);
363 file->dontcare = dontcare;
365 return file->update_status;
368 DEBUGPR ("File `%s' was considered already.\n");
369 return 0;
372 switch (file->command_state)
374 case cs_not_started:
375 case cs_deps_running:
376 break;
377 case cs_running:
378 DEBUGPR ("Still updating file `%s'.\n");
379 return 0;
380 case cs_finished:
381 DEBUGPR ("Finished updating file `%s'.\n");
382 return file->update_status;
383 default:
384 abort ();
387 ++depth;
389 /* Notice recursive update of the same file. */
390 file->updating = 1;
392 /* Looking at the file's modtime beforehand allows the possibility
393 that its name may be changed by a VPATH search, and thus it may
394 not need an implicit rule. If this were not done, the file
395 might get implicit commands that apply to its initial name, only
396 to have that name replaced with another found by VPATH search. */
398 this_mtime = file_mtime (file);
399 check_renamed (file);
400 noexist = this_mtime == (FILE_TIMESTAMP) -1;
401 if (noexist)
402 DEBUGPR ("File `%s' does not exist.\n");
404 must_make = noexist;
406 /* If file was specified as a target with no commands,
407 come up with some default commands. */
409 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
411 if (try_implicit_rule (file, depth))
412 DEBUGPR ("Found an implicit rule for `%s'.\n");
413 else
414 DEBUGPR ("No implicit rule found for `%s'.\n");
415 file->tried_implicit = 1;
417 if (file->cmds == 0 && !file->is_target
418 && default_file != 0 && default_file->cmds != 0)
420 DEBUGPR ("Using default commands for `%s'.\n");
421 file->cmds = default_file->cmds;
424 /* Update all non-intermediate files we depend on, if necessary,
425 and see whether any of them is more recent than this file. */
427 lastd = 0;
428 d = file->deps;
429 while (d != 0)
431 FILE_TIMESTAMP mtime;
433 check_renamed (d->file);
435 mtime = file_mtime (d->file);
436 check_renamed (d->file);
438 if (d->file->updating)
440 error (NILF, "Circular %s <- %s dependency dropped.",
441 file->name, d->file->name);
442 /* We cannot free D here because our the caller will still have
443 a reference to it when we were called recursively via
444 check_dep below. */
445 if (lastd == 0)
446 file->deps = d->next;
447 else
448 lastd->next = d->next;
449 d = d->next;
450 continue;
453 d->file->parent = file;
454 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
455 check_renamed (d->file);
458 register struct file *f = d->file;
459 if (f->double_colon)
460 f = f->double_colon;
463 running |= (f->command_state == cs_running
464 || f->command_state == cs_deps_running);
465 f = f->prev;
467 while (f != 0);
470 if (dep_status != 0 && !keep_going_flag)
471 break;
473 if (!running)
474 d->changed = file_mtime (d->file) != mtime;
476 lastd = d;
477 d = d->next;
480 /* Now we know whether this target needs updating.
481 If it does, update all the intermediate files we depend on. */
483 if (must_make)
485 for (d = file->deps; d != 0; d = d->next)
486 if (d->file->intermediate)
488 FILE_TIMESTAMP mtime = file_mtime (d->file);
489 check_renamed (d->file);
490 d->file->parent = file;
491 dep_status |= update_file (d->file, depth);
492 check_renamed (d->file);
495 register struct file *f = d->file;
496 if (f->double_colon)
497 f = f->double_colon;
500 running |= (f->command_state == cs_running
501 || f->command_state == cs_deps_running);
502 f = f->prev;
504 while (f != 0);
507 if (dep_status != 0 && !keep_going_flag)
508 break;
510 if (!running)
511 d->changed = ((file->phony && file->cmds != 0)
512 || file_mtime (d->file) != mtime);
516 file->updating = 0;
518 DEBUGPR ("Finished dependencies of target file `%s'.\n");
520 if (running)
522 set_command_state (file, cs_deps_running);
523 --depth;
524 DEBUGPR ("The dependencies of `%s' are being made.\n");
525 return 0;
528 /* If any dependency failed, give up now. */
530 if (dep_status != 0)
532 file->update_status = dep_status;
533 notice_finished_file (file);
535 depth--;
537 DEBUGPR ("Giving up on target file `%s'.\n");
539 if (depth == 0 && keep_going_flag
540 && !just_print_flag && !question_flag)
541 error (NILF, "Target `%s' not remade because of errors.", file->name);
543 return dep_status;
546 if (file->command_state == cs_deps_running)
547 /* The commands for some deps were running on the last iteration, but
548 they have finished now. Reset the command_state to not_started to
549 simplify later bookkeeping. It is important that we do this only
550 when the prior state was cs_deps_running, because that prior state
551 was definitely propagated to FILE's also_make's by set_command_state
552 (called above), but in another state an also_make may have
553 independently changed to finished state, and we would confuse that
554 file's bookkeeping (updated, but not_started is bogus state). */
555 set_command_state (file, cs_not_started);
557 /* Now record which dependencies are more
558 recent than this file, so we can define $?. */
560 deps_changed = 0;
561 for (d = file->deps; d != 0; d = d->next)
563 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
564 check_renamed (d->file);
566 #if 1 /* %%% In version 4, remove this code completely to
567 implement not remaking deps if their deps are newer
568 than their parents. */
569 if (d_mtime == (FILE_TIMESTAMP) -1 && !d->file->intermediate)
570 /* We must remake if this dep does not
571 exist and is not intermediate. */
572 must_make = 1;
573 #endif
575 /* Set DEPS_CHANGED if this dep actually changed. */
576 deps_changed |= d->changed;
578 /* Set D->changed if either this dep actually changed,
579 or its dependent, FILE, is older or does not exist. */
580 d->changed |= noexist || d_mtime > this_mtime;
582 if (debug_flag && !noexist)
584 print_spaces (depth);
585 if (d_mtime == (FILE_TIMESTAMP) -1)
586 printf ("Dependency `%s' does not exist.\n", dep_name (d));
587 else
588 printf ("Dependency `%s' is %s than dependent `%s'.\n",
589 dep_name (d), d->changed ? "newer" : "older", file->name);
590 fflush (stdout);
594 /* Here depth returns to the value it had when we were called. */
595 depth--;
597 if (file->double_colon && file->deps == 0)
599 must_make = 1;
600 DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
602 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
604 must_make = 0;
605 DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
608 if (!must_make)
610 if (debug_flag)
612 print_spaces(depth);
613 printf("No need to remake target `%s'", file->name);
614 if (!streq(file->name, file->hname))
615 printf("; using VPATH name `%s'", file->hname);
616 printf(".\n");
617 fflush(stdout);
620 notice_finished_file (file);
622 /* Since we don't need to remake the file, convert it to use the
623 VPATH filename if we found one. hfile will be either the
624 local name if no VPATH or the VPATH name if one was found. */
626 while (file)
628 file->name = file->hname;
629 file = file->prev;
632 return 0;
635 DEBUGPR ("Must remake target `%s'.\n");
637 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
638 VPATH. */
639 if (!streq(file->name, file->hname))
641 if (debug_flag)
643 print_spaces (depth);
644 printf(" Ignoring VPATH name `%s'.\n", file->hname);
645 fflush(stdout);
647 file->ignore_vpath = 1;
650 /* Now, take appropriate actions to remake the file. */
651 remake_file (file);
653 if (file->command_state != cs_finished)
655 DEBUGPR ("Commands of `%s' are being run.\n");
656 return 0;
659 switch (file->update_status)
661 case 2:
662 DEBUGPR ("Failed to remake target file `%s'.\n");
663 break;
664 case 0:
665 DEBUGPR ("Successfully remade target file `%s'.\n");
666 break;
667 case 1:
668 DEBUGPR ("Target file `%s' needs remade under -q.\n");
669 break;
670 default:
671 assert (file->update_status >= 0 && file->update_status <= 2);
672 break;
675 file->updated = 1;
676 return file->update_status;
679 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
680 files listed in its `also_make' member. Under -t, this function also
681 touches FILE.
683 On return, FILE->update_status will no longer be -1 if it was. */
685 void
686 notice_finished_file (file)
687 register struct file *file;
689 struct dep *d;
690 int ran = file->command_state == cs_running;
692 file->command_state = cs_finished;
693 file->updated = 1;
695 if (touch_flag
696 /* The update status will be:
697 -1 if this target was not remade;
698 0 if 0 or more commands (+ or ${MAKE}) were run and won;
699 1 if some commands were run and lost.
700 We touch the target if it has commands which either were not run
701 or won when they ran (i.e. status is 0). */
702 && file->update_status == 0)
704 if (file->cmds != 0 && file->cmds->any_recurse)
706 /* If all the command lines were recursive,
707 we don't want to do the touching. */
708 unsigned int i;
709 for (i = 0; i < file->cmds->ncommand_lines; ++i)
710 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
711 goto have_nonrecursing;
713 else
715 have_nonrecursing:
716 if (file->phony)
717 file->update_status = 0;
718 else
719 /* Should set file's modification date and do nothing else. */
720 file->update_status = touch_file (file);
724 if (ran && !file->phony)
726 struct file *f;
728 if (just_print_flag || question_flag
729 || (file->is_target && file->cmds == 0))
730 file->last_mtime = NEW_MTIME;
731 else
732 file->last_mtime = 0;
734 /* Propagate the change of modification time to all the double-colon
735 entries for this file. */
736 for (f = file->double_colon; f != 0; f = f->next)
737 f->last_mtime = file->last_mtime;
740 if (ran && file->update_status != -1)
741 /* We actually tried to update FILE, which has
742 updated its also_make's as well (if it worked).
743 If it didn't work, it wouldn't work again for them.
744 So mark them as updated with the same status. */
745 for (d = file->also_make; d != 0; d = d->next)
747 d->file->command_state = cs_finished;
748 d->file->updated = 1;
749 d->file->update_status = file->update_status;
751 if (ran && !d->file->phony)
752 /* Fetch the new modification time.
753 We do this instead of just invalidating the cached time
754 so that a vpath_search can happen. Otherwise, it would
755 never be done because the target is already updated. */
756 (void) f_mtime (d->file, 0);
758 else if (file->update_status == -1)
759 /* Nothing was done for FILE, but it needed nothing done.
760 So mark it now as "succeeded". */
761 file->update_status = 0;
764 /* Check whether another file (whose mtime is THIS_MTIME)
765 needs updating on account of a dependency which is file FILE.
766 If it does, store 1 in *MUST_MAKE_PTR.
767 In the process, update any non-intermediate files
768 that FILE depends on (including FILE itself).
769 Return nonzero if any updating failed. */
771 static int
772 check_dep (file, depth, this_mtime, must_make_ptr)
773 struct file *file;
774 unsigned int depth;
775 FILE_TIMESTAMP this_mtime;
776 int *must_make_ptr;
778 register struct dep *d;
779 int dep_status = 0;
781 ++depth;
782 file->updating = 1;
784 if (!file->intermediate)
785 /* If this is a non-intermediate file, update it and record
786 whether it is newer than THIS_MTIME. */
788 FILE_TIMESTAMP mtime;
789 dep_status = update_file (file, depth);
790 check_renamed (file);
791 mtime = file_mtime (file);
792 check_renamed (file);
793 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
794 *must_make_ptr = 1;
796 else
798 /* FILE is an intermediate file. */
799 FILE_TIMESTAMP mtime;
801 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
803 if (try_implicit_rule (file, depth))
804 DEBUGPR ("Found an implicit rule for `%s'.\n");
805 else
806 DEBUGPR ("No implicit rule found for `%s'.\n");
807 file->tried_implicit = 1;
809 if (file->cmds == 0 && !file->is_target
810 && default_file != 0 && default_file->cmds != 0)
812 DEBUGPR ("Using default commands for `%s'.\n");
813 file->cmds = default_file->cmds;
816 /* If the intermediate file actually exists
817 and is newer, then we should remake from it. */
818 check_renamed (file);
819 mtime = file_mtime (file);
820 check_renamed (file);
821 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
822 *must_make_ptr = 1;
823 /* Otherwise, update all non-intermediate files we depend on,
824 if necessary, and see whether any of them is more
825 recent than the file on whose behalf we are checking. */
826 else
828 register struct dep *lastd;
830 lastd = 0;
831 d = file->deps;
832 while (d != 0)
834 if (d->file->updating)
836 error (NILF, "Circular %s <- %s dependency dropped.",
837 file->name, d->file->name);
838 if (lastd == 0)
840 file->deps = d->next;
841 free ((char *) d);
842 d = file->deps;
844 else
846 lastd->next = d->next;
847 free ((char *) d);
848 d = lastd->next;
850 continue;
853 d->file->parent = file;
854 dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
855 check_renamed (d->file);
856 if (dep_status != 0 && !keep_going_flag)
857 break;
859 if (d->file->command_state == cs_running
860 || d->file->command_state == cs_deps_running)
861 /* Record that some of FILE's deps are still being made.
862 This tells the upper levels to wait on processing it until
863 the commands are finished. */
864 set_command_state (file, cs_deps_running);
866 lastd = d;
867 d = d->next;
872 file->updating = 0;
873 return dep_status;
876 /* Touch FILE. Return zero if successful, one if not. */
878 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
880 static int
881 touch_file (file)
882 register struct file *file;
884 if (!silent_flag)
885 message (0, "touch %s", file->name);
887 #ifndef NO_ARCHIVES
888 if (ar_name (file->name))
889 return ar_touch (file->name);
890 else
891 #endif
893 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
895 if (fd < 0)
896 TOUCH_ERROR ("touch: open: ");
897 else
899 struct stat statbuf;
900 char buf;
901 int status;
903 #ifdef EINTR
905 #endif
906 status = fstat (fd, &statbuf);
907 #ifdef EINTR
908 while (status < 0 && errno == EINTR);
909 #endif
910 if (status < 0)
911 TOUCH_ERROR ("touch: fstat: ");
912 /* Rewrite character 0 same as it already is. */
913 if (read (fd, &buf, 1) < 0)
914 TOUCH_ERROR ("touch: read: ");
915 if (lseek (fd, 0L, 0) < 0L)
916 TOUCH_ERROR ("touch: lseek: ");
917 if (write (fd, &buf, 1) < 0)
918 TOUCH_ERROR ("touch: write: ");
919 /* If file length was 0, we just
920 changed it, so change it back. */
921 if (statbuf.st_size == 0)
923 (void) close (fd);
924 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
925 if (fd < 0)
926 TOUCH_ERROR ("touch: open: ");
928 (void) close (fd);
932 return 0;
935 /* Having checked and updated the dependencies of FILE,
936 do whatever is appropriate to remake FILE itself.
937 Return the status from executing FILE's commands. */
939 static void
940 remake_file (file)
941 struct file *file;
943 if (file->cmds == 0)
945 if (file->phony)
946 /* Phony target. Pretend it succeeded. */
947 file->update_status = 0;
948 else if (file->is_target)
949 /* This is a nonexistent target file we cannot make.
950 Pretend it was successfully remade. */
951 file->update_status = 0;
952 else
953 no_rule_error(file);
955 else
957 chop_commands (file->cmds);
959 if (!touch_flag || file->cmds->any_recurse)
961 execute_file_commands (file);
962 return;
964 else
965 /* This tells notice_finished_file it is ok to touch the file. */
966 file->update_status = 0;
969 /* This does the touching under -t. */
970 notice_finished_file (file);
973 /* Return the mtime of a file, given a `struct file'.
974 Caches the time in the struct file to avoid excess stat calls.
976 If the file is not found, and SEARCH is nonzero, VPATH searching and
977 replacement is done. If that fails, a library (-lLIBNAME) is tried and
978 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
979 FILE. */
981 FILE_TIMESTAMP
982 f_mtime (file, search)
983 register struct file *file;
984 int search;
986 FILE_TIMESTAMP mtime;
988 /* File's mtime is not known; must get it from the system. */
990 #ifndef NO_ARCHIVES
991 if (ar_name (file->name))
993 /* This file is an archive-member reference. */
995 char *arname, *memname;
996 struct file *arfile;
997 int arname_used = 0;
999 /* Find the archive's name. */
1000 ar_parse_name (file->name, &arname, &memname);
1002 /* Find the modification time of the archive itself.
1003 Also allow for its name to be changed via VPATH search. */
1004 arfile = lookup_file (arname);
1005 if (arfile == 0)
1007 arfile = enter_file (arname);
1008 arname_used = 1;
1010 mtime = f_mtime (arfile, search);
1011 check_renamed (arfile);
1012 if (search && strcmp (arfile->hname, arname))
1014 /* The archive's name has changed.
1015 Change the archive-member reference accordingly. */
1017 char *name;
1018 unsigned int arlen, memlen;
1020 if (!arname_used)
1022 free (arname);
1023 arname_used = 1;
1026 arname = arfile->hname;
1027 arlen = strlen (arname);
1028 memlen = strlen (memname);
1030 /* free (file->name); */
1032 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1033 bcopy (arname, name, arlen);
1034 name[arlen] = '(';
1035 bcopy (memname, name + arlen + 1, memlen);
1036 name[arlen + 1 + memlen] = ')';
1037 name[arlen + 1 + memlen + 1] = '\0';
1039 /* If the archive was found with GPATH, make the change permanent;
1040 otherwise defer it until later. */
1041 if (arfile->name == arfile->hname)
1042 rename_file (file, name);
1043 else
1044 rehash_file (file, name);
1045 check_renamed (file);
1048 if (!arname_used)
1049 free (arname);
1050 free (memname);
1052 if (mtime == (FILE_TIMESTAMP) -1)
1053 /* The archive doesn't exist, so it's members don't exist either. */
1054 return (FILE_TIMESTAMP) -1;
1056 mtime = ar_member_date (file->hname);
1058 else
1059 #endif
1061 mtime = name_mtime (file->name);
1063 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1065 /* If name_mtime failed, search VPATH. */
1066 char *name = file->name;
1067 if (vpath_search (&name, &mtime)
1068 /* Last resort, is it a library (-lxxx)? */
1069 || (name[0] == '-' && name[1] == 'l'
1070 && library_search (&name, &mtime)))
1072 if (mtime != 0)
1073 /* vpath_search and library_search store zero in MTIME
1074 if they didn't need to do a stat call for their work. */
1075 file->last_mtime = mtime;
1077 /* If we found it in VPATH, see if it's in GPATH too; if so,
1078 change the name right now; if not, defer until after the
1079 dependencies are updated. */
1080 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1082 rename_file (file, name);
1083 check_renamed (file);
1084 return file_mtime (file);
1087 rehash_file (file, name);
1088 check_renamed (file);
1089 mtime = name_mtime (name);
1095 /* Files can have bogus timestamps that nothing newly made will be
1096 "newer" than. Updating their dependents could just result in loops.
1097 So notify the user of the anomaly with a warning.
1099 We only need to do this once, for now. */
1101 static FILE_TIMESTAMP now = 0;
1102 if (!clock_skew_detected
1103 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1104 && !file->updated)
1106 /* This file's time appears to be in the future.
1107 Update our concept of the present, and compare again. */
1109 now = file_timestamp_now ();
1111 #ifdef WINDOWS32
1113 * FAT filesystems round time to nearest even second(!). Just
1114 * allow for any file (NTFS or FAT) to perhaps suffer from this
1115 * braindamage.
1117 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1118 #else
1119 #ifdef __MSDOS__
1120 /* Scrupulous testing indicates that some Windows
1121 filesystems can set file times up to 3 sec into the future! */
1122 if (mtime > now + 3)
1123 #else
1124 if (mtime > now)
1125 #endif
1126 #endif
1128 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1129 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1131 file_timestamp_sprintf (mtimebuf, mtime);
1132 file_timestamp_sprintf (nowbuf, now);
1133 error (NILF, "*** Warning: File `%s' has modification time in the future (%s > %s)",
1134 file->name, mtimebuf, nowbuf);
1135 clock_skew_detected = 1;
1140 /* Store the mtime into all the entries for this file. */
1141 if (file->double_colon)
1142 file = file->double_colon;
1146 /* If this file is not implicit but it is intermediate then it was
1147 made so by the .INTERMEDIATE target. If this file has never
1148 been built by us but was found now, it existed before make
1149 started. So, turn off the intermediate bit so make doesn't
1150 delete it, since it didn't create it. */
1151 if (mtime != (FILE_TIMESTAMP)-1 && file->command_state == cs_not_started
1152 && !file->tried_implicit && file->intermediate)
1153 file->intermediate = 0;
1155 file->last_mtime = mtime;
1156 file = file->prev;
1158 while (file != 0);
1160 return mtime;
1164 /* Return the mtime of the file or archive-member reference NAME. */
1166 static FILE_TIMESTAMP
1167 name_mtime (name)
1168 register char *name;
1170 struct stat st;
1172 if (stat (name, &st) < 0)
1173 return (FILE_TIMESTAMP) -1;
1175 return FILE_TIMESTAMP_STAT_MODTIME (st);
1179 /* Search for a library file specified as -lLIBNAME, searching for a
1180 suitable library file in the system library directories and the VPATH
1181 directories. */
1183 static int
1184 library_search (lib, mtime_ptr)
1185 char **lib;
1186 FILE_TIMESTAMP *mtime_ptr;
1188 static char *dirs[] =
1190 #ifndef _AMIGA
1191 "/lib",
1192 "/usr/lib",
1193 #endif
1194 #if defined(WINDOWS32) && !defined(LIBDIR)
1196 * This is completely up to the user at product install time. Just define
1197 * a placeholder.
1199 #define LIBDIR "."
1200 #endif
1201 LIBDIR, /* Defined by configuration. */
1205 static char *libpatterns = NULL;
1207 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1208 FILE_TIMESTAMP mtime;
1210 /* Loop variables for the libpatterns value. */
1211 char *p, *p2;
1212 int len;
1214 char *file, **dp;
1216 /* If we don't have libpatterns, get it. */
1217 if (!libpatterns)
1219 int save = warn_undefined_variables_flag;
1220 warn_undefined_variables_flag = 0;
1222 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1224 warn_undefined_variables_flag = save;
1227 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1228 p2 = libpatterns;
1229 while ((p = find_next_token (&p2, &len)) != 0)
1231 static char *buf = NULL;
1232 static int buflen = 0;
1233 static int libdir_maxlen = -1;
1234 char *libbuf = variable_expand ("");
1236 /* Expand the pattern using LIBNAME as a replacement. */
1238 char c = p[len];
1239 char *p3, *p4;
1241 p[len] = '\0';
1242 p3 = find_percent (p);
1243 if (!p3)
1245 /* Give a warning if there is no pattern, then remove the
1246 pattern so it's ignored next time. */
1247 error (NILF, ".LIBPATTERNS element `%s' is not a pattern", p);
1248 for (; len; --len, ++p)
1249 *p = ' ';
1250 *p = c;
1251 continue;
1253 p4 = variable_buffer_output (libbuf, p, p3-p);
1254 p4 = variable_buffer_output (p4, libname, strlen (libname));
1255 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1256 p[len] = c;
1259 /* Look first for `libNAME.a' in the current directory. */
1260 mtime = name_mtime (libbuf);
1261 if (mtime != (FILE_TIMESTAMP) -1)
1263 *lib = xstrdup (libbuf);
1264 if (mtime_ptr != 0)
1265 *mtime_ptr = mtime;
1266 return 1;
1269 /* Now try VPATH search on that. */
1271 file = libbuf;
1272 if (vpath_search (&file, mtime_ptr))
1274 *lib = file;
1275 return 1;
1278 /* Now try the standard set of directories. */
1280 if (!buflen)
1282 for (dp = dirs; *dp != 0; ++dp)
1284 int l = strlen (*dp);
1285 if (l > libdir_maxlen)
1286 libdir_maxlen = l;
1288 buflen = strlen (libbuf);
1289 buf = xmalloc(libdir_maxlen + buflen + 2);
1291 else if (buflen < strlen (libbuf))
1293 buflen = strlen (libbuf);
1294 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1297 for (dp = dirs; *dp != 0; ++dp)
1299 sprintf (buf, "%s/%s", *dp, libbuf);
1300 mtime = name_mtime (buf);
1301 if (mtime != (FILE_TIMESTAMP) -1)
1303 *lib = xstrdup (buf);
1304 if (mtime_ptr != 0)
1305 *mtime_ptr = mtime;
1306 return 1;
1311 return 0;