VirtualBox

source: kBuild/trunk/src/kmk/remake.c@ 1019

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

bad assertion.

  • Property svn:eol-style set to native
File size: 51.5 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, unless of course, if it's
385 a multi-maybe file with its order only dependency on the primary file. */
386 if (file->multi_head != NULL && file->multi_head != file)
387 {
388 if (!file->multi_maybe)
389 {
390 DBS (DB_VERBOSE, (_("Considering target file `%s' -> switching to multi head `%s'.\n"),
391 file->name, file->multi_head->name));
392 file = file->multi_head;
393 }
394 else
395 DBF (DB_VERBOSE, _("Considering target file `%s'. (multi-maybe)\n"));
396 /* XXX: optimize dependencies. */
397 }
398 else
399#endif /* CONFIG_WITH_EXPLICIT_MULTITARGET */
400 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
401
402 if (file->updated)
403 {
404 if (file->update_status > 0)
405 {
406 DBF (DB_VERBOSE,
407 _("Recently tried and failed to update file `%s'.\n"));
408
409 /* If the file we tried to make is marked dontcare then no message
410 was printed about it when it failed during the makefile rebuild.
411 If we're trying to build it again in the normal rebuild, print a
412 message now. */
413 if (file->dontcare && !rebuilding_makefiles)
414 {
415 file->dontcare = 0;
416 complain (file);
417 }
418
419 return file->update_status;
420 }
421
422 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
423 return 0;
424 }
425
426 switch (file->command_state)
427 {
428 case cs_not_started:
429 case cs_deps_running:
430 break;
431 case cs_running:
432 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
433 return 0;
434 case cs_finished:
435 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
436 return file->update_status;
437 default:
438 abort ();
439 }
440
441 ++depth;
442
443 /* Notice recursive update of the same file. */
444 start_updating (file);
445
446 /* Looking at the file's modtime beforehand allows the possibility
447 that its name may be changed by a VPATH search, and thus it may
448 not need an implicit rule. If this were not done, the file
449 might get implicit commands that apply to its initial name, only
450 to have that name replaced with another found by VPATH search. */
451
452 this_mtime = file_mtime (file);
453 check_renamed (file);
454 noexist = this_mtime == NONEXISTENT_MTIME;
455 if (noexist)
456 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
457 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
458 && file->low_resolution_time)
459 {
460 /* Avoid spurious rebuilds due to low resolution time stamps. */
461 int ns = FILE_TIMESTAMP_NS (this_mtime);
462 if (ns != 0)
463 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
464 file->name);
465 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
466 }
467
468 must_make = noexist;
469
470 /* If file was specified as a target with no commands,
471 come up with some default commands. */
472
473 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
474 {
475 if (try_implicit_rule (file, depth))
476 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
477 else
478 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
479 file->tried_implicit = 1;
480 }
481 if (file->cmds == 0 && !file->is_target
482 && default_file != 0 && default_file->cmds != 0)
483 {
484 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
485 file->cmds = default_file->cmds;
486 }
487
488 /* Update all non-intermediate files we depend on, if necessary,
489 and see whether any of them is more recent than this file.
490 For explicit multitarget rules we must iterate all the output
491 files to get the correct picture (this means re-evaluating
492 shared dependencies - bad). */
493
494#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
495 for (dep_file = file; dep_file;
496 dep_file = file == file->multi_head ? dep_file->multi_next : NULL)
497 {
498 lastd = 0;
499 d = dep_file->deps;
500#else
501 lastd = 0;
502 d = file->deps;
503#endif
504 while (d != 0)
505 {
506 FILE_TIMESTAMP mtime;
507 int maybe_make;
508 int dontcare = 0;
509
510 check_renamed (d->file);
511
512 mtime = file_mtime (d->file);
513 check_renamed (d->file);
514
515 if (is_updating (d->file))
516 {
517#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
518 /* silently ignore the order-only dep hack. */
519 if (dep_file->multi_maybe && d->file == file)
520 {
521 lastd = d;
522 d = d->next;
523 continue;
524 }
525#endif
526
527 error (NILF, _("Circular %s <- %s dependency dropped."),
528#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
529 dep_file->name, d->file->name);
530#else
531 file->name, d->file->name);
532#endif
533 /* We cannot free D here because our the caller will still have
534 a reference to it when we were called recursively via
535 check_dep below. */
536 if (lastd == 0)
537#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
538 dep_file->deps = d->next;
539#else
540 file->deps = d->next;
541#endif
542 else
543 lastd->next = d->next;
544 d = d->next;
545 continue;
546 }
547
548#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
549 d->file->parent = dep_file;
550#else
551 d->file->parent = file;
552#endif
553 maybe_make = must_make;
554
555 /* Inherit dontcare flag from our parent. */
556 if (rebuilding_makefiles)
557 {
558 dontcare = d->file->dontcare;
559 d->file->dontcare = file->dontcare;
560 }
561
562
563 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
564
565 /* Restore original dontcare flag. */
566 if (rebuilding_makefiles)
567 d->file->dontcare = dontcare;
568
569 if (! d->ignore_mtime)
570 must_make = maybe_make;
571
572 check_renamed (d->file);
573
574 {
575 register struct file *f = d->file;
576 if (f->double_colon)
577 f = f->double_colon;
578 do
579 {
580 running |= (f->command_state == cs_running
581 || f->command_state == cs_deps_running);
582 f = f->prev;
583 }
584 while (f != 0);
585 }
586
587 if (dep_status != 0 && !keep_going_flag)
588 break;
589
590 if (!running)
591 /* The prereq is considered changed if the timestamp has changed while
592 it was built, OR it doesn't exist. */
593 d->changed = ((file_mtime (d->file) != mtime)
594 || (mtime == NONEXISTENT_MTIME));
595
596 lastd = d;
597 d = d->next;
598 }
599#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
600 if (dep_status != 0 && !keep_going_flag)
601 break;
602 }
603#endif
604
605 /* Now we know whether this target needs updating.
606 If it does, update all the intermediate files we depend on. */
607
608 if (must_make || always_make_flag)
609 {
610#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
611 for (dep_file = file; dep_file;
612 dep_file = file == file->multi_head ? dep_file->multi_next : NULL)
613 for (d = dep_file->deps; d != 0; d = d->next)
614#else
615 for (d = file->deps; d != 0; d = d->next)
616#endif
617 if (d->file->intermediate)
618 {
619 int dontcare = 0;
620
621 FILE_TIMESTAMP mtime = file_mtime (d->file);
622 check_renamed (d->file);
623#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
624 d->file->parent = dep_file;
625#else
626 d->file->parent = file;
627#endif
628
629 /* Inherit dontcare flag from our parent. */
630 if (rebuilding_makefiles)
631 {
632 dontcare = d->file->dontcare;
633 d->file->dontcare = file->dontcare;
634 }
635
636
637 dep_status |= update_file (d->file, depth);
638
639 /* Restore original dontcare flag. */
640 if (rebuilding_makefiles)
641 d->file->dontcare = dontcare;
642
643 check_renamed (d->file);
644
645 {
646 register struct file *f = d->file;
647 if (f->double_colon)
648 f = f->double_colon;
649 do
650 {
651 running |= (f->command_state == cs_running
652 || f->command_state == cs_deps_running);
653 f = f->prev;
654 }
655 while (f != 0);
656 }
657
658 if (dep_status != 0 && !keep_going_flag)
659 break;
660
661 if (!running)
662#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
663 d->changed = ((dep_file->phony && dep_file->cmds != 0)
664#else
665 d->changed = ((file->phony && file->cmds != 0)
666#endif
667 || file_mtime (d->file) != mtime);
668 }
669 }
670
671 finish_updating (file);
672
673 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
674
675 if (running)
676 {
677 set_command_state (file, cs_deps_running);
678 --depth;
679 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
680 return 0;
681 }
682
683 /* If any dependency failed, give up now. */
684
685 if (dep_status != 0)
686 {
687 file->update_status = dep_status;
688 notice_finished_file (file);
689
690 --depth;
691
692 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
693
694 if (depth == 0 && keep_going_flag
695 && !just_print_flag && !question_flag)
696 error (NILF,
697 _("Target `%s' not remade because of errors."), file->name);
698
699 return dep_status;
700 }
701
702 if (file->command_state == cs_deps_running)
703 /* The commands for some deps were running on the last iteration, but
704 they have finished now. Reset the command_state to not_started to
705 simplify later bookkeeping. It is important that we do this only
706 when the prior state was cs_deps_running, because that prior state
707 was definitely propagated to FILE's also_make's by set_command_state
708 (called above), but in another state an also_make may have
709 independently changed to finished state, and we would confuse that
710 file's bookkeeping (updated, but not_started is bogus state). */
711 set_command_state (file, cs_not_started);
712
713 /* Now record which prerequisites are more
714 recent than this file, so we can define $?. */
715
716 deps_changed = 0;
717#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
718 for (dep_file = file; dep_file;
719 dep_file = file == file->multi_head ? dep_file->multi_next : NULL)
720#endif
721 for (d = file->deps; d != 0; d = d->next)
722 {
723 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
724#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
725 if (d->file == file && dep_file->multi_maybe)
726 continue;
727#endif
728 check_renamed (d->file);
729
730 if (! d->ignore_mtime)
731 {
732#if 1
733 /* %%% In version 4, remove this code completely to
734 implement not remaking deps if their deps are newer
735 than their parents. */
736 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
737 /* We must remake if this dep does not
738 exist and is not intermediate. */
739 must_make = 1;
740#endif
741
742 /* Set DEPS_CHANGED if this dep actually changed. */
743 deps_changed |= d->changed;
744 }
745
746 /* Set D->changed if either this dep actually changed,
747 or its dependent, FILE, is older or does not exist. */
748 d->changed |= noexist || d_mtime > this_mtime;
749
750 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
751 {
752 const char *fmt = 0;
753
754 if (d->ignore_mtime)
755 {
756 if (ISDB (DB_VERBOSE))
757 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
758 }
759 else if (d_mtime == NONEXISTENT_MTIME)
760 {
761 if (ISDB (DB_BASIC))
762 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
763 }
764 else if (d->changed)
765 {
766 if (ISDB (DB_BASIC))
767 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
768 }
769 else if (ISDB (DB_VERBOSE))
770 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
771
772 if (fmt)
773 {
774 print_spaces (depth);
775 printf (fmt, dep_name (d), file->name);
776 fflush (stdout);
777 }
778 }
779 }
780
781 /* Here depth returns to the value it had when we were called. */
782 depth--;
783
784#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
785 /* maybe-update targets in a multi target should have been remade
786 by now, so return before we remake it again. */
787 if (file->multi_maybe)
788 {
789 assert (file->multi_head->updated);
790 assert (file->update_status <= 0);
791 DBF (DB_VERBOSE, _("Finished maybe-update file `%s'.\n"));
792 return 0;
793 }
794#endif
795
796 if (file->double_colon && file->deps == 0)
797 {
798 must_make = 1;
799 DBF (DB_BASIC,
800 _("Target `%s' is double-colon and has no prerequisites.\n"));
801 }
802 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
803 && !always_make_flag)
804 {
805 must_make = 0;
806 DBF (DB_VERBOSE,
807 _("No commands for `%s' and no prerequisites actually changed.\n"));
808 }
809 else if (!must_make && file->cmds != 0 && always_make_flag)
810 {
811 must_make = 1;
812 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
813 }
814
815 if (!must_make)
816 {
817 if (ISDB (DB_VERBOSE))
818 {
819 print_spaces (depth);
820 printf (_("No need to remake target `%s'"), file->name);
821 if (!streq (file->name, file->hname))
822 printf (_("; using VPATH name `%s'"), file->hname);
823 puts (".");
824 fflush (stdout);
825 }
826
827 notice_finished_file (file);
828
829 /* Since we don't need to remake the file, convert it to use the
830 VPATH filename if we found one. hfile will be either the
831 local name if no VPATH or the VPATH name if one was found. */
832
833 while (file)
834 {
835 file->name = file->hname;
836 file = file->prev;
837 }
838
839 return 0;
840 }
841
842#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
843 if (ISDB(DB_BASIC) && file->multi_head && file->multi_head != file)
844 DBS (DB_BASIC, (_("Must remake target `%s' - primary target `%s'.\n"), file->name, file->multi_head->name));
845 else
846#endif
847 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
848
849 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
850 VPATH. */
851 if (!streq(file->name, file->hname))
852 {
853 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
854 file->ignore_vpath = 1;
855 }
856
857 /* Now, take appropriate actions to remake the file. */
858 remake_file (file);
859
860 if (file->command_state != cs_finished)
861 {
862 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
863 return 0;
864 }
865
866 switch (file->update_status)
867 {
868 case 2:
869 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
870 break;
871 case 0:
872 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
873 break;
874 case 1:
875 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
876 break;
877 default:
878 assert (file->update_status >= 0 && file->update_status <= 2);
879 break;
880 }
881
882 file->updated = 1;
883 return file->update_status;
884}
885
886
887/* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
888 files listed in its `also_make' member. Under -t, this function also
889 touches FILE.
890
891 On return, FILE->update_status will no longer be -1 if it was. */
892
893void
894notice_finished_file (struct file *file)
895{
896 struct dep *d;
897 int ran = file->command_state == cs_running;
898 int touched = 0;
899 DB (DB_JOBS, (_("notice_finished_file - entering: file=%p `%s' update_status=%d command_state=%d\n"), /* bird */
900 file, file->name, file->update_status, file->command_state));
901 file->command_state = cs_finished;
902 file->updated = 1;
903
904#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
905 /* update not_parallel if the file was flagged for that. */
906 if ( ran
907 && (file->command_flags & (COMMANDS_NOTPARALLEL | COMMANDS_NO_COMMANDS))
908 == COMMANDS_NOTPARALLEL)
909 {
910 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [notice_finished_file]\n"), not_parallel,
911 not_parallel - 1, file, file->name));
912 assert(not_parallel >= 1);
913 --not_parallel;
914 }
915#endif
916
917 if (touch_flag
918 /* The update status will be:
919 -1 if this target was not remade;
920 0 if 0 or more commands (+ or ${MAKE}) were run and won;
921 1 if some commands were run and lost.
922 We touch the target if it has commands which either were not run
923 or won when they ran (i.e. status is 0). */
924 && file->update_status == 0)
925 {
926 if (file->cmds != 0 && file->cmds->any_recurse)
927 {
928 /* If all the command lines were recursive,
929 we don't want to do the touching. */
930 unsigned int i;
931 for (i = 0; i < file->cmds->ncommand_lines; ++i)
932 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
933 goto have_nonrecursing;
934 }
935 else
936 {
937 have_nonrecursing:
938 if (file->phony)
939 file->update_status = 0;
940 /* According to POSIX, -t doesn't affect targets with no cmds. */
941 else if (file->cmds != 0)
942 {
943 /* Should set file's modification date and do nothing else. */
944 file->update_status = touch_file (file);
945
946 /* Pretend we ran a real touch command, to suppress the
947 "`foo' is up to date" message. */
948 commands_started++;
949
950 /* Request for the timestamp to be updated (and distributed
951 to the double-colon entries). Simply setting ran=1 would
952 almost have done the trick, but messes up with the also_make
953 updating logic below. */
954 touched = 1;
955 }
956 }
957 }
958
959 if (file->mtime_before_update == UNKNOWN_MTIME)
960 file->mtime_before_update = file->last_mtime;
961
962 if ((ran && !file->phony) || touched)
963 {
964 int i = 0;
965
966 /* If -n, -t, or -q and all the commands are recursive, we ran them so
967 really check the target's mtime again. Otherwise, assume the target
968 would have been updated. */
969
970 if (question_flag || just_print_flag || touch_flag)
971 {
972 for (i = file->cmds->ncommand_lines; i > 0; --i)
973 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
974 break;
975 }
976
977 /* If there were no commands at all, it's always new. */
978
979 else if (file->is_target && file->cmds == 0)
980 i = 1;
981
982 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
983 }
984
985 if (file->double_colon)
986 {
987 /* If this is a double colon rule and it is the last one to be
988 updated, propagate the change of modification time to all the
989 double-colon entries for this file.
990
991 We do it on the last update because it is important to handle
992 individual entries as separate rules with separate timestamps
993 while they are treated as targets and then as one rule with the
994 unified timestamp when they are considered as a prerequisite
995 of some target. */
996
997 struct file *f;
998 FILE_TIMESTAMP max_mtime = file->last_mtime;
999
1000 /* Check that all rules were updated and at the same time find
1001 the max timestamp. We assume UNKNOWN_MTIME is newer then
1002 any other value. */
1003 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
1004 if (max_mtime != UNKNOWN_MTIME
1005 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
1006 max_mtime = f->last_mtime;
1007
1008 if (f == 0)
1009 for (f = file->double_colon; f != 0; f = f->prev)
1010 f->last_mtime = max_mtime;
1011 }
1012
1013 if (ran && file->update_status != -1)
1014#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1015 {
1016#endif
1017 /* We actually tried to update FILE, which has
1018 updated its also_make's as well (if it worked).
1019 If it didn't work, it wouldn't work again for them.
1020 So mark them as updated with the same status. */
1021 for (d = file->also_make; d != 0; d = d->next)
1022 {
1023 d->file->command_state = cs_finished;
1024 d->file->updated = 1;
1025 d->file->update_status = file->update_status;
1026
1027 if (ran && !d->file->phony)
1028 /* Fetch the new modification time.
1029 We do this instead of just invalidating the cached time
1030 so that a vpath_search can happen. Otherwise, it would
1031 never be done because the target is already updated. */
1032 f_mtime (d->file, 0);
1033 }
1034#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1035 /* Same as above but for explicit multi target rules. */
1036 if (file->multi_head)
1037 {
1038 struct file *f2;
1039 assert (file == file->multi_head);
1040 for (f2 = file->multi_next; f2 != 0; f2 = f2->multi_next)
1041 {
1042 f2->command_state = cs_finished;
1043 f2->updated = 1;
1044 f2->update_status = file->update_status;
1045
1046 if (!f2->phony)
1047 f_mtime (f2, 0);
1048 }
1049 }
1050 }
1051#endif
1052 else if (file->update_status == -1)
1053 /* Nothing was done for FILE, but it needed nothing done.
1054 So mark it now as "succeeded". */
1055 file->update_status = 0;
1056}
1057
1058
1059/* Check whether another file (whose mtime is THIS_MTIME) needs updating on
1060 account of a dependency which is file FILE. If it does, store 1 in
1061 *MUST_MAKE_PTR. In the process, update any non-intermediate files that
1062 FILE depends on (including FILE itself). Return nonzero if any updating
1063 failed. */
1064
1065static int
1066check_dep (struct file *file, unsigned int depth,
1067 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
1068{
1069 struct dep *d;
1070 int dep_status = 0;
1071
1072 ++depth;
1073 start_updating (file);
1074
1075 if (file->phony || !file->intermediate)
1076 {
1077 /* If this is a non-intermediate file, update it and record whether it
1078 is newer than THIS_MTIME. */
1079 FILE_TIMESTAMP mtime;
1080 dep_status = update_file (file, depth);
1081 check_renamed (file);
1082 mtime = file_mtime (file);
1083 check_renamed (file);
1084 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
1085 *must_make_ptr = 1;
1086 }
1087 else
1088 {
1089 /* FILE is an intermediate file. */
1090 FILE_TIMESTAMP mtime;
1091
1092 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
1093 {
1094 if (try_implicit_rule (file, depth))
1095 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
1096 else
1097 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
1098 file->tried_implicit = 1;
1099 }
1100 if (file->cmds == 0 && !file->is_target
1101 && default_file != 0 && default_file->cmds != 0)
1102 {
1103 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
1104 file->cmds = default_file->cmds;
1105 }
1106
1107 check_renamed (file);
1108 mtime = file_mtime (file);
1109 check_renamed (file);
1110 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
1111 /* If the intermediate file actually exists and is newer, then we
1112 should remake from it. */
1113 *must_make_ptr = 1;
1114 else
1115 {
1116 /* Otherwise, update all non-intermediate files we depend on, if
1117 necessary, and see whether any of them is more recent than the
1118 file on whose behalf we are checking. */
1119 struct dep *lastd;
1120
1121 lastd = 0;
1122 d = file->deps;
1123 while (d != 0)
1124 {
1125 int maybe_make;
1126
1127 if (is_updating (d->file))
1128 {
1129 error (NILF, _("Circular %s <- %s dependency dropped."),
1130 file->name, d->file->name);
1131 if (lastd == 0)
1132 {
1133 file->deps = d->next;
1134 free_dep (d);
1135 d = file->deps;
1136 }
1137 else
1138 {
1139 lastd->next = d->next;
1140 free_dep (d);
1141 d = lastd->next;
1142 }
1143 continue;
1144 }
1145
1146 d->file->parent = file;
1147 maybe_make = *must_make_ptr;
1148 dep_status |= check_dep (d->file, depth, this_mtime,
1149 &maybe_make);
1150 if (! d->ignore_mtime)
1151 *must_make_ptr = maybe_make;
1152 check_renamed (d->file);
1153 if (dep_status != 0 && !keep_going_flag)
1154 break;
1155
1156 if (d->file->command_state == cs_running
1157 || d->file->command_state == cs_deps_running)
1158 /* Record that some of FILE's deps are still being made.
1159 This tells the upper levels to wait on processing it until
1160 the commands are finished. */
1161 set_command_state (file, cs_deps_running);
1162
1163 lastd = d;
1164 d = d->next;
1165 }
1166 }
1167 }
1168
1169 finish_updating (file);
1170 return dep_status;
1171}
1172
1173
1174/* Touch FILE. Return zero if successful, one if not. */
1175
1176#define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1177
1178static int
1179touch_file (struct file *file)
1180{
1181 if (!silent_flag)
1182 message (0, "touch %s", file->name);
1183
1184#ifndef NO_ARCHIVES
1185 if (ar_name (file->name))
1186 return ar_touch (file->name);
1187 else
1188#endif
1189 {
1190 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1191
1192 if (fd < 0)
1193 TOUCH_ERROR ("touch: open: ");
1194 else
1195 {
1196 struct stat statbuf;
1197 char buf = 'x';
1198 int e;
1199
1200 EINTRLOOP (e, fstat (fd, &statbuf));
1201 if (e < 0)
1202 TOUCH_ERROR ("touch: fstat: ");
1203 /* Rewrite character 0 same as it already is. */
1204 if (read (fd, &buf, 1) < 0)
1205 TOUCH_ERROR ("touch: read: ");
1206 if (lseek (fd, 0L, 0) < 0L)
1207 TOUCH_ERROR ("touch: lseek: ");
1208 if (write (fd, &buf, 1) < 0)
1209 TOUCH_ERROR ("touch: write: ");
1210 /* If file length was 0, we just
1211 changed it, so change it back. */
1212 if (statbuf.st_size == 0)
1213 {
1214 (void) close (fd);
1215 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1216 if (fd < 0)
1217 TOUCH_ERROR ("touch: open: ");
1218 }
1219 (void) close (fd);
1220 }
1221 }
1222
1223 return 0;
1224}
1225
1226
1227/* Having checked and updated the dependencies of FILE,
1228 do whatever is appropriate to remake FILE itself.
1229 Return the status from executing FILE's commands. */
1230
1231static void
1232remake_file (struct file *file)
1233{
1234#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1235 /* Always operate on the primary file. */
1236 if (file->multi_head && file->multi_head != file)
1237 file = file->multi_head;
1238#endif
1239
1240 if (file->cmds == 0)
1241 {
1242 if (file->phony)
1243 /* Phony target. Pretend it succeeded. */
1244 file->update_status = 0;
1245 else if (file->is_target)
1246 /* This is a nonexistent target file we cannot make.
1247 Pretend it was successfully remade. */
1248 file->update_status = 0;
1249 else
1250 {
1251 /* This is a dependency file we cannot remake. Fail. */
1252 if (!rebuilding_makefiles || !file->dontcare)
1253 complain (file);
1254 file->update_status = 2;
1255 }
1256 }
1257 else
1258 {
1259 chop_commands (file->cmds);
1260
1261 /* The normal case: start some commands. */
1262 if (!touch_flag || file->cmds->any_recurse)
1263 {
1264 execute_file_commands (file);
1265 return;
1266 }
1267
1268 /* This tells notice_finished_file it is ok to touch the file. */
1269 file->update_status = 0;
1270 }
1271
1272 /* This does the touching under -t. */
1273 notice_finished_file (file);
1274}
1275
1276
1277/* Return the mtime of a file, given a `struct file'.
1278 Caches the time in the struct file to avoid excess stat calls.
1279
1280 If the file is not found, and SEARCH is nonzero, VPATH searching and
1281 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1282 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1283 FILE. */
1284
1285FILE_TIMESTAMP
1286f_mtime (struct file *file, int search)
1287{
1288 FILE_TIMESTAMP mtime;
1289
1290 /* File's mtime is not known; must get it from the system. */
1291
1292#ifndef NO_ARCHIVES
1293 if (ar_name (file->name))
1294 {
1295 /* This file is an archive-member reference. */
1296
1297 char *arname, *memname;
1298 struct file *arfile;
1299 time_t member_date;
1300
1301 /* Find the archive's name. */
1302 ar_parse_name (file->name, &arname, &memname);
1303
1304 /* Find the modification time of the archive itself.
1305 Also allow for its name to be changed via VPATH search. */
1306 arfile = lookup_file (arname);
1307 if (arfile == 0)
1308 arfile = enter_file (strcache_add (arname));
1309 mtime = f_mtime (arfile, search);
1310 check_renamed (arfile);
1311 if (search && strcmp (arfile->hname, arname))
1312 {
1313 /* The archive's name has changed.
1314 Change the archive-member reference accordingly. */
1315
1316 char *name;
1317 unsigned int arlen, memlen;
1318
1319 arlen = strlen (arfile->hname);
1320 memlen = strlen (memname);
1321
1322 name = xmalloc (arlen + 1 + memlen + 2);
1323 memcpy (name, arfile->hname, arlen);
1324 name[arlen] = '(';
1325 memcpy (name + arlen + 1, memname, memlen);
1326 name[arlen + 1 + memlen] = ')';
1327 name[arlen + 1 + memlen + 1] = '\0';
1328
1329 /* If the archive was found with GPATH, make the change permanent;
1330 otherwise defer it until later. */
1331 if (arfile->name == arfile->hname)
1332 rename_file (file, name);
1333 else
1334 rehash_file (file, name);
1335 check_renamed (file);
1336 }
1337
1338 free (arname);
1339
1340 file->low_resolution_time = 1;
1341
1342 if (mtime == NONEXISTENT_MTIME)
1343 /* The archive doesn't exist, so its members don't exist either. */
1344 return NONEXISTENT_MTIME;
1345
1346 member_date = ar_member_date (file->hname);
1347 mtime = (member_date == (time_t) -1
1348 ? NONEXISTENT_MTIME
1349 : file_timestamp_cons (file->hname, member_date, 0));
1350 }
1351 else
1352#endif
1353 {
1354 mtime = name_mtime (file->name);
1355
1356 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1357 {
1358 /* If name_mtime failed, search VPATH. */
1359 const char *name = vpath_search (file->name, &mtime);
1360 if (name
1361 /* Last resort, is it a library (-lxxx)? */
1362 || (file->name[0] == '-' && file->name[1] == 'l'
1363 && (name = library_search (file->name, &mtime)) != 0))
1364 {
1365 if (mtime != UNKNOWN_MTIME)
1366 /* vpath_search and library_search store UNKNOWN_MTIME
1367 if they didn't need to do a stat call for their work. */
1368 file->last_mtime = mtime;
1369
1370 /* If we found it in VPATH, see if it's in GPATH too; if so,
1371 change the name right now; if not, defer until after the
1372 dependencies are updated. */
1373 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1374 {
1375 rename_file (file, name);
1376 check_renamed (file);
1377 return file_mtime (file);
1378 }
1379
1380 rehash_file (file, name);
1381 check_renamed (file);
1382 /* If the result of a vpath search is -o or -W, preserve it.
1383 Otherwise, find the mtime of the resulting file. */
1384 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1385 mtime = name_mtime (name);
1386 }
1387 }
1388 }
1389
1390 /* Files can have bogus timestamps that nothing newly made will be
1391 "newer" than. Updating their dependents could just result in loops.
1392 So notify the user of the anomaly with a warning.
1393
1394 We only need to do this once, for now. */
1395
1396 if (!clock_skew_detected
1397 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1398 && !file->updated)
1399 {
1400 static FILE_TIMESTAMP adjusted_now;
1401
1402 FILE_TIMESTAMP adjusted_mtime = mtime;
1403
1404#if defined(WINDOWS32) || defined(__MSDOS__)
1405 /* Experimentation has shown that FAT filesystems can set file times
1406 up to 3 seconds into the future! Play it safe. */
1407
1408#define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1409
1410 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1411 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1412 adjusted_mtime -= adjustment;
1413#elif defined(__EMX__)
1414 /* FAT filesystems round time to the nearest even second!
1415 Allow for any file (NTFS or FAT) to perhaps suffer from this
1416 brain damage. */
1417 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1418 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1419 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1420 : 0);
1421#endif
1422
1423 /* If the file's time appears to be in the future, update our
1424 concept of the present and try once more. */
1425 if (adjusted_now < adjusted_mtime)
1426 {
1427 int resolution;
1428 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1429 adjusted_now = now + (resolution - 1);
1430 if (adjusted_now < adjusted_mtime)
1431 {
1432#ifdef NO_FLOAT
1433 error (NILF, _("Warning: File `%s' has modification time in the future"),
1434 file->name);
1435#else
1436 double from_now =
1437 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1438 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1439 / 1e9));
1440 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1441 file->name, from_now);
1442#endif
1443 clock_skew_detected = 1;
1444 }
1445 }
1446 }
1447
1448 /* Store the mtime into all the entries for this file. */
1449 if (file->double_colon)
1450 file = file->double_colon;
1451
1452 do
1453 {
1454 /* If this file is not implicit but it is intermediate then it was
1455 made so by the .INTERMEDIATE target. If this file has never
1456 been built by us but was found now, it existed before make
1457 started. So, turn off the intermediate bit so make doesn't
1458 delete it, since it didn't create it. */
1459 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1460 && file->command_state == cs_not_started
1461 && !file->tried_implicit && file->intermediate)
1462 file->intermediate = 0;
1463
1464 file->last_mtime = mtime;
1465 file = file->prev;
1466 }
1467 while (file != 0);
1468
1469 return mtime;
1470}
1471
1472
1473/* Return the mtime of the file or archive-member reference NAME. */
1474
1475/* First, we check with stat(). If the file does not exist, then we return
1476 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1477 examine each indirection of the symlink and find the newest mtime.
1478 This causes one duplicate stat() when -L is being used, but the code is
1479 much cleaner. */
1480
1481static FILE_TIMESTAMP
1482name_mtime (const char *name)
1483{
1484 FILE_TIMESTAMP mtime;
1485 struct stat st;
1486 int e;
1487
1488 EINTRLOOP (e, stat (name, &st));
1489 if (e == 0)
1490 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1491 else if (errno == ENOENT || errno == ENOTDIR)
1492 mtime = NONEXISTENT_MTIME;
1493 else
1494 {
1495 perror_with_name ("stat: ", name);
1496 return NONEXISTENT_MTIME;
1497 }
1498
1499 /* If we get here we either found it, or it doesn't exist.
1500 If it doesn't exist see if we can use a symlink mtime instead. */
1501
1502#ifdef MAKE_SYMLINKS
1503#ifndef S_ISLNK
1504# define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1505#endif
1506 if (check_symlink_flag)
1507 {
1508 PATH_VAR (lpath);
1509
1510 /* Check each symbolic link segment (if any). Find the latest mtime
1511 amongst all of them (and the target file of course).
1512 Note that we have already successfully dereferenced all the links
1513 above. So, if we run into any error trying to lstat(), or
1514 readlink(), or whatever, something bizarre-o happened. Just give up
1515 and use whatever mtime we've already computed at that point. */
1516 strcpy (lpath, name);
1517 while (1)
1518 {
1519 FILE_TIMESTAMP ltime;
1520 PATH_VAR (lbuf);
1521 long llen;
1522 char *p;
1523
1524 EINTRLOOP (e, lstat (lpath, &st));
1525 if (e)
1526 {
1527 /* Just take what we have so far. */
1528 if (errno != ENOENT && errno != ENOTDIR)
1529 perror_with_name ("lstat: ", lpath);
1530 break;
1531 }
1532
1533 /* If this is not a symlink, we're done (we started with the real
1534 file's mtime so we don't need to test it again). */
1535 if (!S_ISLNK (st.st_mode))
1536 break;
1537
1538 /* If this mtime is newer than what we had, keep the new one. */
1539 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1540 if (ltime > mtime)
1541 mtime = ltime;
1542
1543 /* Set up to check the file pointed to by this link. */
1544 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1545 if (llen < 0)
1546 {
1547 /* Eh? Just take what we have. */
1548 perror_with_name ("readlink: ", lpath);
1549 break;
1550 }
1551 lbuf[llen] = '\0';
1552
1553 /* If the target is fully-qualified or the source is just a
1554 filename, then the new path is the target. Otherwise it's the
1555 source directory plus the target. */
1556 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1557 strcpy (lpath, lbuf);
1558 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1559 /* Eh? Path too long! Again, just go with what we have. */
1560 break;
1561 else
1562 /* Create the next step in the symlink chain. */
1563 strcpy (p+1, lbuf);
1564 }
1565 }
1566#endif
1567
1568 return mtime;
1569}
1570
1571
1572/* Search for a library file specified as -lLIBNAME, searching for a
1573 suitable library file in the system library directories and the VPATH
1574 directories. */
1575
1576static const char *
1577library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
1578{
1579 static char *dirs[] =
1580 {
1581#ifdef KMK
1582 ".",
1583#else /* !KMK */
1584#ifndef _AMIGA
1585 "/lib",
1586 "/usr/lib",
1587#endif
1588#if defined(WINDOWS32) && !defined(LIBDIR)
1589/*
1590 * This is completely up to the user at product install time. Just define
1591 * a placeholder.
1592 */
1593#define LIBDIR "."
1594#endif
1595# ifdef LIBDIR /* bird */
1596 LIBDIR, /* Defined by configuration. */
1597# else /* bird */
1598 ".", /* bird */
1599# endif /* bird */
1600#endif /* !KMK */
1601 0
1602 };
1603
1604 static char *libpatterns = NULL;
1605
1606 const char *libname = lib+2; /* Name without the '-l'. */
1607 FILE_TIMESTAMP mtime;
1608
1609 /* Loop variables for the libpatterns value. */
1610 char *p;
1611 const char *p2;
1612 unsigned int len;
1613
1614 char **dp;
1615
1616 /* If we don't have libpatterns, get it. */
1617 if (!libpatterns)
1618 {
1619 int save = warn_undefined_variables_flag;
1620 warn_undefined_variables_flag = 0;
1621
1622 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1623
1624 warn_undefined_variables_flag = save;
1625 }
1626
1627 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1628 p2 = libpatterns;
1629 while ((p = find_next_token (&p2, &len)) != 0)
1630 {
1631 static char *buf = NULL;
1632 static unsigned int buflen = 0;
1633 static int libdir_maxlen = -1;
1634 char *libbuf = variable_expand ("");
1635 const size_t libbuf_offset = libbuf - variable_buffer; /* bird */
1636
1637 /* Expand the pattern using LIBNAME as a replacement. */
1638 {
1639 char c = p[len];
1640 char *p3, *p4;
1641
1642 p[len] = '\0';
1643 p3 = find_percent (p);
1644 if (!p3)
1645 {
1646 /* Give a warning if there is no pattern, then remove the
1647 pattern so it's ignored next time. */
1648 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1649 for (; len; --len, ++p)
1650 *p = ' ';
1651 *p = c;
1652 continue;
1653 }
1654 p4 = variable_buffer_output (libbuf, p, p3-p);
1655 p4 = variable_buffer_output (p4, libname, strlen (libname));
1656 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1657 p[len] = c;
1658 libbuf = variable_buffer + libbuf_offset; /* bird - variable_buffer may have been reallocated. */
1659 }
1660
1661 /* Look first for `libNAME.a' in the current directory. */
1662 mtime = name_mtime (libbuf);
1663 if (mtime != NONEXISTENT_MTIME)
1664 {
1665 if (mtime_ptr != 0)
1666 *mtime_ptr = mtime;
1667 return strcache_add (libbuf);
1668 }
1669
1670 /* Now try VPATH search on that. */
1671
1672 {
1673 const char *file = vpath_search (libbuf, mtime_ptr);
1674 if (file)
1675 return file;
1676 }
1677
1678 /* Now try the standard set of directories. */
1679
1680 if (!buflen)
1681 {
1682 for (dp = dirs; *dp != 0; ++dp)
1683 {
1684 int l = strlen (*dp);
1685 if (l > libdir_maxlen)
1686 libdir_maxlen = l;
1687 }
1688 buflen = strlen (libbuf);
1689 buf = xmalloc(libdir_maxlen + buflen + 2);
1690 }
1691 else if (buflen < strlen (libbuf))
1692 {
1693 buflen = strlen (libbuf);
1694 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1695 }
1696
1697 for (dp = dirs; *dp != 0; ++dp)
1698 {
1699 sprintf (buf, "%s/%s", *dp, libbuf);
1700 mtime = name_mtime (buf);
1701 if (mtime != NONEXISTENT_MTIME)
1702 {
1703 if (mtime_ptr != 0)
1704 *mtime_ptr = mtime;
1705 return strcache_add (buf);
1706 }
1707 }
1708 }
1709
1710 return 0;
1711}
Note: See TracBrowser for help on using the repository browser.

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