* Various bug fixes.
[make.git] / file.c
blob5ec6cadf501de0b3cea0b959ad18dd659e51a6d0
1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 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
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
22 #include <assert.h>
24 #include "dep.h"
25 #include "filedef.h"
26 #include "job.h"
27 #include "commands.h"
28 #include "variable.h"
29 #include "debug.h"
32 /* Hash table of files the makefile knows how to make. */
34 #ifndef FILE_BUCKETS
35 #define FILE_BUCKETS 1007
36 #endif
37 static struct file *files[FILE_BUCKETS];
39 /* Whether or not .SECONDARY with no prerequisites was given. */
40 static int all_secondary = 0;
42 /* Access the hash table of all file records.
43 lookup_file given a name, return the struct file * for that name,
44 or nil if there is none.
45 enter_file similar, but create one if there is none. */
47 struct file *
48 lookup_file (name)
49 char *name;
51 register struct file *f;
52 register char *n;
53 register unsigned int hashval;
54 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
55 register char *lname, *ln;
56 #endif
58 assert (*name != '\0');
60 /* This is also done in parse_file_seq, so this is redundant
61 for names read from makefiles. It is here for names passed
62 on the command line. */
63 #ifdef VMS
64 # ifndef WANT_CASE_SENSITIVE_TARGETS
65 lname = (char *)malloc(strlen(name) + 1);
66 for (n=name, ln=lname; *n != '\0'; ++n, ++ln)
67 *ln = isupper((unsigned char)*n) ? tolower((unsigned char)*n) : *n;
68 *ln = '\0';
69 name = lname;
70 # endif
72 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
73 name += 2;
74 #endif
75 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
77 name += 2;
78 while (*name == '/')
79 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
80 ++name;
83 if (*name == '\0')
84 /* It was all slashes after a dot. */
85 #ifdef VMS
86 name = "[]";
87 #else
88 #ifdef _AMIGA
89 name = "";
90 #else
91 name = "./";
92 #endif /* AMIGA */
93 #endif /* VMS */
95 hashval = 0;
96 for (n = name; *n != '\0'; ++n)
97 HASHI (hashval, *n);
98 hashval %= FILE_BUCKETS;
100 for (f = files[hashval]; f != 0; f = f->next)
102 if (strieq (f->hname, name))
104 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
105 free (lname);
106 #endif
107 return f;
110 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
111 free (lname);
112 #endif
113 return 0;
116 struct file *
117 enter_file (name)
118 char *name;
120 register struct file *f, *new;
121 register char *n;
122 register unsigned int hashval;
123 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
124 char *lname, *ln;
125 #endif
127 assert (*name != '\0');
129 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
130 lname = (char *)malloc (strlen (name) + 1);
131 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
133 if (isupper((unsigned char)*n))
134 *ln = tolower((unsigned char)*n);
135 else
136 *ln = *n;
138 *ln = 0;
139 /* Creates a possible leak, old value of name is unreachable, but I
140 currently don't know how to fix it. */
141 name = lname;
142 #endif
144 hashval = 0;
145 for (n = name; *n != '\0'; ++n)
146 HASHI (hashval, *n);
147 hashval %= FILE_BUCKETS;
149 for (f = files[hashval]; f != 0; f = f->next)
150 if (strieq (f->hname, name))
151 break;
153 if (f != 0 && !f->double_colon)
155 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
156 free(lname);
157 #endif
158 return f;
161 new = (struct file *) xmalloc (sizeof (struct file));
162 bzero ((char *) new, sizeof (struct file));
163 new->name = new->hname = name;
164 new->update_status = -1;
166 if (f == 0)
168 /* This is a completely new file. */
169 new->next = files[hashval];
170 files[hashval] = new;
172 else
174 /* There is already a double-colon entry for this file. */
175 new->double_colon = f;
176 while (f->prev != 0)
177 f = f->prev;
178 f->prev = new;
181 return new;
184 /* Rehash FILE to NAME. This is not as simple as resetting
185 the `hname' member, since it must be put in a new hash bucket,
186 and possibly merged with an existing file called NAME. */
188 void
189 rehash_file (file, name)
190 register struct file *file;
191 char *name;
193 char *oldname = file->hname;
194 register unsigned int oldhash;
195 register char *n;
197 while (file->renamed != 0)
198 file = file->renamed;
200 /* Find the hash values of the old and new names. */
202 oldhash = 0;
203 for (n = oldname; *n != '\0'; ++n)
204 HASHI (oldhash, *n);
206 file_hash_enter (file, name, oldhash, file->name);
209 /* Rename FILE to NAME. This is not as simple as resetting
210 the `name' member, since it must be put in a new hash bucket,
211 and possibly merged with an existing file called NAME. */
213 void
214 rename_file (file, name)
215 register struct file *file;
216 char *name;
218 rehash_file(file, name);
219 while (file)
221 file->name = file->hname;
222 file = file->prev;
226 void
227 file_hash_enter (file, name, oldhash, oldname)
228 register struct file *file;
229 char *name;
230 unsigned int oldhash;
231 char *oldname;
233 unsigned int oldbucket = oldhash % FILE_BUCKETS;
234 register unsigned int newhash, newbucket;
235 struct file *oldfile;
236 register char *n;
237 register struct file *f;
239 newhash = 0;
240 for (n = name; *n != '\0'; ++n)
241 HASHI (newhash, *n);
242 newbucket = newhash % FILE_BUCKETS;
244 /* Look for an existing file under the new name. */
246 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
247 if (strieq (oldfile->hname, name))
248 break;
250 /* If the old file is the same as the new file, never mind. */
251 if (oldfile == file)
252 return;
254 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
256 /* Remove FILE from its hash bucket. */
258 struct file *lastf = 0;
260 for (f = files[oldbucket]; f != file; f = f->next)
261 lastf = f;
263 if (lastf == 0)
264 files[oldbucket] = f->next;
265 else
266 lastf->next = f->next;
269 /* Give FILE its new name. */
271 file->hname = name;
272 for (f = file->double_colon; f != 0; f = f->prev)
273 f->hname = name;
275 if (oldfile == 0)
277 /* There is no existing file with the new name. */
279 if (newbucket != oldbucket)
281 /* Put FILE in its new hash bucket. */
282 file->next = files[newbucket];
283 files[newbucket] = file;
286 else
288 /* There is an existing file with the new name.
289 We must merge FILE into the existing file. */
291 register struct dep *d;
293 if (file->cmds != 0)
295 if (oldfile->cmds == 0)
296 oldfile->cmds = file->cmds;
297 else if (file->cmds != oldfile->cmds)
299 /* We have two sets of commands. We will go with the
300 one given in the rule explicitly mentioning this name,
301 but give a message to let the user know what's going on. */
302 if (oldfile->cmds->fileinfo.filenm != 0)
303 error (&file->cmds->fileinfo,
304 _("Commands were specified for \
305 file `%s' at %s:%lu,"),
306 oldname, oldfile->cmds->fileinfo.filenm,
307 oldfile->cmds->fileinfo.lineno);
308 else
309 error (&file->cmds->fileinfo,
310 _("Commands for file `%s' were found by \
311 implicit rule search,"),
312 oldname);
313 error (&file->cmds->fileinfo,
314 _("but `%s' is now considered the same file \
315 as `%s'."),
316 oldname, name);
317 error (&file->cmds->fileinfo,
318 _("Commands for `%s' will be ignored \
319 in favor of those for `%s'."),
320 name, oldname);
324 /* Merge the dependencies of the two files. */
326 d = oldfile->deps;
327 if (d == 0)
328 oldfile->deps = file->deps;
329 else
331 while (d->next != 0)
332 d = d->next;
333 d->next = file->deps;
336 merge_variable_set_lists (&oldfile->variables, file->variables);
338 if (oldfile->double_colon && file->is_target && !file->double_colon)
339 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
340 oldname, name);
341 if (!oldfile->double_colon && file->double_colon)
343 if (oldfile->is_target)
344 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
345 oldname, name);
346 else
347 oldfile->double_colon = file->double_colon;
350 if (file->last_mtime > oldfile->last_mtime)
351 /* %%% Kludge so -W wins on a file that gets vpathized. */
352 oldfile->last_mtime = file->last_mtime;
354 oldfile->mtime_before_update = file->mtime_before_update;
356 #define MERGE(field) oldfile->field |= file->field
357 MERGE (precious);
358 MERGE (tried_implicit);
359 MERGE (updating);
360 MERGE (updated);
361 MERGE (is_target);
362 MERGE (cmd_target);
363 MERGE (phony);
364 MERGE (ignore_vpath);
365 #undef MERGE
367 file->renamed = oldfile;
371 /* Remove all nonprecious intermediate files.
372 If SIG is nonzero, this was caused by a fatal signal,
373 meaning that a different message will be printed, and
374 the message will go to stderr rather than stdout. */
376 void
377 remove_intermediates (sig)
378 int sig;
380 register int i;
381 register struct file *f;
382 char doneany;
384 /* If there's no way we will ever remove anything anyway, punt early. */
385 if (question_flag || touch_flag || all_secondary)
386 return;
388 if (sig && just_print_flag)
389 return;
391 doneany = 0;
392 for (i = 0; i < FILE_BUCKETS; ++i)
393 for (f = files[i]; f != 0; f = f->next)
394 if (f->intermediate && (f->dontcare || !f->precious)
395 && !f->secondary && !f->cmd_target)
397 int status;
398 if (f->update_status == -1)
399 /* If nothing would have created this file yet,
400 don't print an "rm" command for it. */
401 continue;
402 if (just_print_flag)
403 status = 0;
404 else
406 status = unlink (f->name);
407 if (status < 0 && errno == ENOENT)
408 continue;
410 if (!f->dontcare)
412 if (sig)
413 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
414 else
416 if (! doneany)
417 DB (DB_BASIC, (_("Removing intermediate files...\n")));
418 if (!silent_flag)
420 if (! doneany)
422 fputs ("rm ", stdout);
423 doneany = 1;
425 else
426 putchar (' ');
427 fputs (f->name, stdout);
428 fflush (stdout);
431 if (status < 0)
432 perror_with_name ("unlink: ", f->name);
436 if (doneany && !sig)
438 putchar ('\n');
439 fflush (stdout);
443 /* For each dependency of each file, make the `struct dep' point
444 at the appropriate `struct file' (which may have to be created).
446 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
447 and various other special targets. */
449 void
450 snap_deps ()
452 register struct file *f, *f2;
453 register struct dep *d;
454 register int i;
456 /* Enter each dependency name as a file. */
457 for (i = 0; i < FILE_BUCKETS; ++i)
458 for (f = files[i]; f != 0; f = f->next)
459 for (f2 = f; f2 != 0; f2 = f2->prev)
460 for (d = f2->deps; d != 0; d = d->next)
461 if (d->name != 0)
463 d->file = lookup_file (d->name);
464 if (d->file == 0)
465 d->file = enter_file (d->name);
466 else
467 free (d->name);
468 d->name = 0;
471 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
472 for (d = f->deps; d != 0; d = d->next)
473 for (f2 = d->file; f2 != 0; f2 = f2->prev)
474 f2->precious = 1;
476 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
477 for (d = f->deps; d != 0; d = d->next)
478 for (f2 = d->file; f2 != 0; f2 = f2->prev)
479 f2->low_resolution_time = 1;
481 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
482 for (d = f->deps; d != 0; d = d->next)
483 for (f2 = d->file; f2 != 0; f2 = f2->prev)
485 /* Mark this file as phony and nonexistent. */
486 f2->phony = 1;
487 f2->last_mtime = NONEXISTENT_MTIME;
488 f2->mtime_before_update = NONEXISTENT_MTIME;
491 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
493 /* .INTERMEDIATE with deps listed
494 marks those deps as intermediate files. */
495 for (d = f->deps; d != 0; d = d->next)
496 for (f2 = d->file; f2 != 0; f2 = f2->prev)
497 f2->intermediate = 1;
498 /* .INTERMEDIATE with no deps does nothing.
499 Marking all files as intermediates is useless
500 since the goal targets would be deleted after they are built. */
503 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
505 /* .SECONDARY with deps listed
506 marks those deps as intermediate files
507 in that they don't get rebuilt if not actually needed;
508 but unlike real intermediate files,
509 these are not deleted after make finishes. */
510 if (f->deps)
511 for (d = f->deps; d != 0; d = d->next)
512 for (f2 = d->file; f2 != 0; f2 = f2->prev)
513 f2->intermediate = f2->secondary = 1;
514 /* .SECONDARY with no deps listed marks *all* files that way. */
515 else
516 all_secondary = 1;
519 f = lookup_file (".EXPORT_ALL_VARIABLES");
520 if (f != 0 && f->is_target)
521 export_all_variables = 1;
523 f = lookup_file (".IGNORE");
524 if (f != 0 && f->is_target)
526 if (f->deps == 0)
527 ignore_errors_flag = 1;
528 else
529 for (d = f->deps; d != 0; d = d->next)
530 for (f2 = d->file; f2 != 0; f2 = f2->prev)
531 f2->command_flags |= COMMANDS_NOERROR;
534 f = lookup_file (".SILENT");
535 if (f != 0 && f->is_target)
537 if (f->deps == 0)
538 silent_flag = 1;
539 else
540 for (d = f->deps; d != 0; d = d->next)
541 for (f2 = d->file; f2 != 0; f2 = f2->prev)
542 f2->command_flags |= COMMANDS_SILENT;
545 f = lookup_file (".POSIX");
546 if (f != 0 && f->is_target)
547 posix_pedantic = 1;
549 f = lookup_file (".NOTPARALLEL");
550 if (f != 0 && f->is_target)
551 not_parallel = 1;
554 /* Set the `command_state' member of FILE and all its `also_make's. */
556 void
557 set_command_state (file, state)
558 struct file *file;
559 int state;
561 struct dep *d;
563 file->command_state = state;
565 for (d = file->also_make; d != 0; d = d->next)
566 d->file->command_state = state;
569 /* Convert an external file timestamp to internal form. */
571 FILE_TIMESTAMP
572 file_timestamp_cons (fname, s, ns)
573 char const *fname;
574 time_t s;
575 int ns;
577 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
578 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
579 FILE_TIMESTAMP ts = product + offset;
581 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
582 && product <= ts && ts <= ORDINARY_MTIME_MAX))
584 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
585 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
586 file_timestamp_sprintf (buf, ts);
587 error (NILF, _("%s: Timestamp out of range; substituting %s"),
588 fname ? fname : _("Current time"), buf);
591 return ts;
594 /* Return the current time as a file timestamp, setting *RESOLUTION to
595 its resolution. */
596 FILE_TIMESTAMP
597 file_timestamp_now (int *resolution)
599 int r;
600 time_t s;
601 int ns;
603 /* Don't bother with high-resolution clocks if file timestamps have
604 only one-second resolution. The code below should work, but it's
605 not worth the hassle of debugging it on hosts where it fails. */
606 #if FILE_TIMESTAMP_HI_RES
607 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
609 struct timespec timespec;
610 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
612 r = 1;
613 s = timespec.tv_sec;
614 ns = timespec.tv_nsec;
615 goto got_time;
618 # endif
619 # if HAVE_GETTIMEOFDAY
621 struct timeval timeval;
622 if (gettimeofday (&timeval, 0) == 0)
624 r = 1000;
625 s = timeval.tv_sec;
626 ns = timeval.tv_usec * 1000;
627 goto got_time;
630 # endif
631 #endif
633 r = 1000000000;
634 s = time ((time_t *) 0);
635 ns = 0;
637 got_time:
638 *resolution = r;
639 return file_timestamp_cons (0, s, ns);
642 /* Place into the buffer P a printable representation of the file
643 timestamp TS. */
644 void
645 file_timestamp_sprintf (p, ts)
646 char *p;
647 FILE_TIMESTAMP ts;
649 time_t t = FILE_TIMESTAMP_S (ts);
650 struct tm *tm = localtime (&t);
652 if (tm)
653 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
654 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
655 tm->tm_hour, tm->tm_min, tm->tm_sec);
656 else if (t < 0)
657 sprintf (p, "%ld", (long) t);
658 else
659 sprintf (p, "%lu", (unsigned long) t);
660 p += strlen (p);
662 /* Append nanoseconds as a fraction, but remove trailing zeros.
663 We don't know the actual timestamp resolution, since clock_getres
664 applies only to local times, whereas this timestamp might come
665 from a remote filesystem. So removing trailing zeros is the
666 best guess that we can do. */
667 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
668 p += strlen (p) - 1;
669 while (*p == '0')
670 p--;
671 p += *p != '.';
673 *p = '\0';
676 /* Print the data base of files. */
678 static void
679 print_file (f)
680 struct file *f;
682 register struct dep *d;
684 putchar ('\n');
685 if (!f->is_target)
686 puts (_("# Not a target:"));
687 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
689 for (d = f->deps; d != 0; d = d->next)
690 printf (" %s", dep_name (d));
691 putchar ('\n');
693 if (f->precious)
694 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
695 if (f->phony)
696 puts (_("# Phony target (prerequisite of .PHONY)."));
697 if (f->cmd_target)
698 puts (_("# Command-line target."));
699 if (f->dontcare)
700 puts (_("# A default or MAKEFILES makefile."));
701 puts (f->tried_implicit
702 ? _("# Implicit rule search has been done.")
703 : _("# Implicit rule search has not been done."));
704 if (f->stem != 0)
705 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
706 if (f->intermediate)
707 puts (_("# File is an intermediate prerequisite."));
708 if (f->also_make != 0)
710 fputs (_("# Also makes:"), stdout);
711 for (d = f->also_make; d != 0; d = d->next)
712 printf (" %s", dep_name (d));
713 putchar ('\n');
715 if (f->last_mtime == UNKNOWN_MTIME)
716 puts (_("# Modification time never checked."));
717 else if (f->last_mtime == NONEXISTENT_MTIME)
718 puts (_("# File does not exist."));
719 else if (f->last_mtime == OLD_MTIME)
720 puts (_("# File is very old."));
721 else
723 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
724 file_timestamp_sprintf (buf, f->last_mtime);
725 printf (_("# Last modified %s\n"), buf);
727 puts (f->updated
728 ? _("# File has been updated.") : _("# File has not been updated."));
729 switch (f->command_state)
731 case cs_running:
732 puts (_("# Commands currently running (THIS IS A BUG)."));
733 break;
734 case cs_deps_running:
735 puts (_("# Dependencies commands running (THIS IS A BUG)."));
736 break;
737 case cs_not_started:
738 case cs_finished:
739 switch (f->update_status)
741 case -1:
742 break;
743 case 0:
744 puts (_("# Successfully updated."));
745 break;
746 case 1:
747 assert (question_flag);
748 puts (_("# Needs to be updated (-q is set)."));
749 break;
750 case 2:
751 puts (_("# Failed to be updated."));
752 break;
753 default:
754 puts (_("# Invalid value in `update_status' member!"));
755 fflush (stdout);
756 fflush (stderr);
757 abort ();
759 break;
760 default:
761 puts (_("# Invalid value in `command_state' member!"));
762 fflush (stdout);
763 fflush (stderr);
764 abort ();
767 if (f->variables != 0)
768 print_file_variables (f);
770 if (f->cmds != 0)
771 print_commands (f->cmds);
774 void
775 print_file_data_base ()
777 register unsigned int i, nfiles, per_bucket;
778 register struct file *file;
780 puts (_("\n# Files"));
782 per_bucket = nfiles = 0;
783 for (i = 0; i < FILE_BUCKETS; ++i)
785 register unsigned int this_bucket = 0;
787 for (file = files[i]; file != 0; file = file->next)
789 register struct file *f;
791 ++this_bucket;
793 for (f = file; f != 0; f = f->prev)
794 print_file (f);
797 nfiles += this_bucket;
798 if (this_bucket > per_bucket)
799 per_bucket = this_bucket;
802 if (nfiles == 0)
803 puts (_("\n# No files."));
804 else
806 printf (_("\n# %u files in %u hash buckets.\n"), nfiles, FILE_BUCKETS);
807 #ifndef NO_FLOAT
808 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
809 ((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);
810 #endif
814 /* EOF */