Add a test for another (still open) bug.
[make.git] / implicit.c
blob8ad63952be6ad1ede3abcfa5d9cd5c46c689989d
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, 2009 Free
4 Software Foundation, Inc.
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
10 version.
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/>. */
19 #include "make.h"
20 #include "filedef.h"
21 #include "rule.h"
22 #include "dep.h"
23 #include "debug.h"
24 #include "variable.h"
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. */
37 int
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
45 should come first. */
47 if (pattern_search (file, 0, depth, 0))
48 return 1;
50 #ifndef NO_ARCHIVES
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))
55 DBF (DB_IMPLICIT,
56 _("Looking for archive-member implicit rule for `%s'.\n"));
57 if (pattern_search (file, 1, depth, 0))
58 return 1;
60 #endif
62 return 0;
66 /* Scans the BUFFER for the next word with whitespace as a separator.
67 Returns the pointer to the beginning of the word. LENGTH hold the
68 length of the word. */
70 static const char *
71 get_next_word (const char *buffer, unsigned int *length)
73 const char *p = buffer, *beg;
74 char c;
76 /* Skip any leading whitespace. */
77 while (isblank ((unsigned char)*p))
78 ++p;
80 beg = p;
81 c = *(p++);
83 if (c == '\0')
84 return 0;
87 /* We already found the first value of "c", above. */
88 while (1)
90 char closeparen;
91 int count;
93 switch (c)
95 case '\0':
96 case ' ':
97 case '\t':
98 goto done_word;
100 case '$':
101 c = *(p++);
102 if (c == '$')
103 break;
105 /* This is a variable reference, so read it to the matching
106 close paren. */
108 if (c == '(')
109 closeparen = ')';
110 else if (c == '{')
111 closeparen = '}';
112 else
113 /* This is a single-letter variable reference. */
114 break;
116 for (count = 0; *p != '\0'; ++p)
118 if (*p == c)
119 ++count;
120 else if (*p == closeparen && --count < 0)
122 ++p;
123 break;
126 break;
128 case '|':
129 goto done;
131 default:
132 break;
135 c = *(p++);
137 done_word:
138 --p;
140 done:
141 if (length)
142 *length = p - beg;
144 return beg;
147 /* This structure stores information about the expanded prerequisites for a
148 pattern rule. NAME is always set to the strcache'd name of the prereq.
149 FILE and PATTERN will be set for intermediate files only. IGNORE_MTIME is
150 copied from the prerequisite we expanded.
152 struct patdeps
154 const char *name;
155 const char *pattern;
156 struct file *file;
157 unsigned int ignore_mtime : 1;
160 /* This structure stores information about pattern rules that we need
161 to try.
163 struct tryrule
165 struct rule *rule;
167 /* Index of the target in this rule that matched the file. */
168 unsigned int matches;
170 /* Stem length for this match. */
171 unsigned int stemlen;
173 /* Definition order of this rule. Used to implement stable sort.*/
174 unsigned int order;
176 /* Nonzero if the LASTSLASH logic was used in matching this rule. */
177 char checked_lastslash;
181 stemlen_compare (const void *v1, const void *v2)
183 const struct tryrule *r1 = (const struct tryrule *)v1;
184 const struct tryrule *r2 = (const struct tryrule *)v2;
185 int r = r1->stemlen - r2->stemlen;
186 return r != 0 ? r : (int)(r1->order - r1->order);
189 /* Search the pattern rules for a rule with an existing dependency to make
190 FILE. If a rule is found, the appropriate commands and deps are put in FILE
191 and 1 is returned. If not, 0 is returned.
193 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
194 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
195 directory and filename parts.
197 If an intermediate file is found by pattern search, the intermediate file
198 is set up as a target by the recursive call and is also made a dependency
199 of FILE.
201 DEPTH is used for debugging messages. */
203 static int
204 pattern_search (struct file *file, int archive,
205 unsigned int depth, unsigned int recursions)
207 /* Filename we are searching for a rule for. */
208 const char *filename = archive ? strchr (file->name, '(') : file->name;
210 /* Length of FILENAME. */
211 unsigned int namelen = strlen (filename);
213 /* The last slash in FILENAME (or nil if there is none). */
214 char *lastslash;
216 /* This is a file-object used as an argument in
217 recursive calls. It never contains any data
218 except during a recursive call. */
219 struct file *int_file = 0;
221 /* List of dependencies found recursively. */
222 struct patdeps *deplist
223 = xmalloc (max_pattern_deps * sizeof (struct patdeps));
224 struct patdeps *pat = deplist;
226 /* All the prerequisites actually found for a rule, after expansion. */
227 struct dep *deps;
229 /* Names of possible dependencies are constructed in this buffer. */
230 char *depname = alloca (namelen + max_pattern_dep_length);
232 /* The start and length of the stem of FILENAME for the current rule. */
233 const char *stem = 0;
234 unsigned int stemlen = 0;
235 unsigned int fullstemlen = 0;
237 /* Buffer in which we store all the rules that are possibly applicable. */
238 struct tryrule *tryrules = xmalloc (num_pattern_rules * max_pattern_targets
239 * sizeof (struct tryrule));
241 /* Number of valid elements in TRYRULES. */
242 unsigned int nrules;
244 /* The index in TRYRULES of the rule we found. */
245 unsigned int foundrule;
247 /* Nonzero if should consider intermediate files as dependencies. */
248 int intermed_ok;
250 /* Nonzero if we have matched a pattern-rule target
251 that is not just `%'. */
252 int specific_rule_matched = 0;
254 struct dep dep_simple;
256 unsigned int ri; /* uninit checks OK */
257 struct rule *rule;
259 char *pathdir = NULL;
260 unsigned long pathlen;
262 PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
264 #ifndef NO_ARCHIVES
265 if (archive || ar_name (filename))
266 lastslash = 0;
267 else
268 #endif
270 /* Set LASTSLASH to point at the last slash in FILENAME
271 but not counting any slash at the end. (foo/bar/ counts as
272 bar/ in directory foo/, not empty in directory foo/bar/.) */
273 #ifdef VMS
274 lastslash = strrchr (filename, ']');
275 if (lastslash == 0)
276 lastslash = strrchr (filename, ':');
277 #else
278 lastslash = strrchr (filename, '/');
279 #ifdef HAVE_DOS_PATHS
280 /* Handle backslashes (possibly mixed with forward slashes)
281 and the case of "d:file". */
283 char *bslash = strrchr (filename, '\\');
284 if (lastslash == 0 || bslash > lastslash)
285 lastslash = bslash;
286 if (lastslash == 0 && filename[0] && filename[1] == ':')
287 lastslash = filename + 1;
289 #endif
290 #endif
291 if (lastslash != 0 && lastslash[1] == '\0')
292 lastslash = 0;
295 pathlen = lastslash - filename + 1;
297 /* First see which pattern rules match this target and may be considered.
298 Put them in TRYRULES. */
300 nrules = 0;
301 for (rule = pattern_rules; rule != 0; rule = rule->next)
303 unsigned int ti;
305 /* If the pattern rule has deps but no commands, ignore it.
306 Users cancel built-in rules by redefining them without commands. */
307 if (rule->deps != 0 && rule->cmds == 0)
308 continue;
310 /* If this rule is in use by a parent pattern_search,
311 don't use it here. */
312 if (rule->in_use)
314 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
315 continue;
318 for (ti = 0; ti < rule->num; ++ti)
320 const char *target = rule->targets[ti];
321 const char *suffix = rule->suffixes[ti];
322 int check_lastslash;
324 /* Rules that can match any filename and are not terminal
325 are ignored if we're recursing, so that they cannot be
326 intermediate files. */
327 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
328 continue;
330 if (rule->lens[ti] > namelen)
331 /* It can't possibly match. */
332 continue;
334 /* From the lengths of the filename and the pattern parts,
335 find the stem: the part of the filename that matches the %. */
336 stem = filename + (suffix - target - 1);
337 stemlen = namelen - rule->lens[ti] + 1;
339 /* Set CHECK_LASTSLASH if FILENAME contains a directory
340 prefix and the target pattern does not contain a slash. */
342 check_lastslash = 0;
343 if (lastslash)
345 #ifdef VMS
346 check_lastslash = (strchr (target, ']') == 0
347 && strchr (target, ':') == 0);
348 #else
349 check_lastslash = strchr (target, '/') == 0;
350 #ifdef HAVE_DOS_PATHS
351 /* Didn't find it yet: check for DOS-type directories. */
352 if (check_lastslash)
354 char *b = strchr (target, '\\');
355 check_lastslash = !(b || (target[0] && target[1] == ':'));
357 #endif
358 #endif
360 if (check_lastslash)
362 /* If so, don't include the directory prefix in STEM here. */
363 if (pathlen > stemlen)
364 continue;
365 stemlen -= pathlen;
366 stem += pathlen;
369 /* Check that the rule pattern matches the text before the stem. */
370 if (check_lastslash)
372 if (stem > (lastslash + 1)
373 && !strneq (target, lastslash + 1, stem - lastslash - 1))
374 continue;
376 else if (stem > filename
377 && !strneq (target, filename, stem - filename))
378 continue;
380 /* Check that the rule pattern matches the text after the stem.
381 We could test simply use streq, but this way we compare the
382 first two characters immediately. This saves time in the very
383 common case where the first character matches because it is a
384 period. */
385 if (*suffix != stem[stemlen]
386 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
387 continue;
389 /* Record if we match a rule that not all filenames will match. */
390 if (target[1] != '\0')
391 specific_rule_matched = 1;
393 /* A rule with no dependencies and no commands exists solely to set
394 specific_rule_matched when it matches. Don't try to use it. */
395 if (rule->deps == 0 && rule->cmds == 0)
396 continue;
398 /* Record this rule in TRYRULES and the index of the matching
399 target in MATCHES. If several targets of the same rule match,
400 that rule will be in TRYRULES more than once. */
401 tryrules[nrules].rule = rule;
402 tryrules[nrules].matches = ti;
403 tryrules[nrules].stemlen = stemlen + (check_lastslash ? pathlen : 0);
404 tryrules[nrules].order = nrules;
405 tryrules[nrules].checked_lastslash = check_lastslash;
406 ++nrules;
410 /* Bail out early if we haven't found any rules. */
411 if (nrules == 0)
412 goto done;
414 /* Sort the rules to place matches with the shortest stem first. This
415 way the most specific rules will be tried first. */
416 if (nrules > 1)
417 qsort (tryrules, nrules, sizeof (struct tryrule), stemlen_compare);
419 /* If we have found a matching rule that won't match all filenames,
420 retroactively reject any non-"terminal" rules that do always match. */
421 if (specific_rule_matched)
422 for (ri = 0; ri < nrules; ++ri)
423 if (!tryrules[ri].rule->terminal)
425 unsigned int j;
426 for (j = 0; j < tryrules[ri].rule->num; ++j)
427 if (tryrules[ri].rule->targets[j][1] == '\0')
429 tryrules[ri].rule = 0;
430 break;
434 /* We are going to do second expansion so initialize file variables
435 for the rule. */
436 initialize_file_variables (file, 0);
438 /* Try each rule once without intermediate files, then once with them. */
439 for (intermed_ok = 0; intermed_ok < 2; ++intermed_ok)
441 pat = deplist;
443 /* Try each pattern rule till we find one that applies. If it does,
444 expand its dependencies (as substituted) and chain them in DEPS. */
445 for (ri = 0; ri < nrules; ri++)
447 struct dep *dep;
448 int check_lastslash;
449 unsigned int failed = 0;
450 int file_variables_set = 0;
451 unsigned int deps_found = 0;
452 /* NPTR points to the part of the prereq we haven't processed. */
453 const char *nptr = 0;
454 const char *dir = NULL;
455 int order_only = 0;
456 unsigned int matches;
458 rule = tryrules[ri].rule;
460 /* RULE is nil when we discover that a rule, already placed in
461 TRYRULES, should not be applied. */
462 if (rule == 0)
463 continue;
465 /* Reject any terminal rules if we're looking to make intermediate
466 files. */
467 if (intermed_ok && rule->terminal)
468 continue;
470 /* From the lengths of the filename and the matching pattern parts,
471 find the stem: the part of the filename that matches the %. */
472 matches = tryrules[ri].matches;
473 stem = filename + (rule->suffixes[matches]
474 - rule->targets[matches]) - 1;
475 stemlen = (namelen - rule->lens[matches]) + 1;
476 check_lastslash = tryrules[ri].checked_lastslash;
477 if (check_lastslash)
479 stem += pathlen;
480 stemlen -= pathlen;
482 /* We need to add the directory prefix, so set it up. */
483 if (! pathdir)
485 pathdir = alloca (pathlen + 1);
486 memcpy (pathdir, filename, pathlen);
487 pathdir[pathlen] = '\0';
489 dir = pathdir;
492 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
493 (int) stemlen, stem));
495 strncpy (stem_str, stem, stemlen);
496 stem_str[stemlen] = '\0';
498 /* If there are no prerequisites, then this rule matches. */
499 if (rule->deps == 0)
500 break;
502 /* Temporary assign STEM to file->stem (needed to set file
503 variables below). */
504 file->stem = stem_str;
506 /* Mark this rule as in use so a recursive pattern_search won't try
507 to use it. */
508 rule->in_use = 1;
510 /* Try each prerequisite; see if it exists or can be created. We'll
511 build a list of prereq info in DEPLIST. Due to 2nd expansion we
512 may have to process multiple prereqs for a single dep entry. */
514 pat = deplist;
515 dep = rule->deps;
516 nptr = dep_name (dep);
517 while (1)
519 struct dep *dl, *d;
520 char *p;
522 /* If we're out of name to parse, start the next prereq. */
523 if (! nptr)
525 dep = dep->next;
526 if (dep == 0)
527 break;
528 nptr = dep_name (dep);
531 /* If we don't need a second expansion, just replace the %. */
532 if (! dep->need_2nd_expansion)
534 dep_simple = *dep;
535 dep_simple.next = 0;
536 p = strchr (nptr, '%');
537 if (p == 0)
538 dep_simple.name = nptr;
539 else
541 char *o = depname;
542 if (check_lastslash)
544 memcpy (o, filename, pathlen);
545 o += pathlen;
547 memcpy (o, nptr, p - nptr);
548 o += p - nptr;
549 memcpy (o, stem_str, stemlen);
550 o += stemlen;
551 strcpy (o, p + 1);
552 dep_simple.name = strcache_add (depname);
554 dl = &dep_simple;
556 /* We've used up this dep, so next time get a new one. */
557 nptr = 0;
558 ++deps_found;
561 /* We have to perform second expansion on this prereq. In an
562 ideal world we would take the dependency line, substitute the
563 stem, re-expand the whole line and chop it into individual
564 prerequisites. Unfortunately this won't work because of the
565 "check_lastslash" twist. Instead, we will have to go word by
566 word, taking $()'s into account. For each word we will
567 substitute the stem, re-expand, chop it up, and, if
568 check_lastslash != 0, add the directory part to each
569 resulting prerequisite. */
570 else
572 int add_dir = 0;
573 unsigned int len;
575 nptr = get_next_word (nptr, &len);
576 if (nptr == 0)
577 continue;
579 /* See this is a transition to order-only prereqs. */
580 if (! order_only && len == 1 && nptr[0] == '|')
582 order_only = 1;
583 nptr += len;
584 continue;
587 /* If the dependency name has %, substitute the stem. If we
588 just replace % with the stem value then later, when we do
589 the 2nd expansion, we will re-expand this stem value
590 again. This is not good if you have certain characters
591 in your stem (like $).
593 Instead, we will replace % with $* and allow the second
594 expansion to take care of it for us. This way (since $*
595 is a simple variable) there won't be additional
596 re-expansion of the stem. */
598 p = lindex (nptr, nptr + len, '%');
599 if (p == 0)
601 memcpy (depname, nptr, len);
602 depname[len] = '\0';
604 else
606 unsigned int i = p - nptr;
607 memcpy (depname, nptr, i);
608 memcpy (depname + i, "$*", 2);
609 memcpy (depname + i + 2, p + 1, len - i - 1);
610 depname[len + 2 - 1] = '\0';
612 if (check_lastslash)
613 add_dir = 1;
616 /* Set file variables. Note that we cannot do it once at the
617 beginning of the function because the stem value changes
618 for each rule. */
619 if (!file_variables_set)
621 set_file_variables (file);
622 file_variables_set = 1;
625 /* Perform the 2nd expansion. */
626 p = variable_expand_for_file (depname, file);
628 /* Parse the expanded string. */
629 dl = PARSE_FILE_SEQ (&p, struct dep, order_only ? '\0' : '|',
630 add_dir ? dir : NULL, 0);
632 for (d = dl; d != NULL; d = d->next)
634 ++deps_found;
635 if (order_only)
636 d->ignore_mtime = 1;
639 /* Set up for the next word. */
640 nptr += len;
643 /* If there are more than max_pattern_deps prerequisites (due to
644 2nd expansion), reset it and realloc the arrays. */
646 if (deps_found > max_pattern_deps)
648 unsigned int l = pat - deplist;
649 deplist = xrealloc (deplist,
650 deps_found * sizeof (struct patdeps));
651 pat = deplist + l;
652 max_pattern_deps = deps_found;
655 /* Go through the nameseq and handle each as a prereq name. */
656 for (d = dl; d != 0; d = d->next)
658 struct dep *expl_d;
659 int is_rule = d->name == dep_name (dep);
661 if (file_impossible_p (d->name))
663 /* If this prereq has already been ruled "impossible",
664 then the rule fails. Don't bother trying it on the
665 second pass either since we know that will fail. */
666 DBS (DB_IMPLICIT,
667 (is_rule
668 ? _("Rejecting impossible rule prerequisite `%s'.\n")
669 : _("Rejecting impossible implicit prerequisite `%s'.\n"),
670 d->name));
671 tryrules[ri].rule = 0;
673 failed = 1;
674 break;
677 memset (pat, '\0', sizeof (struct patdeps));
678 pat->ignore_mtime = d->ignore_mtime;
680 DBS (DB_IMPLICIT,
681 (is_rule
682 ? _("Trying rule prerequisite `%s'.\n")
683 : _("Trying implicit prerequisite `%s'.\n"), d->name));
685 /* If this prereq is also explicitly mentioned for FILE,
686 skip all tests below since it must be built no matter
687 which implicit rule we choose. */
689 for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
690 if (streq (dep_name (expl_d), d->name))
691 break;
692 if (expl_d != 0)
694 (pat++)->name = d->name;
695 continue;
698 /* The DEP->changed flag says that this dependency resides
699 in a nonexistent directory. So we normally can skip
700 looking for the file. However, if CHECK_LASTSLASH is
701 set, then the dependency file we are actually looking for
702 is in a different directory (the one gotten by prepending
703 FILENAME's directory), so it might actually exist. */
705 /* @@ dep->changed check is disabled. */
706 if (lookup_file (d->name) != 0
707 /*|| ((!dep->changed || check_lastslash) && */
708 || file_exists_p (d->name))
710 (pat++)->name = d->name;
711 continue;
714 /* This code, given FILENAME = "lib/foo.o", dependency name
715 "lib/foo.c", and VPATH=src, searches for
716 "src/lib/foo.c". */
718 const char *vname = vpath_search (d->name, 0);
719 if (vname)
721 DBS (DB_IMPLICIT,
722 (_("Found prerequisite `%s' as VPATH `%s'\n"),
723 d->name, vname));
724 (pat++)->name = d->name;
725 continue;
729 /* We could not find the file in any place we should look.
730 Try to make this dependency as an intermediate file, but
731 only on the second pass. */
733 if (intermed_ok)
735 DBS (DB_IMPLICIT,
736 (_("Looking for a rule with intermediate file `%s'.\n"),
737 d->name));
739 if (int_file == 0)
740 int_file = alloca (sizeof (struct file));
741 memset (int_file, '\0', sizeof (struct file));
742 int_file->name = d->name;
744 if (pattern_search (int_file,
746 depth + 1,
747 recursions + 1))
749 pat->pattern = int_file->name;
750 int_file->name = d->name;
751 pat->file = int_file;
752 (pat++)->name = d->name;
753 int_file = 0;
754 continue;
757 /* If we have tried to find P as an intermediate file
758 and failed, mark that name as impossible so we won't
759 go through the search again later. */
760 if (int_file->variables)
761 free_variable_set (int_file->variables);
762 if (int_file->pat_variables)
763 free_variable_set (int_file->pat_variables);
764 file_impossible (d->name);
767 /* A dependency of this rule does not exist. Therefore, this
768 rule fails. */
769 failed = 1;
770 break;
773 /* Free the ns chain. */
774 if (dl != &dep_simple)
775 free_dep_chain (dl);
777 if (failed)
778 break;
781 /* Reset the stem in FILE. */
783 file->stem = 0;
785 /* This rule is no longer `in use' for recursive searches. */
786 rule->in_use = 0;
788 if (! failed)
789 /* This pattern rule does apply. Stop looking for one. */
790 break;
792 /* This pattern rule does not apply. If some of its dependencies
793 succeeded, free the data structure describing them. */
794 /* free_idep_chain (deps); */
795 deps = 0;
798 /* If we found an applicable rule without intermediate files, don't try
799 with them. */
800 if (ri < nrules)
801 break;
803 rule = 0;
806 /* RULE is nil if the loop went through the list but everything failed. */
807 if (rule == 0)
808 goto done;
810 foundrule = ri;
812 /* If we are recursing, store the pattern that matched FILENAME in
813 FILE->name for use in upper levels. */
815 if (recursions > 0)
816 /* Kludge-o-matic */
817 file->name = rule->targets[tryrules[foundrule].matches];
819 /* DEPLIST lists the prerequisites for the rule we found. This includes the
820 intermediate files, if any. Convert them into entries on the deps-chain
821 of FILE. */
823 while (pat-- > deplist)
825 struct dep *dep;
826 const char *s;
828 if (pat->file != 0)
830 /* If we need to use an intermediate file, make sure it is entered
831 as a target, with the info that was found for it in the recursive
832 pattern_search call. We know that the intermediate file did not
833 already exist as a target; therefore we can assume that the deps
834 and cmds of F below are null before we change them. */
836 struct file *imf = pat->file;
837 struct file *f = lookup_file (imf->name);
839 /* We don't want to delete an intermediate file that happened
840 to be a prerequisite of some (other) target. Mark it as
841 precious. */
842 if (f != 0)
843 f->precious = 1;
844 else
845 f = enter_file (imf->name);
847 f->deps = imf->deps;
848 f->cmds = imf->cmds;
849 f->stem = imf->stem;
850 f->variables = imf->variables;
851 f->pat_variables = imf->pat_variables;
852 f->pat_searched = imf->pat_searched;
853 f->also_make = imf->also_make;
854 f->is_target = 1;
855 f->intermediate = 1;
856 f->tried_implicit = 1;
858 imf = lookup_file (pat->pattern);
859 if (imf != 0 && imf->precious)
860 f->precious = 1;
862 for (dep = f->deps; dep != 0; dep = dep->next)
864 dep->file = enter_file (dep->name);
865 dep->name = 0;
866 dep->file->tried_implicit |= dep->changed;
870 dep = alloc_dep ();
871 dep->ignore_mtime = pat->ignore_mtime;
872 s = strcache_add (pat->name);
873 if (recursions)
874 dep->name = s;
875 else
877 dep->file = lookup_file (s);
878 if (dep->file == 0)
879 dep->file = enter_file (s);
882 if (pat->file == 0 && tryrules[foundrule].rule->terminal)
884 /* If the file actually existed (was not an intermediate file), and
885 the rule that found it was a terminal one, then we want to mark
886 the found file so that it will not have implicit rule search done
887 for it. If we are not entering a `struct file' for it now, we
888 indicate this with the `changed' flag. */
889 if (dep->file == 0)
890 dep->changed = 1;
891 else
892 dep->file->tried_implicit = 1;
895 dep->next = file->deps;
896 file->deps = dep;
899 if (!tryrules[foundrule].checked_lastslash)
901 /* Always allocate new storage, since STEM might be on the stack for an
902 intermediate file. */
903 file->stem = strcache_add_len (stem, stemlen);
904 fullstemlen = stemlen;
906 else
908 int dirlen = (lastslash + 1) - filename;
909 char *sp;
911 /* We want to prepend the directory from
912 the original FILENAME onto the stem. */
913 fullstemlen = dirlen + stemlen;
914 sp = alloca (fullstemlen + 1);
915 memcpy (sp, filename, dirlen);
916 memcpy (sp + dirlen, stem, stemlen);
917 sp[fullstemlen] = '\0';
918 file->stem = strcache_add (sp);
921 file->cmds = rule->cmds;
922 file->is_target = 1;
924 /* Set precious flag. */
926 struct file *f = lookup_file (rule->targets[tryrules[foundrule].matches]);
927 if (f && f->precious)
928 file->precious = 1;
931 /* If this rule builds other targets, too, put the others into FILE's
932 `also_make' member. */
934 if (rule->num > 1)
935 for (ri = 0; ri < rule->num; ++ri)
936 if (ri != tryrules[foundrule].matches)
938 char *nm = alloca (rule->lens[ri] + fullstemlen + 1);
939 char *p = nm;
940 struct file *f;
941 struct dep *new = alloc_dep ();
943 /* GKM FIMXE: handle '|' here too */
944 memcpy (p, rule->targets[ri],
945 rule->suffixes[ri] - rule->targets[ri] - 1);
946 p += rule->suffixes[ri] - rule->targets[ri] - 1;
947 memcpy (p, file->stem, fullstemlen);
948 p += fullstemlen;
949 memcpy (p, rule->suffixes[ri],
950 rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
951 new->name = strcache_add (nm);
952 new->file = enter_file (new->name);
953 new->next = file->also_make;
955 /* Set precious flag. */
956 f = lookup_file (rule->targets[ri]);
957 if (f && f->precious)
958 new->file->precious = 1;
960 /* Set the is_target flag so that this file is not treated as
961 intermediate by the pattern rule search algorithm and
962 file_exists_p cannot pick it up yet. */
963 new->file->is_target = 1;
965 file->also_make = new;
968 done:
969 free (tryrules);
970 free (deplist);
972 return rule != 0;