* More fixes for configuring gettext correctly.
[make.git] / file.c
blobd9e4c1bd47a4fa36dd65043b0ed3daa9310b5468
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 <assert.h>
22 #include "make.h"
23 #include "dep.h"
24 #include "filedef.h"
25 #include "job.h"
26 #include "commands.h"
27 #include "variable.h"
30 /* Hash table of files the makefile knows how to make. */
32 #ifndef FILE_BUCKETS
33 #define FILE_BUCKETS 1007
34 #endif
35 static struct file *files[FILE_BUCKETS];
37 /* Number of files with the `intermediate' flag set. */
39 unsigned int num_intermediates = 0;
41 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
43 unsigned int considered = 0;
45 /* Access the hash table of all file records.
46 lookup_file given a name, return the struct file * for that name,
47 or nil if there is none.
48 enter_file similar, but create one if there is none. */
50 struct file *
51 lookup_file (name)
52 char *name;
54 register struct file *f;
55 register char *n;
56 register unsigned int hashval;
57 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
58 register char *lname, *ln;
59 #endif
61 assert (*name != '\0');
63 /* This is also done in parse_file_seq, so this is redundant
64 for names read from makefiles. It is here for names passed
65 on the command line. */
66 #ifdef VMS
67 # ifndef WANT_CASE_SENSITIVE_TARGETS
68 lname = (char *)malloc(strlen(name) + 1);
69 for (n=name, ln=lname; *n != '\0'; ++n, ++ln)
70 *ln = isupper((unsigned char)*n) ? tolower((unsigned char)*n) : *n;
71 *ln = '\0';
72 name = lname;
73 # endif
75 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
76 name += 2;
77 #endif
78 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
80 name += 2;
81 while (*name == '/')
82 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
83 ++name;
86 if (*name == '\0')
87 /* It was all slashes after a dot. */
88 #ifdef VMS
89 name = "[]";
90 #else
91 #ifdef _AMIGA
92 name = "";
93 #else
94 name = "./";
95 #endif /* AMIGA */
96 #endif /* VMS */
98 hashval = 0;
99 for (n = name; *n != '\0'; ++n)
100 HASHI (hashval, *n);
101 hashval %= FILE_BUCKETS;
103 for (f = files[hashval]; f != 0; f = f->next)
105 if (strieq (f->hname, name))
107 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
108 free (lname);
109 #endif
110 return f;
113 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
114 free (lname);
115 #endif
116 return 0;
119 struct file *
120 enter_file (name)
121 char *name;
123 register struct file *f, *new;
124 register char *n;
125 register unsigned int hashval;
126 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
127 char *lname, *ln;
128 #endif
130 assert (*name != '\0');
132 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
133 lname = (char *)malloc (strlen (name) + 1);
134 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
136 if (isupper((unsigned char)*n))
137 *ln = tolower((unsigned char)*n);
138 else
139 *ln = *n;
141 *ln = 0;
142 /* Creates a possible leak, old value of name is unreachable, but I
143 currently don't know how to fix it. */
144 name = lname;
145 #endif
147 hashval = 0;
148 for (n = name; *n != '\0'; ++n)
149 HASHI (hashval, *n);
150 hashval %= FILE_BUCKETS;
152 for (f = files[hashval]; f != 0; f = f->next)
153 if (strieq (f->hname, name))
154 break;
156 if (f != 0 && !f->double_colon)
158 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
159 free(lname);
160 #endif
161 return f;
164 new = (struct file *) xmalloc (sizeof (struct file));
165 bzero ((char *) new, sizeof (struct file));
166 new->name = new->hname = name;
167 new->update_status = -1;
169 if (f == 0)
171 /* This is a completely new file. */
172 new->next = files[hashval];
173 files[hashval] = new;
175 else
177 /* There is already a double-colon entry for this file. */
178 new->double_colon = f;
179 while (f->prev != 0)
180 f = f->prev;
181 f->prev = new;
184 return new;
187 /* Rehash FILE to NAME. This is not as simple as resetting
188 the `hname' member, since it must be put in a new hash bucket,
189 and possibly merged with an existing file called NAME. */
191 void
192 rehash_file (file, name)
193 register struct file *file;
194 char *name;
196 char *oldname = file->hname;
197 register unsigned int oldhash;
198 register char *n;
200 while (file->renamed != 0)
201 file = file->renamed;
203 /* Find the hash values of the old and new names. */
205 oldhash = 0;
206 for (n = oldname; *n != '\0'; ++n)
207 HASHI (oldhash, *n);
209 file_hash_enter (file, name, oldhash, file->name);
212 /* Rename FILE to NAME. This is not as simple as resetting
213 the `name' member, since it must be put in a new hash bucket,
214 and possibly merged with an existing file called NAME. */
216 void
217 rename_file (file, name)
218 register struct file *file;
219 char *name;
221 rehash_file(file, name);
222 while (file)
224 file->name = file->hname;
225 file = file->prev;
229 void
230 file_hash_enter (file, name, oldhash, oldname)
231 register struct file *file;
232 char *name;
233 unsigned int oldhash;
234 char *oldname;
236 unsigned int oldbucket = oldhash % FILE_BUCKETS;
237 register unsigned int newhash, newbucket;
238 struct file *oldfile;
239 register char *n;
240 register struct file *f;
242 newhash = 0;
243 for (n = name; *n != '\0'; ++n)
244 HASHI (newhash, *n);
245 newbucket = newhash % FILE_BUCKETS;
247 /* Look for an existing file under the new name. */
249 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
250 if (strieq (oldfile->hname, name))
251 break;
253 /* If the old file is the same as the new file, never mind. */
254 if (oldfile == file)
255 return;
257 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
259 /* Remove FILE from its hash bucket. */
261 struct file *lastf = 0;
263 for (f = files[oldbucket]; f != file; f = f->next)
264 lastf = f;
266 if (lastf == 0)
267 files[oldbucket] = f->next;
268 else
269 lastf->next = f->next;
272 /* Give FILE its new name. */
274 file->hname = name;
275 for (f = file->double_colon; f != 0; f = f->prev)
276 f->hname = name;
278 if (oldfile == 0)
280 /* There is no existing file with the new name. */
282 if (newbucket != oldbucket)
284 /* Put FILE in its new hash bucket. */
285 file->next = files[newbucket];
286 files[newbucket] = file;
289 else
291 /* There is an existing file with the new name.
292 We must merge FILE into the existing file. */
294 register struct dep *d;
296 if (file->cmds != 0)
298 if (oldfile->cmds == 0)
299 oldfile->cmds = file->cmds;
300 else if (file->cmds != oldfile->cmds)
302 /* We have two sets of commands. We will go with the
303 one given in the rule explicitly mentioning this name,
304 but give a message to let the user know what's going on. */
305 if (oldfile->cmds->fileinfo.filenm != 0)
306 error (&file->cmds->fileinfo,
307 _("Commands were specified for \
308 file `%s' at %s:%lu,"),
309 oldname, oldfile->cmds->fileinfo.filenm,
310 oldfile->cmds->fileinfo.lineno);
311 else
312 error (&file->cmds->fileinfo,
313 _("Commands for file `%s' were found by \
314 implicit rule search,"),
315 oldname);
316 error (&file->cmds->fileinfo,
317 _("but `%s' is now considered the same file \
318 as `%s'."),
319 oldname, name);
320 error (&file->cmds->fileinfo,
321 _("Commands for `%s' will be ignored \
322 in favor of those for `%s'."),
323 name, oldname);
327 /* Merge the dependencies of the two files. */
329 d = oldfile->deps;
330 if (d == 0)
331 oldfile->deps = file->deps;
332 else
334 while (d->next != 0)
335 d = d->next;
336 d->next = file->deps;
339 merge_variable_set_lists (&oldfile->variables, file->variables);
341 if (oldfile->double_colon && file->is_target && !file->double_colon)
342 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
343 oldname, name);
344 if (!oldfile->double_colon && file->double_colon)
346 if (oldfile->is_target)
347 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
348 oldname, name);
349 else
350 oldfile->double_colon = file->double_colon;
353 if (file->last_mtime > oldfile->last_mtime)
354 /* %%% Kludge so -W wins on a file that gets vpathized. */
355 oldfile->last_mtime = file->last_mtime;
357 oldfile->mtime_before_update = file->mtime_before_update;
359 #define MERGE(field) oldfile->field |= file->field
360 MERGE (precious);
361 MERGE (tried_implicit);
362 MERGE (updating);
363 MERGE (updated);
364 MERGE (is_target);
365 MERGE (cmd_target);
366 MERGE (phony);
367 MERGE (ignore_vpath);
368 #undef MERGE
370 file->renamed = oldfile;
374 /* Remove all nonprecious intermediate files.
375 If SIG is nonzero, this was caused by a fatal signal,
376 meaning that a different message will be printed, and
377 the message will go to stderr rather than stdout. */
379 void
380 remove_intermediates (sig)
381 int sig;
383 register int i;
384 register struct file *f;
385 char doneany;
387 if (question_flag || touch_flag)
388 return;
389 if (sig && just_print_flag)
390 return;
392 doneany = 0;
393 for (i = 0; i < FILE_BUCKETS; ++i)
394 for (f = files[i]; f != 0; f = f->next)
395 if (f->intermediate && (f->dontcare || !f->precious)
396 && !f->secondary && !f->cmd_target)
398 int status;
399 if (f->update_status == -1)
400 /* If nothing would have created this file yet,
401 don't print an "rm" command for it. */
402 continue;
403 if (just_print_flag)
404 status = 0;
405 else
407 status = unlink (f->name);
408 if (status < 0 && errno == ENOENT)
409 continue;
411 if (!f->dontcare)
413 if (sig)
414 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
415 else if (!silent_flag)
417 if (! doneany)
419 fputs ("rm ", stdout);
420 doneany = 1;
422 else
423 putchar (' ');
424 fputs (f->name, stdout);
425 fflush (stdout);
427 if (status < 0)
428 perror_with_name ("unlink: ", f->name);
432 if (doneany && !sig)
434 putchar ('\n');
435 fflush (stdout);
439 /* For each dependency of each file, make the `struct dep' point
440 at the appropriate `struct file' (which may have to be created).
442 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
443 and various other special targets. */
445 void
446 snap_deps ()
448 register struct file *f, *f2;
449 register struct dep *d;
450 register int i;
452 /* Enter each dependency name as a file. */
453 for (i = 0; i < FILE_BUCKETS; ++i)
454 for (f = files[i]; f != 0; f = f->next)
455 for (f2 = f; f2 != 0; f2 = f2->prev)
456 for (d = f2->deps; d != 0; d = d->next)
457 if (d->name != 0)
459 d->file = lookup_file (d->name);
460 if (d->file == 0)
461 d->file = enter_file (d->name);
462 else
463 free (d->name);
464 d->name = 0;
467 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
468 for (d = f->deps; d != 0; d = d->next)
469 for (f2 = d->file; f2 != 0; f2 = f2->prev)
470 f2->precious = 1;
472 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
473 for (d = f->deps; d != 0; d = d->next)
474 for (f2 = d->file; f2 != 0; f2 = f2->prev)
476 /* Mark this file as phony and nonexistent. */
477 f2->phony = 1;
478 f2->last_mtime = NONEXISTENT_MTIME;
479 f2->mtime_before_update = NONEXISTENT_MTIME;
482 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
484 /* .INTERMEDIATE with deps listed
485 marks those deps as intermediate files. */
486 for (d = f->deps; d != 0; d = d->next)
487 for (f2 = d->file; f2 != 0; f2 = f2->prev)
488 f2->intermediate = 1;
489 /* .INTERMEDIATE with no deps does nothing.
490 Marking all files as intermediates is useless
491 since the goal targets would be deleted after they are built. */
494 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
496 /* .SECONDARY with deps listed
497 marks those deps as intermediate files
498 in that they don't get rebuilt if not actually needed;
499 but unlike real intermediate files,
500 these are not deleted after make finishes. */
501 if (f->deps)
503 for (d = f->deps; d != 0; d = d->next)
504 for (f2 = d->file; f2 != 0; f2 = f2->prev)
505 f2->intermediate = f2->secondary = 1;
507 /* .SECONDARY with no deps listed marks *all* files that way. */
508 else
510 int i;
511 for (i = 0; i < FILE_BUCKETS; i++)
512 for (f2 = files[i]; f2; f2= f2->next)
513 f2->intermediate = f2->secondary = 1;
517 f = lookup_file (".EXPORT_ALL_VARIABLES");
518 if (f != 0 && f->is_target)
519 export_all_variables = 1;
521 f = lookup_file (".IGNORE");
522 if (f != 0 && f->is_target)
524 if (f->deps == 0)
525 ignore_errors_flag = 1;
526 else
527 for (d = f->deps; d != 0; d = d->next)
528 for (f2 = d->file; f2 != 0; f2 = f2->prev)
529 f2->command_flags |= COMMANDS_NOERROR;
532 f = lookup_file (".SILENT");
533 if (f != 0 && f->is_target)
535 if (f->deps == 0)
536 silent_flag = 1;
537 else
538 for (d = f->deps; d != 0; d = d->next)
539 for (f2 = d->file; f2 != 0; f2 = f2->prev)
540 f2->command_flags |= COMMANDS_SILENT;
543 f = lookup_file (".POSIX");
544 if (f != 0 && f->is_target)
545 posix_pedantic = 1;
547 f = lookup_file (".NOTPARALLEL");
548 if (f != 0 && f->is_target)
549 not_parallel = 1;
552 /* Set the `command_state' member of FILE and all its `also_make's. */
554 void
555 set_command_state (file, state)
556 struct file *file;
557 int state;
559 struct dep *d;
561 file->command_state = state;
563 for (d = file->also_make; d != 0; d = d->next)
564 d->file->command_state = state;
567 /* Convert an external file timestamp to internal form. */
569 FILE_TIMESTAMP
570 file_timestamp_cons (fname, s, ns)
571 char const *fname;
572 time_t s;
573 int ns;
575 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
576 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
577 FILE_TIMESTAMP ts = product + offset;
579 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
580 && product <= ts && ts <= ORDINARY_MTIME_MAX))
582 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
583 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
584 file_timestamp_sprintf (buf, ts);
585 error (NILF, _("%s: Timestamp out of range; substituting %s"),
586 fname ? fname : _("Current time"), buf);
589 return ts;
592 /* Get and print file timestamps. */
594 FILE_TIMESTAMP
595 file_timestamp_now ()
597 time_t s;
598 int ns;
600 /* Don't bother with high-resolution clocks if file timestamps have
601 only one-second resolution. The code below should work, but it's
602 not worth the hassle of debugging it on hosts where it fails. */
603 if (FILE_TIMESTAMP_HI_RES)
605 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
607 struct timespec timespec;
608 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
610 s = timespec.tv_sec;
611 ns = timespec.tv_nsec;
612 goto got_time;
615 #endif
616 #if HAVE_GETTIMEOFDAY
618 struct timeval timeval;
619 if (gettimeofday (&timeval, 0) == 0)
621 s = timeval.tv_sec;
622 ns = timeval.tv_usec * 1000;
623 goto got_time;
626 #endif
629 s = time ((time_t *) 0);
630 ns = 0;
632 got_time:
633 return file_timestamp_cons (0, s, ns);
636 void
637 file_timestamp_sprintf (p, ts)
638 char *p;
639 FILE_TIMESTAMP ts;
641 time_t t = FILE_TIMESTAMP_S (ts);
642 struct tm *tm = localtime (&t);
644 if (tm)
645 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
646 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
647 tm->tm_hour, tm->tm_min, tm->tm_sec);
648 else if (t < 0)
649 sprintf (p, "%ld", (long) t);
650 else
651 sprintf (p, "%lu", (unsigned long) t);
652 p += strlen (p);
654 /* Append nanoseconds as a fraction, but remove trailing zeros.
655 We don't know the actual timestamp resolution, since clock_getres
656 applies only to local times, whereas this timestamp might come
657 from a remote filesystem. So removing trailing zeros is the
658 best guess that we can do. */
659 sprintf (p, ".%09ld", (long) FILE_TIMESTAMP_NS (ts));
660 p += strlen (p) - 1;
661 while (*p == '0')
662 p--;
663 p += *p != '.';
665 *p = '\0';
668 /* Print the data base of files. */
670 static void
671 print_file (f)
672 struct file *f;
674 register struct dep *d;
676 putchar ('\n');
677 if (!f->is_target)
678 puts (_("# Not a target:"));
679 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
681 for (d = f->deps; d != 0; d = d->next)
682 printf (" %s", dep_name (d));
683 putchar ('\n');
685 if (f->precious)
686 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
687 if (f->phony)
688 puts (_("# Phony target (prerequisite of .PHONY)."));
689 if (f->cmd_target)
690 puts (_("# Command-line target."));
691 if (f->dontcare)
692 puts (_("# A default or MAKEFILES makefile."));
693 puts (f->tried_implicit
694 ? _("# Implicit rule search has been done.")
695 : _("# Implicit rule search has not been done."));
696 if (f->stem != 0)
697 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
698 if (f->intermediate)
699 puts (_("# File is an intermediate prerequisite."));
700 if (f->also_make != 0)
702 fputs (_("# Also makes:"), stdout);
703 for (d = f->also_make; d != 0; d = d->next)
704 printf (" %s", dep_name (d));
705 putchar ('\n');
707 if (f->last_mtime == UNKNOWN_MTIME)
708 puts (_("# Modification time never checked."));
709 else if (f->last_mtime == NONEXISTENT_MTIME)
710 puts (_("# File does not exist."));
711 else if (f->last_mtime == OLD_MTIME)
712 puts (_("# File is very old."));
713 else
715 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
716 file_timestamp_sprintf (buf, f->last_mtime);
717 printf (_("# Last modified %s\n"), buf);
719 puts (f->updated
720 ? _("# File has been updated.") : _("# File has not been updated."));
721 switch (f->command_state)
723 case cs_running:
724 puts (_("# Commands currently running (THIS IS A BUG)."));
725 break;
726 case cs_deps_running:
727 puts (_("# Dependencies commands running (THIS IS A BUG)."));
728 break;
729 case cs_not_started:
730 case cs_finished:
731 switch (f->update_status)
733 case -1:
734 break;
735 case 0:
736 puts (_("# Successfully updated."));
737 break;
738 case 1:
739 assert (question_flag);
740 puts (_("# Needs to be updated (-q is set)."));
741 break;
742 case 2:
743 puts (_("# Failed to be updated."));
744 break;
745 default:
746 puts (_("# Invalid value in `update_status' member!"));
747 fflush (stdout);
748 fflush (stderr);
749 abort ();
751 break;
752 default:
753 puts (_("# Invalid value in `command_state' member!"));
754 fflush (stdout);
755 fflush (stderr);
756 abort ();
759 if (f->variables != 0)
760 print_file_variables (f);
762 if (f->cmds != 0)
763 print_commands (f->cmds);
766 void
767 print_file_data_base ()
769 register unsigned int i, nfiles, per_bucket;
770 register struct file *file;
772 puts (_("\n# Files"));
774 per_bucket = nfiles = 0;
775 for (i = 0; i < FILE_BUCKETS; ++i)
777 register unsigned int this_bucket = 0;
779 for (file = files[i]; file != 0; file = file->next)
781 register struct file *f;
783 ++this_bucket;
785 for (f = file; f != 0; f = f->prev)
786 print_file (f);
789 nfiles += this_bucket;
790 if (this_bucket > per_bucket)
791 per_bucket = this_bucket;
794 if (nfiles == 0)
795 puts (_("\n# No files."));
796 else
798 printf (_("\n# %u files in %u hash buckets.\n"), nfiles, FILE_BUCKETS);
799 #ifndef NO_FLOAT
800 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
801 ((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);
802 #endif
806 /* EOF */