VirtualBox

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

Last change on this file since 1993 was 1993, checked in by bird, 17 years ago

Merged in current GNU Make code (CVS from 2008-10-28). Ref #55.

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

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