VirtualBox

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

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

kmk: Created a custom hook into the update_file process for checking additional file dependencies like changes in the commands and such. It's call .MUST_MAKE.

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