* texinfo.tex (\value): handle active _ or - in argument (happens
[make.git] / file.c
bloba60e70fbfaa042fa0636d4b1a5d5bb27e8965ea6
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 "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;
52 #ifdef VMS
53 register char *lname, *ln;
54 #endif
56 if (*name == '\0')
57 abort ();
59 /* This is also done in parse_file_seq, so this is redundant
60 for names read from makefiles. It is here for names passed
61 on the command line. */
62 #ifdef VMS
63 lname = (char *)malloc(strlen(name) + 1);
64 for (n=name, ln=lname; *n != '\0'; ++n, ++ln)
65 *ln = isupper(*n) ? tolower(*n) : *n;
66 *ln = '\0';
67 name = lname;
69 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
70 name += 2;
71 #endif
72 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
74 name += 2;
75 while (*name == '/')
76 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
77 ++name;
80 if (*name == '\0')
81 /* It was all slashes after a dot. */
82 #ifdef VMS
83 name = "[]";
84 #else
85 #ifdef _AMIGA
86 name = "";
87 #else
88 name = "./";
89 #endif /* AMIGA */
90 #endif /* VMS */
92 hashval = 0;
93 for (n = name; *n != '\0'; ++n)
94 HASHI (hashval, *n);
95 hashval %= FILE_BUCKETS;
97 for (f = files[hashval]; f != 0; f = f->next)
99 if (strieq (f->hname, name))
101 #ifdef VMS
102 free (lname);
103 #endif
104 return f;
107 #ifdef VMS
108 free (lname);
109 #endif
110 return 0;
113 struct file *
114 enter_file (name)
115 char *name;
117 register struct file *f, *new;
118 register char *n;
119 register unsigned int hashval;
120 #ifdef VMS
121 char *lname, *ln;
122 #endif
124 if (*name == '\0')
125 abort ();
127 #ifdef VMS
128 lname = (char *)malloc (strlen (name) + 1);
129 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
131 if (isupper(*n))
132 *ln = tolower(*n);
133 else
134 *ln = *n;
136 *ln = 0;
137 name = lname;
138 #endif
140 hashval = 0;
141 for (n = name; *n != '\0'; ++n)
142 HASHI (hashval, *n);
143 hashval %= FILE_BUCKETS;
145 for (f = files[hashval]; f != 0; f = f->next)
146 if (strieq (f->hname, name))
147 break;
149 if (f != 0 && !f->double_colon)
151 #ifdef VMS
152 free(lname);
153 #endif
154 return f;
157 new = (struct file *) xmalloc (sizeof (struct file));
158 bzero ((char *) new, sizeof (struct file));
159 new->name = new->hname = name;
160 new->update_status = -1;
162 if (f == 0)
164 /* This is a completely new file. */
165 new->next = files[hashval];
166 files[hashval] = new;
168 else
170 /* There is already a double-colon entry for this file. */
171 new->double_colon = f;
172 while (f->prev != 0)
173 f = f->prev;
174 f->prev = new;
177 return new;
180 /* Rehash FILE to NAME. This is not as simple as resetting
181 the `hname' member, since it must be put in a new hash bucket,
182 and possibly merged with an existing file called NAME. */
184 void
185 rehash_file (file, name)
186 register struct file *file;
187 char *name;
189 char *oldname = file->hname;
190 register unsigned int oldhash;
191 register char *n;
193 while (file->renamed != 0)
194 file = file->renamed;
196 /* Find the hash values of the old and new names. */
198 oldhash = 0;
199 for (n = oldname; *n != '\0'; ++n)
200 HASHI (oldhash, *n);
202 file_hash_enter (file, name, oldhash, file->name);
205 /* Rename FILE to NAME. This is not as simple as resetting
206 the `name' member, since it must be put in a new hash bucket,
207 and possibly merged with an existing file called NAME. */
209 void
210 rename_file (file, name)
211 register struct file *file;
212 char *name;
214 rehash_file(file, name);
215 while (file)
217 file->name = file->hname;
218 file = file->prev;
222 void
223 file_hash_enter (file, name, oldhash, oldname)
224 register struct file *file;
225 char *name;
226 unsigned int oldhash;
227 char *oldname;
229 unsigned int oldbucket = oldhash % FILE_BUCKETS;
230 register unsigned int newhash, newbucket;
231 struct file *oldfile;
232 register char *n;
233 register struct file *f;
235 newhash = 0;
236 for (n = name; *n != '\0'; ++n)
237 HASHI (newhash, *n);
238 newbucket = newhash % FILE_BUCKETS;
240 /* Look for an existing file under the new name. */
242 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
243 if (strieq (oldfile->hname, name))
244 break;
246 /* If the old file is the same as the new file, something's wrong. */
247 assert (oldfile != file);
249 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
251 /* Remove FILE from its hash bucket. */
253 struct file *lastf = 0;
255 for (f = files[oldbucket]; f != file; f = f->next)
256 lastf = f;
258 if (lastf == 0)
259 files[oldbucket] = f->next;
260 else
261 lastf->next = f->next;
264 /* Give FILE its new name. */
266 file->hname = name;
267 for (f = file->double_colon; f != 0; f = f->prev)
268 f->hname = name;
270 if (oldfile == 0)
272 /* There is no existing file with the new name. */
274 if (newbucket != oldbucket)
276 /* Put FILE in its new hash bucket. */
277 file->next = files[newbucket];
278 files[newbucket] = file;
281 else
283 /* There is an existing file with the new name.
284 We must merge FILE into the existing file. */
286 register struct dep *d;
288 if (file->cmds != 0)
290 if (oldfile->cmds == 0)
291 oldfile->cmds = file->cmds;
292 else if (file->cmds != oldfile->cmds)
294 /* We have two sets of commands. We will go with the
295 one given in the rule explicitly mentioning this name,
296 but give a message to let the user know what's going on. */
297 if (oldfile->cmds->filename != 0)
298 makefile_error (file->cmds->filename, file->cmds->lineno,
299 "Commands were specified for \
300 file `%s' at %s:%u,",
301 oldname, oldfile->cmds->filename,
302 oldfile->cmds->lineno);
303 else
304 makefile_error (file->cmds->filename, file->cmds->lineno,
305 "Commands for file `%s' were found by \
306 implicit rule search,",
307 oldname);
308 makefile_error (file->cmds->filename, file->cmds->lineno,
309 "but `%s' is now considered the same file \
310 as `%s'.",
311 oldname, name);
312 makefile_error (file->cmds->filename, file->cmds->lineno,
313 "Commands for `%s' will be ignored \
314 in favor of those for `%s'.",
315 name, oldname);
319 /* Merge the dependencies of the two files. */
321 d = oldfile->deps;
322 if (d == 0)
323 oldfile->deps = file->deps;
324 else
326 while (d->next != 0)
327 d = d->next;
328 d->next = file->deps;
331 merge_variable_set_lists (&oldfile->variables, file->variables);
333 if (oldfile->double_colon && file->is_target && !file->double_colon)
334 fatal ("can't rename single-colon `%s' to double-colon `%s'",
335 oldname, name);
336 if (!oldfile->double_colon && file->double_colon)
338 if (oldfile->is_target)
339 fatal ("can't rename double-colon `%s' to single-colon `%s'",
340 oldname, name);
341 else
342 oldfile->double_colon = file->double_colon;
345 if (file->last_mtime > oldfile->last_mtime)
346 /* %%% Kludge so -W wins on a file that gets vpathized. */
347 oldfile->last_mtime = file->last_mtime;
349 #define MERGE(field) oldfile->field |= file->field
350 MERGE (precious);
351 MERGE (tried_implicit);
352 MERGE (updating);
353 MERGE (updated);
354 MERGE (is_target);
355 MERGE (cmd_target);
356 MERGE (phony);
357 MERGE (ignore_vpath);
358 #undef MERGE
360 file->renamed = oldfile;
364 /* Remove all nonprecious intermediate files.
365 If SIG is nonzero, this was caused by a fatal signal,
366 meaning that a different message will be printed, and
367 the message will go to stderr rather than stdout. */
369 void
370 remove_intermediates (sig)
371 int sig;
373 register int i;
374 register struct file *f;
375 char doneany;
377 if (question_flag || touch_flag)
378 return;
379 if (sig && just_print_flag)
380 return;
382 doneany = 0;
383 for (i = 0; i < FILE_BUCKETS; ++i)
384 for (f = files[i]; f != 0; f = f->next)
385 if (f->intermediate && (f->dontcare || !f->precious)
386 && !f->secondary)
388 int status;
389 if (f->update_status == -1)
390 /* If nothing would have created this file yet,
391 don't print an "rm" command for it. */
392 continue;
393 else if (just_print_flag)
394 status = 0;
395 else
397 status = unlink (f->name);
398 if (status < 0 && errno == ENOENT)
399 continue;
401 if (!f->dontcare)
403 if (sig)
404 error ("*** Deleting intermediate file `%s'", f->name);
405 else if (!silent_flag)
407 if (! doneany)
409 fputs ("rm ", stdout);
410 doneany = 1;
412 else
413 putchar (' ');
414 fputs (f->name, stdout);
415 fflush (stdout);
417 if (status < 0)
418 perror_with_name ("unlink: ", f->name);
422 if (doneany && !sig)
424 putchar ('\n');
425 fflush (stdout);
429 /* For each dependency of each file, make the `struct dep' point
430 at the appropriate `struct file' (which may have to be created).
432 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
433 and various other special targets. */
435 void
436 snap_deps ()
438 register struct file *f, *f2;
439 register struct dep *d;
440 register int i;
442 /* Enter each dependency name as a file. */
443 for (i = 0; i < FILE_BUCKETS; ++i)
444 for (f = files[i]; f != 0; f = f->next)
445 for (f2 = f; f2 != 0; f2 = f2->prev)
446 for (d = f2->deps; d != 0; d = d->next)
447 if (d->name != 0)
449 d->file = lookup_file (d->name);
450 if (d->file == 0)
451 d->file = enter_file (d->name);
452 else
453 free (d->name);
454 d->name = 0;
457 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
458 for (d = f->deps; d != 0; d = d->next)
459 for (f2 = d->file; f2 != 0; f2 = f2->prev)
460 f2->precious = 1;
462 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
463 for (d = f->deps; d != 0; d = d->next)
464 for (f2 = d->file; f2 != 0; f2 = f2->prev)
466 /* Mark this file as phony and nonexistent. */
467 f2->phony = 1;
468 f2->last_mtime = (time_t) -1;
471 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
473 /* .INTERMEDIATE with deps listed
474 marks those deps as intermediate files. */
475 for (d = f->deps; d != 0; d = d->next)
476 for (f2 = d->file; f2 != 0; f2 = f2->prev)
477 f2->intermediate = 1;
478 /* .INTERMEDIATE with no deps does nothing.
479 Marking all files as intermediates is useless
480 since the goal targets would be deleted after they are built. */
483 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
485 /* .SECONDARY with deps listed
486 marks those deps as intermediate files
487 in that they don't get rebuilt if not actually needed;
488 but unlike real intermediate files,
489 these are not deleted after make finishes. */
490 if (f->deps)
492 for (d = f->deps; d != 0; d = d->next)
493 for (f2 = d->file; f2 != 0; f2 = f2->prev)
494 f2->intermediate = f2->secondary = 1;
496 /* .SECONDARY with no deps listed marks *all* files that way. */
497 else
499 int i;
500 for (i = 0; i < FILE_BUCKETS; i++)
501 for (f2 = files[i]; f2; f2= f2->next)
502 f2->intermediate = f2->secondary = 1;
506 f = lookup_file (".EXPORT_ALL_VARIABLES");
507 if (f != 0 && f->is_target)
508 export_all_variables = 1;
510 f = lookup_file (".IGNORE");
511 if (f != 0 && f->is_target)
513 if (f->deps == 0)
514 ignore_errors_flag = 1;
515 else
516 for (d = f->deps; d != 0; d = d->next)
517 for (f2 = d->file; f2 != 0; f2 = f2->prev)
518 f2->command_flags |= COMMANDS_NOERROR;
521 f = lookup_file (".SILENT");
522 if (f != 0 && f->is_target)
524 if (f->deps == 0)
525 silent_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_SILENT;
532 f = lookup_file (".POSIX");
533 if (f != 0 && f->is_target)
534 posix_pedantic = 1;
537 /* Set the `command_state' member of FILE and all its `also_make's. */
539 void
540 set_command_state (file, state)
541 struct file *file;
542 int state;
544 struct dep *d;
546 file->command_state = state;
548 for (d = file->also_make; d != 0; d = d->next)
549 d->file->command_state = state;
552 /* Print the data base of files. */
554 static void
555 print_file (f)
556 struct file *f;
558 register struct dep *d;
560 putchar ('\n');
561 if (!f->is_target)
562 puts ("# Not a target:");
563 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
565 for (d = f->deps; d != 0; d = d->next)
566 printf (" %s", dep_name (d));
567 putchar ('\n');
569 if (f->precious)
570 puts ("# Precious file (dependency of .PRECIOUS).");
571 if (f->phony)
572 puts ("# Phony target (dependency of .PHONY).");
573 if (f->cmd_target)
574 puts ("# Command-line target.");
575 if (f->dontcare)
576 puts ("# A default or MAKEFILES makefile.");
577 printf ("# Implicit rule search has%s been done.\n",
578 f->tried_implicit ? "" : " not");
579 if (f->stem != 0)
580 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
581 if (f->intermediate)
582 puts ("# File is an intermediate dependency.");
583 if (f->also_make != 0)
585 fputs ("# Also makes:", stdout);
586 for (d = f->also_make; d != 0; d = d->next)
587 printf (" %s", dep_name (d));
588 putchar ('\n');
590 if (f->last_mtime == (time_t) 0)
591 puts ("# Modification time never checked.");
592 else if (f->last_mtime == (time_t) -1)
593 puts ("# File does not exist.");
594 else
595 printf ("# Last modified %.24s (%ld)\n",
596 ctime (&f->last_mtime), (long int) f->last_mtime);
597 printf ("# File has%s been updated.\n",
598 f->updated ? "" : " not");
599 switch (f->command_state)
601 case cs_running:
602 puts ("# Commands currently running (THIS IS A BUG).");
603 break;
604 case cs_deps_running:
605 puts ("# Dependencies commands running (THIS IS A BUG).");
606 break;
607 case cs_not_started:
608 case cs_finished:
609 switch (f->update_status)
611 case -1:
612 break;
613 case 0:
614 puts ("# Successfully updated.");
615 break;
616 case 1:
617 assert (question_flag);
618 puts ("# Needs to be updated (-q is set).");
619 break;
620 case 2:
621 puts ("# Failed to be updated.");
622 break;
623 default:
624 puts ("# Invalid value in `update_status' member!");
625 fflush (stdout);
626 fflush (stderr);
627 abort ();
629 break;
630 default:
631 puts ("# Invalid value in `command_state' member!");
632 fflush (stdout);
633 fflush (stderr);
634 abort ();
637 if (f->variables != 0)
638 print_file_variables (f);
640 if (f->cmds != 0)
641 print_commands (f->cmds);
644 void
645 print_file_data_base ()
647 register unsigned int i, nfiles, per_bucket;
648 register struct file *file;
650 puts ("\n# Files");
652 per_bucket = nfiles = 0;
653 for (i = 0; i < FILE_BUCKETS; ++i)
655 register unsigned int this_bucket = 0;
657 for (file = files[i]; file != 0; file = file->next)
659 register struct file *f;
661 ++this_bucket;
663 for (f = file; f != 0; f = f->prev)
664 print_file (f);
667 nfiles += this_bucket;
668 if (this_bucket > per_bucket)
669 per_bucket = this_bucket;
672 if (nfiles == 0)
673 puts ("\n# No files.");
674 else
676 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
677 #ifndef NO_FLOAT
678 printf ("# average %.3f files per bucket, max %u files in one bucket.\n",
679 ((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);
680 #endif
684 /* EOF */