Various changes getting ready for the release of 3.81.
[make.git] / file.c
blobddec34d00c7a8f8685d56076c01cd8f47459aaf1
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))
183 hash_insert_at (&files, new, file_slot);
184 else
186 /* There is already a double-colon entry for this file. */
187 new->double_colon = f;
188 while (f->prev != 0)
189 f = f->prev;
190 f->prev = new;
193 return new;
196 /* Rename FILE to NAME. This is not as simple as resetting
197 the `name' member, since it must be put in a new hash bucket,
198 and possibly merged with an existing file called NAME. */
200 void
201 rename_file (struct file *from_file, char *to_hname)
203 rehash_file (from_file, to_hname);
204 while (from_file)
206 from_file->name = from_file->hname;
207 from_file = from_file->prev;
211 /* Rehash FILE to NAME. This is not as simple as resetting
212 the `hname' member, since it must be put in a new hash bucket,
213 and possibly merged with an existing file called NAME. */
215 void
216 rehash_file (struct file *from_file, char *to_hname)
218 struct file file_key;
219 struct file **file_slot;
220 struct file *to_file;
221 struct file *deleted_file;
222 struct file *f;
224 file_key.hname = to_hname;
225 if (0 == file_hash_cmp (from_file, &file_key))
226 return;
228 file_key.hname = from_file->hname;
229 while (from_file->renamed != 0)
230 from_file = from_file->renamed;
231 if (file_hash_cmp (from_file, &file_key))
232 /* hname changed unexpectedly */
233 abort ();
235 deleted_file = hash_delete (&files, from_file);
236 if (deleted_file != from_file)
237 /* from_file isn't the one stored in files */
238 abort ();
240 file_key.hname = to_hname;
241 file_slot = (struct file **) hash_find_slot (&files, &file_key);
242 to_file = *file_slot;
244 from_file->hname = to_hname;
245 for (f = from_file->double_colon; f != 0; f = f->prev)
246 f->hname = to_hname;
248 if (HASH_VACANT (to_file))
249 hash_insert_at (&files, from_file, file_slot);
250 else
252 /* TO_FILE already exists under TO_HNAME.
253 We must retain TO_FILE and merge FROM_FILE into it. */
255 if (from_file->cmds != 0)
257 if (to_file->cmds == 0)
258 to_file->cmds = from_file->cmds;
259 else if (from_file->cmds != to_file->cmds)
261 /* We have two sets of commands. We will go with the
262 one given in the rule explicitly mentioning this name,
263 but give a message to let the user know what's going on. */
264 if (to_file->cmds->fileinfo.filenm != 0)
265 error (&from_file->cmds->fileinfo,
266 _("Commands were specified for file `%s' at %s:%lu,"),
267 from_file->name, to_file->cmds->fileinfo.filenm,
268 to_file->cmds->fileinfo.lineno);
269 else
270 error (&from_file->cmds->fileinfo,
271 _("Commands for file `%s' were found by implicit rule search,"),
272 from_file->name);
273 error (&from_file->cmds->fileinfo,
274 _("but `%s' is now considered the same file as `%s'."),
275 from_file->name, to_hname);
276 error (&from_file->cmds->fileinfo,
277 _("Commands for `%s' will be ignored in favor of those for `%s'."),
278 to_hname, from_file->name);
282 /* Merge the dependencies of the two files. */
284 if (to_file->deps == 0)
285 to_file->deps = from_file->deps;
286 else
288 register struct dep *deps = to_file->deps;
289 while (deps->next != 0)
290 deps = deps->next;
291 deps->next = from_file->deps;
294 merge_variable_set_lists (&to_file->variables, from_file->variables);
296 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
297 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
298 from_file->name, to_hname);
299 if (!to_file->double_colon && from_file->double_colon)
301 if (to_file->is_target)
302 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
303 from_file->name, to_hname);
304 else
305 to_file->double_colon = from_file->double_colon;
308 if (from_file->last_mtime > to_file->last_mtime)
309 /* %%% Kludge so -W wins on a file that gets vpathized. */
310 to_file->last_mtime = from_file->last_mtime;
312 to_file->mtime_before_update = from_file->mtime_before_update;
314 #define MERGE(field) to_file->field |= from_file->field
315 MERGE (precious);
316 MERGE (tried_implicit);
317 MERGE (updating);
318 MERGE (updated);
319 MERGE (is_target);
320 MERGE (cmd_target);
321 MERGE (phony);
322 MERGE (ignore_vpath);
323 #undef MERGE
325 from_file->renamed = to_file;
329 /* Remove all nonprecious intermediate files.
330 If SIG is nonzero, this was caused by a fatal signal,
331 meaning that a different message will be printed, and
332 the message will go to stderr rather than stdout. */
334 void
335 remove_intermediates (int sig)
337 register struct file **file_slot;
338 register struct file **file_end;
339 int doneany = 0;
341 /* If there's no way we will ever remove anything anyway, punt early. */
342 if (question_flag || touch_flag || all_secondary)
343 return;
345 if (sig && just_print_flag)
346 return;
348 file_slot = (struct file **) files.ht_vec;
349 file_end = file_slot + files.ht_size;
350 for ( ; file_slot < file_end; file_slot++)
351 if (! HASH_VACANT (*file_slot))
353 register struct file *f = *file_slot;
354 /* Is this file eligible for automatic deletion?
355 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
356 given on the command-line, and it's either a -include makefile or
357 it's not precious. */
358 if (f->intermediate && (f->dontcare || !f->precious)
359 && !f->secondary && !f->cmd_target)
361 int status;
362 if (f->update_status == -1)
363 /* If nothing would have created this file yet,
364 don't print an "rm" command for it. */
365 continue;
366 if (just_print_flag)
367 status = 0;
368 else
370 status = unlink (f->name);
371 if (status < 0 && errno == ENOENT)
372 continue;
374 if (!f->dontcare)
376 if (sig)
377 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
378 else
380 if (! doneany)
381 DB (DB_BASIC, (_("Removing intermediate files...\n")));
382 if (!silent_flag)
384 if (! doneany)
386 fputs ("rm ", stdout);
387 doneany = 1;
389 else
390 putchar (' ');
391 fputs (f->name, stdout);
392 fflush (stdout);
395 if (status < 0)
396 perror_with_name ("unlink: ", f->name);
401 if (doneany && !sig)
403 putchar ('\n');
404 fflush (stdout);
408 struct dep *
409 parse_prereqs (char *p)
411 struct dep *new = (struct dep *)
412 multi_glob (parse_file_seq (&p, '|', sizeof (struct dep), 1),
413 sizeof (struct dep));
415 if (*p)
417 /* Files that follow '|' are "order-only" prerequisites that satisfy the
418 dependency by existing: their modification times are irrelevant. */
419 struct dep *ood;
421 ++p;
422 ood = (struct dep *)
423 multi_glob (parse_file_seq (&p, '\0', sizeof (struct dep), 1),
424 sizeof (struct dep));
426 if (! new)
427 new = ood;
428 else
430 struct dep *dp;
431 for (dp = new; dp->next != NULL; dp = dp->next)
433 dp->next = ood;
436 for (; ood != NULL; ood = ood->next)
437 ood->ignore_mtime = 1;
440 return new;
444 /* Set the intermediate flag. */
446 static void
447 set_intermediate (const void *item)
449 struct file *f = (struct file *) item;
450 f->intermediate = 1;
453 /* Expand and parse each dependency line. */
454 static void
455 expand_deps (struct file *f)
457 struct dep *d;
458 struct dep *old = f->deps;
459 unsigned int last_dep_has_cmds = f->updating;
460 int initialized = 0;
462 f->updating = 0;
463 f->deps = 0;
465 for (d = old; d != 0; d = d->next)
467 struct dep *new, *d1;
468 char *p;
470 if (! d->name)
471 continue;
473 /* Create the dependency list.
474 If we're not doing 2nd expansion, then it's just the name. */
475 if (! d->need_2nd_expansion)
476 p = d->name;
477 else
479 /* If it's from a static pattern rule, convert the patterns into
480 "$*" so they'll expand properly. */
481 if (d->staticpattern)
483 char *o;
484 char *buffer = variable_expand ("");
486 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
488 free (d->name);
489 d->name = savestring (buffer, o - buffer);
490 d->staticpattern = 0;
493 /* We are going to do second expansion so initialize file variables
494 for the file. */
495 if (!initialized)
497 initialize_file_variables (f, 0);
498 initialized = 1;
501 set_file_variables (f);
503 p = variable_expand_for_file (d->name, f);
506 /* Parse the prerequisites. */
507 new = parse_prereqs (p);
509 /* If this dep list was from a static pattern rule, expand the %s. We
510 use patsubst_expand to translate the prerequisites' patterns into
511 plain prerequisite names. */
512 if (new && d->staticpattern)
514 char *pattern = "%";
515 char *buffer = variable_expand ("");
516 struct dep *dp = new, *dl = 0;
518 while (dp != 0)
520 char *percent = find_percent (dp->name);
521 if (percent)
523 /* We have to handle empty stems specially, because that
524 would be equivalent to $(patsubst %,dp->name,) which
525 will always be empty. */
526 if (f->stem[0] == '\0')
527 /* This needs memmove() in ISO C. */
528 bcopy (percent+1, percent, strlen (percent));
529 else
531 char *o = patsubst_expand (buffer, f->stem, pattern,
532 dp->name, pattern+1,
533 percent+1);
534 if (o == buffer)
535 dp->name[0] = '\0';
536 else
538 free (dp->name);
539 dp->name = savestring (buffer, o - buffer);
543 /* If the name expanded to the empty string, ignore it. */
544 if (dp->name[0] == '\0')
546 struct dep *df = dp;
547 if (dp == new)
548 dp = new = new->next;
549 else
550 dp = dl->next = dp->next;
551 free ((char *)df);
552 continue;
555 dl = dp;
556 dp = dp->next;
560 /* Enter them as files. */
561 for (d1 = new; d1 != 0; d1 = d1->next)
563 d1->file = lookup_file (d1->name);
564 if (d1->file == 0)
565 d1->file = enter_file (d1->name);
566 else
567 free (d1->name);
568 d1->name = 0;
569 d1->staticpattern = 0;
570 d1->need_2nd_expansion = 0;
573 /* Add newly parsed deps to f->deps. If this is the last dependency
574 line and this target has commands then put it in front so the
575 last dependency line (the one with commands) ends up being the
576 first. This is important because people expect $< to hold first
577 prerequisite from the rule with commands. If it is not the last
578 dependency line or the rule does not have commands then link it
579 at the end so it appears in makefile order. */
581 if (new != 0)
583 if (d->next == 0 && last_dep_has_cmds)
585 struct dep **d_ptr;
586 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
589 *d_ptr = f->deps;
590 f->deps = new;
592 else
594 struct dep **d_ptr;
595 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
598 *d_ptr = new;
603 free_ns_chain ((struct nameseq *) old);
606 /* For each dependency of each file, make the `struct dep' point
607 at the appropriate `struct file' (which may have to be created).
609 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
610 and various other special targets. */
612 void
613 snap_deps (void)
615 struct file *f;
616 struct file *f2;
617 struct dep *d;
618 struct file **file_slot_0;
619 struct file **file_slot;
620 struct file **file_end;
622 /* Perform second expansion and enter each dependency
623 name as a file. */
625 /* Expand .SUFFIXES first; it's dependencies are used for
626 $$* calculation. */
627 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
628 expand_deps (f);
630 /* We must use hash_dump (), because within this loop
631 we might add new files to the table, possibly causing
632 an in-situ table expansion. */
633 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
634 file_end = file_slot_0 + files.ht_fill;
635 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
636 for (f = *file_slot; f != 0; f = f->prev)
638 if (strcmp (f->name, ".SUFFIXES") != 0)
639 expand_deps (f);
641 free (file_slot_0);
643 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
644 for (d = f->deps; d != 0; d = d->next)
645 for (f2 = d->file; f2 != 0; f2 = f2->prev)
646 f2->precious = 1;
648 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
649 for (d = f->deps; d != 0; d = d->next)
650 for (f2 = d->file; f2 != 0; f2 = f2->prev)
651 f2->low_resolution_time = 1;
653 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
654 for (d = f->deps; d != 0; d = d->next)
655 for (f2 = d->file; f2 != 0; f2 = f2->prev)
657 /* Mark this file as phony nonexistent target. */
658 f2->phony = 1;
659 f2->is_target = 1;
660 f2->last_mtime = NONEXISTENT_MTIME;
661 f2->mtime_before_update = NONEXISTENT_MTIME;
664 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
666 /* .INTERMEDIATE with deps listed
667 marks those deps as intermediate files. */
668 for (d = f->deps; d != 0; d = d->next)
669 for (f2 = d->file; f2 != 0; f2 = f2->prev)
670 f2->intermediate = 1;
671 /* .INTERMEDIATE with no deps does nothing.
672 Marking all files as intermediates is useless
673 since the goal targets would be deleted after they are built. */
676 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
678 /* .SECONDARY with deps listed
679 marks those deps as intermediate files
680 in that they don't get rebuilt if not actually needed;
681 but unlike real intermediate files,
682 these are not deleted after make finishes. */
683 if (f->deps)
684 for (d = f->deps; d != 0; d = d->next)
685 for (f2 = d->file; f2 != 0; f2 = f2->prev)
686 f2->intermediate = f2->secondary = 1;
687 /* .SECONDARY with no deps listed marks *all* files that way. */
688 else
690 all_secondary = 1;
691 hash_map (&files, set_intermediate);
695 f = lookup_file (".EXPORT_ALL_VARIABLES");
696 if (f != 0 && f->is_target)
697 export_all_variables = 1;
699 f = lookup_file (".IGNORE");
700 if (f != 0 && f->is_target)
702 if (f->deps == 0)
703 ignore_errors_flag = 1;
704 else
705 for (d = f->deps; d != 0; d = d->next)
706 for (f2 = d->file; f2 != 0; f2 = f2->prev)
707 f2->command_flags |= COMMANDS_NOERROR;
710 f = lookup_file (".SILENT");
711 if (f != 0 && f->is_target)
713 if (f->deps == 0)
714 silent_flag = 1;
715 else
716 for (d = f->deps; d != 0; d = d->next)
717 for (f2 = d->file; f2 != 0; f2 = f2->prev)
718 f2->command_flags |= COMMANDS_SILENT;
721 f = lookup_file (".POSIX");
722 if (f != 0 && f->is_target)
723 posix_pedantic = 1;
725 f = lookup_file (".NOTPARALLEL");
726 if (f != 0 && f->is_target)
727 not_parallel = 1;
729 /* Remember that we've done this. */
730 snapped_deps = 1;
733 /* Set the `command_state' member of FILE and all its `also_make's. */
735 void
736 set_command_state (struct file *file, enum cmd_state state)
738 struct dep *d;
740 file->command_state = state;
742 for (d = file->also_make; d != 0; d = d->next)
743 d->file->command_state = state;
746 /* Convert an external file timestamp to internal form. */
748 FILE_TIMESTAMP
749 file_timestamp_cons (const char *fname, time_t s, int ns)
751 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
752 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
753 FILE_TIMESTAMP ts = product + offset;
755 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
756 && product <= ts && ts <= ORDINARY_MTIME_MAX))
758 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
759 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
760 file_timestamp_sprintf (buf, ts);
761 error (NILF, _("%s: Timestamp out of range; substituting %s"),
762 fname ? fname : _("Current time"), buf);
765 return ts;
768 /* Return the current time as a file timestamp, setting *RESOLUTION to
769 its resolution. */
770 FILE_TIMESTAMP
771 file_timestamp_now (int *resolution)
773 int r;
774 time_t s;
775 int ns;
777 /* Don't bother with high-resolution clocks if file timestamps have
778 only one-second resolution. The code below should work, but it's
779 not worth the hassle of debugging it on hosts where it fails. */
780 #if FILE_TIMESTAMP_HI_RES
781 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
783 struct timespec timespec;
784 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
786 r = 1;
787 s = timespec.tv_sec;
788 ns = timespec.tv_nsec;
789 goto got_time;
792 # endif
793 # if HAVE_GETTIMEOFDAY
795 struct timeval timeval;
796 if (gettimeofday (&timeval, 0) == 0)
798 r = 1000;
799 s = timeval.tv_sec;
800 ns = timeval.tv_usec * 1000;
801 goto got_time;
804 # endif
805 #endif
807 r = 1000000000;
808 s = time ((time_t *) 0);
809 ns = 0;
811 #if FILE_TIMESTAMP_HI_RES
812 got_time:
813 #endif
814 *resolution = r;
815 return file_timestamp_cons (0, s, ns);
818 /* Place into the buffer P a printable representation of the file
819 timestamp TS. */
820 void
821 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
823 time_t t = FILE_TIMESTAMP_S (ts);
824 struct tm *tm = localtime (&t);
826 if (tm)
827 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
828 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
829 tm->tm_hour, tm->tm_min, tm->tm_sec);
830 else if (t < 0)
831 sprintf (p, "%ld", (long) t);
832 else
833 sprintf (p, "%lu", (unsigned long) t);
834 p += strlen (p);
836 /* Append nanoseconds as a fraction, but remove trailing zeros.
837 We don't know the actual timestamp resolution, since clock_getres
838 applies only to local times, whereas this timestamp might come
839 from a remote filesystem. So removing trailing zeros is the
840 best guess that we can do. */
841 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
842 p += strlen (p) - 1;
843 while (*p == '0')
844 p--;
845 p += *p != '.';
847 *p = '\0';
850 /* Print the data base of files. */
852 static void
853 print_file (const void *item)
855 struct file *f = (struct file *) item;
856 struct dep *d;
857 struct dep *ood = 0;
859 putchar ('\n');
860 if (!f->is_target)
861 puts (_("# Not a target:"));
862 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
864 /* Print all normal dependencies; note any order-only deps. */
865 for (d = f->deps; d != 0; d = d->next)
866 if (! d->ignore_mtime)
867 printf (" %s", dep_name (d));
868 else if (! ood)
869 ood = d;
871 /* Print order-only deps, if we have any. */
872 if (ood)
874 printf (" | %s", dep_name (ood));
875 for (d = ood->next; d != 0; d = d->next)
876 if (d->ignore_mtime)
877 printf (" %s", dep_name (d));
880 putchar ('\n');
882 if (f->precious)
883 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
884 if (f->phony)
885 puts (_("# Phony target (prerequisite of .PHONY)."));
886 if (f->cmd_target)
887 puts (_("# Command-line target."));
888 if (f->dontcare)
889 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
890 puts (f->tried_implicit
891 ? _("# Implicit rule search has been done.")
892 : _("# Implicit rule search has not been done."));
893 if (f->stem != 0)
894 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
895 if (f->intermediate)
896 puts (_("# File is an intermediate prerequisite."));
897 if (f->also_make != 0)
899 fputs (_("# Also makes:"), stdout);
900 for (d = f->also_make; d != 0; d = d->next)
901 printf (" %s", dep_name (d));
902 putchar ('\n');
904 if (f->last_mtime == UNKNOWN_MTIME)
905 puts (_("# Modification time never checked."));
906 else if (f->last_mtime == NONEXISTENT_MTIME)
907 puts (_("# File does not exist."));
908 else if (f->last_mtime == OLD_MTIME)
909 puts (_("# File is very old."));
910 else
912 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
913 file_timestamp_sprintf (buf, f->last_mtime);
914 printf (_("# Last modified %s\n"), buf);
916 puts (f->updated
917 ? _("# File has been updated.") : _("# File has not been updated."));
918 switch (f->command_state)
920 case cs_running:
921 puts (_("# Commands currently running (THIS IS A BUG)."));
922 break;
923 case cs_deps_running:
924 puts (_("# Dependencies commands running (THIS IS A BUG)."));
925 break;
926 case cs_not_started:
927 case cs_finished:
928 switch (f->update_status)
930 case -1:
931 break;
932 case 0:
933 puts (_("# Successfully updated."));
934 break;
935 case 1:
936 assert (question_flag);
937 puts (_("# Needs to be updated (-q is set)."));
938 break;
939 case 2:
940 puts (_("# Failed to be updated."));
941 break;
942 default:
943 puts (_("# Invalid value in `update_status' member!"));
944 fflush (stdout);
945 fflush (stderr);
946 abort ();
948 break;
949 default:
950 puts (_("# Invalid value in `command_state' member!"));
951 fflush (stdout);
952 fflush (stderr);
953 abort ();
956 if (f->variables != 0)
957 print_file_variables (f);
959 if (f->cmds != 0)
960 print_commands (f->cmds);
962 if (f->prev)
963 print_file ((const void *) f->prev);
966 void
967 print_file_data_base (void)
969 puts (_("\n# Files"));
971 hash_map (&files, print_file);
973 fputs (_("\n# files hash-table stats:\n# "), stdout);
974 hash_print_stats (&files, stdout);
977 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
979 char *
980 build_target_list (char *value)
982 static unsigned long last_targ_count = 0;
984 if (files.ht_fill != last_targ_count)
986 unsigned long max = EXPANSION_INCREMENT (strlen (value));
987 unsigned long len;
988 char *p;
989 struct file **fp = (struct file **) files.ht_vec;
990 struct file **end = &fp[files.ht_size];
992 /* Make sure we have at least MAX bytes in the allocated buffer. */
993 value = xrealloc (value, max);
995 p = value;
996 len = 0;
997 for (; fp < end; ++fp)
998 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1000 struct file *f = *fp;
1001 int l = strlen (f->name);
1003 len += l + 1;
1004 if (len > max)
1006 unsigned long off = p - value;
1008 max += EXPANSION_INCREMENT (l + 1);
1009 value = xrealloc (value, max);
1010 p = &value[off];
1013 bcopy (f->name, p, l);
1014 p += l;
1015 *(p++) = ' ';
1017 *(p-1) = '\0';
1019 last_targ_count = files.ht_fill;
1022 return value;
1025 void
1026 init_hash_files (void)
1028 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1031 /* EOF */