1 /* Implicit rule searching for GNU Make.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
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 this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "job.h" /* struct child, used inside commands.h */
24 #include "commands.h" /* set_file_variables */
26 static int pattern_search (struct file *file, int archive,
27 unsigned int depth, unsigned int recursions);
29 /* For a FILE which has no commands specified, try to figure out some
30 from the implicit pattern rules.
31 Returns 1 if a suitable implicit rule was found,
32 after modifying FILE to contain the appropriate commands and deps,
33 or returns 0 if no implicit rule was found. */
36 try_implicit_rule (struct file *file, unsigned int depth)
38 DBF (DB_IMPLICIT, _("Looking for an implicit rule for '%s'.\n"));
40 /* The order of these searches was previously reversed. My logic now is
41 that since the non-archive search uses more information in the target
42 (the archive search omits the archive name), it is more specific and
45 if (pattern_search (file, 0, depth, 0))
49 /* If this is an archive member reference, use just the
50 archive member name to search for implicit rules. */
51 if (ar_name (file->name))
54 _("Looking for archive-member implicit rule for '%s'.\n"));
55 if (pattern_search (file, 1, depth, 0))
64 /* Scans the BUFFER for the next word with whitespace as a separator.
65 Returns the pointer to the beginning of the word. LENGTH hold the
66 length of the word. */
69 get_next_word (const char *buffer, unsigned int *length)
71 const char *p = buffer, *beg;
74 /* Skip any leading whitespace. */
75 while (isblank ((unsigned char)*p))
85 /* We already found the first value of "c", above. */
103 /* This is a variable reference, so read it to the matching
111 /* This is a single-letter variable reference. */
114 for (count = 0; *p != '\0'; ++p)
118 else if (*p == closeparen && --count < 0)
145 /* This structure stores information about the expanded prerequisites for a
146 pattern rule. NAME is always set to the strcache'd name of the prereq.
147 FILE and PATTERN will be set for intermediate files only. IGNORE_MTIME is
148 copied from the prerequisite we expanded.
155 unsigned int ignore_mtime : 1;
158 /* This structure stores information about pattern rules that we need
165 /* Index of the target in this rule that matched the file. */
166 unsigned int matches;
168 /* Stem length for this match. */
169 unsigned int stemlen;
171 /* Definition order of this rule. Used to implement stable sort.*/
174 /* Nonzero if the LASTSLASH logic was used in matching this rule. */
175 char checked_lastslash;
179 stemlen_compare (const void *v1, const void *v2)
181 const struct tryrule *r1 = v1;
182 const struct tryrule *r2 = v2;
183 int r = r1->stemlen - r2->stemlen;
184 return r != 0 ? r : (int)(r1->order - r2->order);
187 /* Search the pattern rules for a rule with an existing dependency to make
188 FILE. If a rule is found, the appropriate commands and deps are put in FILE
189 and 1 is returned. If not, 0 is returned.
191 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
192 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
193 directory and filename parts.
195 If an intermediate file is found by pattern search, the intermediate file
196 is set up as a target by the recursive call and is also made a dependency
199 DEPTH is used for debugging messages. */
202 pattern_search (struct file *file, int archive,
203 unsigned int depth, unsigned int recursions)
205 /* Filename we are searching for a rule for. */
206 const char *filename = archive ? strchr (file->name, '(') : file->name;
208 /* Length of FILENAME. */
209 unsigned int namelen = strlen (filename);
211 /* The last slash in FILENAME (or nil if there is none). */
212 const char *lastslash;
214 /* This is a file-object used as an argument in
215 recursive calls. It never contains any data
216 except during a recursive call. */
217 struct file *int_file = 0;
219 /* List of dependencies found recursively. */
220 unsigned int max_deps = max_pattern_deps;
221 struct patdeps *deplist = xmalloc (max_deps * sizeof (struct patdeps));
222 struct patdeps *pat = deplist;
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 tryrule *tryrules = xmalloc (num_pattern_rules * max_pattern_targets
234 * sizeof (struct tryrule));
236 /* Number of valid elements in TRYRULES. */
239 /* The index in TRYRULES of the rule we found. */
240 unsigned int foundrule;
242 /* Nonzero if should consider intermediate files as dependencies. */
245 /* Nonzero if we have initialized file variables for this target. */
246 int file_vars_initialized = 0;
248 /* Nonzero if we have matched a pattern-rule target
249 that is not just '%'. */
250 int specific_rule_matched = 0;
252 unsigned int ri; /* uninit checks OK */
255 char *pathdir = NULL;
256 unsigned long pathlen;
258 PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
261 if (archive || ar_name (filename))
266 /* Set LASTSLASH to point at the last slash in FILENAME
267 but not counting any slash at the end. (foo/bar/ counts as
268 bar/ in directory foo/, not empty in directory foo/bar/.) */
270 lastslash = strrchr (filename, ']');
272 lastslash = strrchr (filename, ':');
274 lastslash = strrchr (filename, '/');
275 #ifdef HAVE_DOS_PATHS
276 /* Handle backslashes (possibly mixed with forward slashes)
277 and the case of "d:file". */
279 char *bslash = strrchr (filename, '\\');
280 if (lastslash == 0 || bslash > lastslash)
282 if (lastslash == 0 && filename[0] && filename[1] == ':')
283 lastslash = filename + 1;
287 if (lastslash != 0 && lastslash[1] == '\0')
291 pathlen = lastslash - filename + 1;
293 /* First see which pattern rules match this target and may be considered.
294 Put them in TRYRULES. */
297 for (rule = pattern_rules; rule != 0; rule = rule->next)
301 /* If the pattern rule has deps but no commands, ignore it.
302 Users cancel built-in rules by redefining them without commands. */
303 if (rule->deps != 0 && rule->cmds == 0)
306 /* If this rule is in use by a parent pattern_search,
307 don't use it here. */
310 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
314 for (ti = 0; ti < rule->num; ++ti)
316 const char *target = rule->targets[ti];
317 const char *suffix = rule->suffixes[ti];
320 /* Rules that can match any filename and are not terminal
321 are ignored if we're recursing, so that they cannot be
322 intermediate files. */
323 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
326 if (rule->lens[ti] > namelen)
327 /* It can't possibly match. */
330 /* From the lengths of the filename and the pattern parts,
331 find the stem: the part of the filename that matches the %. */
332 stem = filename + (suffix - target - 1);
333 stemlen = namelen - rule->lens[ti] + 1;
335 /* Set CHECK_LASTSLASH if FILENAME contains a directory
336 prefix and the target pattern does not contain a slash. */
342 check_lastslash = (strchr (target, ']') == 0
343 && strchr (target, ':') == 0);
345 check_lastslash = strchr (target, '/') == 0;
346 #ifdef HAVE_DOS_PATHS
347 /* Didn't find it yet: check for DOS-type directories. */
350 char *b = strchr (target, '\\');
351 check_lastslash = !(b || (target[0] && target[1] == ':'));
358 /* If so, don't include the directory prefix in STEM here. */
359 if (pathlen > stemlen)
365 /* Check that the rule pattern matches the text before the stem. */
368 if (stem > (lastslash + 1)
369 && !strneq (target, lastslash + 1, stem - lastslash - 1))
372 else if (stem > filename
373 && !strneq (target, filename, stem - filename))
376 /* Check that the rule pattern matches the text after the stem.
377 We could test simply use streq, but this way we compare the
378 first two characters immediately. This saves time in the very
379 common case where the first character matches because it is a
381 if (*suffix != stem[stemlen]
382 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
385 /* Record if we match a rule that not all filenames will match. */
386 if (target[1] != '\0')
387 specific_rule_matched = 1;
389 /* A rule with no dependencies and no commands exists solely to set
390 specific_rule_matched when it matches. Don't try to use it. */
391 if (rule->deps == 0 && rule->cmds == 0)
394 /* Record this rule in TRYRULES and the index of the matching
395 target in MATCHES. If several targets of the same rule match,
396 that rule will be in TRYRULES more than once. */
397 tryrules[nrules].rule = rule;
398 tryrules[nrules].matches = ti;
399 tryrules[nrules].stemlen = stemlen + (check_lastslash ? pathlen : 0);
400 tryrules[nrules].order = nrules;
401 tryrules[nrules].checked_lastslash = check_lastslash;
406 /* Bail out early if we haven't found any rules. */
410 /* Sort the rules to place matches with the shortest stem first. This
411 way the most specific rules will be tried first. */
413 qsort (tryrules, nrules, sizeof (struct tryrule), stemlen_compare);
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].rule->terminal)
422 for (j = 0; j < tryrules[ri].rule->num; ++j)
423 if (tryrules[ri].rule->targets[j][1] == '\0')
425 tryrules[ri].rule = 0;
430 /* Try each rule once without intermediate files, then once with them. */
431 for (intermed_ok = 0; intermed_ok < 2; ++intermed_ok)
435 /* Try each pattern rule till we find one that applies. If it does,
436 expand its dependencies (as substituted) and chain them in DEPS. */
437 for (ri = 0; ri < nrules; ri++)
441 unsigned int failed = 0;
442 int file_variables_set = 0;
443 unsigned int deps_found = 0;
444 /* NPTR points to the part of the prereq we haven't processed. */
445 const char *nptr = 0;
446 const char *dir = NULL;
448 unsigned int matches;
450 rule = tryrules[ri].rule;
452 /* RULE is nil when we discover that a rule, already placed in
453 TRYRULES, should not be applied. */
457 /* Reject any terminal rules if we're looking to make intermediate
459 if (intermed_ok && rule->terminal)
462 /* From the lengths of the filename and the matching pattern parts,
463 find the stem: the part of the filename that matches the %. */
464 matches = tryrules[ri].matches;
465 stem = filename + (rule->suffixes[matches]
466 - rule->targets[matches]) - 1;
467 stemlen = (namelen - rule->lens[matches]) + 1;
468 check_lastslash = tryrules[ri].checked_lastslash;
474 /* We need to add the directory prefix, so set it up. */
477 pathdir = alloca (pathlen + 1);
478 memcpy (pathdir, filename, pathlen);
479 pathdir[pathlen] = '\0';
484 if (stemlen > GET_PATH_MAX)
486 DBS (DB_IMPLICIT, (_("Stem too long: '%.*s'.\n"),
487 (int) stemlen, stem));
491 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem '%.*s'.\n"),
492 (int) stemlen, stem));
494 strncpy (stem_str, stem, stemlen);
495 stem_str[stemlen] = '\0';
497 /* If there are no prerequisites, then this rule matches. */
501 /* Temporary assign STEM to file->stem (needed to set file
503 file->stem = stem_str;
505 /* Mark this rule as in use so a recursive pattern_search won't try
509 /* Try each prerequisite; see if it exists or can be created. We'll
510 build a list of prereq info in DEPLIST. Due to 2nd expansion we
511 may have to process multiple prereqs for a single dep entry. */
515 nptr = dep_name (dep);
521 /* If we're out of name to parse, start the next prereq. */
527 nptr = dep_name (dep);
530 /* If we don't need a second expansion, just replace the %. */
531 if (! dep->need_2nd_expansion)
533 p = strchr (nptr, '%');
535 strcpy (depname, nptr);
541 memcpy (o, filename, pathlen);
544 memcpy (o, nptr, p - nptr);
546 memcpy (o, stem_str, stemlen);
551 /* Parse the expanded string. It might have wildcards. */
553 dl = PARSE_SIMPLE_SEQ (&p, struct dep);
554 for (d = dl; d != NULL; d = d->next)
557 d->ignore_mtime = dep->ignore_mtime;
560 /* We've used up this dep, so next time get a new one. */
564 /* We have to perform second expansion on this prereq. In an
565 ideal world we would take the dependency line, substitute the
566 stem, re-expand the whole line and chop it into individual
567 prerequisites. Unfortunately this won't work because of the
568 "check_lastslash" twist. Instead, we will have to go word by
569 word, taking $()'s into account. For each word we will
570 substitute the stem, re-expand, chop it up, and, if
571 check_lastslash != 0, add the directory part to each
572 resulting prerequisite. */
579 nptr = get_next_word (nptr, &len);
583 /* See this is a transition to order-only prereqs. */
584 if (! order_only && len == 1 && nptr[0] == '|')
591 /* If the dependency name has %, substitute the stem. If we
592 just replace % with the stem value then later, when we do
593 the 2nd expansion, we will re-expand this stem value
594 again. This is not good if you have certain characters
595 in your stem (like $).
597 Instead, we will replace % with $* and allow the second
598 expansion to take care of it for us. This way (since $*
599 is a simple variable) there won't be additional
600 re-expansion of the stem. */
602 p = lindex (nptr, nptr + len, '%');
605 memcpy (depname, nptr, len);
610 unsigned int i = p - nptr;
611 memcpy (depname, nptr, i);
612 memcpy (depname + i, "$*", 2);
613 memcpy (depname + i + 2, p + 1, len - i - 1);
614 depname[len + 2 - 1] = '\0';
620 /* Set up for the next word. */
623 /* Initialize and set file variables if we haven't already
625 if (!file_vars_initialized)
627 initialize_file_variables (file, 0);
628 set_file_variables (file);
629 file_vars_initialized = 1;
631 /* Update the stem value in $* for this rule. */
632 else if (!file_variables_set)
634 define_variable_for_file (
635 "*", 1, file->stem, o_automatic, 0, file);
636 file_variables_set = 1;
639 /* Perform the 2nd expansion. */
640 p = variable_expand_for_file (depname, file);
643 /* Parse the results into a deps list. */
646 /* Parse the expanded string. */
647 struct dep *dp = PARSE_FILE_SEQ (&p, struct dep,
648 order_only ? MAP_NUL : MAP_PIPE,
649 add_dir ? dir : NULL, PARSEFS_NONE);
652 for (d = dp; d != NULL; d = d->next)
660 /* If we stopped due to an order-only token, note it. */
670 /* If there are more than max_pattern_deps prerequisites (due to
671 2nd expansion), reset it and realloc the arrays. */
673 if (deps_found > max_deps)
675 unsigned int l = pat - deplist;
676 /* This might have changed due to recursion. */
677 max_pattern_deps = MAX(max_pattern_deps, deps_found);
678 max_deps = max_pattern_deps;
679 deplist = xrealloc (deplist,
680 max_deps * sizeof (struct patdeps));
684 /* Go through the nameseq and handle each as a prereq name. */
685 for (d = dl; d != 0; d = d->next)
688 int is_rule = d->name == dep_name (dep);
690 if (file_impossible_p (d->name))
692 /* If this prereq has already been ruled "impossible",
693 then the rule fails. Don't bother trying it on the
694 second pass either since we know that will fail. */
697 ? _("Rejecting impossible rule prerequisite '%s'.\n")
698 : _("Rejecting impossible implicit prerequisite '%s'.\n"),
700 tryrules[ri].rule = 0;
706 memset (pat, '\0', sizeof (struct patdeps));
707 pat->ignore_mtime = d->ignore_mtime;
711 ? _("Trying rule prerequisite '%s'.\n")
712 : _("Trying implicit prerequisite '%s'.\n"), d->name));
714 /* If this prereq is also explicitly mentioned for FILE,
715 skip all tests below since it must be built no matter
716 which implicit rule we choose. */
718 for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
719 if (streq (dep_name (expl_d), d->name))
723 (pat++)->name = d->name;
727 /* The DEP->changed flag says that this dependency resides
728 in a nonexistent directory. So we normally can skip
729 looking for the file. However, if CHECK_LASTSLASH is
730 set, then the dependency file we are actually looking for
731 is in a different directory (the one gotten by prepending
732 FILENAME's directory), so it might actually exist. */
734 /* @@ dep->changed check is disabled. */
735 if (lookup_file (d->name) != 0
736 /*|| ((!dep->changed || check_lastslash) && */
737 || file_exists_p (d->name))
739 (pat++)->name = d->name;
743 /* This code, given FILENAME = "lib/foo.o", dependency name
744 "lib/foo.c", and VPATH=src, searches for
747 const char *vname = vpath_search (d->name, 0, NULL, NULL);
751 (_("Found prerequisite '%s' as VPATH '%s'\n"),
753 (pat++)->name = d->name;
758 /* We could not find the file in any place we should look.
759 Try to make this dependency as an intermediate file, but
760 only on the second pass. */
765 (_("Looking for a rule with intermediate file '%s'.\n"),
769 int_file = alloca (sizeof (struct file));
770 memset (int_file, '\0', sizeof (struct file));
771 int_file->name = d->name;
773 if (pattern_search (int_file,
778 pat->pattern = int_file->name;
779 int_file->name = d->name;
780 pat->file = int_file;
782 (pat++)->name = d->name;
786 /* If we have tried to find P as an intermediate file
787 and failed, mark that name as impossible so we won't
788 go through the search again later. */
789 if (int_file->variables)
790 free_variable_set (int_file->variables);
791 if (int_file->pat_variables)
792 free_variable_set (int_file->pat_variables);
793 file_impossible (d->name);
796 /* A dependency of this rule does not exist. Therefore, this
802 /* Free the ns chain. */
809 /* Reset the stem in FILE. */
813 /* This rule is no longer 'in use' for recursive searches. */
817 /* This pattern rule does apply. Stop looking for one. */
820 /* This pattern rule does not apply. Keep looking. */
823 /* If we found an applicable rule without intermediate files, don't try
831 /* RULE is nil if the loop went through the list but everything failed. */
837 /* If we are recursing, store the pattern that matched FILENAME in
838 FILE->name for use in upper levels. */
842 file->name = rule->targets[tryrules[foundrule].matches];
844 /* DEPLIST lists the prerequisites for the rule we found. This includes the
845 intermediate files, if any. Convert them into entries on the deps-chain
848 while (pat-- > deplist)
855 /* If we need to use an intermediate file, make sure it is entered
856 as a target, with the info that was found for it in the recursive
857 pattern_search call. We know that the intermediate file did not
858 already exist as a target; therefore we can assume that the deps
859 and cmds of F below are null before we change them. */
861 struct file *imf = pat->file;
862 struct file *f = lookup_file (imf->name);
864 /* We don't want to delete an intermediate file that happened
865 to be a prerequisite of some (other) target. Mark it as
870 f = enter_file (imf->name);
875 f->variables = imf->variables;
876 f->pat_variables = imf->pat_variables;
877 f->pat_searched = imf->pat_searched;
878 f->also_make = imf->also_make;
881 f->tried_implicit = 1;
883 imf = lookup_file (pat->pattern);
884 if (imf != 0 && imf->precious)
887 for (dep = f->deps; dep != 0; dep = dep->next)
889 dep->file = enter_file (dep->name);
891 dep->file->tried_implicit |= dep->changed;
896 dep->ignore_mtime = pat->ignore_mtime;
897 s = strcache_add (pat->name);
902 dep->file = lookup_file (s);
904 dep->file = enter_file (s);
907 if (pat->file == 0 && tryrules[foundrule].rule->terminal)
909 /* If the file actually existed (was not an intermediate file), and
910 the rule that found it was a terminal one, then we want to mark
911 the found file so that it will not have implicit rule search done
912 for it. If we are not entering a 'struct file' for it now, we
913 indicate this with the 'changed' flag. */
917 dep->file->tried_implicit = 1;
920 dep->next = file->deps;
924 if (!tryrules[foundrule].checked_lastslash)
926 /* Always allocate new storage, since STEM might be on the stack for an
927 intermediate file. */
928 file->stem = strcache_add_len (stem, stemlen);
929 fullstemlen = stemlen;
933 int dirlen = (lastslash + 1) - filename;
936 /* We want to prepend the directory from
937 the original FILENAME onto the stem. */
938 fullstemlen = dirlen + stemlen;
939 sp = alloca (fullstemlen + 1);
940 memcpy (sp, filename, dirlen);
941 memcpy (sp + dirlen, stem, stemlen);
942 sp[fullstemlen] = '\0';
943 file->stem = strcache_add (sp);
946 file->cmds = rule->cmds;
949 /* Set precious flag. */
951 struct file *f = lookup_file (rule->targets[tryrules[foundrule].matches]);
952 if (f && f->precious)
956 /* If this rule builds other targets, too, put the others into FILE's
957 'also_make' member. */
960 for (ri = 0; ri < rule->num; ++ri)
961 if (ri != tryrules[foundrule].matches)
963 char *nm = alloca (rule->lens[ri] + fullstemlen + 1);
966 struct dep *new = alloc_dep ();
968 /* GKM FIMXE: handle '|' here too */
969 memcpy (p, rule->targets[ri],
970 rule->suffixes[ri] - rule->targets[ri] - 1);
971 p += rule->suffixes[ri] - rule->targets[ri] - 1;
972 memcpy (p, file->stem, fullstemlen);
974 memcpy (p, rule->suffixes[ri],
975 rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
976 new->name = strcache_add (nm);
977 new->file = enter_file (new->name);
978 new->next = file->also_make;
980 /* Set precious flag. */
981 f = lookup_file (rule->targets[ri]);
982 if (f && f->precious)
983 new->file->precious = 1;
985 /* Set the is_target flag so that this file is not treated as
986 intermediate by the pattern rule search algorithm and
987 file_exists_p cannot pick it up yet. */
988 new->file->is_target = 1;
990 file->also_make = new;