1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
34 /* Hash table of files the makefile knows how to make. */
37 file_hash_1 (void const *key
)
39 return_ISTRING_HASH_1 (((struct file
const *) key
)->hname
);
43 file_hash_2 (void const *key
)
45 return_ISTRING_HASH_2 (((struct file
const *) key
)->hname
);
49 file_hash_cmp (void const *x
, void const *y
)
51 return_ISTRING_COMPARE (((struct file
const *) x
)->hname
,
52 ((struct file
const *) y
)->hname
);
56 #define FILE_BUCKETS 1007
58 static struct hash_table files
;
60 /* Whether or not .SECONDARY with no prerequisites was given. */
61 static int all_secondary
= 0;
63 /* Access the hash table of all file records.
64 lookup_file given a name, return the struct file * for that name,
65 or nil if there is none.
66 enter_file similar, but create one if there is none. */
72 register struct file
*f
;
74 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
75 register char *lname
, *ln
;
78 assert (*name
!= '\0');
80 /* This is also done in parse_file_seq, so this is redundant
81 for names read from makefiles. It is here for names passed
82 on the command line. */
84 # ifndef WANT_CASE_SENSITIVE_TARGETS
87 lname
= (char *) malloc (strlen (name
) + 1);
88 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
89 *ln
= isupper ((unsigned char)*n
) ? tolower ((unsigned char)*n
) : *n
;
95 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
98 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
102 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
107 /* It was all slashes after a dot. */
118 file_key
.hname
= name
;
119 f
= (struct file
*) hash_find_item (&files
, &file_key
);
120 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
130 register struct file
*f
;
131 register struct file
*new;
132 register struct file
**file_slot
;
133 struct file file_key
;
134 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
138 assert (*name
!= '\0');
140 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
143 lname
= (char *) malloc (strlen (name
) + 1);
144 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
146 if (isupper ((unsigned char)*n
))
147 *ln
= tolower ((unsigned char)*n
);
153 /* Creates a possible leak, old value of name is unreachable, but I
154 currently don't know how to fix it. */
159 file_key
.hname
= name
;
160 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
162 if (! HASH_VACANT (f
) && !f
->double_colon
)
164 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
170 new = (struct file
*) xmalloc (sizeof (struct file
));
171 bzero ((char *) new, sizeof (struct file
));
172 new->name
= new->hname
= name
;
173 new->update_status
= -1;
176 hash_insert_at (&files
, new, file_slot
);
179 /* There is already a double-colon entry for this file. */
180 new->double_colon
= f
;
189 /* Rename FILE to NAME. This is not as simple as resetting
190 the `name' member, since it must be put in a new hash bucket,
191 and possibly merged with an existing file called NAME. */
194 rename_file (from_file
, to_hname
)
195 register struct file
*from_file
;
198 rehash_file (from_file
, to_hname
);
201 from_file
->name
= from_file
->hname
;
202 from_file
= from_file
->prev
;
206 /* Rehash FILE to NAME. This is not as simple as resetting
207 the `hname' member, since it must be put in a new hash bucket,
208 and possibly merged with an existing file called NAME. */
211 rehash_file (from_file
, to_hname
)
212 register struct file
*from_file
;
215 struct file file_key
;
216 struct file
**file_slot
;
217 struct file
*to_file
;
218 struct file
*deleted_file
;
221 file_key
.hname
= to_hname
;
222 if (0 == file_hash_cmp (from_file
, &file_key
))
225 file_key
.hname
= from_file
->hname
;
226 while (from_file
->renamed
!= 0)
227 from_file
= from_file
->renamed
;
228 if (file_hash_cmp (from_file
, &file_key
))
229 /* hname changed unexpectedly */
232 deleted_file
= hash_delete (&files
, from_file
);
233 if (deleted_file
!= from_file
)
234 /* from_file isn't the one stored in files */
237 file_key
.hname
= to_hname
;
238 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
239 to_file
= *file_slot
;
241 from_file
->hname
= to_hname
;
242 for (f
= from_file
->double_colon
; f
!= 0; f
= f
->prev
)
245 if (HASH_VACANT (to_file
))
246 hash_insert_at (&files
, from_file
, file_slot
);
249 /* TO_FILE already exists under TO_HNAME.
250 We must retain TO_FILE and merge FROM_FILE into it. */
252 if (from_file
->cmds
!= 0)
254 if (to_file
->cmds
== 0)
255 to_file
->cmds
= from_file
->cmds
;
256 else if (from_file
->cmds
!= to_file
->cmds
)
258 /* We have two sets of commands. We will go with the
259 one given in the rule explicitly mentioning this name,
260 but give a message to let the user know what's going on. */
261 if (to_file
->cmds
->fileinfo
.filenm
!= 0)
262 error (&from_file
->cmds
->fileinfo
,
263 _("Commands were specified for file `%s' at %s:%lu,"),
264 from_file
->name
, to_file
->cmds
->fileinfo
.filenm
,
265 to_file
->cmds
->fileinfo
.lineno
);
267 error (&from_file
->cmds
->fileinfo
,
268 _("Commands for file `%s' were found by implicit rule search,"),
270 error (&from_file
->cmds
->fileinfo
,
271 _("but `%s' is now considered the same file as `%s'."),
272 from_file
->name
, to_hname
);
273 error (&from_file
->cmds
->fileinfo
,
274 _("Commands for `%s' will be ignored in favor of those for `%s'."),
275 to_hname
, from_file
->name
);
279 /* Merge the dependencies of the two files. */
281 if (to_file
->deps
== 0)
282 to_file
->deps
= from_file
->deps
;
285 register struct dep
*deps
= to_file
->deps
;
286 while (deps
->next
!= 0)
288 deps
->next
= from_file
->deps
;
291 merge_variable_set_lists (&to_file
->variables
, from_file
->variables
);
293 if (to_file
->double_colon
&& from_file
->is_target
&& !from_file
->double_colon
)
294 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
295 from_file
->name
, to_hname
);
296 if (!to_file
->double_colon
&& from_file
->double_colon
)
298 if (to_file
->is_target
)
299 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
300 from_file
->name
, to_hname
);
302 to_file
->double_colon
= from_file
->double_colon
;
305 if (from_file
->last_mtime
> to_file
->last_mtime
)
306 /* %%% Kludge so -W wins on a file that gets vpathized. */
307 to_file
->last_mtime
= from_file
->last_mtime
;
309 to_file
->mtime_before_update
= from_file
->mtime_before_update
;
311 #define MERGE(field) to_file->field |= from_file->field
313 MERGE (tried_implicit
);
319 MERGE (ignore_vpath
);
322 from_file
->renamed
= to_file
;
326 /* Remove all nonprecious intermediate files.
327 If SIG is nonzero, this was caused by a fatal signal,
328 meaning that a different message will be printed, and
329 the message will go to stderr rather than stdout. */
332 remove_intermediates (sig
)
335 register struct file
**file_slot
;
336 register struct file
**file_end
;
339 /* If there's no way we will ever remove anything anyway, punt early. */
340 if (question_flag
|| touch_flag
|| all_secondary
)
343 if (sig
&& just_print_flag
)
346 file_slot
= (struct file
**) files
.ht_vec
;
347 file_end
= file_slot
+ files
.ht_size
;
348 for ( ; file_slot
< file_end
; file_slot
++)
349 if (! HASH_VACANT (*file_slot
))
351 register struct file
*f
= *file_slot
;
352 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
353 && !f
->secondary
&& !f
->cmd_target
)
356 if (f
->update_status
== -1)
357 /* If nothing would have created this file yet,
358 don't print an "rm" command for it. */
364 status
= unlink (f
->name
);
365 if (status
< 0 && errno
== ENOENT
)
371 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
375 DB (DB_BASIC
, (_("Removing intermediate files...\n")));
380 fputs ("rm ", stdout
);
385 fputs (f
->name
, stdout
);
390 perror_with_name ("unlink: ", f
->name
);
402 /* For each dependency of each file, make the `struct dep' point
403 at the appropriate `struct file' (which may have to be created).
405 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
406 and various other special targets. */
411 register struct file
*f
;
412 register struct file
*f2
;
413 register struct dep
*d
;
414 register struct file
**file_slot_0
;
415 register struct file
**file_slot
;
416 register struct file
**file_end
;
418 /* Enter each dependency name as a file. */
419 /* We must use hash_dump (), because within this loop
420 we might add new files to the table, possibly causing
421 an in-situ table expansion. */
422 file_slot_0
= (struct file
**) hash_dump (&files
, 0, 0);
423 file_end
= file_slot_0
+ files
.ht_fill
;
424 for (file_slot
= file_slot_0
; file_slot
< file_end
; file_slot
++)
425 for (f2
= *file_slot
; f2
!= 0; f2
= f2
->prev
)
426 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
429 d
->file
= lookup_file (d
->name
);
431 d
->file
= enter_file (d
->name
);
438 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
439 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
440 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
443 for (f
= lookup_file (".LOW_RESOLUTION_TIME"); f
!= 0; f
= f
->prev
)
444 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
445 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
446 f2
->low_resolution_time
= 1;
448 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
449 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
450 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
452 /* Mark this file as phony and nonexistent. */
454 f2
->last_mtime
= NONEXISTENT_MTIME
;
455 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
458 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
460 /* .INTERMEDIATE with deps listed
461 marks those deps as intermediate files. */
462 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
463 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
464 f2
->intermediate
= 1;
465 /* .INTERMEDIATE with no deps does nothing.
466 Marking all files as intermediates is useless
467 since the goal targets would be deleted after they are built. */
470 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
472 /* .SECONDARY with deps listed
473 marks those deps as intermediate files
474 in that they don't get rebuilt if not actually needed;
475 but unlike real intermediate files,
476 these are not deleted after make finishes. */
478 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
479 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
480 f2
->intermediate
= f2
->secondary
= 1;
481 /* .SECONDARY with no deps listed marks *all* files that way. */
486 f
= lookup_file (".EXPORT_ALL_VARIABLES");
487 if (f
!= 0 && f
->is_target
)
488 export_all_variables
= 1;
490 f
= lookup_file (".IGNORE");
491 if (f
!= 0 && f
->is_target
)
494 ignore_errors_flag
= 1;
496 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
497 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
498 f2
->command_flags
|= COMMANDS_NOERROR
;
501 f
= lookup_file (".SILENT");
502 if (f
!= 0 && f
->is_target
)
507 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
508 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
509 f2
->command_flags
|= COMMANDS_SILENT
;
512 f
= lookup_file (".POSIX");
513 if (f
!= 0 && f
->is_target
)
516 f
= lookup_file (".NOTPARALLEL");
517 if (f
!= 0 && f
->is_target
)
521 /* Set the `command_state' member of FILE and all its `also_make's. */
524 set_command_state (file
, state
)
530 file
->command_state
= state
;
532 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
533 d
->file
->command_state
= state
;
536 /* Convert an external file timestamp to internal form. */
539 file_timestamp_cons (fname
, s
, ns
)
544 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
545 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
546 FILE_TIMESTAMP ts
= product
+ offset
;
548 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
549 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
551 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
552 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
553 file_timestamp_sprintf (buf
, ts
);
554 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
555 fname
? fname
: _("Current time"), buf
);
561 /* Return the current time as a file timestamp, setting *RESOLUTION to
564 file_timestamp_now (resolution
)
571 /* Don't bother with high-resolution clocks if file timestamps have
572 only one-second resolution. The code below should work, but it's
573 not worth the hassle of debugging it on hosts where it fails. */
574 #if FILE_TIMESTAMP_HI_RES
575 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
577 struct timespec timespec
;
578 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
582 ns
= timespec
.tv_nsec
;
587 # if HAVE_GETTIMEOFDAY
589 struct timeval timeval
;
590 if (gettimeofday (&timeval
, 0) == 0)
594 ns
= timeval
.tv_usec
* 1000;
602 s
= time ((time_t *) 0);
607 return file_timestamp_cons (0, s
, ns
);
610 /* Place into the buffer P a printable representation of the file
613 file_timestamp_sprintf (p
, ts
)
617 time_t t
= FILE_TIMESTAMP_S (ts
);
618 struct tm
*tm
= localtime (&t
);
621 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
622 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
623 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
625 sprintf (p
, "%ld", (long) t
);
627 sprintf (p
, "%lu", (unsigned long) t
);
630 /* Append nanoseconds as a fraction, but remove trailing zeros.
631 We don't know the actual timestamp resolution, since clock_getres
632 applies only to local times, whereas this timestamp might come
633 from a remote filesystem. So removing trailing zeros is the
634 best guess that we can do. */
635 sprintf (p
, ".%09d", FILE_TIMESTAMP_NS (ts
));
644 /* Print the data base of files. */
655 puts (_("# Not a target:"));
656 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
658 /* Print all normal dependencies; note any order-only deps. */
659 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
660 if (! d
->ignore_mtime
)
661 printf (" %s", dep_name (d
));
665 /* Print order-only deps, if we have any. */
668 printf (" | %s", dep_name (ood
));
669 for (d
= ood
->next
; d
!= 0; d
= d
->next
)
671 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 puts (_("\n# Files"));
762 hash_map (&files
, print_file
);
764 fputs (_("\n# files hash-table stats:\n# "), stdout
);
765 hash_print_stats (&files
, stdout
);
768 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
771 build_target_list (value
)
774 static unsigned long last_targ_count
= 0;
776 if (files
.ht_fill
!= last_targ_count
)
778 unsigned long max
= EXPANSION_INCREMENT (strlen (value
));
781 struct file
**fp
= (struct file
**) files
.ht_vec
;
782 struct file
**end
= &fp
[files
.ht_size
];
784 /* Make sure we have at least MAX bytes in the allocated buffer. */
785 value
= xrealloc (value
, max
);
789 for (; fp
< end
; ++fp
)
790 if (!HASH_VACANT (*fp
) && (*fp
)->is_target
)
792 struct file
*f
= *fp
;
793 int l
= strlen (f
->name
);
798 unsigned long off
= p
- value
;
800 max
+= EXPANSION_INCREMENT (l
+ 1);
801 value
= xrealloc (value
, max
);
805 bcopy (f
->name
, p
, l
);
811 last_targ_count
= files
.ht_fill
;
820 hash_init (&files
, 1000, file_hash_1
, file_hash_2
, file_hash_cmp
);