Checkpoint changes. Bug fixes, mostly.
[make.git] / file.c
blob22e95840e9c243f9e995663f1089d07708983aab
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include <assert.h>
21 #include "make.h"
22 #include "dep.h"
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
29 /* Hash table of files the makefile knows how to make. */
31 #ifndef FILE_BUCKETS
32 #define FILE_BUCKETS 1007
33 #endif
34 static struct file *files[FILE_BUCKETS];
36 /* Number of files with the `intermediate' flag set. */
38 unsigned int num_intermediates = 0;
41 /* Access the hash table of all file records.
42 lookup_file given a name, return the struct file * for that name,
43 or nil if there is none.
44 enter_file similar, but create one if there is none. */
46 struct file *
47 lookup_file (name)
48 char *name;
50 register struct file *f;
51 register char *n;
52 register unsigned int hashval;
53 #ifdef VMS
54 register char *lname, *ln;
55 #endif
57 if (*name == '\0')
58 abort ();
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 lname = (char *)malloc(strlen(name) + 1);
65 for (n=name, ln=lname; *n != '\0'; ++n, ++ln)
66 *ln = isupper(*n) ? tolower(*n) : *n;
67 *ln = '\0';
68 name = lname;
70 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
71 name += 2;
72 #endif
73 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
75 name += 2;
76 while (*name == '/')
77 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
78 ++name;
81 if (*name == '\0')
82 /* It was all slashes after a dot. */
83 #ifdef VMS
84 name = "[]";
85 #else
86 #ifdef _AMIGA
87 name = "";
88 #else
89 name = "./";
90 #endif /* AMIGA */
91 #endif /* VMS */
93 hashval = 0;
94 for (n = name; *n != '\0'; ++n)
95 HASHI (hashval, *n);
96 hashval %= FILE_BUCKETS;
98 for (f = files[hashval]; f != 0; f = f->next)
100 if (strieq (f->hname, name))
102 #ifdef VMS
103 free (lname);
104 #endif
105 return f;
108 #ifdef VMS
109 free (lname);
110 #endif
111 return 0;
114 struct file *
115 enter_file (name)
116 char *name;
118 register struct file *f, *new;
119 register char *n;
120 register unsigned int hashval;
121 #ifdef VMS
122 char *lname, *ln;
123 #endif
125 if (*name == '\0')
126 abort ();
128 #ifdef VMS
129 lname = (char *)malloc (strlen (name) + 1);
130 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
132 if (isupper(*n))
133 *ln = tolower(*n);
134 else
135 *ln = *n;
137 *ln = 0;
138 name = lname;
139 #endif
141 hashval = 0;
142 for (n = name; *n != '\0'; ++n)
143 HASHI (hashval, *n);
144 hashval %= FILE_BUCKETS;
146 for (f = files[hashval]; f != 0; f = f->next)
147 if (strieq (f->hname, name))
148 break;
150 if (f != 0 && !f->double_colon)
152 #ifdef VMS
153 free(lname);
154 #endif
155 return f;
158 new = (struct file *) xmalloc (sizeof (struct file));
159 bzero ((char *) new, sizeof (struct file));
160 new->name = new->hname = name;
161 new->update_status = -1;
163 if (f == 0)
165 /* This is a completely new file. */
166 new->next = files[hashval];
167 files[hashval] = new;
169 else
171 /* There is already a double-colon entry for this file. */
172 new->double_colon = f;
173 while (f->prev != 0)
174 f = f->prev;
175 f->prev = new;
178 return new;
181 /* Rehash FILE to NAME. This is not as simple as resetting
182 the `hname' member, since it must be put in a new hash bucket,
183 and possibly merged with an existing file called NAME. */
185 void
186 rehash_file (file, name)
187 register struct file *file;
188 char *name;
190 char *oldname = file->hname;
191 register unsigned int oldhash;
192 register char *n;
194 while (file->renamed != 0)
195 file = file->renamed;
197 /* Find the hash values of the old and new names. */
199 oldhash = 0;
200 for (n = oldname; *n != '\0'; ++n)
201 HASHI (oldhash, *n);
203 file_hash_enter (file, name, oldhash, file->name);
206 /* Rename FILE to NAME. This is not as simple as resetting
207 the `name' member, since it must be put in a new hash bucket,
208 and possibly merged with an existing file called NAME. */
210 void
211 rename_file (file, name)
212 register struct file *file;
213 char *name;
215 rehash_file(file, name);
216 while (file)
218 file->name = file->hname;
219 file = file->prev;
223 void
224 file_hash_enter (file, name, oldhash, oldname)
225 register struct file *file;
226 char *name;
227 unsigned int oldhash;
228 char *oldname;
230 unsigned int oldbucket = oldhash % FILE_BUCKETS;
231 register unsigned int newhash, newbucket;
232 struct file *oldfile;
233 register char *n;
234 register struct file *f;
236 newhash = 0;
237 for (n = name; *n != '\0'; ++n)
238 HASHI (newhash, *n);
239 newbucket = newhash % FILE_BUCKETS;
241 /* Look for an existing file under the new name. */
243 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
244 if (strieq (oldfile->hname, name))
245 break;
247 /* If the old file is the same as the new file, something's wrong. */
248 assert (oldfile != file);
250 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
252 /* Remove FILE from its hash bucket. */
254 struct file *lastf = 0;
256 for (f = files[oldbucket]; f != file; f = f->next)
257 lastf = f;
259 if (lastf == 0)
260 files[oldbucket] = f->next;
261 else
262 lastf->next = f->next;
265 /* Give FILE its new name. */
267 file->hname = name;
268 for (f = file->double_colon; f != 0; f = f->prev)
269 f->hname = name;
271 if (oldfile == 0)
273 /* There is no existing file with the new name. */
275 if (newbucket != oldbucket)
277 /* Put FILE in its new hash bucket. */
278 file->next = files[newbucket];
279 files[newbucket] = file;
282 else
284 /* There is an existing file with the new name.
285 We must merge FILE into the existing file. */
287 register struct dep *d;
289 if (file->cmds != 0)
291 if (oldfile->cmds == 0)
292 oldfile->cmds = file->cmds;
293 else if (file->cmds != oldfile->cmds)
295 /* We have two sets of commands. We will go with the
296 one given in the rule explicitly mentioning this name,
297 but give a message to let the user know what's going on. */
298 if (oldfile->cmds->fileinfo.filenm != 0)
299 error (&file->cmds->fileinfo,
300 "Commands were specified for \
301 file `%s' at %s:%lu,",
302 oldname, oldfile->cmds->fileinfo.filenm,
303 oldfile->cmds->fileinfo.lineno);
304 else
305 error (&file->cmds->fileinfo,
306 "Commands for file `%s' were found by \
307 implicit rule search,",
308 oldname);
309 error (&file->cmds->fileinfo,
310 "but `%s' is now considered the same file \
311 as `%s'.",
312 oldname, name);
313 error (&file->cmds->fileinfo,
314 "Commands for `%s' will be ignored \
315 in favor of those for `%s'.",
316 name, oldname);
320 /* Merge the dependencies of the two files. */
322 d = oldfile->deps;
323 if (d == 0)
324 oldfile->deps = file->deps;
325 else
327 while (d->next != 0)
328 d = d->next;
329 d->next = file->deps;
332 merge_variable_set_lists (&oldfile->variables, file->variables);
334 if (oldfile->double_colon && file->is_target && !file->double_colon)
335 fatal (NILF, "can't rename single-colon `%s' to double-colon `%s'",
336 oldname, name);
337 if (!oldfile->double_colon && file->double_colon)
339 if (oldfile->is_target)
340 fatal (NILF, "can't rename double-colon `%s' to single-colon `%s'",
341 oldname, name);
342 else
343 oldfile->double_colon = file->double_colon;
346 if (file->last_mtime > oldfile->last_mtime)
347 /* %%% Kludge so -W wins on a file that gets vpathized. */
348 oldfile->last_mtime = file->last_mtime;
350 #define MERGE(field) oldfile->field |= file->field
351 MERGE (precious);
352 MERGE (tried_implicit);
353 MERGE (updating);
354 MERGE (updated);
355 MERGE (is_target);
356 MERGE (cmd_target);
357 MERGE (phony);
358 MERGE (ignore_vpath);
359 #undef MERGE
361 file->renamed = oldfile;
365 /* Remove all nonprecious intermediate files.
366 If SIG is nonzero, this was caused by a fatal signal,
367 meaning that a different message will be printed, and
368 the message will go to stderr rather than stdout. */
370 void
371 remove_intermediates (sig)
372 int sig;
374 register int i;
375 register struct file *f;
376 char doneany;
378 if (question_flag || touch_flag)
379 return;
380 if (sig && just_print_flag)
381 return;
383 doneany = 0;
384 for (i = 0; i < FILE_BUCKETS; ++i)
385 for (f = files[i]; f != 0; f = f->next)
386 if (f->intermediate && (f->dontcare || !f->precious)
387 && !f->secondary)
389 int status;
390 if (f->update_status == -1)
391 /* If nothing would have created this file yet,
392 don't print an "rm" command for it. */
393 continue;
394 else if (just_print_flag)
395 status = 0;
396 else
398 status = unlink (f->name);
399 if (status < 0 && errno == ENOENT)
400 continue;
402 if (!f->dontcare)
404 if (sig)
405 error (NILF, "*** Deleting intermediate file `%s'", f->name);
406 else if (!silent_flag)
408 if (! doneany)
410 fputs ("rm ", stdout);
411 doneany = 1;
413 else
414 putchar (' ');
415 fputs (f->name, stdout);
416 fflush (stdout);
418 if (status < 0)
419 perror_with_name ("unlink: ", f->name);
423 if (doneany && !sig)
425 putchar ('\n');
426 fflush (stdout);
430 /* For each dependency of each file, make the `struct dep' point
431 at the appropriate `struct file' (which may have to be created).
433 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
434 and various other special targets. */
436 void
437 snap_deps ()
439 register struct file *f, *f2;
440 register struct dep *d;
441 register int i;
443 /* Enter each dependency name as a file. */
444 for (i = 0; i < FILE_BUCKETS; ++i)
445 for (f = files[i]; f != 0; f = f->next)
446 for (f2 = f; f2 != 0; f2 = f2->prev)
447 for (d = f2->deps; d != 0; d = d->next)
448 if (d->name != 0)
450 d->file = lookup_file (d->name);
451 if (d->file == 0)
452 d->file = enter_file (d->name);
453 else
454 free (d->name);
455 d->name = 0;
458 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
459 for (d = f->deps; d != 0; d = d->next)
460 for (f2 = d->file; f2 != 0; f2 = f2->prev)
461 f2->precious = 1;
463 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
464 for (d = f->deps; d != 0; d = d->next)
465 for (f2 = d->file; f2 != 0; f2 = f2->prev)
467 /* Mark this file as phony and nonexistent. */
468 f2->phony = 1;
469 f2->last_mtime = (FILE_TIMESTAMP) -1;
472 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
474 /* .INTERMEDIATE with deps listed
475 marks those deps as intermediate files. */
476 for (d = f->deps; d != 0; d = d->next)
477 for (f2 = d->file; f2 != 0; f2 = f2->prev)
478 f2->intermediate = 1;
479 /* .INTERMEDIATE with no deps does nothing.
480 Marking all files as intermediates is useless
481 since the goal targets would be deleted after they are built. */
484 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
486 /* .SECONDARY with deps listed
487 marks those deps as intermediate files
488 in that they don't get rebuilt if not actually needed;
489 but unlike real intermediate files,
490 these are not deleted after make finishes. */
491 if (f->deps)
493 for (d = f->deps; d != 0; d = d->next)
494 for (f2 = d->file; f2 != 0; f2 = f2->prev)
495 f2->intermediate = f2->secondary = 1;
497 /* .SECONDARY with no deps listed marks *all* files that way. */
498 else
500 int i;
501 for (i = 0; i < FILE_BUCKETS; i++)
502 for (f2 = files[i]; f2; f2= f2->next)
503 f2->intermediate = f2->secondary = 1;
507 f = lookup_file (".EXPORT_ALL_VARIABLES");
508 if (f != 0 && f->is_target)
509 export_all_variables = 1;
511 f = lookup_file (".IGNORE");
512 if (f != 0 && f->is_target)
514 if (f->deps == 0)
515 ignore_errors_flag = 1;
516 else
517 for (d = f->deps; d != 0; d = d->next)
518 for (f2 = d->file; f2 != 0; f2 = f2->prev)
519 f2->command_flags |= COMMANDS_NOERROR;
522 f = lookup_file (".SILENT");
523 if (f != 0 && f->is_target)
525 if (f->deps == 0)
526 silent_flag = 1;
527 else
528 for (d = f->deps; d != 0; d = d->next)
529 for (f2 = d->file; f2 != 0; f2 = f2->prev)
530 f2->command_flags |= COMMANDS_SILENT;
533 f = lookup_file (".POSIX");
534 if (f != 0 && f->is_target)
535 posix_pedantic = 1;
538 /* Set the `command_state' member of FILE and all its `also_make's. */
540 void
541 set_command_state (file, state)
542 struct file *file;
543 int state;
545 struct dep *d;
547 file->command_state = state;
549 for (d = file->also_make; d != 0; d = d->next)
550 d->file->command_state = state;
553 /* Get and print file timestamps. */
555 FILE_TIMESTAMP
556 file_timestamp_now ()
558 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
559 struct timespec timespec;
560 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
561 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec.tv_sec, timespec.tv_nsec);
562 #endif
563 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
566 void
567 file_timestamp_sprintf (p, ts)
568 char *p;
569 FILE_TIMESTAMP ts;
571 time_t t = FILE_TIMESTAMP_S (ts);
572 struct tm *tm = localtime (&t);
574 if (tm)
575 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
576 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
577 tm->tm_hour, tm->tm_min, tm->tm_sec);
578 else if (t < 0)
579 sprintf (p, "%ld", (long) t);
580 else
581 sprintf (p, "%lu", (unsigned long) t);
582 p += strlen (p);
584 /* Append nanoseconds as a fraction, but remove trailing zeros.
585 We don't know the actual timestamp resolution, since clock_getres
586 applies only to local times, whereas this timestamp might come
587 from a remote filesystem. So removing trailing zeros is the
588 best guess that we can do. */
589 sprintf (p, ".%09ld", (long) FILE_TIMESTAMP_NS (ts));
590 p += strlen (p) - 1;
591 while (*p == '0')
592 p--;
593 p += *p != '.';
595 *p = '\0';
598 /* Print the data base of files. */
600 static void
601 print_file (f)
602 struct file *f;
604 register struct dep *d;
606 putchar ('\n');
607 if (!f->is_target)
608 puts ("# Not a target:");
609 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
611 for (d = f->deps; d != 0; d = d->next)
612 printf (" %s", dep_name (d));
613 putchar ('\n');
615 if (f->precious)
616 puts ("# Precious file (dependency of .PRECIOUS).");
617 if (f->phony)
618 puts ("# Phony target (dependency of .PHONY).");
619 if (f->cmd_target)
620 puts ("# Command-line target.");
621 if (f->dontcare)
622 puts ("# A default or MAKEFILES makefile.");
623 printf ("# Implicit rule search has%s been done.\n",
624 f->tried_implicit ? "" : " not");
625 if (f->stem != 0)
626 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
627 if (f->intermediate)
628 puts ("# File is an intermediate dependency.");
629 if (f->also_make != 0)
631 fputs ("# Also makes:", stdout);
632 for (d = f->also_make; d != 0; d = d->next)
633 printf (" %s", dep_name (d));
634 putchar ('\n');
636 if (f->last_mtime == 0)
637 puts ("# Modification time never checked.");
638 else if (f->last_mtime == (FILE_TIMESTAMP) -1)
639 puts ("# File does not exist.");
640 else
642 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
643 file_timestamp_sprintf (buf, f->last_mtime);
644 printf ("# Last modified %s\n", buf);
646 printf ("# File has%s been updated.\n",
647 f->updated ? "" : " not");
648 switch (f->command_state)
650 case cs_running:
651 puts ("# Commands currently running (THIS IS A BUG).");
652 break;
653 case cs_deps_running:
654 puts ("# Dependencies commands running (THIS IS A BUG).");
655 break;
656 case cs_not_started:
657 case cs_finished:
658 switch (f->update_status)
660 case -1:
661 break;
662 case 0:
663 puts ("# Successfully updated.");
664 break;
665 case 1:
666 assert (question_flag);
667 puts ("# Needs to be updated (-q is set).");
668 break;
669 case 2:
670 puts ("# Failed to be updated.");
671 break;
672 default:
673 puts ("# Invalid value in `update_status' member!");
674 fflush (stdout);
675 fflush (stderr);
676 abort ();
678 break;
679 default:
680 puts ("# Invalid value in `command_state' member!");
681 fflush (stdout);
682 fflush (stderr);
683 abort ();
686 if (f->variables != 0)
687 print_file_variables (f);
689 if (f->cmds != 0)
690 print_commands (f->cmds);
693 void
694 print_file_data_base ()
696 register unsigned int i, nfiles, per_bucket;
697 register struct file *file;
699 puts ("\n# Files");
701 per_bucket = nfiles = 0;
702 for (i = 0; i < FILE_BUCKETS; ++i)
704 register unsigned int this_bucket = 0;
706 for (file = files[i]; file != 0; file = file->next)
708 register struct file *f;
710 ++this_bucket;
712 for (f = file; f != 0; f = f->prev)
713 print_file (f);
716 nfiles += this_bucket;
717 if (this_bucket > per_bucket)
718 per_bucket = this_bucket;
721 if (nfiles == 0)
722 puts ("\n# No files.");
723 else
725 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
726 #ifndef NO_FLOAT
727 printf ("# average %.3f files per bucket, max %u files in one bucket.\n",
728 ((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);
729 #endif
733 /* EOF */