VirtualBox

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

Last change on this file since 2857 was 2857, checked in by bird, 8 years ago

updates

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

© 2023 Oracle
ContactPrivacy policyTerms of Use