(libc-srcdir): Use ../libc.
[make.git] / remake.c
blob8eb6a5d74e7cc47bc0957baa6f0fb4637a59279c
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96 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 VMS
33 #include <starlet.h>
34 #endif
36 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
39 /* Incremented when a command is started (under -n, when one would be). */
40 unsigned int commands_started = 0;
42 static int update_file PARAMS ((struct file *file, unsigned int depth));
43 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
44 static int check_dep PARAMS ((struct file *file, unsigned int depth, time_t this_mtime, int *must_make_ptr));
45 static int touch_file PARAMS ((struct file *file));
46 static void remake_file PARAMS ((struct file *file));
47 static time_t name_mtime PARAMS ((char *name));
48 static int library_search PARAMS ((char **lib, time_t *mtime_ptr));
50 extern time_t f_mtime PARAMS ((struct file *file, int search));
52 #ifdef VMS
53 extern int vms_stat PARAMS ((char *name, struct stat *buf));
54 #endif
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 int
65 update_goal_chain (goals, makefiles)
66 register struct dep *goals;
67 int makefiles;
69 int t = touch_flag, q = question_flag, n = just_print_flag;
70 unsigned int j = job_slots;
71 int status = -1;
73 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
74 : file_mtime (file))
76 /* Duplicate the chain so we can remove things from it. */
78 goals = copy_dep_chain (goals);
81 /* Clear the `changed' flag of each goal in the chain.
82 We will use the flag below to notice when any commands
83 have actually been run for a target. When no commands
84 have been run, we give an "up to date" diagnostic. */
86 struct dep *g;
87 for (g = goals; g != 0; g = g->next)
88 g->changed = 0;
91 if (makefiles)
92 /* Only run one job at a time. */
93 job_slots = 1;
95 /* Update all the goals until they are all finished. */
97 while (goals != 0)
99 register struct dep *g, *lastgoal;
101 /* Start jobs that are waiting for the load to go down. */
103 start_waiting_jobs ();
105 /* Wait for a child to die. */
107 reap_children (1, 0);
109 lastgoal = 0;
110 g = goals;
111 while (g != 0)
113 /* Iterate over all double-colon entries for this file. */
114 struct file *file = g->file;
115 int stop, any_not_updated = 0;
117 for (file = g->file->double_colon ? g->file->double_colon : g->file;
118 file != NULL;
119 file = file->prev)
121 unsigned int ocommands_started;
122 int x;
123 time_t mtime = MTIME (file);
124 check_renamed (file);
125 if (makefiles)
127 if (file->cmd_target)
129 touch_flag = t;
130 question_flag = q;
131 just_print_flag = n;
133 else
134 touch_flag = question_flag = just_print_flag = 0;
137 /* Save the old value of `commands_started' so we can compare
138 later. It will be incremented when any commands are
139 actually run. */
140 ocommands_started = commands_started;
142 x = update_file (file, makefiles ? 1 : 0);
143 check_renamed (file);
145 /* Set the goal's `changed' flag if any commands were started
146 by calling update_file above. We check this flag below to
147 decide when to give an "up to date" diagnostic. */
148 g->changed += commands_started - ocommands_started;
150 stop = 0;
151 if (x != 0 || file->updated)
153 /* If STATUS was not already 1, set it to 1 if
154 updating failed, or to 0 if updating succeeded.
155 Leave STATUS as it is if no updating was done. */
157 if (status < 1)
159 if (file->update_status != 0)
161 /* Updating failed, or -q triggered.
162 The STATUS value tells our caller which. */
163 status = file->update_status;
164 /* If -q just triggered, stop immediately.
165 It doesn't matter how much more we run,
166 since we already know the answer to return. */
167 stop = (!keep_going_flag && !question_flag
168 && !makefiles);
170 else if (MTIME (file) != mtime)
172 /* Updating was done. If this is a makefile and
173 just_print_flag or question_flag is set
174 (meaning -n or -q was given and this file was
175 specified as a command-line target), don't
176 change STATUS. If STATUS is changed, we will
177 get re-exec'd, and fall into an infinite loop. */
178 if (!makefiles
179 || (!just_print_flag && !question_flag))
180 status = 0;
181 if (makefiles && file->dontcare)
182 /* This is a default makefile. Stop remaking. */
183 stop = 1;
188 /* Keep track if any double-colon entry is not finished.
189 When they are all finished, the goal is finished. */
190 any_not_updated |= !file->updated;
192 if (stop)
193 break;
196 /* Reset FILE since it is null at the end of the loop. */
197 file = g->file;
199 if (stop || !any_not_updated)
201 /* If we have found nothing whatever to do for the goal,
202 print a message saying nothing needs doing. */
204 if (!makefiles
205 /* If the update_status is zero, we updated successfully
206 or not at all. G->changed will have been set above if
207 any commands were actually started for this goal. */
208 && file->update_status == 0 && !g->changed
209 /* Never give a message under -s or -q. */
210 && !silent_flag && !question_flag)
211 message (1, ((file->phony || file->cmds == 0)
212 ? "Nothing to be done for `%s'."
213 : "`%s' is up to date."),
214 file->name);
216 /* This goal is finished. Remove it from the chain. */
217 if (lastgoal == 0)
218 goals = g->next;
219 else
220 lastgoal->next = g->next;
222 /* Free the storage. */
223 free ((char *) g);
225 g = lastgoal == 0 ? goals : lastgoal->next;
227 if (stop)
228 break;
230 else
232 lastgoal = g;
233 g = g->next;
238 if (makefiles)
240 touch_flag = t;
241 question_flag = q;
242 just_print_flag = n;
243 job_slots = j;
245 return status;
248 /* If FILE is not up to date, execute the commands for it.
249 Return 0 if successful, 1 if unsuccessful;
250 but with some flag settings, just call `exit' if unsuccessful.
252 DEPTH is the depth in recursions of this function.
253 We increment it during the consideration of our dependencies,
254 then decrement it again after finding out whether this file
255 is out of date.
257 If there are multiple double-colon entries for FILE,
258 each is considered in turn. */
260 static int
261 update_file (file, depth)
262 struct file *file;
263 unsigned int depth;
265 register int status = 0;
266 register struct file *f;
268 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
270 status |= update_file_1 (f, depth);
271 check_renamed (f);
273 if (status != 0 && !keep_going_flag)
274 return status;
276 switch (f->command_state)
278 case cs_finished:
279 /* The file is done being remade. */
280 break;
282 case cs_running:
283 case cs_deps_running:
284 /* Don't run the other :: rules for this
285 file until this rule is finished. */
286 return 0;
288 default:
289 assert (f->command_state == cs_running);
290 break;
294 return status;
297 /* Consider a single `struct file' and update it as appropriate. */
299 static int
300 update_file_1 (file, depth)
301 struct file *file;
302 unsigned int depth;
304 register time_t this_mtime;
305 int noexist, must_make, deps_changed;
306 int dep_status = 0;
307 register struct dep *d, *lastd;
308 int running = 0;
310 DEBUGPR ("Considering target file `%s'.\n");
312 if (file->updated)
314 if (file->update_status > 0)
316 DEBUGPR ("Recently tried and failed to update file `%s'.\n");
317 return file->update_status;
320 DEBUGPR ("File `%s' was considered already.\n");
321 return 0;
324 switch (file->command_state)
326 case cs_not_started:
327 case cs_deps_running:
328 break;
329 case cs_running:
330 DEBUGPR ("Still updating file `%s'.\n");
331 return 0;
332 case cs_finished:
333 DEBUGPR ("Finished updating file `%s'.\n");
334 return file->update_status;
335 default:
336 abort ();
339 ++depth;
341 /* Notice recursive update of the same file. */
342 file->updating = 1;
344 /* Looking at the file's modtime beforehand allows the possibility
345 that its name may be changed by a VPATH search, and thus it may
346 not need an implicit rule. If this were not done, the file
347 might get implicit commands that apply to its initial name, only
348 to have that name replaced with another found by VPATH search. */
350 this_mtime = file_mtime (file);
351 check_renamed (file);
352 noexist = this_mtime == (time_t) -1;
353 if (noexist)
354 DEBUGPR ("File `%s' does not exist.\n");
356 must_make = noexist;
358 /* If file was specified as a target with no commands,
359 come up with some default commands. */
361 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
363 if (try_implicit_rule (file, depth))
364 DEBUGPR ("Found an implicit rule for `%s'.\n");
365 else
366 DEBUGPR ("No implicit rule found for `%s'.\n");
367 file->tried_implicit = 1;
369 if (file->cmds == 0 && !file->is_target
370 && default_file != 0 && default_file->cmds != 0)
372 DEBUGPR ("Using default commands for `%s'.\n");
373 file->cmds = default_file->cmds;
376 /* Update all non-intermediate files we depend on, if necessary,
377 and see whether any of them is more recent than this file. */
379 lastd = 0;
380 d = file->deps;
381 while (d != 0)
383 time_t mtime;
385 check_renamed (d->file);
387 mtime = file_mtime (d->file);
388 check_renamed (d->file);
390 if (d->file->updating)
392 error ("Circular %s <- %s dependency dropped.",
393 file->name, d->file->name);
394 if (lastd == 0)
396 file->deps = d->next;
397 free ((char *) d);
398 d = file->deps;
400 else
402 lastd->next = d->next;
403 free ((char *) d);
404 d = lastd->next;
406 continue;
409 d->file->parent = file;
410 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
411 check_renamed (d->file);
414 register struct file *f = d->file;
415 if (f->double_colon)
416 f = f->double_colon;
419 running |= (f->command_state == cs_running
420 || f->command_state == cs_deps_running);
421 f = f->prev;
423 while (f != 0);
426 if (dep_status != 0 && !keep_going_flag)
427 break;
429 if (!running)
430 d->changed = file_mtime (d->file) != mtime;
432 lastd = d;
433 d = d->next;
436 /* Now we know whether this target needs updating.
437 If it does, update all the intermediate files we depend on. */
439 if (must_make)
441 for (d = file->deps; d != 0; d = d->next)
442 if (d->file->intermediate)
444 time_t mtime = file_mtime (d->file);
445 check_renamed (d->file);
446 d->file->parent = file;
447 dep_status |= update_file (d->file, depth);
448 check_renamed (d->file);
451 register struct file *f = d->file;
452 if (f->double_colon)
453 f = f->double_colon;
456 running |= (f->command_state == cs_running
457 || f->command_state == cs_deps_running);
458 f = f->prev;
460 while (f != 0);
463 if (dep_status != 0 && !keep_going_flag)
464 break;
466 if (!running)
467 d->changed = ((file->phony && file->cmds != 0)
468 || file_mtime (d->file) != mtime);
472 file->updating = 0;
474 DEBUGPR ("Finished dependencies of target file `%s'.\n");
476 if (running)
478 set_command_state (file, cs_deps_running);
479 --depth;
480 DEBUGPR ("The dependencies of `%s' are being made.\n");
481 return 0;
484 /* If any dependency failed, give up now. */
486 if (dep_status != 0)
488 file->update_status = dep_status;
489 notice_finished_file (file);
491 depth--;
493 DEBUGPR ("Giving up on target file `%s'.\n");
495 if (depth == 0 && keep_going_flag
496 && !just_print_flag && !question_flag)
497 error ("Target `%s' not remade because of errors.", file->name);
499 return dep_status;
502 if (file->command_state == cs_deps_running)
503 /* The commands for some deps were running on the last iteration, but
504 they have finished now. Reset the command_state to not_started to
505 simplify later bookkeeping. It is important that we do this only
506 when the prior state was cs_deps_running, because that prior state
507 was definitely propagated to FILE's also_make's by set_command_state
508 (called above), but in another state an also_make may have
509 independently changed to finished state, and we would confuse that
510 file's bookkeeping (updated, but not_started is bogus state). */
511 set_command_state (file, cs_not_started);
513 /* Now record which dependencies are more
514 recent than this file, so we can define $?. */
516 deps_changed = 0;
517 for (d = file->deps; d != 0; d = d->next)
519 time_t d_mtime = file_mtime (d->file);
520 check_renamed (d->file);
522 #if 1 /* %%% In version 4, remove this code completely to
523 implement not remaking deps if their deps are newer
524 than their parents. */
525 if (d_mtime == (time_t) -1 && !d->file->intermediate)
526 /* We must remake if this dep does not
527 exist and is not intermediate. */
528 must_make = 1;
529 #endif
531 /* Set DEPS_CHANGED if this dep actually changed. */
532 deps_changed |= d->changed;
534 /* Set D->changed if either this dep actually changed,
535 or its dependent, FILE, is older or does not exist. */
536 d->changed |= noexist || d_mtime > this_mtime;
538 if (debug_flag && !noexist)
540 print_spaces (depth);
541 if (d_mtime == (time_t) -1)
542 printf ("Dependency `%s' does not exist.\n", dep_name (d));
543 else
544 printf ("Dependency `%s' is %s than dependent `%s'.\n",
545 dep_name (d), d->changed ? "newer" : "older", file->name);
546 fflush (stdout);
550 /* Here depth returns to the value it had when we were called. */
551 depth--;
553 if (file->double_colon && file->deps == 0)
555 must_make = 1;
556 DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
558 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
560 must_make = 0;
561 DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
564 if (!must_make)
566 DEBUGPR ("No need to remake target `%s'.\n");
567 notice_finished_file (file);
568 return 0;
571 DEBUGPR ("Must remake target `%s'.\n");
573 /* Now, take appropriate actions to remake the file. */
574 remake_file (file);
576 if (file->command_state != cs_finished)
578 DEBUGPR ("Commands of `%s' are being run.\n");
579 return 0;
582 switch (file->update_status)
584 case 2:
585 DEBUGPR ("Failed to remake target file `%s'.\n");
586 break;
587 case 0:
588 DEBUGPR ("Successfully remade target file `%s'.\n");
589 break;
590 case 1:
591 DEBUGPR ("Target file `%s' needs remade under -q.\n");
592 break;
593 default:
594 assert (file->update_status >= 0 && file->update_status <= 2);
595 break;
598 file->updated = 1;
599 return file->update_status;
602 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
603 files listed in its `also_make' member. Under -t, this function also
604 touches FILE.
606 On return, FILE->update_status will no longer be -1 if it was. */
608 void
609 notice_finished_file (file)
610 register struct file *file;
612 struct dep *d;
613 int ran = file->command_state == cs_running;
615 file->command_state = cs_finished;
616 file->updated = 1;
618 if (touch_flag
619 /* The update status will be:
620 -1 if this target was not remade;
621 0 if 0 or more commands (+ or ${MAKE}) were run and won;
622 1 if some commands were run and lost.
623 We touch the target if it has commands which either were not run
624 or won when they ran (i.e. status is 0). */
625 && file->update_status == 0)
627 if (file->cmds != 0 && file->cmds->any_recurse)
629 /* If all the command lines were recursive,
630 we don't want to do the touching. */
631 unsigned int i;
632 for (i = 0; i < file->cmds->ncommand_lines; ++i)
633 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
634 goto have_nonrecursing;
636 else
638 have_nonrecursing:
639 if (file->phony)
640 file->update_status = 0;
641 else
642 /* Should set file's modification date and do nothing else. */
643 file->update_status = touch_file (file);
647 if (ran && !file->phony)
649 struct file *f;
651 if (just_print_flag || question_flag
652 || (file->is_target && file->cmds == 0))
653 file->last_mtime = NEW_MTIME;
654 else
655 file->last_mtime = 0;
657 /* Propagate the change of modification time to all the double-colon
658 entries for this file. */
659 for (f = file->double_colon; f != 0; f = f->next)
660 f->last_mtime = file->last_mtime;
663 if (ran && file->update_status != -1)
664 /* We actually tried to update FILE, which has
665 updated its also_make's as well (if it worked).
666 If it didn't work, it wouldn't work again for them.
667 So mark them as updated with the same status. */
668 for (d = file->also_make; d != 0; d = d->next)
670 d->file->command_state = cs_finished;
671 d->file->updated = 1;
672 d->file->update_status = file->update_status;
674 if (ran && !d->file->phony)
675 /* Fetch the new modification time.
676 We do this instead of just invalidating the cached time
677 so that a vpath_search can happen. Otherwise, it would
678 never be done because the target is already updated. */
679 (void) f_mtime (d->file, 0);
681 else if (file->update_status == -1)
682 /* Nothing was done for FILE, but it needed nothing done.
683 So mark it now as "succeeded". */
684 file->update_status = 0;
687 /* Check whether another file (whose mtime is THIS_MTIME)
688 needs updating on account of a dependency which is file FILE.
689 If it does, store 1 in *MUST_MAKE_PTR.
690 In the process, update any non-intermediate files
691 that FILE depends on (including FILE itself).
692 Return nonzero if any updating failed. */
694 static int
695 check_dep (file, depth, this_mtime, must_make_ptr)
696 struct file *file;
697 unsigned int depth;
698 time_t this_mtime;
699 int *must_make_ptr;
701 register struct dep *d;
702 int dep_status = 0;
704 ++depth;
705 file->updating = 1;
707 if (!file->intermediate)
708 /* If this is a non-intermediate file, update it and record
709 whether it is newer than THIS_MTIME. */
711 time_t mtime;
712 dep_status = update_file (file, depth);
713 check_renamed (file);
714 mtime = file_mtime (file);
715 check_renamed (file);
716 if (mtime == (time_t) -1 || mtime > this_mtime)
717 *must_make_ptr = 1;
719 else
721 /* FILE is an intermediate file. */
722 time_t mtime;
724 if (!file->phony && file->cmds == 0 && !file->tried_implicit
725 && file->secondary)
727 if (try_implicit_rule (file, depth))
728 DEBUGPR ("Found an implicit rule for `%s'.\n");
729 else
730 DEBUGPR ("No implicit rule found for `%s'.\n");
731 file->tried_implicit = 1;
733 if (file->cmds == 0 && !file->is_target && file->secondary
734 && default_file != 0 && default_file->cmds != 0)
736 DEBUGPR ("Using default commands for `%s'.\n");
737 file->cmds = default_file->cmds;
740 /* If the intermediate file actually exists
741 and is newer, then we should remake from it. */
742 check_renamed (file);
743 mtime = file_mtime (file);
744 check_renamed (file);
745 if (mtime > this_mtime)
746 *must_make_ptr = 1;
747 /* Otherwise, update all non-intermediate files we depend on,
748 if necessary, and see whether any of them is more
749 recent than the file on whose behalf we are checking. */
750 else
752 register struct dep *lastd;
754 lastd = 0;
755 d = file->deps;
756 while (d != 0)
758 if (d->file->updating)
760 error ("Circular %s <- %s dependency dropped.",
761 file->name, d->file->name);
762 if (lastd == 0)
764 file->deps = d->next;
765 free ((char *) d);
766 d = file->deps;
768 else
770 lastd->next = d->next;
771 free ((char *) d);
772 d = lastd->next;
774 continue;
777 d->file->parent = file;
778 dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
779 check_renamed (d->file);
780 if (dep_status != 0 && !keep_going_flag)
781 break;
783 if (d->file->command_state == cs_running
784 || d->file->command_state == cs_deps_running)
785 /* Record that some of FILE's dependencies are still being made.
786 This tells the upper levels to wait on processing it until
787 the commands are finished. */
788 set_command_state (file, cs_deps_running);
790 lastd = d;
791 d = d->next;
796 file->updating = 0;
797 return dep_status;
800 /* Touch FILE. Return zero if successful, one if not. */
802 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
804 static int
805 touch_file (file)
806 register struct file *file;
808 if (!silent_flag)
809 message (0, "touch %s", file->name);
811 #ifndef NO_ARCHIVES
812 if (ar_name (file->name))
813 return ar_touch (file->name);
814 else
815 #endif
817 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
819 if (fd < 0)
820 TOUCH_ERROR ("touch: open: ");
821 else
823 struct stat statbuf;
824 char buf;
825 int status;
827 #ifdef EINTR
829 #endif
830 status = fstat (fd, &statbuf);
831 #ifdef EINTR
832 while (status < 0 && errno == EINTR);
833 #endif
834 if (status < 0)
835 TOUCH_ERROR ("touch: fstat: ");
836 /* Rewrite character 0 same as it already is. */
837 if (read (fd, &buf, 1) < 0)
838 TOUCH_ERROR ("touch: read: ");
839 if (lseek (fd, 0L, 0) < 0L)
840 TOUCH_ERROR ("touch: lseek: ");
841 if (write (fd, &buf, 1) < 0)
842 TOUCH_ERROR ("touch: write: ");
843 /* If file length was 0, we just
844 changed it, so change it back. */
845 if (statbuf.st_size == 0)
847 (void) close (fd);
848 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
849 if (fd < 0)
850 TOUCH_ERROR ("touch: open: ");
852 (void) close (fd);
856 return 0;
859 /* Having checked and updated the dependencies of FILE,
860 do whatever is appropriate to remake FILE itself.
861 Return the status from executing FILE's commands. */
863 static void
864 remake_file (file)
865 struct file *file;
867 if (file->cmds == 0)
869 if (file->phony)
870 /* Phony target. Pretend it succeeded. */
871 file->update_status = 0;
872 else if (file->is_target)
873 /* This is a nonexistent target file we cannot make.
874 Pretend it was successfully remade. */
875 file->update_status = 0;
876 else
878 /* This is a dependency file we cannot remake. Fail. */
879 static const char msg_noparent[]
880 = "%sNo rule to make target `%s'%s";
881 static const char msg_parent[]
882 = "%sNo rule to make target `%s', needed by `%s'%s";
883 if (keep_going_flag || file->dontcare)
885 if (!file->dontcare)
887 if (file->parent == 0)
888 error (msg_noparent, "*** ", file->name, ".");
889 else
890 error (msg_parent, "*** ",
891 file->name, file->parent->name, ".");
893 file->update_status = 2;
895 else
897 if (file->parent == 0)
898 fatal (msg_noparent, "", file->name, "");
899 else
900 fatal (msg_parent, "", file->name, file->parent->name, "");
904 else
906 chop_commands (file->cmds);
908 if (!touch_flag || file->cmds->any_recurse)
910 execute_file_commands (file);
911 return;
913 else
914 /* This tells notice_finished_file it is ok to touch the file. */
915 file->update_status = 0;
918 /* This does the touching under -t. */
919 notice_finished_file (file);
922 /* Return the mtime of a file, given a `struct file'.
923 Caches the time in the struct file to avoid excess stat calls.
925 If the file is not found, and SEARCH is nonzero, VPATH searching and
926 replacement is done. If that fails, a library (-lLIBNAME) is tried and
927 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
928 FILE. */
930 time_t
931 f_mtime (file, search)
932 register struct file *file;
933 int search;
935 time_t mtime;
937 /* File's mtime is not known; must get it from the system. */
939 #ifndef NO_ARCHIVES
940 if (ar_name (file->name))
942 /* This file is an archive-member reference. */
944 char *arname, *memname;
945 struct file *arfile;
946 int arname_used = 0;
948 /* Find the archive's name. */
949 ar_parse_name (file->name, &arname, &memname);
951 /* Find the modification time of the archive itself.
952 Also allow for its name to be changed via VPATH search. */
953 arfile = lookup_file (arname);
954 if (arfile == 0)
956 arfile = enter_file (arname);
957 arname_used = 1;
959 mtime = f_mtime (arfile, search);
960 check_renamed (arfile);
961 if (search && strcmp (arfile->name, arname))
963 /* The archive's name has changed.
964 Change the archive-member reference accordingly. */
966 unsigned int arlen, memlen;
968 if (!arname_used)
970 free (arname);
971 arname_used = 1;
974 arname = arfile->name;
975 arlen = strlen (arname);
976 memlen = strlen (memname);
978 free (file->name);
980 file->name = (char *) xmalloc (arlen + 1 + memlen + 2);
981 bcopy (arname, file->name, arlen);
982 file->name[arlen] = '(';
983 bcopy (memname, file->name + arlen + 1, memlen);
984 file->name[arlen + 1 + memlen] = ')';
985 file->name[arlen + 1 + memlen + 1] = '\0';
988 if (!arname_used)
989 free (arname);
990 free (memname);
992 if (mtime == (time_t) -1)
993 /* The archive doesn't exist, so it's members don't exist either. */
994 return (time_t) -1;
996 mtime = ar_member_date (file->name);
998 else
999 #endif
1001 mtime = name_mtime (file->name);
1003 if (mtime == (time_t) -1 && search)
1005 /* If name_mtime failed, search VPATH. */
1006 char *name = file->name;
1007 if (vpath_search (&name, &mtime)
1008 /* Last resort, is it a library (-lxxx)? */
1009 || (name[0] == '-' && name[1] == 'l'
1010 && library_search (&name, &mtime)))
1012 if (mtime != 0)
1013 /* vpath_search and library_search store zero in MTIME
1014 if they didn't need to do a stat call for their work. */
1015 file->last_mtime = mtime;
1016 rename_file (file, name);
1017 check_renamed (file);
1018 return file_mtime (file);
1024 /* Files can have bogus timestamps that nothing newly made will be
1025 "newer" than. Updating their dependents could just result in loops.
1026 So notify the user of the anomaly by treating future files as
1027 unrecoverably absent. */
1029 static time_t now;
1030 #ifdef VMS
1031 /* Handle vms 64bit to 32bit time hack introduced in vms_stat() ... */
1032 static unsigned long vms_now[2]; /* assumes 32 bit long ! */
1033 static int vms_now_set = 0;
1035 if (!vms_now_set)
1037 sys$gettim(vms_now);
1038 now = ((vms_now[0]>>24) & 0xff) + ((vms_now[1]<<8) & 0xffffff00);
1039 vms_now_set = 1;
1041 #endif
1042 if (mtime > now && ! file->updated)
1044 /* This file's time appears to be in the future.
1045 Update our concept of the present, and compare again. */
1046 #ifndef VMS
1047 extern time_t time ();
1048 if (mtime > time (&now))
1049 #else
1050 if ((mtime != -1) && (mtime > now))
1051 #endif
1053 error ("*** File `%s' has modification time in the future",
1054 file->name);
1055 file->update_status = 2;
1060 /* Store the mtime into all the entries for this file. */
1061 if (file->double_colon)
1062 file = file->double_colon;
1065 file->last_mtime = mtime;
1066 file = file->prev;
1067 } while (file != 0);
1069 return mtime;
1073 /* Return the mtime of the file or archive-member reference NAME. */
1075 static time_t
1076 name_mtime (name)
1077 register char *name;
1079 struct stat st;
1081 #ifdef VMS
1082 if (vms_stat (name, &st) < 0)
1083 #else
1084 if (stat (name, &st) < 0)
1085 #endif
1086 return (time_t) -1;
1088 return (time_t) st.st_mtime;
1092 /* Search for a library file specified as -lLIBNAME, searching for a
1093 suitable library file in the system library directories and the VPATH
1094 directories. */
1096 static int
1097 library_search (lib, mtime_ptr)
1098 char **lib;
1099 time_t *mtime_ptr;
1101 static char *dirs[] =
1103 #ifndef _AMIGA
1104 "/lib",
1105 "/usr/lib",
1106 #endif
1107 LIBDIR, /* Defined by configuration. */
1111 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1112 time_t mtime;
1114 /* Buffer to construct possible names in. */
1115 char *buf = xmalloc (sizeof (LIBDIR) + 8 + strlen (libname) + 4 + 2 + 1);
1116 char *file, **dp;
1118 /* Look first for `libNAME.a' in the current directory. */
1120 #ifndef _AMIGA
1121 sprintf (buf, "lib%s.a", libname);
1122 #else
1123 sprintf (buf, "%s.lib", libname);
1124 #endif
1125 mtime = name_mtime (buf);
1126 if (mtime != (time_t) -1)
1128 *lib = buf;
1129 if (mtime_ptr != 0)
1130 *mtime_ptr = mtime;
1131 return 1;
1134 /* Now try VPATH search on that. */
1136 file = buf;
1137 if (vpath_search (&file, mtime_ptr))
1139 free (buf);
1140 *lib = file;
1141 return 1;
1144 /* Now try the standard set of directories. */
1146 for (dp = dirs; *dp != 0; ++dp)
1148 #ifndef _AMIGA
1149 sprintf (buf, "%s/lib%s.a", *dp, libname);
1150 #else
1151 sprintf (buf, "%s/%s.lib", *dp, libname);
1152 #endif
1153 mtime = name_mtime (buf);
1154 if (mtime != (time_t) -1)
1156 *lib = buf;
1157 if (mtime_ptr != 0)
1158 *mtime_ptr = mtime;
1159 return 1;
1163 free (buf);
1164 return 0;