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. */
84 for (n
= name
; *n
!= '\0'; ++n
)
86 hashval
%= FILE_BUCKETS
;
88 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
90 if (strieq (f
->name
, name
))
102 register struct file
*f
, *new;
104 register unsigned int hashval
;
113 lname
= (char *)malloc (strlen (name
) + 1);
114 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
126 for (n
= name
; *n
!= '\0'; ++n
)
128 hashval
%= FILE_BUCKETS
;
130 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
131 if (strieq (f
->name
, name
))
134 if (f
!= 0 && !f
->double_colon
)
142 new = (struct file
*) xmalloc (sizeof (struct file
));
143 bzero ((char *) new, sizeof (struct file
));
145 new->update_status
= -1;
149 /* This is a completely new file. */
150 new->next
= files
[hashval
];
151 files
[hashval
] = new;
155 /* There is already a double-colon entry for this file. */
156 new->double_colon
= f
;
165 /* Rename FILE to NAME. This is not as simple as resetting
166 the `name' member, since it must be put in a new hash bucket,
167 and possibly merged with an existing file called NAME. */
170 rename_file (file
, name
)
171 register struct file
*file
;
174 char *oldname
= file
->name
;
175 register unsigned int oldhash
;
178 while (file
->renamed
!= 0)
179 file
= file
->renamed
;
181 /* Find the hash values of the old and new names. */
184 for (n
= oldname
; *n
!= '\0'; ++n
)
187 file_hash_enter (file
, name
, oldhash
, file
->name
);
191 file_hash_enter (file
, name
, oldhash
, oldname
)
192 register struct file
*file
;
194 unsigned int oldhash
;
197 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
198 register unsigned int newhash
, newbucket
;
199 struct file
*oldfile
;
201 register struct file
*f
;
204 for (n
= name
; *n
!= '\0'; ++n
)
206 newbucket
= newhash
% FILE_BUCKETS
;
208 /* Look for an existing file under the new name. */
210 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
211 if (strieq (oldfile
->name
, name
))
214 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
216 /* Remove FILE from its hash bucket. */
218 struct file
*lastf
= 0;
220 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
224 files
[oldbucket
] = f
->next
;
226 lastf
->next
= f
->next
;
229 /* Give FILE its new name. */
232 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
237 /* There is no existing file with the new name. */
239 if (newbucket
!= oldbucket
)
241 /* Put FILE in its new hash bucket. */
242 file
->next
= files
[newbucket
];
243 files
[newbucket
] = file
;
248 /* There is an existing file with the new name.
249 We must merge FILE into the existing file. */
251 register struct dep
*d
;
255 if (oldfile
->cmds
== 0)
256 oldfile
->cmds
= file
->cmds
;
257 else if (file
->cmds
!= oldfile
->cmds
)
259 /* We have two sets of commands. We will go with the
260 one given in the rule explicitly mentioning this name,
261 but give a message to let the user know what's going on. */
262 if (oldfile
->cmds
->filename
!= 0)
263 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
264 "Commands were specified for \
265 file `%s' at %s:%u,",
266 oldname
, oldfile
->cmds
->filename
,
267 oldfile
->cmds
->lineno
);
269 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
270 "Commands for file `%s' were found by \
271 implicit rule search,",
273 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
274 "but `%s' is now considered the same file \
277 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
278 "Commands for `%s' will be ignored \
279 in favor of those for `%s'.",
284 /* Merge the dependencies of the two files. */
288 oldfile
->deps
= file
->deps
;
293 d
->next
= file
->deps
;
296 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
298 if (oldfile
->double_colon
&& !file
->double_colon
)
299 fatal ("can't rename single-colon `%s' to double-colon `%s'",
301 if (!oldfile
->double_colon
&& file
->double_colon
)
302 fatal ("can't rename double-colon `%s' to single-colon `%s'",
305 if (file
->last_mtime
> oldfile
->last_mtime
)
306 /* %%% Kludge so -W wins on a file that gets vpathized. */
307 oldfile
->last_mtime
= file
->last_mtime
;
309 #define MERGE(field) oldfile->field |= file->field
311 MERGE (tried_implicit
);
319 file
->renamed
= oldfile
;
323 /* Remove all nonprecious intermediate files.
324 If SIG is nonzero, this was caused by a fatal signal,
325 meaning that a different message will be printed, and
326 the message will go to stderr rather than stdout. */
329 remove_intermediates (sig
)
333 register struct file
*f
;
336 if (question_flag
|| touch_flag
)
338 if (sig
&& just_print_flag
)
342 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
343 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
344 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
348 if (f
->update_status
== -1)
349 /* If nothing would have created this file yet,
350 don't print an "rm" command for it. */
352 else if (just_print_flag
)
356 status
= unlink (f
->name
);
357 if (status
< 0 && errno
== ENOENT
)
363 error ("*** Deleting intermediate file `%s'", f
->name
);
364 else if (!silent_flag
)
368 fputs ("rm ", stdout
);
373 fputs (f
->name
, stdout
);
377 perror_with_name ("unlink: ", f
->name
);
388 /* For each dependency of each file, make the `struct dep' point
389 at the appropriate `struct file' (which may have to be created).
391 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
392 and various other special targets. */
397 register struct file
*f
, *f2
;
398 register struct dep
*d
;
401 /* Enter each dependency name as a file. */
402 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
403 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
404 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
405 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
408 d
->file
= lookup_file (d
->name
);
410 d
->file
= enter_file (d
->name
);
416 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
417 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
418 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
421 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
422 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
423 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
425 /* Mark this file as phony and nonexistent. */
427 f2
->last_mtime
= (time_t) -1;
430 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
432 /* .INTERMEDIATE with deps listed
433 marks those deps as intermediate files. */
434 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
435 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
436 f2
->intermediate
= 1;
437 /* .INTERMEDIATE with no deps does nothing.
438 Marking all files as intermediates is useless
439 since the goal targets would be deleted after they are built. */
442 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
444 /* .SECONDARY with deps listed
445 marks those deps as intermediate files
446 in that they don't get rebuilt if not actually needed;
447 but unlike real intermediate files,
448 these are not deleted after make finishes. */
451 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
452 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
453 f2
->intermediate
= f2
->secondary
= 1;
455 /* .SECONDARY with no deps listed marks *all* files that way. */
459 for (i
= 0; i
< FILE_BUCKETS
; i
++)
460 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
461 f2
->intermediate
= f2
->secondary
= 1;
465 f
= lookup_file (".EXPORT_ALL_VARIABLES");
466 if (f
!= 0 && f
->is_target
)
467 export_all_variables
= 1;
469 f
= lookup_file (".IGNORE");
470 if (f
!= 0 && f
->is_target
)
473 ignore_errors_flag
= 1;
475 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
476 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
477 f2
->command_flags
|= COMMANDS_NOERROR
;
480 f
= lookup_file (".SILENT");
481 if (f
!= 0 && f
->is_target
)
486 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
487 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
488 f2
->command_flags
|= COMMANDS_SILENT
;
491 f
= lookup_file (".POSIX");
492 if (f
!= 0 && f
->is_target
)
496 /* Set the `command_state' member of FILE and all its `also_make's. */
499 set_command_state (file
, state
)
505 file
->command_state
= state
;
507 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
508 d
->file
->command_state
= state
;
511 /* Print the data base of files. */
517 register struct dep
*d
;
519 extern char *cvt_time
PARAMS ((unsigned long));
523 puts ("# Not a target:");
524 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
526 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
527 printf (" %s", dep_name (d
));
531 puts ("# Precious file (dependency of .PRECIOUS).");
533 puts ("# Phony target (dependency of .PHONY).");
535 puts ("# Command-line target.");
537 puts ("# A default or MAKEFILES makefile.");
538 printf ("# Implicit rule search has%s been done.\n",
539 f
->tried_implicit
? "" : " not");
541 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
543 puts ("# File is an intermediate dependency.");
544 if (f
->also_make
!= 0)
546 fputs ("# Also makes:", stdout
);
547 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
548 printf (" %s", dep_name (d
));
551 if (f
->last_mtime
== (time_t) 0)
552 puts ("# Modification time never checked.");
553 else if (f
->last_mtime
== (time_t) -1)
554 puts ("# File does not exist.");
557 printf ("# Last modified %.24s (%0lx)\n",
558 cvt_time(f
->last_mtime
), (unsigned long) f
->last_mtime
);
560 printf ("# Last modified %.24s (%ld)\n",
561 ctime (&f
->last_mtime
), (long int) f
->last_mtime
);
563 printf ("# File has%s been updated.\n",
564 f
->updated
? "" : " not");
565 switch (f
->command_state
)
568 puts ("# Commands currently running (THIS IS A BUG).");
570 case cs_deps_running
:
571 puts ("# Dependencies commands running (THIS IS A BUG).");
575 switch (f
->update_status
)
580 puts ("# Successfully updated.");
583 assert (question_flag
);
584 puts ("# Needs to be updated (-q is set).");
587 puts ("# Failed to be updated.");
590 puts ("# Invalid value in `update_status' member!");
597 puts ("# Invalid value in `command_state' member!");
603 if (f
->variables
!= 0)
604 print_file_variables (f
);
607 print_commands (f
->cmds
);
611 print_file_data_base ()
613 register unsigned int i
, nfiles
, per_bucket
;
614 register struct file
*file
;
618 per_bucket
= nfiles
= 0;
619 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
621 register unsigned int this_bucket
= 0;
623 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
625 register struct file
*f
;
629 for (f
= file
; f
!= 0; f
= f
->prev
)
633 nfiles
+= this_bucket
;
634 if (this_bucket
> per_bucket
)
635 per_bucket
= this_bucket
;
639 puts ("\n# No files.");
642 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
644 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
645 ((double) nfiles
) / ((double) FILE_BUCKETS
) * 100.0, per_bucket
);