Savannah bug #37179: Use alternate shared library syntax for MacOS.
[make.git] / file.c
blob21582e002ecce13764c8bde5b7a13238768aaad4
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;
577 d->name = o = variable_expand ("");
578 o = subst_expand (o, name, "%", "$*", 1, 2, 0);
579 *o = '\0';
580 free (name);
581 d->name = name = xstrdup (d->name);
582 d->staticpattern = 0;
585 /* We're going to do second expansion so initialize file variables for
586 the file. Since the stem for static pattern rules comes from
587 individual dep lines, we will temporarily set f->stem to d->stem. */
588 if (!initialized)
590 initialize_file_variables (f, 0);
591 initialized = 1;
594 if (d->stem != 0)
595 f->stem = d->stem;
597 set_file_variables (f);
599 p = variable_expand_for_file (d->name, f);
601 if (d->stem != 0)
602 f->stem = file_stem;
604 /* At this point we don't need the name anymore: free it. */
605 free (name);
607 /* Parse the prerequisites and enter them into the file database. */
608 new = enter_prereqs (split_prereqs (p), d->stem);
610 /* If there were no prereqs here (blank!) then throw this one out. */
611 if (new == 0)
613 *dp = d->next;
614 free_dep (d);
615 d = *dp;
616 continue;
619 /* Add newly parsed prerequisites. */
620 next = d->next;
621 *dp = new;
622 for (dp = &new->next, d = new->next; d != 0; dp = &d->next, d = d->next)
624 *dp = next;
625 d = *dp;
629 /* Reset the updating flag. */
631 static void
632 reset_updating (const void *item)
634 struct file *f = (struct file *) item;
635 f->updating = 0;
638 /* For each dependency of each file, make the 'struct dep' point
639 at the appropriate 'struct file' (which may have to be created).
641 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
642 and various other special targets. */
644 void
645 snap_deps (void)
647 struct file *f;
648 struct file *f2;
649 struct dep *d;
651 /* Remember that we've done this. Once we start snapping deps we can no
652 longer define new targets. */
653 snapped_deps = 1;
655 /* Perform second expansion and enter each dependency name as a file. We
656 must use hash_dump() here because within these loops we likely add new
657 files to the table, possibly causing an in-situ table expansion.
659 We only need to do this if second_expansion has been defined; if it
660 hasn't then all deps were expanded as the makefile was read in. If we
661 ever change make to be able to unset .SECONDARY_EXPANSION this will have
662 to change. */
664 if (second_expansion)
666 struct file **file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
667 struct file **file_end = file_slot_0 + files.ht_fill;
668 struct file **file_slot;
669 const char *suffixes;
671 /* Expand .SUFFIXES: its prerequisites are used for $$* calc. */
672 f = lookup_file (".SUFFIXES");
673 suffixes = f ? f->name : 0;
674 for (; f != 0; f = f->prev)
675 expand_deps (f);
677 /* For every target that's not .SUFFIXES, expand its prerequisites. */
679 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
680 for (f = *file_slot; f != 0; f = f->prev)
681 if (f->name != suffixes)
682 expand_deps (f);
683 free (file_slot_0);
685 else
686 /* We're not doing second expansion, so reset updating. */
687 hash_map (&files, reset_updating);
689 /* Now manage all the special targets. */
691 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
692 for (d = f->deps; d != 0; d = d->next)
693 for (f2 = d->file; f2 != 0; f2 = f2->prev)
694 f2->precious = 1;
696 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
697 for (d = f->deps; d != 0; d = d->next)
698 for (f2 = d->file; f2 != 0; f2 = f2->prev)
699 f2->low_resolution_time = 1;
701 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
702 for (d = f->deps; d != 0; d = d->next)
703 for (f2 = d->file; f2 != 0; f2 = f2->prev)
705 /* Mark this file as phony nonexistent target. */
706 f2->phony = 1;
707 f2->is_target = 1;
708 f2->last_mtime = NONEXISTENT_MTIME;
709 f2->mtime_before_update = NONEXISTENT_MTIME;
712 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
713 /* Mark .INTERMEDIATE deps as intermediate files. */
714 for (d = f->deps; d != 0; d = d->next)
715 for (f2 = d->file; f2 != 0; f2 = f2->prev)
716 f2->intermediate = 1;
717 /* .INTERMEDIATE with no deps does nothing.
718 Marking all files as intermediates is useless since the goal targets
719 would be deleted after they are built. */
721 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
722 /* Mark .SECONDARY deps as both intermediate and secondary. */
723 if (f->deps)
724 for (d = f->deps; d != 0; d = d->next)
725 for (f2 = d->file; f2 != 0; f2 = f2->prev)
726 f2->intermediate = f2->secondary = 1;
727 /* .SECONDARY with no deps listed marks *all* files that way. */
728 else
730 all_secondary = 1;
731 hash_map (&files, set_intermediate);
734 f = lookup_file (".EXPORT_ALL_VARIABLES");
735 if (f != 0 && f->is_target)
736 export_all_variables = 1;
738 f = lookup_file (".IGNORE");
739 if (f != 0 && f->is_target)
741 if (f->deps == 0)
742 ignore_errors_flag = 1;
743 else
744 for (d = f->deps; d != 0; d = d->next)
745 for (f2 = d->file; f2 != 0; f2 = f2->prev)
746 f2->command_flags |= COMMANDS_NOERROR;
749 f = lookup_file (".SILENT");
750 if (f != 0 && f->is_target)
752 if (f->deps == 0)
753 silent_flag = 1;
754 else
755 for (d = f->deps; d != 0; d = d->next)
756 for (f2 = d->file; f2 != 0; f2 = f2->prev)
757 f2->command_flags |= COMMANDS_SILENT;
760 f = lookup_file (".NOTPARALLEL");
761 if (f != 0 && f->is_target)
762 not_parallel = 1;
764 #ifndef NO_MINUS_C_MINUS_O
765 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
766 /* This needs more work: what if the user sets this in the makefile?
767 if (posix_pedantic)
768 define_variable_cname ("OUTPUT_OPTION", "", o_default, 1);
770 #endif
773 /* Set the 'command_state' member of FILE and all its 'also_make's. */
775 void
776 set_command_state (struct file *file, enum cmd_state state)
778 struct dep *d;
780 file->command_state = state;
782 for (d = file->also_make; d != 0; d = d->next)
783 d->file->command_state = state;
786 /* Convert an external file timestamp to internal form. */
788 FILE_TIMESTAMP
789 file_timestamp_cons (const char *fname, time_t stamp, long int ns)
791 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
792 FILE_TIMESTAMP s = stamp;
793 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
794 FILE_TIMESTAMP ts = product + offset;
796 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
797 && product <= ts && ts <= ORDINARY_MTIME_MAX))
799 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
800 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
801 file_timestamp_sprintf (buf, ts);
802 error (NILF, _("%s: Timestamp out of range; substituting %s"),
803 fname ? fname : _("Current time"), buf);
806 return ts;
809 /* Return the current time as a file timestamp, setting *RESOLUTION to
810 its resolution. */
811 FILE_TIMESTAMP
812 file_timestamp_now (int *resolution)
814 int r;
815 time_t s;
816 int ns;
818 /* Don't bother with high-resolution clocks if file timestamps have
819 only one-second resolution. The code below should work, but it's
820 not worth the hassle of debugging it on hosts where it fails. */
821 #if FILE_TIMESTAMP_HI_RES
822 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
824 struct timespec timespec;
825 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
827 r = 1;
828 s = timespec.tv_sec;
829 ns = timespec.tv_nsec;
830 goto got_time;
833 # endif
834 # if HAVE_GETTIMEOFDAY
836 struct timeval timeval;
837 if (gettimeofday (&timeval, 0) == 0)
839 r = 1000;
840 s = timeval.tv_sec;
841 ns = timeval.tv_usec * 1000;
842 goto got_time;
845 # endif
846 #endif
848 r = 1000000000;
849 s = time ((time_t *) 0);
850 ns = 0;
852 #if FILE_TIMESTAMP_HI_RES
853 got_time:
854 #endif
855 *resolution = r;
856 return file_timestamp_cons (0, s, ns);
859 /* Place into the buffer P a printable representation of the file
860 timestamp TS. */
861 void
862 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
864 time_t t = FILE_TIMESTAMP_S (ts);
865 struct tm *tm = localtime (&t);
867 if (tm)
868 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
869 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
870 tm->tm_hour, tm->tm_min, tm->tm_sec);
871 else if (t < 0)
872 sprintf (p, "%ld", (long) t);
873 else
874 sprintf (p, "%lu", (unsigned long) t);
875 p += strlen (p);
877 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
878 know the actual timestamp resolution, since clock_getres applies only to
879 local times, whereas this timestamp might come from a remote filesystem.
880 So removing trailing zeros is the best guess that we can do. */
881 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
882 p += strlen (p) - 1;
883 while (*p == '0')
884 p--;
885 p += *p != '.';
887 *p = '\0';
890 /* Print the data base of files. */
892 void
893 print_prereqs (const struct dep *deps)
895 const struct dep *ood = 0;
897 /* Print all normal dependencies; note any order-only deps. */
898 for (; deps != 0; deps = deps->next)
899 if (! deps->ignore_mtime)
900 printf (" %s", dep_name (deps));
901 else if (! ood)
902 ood = deps;
904 /* Print order-only deps, if we have any. */
905 if (ood)
907 printf (" | %s", dep_name (ood));
908 for (ood = ood->next; ood != 0; ood = ood->next)
909 if (ood->ignore_mtime)
910 printf (" %s", dep_name (ood));
913 putchar ('\n');
916 static void
917 print_file (const void *item)
919 const struct file *f = item;
921 putchar ('\n');
923 if (f->cmds && f->cmds->recipe_prefix != cmd_prefix)
925 fputs (".RECIPEPREFIX = ", stdout);
926 cmd_prefix = f->cmds->recipe_prefix;
927 if (cmd_prefix != RECIPEPREFIX_DEFAULT)
928 putchar (cmd_prefix);
929 putchar ('\n');
932 if (f->variables != 0)
933 print_target_variables (f);
935 if (!f->is_target)
936 puts (_("# Not a target:"));
937 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
938 print_prereqs (f->deps);
940 if (f->precious)
941 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
942 if (f->phony)
943 puts (_("# Phony target (prerequisite of .PHONY)."));
944 if (f->cmd_target)
945 puts (_("# Command line target."));
946 if (f->dontcare)
947 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
948 puts (f->tried_implicit
949 ? _("# Implicit rule search has been done.")
950 : _("# Implicit rule search has not been done."));
951 if (f->stem != 0)
952 printf (_("# Implicit/static pattern stem: '%s'\n"), f->stem);
953 if (f->intermediate)
954 puts (_("# File is an intermediate prerequisite."));
955 if (f->also_make != 0)
957 const struct dep *d;
958 fputs (_("# Also makes:"), stdout);
959 for (d = f->also_make; d != 0; d = d->next)
960 printf (" %s", dep_name (d));
961 putchar ('\n');
963 if (f->last_mtime == UNKNOWN_MTIME)
964 puts (_("# Modification time never checked."));
965 else if (f->last_mtime == NONEXISTENT_MTIME)
966 puts (_("# File does not exist."));
967 else if (f->last_mtime == OLD_MTIME)
968 puts (_("# File is very old."));
969 else
971 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
972 file_timestamp_sprintf (buf, f->last_mtime);
973 printf (_("# Last modified %s\n"), buf);
975 puts (f->updated
976 ? _("# File has been updated.") : _("# File has not been updated."));
977 switch (f->command_state)
979 case cs_running:
980 puts (_("# Recipe currently running (THIS IS A BUG)."));
981 break;
982 case cs_deps_running:
983 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
984 break;
985 case cs_not_started:
986 case cs_finished:
987 switch (f->update_status)
989 case -1:
990 break;
991 case 0:
992 puts (_("# Successfully updated."));
993 break;
994 case 1:
995 assert (question_flag);
996 puts (_("# Needs to be updated (-q is set)."));
997 break;
998 case 2:
999 puts (_("# Failed to be updated."));
1000 break;
1001 default:
1002 puts (_("# Invalid value in 'update_status' member!"));
1003 fflush (stdout);
1004 fflush (stderr);
1005 abort ();
1007 break;
1008 default:
1009 puts (_("# Invalid value in 'command_state' member!"));
1010 fflush (stdout);
1011 fflush (stderr);
1012 abort ();
1015 if (f->variables != 0)
1016 print_file_variables (f);
1018 if (f->cmds != 0)
1019 print_commands (f->cmds);
1021 if (f->prev)
1022 print_file ((const void *) f->prev);
1025 void
1026 print_file_data_base (void)
1028 puts (_("\n# Files"));
1030 hash_map (&files, print_file);
1032 fputs (_("\n# files hash-table stats:\n# "), stdout);
1033 hash_print_stats (&files, stdout);
1036 /* Verify the integrity of the data base of files. */
1038 #define VERIFY_CACHED(_p,_n) \
1039 do{\
1040 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1041 error (NULL, "%s: Field '%s' not cached: %s\n", _p->name, # _n, _p->_n); \
1042 }while(0)
1044 static void
1045 verify_file (const void *item)
1047 const struct file *f = item;
1048 const struct dep *d;
1050 VERIFY_CACHED (f, name);
1051 VERIFY_CACHED (f, hname);
1052 VERIFY_CACHED (f, vpath);
1053 VERIFY_CACHED (f, stem);
1055 /* Check the deps. */
1056 for (d = f->deps; d != 0; d = d->next)
1058 if (! d->need_2nd_expansion)
1059 VERIFY_CACHED (d, name);
1060 VERIFY_CACHED (d, stem);
1064 void
1065 verify_file_data_base (void)
1067 hash_map (&files, verify_file);
1070 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1072 char *
1073 build_target_list (char *value)
1075 static unsigned long last_targ_count = 0;
1077 if (files.ht_fill != last_targ_count)
1079 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1080 unsigned long len;
1081 char *p;
1082 struct file **fp = (struct file **) files.ht_vec;
1083 struct file **end = &fp[files.ht_size];
1085 /* Make sure we have at least MAX bytes in the allocated buffer. */
1086 value = xrealloc (value, max);
1088 p = value;
1089 len = 0;
1090 for (; fp < end; ++fp)
1091 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1093 struct file *f = *fp;
1094 int l = strlen (f->name);
1096 len += l + 1;
1097 if (len > max)
1099 unsigned long off = p - value;
1101 max += EXPANSION_INCREMENT (l + 1);
1102 value = xrealloc (value, max);
1103 p = &value[off];
1106 memcpy (p, f->name, l);
1107 p += l;
1108 *(p++) = ' ';
1110 *(p-1) = '\0';
1112 last_targ_count = files.ht_fill;
1115 return value;
1118 void
1119 init_hash_files (void)
1121 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1124 /* EOF */