Checkpoint changes. Bug fixes, mostly.
[make.git] / remake.c
blobd312321edbefe622f71863bd287653957ebe566a
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "filedef.h"
21 #include "job.h"
22 #include "commands.h"
23 #include "dep.h"
24 #include <assert.h>
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #else
29 #include <sys/file.h>
30 #endif
32 #ifdef __MSDOS__
33 #include "variable.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 /* Incremented when a command is started (under -n, when one would be). */
47 unsigned int commands_started = 0;
49 static int update_file PARAMS ((struct file *file, unsigned int depth));
50 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
51 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
52 static int touch_file PARAMS ((struct file *file));
53 static void remake_file PARAMS ((struct file *file));
54 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
55 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
58 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
59 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
60 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
61 be disabled for them unless they were also command-line targets, and we
62 should only make one goal at a time and return as soon as one goal whose
63 `changed' member is nonzero is successfully made. */
65 int
66 update_goal_chain (goals, makefiles)
67 register struct dep *goals;
68 int makefiles;
70 int t = touch_flag, q = question_flag, n = just_print_flag;
71 unsigned int j = job_slots;
72 int status = -1;
74 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
75 : file_mtime (file))
77 /* Duplicate the chain so we can remove things from it. */
79 goals = copy_dep_chain (goals);
82 /* Clear the `changed' flag of each goal in the chain.
83 We will use the flag below to notice when any commands
84 have actually been run for a target. When no commands
85 have been run, we give an "up to date" diagnostic. */
87 struct dep *g;
88 for (g = goals; g != 0; g = g->next)
89 g->changed = 0;
92 #if 0
93 /* Only run one job at a time when building makefiles.
94 No one seems to know why this was done, and no one can think of a good
95 reason to do it. Hopefully an obvious one won't appear as soon as we
96 release the next version :-/. */
97 if (makefiles)
98 job_slots = 1;
99 #endif
101 /* Update all the goals until they are all finished. */
103 while (goals != 0)
105 register struct dep *g, *lastgoal;
107 /* Start jobs that are waiting for the load to go down. */
109 start_waiting_jobs ();
111 /* Wait for a child to die. */
113 reap_children (1, 0);
115 lastgoal = 0;
116 g = goals;
117 while (g != 0)
119 /* Iterate over all double-colon entries for this file. */
120 struct file *file = g->file;
121 int stop = 0, any_not_updated = 0;
123 for (file = g->file->double_colon ? g->file->double_colon : g->file;
124 file != NULL;
125 file = file->prev)
127 unsigned int ocommands_started;
128 int x;
129 FILE_TIMESTAMP mtime = MTIME (file);
130 check_renamed (file);
131 if (makefiles)
133 if (file->cmd_target)
135 touch_flag = t;
136 question_flag = q;
137 just_print_flag = n;
139 else
140 touch_flag = question_flag = just_print_flag = 0;
143 /* Save the old value of `commands_started' so we can compare
144 later. It will be incremented when any commands are
145 actually run. */
146 ocommands_started = commands_started;
148 x = update_file (file, makefiles ? 1 : 0);
149 check_renamed (file);
151 /* Set the goal's `changed' flag if any commands were started
152 by calling update_file above. We check this flag below to
153 decide when to give an "up to date" diagnostic. */
154 g->changed += commands_started - ocommands_started;
156 stop = 0;
157 if (x != 0 || file->updated)
159 /* If STATUS was not already 1, set it to 1 if
160 updating failed, or to 0 if updating succeeded.
161 Leave STATUS as it is if no updating was done. */
163 if (status < 1)
165 if (file->update_status != 0)
167 /* Updating failed, or -q triggered.
168 The STATUS value tells our caller which. */
169 status = file->update_status;
170 /* If -q just triggered, stop immediately.
171 It doesn't matter how much more we run,
172 since we already know the answer to return. */
173 stop = (!keep_going_flag && !question_flag
174 && !makefiles);
176 else if (MTIME (file) != mtime)
178 /* Updating was done. If this is a makefile and
179 just_print_flag or question_flag is set
180 (meaning -n or -q was given and this file was
181 specified as a command-line target), don't
182 change STATUS. If STATUS is changed, we will
183 get re-exec'd, and fall into an infinite loop. */
184 if (!makefiles
185 || (!just_print_flag && !question_flag))
186 status = 0;
187 if (makefiles && file->dontcare)
188 /* This is a default makefile. Stop remaking. */
189 stop = 1;
194 /* Keep track if any double-colon entry is not finished.
195 When they are all finished, the goal is finished. */
196 any_not_updated |= !file->updated;
198 if (stop)
199 break;
202 /* Reset FILE since it is null at the end of the loop. */
203 file = g->file;
205 if (stop || !any_not_updated)
207 /* If we have found nothing whatever to do for the goal,
208 print a message saying nothing needs doing. */
210 if (!makefiles
211 /* If the update_status is zero, we updated successfully
212 or not at all. G->changed will have been set above if
213 any commands were actually started for this goal. */
214 && file->update_status == 0 && !g->changed
215 /* Never give a message under -s or -q. */
216 && !silent_flag && !question_flag)
217 message (1, ((file->phony || file->cmds == 0)
218 ? "Nothing to be done for `%s'."
219 : "`%s' is up to date."),
220 file->name);
222 /* This goal is finished. Remove it from the chain. */
223 if (lastgoal == 0)
224 goals = g->next;
225 else
226 lastgoal->next = g->next;
228 /* Free the storage. */
229 free ((char *) g);
231 g = lastgoal == 0 ? goals : lastgoal->next;
233 if (stop)
234 break;
236 else
238 lastgoal = g;
239 g = g->next;
244 if (makefiles)
246 touch_flag = t;
247 question_flag = q;
248 just_print_flag = n;
249 job_slots = j;
251 return status;
254 /* Generate an error/fatal message if no rules are available for the target.
256 static void
257 no_rule_error(file)
258 struct file *file;
260 static const char msg_noparent[]
261 = "%sNo rule to make target `%s'%s";
262 static const char msg_parent[]
263 = "%sNo rule to make target `%s', needed by `%s'%s";
264 if (keep_going_flag || file->dontcare)
266 if (!file->dontcare)
268 if (file->parent == 0)
269 error (NILF, msg_noparent, "*** ", file->name, ".");
270 else
271 error (NILF, msg_parent, "*** ",
272 file->name, file->parent->name, ".");
273 file->shownerror = 1;
275 file->update_status = 2;
277 else
279 if (file->parent == 0)
280 fatal (NILF, msg_noparent, "", file->name, "");
281 else
282 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
286 /* If FILE is not up to date, execute the commands for it.
287 Return 0 if successful, 1 if unsuccessful;
288 but with some flag settings, just call `exit' if unsuccessful.
290 DEPTH is the depth in recursions of this function.
291 We increment it during the consideration of our dependencies,
292 then decrement it again after finding out whether this file
293 is out of date.
295 If there are multiple double-colon entries for FILE,
296 each is considered in turn. */
298 static int
299 update_file (file, depth)
300 struct file *file;
301 unsigned int depth;
303 register int status = 0;
304 register struct file *f;
306 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
308 status |= update_file_1 (f, depth);
309 check_renamed (f);
311 if (status != 0 && !keep_going_flag)
312 return status;
314 switch (f->command_state)
316 case cs_finished:
317 /* The file is done being remade. */
318 break;
320 case cs_running:
321 case cs_deps_running:
322 /* Don't run the other :: rules for this
323 file until this rule is finished. */
324 return 0;
326 default:
327 assert (f->command_state == cs_running);
328 break;
332 return status;
335 /* Consider a single `struct file' and update it as appropriate. */
337 static int
338 update_file_1 (file, depth)
339 struct file *file;
340 unsigned int depth;
342 register FILE_TIMESTAMP this_mtime;
343 int noexist, must_make, deps_changed;
344 int dep_status = 0;
345 register struct dep *d, *lastd;
346 int running = 0;
348 DEBUGPR ("Considering target file `%s'.\n");
350 if (file->updated)
352 if (file->update_status > 0)
354 DEBUGPR ("Recently tried and failed to update file `%s'.\n");
355 if (!file->shownerror)
357 int dontcare = file->dontcare;
358 file->dontcare = 0;
359 no_rule_error(file);
360 file->dontcare = dontcare;
362 return file->update_status;
365 DEBUGPR ("File `%s' was considered already.\n");
366 return 0;
369 switch (file->command_state)
371 case cs_not_started:
372 case cs_deps_running:
373 break;
374 case cs_running:
375 DEBUGPR ("Still updating file `%s'.\n");
376 return 0;
377 case cs_finished:
378 DEBUGPR ("Finished updating file `%s'.\n");
379 return file->update_status;
380 default:
381 abort ();
384 ++depth;
386 /* Notice recursive update of the same file. */
387 file->updating = 1;
389 /* Looking at the file's modtime beforehand allows the possibility
390 that its name may be changed by a VPATH search, and thus it may
391 not need an implicit rule. If this were not done, the file
392 might get implicit commands that apply to its initial name, only
393 to have that name replaced with another found by VPATH search. */
395 this_mtime = file_mtime (file);
396 check_renamed (file);
397 noexist = this_mtime == (FILE_TIMESTAMP) -1;
398 if (noexist)
399 DEBUGPR ("File `%s' does not exist.\n");
401 must_make = noexist;
403 /* If file was specified as a target with no commands,
404 come up with some default commands. */
406 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
408 if (try_implicit_rule (file, depth))
409 DEBUGPR ("Found an implicit rule for `%s'.\n");
410 else
411 DEBUGPR ("No implicit rule found for `%s'.\n");
412 file->tried_implicit = 1;
414 if (file->cmds == 0 && !file->is_target
415 && default_file != 0 && default_file->cmds != 0)
417 DEBUGPR ("Using default commands for `%s'.\n");
418 file->cmds = default_file->cmds;
421 /* Update all non-intermediate files we depend on, if necessary,
422 and see whether any of them is more recent than this file. */
424 lastd = 0;
425 d = file->deps;
426 while (d != 0)
428 FILE_TIMESTAMP mtime;
430 check_renamed (d->file);
432 mtime = file_mtime (d->file);
433 check_renamed (d->file);
435 if (d->file->updating)
437 error (NILF, "Circular %s <- %s dependency dropped.",
438 file->name, d->file->name);
439 /* We cannot free D here because our the caller will still have
440 a reference to it when we were called recursively via
441 check_dep below. */
442 if (lastd == 0)
443 file->deps = d->next;
444 else
445 lastd->next = d->next;
446 d = d->next;
447 continue;
450 d->file->parent = file;
451 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
452 check_renamed (d->file);
455 register struct file *f = d->file;
456 if (f->double_colon)
457 f = f->double_colon;
460 running |= (f->command_state == cs_running
461 || f->command_state == cs_deps_running);
462 f = f->prev;
464 while (f != 0);
467 if (dep_status != 0 && !keep_going_flag)
468 break;
470 if (!running)
471 d->changed = file_mtime (d->file) != mtime;
473 lastd = d;
474 d = d->next;
477 /* Now we know whether this target needs updating.
478 If it does, update all the intermediate files we depend on. */
480 if (must_make)
482 for (d = file->deps; d != 0; d = d->next)
483 if (d->file->intermediate)
485 FILE_TIMESTAMP mtime = file_mtime (d->file);
486 check_renamed (d->file);
487 d->file->parent = file;
488 dep_status |= update_file (d->file, depth);
489 check_renamed (d->file);
492 register struct file *f = d->file;
493 if (f->double_colon)
494 f = f->double_colon;
497 running |= (f->command_state == cs_running
498 || f->command_state == cs_deps_running);
499 f = f->prev;
501 while (f != 0);
504 if (dep_status != 0 && !keep_going_flag)
505 break;
507 if (!running)
508 d->changed = ((file->phony && file->cmds != 0)
509 || file_mtime (d->file) != mtime);
513 file->updating = 0;
515 DEBUGPR ("Finished dependencies of target file `%s'.\n");
517 if (running)
519 set_command_state (file, cs_deps_running);
520 --depth;
521 DEBUGPR ("The dependencies of `%s' are being made.\n");
522 return 0;
525 /* If any dependency failed, give up now. */
527 if (dep_status != 0)
529 file->update_status = dep_status;
530 notice_finished_file (file);
532 depth--;
534 DEBUGPR ("Giving up on target file `%s'.\n");
536 if (depth == 0 && keep_going_flag
537 && !just_print_flag && !question_flag)
538 error (NILF, "Target `%s' not remade because of errors.", file->name);
540 return dep_status;
543 if (file->command_state == cs_deps_running)
544 /* The commands for some deps were running on the last iteration, but
545 they have finished now. Reset the command_state to not_started to
546 simplify later bookkeeping. It is important that we do this only
547 when the prior state was cs_deps_running, because that prior state
548 was definitely propagated to FILE's also_make's by set_command_state
549 (called above), but in another state an also_make may have
550 independently changed to finished state, and we would confuse that
551 file's bookkeeping (updated, but not_started is bogus state). */
552 set_command_state (file, cs_not_started);
554 /* Now record which dependencies are more
555 recent than this file, so we can define $?. */
557 deps_changed = 0;
558 for (d = file->deps; d != 0; d = d->next)
560 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
561 check_renamed (d->file);
563 #if 1 /* %%% In version 4, remove this code completely to
564 implement not remaking deps if their deps are newer
565 than their parents. */
566 if (d_mtime == (FILE_TIMESTAMP) -1 && !d->file->intermediate)
567 /* We must remake if this dep does not
568 exist and is not intermediate. */
569 must_make = 1;
570 #endif
572 /* Set DEPS_CHANGED if this dep actually changed. */
573 deps_changed |= d->changed;
575 /* Set D->changed if either this dep actually changed,
576 or its dependent, FILE, is older or does not exist. */
577 d->changed |= noexist || d_mtime > this_mtime;
579 if (debug_flag && !noexist)
581 print_spaces (depth);
582 if (d_mtime == (FILE_TIMESTAMP) -1)
583 printf ("Dependency `%s' does not exist.\n", dep_name (d));
584 else
585 printf ("Dependency `%s' is %s than dependent `%s'.\n",
586 dep_name (d), d->changed ? "newer" : "older", file->name);
587 fflush (stdout);
591 /* Here depth returns to the value it had when we were called. */
592 depth--;
594 if (file->double_colon && file->deps == 0)
596 must_make = 1;
597 DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
599 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
601 must_make = 0;
602 DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
605 if (!must_make)
607 if (debug_flag)
609 print_spaces(depth);
610 printf("No need to remake target `%s'", file->name);
611 if (!streq(file->name, file->hname))
612 printf("; using VPATH name `%s'", file->hname);
613 printf(".\n");
614 fflush(stdout);
617 notice_finished_file (file);
619 /* Since we don't need to remake the file, convert it to use the
620 VPATH filename if we found one. hfile will be either the
621 local name if no VPATH or the VPATH name if one was found. */
623 while (file)
625 file->name = file->hname;
626 file = file->prev;
629 return 0;
632 DEBUGPR ("Must remake target `%s'.\n");
634 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
635 VPATH. */
636 if (!streq(file->name, file->hname))
638 if (debug_flag)
640 print_spaces (depth);
641 printf(" Ignoring VPATH name `%s'.\n", file->hname);
642 fflush(stdout);
644 file->ignore_vpath = 1;
647 /* Now, take appropriate actions to remake the file. */
648 remake_file (file);
650 if (file->command_state != cs_finished)
652 DEBUGPR ("Commands of `%s' are being run.\n");
653 return 0;
656 switch (file->update_status)
658 case 2:
659 DEBUGPR ("Failed to remake target file `%s'.\n");
660 break;
661 case 0:
662 DEBUGPR ("Successfully remade target file `%s'.\n");
663 break;
664 case 1:
665 DEBUGPR ("Target file `%s' needs remade under -q.\n");
666 break;
667 default:
668 assert (file->update_status >= 0 && file->update_status <= 2);
669 break;
672 file->updated = 1;
673 return file->update_status;
676 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
677 files listed in its `also_make' member. Under -t, this function also
678 touches FILE.
680 On return, FILE->update_status will no longer be -1 if it was. */
682 void
683 notice_finished_file (file)
684 register struct file *file;
686 struct dep *d;
687 int ran = file->command_state == cs_running;
689 file->command_state = cs_finished;
690 file->updated = 1;
692 if (touch_flag
693 /* The update status will be:
694 -1 if this target was not remade;
695 0 if 0 or more commands (+ or ${MAKE}) were run and won;
696 1 if some commands were run and lost.
697 We touch the target if it has commands which either were not run
698 or won when they ran (i.e. status is 0). */
699 && file->update_status == 0)
701 if (file->cmds != 0 && file->cmds->any_recurse)
703 /* If all the command lines were recursive,
704 we don't want to do the touching. */
705 unsigned int i;
706 for (i = 0; i < file->cmds->ncommand_lines; ++i)
707 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
708 goto have_nonrecursing;
710 else
712 have_nonrecursing:
713 if (file->phony)
714 file->update_status = 0;
715 else
716 /* Should set file's modification date and do nothing else. */
717 file->update_status = touch_file (file);
721 if (ran && !file->phony)
723 struct file *f;
725 if (just_print_flag || question_flag
726 || (file->is_target && file->cmds == 0))
727 file->last_mtime = NEW_MTIME;
728 else
729 file->last_mtime = 0;
731 /* Propagate the change of modification time to all the double-colon
732 entries for this file. */
733 for (f = file->double_colon; f != 0; f = f->next)
734 f->last_mtime = file->last_mtime;
737 if (ran && file->update_status != -1)
738 /* We actually tried to update FILE, which has
739 updated its also_make's as well (if it worked).
740 If it didn't work, it wouldn't work again for them.
741 So mark them as updated with the same status. */
742 for (d = file->also_make; d != 0; d = d->next)
744 d->file->command_state = cs_finished;
745 d->file->updated = 1;
746 d->file->update_status = file->update_status;
748 if (ran && !d->file->phony)
749 /* Fetch the new modification time.
750 We do this instead of just invalidating the cached time
751 so that a vpath_search can happen. Otherwise, it would
752 never be done because the target is already updated. */
753 (void) f_mtime (d->file, 0);
755 else if (file->update_status == -1)
756 /* Nothing was done for FILE, but it needed nothing done.
757 So mark it now as "succeeded". */
758 file->update_status = 0;
761 /* Check whether another file (whose mtime is THIS_MTIME)
762 needs updating on account of a dependency which is file FILE.
763 If it does, store 1 in *MUST_MAKE_PTR.
764 In the process, update any non-intermediate files
765 that FILE depends on (including FILE itself).
766 Return nonzero if any updating failed. */
768 static int
769 check_dep (file, depth, this_mtime, must_make_ptr)
770 struct file *file;
771 unsigned int depth;
772 FILE_TIMESTAMP this_mtime;
773 int *must_make_ptr;
775 register struct dep *d;
776 int dep_status = 0;
778 ++depth;
779 file->updating = 1;
781 if (!file->intermediate)
782 /* If this is a non-intermediate file, update it and record
783 whether it is newer than THIS_MTIME. */
785 FILE_TIMESTAMP mtime;
786 dep_status = update_file (file, depth);
787 check_renamed (file);
788 mtime = file_mtime (file);
789 check_renamed (file);
790 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
791 *must_make_ptr = 1;
793 else
795 /* FILE is an intermediate file. */
796 FILE_TIMESTAMP mtime;
798 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
800 if (try_implicit_rule (file, depth))
801 DEBUGPR ("Found an implicit rule for `%s'.\n");
802 else
803 DEBUGPR ("No implicit rule found for `%s'.\n");
804 file->tried_implicit = 1;
806 if (file->cmds == 0 && !file->is_target
807 && default_file != 0 && default_file->cmds != 0)
809 DEBUGPR ("Using default commands for `%s'.\n");
810 file->cmds = default_file->cmds;
813 /* If the intermediate file actually exists
814 and is newer, then we should remake from it. */
815 check_renamed (file);
816 mtime = file_mtime (file);
817 check_renamed (file);
818 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
819 *must_make_ptr = 1;
820 /* Otherwise, update all non-intermediate files we depend on,
821 if necessary, and see whether any of them is more
822 recent than the file on whose behalf we are checking. */
823 else
825 register struct dep *lastd;
827 lastd = 0;
828 d = file->deps;
829 while (d != 0)
831 if (d->file->updating)
833 error (NILF, "Circular %s <- %s dependency dropped.",
834 file->name, d->file->name);
835 if (lastd == 0)
837 file->deps = d->next;
838 free ((char *) d);
839 d = file->deps;
841 else
843 lastd->next = d->next;
844 free ((char *) d);
845 d = lastd->next;
847 continue;
850 d->file->parent = file;
851 dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
852 check_renamed (d->file);
853 if (dep_status != 0 && !keep_going_flag)
854 break;
856 if (d->file->command_state == cs_running
857 || d->file->command_state == cs_deps_running)
858 /* Record that some of FILE's deps are still being made.
859 This tells the upper levels to wait on processing it until
860 the commands are finished. */
861 set_command_state (file, cs_deps_running);
863 lastd = d;
864 d = d->next;
869 file->updating = 0;
870 return dep_status;
873 /* Touch FILE. Return zero if successful, one if not. */
875 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
877 static int
878 touch_file (file)
879 register struct file *file;
881 if (!silent_flag)
882 message (0, "touch %s", file->name);
884 #ifndef NO_ARCHIVES
885 if (ar_name (file->name))
886 return ar_touch (file->name);
887 else
888 #endif
890 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
892 if (fd < 0)
893 TOUCH_ERROR ("touch: open: ");
894 else
896 struct stat statbuf;
897 char buf;
898 int status;
900 #ifdef EINTR
902 #endif
903 status = fstat (fd, &statbuf);
904 #ifdef EINTR
905 while (status < 0 && errno == EINTR);
906 #endif
907 if (status < 0)
908 TOUCH_ERROR ("touch: fstat: ");
909 /* Rewrite character 0 same as it already is. */
910 if (read (fd, &buf, 1) < 0)
911 TOUCH_ERROR ("touch: read: ");
912 if (lseek (fd, 0L, 0) < 0L)
913 TOUCH_ERROR ("touch: lseek: ");
914 if (write (fd, &buf, 1) < 0)
915 TOUCH_ERROR ("touch: write: ");
916 /* If file length was 0, we just
917 changed it, so change it back. */
918 if (statbuf.st_size == 0)
920 (void) close (fd);
921 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
922 if (fd < 0)
923 TOUCH_ERROR ("touch: open: ");
925 (void) close (fd);
929 return 0;
932 /* Having checked and updated the dependencies of FILE,
933 do whatever is appropriate to remake FILE itself.
934 Return the status from executing FILE's commands. */
936 static void
937 remake_file (file)
938 struct file *file;
940 if (file->cmds == 0)
942 if (file->phony)
943 /* Phony target. Pretend it succeeded. */
944 file->update_status = 0;
945 else if (file->is_target)
946 /* This is a nonexistent target file we cannot make.
947 Pretend it was successfully remade. */
948 file->update_status = 0;
949 else
950 no_rule_error(file);
952 else
954 chop_commands (file->cmds);
956 if (!touch_flag || file->cmds->any_recurse)
958 execute_file_commands (file);
959 return;
961 else
962 /* This tells notice_finished_file it is ok to touch the file. */
963 file->update_status = 0;
966 /* This does the touching under -t. */
967 notice_finished_file (file);
970 /* Return the mtime of a file, given a `struct file'.
971 Caches the time in the struct file to avoid excess stat calls.
973 If the file is not found, and SEARCH is nonzero, VPATH searching and
974 replacement is done. If that fails, a library (-lLIBNAME) is tried and
975 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
976 FILE. */
978 FILE_TIMESTAMP
979 f_mtime (file, search)
980 register struct file *file;
981 int search;
983 FILE_TIMESTAMP mtime;
985 /* File's mtime is not known; must get it from the system. */
987 #ifndef NO_ARCHIVES
988 if (ar_name (file->name))
990 /* This file is an archive-member reference. */
992 char *arname, *memname;
993 struct file *arfile;
994 int arname_used = 0;
996 /* Find the archive's name. */
997 ar_parse_name (file->name, &arname, &memname);
999 /* Find the modification time of the archive itself.
1000 Also allow for its name to be changed via VPATH search. */
1001 arfile = lookup_file (arname);
1002 if (arfile == 0)
1004 arfile = enter_file (arname);
1005 arname_used = 1;
1007 mtime = f_mtime (arfile, search);
1008 check_renamed (arfile);
1009 if (search && strcmp (arfile->hname, arname))
1011 /* The archive's name has changed.
1012 Change the archive-member reference accordingly. */
1014 char *name;
1015 unsigned int arlen, memlen;
1017 if (!arname_used)
1019 free (arname);
1020 arname_used = 1;
1023 arname = arfile->hname;
1024 arlen = strlen (arname);
1025 memlen = strlen (memname);
1027 /* free (file->name); */
1029 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1030 bcopy (arname, name, arlen);
1031 name[arlen] = '(';
1032 bcopy (memname, name + arlen + 1, memlen);
1033 name[arlen + 1 + memlen] = ')';
1034 name[arlen + 1 + memlen + 1] = '\0';
1036 /* If the archive was found with GPATH, make the change permanent;
1037 otherwise defer it until later. */
1038 if (arfile->name == arfile->hname)
1039 rename_file (file, name);
1040 else
1041 rehash_file (file, name);
1042 check_renamed (file);
1045 if (!arname_used)
1046 free (arname);
1047 free (memname);
1049 if (mtime == (FILE_TIMESTAMP) -1)
1050 /* The archive doesn't exist, so it's members don't exist either. */
1051 return (FILE_TIMESTAMP) -1;
1053 mtime = ar_member_date (file->hname);
1055 else
1056 #endif
1058 mtime = name_mtime (file->name);
1060 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1062 /* If name_mtime failed, search VPATH. */
1063 char *name = file->name;
1064 if (vpath_search (&name, &mtime)
1065 /* Last resort, is it a library (-lxxx)? */
1066 || (name[0] == '-' && name[1] == 'l'
1067 && library_search (&name, &mtime)))
1069 if (mtime != 0)
1070 /* vpath_search and library_search store zero in MTIME
1071 if they didn't need to do a stat call for their work. */
1072 file->last_mtime = mtime;
1074 /* If we found it in VPATH, see if it's in GPATH too; if so,
1075 change the name right now; if not, defer until after the
1076 dependencies are updated. */
1077 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1079 rename_file (file, name);
1080 check_renamed (file);
1081 return file_mtime (file);
1084 rehash_file (file, name);
1085 check_renamed (file);
1086 mtime = name_mtime (name);
1092 /* Files can have bogus timestamps that nothing newly made will be
1093 "newer" than. Updating their dependents could just result in loops.
1094 So notify the user of the anomaly with a warning.
1096 We only need to do this once, for now. */
1098 static FILE_TIMESTAMP now = 0;
1099 if (!clock_skew_detected
1100 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1101 && !file->updated)
1103 /* This file's time appears to be in the future.
1104 Update our concept of the present, and compare again. */
1106 now = file_timestamp_now ();
1108 #ifdef WINDOWS32
1110 * FAT filesystems round time to nearest even second(!). Just
1111 * allow for any file (NTFS or FAT) to perhaps suffer from this
1112 * braindamage.
1114 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1115 #else
1116 #ifdef __MSDOS__
1117 /* Scrupulous testing indicates that some Windows
1118 filesystems can set file times up to 3 sec into the future! */
1119 if (mtime > now + 3)
1120 #else
1121 if (mtime > now)
1122 #endif
1123 #endif
1125 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1126 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1128 file_timestamp_sprintf (mtimebuf, mtime);
1129 file_timestamp_sprintf (nowbuf, now);
1130 error (NILF, "*** Warning: File `%s' has modification time in the future (%s > %s)",
1131 file->name, mtimebuf, nowbuf);
1132 clock_skew_detected = 1;
1137 /* Store the mtime into all the entries for this file. */
1138 if (file->double_colon)
1139 file = file->double_colon;
1142 file->last_mtime = mtime;
1143 file = file->prev;
1144 } while (file != 0);
1146 return mtime;
1150 /* Return the mtime of the file or archive-member reference NAME. */
1152 static FILE_TIMESTAMP
1153 name_mtime (name)
1154 register char *name;
1156 struct stat st;
1158 if (stat (name, &st) < 0)
1159 return (FILE_TIMESTAMP) -1;
1161 return FILE_TIMESTAMP_STAT_MODTIME (st);
1165 /* Search for a library file specified as -lLIBNAME, searching for a
1166 suitable library file in the system library directories and the VPATH
1167 directories. */
1169 static int
1170 library_search (lib, mtime_ptr)
1171 char **lib;
1172 FILE_TIMESTAMP *mtime_ptr;
1174 static char *dirs[] =
1176 #ifndef _AMIGA
1177 "/lib",
1178 "/usr/lib",
1179 #endif
1180 #if defined(WINDOWS32) && !defined(LIBDIR)
1182 * This is completely up to the user at product install time. Just define
1183 * a placeholder.
1185 #define LIBDIR "."
1186 #endif
1187 LIBDIR, /* Defined by configuration. */
1191 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1192 FILE_TIMESTAMP mtime;
1194 /* Buffer to construct possible names in. */
1195 char *buf = xmalloc (sizeof (LIBDIR) + 8 + strlen (libname) + 4 + 2 + 1);
1196 char *file, **dp;
1198 /* Look first for `libNAME.a' in the current directory. */
1200 #ifndef _AMIGA
1201 sprintf (buf, "lib%s.a", libname);
1202 #else
1203 sprintf (buf, "%s.lib", libname);
1204 #endif
1205 mtime = name_mtime (buf);
1206 if (mtime != (FILE_TIMESTAMP) -1)
1208 *lib = buf;
1209 if (mtime_ptr != 0)
1210 *mtime_ptr = mtime;
1211 return 1;
1214 /* Now try VPATH search on that. */
1216 file = buf;
1217 if (vpath_search (&file, mtime_ptr))
1219 free (buf);
1220 *lib = file;
1221 return 1;
1224 /* Now try the standard set of directories. */
1226 #ifdef __MSDOS__
1228 /* The default library directory is at ${DJDIR}/lib. */
1229 struct variable *djdir = lookup_variable ("DJDIR", 5);
1231 if (djdir)
1233 size_t djdir_len = strlen (djdir->value);
1235 if (djdir_len > sizeof(LIBDIR) + 8 + strlen(libname) + 4 + 2)
1236 buf = (char *) xrealloc (djdir_len + 1);
1237 sprintf (buf, "%s/lib/lib%s.a", djdir->value, libname);
1238 mtime = name_mtime (buf);
1239 if (mtime != (FILE_TIMESTAMP) -1)
1241 *lib = buf;
1242 if (mtime_ptr != 0)
1243 *mtime_ptr = mtime;
1244 return 1;
1248 #endif
1250 for (dp = dirs; *dp != 0; ++dp)
1252 #ifndef _AMIGA
1253 sprintf (buf, "%s/lib%s.a", *dp, libname);
1254 #else
1255 sprintf (buf, "%s/%s.lib", *dp, libname);
1256 #endif
1257 mtime = name_mtime (buf);
1258 if (mtime != (FILE_TIMESTAMP) -1)
1260 *lib = buf;
1261 if (mtime_ptr != 0)
1262 *mtime_ptr = mtime;
1263 return 1;
1267 free (buf);
1268 return 0;