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 /* Remember whether snap_deps has been invoked: we need this to be sure we
35 don't add new rules (via $(eval ...)) afterwards. In the future it would
36 be nice to support this, but it means we'd need to re-run snap_deps() or
37 at least its functionality... it might mean changing snap_deps() to be run
38 per-file, so we can invoke it after the eval... or remembering which files
39 in the hash have been snapped (a new boolean flag?) and having snap_deps()
40 only work on files which have not yet been snapped. */
43 /* Hash table of files the makefile knows how to make. */
46 file_hash_1 (const void *key
)
48 return_ISTRING_HASH_1 (((struct file
const *) key
)->hname
);
52 file_hash_2 (const void *key
)
54 return_ISTRING_HASH_2 (((struct file
const *) key
)->hname
);
58 file_hash_cmp (const void *x
, const void *y
)
60 return_ISTRING_COMPARE (((struct file
const *) x
)->hname
,
61 ((struct file
const *) y
)->hname
);
65 #define FILE_BUCKETS 1007
67 static struct hash_table files
;
69 /* Whether or not .SECONDARY with no prerequisites was given. */
70 static int all_secondary
= 0;
72 /* Access the hash table of all file records.
73 lookup_file given a name, return the struct file * for that name,
74 or nil if there is none.
75 enter_file similar, but create one if there is none. */
78 lookup_file (char *name
)
80 register struct file
*f
;
82 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
83 register char *lname
, *ln
;
86 assert (*name
!= '\0');
88 /* This is also done in parse_file_seq, so this is redundant
89 for names read from makefiles. It is here for names passed
90 on the command line. */
92 # ifndef WANT_CASE_SENSITIVE_TARGETS
95 lname
= (char *) malloc (strlen (name
) + 1);
96 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
97 *ln
= isupper ((unsigned char)*n
) ? tolower ((unsigned char)*n
) : *n
;
103 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
106 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
110 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
115 /* It was all slashes after a dot. */
126 file_key
.hname
= name
;
127 f
= (struct file
*) hash_find_item (&files
, &file_key
);
128 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
135 enter_file (char *name
)
137 register struct file
*f
;
138 register struct file
*new;
139 register struct file
**file_slot
;
140 struct file file_key
;
141 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
145 assert (*name
!= '\0');
147 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
150 lname
= (char *) malloc (strlen (name
) + 1);
151 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
153 if (isupper ((unsigned char)*n
))
154 *ln
= tolower ((unsigned char)*n
);
160 /* Creates a possible leak, old value of name is unreachable, but I
161 currently don't know how to fix it. */
166 file_key
.hname
= name
;
167 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
169 if (! HASH_VACANT (f
) && !f
->double_colon
)
171 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
177 new = (struct file
*) xmalloc (sizeof (struct file
));
178 bzero ((char *) new, sizeof (struct file
));
179 new->name
= new->hname
= name
;
180 new->update_status
= -1;
183 hash_insert_at (&files
, new, file_slot
);
186 /* There is already a double-colon entry for this file. */
187 new->double_colon
= f
;
196 /* Rename FILE to NAME. This is not as simple as resetting
197 the `name' member, since it must be put in a new hash bucket,
198 and possibly merged with an existing file called NAME. */
201 rename_file (struct file
*from_file
, char *to_hname
)
203 rehash_file (from_file
, to_hname
);
206 from_file
->name
= from_file
->hname
;
207 from_file
= from_file
->prev
;
211 /* Rehash FILE to NAME. This is not as simple as resetting
212 the `hname' member, since it must be put in a new hash bucket,
213 and possibly merged with an existing file called NAME. */
216 rehash_file (struct file
*from_file
, char *to_hname
)
218 struct file file_key
;
219 struct file
**file_slot
;
220 struct file
*to_file
;
221 struct file
*deleted_file
;
224 file_key
.hname
= to_hname
;
225 if (0 == file_hash_cmp (from_file
, &file_key
))
228 file_key
.hname
= from_file
->hname
;
229 while (from_file
->renamed
!= 0)
230 from_file
= from_file
->renamed
;
231 if (file_hash_cmp (from_file
, &file_key
))
232 /* hname changed unexpectedly */
235 deleted_file
= hash_delete (&files
, from_file
);
236 if (deleted_file
!= from_file
)
237 /* from_file isn't the one stored in files */
240 file_key
.hname
= to_hname
;
241 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
242 to_file
= *file_slot
;
244 from_file
->hname
= to_hname
;
245 for (f
= from_file
->double_colon
; f
!= 0; f
= f
->prev
)
248 if (HASH_VACANT (to_file
))
249 hash_insert_at (&files
, from_file
, file_slot
);
252 /* TO_FILE already exists under TO_HNAME.
253 We must retain TO_FILE and merge FROM_FILE into it. */
255 if (from_file
->cmds
!= 0)
257 if (to_file
->cmds
== 0)
258 to_file
->cmds
= from_file
->cmds
;
259 else if (from_file
->cmds
!= to_file
->cmds
)
261 /* We have two sets of commands. We will go with the
262 one given in the rule explicitly mentioning this name,
263 but give a message to let the user know what's going on. */
264 if (to_file
->cmds
->fileinfo
.filenm
!= 0)
265 error (&from_file
->cmds
->fileinfo
,
266 _("Commands were specified for file `%s' at %s:%lu,"),
267 from_file
->name
, to_file
->cmds
->fileinfo
.filenm
,
268 to_file
->cmds
->fileinfo
.lineno
);
270 error (&from_file
->cmds
->fileinfo
,
271 _("Commands for file `%s' were found by implicit rule search,"),
273 error (&from_file
->cmds
->fileinfo
,
274 _("but `%s' is now considered the same file as `%s'."),
275 from_file
->name
, to_hname
);
276 error (&from_file
->cmds
->fileinfo
,
277 _("Commands for `%s' will be ignored in favor of those for `%s'."),
278 to_hname
, from_file
->name
);
282 /* Merge the dependencies of the two files. */
284 if (to_file
->deps
== 0)
285 to_file
->deps
= from_file
->deps
;
288 register struct dep
*deps
= to_file
->deps
;
289 while (deps
->next
!= 0)
291 deps
->next
= from_file
->deps
;
294 merge_variable_set_lists (&to_file
->variables
, from_file
->variables
);
296 if (to_file
->double_colon
&& from_file
->is_target
&& !from_file
->double_colon
)
297 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
298 from_file
->name
, to_hname
);
299 if (!to_file
->double_colon
&& from_file
->double_colon
)
301 if (to_file
->is_target
)
302 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
303 from_file
->name
, to_hname
);
305 to_file
->double_colon
= from_file
->double_colon
;
308 if (from_file
->last_mtime
> to_file
->last_mtime
)
309 /* %%% Kludge so -W wins on a file that gets vpathized. */
310 to_file
->last_mtime
= from_file
->last_mtime
;
312 to_file
->mtime_before_update
= from_file
->mtime_before_update
;
314 #define MERGE(field) to_file->field |= from_file->field
316 MERGE (tried_implicit
);
322 MERGE (ignore_vpath
);
325 from_file
->renamed
= to_file
;
329 /* Remove all nonprecious intermediate files.
330 If SIG is nonzero, this was caused by a fatal signal,
331 meaning that a different message will be printed, and
332 the message will go to stderr rather than stdout. */
335 remove_intermediates (int sig
)
337 register struct file
**file_slot
;
338 register struct file
**file_end
;
341 /* If there's no way we will ever remove anything anyway, punt early. */
342 if (question_flag
|| touch_flag
|| all_secondary
)
345 if (sig
&& just_print_flag
)
348 file_slot
= (struct file
**) files
.ht_vec
;
349 file_end
= file_slot
+ files
.ht_size
;
350 for ( ; file_slot
< file_end
; file_slot
++)
351 if (! HASH_VACANT (*file_slot
))
353 register struct file
*f
= *file_slot
;
354 /* Is this file eligible for automatic deletion?
355 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
356 given on the command-line, and it's either a -include makefile or
357 it's not precious. */
358 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
359 && !f
->secondary
&& !f
->cmd_target
)
362 if (f
->update_status
== -1)
363 /* If nothing would have created this file yet,
364 don't print an "rm" command for it. */
370 status
= unlink (f
->name
);
371 if (status
< 0 && errno
== ENOENT
)
377 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
381 DB (DB_BASIC
, (_("Removing intermediate files...\n")));
386 fputs ("rm ", stdout
);
391 fputs (f
->name
, stdout
);
396 perror_with_name ("unlink: ", f
->name
);
408 /* Set the intermediate flag. */
411 set_intermediate (const void *item
)
413 struct file
*f
= (struct file
*) item
;
417 /* Expand and parse each dependency line. */
419 expand_deps (struct file
*f
)
423 struct dep
*old
= f
->deps
;
424 unsigned int last_dep_has_cmds
= f
->updating
;
430 for (d
= old
; d
!= 0; d
= d
->next
)
436 /* If we need a second expansion on these, set up the file
437 variables, etc. It takes a lot of extra memory and processing
438 to do this, so only do it if it's needed. */
439 if (! d
->need_2nd_expansion
)
443 /* We are going to do second expansion so initialize file
444 variables for the file. */
447 initialize_file_variables (f
, 0);
451 set_file_variables (f
);
453 p
= variable_expand_for_file (d
->name
, f
);
456 /* Parse the dependencies. */
459 parse_file_seq (&p
, '|', sizeof (struct dep
), 1),
460 sizeof (struct dep
));
464 /* Files that follow '|' are special prerequisites that
465 need only exist in order to satisfy the dependency.
466 Their modification times are irrelevant. */
469 for (d_ptr
= &new; *d_ptr
; d_ptr
= &(*d_ptr
)->next
)
473 *d_ptr
= (struct dep
*)
475 parse_file_seq (&p
, '\0', sizeof (struct dep
), 1),
476 sizeof (struct dep
));
478 for (d1
= *d_ptr
; d1
!= 0; d1
= d1
->next
)
479 d1
->ignore_mtime
= 1;
482 /* Enter them as files. */
483 for (d1
= new; d1
!= 0; d1
= d1
->next
)
485 d1
->file
= lookup_file (d1
->name
);
487 d1
->file
= enter_file (d1
->name
);
491 d1
->need_2nd_expansion
= 0;
494 /* Add newly parsed deps to f->deps. If this is the last
495 dependency line and this target has commands then put
496 it in front so the last dependency line (the one with
497 commands) ends up being the first. This is important
498 because people expect $< to hold first prerequisite
499 from the rule with commands. If it is not the last
500 dependency line or the rule does not have commands
501 then link it at the end so it appears in makefile
506 if (d
->next
== 0 && last_dep_has_cmds
)
509 for (d_ptr
= &new; *d_ptr
; d_ptr
= &(*d_ptr
)->next
)
518 for (d_ptr
= &f
->deps
; *d_ptr
; d_ptr
= &(*d_ptr
)->next
)
527 free_ns_chain ((struct nameseq
*) old
);
530 /* For each dependency of each file, make the `struct dep' point
531 at the appropriate `struct file' (which may have to be created).
533 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
534 and various other special targets. */
542 struct file
**file_slot_0
;
543 struct file
**file_slot
;
544 struct file
**file_end
;
546 /* Perform second expansion and enter each dependency
549 /* Expand .SUFFIXES first; it's dependencies are used for
551 for (f
= lookup_file (".SUFFIXES"); f
!= 0; f
= f
->prev
)
554 /* We must use hash_dump (), because within this loop
555 we might add new files to the table, possibly causing
556 an in-situ table expansion. */
557 file_slot_0
= (struct file
**) hash_dump (&files
, 0, 0);
558 file_end
= file_slot_0
+ files
.ht_fill
;
559 for (file_slot
= file_slot_0
; file_slot
< file_end
; file_slot
++)
560 for (f
= *file_slot
; f
!= 0; f
= f
->prev
)
562 if (strcmp (f
->name
, ".SUFFIXES") != 0)
567 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
568 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
569 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
572 for (f
= lookup_file (".LOW_RESOLUTION_TIME"); f
!= 0; f
= f
->prev
)
573 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
574 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
575 f2
->low_resolution_time
= 1;
577 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
578 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
579 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
581 /* Mark this file as phony nonexistent target. */
584 f2
->last_mtime
= NONEXISTENT_MTIME
;
585 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
588 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
590 /* .INTERMEDIATE with deps listed
591 marks those deps as intermediate files. */
592 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
593 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
594 f2
->intermediate
= 1;
595 /* .INTERMEDIATE with no deps does nothing.
596 Marking all files as intermediates is useless
597 since the goal targets would be deleted after they are built. */
600 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
602 /* .SECONDARY with deps listed
603 marks those deps as intermediate files
604 in that they don't get rebuilt if not actually needed;
605 but unlike real intermediate files,
606 these are not deleted after make finishes. */
608 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
609 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
610 f2
->intermediate
= f2
->secondary
= 1;
611 /* .SECONDARY with no deps listed marks *all* files that way. */
615 hash_map (&files
, set_intermediate
);
619 f
= lookup_file (".EXPORT_ALL_VARIABLES");
620 if (f
!= 0 && f
->is_target
)
621 export_all_variables
= 1;
623 f
= lookup_file (".IGNORE");
624 if (f
!= 0 && f
->is_target
)
627 ignore_errors_flag
= 1;
629 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
630 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
631 f2
->command_flags
|= COMMANDS_NOERROR
;
634 f
= lookup_file (".SILENT");
635 if (f
!= 0 && f
->is_target
)
640 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
641 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
642 f2
->command_flags
|= COMMANDS_SILENT
;
645 f
= lookup_file (".POSIX");
646 if (f
!= 0 && f
->is_target
)
649 f
= lookup_file (".NOTPARALLEL");
650 if (f
!= 0 && f
->is_target
)
653 /* Remember that we've done this. */
657 /* Set the `command_state' member of FILE and all its `also_make's. */
660 set_command_state (struct file
*file
, enum cmd_state state
)
664 file
->command_state
= state
;
666 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
667 d
->file
->command_state
= state
;
670 /* Convert an external file timestamp to internal form. */
673 file_timestamp_cons (const char *fname
, time_t s
, int ns
)
675 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
676 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
677 FILE_TIMESTAMP ts
= product
+ offset
;
679 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
680 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
682 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
683 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
684 file_timestamp_sprintf (buf
, ts
);
685 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
686 fname
? fname
: _("Current time"), buf
);
692 /* Return the current time as a file timestamp, setting *RESOLUTION to
695 file_timestamp_now (int *resolution
)
701 /* Don't bother with high-resolution clocks if file timestamps have
702 only one-second resolution. The code below should work, but it's
703 not worth the hassle of debugging it on hosts where it fails. */
704 #if FILE_TIMESTAMP_HI_RES
705 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
707 struct timespec timespec
;
708 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
712 ns
= timespec
.tv_nsec
;
717 # if HAVE_GETTIMEOFDAY
719 struct timeval timeval
;
720 if (gettimeofday (&timeval
, 0) == 0)
724 ns
= timeval
.tv_usec
* 1000;
732 s
= time ((time_t *) 0);
735 #if FILE_TIMESTAMP_HI_RES
739 return file_timestamp_cons (0, s
, ns
);
742 /* Place into the buffer P a printable representation of the file
745 file_timestamp_sprintf (char *p
, FILE_TIMESTAMP ts
)
747 time_t t
= FILE_TIMESTAMP_S (ts
);
748 struct tm
*tm
= localtime (&t
);
751 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
752 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
753 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
755 sprintf (p
, "%ld", (long) t
);
757 sprintf (p
, "%lu", (unsigned long) t
);
760 /* Append nanoseconds as a fraction, but remove trailing zeros.
761 We don't know the actual timestamp resolution, since clock_getres
762 applies only to local times, whereas this timestamp might come
763 from a remote filesystem. So removing trailing zeros is the
764 best guess that we can do. */
765 sprintf (p
, ".%09d", FILE_TIMESTAMP_NS (ts
));
774 /* Print the data base of files. */
777 print_file (const void *item
)
779 struct file
*f
= (struct file
*) item
;
785 puts (_("# Not a target:"));
786 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
788 /* Print all normal dependencies; note any order-only deps. */
789 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
790 if (! d
->ignore_mtime
)
791 printf (" %s", dep_name (d
));
795 /* Print order-only deps, if we have any. */
798 printf (" | %s", dep_name (ood
));
799 for (d
= ood
->next
; d
!= 0; d
= d
->next
)
801 printf (" %s", dep_name (d
));
807 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
809 puts (_("# Phony target (prerequisite of .PHONY)."));
811 puts (_("# Command-line target."));
813 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
814 puts (f
->tried_implicit
815 ? _("# Implicit rule search has been done.")
816 : _("# Implicit rule search has not been done."));
818 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
820 puts (_("# File is an intermediate prerequisite."));
821 if (f
->also_make
!= 0)
823 fputs (_("# Also makes:"), stdout
);
824 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
825 printf (" %s", dep_name (d
));
828 if (f
->last_mtime
== UNKNOWN_MTIME
)
829 puts (_("# Modification time never checked."));
830 else if (f
->last_mtime
== NONEXISTENT_MTIME
)
831 puts (_("# File does not exist."));
832 else if (f
->last_mtime
== OLD_MTIME
)
833 puts (_("# File is very old."));
836 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
837 file_timestamp_sprintf (buf
, f
->last_mtime
);
838 printf (_("# Last modified %s\n"), buf
);
841 ? _("# File has been updated.") : _("# File has not been updated."));
842 switch (f
->command_state
)
845 puts (_("# Commands currently running (THIS IS A BUG)."));
847 case cs_deps_running
:
848 puts (_("# Dependencies commands running (THIS IS A BUG)."));
852 switch (f
->update_status
)
857 puts (_("# Successfully updated."));
860 assert (question_flag
);
861 puts (_("# Needs to be updated (-q is set)."));
864 puts (_("# Failed to be updated."));
867 puts (_("# Invalid value in `update_status' member!"));
874 puts (_("# Invalid value in `command_state' member!"));
880 if (f
->variables
!= 0)
881 print_file_variables (f
);
884 print_commands (f
->cmds
);
887 print_file ((const void *) f
->prev
);
891 print_file_data_base (void)
893 puts (_("\n# Files"));
895 hash_map (&files
, print_file
);
897 fputs (_("\n# files hash-table stats:\n# "), stdout
);
898 hash_print_stats (&files
, stdout
);
901 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
904 build_target_list (char *value
)
906 static unsigned long last_targ_count
= 0;
908 if (files
.ht_fill
!= last_targ_count
)
910 unsigned long max
= EXPANSION_INCREMENT (strlen (value
));
913 struct file
**fp
= (struct file
**) files
.ht_vec
;
914 struct file
**end
= &fp
[files
.ht_size
];
916 /* Make sure we have at least MAX bytes in the allocated buffer. */
917 value
= xrealloc (value
, max
);
921 for (; fp
< end
; ++fp
)
922 if (!HASH_VACANT (*fp
) && (*fp
)->is_target
)
924 struct file
*f
= *fp
;
925 int l
= strlen (f
->name
);
930 unsigned long off
= p
- value
;
932 max
+= EXPANSION_INCREMENT (l
+ 1);
933 value
= xrealloc (value
, max
);
937 bcopy (f
->name
, p
, l
);
943 last_targ_count
= files
.ht_fill
;
950 init_hash_files (void)
952 hash_init (&files
, 1000, file_hash_1
, file_hash_2
, file_hash_cmp
);