Fix Savannah bugs # 15341, 15534, and 15533.
[make.git] / file.c
blob383f460d2ce1436f671e8d2b882ab41da4855db7
1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
23 #include <assert.h>
25 #include "dep.h"
26 #include "filedef.h"
27 #include "job.h"
28 #include "commands.h"
29 #include "variable.h"
30 #include "debug.h"
31 #include "hash.h"
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;
43 /* Hash table of files the makefile knows how to make. */
45 static unsigned long
46 file_hash_1 (const void *key)
48 return_ISTRING_HASH_1 (((struct file const *) key)->hname);
51 static unsigned long
52 file_hash_2 (const void *key)
54 return_ISTRING_HASH_2 (((struct file const *) key)->hname);
57 static int
58 file_hash_cmp (const void *x, const void *y)
60 return_ISTRING_COMPARE (((struct file const *) x)->hname,
61 ((struct file const *) y)->hname);
64 #ifndef FILE_BUCKETS
65 #define FILE_BUCKETS 1007
66 #endif
67 static struct hash_table files;
69 /* Whether or not .SECONDARY with no prerequisites was given. */
70 static int all_secondary = 0;
72 /* Access the hash table of all file records.
73 lookup_file given a name, return the struct file * for that name,
74 or nil if there is none.
75 enter_file similar, but create one if there is none. */
77 struct file *
78 lookup_file (char *name)
80 register struct file *f;
81 struct file file_key;
82 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
83 register char *lname, *ln;
84 #endif
86 assert (*name != '\0');
88 /* This is also done in parse_file_seq, so this is redundant
89 for names read from makefiles. It is here for names passed
90 on the command line. */
91 #ifdef VMS
92 # ifndef WANT_CASE_SENSITIVE_TARGETS
94 register char *n;
95 lname = (char *) malloc (strlen (name) + 1);
96 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
97 *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
98 *ln = '\0';
99 name = lname;
101 # endif
103 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
104 name += 2;
105 #endif
106 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
108 name += 2;
109 while (*name == '/')
110 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
111 ++name;
114 if (*name == '\0')
115 /* It was all slashes after a dot. */
116 #ifdef VMS
117 name = "[]";
118 #else
119 #ifdef _AMIGA
120 name = "";
121 #else
122 name = "./";
123 #endif /* AMIGA */
124 #endif /* VMS */
126 file_key.hname = name;
127 f = (struct file *) hash_find_item (&files, &file_key);
128 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
129 free (lname);
130 #endif
131 return f;
134 struct file *
135 enter_file (char *name)
137 register struct file *f;
138 register struct file *new;
139 register struct file **file_slot;
140 struct file file_key;
141 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
142 char *lname, *ln;
143 #endif
145 assert (*name != '\0');
147 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
149 register char *n;
150 lname = (char *) malloc (strlen (name) + 1);
151 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
153 if (isupper ((unsigned char)*n))
154 *ln = tolower ((unsigned char)*n);
155 else
156 *ln = *n;
159 *ln = 0;
160 /* Creates a possible leak, old value of name is unreachable, but I
161 currently don't know how to fix it. */
162 name = lname;
164 #endif
166 file_key.hname = name;
167 file_slot = (struct file **) hash_find_slot (&files, &file_key);
168 f = *file_slot;
169 if (! HASH_VACANT (f) && !f->double_colon)
171 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
172 free(lname);
173 #endif
174 return f;
177 new = (struct file *) xmalloc (sizeof (struct file));
178 bzero ((char *) new, sizeof (struct file));
179 new->name = new->hname = name;
180 new->update_status = -1;
182 if (HASH_VACANT (f))
184 new->last = new;
185 hash_insert_at (&files, new, file_slot);
187 else
189 /* There is already a double-colon entry for this file. */
190 new->double_colon = f;
191 f->last->prev = new;
192 f->last = new;
195 return new;
198 /* Rename FILE to NAME. This is not as simple as resetting
199 the `name' member, since it must be put in a new hash bucket,
200 and possibly merged with an existing file called NAME. */
202 void
203 rename_file (struct file *from_file, char *to_hname)
205 rehash_file (from_file, to_hname);
206 while (from_file)
208 from_file->name = from_file->hname;
209 from_file = from_file->prev;
213 /* Rehash FILE to NAME. This is not as simple as resetting
214 the `hname' member, since it must be put in a new hash bucket,
215 and possibly merged with an existing file called NAME. */
217 void
218 rehash_file (struct file *from_file, char *to_hname)
220 struct file file_key;
221 struct file **file_slot;
222 struct file *to_file;
223 struct file *deleted_file;
224 struct file *f;
226 file_key.hname = to_hname;
227 if (0 == file_hash_cmp (from_file, &file_key))
228 return;
230 file_key.hname = from_file->hname;
231 while (from_file->renamed != 0)
232 from_file = from_file->renamed;
233 if (file_hash_cmp (from_file, &file_key))
234 /* hname changed unexpectedly */
235 abort ();
237 deleted_file = hash_delete (&files, from_file);
238 if (deleted_file != from_file)
239 /* from_file isn't the one stored in files */
240 abort ();
242 file_key.hname = to_hname;
243 file_slot = (struct file **) hash_find_slot (&files, &file_key);
244 to_file = *file_slot;
246 from_file->hname = to_hname;
247 for (f = from_file->double_colon; f != 0; f = f->prev)
248 f->hname = to_hname;
250 if (HASH_VACANT (to_file))
251 hash_insert_at (&files, from_file, file_slot);
252 else
254 /* TO_FILE already exists under TO_HNAME.
255 We must retain TO_FILE and merge FROM_FILE into it. */
257 if (from_file->cmds != 0)
259 if (to_file->cmds == 0)
260 to_file->cmds = from_file->cmds;
261 else if (from_file->cmds != to_file->cmds)
263 /* We have two sets of commands. We will go with the
264 one given in the rule explicitly mentioning this name,
265 but give a message to let the user know what's going on. */
266 if (to_file->cmds->fileinfo.filenm != 0)
267 error (&from_file->cmds->fileinfo,
268 _("Commands were specified for file `%s' at %s:%lu,"),
269 from_file->name, to_file->cmds->fileinfo.filenm,
270 to_file->cmds->fileinfo.lineno);
271 else
272 error (&from_file->cmds->fileinfo,
273 _("Commands for file `%s' were found by implicit rule search,"),
274 from_file->name);
275 error (&from_file->cmds->fileinfo,
276 _("but `%s' is now considered the same file as `%s'."),
277 from_file->name, to_hname);
278 error (&from_file->cmds->fileinfo,
279 _("Commands for `%s' will be ignored in favor of those for `%s'."),
280 to_hname, from_file->name);
284 /* Merge the dependencies of the two files. */
286 if (to_file->deps == 0)
287 to_file->deps = from_file->deps;
288 else
290 register struct dep *deps = to_file->deps;
291 while (deps->next != 0)
292 deps = deps->next;
293 deps->next = from_file->deps;
296 merge_variable_set_lists (&to_file->variables, from_file->variables);
298 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
299 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
300 from_file->name, to_hname);
301 if (!to_file->double_colon && from_file->double_colon)
303 if (to_file->is_target)
304 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
305 from_file->name, to_hname);
306 else
307 to_file->double_colon = from_file->double_colon;
310 if (from_file->last_mtime > to_file->last_mtime)
311 /* %%% Kludge so -W wins on a file that gets vpathized. */
312 to_file->last_mtime = from_file->last_mtime;
314 to_file->mtime_before_update = from_file->mtime_before_update;
316 #define MERGE(field) to_file->field |= from_file->field
317 MERGE (precious);
318 MERGE (tried_implicit);
319 MERGE (updating);
320 MERGE (updated);
321 MERGE (is_target);
322 MERGE (cmd_target);
323 MERGE (phony);
324 MERGE (ignore_vpath);
325 #undef MERGE
327 from_file->renamed = to_file;
331 /* Remove all nonprecious intermediate files.
332 If SIG is nonzero, this was caused by a fatal signal,
333 meaning that a different message will be printed, and
334 the message will go to stderr rather than stdout. */
336 void
337 remove_intermediates (int sig)
339 register struct file **file_slot;
340 register struct file **file_end;
341 int doneany = 0;
343 /* If there's no way we will ever remove anything anyway, punt early. */
344 if (question_flag || touch_flag || all_secondary)
345 return;
347 if (sig && just_print_flag)
348 return;
350 file_slot = (struct file **) files.ht_vec;
351 file_end = file_slot + files.ht_size;
352 for ( ; file_slot < file_end; file_slot++)
353 if (! HASH_VACANT (*file_slot))
355 register struct file *f = *file_slot;
356 /* Is this file eligible for automatic deletion?
357 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
358 given on the command-line, and it's either a -include makefile or
359 it's not precious. */
360 if (f->intermediate && (f->dontcare || !f->precious)
361 && !f->secondary && !f->cmd_target)
363 int status;
364 if (f->update_status == -1)
365 /* If nothing would have created this file yet,
366 don't print an "rm" command for it. */
367 continue;
368 if (just_print_flag)
369 status = 0;
370 else
372 status = unlink (f->name);
373 if (status < 0 && errno == ENOENT)
374 continue;
376 if (!f->dontcare)
378 if (sig)
379 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
380 else
382 if (! doneany)
383 DB (DB_BASIC, (_("Removing intermediate files...\n")));
384 if (!silent_flag)
386 if (! doneany)
388 fputs ("rm ", stdout);
389 doneany = 1;
391 else
392 putchar (' ');
393 fputs (f->name, stdout);
394 fflush (stdout);
397 if (status < 0)
398 perror_with_name ("unlink: ", f->name);
403 if (doneany && !sig)
405 putchar ('\n');
406 fflush (stdout);
410 struct dep *
411 parse_prereqs (char *p)
413 struct dep *new = (struct dep *)
414 multi_glob (parse_file_seq (&p, '|', sizeof (struct dep), 1),
415 sizeof (struct dep));
417 if (*p)
419 /* Files that follow '|' are "order-only" prerequisites that satisfy the
420 dependency by existing: their modification times are irrelevant. */
421 struct dep *ood;
423 ++p;
424 ood = (struct dep *)
425 multi_glob (parse_file_seq (&p, '\0', sizeof (struct dep), 1),
426 sizeof (struct dep));
428 if (! new)
429 new = ood;
430 else
432 struct dep *dp;
433 for (dp = new; dp->next != NULL; dp = dp->next)
435 dp->next = ood;
438 for (; ood != NULL; ood = ood->next)
439 ood->ignore_mtime = 1;
442 return new;
446 /* Set the intermediate flag. */
448 static void
449 set_intermediate (const void *item)
451 struct file *f = (struct file *) item;
452 f->intermediate = 1;
455 /* Expand and parse each dependency line. */
456 static void
457 expand_deps (struct file *f)
459 struct dep *d;
460 struct dep *old = f->deps;
461 unsigned int last_dep_has_cmds = f->updating;
462 int initialized = 0;
464 f->updating = 0;
465 f->deps = 0;
467 for (d = old; d != 0; d = d->next)
469 struct dep *new, *d1;
470 char *p;
472 if (! d->name)
473 continue;
475 /* Create the dependency list.
476 If we're not doing 2nd expansion, then it's just the name. */
477 if (! d->need_2nd_expansion)
478 p = d->name;
479 else
481 /* If it's from a static pattern rule, convert the patterns into
482 "$*" so they'll expand properly. */
483 if (d->staticpattern)
485 char *o;
486 char *buffer = variable_expand ("");
488 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
490 free (d->name);
491 d->name = savestring (buffer, o - buffer);
492 d->staticpattern = 0;
495 /* We are going to do second expansion so initialize file variables
496 for the file. */
497 if (!initialized)
499 initialize_file_variables (f, 0);
500 initialized = 1;
503 set_file_variables (f);
505 p = variable_expand_for_file (d->name, f);
508 /* Parse the prerequisites. */
509 new = parse_prereqs (p);
511 /* If this dep list was from a static pattern rule, expand the %s. We
512 use patsubst_expand to translate the prerequisites' patterns into
513 plain prerequisite names. */
514 if (new && d->staticpattern)
516 char *pattern = "%";
517 char *buffer = variable_expand ("");
518 struct dep *dp = new, *dl = 0;
520 while (dp != 0)
522 char *percent = find_percent (dp->name);
523 if (percent)
525 /* We have to handle empty stems specially, because that
526 would be equivalent to $(patsubst %,dp->name,) which
527 will always be empty. */
528 if (f->stem[0] == '\0')
529 /* This needs memmove() in ISO C. */
530 bcopy (percent+1, percent, strlen (percent));
531 else
533 char *o = patsubst_expand (buffer, f->stem, pattern,
534 dp->name, pattern+1,
535 percent+1);
536 if (o == buffer)
537 dp->name[0] = '\0';
538 else
540 free (dp->name);
541 dp->name = savestring (buffer, o - buffer);
545 /* If the name expanded to the empty string, ignore it. */
546 if (dp->name[0] == '\0')
548 struct dep *df = dp;
549 if (dp == new)
550 dp = new = new->next;
551 else
552 dp = dl->next = dp->next;
553 free ((char *)df);
554 continue;
557 dl = dp;
558 dp = dp->next;
562 /* Enter them as files. */
563 for (d1 = new; d1 != 0; d1 = d1->next)
565 d1->file = lookup_file (d1->name);
566 if (d1->file == 0)
567 d1->file = enter_file (d1->name);
568 else
569 free (d1->name);
570 d1->name = 0;
571 d1->staticpattern = 0;
572 d1->need_2nd_expansion = 0;
575 /* Add newly parsed deps to f->deps. If this is the last dependency
576 line and this target has commands then put it in front so the
577 last dependency line (the one with commands) ends up being the
578 first. This is important because people expect $< to hold first
579 prerequisite from the rule with commands. If it is not the last
580 dependency line or the rule does not have commands then link it
581 at the end so it appears in makefile order. */
583 if (new != 0)
585 if (d->next == 0 && last_dep_has_cmds)
587 struct dep **d_ptr;
588 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
591 *d_ptr = f->deps;
592 f->deps = new;
594 else
596 struct dep **d_ptr;
597 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
600 *d_ptr = new;
605 free_ns_chain ((struct nameseq *) old);
608 /* For each dependency of each file, make the `struct dep' point
609 at the appropriate `struct file' (which may have to be created).
611 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
612 and various other special targets. */
614 void
615 snap_deps (void)
617 struct file *f;
618 struct file *f2;
619 struct dep *d;
620 struct file **file_slot_0;
621 struct file **file_slot;
622 struct file **file_end;
624 /* Perform second expansion and enter each dependency
625 name as a file. */
627 /* Expand .SUFFIXES first; it's dependencies are used for
628 $$* calculation. */
629 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
630 expand_deps (f);
632 /* We must use hash_dump (), because within this loop
633 we might add new files to the table, possibly causing
634 an in-situ table expansion. */
635 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
636 file_end = file_slot_0 + files.ht_fill;
637 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
638 for (f = *file_slot; f != 0; f = f->prev)
640 if (strcmp (f->name, ".SUFFIXES") != 0)
641 expand_deps (f);
643 free (file_slot_0);
645 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
646 for (d = f->deps; d != 0; d = d->next)
647 for (f2 = d->file; f2 != 0; f2 = f2->prev)
648 f2->precious = 1;
650 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
651 for (d = f->deps; d != 0; d = d->next)
652 for (f2 = d->file; f2 != 0; f2 = f2->prev)
653 f2->low_resolution_time = 1;
655 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
656 for (d = f->deps; d != 0; d = d->next)
657 for (f2 = d->file; f2 != 0; f2 = f2->prev)
659 /* Mark this file as phony nonexistent target. */
660 f2->phony = 1;
661 f2->is_target = 1;
662 f2->last_mtime = NONEXISTENT_MTIME;
663 f2->mtime_before_update = NONEXISTENT_MTIME;
666 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
668 /* .INTERMEDIATE with deps listed
669 marks those deps as intermediate files. */
670 for (d = f->deps; d != 0; d = d->next)
671 for (f2 = d->file; f2 != 0; f2 = f2->prev)
672 f2->intermediate = 1;
673 /* .INTERMEDIATE with no deps does nothing.
674 Marking all files as intermediates is useless
675 since the goal targets would be deleted after they are built. */
678 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
680 /* .SECONDARY with deps listed
681 marks those deps as intermediate files
682 in that they don't get rebuilt if not actually needed;
683 but unlike real intermediate files,
684 these are not deleted after make finishes. */
685 if (f->deps)
686 for (d = f->deps; d != 0; d = d->next)
687 for (f2 = d->file; f2 != 0; f2 = f2->prev)
688 f2->intermediate = f2->secondary = 1;
689 /* .SECONDARY with no deps listed marks *all* files that way. */
690 else
692 all_secondary = 1;
693 hash_map (&files, set_intermediate);
697 f = lookup_file (".EXPORT_ALL_VARIABLES");
698 if (f != 0 && f->is_target)
699 export_all_variables = 1;
701 f = lookup_file (".IGNORE");
702 if (f != 0 && f->is_target)
704 if (f->deps == 0)
705 ignore_errors_flag = 1;
706 else
707 for (d = f->deps; d != 0; d = d->next)
708 for (f2 = d->file; f2 != 0; f2 = f2->prev)
709 f2->command_flags |= COMMANDS_NOERROR;
712 f = lookup_file (".SILENT");
713 if (f != 0 && f->is_target)
715 if (f->deps == 0)
716 silent_flag = 1;
717 else
718 for (d = f->deps; d != 0; d = d->next)
719 for (f2 = d->file; f2 != 0; f2 = f2->prev)
720 f2->command_flags |= COMMANDS_SILENT;
723 f = lookup_file (".NOTPARALLEL");
724 if (f != 0 && f->is_target)
725 not_parallel = 1;
727 #ifndef NO_MINUS_C_MINUS_O
728 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
729 /* This needs more work: what if the user sets this in the makefile?
730 if (posix_pedantic)
731 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
733 #endif
735 /* Remember that we've done this. */
736 snapped_deps = 1;
739 /* Set the `command_state' member of FILE and all its `also_make's. */
741 void
742 set_command_state (struct file *file, enum cmd_state state)
744 struct dep *d;
746 file->command_state = state;
748 for (d = file->also_make; d != 0; d = d->next)
749 d->file->command_state = state;
752 /* Convert an external file timestamp to internal form. */
754 FILE_TIMESTAMP
755 file_timestamp_cons (const char *fname, time_t s, int ns)
757 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
758 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
759 FILE_TIMESTAMP ts = product + offset;
761 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
762 && product <= ts && ts <= ORDINARY_MTIME_MAX))
764 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
765 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
766 file_timestamp_sprintf (buf, ts);
767 error (NILF, _("%s: Timestamp out of range; substituting %s"),
768 fname ? fname : _("Current time"), buf);
771 return ts;
774 /* Return the current time as a file timestamp, setting *RESOLUTION to
775 its resolution. */
776 FILE_TIMESTAMP
777 file_timestamp_now (int *resolution)
779 int r;
780 time_t s;
781 int ns;
783 /* Don't bother with high-resolution clocks if file timestamps have
784 only one-second resolution. The code below should work, but it's
785 not worth the hassle of debugging it on hosts where it fails. */
786 #if FILE_TIMESTAMP_HI_RES
787 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
789 struct timespec timespec;
790 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
792 r = 1;
793 s = timespec.tv_sec;
794 ns = timespec.tv_nsec;
795 goto got_time;
798 # endif
799 # if HAVE_GETTIMEOFDAY
801 struct timeval timeval;
802 if (gettimeofday (&timeval, 0) == 0)
804 r = 1000;
805 s = timeval.tv_sec;
806 ns = timeval.tv_usec * 1000;
807 goto got_time;
810 # endif
811 #endif
813 r = 1000000000;
814 s = time ((time_t *) 0);
815 ns = 0;
817 #if FILE_TIMESTAMP_HI_RES
818 got_time:
819 #endif
820 *resolution = r;
821 return file_timestamp_cons (0, s, ns);
824 /* Place into the buffer P a printable representation of the file
825 timestamp TS. */
826 void
827 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
829 time_t t = FILE_TIMESTAMP_S (ts);
830 struct tm *tm = localtime (&t);
832 if (tm)
833 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
834 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
835 tm->tm_hour, tm->tm_min, tm->tm_sec);
836 else if (t < 0)
837 sprintf (p, "%ld", (long) t);
838 else
839 sprintf (p, "%lu", (unsigned long) t);
840 p += strlen (p);
842 /* Append nanoseconds as a fraction, but remove trailing zeros.
843 We don't know the actual timestamp resolution, since clock_getres
844 applies only to local times, whereas this timestamp might come
845 from a remote filesystem. So removing trailing zeros is the
846 best guess that we can do. */
847 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
848 p += strlen (p) - 1;
849 while (*p == '0')
850 p--;
851 p += *p != '.';
853 *p = '\0';
856 /* Print the data base of files. */
858 static void
859 print_file (const void *item)
861 struct file *f = (struct file *) item;
862 struct dep *d;
863 struct dep *ood = 0;
865 putchar ('\n');
866 if (!f->is_target)
867 puts (_("# Not a target:"));
868 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
870 /* Print all normal dependencies; note any order-only deps. */
871 for (d = f->deps; d != 0; d = d->next)
872 if (! d->ignore_mtime)
873 printf (" %s", dep_name (d));
874 else if (! ood)
875 ood = d;
877 /* Print order-only deps, if we have any. */
878 if (ood)
880 printf (" | %s", dep_name (ood));
881 for (d = ood->next; d != 0; d = d->next)
882 if (d->ignore_mtime)
883 printf (" %s", dep_name (d));
886 putchar ('\n');
888 if (f->precious)
889 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
890 if (f->phony)
891 puts (_("# Phony target (prerequisite of .PHONY)."));
892 if (f->cmd_target)
893 puts (_("# Command-line target."));
894 if (f->dontcare)
895 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
896 puts (f->tried_implicit
897 ? _("# Implicit rule search has been done.")
898 : _("# Implicit rule search has not been done."));
899 if (f->stem != 0)
900 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
901 if (f->intermediate)
902 puts (_("# File is an intermediate prerequisite."));
903 if (f->also_make != 0)
905 fputs (_("# Also makes:"), stdout);
906 for (d = f->also_make; d != 0; d = d->next)
907 printf (" %s", dep_name (d));
908 putchar ('\n');
910 if (f->last_mtime == UNKNOWN_MTIME)
911 puts (_("# Modification time never checked."));
912 else if (f->last_mtime == NONEXISTENT_MTIME)
913 puts (_("# File does not exist."));
914 else if (f->last_mtime == OLD_MTIME)
915 puts (_("# File is very old."));
916 else
918 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
919 file_timestamp_sprintf (buf, f->last_mtime);
920 printf (_("# Last modified %s\n"), buf);
922 puts (f->updated
923 ? _("# File has been updated.") : _("# File has not been updated."));
924 switch (f->command_state)
926 case cs_running:
927 puts (_("# Commands currently running (THIS IS A BUG)."));
928 break;
929 case cs_deps_running:
930 puts (_("# Dependencies commands running (THIS IS A BUG)."));
931 break;
932 case cs_not_started:
933 case cs_finished:
934 switch (f->update_status)
936 case -1:
937 break;
938 case 0:
939 puts (_("# Successfully updated."));
940 break;
941 case 1:
942 assert (question_flag);
943 puts (_("# Needs to be updated (-q is set)."));
944 break;
945 case 2:
946 puts (_("# Failed to be updated."));
947 break;
948 default:
949 puts (_("# Invalid value in `update_status' member!"));
950 fflush (stdout);
951 fflush (stderr);
952 abort ();
954 break;
955 default:
956 puts (_("# Invalid value in `command_state' member!"));
957 fflush (stdout);
958 fflush (stderr);
959 abort ();
962 if (f->variables != 0)
963 print_file_variables (f);
965 if (f->cmds != 0)
966 print_commands (f->cmds);
968 if (f->prev)
969 print_file ((const void *) f->prev);
972 void
973 print_file_data_base (void)
975 puts (_("\n# Files"));
977 hash_map (&files, print_file);
979 fputs (_("\n# files hash-table stats:\n# "), stdout);
980 hash_print_stats (&files, stdout);
983 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
985 char *
986 build_target_list (char *value)
988 static unsigned long last_targ_count = 0;
990 if (files.ht_fill != last_targ_count)
992 unsigned long max = EXPANSION_INCREMENT (strlen (value));
993 unsigned long len;
994 char *p;
995 struct file **fp = (struct file **) files.ht_vec;
996 struct file **end = &fp[files.ht_size];
998 /* Make sure we have at least MAX bytes in the allocated buffer. */
999 value = xrealloc (value, max);
1001 p = value;
1002 len = 0;
1003 for (; fp < end; ++fp)
1004 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1006 struct file *f = *fp;
1007 int l = strlen (f->name);
1009 len += l + 1;
1010 if (len > max)
1012 unsigned long off = p - value;
1014 max += EXPANSION_INCREMENT (l + 1);
1015 value = xrealloc (value, max);
1016 p = &value[off];
1019 bcopy (f->name, p, l);
1020 p += l;
1021 *(p++) = ' ';
1023 *(p-1) = '\0';
1025 last_targ_count = files.ht_fill;
1028 return value;
1031 void
1032 init_hash_files (void)
1034 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1037 /* EOF */