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 /* Struct idep captures information about implicit prerequisites
67 that come from implicit rules. */
70 struct idep
*next
; /* struct dep -compatible interface */
71 const char *name
; /* name of the prerequisite */
72 struct file
*intermediate_file
; /* intermediate file, 0 otherwise */
73 const char *intermediate_pattern
; /* pattern for intermediate file */
74 unsigned char had_stem
; /* had % substituted with stem */
75 unsigned char ignore_mtime
; /* ignore_mtime flag */
79 free_idep_chain (struct idep
*p
)
91 /* Scans the BUFFER for the next word with whitespace as a separator.
92 Returns the pointer to the beginning of the word. LENGTH hold the
93 length of the word. */
96 get_next_word (const char *buffer
, unsigned int *length
)
98 const char *p
= buffer
, *beg
;
101 /* Skip any leading whitespace. */
102 while (isblank ((unsigned char)*p
))
112 /* We already found the first value of "c", above. */
130 /* This is a variable reference, so read it to the matching
138 /* This is a single-letter variable reference. */
141 for (count
= 0; *p
!= '\0'; ++p
)
145 else if (*p
== closeparen
&& --count
< 0)
172 /* Search the pattern rules for a rule with an existing dependency to make
173 FILE. If a rule is found, the appropriate commands and deps are put in FILE
174 and 1 is returned. If not, 0 is returned.
176 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
177 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
178 directory and filename parts.
180 If an intermediate file is found by pattern search, the intermediate file
181 is set up as a target by the recursive call and is also made a dependency
184 DEPTH is used for debugging messages. */
187 pattern_search (struct file
*file
, int archive
,
188 unsigned int depth
, unsigned int recursions
)
190 /* Filename we are searching for a rule for. */
191 const char *filename
= archive
? strchr (file
->name
, '(') : file
->name
;
193 /* Length of FILENAME. */
194 unsigned int namelen
= strlen (filename
);
196 /* The last slash in FILENAME (or nil if there is none). */
199 /* This is a file-object used as an argument in
200 recursive calls. It never contains any data
201 except during a recursive call. */
202 struct file
*intermediate_file
= 0;
204 /* This linked list records all the prerequisites actually
205 found for a rule along with some other useful information
206 (see struct idep for details). */
207 struct idep
* deps
= 0;
209 /* 1 if we need to remove explicit prerequisites, 0 otherwise. */
210 unsigned int remove_explicit_deps
= 0;
212 /* Names of possible dependencies are constructed in this buffer. */
213 char *depname
= alloca (namelen
+ max_pattern_dep_length
);
215 /* The start and length of the stem of FILENAME for the current rule. */
216 const char *stem
= 0;
217 unsigned int stemlen
= 0;
218 unsigned int fullstemlen
= 0;
220 /* Buffer in which we store all the rules that are possibly applicable. */
221 struct rule
**tryrules
= xmalloc (num_pattern_rules
* max_pattern_targets
222 * sizeof (struct rule
*));
224 /* Number of valid elements in TRYRULES. */
227 /* The numbers of the rule targets of each rule
228 in TRYRULES that matched the target file. */
229 unsigned int *matches
= alloca (num_pattern_rules
* sizeof (unsigned int));
231 /* Each element is nonzero if LASTSLASH was used in
232 matching the corresponding element of TRYRULES. */
233 char *checked_lastslash
= alloca (num_pattern_rules
* sizeof (char));
235 /* The index in TRYRULES of the rule we found. */
236 unsigned int foundrule
;
238 /* Nonzero if should consider intermediate files as dependencies. */
241 /* Nonzero if we have matched a pattern-rule target
242 that is not just `%'. */
243 int specific_rule_matched
= 0;
245 unsigned int ri
; /* uninit checks OK */
247 struct dep
*dep
, *expl_d
;
250 struct idep
**id_ptr
;
253 PATH_VAR (stem_str
); /* @@ Need to get rid of stem, stemlen, etc. */
256 if (archive
|| ar_name (filename
))
261 /* Set LASTSLASH to point at the last slash in FILENAME
262 but not counting any slash at the end. (foo/bar/ counts as
263 bar/ in directory foo/, not empty in directory foo/bar/.) */
265 lastslash
= strrchr (filename
, ']');
267 lastslash
= strrchr (filename
, ':');
269 lastslash
= strrchr (filename
, '/');
270 #ifdef HAVE_DOS_PATHS
271 /* Handle backslashes (possibly mixed with forward slashes)
272 and the case of "d:file". */
274 char *bslash
= strrchr (filename
, '\\');
275 if (lastslash
== 0 || bslash
> lastslash
)
277 if (lastslash
== 0 && filename
[0] && filename
[1] == ':')
278 lastslash
= filename
+ 1;
282 if (lastslash
!= 0 && lastslash
[1] == '\0')
286 /* First see which pattern rules match this target
287 and may be considered. Put them in TRYRULES. */
290 for (rule
= pattern_rules
; rule
!= 0; rule
= rule
->next
)
294 /* If the pattern rule has deps but no commands, ignore it.
295 Users cancel built-in rules by redefining them without commands. */
296 if (rule
->deps
!= 0 && rule
->cmds
== 0)
299 /* If this rule is in use by a parent pattern_search,
300 don't use it here. */
303 DBS (DB_IMPLICIT
, (_("Avoiding implicit rule recursion.\n")));
307 for (ti
= 0; ti
< rule
->num
; ++ti
)
309 const char *target
= rule
->targets
[ti
];
310 const char *suffix
= rule
->suffixes
[ti
];
313 /* Rules that can match any filename and are not terminal
314 are ignored if we're recursing, so that they cannot be
315 intermediate files. */
316 if (recursions
> 0 && target
[1] == '\0' && !rule
->terminal
)
319 if (rule
->lens
[ti
] > namelen
)
320 /* It can't possibly match. */
323 /* From the lengths of the filename and the pattern parts,
324 find the stem: the part of the filename that matches the %. */
325 stem
= filename
+ (suffix
- target
- 1);
326 stemlen
= namelen
- rule
->lens
[ti
] + 1;
328 /* Set CHECK_LASTSLASH if FILENAME contains a directory
329 prefix and the target pattern does not contain a slash. */
335 check_lastslash
= (strchr (target
, ']') == 0
336 && strchr (target
, ':') == 0);
338 check_lastslash
= strchr (target
, '/') == 0;
339 #ifdef HAVE_DOS_PATHS
340 /* Didn't find it yet: check for DOS-type directories. */
343 char *b
= strchr (target
, '\\');
344 check_lastslash
= !(b
|| (target
[0] && target
[1] == ':'));
351 /* If so, don't include the directory prefix in STEM here. */
352 unsigned int difference
= lastslash
- filename
+ 1;
353 if (difference
> stemlen
)
355 stemlen
-= difference
;
359 /* Check that the rule pattern matches the text before the stem. */
362 if (stem
> (lastslash
+ 1)
363 && !strneq (target
, lastslash
+ 1, stem
- lastslash
- 1))
366 else if (stem
> filename
367 && !strneq (target
, filename
, stem
- filename
))
370 /* Check that the rule pattern matches the text after the stem.
371 We could test simply use streq, but this way we compare the
372 first two characters immediately. This saves time in the very
373 common case where the first character matches because it is a
375 if (*suffix
!= stem
[stemlen
]
376 || (*suffix
!= '\0' && !streq (&suffix
[1], &stem
[stemlen
+ 1])))
379 /* Record if we match a rule that not all filenames will match. */
380 if (target
[1] != '\0')
381 specific_rule_matched
= 1;
383 /* A rule with no dependencies and no commands exists solely to set
384 specific_rule_matched when it matches. Don't try to use it. */
385 if (rule
->deps
== 0 && rule
->cmds
== 0)
388 /* Record this rule in TRYRULES and the index of the matching
389 target in MATCHES. If several targets of the same rule match,
390 that rule will be in TRYRULES more than once. */
391 tryrules
[nrules
] = rule
;
392 matches
[nrules
] = ti
;
393 checked_lastslash
[nrules
] = check_lastslash
;
398 /* If we have found a matching rule that won't match all filenames,
399 retroactively reject any non-"terminal" rules that do always match. */
400 if (specific_rule_matched
)
401 for (ri
= 0; ri
< nrules
; ++ri
)
402 if (!tryrules
[ri
]->terminal
)
405 for (j
= 0; j
< tryrules
[ri
]->num
; ++j
)
406 if (tryrules
[ri
]->targets
[j
][1] == '\0')
413 /* We are going to do second expansion so initialize file variables
415 initialize_file_variables (file
, 0);
417 /* Try each rule once without intermediate files, then once with them. */
418 for (intermed_ok
= 0; intermed_ok
== !!intermed_ok
; ++intermed_ok
)
420 /* Try each pattern rule till we find one that applies.
421 If it does, expand its dependencies (as substituted)
422 and chain them in DEPS. */
424 for (ri
= 0; ri
< nrules
; ri
++)
426 unsigned int failed
= 0;
428 int file_variables_set
= 0;
432 remove_explicit_deps
= 0;
434 /* RULE is nil when we discover that a rule,
435 already placed in TRYRULES, should not be applied. */
439 /* Reject any terminal rules if we're
440 looking to make intermediate files. */
441 if (intermed_ok
&& rule
->terminal
)
444 /* Mark this rule as in use so a recursive
445 pattern_search won't try to use it. */
448 /* From the lengths of the filename and the matching pattern parts,
449 find the stem: the part of the filename that matches the %. */
451 + (rule
->suffixes
[matches
[ri
]] - rule
->targets
[matches
[ri
]]) - 1;
452 stemlen
= namelen
- rule
->lens
[matches
[ri
]] + 1;
453 check_lastslash
= checked_lastslash
[ri
];
456 stem
+= lastslash
- filename
+ 1;
457 stemlen
-= (lastslash
- filename
) + 1;
460 DBS (DB_IMPLICIT
, (_("Trying pattern rule with stem `%.*s'.\n"),
461 (int) stemlen
, stem
));
463 strncpy (stem_str
, stem
, stemlen
);
464 stem_str
[stemlen
] = '\0';
466 /* Temporary assign STEM to file->stem (needed to set file
468 file
->stem
= stem_str
;
470 /* Try each dependency; see if it "exists". */
472 for (dep
= rule
->deps
; dep
!= 0; dep
= dep
->next
)
477 unsigned int order_only
= 0; /* Set if '|' was seen. */
479 /* In an ideal world we would take the dependency line,
480 substitute the stem, re-expand the whole line and chop it
481 into individual prerequisites. Unfortunately this won't work
482 because of the "check_lastslash" twist. Instead, we will
483 have to go word by word, taking $()'s into account, for each
484 word we will substitute the stem, re-expand, chop it up, and,
485 if check_lastslash != 0, add the directory part to each
486 resulting prerequisite. */
488 p
= get_next_word (dep
->name
, &len
);
492 const char *dir
= NULL
;
497 break; /* No more words */
499 /* Is there a pattern in this prerequisite? */
501 for (p2
= p
; p2
< p
+ len
&& *p2
!= '%'; ++p2
)
504 if (dep
->need_2nd_expansion
)
506 /* If the dependency name has %, substitute the stem.
508 Watch out, we are going to do something tricky
509 here. If we just replace % with the stem value,
510 later, when we do the second expansion, we will
511 re-expand this stem value once again. This is not
512 good especially if you have certain characters in
515 Instead, we will replace % with $* and allow the
516 second expansion to take care of it for us. This way
517 (since $* is a simple variable) there won't be
518 additional re-expansion of the stem. */
522 unsigned int i
= p2
- p
;
523 memcpy (depname
, p
, i
);
524 memcpy (depname
+ i
, "$*", 2);
525 memcpy (depname
+ i
+ 2, p2
+ 1, len
- i
- 1);
526 depname
[len
+ 2 - 1] = '\0';
535 memcpy (depname
, p
, len
);
539 /* Set file variables. Note that we cannot do it once
540 at the beginning of the function because of the stem
542 if (!file_variables_set
)
544 set_file_variables (file
);
545 file_variables_set
= 1;
548 p2
= variable_expand_for_file (depname
, file
);
554 unsigned int i
= p2
- p
;
555 memcpy (depname
, p
, i
);
556 memcpy (depname
+ i
, stem_str
, stemlen
);
557 memcpy (depname
+ i
+ stemlen
, p2
+ 1, len
- i
- 1);
558 depname
[len
+ stemlen
- 1] = '\0';
567 memcpy (depname
, p
, len
);
574 /* If we need to add the directory prefix set it up. */
577 unsigned long l
= lastslash
- filename
+ 1;
578 char *n
= alloca (l
+ 1);
579 memcpy (n
, filename
, l
);
584 /* Parse the dependencies. */
590 for (; *id_ptr
; id_ptr
= &(*id_ptr
)->next
)
593 *id_ptr
= (struct idep
*)
594 parse_file_seq (&p2
, sizeof (struct idep
),
595 order_only
? '\0' : '|', dir
, 0);
597 if (order_only
|| had_stem
)
599 for (d
= *id_ptr
; d
!= 0; d
= d
->next
)
609 if (!order_only
&& *p2
)
620 p
= get_next_word (p
, &len
);
624 /* Reset the stem in FILE. */
628 /* @@ This loop can be combined with the previous one. I do
629 it separately for now for transparency.*/
631 for (d
= deps
; d
!= 0; d
= d
->next
)
633 const char *name
= d
->name
;
635 if (file_impossible_p (name
))
637 /* If this dependency has already been ruled "impossible",
638 then the rule fails and don't bother trying it on the
639 second pass either since we know that will fail too. */
642 ? _("Rejecting impossible implicit prerequisite `%s'.\n")
643 : _("Rejecting impossible rule prerequisite `%s'.\n"),
653 ? _("Trying implicit prerequisite `%s'.\n")
654 : _("Trying rule prerequisite `%s'.\n"), name
));
656 /* If this prerequisite also happened to be explicitly mentioned
657 for FILE skip all the test below since it it has to be built
658 anyway, no matter which implicit rule we choose. */
660 for (expl_d
= file
->deps
; expl_d
!= 0; expl_d
= expl_d
->next
)
661 if (streq (dep_name (expl_d
), name
))
666 /* The DEP->changed flag says that this dependency resides in a
667 nonexistent directory. So we normally can skip looking for
668 the file. However, if CHECK_LASTSLASH is set, then the
669 dependency file we are actually looking for is in a different
670 directory (the one gotten by prepending FILENAME's directory),
671 so it might actually exist. */
673 /* @@ dep->changed check is disabled. */
674 if (lookup_file (name
) != 0
675 /*|| ((!dep->changed || check_lastslash) && */
676 || file_exists_p (name
))
679 /* This code, given FILENAME = "lib/foo.o", dependency name
680 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
682 const char *vname
= vpath_search (name
, 0);
686 (_("Found prerequisite `%s' as VPATH `%s'\n"),
693 /* We could not find the file in any place we should look. Try
694 to make this dependency as an intermediate file, but only on
699 if (intermediate_file
== 0)
700 intermediate_file
= alloca (sizeof (struct file
));
703 (_("Looking for a rule with intermediate file `%s'.\n"),
706 memset (intermediate_file
, '\0', sizeof (struct file
));
707 intermediate_file
->name
= name
;
708 if (pattern_search (intermediate_file
,
713 d
->intermediate_pattern
= intermediate_file
->name
;
714 intermediate_file
->name
= strcache_add (name
);
715 d
->intermediate_file
= intermediate_file
;
716 intermediate_file
= 0;
721 /* If we have tried to find P as an intermediate
722 file and failed, mark that name as impossible
723 so we won't go through the search again later. */
724 if (intermediate_file
->variables
)
725 free_variable_set (intermediate_file
->variables
);
726 file_impossible (name
);
729 /* A dependency of this rule does not exist. Therefore,
735 /* This rule is no longer `in use' for recursive searches. */
740 /* This pattern rule does not apply. If some of its
741 dependencies succeeded, free the data structure
743 free_idep_chain (deps
);
747 /* This pattern rule does apply. Stop looking for one. */
751 /* If we found an applicable rule without
752 intermediate files, don't try with them. */
759 /* RULE is nil if the loop went all the way
760 through the list and everything failed. */
766 /* If we are recursing, store the pattern that matched
767 FILENAME in FILE->name for use in upper levels. */
771 file
->name
= rule
->targets
[matches
[foundrule
]];
773 /* FOUND_FILES lists the dependencies for the rule we found.
774 This includes the intermediate files, if any.
775 Convert them into entries on the deps-chain of FILE. */
777 if (remove_explicit_deps
)
779 /* Remove all the dependencies that didn't come from
780 this implicit rule. */
785 struct dep
*next
= dep
->next
;
792 expl_d
= file
->deps
; /* We will add them at the end. */
795 for (d
= deps
; d
!= 0; d
= d
->next
)
799 if (d
->intermediate_file
!= 0)
801 /* If we need to use an intermediate file,
802 make sure it is entered as a target, with the info that was
803 found for it in the recursive pattern_search call.
804 We know that the intermediate file did not already exist as
805 a target; therefore we can assume that the deps and cmds
806 of F below are null before we change them. */
808 struct file
*imf
= d
->intermediate_file
;
809 register struct file
*f
= lookup_file (imf
->name
);
811 /* We don't want to delete an intermediate file that happened
812 to be a prerequisite of some (other) target. Mark it as
817 f
= enter_file (strcache_add (imf
->name
));
822 f
->also_make
= imf
->also_make
;
827 imf
= lookup_file (d
->intermediate_pattern
);
828 if (imf
!= 0 && imf
->precious
)
833 f
->tried_implicit
= 1;
834 for (dep
= f
->deps
; dep
!= 0; dep
= dep
->next
)
836 dep
->file
= enter_file (dep
->name
);
838 dep
->file
->tried_implicit
|= dep
->changed
;
843 dep
->ignore_mtime
= d
->ignore_mtime
;
844 s
= d
->name
; /* Hijacking the name. */
848 dep
->file
= lookup_file (s
);
850 dep
->file
= enter_file (s
);
855 if (d
->intermediate_file
== 0 && tryrules
[foundrule
]->terminal
)
857 /* If the file actually existed (was not an intermediate file),
858 and the rule that found it was a terminal one, then we want
859 to mark the found file so that it will not have implicit rule
860 search done for it. If we are not entering a `struct file' for
861 it now, we indicate this with the `changed' flag. */
865 dep
->file
->tried_implicit
= 1;
874 if (!checked_lastslash
[foundrule
])
876 /* Always allocate new storage, since STEM might be
877 on the stack for an intermediate file. */
878 file
->stem
= strcache_add_len (stem
, stemlen
);
879 fullstemlen
= stemlen
;
883 int dirlen
= (lastslash
+ 1) - filename
;
886 /* We want to prepend the directory from
887 the original FILENAME onto the stem. */
888 fullstemlen
= dirlen
+ stemlen
;
889 sp
= alloca (fullstemlen
+ 1);
890 memcpy (sp
, filename
, dirlen
);
891 memcpy (sp
+ dirlen
, stem
, stemlen
);
892 sp
[fullstemlen
] = '\0';
893 file
->stem
= strcache_add (sp
);
896 file
->cmds
= rule
->cmds
;
899 /* Set precious flag. */
901 struct file
*f
= lookup_file (rule
->targets
[matches
[foundrule
]]);
902 if (f
&& f
->precious
)
906 /* If this rule builds other targets, too, put the others into FILE's
907 `also_make' member. */
910 for (ri
= 0; ri
< rule
->num
; ++ri
)
911 if (ri
!= matches
[foundrule
])
913 char *nm
= alloca (rule
->lens
[ri
] + fullstemlen
+ 1);
916 struct dep
*new = alloc_dep ();
918 /* GKM FIMXE: handle '|' here too */
919 memcpy (p
, rule
->targets
[ri
],
920 rule
->suffixes
[ri
] - rule
->targets
[ri
] - 1);
921 p
+= rule
->suffixes
[ri
] - rule
->targets
[ri
] - 1;
922 memcpy (p
, file
->stem
, fullstemlen
);
924 memcpy (p
, rule
->suffixes
[ri
],
925 rule
->lens
[ri
] - (rule
->suffixes
[ri
] - rule
->targets
[ri
])+1);
926 new->name
= strcache_add (nm
);
927 new->file
= enter_file (new->name
);
928 new->next
= file
->also_make
;
930 /* Set precious flag. */
931 f
= lookup_file (rule
->targets
[ri
]);
932 if (f
&& f
->precious
)
933 new->file
->precious
= 1;
935 /* Set the is_target flag so that this file is not treated
936 as intermediate by the pattern rule search algorithm and
937 file_exists_p cannot pick it up yet. */
938 new->file
->is_target
= 1;
940 file
->also_make
= new;
944 free_idep_chain (deps
);