New variables, .VARIABLES and .TARGETS.
[make.git] / file.c
blobecb83d8978f139f6e1c2a8cb3ab4e7076fb71082
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 /* Hash table of files the makefile knows how to make. */
36 static unsigned long
37 file_hash_1 (void const *key)
39 return_ISTRING_HASH_1 (((struct file const *) key)->hname);
42 static unsigned long
43 file_hash_2 (void const *key)
45 return_ISTRING_HASH_2 (((struct file const *) key)->hname);
48 static int
49 file_hash_cmp (void const *x, void const *y)
51 return_ISTRING_COMPARE (((struct file const *) x)->hname,
52 ((struct file const *) y)->hname);
55 #ifndef FILE_BUCKETS
56 #define FILE_BUCKETS 1007
57 #endif
58 static struct hash_table files;
60 /* Whether or not .SECONDARY with no prerequisites was given. */
61 static int all_secondary = 0;
63 /* Access the hash table of all file records.
64 lookup_file given a name, return the struct file * for that name,
65 or nil if there is none.
66 enter_file similar, but create one if there is none. */
68 struct file *
69 lookup_file (name)
70 char *name;
72 register struct file *f;
73 struct file file_key;
74 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
75 register char *lname, *ln;
76 #endif
78 assert (*name != '\0');
80 /* This is also done in parse_file_seq, so this is redundant
81 for names read from makefiles. It is here for names passed
82 on the command line. */
83 #ifdef VMS
84 # ifndef WANT_CASE_SENSITIVE_TARGETS
86 register char *n;
87 lname = (char *) malloc (strlen (name) + 1);
88 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
89 *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
90 *ln = '\0';
91 name = lname;
93 # endif
95 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
96 name += 2;
97 #endif
98 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
100 name += 2;
101 while (*name == '/')
102 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
103 ++name;
106 if (*name == '\0')
107 /* It was all slashes after a dot. */
108 #ifdef VMS
109 name = "[]";
110 #else
111 #ifdef _AMIGA
112 name = "";
113 #else
114 name = "./";
115 #endif /* AMIGA */
116 #endif /* VMS */
118 file_key.hname = name;
119 f = (struct file *) hash_find_item (&files, &file_key);
120 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
121 free (lname);
122 #endif
123 return f;
126 struct file *
127 enter_file (name)
128 char *name;
130 register struct file *f;
131 register struct file *new;
132 register struct file **file_slot;
133 struct file file_key;
134 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
135 char *lname, *ln;
136 #endif
138 assert (*name != '\0');
140 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
142 register char *n;
143 lname = (char *) malloc (strlen (name) + 1);
144 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
146 if (isupper ((unsigned char)*n))
147 *ln = tolower ((unsigned char)*n);
148 else
149 *ln = *n;
152 *ln = 0;
153 /* Creates a possible leak, old value of name is unreachable, but I
154 currently don't know how to fix it. */
155 name = lname;
157 #endif
159 file_key.hname = name;
160 file_slot = (struct file **) hash_find_slot (&files, &file_key);
161 f = *file_slot;
162 if (! HASH_VACANT (f) && !f->double_colon)
164 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
165 free(lname);
166 #endif
167 return f;
170 new = (struct file *) xmalloc (sizeof (struct file));
171 bzero ((char *) new, sizeof (struct file));
172 new->name = new->hname = name;
173 new->update_status = -1;
175 if (HASH_VACANT (f))
176 hash_insert_at (&files, new, file_slot);
177 else
179 /* There is already a double-colon entry for this file. */
180 new->double_colon = f;
181 while (f->prev != 0)
182 f = f->prev;
183 f->prev = new;
186 return new;
189 /* Rename FILE to NAME. This is not as simple as resetting
190 the `name' member, since it must be put in a new hash bucket,
191 and possibly merged with an existing file called NAME. */
193 void
194 rename_file (from_file, to_hname)
195 register struct file *from_file;
196 char *to_hname;
198 rehash_file (from_file, to_hname);
199 while (from_file)
201 from_file->name = from_file->hname;
202 from_file = from_file->prev;
206 /* Rehash FILE to NAME. This is not as simple as resetting
207 the `hname' member, since it must be put in a new hash bucket,
208 and possibly merged with an existing file called NAME. */
210 void
211 rehash_file (from_file, to_hname)
212 register struct file *from_file;
213 char *to_hname;
215 struct file file_key;
216 struct file **file_slot;
217 struct file *to_file;
218 struct file *deleted_file;
219 struct file *f;
221 file_key.hname = to_hname;
222 if (0 == file_hash_cmp (from_file, &file_key))
223 return;
225 file_key.hname = from_file->hname;
226 while (from_file->renamed != 0)
227 from_file = from_file->renamed;
228 if (file_hash_cmp (from_file, &file_key))
229 /* hname changed unexpectedly */
230 abort ();
232 deleted_file = hash_delete (&files, from_file);
233 if (deleted_file != from_file)
234 /* from_file isn't the one stored in files */
235 abort ();
237 file_key.hname = to_hname;
238 file_slot = (struct file **) hash_find_slot (&files, &file_key);
239 to_file = *file_slot;
241 from_file->hname = to_hname;
242 for (f = from_file->double_colon; f != 0; f = f->prev)
243 f->hname = to_hname;
245 if (HASH_VACANT (to_file))
246 hash_insert_at (&files, from_file, file_slot);
247 else
249 /* TO_FILE already exists under TO_HNAME.
250 We must retain TO_FILE and merge FROM_FILE into it. */
252 if (from_file->cmds != 0)
254 if (to_file->cmds == 0)
255 to_file->cmds = from_file->cmds;
256 else if (from_file->cmds != to_file->cmds)
258 /* We have two sets of commands. We will go with the
259 one given in the rule explicitly mentioning this name,
260 but give a message to let the user know what's going on. */
261 if (to_file->cmds->fileinfo.filenm != 0)
262 error (&from_file->cmds->fileinfo,
263 _("Commands were specified for file `%s' at %s:%lu,"),
264 from_file->name, to_file->cmds->fileinfo.filenm,
265 to_file->cmds->fileinfo.lineno);
266 else
267 error (&from_file->cmds->fileinfo,
268 _("Commands for file `%s' were found by implicit rule search,"),
269 from_file->name);
270 error (&from_file->cmds->fileinfo,
271 _("but `%s' is now considered the same file as `%s'."),
272 from_file->name, to_hname);
273 error (&from_file->cmds->fileinfo,
274 _("Commands for `%s' will be ignored in favor of those for `%s'."),
275 to_hname, from_file->name);
279 /* Merge the dependencies of the two files. */
281 if (to_file->deps == 0)
282 to_file->deps = from_file->deps;
283 else
285 register struct dep *deps = to_file->deps;
286 while (deps->next != 0)
287 deps = deps->next;
288 deps->next = from_file->deps;
291 merge_variable_set_lists (&to_file->variables, from_file->variables);
293 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
294 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
295 from_file->name, to_hname);
296 if (!to_file->double_colon && from_file->double_colon)
298 if (to_file->is_target)
299 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
300 from_file->name, to_hname);
301 else
302 to_file->double_colon = from_file->double_colon;
305 if (from_file->last_mtime > to_file->last_mtime)
306 /* %%% Kludge so -W wins on a file that gets vpathized. */
307 to_file->last_mtime = from_file->last_mtime;
309 to_file->mtime_before_update = from_file->mtime_before_update;
311 #define MERGE(field) to_file->field |= from_file->field
312 MERGE (precious);
313 MERGE (tried_implicit);
314 MERGE (updating);
315 MERGE (updated);
316 MERGE (is_target);
317 MERGE (cmd_target);
318 MERGE (phony);
319 MERGE (ignore_vpath);
320 #undef MERGE
322 from_file->renamed = to_file;
326 /* Remove all nonprecious intermediate files.
327 If SIG is nonzero, this was caused by a fatal signal,
328 meaning that a different message will be printed, and
329 the message will go to stderr rather than stdout. */
331 void
332 remove_intermediates (sig)
333 int sig;
335 register struct file **file_slot;
336 register struct file **file_end;
337 int doneany = 0;
339 /* If there's no way we will ever remove anything anyway, punt early. */
340 if (question_flag || touch_flag || all_secondary)
341 return;
343 if (sig && just_print_flag)
344 return;
346 file_slot = (struct file **) files.ht_vec;
347 file_end = file_slot + files.ht_size;
348 for ( ; file_slot < file_end; file_slot++)
349 if (! HASH_VACANT (*file_slot))
351 register struct file *f = *file_slot;
352 if (f->intermediate && (f->dontcare || !f->precious)
353 && !f->secondary && !f->cmd_target)
355 int status;
356 if (f->update_status == -1)
357 /* If nothing would have created this file yet,
358 don't print an "rm" command for it. */
359 continue;
360 if (just_print_flag)
361 status = 0;
362 else
364 status = unlink (f->name);
365 if (status < 0 && errno == ENOENT)
366 continue;
368 if (!f->dontcare)
370 if (sig)
371 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
372 else
374 if (! doneany)
375 DB (DB_BASIC, (_("Removing intermediate files...\n")));
376 if (!silent_flag)
378 if (! doneany)
380 fputs ("rm ", stdout);
381 doneany = 1;
383 else
384 putchar (' ');
385 fputs (f->name, stdout);
386 fflush (stdout);
389 if (status < 0)
390 perror_with_name ("unlink: ", f->name);
395 if (doneany && !sig)
397 putchar ('\n');
398 fflush (stdout);
402 /* For each dependency of each file, make the `struct dep' point
403 at the appropriate `struct file' (which may have to be created).
405 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
406 and various other special targets. */
408 void
409 snap_deps ()
411 register struct file *f;
412 register struct file *f2;
413 register struct dep *d;
414 register struct file **file_slot_0;
415 register struct file **file_slot;
416 register struct file **file_end;
418 /* Enter each dependency name as a file. */
419 /* We must use hash_dump (), because within this loop
420 we might add new files to the table, possibly causing
421 an in-situ table expansion. */
422 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
423 file_end = file_slot_0 + files.ht_fill;
424 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
425 for (f2 = *file_slot; f2 != 0; f2 = f2->prev)
426 for (d = f2->deps; d != 0; d = d->next)
427 if (d->name != 0)
429 d->file = lookup_file (d->name);
430 if (d->file == 0)
431 d->file = enter_file (d->name);
432 else
433 free (d->name);
434 d->name = 0;
436 free (file_slot_0);
438 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
439 for (d = f->deps; d != 0; d = d->next)
440 for (f2 = d->file; f2 != 0; f2 = f2->prev)
441 f2->precious = 1;
443 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
444 for (d = f->deps; d != 0; d = d->next)
445 for (f2 = d->file; f2 != 0; f2 = f2->prev)
446 f2->low_resolution_time = 1;
448 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
449 for (d = f->deps; d != 0; d = d->next)
450 for (f2 = d->file; f2 != 0; f2 = f2->prev)
452 /* Mark this file as phony and nonexistent. */
453 f2->phony = 1;
454 f2->last_mtime = NONEXISTENT_MTIME;
455 f2->mtime_before_update = NONEXISTENT_MTIME;
458 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
460 /* .INTERMEDIATE with deps listed
461 marks those deps as intermediate files. */
462 for (d = f->deps; d != 0; d = d->next)
463 for (f2 = d->file; f2 != 0; f2 = f2->prev)
464 f2->intermediate = 1;
465 /* .INTERMEDIATE with no deps does nothing.
466 Marking all files as intermediates is useless
467 since the goal targets would be deleted after they are built. */
470 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
472 /* .SECONDARY with deps listed
473 marks those deps as intermediate files
474 in that they don't get rebuilt if not actually needed;
475 but unlike real intermediate files,
476 these are not deleted after make finishes. */
477 if (f->deps)
478 for (d = f->deps; d != 0; d = d->next)
479 for (f2 = d->file; f2 != 0; f2 = f2->prev)
480 f2->intermediate = f2->secondary = 1;
481 /* .SECONDARY with no deps listed marks *all* files that way. */
482 else
483 all_secondary = 1;
486 f = lookup_file (".EXPORT_ALL_VARIABLES");
487 if (f != 0 && f->is_target)
488 export_all_variables = 1;
490 f = lookup_file (".IGNORE");
491 if (f != 0 && f->is_target)
493 if (f->deps == 0)
494 ignore_errors_flag = 1;
495 else
496 for (d = f->deps; d != 0; d = d->next)
497 for (f2 = d->file; f2 != 0; f2 = f2->prev)
498 f2->command_flags |= COMMANDS_NOERROR;
501 f = lookup_file (".SILENT");
502 if (f != 0 && f->is_target)
504 if (f->deps == 0)
505 silent_flag = 1;
506 else
507 for (d = f->deps; d != 0; d = d->next)
508 for (f2 = d->file; f2 != 0; f2 = f2->prev)
509 f2->command_flags |= COMMANDS_SILENT;
512 f = lookup_file (".POSIX");
513 if (f != 0 && f->is_target)
514 posix_pedantic = 1;
516 f = lookup_file (".NOTPARALLEL");
517 if (f != 0 && f->is_target)
518 not_parallel = 1;
521 /* Set the `command_state' member of FILE and all its `also_make's. */
523 void
524 set_command_state (file, state)
525 struct file *file;
526 int state;
528 struct dep *d;
530 file->command_state = state;
532 for (d = file->also_make; d != 0; d = d->next)
533 d->file->command_state = state;
536 /* Convert an external file timestamp to internal form. */
538 FILE_TIMESTAMP
539 file_timestamp_cons (fname, s, ns)
540 char const *fname;
541 time_t s;
542 int ns;
544 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
545 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
546 FILE_TIMESTAMP ts = product + offset;
548 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
549 && product <= ts && ts <= ORDINARY_MTIME_MAX))
551 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
552 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
553 file_timestamp_sprintf (buf, ts);
554 error (NILF, _("%s: Timestamp out of range; substituting %s"),
555 fname ? fname : _("Current time"), buf);
558 return ts;
561 /* Return the current time as a file timestamp, setting *RESOLUTION to
562 its resolution. */
563 FILE_TIMESTAMP
564 file_timestamp_now (resolution)
565 int *resolution;
567 int r;
568 time_t s;
569 int ns;
571 /* Don't bother with high-resolution clocks if file timestamps have
572 only one-second resolution. The code below should work, but it's
573 not worth the hassle of debugging it on hosts where it fails. */
574 #if FILE_TIMESTAMP_HI_RES
575 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
577 struct timespec timespec;
578 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
580 r = 1;
581 s = timespec.tv_sec;
582 ns = timespec.tv_nsec;
583 goto got_time;
586 # endif
587 # if HAVE_GETTIMEOFDAY
589 struct timeval timeval;
590 if (gettimeofday (&timeval, 0) == 0)
592 r = 1000;
593 s = timeval.tv_sec;
594 ns = timeval.tv_usec * 1000;
595 goto got_time;
598 # endif
599 #endif
601 r = 1000000000;
602 s = time ((time_t *) 0);
603 ns = 0;
605 got_time:
606 *resolution = r;
607 return file_timestamp_cons (0, s, ns);
610 /* Place into the buffer P a printable representation of the file
611 timestamp TS. */
612 void
613 file_timestamp_sprintf (p, ts)
614 char *p;
615 FILE_TIMESTAMP ts;
617 time_t t = FILE_TIMESTAMP_S (ts);
618 struct tm *tm = localtime (&t);
620 if (tm)
621 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
622 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
623 tm->tm_hour, tm->tm_min, tm->tm_sec);
624 else if (t < 0)
625 sprintf (p, "%ld", (long) t);
626 else
627 sprintf (p, "%lu", (unsigned long) t);
628 p += strlen (p);
630 /* Append nanoseconds as a fraction, but remove trailing zeros.
631 We don't know the actual timestamp resolution, since clock_getres
632 applies only to local times, whereas this timestamp might come
633 from a remote filesystem. So removing trailing zeros is the
634 best guess that we can do. */
635 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
636 p += strlen (p) - 1;
637 while (*p == '0')
638 p--;
639 p += *p != '.';
641 *p = '\0';
644 /* Print the data base of files. */
646 static void
647 print_file (f)
648 struct file *f;
650 struct dep *d;
651 struct dep *ood = 0;
653 putchar ('\n');
654 if (!f->is_target)
655 puts (_("# Not a target:"));
656 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
658 /* Print all normal dependencies; note any order-only deps. */
659 for (d = f->deps; d != 0; d = d->next)
660 if (! d->ignore_mtime)
661 printf (" %s", dep_name (d));
662 else if (! ood)
663 ood = d;
665 /* Print order-only deps, if we have any. */
666 if (ood)
668 printf (" | %s", dep_name (ood));
669 for (d = ood->next; d != 0; d = d->next)
670 if (d->ignore_mtime)
671 printf (" %s", dep_name (d));
674 putchar ('\n');
676 if (f->precious)
677 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
678 if (f->phony)
679 puts (_("# Phony target (prerequisite of .PHONY)."));
680 if (f->cmd_target)
681 puts (_("# Command-line target."));
682 if (f->dontcare)
683 puts (_("# A default or MAKEFILES makefile."));
684 puts (f->tried_implicit
685 ? _("# Implicit rule search has been done.")
686 : _("# Implicit rule search has not been done."));
687 if (f->stem != 0)
688 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
689 if (f->intermediate)
690 puts (_("# File is an intermediate prerequisite."));
691 if (f->also_make != 0)
693 fputs (_("# Also makes:"), stdout);
694 for (d = f->also_make; d != 0; d = d->next)
695 printf (" %s", dep_name (d));
696 putchar ('\n');
698 if (f->last_mtime == UNKNOWN_MTIME)
699 puts (_("# Modification time never checked."));
700 else if (f->last_mtime == NONEXISTENT_MTIME)
701 puts (_("# File does not exist."));
702 else if (f->last_mtime == OLD_MTIME)
703 puts (_("# File is very old."));
704 else
706 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
707 file_timestamp_sprintf (buf, f->last_mtime);
708 printf (_("# Last modified %s\n"), buf);
710 puts (f->updated
711 ? _("# File has been updated.") : _("# File has not been updated."));
712 switch (f->command_state)
714 case cs_running:
715 puts (_("# Commands currently running (THIS IS A BUG)."));
716 break;
717 case cs_deps_running:
718 puts (_("# Dependencies commands running (THIS IS A BUG)."));
719 break;
720 case cs_not_started:
721 case cs_finished:
722 switch (f->update_status)
724 case -1:
725 break;
726 case 0:
727 puts (_("# Successfully updated."));
728 break;
729 case 1:
730 assert (question_flag);
731 puts (_("# Needs to be updated (-q is set)."));
732 break;
733 case 2:
734 puts (_("# Failed to be updated."));
735 break;
736 default:
737 puts (_("# Invalid value in `update_status' member!"));
738 fflush (stdout);
739 fflush (stderr);
740 abort ();
742 break;
743 default:
744 puts (_("# Invalid value in `command_state' member!"));
745 fflush (stdout);
746 fflush (stderr);
747 abort ();
750 if (f->variables != 0)
751 print_file_variables (f);
753 if (f->cmds != 0)
754 print_commands (f->cmds);
757 void
758 print_file_data_base ()
760 puts (_("\n# Files"));
762 hash_map (&files, print_file);
764 fputs (_("\n# files hash-table stats:\n# "), stdout);
765 hash_print_stats (&files, stdout);
768 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
770 char *
771 build_target_list (value)
772 char *value;
774 static unsigned long last_targ_count = 0;
776 if (files.ht_fill != last_targ_count)
778 unsigned long max = EXPANSION_INCREMENT (strlen (value));
779 unsigned long len;
780 char *p;
781 struct file **fp = (struct file **) files.ht_vec;
782 struct file **end = &fp[files.ht_size];
784 /* Make sure we have at least MAX bytes in the allocated buffer. */
785 value = xrealloc (value, max);
787 p = value;
788 len = 0;
789 for (; fp < end; ++fp)
790 if (!HASH_VACANT (*fp) && (*fp)->is_target)
792 struct file *f = *fp;
793 int l = strlen (f->name);
795 len += l + 1;
796 if (len > max)
798 unsigned long off = p - value;
800 max += EXPANSION_INCREMENT (l + 1);
801 value = xrealloc (value, max);
802 p = &value[off];
805 bcopy (f->name, p, l);
806 p += l;
807 *(p++) = ' ';
809 *(p-1) = '\0';
811 last_targ_count = files.ht_fill;
814 return value;
817 void
818 init_hash_files ()
820 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
823 /* EOF */