1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 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
;
53 register char *lname
, *ln
;
59 /* This is also done in parse_file_seq, so this is redundant
60 for names read from makefiles. It is here for names passed
61 on the command line. */
63 lname
= (char *)malloc(strlen(name
) + 1);
64 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
65 *ln
= isupper(*n
) ? tolower(*n
) : *n
;
69 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
72 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
76 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
81 /* It was all slashes after a dot. */
93 for (n
= name
; *n
!= '\0'; ++n
)
95 hashval
%= FILE_BUCKETS
;
97 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
99 if (strieq (f
->hname
, name
))
117 register struct file
*f
, *new;
119 register unsigned int hashval
;
128 lname
= (char *)malloc (strlen (name
) + 1);
129 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
141 for (n
= name
; *n
!= '\0'; ++n
)
143 hashval
%= FILE_BUCKETS
;
145 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
146 if (strieq (f
->hname
, name
))
149 if (f
!= 0 && !f
->double_colon
)
157 new = (struct file
*) xmalloc (sizeof (struct file
));
158 bzero ((char *) new, sizeof (struct file
));
159 new->name
= new->hname
= name
;
160 new->update_status
= -1;
164 /* This is a completely new file. */
165 new->next
= files
[hashval
];
166 files
[hashval
] = new;
170 /* There is already a double-colon entry for this file. */
171 new->double_colon
= f
;
180 /* Rehash FILE to NAME. This is not as simple as resetting
181 the `hname' member, since it must be put in a new hash bucket,
182 and possibly merged with an existing file called NAME. */
185 rehash_file (file
, name
)
186 register struct file
*file
;
189 char *oldname
= file
->hname
;
190 register unsigned int oldhash
;
193 while (file
->renamed
!= 0)
194 file
= file
->renamed
;
196 /* Find the hash values of the old and new names. */
199 for (n
= oldname
; *n
!= '\0'; ++n
)
202 file_hash_enter (file
, name
, oldhash
, file
->name
);
205 /* Rename FILE to NAME. This is not as simple as resetting
206 the `name' member, since it must be put in a new hash bucket,
207 and possibly merged with an existing file called NAME. */
210 rename_file (file
, name
)
211 register struct file
*file
;
214 rehash_file(file
, name
);
217 file
->name
= file
->hname
;
223 file_hash_enter (file
, name
, oldhash
, oldname
)
224 register struct file
*file
;
226 unsigned int oldhash
;
229 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
230 register unsigned int newhash
, newbucket
;
231 struct file
*oldfile
;
233 register struct file
*f
;
236 for (n
= name
; *n
!= '\0'; ++n
)
238 newbucket
= newhash
% FILE_BUCKETS
;
240 /* Look for an existing file under the new name. */
242 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
243 if (strieq (oldfile
->hname
, name
))
246 /* If the old file is the same as the new file, something's wrong. */
247 assert (oldfile
!= file
);
249 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
251 /* Remove FILE from its hash bucket. */
253 struct file
*lastf
= 0;
255 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
259 files
[oldbucket
] = f
->next
;
261 lastf
->next
= f
->next
;
264 /* Give FILE its new name. */
267 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
272 /* There is no existing file with the new name. */
274 if (newbucket
!= oldbucket
)
276 /* Put FILE in its new hash bucket. */
277 file
->next
= files
[newbucket
];
278 files
[newbucket
] = file
;
283 /* There is an existing file with the new name.
284 We must merge FILE into the existing file. */
286 register struct dep
*d
;
290 if (oldfile
->cmds
== 0)
291 oldfile
->cmds
= file
->cmds
;
292 else if (file
->cmds
!= oldfile
->cmds
)
294 /* We have two sets of commands. We will go with the
295 one given in the rule explicitly mentioning this name,
296 but give a message to let the user know what's going on. */
297 if (oldfile
->cmds
->filename
!= 0)
298 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
299 "Commands were specified for \
300 file `%s' at %s:%u,",
301 oldname
, oldfile
->cmds
->filename
,
302 oldfile
->cmds
->lineno
);
304 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
305 "Commands for file `%s' were found by \
306 implicit rule search,",
308 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
309 "but `%s' is now considered the same file \
312 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
313 "Commands for `%s' will be ignored \
314 in favor of those for `%s'.",
319 /* Merge the dependencies of the two files. */
323 oldfile
->deps
= file
->deps
;
328 d
->next
= file
->deps
;
331 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
333 if (oldfile
->double_colon
&& file
->is_target
&& !file
->double_colon
)
334 fatal ("can't rename single-colon `%s' to double-colon `%s'",
336 if (!oldfile
->double_colon
&& file
->double_colon
)
338 if (oldfile
->is_target
)
339 fatal ("can't rename double-colon `%s' to single-colon `%s'",
342 oldfile
->double_colon
= file
->double_colon
;
345 if (file
->last_mtime
> oldfile
->last_mtime
)
346 /* %%% Kludge so -W wins on a file that gets vpathized. */
347 oldfile
->last_mtime
= file
->last_mtime
;
349 #define MERGE(field) oldfile->field |= file->field
351 MERGE (tried_implicit
);
357 MERGE (ignore_vpath
);
360 file
->renamed
= oldfile
;
364 /* Remove all nonprecious intermediate files.
365 If SIG is nonzero, this was caused by a fatal signal,
366 meaning that a different message will be printed, and
367 the message will go to stderr rather than stdout. */
370 remove_intermediates (sig
)
374 register struct file
*f
;
377 if (question_flag
|| touch_flag
)
379 if (sig
&& just_print_flag
)
383 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
384 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
385 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
389 if (f
->update_status
== -1)
390 /* If nothing would have created this file yet,
391 don't print an "rm" command for it. */
393 else if (just_print_flag
)
397 status
= unlink (f
->name
);
398 if (status
< 0 && errno
== ENOENT
)
404 error ("*** Deleting intermediate file `%s'", f
->name
);
405 else if (!silent_flag
)
409 fputs ("rm ", stdout
);
414 fputs (f
->name
, stdout
);
418 perror_with_name ("unlink: ", f
->name
);
429 /* For each dependency of each file, make the `struct dep' point
430 at the appropriate `struct file' (which may have to be created).
432 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
433 and various other special targets. */
438 register struct file
*f
, *f2
;
439 register struct dep
*d
;
442 /* Enter each dependency name as a file. */
443 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
444 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
445 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
446 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
449 d
->file
= lookup_file (d
->name
);
451 d
->file
= enter_file (d
->name
);
457 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
458 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
459 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
462 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
463 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
464 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
466 /* Mark this file as phony and nonexistent. */
468 f2
->last_mtime
= (time_t) -1;
471 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
473 /* .INTERMEDIATE with deps listed
474 marks those deps as intermediate files. */
475 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
476 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
477 f2
->intermediate
= 1;
478 /* .INTERMEDIATE with no deps does nothing.
479 Marking all files as intermediates is useless
480 since the goal targets would be deleted after they are built. */
483 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
485 /* .SECONDARY with deps listed
486 marks those deps as intermediate files
487 in that they don't get rebuilt if not actually needed;
488 but unlike real intermediate files,
489 these are not deleted after make finishes. */
492 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
493 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
494 f2
->intermediate
= f2
->secondary
= 1;
496 /* .SECONDARY with no deps listed marks *all* files that way. */
500 for (i
= 0; i
< FILE_BUCKETS
; i
++)
501 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
502 f2
->intermediate
= f2
->secondary
= 1;
506 f
= lookup_file (".EXPORT_ALL_VARIABLES");
507 if (f
!= 0 && f
->is_target
)
508 export_all_variables
= 1;
510 f
= lookup_file (".IGNORE");
511 if (f
!= 0 && f
->is_target
)
514 ignore_errors_flag
= 1;
516 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
517 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
518 f2
->command_flags
|= COMMANDS_NOERROR
;
521 f
= lookup_file (".SILENT");
522 if (f
!= 0 && f
->is_target
)
527 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
528 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
529 f2
->command_flags
|= COMMANDS_SILENT
;
532 f
= lookup_file (".POSIX");
533 if (f
!= 0 && f
->is_target
)
537 /* Set the `command_state' member of FILE and all its `also_make's. */
540 set_command_state (file
, state
)
546 file
->command_state
= state
;
548 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
549 d
->file
->command_state
= state
;
552 /* Print the data base of files. */
558 register struct dep
*d
;
562 puts ("# Not a target:");
563 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
565 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
566 printf (" %s", dep_name (d
));
570 puts ("# Precious file (dependency of .PRECIOUS).");
572 puts ("# Phony target (dependency of .PHONY).");
574 puts ("# Command-line target.");
576 puts ("# A default or MAKEFILES makefile.");
577 printf ("# Implicit rule search has%s been done.\n",
578 f
->tried_implicit
? "" : " not");
580 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
582 puts ("# File is an intermediate dependency.");
583 if (f
->also_make
!= 0)
585 fputs ("# Also makes:", stdout
);
586 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
587 printf (" %s", dep_name (d
));
590 if (f
->last_mtime
== (time_t) 0)
591 puts ("# Modification time never checked.");
592 else if (f
->last_mtime
== (time_t) -1)
593 puts ("# File does not exist.");
595 printf ("# Last modified %.24s (%ld)\n",
596 ctime (&f
->last_mtime
), (long int) f
->last_mtime
);
597 printf ("# File has%s been updated.\n",
598 f
->updated
? "" : " not");
599 switch (f
->command_state
)
602 puts ("# Commands currently running (THIS IS A BUG).");
604 case cs_deps_running
:
605 puts ("# Dependencies commands running (THIS IS A BUG).");
609 switch (f
->update_status
)
614 puts ("# Successfully updated.");
617 assert (question_flag
);
618 puts ("# Needs to be updated (-q is set).");
621 puts ("# Failed to be updated.");
624 puts ("# Invalid value in `update_status' member!");
631 puts ("# Invalid value in `command_state' member!");
637 if (f
->variables
!= 0)
638 print_file_variables (f
);
641 print_commands (f
->cmds
);
645 print_file_data_base ()
647 register unsigned int i
, nfiles
, per_bucket
;
648 register struct file
*file
;
652 per_bucket
= nfiles
= 0;
653 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
655 register unsigned int this_bucket
= 0;
657 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
659 register struct file
*f
;
663 for (f
= file
; f
!= 0; f
= f
->prev
)
667 nfiles
+= this_bucket
;
668 if (this_bucket
> per_bucket
)
669 per_bucket
= this_bucket
;
673 puts ("\n# No files.");
676 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
678 printf ("# average %.3f files per bucket, max %u files in one bucket.\n",
679 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);