- Fix Savannah bug #27093
[make.git] / file.c
bloba618bebc65e54fbb85da5241716c781d0ef68db8
1 /* Target file management for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
21 #include <assert.h>
23 #include "dep.h"
24 #include "filedef.h"
25 #include "job.h"
26 #include "commands.h"
27 #include "variable.h"
28 #include "debug.h"
29 #include "hash.h"
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;
41 /* Hash table of files the makefile knows how to make. */
43 static unsigned long
44 file_hash_1 (const void *key)
46 return_ISTRING_HASH_1 (((struct file const *) key)->hname);
49 static unsigned long
50 file_hash_2 (const void *key)
52 return_ISTRING_HASH_2 (((struct file const *) key)->hname);
55 static int
56 file_hash_cmp (const void *x, const void *y)
58 return_ISTRING_COMPARE (((struct file const *) x)->hname,
59 ((struct file const *) y)->hname);
62 #ifndef FILE_BUCKETS
63 #define FILE_BUCKETS 1007
64 #endif
65 static struct hash_table files;
67 /* Whether or not .SECONDARY with no prerequisites was given. */
68 static int all_secondary = 0;
70 /* Access the hash table of all file records.
71 lookup_file given a name, return the struct file * for that name,
72 or nil if there is none.
75 struct file *
76 lookup_file (const char *name)
78 struct file *f;
79 struct file file_key;
80 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
81 char *lname;
82 #endif
84 assert (*name != '\0');
86 /* This is also done in parse_file_seq, so this is redundant
87 for names read from makefiles. It is here for names passed
88 on the command line. */
89 #ifdef VMS
90 # ifndef WANT_CASE_SENSITIVE_TARGETS
91 if (*name != '.')
93 const char *n;
94 char *ln;
95 lname = xstrdup (name);
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 #if defined(VMS)
117 name = "[]";
118 #elif defined(_AMIGA)
119 name = "";
120 #else
121 name = "./";
122 #endif
124 file_key.hname = name;
125 f = hash_find_item (&files, &file_key);
126 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
127 if (*name != '.')
128 free (lname);
129 #endif
131 return f;
134 /* Look up a file record for file NAME and return it.
135 Create a new record if one doesn't exist. NAME will be stored in the
136 new record so it should be constant or in the strcache etc.
139 struct file *
140 enter_file (const char *name)
142 struct file *f;
143 struct file *new;
144 struct file **file_slot;
145 struct file file_key;
147 assert (*name != '\0');
148 assert (strcache_iscached (name));
150 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
151 if (*name != '.')
153 const char *n;
154 char *lname, *ln;
155 lname = xstrdup (name);
156 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
157 if (isupper ((unsigned char)*n))
158 *ln = tolower ((unsigned char)*n);
159 else
160 *ln = *n;
162 *ln = '\0';
163 name = strcache_add (lname);
164 free (lname);
166 #endif
168 file_key.hname = name;
169 file_slot = (struct file **) hash_find_slot (&files, &file_key);
170 f = *file_slot;
171 if (! HASH_VACANT (f) && !f->double_colon)
172 return f;
174 new = xmalloc (sizeof (struct file));
175 memset (new, '\0', sizeof (struct file));
176 new->name = new->hname = name;
177 new->update_status = -1;
179 if (HASH_VACANT (f))
181 new->last = new;
182 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 f->last->prev = new;
189 f->last = new;
192 return new;
195 /* Rehash FILE to NAME. This is not as simple as resetting
196 the `hname' member, since it must be put in a new hash bucket,
197 and possibly merged with an existing file called NAME. */
199 void
200 rehash_file (struct file *from_file, const char *to_hname)
202 struct file file_key;
203 struct file **file_slot;
204 struct file *to_file;
205 struct file *deleted_file;
206 struct file *f;
208 /* If it's already that name, we're done. */
209 file_key.hname = to_hname;
210 if (! file_hash_cmp (from_file, &file_key))
211 return;
213 /* Find the end of the renamed list for the "from" file. */
214 file_key.hname = from_file->hname;
215 while (from_file->renamed != 0)
216 from_file = from_file->renamed;
217 if (file_hash_cmp (from_file, &file_key))
218 /* hname changed unexpectedly!! */
219 abort ();
221 /* Remove the "from" file from the hash. */
222 deleted_file = hash_delete (&files, from_file);
223 if (deleted_file != from_file)
224 /* from_file isn't the one stored in files */
225 abort ();
227 /* Find where the newly renamed file will go in the hash. */
228 file_key.hname = to_hname;
229 file_slot = (struct file **) hash_find_slot (&files, &file_key);
230 to_file = *file_slot;
232 /* Change the hash name for this file. */
233 from_file->hname = to_hname;
234 for (f = from_file->double_colon; f != 0; f = f->prev)
235 f->hname = to_hname;
237 /* If the new name doesn't exist yet just set it to the renamed file. */
238 if (HASH_VACANT (to_file))
240 hash_insert_at (&files, from_file, file_slot);
241 return;
244 /* TO_FILE already exists under TO_HNAME.
245 We must retain TO_FILE and merge FROM_FILE into it. */
247 if (from_file->cmds != 0)
249 if (to_file->cmds == 0)
250 to_file->cmds = from_file->cmds;
251 else if (from_file->cmds != to_file->cmds)
253 /* We have two sets of commands. We will go with the
254 one given in the rule explicitly mentioning this name,
255 but give a message to let the user know what's going on. */
256 if (to_file->cmds->fileinfo.filenm != 0)
257 error (&from_file->cmds->fileinfo,
258 _("Recipe was specified for file `%s' at %s:%lu,"),
259 from_file->name, to_file->cmds->fileinfo.filenm,
260 to_file->cmds->fileinfo.lineno);
261 else
262 error (&from_file->cmds->fileinfo,
263 _("Recipe for file `%s' was found by implicit rule search,"),
264 from_file->name);
265 error (&from_file->cmds->fileinfo,
266 _("but `%s' is now considered the same file as `%s'."),
267 from_file->name, to_hname);
268 error (&from_file->cmds->fileinfo,
269 _("Recipe for `%s' will be ignored in favor of the one for `%s'."),
270 to_hname, from_file->name);
274 /* Merge the dependencies of the two files. */
276 if (to_file->deps == 0)
277 to_file->deps = from_file->deps;
278 else
280 struct dep *deps = to_file->deps;
281 while (deps->next != 0)
282 deps = deps->next;
283 deps->next = from_file->deps;
286 merge_variable_set_lists (&to_file->variables, from_file->variables);
288 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
289 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
290 from_file->name, to_hname);
291 if (!to_file->double_colon && from_file->double_colon)
293 if (to_file->is_target)
294 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
295 from_file->name, to_hname);
296 else
297 to_file->double_colon = from_file->double_colon;
300 if (from_file->last_mtime > to_file->last_mtime)
301 /* %%% Kludge so -W wins on a file that gets vpathized. */
302 to_file->last_mtime = from_file->last_mtime;
304 to_file->mtime_before_update = from_file->mtime_before_update;
306 #define MERGE(field) to_file->field |= from_file->field
307 MERGE (precious);
308 MERGE (tried_implicit);
309 MERGE (updating);
310 MERGE (updated);
311 MERGE (is_target);
312 MERGE (cmd_target);
313 MERGE (phony);
314 MERGE (ignore_vpath);
315 #undef MERGE
317 from_file->renamed = to_file;
320 /* Rename FILE to NAME. This is not as simple as resetting
321 the `name' member, since it must be put in a new hash bucket,
322 and possibly merged with an existing file called NAME. */
324 void
325 rename_file (struct file *from_file, const char *to_hname)
327 rehash_file (from_file, to_hname);
328 while (from_file)
330 from_file->name = from_file->hname;
331 from_file = from_file->prev;
335 /* Remove all nonprecious intermediate files.
336 If SIG is nonzero, this was caused by a fatal signal,
337 meaning that a different message will be printed, and
338 the message will go to stderr rather than stdout. */
340 void
341 remove_intermediates (int sig)
343 struct file **file_slot;
344 struct file **file_end;
345 int doneany = 0;
347 /* If there's no way we will ever remove anything anyway, punt early. */
348 if (question_flag || touch_flag || all_secondary)
349 return;
351 if (sig && just_print_flag)
352 return;
354 file_slot = (struct file **) files.ht_vec;
355 file_end = file_slot + files.ht_size;
356 for ( ; file_slot < file_end; file_slot++)
357 if (! HASH_VACANT (*file_slot))
359 struct file *f = *file_slot;
360 /* Is this file eligible for automatic deletion?
361 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
362 given on the command line, and it's either a -include makefile or
363 it's not precious. */
364 if (f->intermediate && (f->dontcare || !f->precious)
365 && !f->secondary && !f->cmd_target)
367 int status;
368 if (f->update_status == -1)
369 /* If nothing would have created this file yet,
370 don't print an "rm" command for it. */
371 continue;
372 if (just_print_flag)
373 status = 0;
374 else
376 status = unlink (f->name);
377 if (status < 0 && errno == ENOENT)
378 continue;
380 if (!f->dontcare)
382 if (sig)
383 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
384 else
386 if (! doneany)
387 DB (DB_BASIC, (_("Removing intermediate files...\n")));
388 if (!silent_flag)
390 if (! doneany)
392 fputs ("rm ", stdout);
393 doneany = 1;
395 else
396 putchar (' ');
397 fputs (f->name, stdout);
398 fflush (stdout);
401 if (status < 0)
402 perror_with_name ("unlink: ", f->name);
407 if (doneany && !sig)
409 putchar ('\n');
410 fflush (stdout);
414 struct dep *
415 parse_prereqs (char *p)
417 struct dep *new = (struct dep *)
418 multi_glob (parse_file_seq (&p, '|', sizeof (struct dep), 1),
419 sizeof (struct dep), 0);
421 if (*p)
423 /* Files that follow '|' are "order-only" prerequisites that satisfy the
424 dependency by existing: their modification times are irrelevant. */
425 struct dep *ood;
427 ++p;
428 ood = (struct dep *)
429 multi_glob (parse_file_seq (&p, '\0', sizeof (struct dep), 1),
430 sizeof (struct dep), 0);
432 if (! new)
433 new = ood;
434 else
436 struct dep *dp;
437 for (dp = new; dp->next != NULL; dp = dp->next)
439 dp->next = ood;
442 for (; ood != NULL; ood = ood->next)
443 ood->ignore_mtime = 1;
446 return new;
449 /* Set the intermediate flag. */
451 static void
452 set_intermediate (const void *item)
454 struct file *f = (struct file *) item;
455 f->intermediate = 1;
458 /* Expand and parse each dependency line. */
459 static void
460 expand_deps (struct file *f)
462 struct dep *d;
463 struct dep *old = f->deps;
464 const char *file_stem = f->stem;
465 unsigned int last_dep_has_cmds = f->updating;
466 int initialized = 0;
468 f->updating = 0;
469 f->deps = 0;
471 for (d = old; d != 0; d = d->next)
473 struct dep *new, *d1;
474 char *p;
476 if (! d->name)
477 continue;
479 /* Create the dependency list.
480 If we're not doing 2nd expansion, then it's just the name. We will
481 still need to massage it though. */
482 if (! d->need_2nd_expansion)
484 p = variable_expand ("");
485 variable_buffer_output (p, d->name, strlen (d->name) + 1);
486 p = variable_buffer;
488 else
490 /* If it's from a static pattern rule, convert the patterns into
491 "$*" so they'll expand properly. */
492 if (d->staticpattern)
494 char *o;
495 char *buffer = variable_expand ("");
497 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
499 d->name = strcache_add_len (variable_buffer,
500 o - variable_buffer);
501 d->staticpattern = 0; /* Clear staticpattern so that we don't
502 re-expand %s below. */
505 /* We are going to do second expansion so initialize file variables
506 for the file. Since the stem for static pattern rules comes from
507 individual dep lines, we will temporarily set f->stem to d->stem.
509 if (!initialized)
511 initialize_file_variables (f, 0);
512 initialized = 1;
515 if (d->stem != 0)
516 f->stem = d->stem;
518 set_file_variables (f);
520 p = variable_expand_for_file (d->name, f);
522 if (d->stem != 0)
523 f->stem = file_stem;
526 /* Parse the prerequisites. */
527 new = parse_prereqs (p);
529 /* If this dep list was from a static pattern rule, expand the %s. We
530 use patsubst_expand to translate the prerequisites' patterns into
531 plain prerequisite names. */
532 if (new && d->staticpattern)
534 const char *pattern = "%";
535 char *buffer = variable_expand ("");
536 struct dep *dp = new, *dl = 0;
538 while (dp != 0)
540 char *percent;
541 int nl = strlen (dp->name) + 1;
542 char *nm = alloca (nl);
543 memcpy (nm, dp->name, nl);
544 percent = find_percent (nm);
545 if (percent)
547 char *o;
549 /* We have to handle empty stems specially, because that
550 would be equivalent to $(patsubst %,dp->name,) which
551 will always be empty. */
552 if (d->stem[0] == '\0')
554 memmove (percent, percent+1, strlen (percent));
555 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
557 else
558 o = patsubst_expand_pat (buffer, d->stem, pattern, nm,
559 pattern+1, percent+1);
561 /* If the name expanded to the empty string, ignore it. */
562 if (buffer[0] == '\0')
564 struct dep *df = dp;
565 if (dp == new)
566 dp = new = new->next;
567 else
568 dp = dl->next = dp->next;
569 free_dep (df);
570 continue;
573 /* Save the name. */
574 dp->name = strcache_add_len (buffer, o - buffer);
576 dl = dp;
577 dp = dp->next;
581 /* Enter them as files. */
582 for (d1 = new; d1 != 0; d1 = d1->next)
584 d1->file = lookup_file (d1->name);
585 if (d1->file == 0)
586 d1->file = enter_file (d1->name);
587 d1->name = 0;
588 d1->staticpattern = 0;
589 d1->need_2nd_expansion = 0;
592 /* Add newly parsed deps to f->deps. If this is the last dependency
593 line and this target has commands then put it in front so the
594 last dependency line (the one with commands) ends up being the
595 first. This is important because people expect $< to hold first
596 prerequisite from the rule with commands. If it is not the last
597 dependency line or the rule does not have commands then link it
598 at the end so it appears in makefile order. */
600 if (new != 0)
602 if (d->next == 0 && last_dep_has_cmds)
604 struct dep **d_ptr;
605 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
608 *d_ptr = f->deps;
609 f->deps = new;
611 else
613 struct dep **d_ptr;
614 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
617 *d_ptr = new;
622 free_dep_chain (old);
625 /* For each dependency of each file, make the `struct dep' point
626 at the appropriate `struct file' (which may have to be created).
628 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
629 and various other special targets. */
631 void
632 snap_deps (void)
634 struct file *f;
635 struct file *f2;
636 struct dep *d;
637 struct file **file_slot_0;
638 struct file **file_slot;
639 struct file **file_end;
641 /* Remember that we've done this. Once we start snapping deps we can no
642 longer define new targets. */
643 snapped_deps = 1;
645 /* Perform second expansion and enter each dependency name as a file. */
647 /* Expand .SUFFIXES first; it's dependencies are used for $$* calculation. */
648 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
649 expand_deps (f);
651 /* For every target that's not .SUFFIXES, expand its dependencies.
652 We must use hash_dump (), because within this loop we might add new files
653 to the table, possibly causing an in-situ table expansion. */
654 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
655 file_end = file_slot_0 + files.ht_fill;
656 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
657 for (f = *file_slot; f != 0; f = f->prev)
658 if (strcmp (f->name, ".SUFFIXES") != 0)
659 expand_deps (f);
660 free (file_slot_0);
662 /* Now manage all the special targets. */
664 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
665 for (d = f->deps; d != 0; d = d->next)
666 for (f2 = d->file; f2 != 0; f2 = f2->prev)
667 f2->precious = 1;
669 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
670 for (d = f->deps; d != 0; d = d->next)
671 for (f2 = d->file; f2 != 0; f2 = f2->prev)
672 f2->low_resolution_time = 1;
674 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
675 for (d = f->deps; d != 0; d = d->next)
676 for (f2 = d->file; f2 != 0; f2 = f2->prev)
678 /* Mark this file as phony nonexistent target. */
679 f2->phony = 1;
680 f2->is_target = 1;
681 f2->last_mtime = NONEXISTENT_MTIME;
682 f2->mtime_before_update = NONEXISTENT_MTIME;
685 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
686 /* Mark .INTERMEDIATE deps as intermediate files. */
687 for (d = f->deps; d != 0; d = d->next)
688 for (f2 = d->file; f2 != 0; f2 = f2->prev)
689 f2->intermediate = 1;
690 /* .INTERMEDIATE with no deps does nothing.
691 Marking all files as intermediates is useless since the goal targets
692 would be deleted after they are built. */
694 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
695 /* Mark .SECONDARY deps as both intermediate and secondary. */
696 if (f->deps)
697 for (d = f->deps; d != 0; d = d->next)
698 for (f2 = d->file; f2 != 0; f2 = f2->prev)
699 f2->intermediate = f2->secondary = 1;
700 /* .SECONDARY with no deps listed marks *all* files that way. */
701 else
703 all_secondary = 1;
704 hash_map (&files, set_intermediate);
707 f = lookup_file (".EXPORT_ALL_VARIABLES");
708 if (f != 0 && f->is_target)
709 export_all_variables = 1;
711 f = lookup_file (".IGNORE");
712 if (f != 0 && f->is_target)
714 if (f->deps == 0)
715 ignore_errors_flag = 1;
716 else
717 for (d = f->deps; d != 0; d = d->next)
718 for (f2 = d->file; f2 != 0; f2 = f2->prev)
719 f2->command_flags |= COMMANDS_NOERROR;
722 f = lookup_file (".SILENT");
723 if (f != 0 && f->is_target)
725 if (f->deps == 0)
726 silent_flag = 1;
727 else
728 for (d = f->deps; d != 0; d = d->next)
729 for (f2 = d->file; f2 != 0; f2 = f2->prev)
730 f2->command_flags |= COMMANDS_SILENT;
733 f = lookup_file (".NOTPARALLEL");
734 if (f != 0 && f->is_target)
735 not_parallel = 1;
737 #ifndef NO_MINUS_C_MINUS_O
738 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
739 /* This needs more work: what if the user sets this in the makefile?
740 if (posix_pedantic)
741 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
743 #endif
746 /* Set the `command_state' member of FILE and all its `also_make's. */
748 void
749 set_command_state (struct file *file, enum cmd_state state)
751 struct dep *d;
753 file->command_state = state;
755 for (d = file->also_make; d != 0; d = d->next)
756 d->file->command_state = state;
759 /* Convert an external file timestamp to internal form. */
761 FILE_TIMESTAMP
762 file_timestamp_cons (const char *fname, time_t s, int ns)
764 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
765 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
766 FILE_TIMESTAMP ts = product + offset;
768 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
769 && product <= ts && ts <= ORDINARY_MTIME_MAX))
771 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
772 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
773 file_timestamp_sprintf (buf, ts);
774 error (NILF, _("%s: Timestamp out of range; substituting %s"),
775 fname ? fname : _("Current time"), buf);
778 return ts;
781 /* Return the current time as a file timestamp, setting *RESOLUTION to
782 its resolution. */
783 FILE_TIMESTAMP
784 file_timestamp_now (int *resolution)
786 int r;
787 time_t s;
788 int ns;
790 /* Don't bother with high-resolution clocks if file timestamps have
791 only one-second resolution. The code below should work, but it's
792 not worth the hassle of debugging it on hosts where it fails. */
793 #if FILE_TIMESTAMP_HI_RES
794 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
796 struct timespec timespec;
797 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
799 r = 1;
800 s = timespec.tv_sec;
801 ns = timespec.tv_nsec;
802 goto got_time;
805 # endif
806 # if HAVE_GETTIMEOFDAY
808 struct timeval timeval;
809 if (gettimeofday (&timeval, 0) == 0)
811 r = 1000;
812 s = timeval.tv_sec;
813 ns = timeval.tv_usec * 1000;
814 goto got_time;
817 # endif
818 #endif
820 r = 1000000000;
821 s = time ((time_t *) 0);
822 ns = 0;
824 #if FILE_TIMESTAMP_HI_RES
825 got_time:
826 #endif
827 *resolution = r;
828 return file_timestamp_cons (0, s, ns);
831 /* Place into the buffer P a printable representation of the file
832 timestamp TS. */
833 void
834 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
836 time_t t = FILE_TIMESTAMP_S (ts);
837 struct tm *tm = localtime (&t);
839 if (tm)
840 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
841 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
842 tm->tm_hour, tm->tm_min, tm->tm_sec);
843 else if (t < 0)
844 sprintf (p, "%ld", (long) t);
845 else
846 sprintf (p, "%lu", (unsigned long) t);
847 p += strlen (p);
849 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
850 know the actual timestamp resolution, since clock_getres applies only to
851 local times, whereas this timestamp might come from a remote filesystem.
852 So removing trailing zeros is the best guess that we can do. */
853 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
854 p += strlen (p) - 1;
855 while (*p == '0')
856 p--;
857 p += *p != '.';
859 *p = '\0';
862 /* Print the data base of files. */
864 static void
865 print_file (const void *item)
867 const struct file *f = item;
868 struct dep *d;
869 struct dep *ood = 0;
871 putchar ('\n');
872 if (!f->is_target)
873 puts (_("# Not a target:"));
874 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
876 /* Print all normal dependencies; note any order-only deps. */
877 for (d = f->deps; d != 0; d = d->next)
878 if (! d->ignore_mtime)
879 printf (" %s", dep_name (d));
880 else if (! ood)
881 ood = d;
883 /* Print order-only deps, if we have any. */
884 if (ood)
886 printf (" | %s", dep_name (ood));
887 for (d = ood->next; d != 0; d = d->next)
888 if (d->ignore_mtime)
889 printf (" %s", dep_name (d));
892 putchar ('\n');
894 if (f->precious)
895 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
896 if (f->phony)
897 puts (_("# Phony target (prerequisite of .PHONY)."));
898 if (f->cmd_target)
899 puts (_("# Command line target."));
900 if (f->dontcare)
901 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
902 puts (f->tried_implicit
903 ? _("# Implicit rule search has been done.")
904 : _("# Implicit rule search has not been done."));
905 if (f->stem != 0)
906 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
907 if (f->intermediate)
908 puts (_("# File is an intermediate prerequisite."));
909 if (f->also_make != 0)
911 fputs (_("# Also makes:"), stdout);
912 for (d = f->also_make; d != 0; d = d->next)
913 printf (" %s", dep_name (d));
914 putchar ('\n');
916 if (f->last_mtime == UNKNOWN_MTIME)
917 puts (_("# Modification time never checked."));
918 else if (f->last_mtime == NONEXISTENT_MTIME)
919 puts (_("# File does not exist."));
920 else if (f->last_mtime == OLD_MTIME)
921 puts (_("# File is very old."));
922 else
924 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
925 file_timestamp_sprintf (buf, f->last_mtime);
926 printf (_("# Last modified %s\n"), buf);
928 puts (f->updated
929 ? _("# File has been updated.") : _("# File has not been updated."));
930 switch (f->command_state)
932 case cs_running:
933 puts (_("# Recipe currently running (THIS IS A BUG)."));
934 break;
935 case cs_deps_running:
936 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
937 break;
938 case cs_not_started:
939 case cs_finished:
940 switch (f->update_status)
942 case -1:
943 break;
944 case 0:
945 puts (_("# Successfully updated."));
946 break;
947 case 1:
948 assert (question_flag);
949 puts (_("# Needs to be updated (-q is set)."));
950 break;
951 case 2:
952 puts (_("# Failed to be updated."));
953 break;
954 default:
955 puts (_("# Invalid value in `update_status' member!"));
956 fflush (stdout);
957 fflush (stderr);
958 abort ();
960 break;
961 default:
962 puts (_("# Invalid value in `command_state' member!"));
963 fflush (stdout);
964 fflush (stderr);
965 abort ();
968 if (f->variables != 0)
969 print_file_variables (f);
971 if (f->cmds != 0)
972 print_commands (f->cmds);
974 if (f->prev)
975 print_file ((const void *) f->prev);
978 void
979 print_file_data_base (void)
981 puts (_("\n# Files"));
983 hash_map (&files, print_file);
985 fputs (_("\n# files hash-table stats:\n# "), stdout);
986 hash_print_stats (&files, stdout);
989 /* Verify the integrity of the data base of files. */
991 #define VERIFY_CACHED(_p,_n) \
992 do{\
993 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
994 printf ("%s: Field %s not cached: %s\n", _p->name, # _n, _p->_n); \
995 }while(0)
997 static void
998 verify_file (const void *item)
1000 const struct file *f = item;
1001 const struct dep *d;
1003 VERIFY_CACHED (f, name);
1004 VERIFY_CACHED (f, hname);
1005 VERIFY_CACHED (f, vpath);
1006 VERIFY_CACHED (f, stem);
1008 /* Check the deps. */
1009 for (d = f->deps; d != 0; d = d->next)
1011 VERIFY_CACHED (d, name);
1012 VERIFY_CACHED (d, stem);
1016 void
1017 verify_file_data_base (void)
1019 hash_map (&files, verify_file);
1022 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1024 char *
1025 build_target_list (char *value)
1027 static unsigned long last_targ_count = 0;
1029 if (files.ht_fill != last_targ_count)
1031 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1032 unsigned long len;
1033 char *p;
1034 struct file **fp = (struct file **) files.ht_vec;
1035 struct file **end = &fp[files.ht_size];
1037 /* Make sure we have at least MAX bytes in the allocated buffer. */
1038 value = xrealloc (value, max);
1040 p = value;
1041 len = 0;
1042 for (; fp < end; ++fp)
1043 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1045 struct file *f = *fp;
1046 int l = strlen (f->name);
1048 len += l + 1;
1049 if (len > max)
1051 unsigned long off = p - value;
1053 max += EXPANSION_INCREMENT (l + 1);
1054 value = xrealloc (value, max);
1055 p = &value[off];
1058 memcpy (p, f->name, l);
1059 p += l;
1060 *(p++) = ' ';
1062 *(p-1) = '\0';
1064 last_targ_count = files.ht_fill;
1067 return value;
1070 void
1071 init_hash_files (void)
1073 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1076 /* EOF */