* New config.sub and config.guess
[make.git] / file.c
blob10ae1b84848eb410f0721f0bbf3d15f57f395981
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 else 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 = (FILE_TIMESTAMP) -1;
479 f2->mtime_before_update = (FILE_TIMESTAMP) -1;
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 /* Get and print file timestamps. */
569 FILE_TIMESTAMP
570 file_timestamp_now ()
572 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
573 struct timespec timespec;
574 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
575 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec.tv_sec, timespec.tv_nsec);
576 #endif
577 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
580 void
581 file_timestamp_sprintf (p, ts)
582 char *p;
583 FILE_TIMESTAMP ts;
585 time_t t = FILE_TIMESTAMP_S (ts);
586 struct tm *tm = localtime (&t);
588 if (tm)
589 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
590 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
591 tm->tm_hour, tm->tm_min, tm->tm_sec);
592 else if (t < 0)
593 sprintf (p, "%ld", (long) t);
594 else
595 sprintf (p, "%lu", (unsigned long) t);
596 p += strlen (p);
598 /* Append nanoseconds as a fraction, but remove trailing zeros.
599 We don't know the actual timestamp resolution, since clock_getres
600 applies only to local times, whereas this timestamp might come
601 from a remote filesystem. So removing trailing zeros is the
602 best guess that we can do. */
603 sprintf (p, ".%09ld", (long) FILE_TIMESTAMP_NS (ts));
604 p += strlen (p) - 1;
605 while (*p == '0')
606 p--;
607 p += *p != '.';
609 *p = '\0';
612 /* Print the data base of files. */
614 static void
615 print_file (f)
616 struct file *f;
618 register struct dep *d;
620 putchar ('\n');
621 if (!f->is_target)
622 puts (_("# Not a target:"));
623 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
625 for (d = f->deps; d != 0; d = d->next)
626 printf (" %s", dep_name (d));
627 putchar ('\n');
629 if (f->precious)
630 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
631 if (f->phony)
632 puts (_("# Phony target (prerequisite of .PHONY)."));
633 if (f->cmd_target)
634 puts (_("# Command-line target."));
635 if (f->dontcare)
636 puts (_("# A default or MAKEFILES makefile."));
637 puts (f->tried_implicit
638 ? _("# Implicit rule search has been done.")
639 : _("# Implicit rule search has not been done."));
640 if (f->stem != 0)
641 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
642 if (f->intermediate)
643 puts (_("# File is an intermediate prerequisite."));
644 if (f->also_make != 0)
646 fputs (_("# Also makes:"), stdout);
647 for (d = f->also_make; d != 0; d = d->next)
648 printf (" %s", dep_name (d));
649 putchar ('\n');
651 if (f->last_mtime == 0)
652 puts (_("# Modification time never checked."));
653 else if (f->last_mtime == (FILE_TIMESTAMP) -1)
654 puts (_("# File does not exist."));
655 else
657 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
658 file_timestamp_sprintf (buf, f->last_mtime);
659 printf (_("# Last modified %s\n"), buf);
661 puts (f->updated
662 ? _("# File has been updated.") : _("# File has not been updated."));
663 switch (f->command_state)
665 case cs_running:
666 puts (_("# Commands currently running (THIS IS A BUG)."));
667 break;
668 case cs_deps_running:
669 puts (_("# Dependencies commands running (THIS IS A BUG)."));
670 break;
671 case cs_not_started:
672 case cs_finished:
673 switch (f->update_status)
675 case -1:
676 break;
677 case 0:
678 puts (_("# Successfully updated."));
679 break;
680 case 1:
681 assert (question_flag);
682 puts (_("# Needs to be updated (-q is set)."));
683 break;
684 case 2:
685 puts (_("# Failed to be updated."));
686 break;
687 default:
688 puts (_("# Invalid value in `update_status' member!"));
689 fflush (stdout);
690 fflush (stderr);
691 abort ();
693 break;
694 default:
695 puts (_("# Invalid value in `command_state' member!"));
696 fflush (stdout);
697 fflush (stderr);
698 abort ();
701 if (f->variables != 0)
702 print_file_variables (f);
704 if (f->cmds != 0)
705 print_commands (f->cmds);
708 void
709 print_file_data_base ()
711 register unsigned int i, nfiles, per_bucket;
712 register struct file *file;
714 puts (_("\n# Files"));
716 per_bucket = nfiles = 0;
717 for (i = 0; i < FILE_BUCKETS; ++i)
719 register unsigned int this_bucket = 0;
721 for (file = files[i]; file != 0; file = file->next)
723 register struct file *f;
725 ++this_bucket;
727 for (f = file; f != 0; f = f->prev)
728 print_file (f);
731 nfiles += this_bucket;
732 if (this_bucket > per_bucket)
733 per_bucket = this_bucket;
736 if (nfiles == 0)
737 puts (_("\n# No files."));
738 else
740 printf (_("\n# %u files in %u hash buckets.\n"), nfiles, FILE_BUCKETS);
741 #ifndef NO_FLOAT
742 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
743 ((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);
744 #endif
748 /* EOF */