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, 2005, 2006 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 it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
24 #include "job.h" /* struct child, used inside commands.h */
25 #include "commands.h" /* set_file_variables */
28 pattern_search
PARAMS ((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 char *name
; /* name of the prerequisite */
72 struct file
*intermediate_file
; /* intermediate file, 0 otherwise */
73 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
)
81 register struct idep
* n
;
82 register struct file
*f
;
92 f
= p
->intermediate_file
;
96 || f
->stem
> f
->name
+ strlen (f
->name
)))
105 /* Scans the BUFFER for the next word with whitespace as a separator.
106 Returns the pointer to the beginning of the word. LENGTH hold the
107 length of the word. */
110 get_next_word (char *buffer
, unsigned int *length
)
112 char *p
= buffer
, *beg
;
115 /* Skip any leading whitespace. */
116 while (isblank ((unsigned char)*p
))
126 /* We already found the first value of "c", above. */
144 /* This is a variable reference, so read it to the matching
152 /* This is a single-letter variable reference. */
155 for (count
= 0; *p
!= '\0'; ++p
)
159 else if (*p
== closeparen
&& --count
< 0)
186 /* Search the pattern rules for a rule with an existing dependency to make
187 FILE. If a rule is found, the appropriate commands and deps are put in FILE
188 and 1 is returned. If not, 0 is returned.
190 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
191 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
192 directory and filename parts.
194 If an intermediate file is found by pattern search, the intermediate file
195 is set up as a target by the recursive call and is also made a dependency
198 DEPTH is used for debugging messages. */
201 pattern_search (struct file
*file
, int archive
,
202 unsigned int depth
, unsigned int recursions
)
204 /* Filename we are searching for a rule for. */
205 char *filename
= archive
? strchr (file
->name
, '(') : file
->name
;
207 /* Length of FILENAME. */
208 unsigned int namelen
= strlen (filename
);
210 /* The last slash in FILENAME (or nil if there is none). */
213 /* This is a file-object used as an argument in
214 recursive calls. It never contains any data
215 except during a recursive call. */
216 struct file
*intermediate_file
= 0;
218 /* This linked list records all the prerequisites actually
219 found for a rule along with some other useful information
220 (see struct idep for details). */
221 struct idep
* deps
= 0;
223 /* 1 if we need to remove explicit prerequisites, 0 otherwise. */
224 unsigned int remove_explicit_deps
= 0;
226 /* Names of possible dependencies are constructed in this buffer. */
227 register char *depname
= (char *) alloca (namelen
+ max_pattern_dep_length
);
229 /* The start and length of the stem of FILENAME for the current rule. */
230 register char *stem
= 0;
231 register unsigned int stemlen
= 0;
232 register unsigned int fullstemlen
= 0;
234 /* Buffer in which we store all the rules that are possibly applicable. */
235 struct rule
**tryrules
236 = (struct rule
**) xmalloc (num_pattern_rules
* max_pattern_targets
237 * sizeof (struct rule
*));
239 /* Number of valid elements in TRYRULES. */
242 /* The numbers of the rule targets of each rule
243 in TRYRULES that matched the target file. */
244 unsigned int *matches
245 = (unsigned int *) alloca (num_pattern_rules
* sizeof (unsigned int));
247 /* Each element is nonzero if LASTSLASH was used in
248 matching the corresponding element of TRYRULES. */
249 char *checked_lastslash
250 = (char *) alloca (num_pattern_rules
* sizeof (char));
252 /* The index in TRYRULES of the rule we found. */
253 unsigned int foundrule
;
255 /* Nonzero if should consider intermediate files as dependencies. */
258 /* Nonzero if we have matched a pattern-rule target
259 that is not just `%'. */
260 int specific_rule_matched
= 0;
262 unsigned int i
= 0; /* uninit checks OK */
264 struct dep
*dep
, *expl_d
;
269 struct idep
**id_ptr
;
272 PATH_VAR (stem_str
); /* @@ Need to get rid of stem, stemlen, etc. */
275 if (archive
|| ar_name (filename
))
280 /* Set LASTSLASH to point at the last slash in FILENAME
281 but not counting any slash at the end. (foo/bar/ counts as
282 bar/ in directory foo/, not empty in directory foo/bar/.) */
284 lastslash
= strrchr (filename
, ']');
286 lastslash
= strrchr (filename
, ':');
288 lastslash
= strrchr (filename
, '/');
289 #ifdef HAVE_DOS_PATHS
290 /* Handle backslashes (possibly mixed with forward slashes)
291 and the case of "d:file". */
293 char *bslash
= strrchr (filename
, '\\');
294 if (lastslash
== 0 || bslash
> lastslash
)
296 if (lastslash
== 0 && filename
[0] && filename
[1] == ':')
297 lastslash
= filename
+ 1;
301 if (lastslash
!= 0 && lastslash
[1] == '\0')
305 /* First see which pattern rules match this target
306 and may be considered. Put them in TRYRULES. */
309 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 (i
= 0; rule
->targets
[i
] != 0; ++i
)
326 char *target
= rule
->targets
[i
];
327 char *suffix
= rule
->suffixes
[i
];
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
[i
] > 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
[i
] + 1;
345 /* Set CHECK_LASTSLASH if FILENAME contains a directory
346 prefix and the target pattern does not contain a slash. */
349 check_lastslash
= lastslash
!= 0
350 && ((strchr (target
, ']') == 0)
351 && (strchr (target
, ':') == 0));
353 check_lastslash
= lastslash
!= 0 && strchr (target
, '/') == 0;
357 /* In that case, don't include the
358 directory prefix in STEM here. */
359 unsigned int difference
= lastslash
- filename
+ 1;
360 if (difference
> stemlen
)
362 stemlen
-= difference
;
366 /* Check that the rule pattern matches the text before the stem. */
369 if (stem
> (lastslash
+ 1)
370 && !strneq (target
, lastslash
+ 1, stem
- lastslash
- 1))
373 else if (stem
> filename
374 && !strneq (target
, filename
, stem
- filename
))
377 /* Check that the rule pattern matches the text after the stem.
378 We could test simply use streq, but this way we compare the
379 first two characters immediately. This saves time in the very
380 common case where the first character matches because it is a
382 if (*suffix
!= stem
[stemlen
]
383 || (*suffix
!= '\0' && !streq (&suffix
[1], &stem
[stemlen
+ 1])))
386 /* Record if we match a rule that not all filenames will match. */
387 if (target
[1] != '\0')
388 specific_rule_matched
= 1;
390 /* A rule with no dependencies and no commands exists solely to set
391 specific_rule_matched when it matches. Don't try to use it. */
392 if (rule
->deps
== 0 && rule
->cmds
== 0)
395 /* Record this rule in TRYRULES and the index of the matching
396 target in MATCHES. If several targets of the same rule match,
397 that rule will be in TRYRULES more than once. */
398 tryrules
[nrules
] = rule
;
400 checked_lastslash
[nrules
] = check_lastslash
;
405 /* If we have found a matching rule that won't match all filenames,
406 retroactively reject any non-"terminal" rules that do always match. */
407 if (specific_rule_matched
)
408 for (i
= 0; i
< nrules
; ++i
)
409 if (!tryrules
[i
]->terminal
)
411 register unsigned int j
;
412 for (j
= 0; tryrules
[i
]->targets
[j
] != 0; ++j
)
413 if (tryrules
[i
]->targets
[j
][1] == '\0')
415 if (tryrules
[i
]->targets
[j
] != 0)
419 /* We are going to do second expansion so initialize file variables
421 initialize_file_variables (file
, 0);
423 /* Try each rule once without intermediate files, then once with them. */
424 for (intermed_ok
= 0; intermed_ok
== !!intermed_ok
; ++intermed_ok
)
426 /* Try each pattern rule till we find one that applies.
427 If it does, expand its dependencies (as substituted)
428 and chain them in DEPS. */
430 for (i
= 0; i
< nrules
; i
++)
433 unsigned int failed
= 0;
438 remove_explicit_deps
= 0;
440 /* RULE is nil when we discover that a rule,
441 already placed in TRYRULES, should not be applied. */
445 /* Reject any terminal rules if we're
446 looking to make intermediate files. */
447 if (intermed_ok
&& rule
->terminal
)
450 /* Mark this rule as in use so a recursive
451 pattern_search won't try to use it. */
454 /* From the lengths of the filename and the matching pattern parts,
455 find the stem: the part of the filename that matches the %. */
457 + (rule
->suffixes
[matches
[i
]] - rule
->targets
[matches
[i
]]) - 1;
458 stemlen
= namelen
- rule
->lens
[matches
[i
]] + 1;
459 check_lastslash
= checked_lastslash
[i
];
462 stem
+= lastslash
- filename
+ 1;
463 stemlen
-= (lastslash
- filename
) + 1;
466 DBS (DB_IMPLICIT
, (_("Trying pattern rule with stem `%.*s'.\n"),
467 (int) stemlen
, stem
));
469 strncpy (stem_str
, stem
, stemlen
);
470 stem_str
[stemlen
] = '\0';
472 /* Temporary assign STEM to file->stem and set file variables. */
473 file
->stem
= stem_str
;
474 set_file_variables (file
);
476 /* Try each dependency; see if it "exists". */
478 for (dep
= rule
->deps
; dep
!= 0; dep
= dep
->next
)
482 unsigned int order_only
= 0; /* Set if '|' was seen. */
484 /* In an ideal world we would take the dependency line,
485 substitute the stem, re-expand the whole line and chop it
486 into individual prerequisites. Unfortunately this won't work
487 because of the "check_lastslash" twist. Instead, we will
488 have to go word by word, taking $()'s into account, for each
489 word we will substitute the stem, re-expand, chop it up, and,
490 if check_lastslash != 0, add the directory part to each
491 resulting prerequisite. */
493 p
= get_next_word (dep
->name
, &len
);
501 break; /* No more words */
503 /* Is there a pattern in this prerequisite? */
505 for (p2
= p
; p2
< p
+ len
&& *p2
!= '%'; ++p2
)
508 if (dep
->need_2nd_expansion
)
510 /* If the dependency name has %, substitute the stem.
512 Watch out, we are going to do something tricky
513 here. If we just replace % with the stem value,
514 later, when we do the second expansion, we will
515 re-expand this stem value once again. This is not
516 good especially if you have certain characters in
519 Instead, we will replace % with $* and allow the
520 second expansion to take care of it for us. This way
521 (since $* is a simple variable) there won't be
522 additional re-expansion of the stem. */
526 register unsigned int i
= p2
- p
;
527 bcopy (p
, depname
, i
);
528 bcopy ("$*", depname
+ i
, 2);
529 bcopy (p2
+ 1, depname
+ i
+ 2, len
- i
- 1);
530 depname
[len
+ 2 - 1] = '\0';
539 bcopy (p
, depname
, len
);
543 p2
= variable_expand_for_file (depname
, file
);
549 register unsigned int i
= p2
- p
;
550 bcopy (p
, depname
, i
);
551 bcopy (stem_str
, depname
+ i
, stemlen
);
552 bcopy (p2
+ 1, depname
+ i
+ stemlen
, len
- i
- 1);
553 depname
[len
+ stemlen
- 1] = '\0';
562 bcopy (p
, depname
, len
);
569 /* Parse the dependencies. */
575 for (; *id_ptr
; id_ptr
= &(*id_ptr
)->next
)
578 *id_ptr
= (struct idep
*)
581 order_only
? '\0' : '|',
582 sizeof (struct idep
),
583 1), sizeof (struct idep
));
585 /* @@ It would be nice to teach parse_file_seq or
586 multi_glob to add prefix. This would save us some
589 if (order_only
|| add_dir
|| had_stem
)
591 unsigned long l
= lastslash
- filename
+ 1;
593 for (d
= *id_ptr
; d
!= 0; d
= d
->next
)
602 d
->name
= xmalloc (strlen (p
) + l
+ 1);
604 bcopy (filename
, d
->name
, l
);
605 bcopy (p
, d
->name
+ l
, strlen (p
) + 1);
615 if (!order_only
&& *p2
)
626 p
= get_next_word (p
, &len
);
630 /* Reset the stem in FILE. */
634 /* @@ This loop can be combined with the previous one. I do
635 it separately for now for transparency.*/
637 for (d
= deps
; d
!= 0; d
= d
->next
)
639 char *name
= d
->name
;
641 if (file_impossible_p (name
))
643 /* If this dependency has already been ruled "impossible",
644 then the rule fails and don't bother trying it on the
645 second pass either since we know that will fail too. */
648 ? _("Rejecting impossible implicit prerequisite `%s'.\n")
649 : _("Rejecting impossible rule prerequisite `%s'.\n"),
659 ? _("Trying implicit prerequisite `%s'.\n")
660 : _("Trying rule prerequisite `%s'.\n"), name
));
662 /* If this prerequisite also happened to be explicitly mentioned
663 for FILE skip all the test below since it it has to be built
664 anyway, no matter which implicit rule we choose. */
666 for (expl_d
= file
->deps
; expl_d
!= 0; expl_d
= expl_d
->next
)
667 if (streq (dep_name (expl_d
), name
))
672 /* The DEP->changed flag says that this dependency resides in a
673 nonexistent directory. So we normally can skip looking for
674 the file. However, if CHECK_LASTSLASH is set, then the
675 dependency file we are actually looking for is in a different
676 directory (the one gotten by prepending FILENAME's directory),
677 so it might actually exist. */
679 /* @@ dep->changed check is disabled. */
680 if (((f
= lookup_file (name
)) != 0 && f
->is_target
)
681 /*|| ((!dep->changed || check_lastslash) && */
682 || file_exists_p (name
))
685 /* This code, given FILENAME = "lib/foo.o", dependency name
686 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
688 if (vpath_search (&vname
, (FILE_TIMESTAMP
*) 0))
691 (_("Found prerequisite `%s' as VPATH `%s'\n"),
700 /* We could not find the file in any place we should look. Try
701 to make this dependency as an intermediate file, but only on
706 if (intermediate_file
== 0)
708 = (struct file
*) alloca (sizeof (struct file
));
711 (_("Looking for a rule with intermediate file `%s'.\n"),
714 bzero ((char *) intermediate_file
, sizeof (struct file
));
715 intermediate_file
->name
= name
;
716 if (pattern_search (intermediate_file
,
721 d
->intermediate_file
= intermediate_file
;
722 d
->intermediate_pattern
= intermediate_file
->name
;
724 intermediate_file
->name
= xstrdup (name
);
725 intermediate_file
= 0;
730 /* If we have tried to find P as an intermediate
731 file and failed, mark that name as impossible
732 so we won't go through the search again later. */
733 file_impossible (name
);
736 /* A dependency of this rule does not exist. Therefore,
742 /* This rule is no longer `in use' for recursive searches. */
747 /* This pattern rule does not apply. If some of its
748 dependencies succeeded, free the data structure
750 free_idep_chain (deps
);
754 /* This pattern rule does apply. Stop looking for one. */
758 /* If we found an applicable rule without
759 intermediate files, don't try with them. */
766 /* RULE is nil if the loop went all the way
767 through the list and everything failed. */
773 /* If we are recursing, store the pattern that matched
774 FILENAME in FILE->name for use in upper levels. */
778 file
->name
= rule
->targets
[matches
[foundrule
]];
780 /* FOUND_FILES lists the dependencies for the rule we found.
781 This includes the intermediate files, if any.
782 Convert them into entries on the deps-chain of FILE. */
784 if (remove_explicit_deps
)
786 /* Remove all the dependencies that didn't come from
787 this implicit rule. */
792 struct dep
*next
= dep
->next
;
800 expl_d
= file
->deps
; /* We will add them at the end. */
803 for (d
= deps
; d
!= 0; d
= d
->next
)
807 if (d
->intermediate_file
!= 0)
809 /* If we need to use an intermediate file,
810 make sure it is entered as a target, with the info that was
811 found for it in the recursive pattern_search call.
812 We know that the intermediate file did not already exist as
813 a target; therefore we can assume that the deps and cmds
814 of F below are null before we change them. */
816 struct file
*imf
= d
->intermediate_file
;
817 register struct file
*f
= lookup_file (imf
->name
);
819 /* We don't want to delete an intermediate file that happened
820 to be a prerequisite of some (other) target. Mark it as
825 f
= enter_file (imf
->name
);
830 f
->also_make
= imf
->also_make
;
835 imf
= lookup_file (d
->intermediate_pattern
);
836 if (imf
!= 0 && imf
->precious
)
841 f
->tried_implicit
= 1;
842 for (dep
= f
->deps
; dep
!= 0; dep
= dep
->next
)
844 dep
->file
= enter_file (dep
->name
);
845 /* enter_file uses dep->name _if_ we created a new file. */
846 if (dep
->name
!= dep
->file
->name
)
849 dep
->file
->tried_implicit
|= dep
->changed
;
853 dep
= (struct dep
*) xmalloc (sizeof (struct dep
));
854 dep
->ignore_mtime
= d
->ignore_mtime
;
855 dep
->staticpattern
= 0;
856 dep
->need_2nd_expansion
= 0;
857 s
= d
->name
; /* Hijacking the name. */
862 dep
->file
= lookup_file (s
);
864 /* enter_file consumes S's storage. */
865 dep
->file
= enter_file (s
);
867 /* A copy of S is already allocated in DEP->file->name.
877 if (d
->intermediate_file
== 0 && tryrules
[foundrule
]->terminal
)
879 /* If the file actually existed (was not an intermediate file),
880 and the rule that found it was a terminal one, then we want
881 to mark the found file so that it will not have implicit rule
882 search done for it. If we are not entering a `struct file' for
883 it now, we indicate this with the `changed' flag. */
887 dep
->file
->tried_implicit
= 1;
896 if (!checked_lastslash
[foundrule
])
898 /* Always allocate new storage, since STEM might be
899 on the stack for an intermediate file. */
900 file
->stem
= savestring (stem
, stemlen
);
901 fullstemlen
= stemlen
;
905 int dirlen
= (lastslash
+ 1) - filename
;
907 /* We want to prepend the directory from
908 the original FILENAME onto the stem. */
909 fullstemlen
= dirlen
+ stemlen
;
910 file
->stem
= (char *) xmalloc (fullstemlen
+ 1);
911 bcopy (filename
, file
->stem
, dirlen
);
912 bcopy (stem
, file
->stem
+ dirlen
, stemlen
);
913 file
->stem
[fullstemlen
] = '\0';
916 file
->cmds
= rule
->cmds
;
919 /* Set precious flag. */
921 struct file
*f
= lookup_file (rule
->targets
[matches
[foundrule
]]);
922 if (f
&& f
->precious
)
926 /* If this rule builds other targets, too, put the others into FILE's
927 `also_make' member. */
929 if (rule
->targets
[1] != 0)
930 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
931 if (i
!= matches
[foundrule
])
934 struct dep
*new = (struct dep
*) xmalloc (sizeof (struct dep
));
935 /* GKM FIMXE: handle '|' here too */
936 new->ignore_mtime
= 0;
937 new->staticpattern
= 0;
938 new->need_2nd_expansion
= 0;
939 new->name
= p
= (char *) xmalloc (rule
->lens
[i
] + fullstemlen
+ 1);
940 bcopy (rule
->targets
[i
], p
,
941 rule
->suffixes
[i
] - rule
->targets
[i
] - 1);
942 p
+= rule
->suffixes
[i
] - rule
->targets
[i
] - 1;
943 bcopy (file
->stem
, p
, fullstemlen
);
945 bcopy (rule
->suffixes
[i
], p
,
946 rule
->lens
[i
] - (rule
->suffixes
[i
] - rule
->targets
[i
]) + 1);
947 new->file
= enter_file (new->name
);
948 new->next
= file
->also_make
;
950 /* Set precious flag. */
951 f
= lookup_file (rule
->targets
[i
]);
952 if (f
&& f
->precious
)
953 new->file
->precious
= 1;
955 /* Set the is_target flag so that this file is not treated
956 as intermediate by the pattern rule search algorithm and
957 file_exists_p cannot pick it up yet. */
958 new->file
->is_target
= 1;
960 file
->also_make
= new;
964 free_idep_chain (deps
);