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