(collapse_continuations): Fix skipping of trailing \s so it can never
[make.git] / remake.c
blob528b0baf90996f7b9e7f6f47d847ce2927cb1f4e
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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 "commands.h"
21 #include "job.h"
22 #include "dep.h"
23 #include "file.h"
24 #include <assert.h>
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #else
29 #include <sys/file.h>
30 #endif
32 extern int try_implicit_rule ();
35 /* Incremented when a command is started (under -n, when one would be). */
36 unsigned int commands_started = 0;
38 static int update_file (), update_file_1 (), check_dep (), touch_file ();
39 static void remake_file ();
40 static time_t name_mtime ();
41 static int library_search ();
42 extern time_t f_mtime ();
44 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
45 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
46 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
47 be disabled for them unless they were also command-line targets, and we
48 should only make one goal at a time and return as soon as one goal whose
49 `changed' member is nonzero is successfully made. */
51 int
52 update_goal_chain (goals, makefiles)
53 register struct dep *goals;
54 int makefiles;
56 int t = touch_flag, q = question_flag, n = just_print_flag;
57 unsigned int j = job_slots;
58 int status = -1;
60 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
61 : file_mtime (file))
63 /* Duplicate the chain so we can remove things from it. */
65 goals = copy_dep_chain (goals);
68 /* Clear the `changed' flag of each goal in the chain.
69 We will use the flag below to notice when any commands
70 have actually been run for a target. When no commands
71 have been run, we give an "up to date" diagnostic. */
73 struct dep *g;
74 for (g = goals; g != 0; g = g->next)
75 g->changed = 0;
78 if (makefiles)
79 /* Only run one job at a time. */
80 job_slots = 1;
82 /* Update all the goals until they are all finished. */
84 while (goals != 0)
86 register struct dep *g, *lastgoal;
88 /* Start jobs that are waiting for the load to go down. */
90 start_waiting_jobs ();
92 /* Wait for a child to die. */
94 reap_children (1, 0);
96 lastgoal = 0;
97 g = goals;
98 while (g != 0)
100 /* Iterate over all double-colon entries for this file. */
101 struct file *file = g->file;
102 int stop, any_not_updated = 0;
103 for (file = g->file->double_colon ? g->file->double_colon : g->file;
104 file != NULL;
105 file = file->prev)
107 unsigned int ocommands_started;
108 int x;
109 time_t mtime = MTIME (file);
110 check_renamed (file);
112 if (makefiles)
114 if (file->cmd_target)
116 touch_flag = t;
117 question_flag = q;
118 just_print_flag = n;
120 else
121 touch_flag = question_flag = just_print_flag = 0;
124 /* Save the old value of `commands_started' so we can compare
125 later. It will be incremented when any commands are
126 actually run. */
127 ocommands_started = commands_started;
129 x = update_file (file, makefiles ? 1 : 0);
130 check_renamed (file);
132 /* Set the goal's `changed' flag if any commands were started
133 by calling update_file above. We check this flag below to
134 decide when to give an "up to date" diagnostic. */
135 g->changed += commands_started - ocommands_started;
137 stop = 0;
138 if (x != 0 || file->updated)
140 /* If STATUS was not already 1, set it to 1 if
141 updating failed, or to 0 if updating succeeded.
142 Leave STATUS as it is if no updating was done. */
144 if (status < 1)
146 if (file->update_status != 0)
148 /* Updating failed, or -q triggered.
149 The STATUS value tells our caller which. */
150 status = file->update_status;
151 /* If -q just triggered, stop immediately.
152 It doesn't matter how much more we run,
153 since we already know the answer to return. */
154 stop = (!keep_going_flag && !question_flag
155 && !makefiles);
157 else if (MTIME (file) != mtime)
159 /* Updating was done. If this is a makefile and
160 just_print_flag or question_flag is set
161 (meaning -n or -q was given and this file was
162 specified as a command-line target), don't
163 change STATUS. If STATUS is changed, we will
164 get re-exec'd, and fall into an infinite loop. */
165 if (!makefiles
166 || (!just_print_flag && !question_flag))
167 status = 0;
168 if (makefiles && file->dontcare)
169 /* This is a default makefile. Stop remaking. */
170 stop = 1;
175 /* Keep track if any double-colon entry is not finished.
176 When they are all finished, the goal is finished. */
177 any_not_updated |= !file->updated;
179 if (stop)
180 break;
183 /* Reset FILE since it is null at the end of the loop. */
184 file = g->file;
186 if (stop || !any_not_updated)
188 /* If we have found nothing whatever to do for the goal,
189 print a message saying nothing needs doing. */
191 if (!makefiles
192 /* If the update_status is zero, we updated successfully
193 or not at all. G->changed will have been set above if
194 any commands were actually started for this goal. */
195 && file->update_status == 0 && !g->changed
196 /* Never give a message under -s or -q. */
197 && !silent_flag && !question_flag)
198 message (1, ((file->phony || file->cmds == 0)
199 ? "Nothing to be done for `%s'."
200 : "`%s' is up to date."),
201 file->name);
203 /* This goal is finished. Remove it from the chain. */
204 if (lastgoal == 0)
205 goals = g->next;
206 else
207 lastgoal->next = g->next;
209 /* Free the storage. */
210 free ((char *) g);
212 g = lastgoal == 0 ? goals : lastgoal->next;
214 if (stop)
215 break;
217 else
219 lastgoal = g;
220 g = g->next;
225 if (makefiles)
227 touch_flag = t;
228 question_flag = q;
229 just_print_flag = n;
230 job_slots = j;
233 return status;
236 /* If FILE is not up to date, execute the commands for it.
237 Return 0 if successful, 1 if unsuccessful;
238 but with some flag settings, just call `exit' if unsuccessful.
240 DEPTH is the depth in recursions of this function.
241 We increment it during the consideration of our dependencies,
242 then decrement it again after finding out whether this file
243 is out of date.
245 If there are multiple double-colon entries for FILE,
246 each is considered in turn. */
248 static int
249 update_file (file, depth)
250 struct file *file;
251 unsigned int depth;
253 register int status = 0;
254 register struct file *f;
256 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
258 status |= update_file_1 (f, depth);
259 check_renamed (f);
261 if (status != 0 && !keep_going_flag)
262 return status;
264 switch (f->command_state)
266 case cs_finished:
267 /* The file is done being remade. */
268 break;
270 case cs_running:
271 case cs_deps_running:
272 /* Don't run the other :: rules for this
273 file until this rule is finished. */
274 return 0;
276 default:
277 assert (f->command_state == cs_running);
278 break;
282 return status;
285 /* Consider a single `struct file' and update it as appropriate. */
287 static int
288 update_file_1 (file, depth)
289 struct file *file;
290 unsigned int depth;
292 register time_t this_mtime;
293 int noexist, must_make, deps_changed;
294 int dep_status = 0;
295 register struct dep *d, *lastd;
296 int running = 0;
298 DEBUGPR ("Considering target file `%s'.\n");
300 if (file->updated)
302 if (file->update_status > 0)
304 DEBUGPR ("Recently tried and failed to update file `%s'.\n");
305 return file->update_status;
308 DEBUGPR ("File `%s' was considered already.\n");
309 return 0;
312 switch (file->command_state)
314 case cs_not_started:
315 case cs_deps_running:
316 break;
317 case cs_running:
318 DEBUGPR ("Still updating file `%s'.\n");
319 return 0;
320 case cs_finished:
321 DEBUGPR ("Finished updating file `%s'.\n");
322 return file->update_status;
323 default:
324 abort ();
327 ++depth;
329 /* Notice recursive update of the same file. */
330 file->updating = 1;
332 /* Looking at the file's modtime beforehand allows the possibility
333 that its name may be changed by a VPATH search, and thus it may
334 not need an implicit rule. If this were not done, the file
335 might get implicit commands that apply to its initial name, only
336 to have that name replaced with another found by VPATH search. */
338 this_mtime = file_mtime (file);
339 check_renamed (file);
340 noexist = this_mtime == (time_t) -1;
341 if (noexist)
342 DEBUGPR ("File `%s' does not exist.\n");
344 must_make = noexist;
346 /* If file was specified as a target with no commands,
347 come up with some default commands. */
349 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
351 if (try_implicit_rule (file, depth))
352 DEBUGPR ("Found an implicit rule for `%s'.\n");
353 else
354 DEBUGPR ("No implicit rule found for `%s'.\n");
355 file->tried_implicit = 1;
357 if (file->cmds == 0 && !file->is_target
358 && default_file != 0 && default_file->cmds != 0)
360 DEBUGPR ("Using default commands for `%s'.\n");
361 file->cmds = default_file->cmds;
364 /* Update all non-intermediate files we depend on, if necessary,
365 and see whether any of them is more recent than this file. */
367 lastd = 0;
368 d = file->deps;
369 while (d != 0)
371 time_t mtime;
373 check_renamed (d->file);
375 mtime = file_mtime (d->file);
376 check_renamed (d->file);
378 if (d->file->updating)
380 error ("Circular %s <- %s dependency dropped.",
381 file->name, d->file->name);
382 if (lastd == 0)
384 file->deps = d->next;
385 free ((char *) d);
386 d = file->deps;
388 else
390 lastd->next = d->next;
391 free ((char *) d);
392 d = lastd->next;
394 continue;
397 d->file->parent = file;
398 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
399 check_renamed (d->file);
402 register struct file *f = d->file;
403 if (f->double_colon)
404 f = f->double_colon;
407 running |= (f->command_state == cs_running
408 || f->command_state == cs_deps_running);
409 f = f->prev;
411 while (f != 0);
414 if (dep_status != 0 && !keep_going_flag)
415 break;
417 if (!running)
418 d->changed = file_mtime (d->file) != mtime;
420 lastd = d;
421 d = d->next;
424 /* Now we know whether this target needs updating.
425 If it does, update all the intermediate files we depend on. */
427 if (must_make)
429 for (d = file->deps; d != 0; d = d->next)
430 if (d->file->intermediate)
432 time_t mtime = file_mtime (d->file);
433 check_renamed (d->file);
434 d->file->parent = file;
435 dep_status |= update_file (d->file, depth);
436 check_renamed (d->file);
439 register struct file *f = d->file;
440 if (f->double_colon)
441 f = f->double_colon;
444 running |= (f->command_state == cs_running
445 || f->command_state == cs_deps_running);
446 f = f->prev;
448 while (f != 0);
451 if (dep_status != 0 && !keep_going_flag)
452 break;
454 if (!running)
455 d->changed = ((file->phony && file->cmds != 0)
456 || file_mtime (d->file) != mtime);
460 file->updating = 0;
462 DEBUGPR ("Finished dependencies of target file `%s'.\n");
464 if (running)
466 set_command_state (file, cs_deps_running);
467 --depth;
468 DEBUGPR ("The dependencies of `%s' are being made.\n");
469 return 0;
472 /* If any dependency failed, give up now. */
474 if (dep_status != 0)
476 file->update_status = dep_status;
477 notice_finished_file (file);
479 depth--;
481 DEBUGPR ("Giving up on target file `%s'.\n");
483 if (depth == 0 && keep_going_flag
484 && !just_print_flag && !question_flag)
485 error ("Target `%s' not remade because of errors.", file->name);
487 return dep_status;
490 if (file->command_state == cs_deps_running)
491 /* The commands for some deps were running on the last iteration, but
492 they have finished now. Reset the command_state to not_started to
493 simplify later bookkeeping. It is important that we do this only
494 when the prior state was cs_deps_running, because that prior state
495 was definitely propagated to FILE's also_make's by set_command_state
496 (called above), but in another state an also_make may have
497 independently changed to finished state, and we would confuse that
498 file's bookkeeping (updated, but not_started is bogus state). */
499 set_command_state (file, cs_not_started);
501 /* Now record which dependencies are more
502 recent than this file, so we can define $?. */
504 deps_changed = 0;
505 for (d = file->deps; d != 0; d = d->next)
507 time_t d_mtime = file_mtime (d->file);
508 check_renamed (d->file);
510 #if 1 /* %%% In version 4, remove this code completely to
511 implement not remaking deps if their deps are newer
512 than their parents. */
513 if (d_mtime == (time_t) -1 && !d->file->intermediate)
514 /* We must remake if this dep does not
515 exist and is not intermediate. */
516 must_make = 1;
517 #endif
519 /* Set DEPS_CHANGED if this dep actually changed. */
520 deps_changed |= d->changed;
522 /* Set D->changed if either this dep actually changed,
523 or its dependent, FILE, is older or does not exist. */
524 d->changed |= noexist || d_mtime > this_mtime;
526 if (debug_flag && !noexist)
528 print_spaces (depth);
529 if (d_mtime == (time_t) -1)
530 printf ("Dependency `%s' does not exist.\n", dep_name (d));
531 else
532 printf ("Dependency `%s' is %s than dependent `%s'.\n",
533 dep_name (d), d->changed ? "newer" : "older", file->name);
534 fflush (stdout);
538 /* Here depth returns to the value it had when we were called. */
539 depth--;
541 if (file->double_colon && file->deps == 0)
543 must_make = 1;
544 DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
546 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
548 must_make = 0;
549 DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
552 if (!must_make)
554 DEBUGPR ("No need to remake target `%s'.\n");
555 notice_finished_file (file);
556 return 0;
559 DEBUGPR ("Must remake target `%s'.\n");
561 /* Now, take appropriate actions to remake the file. */
562 remake_file (file);
564 if (file->command_state != cs_finished)
566 DEBUGPR ("Commands of `%s' are being run.\n");
567 return 0;
570 switch (file->update_status)
572 case 2:
573 DEBUGPR ("Failed to remake target file `%s'.\n");
574 break;
575 case 0:
576 DEBUGPR ("Successfully remade target file `%s'.\n");
577 break;
578 case 1:
579 DEBUGPR ("Target file `%s' needs remade under -q.\n");
580 break;
581 default:
582 assert (file->update_status >= 0 && file->update_status <= 2);
583 break;
586 file->updated = 1;
587 return file->update_status;
590 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
591 files listed in its `also_make' member. Under -t, this function also
592 touches FILE.
594 On return, FILE->update_status will no longer be -1 if it was. */
596 void
597 notice_finished_file (file)
598 register struct file *file;
600 struct dep *d;
601 int ran = file->command_state == cs_running;
603 file->command_state = cs_finished;
604 file->updated = 1;
606 if (touch_flag
607 /* The update status will be:
608 -1 if this target was not remade;
609 0 if 0 or more commands (+ or ${MAKE}) were run and won;
610 1 if some commands were run and lost.
611 We touch the target if it has commands which either were not run
612 or won when they ran (i.e. status is 0). */
613 && file->update_status == 0)
615 if (file->cmds != 0 && file->cmds->any_recurse)
617 /* If all the command lines were recursive,
618 we don't want to do the touching. */
619 unsigned int i;
620 for (i = 0; i < file->cmds->ncommand_lines; ++i)
621 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
622 goto have_nonrecursing;
624 else
626 have_nonrecursing:
627 if (file->phony)
628 file->update_status = 0;
629 else
630 /* Should set file's modification date and do nothing else. */
631 file->update_status = touch_file (file);
635 if (ran && !file->phony)
637 struct file *f;
639 if (just_print_flag || question_flag
640 || (file->is_target && file->cmds == 0))
641 file->last_mtime = NEW_MTIME;
642 else
643 file->last_mtime = 0;
645 /* Propagate the change of modification time to all the double-colon
646 entries for this file. */
647 for (f = file->double_colon; f != 0; f = f->next)
648 f->last_mtime = file->last_mtime;
651 if (ran && file->update_status != -1)
652 /* We actually tried to update FILE, which has
653 updated its also_make's as well (if it worked).
654 If it didn't work, it wouldn't work again for them.
655 So mark them as updated with the same status. */
656 for (d = file->also_make; d != 0; d = d->next)
658 d->file->command_state = cs_finished;
659 d->file->updated = 1;
660 d->file->update_status = file->update_status;
662 if (ran && !d->file->phony)
663 /* Fetch the new modification time.
664 We do this instead of just invalidating the cached time
665 so that a vpath_search can happen. Otherwise, it would
666 never be done because the target is already updated. */
667 (void) f_mtime (d->file, 0);
669 else if (file->update_status == -1)
670 /* Nothing was done for FILE, but it needed nothing done.
671 So mark it now as "succeeded". */
672 file->update_status = 0;
675 /* Check whether another file (whose mtime is THIS_MTIME)
676 needs updating on account of a dependency which is file FILE.
677 If it does, store 1 in *MUST_MAKE_PTR.
678 In the process, update any non-intermediate files
679 that FILE depends on (including FILE itself).
680 Return nonzero if any updating failed. */
682 static int
683 check_dep (file, depth, this_mtime, must_make_ptr)
684 struct file *file;
685 unsigned int depth;
686 time_t this_mtime;
687 int *must_make_ptr;
689 register struct dep *d;
690 int dep_status = 0;
692 ++depth;
693 file->updating = 1;
695 if (!file->intermediate)
696 /* If this is a non-intermediate file, update it and record
697 whether it is newer than THIS_MTIME. */
699 time_t mtime;
700 dep_status = update_file (file, depth);
701 check_renamed (file);
702 mtime = file_mtime (file);
703 check_renamed (file);
704 if (mtime == (time_t) -1 || mtime > this_mtime)
705 *must_make_ptr = 1;
707 else
709 /* FILE is an intermediate file.
710 Update all non-intermediate files we depend on, if necessary,
711 and see whether any of them is more recent than the file
712 on whose behalf we are checking. */
713 register struct dep *lastd;
714 lastd = 0;
715 d = file->deps;
716 while (d != 0)
718 if (d->file->updating)
720 error ("Circular %s <- %s dependency dropped.",
721 file->name, d->file->name);
722 if (lastd == 0)
724 file->deps = d->next;
725 free ((char *) d);
726 d = file->deps;
728 else
730 lastd->next = d->next;
731 free ((char *) d);
732 d = lastd->next;
734 continue;
737 d->file->parent = file;
738 dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
739 check_renamed (d->file);
740 if (dep_status != 0 && !keep_going_flag)
741 break;
743 if (d->file->command_state == cs_running
744 || d->file->command_state == cs_deps_running)
745 /* Record that some of FILE's dependencies are still being made.
746 This tells the upper levels to wait on processing it until
747 the commands are finished. */
748 set_command_state (file, cs_deps_running);
750 lastd = d;
751 d = d->next;
755 file->updating = 0;
756 return dep_status;
759 /* Touch FILE. Return zero if successful, one if not. */
761 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
763 static int
764 touch_file (file)
765 register struct file *file;
767 if (!silent_flag)
768 message (0, "touch %s", file->name);
770 #ifndef NO_ARCHIVES
771 if (ar_name (file->name))
772 return ar_touch (file->name);
773 else
774 #endif
776 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
778 if (fd < 0)
779 TOUCH_ERROR ("touch: open: ");
780 else
782 struct stat statbuf;
783 char buf;
784 int status;
786 #ifdef EINTR
788 #endif
789 status = fstat (fd, &statbuf);
790 #ifdef EINTR
791 while (status < 0 && errno == EINTR);
792 #endif
793 if (status < 0)
794 TOUCH_ERROR ("touch: fstat: ");
795 /* Rewrite character 0 same as it already is. */
796 if (read (fd, &buf, 1) < 0)
797 TOUCH_ERROR ("touch: read: ");
798 if (lseek (fd, 0L, 0) < 0L)
799 TOUCH_ERROR ("touch: lseek: ");
800 if (write (fd, &buf, 1) < 0)
801 TOUCH_ERROR ("touch: write: ");
802 /* If file length was 0, we just
803 changed it, so change it back. */
804 if (statbuf.st_size == 0)
806 (void) close (fd);
807 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
808 if (fd < 0)
809 TOUCH_ERROR ("touch: open: ");
811 (void) close (fd);
815 return 0;
818 /* Having checked and updated the dependencies of FILE,
819 do whatever is appropriate to remake FILE itself.
820 Return the status from executing FILE's commands. */
822 static void
823 remake_file (file)
824 struct file *file;
826 if (file->cmds == 0)
828 if (file->phony)
829 /* Phony target. Pretend it succeeded. */
830 file->update_status = 0;
831 else if (file->is_target)
832 /* This is a nonexistent target file we cannot make.
833 Pretend it was successfully remade. */
834 file->update_status = 0;
835 else
837 /* This is a dependency file we cannot remake. Fail. */
838 static const char msg_noparent[]
839 = "%sNo rule to make target `%s'%s";
840 static const char msg_parent[]
841 = "%sNo rule to make target `%s', needed by `%s'%s";
842 if (keep_going_flag || file->dontcare)
844 if (!file->dontcare)
846 if (file->parent == 0)
847 error (msg_noparent, "*** ", file->name, ".");
848 else
849 error (msg_parent, "*** ",
850 file->name, file->parent->name, ".");
852 file->update_status = 2;
854 else
856 if (file->parent == 0)
857 fatal (msg_noparent, "", file->name, "");
858 else
859 fatal (msg_parent, "", file->name, file->parent->name, "");
863 else
865 chop_commands (file->cmds);
867 if (!touch_flag || file->cmds->any_recurse)
869 execute_file_commands (file);
870 return;
872 else
873 /* This tells notice_finished_file it is ok to touch the file. */
874 file->update_status = 0;
877 /* This does the touching under -t. */
878 notice_finished_file (file);
881 /* Return the mtime of a file, given a `struct file'.
882 Caches the time in the struct file to avoid excess stat calls.
884 If the file is not found, and SEARCH is nonzero, VPATH searching and
885 replacement is done. If that fails, a library (-lLIBNAME) is tried and
886 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
887 FILE. */
889 time_t
890 f_mtime (file, search)
891 register struct file *file;
892 int search;
894 time_t mtime;
896 /* File's mtime is not known; must get it from the system. */
898 #ifndef NO_ARCHIVES
899 if (ar_name (file->name))
901 /* This file is an archive-member reference. */
903 char *arname, *memname;
904 struct file *arfile;
905 int arname_used = 0;
907 /* Find the archive's name. */
908 ar_parse_name (file->name, &arname, &memname);
910 /* Find the modification time of the archive itself.
911 Also allow for its name to be changed via VPATH search. */
912 arfile = lookup_file (arname);
913 if (arfile == 0)
915 arfile = enter_file (arname);
916 arname_used = 1;
918 mtime = f_mtime (arfile, search);
919 check_renamed (arfile);
920 if (search && strcmp (arfile->name, arname))
922 /* The archive's name has changed.
923 Change the archive-member reference accordingly. */
925 unsigned int arlen, memlen;
927 if (!arname_used)
929 free (arname);
930 arname_used = 1;
933 arname = arfile->name;
934 arlen = strlen (arname);
935 memlen = strlen (memname);
937 free (file->name);
939 file->name = (char *) xmalloc (arlen + 1 + memlen + 2);
940 bcopy (arname, file->name, arlen);
941 file->name[arlen] = '(';
942 bcopy (memname, file->name + arlen + 1, memlen);
943 file->name[arlen + 1 + memlen] = ')';
944 file->name[arlen + 1 + memlen + 1] = '\0';
947 if (!arname_used)
948 free (arname);
949 free (memname);
951 if (mtime == (time_t) -1)
952 /* The archive doesn't exist, so it's members don't exist either. */
953 return (time_t) -1;
955 mtime = ar_member_date (file->name);
957 else
958 #endif
960 mtime = name_mtime (file->name);
962 if (mtime == (time_t) -1 && search)
964 /* If name_mtime failed, search VPATH. */
965 char *name = file->name;
966 if (vpath_search (&name, &mtime)
967 /* Last resort, is it a library (-lxxx)? */
968 || (name[0] == '-' && name[1] == 'l'
969 && library_search (&name, &mtime)))
971 if (mtime != 0)
972 /* vpath_search and library_search store zero in MTIME
973 if they didn't need to do a stat call for their work. */
974 file->last_mtime = mtime;
975 rename_file (file, name);
976 check_renamed (file);
977 return file_mtime (file);
982 /* Store the mtime into all the entries for this file. */
983 if (file->double_colon)
984 file = file->double_colon;
987 file->last_mtime = mtime;
988 file = file->prev;
989 } while (file != 0);
991 return mtime;
995 /* Return the mtime of the file or archive-member reference NAME. */
997 static time_t
998 name_mtime (name)
999 register char *name;
1001 struct stat st;
1003 if (stat (name, &st) < 0)
1004 return (time_t) -1;
1006 return (time_t) st.st_mtime;
1010 /* Search for a library file specified as -lLIBNAME, searching for a
1011 suitable library file in the system library directories and the VPATH
1012 directories. */
1014 static int
1015 library_search (lib, mtime_ptr)
1016 char **lib;
1017 time_t *mtime_ptr;
1019 static char *dirs[] =
1021 "/lib",
1022 "/usr/lib",
1023 LIBDIR, /* Defined by configuration. */
1027 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1028 time_t mtime;
1030 /* Buffer to construct possible names in. */
1031 char *buf = xmalloc (sizeof (LIBDIR) + 8 + strlen (libname) + 4 + 2 + 1);
1032 char *file, **dp;
1034 /* Look first for `libNAME.a' in the current directory. */
1036 sprintf (buf, "lib%s.a", libname);
1037 mtime = name_mtime (buf);
1038 if (mtime != (time_t) -1)
1040 *lib = buf;
1041 if (mtime_ptr != 0)
1042 *mtime_ptr = mtime;
1043 return 1;
1046 /* Now try VPATH search on that. */
1048 file = buf;
1049 if (vpath_search (&file, mtime_ptr))
1051 free (buf);
1052 *lib = file;
1053 return 1;
1056 /* Now try the standard set of directories. */
1058 for (dp = dirs; *dp != 0; ++dp)
1060 sprintf (buf, "%s/lib%s.a", *dp, libname);
1061 mtime = name_mtime (buf);
1062 if (mtime != (time_t) -1)
1064 *lib = buf;
1065 if (mtime_ptr != 0)
1066 *mtime_ptr = mtime;
1067 return 1;
1071 free (buf);
1072 return 0;