VirtualBox

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

Last change on this file since 2554 was 2100, checked in by bird, 16 years ago

space

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

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