* Ensure -Iglob comes before any user-specified CPPFLAGS.
[make.git] / remake.c
blob1d7ac0808808945901da1ca427187b139998440d
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97,99 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "dep.h"
25 #include "variable.h"
27 #include <assert.h>
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #else
32 #include <sys/file.h>
33 #endif
35 #ifdef VMS
36 #include <starlet.h>
37 #endif
38 #ifdef WINDOWS32
39 #include <io.h>
40 #endif
42 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
45 /* Incremented when a command is started (under -n, when one would be). */
46 unsigned int commands_started = 0;
48 static int update_file PARAMS ((struct file *file, unsigned int depth));
49 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
50 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
51 static int touch_file PARAMS ((struct file *file));
52 static void remake_file PARAMS ((struct file *file));
53 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
54 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
57 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
58 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
59 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
60 be disabled for them unless they were also command-line targets, and we
61 should only make one goal at a time and return as soon as one goal whose
62 `changed' member is nonzero is successfully made. */
64 /* We need to know this "lower down" for correct error handling. */
65 static int updating_makefiles = 0;
67 int
68 update_goal_chain (goals, makefiles)
69 register struct dep *goals;
70 int makefiles;
72 int t = touch_flag, q = question_flag, n = just_print_flag;
73 unsigned int j = job_slots;
74 int status = -1;
76 updating_makefiles = makefiles;
78 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
79 : file_mtime (file))
81 /* Duplicate the chain so we can remove things from it. */
83 goals = copy_dep_chain (goals);
86 /* Clear the `changed' flag of each goal in the chain.
87 We will use the flag below to notice when any commands
88 have actually been run for a target. When no commands
89 have been run, we give an "up to date" diagnostic. */
91 struct dep *g;
92 for (g = goals; g != 0; g = g->next)
93 g->changed = 0;
96 /* All files start with the considered bit 0, so the global value is 1. */
97 considered = 1;
99 /* Update all the goals until they are all finished. */
101 while (goals != 0)
103 register struct dep *g, *lastgoal;
105 /* Start jobs that are waiting for the load to go down. */
107 start_waiting_jobs ();
109 /* Wait for a child to die. */
111 reap_children (1, 0);
113 lastgoal = 0;
114 g = goals;
115 while (g != 0)
117 /* Iterate over all double-colon entries for this file. */
118 struct file *file;
119 int stop = 0, any_not_updated = 0;
121 for (file = g->file->double_colon ? g->file->double_colon : g->file;
122 file != NULL;
123 file = file->prev)
125 unsigned int ocommands_started;
126 int x;
127 check_renamed (file);
128 if (makefiles)
130 if (file->cmd_target)
132 touch_flag = t;
133 question_flag = q;
134 just_print_flag = n;
136 else
137 touch_flag = question_flag = just_print_flag = 0;
140 /* Save the old value of `commands_started' so we can compare
141 later. It will be incremented when any commands are
142 actually run. */
143 ocommands_started = commands_started;
145 x = update_file (file, makefiles ? 1 : 0);
146 check_renamed (file);
148 /* Set the goal's `changed' flag if any commands were started
149 by calling update_file above. We check this flag below to
150 decide when to give an "up to date" diagnostic. */
151 g->changed += commands_started - ocommands_started;
153 /* If we updated a file and STATUS was not already 1, set it to
154 1 if updating failed, or to 0 if updating succeeded. Leave
155 STATUS as it is if no updating was done. */
157 stop = 0;
158 if ((x != 0 || file->updated) && status < 1)
160 if (file->update_status != 0)
162 /* Updating failed, or -q triggered. The STATUS value
163 tells our caller which. */
164 status = file->update_status;
165 /* If -q just triggered, stop immediately. It doesn't
166 matter how much more we run, since we already know
167 the answer to return. */
168 stop = (!keep_going_flag && !question_flag
169 && !makefiles);
171 else
173 FILE_TIMESTAMP mtime = MTIME (file);
174 check_renamed (file);
176 if (file->updated && g->changed &&
177 mtime != file->mtime_before_update)
179 /* Updating was done. If this is a makefile and
180 just_print_flag or question_flag is set (meaning
181 -n or -q was given and this file was specified
182 as a command-line target), don't change STATUS.
183 If STATUS is changed, we will get re-exec'd, and
184 enter an infinite loop. */
185 if (!makefiles
186 || (!just_print_flag && !question_flag))
187 status = 0;
188 if (makefiles && file->dontcare)
189 /* This is a default makefile; stop remaking. */
190 stop = 1;
195 /* Keep track if any double-colon entry is not finished.
196 When they are all finished, the goal is finished. */
197 any_not_updated |= !file->updated;
199 if (stop)
200 break;
203 /* Reset FILE since it is null at the end of the loop. */
204 file = g->file;
206 if (stop || !any_not_updated)
208 /* If we have found nothing whatever to do for the goal,
209 print a message saying nothing needs doing. */
211 if (!makefiles
212 /* If the update_status is zero, we updated successfully
213 or not at all. G->changed will have been set above if
214 any commands were actually started for this goal. */
215 && file->update_status == 0 && !g->changed
216 /* Never give a message under -s or -q. */
217 && !silent_flag && !question_flag)
218 message (1, ((file->phony || file->cmds == 0)
219 ? _("Nothing to be done for `%s'.")
220 : _("`%s' is up to date.")),
221 file->name);
223 /* This goal is finished. Remove it from the chain. */
224 if (lastgoal == 0)
225 goals = g->next;
226 else
227 lastgoal->next = g->next;
229 /* Free the storage. */
230 free ((char *) g);
232 g = lastgoal == 0 ? goals : lastgoal->next;
234 if (stop)
235 break;
237 else
239 lastgoal = g;
240 g = g->next;
244 /* If we reached the end of the dependency graph toggle the considered
245 flag for the next pass. */
246 if (g == 0)
247 considered = !considered;
250 if (makefiles)
252 touch_flag = t;
253 question_flag = q;
254 just_print_flag = n;
255 job_slots = j;
257 return status;
260 /* Generate an error/fatal message if no rules are available for the target.
262 static void
263 no_rule_error(file)
264 struct file *file;
266 static const char msg_noparent[]
267 = _("%sNo rule to make target `%s'%s");
268 static const char msg_parent[]
269 = _("%sNo rule to make target `%s', needed by `%s'%s");
271 if (keep_going_flag || file->dontcare)
273 /* If the previous attempt was made while we were creating
274 makefiles, but we aren't anymore, print an error now. */
275 if (!file->dontcare
276 || (file->mfile_status && !updating_makefiles))
278 if (file->parent == 0)
279 error (NILF, msg_noparent, "*** ", file->name, ".");
280 else
281 error (NILF, msg_parent, "*** ",
282 file->name, file->parent->name, ".");
284 file->update_status = 2;
285 file->mfile_status = updating_makefiles;
287 else if (file->parent == 0)
288 fatal (NILF, msg_noparent, "", file->name, "");
289 else
290 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
293 /* If FILE is not up to date, execute the commands for it.
294 Return 0 if successful, 1 if unsuccessful;
295 but with some flag settings, just call `exit' if unsuccessful.
297 DEPTH is the depth in recursions of this function.
298 We increment it during the consideration of our dependencies,
299 then decrement it again after finding out whether this file
300 is out of date.
302 If there are multiple double-colon entries for FILE,
303 each is considered in turn. */
305 static int
306 update_file (file, depth)
307 struct file *file;
308 unsigned int depth;
310 register int status = 0;
311 register struct file *f;
313 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
315 /* Prune the dependency graph: if we've already been here on _this_
316 pass through the dependency graph, we don't have to go any further.
317 We won't reap_children until we start the next pass, so no state
318 change is possible below here until then. */
319 if (f->considered == considered)
321 DEBUGPR (_("Pruning file `%s'.\n"));
322 continue;
324 f->considered = considered;
326 status |= update_file_1 (f, depth);
327 check_renamed (f);
329 if (status != 0 && !keep_going_flag)
330 return status;
332 switch (f->command_state)
334 case cs_finished:
335 /* The file is done being remade. */
336 break;
338 case cs_running:
339 case cs_deps_running:
340 /* Don't run the other :: rules for this
341 file until this rule is finished. */
342 return 0;
344 default:
345 assert (f->command_state == cs_running);
346 break;
350 return status;
353 /* Consider a single `struct file' and update it as appropriate. */
355 static int
356 update_file_1 (file, depth)
357 struct file *file;
358 unsigned int depth;
360 register FILE_TIMESTAMP this_mtime;
361 int noexist, must_make, deps_changed;
362 int dep_status = 0;
363 register struct dep *d, *lastd;
364 int running = 0;
366 DEBUGPR (_("Considering target file `%s'.\n"));
368 if (file->updated)
370 if (file->update_status > 0)
372 DEBUGPR (_("Recently tried and failed to update file `%s'.\n"));
373 no_rule_error(file);
374 return file->update_status;
377 DEBUGPR (_("File `%s' was considered already.\n"));
378 return 0;
381 switch (file->command_state)
383 case cs_not_started:
384 case cs_deps_running:
385 break;
386 case cs_running:
387 DEBUGPR (_("Still updating file `%s'.\n"));
388 return 0;
389 case cs_finished:
390 DEBUGPR (_("Finished updating file `%s'.\n"));
391 return file->update_status;
392 default:
393 abort ();
396 ++depth;
398 /* Notice recursive update of the same file. */
399 file->updating = 1;
401 /* Looking at the file's modtime beforehand allows the possibility
402 that its name may be changed by a VPATH search, and thus it may
403 not need an implicit rule. If this were not done, the file
404 might get implicit commands that apply to its initial name, only
405 to have that name replaced with another found by VPATH search. */
407 this_mtime = file_mtime (file);
408 check_renamed (file);
409 noexist = this_mtime == (FILE_TIMESTAMP) -1;
410 if (noexist)
411 DEBUGPR (_("File `%s' does not exist.\n"));
413 must_make = noexist;
415 /* If file was specified as a target with no commands,
416 come up with some default commands. */
418 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
420 if (try_implicit_rule (file, depth))
421 DEBUGPR (_("Found an implicit rule for `%s'.\n"));
422 else
423 DEBUGPR (_("No implicit rule found for `%s'.\n"));
424 file->tried_implicit = 1;
426 if (file->cmds == 0 && !file->is_target
427 && default_file != 0 && default_file->cmds != 0)
429 DEBUGPR (_("Using default commands for `%s'.\n"));
430 file->cmds = default_file->cmds;
433 /* Update all non-intermediate files we depend on, if necessary,
434 and see whether any of them is more recent than this file. */
436 lastd = 0;
437 d = file->deps;
438 while (d != 0)
440 FILE_TIMESTAMP mtime;
442 check_renamed (d->file);
444 mtime = file_mtime (d->file);
445 check_renamed (d->file);
447 if (d->file->updating)
449 error (NILF, _("Circular %s <- %s dependency dropped."),
450 file->name, d->file->name);
451 /* We cannot free D here because our the caller will still have
452 a reference to it when we were called recursively via
453 check_dep below. */
454 if (lastd == 0)
455 file->deps = d->next;
456 else
457 lastd->next = d->next;
458 d = d->next;
459 continue;
462 d->file->parent = file;
463 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
464 check_renamed (d->file);
467 register struct file *f = d->file;
468 if (f->double_colon)
469 f = f->double_colon;
472 running |= (f->command_state == cs_running
473 || f->command_state == cs_deps_running);
474 f = f->prev;
476 while (f != 0);
479 if (dep_status != 0 && !keep_going_flag)
480 break;
482 if (!running)
483 d->changed = file_mtime (d->file) != mtime;
485 lastd = d;
486 d = d->next;
489 /* Now we know whether this target needs updating.
490 If it does, update all the intermediate files we depend on. */
492 if (must_make)
494 for (d = file->deps; d != 0; d = d->next)
495 if (d->file->intermediate)
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);
504 register struct file *f = d->file;
505 if (f->double_colon)
506 f = f->double_colon;
509 running |= (f->command_state == cs_running
510 || f->command_state == cs_deps_running);
511 f = f->prev;
513 while (f != 0);
516 if (dep_status != 0 && !keep_going_flag)
517 break;
519 if (!running)
520 d->changed = ((file->phony && file->cmds != 0)
521 || file_mtime (d->file) != mtime);
525 file->updating = 0;
527 DEBUGPR (_("Finished prerequisites of target file `%s'.\n"));
529 if (running)
531 set_command_state (file, cs_deps_running);
532 --depth;
533 DEBUGPR (_("The prerequisites of `%s' are being made.\n"));
534 return 0;
537 /* If any dependency failed, give up now. */
539 if (dep_status != 0)
541 file->update_status = dep_status;
542 notice_finished_file (file);
544 depth--;
546 DEBUGPR (_("Giving up on target file `%s'.\n"));
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);
553 return dep_status;
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);
567 /* Now record which dependencies are more
568 recent than this file, so we can define $?. */
570 deps_changed = 0;
571 for (d = file->deps; d != 0; d = d->next)
573 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
574 check_renamed (d->file);
576 #if 1 /* %%% 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 == (FILE_TIMESTAMP) -1 && !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;
588 /* Set D->changed if either this dep actually changed,
589 or its dependent, FILE, is older or does not exist. */
590 d->changed |= noexist || d_mtime > this_mtime;
592 if (debug_flag && !noexist)
594 print_spaces (depth);
595 if (d_mtime == (FILE_TIMESTAMP) -1)
596 printf (_("Prerequisite `%s' does not exist.\n"), dep_name (d));
597 else
598 printf (_("Prerequisite `%s' is %s than target `%s'.\n"),
599 dep_name (d), d->changed ? _("newer") : _("older"), file->name);
600 fflush (stdout);
604 /* Here depth returns to the value it had when we were called. */
605 depth--;
607 if (file->double_colon && file->deps == 0)
609 must_make = 1;
610 DEBUGPR (_("Target `%s' is double-colon and has no prerequisites.\n"));
612 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
614 must_make = 0;
615 DEBUGPR (_("No commands for `%s' and no prerequisites actually changed.\n"));
618 if (!must_make)
620 if (debug_flag)
622 print_spaces(depth);
623 printf(_("No need to remake target `%s'"), file->name);
624 if (!streq(file->name, file->hname))
625 printf(_("; using VPATH name `%s'"), file->hname);
626 printf(".\n");
627 fflush(stdout);
630 notice_finished_file (file);
632 /* Since we don't need to remake the file, convert it to use the
633 VPATH filename if we found one. hfile will be either the
634 local name if no VPATH or the VPATH name if one was found. */
636 while (file)
638 file->name = file->hname;
639 file = file->prev;
642 return 0;
645 DEBUGPR (_("Must remake target `%s'.\n"));
647 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
648 VPATH. */
649 if (!streq(file->name, file->hname))
651 if (debug_flag)
653 print_spaces (depth);
654 printf(_(" Ignoring VPATH name `%s'.\n"), file->hname);
655 fflush(stdout);
657 file->ignore_vpath = 1;
660 /* Now, take appropriate actions to remake the file. */
661 remake_file (file);
663 if (file->command_state != cs_finished)
665 DEBUGPR (_("Commands of `%s' are being run.\n"));
666 return 0;
669 switch (file->update_status)
671 case 2:
672 DEBUGPR (_("Failed to remake target file `%s'.\n"));
673 break;
674 case 0:
675 DEBUGPR (_("Successfully remade target file `%s'.\n"));
676 break;
677 case 1:
678 DEBUGPR (_("Target file `%s' needs remade under -q.\n"));
679 break;
680 default:
681 assert (file->update_status >= 0 && file->update_status <= 2);
682 break;
685 file->updated = 1;
686 return file->update_status;
689 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
690 files listed in its `also_make' member. Under -t, this function also
691 touches FILE.
693 On return, FILE->update_status will no longer be -1 if it was. */
695 void
696 notice_finished_file (file)
697 register struct file *file;
699 struct dep *d;
700 int ran = file->command_state == cs_running;
702 file->command_state = cs_finished;
703 file->updated = 1;
705 if (touch_flag
706 /* The update status will be:
707 -1 if this target was not remade;
708 0 if 0 or more commands (+ or ${MAKE}) were run and won;
709 1 if some commands were run and lost.
710 We touch the target if it has commands which either were not run
711 or won when they ran (i.e. status is 0). */
712 && file->update_status == 0)
714 if (file->cmds != 0 && file->cmds->any_recurse)
716 /* If all the command lines were recursive,
717 we don't want to do the touching. */
718 unsigned int i;
719 for (i = 0; i < file->cmds->ncommand_lines; ++i)
720 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
721 goto have_nonrecursing;
723 else
725 have_nonrecursing:
726 if (file->phony)
727 file->update_status = 0;
728 else
729 /* Should set file's modification date and do nothing else. */
730 file->update_status = touch_file (file);
734 if (file->mtime_before_update == 0)
735 file->mtime_before_update = file->last_mtime;
737 if (ran && !file->phony)
739 struct file *f;
740 int i = 0;
742 /* If -n or -q and all the commands are recursive, we ran them so
743 really check the target's mtime again. Otherwise, assume the target
744 would have been updated. */
746 if (question_flag || just_print_flag)
747 for (i = file->cmds->ncommand_lines; i > 0; --i)
748 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
749 break;
751 /* If there were no commands at all, it's always new. */
753 else if (file->is_target && file->cmds == 0)
754 i = 1;
756 file->last_mtime = i == 0 ? 0 : NEW_MTIME;
758 /* Propagate the change of modification time to all the double-colon
759 entries for this file. */
760 for (f = file->double_colon; f != 0; f = f->next)
761 f->last_mtime = file->last_mtime;
764 if (ran && file->update_status != -1)
765 /* We actually tried to update FILE, which has
766 updated its also_make's as well (if it worked).
767 If it didn't work, it wouldn't work again for them.
768 So mark them as updated with the same status. */
769 for (d = file->also_make; d != 0; d = d->next)
771 d->file->command_state = cs_finished;
772 d->file->updated = 1;
773 d->file->update_status = file->update_status;
775 if (ran && !d->file->phony)
776 /* Fetch the new modification time.
777 We do this instead of just invalidating the cached time
778 so that a vpath_search can happen. Otherwise, it would
779 never be done because the target is already updated. */
780 (void) f_mtime (d->file, 0);
782 else if (file->update_status == -1)
783 /* Nothing was done for FILE, but it needed nothing done.
784 So mark it now as "succeeded". */
785 file->update_status = 0;
788 /* Check whether another file (whose mtime is THIS_MTIME)
789 needs updating on account of a dependency which is file FILE.
790 If it does, store 1 in *MUST_MAKE_PTR.
791 In the process, update any non-intermediate files
792 that FILE depends on (including FILE itself).
793 Return nonzero if any updating failed. */
795 static int
796 check_dep (file, depth, this_mtime, must_make_ptr)
797 struct file *file;
798 unsigned int depth;
799 FILE_TIMESTAMP this_mtime;
800 int *must_make_ptr;
802 register struct dep *d;
803 int dep_status = 0;
805 ++depth;
806 file->updating = 1;
808 if (!file->intermediate)
809 /* If this is a non-intermediate file, update it and record
810 whether it is newer than THIS_MTIME. */
812 FILE_TIMESTAMP mtime;
813 dep_status = update_file (file, depth);
814 check_renamed (file);
815 mtime = file_mtime (file);
816 check_renamed (file);
817 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
818 *must_make_ptr = 1;
820 else
822 /* FILE is an intermediate file. */
823 FILE_TIMESTAMP mtime;
825 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
827 if (try_implicit_rule (file, depth))
828 DEBUGPR (_("Found an implicit rule for `%s'.\n"));
829 else
830 DEBUGPR (_("No implicit rule found for `%s'.\n"));
831 file->tried_implicit = 1;
833 if (file->cmds == 0 && !file->is_target
834 && default_file != 0 && default_file->cmds != 0)
836 DEBUGPR (_("Using default commands for `%s'.\n"));
837 file->cmds = default_file->cmds;
840 /* If the intermediate file actually exists
841 and is newer, then we should remake from it. */
842 check_renamed (file);
843 mtime = file_mtime (file);
844 check_renamed (file);
845 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
846 *must_make_ptr = 1;
847 /* Otherwise, update all non-intermediate files we depend on,
848 if necessary, and see whether any of them is more
849 recent than the file on whose behalf we are checking. */
850 else
852 register struct dep *lastd;
854 lastd = 0;
855 d = file->deps;
856 while (d != 0)
858 if (d->file->updating)
860 error (NILF, _("Circular %s <- %s dependency dropped."),
861 file->name, d->file->name);
862 if (lastd == 0)
864 file->deps = d->next;
865 free ((char *) d);
866 d = file->deps;
868 else
870 lastd->next = d->next;
871 free ((char *) d);
872 d = lastd->next;
874 continue;
877 d->file->parent = file;
878 dep_status |= check_dep (d->file, depth, this_mtime,
879 must_make_ptr);
880 check_renamed (d->file);
881 if (dep_status != 0 && !keep_going_flag)
882 break;
884 if (d->file->command_state == cs_running
885 || d->file->command_state == cs_deps_running)
886 /* Record that some of FILE's deps are still being made.
887 This tells the upper levels to wait on processing it until
888 the commands are finished. */
889 set_command_state (file, cs_deps_running);
891 lastd = d;
892 d = d->next;
897 file->updating = 0;
898 return dep_status;
901 /* Touch FILE. Return zero if successful, one if not. */
903 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
905 static int
906 touch_file (file)
907 register struct file *file;
909 if (!silent_flag)
910 message (0, "touch %s", file->name);
912 #ifndef NO_ARCHIVES
913 if (ar_name (file->name))
914 return ar_touch (file->name);
915 else
916 #endif
918 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
920 if (fd < 0)
921 TOUCH_ERROR ("touch: open: ");
922 else
924 struct stat statbuf;
925 char buf;
926 int status;
929 status = fstat (fd, &statbuf);
930 while (status < 0 && EINTR_SET);
932 if (status < 0)
933 TOUCH_ERROR ("touch: fstat: ");
934 /* Rewrite character 0 same as it already is. */
935 if (read (fd, &buf, 1) < 0)
936 TOUCH_ERROR ("touch: read: ");
937 if (lseek (fd, 0L, 0) < 0L)
938 TOUCH_ERROR ("touch: lseek: ");
939 if (write (fd, &buf, 1) < 0)
940 TOUCH_ERROR ("touch: write: ");
941 /* If file length was 0, we just
942 changed it, so change it back. */
943 if (statbuf.st_size == 0)
945 (void) close (fd);
946 fd = open (file->name, O_RDWR | O_TRUNC, 0666);
947 if (fd < 0)
948 TOUCH_ERROR ("touch: open: ");
950 (void) close (fd);
954 return 0;
957 /* Having checked and updated the dependencies of FILE,
958 do whatever is appropriate to remake FILE itself.
959 Return the status from executing FILE's commands. */
961 static void
962 remake_file (file)
963 struct file *file;
965 if (file->cmds == 0)
967 if (file->phony)
968 /* Phony target. Pretend it succeeded. */
969 file->update_status = 0;
970 else if (file->is_target)
971 /* This is a nonexistent target file we cannot make.
972 Pretend it was successfully remade. */
973 file->update_status = 0;
974 else
975 no_rule_error (file);
977 else
979 chop_commands (file->cmds);
981 /* The normal case: start some commands. */
982 if (!touch_flag || file->cmds->any_recurse)
984 execute_file_commands (file);
985 return;
988 /* This tells notice_finished_file it is ok to touch the file. */
989 file->update_status = 0;
992 /* This does the touching under -t. */
993 notice_finished_file (file);
996 /* Return the mtime of a file, given a `struct file'.
997 Caches the time in the struct file to avoid excess stat calls.
999 If the file is not found, and SEARCH is nonzero, VPATH searching and
1000 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1001 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1002 FILE. */
1004 FILE_TIMESTAMP
1005 f_mtime (file, search)
1006 register struct file *file;
1007 int search;
1009 FILE_TIMESTAMP mtime;
1011 /* File's mtime is not known; must get it from the system. */
1013 #ifndef NO_ARCHIVES
1014 if (ar_name (file->name))
1016 /* This file is an archive-member reference. */
1018 char *arname, *memname;
1019 struct file *arfile;
1020 int arname_used = 0;
1022 /* Find the archive's name. */
1023 ar_parse_name (file->name, &arname, &memname);
1025 /* Find the modification time of the archive itself.
1026 Also allow for its name to be changed via VPATH search. */
1027 arfile = lookup_file (arname);
1028 if (arfile == 0)
1030 arfile = enter_file (arname);
1031 arname_used = 1;
1033 mtime = f_mtime (arfile, search);
1034 check_renamed (arfile);
1035 if (search && strcmp (arfile->hname, arname))
1037 /* The archive's name has changed.
1038 Change the archive-member reference accordingly. */
1040 char *name;
1041 unsigned int arlen, memlen;
1043 if (!arname_used)
1045 free (arname);
1046 arname_used = 1;
1049 arname = arfile->hname;
1050 arlen = strlen (arname);
1051 memlen = strlen (memname);
1053 /* free (file->name); */
1055 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1056 bcopy (arname, name, arlen);
1057 name[arlen] = '(';
1058 bcopy (memname, name + arlen + 1, memlen);
1059 name[arlen + 1 + memlen] = ')';
1060 name[arlen + 1 + memlen + 1] = '\0';
1062 /* If the archive was found with GPATH, make the change permanent;
1063 otherwise defer it until later. */
1064 if (arfile->name == arfile->hname)
1065 rename_file (file, name);
1066 else
1067 rehash_file (file, name);
1068 check_renamed (file);
1071 if (!arname_used)
1072 free (arname);
1073 free (memname);
1075 if (mtime == (FILE_TIMESTAMP) -1)
1076 /* The archive doesn't exist, so it's members don't exist either. */
1077 return (FILE_TIMESTAMP) -1;
1079 mtime = ar_member_date (file->hname);
1081 else
1082 #endif
1084 mtime = name_mtime (file->name);
1086 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1088 /* If name_mtime failed, search VPATH. */
1089 char *name = file->name;
1090 if (vpath_search (&name, &mtime)
1091 /* Last resort, is it a library (-lxxx)? */
1092 || (name[0] == '-' && name[1] == 'l'
1093 && library_search (&name, &mtime)))
1095 if (mtime != 0)
1096 /* vpath_search and library_search store zero in MTIME
1097 if they didn't need to do a stat call for their work. */
1098 file->last_mtime = mtime;
1100 /* If we found it in VPATH, see if it's in GPATH too; if so,
1101 change the name right now; if not, defer until after the
1102 dependencies are updated. */
1103 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1105 rename_file (file, name);
1106 check_renamed (file);
1107 return file_mtime (file);
1110 rehash_file (file, name);
1111 check_renamed (file);
1112 mtime = name_mtime (name);
1118 /* Files can have bogus timestamps that nothing newly made will be
1119 "newer" than. Updating their dependents could just result in loops.
1120 So notify the user of the anomaly with a warning.
1122 We only need to do this once, for now. */
1124 static FILE_TIMESTAMP now = 0;
1125 if (!clock_skew_detected
1126 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1127 && !file->updated)
1129 /* This file's time appears to be in the future.
1130 Update our concept of the present, and compare again. */
1132 now = file_timestamp_now ();
1134 #ifdef WINDOWS32
1136 * FAT filesystems round time to nearest even second(!). Just
1137 * allow for any file (NTFS or FAT) to perhaps suffer from this
1138 * braindamage.
1140 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1141 #else
1142 #ifdef __MSDOS__
1143 /* Scrupulous testing indicates that some Windows
1144 filesystems can set file times up to 3 sec into the future! */
1145 if (mtime > now + 3)
1146 #else
1147 if (mtime > now)
1148 #endif
1149 #endif
1151 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1152 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1154 file_timestamp_sprintf (mtimebuf, mtime);
1155 file_timestamp_sprintf (nowbuf, now);
1156 error (NILF, _("*** Warning: File `%s' has modification time in the future (%s > %s)"),
1157 file->name, mtimebuf, nowbuf);
1158 clock_skew_detected = 1;
1163 /* Store the mtime into all the entries for this file. */
1164 if (file->double_colon)
1165 file = file->double_colon;
1169 /* If this file is not implicit but it is intermediate then it was
1170 made so by the .INTERMEDIATE target. If this file has never
1171 been built by us but was found now, it existed before make
1172 started. So, turn off the intermediate bit so make doesn't
1173 delete it, since it didn't create it. */
1174 if (mtime != (FILE_TIMESTAMP)-1 && file->command_state == cs_not_started
1175 && !file->tried_implicit && file->intermediate)
1176 file->intermediate = 0;
1178 file->last_mtime = mtime;
1179 file = file->prev;
1181 while (file != 0);
1183 return mtime;
1187 /* Return the mtime of the file or archive-member reference NAME. */
1189 static FILE_TIMESTAMP
1190 name_mtime (name)
1191 register char *name;
1193 struct stat st;
1195 if (stat (name, &st) < 0)
1196 return (FILE_TIMESTAMP) -1;
1198 return FILE_TIMESTAMP_STAT_MODTIME (st);
1202 /* Search for a library file specified as -lLIBNAME, searching for a
1203 suitable library file in the system library directories and the VPATH
1204 directories. */
1206 static int
1207 library_search (lib, mtime_ptr)
1208 char **lib;
1209 FILE_TIMESTAMP *mtime_ptr;
1211 static char *dirs[] =
1213 #ifndef _AMIGA
1214 "/lib",
1215 "/usr/lib",
1216 #endif
1217 #if defined(WINDOWS32) && !defined(LIBDIR)
1219 * This is completely up to the user at product install time. Just define
1220 * a placeholder.
1222 #define LIBDIR "."
1223 #endif
1224 LIBDIR, /* Defined by configuration. */
1228 static char *libpatterns = NULL;
1230 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1231 FILE_TIMESTAMP mtime;
1233 /* Loop variables for the libpatterns value. */
1234 char *p, *p2;
1235 int len;
1237 char *file, **dp;
1239 /* If we don't have libpatterns, get it. */
1240 if (!libpatterns)
1242 int save = warn_undefined_variables_flag;
1243 warn_undefined_variables_flag = 0;
1245 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1247 warn_undefined_variables_flag = save;
1250 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1251 p2 = libpatterns;
1252 while ((p = find_next_token (&p2, &len)) != 0)
1254 static char *buf = NULL;
1255 static int buflen = 0;
1256 static int libdir_maxlen = -1;
1257 char *libbuf = variable_expand ("");
1259 /* Expand the pattern using LIBNAME as a replacement. */
1261 char c = p[len];
1262 char *p3, *p4;
1264 p[len] = '\0';
1265 p3 = find_percent (p);
1266 if (!p3)
1268 /* Give a warning if there is no pattern, then remove the
1269 pattern so it's ignored next time. */
1270 error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
1271 for (; len; --len, ++p)
1272 *p = ' ';
1273 *p = c;
1274 continue;
1276 p4 = variable_buffer_output (libbuf, p, p3-p);
1277 p4 = variable_buffer_output (p4, libname, strlen (libname));
1278 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1279 p[len] = c;
1282 /* Look first for `libNAME.a' in the current directory. */
1283 mtime = name_mtime (libbuf);
1284 if (mtime != (FILE_TIMESTAMP) -1)
1286 *lib = xstrdup (libbuf);
1287 if (mtime_ptr != 0)
1288 *mtime_ptr = mtime;
1289 return 1;
1292 /* Now try VPATH search on that. */
1294 file = libbuf;
1295 if (vpath_search (&file, mtime_ptr))
1297 *lib = file;
1298 return 1;
1301 /* Now try the standard set of directories. */
1303 if (!buflen)
1305 for (dp = dirs; *dp != 0; ++dp)
1307 int l = strlen (*dp);
1308 if (l > libdir_maxlen)
1309 libdir_maxlen = l;
1311 buflen = strlen (libbuf);
1312 buf = xmalloc(libdir_maxlen + buflen + 2);
1314 else if (buflen < strlen (libbuf))
1316 buflen = strlen (libbuf);
1317 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1320 for (dp = dirs; *dp != 0; ++dp)
1322 sprintf (buf, "%s/%s", *dp, libbuf);
1323 mtime = name_mtime (buf);
1324 if (mtime != (FILE_TIMESTAMP) -1)
1326 *lib = xstrdup (buf);
1327 if (mtime_ptr != 0)
1328 *mtime_ptr = mtime;
1329 return 1;
1334 return 0;