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. */
31 /* Hash table of files the makefile knows how to make. */
34 #define FILE_BUCKETS 1007
36 static struct file
*files
[FILE_BUCKETS
];
38 /* Whether or not .SECONDARY with no prerequisites was given. */
39 static int all_secondary
= 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
;
53 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
54 register char *lname
, *ln
;
57 assert (*name
!= '\0');
59 /* This is also done in parse_file_seq, so this is redundant
60 for names read from makefiles. It is here for names passed
61 on the command line. */
63 # ifndef WANT_CASE_SENSITIVE_TARGETS
64 lname
= (char *)malloc(strlen(name
) + 1);
65 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
66 *ln
= isupper((unsigned char)*n
) ? tolower((unsigned char)*n
) : *n
;
71 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
74 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
78 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
83 /* It was all slashes after a dot. */
95 for (n
= name
; *n
!= '\0'; ++n
)
97 hashval
%= FILE_BUCKETS
;
99 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
101 if (strieq (f
->hname
, name
))
103 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
109 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
119 register struct file
*f
, *new;
121 register unsigned int hashval
;
122 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
126 assert (*name
!= '\0');
128 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
129 lname
= (char *)malloc (strlen (name
) + 1);
130 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
132 if (isupper((unsigned char)*n
))
133 *ln
= tolower((unsigned char)*n
);
138 /* Creates a possible leak, old value of name is unreachable, but I
139 currently don't know how to fix it. */
144 for (n
= name
; *n
!= '\0'; ++n
)
146 hashval
%= FILE_BUCKETS
;
148 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
149 if (strieq (f
->hname
, name
))
152 if (f
!= 0 && !f
->double_colon
)
154 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
160 new = (struct file
*) xmalloc (sizeof (struct file
));
161 bzero ((char *) new, sizeof (struct file
));
162 new->name
= new->hname
= name
;
163 new->update_status
= -1;
167 /* This is a completely new file. */
168 new->next
= files
[hashval
];
169 files
[hashval
] = new;
173 /* There is already a double-colon entry for this file. */
174 new->double_colon
= f
;
183 /* Rehash FILE to NAME. This is not as simple as resetting
184 the `hname' member, since it must be put in a new hash bucket,
185 and possibly merged with an existing file called NAME. */
188 rehash_file (file
, name
)
189 register struct file
*file
;
192 char *oldname
= file
->hname
;
193 register unsigned int oldhash
;
196 while (file
->renamed
!= 0)
197 file
= file
->renamed
;
199 /* Find the hash values of the old and new names. */
202 for (n
= oldname
; *n
!= '\0'; ++n
)
205 file_hash_enter (file
, name
, oldhash
, file
->name
);
208 /* Rename FILE to NAME. This is not as simple as resetting
209 the `name' member, since it must be put in a new hash bucket,
210 and possibly merged with an existing file called NAME. */
213 rename_file (file
, name
)
214 register struct file
*file
;
217 rehash_file(file
, name
);
220 file
->name
= file
->hname
;
226 file_hash_enter (file
, name
, oldhash
, oldname
)
227 register struct file
*file
;
229 unsigned int oldhash
;
232 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
233 register unsigned int newhash
, newbucket
;
234 struct file
*oldfile
;
236 register struct file
*f
;
239 for (n
= name
; *n
!= '\0'; ++n
)
241 newbucket
= newhash
% FILE_BUCKETS
;
243 /* Look for an existing file under the new name. */
245 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
246 if (strieq (oldfile
->hname
, name
))
249 /* If the old file is the same as the new file, never mind. */
253 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
255 /* Remove FILE from its hash bucket. */
257 struct file
*lastf
= 0;
259 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
263 files
[oldbucket
] = f
->next
;
265 lastf
->next
= f
->next
;
268 /* Give FILE its new name. */
271 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
276 /* There is no existing file with the new name. */
278 if (newbucket
!= oldbucket
)
280 /* Put FILE in its new hash bucket. */
281 file
->next
= files
[newbucket
];
282 files
[newbucket
] = file
;
287 /* There is an existing file with the new name.
288 We must merge FILE into the existing file. */
290 register struct dep
*d
;
294 if (oldfile
->cmds
== 0)
295 oldfile
->cmds
= file
->cmds
;
296 else if (file
->cmds
!= oldfile
->cmds
)
298 /* We have two sets of commands. We will go with the
299 one given in the rule explicitly mentioning this name,
300 but give a message to let the user know what's going on. */
301 if (oldfile
->cmds
->fileinfo
.filenm
!= 0)
302 error (&file
->cmds
->fileinfo
,
303 _("Commands were specified for \
304 file `%s' at %s:%lu,"),
305 oldname
, oldfile
->cmds
->fileinfo
.filenm
,
306 oldfile
->cmds
->fileinfo
.lineno
);
308 error (&file
->cmds
->fileinfo
,
309 _("Commands for file `%s' were found by \
310 implicit rule search,"),
312 error (&file
->cmds
->fileinfo
,
313 _("but `%s' is now considered the same file \
316 error (&file
->cmds
->fileinfo
,
317 _("Commands for `%s' will be ignored \
318 in favor of those for `%s'."),
323 /* Merge the dependencies of the two files. */
327 oldfile
->deps
= file
->deps
;
332 d
->next
= file
->deps
;
335 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
337 if (oldfile
->double_colon
&& file
->is_target
&& !file
->double_colon
)
338 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
340 if (!oldfile
->double_colon
&& file
->double_colon
)
342 if (oldfile
->is_target
)
343 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
346 oldfile
->double_colon
= file
->double_colon
;
349 if (file
->last_mtime
> oldfile
->last_mtime
)
350 /* %%% Kludge so -W wins on a file that gets vpathized. */
351 oldfile
->last_mtime
= file
->last_mtime
;
353 oldfile
->mtime_before_update
= file
->mtime_before_update
;
355 #define MERGE(field) oldfile->field |= file->field
357 MERGE (tried_implicit
);
363 MERGE (ignore_vpath
);
366 file
->renamed
= oldfile
;
370 /* Remove all nonprecious intermediate files.
371 If SIG is nonzero, this was caused by a fatal signal,
372 meaning that a different message will be printed, and
373 the message will go to stderr rather than stdout. */
376 remove_intermediates (sig
)
380 register struct file
*f
;
383 /* If there's no way we will ever remove anything anyway, punt early. */
384 if (question_flag
|| touch_flag
|| all_secondary
)
387 if (sig
&& just_print_flag
)
391 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
392 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
393 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
394 && !f
->secondary
&& !f
->cmd_target
)
397 if (f
->update_status
== -1)
398 /* If nothing would have created this file yet,
399 don't print an "rm" command for it. */
405 status
= unlink (f
->name
);
406 if (status
< 0 && errno
== ENOENT
)
412 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
413 else if (!silent_flag
)
417 fputs ("rm ", stdout
);
422 fputs (f
->name
, stdout
);
426 perror_with_name ("unlink: ", f
->name
);
437 /* For each dependency of each file, make the `struct dep' point
438 at the appropriate `struct file' (which may have to be created).
440 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
441 and various other special targets. */
446 register struct file
*f
, *f2
;
447 register struct dep
*d
;
450 /* Enter each dependency name as a file. */
451 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
452 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
453 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
454 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
457 d
->file
= lookup_file (d
->name
);
459 d
->file
= enter_file (d
->name
);
465 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
466 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
467 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
470 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
471 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
472 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
474 /* Mark this file as phony and nonexistent. */
476 f2
->last_mtime
= NONEXISTENT_MTIME
;
477 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
480 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
482 /* .INTERMEDIATE with deps listed
483 marks those deps as intermediate files. */
484 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
485 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
486 f2
->intermediate
= 1;
487 /* .INTERMEDIATE with no deps does nothing.
488 Marking all files as intermediates is useless
489 since the goal targets would be deleted after they are built. */
492 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
494 /* .SECONDARY with deps listed
495 marks those deps as intermediate files
496 in that they don't get rebuilt if not actually needed;
497 but unlike real intermediate files,
498 these are not deleted after make finishes. */
500 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
501 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
502 f2
->intermediate
= f2
->secondary
= 1;
503 /* .SECONDARY with no deps listed marks *all* files that way. */
508 f
= lookup_file (".EXPORT_ALL_VARIABLES");
509 if (f
!= 0 && f
->is_target
)
510 export_all_variables
= 1;
512 f
= lookup_file (".IGNORE");
513 if (f
!= 0 && f
->is_target
)
516 ignore_errors_flag
= 1;
518 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
519 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
520 f2
->command_flags
|= COMMANDS_NOERROR
;
523 f
= lookup_file (".SILENT");
524 if (f
!= 0 && f
->is_target
)
529 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
530 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
531 f2
->command_flags
|= COMMANDS_SILENT
;
534 f
= lookup_file (".POSIX");
535 if (f
!= 0 && f
->is_target
)
538 f
= lookup_file (".NOTPARALLEL");
539 if (f
!= 0 && f
->is_target
)
543 /* Set the `command_state' member of FILE and all its `also_make's. */
546 set_command_state (file
, state
)
552 file
->command_state
= state
;
554 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
555 d
->file
->command_state
= state
;
558 /* Convert an external file timestamp to internal form. */
561 file_timestamp_cons (fname
, s
, ns
)
566 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
567 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
568 FILE_TIMESTAMP ts
= product
+ offset
;
570 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
571 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
573 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
574 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
575 file_timestamp_sprintf (buf
, ts
);
576 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
577 fname
? fname
: _("Current time"), buf
);
583 /* Get and print file timestamps. */
586 file_timestamp_now ()
591 /* Don't bother with high-resolution clocks if file timestamps have
592 only one-second resolution. The code below should work, but it's
593 not worth the hassle of debugging it on hosts where it fails. */
594 if (FILE_TIMESTAMP_HI_RES
)
596 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
598 struct timespec timespec
;
599 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
602 ns
= timespec
.tv_nsec
;
607 #if HAVE_GETTIMEOFDAY
609 struct timeval timeval
;
610 if (gettimeofday (&timeval
, 0) == 0)
613 ns
= timeval
.tv_usec
* 1000;
620 s
= time ((time_t *) 0);
624 return file_timestamp_cons (0, s
, ns
);
628 file_timestamp_sprintf (p
, ts
)
632 time_t t
= FILE_TIMESTAMP_S (ts
);
633 struct tm
*tm
= localtime (&t
);
636 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
637 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
638 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
640 sprintf (p
, "%ld", (long) t
);
642 sprintf (p
, "%lu", (unsigned long) t
);
645 /* Append nanoseconds as a fraction, but remove trailing zeros.
646 We don't know the actual timestamp resolution, since clock_getres
647 applies only to local times, whereas this timestamp might come
648 from a remote filesystem. So removing trailing zeros is the
649 best guess that we can do. */
650 sprintf (p
, ".%09ld", (long) FILE_TIMESTAMP_NS (ts
));
659 /* Print the data base of files. */
665 register struct dep
*d
;
669 puts (_("# Not a target:"));
670 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
672 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
673 printf (" %s", dep_name (d
));
677 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
679 puts (_("# Phony target (prerequisite of .PHONY)."));
681 puts (_("# Command-line target."));
683 puts (_("# A default or MAKEFILES makefile."));
684 puts (f
->tried_implicit
685 ? _("# Implicit rule search has been done.")
686 : _("# Implicit rule search has not been done."));
688 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
690 puts (_("# File is an intermediate prerequisite."));
691 if (f
->also_make
!= 0)
693 fputs (_("# Also makes:"), stdout
);
694 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
695 printf (" %s", dep_name (d
));
698 if (f
->last_mtime
== UNKNOWN_MTIME
)
699 puts (_("# Modification time never checked."));
700 else if (f
->last_mtime
== NONEXISTENT_MTIME
)
701 puts (_("# File does not exist."));
702 else if (f
->last_mtime
== OLD_MTIME
)
703 puts (_("# File is very old."));
706 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
707 file_timestamp_sprintf (buf
, f
->last_mtime
);
708 printf (_("# Last modified %s\n"), buf
);
711 ? _("# File has been updated.") : _("# File has not been updated."));
712 switch (f
->command_state
)
715 puts (_("# Commands currently running (THIS IS A BUG)."));
717 case cs_deps_running
:
718 puts (_("# Dependencies commands running (THIS IS A BUG)."));
722 switch (f
->update_status
)
727 puts (_("# Successfully updated."));
730 assert (question_flag
);
731 puts (_("# Needs to be updated (-q is set)."));
734 puts (_("# Failed to be updated."));
737 puts (_("# Invalid value in `update_status' member!"));
744 puts (_("# Invalid value in `command_state' member!"));
750 if (f
->variables
!= 0)
751 print_file_variables (f
);
754 print_commands (f
->cmds
);
758 print_file_data_base ()
760 register unsigned int i
, nfiles
, per_bucket
;
761 register struct file
*file
;
763 puts (_("\n# Files"));
765 per_bucket
= nfiles
= 0;
766 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
768 register unsigned int this_bucket
= 0;
770 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
772 register struct file
*f
;
776 for (f
= file
; f
!= 0; f
= f
->prev
)
780 nfiles
+= this_bucket
;
781 if (this_bucket
> per_bucket
)
782 per_bucket
= this_bucket
;
786 puts (_("\n# No files."));
789 printf (_("\n# %u files in %u hash buckets.\n"), nfiles
, FILE_BUCKETS
);
791 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
792 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);