source: trunk/src/gmake/remake.c@ 503

Last change on this file since 503 was 503, checked in by bird, 19 years ago

Untested merge with GNU Make v3.81 (vendor/gnumake/2005-05-16 -> vendor/gnumake/current).

  • Property svn:eol-style set to native
File size: 46.7 KB
Line 
1/* Basic dependency engine for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20#include "filedef.h"
21#include "job.h"
22#include "commands.h"
23#include "dep.h"
24#include "variable.h"
25#include "debug.h"
26
27#include <assert.h>
28
29#ifdef HAVE_FCNTL_H
30#include <fcntl.h>
31#else
32#include <sys/file.h>
33#endif
34
35#ifdef VMS
36#include <starlet.h>
37#endif
38#ifdef WINDOWS32
39#include <io.h>
40#endif
41
42extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
43
44
45/* The test for circular dependencies is based on the 'updating' bit in
46 `struct file'. However, double colon targets have seperate `struct
47 file's; make sure we always use the base of the double colon chain. */
48
49#define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
50 ->updating = 1)
51#define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 0)
53#define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating)
55
56
57/* Incremented when a command is started (under -n, when one would be). */
58unsigned int commands_started = 0;
59
60/* Current value for pruning the scan of the goal chain (toggle 0/1). */
61static unsigned int considered;
62
63static int update_file PARAMS ((struct file *file, unsigned int depth));
64static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
65static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
66static int touch_file PARAMS ((struct file *file));
67static void remake_file PARAMS ((struct file *file));
68static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
69static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
70
71
72
73/* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
74 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
75
76 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
77 and -n should be disabled for them unless they were also command-line
78 targets, and we should only make one goal at a time and return as soon as
79 one goal whose `changed' member is nonzero is successfully made. */
80
81int
82update_goal_chain (struct dep *goals)
83{
84 int t = touch_flag, q = question_flag, n = just_print_flag;
85 unsigned int j = job_slots;
86 int status = -1;
87
88#define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
89 : file_mtime (file))
90
91 /* Duplicate the chain so we can remove things from it. */
92
93 goals = copy_dep_chain (goals);
94
95 {
96 /* Clear the `changed' flag of each goal in the chain.
97 We will use the flag below to notice when any commands
98 have actually been run for a target. When no commands
99 have been run, we give an "up to date" diagnostic. */
100
101 struct dep *g;
102 for (g = goals; g != 0; g = g->next)
103 g->changed = 0;
104 }
105
106 /* All files start with the considered bit 0, so the global value is 1. */
107 considered = 1;
108
109 /* Update all the goals until they are all finished. */
110
111 while (goals != 0)
112 {
113 register struct dep *g, *lastgoal;
114
115 /* Start jobs that are waiting for the load to go down. */
116
117 start_waiting_jobs ();
118
119 /* Wait for a child to die. */
120
121 reap_children (1, 0);
122
123 lastgoal = 0;
124 g = goals;
125 while (g != 0)
126 {
127 /* Iterate over all double-colon entries for this file. */
128 struct file *file;
129 int stop = 0, any_not_updated = 0;
130
131 for (file = g->file->double_colon ? g->file->double_colon : g->file;
132 file != NULL;
133 file = file->prev)
134 {
135 unsigned int ocommands_started;
136 int x;
137 check_renamed (file);
138 if (rebuilding_makefiles)
139 {
140 if (file->cmd_target)
141 {
142 touch_flag = t;
143 question_flag = q;
144 just_print_flag = n;
145 }
146 else
147 touch_flag = question_flag = just_print_flag = 0;
148 }
149
150 /* Save the old value of `commands_started' so we can compare
151 later. It will be incremented when any commands are
152 actually run. */
153 ocommands_started = commands_started;
154
155 x = update_file (file, rebuilding_makefiles ? 1 : 0);
156 check_renamed (file);
157
158 /* Set the goal's `changed' flag if any commands were started
159 by calling update_file above. We check this flag below to
160 decide when to give an "up to date" diagnostic. */
161 if (commands_started > ocommands_started)
162 g->changed = 1;
163
164 /* If we updated a file and STATUS was not already 1, set it to
165 1 if updating failed, or to 0 if updating succeeded. Leave
166 STATUS as it is if no updating was done. */
167
168 stop = 0;
169 if ((x != 0 || file->updated) && status < 1)
170 {
171 if (file->update_status != 0)
172 {
173 /* Updating failed, or -q triggered. The STATUS value
174 tells our caller which. */
175 status = file->update_status;
176 /* If -q just triggered, stop immediately. It doesn't
177 matter how much more we run, since we already know
178 the answer to return. */
179 stop = (question_flag && !keep_going_flag
180 && !rebuilding_makefiles);
181 }
182 else
183 {
184 FILE_TIMESTAMP mtime = MTIME (file);
185 check_renamed (file);
186
187 if (file->updated && g->changed &&
188 mtime != file->mtime_before_update)
189 {
190 /* Updating was done. If this is a makefile and
191 just_print_flag or question_flag is set (meaning
192 -n or -q was given and this file was specified
193 as a command-line target), don't change STATUS.
194 If STATUS is changed, we will get re-exec'd, and
195 enter an infinite loop. */
196 if (!rebuilding_makefiles
197 || (!just_print_flag && !question_flag))
198 status = 0;
199 if (rebuilding_makefiles && file->dontcare)
200 /* This is a default makefile; stop remaking. */
201 stop = 1;
202 }
203 }
204 }
205
206 /* Keep track if any double-colon entry is not finished.
207 When they are all finished, the goal is finished. */
208 any_not_updated |= !file->updated;
209
210 if (stop)
211 break;
212 }
213
214 /* Reset FILE since it is null at the end of the loop. */
215 file = g->file;
216
217 if (stop || !any_not_updated)
218 {
219 /* If we have found nothing whatever to do for the goal,
220 print a message saying nothing needs doing. */
221
222 if (!rebuilding_makefiles
223 /* If the update_status is zero, we updated successfully
224 or not at all. G->changed will have been set above if
225 any commands were actually started for this goal. */
226 && file->update_status == 0 && !g->changed
227 /* Never give a message under -s or -q. */
228 && !silent_flag && !question_flag)
229 message (1, ((file->phony || file->cmds == 0)
230 ? _("Nothing to be done for `%s'.")
231 : _("`%s' is up to date.")),
232 file->name);
233
234 /* This goal is finished. Remove it from the chain. */
235 if (lastgoal == 0)
236 goals = g->next;
237 else
238 lastgoal->next = g->next;
239
240 /* Free the storage. */
241 free ((char *) g);
242
243 g = lastgoal == 0 ? goals : lastgoal->next;
244
245 if (stop)
246 break;
247 }
248 else
249 {
250 lastgoal = g;
251 g = g->next;
252 }
253 }
254
255 /* If we reached the end of the dependency graph toggle the considered
256 flag for the next pass. */
257 if (g == 0)
258 considered = !considered;
259 }
260
261 if (rebuilding_makefiles)
262 {
263 touch_flag = t;
264 question_flag = q;
265 just_print_flag = n;
266 job_slots = j;
267 }
268
269 return status;
270}
271
272
273/* If FILE is not up to date, execute the commands for it.
274 Return 0 if successful, 1 if unsuccessful;
275 but with some flag settings, just call `exit' if unsuccessful.
276
277 DEPTH is the depth in recursions of this function.
278 We increment it during the consideration of our dependencies,
279 then decrement it again after finding out whether this file
280 is out of date.
281
282 If there are multiple double-colon entries for FILE,
283 each is considered in turn. */
284
285static int
286update_file (struct file *file, unsigned int depth)
287{
288 register int status = 0;
289 register struct file *f;
290
291 f = file->double_colon ? file->double_colon : file;
292
293 /* Prune the dependency graph: if we've already been here on _this_
294 pass through the dependency graph, we don't have to go any further.
295 We won't reap_children until we start the next pass, so no state
296 change is possible below here until then. */
297 if (f->considered == considered)
298 {
299 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
300 return f->command_state == cs_finished ? f->update_status : 0;
301 }
302
303 /* This loop runs until we start commands for a double colon rule, or until
304 the chain is exhausted. */
305 for (; f != 0; f = f->prev)
306 {
307 f->considered = considered;
308
309 status |= update_file_1 (f, depth);
310 check_renamed (f);
311
312 /* Clean up any alloca() used during the update. */
313 alloca (0);
314
315 /* If we got an error, don't bother with double_colon etc. */
316 if (status != 0 && !keep_going_flag)
317 return status;
318
319 if (f->command_state == cs_running
320 || f->command_state == cs_deps_running)
321 {
322 /* Don't run the other :: rules for this
323 file until this rule is finished. */
324 status = 0;
325 break;
326 }
327 }
328
329 /* Process the remaining rules in the double colon chain so they're marked
330 considered. Start their prerequisites, too. */
331 if (file->double_colon)
332 for (; f != 0 ; f = f->prev)
333 {
334 struct dep *d;
335
336 f->considered = considered;
337
338 for (d = f->deps; d != 0; d = d->next)
339 status |= update_file (d->file, depth + 1);
340 }
341
342 return status;
343}
344
345
346/* Show a message stating the target failed to build. */
347
348static void
349complain (const struct file *file)
350{
351 const char *msg_noparent
352 = _("%sNo rule to make target `%s'%s");
353 const char *msg_parent
354 = _("%sNo rule to make target `%s', needed by `%s'%s");
355
356 if (!keep_going_flag)
357 {
358 if (file->parent == 0)
359 fatal (NILF, msg_noparent, "", file->name, "");
360
361 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
362 }
363
364 if (file->parent == 0)
365 error (NILF, msg_noparent, "*** ", file->name, ".");
366 else
367 error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
368}
369
370/* Consider a single `struct file' and update it as appropriate. */
371
372static int
373update_file_1 (struct file *file, unsigned int depth)
374{
375 register FILE_TIMESTAMP this_mtime;
376 int noexist, must_make, deps_changed;
377 int dep_status = 0;
378 register struct dep *d, *lastd;
379 int running = 0;
380
381 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
382
383 if (file->updated)
384 {
385 if (file->update_status > 0)
386 {
387 DBF (DB_VERBOSE,
388 _("Recently tried and failed to update file `%s'.\n"));
389
390 /* If the file we tried to make is marked dontcare then no message
391 was printed about it when it failed during the makefile rebuild.
392 If we're trying to build it again in the normal rebuild, print a
393 message now. */
394 if (file->dontcare && !rebuilding_makefiles)
395 {
396 file->dontcare = 0;
397 complain (file);
398 }
399
400 return file->update_status;
401 }
402
403 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
404 return 0;
405 }
406
407 switch (file->command_state)
408 {
409 case cs_not_started:
410 case cs_deps_running:
411 break;
412 case cs_running:
413 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
414 return 0;
415 case cs_finished:
416 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
417 return file->update_status;
418 default:
419 abort ();
420 }
421
422 ++depth;
423
424 /* Notice recursive update of the same file. */
425 start_updating (file);
426
427 /* Looking at the file's modtime beforehand allows the possibility
428 that its name may be changed by a VPATH search, and thus it may
429 not need an implicit rule. If this were not done, the file
430 might get implicit commands that apply to its initial name, only
431 to have that name replaced with another found by VPATH search. */
432
433 this_mtime = file_mtime (file);
434 check_renamed (file);
435 noexist = this_mtime == NONEXISTENT_MTIME;
436 if (noexist)
437 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
438 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
439 && file->low_resolution_time)
440 {
441 /* Avoid spurious rebuilds due to low resolution time stamps. */
442 int ns = FILE_TIMESTAMP_NS (this_mtime);
443 if (ns != 0)
444 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
445 file->name);
446 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
447 }
448
449 must_make = noexist;
450
451 /* If file was specified as a target with no commands,
452 come up with some default commands. */
453
454 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
455 {
456 if (try_implicit_rule (file, depth))
457 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
458 else
459 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
460 file->tried_implicit = 1;
461 }
462 if (file->cmds == 0 && !file->is_target
463 && default_file != 0 && default_file->cmds != 0)
464 {
465 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
466 file->cmds = default_file->cmds;
467 }
468
469 /* Update all non-intermediate files we depend on, if necessary,
470 and see whether any of them is more recent than this file. */
471
472 lastd = 0;
473 d = file->deps;
474 while (d != 0)
475 {
476 FILE_TIMESTAMP mtime;
477 int maybe_make;
478 int dontcare = 0;
479
480 check_renamed (d->file);
481
482 mtime = file_mtime (d->file);
483 check_renamed (d->file);
484
485 if (is_updating (d->file))
486 {
487 error (NILF, _("Circular %s <- %s dependency dropped."),
488 file->name, d->file->name);
489 /* We cannot free D here because our the caller will still have
490 a reference to it when we were called recursively via
491 check_dep below. */
492 if (lastd == 0)
493 file->deps = d->next;
494 else
495 lastd->next = d->next;
496 d = d->next;
497 continue;
498 }
499
500 d->file->parent = file;
501 maybe_make = must_make;
502
503 /* Inherit dontcare flag from our parent. */
504 if (rebuilding_makefiles)
505 {
506 dontcare = d->file->dontcare;
507 d->file->dontcare = file->dontcare;
508 }
509
510
511 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
512
513 /* Restore original dontcare flag. */
514 if (rebuilding_makefiles)
515 d->file->dontcare = dontcare;
516
517 if (! d->ignore_mtime)
518 must_make = maybe_make;
519
520 check_renamed (d->file);
521
522 {
523 register struct file *f = d->file;
524 if (f->double_colon)
525 f = f->double_colon;
526 do
527 {
528 running |= (f->command_state == cs_running
529 || f->command_state == cs_deps_running);
530 f = f->prev;
531 }
532 while (f != 0);
533 }
534
535 if (dep_status != 0 && !keep_going_flag)
536 break;
537
538 if (!running)
539 /* The prereq is considered changed if the timestamp has changed while
540 it was built, OR it doesn't exist.
541 This causes the Linux kernel build to break. We'll defer this
542 fix until GNU make 3.82 to give them time to update. */
543 d->changed = ((file_mtime (d->file) != mtime)
544 /* || (mtime == NONEXISTENT_MTIME) */);
545
546 lastd = d;
547 d = d->next;
548 }
549
550 /* Now we know whether this target needs updating.
551 If it does, update all the intermediate files we depend on. */
552
553 if (must_make || always_make_flag)
554 {
555 for (d = file->deps; d != 0; d = d->next)
556 if (d->file->intermediate)
557 {
558 int dontcare = 0;
559
560 FILE_TIMESTAMP mtime = file_mtime (d->file);
561 check_renamed (d->file);
562 d->file->parent = file;
563
564 /* Inherit dontcare flag from our parent. */
565 if (rebuilding_makefiles)
566 {
567 dontcare = d->file->dontcare;
568 d->file->dontcare = file->dontcare;
569 }
570
571
572 dep_status |= update_file (d->file, depth);
573
574 /* Restore original dontcare flag. */
575 if (rebuilding_makefiles)
576 d->file->dontcare = dontcare;
577
578 check_renamed (d->file);
579
580 {
581 register struct file *f = d->file;
582 if (f->double_colon)
583 f = f->double_colon;
584 do
585 {
586 running |= (f->command_state == cs_running
587 || f->command_state == cs_deps_running);
588 f = f->prev;
589 }
590 while (f != 0);
591 }
592
593 if (dep_status != 0 && !keep_going_flag)
594 break;
595
596 if (!running)
597 d->changed = ((file->phony && file->cmds != 0)
598 || file_mtime (d->file) != mtime);
599 }
600 }
601
602 finish_updating (file);
603
604 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
605
606 if (running)
607 {
608 set_command_state (file, cs_deps_running);
609 --depth;
610 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
611 return 0;
612 }
613
614 /* If any dependency failed, give up now. */
615
616 if (dep_status != 0)
617 {
618 file->update_status = dep_status;
619 notice_finished_file (file);
620
621 --depth;
622
623 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
624
625 if (depth == 0 && keep_going_flag
626 && !just_print_flag && !question_flag)
627 error (NILF,
628 _("Target `%s' not remade because of errors."), file->name);
629
630 return dep_status;
631 }
632
633 if (file->command_state == cs_deps_running)
634 /* The commands for some deps were running on the last iteration, but
635 they have finished now. Reset the command_state to not_started to
636 simplify later bookkeeping. It is important that we do this only
637 when the prior state was cs_deps_running, because that prior state
638 was definitely propagated to FILE's also_make's by set_command_state
639 (called above), but in another state an also_make may have
640 independently changed to finished state, and we would confuse that
641 file's bookkeeping (updated, but not_started is bogus state). */
642 set_command_state (file, cs_not_started);
643
644 /* Now record which prerequisites are more
645 recent than this file, so we can define $?. */
646
647 deps_changed = 0;
648 for (d = file->deps; d != 0; d = d->next)
649 {
650 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
651 check_renamed (d->file);
652
653 if (! d->ignore_mtime)
654 {
655#if 1
656 /* %%% In version 4, remove this code completely to
657 implement not remaking deps if their deps are newer
658 than their parents. */
659 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
660 /* We must remake if this dep does not
661 exist and is not intermediate. */
662 must_make = 1;
663#endif
664
665 /* Set DEPS_CHANGED if this dep actually changed. */
666 deps_changed |= d->changed;
667 }
668
669 /* Set D->changed if either this dep actually changed,
670 or its dependent, FILE, is older or does not exist. */
671 d->changed |= noexist || d_mtime > this_mtime;
672
673 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
674 {
675 const char *fmt = 0;
676
677 if (d->ignore_mtime)
678 {
679 if (ISDB (DB_VERBOSE))
680 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
681 }
682 else if (d_mtime == NONEXISTENT_MTIME)
683 {
684 if (ISDB (DB_BASIC))
685 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
686 }
687 else if (d->changed)
688 {
689 if (ISDB (DB_BASIC))
690 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
691 }
692 else if (ISDB (DB_VERBOSE))
693 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
694
695 if (fmt)
696 {
697 print_spaces (depth);
698 printf (fmt, dep_name (d), file->name);
699 fflush (stdout);
700 }
701 }
702 }
703
704 /* Here depth returns to the value it had when we were called. */
705 depth--;
706
707 if (file->double_colon && file->deps == 0)
708 {
709 must_make = 1;
710 DBF (DB_BASIC,
711 _("Target `%s' is double-colon and has no prerequisites.\n"));
712 }
713 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
714 && !always_make_flag)
715 {
716 must_make = 0;
717 DBF (DB_VERBOSE,
718 _("No commands for `%s' and no prerequisites actually changed.\n"));
719 }
720 else if (!must_make && file->cmds != 0 && always_make_flag)
721 {
722 must_make = 1;
723 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
724 }
725
726 if (!must_make)
727 {
728 if (ISDB (DB_VERBOSE))
729 {
730 print_spaces (depth);
731 printf (_("No need to remake target `%s'"), file->name);
732 if (!streq (file->name, file->hname))
733 printf (_("; using VPATH name `%s'"), file->hname);
734 puts (".");
735 fflush (stdout);
736 }
737
738 notice_finished_file (file);
739
740 /* Since we don't need to remake the file, convert it to use the
741 VPATH filename if we found one. hfile will be either the
742 local name if no VPATH or the VPATH name if one was found. */
743
744 while (file)
745 {
746 file->name = file->hname;
747 file = file->prev;
748 }
749
750 return 0;
751 }
752
753 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
754
755 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
756 VPATH. */
757 if (!streq(file->name, file->hname))
758 {
759 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
760 file->ignore_vpath = 1;
761 }
762
763 /* Now, take appropriate actions to remake the file. */
764 remake_file (file);
765
766 if (file->command_state != cs_finished)
767 {
768 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
769 return 0;
770 }
771
772 switch (file->update_status)
773 {
774 case 2:
775 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
776 break;
777 case 0:
778 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
779 break;
780 case 1:
781 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
782 break;
783 default:
784 assert (file->update_status >= 0 && file->update_status <= 2);
785 break;
786 }
787
788 file->updated = 1;
789 return file->update_status;
790}
791
792
793/* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
794 files listed in its `also_make' member. Under -t, this function also
795 touches FILE.
796
797 On return, FILE->update_status will no longer be -1 if it was. */
798
799void
800notice_finished_file (struct file *file)
801{
802 struct dep *d;
803 int ran = file->command_state == cs_running;
804 int touched = 0;
805 DB (DB_JOBS, (_("notice_finished_file - entering: file=%p `%s' update_status=%d command_state=%d\n"),
806 file, file->name, file->update_status, file->command_state));
807 file->command_state = cs_finished;
808 file->updated = 1;
809
810 /* update not_parallel if the file was flagged for that. */
811 if (ran && (file->command_flags & (COMMANDS_NOTPARALLEL | COMMANDS_NO_COMMANDS))
812 == COMMANDS_NOTPARALLEL)
813 {
814 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s')\n"), not_parallel,
815 not_parallel - 1, file, file->name));
816 assert(not_parallel >= 1);
817 --not_parallel;
818 }
819
820 if (touch_flag
821 /* The update status will be:
822 -1 if this target was not remade;
823 0 if 0 or more commands (+ or ${MAKE}) were run and won;
824 1 if some commands were run and lost.
825 We touch the target if it has commands which either were not run
826 or won when they ran (i.e. status is 0). */
827 && file->update_status == 0)
828 {
829 if (file->cmds != 0 && file->cmds->any_recurse)
830 {
831 /* If all the command lines were recursive,
832 we don't want to do the touching. */
833 unsigned int i;
834 for (i = 0; i < file->cmds->ncommand_lines; ++i)
835 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
836 goto have_nonrecursing;
837 }
838 else
839 {
840 have_nonrecursing:
841 if (file->phony)
842 file->update_status = 0;
843 /* According to POSIX, -t doesn't affect targets with no cmds. */
844 else if (file->cmds != 0)
845 {
846 /* Should set file's modification date and do nothing else. */
847 file->update_status = touch_file (file);
848
849 /* Pretend we ran a real touch command, to suppress the
850 "`foo' is up to date" message. */
851 commands_started++;
852
853 /* Request for the timestamp to be updated (and distributed
854 to the double-colon entries). Simply setting ran=1 would
855 almost have done the trick, but messes up with the also_make
856 updating logic below. */
857 touched = 1;
858 }
859 }
860 }
861
862 if (file->mtime_before_update == UNKNOWN_MTIME)
863 file->mtime_before_update = file->last_mtime;
864
865 if ((ran && !file->phony) || touched)
866 {
867 int i = 0;
868
869 /* If -n, -t, or -q and all the commands are recursive, we ran them so
870 really check the target's mtime again. Otherwise, assume the target
871 would have been updated. */
872
873 if (question_flag || just_print_flag || touch_flag)
874 {
875 for (i = file->cmds->ncommand_lines; i > 0; --i)
876 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
877 break;
878 }
879
880 /* If there were no commands at all, it's always new. */
881
882 else if (file->is_target && file->cmds == 0)
883 i = 1;
884
885 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
886 }
887
888 if (file->double_colon)
889 {
890 /* If this is a double colon rule and it is the last one to be
891 updated, propagate the change of modification time to all the
892 double-colon entries for this file.
893
894 We do it on the last update because it is important to handle
895 individual entries as separate rules with separate timestamps
896 while they are treated as targets and then as one rule with the
897 unified timestamp when they are considered as a prerequisite
898 of some target. */
899
900 struct file *f;
901 FILE_TIMESTAMP max_mtime = file->last_mtime;
902
903 /* Check that all rules were updated and at the same time find
904 the max timestamp. We assume UNKNOWN_MTIME is newer then
905 any other value. */
906 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
907 if (max_mtime != UNKNOWN_MTIME
908 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
909 max_mtime = f->last_mtime;
910
911 if (f == 0)
912 for (f = file->double_colon; f != 0; f = f->prev)
913 f->last_mtime = max_mtime;
914 }
915
916 if (ran && file->update_status != -1)
917 /* We actually tried to update FILE, which has
918 updated its also_make's as well (if it worked).
919 If it didn't work, it wouldn't work again for them.
920 So mark them as updated with the same status. */
921 for (d = file->also_make; d != 0; d = d->next)
922 {
923 d->file->command_state = cs_finished;
924 d->file->updated = 1;
925 d->file->update_status = file->update_status;
926
927 if (ran && !d->file->phony)
928 /* Fetch the new modification time.
929 We do this instead of just invalidating the cached time
930 so that a vpath_search can happen. Otherwise, it would
931 never be done because the target is already updated. */
932 (void) f_mtime (d->file, 0);
933 }
934 else if (file->update_status == -1)
935 /* Nothing was done for FILE, but it needed nothing done.
936 So mark it now as "succeeded". */
937 file->update_status = 0;
938
939DB (DB_JOBS, (_("notice_finished_file - leaving: file=%p `%s' update_status=%d command_state=%d\n"), file, file->name, file->update_status, file->command_state));
940}
941
942
943/* Check whether another file (whose mtime is THIS_MTIME)
944 needs updating on account of a dependency which is file FILE.
945 If it does, store 1 in *MUST_MAKE_PTR.
946 In the process, update any non-intermediate files
947 that FILE depends on (including FILE itself).
948 Return nonzero if any updating failed. */
949
950static int
951check_dep (struct file *file, unsigned int depth,
952 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
953{
954 struct dep *d;
955 int dep_status = 0;
956
957 ++depth;
958 start_updating (file);
959
960 if (file->phony || !file->intermediate)
961 {
962 /* If this is a non-intermediate file, update it and record
963 whether it is newer than THIS_MTIME. */
964 FILE_TIMESTAMP mtime;
965 dep_status = update_file (file, depth);
966 check_renamed (file);
967 mtime = file_mtime (file);
968 check_renamed (file);
969 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
970 *must_make_ptr = 1;
971 }
972 else
973 {
974 /* FILE is an intermediate file. */
975 FILE_TIMESTAMP mtime;
976
977 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
978 {
979 if (try_implicit_rule (file, depth))
980 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
981 else
982 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
983 file->tried_implicit = 1;
984 }
985 if (file->cmds == 0 && !file->is_target
986 && default_file != 0 && default_file->cmds != 0)
987 {
988 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
989 file->cmds = default_file->cmds;
990 }
991
992 /* If the intermediate file actually exists
993 and is newer, then we should remake from it. */
994 check_renamed (file);
995 mtime = file_mtime (file);
996 check_renamed (file);
997 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
998 *must_make_ptr = 1;
999 /* Otherwise, update all non-intermediate files we depend on,
1000 if necessary, and see whether any of them is more
1001 recent than the file on whose behalf we are checking. */
1002 else
1003 {
1004 struct dep *lastd;
1005
1006 lastd = 0;
1007 d = file->deps;
1008 while (d != 0)
1009 {
1010 int maybe_make;
1011
1012 if (is_updating (d->file))
1013 {
1014 error (NILF, _("Circular %s <- %s dependency dropped."),
1015 file->name, d->file->name);
1016 if (lastd == 0)
1017 {
1018 file->deps = d->next;
1019 free_dep (d);
1020 d = file->deps;
1021 }
1022 else
1023 {
1024 lastd->next = d->next;
1025 free_dep (d);
1026 d = lastd->next;
1027 }
1028 continue;
1029 }
1030
1031 d->file->parent = file;
1032 maybe_make = *must_make_ptr;
1033 dep_status |= check_dep (d->file, depth, this_mtime,
1034 &maybe_make);
1035 if (! d->ignore_mtime)
1036 *must_make_ptr = maybe_make;
1037 check_renamed (d->file);
1038 if (dep_status != 0 && !keep_going_flag)
1039 break;
1040
1041 if (d->file->command_state == cs_running
1042 || d->file->command_state == cs_deps_running)
1043 /* Record that some of FILE's deps are still being made.
1044 This tells the upper levels to wait on processing it until
1045 the commands are finished. */
1046 set_command_state (file, cs_deps_running);
1047
1048 lastd = d;
1049 d = d->next;
1050 }
1051 }
1052 }
1053
1054 finish_updating (file);
1055 return dep_status;
1056}
1057
1058
1059/* Touch FILE. Return zero if successful, one if not. */
1060
1061#define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1062
1063static int
1064touch_file (struct file *file)
1065{
1066 if (!silent_flag)
1067 message (0, "touch %s", file->name);
1068
1069#ifndef NO_ARCHIVES
1070 if (ar_name (file->name))
1071 return ar_touch (file->name);
1072 else
1073#endif
1074 {
1075 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1076
1077 if (fd < 0)
1078 TOUCH_ERROR ("touch: open: ");
1079 else
1080 {
1081 struct stat statbuf;
1082 char buf;
1083 int e;
1084
1085 EINTRLOOP (e, fstat (fd, &statbuf));
1086 if (e < 0)
1087 TOUCH_ERROR ("touch: fstat: ");
1088 /* Rewrite character 0 same as it already is. */
1089 if (read (fd, &buf, 1) < 0)
1090 TOUCH_ERROR ("touch: read: ");
1091 if (lseek (fd, 0L, 0) < 0L)
1092 TOUCH_ERROR ("touch: lseek: ");
1093 if (write (fd, &buf, 1) < 0)
1094 TOUCH_ERROR ("touch: write: ");
1095 /* If file length was 0, we just
1096 changed it, so change it back. */
1097 if (statbuf.st_size == 0)
1098 {
1099 (void) close (fd);
1100 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1101 if (fd < 0)
1102 TOUCH_ERROR ("touch: open: ");
1103 }
1104 (void) close (fd);
1105 }
1106 }
1107
1108 return 0;
1109}
1110
1111
1112/* Having checked and updated the dependencies of FILE,
1113 do whatever is appropriate to remake FILE itself.
1114 Return the status from executing FILE's commands. */
1115
1116static void
1117remake_file (struct file *file)
1118{
1119 if (file->cmds == 0)
1120 {
1121 if (file->phony)
1122 /* Phony target. Pretend it succeeded. */
1123 file->update_status = 0;
1124 else if (file->is_target)
1125 /* This is a nonexistent target file we cannot make.
1126 Pretend it was successfully remade. */
1127 file->update_status = 0;
1128 else
1129 {
1130 /* This is a dependency file we cannot remake. Fail. */
1131 if (!rebuilding_makefiles || !file->dontcare)
1132 complain (file);
1133 file->update_status = 2;
1134 }
1135 }
1136 else
1137 {
1138 chop_commands (file->cmds);
1139
1140 /* The normal case: start some commands. */
1141 if (!touch_flag || file->cmds->any_recurse)
1142 {
1143 execute_file_commands (file);
1144 return;
1145 }
1146
1147 /* This tells notice_finished_file it is ok to touch the file. */
1148 file->update_status = 0;
1149 }
1150
1151 /* This does the touching under -t. */
1152 notice_finished_file (file);
1153}
1154
1155
1156/* Return the mtime of a file, given a `struct file'.
1157 Caches the time in the struct file to avoid excess stat calls.
1158
1159 If the file is not found, and SEARCH is nonzero, VPATH searching and
1160 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1161 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1162 FILE. */
1163
1164FILE_TIMESTAMP
1165f_mtime (struct file *file, int search)
1166{
1167 FILE_TIMESTAMP mtime;
1168
1169 /* File's mtime is not known; must get it from the system. */
1170
1171#ifndef NO_ARCHIVES
1172 if (ar_name (file->name))
1173 {
1174 /* This file is an archive-member reference. */
1175
1176 char *arname, *memname;
1177 struct file *arfile;
1178 int arname_used = 0;
1179 time_t member_date;
1180
1181 /* Find the archive's name. */
1182 ar_parse_name (file->name, &arname, &memname);
1183
1184 /* Find the modification time of the archive itself.
1185 Also allow for its name to be changed via VPATH search. */
1186 arfile = lookup_file (arname);
1187 if (arfile == 0)
1188 {
1189 arfile = enter_file (arname);
1190 arname_used = 1;
1191 }
1192 mtime = f_mtime (arfile, search);
1193 check_renamed (arfile);
1194 if (search && strcmp (arfile->hname, arname))
1195 {
1196 /* The archive's name has changed.
1197 Change the archive-member reference accordingly. */
1198
1199 char *name;
1200 unsigned int arlen, memlen;
1201
1202 if (!arname_used)
1203 {
1204 free (arname);
1205 arname_used = 1;
1206 }
1207
1208 arname = arfile->hname;
1209 arlen = strlen (arname);
1210 memlen = strlen (memname);
1211
1212 /* free (file->name); */
1213
1214 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1215 bcopy (arname, name, arlen);
1216 name[arlen] = '(';
1217 bcopy (memname, name + arlen + 1, memlen);
1218 name[arlen + 1 + memlen] = ')';
1219 name[arlen + 1 + memlen + 1] = '\0';
1220
1221 /* If the archive was found with GPATH, make the change permanent;
1222 otherwise defer it until later. */
1223 if (arfile->name == arfile->hname)
1224 rename_file (file, name);
1225 else
1226 rehash_file (file, name);
1227 check_renamed (file);
1228 }
1229
1230 if (!arname_used)
1231 free (arname);
1232 free (memname);
1233
1234 file->low_resolution_time = 1;
1235
1236 if (mtime == NONEXISTENT_MTIME)
1237 /* The archive doesn't exist, so its members don't exist either. */
1238 return NONEXISTENT_MTIME;
1239
1240 member_date = ar_member_date (file->hname);
1241 mtime = (member_date == (time_t) -1
1242 ? NONEXISTENT_MTIME
1243 : file_timestamp_cons (file->hname, member_date, 0));
1244 }
1245 else
1246#endif
1247 {
1248 mtime = name_mtime (file->name);
1249
1250 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1251 {
1252 /* If name_mtime failed, search VPATH. */
1253 char *name = file->name;
1254 if (vpath_search (&name, &mtime)
1255 /* Last resort, is it a library (-lxxx)? */
1256 || (name[0] == '-' && name[1] == 'l'
1257 && library_search (&name, &mtime)))
1258 {
1259 if (mtime != UNKNOWN_MTIME)
1260 /* vpath_search and library_search store UNKNOWN_MTIME
1261 if they didn't need to do a stat call for their work. */
1262 file->last_mtime = mtime;
1263
1264 /* If we found it in VPATH, see if it's in GPATH too; if so,
1265 change the name right now; if not, defer until after the
1266 dependencies are updated. */
1267 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1268 {
1269 rename_file (file, name);
1270 check_renamed (file);
1271 return file_mtime (file);
1272 }
1273
1274 rehash_file (file, name);
1275 check_renamed (file);
1276 /* If the result of a vpath search is -o or -W, preserve it.
1277 Otherwise, find the mtime of the resulting file. */
1278 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1279 mtime = name_mtime (name);
1280 }
1281 }
1282 }
1283
1284 /* Files can have bogus timestamps that nothing newly made will be
1285 "newer" than. Updating their dependents could just result in loops.
1286 So notify the user of the anomaly with a warning.
1287
1288 We only need to do this once, for now. */
1289
1290 if (!clock_skew_detected
1291 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1292 && !file->updated)
1293 {
1294 static FILE_TIMESTAMP adjusted_now;
1295
1296 FILE_TIMESTAMP adjusted_mtime = mtime;
1297
1298#if defined(WINDOWS32) || defined(__MSDOS__)
1299 /* Experimentation has shown that FAT filesystems can set file times
1300 up to 3 seconds into the future! Play it safe. */
1301
1302#define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1303
1304 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1305 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1306 adjusted_mtime -= adjustment;
1307#elif defined(__EMX__)
1308 /* FAT filesystems round time to the nearest even second!
1309 Allow for any file (NTFS or FAT) to perhaps suffer from this
1310 brain damage. */
1311 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1312 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1313 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1314 : 0);
1315#endif
1316
1317 /* If the file's time appears to be in the future, update our
1318 concept of the present and try once more. */
1319 if (adjusted_now < adjusted_mtime)
1320 {
1321 int resolution;
1322 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1323 adjusted_now = now + (resolution - 1);
1324 if (adjusted_now < adjusted_mtime)
1325 {
1326#ifdef NO_FLOAT
1327 error (NILF, _("Warning: File `%s' has modification time in the future"),
1328 file->name);
1329#else
1330 double from_now =
1331 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1332 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1333 / 1e9));
1334 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1335 file->name, from_now);
1336#endif
1337 clock_skew_detected = 1;
1338 }
1339 }
1340 }
1341
1342 /* Store the mtime into all the entries for this file. */
1343 if (file->double_colon)
1344 file = file->double_colon;
1345
1346 do
1347 {
1348 /* If this file is not implicit but it is intermediate then it was
1349 made so by the .INTERMEDIATE target. If this file has never
1350 been built by us but was found now, it existed before make
1351 started. So, turn off the intermediate bit so make doesn't
1352 delete it, since it didn't create it. */
1353 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1354 && file->command_state == cs_not_started
1355 && !file->tried_implicit && file->intermediate)
1356 file->intermediate = 0;
1357
1358 file->last_mtime = mtime;
1359 file = file->prev;
1360 }
1361 while (file != 0);
1362
1363 return mtime;
1364}
1365
1366
1367/* Return the mtime of the file or archive-member reference NAME. */
1368
1369/* First, we check with stat(). If the file does not exist, then we return
1370 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1371 examine each indirection of the symlink and find the newest mtime.
1372 This causes one duplicate stat() when -L is being used, but the code is
1373 much cleaner. */
1374
1375static FILE_TIMESTAMP
1376name_mtime (char *name)
1377{
1378 FILE_TIMESTAMP mtime;
1379 struct stat st;
1380 int e;
1381
1382 EINTRLOOP (e, stat (name, &st));
1383 if (e == 0)
1384 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1385 else if (errno == ENOENT || errno == ENOTDIR)
1386 mtime = NONEXISTENT_MTIME;
1387 else
1388 {
1389 perror_with_name ("stat: ", name);
1390 return NONEXISTENT_MTIME;
1391 }
1392
1393 /* If we get here we either found it, or it doesn't exist.
1394 If it doesn't exist see if we can use a symlink mtime instead. */
1395
1396#ifdef MAKE_SYMLINKS
1397#ifndef S_ISLNK
1398# define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1399#endif
1400 if (check_symlink_flag)
1401 {
1402 PATH_VAR (lpath);
1403
1404 /* Check each symbolic link segment (if any). Find the latest mtime
1405 amongst all of them (and the target file of course).
1406 Note that we have already successfully dereferenced all the links
1407 above. So, if we run into any error trying to lstat(), or
1408 readlink(), or whatever, something bizarre-o happened. Just give up
1409 and use whatever mtime we've already computed at that point. */
1410 strcpy (lpath, name);
1411 while (1)
1412 {
1413 FILE_TIMESTAMP ltime;
1414 PATH_VAR (lbuf);
1415 long llen;
1416 char *p;
1417
1418 EINTRLOOP (e, lstat (lpath, &st));
1419 if (e)
1420 {
1421 /* Just take what we have so far. */
1422 if (errno != ENOENT && errno != ENOTDIR)
1423 perror_with_name ("lstat: ", lpath);
1424 break;
1425 }
1426
1427 /* If this is not a symlink, we're done (we started with the real
1428 file's mtime so we don't need to test it again). */
1429 if (!S_ISLNK (st.st_mode))
1430 break;
1431
1432 /* If this mtime is newer than what we had, keep the new one. */
1433 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1434 if (ltime > mtime)
1435 mtime = ltime;
1436
1437 /* Set up to check the file pointed to by this link. */
1438 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1439 if (llen < 0)
1440 {
1441 /* Eh? Just take what we have. */
1442 perror_with_name ("readlink: ", lpath);
1443 break;
1444 }
1445 lbuf[llen] = '\0';
1446
1447 /* If the target is fully-qualified or the source is just a
1448 filename, then the new path is the target. Otherwise it's the
1449 source directory plus the target. */
1450 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1451 strcpy (lpath, lbuf);
1452 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1453 /* Eh? Path too long! Again, just go with what we have. */
1454 break;
1455 else
1456 /* Create the next step in the symlink chain. */
1457 strcpy (p+1, lbuf);
1458 }
1459 }
1460#endif
1461
1462 return mtime;
1463}
1464
1465
1466/* Search for a library file specified as -lLIBNAME, searching for a
1467 suitable library file in the system library directories and the VPATH
1468 directories. */
1469
1470static int
1471library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1472{
1473 static char *dirs[] =
1474 {
1475#ifndef _AMIGA
1476 "/lib",
1477 "/usr/lib",
1478#endif
1479#if defined(WINDOWS32) && !defined(LIBDIR)
1480/*
1481 * This is completely up to the user at product install time. Just define
1482 * a placeholder.
1483 */
1484#define LIBDIR "."
1485#endif
1486 LIBDIR, /* Defined by configuration. */
1487 0
1488 };
1489
1490 static char *libpatterns = NULL;
1491
1492 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1493 FILE_TIMESTAMP mtime;
1494
1495 /* Loop variables for the libpatterns value. */
1496 char *p, *p2;
1497 unsigned int len;
1498
1499 char *file, **dp;
1500
1501 /* If we don't have libpatterns, get it. */
1502 if (!libpatterns)
1503 {
1504 int save = warn_undefined_variables_flag;
1505 warn_undefined_variables_flag = 0;
1506
1507 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1508
1509 warn_undefined_variables_flag = save;
1510 }
1511
1512 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1513 p2 = libpatterns;
1514 while ((p = find_next_token (&p2, &len)) != 0)
1515 {
1516 static char *buf = NULL;
1517 static unsigned int buflen = 0;
1518 static int libdir_maxlen = -1;
1519 char *libbuf = variable_expand ("");
1520
1521 /* Expand the pattern using LIBNAME as a replacement. */
1522 {
1523 char c = p[len];
1524 char *p3, *p4;
1525
1526 p[len] = '\0';
1527 p3 = find_percent (p);
1528 if (!p3)
1529 {
1530 /* Give a warning if there is no pattern, then remove the
1531 pattern so it's ignored next time. */
1532 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1533 for (; len; --len, ++p)
1534 *p = ' ';
1535 *p = c;
1536 continue;
1537 }
1538 p4 = variable_buffer_output (libbuf, p, p3-p);
1539 p4 = variable_buffer_output (p4, libname, strlen (libname));
1540 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1541 p[len] = c;
1542 }
1543
1544 /* Look first for `libNAME.a' in the current directory. */
1545 mtime = name_mtime (libbuf);
1546 if (mtime != NONEXISTENT_MTIME)
1547 {
1548 *lib = xstrdup (libbuf);
1549 if (mtime_ptr != 0)
1550 *mtime_ptr = mtime;
1551 return 1;
1552 }
1553
1554 /* Now try VPATH search on that. */
1555
1556 file = libbuf;
1557 if (vpath_search (&file, mtime_ptr))
1558 {
1559 *lib = file;
1560 return 1;
1561 }
1562
1563 /* Now try the standard set of directories. */
1564
1565 if (!buflen)
1566 {
1567 for (dp = dirs; *dp != 0; ++dp)
1568 {
1569 int l = strlen (*dp);
1570 if (l > libdir_maxlen)
1571 libdir_maxlen = l;
1572 }
1573 buflen = strlen (libbuf);
1574 buf = xmalloc(libdir_maxlen + buflen + 2);
1575 }
1576 else if (buflen < strlen (libbuf))
1577 {
1578 buflen = strlen (libbuf);
1579 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1580 }
1581
1582 for (dp = dirs; *dp != 0; ++dp)
1583 {
1584 sprintf (buf, "%s/%s", *dp, libbuf);
1585 mtime = name_mtime (buf);
1586 if (mtime != NONEXISTENT_MTIME)
1587 {
1588 *lib = xstrdup (buf);
1589 if (mtime_ptr != 0)
1590 *mtime_ptr = mtime;
1591 return 1;
1592 }
1593 }
1594 }
1595
1596 return 0;
1597}
Note: See TracBrowser for help on using the repository browser.