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. */
29 /* Hash table of files the makefile knows how to make. */
32 #define FILE_BUCKETS 1007
34 static struct file
*files
[FILE_BUCKETS
];
36 /* Number of files with the `intermediate' flag set. */
38 unsigned int num_intermediates
= 0;
41 /* Access the hash table of all file records.
42 lookup_file given a name, return the struct file * for that name,
43 or nil if there is none.
44 enter_file similar, but create one if there is none. */
50 register struct file
*f
;
52 register unsigned int hashval
;
54 register char *lname
, *ln
;
60 /* This is also done in parse_file_seq, so this is redundant
61 for names read from makefiles. It is here for names passed
62 on the command line. */
64 lname
= (char *)malloc(strlen(name
) + 1);
65 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
66 *ln
= isupper(*n
) ? tolower(*n
) : *n
;
70 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
73 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
77 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
82 /* It was all slashes after a dot. */
94 for (n
= name
; *n
!= '\0'; ++n
)
96 hashval
%= FILE_BUCKETS
;
98 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
100 if (strieq (f
->hname
, name
))
118 register struct file
*f
, *new;
120 register unsigned int hashval
;
129 lname
= (char *)malloc (strlen (name
) + 1);
130 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
142 for (n
= name
; *n
!= '\0'; ++n
)
144 hashval
%= FILE_BUCKETS
;
146 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
147 if (strieq (f
->hname
, name
))
150 if (f
!= 0 && !f
->double_colon
)
158 new = (struct file
*) xmalloc (sizeof (struct file
));
159 bzero ((char *) new, sizeof (struct file
));
160 new->name
= new->hname
= name
;
161 new->update_status
= -1;
165 /* This is a completely new file. */
166 new->next
= files
[hashval
];
167 files
[hashval
] = new;
171 /* There is already a double-colon entry for this file. */
172 new->double_colon
= f
;
181 /* Rehash FILE to NAME. This is not as simple as resetting
182 the `hname' member, since it must be put in a new hash bucket,
183 and possibly merged with an existing file called NAME. */
186 rehash_file (file
, name
)
187 register struct file
*file
;
190 char *oldname
= file
->hname
;
191 register unsigned int oldhash
;
194 while (file
->renamed
!= 0)
195 file
= file
->renamed
;
197 /* Find the hash values of the old and new names. */
200 for (n
= oldname
; *n
!= '\0'; ++n
)
203 file_hash_enter (file
, name
, oldhash
, file
->name
);
206 /* Rename FILE to NAME. This is not as simple as resetting
207 the `name' member, since it must be put in a new hash bucket,
208 and possibly merged with an existing file called NAME. */
211 rename_file (file
, name
)
212 register struct file
*file
;
215 rehash_file(file
, name
);
218 file
->name
= file
->hname
;
224 file_hash_enter (file
, name
, oldhash
, oldname
)
225 register struct file
*file
;
227 unsigned int oldhash
;
230 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
231 register unsigned int newhash
, newbucket
;
232 struct file
*oldfile
;
234 register struct file
*f
;
237 for (n
= name
; *n
!= '\0'; ++n
)
239 newbucket
= newhash
% FILE_BUCKETS
;
241 /* Look for an existing file under the new name. */
243 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
244 if (strieq (oldfile
->hname
, name
))
247 /* If the old file is the same as the new file, something's wrong. */
248 assert (oldfile
!= file
);
250 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
252 /* Remove FILE from its hash bucket. */
254 struct file
*lastf
= 0;
256 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
260 files
[oldbucket
] = f
->next
;
262 lastf
->next
= f
->next
;
265 /* Give FILE its new name. */
268 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
273 /* There is no existing file with the new name. */
275 if (newbucket
!= oldbucket
)
277 /* Put FILE in its new hash bucket. */
278 file
->next
= files
[newbucket
];
279 files
[newbucket
] = file
;
284 /* There is an existing file with the new name.
285 We must merge FILE into the existing file. */
287 register struct dep
*d
;
291 if (oldfile
->cmds
== 0)
292 oldfile
->cmds
= file
->cmds
;
293 else if (file
->cmds
!= oldfile
->cmds
)
295 /* We have two sets of commands. We will go with the
296 one given in the rule explicitly mentioning this name,
297 but give a message to let the user know what's going on. */
298 if (oldfile
->cmds
->fileinfo
.filenm
!= 0)
299 error (&file
->cmds
->fileinfo
,
300 "Commands were specified for \
301 file `%s' at %s:%lu,",
302 oldname
, oldfile
->cmds
->fileinfo
.filenm
,
303 oldfile
->cmds
->fileinfo
.lineno
);
305 error (&file
->cmds
->fileinfo
,
306 "Commands for file `%s' were found by \
307 implicit rule search,",
309 error (&file
->cmds
->fileinfo
,
310 "but `%s' is now considered the same file \
313 error (&file
->cmds
->fileinfo
,
314 "Commands for `%s' will be ignored \
315 in favor of those for `%s'.",
320 /* Merge the dependencies of the two files. */
324 oldfile
->deps
= file
->deps
;
329 d
->next
= file
->deps
;
332 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
334 if (oldfile
->double_colon
&& file
->is_target
&& !file
->double_colon
)
335 fatal (NILF
, "can't rename single-colon `%s' to double-colon `%s'",
337 if (!oldfile
->double_colon
&& file
->double_colon
)
339 if (oldfile
->is_target
)
340 fatal (NILF
, "can't rename double-colon `%s' to single-colon `%s'",
343 oldfile
->double_colon
= file
->double_colon
;
346 if (file
->last_mtime
> oldfile
->last_mtime
)
347 /* %%% Kludge so -W wins on a file that gets vpathized. */
348 oldfile
->last_mtime
= file
->last_mtime
;
350 #define MERGE(field) oldfile->field |= file->field
352 MERGE (tried_implicit
);
358 MERGE (ignore_vpath
);
361 file
->renamed
= oldfile
;
365 /* Remove all nonprecious intermediate files.
366 If SIG is nonzero, this was caused by a fatal signal,
367 meaning that a different message will be printed, and
368 the message will go to stderr rather than stdout. */
371 remove_intermediates (sig
)
375 register struct file
*f
;
378 if (question_flag
|| touch_flag
)
380 if (sig
&& just_print_flag
)
384 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
385 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
386 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
390 if (f
->update_status
== -1)
391 /* If nothing would have created this file yet,
392 don't print an "rm" command for it. */
394 else if (just_print_flag
)
398 status
= unlink (f
->name
);
399 if (status
< 0 && errno
== ENOENT
)
405 error (NILF
, "*** Deleting intermediate file `%s'", f
->name
);
406 else if (!silent_flag
)
410 fputs ("rm ", stdout
);
415 fputs (f
->name
, stdout
);
419 perror_with_name ("unlink: ", f
->name
);
430 /* For each dependency of each file, make the `struct dep' point
431 at the appropriate `struct file' (which may have to be created).
433 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
434 and various other special targets. */
439 register struct file
*f
, *f2
;
440 register struct dep
*d
;
443 /* Enter each dependency name as a file. */
444 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
445 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
446 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
447 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
450 d
->file
= lookup_file (d
->name
);
452 d
->file
= enter_file (d
->name
);
458 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
459 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
460 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
463 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
464 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
465 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
467 /* Mark this file as phony and nonexistent. */
469 f2
->last_mtime
= (FILE_TIMESTAMP
) -1;
472 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
474 /* .INTERMEDIATE with deps listed
475 marks those deps as intermediate files. */
476 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
477 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
478 f2
->intermediate
= 1;
479 /* .INTERMEDIATE with no deps does nothing.
480 Marking all files as intermediates is useless
481 since the goal targets would be deleted after they are built. */
484 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
486 /* .SECONDARY with deps listed
487 marks those deps as intermediate files
488 in that they don't get rebuilt if not actually needed;
489 but unlike real intermediate files,
490 these are not deleted after make finishes. */
493 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
494 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
495 f2
->intermediate
= f2
->secondary
= 1;
497 /* .SECONDARY with no deps listed marks *all* files that way. */
501 for (i
= 0; i
< FILE_BUCKETS
; i
++)
502 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
503 f2
->intermediate
= f2
->secondary
= 1;
507 f
= lookup_file (".EXPORT_ALL_VARIABLES");
508 if (f
!= 0 && f
->is_target
)
509 export_all_variables
= 1;
511 f
= lookup_file (".IGNORE");
512 if (f
!= 0 && f
->is_target
)
515 ignore_errors_flag
= 1;
517 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
518 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
519 f2
->command_flags
|= COMMANDS_NOERROR
;
522 f
= lookup_file (".SILENT");
523 if (f
!= 0 && f
->is_target
)
528 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
529 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
530 f2
->command_flags
|= COMMANDS_SILENT
;
533 f
= lookup_file (".POSIX");
534 if (f
!= 0 && f
->is_target
)
538 /* Set the `command_state' member of FILE and all its `also_make's. */
541 set_command_state (file
, state
)
547 file
->command_state
= state
;
549 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
550 d
->file
->command_state
= state
;
553 /* Get and print file timestamps. */
556 file_timestamp_now ()
558 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
559 struct timespec timespec
;
560 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
561 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec
.tv_sec
, timespec
.tv_nsec
);
563 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
567 file_timestamp_sprintf (p
, ts
)
571 time_t t
= FILE_TIMESTAMP_S (ts
);
572 struct tm
*tm
= localtime (&t
);
575 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
576 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
577 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
579 sprintf (p
, "%ld", (long) t
);
581 sprintf (p
, "%lu", (unsigned long) t
);
584 /* Append nanoseconds as a fraction, but remove trailing zeros.
585 We don't know the actual timestamp resolution, since clock_getres
586 applies only to local times, whereas this timestamp might come
587 from a remote filesystem. So removing trailing zeros is the
588 best guess that we can do. */
589 sprintf (p
, ".%09ld", (long) FILE_TIMESTAMP_NS (ts
));
598 /* Print the data base of files. */
604 register struct dep
*d
;
608 puts ("# Not a target:");
609 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
611 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
612 printf (" %s", dep_name (d
));
616 puts ("# Precious file (dependency of .PRECIOUS).");
618 puts ("# Phony target (dependency of .PHONY).");
620 puts ("# Command-line target.");
622 puts ("# A default or MAKEFILES makefile.");
623 printf ("# Implicit rule search has%s been done.\n",
624 f
->tried_implicit
? "" : " not");
626 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
628 puts ("# File is an intermediate dependency.");
629 if (f
->also_make
!= 0)
631 fputs ("# Also makes:", stdout
);
632 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
633 printf (" %s", dep_name (d
));
636 if (f
->last_mtime
== 0)
637 puts ("# Modification time never checked.");
638 else if (f
->last_mtime
== (FILE_TIMESTAMP
) -1)
639 puts ("# File does not exist.");
642 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
643 file_timestamp_sprintf (buf
, f
->last_mtime
);
644 printf ("# Last modified %s\n", buf
);
646 printf ("# File has%s been updated.\n",
647 f
->updated
? "" : " not");
648 switch (f
->command_state
)
651 puts ("# Commands currently running (THIS IS A BUG).");
653 case cs_deps_running
:
654 puts ("# Dependencies commands running (THIS IS A BUG).");
658 switch (f
->update_status
)
663 puts ("# Successfully updated.");
666 assert (question_flag
);
667 puts ("# Needs to be updated (-q is set).");
670 puts ("# Failed to be updated.");
673 puts ("# Invalid value in `update_status' member!");
680 puts ("# Invalid value in `command_state' member!");
686 if (f
->variables
!= 0)
687 print_file_variables (f
);
690 print_commands (f
->cmds
);
694 print_file_data_base ()
696 register unsigned int i
, nfiles
, per_bucket
;
697 register struct file
*file
;
701 per_bucket
= nfiles
= 0;
702 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
704 register unsigned int this_bucket
= 0;
706 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
708 register struct file
*f
;
712 for (f
= file
; f
!= 0; f
= f
->prev
)
716 nfiles
+= this_bucket
;
717 if (this_bucket
> per_bucket
)
718 per_bucket
= this_bucket
;
722 puts ("\n# No files.");
725 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
727 printf ("# average %.3f files per bucket, max %u files in one bucket.\n",
728 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);