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. */
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
= NONEXISTENT_MTIME
;
479 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
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 /* Convert an external file timestamp to internal form. */
570 file_timestamp_cons (fname
, s
, ns
)
575 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
576 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
577 FILE_TIMESTAMP ts
= product
+ offset
;
579 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
580 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
582 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
583 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
584 file_timestamp_sprintf (buf
, ts
);
585 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
586 fname
? fname
: _("Current time"), buf
);
592 /* Get and print file timestamps. */
595 file_timestamp_now ()
600 /* Don't bother with high-resolution clocks if file timestamps have
601 only one-second resolution. The code below should work, but it's
602 not worth the hassle of debugging it on hosts where it fails. */
603 if (FILE_TIMESTAMP_HI_RES
)
605 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
607 struct timespec timespec
;
608 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
611 ns
= timespec
.tv_nsec
;
616 #if HAVE_GETTIMEOFDAY
618 struct timeval timeval
;
619 if (gettimeofday (&timeval
, 0) == 0)
622 ns
= timeval
.tv_usec
* 1000;
629 s
= time ((time_t *) 0);
633 return file_timestamp_cons (0, s
, ns
);
637 file_timestamp_sprintf (p
, ts
)
641 time_t t
= FILE_TIMESTAMP_S (ts
);
642 struct tm
*tm
= localtime (&t
);
645 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
646 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
647 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
649 sprintf (p
, "%ld", (long) t
);
651 sprintf (p
, "%lu", (unsigned long) t
);
654 /* Append nanoseconds as a fraction, but remove trailing zeros.
655 We don't know the actual timestamp resolution, since clock_getres
656 applies only to local times, whereas this timestamp might come
657 from a remote filesystem. So removing trailing zeros is the
658 best guess that we can do. */
659 sprintf (p
, ".%09ld", (long) FILE_TIMESTAMP_NS (ts
));
668 /* Print the data base of files. */
674 register struct dep
*d
;
678 puts (_("# Not a target:"));
679 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
681 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
682 printf (" %s", dep_name (d
));
686 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
688 puts (_("# Phony target (prerequisite of .PHONY)."));
690 puts (_("# Command-line target."));
692 puts (_("# A default or MAKEFILES makefile."));
693 puts (f
->tried_implicit
694 ? _("# Implicit rule search has been done.")
695 : _("# Implicit rule search has not been done."));
697 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
699 puts (_("# File is an intermediate prerequisite."));
700 if (f
->also_make
!= 0)
702 fputs (_("# Also makes:"), stdout
);
703 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
704 printf (" %s", dep_name (d
));
707 if (f
->last_mtime
== UNKNOWN_MTIME
)
708 puts (_("# Modification time never checked."));
709 else if (f
->last_mtime
== NONEXISTENT_MTIME
)
710 puts (_("# File does not exist."));
711 else if (f
->last_mtime
== OLD_MTIME
)
712 puts (_("# File is very old."));
715 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
716 file_timestamp_sprintf (buf
, f
->last_mtime
);
717 printf (_("# Last modified %s\n"), buf
);
720 ? _("# File has been updated.") : _("# File has not been updated."));
721 switch (f
->command_state
)
724 puts (_("# Commands currently running (THIS IS A BUG)."));
726 case cs_deps_running
:
727 puts (_("# Dependencies commands running (THIS IS A BUG)."));
731 switch (f
->update_status
)
736 puts (_("# Successfully updated."));
739 assert (question_flag
);
740 puts (_("# Needs to be updated (-q is set)."));
743 puts (_("# Failed to be updated."));
746 puts (_("# Invalid value in `update_status' member!"));
753 puts (_("# Invalid value in `command_state' member!"));
759 if (f
->variables
!= 0)
760 print_file_variables (f
);
763 print_commands (f
->cmds
);
767 print_file_data_base ()
769 register unsigned int i
, nfiles
, per_bucket
;
770 register struct file
*file
;
772 puts (_("\n# Files"));
774 per_bucket
= nfiles
= 0;
775 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
777 register unsigned int this_bucket
= 0;
779 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
781 register struct file
*f
;
785 for (f
= file
; f
!= 0; f
= f
->prev
)
789 nfiles
+= this_bucket
;
790 if (this_bucket
> per_bucket
)
791 per_bucket
= this_bucket
;
795 puts (_("\n# No files."));
798 printf (_("\n# %u files in %u hash buckets.\n"), nfiles
, FILE_BUCKETS
);
800 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
801 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);