VirtualBox

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

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

Consider the dependencies for all the target files for explicit multi target rules.

  • 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->updated);
791 assert (file->update_status <= 0);
792 DBF (DB_VERBOSE, _("Finished maybe-update file `%s'.\n"));
793 return 0;
794 }
795#endif
796
797 if (file->double_colon && file->deps == 0)
798 {
799 must_make = 1;
800 DBF (DB_BASIC,
801 _("Target `%s' is double-colon and has no prerequisites.\n"));
802 }
803 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
804 && !always_make_flag)
805 {
806 must_make = 0;
807 DBF (DB_VERBOSE,
808 _("No commands for `%s' and no prerequisites actually changed.\n"));
809 }
810 else if (!must_make && file->cmds != 0 && always_make_flag)
811 {
812 must_make = 1;
813 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
814 }
815
816 if (!must_make)
817 {
818 if (ISDB (DB_VERBOSE))
819 {
820 print_spaces (depth);
821 printf (_("No need to remake target `%s'"), file->name);
822 if (!streq (file->name, file->hname))
823 printf (_("; using VPATH name `%s'"), file->hname);
824 puts (".");
825 fflush (stdout);
826 }
827
828 notice_finished_file (file);
829
830 /* Since we don't need to remake the file, convert it to use the
831 VPATH filename if we found one. hfile will be either the
832 local name if no VPATH or the VPATH name if one was found. */
833
834 while (file)
835 {
836 file->name = file->hname;
837 file = file->prev;
838 }
839
840 return 0;
841 }
842
843#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
844 if (ISDB(DB_BASIC) && file->multi_head && file->multi_head != file)
845 DBS (DB_BASIC, (_("Must remake target `%s' - primary target `%s'.\n"), file->name, file->multi_head->name));
846 else
847#endif
848 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
849
850 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
851 VPATH. */
852 if (!streq(file->name, file->hname))
853 {
854 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
855 file->ignore_vpath = 1;
856 }
857
858 /* Now, take appropriate actions to remake the file. */
859 remake_file (file);
860
861 if (file->command_state != cs_finished)
862 {
863 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
864 return 0;
865 }
866
867 switch (file->update_status)
868 {
869 case 2:
870 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
871 break;
872 case 0:
873 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
874 break;
875 case 1:
876 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
877 break;
878 default:
879 assert (file->update_status >= 0 && file->update_status <= 2);
880 break;
881 }
882
883 file->updated = 1;
884 return file->update_status;
885}
886
887
888/* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
889 files listed in its `also_make' member. Under -t, this function also
890 touches FILE.
891
892 On return, FILE->update_status will no longer be -1 if it was. */
893
894void
895notice_finished_file (struct file *file)
896{
897 struct dep *d;
898 int ran = file->command_state == cs_running;
899 int touched = 0;
900 DB (DB_JOBS, (_("notice_finished_file - entering: file=%p `%s' update_status=%d command_state=%d\n"), /* bird */
901 file, file->name, file->update_status, file->command_state));
902 file->command_state = cs_finished;
903 file->updated = 1;
904
905#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
906 /* update not_parallel if the file was flagged for that. */
907 if ( ran
908 && (file->command_flags & (COMMANDS_NOTPARALLEL | COMMANDS_NO_COMMANDS))
909 == COMMANDS_NOTPARALLEL)
910 {
911 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [notice_finished_file]\n"), not_parallel,
912 not_parallel - 1, file, file->name));
913 assert(not_parallel >= 1);
914 --not_parallel;
915 }
916#endif
917
918 if (touch_flag
919 /* The update status will be:
920 -1 if this target was not remade;
921 0 if 0 or more commands (+ or ${MAKE}) were run and won;
922 1 if some commands were run and lost.
923 We touch the target if it has commands which either were not run
924 or won when they ran (i.e. status is 0). */
925 && file->update_status == 0)
926 {
927 if (file->cmds != 0 && file->cmds->any_recurse)
928 {
929 /* If all the command lines were recursive,
930 we don't want to do the touching. */
931 unsigned int i;
932 for (i = 0; i < file->cmds->ncommand_lines; ++i)
933 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
934 goto have_nonrecursing;
935 }
936 else
937 {
938 have_nonrecursing:
939 if (file->phony)
940 file->update_status = 0;
941 /* According to POSIX, -t doesn't affect targets with no cmds. */
942 else if (file->cmds != 0)
943 {
944 /* Should set file's modification date and do nothing else. */
945 file->update_status = touch_file (file);
946
947 /* Pretend we ran a real touch command, to suppress the
948 "`foo' is up to date" message. */
949 commands_started++;
950
951 /* Request for the timestamp to be updated (and distributed
952 to the double-colon entries). Simply setting ran=1 would
953 almost have done the trick, but messes up with the also_make
954 updating logic below. */
955 touched = 1;
956 }
957 }
958 }
959
960 if (file->mtime_before_update == UNKNOWN_MTIME)
961 file->mtime_before_update = file->last_mtime;
962
963 if ((ran && !file->phony) || touched)
964 {
965 int i = 0;
966
967 /* If -n, -t, or -q and all the commands are recursive, we ran them so
968 really check the target's mtime again. Otherwise, assume the target
969 would have been updated. */
970
971 if (question_flag || just_print_flag || touch_flag)
972 {
973 for (i = file->cmds->ncommand_lines; i > 0; --i)
974 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
975 break;
976 }
977
978 /* If there were no commands at all, it's always new. */
979
980 else if (file->is_target && file->cmds == 0)
981 i = 1;
982
983 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
984 }
985
986 if (file->double_colon)
987 {
988 /* If this is a double colon rule and it is the last one to be
989 updated, propagate the change of modification time to all the
990 double-colon entries for this file.
991
992 We do it on the last update because it is important to handle
993 individual entries as separate rules with separate timestamps
994 while they are treated as targets and then as one rule with the
995 unified timestamp when they are considered as a prerequisite
996 of some target. */
997
998 struct file *f;
999 FILE_TIMESTAMP max_mtime = file->last_mtime;
1000
1001 /* Check that all rules were updated and at the same time find
1002 the max timestamp. We assume UNKNOWN_MTIME is newer then
1003 any other value. */
1004 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
1005 if (max_mtime != UNKNOWN_MTIME
1006 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
1007 max_mtime = f->last_mtime;
1008
1009 if (f == 0)
1010 for (f = file->double_colon; f != 0; f = f->prev)
1011 f->last_mtime = max_mtime;
1012 }
1013
1014 if (ran && file->update_status != -1)
1015#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1016 {
1017#endif
1018 /* We actually tried to update FILE, which has
1019 updated its also_make's as well (if it worked).
1020 If it didn't work, it wouldn't work again for them.
1021 So mark them as updated with the same status. */
1022 for (d = file->also_make; d != 0; d = d->next)
1023 {
1024 d->file->command_state = cs_finished;
1025 d->file->updated = 1;
1026 d->file->update_status = file->update_status;
1027
1028 if (ran && !d->file->phony)
1029 /* Fetch the new modification time.
1030 We do this instead of just invalidating the cached time
1031 so that a vpath_search can happen. Otherwise, it would
1032 never be done because the target is already updated. */
1033 f_mtime (d->file, 0);
1034 }
1035#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1036 /* Same as above but for explicit multi target rules. */
1037 if (file->multi_head)
1038 {
1039 struct file *f2;
1040 assert (file == file->multi_head);
1041 for (f2 = file->multi_next; f2 != 0; f2 = f2->multi_next)
1042 {
1043 f2->command_state = cs_finished;
1044 f2->updated = 1;
1045 f2->update_status = file->update_status;
1046
1047 if (!f2->phony)
1048 f_mtime (f2, 0);
1049 }
1050 }
1051 }
1052#endif
1053 else if (file->update_status == -1)
1054 /* Nothing was done for FILE, but it needed nothing done.
1055 So mark it now as "succeeded". */
1056 file->update_status = 0;
1057}
1058
1059
1060/* Check whether another file (whose mtime is THIS_MTIME) needs updating on
1061 account of a dependency which is file FILE. If it does, store 1 in
1062 *MUST_MAKE_PTR. In the process, update any non-intermediate files that
1063 FILE depends on (including FILE itself). Return nonzero if any updating
1064 failed. */
1065
1066static int
1067check_dep (struct file *file, unsigned int depth,
1068 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
1069{
1070 struct dep *d;
1071 int dep_status = 0;
1072
1073 ++depth;
1074 start_updating (file);
1075
1076 if (file->phony || !file->intermediate)
1077 {
1078 /* If this is a non-intermediate file, update it and record whether it
1079 is newer than THIS_MTIME. */
1080 FILE_TIMESTAMP mtime;
1081 dep_status = update_file (file, depth);
1082 check_renamed (file);
1083 mtime = file_mtime (file);
1084 check_renamed (file);
1085 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
1086 *must_make_ptr = 1;
1087 }
1088 else
1089 {
1090 /* FILE is an intermediate file. */
1091 FILE_TIMESTAMP mtime;
1092
1093 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
1094 {
1095 if (try_implicit_rule (file, depth))
1096 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
1097 else
1098 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
1099 file->tried_implicit = 1;
1100 }
1101 if (file->cmds == 0 && !file->is_target
1102 && default_file != 0 && default_file->cmds != 0)
1103 {
1104 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
1105 file->cmds = default_file->cmds;
1106 }
1107
1108 check_renamed (file);
1109 mtime = file_mtime (file);
1110 check_renamed (file);
1111 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
1112 /* If the intermediate file actually exists and is newer, then we
1113 should remake from it. */
1114 *must_make_ptr = 1;
1115 else
1116 {
1117 /* Otherwise, update all non-intermediate files we depend on, if
1118 necessary, and see whether any of them is more recent than the
1119 file on whose behalf we are checking. */
1120 struct dep *lastd;
1121
1122 lastd = 0;
1123 d = file->deps;
1124 while (d != 0)
1125 {
1126 int maybe_make;
1127
1128 if (is_updating (d->file))
1129 {
1130 error (NILF, _("Circular %s <- %s dependency dropped."),
1131 file->name, d->file->name);
1132 if (lastd == 0)
1133 {
1134 file->deps = d->next;
1135 free_dep (d);
1136 d = file->deps;
1137 }
1138 else
1139 {
1140 lastd->next = d->next;
1141 free_dep (d);
1142 d = lastd->next;
1143 }
1144 continue;
1145 }
1146
1147 d->file->parent = file;
1148 maybe_make = *must_make_ptr;
1149 dep_status |= check_dep (d->file, depth, this_mtime,
1150 &maybe_make);
1151 if (! d->ignore_mtime)
1152 *must_make_ptr = maybe_make;
1153 check_renamed (d->file);
1154 if (dep_status != 0 && !keep_going_flag)
1155 break;
1156
1157 if (d->file->command_state == cs_running
1158 || d->file->command_state == cs_deps_running)
1159 /* Record that some of FILE's deps are still being made.
1160 This tells the upper levels to wait on processing it until
1161 the commands are finished. */
1162 set_command_state (file, cs_deps_running);
1163
1164 lastd = d;
1165 d = d->next;
1166 }
1167 }
1168 }
1169
1170 finish_updating (file);
1171 return dep_status;
1172}
1173
1174
1175/* Touch FILE. Return zero if successful, one if not. */
1176
1177#define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
1178
1179static int
1180touch_file (struct file *file)
1181{
1182 if (!silent_flag)
1183 message (0, "touch %s", file->name);
1184
1185#ifndef NO_ARCHIVES
1186 if (ar_name (file->name))
1187 return ar_touch (file->name);
1188 else
1189#endif
1190 {
1191 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
1192
1193 if (fd < 0)
1194 TOUCH_ERROR ("touch: open: ");
1195 else
1196 {
1197 struct stat statbuf;
1198 char buf = 'x';
1199 int e;
1200
1201 EINTRLOOP (e, fstat (fd, &statbuf));
1202 if (e < 0)
1203 TOUCH_ERROR ("touch: fstat: ");
1204 /* Rewrite character 0 same as it already is. */
1205 if (read (fd, &buf, 1) < 0)
1206 TOUCH_ERROR ("touch: read: ");
1207 if (lseek (fd, 0L, 0) < 0L)
1208 TOUCH_ERROR ("touch: lseek: ");
1209 if (write (fd, &buf, 1) < 0)
1210 TOUCH_ERROR ("touch: write: ");
1211 /* If file length was 0, we just
1212 changed it, so change it back. */
1213 if (statbuf.st_size == 0)
1214 {
1215 (void) close (fd);
1216 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
1217 if (fd < 0)
1218 TOUCH_ERROR ("touch: open: ");
1219 }
1220 (void) close (fd);
1221 }
1222 }
1223
1224 return 0;
1225}
1226
1227
1228/* Having checked and updated the dependencies of FILE,
1229 do whatever is appropriate to remake FILE itself.
1230 Return the status from executing FILE's commands. */
1231
1232static void
1233remake_file (struct file *file)
1234{
1235#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1236 /* Always operate on the primary file. */
1237 if (file->multi_head && file->multi_head != file)
1238 file = file->multi_head;
1239#endif
1240
1241 if (file->cmds == 0)
1242 {
1243 if (file->phony)
1244 /* Phony target. Pretend it succeeded. */
1245 file->update_status = 0;
1246 else if (file->is_target)
1247 /* This is a nonexistent target file we cannot make.
1248 Pretend it was successfully remade. */
1249 file->update_status = 0;
1250 else
1251 {
1252 /* This is a dependency file we cannot remake. Fail. */
1253 if (!rebuilding_makefiles || !file->dontcare)
1254 complain (file);
1255 file->update_status = 2;
1256 }
1257 }
1258 else
1259 {
1260 chop_commands (file->cmds);
1261
1262 /* The normal case: start some commands. */
1263 if (!touch_flag || file->cmds->any_recurse)
1264 {
1265 execute_file_commands (file);
1266 return;
1267 }
1268
1269 /* This tells notice_finished_file it is ok to touch the file. */
1270 file->update_status = 0;
1271 }
1272
1273 /* This does the touching under -t. */
1274 notice_finished_file (file);
1275}
1276
1277
1278/* Return the mtime of a file, given a `struct file'.
1279 Caches the time in the struct file to avoid excess stat calls.
1280
1281 If the file is not found, and SEARCH is nonzero, VPATH searching and
1282 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1283 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1284 FILE. */
1285
1286FILE_TIMESTAMP
1287f_mtime (struct file *file, int search)
1288{
1289 FILE_TIMESTAMP mtime;
1290
1291 /* File's mtime is not known; must get it from the system. */
1292
1293#ifndef NO_ARCHIVES
1294 if (ar_name (file->name))
1295 {
1296 /* This file is an archive-member reference. */
1297
1298 char *arname, *memname;
1299 struct file *arfile;
1300 time_t member_date;
1301
1302 /* Find the archive's name. */
1303 ar_parse_name (file->name, &arname, &memname);
1304
1305 /* Find the modification time of the archive itself.
1306 Also allow for its name to be changed via VPATH search. */
1307 arfile = lookup_file (arname);
1308 if (arfile == 0)
1309 arfile = enter_file (strcache_add (arname));
1310 mtime = f_mtime (arfile, search);
1311 check_renamed (arfile);
1312 if (search && strcmp (arfile->hname, arname))
1313 {
1314 /* The archive's name has changed.
1315 Change the archive-member reference accordingly. */
1316
1317 char *name;
1318 unsigned int arlen, memlen;
1319
1320 arlen = strlen (arfile->hname);
1321 memlen = strlen (memname);
1322
1323 name = xmalloc (arlen + 1 + memlen + 2);
1324 memcpy (name, arfile->hname, arlen);
1325 name[arlen] = '(';
1326 memcpy (name + arlen + 1, memname, memlen);
1327 name[arlen + 1 + memlen] = ')';
1328 name[arlen + 1 + memlen + 1] = '\0';
1329
1330 /* If the archive was found with GPATH, make the change permanent;
1331 otherwise defer it until later. */
1332 if (arfile->name == arfile->hname)
1333 rename_file (file, name);
1334 else
1335 rehash_file (file, name);
1336 check_renamed (file);
1337 }
1338
1339 free (arname);
1340
1341 file->low_resolution_time = 1;
1342
1343 if (mtime == NONEXISTENT_MTIME)
1344 /* The archive doesn't exist, so its members don't exist either. */
1345 return NONEXISTENT_MTIME;
1346
1347 member_date = ar_member_date (file->hname);
1348 mtime = (member_date == (time_t) -1
1349 ? NONEXISTENT_MTIME
1350 : file_timestamp_cons (file->hname, member_date, 0));
1351 }
1352 else
1353#endif
1354 {
1355 mtime = name_mtime (file->name);
1356
1357 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1358 {
1359 /* If name_mtime failed, search VPATH. */
1360 const char *name = vpath_search (file->name, &mtime);
1361 if (name
1362 /* Last resort, is it a library (-lxxx)? */
1363 || (file->name[0] == '-' && file->name[1] == 'l'
1364 && (name = library_search (file->name, &mtime)) != 0))
1365 {
1366 if (mtime != UNKNOWN_MTIME)
1367 /* vpath_search and library_search store UNKNOWN_MTIME
1368 if they didn't need to do a stat call for their work. */
1369 file->last_mtime = mtime;
1370
1371 /* If we found it in VPATH, see if it's in GPATH too; if so,
1372 change the name right now; if not, defer until after the
1373 dependencies are updated. */
1374 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1375 {
1376 rename_file (file, name);
1377 check_renamed (file);
1378 return file_mtime (file);
1379 }
1380
1381 rehash_file (file, name);
1382 check_renamed (file);
1383 /* If the result of a vpath search is -o or -W, preserve it.
1384 Otherwise, find the mtime of the resulting file. */
1385 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1386 mtime = name_mtime (name);
1387 }
1388 }
1389 }
1390
1391 /* Files can have bogus timestamps that nothing newly made will be
1392 "newer" than. Updating their dependents could just result in loops.
1393 So notify the user of the anomaly with a warning.
1394
1395 We only need to do this once, for now. */
1396
1397 if (!clock_skew_detected
1398 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1399 && !file->updated)
1400 {
1401 static FILE_TIMESTAMP adjusted_now;
1402
1403 FILE_TIMESTAMP adjusted_mtime = mtime;
1404
1405#if defined(WINDOWS32) || defined(__MSDOS__)
1406 /* Experimentation has shown that FAT filesystems can set file times
1407 up to 3 seconds into the future! Play it safe. */
1408
1409#define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1410
1411 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1412 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1413 adjusted_mtime -= adjustment;
1414#elif defined(__EMX__)
1415 /* FAT filesystems round time to the nearest even second!
1416 Allow for any file (NTFS or FAT) to perhaps suffer from this
1417 brain damage. */
1418 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1419 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1420 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1421 : 0);
1422#endif
1423
1424 /* If the file's time appears to be in the future, update our
1425 concept of the present and try once more. */
1426 if (adjusted_now < adjusted_mtime)
1427 {
1428 int resolution;
1429 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1430 adjusted_now = now + (resolution - 1);
1431 if (adjusted_now < adjusted_mtime)
1432 {
1433#ifdef NO_FLOAT
1434 error (NILF, _("Warning: File `%s' has modification time in the future"),
1435 file->name);
1436#else
1437 double from_now =
1438 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1439 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1440 / 1e9));
1441 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1442 file->name, from_now);
1443#endif
1444 clock_skew_detected = 1;
1445 }
1446 }
1447 }
1448
1449 /* Store the mtime into all the entries for this file. */
1450 if (file->double_colon)
1451 file = file->double_colon;
1452
1453 do
1454 {
1455 /* If this file is not implicit but it is intermediate then it was
1456 made so by the .INTERMEDIATE target. If this file has never
1457 been built by us but was found now, it existed before make
1458 started. So, turn off the intermediate bit so make doesn't
1459 delete it, since it didn't create it. */
1460 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1461 && file->command_state == cs_not_started
1462 && !file->tried_implicit && file->intermediate)
1463 file->intermediate = 0;
1464
1465 file->last_mtime = mtime;
1466 file = file->prev;
1467 }
1468 while (file != 0);
1469
1470 return mtime;
1471}
1472
1473
1474/* Return the mtime of the file or archive-member reference NAME. */
1475
1476/* First, we check with stat(). If the file does not exist, then we return
1477 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1478 examine each indirection of the symlink and find the newest mtime.
1479 This causes one duplicate stat() when -L is being used, but the code is
1480 much cleaner. */
1481
1482static FILE_TIMESTAMP
1483name_mtime (const char *name)
1484{
1485 FILE_TIMESTAMP mtime;
1486 struct stat st;
1487 int e;
1488
1489 EINTRLOOP (e, stat (name, &st));
1490 if (e == 0)
1491 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1492 else if (errno == ENOENT || errno == ENOTDIR)
1493 mtime = NONEXISTENT_MTIME;
1494 else
1495 {
1496 perror_with_name ("stat: ", name);
1497 return NONEXISTENT_MTIME;
1498 }
1499
1500 /* If we get here we either found it, or it doesn't exist.
1501 If it doesn't exist see if we can use a symlink mtime instead. */
1502
1503#ifdef MAKE_SYMLINKS
1504#ifndef S_ISLNK
1505# define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1506#endif
1507 if (check_symlink_flag)
1508 {
1509 PATH_VAR (lpath);
1510
1511 /* Check each symbolic link segment (if any). Find the latest mtime
1512 amongst all of them (and the target file of course).
1513 Note that we have already successfully dereferenced all the links
1514 above. So, if we run into any error trying to lstat(), or
1515 readlink(), or whatever, something bizarre-o happened. Just give up
1516 and use whatever mtime we've already computed at that point. */
1517 strcpy (lpath, name);
1518 while (1)
1519 {
1520 FILE_TIMESTAMP ltime;
1521 PATH_VAR (lbuf);
1522 long llen;
1523 char *p;
1524
1525 EINTRLOOP (e, lstat (lpath, &st));
1526 if (e)
1527 {
1528 /* Just take what we have so far. */
1529 if (errno != ENOENT && errno != ENOTDIR)
1530 perror_with_name ("lstat: ", lpath);
1531 break;
1532 }
1533
1534 /* If this is not a symlink, we're done (we started with the real
1535 file's mtime so we don't need to test it again). */
1536 if (!S_ISLNK (st.st_mode))
1537 break;
1538
1539 /* If this mtime is newer than what we had, keep the new one. */
1540 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1541 if (ltime > mtime)
1542 mtime = ltime;
1543
1544 /* Set up to check the file pointed to by this link. */
1545 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1546 if (llen < 0)
1547 {
1548 /* Eh? Just take what we have. */
1549 perror_with_name ("readlink: ", lpath);
1550 break;
1551 }
1552 lbuf[llen] = '\0';
1553
1554 /* If the target is fully-qualified or the source is just a
1555 filename, then the new path is the target. Otherwise it's the
1556 source directory plus the target. */
1557 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1558 strcpy (lpath, lbuf);
1559 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1560 /* Eh? Path too long! Again, just go with what we have. */
1561 break;
1562 else
1563 /* Create the next step in the symlink chain. */
1564 strcpy (p+1, lbuf);
1565 }
1566 }
1567#endif
1568
1569 return mtime;
1570}
1571
1572
1573/* Search for a library file specified as -lLIBNAME, searching for a
1574 suitable library file in the system library directories and the VPATH
1575 directories. */
1576
1577static const char *
1578library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
1579{
1580 static char *dirs[] =
1581 {
1582#ifdef KMK
1583 ".",
1584#else /* !KMK */
1585#ifndef _AMIGA
1586 "/lib",
1587 "/usr/lib",
1588#endif
1589#if defined(WINDOWS32) && !defined(LIBDIR)
1590/*
1591 * This is completely up to the user at product install time. Just define
1592 * a placeholder.
1593 */
1594#define LIBDIR "."
1595#endif
1596# ifdef LIBDIR /* bird */
1597 LIBDIR, /* Defined by configuration. */
1598# else /* bird */
1599 ".", /* bird */
1600# endif /* bird */
1601#endif /* !KMK */
1602 0
1603 };
1604
1605 static char *libpatterns = NULL;
1606
1607 const char *libname = lib+2; /* Name without the '-l'. */
1608 FILE_TIMESTAMP mtime;
1609
1610 /* Loop variables for the libpatterns value. */
1611 char *p;
1612 const char *p2;
1613 unsigned int len;
1614
1615 char **dp;
1616
1617 /* If we don't have libpatterns, get it. */
1618 if (!libpatterns)
1619 {
1620 int save = warn_undefined_variables_flag;
1621 warn_undefined_variables_flag = 0;
1622
1623 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1624
1625 warn_undefined_variables_flag = save;
1626 }
1627
1628 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1629 p2 = libpatterns;
1630 while ((p = find_next_token (&p2, &len)) != 0)
1631 {
1632 static char *buf = NULL;
1633 static unsigned int buflen = 0;
1634 static int libdir_maxlen = -1;
1635 char *libbuf = variable_expand ("");
1636 const size_t libbuf_offset = libbuf - variable_buffer; /* bird */
1637
1638 /* Expand the pattern using LIBNAME as a replacement. */
1639 {
1640 char c = p[len];
1641 char *p3, *p4;
1642
1643 p[len] = '\0';
1644 p3 = find_percent (p);
1645 if (!p3)
1646 {
1647 /* Give a warning if there is no pattern, then remove the
1648 pattern so it's ignored next time. */
1649 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1650 for (; len; --len, ++p)
1651 *p = ' ';
1652 *p = c;
1653 continue;
1654 }
1655 p4 = variable_buffer_output (libbuf, p, p3-p);
1656 p4 = variable_buffer_output (p4, libname, strlen (libname));
1657 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1658 p[len] = c;
1659 libbuf = variable_buffer + libbuf_offset; /* bird - variable_buffer may have been reallocated. */
1660 }
1661
1662 /* Look first for `libNAME.a' in the current directory. */
1663 mtime = name_mtime (libbuf);
1664 if (mtime != NONEXISTENT_MTIME)
1665 {
1666 if (mtime_ptr != 0)
1667 *mtime_ptr = mtime;
1668 return strcache_add (libbuf);
1669 }
1670
1671 /* Now try VPATH search on that. */
1672
1673 {
1674 const char *file = vpath_search (libbuf, mtime_ptr);
1675 if (file)
1676 return file;
1677 }
1678
1679 /* Now try the standard set of directories. */
1680
1681 if (!buflen)
1682 {
1683 for (dp = dirs; *dp != 0; ++dp)
1684 {
1685 int l = strlen (*dp);
1686 if (l > libdir_maxlen)
1687 libdir_maxlen = l;
1688 }
1689 buflen = strlen (libbuf);
1690 buf = xmalloc(libdir_maxlen + buflen + 2);
1691 }
1692 else if (buflen < strlen (libbuf))
1693 {
1694 buflen = strlen (libbuf);
1695 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1696 }
1697
1698 for (dp = dirs; *dp != 0; ++dp)
1699 {
1700 sprintf (buf, "%s/%s", *dp, libbuf);
1701 mtime = name_mtime (buf);
1702 if (mtime != NONEXISTENT_MTIME)
1703 {
1704 if (mtime_ptr != 0)
1705 *mtime_ptr = mtime;
1706 return strcache_add (buf);
1707 }
1708 }
1709 }
1710
1711 return 0;
1712}
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