Updated from libc
[make.git] / file.c
blob5bf47081e52faab866b9eaee27e187601e123f82
1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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 "make.h"
20 #include "dep.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
25 #include <assert.h>
28 /* Hash table of files the makefile knows how to make. */
30 #ifndef FILE_BUCKETS
31 #define FILE_BUCKETS 1007
32 #endif
33 static struct file *files[FILE_BUCKETS];
35 /* Number of files with the `intermediate' flag set. */
37 unsigned int num_intermediates = 0;
40 /* Access the hash table of all file records.
41 lookup_file given a name, return the struct file * for that name,
42 or nil if there is none.
43 enter_file similar, but create one if there is none. */
45 struct file *
46 lookup_file (name)
47 char *name;
49 register struct file *f;
50 register char *n;
51 register unsigned int hashval;
53 if (*name == '\0')
54 abort ();
56 /* This is also done in parse_file_seq, so this is redundant
57 for names read from makefiles. It is here for names passed
58 on the command line. */
59 #ifdef VMS
60 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
61 name += 2;
62 #endif
63 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
65 name += 2;
66 while (*name == '/')
67 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
68 ++name;
71 if (*name == '\0')
72 /* It was all slashes after a dot. */
73 #ifdef VMS
74 name = "[]";
75 #else
76 name = "./";
77 #endif
79 hashval = 0;
80 for (n = name; *n != '\0'; ++n)
81 HASH (hashval, *n);
82 hashval %= FILE_BUCKETS;
84 for (f = files[hashval]; f != 0; f = f->next)
86 if (streq (f->name, name))
88 return f;
91 return 0;
94 struct file *
95 enter_file (name)
96 char *name;
98 register struct file *f, *new;
99 register char *n;
100 register unsigned int hashval;
101 #ifdef VMS
102 char *lname, *ln;
103 #endif
105 if (*name == '\0')
106 abort ();
108 #ifdef VMS
109 lname = (char *)malloc (strlen (name) + 1);
110 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
112 if (isupper(*n))
113 *ln = tolower(*n);
114 else
115 *ln = *n;
117 *ln = 0;
118 name = lname;
119 #endif
121 hashval = 0;
122 for (n = name; *n != '\0'; ++n)
123 HASH (hashval, *n);
124 hashval %= FILE_BUCKETS;
126 for (f = files[hashval]; f != 0; f = f->next)
127 if (streq (f->name, name))
128 break;
130 if (f != 0 && !f->double_colon)
132 #ifdef VMS
133 free(lname);
134 #endif
135 return f;
138 new = (struct file *) xmalloc (sizeof (struct file));
139 bzero ((char *) new, sizeof (struct file));
140 new->name = name;
141 new->update_status = -1;
143 if (f == 0)
145 /* This is a completely new file. */
146 new->next = files[hashval];
147 files[hashval] = new;
149 else
151 /* There is already a double-colon entry for this file. */
152 new->double_colon = f;
153 while (f->prev != 0)
154 f = f->prev;
155 f->prev = new;
158 return new;
161 /* Rename FILE to NAME. This is not as simple as resetting
162 the `name' member, since it must be put in a new hash bucket,
163 and possibly merged with an existing file called NAME. */
165 void
166 rename_file (file, name)
167 register struct file *file;
168 char *name;
170 char *oldname = file->name;
171 register unsigned int oldhash;
172 register char *n;
174 while (file->renamed != 0)
175 file = file->renamed;
177 /* Find the hash values of the old and new names. */
179 oldhash = 0;
180 for (n = oldname; *n != '\0'; ++n)
181 HASH (oldhash, *n);
183 file_hash_enter (file, name, oldhash, file->name);
186 void
187 file_hash_enter (file, name, oldhash, oldname)
188 register struct file *file;
189 char *name;
190 unsigned int oldhash;
191 char *oldname;
193 unsigned int oldbucket = oldhash % FILE_BUCKETS;
194 register unsigned int newhash, newbucket;
195 struct file *oldfile;
196 register char *n;
197 register struct file *f;
199 newhash = 0;
200 for (n = name; *n != '\0'; ++n)
201 HASH (newhash, *n);
202 newbucket = newhash % FILE_BUCKETS;
204 /* Look for an existing file under the new name. */
206 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
207 if (streq (oldfile->name, name))
208 break;
210 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
212 /* Remove FILE from its hash bucket. */
214 struct file *lastf = 0;
216 for (f = files[oldbucket]; f != file; f = f->next)
217 lastf = f;
219 if (lastf == 0)
220 files[oldbucket] = f->next;
221 else
222 lastf->next = f->next;
225 /* Give FILE its new name. */
227 file->name = name;
228 for (f = file->double_colon; f != 0; f = f->prev)
229 f->name = name;
231 if (oldfile == 0)
233 /* There is no existing file with the new name. */
235 if (newbucket != oldbucket)
237 /* Put FILE in its new hash bucket. */
238 file->next = files[newbucket];
239 files[newbucket] = file;
242 else
244 /* There is an existing file with the new name.
245 We must merge FILE into the existing file. */
247 register struct dep *d;
249 if (file->cmds != 0)
251 if (oldfile->cmds == 0)
252 oldfile->cmds = file->cmds;
253 else if (file->cmds != oldfile->cmds)
255 /* We have two sets of commands. We will go with the
256 one given in the rule explicitly mentioning this name,
257 but give a message to let the user know what's going on. */
258 if (oldfile->cmds->filename != 0)
259 makefile_error (file->cmds->filename, file->cmds->lineno,
260 "Commands were specified for \
261 file `%s' at %s:%u,",
262 oldname, oldfile->cmds->filename,
263 oldfile->cmds->lineno);
264 else
265 makefile_error (file->cmds->filename, file->cmds->lineno,
266 "Commands for file `%s' were found by \
267 implicit rule search,",
268 oldname);
269 makefile_error (file->cmds->filename, file->cmds->lineno,
270 "but `%s' is now considered the same file \
271 as `%s'.",
272 oldname, name);
273 makefile_error (file->cmds->filename, file->cmds->lineno,
274 "Commands for `%s' will be ignored \
275 in favor of those for `%s'.",
276 name, oldname);
280 /* Merge the dependencies of the two files. */
282 d = oldfile->deps;
283 if (d == 0)
284 oldfile->deps = file->deps;
285 else
287 while (d->next != 0)
288 d = d->next;
289 d->next = file->deps;
292 merge_variable_set_lists (&oldfile->variables, file->variables);
294 if (oldfile->double_colon && !file->double_colon)
295 fatal ("can't rename single-colon `%s' to double-colon `%s'",
296 oldname, name);
297 if (!oldfile->double_colon && file->double_colon)
298 fatal ("can't rename double-colon `%s' to single-colon `%s'",
299 oldname, name);
301 if (file->last_mtime > oldfile->last_mtime)
302 /* %%% Kludge so -W wins on a file that gets vpathized. */
303 oldfile->last_mtime = file->last_mtime;
305 #define MERGE(field) oldfile->field |= file->field
306 MERGE (precious);
307 MERGE (tried_implicit);
308 MERGE (updating);
309 MERGE (updated);
310 MERGE (is_target);
311 MERGE (cmd_target);
312 MERGE (phony);
313 #undef MERGE
315 file->renamed = oldfile;
319 /* Remove all nonprecious intermediate files.
320 If SIG is nonzero, this was caused by a fatal signal,
321 meaning that a different message will be printed, and
322 the message will go to stderr rather than stdout. */
324 void
325 remove_intermediates (sig)
326 int sig;
328 register int i;
329 register struct file *f;
330 char doneany;
332 if (question_flag || touch_flag)
333 return;
334 if (sig && just_print_flag)
335 return;
337 doneany = 0;
338 for (i = 0; i < FILE_BUCKETS; ++i)
339 for (f = files[i]; f != 0; f = f->next)
340 if (f->intermediate && (f->dontcare || !f->precious)
341 && !f->secondary)
343 int status;
344 if (f->update_status == -1)
345 /* If nothing would have created this file yet,
346 don't print an "rm" command for it. */
347 continue;
348 else if (just_print_flag)
349 status = 0;
350 else
352 status = unlink (f->name);
353 if (status < 0 && errno == ENOENT)
354 continue;
356 if (!f->dontcare)
358 if (sig)
359 error ("*** Deleting intermediate file `%s'", f->name);
360 else if (!silent_flag)
362 if (! doneany)
364 fputs ("rm ", stdout);
365 doneany = 1;
367 else
368 putchar (' ');
369 fputs (f->name, stdout);
370 fflush (stdout);
372 if (status < 0)
373 perror_with_name ("unlink: ", f->name);
377 if (doneany && !sig)
379 putchar ('\n');
380 fflush (stdout);
384 /* For each dependency of each file, make the `struct dep' point
385 at the appropriate `struct file' (which may have to be created).
387 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
388 and various other special targets. */
390 void
391 snap_deps ()
393 register struct file *f, *f2;
394 register struct dep *d;
395 register int i;
397 /* Enter each dependency name as a file. */
398 for (i = 0; i < FILE_BUCKETS; ++i)
399 for (f = files[i]; f != 0; f = f->next)
400 for (f2 = f; f2 != 0; f2 = f2->prev)
401 for (d = f2->deps; d != 0; d = d->next)
402 if (d->name != 0)
404 d->file = lookup_file (d->name);
405 if (d->file == 0)
406 d->file = enter_file (d->name);
407 else
408 free (d->name);
409 d->name = 0;
412 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
413 for (d = f->deps; d != 0; d = d->next)
414 for (f2 = d->file; f2 != 0; f2 = f2->prev)
415 f2->precious = 1;
417 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
418 for (d = f->deps; d != 0; d = d->next)
419 for (f2 = d->file; f2 != 0; f2 = f2->prev)
421 /* Mark this file as phony and nonexistent. */
422 f2->phony = 1;
423 f2->last_mtime = (time_t) -1;
426 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
428 /* .INTERMEDIATE with deps listed
429 marks those deps as intermediate files. */
430 for (d = f->deps; d != 0; d = d->next)
431 for (f2 = d->file; f2 != 0; f2 = f2->prev)
432 f2->intermediate = 1;
433 /* .INTERMEDIATE with no deps does nothing.
434 Marking all files as intermediates is useless
435 since the goal targets would be deleted after they are built. */
438 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
440 /* .SECONDARY with deps listed
441 marks those deps as intermediate files
442 in that they don't get rebuilt if not actually needed;
443 but unlike real intermediate files,
444 these are not deleted after make finishes. */
445 if (f->deps)
447 for (d = f->deps; d != 0; d = d->next)
448 for (f2 = d->file; f2 != 0; f2 = f2->prev)
449 f2->intermediate = f2->secondary = 1;
451 /* .SECONDARY with no deps listed marks *all* files that way. */
452 else
454 int i;
455 for (i = 0; i < FILE_BUCKETS; i++)
456 for (f2 = files[i]; f2; f2= f2->next)
457 f2->intermediate = f2->secondary = 1;
461 f = lookup_file (".EXPORT_ALL_VARIABLES");
462 if (f != 0 && f->is_target)
463 export_all_variables = 1;
465 f = lookup_file (".IGNORE");
466 if (f != 0 && f->is_target)
468 if (f->deps == 0)
469 ignore_errors_flag = 1;
470 else
471 for (d = f->deps; d != 0; d = d->next)
472 for (f2 = d->file; f2 != 0; f2 = f2->prev)
473 f2->command_flags |= COMMANDS_NOERROR;
476 f = lookup_file (".SILENT");
477 if (f != 0 && f->is_target)
479 if (f->deps == 0)
480 silent_flag = 1;
481 else
482 for (d = f->deps; d != 0; d = d->next)
483 for (f2 = d->file; f2 != 0; f2 = f2->prev)
484 f2->command_flags |= COMMANDS_SILENT;
487 f = lookup_file (".POSIX");
488 if (f != 0 && f->is_target)
489 posix_pedantic = 1;
492 /* Set the `command_state' member of FILE and all its `also_make's. */
494 void
495 set_command_state (file, state)
496 struct file *file;
497 int state;
499 struct dep *d;
501 file->command_state = state;
503 for (d = file->also_make; d != 0; d = d->next)
504 d->file->command_state = state;
507 /* Print the data base of files. */
509 static void
510 print_file (f)
511 struct file *f;
513 register struct dep *d;
514 #ifdef VMS
515 extern char *cvt_time PARAMS ((unsigned long));
516 #endif
517 putchar ('\n');
518 if (!f->is_target)
519 puts ("# Not a target:");
520 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
522 for (d = f->deps; d != 0; d = d->next)
523 printf (" %s", dep_name (d));
524 putchar ('\n');
526 if (f->precious)
527 puts ("# Precious file (dependency of .PRECIOUS).");
528 if (f->phony)
529 puts ("# Phony target (dependency of .PHONY).");
530 if (f->cmd_target)
531 puts ("# Command-line target.");
532 if (f->dontcare)
533 puts ("# A default or MAKEFILES makefile.");
534 printf ("# Implicit rule search has%s been done.\n",
535 f->tried_implicit ? "" : " not");
536 if (f->stem != 0)
537 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
538 if (f->intermediate)
539 puts ("# File is an intermediate dependency.");
540 if (f->also_make != 0)
542 fputs ("# Also makes:", stdout);
543 for (d = f->also_make; d != 0; d = d->next)
544 printf (" %s", dep_name (d));
545 putchar ('\n');
547 if (f->last_mtime == (time_t) 0)
548 puts ("# Modification time never checked.");
549 else if (f->last_mtime == (time_t) -1)
550 puts ("# File does not exist.");
551 else
552 #ifdef VMS
553 printf ("# Last modified %.24s (%0lx)\n",
554 cvt_time(f->last_mtime), (unsigned long) f->last_mtime);
555 #else
556 printf ("# Last modified %.24s (%ld)\n",
557 ctime (&f->last_mtime), (long int) f->last_mtime);
558 #endif
559 printf ("# File has%s been updated.\n",
560 f->updated ? "" : " not");
561 switch (f->command_state)
563 case cs_running:
564 puts ("# Commands currently running (THIS IS A BUG).");
565 break;
566 case cs_deps_running:
567 puts ("# Dependencies commands running (THIS IS A BUG).");
568 break;
569 case cs_not_started:
570 case cs_finished:
571 switch (f->update_status)
573 case -1:
574 break;
575 case 0:
576 puts ("# Successfully updated.");
577 break;
578 case 1:
579 assert (question_flag);
580 puts ("# Needs to be updated (-q is set).");
581 break;
582 case 2:
583 puts ("# Failed to be updated.");
584 break;
585 default:
586 puts ("# Invalid value in `update_status' member!");
587 fflush (stdout);
588 fflush (stderr);
589 abort ();
591 break;
592 default:
593 puts ("# Invalid value in `command_state' member!");
594 fflush (stdout);
595 fflush (stderr);
596 abort ();
599 if (f->variables != 0)
600 print_file_variables (f);
602 if (f->cmds != 0)
603 print_commands (f->cmds);
606 void
607 print_file_data_base ()
609 register unsigned int i, nfiles, per_bucket;
610 register struct file *file;
612 puts ("\n# Files");
614 per_bucket = nfiles = 0;
615 for (i = 0; i < FILE_BUCKETS; ++i)
617 register unsigned int this_bucket = 0;
619 for (file = files[i]; file != 0; file = file->next)
621 register struct file *f;
623 ++this_bucket;
625 for (f = file; f != 0; f = f->prev)
626 print_file (f);
629 nfiles += this_bucket;
630 if (this_bucket > per_bucket)
631 per_bucket = this_bucket;
634 if (nfiles == 0)
635 puts ("\n# No files.");
636 else
638 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
639 #ifndef NO_FLOAT
640 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
641 ((double) nfiles) / ((double) FILE_BUCKETS) * 100.0, per_bucket);
642 #endif
646 /* EOF */