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, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
30 /* Hash table of files the makefile knows how to make. */
33 #define FILE_BUCKETS 1007
35 static struct file
*files
[FILE_BUCKETS
];
37 /* Number of files with the `intermediate' flag set. */
39 unsigned int num_intermediates
= 0;
41 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
43 unsigned int considered
= 0;
45 /* Access the hash table of all file records.
46 lookup_file given a name, return the struct file * for that name,
47 or nil if there is none.
48 enter_file similar, but create one if there is none. */
54 register struct file
*f
;
56 register unsigned int hashval
;
57 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
58 register char *lname
, *ln
;
61 assert (*name
!= '\0');
63 /* This is also done in parse_file_seq, so this is redundant
64 for names read from makefiles. It is here for names passed
65 on the command line. */
67 # ifndef WANT_CASE_SENSITIVE_TARGETS
68 lname
= (char *)malloc(strlen(name
) + 1);
69 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
70 *ln
= isupper((unsigned char)*n
) ? tolower((unsigned char)*n
) : *n
;
75 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
78 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
82 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
87 /* It was all slashes after a dot. */
99 for (n
= name
; *n
!= '\0'; ++n
)
101 hashval
%= FILE_BUCKETS
;
103 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
105 if (strieq (f
->hname
, name
))
107 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
113 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
123 register struct file
*f
, *new;
125 register unsigned int hashval
;
126 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
130 assert (*name
!= '\0');
132 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
133 lname
= (char *)malloc (strlen (name
) + 1);
134 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
136 if (isupper((unsigned char)*n
))
137 *ln
= tolower((unsigned char)*n
);
142 /* Creates a possible leak, old value of name is unreachable, but I
143 currently don't know how to fix it. */
148 for (n
= name
; *n
!= '\0'; ++n
)
150 hashval
%= FILE_BUCKETS
;
152 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
153 if (strieq (f
->hname
, name
))
156 if (f
!= 0 && !f
->double_colon
)
158 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
164 new = (struct file
*) xmalloc (sizeof (struct file
));
165 bzero ((char *) new, sizeof (struct file
));
166 new->name
= new->hname
= name
;
167 new->update_status
= -1;
171 /* This is a completely new file. */
172 new->next
= files
[hashval
];
173 files
[hashval
] = new;
177 /* There is already a double-colon entry for this file. */
178 new->double_colon
= f
;
187 /* Rehash FILE to NAME. This is not as simple as resetting
188 the `hname' member, since it must be put in a new hash bucket,
189 and possibly merged with an existing file called NAME. */
192 rehash_file (file
, name
)
193 register struct file
*file
;
196 char *oldname
= file
->hname
;
197 register unsigned int oldhash
;
200 while (file
->renamed
!= 0)
201 file
= file
->renamed
;
203 /* Find the hash values of the old and new names. */
206 for (n
= oldname
; *n
!= '\0'; ++n
)
209 file_hash_enter (file
, name
, oldhash
, file
->name
);
212 /* Rename FILE to NAME. This is not as simple as resetting
213 the `name' member, since it must be put in a new hash bucket,
214 and possibly merged with an existing file called NAME. */
217 rename_file (file
, name
)
218 register struct file
*file
;
221 rehash_file(file
, name
);
224 file
->name
= file
->hname
;
230 file_hash_enter (file
, name
, oldhash
, oldname
)
231 register struct file
*file
;
233 unsigned int oldhash
;
236 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
237 register unsigned int newhash
, newbucket
;
238 struct file
*oldfile
;
240 register struct file
*f
;
243 for (n
= name
; *n
!= '\0'; ++n
)
245 newbucket
= newhash
% FILE_BUCKETS
;
247 /* Look for an existing file under the new name. */
249 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
250 if (strieq (oldfile
->hname
, name
))
253 /* If the old file is the same as the new file, never mind. */
257 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
259 /* Remove FILE from its hash bucket. */
261 struct file
*lastf
= 0;
263 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
267 files
[oldbucket
] = f
->next
;
269 lastf
->next
= f
->next
;
272 /* Give FILE its new name. */
275 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
280 /* There is no existing file with the new name. */
282 if (newbucket
!= oldbucket
)
284 /* Put FILE in its new hash bucket. */
285 file
->next
= files
[newbucket
];
286 files
[newbucket
] = file
;
291 /* There is an existing file with the new name.
292 We must merge FILE into the existing file. */
294 register struct dep
*d
;
298 if (oldfile
->cmds
== 0)
299 oldfile
->cmds
= file
->cmds
;
300 else if (file
->cmds
!= oldfile
->cmds
)
302 /* We have two sets of commands. We will go with the
303 one given in the rule explicitly mentioning this name,
304 but give a message to let the user know what's going on. */
305 if (oldfile
->cmds
->fileinfo
.filenm
!= 0)
306 error (&file
->cmds
->fileinfo
,
307 _("Commands were specified for \
308 file `%s' at %s:%lu,"),
309 oldname
, oldfile
->cmds
->fileinfo
.filenm
,
310 oldfile
->cmds
->fileinfo
.lineno
);
312 error (&file
->cmds
->fileinfo
,
313 _("Commands for file `%s' were found by \
314 implicit rule search,"),
316 error (&file
->cmds
->fileinfo
,
317 _("but `%s' is now considered the same file \
320 error (&file
->cmds
->fileinfo
,
321 _("Commands for `%s' will be ignored \
322 in favor of those for `%s'."),
327 /* Merge the dependencies of the two files. */
331 oldfile
->deps
= file
->deps
;
336 d
->next
= file
->deps
;
339 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
341 if (oldfile
->double_colon
&& file
->is_target
&& !file
->double_colon
)
342 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
344 if (!oldfile
->double_colon
&& file
->double_colon
)
346 if (oldfile
->is_target
)
347 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
350 oldfile
->double_colon
= file
->double_colon
;
353 if (file
->last_mtime
> oldfile
->last_mtime
)
354 /* %%% Kludge so -W wins on a file that gets vpathized. */
355 oldfile
->last_mtime
= file
->last_mtime
;
357 oldfile
->mtime_before_update
= file
->mtime_before_update
;
359 #define MERGE(field) oldfile->field |= file->field
361 MERGE (tried_implicit
);
367 MERGE (ignore_vpath
);
370 file
->renamed
= oldfile
;
374 /* Remove all nonprecious intermediate files.
375 If SIG is nonzero, this was caused by a fatal signal,
376 meaning that a different message will be printed, and
377 the message will go to stderr rather than stdout. */
380 remove_intermediates (sig
)
384 register struct file
*f
;
387 if (question_flag
|| touch_flag
)
389 if (sig
&& just_print_flag
)
393 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
394 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
395 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
396 && !f
->secondary
&& !f
->cmd_target
)
399 if (f
->update_status
== -1)
400 /* If nothing would have created this file yet,
401 don't print an "rm" command for it. */
403 else if (just_print_flag
)
407 status
= unlink (f
->name
);
408 if (status
< 0 && errno
== ENOENT
)
414 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
415 else if (!silent_flag
)
419 fputs ("rm ", stdout
);
424 fputs (f
->name
, stdout
);
428 perror_with_name ("unlink: ", f
->name
);
439 /* For each dependency of each file, make the `struct dep' point
440 at the appropriate `struct file' (which may have to be created).
442 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
443 and various other special targets. */
448 register struct file
*f
, *f2
;
449 register struct dep
*d
;
452 /* Enter each dependency name as a file. */
453 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
454 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
455 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
456 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
459 d
->file
= lookup_file (d
->name
);
461 d
->file
= enter_file (d
->name
);
467 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
468 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
469 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
472 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
473 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
474 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
476 /* Mark this file as phony and nonexistent. */
478 f2
->last_mtime
= (FILE_TIMESTAMP
) -1;
479 f2
->mtime_before_update
= (FILE_TIMESTAMP
) -1;
482 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
484 /* .INTERMEDIATE with deps listed
485 marks those deps as intermediate files. */
486 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
487 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
488 f2
->intermediate
= 1;
489 /* .INTERMEDIATE with no deps does nothing.
490 Marking all files as intermediates is useless
491 since the goal targets would be deleted after they are built. */
494 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
496 /* .SECONDARY with deps listed
497 marks those deps as intermediate files
498 in that they don't get rebuilt if not actually needed;
499 but unlike real intermediate files,
500 these are not deleted after make finishes. */
503 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
504 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
505 f2
->intermediate
= f2
->secondary
= 1;
507 /* .SECONDARY with no deps listed marks *all* files that way. */
511 for (i
= 0; i
< FILE_BUCKETS
; i
++)
512 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
513 f2
->intermediate
= f2
->secondary
= 1;
517 f
= lookup_file (".EXPORT_ALL_VARIABLES");
518 if (f
!= 0 && f
->is_target
)
519 export_all_variables
= 1;
521 f
= lookup_file (".IGNORE");
522 if (f
!= 0 && f
->is_target
)
525 ignore_errors_flag
= 1;
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_NOERROR
;
532 f
= lookup_file (".SILENT");
533 if (f
!= 0 && f
->is_target
)
538 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
539 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
540 f2
->command_flags
|= COMMANDS_SILENT
;
543 f
= lookup_file (".POSIX");
544 if (f
!= 0 && f
->is_target
)
547 f
= lookup_file (".NOTPARALLEL");
548 if (f
!= 0 && f
->is_target
)
552 /* Set the `command_state' member of FILE and all its `also_make's. */
555 set_command_state (file
, state
)
561 file
->command_state
= state
;
563 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
564 d
->file
->command_state
= state
;
567 /* Get and print file timestamps. */
570 file_timestamp_now ()
572 /* Don't bother with high-resolution clocks if file timestamps have
573 only one-second resolution. The code below should work, but it's
574 not worth the hassle of debugging it on hosts where it fails. */
575 if (1 < FILE_TIMESTAMPS_PER_S
)
577 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
579 struct timespec timespec
;
580 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
581 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec
.tv_sec
,
585 #if HAVE_GETTIMEOFDAY
587 struct timeval timeval
;
588 if (gettimeofday (&timeval
, 0) == 0)
589 return FILE_TIMESTAMP_FROM_S_AND_NS (timeval
.tv_sec
,
590 timeval
.tv_usec
* 1000);
595 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
599 file_timestamp_sprintf (p
, ts
)
603 time_t t
= FILE_TIMESTAMP_S (ts
);
604 struct tm
*tm
= localtime (&t
);
607 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
608 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
609 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
611 sprintf (p
, "%ld", (long) t
);
613 sprintf (p
, "%lu", (unsigned long) t
);
616 /* Append nanoseconds as a fraction, but remove trailing zeros.
617 We don't know the actual timestamp resolution, since clock_getres
618 applies only to local times, whereas this timestamp might come
619 from a remote filesystem. So removing trailing zeros is the
620 best guess that we can do. */
621 sprintf (p
, ".%09ld", (long) FILE_TIMESTAMP_NS (ts
));
630 /* Print the data base of files. */
636 register struct dep
*d
;
640 puts (_("# Not a target:"));
641 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
643 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
644 printf (" %s", dep_name (d
));
648 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
650 puts (_("# Phony target (prerequisite of .PHONY)."));
652 puts (_("# Command-line target."));
654 puts (_("# A default or MAKEFILES makefile."));
655 puts (f
->tried_implicit
656 ? _("# Implicit rule search has been done.")
657 : _("# Implicit rule search has not been done."));
659 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
661 puts (_("# File is an intermediate prerequisite."));
662 if (f
->also_make
!= 0)
664 fputs (_("# Also makes:"), stdout
);
665 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
666 printf (" %s", dep_name (d
));
669 if (f
->last_mtime
== 0)
670 puts (_("# Modification time never checked."));
671 else if (f
->last_mtime
== (FILE_TIMESTAMP
) -1)
672 puts (_("# File does not exist."));
675 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
676 file_timestamp_sprintf (buf
, f
->last_mtime
);
677 printf (_("# Last modified %s\n"), buf
);
680 ? _("# File has been updated.") : _("# File has not been updated."));
681 switch (f
->command_state
)
684 puts (_("# Commands currently running (THIS IS A BUG)."));
686 case cs_deps_running
:
687 puts (_("# Dependencies commands running (THIS IS A BUG)."));
691 switch (f
->update_status
)
696 puts (_("# Successfully updated."));
699 assert (question_flag
);
700 puts (_("# Needs to be updated (-q is set)."));
703 puts (_("# Failed to be updated."));
706 puts (_("# Invalid value in `update_status' member!"));
713 puts (_("# Invalid value in `command_state' member!"));
719 if (f
->variables
!= 0)
720 print_file_variables (f
);
723 print_commands (f
->cmds
);
727 print_file_data_base ()
729 register unsigned int i
, nfiles
, per_bucket
;
730 register struct file
*file
;
732 puts (_("\n# Files"));
734 per_bucket
= nfiles
= 0;
735 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
737 register unsigned int this_bucket
= 0;
739 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
741 register struct file
*f
;
745 for (f
= file
; f
!= 0; f
= f
->prev
)
749 nfiles
+= this_bucket
;
750 if (this_bucket
> per_bucket
)
751 per_bucket
= this_bucket
;
755 puts (_("\n# No files."));
758 printf (_("\n# %u files in %u hash buckets.\n"), nfiles
, FILE_BUCKETS
);
760 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
761 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);