VirtualBox

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

Last change on this file since 3387 was 3138, checked in by bird, 6 years ago

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use