VirtualBox

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

Last change on this file since 1891 was 1862, checked in by bird, 16 years ago

kmk: some preliminary allocation caching. seems dep, variables, variable sets and files would be good candidates for generic alloc caches.

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