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)
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. */
27 /* Hash table of files the makefile knows how to make. */
30 #define FILE_BUCKETS 1007
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. */
48 register struct file
*f
;
50 register unsigned int hashval
;
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')
62 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
67 /* It was all slashes after a dot. */
71 for (n
= name
; *n
!= '\0'; ++n
)
73 hashval
%= FILE_BUCKETS
;
75 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
76 if (streq (f
->name
, name
))
85 register struct file
*f
, *new;
87 register unsigned int hashval
;
93 for (n
= name
; *n
!= '\0'; ++n
)
95 hashval
%= FILE_BUCKETS
;
97 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
98 if (streq (f
->name
, name
))
101 if (f
!= 0 && !f
->double_colon
)
104 new = (struct file
*) xmalloc (sizeof (struct file
));
105 bzero ((char *) new, sizeof (struct file
));
107 new->update_status
= -1;
111 /* This is a completely new file. */
112 new->next
= files
[hashval
];
113 files
[hashval
] = new;
117 /* There is already a double-colon entry for this file. */
118 new->double_colon
= f
;
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. */
132 rename_file (file
, name
)
133 register struct file
*file
;
136 char *oldname
= file
->name
;
137 register unsigned int oldhash
;
140 while (file
->renamed
!= 0)
141 file
= file
->renamed
;
143 /* Find the hash values of the old and new names. */
146 for (n
= oldname
; *n
!= '\0'; ++n
)
149 file_hash_enter (file
, name
, oldhash
, file
->name
);
153 file_hash_enter (file
, name
, oldhash
, oldname
)
154 register struct file
*file
;
156 unsigned int oldhash
;
159 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
160 register unsigned int newhash
, newbucket
;
161 struct file
*oldfile
;
163 register struct file
*f
;
166 for (n
= name
; *n
!= '\0'; ++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
))
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
)
186 files
[oldbucket
] = f
->next
;
188 lastf
->next
= f
->next
;
191 /* Give FILE its new name. */
194 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
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
;
210 /* There is an existing file with the new name.
211 We must merge FILE into the existing file. */
213 register struct dep
*d
;
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
);
231 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
232 "Commands for file `%s' were found by \
233 implicit rule search,",
235 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
236 "but `%s' is now considered the same file \
239 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
240 "Commands for `%s' will be ignored \
241 in favor of those for `%s'.",
246 /* Merge the dependencies of the two files. */
250 oldfile
->deps
= file
->deps
;
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'",
263 if (!oldfile
->double_colon
&& file
->double_colon
)
264 fatal ("can't rename double-colon `%s' to single-colon `%s'",
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
273 MERGE (tried_implicit
);
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. */
291 remove_intermediates (sig
)
295 register struct file
*f
;
298 if (question_flag
|| touch_flag
)
300 if (sig
&& just_print_flag
)
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
))
309 if (f
->update_status
== -1)
310 /* If nothing would have created this file yet,
311 don't print an "rm" command for it. */
313 else if (just_print_flag
)
317 status
= unlink (f
->name
);
318 if (status
< 0 && errno
== ENOENT
)
324 error ("*** Deleting intermediate file `%s'", f
->name
);
325 else if (!silent_flag
)
329 fputs ("rm ", stdout
);
334 fputs (f
->name
, stdout
);
338 perror_with_name ("unlink: ", f
->name
);
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. */
357 register struct file
*f
, *f2
;
358 register struct dep
*d
;
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
)
368 d
->file
= lookup_file (d
->name
);
370 d
->file
= enter_file (d
->name
);
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
)
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. */
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
)
398 ignore_errors_flag
= 1;
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
)
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
)
421 /* Set the `command_state' member of FILE and all its `also_make's. */
424 set_command_state (file
, state
)
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. */
442 register struct dep
*d
;
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
));
454 puts ("# Precious file (dependency of .PRECIOUS).");
456 puts ("# Phony target (dependency of .PHONY).");
458 puts ("# Command-line target.");
460 puts ("# A default or MAKEFILES makefile.");
461 printf ("# Implicit rule search has%s been done.\n",
462 f
->tried_implicit
? "" : " not");
464 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
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
));
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.");
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
)
486 puts ("# Commands currently running (THIS IS A BUG).");
488 case cs_deps_running
:
489 puts ("# Dependencies commands running (THIS IS A BUG).");
493 switch (f
->update_status
)
498 puts ("# Successfully updated.");
501 assert (question_flag
);
502 puts ("# Needs to be updated (-q is set).");
505 puts ("# Failed to be updated.");
508 puts ("# Invalid value in `update_status' member!");
515 puts ("# Invalid value in `command_state' member!");
521 if (f
->variables
!= 0)
522 print_file_variables (f
);
525 print_commands (f
->cmds
);
529 print_file_data_base ()
531 register unsigned int i
, nfiles
, per_bucket
;
532 register struct file
*file
;
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
;
547 for (f
= file
; f
!= 0; f
= f
->prev
)
551 nfiles
+= this_bucket
;
552 if (this_bucket
> per_bucket
)
553 per_bucket
= this_bucket
;
557 puts ("\n# No files.");
560 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
562 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
563 ((double) nfiles
) / ((double) FILE_BUCKETS
) * 100.0, per_bucket
);