(touch_file): Use message instead of printf.
[make.git] / file.c
blob82e7442cd66d77ecdd87a0cf2bb21f996554f4e9
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 "commands.h"
21 #include "dep.h"
22 #include "file.h"
23 #include "variable.h"
24 #include <assert.h>
27 /* Hash table of files the makefile knows how to make. */
29 #ifndef FILE_BUCKETS
30 #define FILE_BUCKETS 1007
31 #endif
32 static struct file *files[FILE_BUCKETS];
34 /* Number of files with the `intermediate' flag set. */
36 unsigned int num_intermediates = 0;
39 /* Access the hash table of all file records.
40 lookup_file given a name, return the struct file * for that name,
41 or nil if there is none.
42 enter_file similar, but create one if there is none. */
44 struct file *
45 lookup_file (name)
46 char *name;
48 register struct file *f;
49 register char *n;
50 register unsigned int hashval;
52 if (*name == '\0')
53 abort ();
55 /* This is also done in parse_file_seq, so this is redundant
56 for names read from makefiles. It is here for names passed
57 on the command line. */
58 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
60 name += 2;
61 while (*name == '/')
62 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
63 ++name;
66 if (*name == '\0')
67 /* It was all slashes after a dot. */
68 name = "./";
70 hashval = 0;
71 for (n = name; *n != '\0'; ++n)
72 HASH (hashval, *n);
73 hashval %= FILE_BUCKETS;
75 for (f = files[hashval]; f != 0; f = f->next)
76 if (streq (f->name, name))
77 return f;
78 return 0;
81 struct file *
82 enter_file (name)
83 char *name;
85 register struct file *f, *new;
86 register char *n;
87 register unsigned int hashval;
89 if (*name == '\0')
90 abort ();
92 hashval = 0;
93 for (n = name; *n != '\0'; ++n)
94 HASH (hashval, *n);
95 hashval %= FILE_BUCKETS;
97 for (f = files[hashval]; f != 0; f = f->next)
98 if (streq (f->name, name))
99 break;
101 if (f != 0 && !f->double_colon)
102 return f;
104 new = (struct file *) xmalloc (sizeof (struct file));
105 bzero ((char *) new, sizeof (struct file));
106 new->name = name;
107 new->update_status = -1;
109 if (f == 0)
111 /* This is a completely new file. */
112 new->next = files[hashval];
113 files[hashval] = new;
115 else
117 /* There is already a double-colon entry for this file. */
118 new->double_colon = f;
119 while (f->prev != 0)
120 f = f->prev;
121 f->prev = new;
124 return new;
127 /* Rename FILE to NAME. This is not as simple as resetting
128 the `name' member, since it must be put in a new hash bucket,
129 and possibly merged with an existing file called NAME. */
131 void
132 rename_file (file, name)
133 register struct file *file;
134 char *name;
136 char *oldname = file->name;
137 register unsigned int oldhash;
138 register char *n;
140 while (file->renamed != 0)
141 file = file->renamed;
143 /* Find the hash values of the old and new names. */
145 oldhash = 0;
146 for (n = oldname; *n != '\0'; ++n)
147 HASH (oldhash, *n);
149 file_hash_enter (file, name, oldhash, file->name);
152 void
153 file_hash_enter (file, name, oldhash, oldname)
154 register struct file *file;
155 char *name;
156 unsigned int oldhash;
157 char *oldname;
159 unsigned int oldbucket = oldhash % FILE_BUCKETS;
160 register unsigned int newhash, newbucket;
161 struct file *oldfile;
162 register char *n;
163 register struct file *f;
165 newhash = 0;
166 for (n = name; *n != '\0'; ++n)
167 HASH (newhash, *n);
168 newbucket = newhash % FILE_BUCKETS;
170 /* Look for an existing file under the new name. */
172 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
173 if (streq (oldfile->name, name))
174 break;
176 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
178 /* Remove FILE from its hash bucket. */
180 struct file *lastf = 0;
182 for (f = files[oldbucket]; f != file; f = f->next)
183 lastf = f;
185 if (lastf == 0)
186 files[oldbucket] = f->next;
187 else
188 lastf->next = f->next;
191 /* Give FILE its new name. */
193 file->name = name;
194 for (f = file->double_colon; f != 0; f = f->prev)
195 f->name = name;
197 if (oldfile == 0)
199 /* There is no existing file with the new name. */
201 if (newbucket != oldbucket)
203 /* Put FILE in its new hash bucket. */
204 file->next = files[newbucket];
205 files[newbucket] = file;
208 else
210 /* There is an existing file with the new name.
211 We must merge FILE into the existing file. */
213 register struct dep *d;
215 if (file->cmds != 0)
217 if (oldfile->cmds == 0)
218 oldfile->cmds = file->cmds;
219 else if (file->cmds != oldfile->cmds)
221 /* We have two sets of commands. We will go with the
222 one given in the rule explicitly mentioning this name,
223 but give a message to let the user know what's going on. */
224 if (oldfile->cmds->filename != 0)
225 makefile_error (file->cmds->filename, file->cmds->lineno,
226 "Commands were specified for \
227 file `%s' at %s:%u,",
228 oldname, oldfile->cmds->filename,
229 oldfile->cmds->lineno);
230 else
231 makefile_error (file->cmds->filename, file->cmds->lineno,
232 "Commands for file `%s' were found by \
233 implicit rule search,",
234 oldname);
235 makefile_error (file->cmds->filename, file->cmds->lineno,
236 "but `%s' is now considered the same file \
237 as `%s'.",
238 oldname, name);
239 makefile_error (file->cmds->filename, file->cmds->lineno,
240 "Commands for `%s' will be ignored \
241 in favor of those for `%s'.",
242 name, oldname);
246 /* Merge the dependencies of the two files. */
248 d = oldfile->deps;
249 if (d == 0)
250 oldfile->deps = file->deps;
251 else
253 while (d->next != 0)
254 d = d->next;
255 d->next = file->deps;
258 merge_variable_set_lists (&oldfile->variables, file->variables);
260 if (oldfile->double_colon && !file->double_colon)
261 fatal ("can't rename single-colon `%s' to double-colon `%s'",
262 oldname, name);
263 if (!oldfile->double_colon && file->double_colon)
264 fatal ("can't rename double-colon `%s' to single-colon `%s'",
265 oldname, name);
267 if (file->last_mtime > oldfile->last_mtime)
268 /* %%% Kludge so -W wins on a file that gets vpathized. */
269 oldfile->last_mtime = file->last_mtime;
271 #define MERGE(field) oldfile->field |= file->field
272 MERGE (precious);
273 MERGE (tried_implicit);
274 MERGE (updating);
275 MERGE (updated);
276 MERGE (is_target);
277 MERGE (cmd_target);
278 MERGE (phony);
279 #undef MERGE
281 file->renamed = oldfile;
285 /* Remove all nonprecious intermediate files.
286 If SIG is nonzero, this was caused by a fatal signal,
287 meaning that a different message will be printed, and
288 the message will go to stderr rather than stdout. */
290 void
291 remove_intermediates (sig)
292 int sig;
294 register int i;
295 register struct file *f;
296 char doneany;
298 if (question_flag || touch_flag)
299 return;
300 if (sig && just_print_flag)
301 return;
303 doneany = 0;
304 for (i = 0; i < FILE_BUCKETS; ++i)
305 for (f = files[i]; f != 0; f = f->next)
306 if (f->intermediate && (f->dontcare || !f->precious))
308 int status;
309 if (f->update_status == -1)
310 /* If nothing would have created this file yet,
311 don't print an "rm" command for it. */
312 continue;
313 else if (just_print_flag)
314 status = 0;
315 else
317 status = unlink (f->name);
318 if (status < 0 && errno == ENOENT)
319 continue;
321 if (!f->dontcare)
323 if (sig)
324 error ("*** Deleting intermediate file `%s'", f->name);
325 else if (!silent_flag)
327 if (! doneany)
329 fputs ("rm ", stdout);
330 doneany = 1;
332 else
333 putchar (' ');
334 fputs (f->name, stdout);
335 fflush (stdout);
337 if (status < 0)
338 perror_with_name ("unlink: ", f->name);
342 if (doneany && !sig)
344 putchar ('\n');
345 fflush (stdout);
349 /* For each dependency of each file, make the `struct dep' point
350 at the appropriate `struct file' (which may have to be created).
352 Also mark the files depended on by .PRECIOUS and .PHONY. */
354 void
355 snap_deps ()
357 register struct file *f, *f2;
358 register struct dep *d;
359 register int i;
361 /* Enter each dependency name as a file. */
362 for (i = 0; i < FILE_BUCKETS; ++i)
363 for (f = files[i]; f != 0; f = f->next)
364 for (f2 = f; f2 != 0; f2 = f2->prev)
365 for (d = f2->deps; d != 0; d = d->next)
366 if (d->name != 0)
368 d->file = lookup_file (d->name);
369 if (d->file == 0)
370 d->file = enter_file (d->name);
371 else
372 free (d->name);
373 d->name = 0;
376 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
377 for (d = f->deps; d != 0; d = d->next)
378 for (f2 = d->file; f2 != 0; f2 = f2->prev)
379 f2->precious = 1;
381 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
382 for (d = f->deps; d != 0; d = d->next)
383 for (f2 = d->file; f2 != 0; f2 = f2->prev)
385 /* Mark this file as phony and nonexistent. */
386 f2->phony = 1;
387 f2->last_mtime = (time_t) -1;
390 f = lookup_file (".EXPORT_ALL_VARIABLES");
391 if (f != 0 && f->is_target)
392 export_all_variables = 1;
394 f = lookup_file (".IGNORE");
395 if (f != 0 && f->is_target)
397 if (f->deps == 0)
398 ignore_errors_flag = 1;
399 else
400 for (d = f->deps; d != 0; d = d->next)
401 for (f2 = d->file; f2 != 0; f2 = f2->prev)
402 f2->command_flags |= COMMANDS_NOERROR;
405 f = lookup_file (".SILENT");
406 if (f != 0 && f->is_target)
408 if (f->deps == 0)
409 silent_flag = 1;
410 else
411 for (d = f->deps; d != 0; d = d->next)
412 for (f2 = d->file; f2 != 0; f2 = f2->prev)
413 f2->command_flags |= COMMANDS_SILENT;
416 f = lookup_file (".POSIX");
417 if (f != 0 && f->is_target)
418 posix_pedantic = 1;
421 /* Set the `command_state' member of FILE and all its `also_make's. */
423 void
424 set_command_state (file, state)
425 struct file *file;
426 int state;
428 struct dep *d;
430 file->command_state = state;
432 for (d = file->also_make; d != 0; d = d->next)
433 d->file->command_state = state;
436 /* Print the data base of files. */
438 static void
439 print_file (f)
440 struct file *f;
442 register struct dep *d;
444 putchar ('\n');
445 if (!f->is_target)
446 puts ("# Not a target:");
447 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
449 for (d = f->deps; d != 0; d = d->next)
450 printf (" %s", dep_name (d));
451 putchar ('\n');
453 if (f->precious)
454 puts ("# Precious file (dependency of .PRECIOUS).");
455 if (f->phony)
456 puts ("# Phony target (dependency of .PHONY).");
457 if (f->cmd_target)
458 puts ("# Command-line target.");
459 if (f->dontcare)
460 puts ("# A default or MAKEFILES makefile.");
461 printf ("# Implicit rule search has%s been done.\n",
462 f->tried_implicit ? "" : " not");
463 if (f->stem != 0)
464 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
465 if (f->intermediate)
466 puts ("# File is an intermediate dependency.");
467 if (f->also_make != 0)
469 fputs ("# Also makes:", stdout);
470 for (d = f->also_make; d != 0; d = d->next)
471 printf (" %s", dep_name (d));
472 putchar ('\n');
474 if (f->last_mtime == (time_t) 0)
475 puts ("# Modification time never checked.");
476 else if (f->last_mtime == (time_t) -1)
477 puts ("# File does not exist.");
478 else
479 printf ("# Last modified %.24s (%ld)\n",
480 ctime (&f->last_mtime), (long int) f->last_mtime);
481 printf ("# File has%s been updated.\n",
482 f->updated ? "" : " not");
483 switch (f->command_state)
485 case cs_running:
486 puts ("# Commands currently running (THIS IS A BUG).");
487 break;
488 case cs_deps_running:
489 puts ("# Dependencies commands running (THIS IS A BUG).");
490 break;
491 case cs_not_started:
492 case cs_finished:
493 switch (f->update_status)
495 case -1:
496 break;
497 case 0:
498 puts ("# Successfully updated.");
499 break;
500 case 1:
501 assert (question_flag);
502 puts ("# Needs to be updated (-q is set).");
503 break;
504 case 2:
505 puts ("# Failed to be updated.");
506 break;
507 default:
508 puts ("# Invalid value in `update_status' member!");
509 fflush (stdout);
510 fflush (stderr);
511 abort ();
513 break;
514 default:
515 puts ("# Invalid value in `command_state' member!");
516 fflush (stdout);
517 fflush (stderr);
518 abort ();
521 if (f->variables != 0)
522 print_file_variables (f);
524 if (f->cmds != 0)
525 print_commands (f->cmds);
528 void
529 print_file_data_base ()
531 register unsigned int i, nfiles, per_bucket;
532 register struct file *file;
534 puts ("\n# Files");
536 per_bucket = nfiles = 0;
537 for (i = 0; i < FILE_BUCKETS; ++i)
539 register unsigned int this_bucket = 0;
541 for (file = files[i]; file != 0; file = file->next)
543 register struct file *f;
545 ++this_bucket;
547 for (f = file; f != 0; f = f->prev)
548 print_file (f);
551 nfiles += this_bucket;
552 if (this_bucket > per_bucket)
553 per_bucket = this_bucket;
556 if (nfiles == 0)
557 puts ("\n# No files.");
558 else
560 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
561 #ifndef NO_FLOAT
562 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
563 ((double) nfiles) / ((double) FILE_BUCKETS) * 100.0, per_bucket);
564 #endif