1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96 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
->is_target
&& !file
->double_colon
)
299 fatal ("can't rename single-colon `%s' to double-colon `%s'",
301 if (!oldfile
->double_colon
&& file
->double_colon
)
303 if (oldfile
->is_target
)
304 fatal ("can't rename double-colon `%s' to single-colon `%s'",
307 oldfile
->double_colon
= file
->double_colon
;
310 if (file
->last_mtime
> oldfile
->last_mtime
)
311 /* %%% Kludge so -W wins on a file that gets vpathized. */
312 oldfile
->last_mtime
= file
->last_mtime
;
314 #define MERGE(field) oldfile->field |= file->field
316 MERGE (tried_implicit
);
324 file
->renamed
= oldfile
;
328 /* Remove all nonprecious intermediate files.
329 If SIG is nonzero, this was caused by a fatal signal,
330 meaning that a different message will be printed, and
331 the message will go to stderr rather than stdout. */
334 remove_intermediates (sig
)
338 register struct file
*f
;
341 if (question_flag
|| touch_flag
)
343 if (sig
&& just_print_flag
)
347 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
348 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
349 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
353 if (f
->update_status
== -1)
354 /* If nothing would have created this file yet,
355 don't print an "rm" command for it. */
357 else if (just_print_flag
)
361 status
= unlink (f
->name
);
362 if (status
< 0 && errno
== ENOENT
)
368 error ("*** Deleting intermediate file `%s'", f
->name
);
369 else if (!silent_flag
)
373 fputs ("rm ", stdout
);
378 fputs (f
->name
, stdout
);
382 perror_with_name ("unlink: ", f
->name
);
393 /* For each dependency of each file, make the `struct dep' point
394 at the appropriate `struct file' (which may have to be created).
396 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
397 and various other special targets. */
402 register struct file
*f
, *f2
;
403 register struct dep
*d
;
406 /* Enter each dependency name as a file. */
407 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
408 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
409 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
410 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
413 d
->file
= lookup_file (d
->name
);
415 d
->file
= enter_file (d
->name
);
421 for (f
= lookup_file (".PRECIOUS"); 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
)
426 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
427 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
428 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
430 /* Mark this file as phony and nonexistent. */
432 f2
->last_mtime
= (time_t) -1;
435 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
437 /* .INTERMEDIATE with deps listed
438 marks those deps as intermediate files. */
439 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
440 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
441 f2
->intermediate
= 1;
442 /* .INTERMEDIATE with no deps does nothing.
443 Marking all files as intermediates is useless
444 since the goal targets would be deleted after they are built. */
447 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
449 /* .SECONDARY with deps listed
450 marks those deps as intermediate files
451 in that they don't get rebuilt if not actually needed;
452 but unlike real intermediate files,
453 these are not deleted after make finishes. */
456 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
457 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
458 f2
->intermediate
= f2
->secondary
= 1;
460 /* .SECONDARY with no deps listed marks *all* files that way. */
464 for (i
= 0; i
< FILE_BUCKETS
; i
++)
465 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
466 f2
->intermediate
= f2
->secondary
= 1;
470 f
= lookup_file (".EXPORT_ALL_VARIABLES");
471 if (f
!= 0 && f
->is_target
)
472 export_all_variables
= 1;
474 f
= lookup_file (".IGNORE");
475 if (f
!= 0 && f
->is_target
)
478 ignore_errors_flag
= 1;
480 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
481 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
482 f2
->command_flags
|= COMMANDS_NOERROR
;
485 f
= lookup_file (".SILENT");
486 if (f
!= 0 && f
->is_target
)
491 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
492 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
493 f2
->command_flags
|= COMMANDS_SILENT
;
496 f
= lookup_file (".POSIX");
497 if (f
!= 0 && f
->is_target
)
501 /* Set the `command_state' member of FILE and all its `also_make's. */
504 set_command_state (file
, state
)
510 file
->command_state
= state
;
512 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
513 d
->file
->command_state
= state
;
516 /* Print the data base of files. */
522 register struct dep
*d
;
524 extern char *cvt_time
PARAMS ((unsigned long));
528 puts ("# Not a target:");
529 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
531 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
532 printf (" %s", dep_name (d
));
536 puts ("# Precious file (dependency of .PRECIOUS).");
538 puts ("# Phony target (dependency of .PHONY).");
540 puts ("# Command-line target.");
542 puts ("# A default or MAKEFILES makefile.");
543 printf ("# Implicit rule search has%s been done.\n",
544 f
->tried_implicit
? "" : " not");
546 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
548 puts ("# File is an intermediate dependency.");
549 if (f
->also_make
!= 0)
551 fputs ("# Also makes:", stdout
);
552 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
553 printf (" %s", dep_name (d
));
556 if (f
->last_mtime
== (time_t) 0)
557 puts ("# Modification time never checked.");
558 else if (f
->last_mtime
== (time_t) -1)
559 puts ("# File does not exist.");
562 printf ("# Last modified %.24s (%0lx)\n",
563 cvt_time(f
->last_mtime
), (unsigned long) f
->last_mtime
);
565 printf ("# Last modified %.24s (%ld)\n",
566 ctime (&f
->last_mtime
), (long int) f
->last_mtime
);
568 printf ("# File has%s been updated.\n",
569 f
->updated
? "" : " not");
570 switch (f
->command_state
)
573 puts ("# Commands currently running (THIS IS A BUG).");
575 case cs_deps_running
:
576 puts ("# Dependencies commands running (THIS IS A BUG).");
580 switch (f
->update_status
)
585 puts ("# Successfully updated.");
588 assert (question_flag
);
589 puts ("# Needs to be updated (-q is set).");
592 puts ("# Failed to be updated.");
595 puts ("# Invalid value in `update_status' member!");
602 puts ("# Invalid value in `command_state' member!");
608 if (f
->variables
!= 0)
609 print_file_variables (f
);
612 print_commands (f
->cmds
);
616 print_file_data_base ()
618 register unsigned int i
, nfiles
, per_bucket
;
619 register struct file
*file
;
623 per_bucket
= nfiles
= 0;
624 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
626 register unsigned int this_bucket
= 0;
628 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
630 register struct file
*f
;
634 for (f
= file
; f
!= 0; f
= f
->prev
)
638 nfiles
+= this_bucket
;
639 if (this_bucket
> per_bucket
)
640 per_bucket
= this_bucket
;
644 puts ("\n# No files.");
647 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
649 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
650 ((double) nfiles
) / ((double) FILE_BUCKETS
) * 100.0, per_bucket
);