VirtualBox

source: kBuild/trunk/src/gmake/remake.c@ 225

Last change on this file since 225 was 218, checked in by bird, 20 years ago

2nd try on .NOTPARALLEL.

  • Property svn:eol-style set to native
File size: 40.7 KB
Line 
1/* Basic dependency engine for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
32002 Free Software Foundation, Inc.
4This file is part of GNU Make.
5
6GNU Make is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Make is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Make; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21#include "make.h"
22#include "filedef.h"
23#include "job.h"
24#include "commands.h"
25#include "dep.h"
26#include "variable.h"
27#include "debug.h"
28
29#include <assert.h>
30
31#ifdef HAVE_FCNTL_H
32#include <fcntl.h>
33#else
34#include <sys/file.h>
35#endif
36
37#ifdef VMS
38#include <starlet.h>
39#endif
40#ifdef WINDOWS32
41#include <io.h>
42#endif
43
44extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
45
46
47/* The test for circular dependencies is based on the 'updating' bit in
48 `struct file'. However, double colon targets have seperate `struct
49 file's; make sure we always use the base of the double colon chain. */
50
51#define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
52 ->updating = 1)
53#define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
54 ->updating = 0)
55#define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
56 ->updating)
57
58
59/* Incremented when a command is started (under -n, when one would be). */
60unsigned int commands_started = 0;
61
62/* Current value for pruning the scan of the goal chain (toggle 0/1). */
63static unsigned int considered;
64
65static int update_file PARAMS ((struct file *file, unsigned int depth));
66static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
67static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
68static int touch_file PARAMS ((struct file *file));
69static void remake_file PARAMS ((struct file *file));
70static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
71static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
72
73
74
75/* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
76 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
77 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
78 be disabled for them unless they were also command-line targets, and we
79 should only make one goal at a time and return as soon as one goal whose
80 `changed' member is nonzero is successfully made. */
81
82int
83update_goal_chain (struct dep *goals, int makefiles)
84{
85 int t = touch_flag, q = question_flag, n = just_print_flag;
86 unsigned int j = job_slots;
87 int status = -1;
88
89#define MTIME(file) (makefiles ? file_mtime_no_search (file) \
90 : file_mtime (file))
91
92 /* Duplicate the chain so we can remove things from it. */
93
94 goals = copy_dep_chain (goals);
95
96 {
97 /* Clear the `changed' flag of each goal in the chain.
98 We will use the flag below to notice when any commands
99 have actually been run for a target. When no commands
100 have been run, we give an "up to date" diagnostic. */
101
102 struct dep *g;
103 for (g = goals; g != 0; g = g->next)
104 g->changed = 0;
105 }
106
107 /* All files start with the considered bit 0, so the global value is 1. */
108 considered = 1;
109
110 /* Update all the goals until they are all finished. */
111
112 while (goals != 0)
113 {
114 register struct dep *g, *lastgoal;
115
116 /* Start jobs that are waiting for the load to go down. */
117
118 start_waiting_jobs ();
119
120 /* Wait for a child to die. */
121
122 reap_children (1, 0);
123
124 lastgoal = 0;
125 g = goals;
126 while (g != 0)
127 {
128 /* Iterate over all double-colon entries for this file. */
129 struct file *file;
130 int stop = 0, any_not_updated = 0;
131
132 for (file = g->file->double_colon ? g->file->double_colon : g->file;
133 file != NULL;
134 file = file->prev)
135 {
136 unsigned int ocommands_started;
137 int x;
138 check_renamed (file);
139 if (makefiles)
140 {
141 if (file->cmd_target)
142 {
143 touch_flag = t;
144 question_flag = q;
145 just_print_flag = n;
146 }
147 else
148 touch_flag = question_flag = just_print_flag = 0;
149 }
150
151 /* Save the old value of `commands_started' so we can compare
152 later. It will be incremented when any commands are
153 actually run. */
154 ocommands_started = commands_started;
155
156 x = update_file (file, makefiles ? 1 : 0);
157 check_renamed (file);
158
159 /* Set the goal's `changed' flag if any commands were started
160 by calling update_file above. We check this flag below to
161 decide when to give an "up to date" diagnostic. */
162 g->changed += commands_started - ocommands_started;
163
164 /* If we updated a file and STATUS was not already 1, set it to
165 1 if updating failed, or to 0 if updating succeeded. Leave
166 STATUS as it is if no updating was done. */
167
168 stop = 0;
169 if ((x != 0 || file->updated) && status < 1)
170 {
171 if (file->update_status != 0)
172 {
173 /* Updating failed, or -q triggered. The STATUS value
174 tells our caller which. */
175 status = file->update_status;
176 /* If -q just triggered, stop immediately. It doesn't
177 matter how much more we run, since we already know
178 the answer to return. */
179 stop = (!keep_going_flag && !question_flag
180 && !makefiles);
181 }
182 else
183 {
184 FILE_TIMESTAMP mtime = MTIME (file);
185 check_renamed (file);
186
187 if (file->updated && g->changed &&
188 mtime != file->mtime_before_update)
189 {
190 /* Updating was done. If this is a makefile and
191 just_print_flag or question_flag is set (meaning
192 -n or -q was given and this file was specified
193 as a command-line target), don't change STATUS.
194 If STATUS is changed, we will get re-exec'd, and
195 enter an infinite loop. */
196 if (!makefiles
197 || (!just_print_flag && !question_flag))
198 status = 0;
199 if (makefiles && file->dontcare)
200 /* This is a default makefile; stop remaking. */
201 stop = 1;
202 }
203 }
204 }
205
206 /* Keep track if any double-colon entry is not finished.
207 When they are all finished, the goal is finished. */
208 any_not_updated |= !file->updated;
209
210 if (stop)
211 break;
212 }
213
214 /* Reset FILE since it is null at the end of the loop. */
215 file = g->file;
216
217 if (stop || !any_not_updated)
218 {
219 /* If we have found nothing whatever to do for the goal,
220 print a message saying nothing needs doing. */
221
222 if (!makefiles
223 /* If the update_status is zero, we updated successfully
224 or not at all. G->changed will have been set above if
225 any commands were actually started for this goal. */
226 && file->update_status == 0 && !g->changed
227 /* Never give a message under -s or -q. */
228 && !silent_flag && !question_flag)
229 message (1, ((file->phony || file->cmds == 0)
230 ? _("Nothing to be done for `%s'.")
231 : _("`%s' is up to date.")),
232 file->name);
233
234 /* This goal is finished. Remove it from the chain. */
235 if (lastgoal == 0)
236 goals = g->next;
237 else
238 lastgoal->next = g->next;
239
240 /* Free the storage. */
241 free ((char *) g);
242
243 g = lastgoal == 0 ? goals : lastgoal->next;
244
245 if (stop)
246 break;
247 }
248 else
249 {
250 lastgoal = g;
251 g = g->next;
252 }
253 }
254
255 /* If we reached the end of the dependency graph toggle the considered
256 flag for the next pass. */
257 if (g == 0)
258 considered = !considered;
259 }
260
261 if (makefiles)
262 {
263 touch_flag = t;
264 question_flag = q;
265 just_print_flag = n;
266 job_slots = j;
267 }
268 return status;
269}
270
271
272/* If FILE is not up to date, execute the commands for it.
273 Return 0 if successful, 1 if unsuccessful;
274 but with some flag settings, just call `exit' if unsuccessful.
275
276 DEPTH is the depth in recursions of this function.
277 We increment it during the consideration of our dependencies,
278 then decrement it again after finding out whether this file
279 is out of date.
280
281 If there are multiple double-colon entries for FILE,
282 each is considered in turn. */
283
284static int
285update_file (struct file *file, unsigned int depth)
286{
287 register int status = 0;
288 register struct file *f;
289
290 f = file->double_colon ? file->double_colon : file;
291
292 /* Prune the dependency graph: if we've already been here on _this_
293 pass through the dependency graph, we don't have to go any further.
294 We won't reap_children until we start the next pass, so no state
295 change is possible below here until then. */
296 if (f->considered == considered)
297 {
298 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
299 return f->command_state == cs_finished ? f->update_status : 0;
300 }
301
302 /* This loop runs until we start commands for a double colon rule, or until
303 the chain is exhausted. */
304 for (; f != 0; f = f->prev)
305 {
306 f->considered = considered;
307
308 status |= update_file_1 (f, depth);
309 check_renamed (f);
310
311 if (status != 0 && !keep_going_flag)
312 break;
313
314 if (f->command_state == cs_running
315 || f->command_state == cs_deps_running)
316 {
317 /* Don't run the other :: rules for this
318 file until this rule is finished. */
319 status = 0;
320 break;
321 }
322 }
323
324 /* Process the remaining rules in the double colon chain so they're marked
325 considered. Start their prerequisites, too. */
326 for (; f != 0 ; f = f->prev)
327 {
328 struct dep *d;
329
330 f->considered = considered;
331
332 for (d = f->deps; d != 0; d = d->next)
333 status |= update_file (d->file, depth + 1);
334 }
335
336 return status;
337}
338
339
340/* Consider a single `struct file' and update it as appropriate. */
341
342static int
343update_file_1 (struct file *file, unsigned int depth)
344{
345 register FILE_TIMESTAMP this_mtime;
346 int noexist, must_make, deps_changed;
347 int dep_status = 0;
348 register struct dep *d, *lastd;
349 int running = 0;
350
351 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
352
353 if (file->updated)
354 {
355 if (file->update_status > 0)
356 {
357 DBF (DB_VERBOSE,
358 _("Recently tried and failed to update file `%s'.\n"));
359 return file->update_status;
360 }
361
362 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
363 return 0;
364 }
365
366 switch (file->command_state)
367 {
368 case cs_not_started:
369 case cs_deps_running:
370 break;
371 case cs_running:
372 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
373 return 0;
374 case cs_finished:
375 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
376 return file->update_status;
377 default:
378 abort ();
379 }
380
381 ++depth;
382
383 /* Notice recursive update of the same file. */
384 start_updating (file);
385
386 /* Looking at the file's modtime beforehand allows the possibility
387 that its name may be changed by a VPATH search, and thus it may
388 not need an implicit rule. If this were not done, the file
389 might get implicit commands that apply to its initial name, only
390 to have that name replaced with another found by VPATH search. */
391
392 this_mtime = file_mtime (file);
393 check_renamed (file);
394 noexist = this_mtime == NONEXISTENT_MTIME;
395 if (noexist)
396 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
397 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
398 && file->low_resolution_time)
399 {
400 /* Avoid spurious rebuilds due to low resolution time stamps. */
401 int ns = FILE_TIMESTAMP_NS (this_mtime);
402 if (ns != 0)
403 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
404 file->name);
405 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
406 }
407
408 must_make = noexist;
409
410 /* If file was specified as a target with no commands,
411 come up with some default commands. */
412
413 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
414 {
415 if (try_implicit_rule (file, depth))
416 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
417 else
418 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
419 file->tried_implicit = 1;
420 }
421 if (file->cmds == 0 && !file->is_target
422 && default_file != 0 && default_file->cmds != 0)
423 {
424 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
425 file->cmds = default_file->cmds;
426 }
427
428 /* Update all non-intermediate files we depend on, if necessary,
429 and see whether any of them is more recent than this file. */
430
431 lastd = 0;
432 d = file->deps;
433 while (d != 0)
434 {
435 FILE_TIMESTAMP mtime;
436 int maybe_make;
437
438 check_renamed (d->file);
439
440 mtime = file_mtime (d->file);
441 check_renamed (d->file);
442
443 if (is_updating (d->file))
444 {
445 error (NILF, _("Circular %s <- %s dependency dropped."),
446 file->name, d->file->name);
447 /* We cannot free D here because our the caller will still have
448 a reference to it when we were called recursively via
449 check_dep below. */
450 if (lastd == 0)
451 file->deps = d->next;
452 else
453 lastd->next = d->next;
454 d = d->next;
455 continue;
456 }
457
458 d->file->parent = file;
459 maybe_make = must_make;
460 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
461 if (! d->ignore_mtime)
462 must_make = maybe_make;
463
464 check_renamed (d->file);
465
466 {
467 register struct file *f = d->file;
468 if (f->double_colon)
469 f = f->double_colon;
470 do
471 {
472 running |= (f->command_state == cs_running
473 || f->command_state == cs_deps_running);
474 f = f->prev;
475 }
476 while (f != 0);
477 }
478
479 if (dep_status != 0 && !keep_going_flag)
480 break;
481
482 if (!running)
483 d->changed = file_mtime (d->file) != mtime;
484
485 lastd = d;
486 d = d->next;
487 }
488
489 /* Now we know whether this target needs updating.
490 If it does, update all the intermediate files we depend on. */
491
492 if (must_make || always_make_flag)
493 {
494 for (d = file->deps; d != 0; d = d->next)
495 if (d->file->intermediate)
496 {
497 FILE_TIMESTAMP mtime = file_mtime (d->file);
498 check_renamed (d->file);
499 d->file->parent = file;
500 dep_status |= update_file (d->file, depth);
501 check_renamed (d->file);
502
503 {
504 register struct file *f = d->file;
505 if (f->double_colon)
506 f = f->double_colon;
507 do
508 {
509 running |= (f->command_state == cs_running
510 || f->command_state == cs_deps_running);
511 f = f->prev;
512 }
513 while (f != 0);
514 }
515
516 if (dep_status != 0 && !keep_going_flag)
517 break;
518
519 if (!running)
520 d->changed = ((file->phony && file->cmds != 0)
521 || file_mtime (d->file) != mtime);
522 }
523 }
524
525 finish_updating (file);
526
527 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
528
529 if (running)
530 {
531 set_command_state (file, cs_deps_running);
532 --depth;
533 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
534 return 0;
535 }
536
537 /* If any dependency failed, give up now. */
538
539 if (dep_status != 0)
540 {
541 file->update_status = dep_status;
542 notice_finished_file (file);
543
544 --depth;
545
546 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
547
548 if (depth == 0 && keep_going_flag
549 && !just_print_flag && !question_flag)
550 error (NILF,
551 _("Target `%s' not remade because of errors."), file->name);
552
553 return dep_status;
554 }
555
556 if (file->command_state == cs_deps_running)
557 /* The commands for some deps were running on the last iteration, but
558 they have finished now. Reset the command_state to not_started to
559 simplify later bookkeeping. It is important that we do this only
560 when the prior state was cs_deps_running, because that prior state
561 was definitely propagated to FILE's also_make's by set_command_state
562 (called above), but in another state an also_make may have
563 independently changed to finished state, and we would confuse that
564 file's bookkeeping (updated, but not_started is bogus state). */
565 set_command_state (file, cs_not_started);
566
567 /* Now record which prerequisites are more
568 recent than this file, so we can define $?. */
569
570 deps_changed = 0;
571 for (d = file->deps; d != 0; d = d->next)
572 {
573 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
574 check_renamed (d->file);
575
576 if (! d->ignore_mtime)
577 {
578#if 1
579 /* %%% In version 4, remove this code completely to
580 implement not remaking deps if their deps are newer
581 than their parents. */
582 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
583 /* We must remake if this dep does not
584 exist and is not intermediate. */
585 must_make = 1;
586#endif
587
588 /* Set DEPS_CHANGED if this dep actually changed. */
589 deps_changed |= d->changed;
590 }
591
592 /* Set D->changed if either this dep actually changed,
593 or its dependent, FILE, is older or does not exist. */
594 d->changed |= noexist || d_mtime > this_mtime;
595
596 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
597 {
598 const char *fmt = 0;
599
600 if (d->ignore_mtime)
601 {
602 if (ISDB (DB_VERBOSE))
603 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
604 }
605 else if (d_mtime == NONEXISTENT_MTIME)
606 {
607 if (ISDB (DB_BASIC))
608 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
609 }
610 else if (d->changed)
611 {
612 if (ISDB (DB_BASIC))
613 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
614 }
615 else if (ISDB (DB_VERBOSE))
616 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
617
618 if (fmt)
619 {
620 print_spaces (depth);
621 printf (fmt, dep_name (d), file->name);
622 fflush (stdout);
623 }
624 }
625 }
626
627 /* Here depth returns to the value it had when we were called. */
628 depth--;
629
630 if (file->double_colon && file->deps == 0)
631 {
632 must_make = 1;
633 DBF (DB_BASIC,
634 _("Target `%s' is double-colon and has no prerequisites.\n"));
635 }
636 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
637 && !always_make_flag)
638 {
639 must_make = 0;
640 DBF (DB_VERBOSE,
641 _("No commands for `%s' and no prerequisites actually changed.\n"));
642 }
643 else if (!must_make && file->cmds != 0 && always_make_flag)
644 {
645 must_make = 1;
646 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
647 }
648
649 if (!must_make)
650 {
651 if (ISDB (DB_VERBOSE))
652 {
653 print_spaces (depth);
654 printf (_("No need to remake target `%s'"), file->name);
655 if (!streq (file->name, file->hname))
656 printf (_("; using VPATH name `%s'"), file->hname);
657 puts (".");
658 fflush (stdout);
659 }
660
661 notice_finished_file (file);
662
663 /* Since we don't need to remake the file, convert it to use the
664 VPATH filename if we found one. hfile will be either the
665 local name if no VPATH or the VPATH name if one was found. */
666
667 while (file)
668 {
669 file->name = file->hname;
670 file = file->prev;
671 }
672
673 return 0;
674 }
675
676 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
677
678 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
679 VPATH. */
680 if (!streq(file->name, file->hname))
681 {
682 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
683 file->ignore_vpath = 1;
684 }
685
686 /* Now, take appropriate actions to remake the file. */
687 remake_file (file);
688
689 if (file->command_state != cs_finished)
690 {
691 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
692 return 0;
693 }
694
695 switch (file->update_status)
696 {
697 case 2:
698 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
699 break;
700 case 0:
701 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
702 break;
703 case 1:
704 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
705 break;
706 default:
707 assert (file->update_status >= 0 && file->update_status <= 2);
708 break;
709 }
710
711 file->updated = 1;
712 return file->update_status;
713}
714
715
716/* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
717 files listed in its `also_make' member. Under -t, this function also
718 touches FILE.
719
720 On return, FILE->update_status will no longer be -1 if it was. */
721
722void
723notice_finished_file (struct file *file)
724{
725 struct dep *d;
726 int ran = file->command_state == cs_running;
727 int touched = 0;
728DB (DB_JOBS, (_("notice_finished_file - entering: file=%p `%s' update_status=%d command_state=%d\n"), file, file->name, file->update_status, file->command_state));
729 file->command_state = cs_finished;
730 file->updated = 1;
731
732 /* update not_parallel if the file was flagged for that. */
733 if (ran && (file->command_flags & COMMANDS_NOTPARALLEL))
734 {
735 assert(not_parallel == 1);
736 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s')\n"), not_parallel, not_parallel - 1, file, file->name));
737 --not_parallel;
738 }
739
740 if (touch_flag
741 /* The update status will be:
742 -1 if this target was not remade;
743 0 if 0 or more commands (+ or ${MAKE}) were run and won;
744 1 if some commands were run and lost.
745 We touch the target if it has commands which either were not run
746 or won when they ran (i.e. status is 0). */
747 && file->update_status == 0)
748 {
749 if (file->cmds != 0 && file->cmds->any_recurse)
750 {
751 /* If all the command lines were recursive,
752 we don't want to do the touching. */
753 unsigned int i;
754 for (i = 0; i < file->cmds->ncommand_lines; ++i)
755 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
756 goto have_nonrecursing;
757 }
758 else
759 {
760 have_nonrecursing:
761 if (file->phony)
762 file->update_status = 0;
763 /* According to POSIX, -t doesn't affect targets with no cmds. */
764 else if (file->cmds != 0)
765 {
766 /* Should set file's modification date and do nothing else. */
767 file->update_status = touch_file (file);
768
769 /* Pretend we ran a real touch command, to suppress the
770 "`foo' is up to date" message. */
771 commands_started++;
772
773 /* Request for the timestamp to be updated (and distributed
774 to the double-colon entries). Simply setting ran=1 would
775 almost have done the trick, but messes up with the also_make
776 updating logic below. */
777 touched = 1;
778 }
779 }
780 }
781
782 if (file->mtime_before_update == UNKNOWN_MTIME)
783 file->mtime_before_update = file->last_mtime;
784
785 if ((ran && !file->phony) || touched)
786 {
787 struct file *f;
788 int i = 0;
789
790 /* If -n, -t, or -q and all the commands are recursive, we ran them so
791 really check the target's mtime again. Otherwise, assume the target
792 would have been updated. */
793
794 if (question_flag || just_print_flag || touch_flag)
795 {
796 for (i = file->cmds->ncommand_lines; i > 0; --i)
797 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
798 break;
799 }
800
801 /* If there were no commands at all, it's always new. */
802
803 else if (file->is_target && file->cmds == 0)
804 i = 1;
805
806 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
807
808 /* Propagate the change of modification time to all the double-colon
809 entries for this file. */
810 for (f = file->double_colon; f != 0; f = f->prev)
811 f->last_mtime = file->last_mtime;
812 }
813
814 if (ran && file->update_status != -1)
815 /* We actually tried to update FILE, which has
816 updated its also_make's as well (if it worked).
817 If it didn't work, it wouldn't work again for them.
818 So mark them as updated with the same status. */
819 for (d = file->also_make; d != 0; d = d->next)
820 {
821 d->file->command_state = cs_finished;
822 d->file->updated = 1;
823 d->file->update_status = file->update_status;
824
825 if (ran && !d->file->phony)
826 /* Fetch the new modification time.
827 We do this instead of just invalidating the cached time
828 so that a vpath_search can happen. Otherwise, it would
829 never be done because the target is already updated. */
830 (void) f_mtime (d->file, 0);
831 }
832 else if (file->update_status == -1)
833 /* Nothing was done for FILE, but it needed nothing done.
834 So mark it now as "succeeded". */
835 file->update_status = 0;
836
837DB (DB_JOBS, (_("notice_finished_file - leaving: file=%p `%s' update_status=%d command_state=%d\n"), file, file->name, file->update_status, file->command_state));
838}
839
840
841/* Check whether another file (whose mtime is THIS_MTIME)
842 needs updating on account of a dependency which is file FILE.
843 If it does, store 1 in *MUST_MAKE_PTR.
844 In the process, update any non-intermediate files
845 that FILE depends on (including FILE itself).
846 Return nonzero if any updating failed. */
847
848static int
849check_dep (struct file *file, unsigned int depth,
850 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
851{
852 struct dep *d;
853 int dep_status = 0;
854
855 ++depth;
856 start_updating (file);
857
858 if (!file->intermediate)
859 /* If this is a non-intermediate file, update it and record
860 whether it is newer than THIS_MTIME. */
861 {
862 FILE_TIMESTAMP mtime;
863 dep_status = update_file (file, depth);
864 check_renamed (file);
865 mtime = file_mtime (file);
866 check_renamed (file);
867 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
868 *must_make_ptr = 1;
869 }
870 else
871 {
872 /* FILE is an intermediate file. */
873 FILE_TIMESTAMP mtime;
874
875 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
876 {
877 if (try_implicit_rule (file, depth))
878 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
879 else
880 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
881 file->tried_implicit = 1;
882 }
883 if (file->cmds == 0 && !file->is_target
884 && default_file != 0 && default_file->cmds != 0)
885 {
886 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
887 file->cmds = default_file->cmds;
888 }
889
890 /* If the intermediate file actually exists
891 and is newer, then we should remake from it. */
892 check_renamed (file);
893 mtime = file_mtime (file);
894 check_renamed (file);
895 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
896 *must_make_ptr = 1;
897 /* Otherwise, update all non-intermediate files we depend on,
898 if necessary, and see whether any of them is more
899 recent than the file on whose behalf we are checking. */
900 else
901 {
902 struct dep *lastd;
903
904 lastd = 0;
905 d = file->deps;
906 while (d != 0)
907 {
908 int maybe_make;
909
910 if (is_updating (d->file))
911 {
912 error (NILF, _("Circular %s <- %s dependency dropped."),
913 file->name, d->file->name);
914 if (lastd == 0)
915 {
916 file->deps = d->next;
917 free ((char *) d);
918 d = file->deps;
919 }
920 else
921 {
922 lastd->next = d->next;
923 free ((char *) d);
924 d = lastd->next;
925 }
926 continue;
927 }
928
929 d->file->parent = file;
930 maybe_make = *must_make_ptr;
931 dep_status |= check_dep (d->file, depth, this_mtime,
932 &maybe_make);
933 if (! d->ignore_mtime)
934 *must_make_ptr = maybe_make;
935 check_renamed (d->file);
936 if (dep_status != 0 && !keep_going_flag)
937 break;
938
939 if (d->file->command_state == cs_running
940 || d->file->command_state == cs_deps_running)
941 /* Record that some of FILE's deps are still being made.
942 This tells the upper levels to wait on processing it until
943 the commands are finished. */
944 set_command_state (file, cs_deps_running);
945
946 lastd = d;
947 d = d->next;
948 }
949 }
950 }
951
952 finish_updating (file);
953 return dep_status;
954}
955
956
957/* Touch FILE. Return zero if successful, one if not. */
958
959#define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
960
961static int
962touch_file (struct file *file)
963{
964 if (!silent_flag)
965 message (0, "touch %s", file->name);
966
967#ifndef NO_ARCHIVES
968 if (ar_name (file->name))
969 return ar_touch (file->name);
970 else
971#endif
972 {
973 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
974
975 if (fd < 0)
976 TOUCH_ERROR ("touch: open: ");
977 else
978 {
979 struct stat statbuf;
980 char buf;
981 int e;
982
983 EINTRLOOP (e, fstat (fd, &statbuf));
984 if (e < 0)
985 TOUCH_ERROR ("touch: fstat: ");
986 /* Rewrite character 0 same as it already is. */
987 if (read (fd, &buf, 1) < 0)
988 TOUCH_ERROR ("touch: read: ");
989 if (lseek (fd, 0L, 0) < 0L)
990 TOUCH_ERROR ("touch: lseek: ");
991 if (write (fd, &buf, 1) < 0)
992 TOUCH_ERROR ("touch: write: ");
993 /* If file length was 0, we just
994 changed it, so change it back. */
995 if (statbuf.st_size == 0)
996 {
997 (void) close (fd);
998 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
999 if (fd < 0)
1000 TOUCH_ERROR ("touch: open: ");
1001 }
1002 (void) close (fd);
1003 }
1004 }
1005
1006 return 0;
1007}
1008
1009
1010/* Having checked and updated the dependencies of FILE,
1011 do whatever is appropriate to remake FILE itself.
1012 Return the status from executing FILE's commands. */
1013
1014static void
1015remake_file (struct file *file)
1016{
1017 if (file->cmds == 0)
1018 {
1019 if (file->phony)
1020 /* Phony target. Pretend it succeeded. */
1021 file->update_status = 0;
1022 else if (file->is_target)
1023 /* This is a nonexistent target file we cannot make.
1024 Pretend it was successfully remade. */
1025 file->update_status = 0;
1026 else
1027 {
1028 const char *msg_noparent
1029 = _("%sNo rule to make target `%s'%s");
1030 const char *msg_parent
1031 = _("%sNo rule to make target `%s', needed by `%s'%s");
1032
1033 /* This is a dependency file we cannot remake. Fail. */
1034 if (!keep_going_flag && !file->dontcare)
1035 {
1036 if (file->parent == 0)
1037 fatal (NILF, msg_noparent, "", file->name, "");
1038
1039 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
1040 }
1041
1042 if (!file->dontcare)
1043 {
1044 if (file->parent == 0)
1045 error (NILF, msg_noparent, "*** ", file->name, ".");
1046 else
1047 error (NILF, msg_parent, "*** ",
1048 file->name, file->parent->name, ".");
1049 }
1050 file->update_status = 2;
1051 }
1052 }
1053 else
1054 {
1055 chop_commands (file->cmds);
1056
1057 /* The normal case: start some commands. */
1058 if (!touch_flag || file->cmds->any_recurse)
1059 {
1060 execute_file_commands (file);
1061 return;
1062 }
1063
1064 /* This tells notice_finished_file it is ok to touch the file. */
1065 file->update_status = 0;
1066 }
1067
1068 /* This does the touching under -t. */
1069 notice_finished_file (file);
1070}
1071
1072
1073/* Return the mtime of a file, given a `struct file'.
1074 Caches the time in the struct file to avoid excess stat calls.
1075
1076 If the file is not found, and SEARCH is nonzero, VPATH searching and
1077 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1078 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1079 FILE. */
1080
1081FILE_TIMESTAMP
1082f_mtime (struct file *file, int search)
1083{
1084 FILE_TIMESTAMP mtime;
1085
1086 /* File's mtime is not known; must get it from the system. */
1087
1088#ifndef NO_ARCHIVES
1089 if (ar_name (file->name))
1090 {
1091 /* This file is an archive-member reference. */
1092
1093 char *arname, *memname;
1094 struct file *arfile;
1095 int arname_used = 0;
1096 time_t member_date;
1097
1098 /* Find the archive's name. */
1099 ar_parse_name (file->name, &arname, &memname);
1100
1101 /* Find the modification time of the archive itself.
1102 Also allow for its name to be changed via VPATH search. */
1103 arfile = lookup_file (arname);
1104 if (arfile == 0)
1105 {
1106 arfile = enter_file (arname);
1107 arname_used = 1;
1108 }
1109 mtime = f_mtime (arfile, search);
1110 check_renamed (arfile);
1111 if (search && strcmp (arfile->hname, arname))
1112 {
1113 /* The archive's name has changed.
1114 Change the archive-member reference accordingly. */
1115
1116 char *name;
1117 unsigned int arlen, memlen;
1118
1119 if (!arname_used)
1120 {
1121 free (arname);
1122 arname_used = 1;
1123 }
1124
1125 arname = arfile->hname;
1126 arlen = strlen (arname);
1127 memlen = strlen (memname);
1128
1129 /* free (file->name); */
1130
1131 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1132 bcopy (arname, name, arlen);
1133 name[arlen] = '(';
1134 bcopy (memname, name + arlen + 1, memlen);
1135 name[arlen + 1 + memlen] = ')';
1136 name[arlen + 1 + memlen + 1] = '\0';
1137
1138 /* If the archive was found with GPATH, make the change permanent;
1139 otherwise defer it until later. */
1140 if (arfile->name == arfile->hname)
1141 rename_file (file, name);
1142 else
1143 rehash_file (file, name);
1144 check_renamed (file);
1145 }
1146
1147 if (!arname_used)
1148 free (arname);
1149 free (memname);
1150
1151 file->low_resolution_time = 1;
1152
1153 if (mtime == NONEXISTENT_MTIME)
1154 /* The archive doesn't exist, so its members don't exist either. */
1155 return NONEXISTENT_MTIME;
1156
1157 member_date = ar_member_date (file->hname);
1158 mtime = (member_date == (time_t) -1
1159 ? NONEXISTENT_MTIME
1160 : file_timestamp_cons (file->hname, member_date, 0));
1161 }
1162 else
1163#endif
1164 {
1165 mtime = name_mtime (file->name);
1166
1167 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1168 {
1169 /* If name_mtime failed, search VPATH. */
1170 char *name = file->name;
1171 if (vpath_search (&name, &mtime)
1172 /* Last resort, is it a library (-lxxx)? */
1173 || (name[0] == '-' && name[1] == 'l'
1174 && library_search (&name, &mtime)))
1175 {
1176 if (mtime != UNKNOWN_MTIME)
1177 /* vpath_search and library_search store UNKNOWN_MTIME
1178 if they didn't need to do a stat call for their work. */
1179 file->last_mtime = mtime;
1180
1181 /* If we found it in VPATH, see if it's in GPATH too; if so,
1182 change the name right now; if not, defer until after the
1183 dependencies are updated. */
1184 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1185 {
1186 rename_file (file, name);
1187 check_renamed (file);
1188 return file_mtime (file);
1189 }
1190
1191 rehash_file (file, name);
1192 check_renamed (file);
1193 mtime = name_mtime (name);
1194 }
1195 }
1196 }
1197
1198 {
1199 /* Files can have bogus timestamps that nothing newly made will be
1200 "newer" than. Updating their dependents could just result in loops.
1201 So notify the user of the anomaly with a warning.
1202
1203 We only need to do this once, for now. */
1204
1205 if (!clock_skew_detected
1206 && mtime != NONEXISTENT_MTIME
1207 && !file->updated)
1208 {
1209 static FILE_TIMESTAMP adjusted_now;
1210
1211 FILE_TIMESTAMP adjusted_mtime = mtime;
1212
1213#if defined(WINDOWS32) || defined(__MSDOS__)
1214 /* Experimentation has shown that FAT filesystems can set file times
1215 up to 3 seconds into the future! Play it safe. */
1216
1217#define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1218
1219 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1220 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1221 adjusted_mtime -= adjustment;
1222#elif defined(__EMX__)
1223 /* FAT filesystems round time to the nearest even second!
1224 Allow for any file (NTFS or FAT) to perhaps suffer from this
1225 brain damage. */
1226 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1227 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1228 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1229 : 0);
1230#endif
1231
1232 /* If the file's time appears to be in the future, update our
1233 concept of the present and try once more. */
1234 if (adjusted_now < adjusted_mtime)
1235 {
1236 int resolution;
1237 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1238 adjusted_now = now + (resolution - 1);
1239 if (adjusted_now < adjusted_mtime)
1240 {
1241#ifdef NO_FLOAT
1242 error (NILF, _("Warning: File `%s' has modification time in the future"),
1243 file->name);
1244#else
1245 double from_now =
1246 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1247 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1248 / 1e9));
1249 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1250 file->name, from_now);
1251#endif
1252 clock_skew_detected = 1;
1253 }
1254 }
1255 }
1256 }
1257
1258 /* Store the mtime into all the entries for this file. */
1259 if (file->double_colon)
1260 file = file->double_colon;
1261
1262 do
1263 {
1264 /* If this file is not implicit but it is intermediate then it was
1265 made so by the .INTERMEDIATE target. If this file has never
1266 been built by us but was found now, it existed before make
1267 started. So, turn off the intermediate bit so make doesn't
1268 delete it, since it didn't create it. */
1269 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1270 && file->command_state == cs_not_started
1271 && !file->tried_implicit && file->intermediate)
1272 file->intermediate = 0;
1273
1274 file->last_mtime = mtime;
1275 file = file->prev;
1276 }
1277 while (file != 0);
1278
1279 return mtime;
1280}
1281
1282
1283/* Return the mtime of the file or archive-member reference NAME. */
1284
1285static FILE_TIMESTAMP
1286name_mtime (char *name)
1287{
1288 struct stat st;
1289 int e;
1290
1291 EINTRLOOP (e, stat (name, &st));
1292 if (e != 0)
1293 {
1294 if (errno != ENOENT && errno != ENOTDIR)
1295 perror_with_name ("stat:", name);
1296 return NONEXISTENT_MTIME;
1297 }
1298
1299 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1300}
1301
1302
1303/* Search for a library file specified as -lLIBNAME, searching for a
1304 suitable library file in the system library directories and the VPATH
1305 directories. */
1306
1307static int
1308library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1309{
1310 static char *dirs[] =
1311 {
1312#ifndef _AMIGA
1313 "/lib",
1314 "/usr/lib",
1315#endif
1316#if defined(WINDOWS32) && !defined(LIBDIR)
1317/*
1318 * This is completely up to the user at product install time. Just define
1319 * a placeholder.
1320 */
1321#define LIBDIR "."
1322#endif
1323 LIBDIR, /* Defined by configuration. */
1324 0
1325 };
1326
1327 static char *libpatterns = NULL;
1328
1329 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1330 FILE_TIMESTAMP mtime;
1331
1332 /* Loop variables for the libpatterns value. */
1333 char *p, *p2;
1334 unsigned int len;
1335
1336 char *file, **dp;
1337
1338 /* If we don't have libpatterns, get it. */
1339 if (!libpatterns)
1340 {
1341 int save = warn_undefined_variables_flag;
1342 warn_undefined_variables_flag = 0;
1343
1344 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1345
1346 warn_undefined_variables_flag = save;
1347 }
1348
1349 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1350 p2 = libpatterns;
1351 while ((p = find_next_token (&p2, &len)) != 0)
1352 {
1353 static char *buf = NULL;
1354 static unsigned int buflen = 0;
1355 static int libdir_maxlen = -1;
1356 char *libbuf = variable_expand ("");
1357
1358 /* Expand the pattern using LIBNAME as a replacement. */
1359 {
1360 char c = p[len];
1361 char *p3, *p4;
1362
1363 p[len] = '\0';
1364 p3 = find_percent (p);
1365 if (!p3)
1366 {
1367 /* Give a warning if there is no pattern, then remove the
1368 pattern so it's ignored next time. */
1369 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1370 for (; len; --len, ++p)
1371 *p = ' ';
1372 *p = c;
1373 continue;
1374 }
1375 p4 = variable_buffer_output (libbuf, p, p3-p);
1376 p4 = variable_buffer_output (p4, libname, strlen (libname));
1377 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1378 p[len] = c;
1379 }
1380
1381 /* Look first for `libNAME.a' in the current directory. */
1382 mtime = name_mtime (libbuf);
1383 if (mtime != NONEXISTENT_MTIME)
1384 {
1385 *lib = xstrdup (libbuf);
1386 if (mtime_ptr != 0)
1387 *mtime_ptr = mtime;
1388 return 1;
1389 }
1390
1391 /* Now try VPATH search on that. */
1392
1393 file = libbuf;
1394 if (vpath_search (&file, mtime_ptr))
1395 {
1396 *lib = file;
1397 return 1;
1398 }
1399
1400 /* Now try the standard set of directories. */
1401
1402 if (!buflen)
1403 {
1404 for (dp = dirs; *dp != 0; ++dp)
1405 {
1406 int l = strlen (*dp);
1407 if (l > libdir_maxlen)
1408 libdir_maxlen = l;
1409 }
1410 buflen = strlen (libbuf);
1411 buf = xmalloc(libdir_maxlen + buflen + 2);
1412 }
1413 else if (buflen < strlen (libbuf))
1414 {
1415 buflen = strlen (libbuf);
1416 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1417 }
1418
1419 for (dp = dirs; *dp != 0; ++dp)
1420 {
1421 sprintf (buf, "%s/%s", *dp, libbuf);
1422 mtime = name_mtime (buf);
1423 if (mtime != NONEXISTENT_MTIME)
1424 {
1425 *lib = xstrdup (buf);
1426 if (mtime_ptr != 0)
1427 *mtime_ptr = mtime;
1428 return 1;
1429 }
1430 }
1431 }
1432
1433 return 0;
1434}
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