1 /* Implicit rule searching 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/>. */
25 #include "job.h" /* struct child, used inside commands.h */
26 #include "commands.h" /* set_file_variables */
28 static int pattern_search (struct file
*file
, int archive
,
29 unsigned int depth
, unsigned int recursions
);
31 /* For a FILE which has no commands specified, try to figure out some
32 from the implicit pattern rules.
33 Returns 1 if a suitable implicit rule was found,
34 after modifying FILE to contain the appropriate commands and deps,
35 or returns 0 if no implicit rule was found. */
38 try_implicit_rule (struct file
*file
, unsigned int depth
)
40 DBF (DB_IMPLICIT
, _("Looking for an implicit rule for `%s'.\n"));
42 /* The order of these searches was previously reversed. My logic now is
43 that since the non-archive search uses more information in the target
44 (the archive search omits the archive name), it is more specific and
47 if (pattern_search (file
, 0, depth
, 0))
51 /* If this is an archive member reference, use just the
52 archive member name to search for implicit rules. */
53 if (ar_name (file
->name
))
56 _("Looking for archive-member implicit rule for `%s'.\n"));
57 if (pattern_search (file
, 1, depth
, 0))
66 #ifdef CONFIG_WITH_ALLOC_CACHES
67 struct alloccache idep_cache
;
70 /* Struct idep captures information about implicit prerequisites
71 that come from implicit rules. */
74 struct idep
*next
; /* struct dep -compatible interface */
75 const char *name
; /* name of the prerequisite */
76 struct file
*intermediate_file
; /* intermediate file, 0 otherwise */
77 const char *intermediate_pattern
; /* pattern for intermediate file */
78 unsigned char had_stem
; /* had % substituted with stem */
79 unsigned char ignore_mtime
; /* ignore_mtime flag */
83 free_idep_chain (struct idep
*p
)
90 #ifndef CONFIG_WITH_ALLOC_CACHES
93 alloccache_free (&idep_cache
, p
);
99 /* Scans the BUFFER for the next word with whitespace as a separator.
100 Returns the pointer to the beginning of the word. LENGTH hold the
101 length of the word. */
104 get_next_word (const char *buffer
, unsigned int *length
)
106 const char *p
= buffer
, *beg
;
109 /* Skip any leading whitespace. */
110 while (isblank ((unsigned char)*p
))
118 if (length
) /* bird: shut up gcc. */
124 /* We already found the first value of "c", above. */
142 /* This is a variable reference, so read it to the matching
150 /* This is a single-letter variable reference. */
153 for (count
= 0; *p
!= '\0'; ++p
)
157 else if (*p
== closeparen
&& --count
< 0)
184 /* Search the pattern rules for a rule with an existing dependency to make
185 FILE. If a rule is found, the appropriate commands and deps are put in FILE
186 and 1 is returned. If not, 0 is returned.
188 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
189 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
190 directory and filename parts.
192 If an intermediate file is found by pattern search, the intermediate file
193 is set up as a target by the recursive call and is also made a dependency
196 DEPTH is used for debugging messages. */
199 pattern_search (struct file
*file
, int archive
,
200 unsigned int depth
, unsigned int recursions
)
202 /* Filename we are searching for a rule for. */
203 const char *filename
= archive
? strchr (file
->name
, '(') : file
->name
;
205 /* Length of FILENAME. */
206 unsigned int namelen
= strlen (filename
);
208 /* The last slash in FILENAME (or nil if there is none). */
211 /* This is a file-object used as an argument in
212 recursive calls. It never contains any data
213 except during a recursive call. */
214 struct file
*intermediate_file
= 0;
216 /* This linked list records all the prerequisites actually
217 found for a rule along with some other useful information
218 (see struct idep for details). */
219 struct idep
* deps
= 0;
221 /* 1 if we need to remove explicit prerequisites, 0 otherwise. */
222 unsigned int remove_explicit_deps
= 0;
224 /* Names of possible dependencies are constructed in this buffer. */
225 char *depname
= alloca (namelen
+ max_pattern_dep_length
);
227 /* The start and length of the stem of FILENAME for the current rule. */
228 const char *stem
= 0;
229 unsigned int stemlen
= 0;
230 unsigned int fullstemlen
= 0;
232 /* Buffer in which we store all the rules that are possibly applicable. */
233 struct rule
**tryrules
= xmalloc (num_pattern_rules
* max_pattern_targets
234 * sizeof (struct rule
*));
236 /* Number of valid elements in TRYRULES. */
239 /* The numbers of the rule targets of each rule
240 in TRYRULES that matched the target file. */
241 unsigned int *matches
= alloca (num_pattern_rules
* sizeof (unsigned int));
243 /* Each element is nonzero if LASTSLASH was used in
244 matching the corresponding element of TRYRULES. */
245 char *checked_lastslash
= alloca (num_pattern_rules
* sizeof (char));
247 /* The index in TRYRULES of the rule we found. */
248 unsigned int foundrule
;
250 /* Nonzero if should consider intermediate files as dependencies. */
253 /* Nonzero if we have matched a pattern-rule target
254 that is not just `%'. */
255 int specific_rule_matched
= 0;
257 unsigned int ri
; /* uninit checks OK */
259 struct dep
*dep
, *expl_d
;
262 struct idep
**id_ptr
;
265 PATH_VAR (stem_str
); /* @@ Need to get rid of stem, stemlen, etc. */
267 #ifdef CONFIG_WITH_ALLOC_CACHES
268 if (!idep_cache
.size
)
269 alloccache_init (&idep_cache
, sizeof (struct idep
), "idep", NULL
, NULL
);
273 if (archive
|| ar_name (filename
))
278 /* Set LASTSLASH to point at the last slash in FILENAME
279 but not counting any slash at the end. (foo/bar/ counts as
280 bar/ in directory foo/, not empty in directory foo/bar/.) */
282 lastslash
= strrchr (filename
, ']');
284 lastslash
= strrchr (filename
, ':');
286 lastslash
= strrchr (filename
, '/');
287 #ifdef HAVE_DOS_PATHS
288 /* Handle backslashes (possibly mixed with forward slashes)
289 and the case of "d:file". */
291 char *bslash
= strrchr (filename
, '\\');
292 if (lastslash
== 0 || bslash
> lastslash
)
294 if (lastslash
== 0 && filename
[0] && filename
[1] == ':')
295 lastslash
= (char *)filename
+ 1;
299 if (lastslash
!= 0 && lastslash
[1] == '\0')
303 /* First see which pattern rules match this target
304 and may be considered. Put them in TRYRULES. */
307 for (rule
= pattern_rules
; rule
!= 0; rule
= rule
->next
)
311 /* If the pattern rule has deps but no commands, ignore it.
312 Users cancel built-in rules by redefining them without commands. */
313 if (rule
->deps
!= 0 && rule
->cmds
== 0)
316 /* If this rule is in use by a parent pattern_search,
317 don't use it here. */
320 DBS (DB_IMPLICIT
, (_("Avoiding implicit rule recursion.\n")));
324 for (ti
= 0; ti
< rule
->num
; ++ti
)
326 const char *target
= rule
->targets
[ti
];
327 const char *suffix
= rule
->suffixes
[ti
];
330 /* Rules that can match any filename and are not terminal
331 are ignored if we're recursing, so that they cannot be
332 intermediate files. */
333 if (recursions
> 0 && target
[1] == '\0' && !rule
->terminal
)
336 if (rule
->lens
[ti
] > namelen
)
337 /* It can't possibly match. */
340 /* From the lengths of the filename and the pattern parts,
341 find the stem: the part of the filename that matches the %. */
342 stem
= filename
+ (suffix
- target
- 1);
343 stemlen
= namelen
- rule
->lens
[ti
] + 1;
345 /* Set CHECK_LASTSLASH if FILENAME contains a directory
346 prefix and the target pattern does not contain a slash. */
352 check_lastslash
= (strchr (target
, ']') == 0
353 && strchr (target
, ':') == 0);
355 check_lastslash
= strchr (target
, '/') == 0;
356 #ifdef HAVE_DOS_PATHS
357 /* Didn't find it yet: check for DOS-type directories. */
360 char *b
= strchr (target
, '\\');
361 check_lastslash
= !(b
|| (target
[0] && target
[1] == ':'));
368 /* If so, don't include the directory prefix in STEM here. */
369 unsigned int difference
= lastslash
- filename
+ 1;
370 if (difference
> stemlen
)
372 stemlen
-= difference
;
376 /* Check that the rule pattern matches the text before the stem. */
379 if (stem
> (lastslash
+ 1)
380 && !strneq (target
, lastslash
+ 1, stem
- lastslash
- 1))
383 else if (stem
> filename
384 && !strneq (target
, filename
, stem
- filename
))
387 /* Check that the rule pattern matches the text after the stem.
388 We could test simply use streq, but this way we compare the
389 first two characters immediately. This saves time in the very
390 common case where the first character matches because it is a
392 if (*suffix
!= stem
[stemlen
]
393 || (*suffix
!= '\0' && !streq (&suffix
[1], &stem
[stemlen
+ 1])))
396 /* Record if we match a rule that not all filenames will match. */
397 if (target
[1] != '\0')
398 specific_rule_matched
= 1;
400 /* A rule with no dependencies and no commands exists solely to set
401 specific_rule_matched when it matches. Don't try to use it. */
402 if (rule
->deps
== 0 && rule
->cmds
== 0)
405 /* Record this rule in TRYRULES and the index of the matching
406 target in MATCHES. If several targets of the same rule match,
407 that rule will be in TRYRULES more than once. */
408 tryrules
[nrules
] = rule
;
409 matches
[nrules
] = ti
;
410 checked_lastslash
[nrules
] = check_lastslash
;
415 /* If we have found a matching rule that won't match all filenames,
416 retroactively reject any non-"terminal" rules that do always match. */
417 if (specific_rule_matched
)
418 for (ri
= 0; ri
< nrules
; ++ri
)
419 if (!tryrules
[ri
]->terminal
)
422 for (j
= 0; j
< tryrules
[ri
]->num
; ++j
)
423 if (tryrules
[ri
]->targets
[j
][1] == '\0')
430 /* We are going to do second expansion so initialize file variables
432 initialize_file_variables (file
, 0);
434 /* Try each rule once without intermediate files, then once with them. */
435 for (intermed_ok
= 0; intermed_ok
== !!intermed_ok
; ++intermed_ok
)
437 /* Try each pattern rule till we find one that applies.
438 If it does, expand its dependencies (as substituted)
439 and chain them in DEPS. */
441 for (ri
= 0; ri
< nrules
; ri
++)
444 unsigned int failed
= 0;
446 int file_variables_set
= 0;
450 remove_explicit_deps
= 0;
452 /* RULE is nil when we discover that a rule,
453 already placed in TRYRULES, should not be applied. */
457 /* Reject any terminal rules if we're
458 looking to make intermediate files. */
459 if (intermed_ok
&& rule
->terminal
)
462 /* Mark this rule as in use so a recursive
463 pattern_search won't try to use it. */
466 /* From the lengths of the filename and the matching pattern parts,
467 find the stem: the part of the filename that matches the %. */
469 + (rule
->suffixes
[matches
[ri
]] - rule
->targets
[matches
[ri
]]) - 1;
470 stemlen
= namelen
- rule
->lens
[matches
[ri
]] + 1;
471 check_lastslash
= checked_lastslash
[ri
];
474 stem
+= lastslash
- filename
+ 1;
475 stemlen
-= (lastslash
- filename
) + 1;
478 DBS (DB_IMPLICIT
, (_("Trying pattern rule with stem `%.*s'.\n"),
479 (int) stemlen
, stem
));
481 strncpy (stem_str
, stem
, stemlen
);
482 stem_str
[stemlen
] = '\0';
484 /* Temporary assign STEM to file->stem (needed to set file
486 file
->stem
= stem_str
;
488 /* Try each dependency; see if it "exists". */
490 for (dep
= rule
->deps
; dep
!= 0; dep
= dep
->next
)
495 unsigned int order_only
= 0; /* Set if '|' was seen. */
497 /* In an ideal world we would take the dependency line,
498 substitute the stem, re-expand the whole line and chop it
499 into individual prerequisites. Unfortunately this won't work
500 because of the "check_lastslash" twist. Instead, we will
501 have to go word by word, taking $()'s into account, for each
502 word we will substitute the stem, re-expand, chop it up, and,
503 if check_lastslash != 0, add the directory part to each
504 resulting prerequisite. */
506 p
= get_next_word (dep
->name
, &len
);
514 break; /* No more words */
516 /* Is there a pattern in this prerequisite? */
518 for (p2
= p
; p2
< p
+ len
&& *p2
!= '%'; ++p2
)
521 if (dep
->need_2nd_expansion
)
523 /* If the dependency name has %, substitute the stem.
525 Watch out, we are going to do something tricky
526 here. If we just replace % with the stem value,
527 later, when we do the second expansion, we will
528 re-expand this stem value once again. This is not
529 good especially if you have certain characters in
532 Instead, we will replace % with $* and allow the
533 second expansion to take care of it for us. This way
534 (since $* is a simple variable) there won't be
535 additional re-expansion of the stem. */
539 unsigned int i
= p2
- p
;
540 memcpy (depname
, p
, i
);
541 memcpy (depname
+ i
, "$*", 2);
542 memcpy (depname
+ i
+ 2, p2
+ 1, len
- i
- 1);
543 depname
[len
+ 2 - 1] = '\0';
552 memcpy (depname
, p
, len
);
556 /* Set file variables. Note that we cannot do it once
557 at the beginning of the function because of the stem
559 if (!file_variables_set
)
561 #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
562 set_file_variables (file
, 0 /* real call */);
564 set_file_variables (file
);
566 file_variables_set
= 1;
569 p2
= variable_expand_for_file (depname
, file
);
575 unsigned int i
= p2
- p
;
576 memcpy (depname
, p
, i
);
577 memcpy (depname
+ i
, stem_str
, stemlen
);
578 memcpy (depname
+ i
+ stemlen
, p2
+ 1, len
- i
- 1);
579 depname
[len
+ stemlen
- 1] = '\0';
588 memcpy (depname
, p
, len
);
595 /* Parse the dependencies. */
601 for (; *id_ptr
; id_ptr
= &(*id_ptr
)->next
)
604 #ifndef CONFIG_WITH_ALLOC_CACHES
605 *id_ptr
= (struct idep
*)
608 order_only
? '\0' : '|',
609 sizeof (struct idep
),
610 1), sizeof (struct idep
));
612 *id_ptr
= (struct idep
*)
615 order_only
? '\0' : '|',
620 /* @@ It would be nice to teach parse_file_seq or
621 multi_glob to add prefix. This would save us some
624 if (order_only
|| add_dir
|| had_stem
)
626 unsigned long l
= lastslash
- filename
+ 1;
628 for (d
= *id_ptr
; d
!= 0; d
= d
->next
)
635 char *n
= alloca (strlen (d
->name
) + l
+ 1);
636 memcpy (n
, filename
, l
);
637 memcpy (n
+l
, d
->name
, strlen (d
->name
) + 1);
638 d
->name
= strcache_add (n
);
646 if (!order_only
&& *p2
)
657 p
= get_next_word (p
, &len
);
661 /* Reset the stem in FILE. */
665 /* @@ This loop can be combined with the previous one. I do
666 it separately for now for transparency.*/
668 for (d
= deps
; d
!= 0; d
= d
->next
)
670 const char *name
= d
->name
;
672 if (file_impossible_p (name
))
674 /* If this dependency has already been ruled "impossible",
675 then the rule fails and don't bother trying it on the
676 second pass either since we know that will fail too. */
679 ? _("Rejecting impossible implicit prerequisite `%s'.\n")
680 : _("Rejecting impossible rule prerequisite `%s'.\n"),
690 ? _("Trying implicit prerequisite `%s'.\n")
691 : _("Trying rule prerequisite `%s'.\n"), name
));
693 /* If this prerequisite also happened to be explicitly mentioned
694 for FILE skip all the test below since it it has to be built
695 anyway, no matter which implicit rule we choose. */
697 for (expl_d
= file
->deps
; expl_d
!= 0; expl_d
= expl_d
->next
)
698 if (streq (dep_name (expl_d
), name
))
703 /* The DEP->changed flag says that this dependency resides in a
704 nonexistent directory. So we normally can skip looking for
705 the file. However, if CHECK_LASTSLASH is set, then the
706 dependency file we are actually looking for is in a different
707 directory (the one gotten by prepending FILENAME's directory),
708 so it might actually exist. */
710 /* @@ dep->changed check is disabled. */
711 if (((f
= lookup_file (name
)) != 0 && f
->is_target
)
712 /*|| ((!dep->changed || check_lastslash) && */
713 || file_exists_p (name
))
716 /* This code, given FILENAME = "lib/foo.o", dependency name
717 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
719 const char *vname
= vpath_search (name
, 0);
723 (_("Found prerequisite `%s' as VPATH `%s'\n"),
730 /* We could not find the file in any place we should look. Try
731 to make this dependency as an intermediate file, but only on
736 if (intermediate_file
== 0)
737 intermediate_file
= alloca (sizeof (struct file
));
740 (_("Looking for a rule with intermediate file `%s'.\n"),
743 memset (intermediate_file
, '\0', sizeof (struct file
));
744 intermediate_file
->name
= name
;
745 if (pattern_search (intermediate_file
,
750 d
->intermediate_pattern
= intermediate_file
->name
;
751 intermediate_file
->name
= strcache_add (name
);
752 d
->intermediate_file
= intermediate_file
;
753 intermediate_file
= 0;
758 /* If we have tried to find P as an intermediate
759 file and failed, mark that name as impossible
760 so we won't go through the search again later. */
761 if (intermediate_file
->variables
)
762 free_variable_set (intermediate_file
->variables
);
763 file_impossible (name
);
766 /* A dependency of this rule does not exist. Therefore,
772 /* This rule is no longer `in use' for recursive searches. */
777 /* This pattern rule does not apply. If some of its
778 dependencies succeeded, free the data structure
780 free_idep_chain (deps
);
784 /* This pattern rule does apply. Stop looking for one. */
788 /* If we found an applicable rule without
789 intermediate files, don't try with them. */
796 /* RULE is nil if the loop went all the way
797 through the list and everything failed. */
803 /* If we are recursing, store the pattern that matched
804 FILENAME in FILE->name for use in upper levels. */
808 file
->name
= rule
->targets
[matches
[foundrule
]];
810 /* FOUND_FILES lists the dependencies for the rule we found.
811 This includes the intermediate files, if any.
812 Convert them into entries on the deps-chain of FILE. */
814 if (remove_explicit_deps
)
816 /* Remove all the dependencies that didn't come from
817 this implicit rule. */
822 struct dep
*next
= dep
->next
;
829 expl_d
= file
->deps
; /* We will add them at the end. */
832 for (d
= deps
; d
!= 0; d
= d
->next
)
836 if (d
->intermediate_file
!= 0)
838 /* If we need to use an intermediate file,
839 make sure it is entered as a target, with the info that was
840 found for it in the recursive pattern_search call.
841 We know that the intermediate file did not already exist as
842 a target; therefore we can assume that the deps and cmds
843 of F below are null before we change them. */
845 struct file
*imf
= d
->intermediate_file
;
846 register struct file
*f
= lookup_file (imf
->name
);
848 /* We don't want to delete an intermediate file that happened
849 to be a prerequisite of some (other) target. Mark it as
854 f
= enter_file (strcache_add (imf
->name
));
859 f
->also_make
= imf
->also_make
;
864 imf
= lookup_file (d
->intermediate_pattern
);
865 if (imf
!= 0 && imf
->precious
)
870 f
->tried_implicit
= 1;
871 for (dep
= f
->deps
; dep
!= 0; dep
= dep
->next
)
873 dep
->file
= enter_file (dep
->name
);
875 dep
->file
->tried_implicit
|= dep
->changed
;
880 dep
->ignore_mtime
= d
->ignore_mtime
;
881 s
= d
->name
; /* Hijacking the name. */
885 dep
->file
= lookup_file (s
);
887 dep
->file
= enter_file (s
);
892 if (d
->intermediate_file
== 0 && tryrules
[foundrule
]->terminal
)
894 /* If the file actually existed (was not an intermediate file),
895 and the rule that found it was a terminal one, then we want
896 to mark the found file so that it will not have implicit rule
897 search done for it. If we are not entering a `struct file' for
898 it now, we indicate this with the `changed' flag. */
902 dep
->file
->tried_implicit
= 1;
911 if (!checked_lastslash
[foundrule
])
913 /* Always allocate new storage, since STEM might be
914 on the stack for an intermediate file. */
915 file
->stem
= strcache_add_len (stem
, stemlen
);
916 fullstemlen
= stemlen
;
920 int dirlen
= (lastslash
+ 1) - filename
;
923 /* We want to prepend the directory from
924 the original FILENAME onto the stem. */
925 fullstemlen
= dirlen
+ stemlen
;
926 sp
= alloca (fullstemlen
+ 1);
927 memcpy (sp
, filename
, dirlen
);
928 memcpy (sp
+ dirlen
, stem
, stemlen
);
929 sp
[fullstemlen
] = '\0';
930 #ifndef CONFIG_WITH_VALUE_LENGTH
931 file
->stem
= strcache_add (sp
);
933 file
->stem
= strcache_add_len (sp
, fullstemlen
);
937 file
->cmds
= rule
->cmds
;
940 /* Set precious flag. */
942 struct file
*f
= lookup_file (rule
->targets
[matches
[foundrule
]]);
943 if (f
&& f
->precious
)
947 /* If this rule builds other targets, too, put the others into FILE's
948 `also_make' member. */
951 for (ri
= 0; ri
< rule
->num
; ++ri
)
952 if (ri
!= matches
[foundrule
])
954 char *p
= alloca (rule
->lens
[ri
] + fullstemlen
+ 1);
956 struct dep
*new = alloc_dep ();
958 /* GKM FIMXE: handle '|' here too */
959 memcpy (p
, rule
->targets
[ri
],
960 rule
->suffixes
[ri
] - rule
->targets
[ri
] - 1);
961 p
+= rule
->suffixes
[ri
] - rule
->targets
[ri
] - 1;
962 memcpy (p
, file
->stem
, fullstemlen
);
964 memcpy (p
, rule
->suffixes
[ri
],
965 rule
->lens
[ri
] - (rule
->suffixes
[ri
] - rule
->targets
[ri
])+1);
966 new->name
= strcache_add (p
);
967 new->file
= enter_file (new->name
);
968 new->next
= file
->also_make
;
970 /* Set precious flag. */
971 f
= lookup_file (rule
->targets
[ri
]);
972 if (f
&& f
->precious
)
973 new->file
->precious
= 1;
975 /* Set the is_target flag so that this file is not treated
976 as intermediate by the pattern rule search algorithm and
977 file_exists_p cannot pick it up yet. */
978 new->file
->is_target
= 1;
980 file
->also_make
= new;
984 free_idep_chain (deps
);