1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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)
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. */
26 /* Hash table of files the makefile knows how to make. */
29 #define FILE_BUCKETS 1007
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. */
47 register struct file
*f
;
49 register unsigned int hashval
;
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')
61 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
66 /* It was all slashes after a dot. */
70 for (n
= name
; *n
!= '\0'; ++n
)
72 hashval
%= FILE_BUCKETS
;
74 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
75 if (streq (f
->name
, name
))
84 register struct file
*f
, *new;
86 register unsigned int hashval
;
92 for (n
= name
; *n
!= '\0'; ++n
)
94 hashval
%= FILE_BUCKETS
;
96 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
97 if (streq (f
->name
, name
))
100 if (f
!= 0 && !f
->double_colon
)
103 new = (struct file
*) xmalloc (sizeof (struct file
));
104 bzero ((char *) new, sizeof (struct file
));
106 new->update_status
= -1;
110 /* This is a completely new file. */
111 new->next
= files
[hashval
];
112 files
[hashval
] = new;
116 /* There is already a double-colon entry for this file. */
117 new->double_colon
= f
;
126 /* Rename FILE to NAME. This is not as simple as resetting
127 the `name' member, since it must be put in a new hash bucket,
128 and possibly merged with an existing file called NAME. */
131 rename_file (file
, name
)
132 register struct file
*file
;
135 char *oldname
= file
->name
;
136 register unsigned int oldhash
;
139 while (file
->renamed
!= 0)
140 file
= file
->renamed
;
142 /* Find the hash values of the old and new names. */
145 for (n
= oldname
; *n
!= '\0'; ++n
)
148 file_hash_enter (file
, name
, oldhash
, file
->name
);
152 file_hash_enter (file
, name
, oldhash
, oldname
)
153 register struct file
*file
;
155 unsigned int oldhash
;
158 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
159 register unsigned int newhash
, newbucket
;
160 struct file
*oldfile
;
162 register struct file
*f
;
165 for (n
= name
; *n
!= '\0'; ++n
)
167 newbucket
= newhash
% FILE_BUCKETS
;
169 /* Look for an existing file under the new name. */
171 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
172 if (streq (oldfile
->name
, name
))
175 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
177 /* Remove FILE from its hash bucket. */
179 struct file
*lastf
= 0;
181 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
185 files
[oldbucket
] = f
->next
;
187 lastf
->next
= f
->next
;
190 /* Give FILE its new name. */
193 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
198 /* There is no existing file with the new name. */
200 if (newbucket
!= oldbucket
)
202 /* Put FILE in its new hash bucket. */
203 file
->next
= files
[newbucket
];
204 files
[newbucket
] = file
;
209 /* There is an existing file with the new name.
210 We must merge FILE into the existing file. */
212 register struct dep
*d
;
216 if (oldfile
->cmds
== 0)
217 oldfile
->cmds
= file
->cmds
;
218 else if (file
->cmds
!= oldfile
->cmds
)
220 /* We have two sets of commands. We will go with the
221 one given in the rule explicitly mentioning this name,
222 but give a message to let the user know what's going on. */
223 if (oldfile
->cmds
->filename
!= 0)
224 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
225 "Commands were specified for \
226 file `%s' at %s:%u,",
227 oldname
, oldfile
->cmds
->filename
,
228 oldfile
->cmds
->lineno
);
230 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
231 "Commands for file `%s' were found by \
232 implicit rule search,",
234 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
235 "but `%s' is now considered the same file \
238 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
239 "Commands for `%s' will be ignored \
240 in favor of those for `%s'.",
245 /* Merge the dependencies of the two files. */
249 oldfile
->deps
= file
->deps
;
254 d
->next
= file
->deps
;
257 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
259 if (oldfile
->double_colon
&& !file
->double_colon
)
260 fatal ("can't rename single-colon `%s' to double-colon `%s'",
262 if (!oldfile
->double_colon
&& file
->double_colon
)
263 fatal ("can't rename double-colon `%s' to single-colon `%s'",
266 if (file
->last_mtime
> oldfile
->last_mtime
)
267 /* %%% Kludge so -W wins on a file that gets vpathized. */
268 oldfile
->last_mtime
= file
->last_mtime
;
270 #define MERGE(field) oldfile->field |= file->field
272 MERGE (tried_implicit
);
280 file
->renamed
= oldfile
;
284 /* Remove all nonprecious intermediate files.
285 If SIG is nonzero, this was caused by a fatal signal,
286 meaning that a different message will be printed, and
287 the message will go to stderr rather than stdout. */
290 remove_intermediates (sig
)
294 register struct file
*f
;
297 if (!sig
&& just_print_flag
)
301 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
302 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
303 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
))
310 status
= unlink (f
->name
);
311 if (status
< 0 && errno
== ENOENT
)
317 error ("*** Deleting intermediate file `%s'", f
->name
);
318 else if (!silent_flag
)
322 fputs ("rm ", stdout
);
327 fputs (f
->name
, stdout
);
331 perror_with_name ("unlink: ", f
->name
);
342 /* For each dependency of each file, make the `struct dep' point
343 at the appropriate `struct file' (which may have to be created).
345 Also mark the files depended on by .PRECIOUS and .PHONY. */
350 register struct file
*f
, *f2
;
351 register struct dep
*d
;
354 /* Enter each dependency name as a file. */
355 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
356 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
357 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
358 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
361 d
->file
= lookup_file (d
->name
);
363 d
->file
= enter_file (d
->name
);
369 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
370 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
371 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
374 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
375 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
376 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
378 /* Mark this file as phony and nonexistent. */
380 f2
->last_mtime
= (time_t) -1;
383 f
= lookup_file (".EXPORT_ALL_VARIABLES");
384 if (f
!= 0 && f
->is_target
)
385 export_all_variables
= 1;
387 f
= lookup_file (".IGNORE");
388 if (f
!= 0 && f
->is_target
)
391 ignore_errors_flag
= 1;
393 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
394 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
395 f2
->command_flags
|= COMMANDS_NOERROR
;
398 f
= lookup_file (".SILENT");
399 if (f
!= 0 && f
->is_target
)
404 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
405 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
406 f2
->command_flags
|= COMMANDS_SILENT
;
409 f
= lookup_file (".POSIX");
410 if (f
!= 0 && f
->is_target
)
414 /* Set the `command_state' member of FILE and all its `also_make's. */
417 set_command_state (file
, state
)
423 file
->command_state
= state
;
425 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
426 d
->file
->command_state
= state
;
429 /* Print the data base of files. */
435 register struct dep
*d
;
439 puts ("# Not a target:");
440 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
442 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
443 printf (" %s", dep_name (d
));
447 puts ("# Precious file (dependency of .PRECIOUS).");
449 puts ("# Phony target (dependency of .PHONY).");
451 puts ("# Command-line target.");
453 puts ("# A default or MAKEFILES makefile.");
454 printf ("# Implicit rule search has%s been done.\n",
455 f
->tried_implicit
? "" : " not");
457 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
459 puts ("# File is an intermediate dependency.");
460 if (f
->also_make
!= 0)
462 fputs ("# Also makes:", stdout
);
463 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
464 printf (" %s", dep_name (d
));
467 if (f
->last_mtime
== (time_t) 0)
468 puts ("# Modification time never checked.");
469 else if (f
->last_mtime
== (time_t) -1)
470 puts ("# File does not exist.");
472 printf ("# Last modified %.24s (%ld)\n",
473 ctime (&f
->last_mtime
), (long int) f
->last_mtime
);
474 printf ("# File has%s been updated.\n",
475 f
->updated
? "" : " not");
476 switch (f
->command_state
)
479 puts ("# Commands currently running (THIS IS A BUG).");
481 case cs_deps_running
:
482 puts ("# Dependencies commands running (THIS IS A BUG).");
486 switch (f
->update_status
)
491 puts ("# Successfully updated.");
494 puts ("# Failed to be updated.");
497 puts ("# Invalid value in `update_status' member!");
504 puts ("# Invalid value in `command_state' member!");
510 if (f
->variables
!= 0)
511 print_file_variables (f
);
514 print_commands (f
->cmds
);
518 print_file_data_base ()
520 register unsigned int i
, nfiles
, per_bucket
;
521 register struct file
*file
;
525 per_bucket
= nfiles
= 0;
526 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
528 register unsigned int this_bucket
= 0;
530 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
532 register struct file
*f
;
536 for (f
= file
; f
!= 0; f
= f
->prev
)
540 nfiles
+= this_bucket
;
541 if (this_bucket
> per_bucket
)
542 per_bucket
= this_bucket
;
546 puts ("\n# No files.");
549 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
551 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
552 ((double) nfiles
) / ((double) FILE_BUCKETS
) * 100.0, per_bucket
);