source: trunk/src/kmk/remake.c@ 1020

Last change on this file since 1020 was 1020, checked in by bird, 18 years ago

no special casing for multi-maybe stuff. (it seems kind of unnecessary now)

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