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. */
32 /* Hash table of files the makefile knows how to make. */
35 #define FILE_BUCKETS 1007
37 static struct file
*files
[FILE_BUCKETS
];
39 /* Whether or not .SECONDARY with no prerequisites was given. */
40 static int all_secondary
= 0;
42 /* Access the hash table of all file records.
43 lookup_file given a name, return the struct file * for that name,
44 or nil if there is none.
45 enter_file similar, but create one if there is none. */
51 register struct file
*f
;
53 register unsigned int hashval
;
54 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
55 register char *lname
, *ln
;
58 assert (*name
!= '\0');
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 # ifndef WANT_CASE_SENSITIVE_TARGETS
65 lname
= (char *)malloc(strlen(name
) + 1);
66 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
67 *ln
= isupper((unsigned char)*n
) ? tolower((unsigned char)*n
) : *n
;
72 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
75 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
79 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
84 /* It was all slashes after a dot. */
96 for (n
= name
; *n
!= '\0'; ++n
)
98 hashval
%= FILE_BUCKETS
;
100 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
102 if (strieq (f
->hname
, name
))
104 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
110 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
120 register struct file
*f
, *new;
122 register unsigned int hashval
;
123 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
127 assert (*name
!= '\0');
129 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
130 lname
= (char *)malloc (strlen (name
) + 1);
131 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
133 if (isupper((unsigned char)*n
))
134 *ln
= tolower((unsigned char)*n
);
139 /* Creates a possible leak, old value of name is unreachable, but I
140 currently don't know how to fix it. */
145 for (n
= name
; *n
!= '\0'; ++n
)
147 hashval
%= FILE_BUCKETS
;
149 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
150 if (strieq (f
->hname
, name
))
153 if (f
!= 0 && !f
->double_colon
)
155 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
161 new = (struct file
*) xmalloc (sizeof (struct file
));
162 bzero ((char *) new, sizeof (struct file
));
163 new->name
= new->hname
= name
;
164 new->update_status
= -1;
168 /* This is a completely new file. */
169 new->next
= files
[hashval
];
170 files
[hashval
] = new;
174 /* There is already a double-colon entry for this file. */
175 new->double_colon
= f
;
184 /* Rehash FILE to NAME. This is not as simple as resetting
185 the `hname' member, since it must be put in a new hash bucket,
186 and possibly merged with an existing file called NAME. */
189 rehash_file (file
, name
)
190 register struct file
*file
;
193 char *oldname
= file
->hname
;
194 register unsigned int oldhash
;
197 while (file
->renamed
!= 0)
198 file
= file
->renamed
;
200 /* Find the hash values of the old and new names. */
203 for (n
= oldname
; *n
!= '\0'; ++n
)
206 file_hash_enter (file
, name
, oldhash
, file
->name
);
209 /* Rename FILE to NAME. This is not as simple as resetting
210 the `name' member, since it must be put in a new hash bucket,
211 and possibly merged with an existing file called NAME. */
214 rename_file (file
, name
)
215 register struct file
*file
;
218 rehash_file(file
, name
);
221 file
->name
= file
->hname
;
227 file_hash_enter (file
, name
, oldhash
, oldname
)
228 register struct file
*file
;
230 unsigned int oldhash
;
233 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
234 register unsigned int newhash
, newbucket
;
235 struct file
*oldfile
;
237 register struct file
*f
;
240 for (n
= name
; *n
!= '\0'; ++n
)
242 newbucket
= newhash
% FILE_BUCKETS
;
244 /* Look for an existing file under the new name. */
246 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
247 if (strieq (oldfile
->hname
, name
))
250 /* If the old file is the same as the new file, never mind. */
254 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
256 /* Remove FILE from its hash bucket. */
258 struct file
*lastf
= 0;
260 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
264 files
[oldbucket
] = f
->next
;
266 lastf
->next
= f
->next
;
269 /* Give FILE its new name. */
272 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
277 /* There is no existing file with the new name. */
279 if (newbucket
!= oldbucket
)
281 /* Put FILE in its new hash bucket. */
282 file
->next
= files
[newbucket
];
283 files
[newbucket
] = file
;
288 /* There is an existing file with the new name.
289 We must merge FILE into the existing file. */
291 register struct dep
*d
;
295 if (oldfile
->cmds
== 0)
296 oldfile
->cmds
= file
->cmds
;
297 else if (file
->cmds
!= oldfile
->cmds
)
299 /* We have two sets of commands. We will go with the
300 one given in the rule explicitly mentioning this name,
301 but give a message to let the user know what's going on. */
302 if (oldfile
->cmds
->fileinfo
.filenm
!= 0)
303 error (&file
->cmds
->fileinfo
,
304 _("Commands were specified for \
305 file `%s' at %s:%lu,"),
306 oldname
, oldfile
->cmds
->fileinfo
.filenm
,
307 oldfile
->cmds
->fileinfo
.lineno
);
309 error (&file
->cmds
->fileinfo
,
310 _("Commands for file `%s' were found by \
311 implicit rule search,"),
313 error (&file
->cmds
->fileinfo
,
314 _("but `%s' is now considered the same file \
317 error (&file
->cmds
->fileinfo
,
318 _("Commands for `%s' will be ignored \
319 in favor of those for `%s'."),
324 /* Merge the dependencies of the two files. */
328 oldfile
->deps
= file
->deps
;
333 d
->next
= file
->deps
;
336 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
338 if (oldfile
->double_colon
&& file
->is_target
&& !file
->double_colon
)
339 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
341 if (!oldfile
->double_colon
&& file
->double_colon
)
343 if (oldfile
->is_target
)
344 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
347 oldfile
->double_colon
= file
->double_colon
;
350 if (file
->last_mtime
> oldfile
->last_mtime
)
351 /* %%% Kludge so -W wins on a file that gets vpathized. */
352 oldfile
->last_mtime
= file
->last_mtime
;
354 oldfile
->mtime_before_update
= file
->mtime_before_update
;
356 #define MERGE(field) oldfile->field |= file->field
358 MERGE (tried_implicit
);
364 MERGE (ignore_vpath
);
367 file
->renamed
= oldfile
;
371 /* Remove all nonprecious intermediate files.
372 If SIG is nonzero, this was caused by a fatal signal,
373 meaning that a different message will be printed, and
374 the message will go to stderr rather than stdout. */
377 remove_intermediates (sig
)
381 register struct file
*f
;
384 /* If there's no way we will ever remove anything anyway, punt early. */
385 if (question_flag
|| touch_flag
|| all_secondary
)
388 if (sig
&& just_print_flag
)
392 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
393 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
394 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
395 && !f
->secondary
&& !f
->cmd_target
)
398 if (f
->update_status
== -1)
399 /* If nothing would have created this file yet,
400 don't print an "rm" command for it. */
406 status
= unlink (f
->name
);
407 if (status
< 0 && errno
== ENOENT
)
413 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
417 DB (DB_BASIC
, (_("Removing intermediate files...\n")));
422 fputs ("rm ", stdout
);
427 fputs (f
->name
, stdout
);
432 perror_with_name ("unlink: ", f
->name
);
443 /* For each dependency of each file, make the `struct dep' point
444 at the appropriate `struct file' (which may have to be created).
446 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
447 and various other special targets. */
452 register struct file
*f
, *f2
;
453 register struct dep
*d
;
456 /* Enter each dependency name as a file. */
457 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
458 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
459 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
460 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
463 d
->file
= lookup_file (d
->name
);
465 d
->file
= enter_file (d
->name
);
471 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
472 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
473 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
476 for (f
= lookup_file (".LOW_RESOLUTION_TIME"); f
!= 0; f
= f
->prev
)
477 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
478 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
479 f2
->low_resolution_time
= 1;
481 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
482 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
483 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
485 /* Mark this file as phony and nonexistent. */
487 f2
->last_mtime
= NONEXISTENT_MTIME
;
488 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
491 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
493 /* .INTERMEDIATE with deps listed
494 marks those deps as intermediate files. */
495 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
496 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
497 f2
->intermediate
= 1;
498 /* .INTERMEDIATE with no deps does nothing.
499 Marking all files as intermediates is useless
500 since the goal targets would be deleted after they are built. */
503 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
505 /* .SECONDARY with deps listed
506 marks those deps as intermediate files
507 in that they don't get rebuilt if not actually needed;
508 but unlike real intermediate files,
509 these are not deleted after make finishes. */
511 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
512 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
513 f2
->intermediate
= f2
->secondary
= 1;
514 /* .SECONDARY with no deps listed marks *all* files that way. */
519 f
= lookup_file (".EXPORT_ALL_VARIABLES");
520 if (f
!= 0 && f
->is_target
)
521 export_all_variables
= 1;
523 f
= lookup_file (".IGNORE");
524 if (f
!= 0 && f
->is_target
)
527 ignore_errors_flag
= 1;
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_NOERROR
;
534 f
= lookup_file (".SILENT");
535 if (f
!= 0 && f
->is_target
)
540 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
541 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
542 f2
->command_flags
|= COMMANDS_SILENT
;
545 f
= lookup_file (".POSIX");
546 if (f
!= 0 && f
->is_target
)
549 f
= lookup_file (".NOTPARALLEL");
550 if (f
!= 0 && f
->is_target
)
554 /* Set the `command_state' member of FILE and all its `also_make's. */
557 set_command_state (file
, state
)
563 file
->command_state
= state
;
565 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
566 d
->file
->command_state
= state
;
569 /* Convert an external file timestamp to internal form. */
572 file_timestamp_cons (fname
, s
, ns
)
577 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
578 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
579 FILE_TIMESTAMP ts
= product
+ offset
;
581 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
582 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
584 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
585 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
586 file_timestamp_sprintf (buf
, ts
);
587 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
588 fname
? fname
: _("Current time"), buf
);
594 /* Return the current time as a file timestamp, setting *RESOLUTION to
597 file_timestamp_now (int *resolution
)
603 /* Don't bother with high-resolution clocks if file timestamps have
604 only one-second resolution. The code below should work, but it's
605 not worth the hassle of debugging it on hosts where it fails. */
606 #if FILE_TIMESTAMP_HI_RES
607 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
609 struct timespec timespec
;
610 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
614 ns
= timespec
.tv_nsec
;
619 # if HAVE_GETTIMEOFDAY
621 struct timeval timeval
;
622 if (gettimeofday (&timeval
, 0) == 0)
626 ns
= timeval
.tv_usec
* 1000;
634 s
= time ((time_t *) 0);
639 return file_timestamp_cons (0, s
, ns
);
642 /* Place into the buffer P a printable representation of the file
645 file_timestamp_sprintf (p
, ts
)
649 time_t t
= FILE_TIMESTAMP_S (ts
);
650 struct tm
*tm
= localtime (&t
);
653 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
654 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
655 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
657 sprintf (p
, "%ld", (long) t
);
659 sprintf (p
, "%lu", (unsigned long) t
);
662 /* Append nanoseconds as a fraction, but remove trailing zeros.
663 We don't know the actual timestamp resolution, since clock_getres
664 applies only to local times, whereas this timestamp might come
665 from a remote filesystem. So removing trailing zeros is the
666 best guess that we can do. */
667 sprintf (p
, ".%09d", FILE_TIMESTAMP_NS (ts
));
676 /* Print the data base of files. */
682 register struct dep
*d
;
686 puts (_("# Not a target:"));
687 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
689 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
690 printf (" %s", dep_name (d
));
694 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
696 puts (_("# Phony target (prerequisite of .PHONY)."));
698 puts (_("# Command-line target."));
700 puts (_("# A default or MAKEFILES makefile."));
701 puts (f
->tried_implicit
702 ? _("# Implicit rule search has been done.")
703 : _("# Implicit rule search has not been done."));
705 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
707 puts (_("# File is an intermediate prerequisite."));
708 if (f
->also_make
!= 0)
710 fputs (_("# Also makes:"), stdout
);
711 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
712 printf (" %s", dep_name (d
));
715 if (f
->last_mtime
== UNKNOWN_MTIME
)
716 puts (_("# Modification time never checked."));
717 else if (f
->last_mtime
== NONEXISTENT_MTIME
)
718 puts (_("# File does not exist."));
719 else if (f
->last_mtime
== OLD_MTIME
)
720 puts (_("# File is very old."));
723 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
724 file_timestamp_sprintf (buf
, f
->last_mtime
);
725 printf (_("# Last modified %s\n"), buf
);
728 ? _("# File has been updated.") : _("# File has not been updated."));
729 switch (f
->command_state
)
732 puts (_("# Commands currently running (THIS IS A BUG)."));
734 case cs_deps_running
:
735 puts (_("# Dependencies commands running (THIS IS A BUG)."));
739 switch (f
->update_status
)
744 puts (_("# Successfully updated."));
747 assert (question_flag
);
748 puts (_("# Needs to be updated (-q is set)."));
751 puts (_("# Failed to be updated."));
754 puts (_("# Invalid value in `update_status' member!"));
761 puts (_("# Invalid value in `command_state' member!"));
767 if (f
->variables
!= 0)
768 print_file_variables (f
);
771 print_commands (f
->cmds
);
775 print_file_data_base ()
777 register unsigned int i
, nfiles
, per_bucket
;
778 register struct file
*file
;
780 puts (_("\n# Files"));
782 per_bucket
= nfiles
= 0;
783 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
785 register unsigned int this_bucket
= 0;
787 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
789 register struct file
*f
;
793 for (f
= file
; f
!= 0; f
= f
->prev
)
797 nfiles
+= this_bucket
;
798 if (this_bucket
> per_bucket
)
799 per_bucket
= this_bucket
;
803 puts (_("\n# No files."));
806 printf (_("\n# %u files in %u hash buckets.\n"), nfiles
, FILE_BUCKETS
);
808 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
809 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);