Regenerated for 3.74.5
[make.git] / file.c
blobacc0fd6aa16d21df69c94296c9832e857400fdd2
1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96 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 #ifdef _AMIGA
77 name = "";
78 #else
79 name = "./";
80 #endif /* AMIGA */
81 #endif /* VMS */
83 hashval = 0;
84 for (n = name; *n != '\0'; ++n)
85 HASHI (hashval, *n);
86 hashval %= FILE_BUCKETS;
88 for (f = files[hashval]; f != 0; f = f->next)
90 if (strieq (f->name, name))
92 return f;
95 return 0;
98 struct file *
99 enter_file (name)
100 char *name;
102 register struct file *f, *new;
103 register char *n;
104 register unsigned int hashval;
105 #ifdef VMS
106 char *lname, *ln;
107 #endif
109 if (*name == '\0')
110 abort ();
112 #ifdef VMS
113 lname = (char *)malloc (strlen (name) + 1);
114 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
116 if (isupper(*n))
117 *ln = tolower(*n);
118 else
119 *ln = *n;
121 *ln = 0;
122 name = lname;
123 #endif
125 hashval = 0;
126 for (n = name; *n != '\0'; ++n)
127 HASHI (hashval, *n);
128 hashval %= FILE_BUCKETS;
130 for (f = files[hashval]; f != 0; f = f->next)
131 if (strieq (f->name, name))
132 break;
134 if (f != 0 && !f->double_colon)
136 #ifdef VMS
137 free(lname);
138 #endif
139 return f;
142 new = (struct file *) xmalloc (sizeof (struct file));
143 bzero ((char *) new, sizeof (struct file));
144 new->name = name;
145 new->update_status = -1;
147 if (f == 0)
149 /* This is a completely new file. */
150 new->next = files[hashval];
151 files[hashval] = new;
153 else
155 /* There is already a double-colon entry for this file. */
156 new->double_colon = f;
157 while (f->prev != 0)
158 f = f->prev;
159 f->prev = new;
162 return new;
165 /* Rename FILE to NAME. This is not as simple as resetting
166 the `name' member, since it must be put in a new hash bucket,
167 and possibly merged with an existing file called NAME. */
169 void
170 rename_file (file, name)
171 register struct file *file;
172 char *name;
174 char *oldname = file->name;
175 register unsigned int oldhash;
176 register char *n;
178 while (file->renamed != 0)
179 file = file->renamed;
181 /* Find the hash values of the old and new names. */
183 oldhash = 0;
184 for (n = oldname; *n != '\0'; ++n)
185 HASHI (oldhash, *n);
187 file_hash_enter (file, name, oldhash, file->name);
190 void
191 file_hash_enter (file, name, oldhash, oldname)
192 register struct file *file;
193 char *name;
194 unsigned int oldhash;
195 char *oldname;
197 unsigned int oldbucket = oldhash % FILE_BUCKETS;
198 register unsigned int newhash, newbucket;
199 struct file *oldfile;
200 register char *n;
201 register struct file *f;
203 newhash = 0;
204 for (n = name; *n != '\0'; ++n)
205 HASHI (newhash, *n);
206 newbucket = newhash % FILE_BUCKETS;
208 /* Look for an existing file under the new name. */
210 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
211 if (strieq (oldfile->name, name))
212 break;
214 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
216 /* Remove FILE from its hash bucket. */
218 struct file *lastf = 0;
220 for (f = files[oldbucket]; f != file; f = f->next)
221 lastf = f;
223 if (lastf == 0)
224 files[oldbucket] = f->next;
225 else
226 lastf->next = f->next;
229 /* Give FILE its new name. */
231 file->name = name;
232 for (f = file->double_colon; f != 0; f = f->prev)
233 f->name = name;
235 if (oldfile == 0)
237 /* There is no existing file with the new name. */
239 if (newbucket != oldbucket)
241 /* Put FILE in its new hash bucket. */
242 file->next = files[newbucket];
243 files[newbucket] = file;
246 else
248 /* There is an existing file with the new name.
249 We must merge FILE into the existing file. */
251 register struct dep *d;
253 if (file->cmds != 0)
255 if (oldfile->cmds == 0)
256 oldfile->cmds = file->cmds;
257 else if (file->cmds != oldfile->cmds)
259 /* We have two sets of commands. We will go with the
260 one given in the rule explicitly mentioning this name,
261 but give a message to let the user know what's going on. */
262 if (oldfile->cmds->filename != 0)
263 makefile_error (file->cmds->filename, file->cmds->lineno,
264 "Commands were specified for \
265 file `%s' at %s:%u,",
266 oldname, oldfile->cmds->filename,
267 oldfile->cmds->lineno);
268 else
269 makefile_error (file->cmds->filename, file->cmds->lineno,
270 "Commands for file `%s' were found by \
271 implicit rule search,",
272 oldname);
273 makefile_error (file->cmds->filename, file->cmds->lineno,
274 "but `%s' is now considered the same file \
275 as `%s'.",
276 oldname, name);
277 makefile_error (file->cmds->filename, file->cmds->lineno,
278 "Commands for `%s' will be ignored \
279 in favor of those for `%s'.",
280 name, oldname);
284 /* Merge the dependencies of the two files. */
286 d = oldfile->deps;
287 if (d == 0)
288 oldfile->deps = file->deps;
289 else
291 while (d->next != 0)
292 d = d->next;
293 d->next = file->deps;
296 merge_variable_set_lists (&oldfile->variables, file->variables);
298 if (oldfile->double_colon && file->is_target && !file->double_colon)
299 fatal ("can't rename single-colon `%s' to double-colon `%s'",
300 oldname, name);
301 if (!oldfile->double_colon && file->double_colon)
303 if (oldfile->is_target)
304 fatal ("can't rename double-colon `%s' to single-colon `%s'",
305 oldname, name);
306 else
307 oldfile->double_colon = file->double_colon;
310 if (file->last_mtime > oldfile->last_mtime)
311 /* %%% Kludge so -W wins on a file that gets vpathized. */
312 oldfile->last_mtime = file->last_mtime;
314 #define MERGE(field) oldfile->field |= file->field
315 MERGE (precious);
316 MERGE (tried_implicit);
317 MERGE (updating);
318 MERGE (updated);
319 MERGE (is_target);
320 MERGE (cmd_target);
321 MERGE (phony);
322 #undef MERGE
324 file->renamed = oldfile;
328 /* Remove all nonprecious intermediate files.
329 If SIG is nonzero, this was caused by a fatal signal,
330 meaning that a different message will be printed, and
331 the message will go to stderr rather than stdout. */
333 void
334 remove_intermediates (sig)
335 int sig;
337 register int i;
338 register struct file *f;
339 char doneany;
341 if (question_flag || touch_flag)
342 return;
343 if (sig && just_print_flag)
344 return;
346 doneany = 0;
347 for (i = 0; i < FILE_BUCKETS; ++i)
348 for (f = files[i]; f != 0; f = f->next)
349 if (f->intermediate && (f->dontcare || !f->precious)
350 && !f->secondary)
352 int status;
353 if (f->update_status == -1)
354 /* If nothing would have created this file yet,
355 don't print an "rm" command for it. */
356 continue;
357 else if (just_print_flag)
358 status = 0;
359 else
361 status = unlink (f->name);
362 if (status < 0 && errno == ENOENT)
363 continue;
365 if (!f->dontcare)
367 if (sig)
368 error ("*** Deleting intermediate file `%s'", f->name);
369 else if (!silent_flag)
371 if (! doneany)
373 fputs ("rm ", stdout);
374 doneany = 1;
376 else
377 putchar (' ');
378 fputs (f->name, stdout);
379 fflush (stdout);
381 if (status < 0)
382 perror_with_name ("unlink: ", f->name);
386 if (doneany && !sig)
388 putchar ('\n');
389 fflush (stdout);
393 /* For each dependency of each file, make the `struct dep' point
394 at the appropriate `struct file' (which may have to be created).
396 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
397 and various other special targets. */
399 void
400 snap_deps ()
402 register struct file *f, *f2;
403 register struct dep *d;
404 register int i;
406 /* Enter each dependency name as a file. */
407 for (i = 0; i < FILE_BUCKETS; ++i)
408 for (f = files[i]; f != 0; f = f->next)
409 for (f2 = f; f2 != 0; f2 = f2->prev)
410 for (d = f2->deps; d != 0; d = d->next)
411 if (d->name != 0)
413 d->file = lookup_file (d->name);
414 if (d->file == 0)
415 d->file = enter_file (d->name);
416 else
417 free (d->name);
418 d->name = 0;
421 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
422 for (d = f->deps; d != 0; d = d->next)
423 for (f2 = d->file; f2 != 0; f2 = f2->prev)
424 f2->precious = 1;
426 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
427 for (d = f->deps; d != 0; d = d->next)
428 for (f2 = d->file; f2 != 0; f2 = f2->prev)
430 /* Mark this file as phony and nonexistent. */
431 f2->phony = 1;
432 f2->last_mtime = (time_t) -1;
435 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
437 /* .INTERMEDIATE with deps listed
438 marks those deps as intermediate files. */
439 for (d = f->deps; d != 0; d = d->next)
440 for (f2 = d->file; f2 != 0; f2 = f2->prev)
441 f2->intermediate = 1;
442 /* .INTERMEDIATE with no deps does nothing.
443 Marking all files as intermediates is useless
444 since the goal targets would be deleted after they are built. */
447 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
449 /* .SECONDARY with deps listed
450 marks those deps as intermediate files
451 in that they don't get rebuilt if not actually needed;
452 but unlike real intermediate files,
453 these are not deleted after make finishes. */
454 if (f->deps)
456 for (d = f->deps; d != 0; d = d->next)
457 for (f2 = d->file; f2 != 0; f2 = f2->prev)
458 f2->intermediate = f2->secondary = 1;
460 /* .SECONDARY with no deps listed marks *all* files that way. */
461 else
463 int i;
464 for (i = 0; i < FILE_BUCKETS; i++)
465 for (f2 = files[i]; f2; f2= f2->next)
466 f2->intermediate = f2->secondary = 1;
470 f = lookup_file (".EXPORT_ALL_VARIABLES");
471 if (f != 0 && f->is_target)
472 export_all_variables = 1;
474 f = lookup_file (".IGNORE");
475 if (f != 0 && f->is_target)
477 if (f->deps == 0)
478 ignore_errors_flag = 1;
479 else
480 for (d = f->deps; d != 0; d = d->next)
481 for (f2 = d->file; f2 != 0; f2 = f2->prev)
482 f2->command_flags |= COMMANDS_NOERROR;
485 f = lookup_file (".SILENT");
486 if (f != 0 && f->is_target)
488 if (f->deps == 0)
489 silent_flag = 1;
490 else
491 for (d = f->deps; d != 0; d = d->next)
492 for (f2 = d->file; f2 != 0; f2 = f2->prev)
493 f2->command_flags |= COMMANDS_SILENT;
496 f = lookup_file (".POSIX");
497 if (f != 0 && f->is_target)
498 posix_pedantic = 1;
501 /* Set the `command_state' member of FILE and all its `also_make's. */
503 void
504 set_command_state (file, state)
505 struct file *file;
506 int state;
508 struct dep *d;
510 file->command_state = state;
512 for (d = file->also_make; d != 0; d = d->next)
513 d->file->command_state = state;
516 /* Print the data base of files. */
518 static void
519 print_file (f)
520 struct file *f;
522 register struct dep *d;
523 #ifdef VMS
524 extern char *cvt_time PARAMS ((unsigned long));
525 #endif
526 putchar ('\n');
527 if (!f->is_target)
528 puts ("# Not a target:");
529 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
531 for (d = f->deps; d != 0; d = d->next)
532 printf (" %s", dep_name (d));
533 putchar ('\n');
535 if (f->precious)
536 puts ("# Precious file (dependency of .PRECIOUS).");
537 if (f->phony)
538 puts ("# Phony target (dependency of .PHONY).");
539 if (f->cmd_target)
540 puts ("# Command-line target.");
541 if (f->dontcare)
542 puts ("# A default or MAKEFILES makefile.");
543 printf ("# Implicit rule search has%s been done.\n",
544 f->tried_implicit ? "" : " not");
545 if (f->stem != 0)
546 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
547 if (f->intermediate)
548 puts ("# File is an intermediate dependency.");
549 if (f->also_make != 0)
551 fputs ("# Also makes:", stdout);
552 for (d = f->also_make; d != 0; d = d->next)
553 printf (" %s", dep_name (d));
554 putchar ('\n');
556 if (f->last_mtime == (time_t) 0)
557 puts ("# Modification time never checked.");
558 else if (f->last_mtime == (time_t) -1)
559 puts ("# File does not exist.");
560 else
561 #ifdef VMS
562 printf ("# Last modified %.24s (%0lx)\n",
563 cvt_time(f->last_mtime), (unsigned long) f->last_mtime);
564 #else
565 printf ("# Last modified %.24s (%ld)\n",
566 ctime (&f->last_mtime), (long int) f->last_mtime);
567 #endif
568 printf ("# File has%s been updated.\n",
569 f->updated ? "" : " not");
570 switch (f->command_state)
572 case cs_running:
573 puts ("# Commands currently running (THIS IS A BUG).");
574 break;
575 case cs_deps_running:
576 puts ("# Dependencies commands running (THIS IS A BUG).");
577 break;
578 case cs_not_started:
579 case cs_finished:
580 switch (f->update_status)
582 case -1:
583 break;
584 case 0:
585 puts ("# Successfully updated.");
586 break;
587 case 1:
588 assert (question_flag);
589 puts ("# Needs to be updated (-q is set).");
590 break;
591 case 2:
592 puts ("# Failed to be updated.");
593 break;
594 default:
595 puts ("# Invalid value in `update_status' member!");
596 fflush (stdout);
597 fflush (stderr);
598 abort ();
600 break;
601 default:
602 puts ("# Invalid value in `command_state' member!");
603 fflush (stdout);
604 fflush (stderr);
605 abort ();
608 if (f->variables != 0)
609 print_file_variables (f);
611 if (f->cmds != 0)
612 print_commands (f->cmds);
615 void
616 print_file_data_base ()
618 register unsigned int i, nfiles, per_bucket;
619 register struct file *file;
621 puts ("\n# Files");
623 per_bucket = nfiles = 0;
624 for (i = 0; i < FILE_BUCKETS; ++i)
626 register unsigned int this_bucket = 0;
628 for (file = files[i]; file != 0; file = file->next)
630 register struct file *f;
632 ++this_bucket;
634 for (f = file; f != 0; f = f->prev)
635 print_file (f);
638 nfiles += this_bucket;
639 if (this_bucket > per_bucket)
640 per_bucket = this_bucket;
643 if (nfiles == 0)
644 puts ("\n# No files.");
645 else
647 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
648 #ifndef NO_FLOAT
649 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
650 ((double) nfiles) / ((double) FILE_BUCKETS) * 100.0, per_bucket);
651 #endif
655 /* EOF */