VirtualBox

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

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

remake.c: moved call_must_make_target_var out of the dep eval loop and added a DB_BASIC printf for hits. Some sketchy .IS_CHANGED target variable to complement the .MUST_MAKE one, the concept needds more work though.

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