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 (const void *key
)
39 return_ISTRING_HASH_1 (((struct file
const *) key
)->hname
);
43 file_hash_2 (const void *key
)
45 return_ISTRING_HASH_2 (((struct file
const *) key
)->hname
);
49 file_hash_cmp (const void *x
, const void *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. */
69 lookup_file (char *name
)
71 register struct file
*f
;
73 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
74 register char *lname
, *ln
;
77 assert (*name
!= '\0');
79 /* This is also done in parse_file_seq, so this is redundant
80 for names read from makefiles. It is here for names passed
81 on the command line. */
83 # ifndef WANT_CASE_SENSITIVE_TARGETS
86 lname
= (char *) malloc (strlen (name
) + 1);
87 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
88 *ln
= isupper ((unsigned char)*n
) ? tolower ((unsigned char)*n
) : *n
;
94 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
97 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
101 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
106 /* It was all slashes after a dot. */
117 file_key
.hname
= name
;
118 f
= (struct file
*) hash_find_item (&files
, &file_key
);
119 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
126 enter_file (char *name
)
128 register struct file
*f
;
129 register struct file
*new;
130 register struct file
**file_slot
;
131 struct file file_key
;
132 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
136 assert (*name
!= '\0');
138 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
141 lname
= (char *) malloc (strlen (name
) + 1);
142 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
144 if (isupper ((unsigned char)*n
))
145 *ln
= tolower ((unsigned char)*n
);
151 /* Creates a possible leak, old value of name is unreachable, but I
152 currently don't know how to fix it. */
157 file_key
.hname
= name
;
158 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
160 if (! HASH_VACANT (f
) && !f
->double_colon
)
162 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
168 new = (struct file
*) xmalloc (sizeof (struct file
));
169 bzero ((char *) new, sizeof (struct file
));
170 new->name
= new->hname
= name
;
171 new->update_status
= -1;
174 hash_insert_at (&files
, new, file_slot
);
177 /* There is already a double-colon entry for this file. */
178 new->double_colon
= f
;
187 /* Rename FILE to NAME. This is not as simple as resetting
188 the `name' member, since it must be put in a new hash bucket,
189 and possibly merged with an existing file called NAME. */
192 rename_file (struct file
*from_file
, char *to_hname
)
194 rehash_file (from_file
, to_hname
);
197 from_file
->name
= from_file
->hname
;
198 from_file
= from_file
->prev
;
202 /* Rehash FILE to NAME. This is not as simple as resetting
203 the `hname' member, since it must be put in a new hash bucket,
204 and possibly merged with an existing file called NAME. */
207 rehash_file (struct file
*from_file
, char *to_hname
)
209 struct file file_key
;
210 struct file
**file_slot
;
211 struct file
*to_file
;
212 struct file
*deleted_file
;
215 file_key
.hname
= to_hname
;
216 if (0 == file_hash_cmp (from_file
, &file_key
))
219 file_key
.hname
= from_file
->hname
;
220 while (from_file
->renamed
!= 0)
221 from_file
= from_file
->renamed
;
222 if (file_hash_cmp (from_file
, &file_key
))
223 /* hname changed unexpectedly */
226 deleted_file
= hash_delete (&files
, from_file
);
227 if (deleted_file
!= from_file
)
228 /* from_file isn't the one stored in files */
231 file_key
.hname
= to_hname
;
232 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
233 to_file
= *file_slot
;
235 from_file
->hname
= to_hname
;
236 for (f
= from_file
->double_colon
; f
!= 0; f
= f
->prev
)
239 if (HASH_VACANT (to_file
))
240 hash_insert_at (&files
, from_file
, file_slot
);
243 /* TO_FILE already exists under TO_HNAME.
244 We must retain TO_FILE and merge FROM_FILE into it. */
246 if (from_file
->cmds
!= 0)
248 if (to_file
->cmds
== 0)
249 to_file
->cmds
= from_file
->cmds
;
250 else if (from_file
->cmds
!= to_file
->cmds
)
252 /* We have two sets of commands. We will go with the
253 one given in the rule explicitly mentioning this name,
254 but give a message to let the user know what's going on. */
255 if (to_file
->cmds
->fileinfo
.filenm
!= 0)
256 error (&from_file
->cmds
->fileinfo
,
257 _("Commands were specified for file `%s' at %s:%lu,"),
258 from_file
->name
, to_file
->cmds
->fileinfo
.filenm
,
259 to_file
->cmds
->fileinfo
.lineno
);
261 error (&from_file
->cmds
->fileinfo
,
262 _("Commands for file `%s' were found by implicit rule search,"),
264 error (&from_file
->cmds
->fileinfo
,
265 _("but `%s' is now considered the same file as `%s'."),
266 from_file
->name
, to_hname
);
267 error (&from_file
->cmds
->fileinfo
,
268 _("Commands for `%s' will be ignored in favor of those for `%s'."),
269 to_hname
, from_file
->name
);
273 /* Merge the dependencies of the two files. */
275 if (to_file
->deps
== 0)
276 to_file
->deps
= from_file
->deps
;
279 register struct dep
*deps
= to_file
->deps
;
280 while (deps
->next
!= 0)
282 deps
->next
= from_file
->deps
;
285 merge_variable_set_lists (&to_file
->variables
, from_file
->variables
);
287 if (to_file
->double_colon
&& from_file
->is_target
&& !from_file
->double_colon
)
288 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
289 from_file
->name
, to_hname
);
290 if (!to_file
->double_colon
&& from_file
->double_colon
)
292 if (to_file
->is_target
)
293 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
294 from_file
->name
, to_hname
);
296 to_file
->double_colon
= from_file
->double_colon
;
299 if (from_file
->last_mtime
> to_file
->last_mtime
)
300 /* %%% Kludge so -W wins on a file that gets vpathized. */
301 to_file
->last_mtime
= from_file
->last_mtime
;
303 to_file
->mtime_before_update
= from_file
->mtime_before_update
;
305 #define MERGE(field) to_file->field |= from_file->field
307 MERGE (tried_implicit
);
313 MERGE (ignore_vpath
);
316 from_file
->renamed
= to_file
;
320 /* Remove all nonprecious intermediate files.
321 If SIG is nonzero, this was caused by a fatal signal,
322 meaning that a different message will be printed, and
323 the message will go to stderr rather than stdout. */
326 remove_intermediates (int sig
)
328 register struct file
**file_slot
;
329 register struct file
**file_end
;
332 /* If there's no way we will ever remove anything anyway, punt early. */
333 if (question_flag
|| touch_flag
|| all_secondary
)
336 if (sig
&& just_print_flag
)
339 file_slot
= (struct file
**) files
.ht_vec
;
340 file_end
= file_slot
+ files
.ht_size
;
341 for ( ; file_slot
< file_end
; file_slot
++)
342 if (! HASH_VACANT (*file_slot
))
344 register struct file
*f
= *file_slot
;
345 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
346 && !f
->secondary
&& !f
->cmd_target
)
349 if (f
->update_status
== -1)
350 /* If nothing would have created this file yet,
351 don't print an "rm" command for it. */
357 status
= unlink (f
->name
);
358 if (status
< 0 && errno
== ENOENT
)
364 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
368 DB (DB_BASIC
, (_("Removing intermediate files...\n")));
373 fputs ("rm ", stdout
);
378 fputs (f
->name
, stdout
);
383 perror_with_name ("unlink: ", f
->name
);
395 /* For each dependency of each file, make the `struct dep' point
396 at the appropriate `struct file' (which may have to be created).
398 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
399 and various other special targets. */
404 register struct file
*f
;
405 register struct file
*f2
;
406 register struct dep
*d
;
407 register struct file
**file_slot_0
;
408 register struct file
**file_slot
;
409 register struct file
**file_end
;
411 /* Enter each dependency name as a file. */
412 /* We must use hash_dump (), because within this loop
413 we might add new files to the table, possibly causing
414 an in-situ table expansion. */
415 file_slot_0
= (struct file
**) hash_dump (&files
, 0, 0);
416 file_end
= file_slot_0
+ files
.ht_fill
;
417 for (file_slot
= file_slot_0
; file_slot
< file_end
; file_slot
++)
418 for (f2
= *file_slot
; f2
!= 0; f2
= f2
->prev
)
419 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
422 d
->file
= lookup_file (d
->name
);
424 d
->file
= enter_file (d
->name
);
431 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
432 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
433 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
436 for (f
= lookup_file (".LOW_RESOLUTION_TIME"); f
!= 0; f
= f
->prev
)
437 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
438 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
439 f2
->low_resolution_time
= 1;
441 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
442 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
443 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
445 /* Mark this file as phony and nonexistent. */
447 f2
->last_mtime
= NONEXISTENT_MTIME
;
448 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
451 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
453 /* .INTERMEDIATE with deps listed
454 marks those deps as intermediate files. */
455 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
456 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
457 f2
->intermediate
= 1;
458 /* .INTERMEDIATE with no deps does nothing.
459 Marking all files as intermediates is useless
460 since the goal targets would be deleted after they are built. */
463 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
465 /* .SECONDARY with deps listed
466 marks those deps as intermediate files
467 in that they don't get rebuilt if not actually needed;
468 but unlike real intermediate files,
469 these are not deleted after make finishes. */
471 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
472 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
473 f2
->intermediate
= f2
->secondary
= 1;
474 /* .SECONDARY with no deps listed marks *all* files that way. */
479 f
= lookup_file (".EXPORT_ALL_VARIABLES");
480 if (f
!= 0 && f
->is_target
)
481 export_all_variables
= 1;
483 f
= lookup_file (".IGNORE");
484 if (f
!= 0 && f
->is_target
)
487 ignore_errors_flag
= 1;
489 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
490 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
491 f2
->command_flags
|= COMMANDS_NOERROR
;
494 f
= lookup_file (".SILENT");
495 if (f
!= 0 && f
->is_target
)
500 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
501 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
502 f2
->command_flags
|= COMMANDS_SILENT
;
505 f
= lookup_file (".POSIX");
506 if (f
!= 0 && f
->is_target
)
509 f
= lookup_file (".NOTPARALLEL");
510 if (f
!= 0 && f
->is_target
)
514 /* Set the `command_state' member of FILE and all its `also_make's. */
517 set_command_state (struct file
*file
, int state
)
521 file
->command_state
= state
;
523 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
524 d
->file
->command_state
= state
;
527 /* Convert an external file timestamp to internal form. */
530 file_timestamp_cons (const char *fname
, time_t s
, int ns
)
532 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
533 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
534 FILE_TIMESTAMP ts
= product
+ offset
;
536 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
537 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
539 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
540 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
541 file_timestamp_sprintf (buf
, ts
);
542 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
543 fname
? fname
: _("Current time"), buf
);
549 /* Return the current time as a file timestamp, setting *RESOLUTION to
552 file_timestamp_now (int *resolution
)
558 /* Don't bother with high-resolution clocks if file timestamps have
559 only one-second resolution. The code below should work, but it's
560 not worth the hassle of debugging it on hosts where it fails. */
561 #if FILE_TIMESTAMP_HI_RES
562 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
564 struct timespec timespec
;
565 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
569 ns
= timespec
.tv_nsec
;
574 # if HAVE_GETTIMEOFDAY
576 struct timeval timeval
;
577 if (gettimeofday (&timeval
, 0) == 0)
581 ns
= timeval
.tv_usec
* 1000;
589 s
= time ((time_t *) 0);
592 #if FILE_TIMESTAMP_HI_RES
596 return file_timestamp_cons (0, s
, ns
);
599 /* Place into the buffer P a printable representation of the file
602 file_timestamp_sprintf (char *p
, FILE_TIMESTAMP ts
)
604 time_t t
= FILE_TIMESTAMP_S (ts
);
605 struct tm
*tm
= localtime (&t
);
608 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
609 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
610 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
612 sprintf (p
, "%ld", (long) t
);
614 sprintf (p
, "%lu", (unsigned long) t
);
617 /* Append nanoseconds as a fraction, but remove trailing zeros.
618 We don't know the actual timestamp resolution, since clock_getres
619 applies only to local times, whereas this timestamp might come
620 from a remote filesystem. So removing trailing zeros is the
621 best guess that we can do. */
622 sprintf (p
, ".%09d", FILE_TIMESTAMP_NS (ts
));
631 /* Print the data base of files. */
634 print_file (const void *item
)
636 struct file
*f
= (struct file
*)f
;
642 puts (_("# Not a target:"));
643 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
645 /* Print all normal dependencies; note any order-only deps. */
646 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
647 if (! d
->ignore_mtime
)
648 printf (" %s", dep_name (d
));
652 /* Print order-only deps, if we have any. */
655 printf (" | %s", dep_name (ood
));
656 for (d
= ood
->next
; d
!= 0; d
= d
->next
)
658 printf (" %s", dep_name (d
));
664 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
666 puts (_("# Phony target (prerequisite of .PHONY)."));
668 puts (_("# Command-line target."));
670 puts (_("# A default or MAKEFILES makefile."));
671 puts (f
->tried_implicit
672 ? _("# Implicit rule search has been done.")
673 : _("# Implicit rule search has not been done."));
675 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
677 puts (_("# File is an intermediate prerequisite."));
678 if (f
->also_make
!= 0)
680 fputs (_("# Also makes:"), stdout
);
681 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
682 printf (" %s", dep_name (d
));
685 if (f
->last_mtime
== UNKNOWN_MTIME
)
686 puts (_("# Modification time never checked."));
687 else if (f
->last_mtime
== NONEXISTENT_MTIME
)
688 puts (_("# File does not exist."));
689 else if (f
->last_mtime
== OLD_MTIME
)
690 puts (_("# File is very old."));
693 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
694 file_timestamp_sprintf (buf
, f
->last_mtime
);
695 printf (_("# Last modified %s\n"), buf
);
698 ? _("# File has been updated.") : _("# File has not been updated."));
699 switch (f
->command_state
)
702 puts (_("# Commands currently running (THIS IS A BUG)."));
704 case cs_deps_running
:
705 puts (_("# Dependencies commands running (THIS IS A BUG)."));
709 switch (f
->update_status
)
714 puts (_("# Successfully updated."));
717 assert (question_flag
);
718 puts (_("# Needs to be updated (-q is set)."));
721 puts (_("# Failed to be updated."));
724 puts (_("# Invalid value in `update_status' member!"));
731 puts (_("# Invalid value in `command_state' member!"));
737 if (f
->variables
!= 0)
738 print_file_variables (f
);
741 print_commands (f
->cmds
);
745 print_file_data_base (void)
747 puts (_("\n# Files"));
749 hash_map (&files
, print_file
);
751 fputs (_("\n# files hash-table stats:\n# "), stdout
);
752 hash_print_stats (&files
, stdout
);
755 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
758 build_target_list (char *value
)
760 static unsigned long last_targ_count
= 0;
762 if (files
.ht_fill
!= last_targ_count
)
764 unsigned long max
= EXPANSION_INCREMENT (strlen (value
));
767 struct file
**fp
= (struct file
**) files
.ht_vec
;
768 struct file
**end
= &fp
[files
.ht_size
];
770 /* Make sure we have at least MAX bytes in the allocated buffer. */
771 value
= xrealloc (value
, max
);
775 for (; fp
< end
; ++fp
)
776 if (!HASH_VACANT (*fp
) && (*fp
)->is_target
)
778 struct file
*f
= *fp
;
779 int l
= strlen (f
->name
);
784 unsigned long off
= p
- value
;
786 max
+= EXPANSION_INCREMENT (l
+ 1);
787 value
= xrealloc (value
, max
);
791 bcopy (f
->name
, p
, l
);
797 last_targ_count
= files
.ht_fill
;
804 init_hash_files (void)
806 hash_init (&files
, 1000, file_hash_1
, file_hash_2
, file_hash_cmp
);