Update the required version of gettext to get new bugfix macros.
[make.git] / file.c
blob896f7e223059902f82dae19d9fce42bfa5415cfa
1 /* Target file management for GNU Make.
2 Copyright (C) 1988-2012 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify 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.
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.
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/>. */
17 #include "make.h"
19 #include <assert.h>
21 #include "dep.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "variable.h"
26 #include "debug.h"
27 #include "hash.h"
30 /* Remember whether snap_deps has been invoked: we need this to be sure we
31 don't add new rules (via $(eval ...)) afterwards. In the future it would
32 be nice to support this, but it means we'd need to re-run snap_deps() or
33 at least its functionality... it might mean changing snap_deps() to be run
34 per-file, so we can invoke it after the eval... or remembering which files
35 in the hash have been snapped (a new boolean flag?) and having snap_deps()
36 only work on files which have not yet been snapped. */
37 int snapped_deps = 0;
39 /* Hash table of files the makefile knows how to make. */
41 static unsigned long
42 file_hash_1 (const void *key)
44 return_ISTRING_HASH_1 (((struct file const *) key)->hname);
47 static unsigned long
48 file_hash_2 (const void *key)
50 return_ISTRING_HASH_2 (((struct file const *) key)->hname);
53 static int
54 file_hash_cmp (const void *x, const void *y)
56 return_ISTRING_COMPARE (((struct file const *) x)->hname,
57 ((struct file const *) y)->hname);
60 #ifndef FILE_BUCKETS
61 #define FILE_BUCKETS 1007
62 #endif
63 static struct hash_table files;
65 /* Whether or not .SECONDARY with no prerequisites was given. */
66 static int all_secondary = 0;
68 /* Access the hash table of all file records.
69 lookup_file given a name, return the struct file * for that name,
70 or nil if there is none.
73 struct file *
74 lookup_file (const char *name)
76 struct file *f;
77 struct file file_key;
78 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
79 char *lname;
80 #endif
82 assert (*name != '\0');
84 /* This is also done in parse_file_seq, so this is redundant
85 for names read from makefiles. It is here for names passed
86 on the command line. */
87 #ifdef VMS
88 # ifndef WANT_CASE_SENSITIVE_TARGETS
89 if (*name != '.')
91 const char *n;
92 char *ln;
93 lname = xstrdup (name);
94 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
95 *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
96 *ln = '\0';
97 name = lname;
99 # endif
101 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
102 name += 2;
103 #endif
104 while (name[0] == '.'
105 #ifdef HAVE_DOS_PATHS
106 && (name[1] == '/' || name[1] == '\\')
107 #else
108 && name[1] == '/'
109 #endif
110 && name[2] != '\0')
112 name += 2;
113 while (*name == '/'
114 #ifdef HAVE_DOS_PATHS
115 || *name == '\\'
116 #endif
118 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
119 ++name;
122 if (*name == '\0')
123 /* It was all slashes after a dot. */
124 #if defined(VMS)
125 name = "[]";
126 #elif defined(_AMIGA)
127 name = "";
128 #else
129 name = "./";
130 #endif
132 file_key.hname = name;
133 f = hash_find_item (&files, &file_key);
134 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
135 if (*name != '.')
136 free (lname);
137 #endif
139 return f;
142 /* Look up a file record for file NAME and return it.
143 Create a new record if one doesn't exist. NAME will be stored in the
144 new record so it should be constant or in the strcache etc.
147 struct file *
148 enter_file (const char *name)
150 struct file *f;
151 struct file *new;
152 struct file **file_slot;
153 struct file file_key;
155 assert (*name != '\0');
156 assert (strcache_iscached (name));
158 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
159 if (*name != '.')
161 const char *n;
162 char *lname, *ln;
163 lname = xstrdup (name);
164 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
165 if (isupper ((unsigned char)*n))
166 *ln = tolower ((unsigned char)*n);
167 else
168 *ln = *n;
170 *ln = '\0';
171 name = strcache_add (lname);
172 free (lname);
174 #endif
176 file_key.hname = name;
177 file_slot = (struct file **) hash_find_slot (&files, &file_key);
178 f = *file_slot;
179 if (! HASH_VACANT (f) && !f->double_colon)
180 return f;
182 new = xcalloc (sizeof (struct file));
183 new->name = new->hname = name;
184 new->update_status = -1;
186 if (HASH_VACANT (f))
188 new->last = new;
189 hash_insert_at (&files, new, file_slot);
191 else
193 /* There is already a double-colon entry for this file. */
194 new->double_colon = f;
195 f->last->prev = new;
196 f->last = new;
199 return new;
202 /* Rehash FILE to NAME. This is not as simple as resetting
203 the 'hname' member, since it must be put in a new hash bucket,
204 and possibly merged with an existing file called NAME. */
206 void
207 rehash_file (struct file *from_file, const char *to_hname)
209 struct file file_key;
210 struct file **file_slot;
211 struct file *to_file;
212 struct file *deleted_file;
213 struct file *f;
215 /* If it's already that name, we're done. */
216 file_key.hname = to_hname;
217 if (! file_hash_cmp (from_file, &file_key))
218 return;
220 /* Find the end of the renamed list for the "from" file. */
221 file_key.hname = from_file->hname;
222 while (from_file->renamed != 0)
223 from_file = from_file->renamed;
224 if (file_hash_cmp (from_file, &file_key))
225 /* hname changed unexpectedly!! */
226 abort ();
228 /* Remove the "from" file from the hash. */
229 deleted_file = hash_delete (&files, from_file);
230 if (deleted_file != from_file)
231 /* from_file isn't the one stored in files */
232 abort ();
234 /* Find where the newly renamed file will go in the hash. */
235 file_key.hname = to_hname;
236 file_slot = (struct file **) hash_find_slot (&files, &file_key);
237 to_file = *file_slot;
239 /* Change the hash name for this file. */
240 from_file->hname = to_hname;
241 for (f = from_file->double_colon; f != 0; f = f->prev)
242 f->hname = to_hname;
244 /* If the new name doesn't exist yet just set it to the renamed file. */
245 if (HASH_VACANT (to_file))
247 hash_insert_at (&files, from_file, file_slot);
248 return;
251 /* TO_FILE already exists under TO_HNAME.
252 We must retain TO_FILE and merge FROM_FILE into it. */
254 if (from_file->cmds != 0)
256 if (to_file->cmds == 0)
257 to_file->cmds = from_file->cmds;
258 else if (from_file->cmds != to_file->cmds)
260 /* We have two sets of commands. We will go with the
261 one given in the rule explicitly mentioning this name,
262 but give a message to let the user know what's going on. */
263 if (to_file->cmds->fileinfo.filenm != 0)
264 error (&from_file->cmds->fileinfo,
265 _("Recipe was specified for file '%s' at %s:%lu,"),
266 from_file->name, to_file->cmds->fileinfo.filenm,
267 to_file->cmds->fileinfo.lineno);
268 else
269 error (&from_file->cmds->fileinfo,
270 _("Recipe for file '%s' was found by implicit rule search,"),
271 from_file->name);
272 error (&from_file->cmds->fileinfo,
273 _("but '%s' is now considered the same file as '%s'."),
274 from_file->name, to_hname);
275 error (&from_file->cmds->fileinfo,
276 _("Recipe for '%s' will be ignored in favor of the one for '%s'."),
277 to_hname, from_file->name);
281 /* Merge the dependencies of the two files. */
283 if (to_file->deps == 0)
284 to_file->deps = from_file->deps;
285 else
287 struct dep *deps = to_file->deps;
288 while (deps->next != 0)
289 deps = deps->next;
290 deps->next = from_file->deps;
293 merge_variable_set_lists (&to_file->variables, from_file->variables);
295 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
296 fatal (NILF, _("can't rename single-colon '%s' to double-colon '%s'"),
297 from_file->name, to_hname);
298 if (!to_file->double_colon && from_file->double_colon)
300 if (to_file->is_target)
301 fatal (NILF, _("can't rename double-colon '%s' to single-colon '%s'"),
302 from_file->name, to_hname);
303 else
304 to_file->double_colon = from_file->double_colon;
307 if (from_file->last_mtime > to_file->last_mtime)
308 /* %%% Kludge so -W wins on a file that gets vpathized. */
309 to_file->last_mtime = from_file->last_mtime;
311 to_file->mtime_before_update = from_file->mtime_before_update;
313 #define MERGE(field) to_file->field |= from_file->field
314 MERGE (precious);
315 MERGE (tried_implicit);
316 MERGE (updating);
317 MERGE (updated);
318 MERGE (is_target);
319 MERGE (cmd_target);
320 MERGE (phony);
321 MERGE (ignore_vpath);
322 #undef MERGE
324 from_file->renamed = to_file;
327 /* Rename FILE to NAME. This is not as simple as resetting
328 the 'name' member, since it must be put in a new hash bucket,
329 and possibly merged with an existing file called NAME. */
331 void
332 rename_file (struct file *from_file, const char *to_hname)
334 rehash_file (from_file, to_hname);
335 while (from_file)
337 from_file->name = from_file->hname;
338 from_file = from_file->prev;
342 /* Remove all nonprecious intermediate files.
343 If SIG is nonzero, this was caused by a fatal signal,
344 meaning that a different message will be printed, and
345 the message will go to stderr rather than stdout. */
347 void
348 remove_intermediates (int sig)
350 struct file **file_slot;
351 struct file **file_end;
352 int doneany = 0;
354 /* If there's no way we will ever remove anything anyway, punt early. */
355 if (question_flag || touch_flag || all_secondary)
356 return;
358 if (sig && just_print_flag)
359 return;
361 file_slot = (struct file **) files.ht_vec;
362 file_end = file_slot + files.ht_size;
363 for ( ; file_slot < file_end; file_slot++)
364 if (! HASH_VACANT (*file_slot))
366 struct file *f = *file_slot;
367 /* Is this file eligible for automatic deletion?
368 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
369 given on the command line, and it's either a -include makefile or
370 it's not precious. */
371 if (f->intermediate && (f->dontcare || !f->precious)
372 && !f->secondary && !f->cmd_target)
374 int status;
375 if (f->update_status == -1)
376 /* If nothing would have created this file yet,
377 don't print an "rm" command for it. */
378 continue;
379 if (just_print_flag)
380 status = 0;
381 else
383 status = unlink (f->name);
384 if (status < 0 && errno == ENOENT)
385 continue;
387 if (!f->dontcare)
389 if (sig)
390 error (NILF, _("*** Deleting intermediate file '%s'"), f->name);
391 else
393 if (! doneany)
394 DB (DB_BASIC, (_("Removing intermediate files...\n")));
395 if (!silent_flag)
397 if (! doneany)
399 fputs ("rm ", stdout);
400 doneany = 1;
402 else
403 putchar (' ');
404 fputs (f->name, stdout);
405 fflush (stdout);
408 if (status < 0)
409 perror_with_name ("unlink: ", f->name);
414 if (doneany && !sig)
416 putchar ('\n');
417 fflush (stdout);
421 /* Given a string containing prerequisites (fully expanded), break it up into
422 a struct dep list. Enter each of these prereqs into the file database.
424 struct dep *
425 split_prereqs (char *p)
427 struct dep *new = PARSE_FILE_SEQ (&p, struct dep, '|', NULL, 0);
429 if (*p)
431 /* Files that follow '|' are "order-only" prerequisites that satisfy the
432 dependency by existing: their modification times are irrelevant. */
433 struct dep *ood;
435 ++p;
436 ood = PARSE_FILE_SEQ (&p, struct dep, '\0', NULL, 0);
438 if (! new)
439 new = ood;
440 else
442 struct dep *dp;
443 for (dp = new; dp->next != NULL; dp = dp->next)
445 dp->next = ood;
448 for (; ood != NULL; ood = ood->next)
449 ood->ignore_mtime = 1;
452 return new;
455 /* Given a list of prerequisites, enter them into the file database.
456 If STEM is set then first expand patterns using STEM. */
457 struct dep *
458 enter_prereqs (struct dep *deps, const char *stem)
460 struct dep *d1;
462 if (deps == 0)
463 return 0;
465 /* If we have a stem, expand the %'s. We use patsubst_expand to translate
466 the prerequisites' patterns into plain prerequisite names. */
467 if (stem)
469 const char *pattern = "%";
470 char *buffer = variable_expand ("");
471 struct dep *dp = deps, *dl = 0;
473 while (dp != 0)
475 char *percent;
476 int nl = strlen (dp->name) + 1;
477 char *nm = alloca (nl);
478 memcpy (nm, dp->name, nl);
479 percent = find_percent (nm);
480 if (percent)
482 char *o;
484 /* We have to handle empty stems specially, because that
485 would be equivalent to $(patsubst %,dp->name,) which
486 will always be empty. */
487 if (stem[0] == '\0')
489 memmove (percent, percent+1, strlen (percent));
490 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
492 else
493 o = patsubst_expand_pat (buffer, stem, pattern, nm,
494 pattern+1, percent+1);
496 /* If the name expanded to the empty string, ignore it. */
497 if (buffer[0] == '\0')
499 struct dep *df = dp;
500 if (dp == deps)
501 dp = deps = deps->next;
502 else
503 dp = dl->next = dp->next;
504 free_dep (df);
505 continue;
508 /* Save the name. */
509 dp->name = strcache_add_len (buffer, o - buffer);
511 dp->stem = stem;
512 dp->staticpattern = 1;
513 dl = dp;
514 dp = dp->next;
518 /* Enter them as files, unless they need a 2nd expansion. */
519 for (d1 = deps; d1 != 0; d1 = d1->next)
521 if (d1->need_2nd_expansion)
522 continue;
524 d1->file = lookup_file (d1->name);
525 if (d1->file == 0)
526 d1->file = enter_file (d1->name);
527 d1->staticpattern = 0;
528 d1->name = 0;
531 return deps;
534 /* Set the intermediate flag. */
536 static void
537 set_intermediate (const void *item)
539 struct file *f = (struct file *) item;
540 f->intermediate = 1;
543 /* Expand and parse each dependency line. */
544 static void
545 expand_deps (struct file *f)
547 struct dep *d;
548 struct dep **dp;
549 const char *file_stem = f->stem;
550 int initialized = 0;
552 f->updating = 0;
554 /* Walk through the dependencies. For any dependency that needs 2nd
555 expansion, expand it then insert the result into the list. */
556 dp = &f->deps;
557 d = f->deps;
558 while (d != 0)
560 char *p;
561 struct dep *new, *next;
562 char *name = (char *)d->name;
564 if (! d->name || ! d->need_2nd_expansion)
566 /* This one is all set already. */
567 dp = &d->next;
568 d = d->next;
569 continue;
572 /* If it's from a static pattern rule, convert the patterns into
573 "$*" so they'll expand properly. */
574 if (d->staticpattern)
576 char *o = variable_expand ("");
577 o = subst_expand (o, name, "%", "$*", 1, 2, 0);
578 *o = '\0';
579 free (name);
580 d->name = name = xstrdup (variable_buffer);
581 d->staticpattern = 0;
584 /* We're going to do second expansion so initialize file variables for
585 the file. Since the stem for static pattern rules comes from
586 individual dep lines, we will temporarily set f->stem to d->stem. */
587 if (!initialized)
589 initialize_file_variables (f, 0);
590 initialized = 1;
593 if (d->stem != 0)
594 f->stem = d->stem;
596 set_file_variables (f);
598 p = variable_expand_for_file (d->name, f);
600 if (d->stem != 0)
601 f->stem = file_stem;
603 /* At this point we don't need the name anymore: free it. */
604 free (name);
606 /* Parse the prerequisites and enter them into the file database. */
607 new = enter_prereqs (split_prereqs (p), d->stem);
609 /* If there were no prereqs here (blank!) then throw this one out. */
610 if (new == 0)
612 *dp = d->next;
613 free_dep (d);
614 d = *dp;
615 continue;
618 /* Add newly parsed prerequisites. */
619 next = d->next;
620 *dp = new;
621 for (dp = &new->next, d = new->next; d != 0; dp = &d->next, d = d->next)
623 *dp = next;
624 d = *dp;
628 /* Reset the updating flag. */
630 static void
631 reset_updating (const void *item)
633 struct file *f = (struct file *) item;
634 f->updating = 0;
637 /* For each dependency of each file, make the 'struct dep' point
638 at the appropriate 'struct file' (which may have to be created).
640 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
641 and various other special targets. */
643 void
644 snap_deps (void)
646 struct file *f;
647 struct file *f2;
648 struct dep *d;
650 /* Remember that we've done this. Once we start snapping deps we can no
651 longer define new targets. */
652 snapped_deps = 1;
654 /* Perform second expansion and enter each dependency name as a file. We
655 must use hash_dump() here because within these loops we likely add new
656 files to the table, possibly causing an in-situ table expansion.
658 We only need to do this if second_expansion has been defined; if it
659 hasn't then all deps were expanded as the makefile was read in. If we
660 ever change make to be able to unset .SECONDARY_EXPANSION this will have
661 to change. */
663 if (second_expansion)
665 struct file **file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
666 struct file **file_end = file_slot_0 + files.ht_fill;
667 struct file **file_slot;
668 const char *suffixes;
670 /* Expand .SUFFIXES: its prerequisites are used for $$* calc. */
671 f = lookup_file (".SUFFIXES");
672 suffixes = f ? f->name : 0;
673 for (; f != 0; f = f->prev)
674 expand_deps (f);
676 /* For every target that's not .SUFFIXES, expand its prerequisites. */
678 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
679 for (f = *file_slot; f != 0; f = f->prev)
680 if (f->name != suffixes)
681 expand_deps (f);
682 free (file_slot_0);
684 else
685 /* We're not doing second expansion, so reset updating. */
686 hash_map (&files, reset_updating);
688 /* Now manage all the special targets. */
690 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
691 for (d = f->deps; d != 0; d = d->next)
692 for (f2 = d->file; f2 != 0; f2 = f2->prev)
693 f2->precious = 1;
695 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
696 for (d = f->deps; d != 0; d = d->next)
697 for (f2 = d->file; f2 != 0; f2 = f2->prev)
698 f2->low_resolution_time = 1;
700 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
701 for (d = f->deps; d != 0; d = d->next)
702 for (f2 = d->file; f2 != 0; f2 = f2->prev)
704 /* Mark this file as phony nonexistent target. */
705 f2->phony = 1;
706 f2->is_target = 1;
707 f2->last_mtime = NONEXISTENT_MTIME;
708 f2->mtime_before_update = NONEXISTENT_MTIME;
711 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
712 /* Mark .INTERMEDIATE deps as intermediate files. */
713 for (d = f->deps; d != 0; d = d->next)
714 for (f2 = d->file; f2 != 0; f2 = f2->prev)
715 f2->intermediate = 1;
716 /* .INTERMEDIATE with no deps does nothing.
717 Marking all files as intermediates is useless since the goal targets
718 would be deleted after they are built. */
720 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
721 /* Mark .SECONDARY deps as both intermediate and secondary. */
722 if (f->deps)
723 for (d = f->deps; d != 0; d = d->next)
724 for (f2 = d->file; f2 != 0; f2 = f2->prev)
725 f2->intermediate = f2->secondary = 1;
726 /* .SECONDARY with no deps listed marks *all* files that way. */
727 else
729 all_secondary = 1;
730 hash_map (&files, set_intermediate);
733 f = lookup_file (".EXPORT_ALL_VARIABLES");
734 if (f != 0 && f->is_target)
735 export_all_variables = 1;
737 f = lookup_file (".IGNORE");
738 if (f != 0 && f->is_target)
740 if (f->deps == 0)
741 ignore_errors_flag = 1;
742 else
743 for (d = f->deps; d != 0; d = d->next)
744 for (f2 = d->file; f2 != 0; f2 = f2->prev)
745 f2->command_flags |= COMMANDS_NOERROR;
748 f = lookup_file (".SILENT");
749 if (f != 0 && f->is_target)
751 if (f->deps == 0)
752 silent_flag = 1;
753 else
754 for (d = f->deps; d != 0; d = d->next)
755 for (f2 = d->file; f2 != 0; f2 = f2->prev)
756 f2->command_flags |= COMMANDS_SILENT;
759 f = lookup_file (".NOTPARALLEL");
760 if (f != 0 && f->is_target)
761 not_parallel = 1;
763 #ifndef NO_MINUS_C_MINUS_O
764 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
765 /* This needs more work: what if the user sets this in the makefile?
766 if (posix_pedantic)
767 define_variable_cname ("OUTPUT_OPTION", "", o_default, 1);
769 #endif
772 /* Set the 'command_state' member of FILE and all its 'also_make's. */
774 void
775 set_command_state (struct file *file, enum cmd_state state)
777 struct dep *d;
779 file->command_state = state;
781 for (d = file->also_make; d != 0; d = d->next)
782 d->file->command_state = state;
785 /* Convert an external file timestamp to internal form. */
787 FILE_TIMESTAMP
788 file_timestamp_cons (const char *fname, time_t stamp, long int ns)
790 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
791 FILE_TIMESTAMP s = stamp;
792 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
793 FILE_TIMESTAMP ts = product + offset;
795 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
796 && product <= ts && ts <= ORDINARY_MTIME_MAX))
798 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
799 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
800 file_timestamp_sprintf (buf, ts);
801 error (NILF, _("%s: Timestamp out of range; substituting %s"),
802 fname ? fname : _("Current time"), buf);
805 return ts;
808 /* Return the current time as a file timestamp, setting *RESOLUTION to
809 its resolution. */
810 FILE_TIMESTAMP
811 file_timestamp_now (int *resolution)
813 int r;
814 time_t s;
815 int ns;
817 /* Don't bother with high-resolution clocks if file timestamps have
818 only one-second resolution. The code below should work, but it's
819 not worth the hassle of debugging it on hosts where it fails. */
820 #if FILE_TIMESTAMP_HI_RES
821 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
823 struct timespec timespec;
824 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
826 r = 1;
827 s = timespec.tv_sec;
828 ns = timespec.tv_nsec;
829 goto got_time;
832 # endif
833 # if HAVE_GETTIMEOFDAY
835 struct timeval timeval;
836 if (gettimeofday (&timeval, 0) == 0)
838 r = 1000;
839 s = timeval.tv_sec;
840 ns = timeval.tv_usec * 1000;
841 goto got_time;
844 # endif
845 #endif
847 r = 1000000000;
848 s = time ((time_t *) 0);
849 ns = 0;
851 #if FILE_TIMESTAMP_HI_RES
852 got_time:
853 #endif
854 *resolution = r;
855 return file_timestamp_cons (0, s, ns);
858 /* Place into the buffer P a printable representation of the file
859 timestamp TS. */
860 void
861 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
863 time_t t = FILE_TIMESTAMP_S (ts);
864 struct tm *tm = localtime (&t);
866 if (tm)
867 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
868 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
869 tm->tm_hour, tm->tm_min, tm->tm_sec);
870 else if (t < 0)
871 sprintf (p, "%ld", (long) t);
872 else
873 sprintf (p, "%lu", (unsigned long) t);
874 p += strlen (p);
876 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
877 know the actual timestamp resolution, since clock_getres applies only to
878 local times, whereas this timestamp might come from a remote filesystem.
879 So removing trailing zeros is the best guess that we can do. */
880 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
881 p += strlen (p) - 1;
882 while (*p == '0')
883 p--;
884 p += *p != '.';
886 *p = '\0';
889 /* Print the data base of files. */
891 void
892 print_prereqs (const struct dep *deps)
894 const struct dep *ood = 0;
896 /* Print all normal dependencies; note any order-only deps. */
897 for (; deps != 0; deps = deps->next)
898 if (! deps->ignore_mtime)
899 printf (" %s", dep_name (deps));
900 else if (! ood)
901 ood = deps;
903 /* Print order-only deps, if we have any. */
904 if (ood)
906 printf (" | %s", dep_name (ood));
907 for (ood = ood->next; ood != 0; ood = ood->next)
908 if (ood->ignore_mtime)
909 printf (" %s", dep_name (ood));
912 putchar ('\n');
915 static void
916 print_file (const void *item)
918 const struct file *f = item;
920 putchar ('\n');
922 if (f->cmds && f->cmds->recipe_prefix != cmd_prefix)
924 fputs (".RECIPEPREFIX = ", stdout);
925 cmd_prefix = f->cmds->recipe_prefix;
926 if (cmd_prefix != RECIPEPREFIX_DEFAULT)
927 putchar (cmd_prefix);
928 putchar ('\n');
931 if (f->variables != 0)
932 print_target_variables (f);
934 if (!f->is_target)
935 puts (_("# Not a target:"));
936 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
937 print_prereqs (f->deps);
939 if (f->precious)
940 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
941 if (f->phony)
942 puts (_("# Phony target (prerequisite of .PHONY)."));
943 if (f->cmd_target)
944 puts (_("# Command line target."));
945 if (f->dontcare)
946 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
947 puts (f->tried_implicit
948 ? _("# Implicit rule search has been done.")
949 : _("# Implicit rule search has not been done."));
950 if (f->stem != 0)
951 printf (_("# Implicit/static pattern stem: '%s'\n"), f->stem);
952 if (f->intermediate)
953 puts (_("# File is an intermediate prerequisite."));
954 if (f->also_make != 0)
956 const struct dep *d;
957 fputs (_("# Also makes:"), stdout);
958 for (d = f->also_make; d != 0; d = d->next)
959 printf (" %s", dep_name (d));
960 putchar ('\n');
962 if (f->last_mtime == UNKNOWN_MTIME)
963 puts (_("# Modification time never checked."));
964 else if (f->last_mtime == NONEXISTENT_MTIME)
965 puts (_("# File does not exist."));
966 else if (f->last_mtime == OLD_MTIME)
967 puts (_("# File is very old."));
968 else
970 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
971 file_timestamp_sprintf (buf, f->last_mtime);
972 printf (_("# Last modified %s\n"), buf);
974 puts (f->updated
975 ? _("# File has been updated.") : _("# File has not been updated."));
976 switch (f->command_state)
978 case cs_running:
979 puts (_("# Recipe currently running (THIS IS A BUG)."));
980 break;
981 case cs_deps_running:
982 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
983 break;
984 case cs_not_started:
985 case cs_finished:
986 switch (f->update_status)
988 case -1:
989 break;
990 case 0:
991 puts (_("# Successfully updated."));
992 break;
993 case 1:
994 assert (question_flag);
995 puts (_("# Needs to be updated (-q is set)."));
996 break;
997 case 2:
998 puts (_("# Failed to be updated."));
999 break;
1000 default:
1001 puts (_("# Invalid value in 'update_status' member!"));
1002 fflush (stdout);
1003 fflush (stderr);
1004 abort ();
1006 break;
1007 default:
1008 puts (_("# Invalid value in 'command_state' member!"));
1009 fflush (stdout);
1010 fflush (stderr);
1011 abort ();
1014 if (f->variables != 0)
1015 print_file_variables (f);
1017 if (f->cmds != 0)
1018 print_commands (f->cmds);
1020 if (f->prev)
1021 print_file ((const void *) f->prev);
1024 void
1025 print_file_data_base (void)
1027 puts (_("\n# Files"));
1029 hash_map (&files, print_file);
1031 fputs (_("\n# files hash-table stats:\n# "), stdout);
1032 hash_print_stats (&files, stdout);
1035 /* Verify the integrity of the data base of files. */
1037 #define VERIFY_CACHED(_p,_n) \
1038 do{\
1039 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1040 error (NULL, "%s: Field '%s' not cached: %s\n", _p->name, # _n, _p->_n); \
1041 }while(0)
1043 static void
1044 verify_file (const void *item)
1046 const struct file *f = item;
1047 const struct dep *d;
1049 VERIFY_CACHED (f, name);
1050 VERIFY_CACHED (f, hname);
1051 VERIFY_CACHED (f, vpath);
1052 VERIFY_CACHED (f, stem);
1054 /* Check the deps. */
1055 for (d = f->deps; d != 0; d = d->next)
1057 if (! d->need_2nd_expansion)
1058 VERIFY_CACHED (d, name);
1059 VERIFY_CACHED (d, stem);
1063 void
1064 verify_file_data_base (void)
1066 hash_map (&files, verify_file);
1069 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1071 char *
1072 build_target_list (char *value)
1074 static unsigned long last_targ_count = 0;
1076 if (files.ht_fill != last_targ_count)
1078 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1079 unsigned long len;
1080 char *p;
1081 struct file **fp = (struct file **) files.ht_vec;
1082 struct file **end = &fp[files.ht_size];
1084 /* Make sure we have at least MAX bytes in the allocated buffer. */
1085 value = xrealloc (value, max);
1087 p = value;
1088 len = 0;
1089 for (; fp < end; ++fp)
1090 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1092 struct file *f = *fp;
1093 int l = strlen (f->name);
1095 len += l + 1;
1096 if (len > max)
1098 unsigned long off = p - value;
1100 max += EXPANSION_INCREMENT (l + 1);
1101 value = xrealloc (value, max);
1102 p = &value[off];
1105 memcpy (p, f->name, l);
1106 p += l;
1107 *(p++) = ' ';
1109 *(p-1) = '\0';
1111 last_targ_count = files.ht_fill;
1114 return value;
1117 void
1118 init_hash_files (void)
1120 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1123 /* EOF */