1 /* Target file management for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
30 #ifdef CONFIG_WITH_STRCACHE2
35 /* Remember whether snap_deps has been invoked: we need this to be sure we
36 don't add new rules (via $(eval ...)) afterwards. In the future it would
37 be nice to support this, but it means we'd need to re-run snap_deps() or
38 at least its functionality... it might mean changing snap_deps() to be run
39 per-file, so we can invoke it after the eval... or remembering which files
40 in the hash have been snapped (a new boolean flag?) and having snap_deps()
41 only work on files which have not yet been snapped. */
44 /* Hash table of files the makefile knows how to make. */
46 #ifndef CONFIG_WITH_STRCACHE2
48 file_hash_1 (const void *key
)
50 return_ISTRING_HASH_1 (((struct file
const *) key
)->hname
);
54 file_hash_2 (const void *key
)
56 return_ISTRING_HASH_2 (((struct file
const *) key
)->hname
);
58 #endif /* !CONFIG_WITH_STRCACHE2 */
61 file_hash_cmp (const void *x
, const void *y
)
63 #ifndef CONFIG_WITH_STRCACHE2
64 return_ISTRING_COMPARE (((struct file
const *) x
)->hname
,
65 ((struct file
const *) y
)->hname
);
66 #else /* CONFIG_WITH_STRCACHE2 */
67 return ((struct file
const *) x
)->hname
68 == ((struct file
const *) y
)->hname
? 0 : -1;
69 #endif /* CONFIG_WITH_STRCACHE2 */
73 #define FILE_BUCKETS 1007
75 static struct hash_table files
;
77 /* Whether or not .SECONDARY with no prerequisites was given. */
78 static int all_secondary
= 0;
80 /* Access the hash table of all file records.
81 lookup_file given a name, return the struct file * for that name,
82 or nil if there is none.
85 #ifndef CONFIG_WITH_STRCACHE2
87 lookup_file (const char *name
)
88 #else /* CONFIG_WITH_STRCACHE2 */
89 MY_INLINE
struct file
*
90 lookup_file_common (const char *name
, int cached
)
91 #endif /* CONFIG_WITH_STRCACHE2 */
95 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
99 assert (*name
!= '\0');
101 /* This is also done in parse_file_seq, so this is redundant
102 for names read from makefiles. It is here for names passed
103 on the command line. */
105 # ifndef WANT_CASE_SENSITIVE_TARGETS
110 lname
= xstrdup (name
);
111 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
112 *ln
= isupper ((unsigned char)*n
) ? tolower ((unsigned char)*n
) : *n
;
118 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
121 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
125 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
130 /* It was all slashes after a dot. */
133 #elif defined(_AMIGA)
139 #ifndef CONFIG_WITH_STRCACHE2
140 file_key
.hname
= name
;
141 f
= hash_find_item (&files
, &file_key
);
142 #else /* CONFIG_WITH_STRCACHE2 */
145 file_key
.hname
= strcache2_lookup_file (&file_strcache
, name
, strlen (name
));
147 f
= hash_find_item_strcached (&files
, &file_key
);
153 file_key
.hname
= name
;
154 f
= hash_find_item_strcached (&files
, &file_key
);
157 #endif /* CONFIG_WITH_STRCACHE2 */
158 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
166 #ifdef CONFIG_WITH_STRCACHE2
167 /* Given a name, return the struct file * for that name,
168 or nil if there is none. */
171 lookup_file (const char *name
)
173 return lookup_file_common (name
, 0 /* cached */);
176 /* Given a name in the strcache, return the struct file * for that name,
177 or nil if there is none. */
179 lookup_file_cached (const char *name
)
181 assert (strcache_iscached (name
));
182 return lookup_file_common (name
, 1 /* cached */);
184 #endif /* CONFIG_WITH_STRCACHE2 */
187 /* Look up a file record for file NAME and return it.
188 Create a new record if one doesn't exist. NAME will be stored in the
189 new record so it should be constant or in the strcache etc.
193 enter_file (const char *name
)
197 struct file
**file_slot
;
198 struct file file_key
;
200 assert (*name
!= '\0');
201 assert (strcache_iscached (name
));
203 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
208 lname
= xstrdup (name
);
209 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
210 if (isupper ((unsigned char)*n
))
211 *ln
= tolower ((unsigned char)*n
);
216 name
= strcache_add (lname
);
221 file_key
.hname
= name
;
222 #ifndef CONFIG_WITH_STRCACHE2
223 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
224 #else /* CONFIG_WITH_STRCACHE2 */
225 file_slot
= (struct file
**) hash_find_slot_strcached (&files
, &file_key
);
226 #endif /* CONFIG_WITH_STRCACHE2 */
228 if (! HASH_VACANT (f
) && !f
->double_colon
)
231 #ifndef CONFIG_WITH_ALLOC_CACHES
232 new = xmalloc (sizeof (struct file
));
233 memset (new, '\0', sizeof (struct file
));
235 new = alloccache_calloc (&file_cache
);
237 new->name
= new->hname
= name
;
238 new->update_status
= -1;
243 hash_insert_at (&files
, new, file_slot
);
247 /* There is already a double-colon entry for this file. */
248 new->double_colon
= f
;
253 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
254 /* Check if the name needs 2nd expansion or not. */
255 if (second_target_expansion
&& strchr (name
, '$') != NULL
)
256 new->need_2nd_target_expansion
= 1;
262 /* Rehash FILE to NAME. This is not as simple as resetting
263 the `hname' member, since it must be put in a new hash bucket,
264 and possibly merged with an existing file called NAME. */
267 rehash_file (struct file
*from_file
, const char *to_hname
)
269 struct file file_key
;
270 struct file
**file_slot
;
271 struct file
*to_file
;
272 struct file
*deleted_file
;
275 #ifdef CONFIG_WITH_STRCACHE2
276 assert (strcache_iscached (to_hname
));
277 assert (strcache_iscached (from_file
->hname
));
280 /* If it's already that name, we're done. */
281 file_key
.hname
= to_hname
;
282 if (! file_hash_cmp (from_file
, &file_key
))
285 /* Find the end of the renamed list for the "from" file. */
286 file_key
.hname
= from_file
->hname
;
287 while (from_file
->renamed
!= 0)
288 from_file
= from_file
->renamed
;
289 if (file_hash_cmp (from_file
, &file_key
))
290 /* hname changed unexpectedly!! */
293 /* Remove the "from" file from the hash. */
294 #ifndef CONFIG_WITH_STRCACHE2
295 deleted_file
= hash_delete (&files
, from_file
);
297 deleted_file
= hash_delete_strcached (&files
, from_file
);
299 if (deleted_file
!= from_file
)
300 /* from_file isn't the one stored in files */
303 /* Find where the newly renamed file will go in the hash. */
304 file_key
.hname
= to_hname
;
305 #ifndef CONFIG_WITH_STRCACHE2
306 file_slot
= (struct file
**) hash_find_slot (&files
, &file_key
);
307 #else /* CONFIG_WITH_STRCACHE2 */
308 file_slot
= (struct file
**) hash_find_slot_strcached (&files
, &file_key
);
309 #endif /* CONFIG_WITH_STRCACHE2 */
310 to_file
= *file_slot
;
312 /* Change the hash name for this file. */
313 from_file
->hname
= to_hname
;
314 for (f
= from_file
->double_colon
; f
!= 0; f
= f
->prev
)
317 /* If the new name doesn't exist yet just set it to the renamed file. */
318 if (HASH_VACANT (to_file
))
320 hash_insert_at (&files
, from_file
, file_slot
);
324 /* TO_FILE already exists under TO_HNAME.
325 We must retain TO_FILE and merge FROM_FILE into it. */
327 if (from_file
->cmds
!= 0)
329 if (to_file
->cmds
== 0)
330 to_file
->cmds
= from_file
->cmds
;
331 else if (from_file
->cmds
!= to_file
->cmds
)
333 /* We have two sets of commands. We will go with the
334 one given in the rule explicitly mentioning this name,
335 but give a message to let the user know what's going on. */
336 if (to_file
->cmds
->fileinfo
.filenm
!= 0)
337 error (&from_file
->cmds
->fileinfo
,
338 _("Recipe was specified for file `%s' at %s:%lu,"),
339 from_file
->name
, to_file
->cmds
->fileinfo
.filenm
,
340 to_file
->cmds
->fileinfo
.lineno
);
342 error (&from_file
->cmds
->fileinfo
,
343 _("Recipe for file `%s' was found by implicit rule search,"),
345 error (&from_file
->cmds
->fileinfo
,
346 _("but `%s' is now considered the same file as `%s'."),
347 from_file
->name
, to_hname
);
348 error (&from_file
->cmds
->fileinfo
,
349 _("Recipe for `%s' will be ignored in favor of the one for `%s'."),
350 to_hname
, from_file
->name
);
354 /* Merge the dependencies of the two files. */
356 if (to_file
->deps
== 0)
357 to_file
->deps
= from_file
->deps
;
360 struct dep
*deps
= to_file
->deps
;
361 while (deps
->next
!= 0)
363 deps
->next
= from_file
->deps
;
366 merge_variable_set_lists (&to_file
->variables
, from_file
->variables
);
368 if (to_file
->double_colon
&& from_file
->is_target
&& !from_file
->double_colon
)
369 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
370 from_file
->name
, to_hname
);
371 if (!to_file
->double_colon
&& from_file
->double_colon
)
373 if (to_file
->is_target
)
374 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
375 from_file
->name
, to_hname
);
377 to_file
->double_colon
= from_file
->double_colon
;
380 if (from_file
->last_mtime
> to_file
->last_mtime
)
381 /* %%% Kludge so -W wins on a file that gets vpathized. */
382 to_file
->last_mtime
= from_file
->last_mtime
;
384 to_file
->mtime_before_update
= from_file
->mtime_before_update
;
386 #define MERGE(field) to_file->field |= from_file->field
388 MERGE (tried_implicit
);
394 MERGE (ignore_vpath
);
397 from_file
->renamed
= to_file
;
400 /* Rename FILE to NAME. This is not as simple as resetting
401 the `name' member, since it must be put in a new hash bucket,
402 and possibly merged with an existing file called NAME. */
405 rename_file (struct file
*from_file
, const char *to_hname
)
407 rehash_file (from_file
, to_hname
);
410 from_file
->name
= from_file
->hname
;
411 from_file
= from_file
->prev
;
415 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
416 /* Performs secondary target name expansion and then renames
417 the file using rename_file. */
419 do_2nd_target_expansion (struct file
*f
)
422 char *tmp_name
= allocated_variable_expand_2 (
423 f
->name
, strcache2_get_len (&file_strcache
, f
->name
), &len
);
424 const char *name
= strcache_add_len (tmp_name
, len
);
426 rename_file (f
, name
);
428 #endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
430 /* Remove all nonprecious intermediate files.
431 If SIG is nonzero, this was caused by a fatal signal,
432 meaning that a different message will be printed, and
433 the message will go to stderr rather than stdout. */
436 remove_intermediates (int sig
)
438 struct file
**file_slot
;
439 struct file
**file_end
;
442 /* If there's no way we will ever remove anything anyway, punt early. */
443 if (question_flag
|| touch_flag
|| all_secondary
)
446 if (sig
&& just_print_flag
)
449 file_slot
= (struct file
**) files
.ht_vec
;
450 file_end
= file_slot
+ files
.ht_size
;
451 for ( ; file_slot
< file_end
; file_slot
++)
452 if (! HASH_VACANT (*file_slot
))
454 struct file
*f
= *file_slot
;
455 /* Is this file eligible for automatic deletion?
456 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
457 given on the command line, and it's either a -include makefile or
458 it's not precious. */
459 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
460 && !f
->secondary
&& !f
->cmd_target
)
463 if (f
->update_status
== -1)
464 /* If nothing would have created this file yet,
465 don't print an "rm" command for it. */
471 status
= unlink (f
->name
);
472 if (status
< 0 && errno
== ENOENT
)
478 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
482 DB (DB_BASIC
, (_("Removing intermediate files...\n")));
487 fputs ("rm ", stdout
);
492 fputs (f
->name
, stdout
);
497 perror_with_name ("unlink: ", f
->name
);
510 parse_prereqs (char *p
)
512 #ifndef CONFIG_WITH_ALLOC_CACHES
513 struct dep
*new = (struct dep
*)
514 multi_glob (parse_file_seq (&p
, '|', sizeof (struct dep
), 1),
515 sizeof (struct dep
));
517 struct dep
*new = (struct dep
*)
518 multi_glob (parse_file_seq (&p
, '|', &dep_cache
, 1), &dep_cache
);
523 /* Files that follow '|' are "order-only" prerequisites that satisfy the
524 dependency by existing: their modification times are irrelevant. */
528 #ifndef CONFIG_WITH_ALLOC_CACHES
530 multi_glob (parse_file_seq (&p
, '\0', sizeof (struct dep
), 1),
531 sizeof (struct dep
));
534 multi_glob (parse_file_seq (&p
, '\0', &dep_cache
, 1), &dep_cache
);
542 for (dp
= new; dp
->next
!= NULL
; dp
= dp
->next
)
547 for (; ood
!= NULL
; ood
= ood
->next
)
548 ood
->ignore_mtime
= 1;
554 /* Set the intermediate flag. */
557 set_intermediate (const void *item
)
559 struct file
*f
= (struct file
*) item
;
563 /* Expand and parse each dependency line. */
565 expand_deps (struct file
*f
)
568 struct dep
*old
= f
->deps
;
569 const char *file_stem
= f
->stem
;
570 unsigned int last_dep_has_cmds
= f
->updating
;
576 for (d
= old
; d
!= 0; d
= d
->next
)
578 struct dep
*new, *d1
;
580 #ifdef CONFIG_WITH_STRCACHE2
587 #ifdef CONFIG_WITH_INCLUDEDEP
588 /* Dependencies loaded by includedep are ready for use and we skip
589 the expensive parsing and globbing for them. To avoid wasting
590 lots of time walking the f->deps chain, we will advance D and
591 process all subsequent includedep records. */
595 new = d1
= alloc_dep();
596 d1
->staticpattern
= 0;
597 d1
->need_2nd_expansion
= 0;
599 d1
->file
= lookup_file (d
->name
);
601 d1
->file
= enter_file (d
->name
);
603 while (d
->next
&& d
->next
->includedep
)
606 d1
= d1
->next
= alloc_dep();
607 d1
->staticpattern
= 0;
608 d1
->need_2nd_expansion
= 0;
610 d1
->file
= lookup_file (d
->name
);
612 d1
->file
= enter_file (d
->name
);
619 /* Create the dependency list.
620 If we're not doing 2nd expansion, then it's just the name. We will
621 still need to massage it though. */
622 if (! d
->need_2nd_expansion
)
624 p
= variable_expand ("");
625 #ifndef CONFIG_WITH_STRCACHE2
626 variable_buffer_output (p
, d
->name
, strlen (d
->name
) + 1);
628 len
= strcache2_get_len (&file_strcache
, d
->name
);
629 variable_buffer_output (p
, d
->name
, len
+ 1);
635 /* If it's from a static pattern rule, convert the patterns into
636 "$*" so they'll expand properly. */
637 if (d
->staticpattern
)
640 char *buffer
= variable_expand ("");
642 o
= subst_expand (buffer
, d
->name
, "%", "$*", 1, 2, 0);
643 buffer
= variable_buffer
; /* bird - variable_buffer may have been reallocated. */
645 d
->name
= strcache_add_len (variable_buffer
,
646 o
- variable_buffer
);
647 d
->staticpattern
= 0; /* Clear staticpattern so that we don't
648 re-expand %s below. */
651 /* We are going to do second expansion so initialize file variables
652 for the file. Since the stem for static pattern rules comes from
653 individual dep lines, we will temporarily set f->stem to d->stem.
657 initialize_file_variables (f
, 0);
664 #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
665 set_file_variables (f
, 0 /* real call, f->deps == 0 so we're ok. */);
667 set_file_variables (f
);
670 #if !defined (CONFIG_WITH_VALUE_LENGTH) || !defined (CONFIG_WITH_STRCACHE2)
671 p
= variable_expand_for_file (d
->name
, f
);
673 len
= strcache2_get_len (&file_strcache
, d
->name
);
674 p
= variable_expand_for_file_2 (NULL
, d
->name
, len
, f
, &len
);
681 /* Parse the prerequisites. */
682 new = parse_prereqs (p
);
684 /* If this dep list was from a static pattern rule, expand the %s. We
685 use patsubst_expand to translate the prerequisites' patterns into
686 plain prerequisite names. */
687 if (new && d
->staticpattern
)
689 const char *pattern
= "%";
690 char *buffer
= variable_expand ("");
691 struct dep
*dp
= new, *dl
= 0;
697 int nl
= strlen (dp
->name
) + 1;
698 char *nm
= alloca (nl
);
699 memcpy (nm
, dp
->name
, nl
);
700 percent
= find_percent (nm
);
701 #else /* KMK - don't make a stack copy unless it's actually required! */
702 unsigned int nl
= strcache2_get_len (&file_strcache
, dp
->name
);
704 percent
= memchr (dp
->name
, '%', nl
);
707 nm
= alloca (nl
+ 1);
708 memcpy (nm
, dp
->name
, nl
+ 1);
709 percent
= find_percent (nm
);
716 /* We have to handle empty stems specially, because that
717 would be equivalent to $(patsubst %,dp->name,) which
718 will always be empty. */
719 if (d
->stem
[0] == '\0')
721 memmove (percent
, percent
+1, strlen (percent
));
722 o
= variable_buffer_output (buffer
, nm
, strlen (nm
) + 1);
726 o
= patsubst_expand_pat (buffer
, d
->stem
, pattern
, nm
,
727 pattern
+1, percent
+1);
728 o
= variable_buffer_output (o
, "", 1); /* bird fix - patsubst_expand_pat doesn't terminate,
729 the if case does and strcache would appreciate it. */
731 buffer
= variable_buffer
; /* bird fix - variable_buffer may have been reallocated. */
734 /* If the name expanded to the empty string, ignore it. */
735 if (buffer
[0] == '\0')
739 dp
= new = new->next
;
741 dp
= dl
->next
= dp
->next
;
747 dp
->name
= strcache_add_len (buffer
, o
- buffer
- 1); /* bird fix - don't include the terminator. */
754 /* Enter them as files. */
755 for (d1
= new; d1
!= 0; d1
= d1
->next
)
757 d1
->file
= lookup_file (d1
->name
);
759 d1
->file
= enter_file (d1
->name
);
761 d1
->staticpattern
= 0;
762 d1
->need_2nd_expansion
= 0;
765 #ifdef CONFIG_WITH_INCLUDEDEP
769 /* Add newly parsed deps to f->deps. If this is the last dependency
770 line and this target has commands then put it in front so the
771 last dependency line (the one with commands) ends up being the
772 first. This is important because people expect $< to hold first
773 prerequisite from the rule with commands. If it is not the last
774 dependency line or the rule does not have commands then link it
775 at the end so it appears in makefile order. */
779 if (d
->next
== 0 && last_dep_has_cmds
)
782 for (d_ptr
= &new; *d_ptr
; d_ptr
= &(*d_ptr
)->next
)
791 for (d_ptr
= &f
->deps
; *d_ptr
; d_ptr
= &(*d_ptr
)->next
)
799 free_dep_chain (old
);
802 /* For each dependency of each file, make the `struct dep' point
803 at the appropriate `struct file' (which may have to be created).
805 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
806 and various other special targets. */
814 struct file
**file_slot_0
;
815 struct file
**file_slot
;
816 struct file
**file_end
;
818 /* Perform second expansion and enter each dependency name as a file. */
820 /* Expand .SUFFIXES first; it's dependencies are used for $$* calculation. */
821 for (f
= lookup_file (".SUFFIXES"); f
!= 0; f
= f
->prev
)
824 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
825 /* Perform 2nd target expansion on files which requires this. This will
826 be re-inserting (delete+insert) hash table entries so we have to use
828 if (second_target_expansion
)
830 # ifdef KMK /* turn on warnings here. */
831 int save
= warn_undefined_variables_flag
;
832 warn_undefined_variables_flag
= 1;
835 file_slot_0
= (struct file
**) hash_dump (&files
, 0, 0);
836 file_end
= file_slot_0
+ files
.ht_fill
;
837 for (file_slot
= file_slot_0
; file_slot
< file_end
; file_slot
++)
838 for (f
= *file_slot
; f
!= 0; f
= f
->prev
)
839 if (f
->need_2nd_target_expansion
)
840 do_2nd_target_expansion (f
);
844 warn_undefined_variables_flag
= save
;
847 /* Disable second target expansion now since we won't expand files
848 entered after this point. (Saves CPU cycles in enter_file()). */
849 second_target_expansion
= 0;
851 #endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
853 #ifdef CONFIG_WITH_INCLUDEDEP
854 /* Process any queued includedep files. Since includedep is supposed
855 to be *simple* stuff, we can do this after the second target expansion
856 and thereby save a little time. */
857 incdep_flush_and_term ();
858 #endif /* CONFIG_WITH_INCLUDEDEP */
860 /* For every target that's not .SUFFIXES, expand its dependencies.
861 We must use hash_dump (), because within this loop we might add new files
862 to the table, possibly causing an in-situ table expansion. */
863 file_slot_0
= (struct file
**) hash_dump (&files
, 0, 0);
864 file_end
= file_slot_0
+ files
.ht_fill
;
865 for (file_slot
= file_slot_0
; file_slot
< file_end
; file_slot
++)
866 for (f
= *file_slot
; f
!= 0; f
= f
->prev
)
867 #ifndef CONFIG_WITH_STRCACHE2
868 if (strcmp (f
->name
, ".SUFFIXES") != 0)
870 if (f
->name
!= suffixes_strcached
)
874 /* This is a HACK to work around the still broken test #9 in
875 features/double_colon. It produces the wrong result if the build is
876 parallel because of changed evaluation order. Just make these
877 problematic rules execute in single field till a proper fix is
880 for (file_slot
= file_slot_0
; file_slot
< file_end
; file_slot
++)
881 if ( (f
= *file_slot
) != 0
883 && ( f
->double_colon
!= f
885 for (f2
= f
->double_colon
; f2
!= 0; f2
= f2
->prev
)
886 f2
->command_flags
|= COMMANDS_NOTPARALLEL
;
890 /* Now manage all the special targets. */
892 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
893 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
894 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
897 for (f
= lookup_file (".LOW_RESOLUTION_TIME"); f
!= 0; f
= f
->prev
)
898 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
899 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
900 f2
->low_resolution_time
= 1;
902 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
903 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
904 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
906 /* Mark this file as phony nonexistent target. */
909 f2
->last_mtime
= NONEXISTENT_MTIME
;
910 f2
->mtime_before_update
= NONEXISTENT_MTIME
;
913 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
914 /* Mark .INTERMEDIATE deps as intermediate files. */
915 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
916 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
917 f2
->intermediate
= 1;
918 /* .INTERMEDIATE with no deps does nothing.
919 Marking all files as intermediates is useless since the goal targets
920 would be deleted after they are built. */
922 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
923 /* Mark .SECONDARY deps as both intermediate and secondary. */
925 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
926 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
927 f2
->intermediate
= f2
->secondary
= 1;
928 /* .SECONDARY with no deps listed marks *all* files that way. */
932 hash_map (&files
, set_intermediate
);
935 f
= lookup_file (".EXPORT_ALL_VARIABLES");
936 if (f
!= 0 && f
->is_target
)
937 export_all_variables
= 1;
939 f
= lookup_file (".IGNORE");
940 if (f
!= 0 && f
->is_target
)
943 ignore_errors_flag
= 1;
945 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
946 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
947 f2
->command_flags
|= COMMANDS_NOERROR
;
950 f
= lookup_file (".SILENT");
951 if (f
!= 0 && f
->is_target
)
956 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
957 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
958 f2
->command_flags
|= COMMANDS_SILENT
;
961 f
= lookup_file (".NOTPARALLEL");
962 if (f
!= 0 && f
->is_target
)
963 #ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
965 #else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
969 DB (DB_KMK
, (_("not_parallel -1\n")));
973 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
974 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
975 f2
->command_flags
|= COMMANDS_NOTPARALLEL
;
977 #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
979 #ifndef NO_MINUS_C_MINUS_O
980 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
981 /* This needs more work: what if the user sets this in the makefile?
983 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
987 /* Remember that we've done this. */
991 /* Set the `command_state' member of FILE and all its `also_make's. */
994 set_command_state (struct file
*file
, enum cmd_state state
)
998 file
->command_state
= state
;
1000 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
1001 d
->file
->command_state
= state
;
1003 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1004 if (file
->multi_head
)
1005 for (file
= file
->multi_head
; file
!= 0; file
= file
->multi_next
)
1006 file
->command_state
= state
;
1010 /* Convert an external file timestamp to internal form. */
1013 file_timestamp_cons (const char *fname
, time_t s
, int ns
)
1015 int offset
= ORDINARY_MTIME_MIN
+ (FILE_TIMESTAMP_HI_RES
? ns
: 0);
1016 FILE_TIMESTAMP product
= (FILE_TIMESTAMP
) s
<< FILE_TIMESTAMP_LO_BITS
;
1017 FILE_TIMESTAMP ts
= product
+ offset
;
1019 if (! (s
<= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX
)
1020 && product
<= ts
&& ts
<= ORDINARY_MTIME_MAX
))
1022 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
1023 ts
= s
<= OLD_MTIME
? ORDINARY_MTIME_MIN
: ORDINARY_MTIME_MAX
;
1024 file_timestamp_sprintf (buf
, ts
);
1025 error (NILF
, _("%s: Timestamp out of range; substituting %s"),
1026 fname
? fname
: _("Current time"), buf
);
1032 /* Return the current time as a file timestamp, setting *RESOLUTION to
1035 file_timestamp_now (int *resolution
)
1041 /* Don't bother with high-resolution clocks if file timestamps have
1042 only one-second resolution. The code below should work, but it's
1043 not worth the hassle of debugging it on hosts where it fails. */
1044 #if FILE_TIMESTAMP_HI_RES
1045 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
1047 struct timespec timespec
;
1048 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
1051 s
= timespec
.tv_sec
;
1052 ns
= timespec
.tv_nsec
;
1057 # if HAVE_GETTIMEOFDAY
1059 struct timeval timeval
;
1060 if (gettimeofday (&timeval
, 0) == 0)
1064 ns
= timeval
.tv_usec
* 1000;
1072 s
= time ((time_t *) 0);
1075 #if FILE_TIMESTAMP_HI_RES
1079 return file_timestamp_cons (0, s
, ns
);
1082 /* Place into the buffer P a printable representation of the file
1085 file_timestamp_sprintf (char *p
, FILE_TIMESTAMP ts
)
1087 time_t t
= FILE_TIMESTAMP_S (ts
);
1088 struct tm
*tm
= localtime (&t
);
1091 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
1092 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
1093 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1095 sprintf (p
, "%ld", (long) t
);
1097 sprintf (p
, "%lu", (unsigned long) t
);
1100 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
1101 know the actual timestamp resolution, since clock_getres applies only to
1102 local times, whereas this timestamp might come from a remote filesystem.
1103 So removing trailing zeros is the best guess that we can do. */
1104 sprintf (p
, ".%09d", FILE_TIMESTAMP_NS (ts
));
1105 p
+= strlen (p
) - 1;
1113 /* Print the data base of files. */
1116 print_file (const void *item
)
1118 const struct file
*f
= item
;
1120 struct dep
*ood
= 0;
1124 puts (_("# Not a target:"));
1125 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1128 const struct file
*f2
;
1129 if (f
->multi_head
== f
)
1131 int multi_maybe
= -1;
1132 assert (!f
->multi_maybe
);
1133 assert (!f
->double_colon
);
1135 printf ("%s", f
->name
);
1136 for (f2
= f
->multi_next
; f2
!= 0; f2
= f2
->multi_next
)
1138 printf (" %s%s", f2
->multi_maybe
!= multi_maybe
1139 ? f2
->multi_maybe
? "+| " : "+ " : "",
1141 multi_maybe
= f2
->multi_maybe
;
1146 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
1150 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
1152 /* Print all normal dependencies; note any order-only deps. */
1153 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
1154 if (! d
->ignore_mtime
)
1155 printf (" %s", dep_name (d
));
1159 /* Print order-only deps, if we have any. */
1162 printf (" | %s", dep_name (ood
));
1163 for (d
= ood
->next
; d
!= 0; d
= d
->next
)
1164 if (d
->ignore_mtime
)
1165 printf (" %s", dep_name (d
));
1170 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1171 if (f
->multi_head
&& f
->multi_head
!= f
)
1173 const struct file
*f2
;
1174 fputs (_("# In multi target list:"), stdout
);
1175 for (f2
= f
->multi_head
; f2
!= 0; f2
= f2
->multi_next
)
1176 printf (" %s%s", f2
->name
, f
== f2
? "(*)" : "");
1179 puts (_("# File is an optional multi target member."));
1184 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1186 puts (_("# Phony target (prerequisite of .PHONY)."));
1188 puts (_("# Command line target."));
1190 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1191 puts (f
->tried_implicit
1192 ? _("# Implicit rule search has been done.")
1193 : _("# Implicit rule search has not been done."));
1195 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
1196 if (f
->intermediate
)
1197 puts (_("# File is an intermediate prerequisite."));
1198 if (f
->also_make
!= 0)
1200 fputs (_("# Also makes:"), stdout
);
1201 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
1202 printf (" %s", dep_name (d
));
1205 if (f
->last_mtime
== UNKNOWN_MTIME
)
1206 puts (_("# Modification time never checked."));
1207 else if (f
->last_mtime
== NONEXISTENT_MTIME
)
1208 puts (_("# File does not exist."));
1209 else if (f
->last_mtime
== OLD_MTIME
)
1210 puts (_("# File is very old."));
1213 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
1214 file_timestamp_sprintf (buf
, f
->last_mtime
);
1215 printf (_("# Last modified %s\n"), buf
);
1218 ? _("# File has been updated.") : _("# File has not been updated."));
1219 switch (f
->command_state
)
1222 puts (_("# Recipe currently running (THIS IS A BUG)."));
1224 case cs_deps_running
:
1225 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
1227 case cs_not_started
:
1229 switch (f
->update_status
)
1234 puts (_("# Successfully updated."));
1237 assert (question_flag
);
1238 puts (_("# Needs to be updated (-q is set)."));
1241 puts (_("# Failed to be updated."));
1244 puts (_("# Invalid value in `update_status' member!"));
1251 puts (_("# Invalid value in `command_state' member!"));
1257 if (f
->variables
!= 0)
1258 print_file_variables (f
);
1261 print_commands (f
->cmds
);
1264 print_file ((const void *) f
->prev
);
1268 print_file_data_base (void)
1270 puts (_("\n# Files"));
1272 hash_map (&files
, print_file
);
1274 fputs (_("\n# files hash-table stats:\n# "), stdout
);
1275 hash_print_stats (&files
, stdout
);
1278 #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
1280 print_file_stats (void)
1282 fputs (_("\n# files hash-table stats:\n# "), stdout
);
1283 hash_print_stats (&files
, stdout
);
1284 fputs ("\n", stdout
);
1288 /* Verify the integrity of the data base of files. */
1290 #define VERIFY_CACHED(_p,_n) \
1292 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1293 printf ("%s: Field %s not cached: %s\n", _p->name, # _n, _p->_n); \
1297 verify_file (const void *item
)
1299 const struct file
*f
= item
;
1300 const struct dep
*d
;
1302 VERIFY_CACHED (f
, name
);
1303 VERIFY_CACHED (f
, hname
);
1304 VERIFY_CACHED (f
, vpath
);
1305 VERIFY_CACHED (f
, stem
);
1307 /* Check the deps. */
1308 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
1310 VERIFY_CACHED (d
, name
);
1311 VERIFY_CACHED (d
, stem
);
1316 verify_file_data_base (void)
1318 hash_map (&files
, verify_file
);
1321 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1324 build_target_list (char *value
)
1326 static unsigned long last_targ_count
= 0;
1328 if (files
.ht_fill
!= last_targ_count
)
1330 unsigned long max
= EXPANSION_INCREMENT (strlen (value
));
1333 struct file
**fp
= (struct file
**) files
.ht_vec
;
1334 struct file
**end
= &fp
[files
.ht_size
];
1336 /* Make sure we have at least MAX bytes in the allocated buffer. */
1337 value
= xrealloc (value
, max
);
1341 for (; fp
< end
; ++fp
)
1342 if (!HASH_VACANT (*fp
) && (*fp
)->is_target
)
1344 struct file
*f
= *fp
;
1345 int l
= strlen (f
->name
);
1350 unsigned long off
= p
- value
;
1352 max
+= EXPANSION_INCREMENT (l
+ 1);
1353 value
= xrealloc (value
, max
);
1357 memcpy (p
, f
->name
, l
);
1363 last_targ_count
= files
.ht_fill
;
1370 init_hash_files (void)
1372 #ifndef CONFIG_WITH_STRCACHE2
1374 hash_init (&files
, /*65535*/ 32755, file_hash_1
, file_hash_2
, file_hash_cmp
);
1376 hash_init (&files
, 1000, file_hash_1
, file_hash_2
, file_hash_cmp
);
1378 #else /* CONFIG_WITH_STRCACHE2 */
1380 hash_init_strcached (&files
, 32755, &file_strcache
,
1381 offsetof (struct file
, hname
));
1383 hash_init_strcached (&files
, 1000, &file_strcache
,
1384 offsetof (struct file
, hname
));
1386 #endif /* CONFIG_WITH_STRCACHE2 */