Debian Codegraph @

src/file.c

raw symbols SCIP
1 /* Target file management 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 "file_basic.h"
19
20 #include <assert.h>
21
22 #include "filedef.h"
23 #include "file.h"
24 #include "dep.h"
25 #include "job.h"
26 #include "commands.h"
27 #include "variable.h"
28 #include "debug.h"
29 #include "hash.h"
30 #include "profile.h"
31 #include "globals.h"
32
33
34 /* Remember whether snap_deps has been invoked: we need this to be sure we
35 don't add new rules (via $(eval ...)) afterwards. In the future it would
36 be nice to support this, but it means we'd need to re-run snap_deps() or
37 at least its functionality... it might mean changing snap_deps() to be run
38 per-file, so we can invoke it after the eval... or remembering which files
39 in the hash have been snapped (a new boolean flag?) and having snap_deps()
40 only work on files which have not yet been snapped. */
41 int snapped_deps = 0;
42
43 /* Hash table of files the makefile knows how to make. */
44
45 /* Whether or not .SECONDARY with no prerequisites was given. */
46 static int all_secondary = 0;
47
48
49 /* Rehash FILE to NAME. This is not as simple as resetting
50 the 'hname' member, since it must be put in a new hash bucket,
51 and possibly merged with an existing file called NAME. */
52
53 void
54 rehash_file (struct file *from_file, const char *to_hname)
55 {
56 struct file file_key;
57 struct file **file_slot;
58 struct file *to_file;
59 struct file *deleted_file;
60 struct file *f;
61
62 /* If it's already that name, we're done. */
63 from_file->builtin = 0;
64 file_key.hname = to_hname;
65 if (! file_hash_cmp (from_file, &file_key))
66 return;
67
68 /* Find the end of the renamed list for the "from" file. */
69 file_key.hname = from_file->hname;
70 while (from_file->renamed != 0)
71 from_file = from_file->renamed;
72 if (file_hash_cmp (from_file, &file_key))
73 /* hname changed unexpectedly!! */
74 abort ();
75
76 /* Remove the "from" file from the hash. */
77 deleted_file = hash_delete (&files, from_file);
78 if (deleted_file != from_file)
79 /* from_file isn't the one stored in files */
80 abort ();
81
82 /* Find where the newly renamed file will go in the hash. */
83 file_key.hname = to_hname;
84 file_slot = (struct file **) hash_find_slot (&files, &file_key);
85 to_file = *file_slot;
86
87 /* Change the hash name for this file. */
88 from_file->hname = to_hname;
89 for (f = from_file->double_colon; f != 0; f = f->prev)
90 f->hname = to_hname;
91
92 /* If the new name doesn't exist yet just set it to the renamed file. */
93 if (HASH_VACANT (to_file))
94 {
95 hash_insert_at (&files, from_file, file_slot);
96 return;
97 }
98
99 /* TO_FILE already exists under TO_HNAME.
100 We must retain TO_FILE and merge FROM_FILE into it. */
101
102 if (from_file->cmds != 0)
103 {
104 if (to_file->cmds == 0)
105 to_file->cmds = from_file->cmds;
106 else if (from_file->cmds != to_file->cmds)
107 {
108 size_t l = strlen (from_file->name);
109 /* We have two sets of commands. We will go with the
110 one given in the rule explicitly mentioning this name,
111 but give a message to let the user know what's going on. */
112 if (to_file->cmds->fileinfo.filenm != 0)
113 error (&from_file->cmds->fileinfo,
114 l + strlen (to_file->cmds->fileinfo.filenm) + INTSTR_LENGTH,
115 _("Recipe was specified for file '%s' at %s:%lu,"),
116 from_file->name, to_file->cmds->fileinfo.filenm,
117 to_file->cmds->fileinfo.lineno);
118 else
119 error (&from_file->cmds->fileinfo, l,
120 _("Recipe for file '%s' was found by implicit rule search,"),
121 from_file->name);
122 l += strlen (to_hname);
123 error (&from_file->cmds->fileinfo, l,
124 _("but '%s' is now considered the same file as '%s'."),
125 from_file->name, to_hname);
126 error (&from_file->cmds->fileinfo, l,
127 _("Recipe for '%s' will be ignored in favor of the one for '%s'."),
128 to_hname, from_file->name);
129 }
130 }
131
132 /* Merge the dependencies of the two files. */
133
134 if (to_file->deps == 0)
135 to_file->deps = from_file->deps;
136 else
137 {
138 struct dep *deps = to_file->deps;
139 while (deps->next != 0)
140 deps = deps->next;
141 deps->next = from_file->deps;
142 }
143
144 merge_variable_set_lists (&to_file->variables, from_file->variables);
145
146 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
147 OSS (fatal, NILF, _("can't rename single-colon '%s' to double-colon '%s'"),
148 from_file->name, to_hname);
149 if (!to_file->double_colon && from_file->double_colon)
150 {
151 if (to_file->is_target)
152 OSS (fatal, NILF,
153 _("can't rename double-colon '%s' to single-colon '%s'"),
154 from_file->name, to_hname);
155 else
156 to_file->double_colon = from_file->double_colon;
157 }
158
159 if (from_file->last_mtime > to_file->last_mtime)
160 /* %%% Kludge so -W wins on a file that gets vpathized. */
161 to_file->last_mtime = from_file->last_mtime;
162
163 to_file->mtime_before_update = from_file->mtime_before_update;
164
165 #define MERGE(field) to_file->field |= from_file->field
166 MERGE (precious);
167 MERGE (tried_implicit);
168 MERGE (updating);
169 MERGE (updated);
170 MERGE (is_target);
171 MERGE (cmd_target);
172 MERGE (phony);
173 MERGE (loaded);
174 MERGE (ignore_vpath);
175 #undef MERGE
176
177 to_file->builtin = 0;
178 from_file->renamed = to_file;
179 }
180
181 /* Rename FILE to NAME. This is not as simple as resetting
182 the 'name' member, since it must be put in a new hash bucket,
183 and possibly merged with an existing file called NAME. */
184
185 void
186 rename_file (struct file *from_file, const char *to_hname)
187 {
188 rehash_file (from_file, to_hname);
189 while (from_file)
190 {
191 from_file->name = from_file->hname;
192 from_file = from_file->prev;
193 }
194 }
195
196 /* Remove all nonprecious intermediate files.
197 If SIG is nonzero, this was caused by a fatal signal,
198 meaning that a different message will be printed, and
199 the message will go to stderr rather than stdout. */
200
201 void
202 remove_intermediates (int sig)
203 {
204 struct file **file_slot;
205 struct file **file_end;
206 int doneany = 0;
207
208 /* If there's no way we will ever remove anything anyway, punt early. */
209 if (question_flag || touch_flag || all_secondary)
210 return;
211
212 if (sig && just_print_flag)
213 return;
214
215 file_slot = (struct file **) files.ht_vec;
216 file_end = file_slot + files.ht_size;
217 for ( ; file_slot < file_end; file_slot++)
218 if (! HASH_VACANT (*file_slot))
219 {
220 struct file *f = *file_slot;
221 /* Is this file eligible for automatic deletion?
222 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
223 given on the command line, and it's either a -include makefile or
224 it's not precious. */
225 if (f->intermediate && (f->dontcare || !f->precious)
226 && !f->secondary && !f->cmd_target)
227 {
228 int status;
229 if (f->update_status == us_none)
230 /* If nothing would have created this file yet,
231 don't print an "rm" command for it. */
232 continue;
233 if (just_print_flag)
234 status = 0;
235 else
236 {
237 status = unlink (f->name);
238 if (status < 0 && errno == ENOENT)
239 continue;
240 }
241 if (!f->dontcare)
242 {
243 if (sig)
244 OS (error, NILF,
245 _("*** Deleting intermediate file '%s'"), f->name);
246 else
247 {
248 if (! doneany)
249 DB (DB_BASIC, (_("Removing intermediate files...\n")));
250 if (!run_silent)
251 {
252 if (! doneany)
253 {
254 fputs ("rm ", stdout);
255 doneany = 1;
256 }
257 else
258 putchar (' ');
259 fputs (f->name, stdout);
260 fflush (stdout);
261 }
262 }
263 if (status < 0)
264 perror_with_name ("unlink: ", f->name);
265 }
266 }
267 }
268
269 if (doneany && !sig)
270 {
271 putchar ('\n');
272 fflush (stdout);
273 }
274 }
275
276 /* Given a string containing prerequisites (fully expanded), break it up into
277 a struct dep list. Enter each of these prereqs into the file database.
278 */
279 struct dep *
280 split_prereqs (char *p)
281 {
282 struct dep *new = PARSE_FILE_SEQ (&p, struct dep, MAP_PIPE, NULL,
283 PARSEFS_NONE);
284
285 if (*p)
286 {
287 /* Files that follow '|' are "order-only" prerequisites that satisfy the
288 dependency by existing: their modification times are irrelevant. */
289 struct dep *ood;
290
291 ++p;
292 ood = PARSE_SIMPLE_SEQ (&p, struct dep);
293
294 if (! new)
295 new = ood;
296 else
297 {
298 struct dep *dp;
299 for (dp = new; dp->next != NULL; dp = dp->next)
300 ;
301 dp->next = ood;
302 }
303
304 for (; ood != NULL; ood = ood->next)
305 ood->ignore_mtime = 1;
306 }
307
308 return new;
309 }
310
311 /* Given a list of prerequisites, enter them into the file database.
312 If STEM is set then first expand patterns using STEM. */
313 struct dep *
314 enter_prereqs (struct dep *deps, const char *stem)
315 {
316 struct dep *d1;
317
318 if (deps == 0)
319 return 0;
320
321 /* If we have a stem, expand the %'s. We use patsubst_expand to translate
322 the prerequisites' patterns into plain prerequisite names. */
323 if (stem)
324 {
325 const char *pattern = "%";
326 char *buffer = variable_expand ("");
327 struct dep *dp = deps, *dl = 0;
328
329 while (dp != 0)
330 {
331 char *percent;
332 size_t nl = strlen (dp->name) + 1;
333 char *nm = alloca (nl);
334 memcpy (nm, dp->name, nl);
335 percent = find_percent (nm);
336 if (percent)
337 {
338 char *o;
339
340 /* We have to handle empty stems specially, because that
341 would be equivalent to $(patsubst %,dp->name,) which
342 will always be empty. */
343 if (stem[0] == '\0')
344 {
345 memmove (percent, percent+1, strlen (percent));
346 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
347 }
348 else
349 o = patsubst_expand_pat (buffer, stem, pattern, nm,
350 pattern+1, percent+1);
351
352 /* If the name expanded to the empty string, ignore it. */
353 if (buffer[0] == '\0')
354 {
355 struct dep *df = dp;
356 if (dp == deps)
357 dp = deps = deps->next;
358 else
359 dp = dl->next = dp->next;
360 free_dep (df);
361 continue;
362 }
363
364 /* Save the name. */
365 dp->name = strcache_add_len (buffer, o - buffer);
366 }
367 dp->stem = stem;
368 dp->staticpattern = 1;
369 dl = dp;
370 dp = dp->next;
371 }
372 }
373
374 /* Enter them as files, unless they need a 2nd expansion. */
375 for (d1 = deps; d1 != 0; d1 = d1->next)
376 {
377 if (d1->need_2nd_expansion)
378 continue;
379
380 d1->file = lookup_file (d1->name);
381 if (d1->file == 0)
382 d1->file = enter_file (d1->name);
383 d1->staticpattern = 0;
384 d1->name = 0;
385 }
386
387 return deps;
388 }
389
390 /* Expand and parse each dependency line. */
391 void
392 expand_deps (struct file *f)
393 {
394 struct dep *d;
395 struct dep **dp;
396 const char *file_stem = f->stem;
397 int initialized = 0;
398
399 f->updating = 0;
400
401 /* Walk through the dependencies. For any dependency that needs 2nd
402 expansion, expand it then insert the result into the list. */
403 dp = &f->deps;
404 d = f->deps;
405 while (d != 0)
406 {
407 char *p;
408 struct dep *new, *next;
409 char *name = (char *)d->name;
410
411 if (! d->name || ! d->need_2nd_expansion)
412 {
413 /* This one is all set already. */
414 dp = &d->next;
415 d = d->next;
416 continue;
417 }
418
419 /* If it's from a static pattern rule, convert the patterns into
420 "$*" so they'll expand properly. */
421 if (d->staticpattern)
422 {
423 char *o = variable_expand ("");
424 o = subst_expand (o, name, "%", "$*", 1, 2, 0);
425 *o = '\0';
426 free (name);
427 d->name = name = xstrdup (variable_buffer);
428 d->staticpattern = 0;
429 }
430
431 /* We're going to do second expansion so initialize file variables for
432 the file. Since the stem for static pattern rules comes from
433 individual dep lines, we will temporarily set f->stem to d->stem. */
434 if (!initialized)
435 {
436 initialize_file_variables (f, 0);
437 initialized = 1;
438 }
439
440 if (d->stem != 0)
441 f->stem = d->stem;
442
443 set_file_variables (f);
444
445 p = variable_expand_for_file (d->name, f);
446
447 if (d->stem != 0)
448 f->stem = file_stem;
449
450 /* At this point we don't need the name anymore: free it. */
451 free (name);
452
453 /* Parse the prerequisites and enter them into the file database. */
454 new = enter_prereqs (split_prereqs (p), d->stem);
455
456 /* If there were no prereqs here (blank!) then throw this one out. */
457 if (new == 0)
458 {
459 *dp = d->next;
460 free_dep (d);
461 d = *dp;
462 continue;
463 }
464
465 /* Add newly parsed prerequisites. */
466 next = d->next;
467 *dp = new;
468 for (dp = &new->next, d = new->next; d != 0; dp = &d->next, d = d->next)
469 ;
470 *dp = next;
471 d = *dp;
472 }
473 }
474
475 /* Add extra prereqs to the file in question. */
476
477 struct dep *
478 expand_extra_prereqs (const struct variable *extra)
479 {
480 struct dep *d;
481 struct dep *prereqs = extra ? split_prereqs (variable_expand (extra->value)) : NULL;
482
483 for (d = prereqs; d; d = d->next)
484 {
485 d->file = lookup_file (d->name);
486 if (!d->file)
487 d->file = enter_file (d->name);
488 d->name = NULL;
489 d->ignore_automatic_vars = 1;
490 }
491
492 return prereqs;
493 }
494
495 /* Perform per-file snap operations. */
496
497 static void
498 snap_file (const void *item, void *arg)
499 {
500 struct file *f = (struct file*)item;
501 struct dep *prereqs = NULL;
502
503 /* If we're not doing second expansion then reset updating. */
504 if (!second_expansion)
505 f->updating = 0;
506
507 /* If .SECONDARY is set with no deps, mark all targets as intermediate. */
508 if (all_secondary)
509 f->intermediate = 1;
510
511 /* If .EXTRA_PREREQS is set, add them as ignored by automatic variables. */
512 if (f->variables)
513 prereqs = expand_extra_prereqs (lookup_variable_in_set (STRING_SIZE_TUPLE(".EXTRA_PREREQS"), f->variables->set));
514
515 else if (f->is_target)
516 prereqs = copy_dep_chain (arg);
517
518 if (prereqs)
519 {
520 struct dep *d;
521 for (d = prereqs; d; d = d->next)
522 if (streq (f->name, dep_name (d)))
523 /* Skip circular dependencies. */
524 break;
525
526 if (d)
527 /* We broke early: must have found a circular dependency. */
528 free_dep_chain (prereqs);
529 else if (!f->deps)
530 f->deps = prereqs;
531 else
532 {
533 d = f->deps;
534 while (d->next)
535 d = d->next;
536 d->next = prereqs;
537 }
538 }
539 }
540
541 /* For each dependency of each file, make the 'struct dep' point
542 at the appropriate 'struct file' (which may have to be created).
543
544 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
545 and various other special targets. */
546
547 extern void
548 snap_deps (void)
549 {
550 struct file *f;
551 struct file *f2;
552 struct dep *d;
553
554 /* Remember that we've done this. Once we start snapping deps we can no
555 longer define new targets. */
556 snapped_deps = 1;
557
558 /* Perform second expansion and enter each dependency name as a file. We
559 must use hash_dump() here because within these loops we likely add new
560 files to the table, possibly causing an in-situ table expansion.
561
562 We only need to do this if second_expansion has been defined; if it
563 hasn't then all deps were expanded as the makefile was read in. If we
564 ever change make to be able to unset .SECONDARY_EXPANSION this will have
565 to change. */
566
567 if (second_expansion)
568 {
569 struct file **file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
570 struct file **file_end = file_slot_0 + files.ht_fill;
571 struct file **file_slot;
572 const char *suffixes;
573
574 /* Expand .SUFFIXES: its prerequisites are used for $$* calc. */
575 f = lookup_file (".SUFFIXES");
576 suffixes = f ? f->name : 0;
577 for (; f != 0; f = f->prev)
578 expand_deps (f);
579
580 /* For every target that's not .SUFFIXES, expand its prerequisites. */
581
582 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
583 for (f = *file_slot; f != 0; f = f->prev)
584 if (f->name != suffixes)
585 expand_deps (f);
586 free (file_slot_0);
587 }
588
589 /* Now manage all the special targets. */
590
591 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
592 for (d = f->deps; d != 0; d = d->next)
593 for (f2 = d->file; f2 != 0; f2 = f2->prev)
594 f2->precious = 1;
595
596 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
597 for (d = f->deps; d != 0; d = d->next)
598 for (f2 = d->file; f2 != 0; f2 = f2->prev)
599 f2->low_resolution_time = 1;
600
601 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
602 for (d = f->deps; d != 0; d = d->next)
603 for (f2 = d->file; f2 != 0; f2 = f2->prev)
604 {
605 /* Mark this file as phony nonexistent target. */
606 f2->phony = 1;
607 f2->is_target = 1;
608 f2->last_mtime = NONEXISTENT_MTIME;
609 f2->mtime_before_update = NONEXISTENT_MTIME;
610 }
611
612 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
613 /* Mark .INTERMEDIATE deps as intermediate files. */
614 for (d = f->deps; d != 0; d = d->next)
615 for (f2 = d->file; f2 != 0; f2 = f2->prev)
616 f2->intermediate = 1;
617 /* .INTERMEDIATE with no deps does nothing.
618 Marking all files as intermediates is useless since the goal targets
619 would be deleted after they are built. */
620
621 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
622 /* Mark .SECONDARY deps as both intermediate and secondary. */
623 if (f->deps)
624 for (d = f->deps; d != 0; d = d->next)
625 for (f2 = d->file; f2 != 0; f2 = f2->prev)
626 f2->intermediate = f2->secondary = 1;
627 /* .SECONDARY with no deps listed marks *all* files that way. */
628 else
629 all_secondary = 1;
630
631 f = lookup_file (".EXPORT_ALL_VARIABLES");
632 if (f != 0 && f->is_target)
633 export_all_variables = 1;
634
635 f = lookup_file (".IGNORE");
636 if (f != 0 && f->is_target)
637 {
638 if (f->deps == 0)
639 ignore_errors_flag = 1;
640 else
641 for (d = f->deps; d != 0; d = d->next)
642 for (f2 = d->file; f2 != 0; f2 = f2->prev)
643 f2->command_flags |= COMMANDS_NOERROR;
644 }
645
646 f = lookup_file (".SILENT");
647 if (f != 0 && f->is_target)
648 {
649 if (f->deps == 0)
650 run_silent = 1;
651 else
652 for (d = f->deps; d != 0; d = d->next)
653 for (f2 = d->file; f2 != 0; f2 = f2->prev)
654 f2->command_flags |= COMMANDS_SILENT;
655 }
656
657 f = lookup_file (".NOTPARALLEL");
658 if (f != 0 && f->is_target)
659 not_parallel = 1;
660
661 {
662 struct dep *prereqs = expand_extra_prereqs (lookup_variable (STRING_SIZE_TUPLE(".EXTRA_PREREQS")));
663
664 /* Perform per-file snap operations. */
665 hash_map_arg(&files, snap_file, prereqs);
666
667 free_dep_chain (prereqs);
668 }
669
670 #ifndef NO_MINUS_C_MINUS_O
671 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
672 /* This needs more work: what if the user sets this in the makefile?
673 if (posix_pedantic)
674 define_variable_cname ("OUTPUT_OPTION", "", o_default, 1);
675 */
676 #endif
677 }
678
679 /* Set the 'command_state' member of FILE and all its 'also_make's.
680 Don't decrease the state of also_make's (e.g., don't downgrade a 'running'
681 also_make to a 'deps_running' also_make). */
682
683 void
684 set_command_state (struct file *file, enum cmd_state state)
685 {
686 struct dep *d;
687
688 file->command_state = state;
689
690 for (d = file->also_make; d != 0; d = d->next) {
691 if (state > d->file->command_state) {
692 d->file->command_state = state;
693 }
694 }
695
696 if (profile_flag) {
697 profile_add_timestamp(file);
698 }
699 }
700
701 /* Convert an external file timestamp to internal form. */
702
703 FILE_TIMESTAMP
704 file_timestamp_cons (const char *fname, time_t stamp, long int ns)
705 {
706 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
707 FILE_TIMESTAMP s = stamp;
708 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
709 FILE_TIMESTAMP ts = product + offset;
710
711 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
712 && product <= ts && ts <= ORDINARY_MTIME_MAX))
713 {
714 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
715 const char *f = fname ? fname : _("Current time");
716 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
717 file_timestamp_sprintf (buf, ts);
718 OSS (error, NILF,
719 _("%s: Timestamp out of range; substituting %s"), f, buf);
720 }
721
722 return ts;
723 }
724
725 /* Return the current time as a file timestamp, setting *RESOLUTION to
726 its resolution. */
727 FILE_TIMESTAMP
728 file_timestamp_now (int *resolution)
729 {
730 int r;
731 time_t s;
732 int ns;
733
734 /* Don't bother with high-resolution clocks if file timestamps have
735 only one-second resolution. The code below should work, but it's
736 not worth the hassle of debugging it on hosts where it fails. */
737 #if FILE_TIMESTAMP_HI_RES
738 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
739 {
740 struct timespec timespec;
741 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
742 {
743 r = 1;
744 s = timespec.tv_sec;
745 ns = timespec.tv_nsec;
746 goto got_time;
747 }
748 }
749 # endif
750 # if HAVE_GETTIMEOFDAY
751 {
752 struct timeval timeval;
753 if (gettimeofday (&timeval, 0) == 0)
754 {
755 r = 1000;
756 s = timeval.tv_sec;
757 ns = timeval.tv_usec * 1000;
758 goto got_time;
759 }
760 }
761 # endif
762 #endif
763
764 r = 1000000000;
765 s = time ((time_t *) 0);
766 ns = 0;
767
768 #if FILE_TIMESTAMP_HI_RES
769 got_time:
770 #endif
771 *resolution = r;
772 return file_timestamp_cons (0, s, ns);
773 }
774
775 /* Place into the buffer P a printable representation of the file
776 timestamp TS. */
777 void
778 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
779 {
780 time_t t = FILE_TIMESTAMP_S (ts);
781 struct tm *tm = localtime (&t);
782
783 if (tm)
784 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
785 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
786 tm->tm_hour, tm->tm_min, tm->tm_sec);
787 else if (t < 0)
788 sprintf (p, "%ld", (long) t);
789 else
790 sprintf (p, "%lu", (unsigned long) t);
791 p += strlen (p);
792
793 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
794 know the actual timestamp resolution, since clock_getres applies only to
795 local times, whereas this timestamp might come from a remote filesystem.
796 So removing trailing zeros is the best guess that we can do. */
797 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
798 p += strlen (p) - 1;
799 while (*p == '0')
800 p--;
801 p += *p != '.';
802
803 *p = '\0';
804 }
805
806
807 /*! Print some or all properties of the data base of files.
808 */
809 void
810 print_target_props (file_t *p_target, print_target_mask_t i_mask)
811 {
812 dep_t *d;
813 dep_t *ood = 0;
814
815 putchar ('\n');
816 if (!p_target->is_target)
817 puts (_("# Not a target:"));
818 printf ("%s:%s", p_target->name, p_target->double_colon ? ":" : "");
819
820 /* Print all normal dependencies; note any order-only deps. */
821 for (d = p_target->deps; d != 0; d = d->next)
822 if (! d->ignore_mtime) {
823 if (i_mask & PRINT_TARGET_NONORDER)
824 printf (" %s", dep_name (d));
825 } else if (! ood)
826 ood = d;
827
828 if (i_mask & PRINT_TARGET_ORDER) {
829 /* Print order-only deps, if we have any. */
830 if (ood)
831 {
832 printf (" | %s", dep_name (ood));
833 for (d = ood->next; d != 0; d = d->next)
834 if (d->ignore_mtime)
835 printf (" %s", dep_name (d));
836 }
837 }
838
839 putchar ('\n');
840
841 if (i_mask & PRINT_TARGET_ATTRS) {
842
843 if (p_target->precious)
844 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
845 if (p_target->phony)
846 puts (_("# Phony target (prerequisite of .PHONY)."));
847 if (p_target->cmd_target)
848 puts (_("# Command-line target."));
849 if (p_target->dontcare)
850 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
851 puts (p_target->tried_implicit
852 ? _("# Implicit rule search has been done.")
853 : _("# Implicit rule search has not been done."));
854 if (p_target->stem != 0)
855 printf (_("# Implicit/static pattern stem: `%s'\n"), p_target->stem);
856 if (p_target->intermediate)
857 puts (_("# File is an intermediate prerequisite."));
858 if (p_target->also_make != 0)
859 {
860 fputs (_("# Also makes:"), stdout);
861 for (d = p_target->also_make; d != 0; d = d->next)
862 printf (" %s", dep_name (d));
863 putchar ('\n');
864 }
865 }
866
867 if (i_mask & PRINT_TARGET_TIME) {
868
869 if (p_target->last_mtime == UNKNOWN_MTIME)
870 puts (_("# Modification time never checked."));
871 else if (p_target->last_mtime == NONEXISTENT_MTIME)
872 puts (_("# File does not exist."));
873 else if (p_target->last_mtime == OLD_MTIME)
874 puts (_("# File is very old."));
875 else
876 {
877 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
878 file_timestamp_sprintf (buf, p_target->last_mtime);
879 printf (_("# Last modified %s\n"), buf);
880 }
881 puts (p_target->updated
882 ? _("# File has been updated.")
883 : _("# File has not been updated."));
884 }
885
886 if (i_mask & PRINT_TARGET_STATE) {
887
888 switch (p_target->command_state)
889 {
890 case cs_running:
891 puts (_("# Commands currently running (THIS IS A BUG)."));
892 break;
893 case cs_deps_running:
894 puts (_("# Dependencies commands running (THIS IS A BUG)."));
895 break;
896 case cs_not_started:
897 puts (_("# Commands not yet started."));
898 break;
899 case cs_finished:
900 switch (p_target->update_status)
901 {
902 case us_success:
903 puts (_("# Successfully updated."));
904 break;
905 case us_none:
906 puts (_("# No attempt to update has been made."));
907 break;
908 case us_question:
909 assert (question_flag);
910 puts (_("# Needs to be updated (-q is set)."));
911 break;
912 case us_failed:
913 puts (_("# Failed to be updated."));
914 break;
915 default:
916 puts (_("# Invalid value in `update_status' member!"));
917 fflush (stdout);
918 fflush (stderr);
919 abort ();
920 }
921 break;
922 default:
923 puts (_("# Invalid value in `command_state' member!"));
924 fflush (stdout);
925 fflush (stderr);
926 abort ();
927 }
928 }
929
930 if (p_target->variables != 0 && i_mask & PRINT_TARGET_VARS)
931 print_file_variables (p_target, i_mask & PRINT_TARGET_VARS_HASH);
932
933 if (p_target->cmds != 0 && i_mask & PRINT_TARGET_CMDS)
934 print_commands (p_target, p_target->cmds, false);
935
936 if (p_target->cmds != 0 && i_mask & PRINT_TARGET_CMDS_EXP)
937 print_commands (p_target, p_target->cmds, true);
938
939 if (p_target->prev && i_mask & PRINT_TARGET_PREV)
940 print_target_props (p_target->prev, i_mask);
941 }
942
943 /* Print the data base of files. */
944
945 void
946 print_prereqs (const struct dep *deps)
947 {
948 const struct dep *ood = 0;
949
950 /* Print all normal dependencies; note any order-only deps. */
951 for (; deps != 0; deps = deps->next)
952 if (! deps->ignore_mtime)
953 printf (" %s", dep_name (deps));
954 else if (! ood)
955 ood = deps;
956
957 /* Print order-only deps, if we have any. */
958 if (ood)
959 {
960 printf (" | %s", dep_name (ood));
961 for (ood = ood->next; ood != 0; ood = ood->next)
962 if (ood->ignore_mtime)
963 printf (" %s", dep_name (ood));
964 }
965
966 putchar ('\n');
967 }
968
969 static void
970 print_file (const void *item)
971 {
972 const struct file *f = item;
973
974 /* If we're not using builtin targets, don't show them.
975
976 Ideally we'd be able to delete them altogether but currently there's no
977 facility to ever delete a file once it's been added. */
978 if (no_builtin_rules_flag && f->builtin)
979 return;
980
981 putchar ('\n');
982
983 if (f->cmds && f->cmds->recipe_prefix != cmd_prefix)
984 {
985 fputs (".RECIPEPREFIX = ", stdout);
986 cmd_prefix = f->cmds->recipe_prefix;
987 if (cmd_prefix != RECIPEPREFIX_DEFAULT)
988 putchar (cmd_prefix);
989 putchar ('\n');
990 }
991
992 if (f->variables != 0)
993 print_target_variables (f);
994
995 if (!f->is_target)
996 puts (_("# Not a target:"));
997 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
998 print_prereqs (f->deps);
999
1000 if (f->precious)
1001 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1002 if (f->phony)
1003 puts (_("# Phony target (prerequisite of .PHONY)."));
1004 if (f->cmd_target)
1005 puts (_("# Command line target."));
1006 if (f->dontcare)
1007 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1008 if (f->builtin)
1009 puts (_("# Builtin rule"));
1010 puts (f->tried_implicit
1011 ? _("# Implicit rule search has been done.")
1012 : _("# Implicit rule search has not been done."));
1013 if (f->stem != 0)
1014 printf (_("# Implicit/static pattern stem: '%s'\n"), f->stem);
1015 if (f->intermediate)
1016 puts (_("# File is an intermediate prerequisite."));
1017 if (f->also_make != 0)
1018 {
1019 const struct dep *d;
1020 fputs (_("# Also makes:"), stdout);
1021 for (d = f->also_make; d != 0; d = d->next)
1022 printf (" %s", dep_name (d));
1023 putchar ('\n');
1024 }
1025 if (f->last_mtime == UNKNOWN_MTIME)
1026 puts (_("# Modification time never checked."));
1027 else if (f->last_mtime == NONEXISTENT_MTIME)
1028 puts (_("# File does not exist."));
1029 else if (f->last_mtime == OLD_MTIME)
1030 puts (_("# File is very old."));
1031 else
1032 {
1033 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1034 file_timestamp_sprintf (buf, f->last_mtime);
1035 printf (_("# Last modified %s\n"), buf);
1036 }
1037 puts (f->updated
1038 ? _("# File has been updated.") : _("# File has not been updated."));
1039 switch (f->command_state)
1040 {
1041 case cs_running:
1042 puts (_("# Recipe currently running (THIS IS A BUG)."));
1043 break;
1044 case cs_deps_running:
1045 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
1046 break;
1047 case cs_not_started:
1048 case cs_finished:
1049 switch (f->update_status)
1050 {
1051 case us_none:
1052 break;
1053 case us_success:
1054 puts (_("# Successfully updated."));
1055 break;
1056 case us_question:
1057 assert (question_flag);
1058 puts (_("# Needs to be updated (-q is set)."));
1059 break;
1060 case us_failed:
1061 puts (_("# Failed to be updated."));
1062 break;
1063 }
1064 break;
1065 default:
1066 puts (_("# Invalid value in 'command_state' member!"));
1067 fflush (stdout);
1068 fflush (stderr);
1069 abort ();
1070 }
1071
1072 if (f->variables != 0)
1073 print_file_variables (f, 1);
1074
1075 if (f->cmds != 0) {
1076 print_commands (NULL, f->cmds, false);
1077 // print_commands (p_target, p_target->cmds, false);
1078 }
1079
1080 if (f->prev)
1081 print_file ((const void *) f->prev);
1082 }
1083
1084 /*!
1085 Print the data base of files.
1086 */
1087 void
1088 print_target (const void *item)
1089 {
1090 file_t *p_target = (file_t *) item;
1091 print_target_props(p_target, PRINT_TARGET_ALL);
1092 }
1093
1094 void
1095 print_file_data_base (void)
1096 {
1097 puts (_("\n# Files"));
1098
1099 hash_map (&files, print_file);
1100
1101 fputs (_("\n# files hash-table stats:\n# "), stdout);
1102 hash_print_stats (&files, stdout);
1103 }
1104
1105 /* Verify the integrity of the data base of files. */
1106
1107 #define VERIFY_CACHED(_p,_n) \
1108 do{ \
1109 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1110 error (NULL, strlen (_p->name) + CSTRLEN (# _n) + strlen (_p->_n), \
1111 _("%s: Field '%s' not cached: %s"), _p->name, # _n, _p->_n); \
1112 }while(0)
1113
1114 static void
1115 verify_file (const void *item)
1116 {
1117 const struct file *f = item;
1118 const struct dep *d;
1119
1120 VERIFY_CACHED (f, name);
1121 VERIFY_CACHED (f, hname);
1122 VERIFY_CACHED (f, vpath);
1123 VERIFY_CACHED (f, stem);
1124
1125 /* Check the deps. */
1126 for (d = f->deps; d != 0; d = d->next)
1127 {
1128 if (! d->need_2nd_expansion)
1129 VERIFY_CACHED (d, name);
1130 VERIFY_CACHED (d, stem);
1131 }
1132 }
1133
1134 void
1135 verify_file_data_base (void)
1136 {
1137 hash_map (&files, verify_file);
1138 }
1139
1140 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1141
1142 char *
1143 build_target_list (char *value)
1144 {
1145 static unsigned long last_targ_count = 0;
1146
1147 if (files.ht_fill != last_targ_count)
1148 {
1149 size_t max = EXPANSION_INCREMENT (strlen (value));
1150 size_t len;
1151 char *p;
1152 struct file **fp = (struct file **) files.ht_vec;
1153 struct file **end = &fp[files.ht_size];
1154
1155 /* Make sure we have at least MAX bytes in the allocated buffer. */
1156 value = xrealloc (value, max);
1157
1158 p = value;
1159 len = 0;
1160 for (; fp < end; ++fp)
1161 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1162 {
1163 struct file *f = *fp;
1164 size_t l = strlen (f->name);
1165
1166 len += l + 1;
1167 if (len > max)
1168 {
1169 size_t off = p - value;
1170
1171 max += EXPANSION_INCREMENT (l + 1);
1172 value = xrealloc (value, max);
1173 p = &value[off];
1174 }
1175
1176 memcpy (p, f->name, l);
1177 p += l;
1178 *(p++) = ' ';
1179 }
1180 *(p-1) = '\0';
1181
1182 last_targ_count = files.ht_fill;
1183 }
1184
1185 return value;
1186 }
1187
1188 /* EOF */