Portability fix for glob.h building in FreeBSD ports system.
[make.git] / remake.c
blobc679d11128e0293511ac4ec16691537ef1843d28
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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"
29 #include <assert.h>
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #else
34 #include <sys/file.h>
35 #endif
37 #ifdef VMS
38 #include <starlet.h>
39 #endif
40 #ifdef WINDOWS32
41 #include <io.h>
42 #endif
44 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
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. */
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)
59 /* Incremented when a command is started (under -n, when one would be). */
60 unsigned int commands_started = 0;
62 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
63 static unsigned int considered;
65 static int update_file PARAMS ((struct file *file, unsigned int depth));
66 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
67 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
68 static int touch_file PARAMS ((struct file *file));
69 static void remake_file PARAMS ((struct file *file));
70 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
71 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
74 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
75 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
76 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
77 be disabled for them unless they were also command-line targets, and we
78 should only make one goal at a time and return as soon as one goal whose
79 `changed' member is nonzero is successfully made. */
81 int
82 update_goal_chain (struct dep *goals, int makefiles)
84 int t = touch_flag, q = question_flag, n = just_print_flag;
85 unsigned int j = job_slots;
86 int status = -1;
88 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
89 : file_mtime (file))
91 /* Duplicate the chain so we can remove things from it. */
93 goals = copy_dep_chain (goals);
96 /* Clear the `changed' flag of each goal in the chain.
97 We will use the flag below to notice when any commands
98 have actually been run for a target. When no commands
99 have been run, we give an "up to date" diagnostic. */
101 struct dep *g;
102 for (g = goals; g != 0; g = g->next)
103 g->changed = 0;
106 /* All files start with the considered bit 0, so the global value is 1. */
107 considered = 1;
109 /* Update all the goals until they are all finished. */
111 while (goals != 0)
113 register struct dep *g, *lastgoal;
115 /* Start jobs that are waiting for the load to go down. */
117 start_waiting_jobs ();
119 /* Wait for a child to die. */
121 reap_children (1, 0);
123 lastgoal = 0;
124 g = goals;
125 while (g != 0)
127 /* Iterate over all double-colon entries for this file. */
128 struct file *file;
129 int stop = 0, any_not_updated = 0;
131 for (file = g->file->double_colon ? g->file->double_colon : g->file;
132 file != NULL;
133 file = file->prev)
135 unsigned int ocommands_started;
136 int x;
137 check_renamed (file);
138 if (makefiles)
140 if (file->cmd_target)
142 touch_flag = t;
143 question_flag = q;
144 just_print_flag = n;
146 else
147 touch_flag = question_flag = just_print_flag = 0;
150 /* Save the old value of `commands_started' so we can compare
151 later. It will be incremented when any commands are
152 actually run. */
153 ocommands_started = commands_started;
155 x = update_file (file, makefiles ? 1 : 0);
156 check_renamed (file);
158 /* Set the goal's `changed' flag if any commands were started
159 by calling update_file above. We check this flag below to
160 decide when to give an "up to date" diagnostic. */
161 g->changed += commands_started - ocommands_started;
163 /* If we updated a file and STATUS was not already 1, set it to
164 1 if updating failed, or to 0 if updating succeeded. Leave
165 STATUS as it is if no updating was done. */
167 stop = 0;
168 if ((x != 0 || file->updated) && status < 1)
170 if (file->update_status != 0)
172 /* Updating failed, or -q triggered. The STATUS value
173 tells our caller which. */
174 status = file->update_status;
175 /* If -q just triggered, stop immediately. It doesn't
176 matter how much more we run, since we already know
177 the answer to return. */
178 stop = (!keep_going_flag && !question_flag
179 && !makefiles);
181 else
183 FILE_TIMESTAMP mtime = MTIME (file);
184 check_renamed (file);
186 if (file->updated && g->changed &&
187 mtime != file->mtime_before_update)
189 /* Updating was done. If this is a makefile and
190 just_print_flag or question_flag is set (meaning
191 -n or -q was given and this file was specified
192 as a command-line target), don't change STATUS.
193 If STATUS is changed, we will get re-exec'd, and
194 enter an infinite loop. */
195 if (!makefiles
196 || (!just_print_flag && !question_flag))
197 status = 0;
198 if (makefiles && file->dontcare)
199 /* This is a default makefile; stop remaking. */
200 stop = 1;
205 /* Keep track if any double-colon entry is not finished.
206 When they are all finished, the goal is finished. */
207 any_not_updated |= !file->updated;
209 if (stop)
210 break;
213 /* Reset FILE since it is null at the end of the loop. */
214 file = g->file;
216 if (stop || !any_not_updated)
218 /* If we have found nothing whatever to do for the goal,
219 print a message saying nothing needs doing. */
221 if (!makefiles
222 /* If the update_status is zero, we updated successfully
223 or not at all. G->changed will have been set above if
224 any commands were actually started for this goal. */
225 && file->update_status == 0 && !g->changed
226 /* Never give a message under -s or -q. */
227 && !silent_flag && !question_flag)
228 message (1, ((file->phony || file->cmds == 0)
229 ? _("Nothing to be done for `%s'.")
230 : _("`%s' is up to date.")),
231 file->name);
233 /* This goal is finished. Remove it from the chain. */
234 if (lastgoal == 0)
235 goals = g->next;
236 else
237 lastgoal->next = g->next;
239 /* Free the storage. */
240 free ((char *) g);
242 g = lastgoal == 0 ? goals : lastgoal->next;
244 if (stop)
245 break;
247 else
249 lastgoal = g;
250 g = g->next;
254 /* If we reached the end of the dependency graph toggle the considered
255 flag for the next pass. */
256 if (g == 0)
257 considered = !considered;
260 if (makefiles)
262 touch_flag = t;
263 question_flag = q;
264 just_print_flag = n;
265 job_slots = j;
267 return status;
270 /* If FILE is not up to date, execute the commands for it.
271 Return 0 if successful, 1 if unsuccessful;
272 but with some flag settings, just call `exit' if unsuccessful.
274 DEPTH is the depth in recursions of this function.
275 We increment it during the consideration of our dependencies,
276 then decrement it again after finding out whether this file
277 is out of date.
279 If there are multiple double-colon entries for FILE,
280 each is considered in turn. */
282 static int
283 update_file (struct file *file, unsigned int depth)
285 register int status = 0;
286 register struct file *f;
288 f = file->double_colon ? file->double_colon : file;
290 /* Prune the dependency graph: if we've already been here on _this_
291 pass through the dependency graph, we don't have to go any further.
292 We won't reap_children until we start the next pass, so no state
293 change is possible below here until then. */
294 if (f->considered == considered)
296 DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
297 return f->command_state == cs_finished ? f->update_status : 0;
300 /* This loop runs until we start commands for a double colon rule, or until
301 the chain is exhausted. */
302 for (; f != 0; f = f->prev)
304 f->considered = considered;
306 status |= update_file_1 (f, depth);
307 check_renamed (f);
309 if (status != 0 && !keep_going_flag)
310 break;
312 if (f->command_state == cs_running
313 || f->command_state == cs_deps_running)
315 /* Don't run the other :: rules for this
316 file until this rule is finished. */
317 status = 0;
318 break;
322 /* Process the remaining rules in the double colon chain so they're marked
323 considered. Start their prerequisites, too. */
324 for (; f != 0 ; f = f->prev)
326 struct dep *d;
328 f->considered = considered;
330 for (d = f->deps; d != 0; d = d->next)
331 status |= update_file (d->file, depth + 1);
334 return status;
337 /* Consider a single `struct file' and update it as appropriate. */
339 static int
340 update_file_1 (struct file *file, unsigned int depth)
342 register FILE_TIMESTAMP this_mtime;
343 int noexist, must_make, deps_changed;
344 int dep_status = 0;
345 register struct dep *d, *lastd;
346 int running = 0;
348 DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
350 if (file->updated)
352 if (file->update_status > 0)
354 DBF (DB_VERBOSE,
355 _("Recently tried and failed to update file `%s'.\n"));
356 return file->update_status;
359 DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
360 return 0;
363 switch (file->command_state)
365 case cs_not_started:
366 case cs_deps_running:
367 break;
368 case cs_running:
369 DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
370 return 0;
371 case cs_finished:
372 DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
373 return file->update_status;
374 default:
375 abort ();
378 ++depth;
380 /* Notice recursive update of the same file. */
381 start_updating (file);
383 /* Looking at the file's modtime beforehand allows the possibility
384 that its name may be changed by a VPATH search, and thus it may
385 not need an implicit rule. If this were not done, the file
386 might get implicit commands that apply to its initial name, only
387 to have that name replaced with another found by VPATH search. */
389 this_mtime = file_mtime (file);
390 check_renamed (file);
391 noexist = this_mtime == NONEXISTENT_MTIME;
392 if (noexist)
393 DBF (DB_BASIC, _("File `%s' does not exist.\n"));
394 else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
395 && file->low_resolution_time)
397 /* Avoid spurious rebuilds due to low resolution time stamps. */
398 int ns = FILE_TIMESTAMP_NS (this_mtime);
399 if (ns != 0)
400 error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
401 file->name);
402 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
405 must_make = noexist;
407 /* If file was specified as a target with no commands,
408 come up with some default commands. */
410 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
412 if (try_implicit_rule (file, depth))
413 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
414 else
415 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
416 file->tried_implicit = 1;
418 if (file->cmds == 0 && !file->is_target
419 && default_file != 0 && default_file->cmds != 0)
421 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
422 file->cmds = default_file->cmds;
425 /* Update all non-intermediate files we depend on, if necessary,
426 and see whether any of them is more recent than this file. */
428 lastd = 0;
429 d = file->deps;
430 while (d != 0)
432 FILE_TIMESTAMP mtime;
433 int maybe_make;
435 check_renamed (d->file);
437 mtime = file_mtime (d->file);
438 check_renamed (d->file);
440 if (is_updating (d->file))
442 error (NILF, _("Circular %s <- %s dependency dropped."),
443 file->name, d->file->name);
444 /* We cannot free D here because our the caller will still have
445 a reference to it when we were called recursively via
446 check_dep below. */
447 if (lastd == 0)
448 file->deps = d->next;
449 else
450 lastd->next = d->next;
451 d = d->next;
452 continue;
455 d->file->parent = file;
456 maybe_make = must_make;
457 dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
458 if (! d->ignore_mtime)
459 must_make = maybe_make;
461 check_renamed (d->file);
464 register struct file *f = d->file;
465 if (f->double_colon)
466 f = f->double_colon;
469 running |= (f->command_state == cs_running
470 || f->command_state == cs_deps_running);
471 f = f->prev;
473 while (f != 0);
476 if (dep_status != 0 && !keep_going_flag)
477 break;
479 if (!running)
480 d->changed = file_mtime (d->file) != mtime;
482 lastd = d;
483 d = d->next;
486 /* Now we know whether this target needs updating.
487 If it does, update all the intermediate files we depend on. */
489 if (must_make || always_make_flag)
491 for (d = file->deps; d != 0; d = d->next)
492 if (d->file->intermediate)
494 FILE_TIMESTAMP mtime = file_mtime (d->file);
495 check_renamed (d->file);
496 d->file->parent = file;
497 dep_status |= update_file (d->file, depth);
498 check_renamed (d->file);
501 register struct file *f = d->file;
502 if (f->double_colon)
503 f = f->double_colon;
506 running |= (f->command_state == cs_running
507 || f->command_state == cs_deps_running);
508 f = f->prev;
510 while (f != 0);
513 if (dep_status != 0 && !keep_going_flag)
514 break;
516 if (!running)
517 d->changed = ((file->phony && file->cmds != 0)
518 || file_mtime (d->file) != mtime);
522 finish_updating (file);
524 DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
526 if (running)
528 set_command_state (file, cs_deps_running);
529 --depth;
530 DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
531 return 0;
534 /* If any dependency failed, give up now. */
536 if (dep_status != 0)
538 file->update_status = dep_status;
539 notice_finished_file (file);
541 --depth;
543 DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
545 if (depth == 0 && keep_going_flag
546 && !just_print_flag && !question_flag)
547 error (NILF,
548 _("Target `%s' not remade because of errors."), file->name);
550 return dep_status;
553 if (file->command_state == cs_deps_running)
554 /* The commands for some deps were running on the last iteration, but
555 they have finished now. Reset the command_state to not_started to
556 simplify later bookkeeping. It is important that we do this only
557 when the prior state was cs_deps_running, because that prior state
558 was definitely propagated to FILE's also_make's by set_command_state
559 (called above), but in another state an also_make may have
560 independently changed to finished state, and we would confuse that
561 file's bookkeeping (updated, but not_started is bogus state). */
562 set_command_state (file, cs_not_started);
564 /* Now record which prerequisites are more
565 recent than this file, so we can define $?. */
567 deps_changed = 0;
568 for (d = file->deps; d != 0; d = d->next)
570 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
571 check_renamed (d->file);
573 if (! d->ignore_mtime)
575 #if 1
576 /* %%% In version 4, remove this code completely to
577 implement not remaking deps if their deps are newer
578 than their parents. */
579 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
580 /* We must remake if this dep does not
581 exist and is not intermediate. */
582 must_make = 1;
583 #endif
585 /* Set DEPS_CHANGED if this dep actually changed. */
586 deps_changed |= d->changed;
589 /* Set D->changed if either this dep actually changed,
590 or its dependent, FILE, is older or does not exist. */
591 d->changed |= noexist || d_mtime > this_mtime;
593 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
595 const char *fmt = 0;
597 if (d->ignore_mtime)
599 if (ISDB (DB_VERBOSE))
600 fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
602 else if (d_mtime == NONEXISTENT_MTIME)
604 if (ISDB (DB_BASIC))
605 fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
607 else if (d->changed)
609 if (ISDB (DB_BASIC))
610 fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
612 else if (ISDB (DB_VERBOSE))
613 fmt = _("Prerequisite `%s' is older than target `%s'.\n");
615 if (fmt)
617 print_spaces (depth);
618 printf (fmt, dep_name (d), file->name);
619 fflush (stdout);
624 /* Here depth returns to the value it had when we were called. */
625 depth--;
627 if (file->double_colon && file->deps == 0)
629 must_make = 1;
630 DBF (DB_BASIC,
631 _("Target `%s' is double-colon and has no prerequisites.\n"));
633 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
634 && !always_make_flag)
636 must_make = 0;
637 DBF (DB_VERBOSE,
638 _("No commands for `%s' and no prerequisites actually changed.\n"));
640 else if (!must_make && file->cmds != 0 && always_make_flag)
642 must_make = 1;
643 DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
646 if (!must_make)
648 if (ISDB (DB_VERBOSE))
650 print_spaces (depth);
651 printf (_("No need to remake target `%s'"), file->name);
652 if (!streq (file->name, file->hname))
653 printf (_("; using VPATH name `%s'"), file->hname);
654 puts (".");
655 fflush (stdout);
658 notice_finished_file (file);
660 /* Since we don't need to remake the file, convert it to use the
661 VPATH filename if we found one. hfile will be either the
662 local name if no VPATH or the VPATH name if one was found. */
664 while (file)
666 file->name = file->hname;
667 file = file->prev;
670 return 0;
673 DBF (DB_BASIC, _("Must remake target `%s'.\n"));
675 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
676 VPATH. */
677 if (!streq(file->name, file->hname))
679 DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
680 file->ignore_vpath = 1;
683 /* Now, take appropriate actions to remake the file. */
684 remake_file (file);
686 if (file->command_state != cs_finished)
688 DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
689 return 0;
692 switch (file->update_status)
694 case 2:
695 DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
696 break;
697 case 0:
698 DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
699 break;
700 case 1:
701 DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
702 break;
703 default:
704 assert (file->update_status >= 0 && file->update_status <= 2);
705 break;
708 file->updated = 1;
709 return file->update_status;
712 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
713 files listed in its `also_make' member. Under -t, this function also
714 touches FILE.
716 On return, FILE->update_status will no longer be -1 if it was. */
718 void
719 notice_finished_file (struct file *file)
721 struct dep *d;
722 int ran = file->command_state == cs_running;
723 int touched = 0;
725 file->command_state = cs_finished;
726 file->updated = 1;
728 if (touch_flag
729 /* The update status will be:
730 -1 if this target was not remade;
731 0 if 0 or more commands (+ or ${MAKE}) were run and won;
732 1 if some commands were run and lost.
733 We touch the target if it has commands which either were not run
734 or won when they ran (i.e. status is 0). */
735 && file->update_status == 0)
737 if (file->cmds != 0 && file->cmds->any_recurse)
739 /* If all the command lines were recursive,
740 we don't want to do the touching. */
741 unsigned int i;
742 for (i = 0; i < file->cmds->ncommand_lines; ++i)
743 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
744 goto have_nonrecursing;
746 else
748 have_nonrecursing:
749 if (file->phony)
750 file->update_status = 0;
751 else
753 /* Should set file's modification date and do nothing else. */
754 file->update_status = touch_file (file);
756 /* Pretend we ran a real touch command, to suppress the
757 "`foo' is up to date" message. */
758 commands_started++;
760 /* Request for the timestamp to be updated (and distributed
761 to the double-colon entries). Simply setting ran=1 would
762 almost have done the trick, but messes up with the also_make
763 updating logic below. */
764 touched = 1;
769 if (file->mtime_before_update == UNKNOWN_MTIME)
770 file->mtime_before_update = file->last_mtime;
772 if ((ran && !file->phony) || touched)
774 struct file *f;
775 int i = 0;
777 /* If -n, -t, or -q and all the commands are recursive, we ran them so
778 really check the target's mtime again. Otherwise, assume the target
779 would have been updated. */
781 if (question_flag || just_print_flag || touch_flag)
783 for (i = file->cmds->ncommand_lines; i > 0; --i)
784 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
785 break;
788 /* If there were no commands at all, it's always new. */
790 else if (file->is_target && file->cmds == 0)
791 i = 1;
793 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
795 /* Propagate the change of modification time to all the double-colon
796 entries for this file. */
797 for (f = file->double_colon; f != 0; f = f->prev)
798 f->last_mtime = file->last_mtime;
801 if (ran && file->update_status != -1)
802 /* We actually tried to update FILE, which has
803 updated its also_make's as well (if it worked).
804 If it didn't work, it wouldn't work again for them.
805 So mark them as updated with the same status. */
806 for (d = file->also_make; d != 0; d = d->next)
808 d->file->command_state = cs_finished;
809 d->file->updated = 1;
810 d->file->update_status = file->update_status;
812 if (ran && !d->file->phony)
813 /* Fetch the new modification time.
814 We do this instead of just invalidating the cached time
815 so that a vpath_search can happen. Otherwise, it would
816 never be done because the target is already updated. */
817 (void) f_mtime (d->file, 0);
819 else if (file->update_status == -1)
820 /* Nothing was done for FILE, but it needed nothing done.
821 So mark it now as "succeeded". */
822 file->update_status = 0;
825 /* Check whether another file (whose mtime is THIS_MTIME)
826 needs updating on account of a dependency which is file FILE.
827 If it does, store 1 in *MUST_MAKE_PTR.
828 In the process, update any non-intermediate files
829 that FILE depends on (including FILE itself).
830 Return nonzero if any updating failed. */
832 static int
833 check_dep (struct file *file, unsigned int depth,
834 FILE_TIMESTAMP this_mtime, int *must_make_ptr)
836 struct dep *d;
837 int dep_status = 0;
839 ++depth;
840 start_updating (file);
842 if (!file->intermediate)
843 /* If this is a non-intermediate file, update it and record
844 whether it is newer than THIS_MTIME. */
846 FILE_TIMESTAMP mtime;
847 dep_status = update_file (file, depth);
848 check_renamed (file);
849 mtime = file_mtime (file);
850 check_renamed (file);
851 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
852 *must_make_ptr = 1;
854 else
856 /* FILE is an intermediate file. */
857 FILE_TIMESTAMP mtime;
859 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
861 if (try_implicit_rule (file, depth))
862 DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
863 else
864 DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
865 file->tried_implicit = 1;
867 if (file->cmds == 0 && !file->is_target
868 && default_file != 0 && default_file->cmds != 0)
870 DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
871 file->cmds = default_file->cmds;
874 /* If the intermediate file actually exists
875 and is newer, then we should remake from it. */
876 check_renamed (file);
877 mtime = file_mtime (file);
878 check_renamed (file);
879 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
880 *must_make_ptr = 1;
881 /* Otherwise, update all non-intermediate files we depend on,
882 if necessary, and see whether any of them is more
883 recent than the file on whose behalf we are checking. */
884 else
886 struct dep *lastd;
888 lastd = 0;
889 d = file->deps;
890 while (d != 0)
892 int maybe_make;
894 if (is_updating (d->file))
896 error (NILF, _("Circular %s <- %s dependency dropped."),
897 file->name, d->file->name);
898 if (lastd == 0)
900 file->deps = d->next;
901 free ((char *) d);
902 d = file->deps;
904 else
906 lastd->next = d->next;
907 free ((char *) d);
908 d = lastd->next;
910 continue;
913 d->file->parent = file;
914 maybe_make = *must_make_ptr;
915 dep_status |= check_dep (d->file, depth, this_mtime,
916 &maybe_make);
917 if (! d->ignore_mtime)
918 *must_make_ptr = maybe_make;
919 check_renamed (d->file);
920 if (dep_status != 0 && !keep_going_flag)
921 break;
923 if (d->file->command_state == cs_running
924 || d->file->command_state == cs_deps_running)
925 /* Record that some of FILE's deps are still being made.
926 This tells the upper levels to wait on processing it until
927 the commands are finished. */
928 set_command_state (file, cs_deps_running);
930 lastd = d;
931 d = d->next;
936 finish_updating (file);
937 return dep_status;
940 /* Touch FILE. Return zero if successful, one if not. */
942 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
944 static int
945 touch_file (struct file *file)
947 if (!silent_flag)
948 message (0, "touch %s", file->name);
950 #ifndef NO_ARCHIVES
951 if (ar_name (file->name))
952 return ar_touch (file->name);
953 else
954 #endif
956 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
958 if (fd < 0)
959 TOUCH_ERROR ("touch: open: ");
960 else
962 struct stat statbuf;
963 char buf;
964 int e;
966 EINTRLOOP (e, fstat (fd, &statbuf));
967 if (e < 0)
968 TOUCH_ERROR ("touch: fstat: ");
969 /* Rewrite character 0 same as it already is. */
970 if (read (fd, &buf, 1) < 0)
971 TOUCH_ERROR ("touch: read: ");
972 if (lseek (fd, 0L, 0) < 0L)
973 TOUCH_ERROR ("touch: lseek: ");
974 if (write (fd, &buf, 1) < 0)
975 TOUCH_ERROR ("touch: write: ");
976 /* If file length was 0, we just
977 changed it, so change it back. */
978 if (statbuf.st_size == 0)
980 (void) close (fd);
981 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
982 if (fd < 0)
983 TOUCH_ERROR ("touch: open: ");
985 (void) close (fd);
989 return 0;
992 /* Having checked and updated the dependencies of FILE,
993 do whatever is appropriate to remake FILE itself.
994 Return the status from executing FILE's commands. */
996 static void
997 remake_file (struct file *file)
999 if (file->cmds == 0)
1001 if (file->phony)
1002 /* Phony target. Pretend it succeeded. */
1003 file->update_status = 0;
1004 else if (file->is_target)
1005 /* This is a nonexistent target file we cannot make.
1006 Pretend it was successfully remade. */
1007 file->update_status = 0;
1008 else
1010 const char *msg_noparent
1011 = _("%sNo rule to make target `%s'%s");
1012 const char *msg_parent
1013 = _("%sNo rule to make target `%s', needed by `%s'%s");
1015 /* This is a dependency file we cannot remake. Fail. */
1016 if (!keep_going_flag && !file->dontcare)
1018 if (file->parent == 0)
1019 fatal (NILF, msg_noparent, "", file->name, "");
1021 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
1024 if (!file->dontcare)
1026 if (file->parent == 0)
1027 error (NILF, msg_noparent, "*** ", file->name, ".");
1028 else
1029 error (NILF, msg_parent, "*** ",
1030 file->name, file->parent->name, ".");
1032 file->update_status = 2;
1035 else
1037 chop_commands (file->cmds);
1039 /* The normal case: start some commands. */
1040 if (!touch_flag || file->cmds->any_recurse)
1042 execute_file_commands (file);
1043 return;
1046 /* This tells notice_finished_file it is ok to touch the file. */
1047 file->update_status = 0;
1050 /* This does the touching under -t. */
1051 notice_finished_file (file);
1054 /* Return the mtime of a file, given a `struct file'.
1055 Caches the time in the struct file to avoid excess stat calls.
1057 If the file is not found, and SEARCH is nonzero, VPATH searching and
1058 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1059 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1060 FILE. */
1062 FILE_TIMESTAMP
1063 f_mtime (struct file *file, int search)
1065 FILE_TIMESTAMP mtime;
1067 /* File's mtime is not known; must get it from the system. */
1069 #ifndef NO_ARCHIVES
1070 if (ar_name (file->name))
1072 /* This file is an archive-member reference. */
1074 char *arname, *memname;
1075 struct file *arfile;
1076 int arname_used = 0;
1077 time_t member_date;
1079 /* Find the archive's name. */
1080 ar_parse_name (file->name, &arname, &memname);
1082 /* Find the modification time of the archive itself.
1083 Also allow for its name to be changed via VPATH search. */
1084 arfile = lookup_file (arname);
1085 if (arfile == 0)
1087 arfile = enter_file (arname);
1088 arname_used = 1;
1090 mtime = f_mtime (arfile, search);
1091 check_renamed (arfile);
1092 if (search && strcmp (arfile->hname, arname))
1094 /* The archive's name has changed.
1095 Change the archive-member reference accordingly. */
1097 char *name;
1098 unsigned int arlen, memlen;
1100 if (!arname_used)
1102 free (arname);
1103 arname_used = 1;
1106 arname = arfile->hname;
1107 arlen = strlen (arname);
1108 memlen = strlen (memname);
1110 /* free (file->name); */
1112 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1113 bcopy (arname, name, arlen);
1114 name[arlen] = '(';
1115 bcopy (memname, name + arlen + 1, memlen);
1116 name[arlen + 1 + memlen] = ')';
1117 name[arlen + 1 + memlen + 1] = '\0';
1119 /* If the archive was found with GPATH, make the change permanent;
1120 otherwise defer it until later. */
1121 if (arfile->name == arfile->hname)
1122 rename_file (file, name);
1123 else
1124 rehash_file (file, name);
1125 check_renamed (file);
1128 if (!arname_used)
1129 free (arname);
1130 free (memname);
1132 file->low_resolution_time = 1;
1134 if (mtime == NONEXISTENT_MTIME)
1135 /* The archive doesn't exist, so its members don't exist either. */
1136 return NONEXISTENT_MTIME;
1138 member_date = ar_member_date (file->hname);
1139 mtime = (member_date == (time_t) -1
1140 ? NONEXISTENT_MTIME
1141 : file_timestamp_cons (file->hname, member_date, 0));
1143 else
1144 #endif
1146 mtime = name_mtime (file->name);
1148 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1150 /* If name_mtime failed, search VPATH. */
1151 char *name = file->name;
1152 if (vpath_search (&name, &mtime)
1153 /* Last resort, is it a library (-lxxx)? */
1154 || (name[0] == '-' && name[1] == 'l'
1155 && library_search (&name, &mtime)))
1157 if (mtime != UNKNOWN_MTIME)
1158 /* vpath_search and library_search store UNKNOWN_MTIME
1159 if they didn't need to do a stat call for their work. */
1160 file->last_mtime = mtime;
1162 /* If we found it in VPATH, see if it's in GPATH too; if so,
1163 change the name right now; if not, defer until after the
1164 dependencies are updated. */
1165 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1167 rename_file (file, name);
1168 check_renamed (file);
1169 return file_mtime (file);
1172 rehash_file (file, name);
1173 check_renamed (file);
1174 mtime = name_mtime (name);
1180 /* Files can have bogus timestamps that nothing newly made will be
1181 "newer" than. Updating their dependents could just result in loops.
1182 So notify the user of the anomaly with a warning.
1184 We only need to do this once, for now. */
1186 if (!clock_skew_detected
1187 && mtime != NONEXISTENT_MTIME
1188 && !file->updated)
1190 static FILE_TIMESTAMP adjusted_now;
1192 FILE_TIMESTAMP adjusted_mtime = mtime;
1194 #if defined(WINDOWS32) || defined(__MSDOS__)
1195 /* Experimentation has shown that FAT filesystems can set file times
1196 up to 3 seconds into the future! Play it safe. */
1198 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1200 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1201 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1202 adjusted_mtime -= adjustment;
1203 #endif
1205 /* If the file's time appears to be in the future, update our
1206 concept of the present and try once more. */
1207 if (adjusted_now < adjusted_mtime)
1209 int resolution;
1210 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1211 adjusted_now = now + (resolution - 1);
1212 if (adjusted_now < adjusted_mtime)
1214 #ifdef NO_FLOAT
1215 error (NILF, _("Warning: File `%s' has modification time in the future"),
1216 file->name);
1217 #else
1218 double from_now =
1219 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1220 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1221 / 1e9));
1222 error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
1223 file->name, from_now);
1224 #endif
1225 clock_skew_detected = 1;
1231 /* Store the mtime into all the entries for this file. */
1232 if (file->double_colon)
1233 file = file->double_colon;
1237 /* If this file is not implicit but it is intermediate then it was
1238 made so by the .INTERMEDIATE target. If this file has never
1239 been built by us but was found now, it existed before make
1240 started. So, turn off the intermediate bit so make doesn't
1241 delete it, since it didn't create it. */
1242 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1243 && file->command_state == cs_not_started
1244 && !file->tried_implicit && file->intermediate)
1245 file->intermediate = 0;
1247 file->last_mtime = mtime;
1248 file = file->prev;
1250 while (file != 0);
1252 return mtime;
1256 /* Return the mtime of the file or archive-member reference NAME. */
1258 static FILE_TIMESTAMP
1259 name_mtime (char *name)
1261 struct stat st;
1262 int e;
1264 EINTRLOOP (e, stat (name, &st));
1265 if (e != 0)
1267 if (errno != ENOENT && errno != ENOTDIR)
1268 perror_with_name ("stat:", name);
1269 return NONEXISTENT_MTIME;
1272 return FILE_TIMESTAMP_STAT_MODTIME (name, st);
1276 /* Search for a library file specified as -lLIBNAME, searching for a
1277 suitable library file in the system library directories and the VPATH
1278 directories. */
1280 static int
1281 library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
1283 static char *dirs[] =
1285 #ifndef _AMIGA
1286 "/lib",
1287 "/usr/lib",
1288 #endif
1289 #if defined(WINDOWS32) && !defined(LIBDIR)
1291 * This is completely up to the user at product install time. Just define
1292 * a placeholder.
1294 #define LIBDIR "."
1295 #endif
1296 LIBDIR, /* Defined by configuration. */
1300 static char *libpatterns = NULL;
1302 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1303 FILE_TIMESTAMP mtime;
1305 /* Loop variables for the libpatterns value. */
1306 char *p, *p2;
1307 unsigned int len;
1309 char *file, **dp;
1311 /* If we don't have libpatterns, get it. */
1312 if (!libpatterns)
1314 int save = warn_undefined_variables_flag;
1315 warn_undefined_variables_flag = 0;
1317 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1319 warn_undefined_variables_flag = save;
1322 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1323 p2 = libpatterns;
1324 while ((p = find_next_token (&p2, &len)) != 0)
1326 static char *buf = NULL;
1327 static int buflen = 0;
1328 static int libdir_maxlen = -1;
1329 char *libbuf = variable_expand ("");
1331 /* Expand the pattern using LIBNAME as a replacement. */
1333 char c = p[len];
1334 char *p3, *p4;
1336 p[len] = '\0';
1337 p3 = find_percent (p);
1338 if (!p3)
1340 /* Give a warning if there is no pattern, then remove the
1341 pattern so it's ignored next time. */
1342 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1343 for (; len; --len, ++p)
1344 *p = ' ';
1345 *p = c;
1346 continue;
1348 p4 = variable_buffer_output (libbuf, p, p3-p);
1349 p4 = variable_buffer_output (p4, libname, strlen (libname));
1350 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1351 p[len] = c;
1354 /* Look first for `libNAME.a' in the current directory. */
1355 mtime = name_mtime (libbuf);
1356 if (mtime != NONEXISTENT_MTIME)
1358 *lib = xstrdup (libbuf);
1359 if (mtime_ptr != 0)
1360 *mtime_ptr = mtime;
1361 return 1;
1364 /* Now try VPATH search on that. */
1366 file = libbuf;
1367 if (vpath_search (&file, mtime_ptr))
1369 *lib = file;
1370 return 1;
1373 /* Now try the standard set of directories. */
1375 if (!buflen)
1377 for (dp = dirs; *dp != 0; ++dp)
1379 int l = strlen (*dp);
1380 if (l > libdir_maxlen)
1381 libdir_maxlen = l;
1383 buflen = strlen (libbuf);
1384 buf = xmalloc(libdir_maxlen + buflen + 2);
1386 else if (buflen < strlen (libbuf))
1388 buflen = strlen (libbuf);
1389 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1392 for (dp = dirs; *dp != 0; ++dp)
1394 sprintf (buf, "%s/%s", *dp, libbuf);
1395 mtime = name_mtime (buf);
1396 if (mtime != NONEXISTENT_MTIME)
1398 *lib = xstrdup (buf);
1399 if (mtime_ptr != 0)
1400 *mtime_ptr = mtime;
1401 return 1;
1406 return 0;