VirtualBox

source: kBuild/vendor/gnumake/current/remake.c@ 2596

Last change on this file since 2596 was 2596, checked in by bird, 12 years ago

gnumake/current -> 3.82-cvs.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette