Updated from libc
[make.git] / file.c
blobaa310775dae6373376a599076d2bc703614091c3
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 #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->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)
302 fatal ("can't rename double-colon `%s' to single-colon `%s'",
303 oldname, name);
305 if (file->last_mtime > oldfile->last_mtime)
306 /* %%% Kludge so -W wins on a file that gets vpathized. */
307 oldfile->last_mtime = file->last_mtime;
309 #define MERGE(field) oldfile->field |= file->field
310 MERGE (precious);
311 MERGE (tried_implicit);
312 MERGE (updating);
313 MERGE (updated);
314 MERGE (is_target);
315 MERGE (cmd_target);
316 MERGE (phony);
317 #undef MERGE
319 file->renamed = oldfile;
323 /* Remove all nonprecious intermediate files.
324 If SIG is nonzero, this was caused by a fatal signal,
325 meaning that a different message will be printed, and
326 the message will go to stderr rather than stdout. */
328 void
329 remove_intermediates (sig)
330 int sig;
332 register int i;
333 register struct file *f;
334 char doneany;
336 if (question_flag || touch_flag)
337 return;
338 if (sig && just_print_flag)
339 return;
341 doneany = 0;
342 for (i = 0; i < FILE_BUCKETS; ++i)
343 for (f = files[i]; f != 0; f = f->next)
344 if (f->intermediate && (f->dontcare || !f->precious)
345 && !f->secondary)
347 int status;
348 if (f->update_status == -1)
349 /* If nothing would have created this file yet,
350 don't print an "rm" command for it. */
351 continue;
352 else if (just_print_flag)
353 status = 0;
354 else
356 status = unlink (f->name);
357 if (status < 0 && errno == ENOENT)
358 continue;
360 if (!f->dontcare)
362 if (sig)
363 error ("*** Deleting intermediate file `%s'", f->name);
364 else if (!silent_flag)
366 if (! doneany)
368 fputs ("rm ", stdout);
369 doneany = 1;
371 else
372 putchar (' ');
373 fputs (f->name, stdout);
374 fflush (stdout);
376 if (status < 0)
377 perror_with_name ("unlink: ", f->name);
381 if (doneany && !sig)
383 putchar ('\n');
384 fflush (stdout);
388 /* For each dependency of each file, make the `struct dep' point
389 at the appropriate `struct file' (which may have to be created).
391 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
392 and various other special targets. */
394 void
395 snap_deps ()
397 register struct file *f, *f2;
398 register struct dep *d;
399 register int i;
401 /* Enter each dependency name as a file. */
402 for (i = 0; i < FILE_BUCKETS; ++i)
403 for (f = files[i]; f != 0; f = f->next)
404 for (f2 = f; f2 != 0; f2 = f2->prev)
405 for (d = f2->deps; d != 0; d = d->next)
406 if (d->name != 0)
408 d->file = lookup_file (d->name);
409 if (d->file == 0)
410 d->file = enter_file (d->name);
411 else
412 free (d->name);
413 d->name = 0;
416 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
417 for (d = f->deps; d != 0; d = d->next)
418 for (f2 = d->file; f2 != 0; f2 = f2->prev)
419 f2->precious = 1;
421 for (f = lookup_file (".PHONY"); 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)
425 /* Mark this file as phony and nonexistent. */
426 f2->phony = 1;
427 f2->last_mtime = (time_t) -1;
430 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
432 /* .INTERMEDIATE with deps listed
433 marks those deps as intermediate files. */
434 for (d = f->deps; d != 0; d = d->next)
435 for (f2 = d->file; f2 != 0; f2 = f2->prev)
436 f2->intermediate = 1;
437 /* .INTERMEDIATE with no deps does nothing.
438 Marking all files as intermediates is useless
439 since the goal targets would be deleted after they are built. */
442 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
444 /* .SECONDARY with deps listed
445 marks those deps as intermediate files
446 in that they don't get rebuilt if not actually needed;
447 but unlike real intermediate files,
448 these are not deleted after make finishes. */
449 if (f->deps)
451 for (d = f->deps; d != 0; d = d->next)
452 for (f2 = d->file; f2 != 0; f2 = f2->prev)
453 f2->intermediate = f2->secondary = 1;
455 /* .SECONDARY with no deps listed marks *all* files that way. */
456 else
458 int i;
459 for (i = 0; i < FILE_BUCKETS; i++)
460 for (f2 = files[i]; f2; f2= f2->next)
461 f2->intermediate = f2->secondary = 1;
465 f = lookup_file (".EXPORT_ALL_VARIABLES");
466 if (f != 0 && f->is_target)
467 export_all_variables = 1;
469 f = lookup_file (".IGNORE");
470 if (f != 0 && f->is_target)
472 if (f->deps == 0)
473 ignore_errors_flag = 1;
474 else
475 for (d = f->deps; d != 0; d = d->next)
476 for (f2 = d->file; f2 != 0; f2 = f2->prev)
477 f2->command_flags |= COMMANDS_NOERROR;
480 f = lookup_file (".SILENT");
481 if (f != 0 && f->is_target)
483 if (f->deps == 0)
484 silent_flag = 1;
485 else
486 for (d = f->deps; d != 0; d = d->next)
487 for (f2 = d->file; f2 != 0; f2 = f2->prev)
488 f2->command_flags |= COMMANDS_SILENT;
491 f = lookup_file (".POSIX");
492 if (f != 0 && f->is_target)
493 posix_pedantic = 1;
496 /* Set the `command_state' member of FILE and all its `also_make's. */
498 void
499 set_command_state (file, state)
500 struct file *file;
501 int state;
503 struct dep *d;
505 file->command_state = state;
507 for (d = file->also_make; d != 0; d = d->next)
508 d->file->command_state = state;
511 /* Print the data base of files. */
513 static void
514 print_file (f)
515 struct file *f;
517 register struct dep *d;
518 #ifdef VMS
519 extern char *cvt_time PARAMS ((unsigned long));
520 #endif
521 putchar ('\n');
522 if (!f->is_target)
523 puts ("# Not a target:");
524 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
526 for (d = f->deps; d != 0; d = d->next)
527 printf (" %s", dep_name (d));
528 putchar ('\n');
530 if (f->precious)
531 puts ("# Precious file (dependency of .PRECIOUS).");
532 if (f->phony)
533 puts ("# Phony target (dependency of .PHONY).");
534 if (f->cmd_target)
535 puts ("# Command-line target.");
536 if (f->dontcare)
537 puts ("# A default or MAKEFILES makefile.");
538 printf ("# Implicit rule search has%s been done.\n",
539 f->tried_implicit ? "" : " not");
540 if (f->stem != 0)
541 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
542 if (f->intermediate)
543 puts ("# File is an intermediate dependency.");
544 if (f->also_make != 0)
546 fputs ("# Also makes:", stdout);
547 for (d = f->also_make; d != 0; d = d->next)
548 printf (" %s", dep_name (d));
549 putchar ('\n');
551 if (f->last_mtime == (time_t) 0)
552 puts ("# Modification time never checked.");
553 else if (f->last_mtime == (time_t) -1)
554 puts ("# File does not exist.");
555 else
556 #ifdef VMS
557 printf ("# Last modified %.24s (%0lx)\n",
558 cvt_time(f->last_mtime), (unsigned long) f->last_mtime);
559 #else
560 printf ("# Last modified %.24s (%ld)\n",
561 ctime (&f->last_mtime), (long int) f->last_mtime);
562 #endif
563 printf ("# File has%s been updated.\n",
564 f->updated ? "" : " not");
565 switch (f->command_state)
567 case cs_running:
568 puts ("# Commands currently running (THIS IS A BUG).");
569 break;
570 case cs_deps_running:
571 puts ("# Dependencies commands running (THIS IS A BUG).");
572 break;
573 case cs_not_started:
574 case cs_finished:
575 switch (f->update_status)
577 case -1:
578 break;
579 case 0:
580 puts ("# Successfully updated.");
581 break;
582 case 1:
583 assert (question_flag);
584 puts ("# Needs to be updated (-q is set).");
585 break;
586 case 2:
587 puts ("# Failed to be updated.");
588 break;
589 default:
590 puts ("# Invalid value in `update_status' member!");
591 fflush (stdout);
592 fflush (stderr);
593 abort ();
595 break;
596 default:
597 puts ("# Invalid value in `command_state' member!");
598 fflush (stdout);
599 fflush (stderr);
600 abort ();
603 if (f->variables != 0)
604 print_file_variables (f);
606 if (f->cmds != 0)
607 print_commands (f->cmds);
610 void
611 print_file_data_base ()
613 register unsigned int i, nfiles, per_bucket;
614 register struct file *file;
616 puts ("\n# Files");
618 per_bucket = nfiles = 0;
619 for (i = 0; i < FILE_BUCKETS; ++i)
621 register unsigned int this_bucket = 0;
623 for (file = files[i]; file != 0; file = file->next)
625 register struct file *f;
627 ++this_bucket;
629 for (f = file; f != 0; f = f->prev)
630 print_file (f);
633 nfiles += this_bucket;
634 if (this_bucket > per_bucket)
635 per_bucket = this_bucket;
638 if (nfiles == 0)
639 puts ("\n# No files.");
640 else
642 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
643 #ifndef NO_FLOAT
644 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
645 ((double) nfiles) / ((double) FILE_BUCKETS) * 100.0, per_bucket);
646 #endif
650 /* EOF */