Debian Codegraph @

src/remake.c

raw symbols SCIP
1 /* Basic dependency engine for GNU Make.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "makeint.h"
18 #include "globals.h"
19 #include "profile.h"
20 // debugger include(s)
21 #include "cmd.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "dep.h"
26 #include "variable.h"
27 #include "debug.h"
28
29 #include <assert.h>
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #else
34 #include <sys/file.h>
35 #endif
36
37 /* The test for circular dependencies is based on the 'updating' bit in
38 'struct file'. However, double colon targets have separate 'struct
39 file's; make sure we always use the base of the double colon chain. */
40
41 #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
42 ->updating = 1)
43 #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
44 ->updating = 0)
45 #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
46 ->updating)
47
48
49 /* Incremented when a command is started (under -n, when one would be). */
50 unsigned int commands_started = 0;
51
52 /* Set to the goal dependency. Mostly needed for remaking makefiles. */
53 static struct goaldep *goal_list;
54 static struct dep *goal_dep;
55
56 /* Current value for pruning the scan of the goal chain.
57 All files start with considered == 0. */
58 static unsigned int considered = 0;
59
60 static enum update_status update_file (struct file *file, unsigned int depth,
61 target_stack_node_t *p_call_stack);
62 static enum update_status update_file_1 (struct file *file, unsigned int depth,
63 target_stack_node_t *p_call_stack);
64
65 static enum update_status check_dep (struct file *file, unsigned int depth,
66 FILE_TIMESTAMP this_mtime, int *must_make_ptr,
67 target_stack_node_t *p_call_stack);
68 static enum update_status touch_file (struct file *file);
69 static void remake_file (struct file *file,
70 target_stack_node_t *p_call_stack);
71 static FILE_TIMESTAMP name_mtime (const char *name);
72 static const char *library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr);
73
74
75 /* Remake all the goals in the 'struct dep' chain GOALS. Return -1 if nothing
76 was done, 0 if all goals were updated successfully, or 1 if a goal failed.
77
78 If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
79 and -n should be disabled for them unless they were also command-line
80 targets, and we should only make one goal at a time and return as soon as
81 one goal whose 'changed' member is nonzero is successfully made. */
82
83 enum update_status
84 update_goal_chain (struct goaldep *goaldeps)
85 {
86 int t = touch_flag, q = question_flag, n = just_print_flag;
87 enum update_status status = us_none;
88
89 /* Duplicate the chain so we can remove things from it. */
90
91 struct dep *goals = copy_dep_chain ((struct dep *)goaldeps);
92
93 goal_list = rebuilding_makefiles ? goaldeps : NULL;
94
95 #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
96 : file_mtime (file))
97
98 /* Start a fresh batch of consideration. */
99 ++considered;
100
101 /* Update all the goals until they are all finished. */
102
103 while (goals != 0)
104 {
105 struct dep *g, *lastgoal;
106
107 /* Start jobs that are waiting for the load to go down. */
108
109 start_waiting_jobs (NULL);
110
111 /* Wait for a child to die. */
112
113 reap_children (1, 0, NULL);
114
115 lastgoal = 0;
116 g = goals;
117 while (g != 0)
118 {
119 /* Iterate over all double-colon entries for this file. */
120 struct file *file;
121 int stop = 0, any_not_updated = 0;
122
123 goal_dep = g;
124
125 for (file = g->file->double_colon ? g->file->double_colon : g->file;
126 file != NULL;
127 file = file->prev)
128 {
129 unsigned int ocommands_started;
130 enum update_status fail;
131
132 file->dontcare = ANY_SET (g->flags, RM_DONTCARE);
133
134 check_renamed (file);
135 if (rebuilding_makefiles)
136 {
137 if (file->cmd_target)
138 {
139 touch_flag = t;
140 question_flag = q;
141 just_print_flag = n;
142 }
143 else
144 touch_flag = question_flag = just_print_flag = 0;
145 }
146
147 /* Save the old value of 'commands_started' so we can compare
148 later. It will be incremented when any commands are
149 actually run. */
150 ocommands_started = commands_started;
151
152 fail = update_file (file, rebuilding_makefiles ? 1 : 0, NULL);
153 check_renamed (file);
154
155 /* Set the goal's 'changed' flag if any commands were started
156 by calling update_file above. We check this flag below to
157 decide when to give an "up to date" diagnostic. */
158 if (commands_started > ocommands_started)
159 g->changed = 1;
160
161 stop = 0;
162 if ((fail || file->updated) && status < us_question)
163 {
164 /* We updated this goal. Update STATUS and decide whether
165 to stop. */
166 if (file->update_status)
167 {
168 /* Updating failed, or -q triggered. The STATUS value
169 tells our caller which. */
170 status = file->update_status;
171 /* If -q just triggered, stop immediately. It doesn't
172 matter how much more we run, since we already know
173 the answer to return. */
174 stop = (question_flag && !keep_going_flag
175 && !rebuilding_makefiles);
176 }
177 else
178 {
179 FILE_TIMESTAMP mtime = MTIME (file);
180 check_renamed (file);
181
182 if (file->updated && g->changed &&
183 mtime != file->mtime_before_update)
184 {
185 /* Updating was done. If this is a makefile and
186 just_print_flag or question_flag is set (meaning
187 -n or -q was given and this file was specified
188 as a command-line target), don't change STATUS.
189 If STATUS is changed, we will get re-exec'd, and
190 enter an infinite loop. */
191 if (!rebuilding_makefiles
192 || (!just_print_flag && !question_flag))
193 status = us_success;
194 if (rebuilding_makefiles && file->dontcare)
195 /* This is a default makefile; stop remaking. */
196 stop = 1;
197 }
198 }
199 }
200
201 /* Keep track if any double-colon entry is not finished.
202 When they are all finished, the goal is finished. */
203 any_not_updated |= !file->updated;
204
205 file->dontcare = 0;
206
207 if (stop)
208 break;
209 }
210
211 /* Reset FILE since it is null at the end of the loop. */
212 file = g->file;
213
214 if (stop || !any_not_updated)
215 {
216 /* If we have found nothing whatever to do for the goal,
217 print a message saying nothing needs doing. */
218
219 if (!rebuilding_makefiles
220 /* If the update_status is success, we updated successfully
221 or not at all. G->changed will have been set above if
222 any commands were actually started for this goal. */
223 && file->update_status == us_success && !g->changed
224 /* Never give a message under -s or -q. */
225 && !run_silent && !question_flag) {
226 OS (message, 1, ((file->phony || file->cmds == 0)
227 ? _("Nothing to be done for '%s'.")
228 : _("'%s' is up to date.")),
229 file->name);
230 if ( debugger_enabled && b_debugger_goal )
231 enter_debugger(NULL, file, -2, DEBUG_GOAL_UPDATED_HIT);
232 }
233
234 /* This goal is finished. Remove it from the chain. */
235 if (lastgoal == 0)
236 goals = g->next;
237 else
238 lastgoal->next = g->next;
239
240 /* Free the storage. */
241 free (g);
242
243 g = lastgoal == 0 ? goals : lastgoal->next;
244
245 if (stop)
246 break;
247 }
248 else
249 {
250 lastgoal = g;
251 g = g->next;
252 }
253 }
254
255 /* If we reached the end of the dependency graph update CONSIDERED
256 for the next pass. */
257 if (g == 0)
258 ++considered;
259 }
260
261 if (rebuilding_makefiles)
262 {
263 touch_flag = t;
264 question_flag = q;
265 just_print_flag = n;
266 }
267
268 return status;
269 }
270
271 /* If we're rebuilding an included makefile that failed, and we care
272 about errors, show an error message the first time. */
273
274 void
275 show_goal_error (void)
276 {
277 struct goaldep *goal;
278
279 if ((goal_dep->flags & (RM_INCLUDED|RM_DONTCARE)) != RM_INCLUDED)
280 return;
281
282 for (goal = goal_list; goal; goal = goal->next)
283 if (goal_dep->file == goal->file)
284 {
285 if (goal->error)
286 {
287 OSS (error, &goal->floc, "%s: %s",
288 goal->file->name, strerror ((int)goal->error));
289 goal->error = 0;
290 }
291 return;
292 }
293 }
294
295 /* If FILE is not up to date, execute the commands for it.
296 Return 0 if successful, non-0 if unsuccessful;
297 but with some flag settings, just call 'exit' if unsuccessful.
298
299 DEPTH is the depth in recursions of this function.
300 We increment it during the consideration of our dependencies,
301 then decrement it again after finding out whether this file
302 is out of date.
303
304 If there are multiple double-colon entries for FILE,
305 each is considered in turn. */
306
307 static enum update_status
308 update_file (struct file *file, unsigned int depth,
309 target_stack_node_t *p_call_stack)
310 {
311 enum update_status status = us_success;
312 struct file *f;
313
314 f = file->double_colon ? file->double_colon : file;
315
316 /* Prune the dependency graph: if we've already been here on _this_
317 pass through the dependency graph, we don't have to go any further.
318 We won't reap_children until we start the next pass, so no state
319 change is possible below here until then. */
320 if (f->considered == considered)
321 {
322 /* Check for the case where a target has been tried and failed but
323 the diagnostics haven't been issued. If we need the diagnostics
324 then we will have to continue. */
325 if (!(f->updated && f->update_status > us_none
326 && !f->dontcare && f->no_diag))
327 {
328 DBF (DB_VERBOSE, _("Pruning file '%s'.\n"));
329 return f->command_state == cs_finished ? f->update_status : us_success;
330 }
331 }
332
333 /* This loop runs until we start commands for a double colon rule, or until
334 the chain is exhausted. */
335 for (; f != 0; f = f->prev)
336 {
337 enum update_status new;
338
339 f->considered = considered;
340
341 new = update_file_1 (f, depth, p_call_stack);
342 check_renamed (f);
343
344 /* Clean up any alloca() used during the update. */
345 alloca (0);
346
347 /* If we got an error, don't bother with double_colon etc. */
348 if (new && !keep_going_flag)
349 return new;
350
351 if (f->command_state == cs_running
352 || f->command_state == cs_deps_running)
353 /* Don't run other :: rules for this target until
354 this rule is finished. */
355 return us_success;
356
357 if (new > status)
358 status = new;
359 }
360
361 return status;
362 }
363
364 /* Show a message stating the target failed to build. */
365
366 static void
367 complain (struct file *file)
368 {
369 /* If this file has no_diag set then it means we tried to update it
370 before in the dontcare mode and failed. The target that actually
371 failed is not necessarily this file but could be one of its direct
372 or indirect dependencies. So traverse this file's dependencies and
373 find the one that actually caused the failure. */
374
375 struct dep *d;
376
377 for (d = file->deps; d != 0; d = d->next)
378 {
379 if (d->file->updated && d->file->update_status > us_none && file->no_diag)
380 {
381 complain (d->file);
382 break;
383 }
384 }
385
386 if (d == 0)
387 {
388 show_goal_error ();
389
390 /* Didn't find any dependencies to complain about. */
391 if (file->parent)
392 {
393 size_t l = strlen (file->name) + strlen (file->parent->name) + 4;
394 const char *m = _("%sNo rule to make target '%s', needed by '%s'%s");
395
396 if (!keep_going_flag)
397 fatal (NILF, l, m, "", file->name, file->parent->name, "");
398
399 error (NILF, l, m, "*** ", file->name, file->parent->name, ".");
400 }
401 else
402 {
403 size_t l = strlen (file->name) + 4;
404 const char *m = _("%sNo rule to make target '%s'%s");
405
406 if (!keep_going_flag)
407 fatal (NILF, l, m, "", file->name, "");
408
409 error (NILF, l, m, "*** ", file->name, ".");
410 }
411
412 file->no_diag = 0;
413 }
414 }
415
416 /* Consider a single 'struct file' and update it as appropriate.
417 Return 0 on success, or non-0 on failure. */
418
419 static enum update_status
420 update_file_1 (struct file *file, unsigned int depth,
421 target_stack_node_t *p_call_stack)
422 {
423 enum update_status dep_status = us_success;
424 FILE_TIMESTAMP this_mtime;
425 int noexist, must_make, deps_changed;
426 struct file *ofile;
427 struct dep *d, *ad;
428 struct dep amake;
429 int running = 0;
430
431 DBF (DB_VERBOSE, _("Considering target file '%s'.\n"));
432 p_stack_top = p_call_stack = trace_push_target(p_call_stack, file);
433
434 /* We don't want to step into file dependencies when there are
435 no associated commands. There or too often too many of them.
436 */
437 if ( (i_debugger_stepping && file->cmds) ||
438 (file->tracing & BRK_BEFORE_PREREQ) )
439 enter_debugger(p_call_stack, file, 0, DEBUG_BRKPT_BEFORE_PREREQ);
440
441 if (file->updated)
442 {
443 if (file->update_status > us_none)
444 {
445 DBF (DB_VERBOSE,
446 _("Recently tried and failed to update file '%s'.\n"));
447
448 /* If the file we tried to make is marked no_diag then no message
449 was printed about it when it failed during the makefile rebuild.
450 If we're trying to build it again in the normal rebuild, print a
451 message now. */
452 if (file->no_diag && !file->dontcare)
453 complain (file);
454
455 return file->update_status;
456 }
457
458 DBF (DB_VERBOSE, _("File '%s' was considered already.\n"));
459 trace_pop_target(p_call_stack);
460 return 0;
461 }
462
463 switch (file->command_state)
464 {
465 case cs_not_started:
466 case cs_deps_running:
467 break;
468 case cs_running:
469 DBF (DB_VERBOSE, _("Still updating file '%s'.\n"));
470 trace_pop_target(p_call_stack);
471 return 0;
472 case cs_finished:
473 DBF (DB_VERBOSE, _("Finished updating file '%s'.\n"));
474 trace_pop_target(p_call_stack);
475 return file->update_status;
476 default:
477 abort ();
478 }
479
480 /* Determine whether the diagnostics will be issued should this update
481 fail. */
482 file->no_diag = file->dontcare;
483
484 ++depth;
485
486 /* Notice recursive update of the same file. */
487 start_updating (file);
488
489 /* We might change file if we find a different one via vpath;
490 remember this one to turn off updating. */
491 ofile = file;
492
493 /* Looking at the file's modtime beforehand allows the possibility
494 that its name may be changed by a VPATH search, and thus it may
495 not need an implicit rule. If this were not done, the file
496 might get implicit commands that apply to its initial name, only
497 to have that name replaced with another found by VPATH search. */
498
499 this_mtime = file_mtime (file);
500 check_renamed (file);
501 noexist = this_mtime == NONEXISTENT_MTIME;
502 if (noexist) {
503 DBF (DB_BASIC, _("File '%s' does not exist.\n"));
504 if (i_debugger_nexting && file->cmds) {
505 enter_debugger(p_call_stack, file, 0, DEBUG_STEP_HIT);
506 }
507 } else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
508 && file->low_resolution_time)
509 {
510 /* Avoid spurious rebuilds due to low resolution time stamps. */
511 int ns = FILE_TIMESTAMP_NS (this_mtime);
512 if (ns != 0)
513 OS (error, NILF,
514 _("*** Warning: .LOW_RESOLUTION_TIME file '%s' has a high resolution time stamp"),
515 file->name);
516 this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
517 }
518
519 must_make = noexist;
520
521 /* If file was specified as a target with no commands,
522 come up with some default commands. */
523
524 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
525 {
526 if (try_implicit_rule (file, depth))
527 DBF (DB_IMPLICIT, _("Found an implicit rule for '%s'.\n"));
528 else
529 DBF (DB_IMPLICIT, _("No implicit rule found for '%s'.\n"));
530 file->tried_implicit = 1;
531 }
532 if (file->cmds == 0 && !file->is_target
533 && default_file != 0 && default_file->cmds != 0)
534 {
535 DBF (DB_IMPLICIT, _("Using default recipe for '%s'.\n"));
536 file->cmds = default_file->cmds;
537 }
538
539 /* Update all non-intermediate files we depend on, if necessary, and see
540 whether any of them is more recent than this file. We need to walk our
541 deps, AND the deps of any also_make targets to ensure everything happens
542 in the correct order. */
543
544 amake.file = file;
545 amake.next = file->also_make;
546 ad = &amake;
547 while (ad)
548 {
549 struct dep *lastd = 0;
550
551 /* Find the deps we're scanning */
552 d = ad->file->deps;
553 ad = ad->next;
554
555 while (d)
556 {
557 enum update_status new;
558 FILE_TIMESTAMP mtime;
559 int maybe_make;
560 int dontcare = 0;
561
562 check_renamed (d->file);
563
564 mtime = file_mtime (d->file);
565 check_renamed (d->file);
566
567 if (is_updating (d->file))
568 {
569 OSS (error, NILF, _("Circular %s <- %s dependency dropped."),
570 file->name, d->file->name);
571 /* We cannot free D here because our the caller will still have
572 a reference to it when we were called recursively via
573 check_dep below. */
574 if (lastd == 0)
575 file->deps = d->next;
576 else
577 lastd->next = d->next;
578 d = d->next;
579 continue;
580 }
581
582 d->file->parent = file;
583 maybe_make = must_make;
584
585 /* Inherit dontcare flag from our parent. */
586 if (rebuilding_makefiles)
587 {
588 dontcare = d->file->dontcare;
589 d->file->dontcare = file->dontcare;
590 }
591
592 new = check_dep (d->file, depth, this_mtime, &maybe_make,
593 p_call_stack);
594 if (new > dep_status)
595 dep_status = new;
596
597 /* Restore original dontcare flag. */
598 if (rebuilding_makefiles)
599 d->file->dontcare = dontcare;
600
601 if (! d->ignore_mtime)
602 must_make = maybe_make;
603
604 check_renamed (d->file);
605
606 {
607 struct file *f = d->file;
608 if (f->double_colon)
609 f = f->double_colon;
610 do
611 {
612 running |= (f->command_state == cs_running
613 || f->command_state == cs_deps_running);
614 f = f->prev;
615 }
616 while (f != 0);
617 }
618
619 if (dep_status && !keep_going_flag)
620 break;
621
622 if (!running)
623 /* The prereq is considered changed if the timestamp has changed
624 while it was built, OR it doesn't exist. */
625 d->changed = ((file_mtime (d->file) != mtime)
626 || (mtime == NONEXISTENT_MTIME));
627
628 lastd = d;
629 d = d->next;
630 }
631 }
632
633 /* Now we know whether this target needs updating.
634 If it does, update all the intermediate files we depend on. */
635
636 if (must_make || always_make_flag)
637 {
638 for (d = file->deps; d != 0; d = d->next)
639 if (d->file->intermediate)
640 {
641 enum update_status new;
642 int dontcare = 0;
643
644 FILE_TIMESTAMP mtime = file_mtime (d->file);
645 check_renamed (d->file);
646 d->file->parent = file;
647
648 /* Inherit dontcare flag from our parent. */
649 if (rebuilding_makefiles)
650 {
651 dontcare = d->file->dontcare;
652 d->file->dontcare = file->dontcare;
653 }
654
655 /* We may have already considered this file, when we didn't know
656 we'd need to update it. Force update_file() to consider it and
657 not prune it. */
658 d->file->considered = 0;
659
660 new = update_file (d->file, depth, p_call_stack);
661 if (new > dep_status)
662 dep_status = new;
663
664 /* Restore original dontcare flag. */
665 if (rebuilding_makefiles)
666 d->file->dontcare = dontcare;
667
668 check_renamed (d->file);
669
670 {
671 struct file *f = d->file;
672 if (f->double_colon)
673 f = f->double_colon;
674 do
675 {
676 running |= (f->command_state == cs_running
677 || f->command_state == cs_deps_running);
678 f = f->prev;
679 }
680 while (f != 0);
681 }
682
683 if (dep_status && !keep_going_flag)
684 break;
685
686 if (!running)
687 d->changed = ((file->phony && file->cmds != 0)
688 || file_mtime (d->file) != mtime);
689 }
690 }
691
692 finish_updating (file);
693 finish_updating (ofile);
694
695 DBF (DB_VERBOSE, _("Finished prerequisites of target file '%s'.\n"));
696
697 if ( file->tracing & BRK_AFTER_PREREQ )
698 enter_debugger(p_call_stack, file, 0, DEBUG_BRKPT_AFTER_PREREQ);
699
700 if (running)
701 {
702 set_command_state (file, cs_deps_running);
703 --depth;
704 DBF (DB_VERBOSE, _("The prerequisites of '%s' are being made.\n"));
705 trace_pop_target(p_call_stack);
706 return 0;
707 }
708
709 /* If any dependency failed, give up now. */
710
711 if (dep_status)
712 {
713 /* I'm not sure if we can't just assign dep_status... */
714 file->update_status = dep_status == us_none ? us_failed : dep_status;
715 notice_finished_file (file);
716
717 --depth;
718
719 DBF (DB_VERBOSE, _("Giving up on target file '%s'.\n"));
720
721 if (depth == 0 && keep_going_flag
722 && !just_print_flag && !question_flag)
723 OS (error, NILF,
724 _("Target '%s' not remade because of errors."), file->name);
725
726 trace_pop_target(p_call_stack);
727 return dep_status;
728 }
729
730 if (file->command_state == cs_deps_running)
731 /* The commands for some deps were running on the last iteration, but
732 they have finished now. Reset the command_state to not_started to
733 simplify later bookkeeping. It is important that we do this only
734 when the prior state was cs_deps_running, because that prior state
735 was definitely propagated to FILE's also_make's by set_command_state
736 (called above), but in another state an also_make may have
737 independently changed to finished state, and we would confuse that
738 file's bookkeeping (updated, but not_started is bogus state). */
739 set_command_state (file, cs_not_started);
740
741 /* Now record which prerequisites are more
742 recent than this file, so we can define $?. */
743
744 deps_changed = 0;
745 for (d = file->deps; d != 0; d = d->next)
746 {
747 FILE_TIMESTAMP d_mtime = file_mtime (d->file);
748 check_renamed (d->file);
749
750 if (! d->ignore_mtime)
751 {
752 #if 1
753 /* %%% In version 4, remove this code completely to
754 implement not remaking deps if their deps are newer
755 than their parents. */
756 if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
757 /* We must remake if this dep does not
758 exist and is not intermediate. */
759 must_make = 1;
760 #endif
761
762 /* Set DEPS_CHANGED if this dep actually changed. */
763 deps_changed |= d->changed;
764 }
765
766 /* Set D->changed if either this dep actually changed,
767 or its dependent, FILE, is older or does not exist. */
768 d->changed |= noexist || d_mtime > this_mtime;
769
770 if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
771 {
772 const char *fmt = 0;
773
774 if (d->ignore_mtime)
775 {
776 if (ISDB (DB_VERBOSE))
777 fmt = _("Prerequisite '%s' is order-only for target '%s'.\n");
778 }
779 else if (d_mtime == NONEXISTENT_MTIME)
780 {
781 if (ISDB (DB_BASIC))
782 fmt = _("Prerequisite '%s' of target '%s' does not exist.\n");
783 }
784 else if (d->changed)
785 {
786 if (ISDB (DB_BASIC))
787 fmt = _("Prerequisite '%s' is newer than target '%s'.\n");
788 }
789 else if (ISDB (DB_VERBOSE))
790 fmt = _("Prerequisite '%s' is older than target '%s'.\n");
791
792 if (fmt)
793 {
794 print_spaces (depth);
795 printf (fmt, dep_name (d), file->name);
796 fflush (stdout);
797 }
798 }
799 }
800
801 /* Here depth returns to the value it had when we were called. */
802 depth--;
803
804 if (file->double_colon && file->deps == 0)
805 {
806 must_make = 1;
807 DBF (DB_BASIC,
808 _("Target '%s' is double-colon and has no prerequisites.\n"));
809 }
810 else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
811 && !always_make_flag)
812 {
813 must_make = 0;
814 DBF (DB_VERBOSE,
815 _("No recipe for '%s' and no prerequisites actually changed.\n"));
816 }
817 else if (!must_make && file->cmds != 0 && always_make_flag)
818 {
819 must_make = 1;
820 DBF (DB_VERBOSE, _("Making '%s' due to always-make flag.\n"));
821 }
822
823 if (!must_make)
824 {
825 if (ISDB (DB_VERBOSE))
826 {
827 print_spaces (depth);
828 printf (_("No need to remake target '%s'"), file->name);
829 if (!streq (file->name, file->hname))
830 printf (_("; using VPATH name '%s'"), file->hname);
831 puts (".");
832 fflush (stdout);
833 }
834
835 notice_finished_file (file);
836 trace_pop_target(p_call_stack);
837
838 /* Since we don't need to remake the file, convert it to use the
839 VPATH filename if we found one. hfile will be either the
840 local name if no VPATH or the VPATH name if one was found. */
841
842 while (file)
843 {
844 file->name = file->hname;
845 file = file->prev;
846 }
847
848 return 0;
849 }
850
851 DBF (DB_BASIC, _("Must remake target '%s'.\n"));
852
853 /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
854 VPATH. */
855 if (!streq (file->name, file->hname))
856 {
857 DB (DB_BASIC, (_(" Ignoring VPATH name '%s'.\n"), file->hname));
858 file->ignore_vpath = 1;
859 }
860
861 /* Now, take appropriate actions to remake the file. */
862 remake_file (file, p_call_stack);
863
864 if (file->command_state != cs_finished)
865 {
866 DBF (DB_VERBOSE, _("Recipe of '%s' is being run.\n"));
867 if ( file->tracing & BRK_AFTER_CMD || i_debugger_stepping )
868 enter_debugger(p_call_stack, file, 0, DEBUG_BRKPT_AFTER_CMD);
869 trace_pop_target(p_call_stack);
870 return 0;
871 }
872
873 switch (file->update_status)
874 {
875 case us_failed:
876 DBF (DB_BASIC, _("Failed to remake target file '%s'.\n"));
877 break;
878 case us_success:
879 DBF (DB_BASIC, _("Successfully remade target file '%s'.\n"));
880 break;
881 case us_question:
882 DBF (DB_BASIC, _("Target file '%s' needs to be remade under -q.\n"));
883 break;
884 case us_none:
885 break;
886 }
887
888 file->updated = 1;
889 if ( file->tracing & BRK_AFTER_CMD || i_debugger_stepping )
890 enter_debugger(p_call_stack, file, 0, DEBUG_BRKPT_AFTER_CMD);
891
892 trace_pop_target(p_call_stack);
893 return file->update_status;
894 }
895
896 /* Set FILE's 'updated' flag and re-check its mtime and the mtime's of all
897 files listed in its 'also_make' member. Under -t, this function also
898 touches FILE.
899
900 On return, FILE->update_status will no longer be us_none if it was. */
901
902 void
903 notice_finished_file (struct file *file)
904 {
905 struct dep *d;
906 int ran = file->command_state == cs_running;
907 int touched = 0;
908
909 file->command_state = cs_finished;
910 file->updated = 1;
911
912 if (touch_flag
913 /* The update status will be:
914 us_success if 0 or more commands (+ or ${MAKE}) were run and won;
915 us_none if this target was not remade;
916 >us_none if some commands were run and lost.
917 We touch the target if it has commands which either were not run
918 or won when they ran (i.e. status is 0). */
919 && file->update_status == us_success)
920 {
921 if (file->cmds != 0 && file->cmds->any_recurse)
922 {
923 /* If all the command lines were recursive,
924 we don't want to do the touching. */
925 unsigned int i;
926 for (i = 0; i < file->cmds->ncommand_lines; ++i)
927 if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
928 goto have_nonrecursing;
929 }
930 else
931 {
932 have_nonrecursing:
933 if (file->phony)
934 file->update_status = us_success;
935 /* According to POSIX, -t doesn't affect targets with no cmds. */
936 else if (file->cmds != 0)
937 {
938 /* Should set file's modification date and do nothing else. */
939 file->update_status = touch_file (file);
940
941 /* Pretend we ran a real touch command, to suppress the
942 "'foo' is up to date" message. */
943 commands_started++;
944
945 /* Request for the timestamp to be updated (and distributed
946 to the double-colon entries). Simply setting ran=1 would
947 almost have done the trick, but messes up with the also_make
948 updating logic below. */
949 touched = 1;
950 }
951 }
952 }
953
954 if (file->mtime_before_update == UNKNOWN_MTIME)
955 file->mtime_before_update = file->last_mtime;
956
957 if ((ran && !file->phony) || touched)
958 {
959 int i = 0;
960
961 /* If -n, -t, or -q and all the commands are recursive, we ran them so
962 really check the target's mtime again. Otherwise, assume the target
963 would have been updated. */
964
965 if ((question_flag || just_print_flag || touch_flag) && file->cmds)
966 {
967 for (i = file->cmds->ncommand_lines; i > 0; --i)
968 if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
969 break;
970 }
971
972 /* If there were no commands at all, it's always new. */
973
974 else if (file->is_target && file->cmds == 0)
975 i = 1;
976
977 file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
978 }
979
980 if (file->double_colon)
981 {
982 /* If this is a double colon rule and it is the last one to be
983 updated, propagate the change of modification time to all the
984 double-colon entries for this file.
985
986 We do it on the last update because it is important to handle
987 individual entries as separate rules with separate timestamps
988 while they are treated as targets and then as one rule with the
989 unified timestamp when they are considered as a prerequisite
990 of some target. */
991
992 struct file *f;
993 FILE_TIMESTAMP max_mtime = file->last_mtime;
994
995 /* Check that all rules were updated and at the same time find
996 the max timestamp. We assume UNKNOWN_MTIME is newer then
997 any other value. */
998 for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
999 if (max_mtime != UNKNOWN_MTIME
1000 && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
1001 max_mtime = f->last_mtime;
1002
1003 if (f == 0)
1004 for (f = file->double_colon; f != 0; f = f->prev)
1005 f->last_mtime = max_mtime;
1006 }
1007
1008 if (ran && file->update_status != us_none)
1009 /* We actually tried to update FILE, which has
1010 updated its also_make's as well (if it worked).
1011 If it didn't work, it wouldn't work again for them.
1012 So mark them as updated with the same status. */
1013 for (d = file->also_make; d != 0; d = d->next)
1014 {
1015 d->file->command_state = cs_finished;
1016 d->file->updated = 1;
1017 d->file->update_status = file->update_status;
1018
1019 if (ran && !d->file->phony)
1020 /* Fetch the new modification time.
1021 We do this instead of just invalidating the cached time
1022 so that a vpath_search can happen. Otherwise, it would
1023 never be done because the target is already updated. */
1024 f_mtime (d->file, 0);
1025 }
1026 else if (file->update_status == us_none)
1027 /* Nothing was done for FILE, but it needed nothing done.
1028 So mark it now as "succeeded". */
1029 file->update_status = us_success;
1030
1031 if (profile_flag) {
1032 profile_add_timestamp(file);
1033 }
1034 }
1035
1036 /* Check whether another file (whose mtime is THIS_MTIME) needs updating on
1037 account of a dependency which is file FILE. If it does, store 1 in
1038 *MUST_MAKE_PTR. In the process, update any non-intermediate files that
1039 FILE depends on (including FILE itself). Return nonzero if any updating
1040 failed. */
1041
1042 static enum update_status
1043 check_dep (struct file *file, unsigned int depth,
1044 FILE_TIMESTAMP this_mtime, int *must_make_ptr,
1045 target_stack_node_t *p_call_stack)
1046 {
1047 struct file *ofile;
1048 struct dep *d;
1049 enum update_status dep_status = us_success;
1050
1051 ++depth;
1052 start_updating (file);
1053
1054 /* We might change file if we find a different one via vpath;
1055 remember this one to turn off updating. */
1056 ofile = file;
1057
1058 if (file->phony || !file->intermediate)
1059 {
1060 /* If this is a non-intermediate file, update it and record whether it
1061 is newer than THIS_MTIME. */
1062 FILE_TIMESTAMP mtime;
1063 dep_status = update_file (file, depth, p_call_stack);
1064 check_renamed (file);
1065 mtime = file_mtime (file);
1066 check_renamed (file);
1067 if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
1068 *must_make_ptr = 1;
1069 }
1070 else
1071 {
1072 /* FILE is an intermediate file. */
1073 FILE_TIMESTAMP mtime;
1074
1075 if (!file->phony && file->cmds == 0 && !file->tried_implicit)
1076 {
1077 if (try_implicit_rule (file, depth))
1078 DBF (DB_IMPLICIT, _("Found an implicit rule for '%s'.\n"));
1079 else
1080 DBF (DB_IMPLICIT, _("No implicit rule found for '%s'.\n"));
1081 file->tried_implicit = 1;
1082 }
1083 if (file->cmds == 0 && !file->is_target
1084 && default_file != 0 && default_file->cmds != 0)
1085 {
1086 DBF (DB_IMPLICIT, _("Using default commands for '%s'.\n"));
1087 file->cmds = default_file->cmds;
1088 }
1089
1090 check_renamed (file);
1091 mtime = file_mtime (file);
1092 check_renamed (file);
1093 if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
1094 /* If the intermediate file actually exists and is newer, then we
1095 should remake from it. */
1096 *must_make_ptr = 1;
1097 else
1098 {
1099 /* Otherwise, update all non-intermediate files we depend on, if
1100 necessary, and see whether any of them is more recent than the
1101 file on whose behalf we are checking. */
1102 struct dep *ld;
1103 int deps_running = 0;
1104
1105 /* If this target is not running, set it's state so that we check it
1106 fresh. It could be it was checked as part of an order-only
1107 prerequisite and so wasn't rebuilt then, but should be now. */
1108 if (file->command_state != cs_running)
1109 {
1110 /* If the target was waiting for a dependency it has to be
1111 reconsidered, as that dependency might have finished. */
1112 if (file->command_state == cs_deps_running)
1113 file->considered = 0;
1114
1115 set_command_state (file, cs_not_started);
1116 }
1117
1118 ld = 0;
1119 d = file->deps;
1120 while (d != 0)
1121 {
1122 enum update_status new;
1123 int maybe_make;
1124
1125 if (is_updating (d->file))
1126 {
1127 OSS (error, NILF, _("Circular %s <- %s dependency dropped."),
1128 file->name, d->file->name);
1129 if (ld == 0)
1130 {
1131 file->deps = d->next;
1132 free_dep (d);
1133 d = file->deps;
1134 }
1135 else
1136 {
1137 ld->next = d->next;
1138 free_dep (d);
1139 d = ld->next;
1140 }
1141 continue;
1142 }
1143
1144 d->file->parent = file;
1145 maybe_make = *must_make_ptr;
1146 new = check_dep (d->file, depth, this_mtime, &maybe_make, p_call_stack);
1147 if (new > dep_status)
1148 dep_status = new;
1149
1150 if (! d->ignore_mtime)
1151 *must_make_ptr = maybe_make;
1152 check_renamed (d->file);
1153 if (dep_status && !keep_going_flag)
1154 break;
1155
1156 if (d->file->command_state == cs_running
1157 || d->file->command_state == cs_deps_running)
1158 deps_running = 1;
1159
1160 ld = d;
1161 d = d->next;
1162 }
1163
1164 if (deps_running)
1165 /* Record that some of FILE's deps are still being made.
1166 This tells the upper levels to wait on processing it until the
1167 commands are finished. */
1168 set_command_state (file, cs_deps_running);
1169 }
1170 }
1171
1172 finish_updating (file);
1173 finish_updating (ofile);
1174
1175 return dep_status;
1176 }
1177
1178 /* Touch FILE. Return us_success if successful, us_failed if not. */
1179
1180 #define TOUCH_ERROR(call) do{ perror_with_name ((call), file->name); \
1181 return us_failed; }while(0)
1182
1183 static enum update_status
1184 touch_file (struct file *file)
1185 {
1186 if (!run_silent)
1187 OS (message, 0, "touch %s", file->name);
1188
1189 /* Print-only (-n) takes precedence over touch (-t). */
1190 if (just_print_flag)
1191 return us_success;
1192
1193 #ifndef NO_ARCHIVES
1194 if (ar_name (file->name))
1195 return ar_touch (file->name) ? us_failed : us_success;
1196 else
1197 #endif
1198 {
1199 int fd;
1200
1201 EINTRLOOP (fd, open (file->name, O_RDWR | O_CREAT, 0666));
1202 if (fd < 0)
1203 TOUCH_ERROR ("touch: open: ");
1204 else
1205 {
1206 struct stat statbuf;
1207 char buf = 'x';
1208 int e;
1209
1210 EINTRLOOP (e, fstat (fd, &statbuf));
1211 if (e < 0)
1212 TOUCH_ERROR ("touch: fstat: ");
1213 /* Rewrite character 0 same as it already is. */
1214 EINTRLOOP (e, read (fd, &buf, 1));
1215 if (e < 0)
1216 TOUCH_ERROR ("touch: read: ");
1217 {
1218 off_t o;
1219 EINTRLOOP (o, lseek (fd, 0L, 0));
1220 if (o < 0L)
1221 TOUCH_ERROR ("touch: lseek: ");
1222 }
1223 EINTRLOOP (e, write (fd, &buf, 1));
1224 if (e < 0)
1225 TOUCH_ERROR ("touch: write: ");
1226
1227 /* If file length was 0, we just changed it, so change it back. */
1228 if (statbuf.st_size == 0)
1229 {
1230 (void) close (fd);
1231 EINTRLOOP (fd, open (file->name, O_RDWR | O_TRUNC, 0666));
1232 if (fd < 0)
1233 TOUCH_ERROR ("touch: open: ");
1234 }
1235 (void) close (fd);
1236 }
1237 }
1238
1239 return us_success;
1240 }
1241
1242 /* Having checked and updated the dependencies of FILE,
1243 do whatever is appropriate to remake FILE itself.
1244 Return the status from executing FILE's commands. */
1245
1246 static void
1247 remake_file (file_t *file, target_stack_node_t *p_call_stack)
1248 {
1249 if (file->cmds == 0)
1250 {
1251 if (file->phony)
1252 /* Phony target. Pretend it succeeded. */
1253 file->update_status = us_success;
1254 else if (file->is_target)
1255 /* This is a nonexistent target file we cannot make.
1256 Pretend it was successfully remade. */
1257 file->update_status = us_success;
1258 else
1259 {
1260 /* This is a dependency file we cannot remake. Fail. */
1261 if (!rebuilding_makefiles || !file->dontcare)
1262 complain (file);
1263 file->update_status = us_failed;
1264 }
1265 }
1266 else
1267 {
1268 chop_commands (file->cmds);
1269
1270 /* The normal case: start some commands. */
1271 if (!touch_flag || file->cmds->any_recurse)
1272 {
1273 execute_file_commands (file, p_call_stack);
1274 return;
1275 }
1276
1277 /* This tells notice_finished_file it is ok to touch the file. */
1278 file->update_status = us_success;
1279 }
1280
1281 /* This does the touching under -t. */
1282 notice_finished_file (file);
1283 }
1284
1285 /* Return the mtime of a file, given a 'struct file'.
1286 Caches the time in the struct file to avoid excess stat calls.
1287
1288 If the file is not found, and SEARCH is nonzero, VPATH searching and
1289 replacement is done. If that fails, a library (-lLIBNAME) is tried and
1290 the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
1291 FILE. */
1292
1293 FILE_TIMESTAMP
1294 f_mtime (struct file *file, int search)
1295 {
1296 FILE_TIMESTAMP mtime;
1297 unsigned int propagate_timestamp;
1298
1299 /* File's mtime is not known; must get it from the system. */
1300
1301 #ifndef NO_ARCHIVES
1302 if (ar_name (file->name))
1303 {
1304 /* This file is an archive-member reference. */
1305
1306 char *arname, *memname;
1307 struct file *arfile;
1308 int found;
1309 time_t member_date;
1310
1311 /* Find the archive's name. */
1312 ar_parse_name (file->name, &arname, &memname);
1313
1314 /* Find the modification time of the archive itself.
1315 Also allow for its name to be changed via VPATH search. */
1316 arfile = lookup_file (arname);
1317 if (arfile == 0)
1318 arfile = enter_file (strcache_add (arname));
1319 mtime = f_mtime (arfile, search);
1320 check_renamed (arfile);
1321 if (search && strcmp (arfile->hname, arname))
1322 {
1323 /* The archive's name has changed.
1324 Change the archive-member reference accordingly. */
1325
1326 char *name;
1327 size_t arlen, memlen;
1328
1329 arlen = strlen (arfile->hname);
1330 memlen = strlen (memname);
1331
1332 name = alloca (arlen + 1 + memlen + 2);
1333 memcpy (name, arfile->hname, arlen);
1334 name[arlen] = '(';
1335 memcpy (name + arlen + 1, memname, memlen);
1336 name[arlen + 1 + memlen] = ')';
1337 name[arlen + 1 + memlen + 1] = '\0';
1338
1339 /* If the archive was found with GPATH, make the change permanent;
1340 otherwise defer it until later. */
1341 if (arfile->name == arfile->hname)
1342 rename_file (file, strcache_add (name));
1343 else
1344 rehash_file (file, strcache_add (name));
1345 check_renamed (file);
1346 }
1347
1348 free (arname);
1349
1350 file->low_resolution_time = 1;
1351
1352 if (mtime == NONEXISTENT_MTIME)
1353 /* The archive doesn't exist, so its members don't exist either. */
1354 return NONEXISTENT_MTIME;
1355
1356 found = ar_member_date (file->hname, &member_date);
1357 if (found && member_date == (time_t) 0)
1358 {
1359 OSS (error, NILF,
1360 _("Warning: Archive '%s' seems to have been created in deterministic mode. '%s' will always be updated. Please consider passing the U flag to ar to avoid the problem."),
1361 arfile->name, memname);
1362
1363 }
1364 mtime = found ? file_timestamp_cons (file->hname, member_date, 0) : NONEXISTENT_MTIME;
1365 }
1366 else
1367 #endif
1368 {
1369 mtime = name_mtime (file->name);
1370
1371 if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
1372 {
1373 /* If name_mtime failed, search VPATH. */
1374 const char *name = vpath_search (file->name, &mtime, NULL, NULL);
1375 if (name
1376 /* Last resort, is it a library (-lxxx)? */
1377 || (file->name[0] == '-' && file->name[1] == 'l'
1378 && (name = library_search (file->name, &mtime)) != 0))
1379 {
1380 size_t name_len;
1381
1382 if (mtime != UNKNOWN_MTIME)
1383 /* vpath_search and library_search store UNKNOWN_MTIME
1384 if they didn't need to do a stat call for their work. */
1385 file->last_mtime = mtime;
1386
1387 /* If we found it in VPATH, see if it's in GPATH too; if so,
1388 change the name right now; if not, defer until after the
1389 dependencies are updated. */
1390 name_len = strlen (name) - strlen (file->name);
1391 if (name[name_len - 1] == '/')
1392 name_len--;
1393 if (gpath_search (name, name_len))
1394 {
1395 rename_file (file, name);
1396 check_renamed (file);
1397 return file_mtime (file);
1398 }
1399
1400 rehash_file (file, name);
1401 check_renamed (file);
1402 /* If the result of a vpath search is -o or -W, preserve it.
1403 Otherwise, find the mtime of the resulting file. */
1404 if (mtime != OLD_MTIME && mtime != NEW_MTIME)
1405 mtime = name_mtime (name);
1406 }
1407 }
1408 }
1409
1410 /* Files can have bogus timestamps that nothing newly made will be
1411 "newer" than. Updating their dependents could just result in loops.
1412 So notify the user of the anomaly with a warning.
1413
1414 We only need to do this once, for now. */
1415
1416 if (!clock_skew_detected
1417 && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
1418 && !file->updated)
1419 {
1420 static FILE_TIMESTAMP adjusted_now;
1421
1422 FILE_TIMESTAMP adjusted_mtime = mtime;
1423
1424 #if defined(WINDOWS32) || defined(__MSDOS__)
1425 /* Experimentation has shown that FAT filesystems can set file times
1426 up to 3 seconds into the future! Play it safe. */
1427
1428 #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
1429
1430 FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
1431 if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
1432 adjusted_mtime -= adjustment;
1433 #elif defined(__EMX__)
1434 /* FAT filesystems round time to the nearest even second!
1435 Allow for any file (NTFS or FAT) to perhaps suffer from this
1436 brain damage. */
1437 FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
1438 && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
1439 ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
1440 : 0);
1441 #endif
1442
1443 /* If the file's time appears to be in the future, update our
1444 concept of the present and try once more. */
1445 if (adjusted_now < adjusted_mtime)
1446 {
1447 int resolution;
1448 FILE_TIMESTAMP now = file_timestamp_now (&resolution);
1449 adjusted_now = now + (resolution - 1);
1450 if (adjusted_now < adjusted_mtime)
1451 {
1452 double from_now =
1453 (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
1454 + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
1455 / 1e9));
1456 char from_now_string[100];
1457
1458 if (from_now >= 99 && from_now <= ULONG_MAX)
1459 sprintf (from_now_string, "%lu", (unsigned long) from_now);
1460 else
1461 sprintf (from_now_string, "%.2g", from_now);
1462 OSS (error, NILF,
1463 _("Warning: File '%s' has modification time %s s in the future"),
1464 file->name, from_now_string);
1465 clock_skew_detected = 1;
1466 }
1467 }
1468 }
1469
1470 /* Store the mtime into all the entries for this file for which it is safe
1471 to do so: avoid propagating timestamps to double-colon rules that haven't
1472 been examined so they're run or not based on the pre-update timestamp. */
1473 if (file->double_colon)
1474 file = file->double_colon;
1475
1476 propagate_timestamp = file->updated;
1477 do
1478 {
1479 /* If this file is not implicit but it is intermediate then it was
1480 made so by the .INTERMEDIATE target. If this file has never
1481 been built by us but was found now, it existed before make
1482 started. So, turn off the intermediate bit so make doesn't
1483 delete it, since it didn't create it. */
1484 if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
1485 && !file->tried_implicit && file->intermediate)
1486 file->intermediate = 0;
1487
1488 if (file->updated == propagate_timestamp)
1489 file->last_mtime = mtime;
1490 file = file->prev;
1491 }
1492 while (file != 0);
1493
1494 return mtime;
1495 }
1496
1497
1498 /* Return the mtime of the file or archive-member reference NAME. */
1499
1500 /* First, we check with stat(). If the file does not exist, then we return
1501 NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
1502 examine each indirection of the symlink and find the newest mtime.
1503 This causes one duplicate stat() when -L is being used, but the code is
1504 much cleaner. */
1505
1506 static FILE_TIMESTAMP
1507 name_mtime (const char *name)
1508 {
1509 FILE_TIMESTAMP mtime;
1510 struct stat st;
1511 int e;
1512
1513 #if defined(WINDOWS32)
1514 {
1515 char tem[MAXPATHLEN], *tstart, *tend;
1516 const char *p = name + strlen (name);
1517
1518 /* Remove any trailing slashes and "."/"..". MS-Windows stat
1519 fails on valid directories if NAME ends in a slash, and we need
1520 to emulate the Posix behavior where stat on "foo/" or "foo/."
1521 succeeds ONLY if "foo" is a directory. */
1522 if (p > name)
1523 {
1524 memcpy (tem, name, p - name + 1);
1525 tstart = tem;
1526 if (tstart[1] == ':')
1527 tstart += 2;
1528 tend = tem + (p - name - 1);
1529 if (*tend == '.' && tend > tstart)
1530 tend--;
1531 if (*tend == '.' && tend > tstart)
1532 tend--;
1533 for ( ; tend > tstart && (*tend == '/' || *tend == '\\'); tend--)
1534 *tend = '\0';
1535 }
1536 else
1537 {
1538 tem[0] = '\0';
1539 tend = &tem[0];
1540 }
1541
1542 e = stat (tem, &st);
1543 if (e == 0 && !_S_ISDIR (st.st_mode) && tend < tem + (p - name - 1))
1544 {
1545 errno = ENOTDIR;
1546 e = -1;
1547 }
1548 }
1549 #else
1550 EINTRLOOP (e, stat (name, &st));
1551 #endif
1552 if (e == 0)
1553 mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
1554 else if (errno == ENOENT || errno == ENOTDIR)
1555 mtime = NONEXISTENT_MTIME;
1556 else
1557 {
1558 perror_with_name ("stat: ", name);
1559 return NONEXISTENT_MTIME;
1560 }
1561
1562 /* If we get here we either found it, or it doesn't exist.
1563 If it doesn't exist see if we can use a symlink mtime instead. */
1564
1565 #ifdef MAKE_SYMLINKS
1566 #ifndef S_ISLNK
1567 # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
1568 #endif
1569 if (check_symlink_flag && strlen (name) <= GET_PATH_MAX)
1570 {
1571 PATH_VAR (lpath);
1572
1573 /* Check each symbolic link segment (if any). Find the latest mtime
1574 amongst all of them (and the target file of course).
1575 Note that we have already successfully dereferenced all the links
1576 above. So, if we run into any error trying to lstat(), or
1577 readlink(), or whatever, something bizarre-o happened. Just give up
1578 and use whatever mtime we've already computed at that point. */
1579 strcpy (lpath, name);
1580 while (1)
1581 {
1582 FILE_TIMESTAMP ltime;
1583 PATH_VAR (lbuf);
1584 long llen;
1585 char *p;
1586
1587 EINTRLOOP (e, lstat (lpath, &st));
1588 if (e)
1589 {
1590 /* Just take what we have so far. */
1591 if (errno != ENOENT && errno != ENOTDIR)
1592 perror_with_name ("lstat: ", lpath);
1593 break;
1594 }
1595
1596 /* If this is not a symlink, we're done (we started with the real
1597 file's mtime so we don't need to test it again). */
1598 if (!S_ISLNK (st.st_mode))
1599 break;
1600
1601 /* If this mtime is newer than what we had, keep the new one. */
1602 ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
1603 if (ltime > mtime)
1604 mtime = ltime;
1605
1606 /* Set up to check the file pointed to by this link. */
1607 EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
1608 if (llen < 0)
1609 {
1610 /* Eh? Just take what we have. */
1611 perror_with_name ("readlink: ", lpath);
1612 break;
1613 }
1614 lbuf[llen] = '\0';
1615
1616 /* If the target is fully-qualified or the source is just a
1617 filename, then the new path is the target. Otherwise it's the
1618 source directory plus the target. */
1619 if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
1620 strcpy (lpath, lbuf);
1621 else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
1622 /* Eh? Path too long! Again, just go with what we have. */
1623 break;
1624 else
1625 /* Create the next step in the symlink chain. */
1626 strcpy (p+1, lbuf);
1627 }
1628 }
1629 #endif
1630
1631 return mtime;
1632 }
1633
1634
1635 /* Search for a library file specified as -lLIBNAME, searching for a
1636 suitable library file in the system library directories and the VPATH
1637 directories. */
1638
1639 static const char *
1640 library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
1641 {
1642 static const char *dirs[] =
1643 {
1644 "/lib",
1645 "/usr/lib",
1646 #if defined(WINDOWS32) && !defined(LIBDIR)
1647 /*
1648 * This is completely up to the user at product install time. Just define
1649 * a placeholder.
1650 */
1651 #define LIBDIR "."
1652 #endif
1653 LIBDIR, /* Defined by configuration. */
1654 0
1655 };
1656
1657 const char *file = 0;
1658 char *libpatterns;
1659 FILE_TIMESTAMP mtime;
1660
1661 /* Loop variables for the libpatterns value. */
1662 char *p;
1663 const char *p2;
1664 size_t len;
1665 size_t liblen;
1666
1667 /* Information about the earliest (in the vpath sequence) match. */
1668 unsigned int best_vpath = 0, best_path = 0;
1669
1670 const char **dp;
1671
1672 libpatterns = xstrdup (variable_expand ("$(.LIBPATTERNS)"));
1673
1674 /* Skip the '-l'. */
1675 lib += 2;
1676 liblen = strlen (lib);
1677
1678 /* Loop through all the patterns in .LIBPATTERNS, and search on each one.
1679 To implement the linker-compatible behavior we have to search through
1680 all entries in .LIBPATTERNS and choose the "earliest" one. */
1681 p2 = libpatterns;
1682 while ((p = find_next_token (&p2, &len)) != 0)
1683 {
1684 static char *buf = NULL;
1685 static size_t buflen = 0;
1686 static size_t libdir_maxlen = 0;
1687 static unsigned int std_dirs = 0;
1688 char *libbuf = variable_expand ("");
1689
1690 /* Expand the pattern using LIB as a replacement. */
1691 {
1692 char c = p[len];
1693 char *p3, *p4;
1694
1695 p[len] = '\0';
1696 p3 = find_percent (p);
1697 if (!p3)
1698 {
1699 /* Give a warning if there is no pattern. */
1700 OS (error, NILF,
1701 _(".LIBPATTERNS element '%s' is not a pattern"), p);
1702 p[len] = c;
1703 continue;
1704 }
1705 p4 = variable_buffer_output (libbuf, p, p3-p);
1706 p4 = variable_buffer_output (p4, lib, liblen);
1707 p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
1708 p[len] = c;
1709 }
1710
1711 /* Look first for 'libNAME.a' in the current directory. */
1712 mtime = name_mtime (libbuf);
1713 if (mtime != NONEXISTENT_MTIME)
1714 {
1715 if (mtime_ptr != 0)
1716 *mtime_ptr = mtime;
1717 file = strcache_add (libbuf);
1718 /* This by definition will have the best index, so stop now. */
1719 break;
1720 }
1721
1722 /* Now try VPATH search on that. */
1723
1724 {
1725 unsigned int vpath_index, path_index;
1726 const char* f = vpath_search (libbuf, mtime_ptr ? &mtime : NULL,
1727 &vpath_index, &path_index);
1728 if (f)
1729 {
1730 /* If we have a better match, record it. */
1731 if (file == 0 ||
1732 vpath_index < best_vpath ||
1733 (vpath_index == best_vpath && path_index < best_path))
1734 {
1735 file = f;
1736 best_vpath = vpath_index;
1737 best_path = path_index;
1738
1739 if (mtime_ptr != 0)
1740 *mtime_ptr = mtime;
1741 }
1742 }
1743 }
1744
1745 /* Now try the standard set of directories. */
1746
1747 if (!buflen)
1748 {
1749 for (dp = dirs; *dp != 0; ++dp)
1750 {
1751 size_t l = strlen (*dp);
1752 if (l > libdir_maxlen)
1753 libdir_maxlen = l;
1754 std_dirs++;
1755 }
1756 buflen = strlen (libbuf);
1757 buf = xmalloc (libdir_maxlen + buflen + 2);
1758 }
1759 else if (buflen < strlen (libbuf))
1760 {
1761 buflen = strlen (libbuf);
1762 buf = xrealloc (buf, libdir_maxlen + buflen + 2);
1763 }
1764
1765 {
1766 /* Use the last std_dirs index for standard directories. This
1767 was it will always be greater than the VPATH index. */
1768 unsigned int vpath_index = ~((unsigned int)0) - std_dirs;
1769
1770 for (dp = dirs; *dp != 0; ++dp)
1771 {
1772 sprintf (buf, "%s/%s", *dp, libbuf);
1773 mtime = name_mtime (buf);
1774 if (mtime != NONEXISTENT_MTIME)
1775 {
1776 if (file == 0 || vpath_index < best_vpath)
1777 {
1778 file = strcache_add (buf);
1779 best_vpath = vpath_index;
1780
1781 if (mtime_ptr != 0)
1782 *mtime_ptr = mtime;
1783 }
1784 }
1785
1786 vpath_index++;
1787 }
1788 }
1789
1790 }
1791
1792 free (libpatterns);
1793 return file;
1794 }