Formerly main.c.~78~
[make.git] / file.c
blobd5b4c02e109adf72dc2012d8c75484634834f29b
1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
2 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"
26 /* Hash table of files the makefile knows how to make. */
28 #ifndef FILE_BUCKETS
29 #define FILE_BUCKETS 1007
30 #endif
31 static struct file *files[FILE_BUCKETS];
33 /* Number of files with the `intermediate' flag set. */
35 unsigned int num_intermediates = 0;
38 /* Access the hash table of all file records.
39 lookup_file given a name, return the struct file * for that name,
40 or nil if there is none.
41 enter_file similar, but create one if there is none. */
43 struct file *
44 lookup_file (name)
45 char *name;
47 register struct file *f;
48 register char *n;
49 register unsigned int hashval;
51 if (*name == '\0')
52 abort ();
54 /* This is also done in parse_file_seq, so this is redundant
55 for names read from makefiles. It is here for names passed
56 on the command line. */
57 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
59 name += 2;
60 while (*name == '/')
61 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
62 ++name;
65 if (*name == '\0')
66 /* It was all slashes after a dot. */
67 name = "./";
69 hashval = 0;
70 for (n = name; *n != '\0'; ++n)
71 HASH (hashval, *n);
72 hashval %= FILE_BUCKETS;
74 for (f = files[hashval]; f != 0; f = f->next)
75 if (streq (f->name, name))
76 return f;
77 return 0;
80 struct file *
81 enter_file (name)
82 char *name;
84 register struct file *f, *new;
85 register char *n;
86 register unsigned int hashval;
88 if (*name == '\0')
89 abort ();
91 /* This is also done in parse_file_seq, so this is redundant
92 for names read from makefiles. It is here for names passed
93 on the command line. */
94 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
96 name += 2;
97 while (*name == '/')
98 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
99 ++name;
102 if (*name == '\0')
104 /* It was all slashes! Move back to the dot and truncate
105 it after the first slash, so it becomes just "./". */
107 --name;
108 while (name[0] != '.');
109 name[2] = '\0';
112 hashval = 0;
113 for (n = name; *n != '\0'; ++n)
114 HASH (hashval, *n);
115 hashval %= FILE_BUCKETS;
117 for (f = files[hashval]; f != 0; f = f->next)
118 if (streq (f->name, name))
119 break;
121 if (f != 0 && !f->double_colon)
122 return f;
124 new = (struct file *) xmalloc (sizeof (struct file));
125 bzero ((char *) new, sizeof (struct file));
126 new->name = name;
127 new->update_status = -1;
129 if (f == 0)
131 /* This is a completely new file. */
132 new->next = files[hashval];
133 files[hashval] = new;
135 else
137 /* There is already a double-colon entry for this file. */
138 while (f->prev != 0)
139 f = f->prev;
140 f->prev = new;
143 return new;
146 /* Rename FILE to NAME. This is not as simple as resetting
147 the `name' member, since it must be put in a new hash bucket,
148 and possibly merged with an existing file called NAME. */
150 void
151 rename_file (file, name)
152 register struct file *file;
153 char *name;
155 char *oldname = file->name;
156 register unsigned int oldhash;
157 register char *n;
159 while (file->renamed != 0)
160 file = file->renamed;
162 /* Find the hash values of the old and new names. */
164 oldhash = 0;
165 for (n = oldname; *n != '\0'; ++n)
166 HASH (oldhash, *n);
168 file_hash_enter (file, name, oldhash, file->name);
171 void
172 file_hash_enter (file, name, oldhash, oldname)
173 register struct file *file;
174 char *name;
175 unsigned int oldhash;
176 char *oldname;
178 unsigned int oldbucket = oldhash % FILE_BUCKETS;
179 register unsigned int newhash, newbucket;
180 struct file *oldfile;
181 register char *n;
182 register struct file *f;
184 newhash = 0;
185 for (n = name; *n != '\0'; ++n)
186 HASH (newhash, *n);
187 newbucket = newhash % FILE_BUCKETS;
189 /* Look for an existing file under the new name. */
191 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
192 if (streq (oldfile->name, name))
193 break;
195 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
197 /* Remove FILE from its hash bucket. */
199 struct file *lastf = 0;
201 for (f = files[oldbucket]; f != file; f = f->next)
202 lastf = f;
204 if (lastf == 0)
205 files[oldbucket] = f->next;
206 else
207 lastf->next = f->next;
210 /* Give FILE its new name. */
212 for (f = file; f != 0; f = f->prev)
213 f->name = name;
215 if (oldfile == 0)
217 /* There is no existing file with the new name. */
219 if (newbucket != oldbucket)
221 /* Put FILE in its new hash bucket. */
222 file->next = files[newbucket];
223 files[newbucket] = file;
226 else
228 /* There is an existing file with the new name.
229 We must merge FILE into the existing file. */
231 register struct dep *d;
233 if (file->cmds != 0)
235 if (oldfile->cmds == 0)
236 oldfile->cmds = file->cmds;
237 else if (file->cmds != oldfile->cmds)
239 /* We have two sets of commands. We will go with the
240 one given in the rule explicitly mentioning this name,
241 but give a message to let the user know what's going on. */
242 if (oldfile->cmds->filename != 0)
243 makefile_error (file->cmds->filename, file->cmds->lineno,
244 "Commands were specified for \
245 file `%s' at %s:%u,",
246 oldname, oldfile->cmds->filename,
247 oldfile->cmds->lineno);
248 else
249 makefile_error (file->cmds->filename, file->cmds->lineno,
250 "Commands for file `%s' were found by \
251 implicit rule search,",
252 oldname);
253 makefile_error (file->cmds->filename, file->cmds->lineno,
254 "but `%s' is now considered the same file \
255 as `%s'.",
256 oldname, name);
257 makefile_error (file->cmds->filename, file->cmds->lineno,
258 "Commands for `%s' will be ignored \
259 in favor of those for `%s'.",
260 name, oldname);
264 /* Merge the dependencies of the two files. */
266 d = oldfile->deps;
267 if (d == 0)
268 oldfile->deps = file->deps;
269 else
271 while (d->next != 0)
272 d = d->next;
273 d->next = file->deps;
276 merge_variable_set_lists (&oldfile->variables, file->variables);
278 if (oldfile->double_colon && !file->double_colon)
279 fatal ("can't rename single-colon `%s' to double-colon `%s'",
280 oldname, name);
281 if (!oldfile->double_colon && file->double_colon)
282 fatal ("can't rename double-colon `%s' to single-colon `%s'",
283 oldname, name);
285 if (file->last_mtime > oldfile->last_mtime)
286 /* %%% Kludge so -W wins on a file that gets vpathized. */
287 oldfile->last_mtime = file->last_mtime;
289 #define MERGE(field) oldfile->field |= file->field
290 MERGE (precious);
291 MERGE (tried_implicit);
292 MERGE (updating);
293 MERGE (updated);
294 MERGE (is_target);
295 MERGE (cmd_target);
296 MERGE (phony);
297 #undef MERGE
299 file->renamed = oldfile;
303 /* Remove all nonprecious intermediate files.
304 If SIG is nonzero, this was caused by a fatal signal,
305 meaning that a different message will be printed, and
306 the message will go to stderr rather than stdout. */
308 void
309 remove_intermediates (sig)
310 int sig;
312 register int i;
313 register struct file *f;
314 char doneany;
316 if (!sig && just_print_flag)
317 return;
319 doneany = 0;
320 for (i = 0; i < FILE_BUCKETS; ++i)
321 for (f = files[i]; f != 0; f = f->next)
322 if (f->intermediate && (f->dontcare || !f->precious))
324 int status;
325 if (just_print_flag)
326 status = 0;
327 else
329 status = unlink (f->name);
330 if (status < 0 && errno == ENOENT)
331 continue;
333 if (!f->dontcare)
335 if (sig)
336 error ("*** Deleting file `%s'", f->name);
337 else if (!silent_flag)
339 if (!doneany)
341 fputs ("rm ", stdout);
342 doneany = 1;
344 putchar (' ');
345 fputs (f->name, stdout);
346 fflush (stdout);
348 if (status < 0)
349 perror_with_name ("unlink: ", f->name);
353 if (doneany && !sig)
355 putchar ('\n');
356 fflush (stdout);
360 /* For each dependency of each file, make the `struct dep' point
361 at the appropriate `struct file' (which may have to be created).
363 Also mark the files depended on by .PRECIOUS and .PHONY. */
365 void
366 snap_deps ()
368 register struct file *f, *f2;
369 register struct dep *d;
370 register int i;
372 /* Enter each dependency name as a file. */
373 for (i = 0; i < FILE_BUCKETS; ++i)
374 for (f = files[i]; f != 0; f = f->next)
375 for (f2 = f; f2 != 0; f2 = f2->prev)
376 for (d = f2->deps; d != 0; d = d->next)
377 if (d->name != 0)
379 d->file = lookup_file (d->name);
380 if (d->file == 0)
381 d->file = enter_file (d->name);
382 else
383 free (d->name);
384 d->name = 0;
387 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
388 for (d = f->deps; d != 0; d = d->next)
389 for (f2 = d->file; f2 != 0; f2 = f2->prev)
390 f2->precious = 1;
392 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
393 for (d = f->deps; d != 0; d = d->next)
394 for (f2 = d->file; f2 != 0; f2 = f2->prev)
396 /* Mark this file as phony and nonexistent. */
397 f2->phony = 1;
398 f2->last_mtime = (time_t) -1;
401 f = lookup_file (".EXPORT_ALL_VARIABLES");
402 if (f != 0 && f->is_target)
403 export_all_variables = 1;
406 /* Print the data base of files. */
408 void
409 print_file_data_base ()
411 register unsigned int i, nfiles, per_bucket;
412 register struct file *file;
413 register struct dep *d;
415 puts ("\n# Files");
417 per_bucket = nfiles = 0;
418 for (i = 0; i < FILE_BUCKETS; ++i)
420 register unsigned int this_bucket = 0;
422 for (file = files[i]; file != 0; file = file->next)
424 register struct file *f;
426 ++this_bucket;
428 for (f = file; f != 0; f = f->prev)
430 putchar ('\n');
431 if (!f->is_target)
432 puts ("# Not a target:");
433 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
435 for (d = f->deps; d != 0; d = d->next)
436 printf (" %s", dep_name (d));
437 putchar ('\n');
439 if (f->precious)
440 puts ("# Precious file (dependency of .PRECIOUS).");
441 if (f->phony)
442 puts ("# Phony target (dependency of .PHONY).");
443 if (f->cmd_target)
444 puts ("# Command-line target.");
445 if (f->dontcare)
446 puts ("# A default or MAKEFILES makefile.");
447 printf ("# Implicit rule search has%s been done.\n",
448 f->tried_implicit ? "" : " not");
449 if (f->stem != 0)
450 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
451 if (f->intermediate)
452 puts ("# File is an intermediate dependency.");
453 if (f->also_make != 0)
455 fputs ("# Also makes:", stdout);
456 for (d = f->also_make; d != 0; d = d->next)
457 printf (" %s", dep_name (d));
458 putchar ('\n');
460 if (f->last_mtime == (time_t) 0)
461 puts ("# Modification time never checked.");
462 else if (f->last_mtime == (time_t) -1)
463 puts ("# File does not exist.");
464 else
465 printf ("# Last modified %.24s (%ld)\n",
466 ctime (&f->last_mtime), (long int) f->last_mtime);
467 printf ("# File has%s been updated.\n",
468 f->updated ? "" : " not");
469 switch (f->command_state)
471 case cs_running:
472 puts ("# Commands currently running (THIS IS A BUG).");
473 break;
474 case cs_deps_running:
475 puts ("# Dependencies commands running (THIS IS A BUG).");
476 break;
477 case cs_not_started:
478 case cs_finished:
479 switch (f->update_status)
481 case -1:
482 break;
483 case 0:
484 puts ("# Successfully updated.");
485 break;
486 case 1:
487 puts ("# Failed to be updated.");
488 break;
489 default:
490 puts ("# Invalid value in `update_status' member!");
491 fflush (stdout);
492 fflush (stderr);
493 abort ();
495 break;
496 default:
497 puts ("# Invalid value in `command_state' member!");
498 fflush (stdout);
499 fflush (stderr);
500 abort ();
503 if (f->variables != 0)
504 print_file_variables (file);
506 if (f->cmds != 0)
507 print_commands (f->cmds);
511 nfiles += this_bucket;
512 if (this_bucket > per_bucket)
513 per_bucket = this_bucket;
516 if (nfiles == 0)
517 puts ("\n# No files.");
518 else
520 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
521 #ifndef NO_FLOAT
522 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
523 ((double) FILE_BUCKETS) / ((double) nfiles) * 100.0, per_bucket);
524 #endif