* Update the manual.
[make.git] / remake.c
blob90de313c71b62378bb46273a5de93ed28372bde6
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 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 __MSDOS__
36 #include "variable.h"
37 #endif
39 #ifdef VMS
40 #include <starlet.h>
41 #endif
42 #ifdef WINDOWS32
43 #include <io.h>
44 #endif
46 extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
49 /* Incremented when a command is started (under -n, when one would be). */
50 unsigned int commands_started = 0;
52 static int update_file PARAMS ((struct file *file, unsigned int depth));
53 static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
54 static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
55 static int touch_file PARAMS ((struct file *file));
56 static void remake_file PARAMS ((struct file *file));
57 static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
58 static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
61 /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
62 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
63 If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
64 be disabled for them unless they were also command-line targets, and we
65 should only make one goal at a time and return as soon as one goal whose
66 `changed' member is nonzero is successfully made. */
68 /* We need to know this "lower down" for correct error handling. */
69 static int updating_makefiles = 0;
71 int
72 update_goal_chain (goals, makefiles)
73 register struct dep *goals;
74 int makefiles;
76 int t = touch_flag, q = question_flag, n = just_print_flag;
77 unsigned int j = job_slots;
78 int status = -1;
80 updating_makefiles = makefiles;
82 #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
83 : file_mtime (file))
85 /* Duplicate the chain so we can remove things from it. */
87 goals = copy_dep_chain (goals);
90 /* Clear the `changed' flag of each goal in the chain.
91 We will use the flag below to notice when any commands
92 have actually been run for a target. When no commands
93 have been run, we give an "up to date" diagnostic. */
95 struct dep *g;
96 for (g = goals; g != 0; g = g->next)
97 g->changed = 0;
100 #if 0
101 /* Only run one job at a time when building makefiles.
102 No one seems to know why this was done, and no one can think of a good
103 reason to do it. Hopefully an obvious one won't appear as soon as we
104 release the next version :-/. */
105 if (makefiles)
106 job_slots = 1;
107 #endif
109 /* All files start with the considered bit 0, so the global value is 1. */
110 considered = 1;
112 /* Update all the goals until they are all finished. */
114 while (goals != 0)
116 register struct dep *g, *lastgoal;
118 /* Start jobs that are waiting for the load to go down. */
120 start_waiting_jobs ();
122 /* Wait for a child to die. */
124 reap_children (1, 0);
126 lastgoal = 0;
127 g = goals;
128 while (g != 0)
130 /* Iterate over all double-colon entries for this file. */
131 struct file *file;
132 int stop = 0, any_not_updated = 0;
134 for (file = g->file->double_colon ? g->file->double_colon : g->file;
135 file != NULL;
136 file = file->prev)
138 unsigned int ocommands_started;
139 int x;
140 check_renamed (file);
141 if (makefiles)
143 if (file->cmd_target)
145 touch_flag = t;
146 question_flag = q;
147 just_print_flag = n;
149 else
150 touch_flag = question_flag = just_print_flag = 0;
153 /* Save the old value of `commands_started' so we can compare
154 later. It will be incremented when any commands are
155 actually run. */
156 ocommands_started = commands_started;
158 x = update_file (file, makefiles ? 1 : 0);
159 check_renamed (file);
161 /* Set the goal's `changed' flag if any commands were started
162 by calling update_file above. We check this flag below to
163 decide when to give an "up to date" diagnostic. */
164 g->changed += commands_started - ocommands_started;
166 stop = 0;
167 if (x != 0 || file->updated)
169 /* If STATUS was not already 1, set it to 1 if
170 updating failed, or to 0 if updating succeeded.
171 Leave STATUS as it is if no updating was done. */
173 if (status < 1)
175 if (file->update_status != 0)
177 /* Updating failed, or -q triggered.
178 The STATUS value tells our caller which. */
179 status = file->update_status;
180 /* If -q just triggered, stop immediately.
181 It doesn't matter how much more we run,
182 since we already know the answer to return. */
183 stop = (!keep_going_flag && !question_flag
184 && !makefiles);
186 else if (file->updated && g->changed &&
187 file->last_mtime != file->mtime_before_update)
189 /* Updating was done. If this is a makefile and
190 just_print_flag or question_flag is set
191 (meaning -n or -q was given and this file was
192 specified as a command-line target), don't
193 change STATUS. If STATUS is changed, we will
194 get re-exec'd, and fall into 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 /* Generate an error/fatal message if no rules are available for the target.
272 static void
273 no_rule_error(file)
274 struct file *file;
276 static const char msg_noparent[]
277 = "%sNo rule to make target `%s'%s";
278 static const char msg_parent[]
279 = "%sNo rule to make target `%s', needed by `%s'%s";
281 if (keep_going_flag || file->dontcare)
283 /* If the previous attempt was made while we were creating
284 makefiles, but we aren't anymore, print an error now. */
285 if (!file->dontcare
286 || (file->mfile_status && !updating_makefiles))
288 if (file->parent == 0)
289 error (NILF, msg_noparent, "*** ", file->name, ".");
290 else
291 error (NILF, msg_parent, "*** ",
292 file->name, file->parent->name, ".");
294 file->update_status = 2;
295 file->mfile_status = updating_makefiles;
297 else if (file->parent == 0)
298 fatal (NILF, msg_noparent, "", file->name, "");
299 else
300 fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
303 /* If FILE is not up to date, execute the commands for it.
304 Return 0 if successful, 1 if unsuccessful;
305 but with some flag settings, just call `exit' if unsuccessful.
307 DEPTH is the depth in recursions of this function.
308 We increment it during the consideration of our dependencies,
309 then decrement it again after finding out whether this file
310 is out of date.
312 If there are multiple double-colon entries for FILE,
313 each is considered in turn. */
315 static int
316 update_file (file, depth)
317 struct file *file;
318 unsigned int depth;
320 register int status = 0;
321 register struct file *f;
323 /* Prune the dependency graph: if we've already been here on _this_ pass
324 through the dependency graph, we don't have to go any further. We won't
325 reap_children until we start the next pass, so no state change is
326 possible below here until then. */
327 if (file->considered == considered)
329 DEBUGPR ("Pruning file `%s'.\n");
330 return 0;
332 file->considered = considered;
334 for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
336 status |= update_file_1 (f, depth);
337 check_renamed (f);
339 if (status != 0 && !keep_going_flag)
340 return status;
342 switch (f->command_state)
344 case cs_finished:
345 /* The file is done being remade. */
346 break;
348 case cs_running:
349 case cs_deps_running:
350 /* Don't run the other :: rules for this
351 file until this rule is finished. */
352 return 0;
354 default:
355 assert (f->command_state == cs_running);
356 break;
360 return status;
363 /* Consider a single `struct file' and update it as appropriate. */
365 static int
366 update_file_1 (file, depth)
367 struct file *file;
368 unsigned int depth;
370 register FILE_TIMESTAMP this_mtime;
371 int noexist, must_make, deps_changed;
372 int dep_status = 0;
373 register struct dep *d, *lastd;
374 int running = 0;
376 DEBUGPR ("Considering target file `%s'.\n");
378 if (file->updated)
380 if (file->update_status > 0)
382 DEBUGPR ("Recently tried and failed to update file `%s'.\n");
383 no_rule_error(file);
384 return file->update_status;
387 DEBUGPR ("File `%s' was considered already.\n");
388 return 0;
391 switch (file->command_state)
393 case cs_not_started:
394 case cs_deps_running:
395 break;
396 case cs_running:
397 DEBUGPR ("Still updating file `%s'.\n");
398 return 0;
399 case cs_finished:
400 DEBUGPR ("Finished updating file `%s'.\n");
401 return file->update_status;
402 default:
403 abort ();
406 ++depth;
408 /* Notice recursive update of the same file. */
409 file->updating = 1;
411 /* Looking at the file's modtime beforehand allows the possibility
412 that its name may be changed by a VPATH search, and thus it may
413 not need an implicit rule. If this were not done, the file
414 might get implicit commands that apply to its initial name, only
415 to have that name replaced with another found by VPATH search. */
417 this_mtime = file_mtime (file);
418 check_renamed (file);
419 noexist = this_mtime == (FILE_TIMESTAMP) -1;
420 if (noexist)
421 DEBUGPR ("File `%s' does not exist.\n");
423 must_make = noexist;
425 /* If file was specified as a target with no commands,
426 come up with some default commands. */
428 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
430 if (try_implicit_rule (file, depth))
431 DEBUGPR ("Found an implicit rule for `%s'.\n");
432 else
433 DEBUGPR ("No implicit rule found for `%s'.\n");
434 file->tried_implicit = 1;
436 if (file->cmds == 0 && !file->is_target
437 && default_file != 0 && default_file->cmds != 0)
439 DEBUGPR ("Using default commands for `%s'.\n");
440 file->cmds = default_file->cmds;
443 /* Update all non-intermediate files we depend on, if necessary,
444 and see whether any of them is more recent than this file. */
446 lastd = 0;
447 d = file->deps;
448 while (d != 0)
450 FILE_TIMESTAMP mtime;
452 check_renamed (d->file);
454 mtime = file_mtime (d->file);
455 check_renamed (d->file);
457 if (d->file->updating)
459 error (NILF, "Circular %s <- %s dependency dropped.",
460 file->name, d->file->name);
461 /* We cannot free D here because our the caller will still have
462 a reference to it when we were called recursively via
463 check_dep below. */
464 if (lastd == 0)
465 file->deps = d->next;
466 else
467 lastd->next = d->next;
468 d = d->next;
469 continue;
472 d->file->parent = file;
473 dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
474 check_renamed (d->file);
477 register struct file *f = d->file;
478 if (f->double_colon)
479 f = f->double_colon;
482 running |= (f->command_state == cs_running
483 || f->command_state == cs_deps_running);
484 f = f->prev;
486 while (f != 0);
489 if (dep_status != 0 && !keep_going_flag)
490 break;
492 if (!running)
493 d->changed = file_mtime (d->file) != mtime;
495 lastd = d;
496 d = d->next;
499 /* Now we know whether this target needs updating.
500 If it does, update all the intermediate files we depend on. */
502 if (must_make)
504 for (d = file->deps; d != 0; d = d->next)
505 if (d->file->intermediate)
507 FILE_TIMESTAMP mtime = file_mtime (d->file);
508 check_renamed (d->file);
509 d->file->parent = file;
510 dep_status |= update_file (d->file, depth);
511 check_renamed (d->file);
514 register struct file *f = d->file;
515 if (f->double_colon)
516 f = f->double_colon;
519 running |= (f->command_state == cs_running
520 || f->command_state == cs_deps_running);
521 f = f->prev;
523 while (f != 0);
526 if (dep_status != 0 && !keep_going_flag)
527 break;
529 if (!running)
530 d->changed = ((file->phony && file->cmds != 0)
531 || file_mtime (d->file) != mtime);
535 file->updating = 0;
537 DEBUGPR ("Finished dependencies of target file `%s'.\n");
539 if (running)
541 set_command_state (file, cs_deps_running);
542 --depth;
543 DEBUGPR ("The dependencies of `%s' are being made.\n");
544 return 0;
547 /* If any dependency failed, give up now. */
549 if (dep_status != 0)
551 file->update_status = dep_status;
552 notice_finished_file (file);
554 depth--;
556 DEBUGPR ("Giving up on target file `%s'.\n");
558 if (depth == 0 && keep_going_flag
559 && !just_print_flag && !question_flag)
560 error (NILF, "Target `%s' not remade because of errors.", file->name);
562 return dep_status;
565 if (file->command_state == cs_deps_running)
566 /* The commands for some deps were running on the last iteration, but
567 they have finished now. Reset the command_state to not_started to
568 simplify later bookkeeping. It is important that we do this only
569 when the prior state was cs_deps_running, because that prior state
570 was definitely propagated to FILE's also_make's by set_command_state
571 (called above), but in another state an also_make may have
572 independently changed to finished state, and we would confuse that
573 file's bookkeeping (updated, but not_started is bogus state). */
574 set_command_state (file, cs_not_started);
576 /* Now record which dependencies are more
577 recent than this file, so we can define $?. */
579 deps_changed = 0;
580 for (d = file->deps; d != 0; d = d->next)
582 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
583 check_renamed (d->file);
585 #if 1 /* %%% In version 4, remove this code completely to
586 implement not remaking deps if their deps are newer
587 than their parents. */
588 if (d_mtime == (FILE_TIMESTAMP) -1 && !d->file->intermediate)
589 /* We must remake if this dep does not
590 exist and is not intermediate. */
591 must_make = 1;
592 #endif
594 /* Set DEPS_CHANGED if this dep actually changed. */
595 deps_changed |= d->changed;
597 /* Set D->changed if either this dep actually changed,
598 or its dependent, FILE, is older or does not exist. */
599 d->changed |= noexist || d_mtime > this_mtime;
601 if (debug_flag && !noexist)
603 print_spaces (depth);
604 if (d_mtime == (FILE_TIMESTAMP) -1)
605 printf ("Dependency `%s' does not exist.\n", dep_name (d));
606 else
607 printf ("Dependency `%s' is %s than dependent `%s'.\n",
608 dep_name (d), d->changed ? "newer" : "older", file->name);
609 fflush (stdout);
613 /* Here depth returns to the value it had when we were called. */
614 depth--;
616 if (file->double_colon && file->deps == 0)
618 must_make = 1;
619 DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
621 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
623 must_make = 0;
624 DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
627 if (!must_make)
629 if (debug_flag)
631 print_spaces(depth);
632 printf("No need to remake target `%s'", file->name);
633 if (!streq(file->name, file->hname))
634 printf("; using VPATH name `%s'", file->hname);
635 printf(".\n");
636 fflush(stdout);
639 notice_finished_file (file);
641 /* Since we don't need to remake the file, convert it to use the
642 VPATH filename if we found one. hfile will be either the
643 local name if no VPATH or the VPATH name if one was found. */
645 while (file)
647 file->name = file->hname;
648 file = file->prev;
651 return 0;
654 DEBUGPR ("Must remake target `%s'.\n");
656 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
657 VPATH. */
658 if (!streq(file->name, file->hname))
660 if (debug_flag)
662 print_spaces (depth);
663 printf(" Ignoring VPATH name `%s'.\n", file->hname);
664 fflush(stdout);
666 file->ignore_vpath = 1;
669 /* Now, take appropriate actions to remake the file. */
670 remake_file (file);
672 if (file->command_state != cs_finished)
674 DEBUGPR ("Commands of `%s' are being run.\n");
675 return 0;
678 switch (file->update_status)
680 case 2:
681 DEBUGPR ("Failed to remake target file `%s'.\n");
682 break;
683 case 0:
684 DEBUGPR ("Successfully remade target file `%s'.\n");
685 break;
686 case 1:
687 DEBUGPR ("Target file `%s' needs remade under -q.\n");
688 break;
689 default:
690 assert (file->update_status >= 0 && file->update_status <= 2);
691 break;
694 file->updated = 1;
695 return file->update_status;
698 /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
699 files listed in its `also_make' member. Under -t, this function also
700 touches FILE.
702 On return, FILE->update_status will no longer be -1 if it was. */
704 void
705 notice_finished_file (file)
706 register struct file *file;
708 struct dep *d;
709 int ran = file->command_state == cs_running;
711 file->command_state = cs_finished;
712 file->updated = 1;
714 if (touch_flag
715 /* The update status will be:
716 -1 if this target was not remade;
717 0 if 0 or more commands (+ or ${MAKE}) were run and won;
718 1 if some commands were run and lost.
719 We touch the target if it has commands which either were not run
720 or won when they ran (i.e. status is 0). */
721 && file->update_status == 0)
723 if (file->cmds != 0 && file->cmds->any_recurse)
725 /* If all the command lines were recursive,
726 we don't want to do the touching. */
727 unsigned int i;
728 for (i = 0; i < file->cmds->ncommand_lines; ++i)
729 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
730 goto have_nonrecursing;
732 else
734 have_nonrecursing:
735 if (file->phony)
736 file->update_status = 0;
737 else
738 /* Should set file's modification date and do nothing else. */
739 file->update_status = touch_file (file);
743 if (ran && !file->phony)
745 struct file *f;
747 assert(file->mtime_before_update == 0);
748 file->mtime_before_update = file->last_mtime;
750 if (just_print_flag || question_flag
751 || (file->is_target && file->cmds == 0))
752 file->last_mtime = NEW_MTIME;
753 else
754 file->last_mtime = 0;
756 /* Propagate the change of modification time to all the double-colon
757 entries for this file. */
758 for (f = file->double_colon; f != 0; f = f->next)
759 f->last_mtime = file->last_mtime;
762 if (ran && file->update_status != -1)
763 /* We actually tried to update FILE, which has
764 updated its also_make's as well (if it worked).
765 If it didn't work, it wouldn't work again for them.
766 So mark them as updated with the same status. */
767 for (d = file->also_make; d != 0; d = d->next)
769 d->file->command_state = cs_finished;
770 d->file->updated = 1;
771 d->file->update_status = file->update_status;
773 if (ran && !d->file->phony)
774 /* Fetch the new modification time.
775 We do this instead of just invalidating the cached time
776 so that a vpath_search can happen. Otherwise, it would
777 never be done because the target is already updated. */
778 (void) f_mtime (d->file, 0);
780 else if (file->update_status == -1)
781 /* Nothing was done for FILE, but it needed nothing done.
782 So mark it now as "succeeded". */
783 file->update_status = 0;
786 /* Check whether another file (whose mtime is THIS_MTIME)
787 needs updating on account of a dependency which is file FILE.
788 If it does, store 1 in *MUST_MAKE_PTR.
789 In the process, update any non-intermediate files
790 that FILE depends on (including FILE itself).
791 Return nonzero if any updating failed. */
793 static int
794 check_dep (file, depth, this_mtime, must_make_ptr)
795 struct file *file;
796 unsigned int depth;
797 FILE_TIMESTAMP this_mtime;
798 int *must_make_ptr;
800 register struct dep *d;
801 int dep_status = 0;
803 ++depth;
804 file->updating = 1;
806 if (!file->intermediate)
807 /* If this is a non-intermediate file, update it and record
808 whether it is newer than THIS_MTIME. */
810 FILE_TIMESTAMP mtime;
811 dep_status = update_file (file, depth);
812 check_renamed (file);
813 mtime = file_mtime (file);
814 check_renamed (file);
815 if (mtime == (FILE_TIMESTAMP) -1 || mtime > this_mtime)
816 *must_make_ptr = 1;
818 else
820 /* FILE is an intermediate file. */
821 FILE_TIMESTAMP mtime;
823 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
825 if (try_implicit_rule (file, depth))
826 DEBUGPR ("Found an implicit rule for `%s'.\n");
827 else
828 DEBUGPR ("No implicit rule found for `%s'.\n");
829 file->tried_implicit = 1;
831 if (file->cmds == 0 && !file->is_target
832 && default_file != 0 && default_file->cmds != 0)
834 DEBUGPR ("Using default commands for `%s'.\n");
835 file->cmds = default_file->cmds;
838 /* If the intermediate file actually exists
839 and is newer, then we should remake from it. */
840 check_renamed (file);
841 mtime = file_mtime (file);
842 check_renamed (file);
843 if (mtime != (FILE_TIMESTAMP) -1 && mtime > this_mtime)
844 *must_make_ptr = 1;
845 /* Otherwise, update all non-intermediate files we depend on,
846 if necessary, and see whether any of them is more
847 recent than the file on whose behalf we are checking. */
848 else
850 register struct dep *lastd;
852 lastd = 0;
853 d = file->deps;
854 while (d != 0)
856 if (d->file->updating)
858 error (NILF, "Circular %s <- %s dependency dropped.",
859 file->name, d->file->name);
860 if (lastd == 0)
862 file->deps = d->next;
863 free ((char *) d);
864 d = file->deps;
866 else
868 lastd->next = d->next;
869 free ((char *) d);
870 d = lastd->next;
872 continue;
875 d->file->parent = file;
876 dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
877 check_renamed (d->file);
878 if (dep_status != 0 && !keep_going_flag)
879 break;
881 if (d->file->command_state == cs_running
882 || d->file->command_state == cs_deps_running)
883 /* Record that some of FILE's deps are still being made.
884 This tells the upper levels to wait on processing it until
885 the commands are finished. */
886 set_command_state (file, cs_deps_running);
888 lastd = d;
889 d = d->next;
894 file->updating = 0;
895 return dep_status;
898 /* Touch FILE. Return zero if successful, one if not. */
900 #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
902 static int
903 touch_file (file)
904 register struct file *file;
906 if (!silent_flag)
907 message (0, "touch %s", file->name);
909 #ifndef NO_ARCHIVES
910 if (ar_name (file->name))
911 return ar_touch (file->name);
912 else
913 #endif
915 int fd = open (file->name, O_RDWR | O_CREAT, 0666);
917 if (fd < 0)
918 TOUCH_ERROR ("touch: open: ");
919 else
921 struct stat statbuf;
922 char buf;
923 int status;
925 #ifdef EINTR
927 #endif
928 status = fstat (fd, &statbuf);
929 #ifdef EINTR
930 while (status < 0 && errno == EINTR);
931 #endif
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 if (!touch_flag || file->cmds->any_recurse)
983 execute_file_commands (file);
984 return;
986 else
987 /* This tells notice_finished_file it is ok to touch the file. */
988 file->update_status = 0;
991 /* This does the touching under -t. */
992 notice_finished_file (file);
995 /* Return the mtime of a file, given a `struct file'.
996 Caches the time in the struct file to avoid excess stat calls.
998 If the file is not found, and SEARCH is nonzero, VPATH searching and
999 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1000 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1001 FILE. */
1003 FILE_TIMESTAMP
1004 f_mtime (file, search)
1005 register struct file *file;
1006 int search;
1008 FILE_TIMESTAMP mtime;
1010 /* File's mtime is not known; must get it from the system. */
1012 #ifndef NO_ARCHIVES
1013 if (ar_name (file->name))
1015 /* This file is an archive-member reference. */
1017 char *arname, *memname;
1018 struct file *arfile;
1019 int arname_used = 0;
1021 /* Find the archive's name. */
1022 ar_parse_name (file->name, &arname, &memname);
1024 /* Find the modification time of the archive itself.
1025 Also allow for its name to be changed via VPATH search. */
1026 arfile = lookup_file (arname);
1027 if (arfile == 0)
1029 arfile = enter_file (arname);
1030 arname_used = 1;
1032 mtime = f_mtime (arfile, search);
1033 check_renamed (arfile);
1034 if (search && strcmp (arfile->hname, arname))
1036 /* The archive's name has changed.
1037 Change the archive-member reference accordingly. */
1039 char *name;
1040 unsigned int arlen, memlen;
1042 if (!arname_used)
1044 free (arname);
1045 arname_used = 1;
1048 arname = arfile->hname;
1049 arlen = strlen (arname);
1050 memlen = strlen (memname);
1052 /* free (file->name); */
1054 name = (char *) xmalloc (arlen + 1 + memlen + 2);
1055 bcopy (arname, name, arlen);
1056 name[arlen] = '(';
1057 bcopy (memname, name + arlen + 1, memlen);
1058 name[arlen + 1 + memlen] = ')';
1059 name[arlen + 1 + memlen + 1] = '\0';
1061 /* If the archive was found with GPATH, make the change permanent;
1062 otherwise defer it until later. */
1063 if (arfile->name == arfile->hname)
1064 rename_file (file, name);
1065 else
1066 rehash_file (file, name);
1067 check_renamed (file);
1070 if (!arname_used)
1071 free (arname);
1072 free (memname);
1074 if (mtime == (FILE_TIMESTAMP) -1)
1075 /* The archive doesn't exist, so it's members don't exist either. */
1076 return (FILE_TIMESTAMP) -1;
1078 mtime = ar_member_date (file->hname);
1080 else
1081 #endif
1083 mtime = name_mtime (file->name);
1085 if (mtime == (FILE_TIMESTAMP) -1 && search && !file->ignore_vpath)
1087 /* If name_mtime failed, search VPATH. */
1088 char *name = file->name;
1089 if (vpath_search (&name, &mtime)
1090 /* Last resort, is it a library (-lxxx)? */
1091 || (name[0] == '-' && name[1] == 'l'
1092 && library_search (&name, &mtime)))
1094 if (mtime != 0)
1095 /* vpath_search and library_search store zero in MTIME
1096 if they didn't need to do a stat call for their work. */
1097 file->last_mtime = mtime;
1099 /* If we found it in VPATH, see if it's in GPATH too; if so,
1100 change the name right now; if not, defer until after the
1101 dependencies are updated. */
1102 if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
1104 rename_file (file, name);
1105 check_renamed (file);
1106 return file_mtime (file);
1109 rehash_file (file, name);
1110 check_renamed (file);
1111 mtime = name_mtime (name);
1117 /* Files can have bogus timestamps that nothing newly made will be
1118 "newer" than. Updating their dependents could just result in loops.
1119 So notify the user of the anomaly with a warning.
1121 We only need to do this once, for now. */
1123 static FILE_TIMESTAMP now = 0;
1124 if (!clock_skew_detected
1125 && mtime != (FILE_TIMESTAMP)-1 && mtime > now
1126 && !file->updated)
1128 /* This file's time appears to be in the future.
1129 Update our concept of the present, and compare again. */
1131 now = file_timestamp_now ();
1133 #ifdef WINDOWS32
1135 * FAT filesystems round time to nearest even second(!). Just
1136 * allow for any file (NTFS or FAT) to perhaps suffer from this
1137 * braindamage.
1139 if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
1140 #else
1141 #ifdef __MSDOS__
1142 /* Scrupulous testing indicates that some Windows
1143 filesystems can set file times up to 3 sec into the future! */
1144 if (mtime > now + 3)
1145 #else
1146 if (mtime > now)
1147 #endif
1148 #endif
1150 char mtimebuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1151 char nowbuf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1153 file_timestamp_sprintf (mtimebuf, mtime);
1154 file_timestamp_sprintf (nowbuf, now);
1155 error (NILF, "*** Warning: File `%s' has modification time in the future (%s > %s)",
1156 file->name, mtimebuf, nowbuf);
1157 clock_skew_detected = 1;
1162 /* Store the mtime into all the entries for this file. */
1163 if (file->double_colon)
1164 file = file->double_colon;
1168 /* If this file is not implicit but it is intermediate then it was
1169 made so by the .INTERMEDIATE target. If this file has never
1170 been built by us but was found now, it existed before make
1171 started. So, turn off the intermediate bit so make doesn't
1172 delete it, since it didn't create it. */
1173 if (mtime != (FILE_TIMESTAMP)-1 && file->command_state == cs_not_started
1174 && !file->tried_implicit && file->intermediate)
1175 file->intermediate = 0;
1177 file->last_mtime = mtime;
1178 file = file->prev;
1180 while (file != 0);
1182 return mtime;
1186 /* Return the mtime of the file or archive-member reference NAME. */
1188 static FILE_TIMESTAMP
1189 name_mtime (name)
1190 register char *name;
1192 struct stat st;
1194 if (stat (name, &st) < 0)
1195 return (FILE_TIMESTAMP) -1;
1197 return FILE_TIMESTAMP_STAT_MODTIME (st);
1201 /* Search for a library file specified as -lLIBNAME, searching for a
1202 suitable library file in the system library directories and the VPATH
1203 directories. */
1205 static int
1206 library_search (lib, mtime_ptr)
1207 char **lib;
1208 FILE_TIMESTAMP *mtime_ptr;
1210 static char *dirs[] =
1212 #ifndef _AMIGA
1213 "/lib",
1214 "/usr/lib",
1215 #endif
1216 #if defined(WINDOWS32) && !defined(LIBDIR)
1218 * This is completely up to the user at product install time. Just define
1219 * a placeholder.
1221 #define LIBDIR "."
1222 #endif
1223 LIBDIR, /* Defined by configuration. */
1227 static char *libpatterns = NULL;
1229 char *libname = &(*lib)[2]; /* Name without the `-l'. */
1230 FILE_TIMESTAMP mtime;
1232 /* Loop variables for the libpatterns value. */
1233 char *p, *p2;
1234 int len;
1236 char *file, **dp;
1238 /* If we don't have libpatterns, get it. */
1239 if (!libpatterns)
1241 int save = warn_undefined_variables_flag;
1242 warn_undefined_variables_flag = 0;
1244 libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
1246 warn_undefined_variables_flag = save;
1249 /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
1250 p2 = libpatterns;
1251 while ((p = find_next_token (&p2, &len)) != 0)
1253 static char *buf = NULL;
1254 static int buflen = 0;
1255 static int libdir_maxlen = -1;
1256 char *libbuf = variable_expand ("");
1258 /* Expand the pattern using LIBNAME as a replacement. */
1260 char c = p[len];
1261 char *p3, *p4;
1263 p[len] = '\0';
1264 p3 = find_percent (p);
1265 if (!p3)
1267 /* Give a warning if there is no pattern, then remove the
1268 pattern so it's ignored next time. */
1269 error (NILF, ".LIBPATTERNS element `%s' is not a pattern", p);
1270 for (; len; --len, ++p)
1271 *p = ' ';
1272 *p = c;
1273 continue;
1275 p4 = variable_buffer_output (libbuf, p, p3-p);
1276 p4 = variable_buffer_output (p4, libname, strlen (libname));
1277 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1278 p[len] = c;
1281 /* Look first for `libNAME.a' in the current directory. */
1282 mtime = name_mtime (libbuf);
1283 if (mtime != (FILE_TIMESTAMP) -1)
1285 *lib = xstrdup (libbuf);
1286 if (mtime_ptr != 0)
1287 *mtime_ptr = mtime;
1288 return 1;
1291 /* Now try VPATH search on that. */
1293 file = libbuf;
1294 if (vpath_search (&file, mtime_ptr))
1296 *lib = file;
1297 return 1;
1300 /* Now try the standard set of directories. */
1302 if (!buflen)
1304 for (dp = dirs; *dp != 0; ++dp)
1306 int l = strlen (*dp);
1307 if (l > libdir_maxlen)
1308 libdir_maxlen = l;
1310 buflen = strlen (libbuf);
1311 buf = xmalloc(libdir_maxlen + buflen + 2);
1313 else if (buflen < strlen (libbuf))
1315 buflen = strlen (libbuf);
1316 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1319 for (dp = dirs; *dp != 0; ++dp)
1321 sprintf (buf, "%s/%s", *dp, libbuf);
1322 mtime = name_mtime (buf);
1323 if (mtime != (FILE_TIMESTAMP) -1)
1325 *lib = xstrdup (buf);
1326 if (mtime_ptr != 0)
1327 *mtime_ptr = mtime;
1328 return 1;
1333 return 0;