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. */
28 /* Hash table of files the makefile knows how to make. */
31 #define FILE_BUCKETS 1007
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. */
49 register struct file
*f
;
51 register unsigned int hashval
;
56 /* This is also done in parse_file_seq, so this is redundant
57 for names read from makefiles. It is here for names passed
58 on the command line. */
60 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
63 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
67 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
72 /* It was all slashes after a dot. */
80 for (n
= name
; *n
!= '\0'; ++n
)
82 hashval
%= FILE_BUCKETS
;
84 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
86 if (streq (f
->name
, name
))
98 register struct file
*f
, *new;
100 register unsigned int hashval
;
109 lname
= (char *)malloc (strlen (name
) + 1);
110 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
122 for (n
= name
; *n
!= '\0'; ++n
)
124 hashval
%= FILE_BUCKETS
;
126 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
127 if (streq (f
->name
, name
))
130 if (f
!= 0 && !f
->double_colon
)
138 new = (struct file
*) xmalloc (sizeof (struct file
));
139 bzero ((char *) new, sizeof (struct file
));
141 new->update_status
= -1;
145 /* This is a completely new file. */
146 new->next
= files
[hashval
];
147 files
[hashval
] = new;
151 /* There is already a double-colon entry for this file. */
152 new->double_colon
= f
;
161 /* Rename FILE to NAME. This is not as simple as resetting
162 the `name' member, since it must be put in a new hash bucket,
163 and possibly merged with an existing file called NAME. */
166 rename_file (file
, name
)
167 register struct file
*file
;
170 char *oldname
= file
->name
;
171 register unsigned int oldhash
;
174 while (file
->renamed
!= 0)
175 file
= file
->renamed
;
177 /* Find the hash values of the old and new names. */
180 for (n
= oldname
; *n
!= '\0'; ++n
)
183 file_hash_enter (file
, name
, oldhash
, file
->name
);
187 file_hash_enter (file
, name
, oldhash
, oldname
)
188 register struct file
*file
;
190 unsigned int oldhash
;
193 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
194 register unsigned int newhash
, newbucket
;
195 struct file
*oldfile
;
197 register struct file
*f
;
200 for (n
= name
; *n
!= '\0'; ++n
)
202 newbucket
= newhash
% FILE_BUCKETS
;
204 /* Look for an existing file under the new name. */
206 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
207 if (streq (oldfile
->name
, name
))
210 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
212 /* Remove FILE from its hash bucket. */
214 struct file
*lastf
= 0;
216 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
220 files
[oldbucket
] = f
->next
;
222 lastf
->next
= f
->next
;
225 /* Give FILE its new name. */
228 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
233 /* There is no existing file with the new name. */
235 if (newbucket
!= oldbucket
)
237 /* Put FILE in its new hash bucket. */
238 file
->next
= files
[newbucket
];
239 files
[newbucket
] = file
;
244 /* There is an existing file with the new name.
245 We must merge FILE into the existing file. */
247 register struct dep
*d
;
251 if (oldfile
->cmds
== 0)
252 oldfile
->cmds
= file
->cmds
;
253 else if (file
->cmds
!= oldfile
->cmds
)
255 /* We have two sets of commands. We will go with the
256 one given in the rule explicitly mentioning this name,
257 but give a message to let the user know what's going on. */
258 if (oldfile
->cmds
->filename
!= 0)
259 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
260 "Commands were specified for \
261 file `%s' at %s:%u,",
262 oldname
, oldfile
->cmds
->filename
,
263 oldfile
->cmds
->lineno
);
265 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
266 "Commands for file `%s' were found by \
267 implicit rule search,",
269 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
270 "but `%s' is now considered the same file \
273 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
274 "Commands for `%s' will be ignored \
275 in favor of those for `%s'.",
280 /* Merge the dependencies of the two files. */
284 oldfile
->deps
= file
->deps
;
289 d
->next
= file
->deps
;
292 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
294 if (oldfile
->double_colon
&& !file
->double_colon
)
295 fatal ("can't rename single-colon `%s' to double-colon `%s'",
297 if (!oldfile
->double_colon
&& file
->double_colon
)
298 fatal ("can't rename double-colon `%s' to single-colon `%s'",
301 if (file
->last_mtime
> oldfile
->last_mtime
)
302 /* %%% Kludge so -W wins on a file that gets vpathized. */
303 oldfile
->last_mtime
= file
->last_mtime
;
305 #define MERGE(field) oldfile->field |= file->field
307 MERGE (tried_implicit
);
315 file
->renamed
= oldfile
;
319 /* Remove all nonprecious intermediate files.
320 If SIG is nonzero, this was caused by a fatal signal,
321 meaning that a different message will be printed, and
322 the message will go to stderr rather than stdout. */
325 remove_intermediates (sig
)
329 register struct file
*f
;
332 if (question_flag
|| touch_flag
)
334 if (sig
&& just_print_flag
)
338 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
339 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
340 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
344 if (f
->update_status
== -1)
345 /* If nothing would have created this file yet,
346 don't print an "rm" command for it. */
348 else if (just_print_flag
)
352 status
= unlink (f
->name
);
353 if (status
< 0 && errno
== ENOENT
)
359 error ("*** Deleting intermediate file `%s'", f
->name
);
360 else if (!silent_flag
)
364 fputs ("rm ", stdout
);
369 fputs (f
->name
, stdout
);
373 perror_with_name ("unlink: ", f
->name
);
384 /* For each dependency of each file, make the `struct dep' point
385 at the appropriate `struct file' (which may have to be created).
387 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
388 and various other special targets. */
393 register struct file
*f
, *f2
;
394 register struct dep
*d
;
397 /* Enter each dependency name as a file. */
398 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
399 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
400 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
401 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
404 d
->file
= lookup_file (d
->name
);
406 d
->file
= enter_file (d
->name
);
412 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
413 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
414 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
417 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
418 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
419 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
421 /* Mark this file as phony and nonexistent. */
423 f2
->last_mtime
= (time_t) -1;
426 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
428 /* .INTERMEDIATE with deps listed
429 marks those deps as intermediate files. */
430 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
431 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
432 f2
->intermediate
= 1;
433 /* .INTERMEDIATE with no deps does nothing.
434 Marking all files as intermediates is useless
435 since the goal targets would be deleted after they are built. */
438 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
440 /* .SECONDARY with deps listed
441 marks those deps as intermediate files
442 in that they don't get rebuilt if not actually needed;
443 but unlike real intermediate files,
444 these are not deleted after make finishes. */
447 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
448 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
449 f2
->intermediate
= f2
->secondary
= 1;
451 /* .SECONDARY with no deps listed marks *all* files that way. */
455 for (i
= 0; i
< FILE_BUCKETS
; i
++)
456 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
457 f2
->intermediate
= f2
->secondary
= 1;
461 f
= lookup_file (".EXPORT_ALL_VARIABLES");
462 if (f
!= 0 && f
->is_target
)
463 export_all_variables
= 1;
465 f
= lookup_file (".IGNORE");
466 if (f
!= 0 && f
->is_target
)
469 ignore_errors_flag
= 1;
471 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
472 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
473 f2
->command_flags
|= COMMANDS_NOERROR
;
476 f
= lookup_file (".SILENT");
477 if (f
!= 0 && f
->is_target
)
482 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
483 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
484 f2
->command_flags
|= COMMANDS_SILENT
;
487 f
= lookup_file (".POSIX");
488 if (f
!= 0 && f
->is_target
)
492 /* Set the `command_state' member of FILE and all its `also_make's. */
495 set_command_state (file
, state
)
501 file
->command_state
= state
;
503 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
504 d
->file
->command_state
= state
;
507 /* Print the data base of files. */
513 register struct dep
*d
;
515 extern char *cvt_time
PARAMS ((unsigned long));
519 puts ("# Not a target:");
520 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
522 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
523 printf (" %s", dep_name (d
));
527 puts ("# Precious file (dependency of .PRECIOUS).");
529 puts ("# Phony target (dependency of .PHONY).");
531 puts ("# Command-line target.");
533 puts ("# A default or MAKEFILES makefile.");
534 printf ("# Implicit rule search has%s been done.\n",
535 f
->tried_implicit
? "" : " not");
537 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
539 puts ("# File is an intermediate dependency.");
540 if (f
->also_make
!= 0)
542 fputs ("# Also makes:", stdout
);
543 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
544 printf (" %s", dep_name (d
));
547 if (f
->last_mtime
== (time_t) 0)
548 puts ("# Modification time never checked.");
549 else if (f
->last_mtime
== (time_t) -1)
550 puts ("# File does not exist.");
553 printf ("# Last modified %.24s (%0lx)\n",
554 cvt_time(f
->last_mtime
), (unsigned long) f
->last_mtime
);
556 printf ("# Last modified %.24s (%ld)\n",
557 ctime (&f
->last_mtime
), (long int) f
->last_mtime
);
559 printf ("# File has%s been updated.\n",
560 f
->updated
? "" : " not");
561 switch (f
->command_state
)
564 puts ("# Commands currently running (THIS IS A BUG).");
566 case cs_deps_running
:
567 puts ("# Dependencies commands running (THIS IS A BUG).");
571 switch (f
->update_status
)
576 puts ("# Successfully updated.");
579 assert (question_flag
);
580 puts ("# Needs to be updated (-q is set).");
583 puts ("# Failed to be updated.");
586 puts ("# Invalid value in `update_status' member!");
593 puts ("# Invalid value in `command_state' member!");
599 if (f
->variables
!= 0)
600 print_file_variables (f
);
603 print_commands (f
->cmds
);
607 print_file_data_base ()
609 register unsigned int i
, nfiles
, per_bucket
;
610 register struct file
*file
;
614 per_bucket
= nfiles
= 0;
615 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
617 register unsigned int this_bucket
= 0;
619 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
621 register struct file
*f
;
625 for (f
= file
; f
!= 0; f
= f
->prev
)
629 nfiles
+= this_bucket
;
630 if (this_bucket
> per_bucket
)
631 per_bucket
= this_bucket
;
635 puts ("\n# No files.");
638 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
640 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
641 ((double) nfiles
) / ((double) FILE_BUCKETS
) * 100.0, per_bucket
);