Fix some memory leaks, found with valgrind.
[make.git] / implicit.c
blobc3e35f7e08842cf5a2723706d6794d80a88004ba
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 /* Nonzero if the LASTSLASH logic was used in matching this rule. */
171 char checked_lastslash;
174 /* Search the pattern rules for a rule with an existing dependency to make
175 FILE. If a rule is found, the appropriate commands and deps are put in FILE
176 and 1 is returned. If not, 0 is returned.
178 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
179 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
180 directory and filename parts.
182 If an intermediate file is found by pattern search, the intermediate file
183 is set up as a target by the recursive call and is also made a dependency
184 of FILE.
186 DEPTH is used for debugging messages. */
188 static int
189 pattern_search (struct file *file, int archive,
190 unsigned int depth, unsigned int recursions)
192 /* Filename we are searching for a rule for. */
193 const char *filename = archive ? strchr (file->name, '(') : file->name;
195 /* Length of FILENAME. */
196 unsigned int namelen = strlen (filename);
198 /* The last slash in FILENAME (or nil if there is none). */
199 char *lastslash;
201 /* This is a file-object used as an argument in
202 recursive calls. It never contains any data
203 except during a recursive call. */
204 struct file *int_file = 0;
206 /* List of dependencies found recursively. */
207 struct patdeps *deplist
208 = xmalloc (max_pattern_deps * sizeof (struct patdeps));
209 struct patdeps *pat = deplist;
211 /* All the prerequisites actually found for a rule, after expansion. */
212 struct dep *deps;
214 /* Names of possible dependencies are constructed in this buffer. */
215 char *depname = alloca (namelen + max_pattern_dep_length);
217 /* The start and length of the stem of FILENAME for the current rule. */
218 const char *stem = 0;
219 unsigned int stemlen = 0;
220 unsigned int fullstemlen = 0;
222 /* Buffer in which we store all the rules that are possibly applicable. */
223 struct tryrule *tryrules = xmalloc (num_pattern_rules * max_pattern_targets
224 * sizeof (struct tryrule));
226 /* Number of valid elements in TRYRULES. */
227 unsigned int nrules;
229 /* The index in TRYRULES of the rule we found. */
230 unsigned int foundrule;
232 /* Nonzero if should consider intermediate files as dependencies. */
233 int intermed_ok;
235 /* Nonzero if we have matched a pattern-rule target
236 that is not just `%'. */
237 int specific_rule_matched = 0;
239 struct dep dep_simple;
241 unsigned int ri; /* uninit checks OK */
242 struct rule *rule;
244 char *pathdir = NULL;
245 unsigned long pathlen;
247 PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
249 #ifndef NO_ARCHIVES
250 if (archive || ar_name (filename))
251 lastslash = 0;
252 else
253 #endif
255 /* Set LASTSLASH to point at the last slash in FILENAME
256 but not counting any slash at the end. (foo/bar/ counts as
257 bar/ in directory foo/, not empty in directory foo/bar/.) */
258 #ifdef VMS
259 lastslash = strrchr (filename, ']');
260 if (lastslash == 0)
261 lastslash = strrchr (filename, ':');
262 #else
263 lastslash = strrchr (filename, '/');
264 #ifdef HAVE_DOS_PATHS
265 /* Handle backslashes (possibly mixed with forward slashes)
266 and the case of "d:file". */
268 char *bslash = strrchr (filename, '\\');
269 if (lastslash == 0 || bslash > lastslash)
270 lastslash = bslash;
271 if (lastslash == 0 && filename[0] && filename[1] == ':')
272 lastslash = filename + 1;
274 #endif
275 #endif
276 if (lastslash != 0 && lastslash[1] == '\0')
277 lastslash = 0;
280 pathlen = lastslash - filename + 1;
282 /* First see which pattern rules match this target and may be considered.
283 Put them in TRYRULES. */
285 nrules = 0;
286 for (rule = pattern_rules; rule != 0; rule = rule->next)
288 unsigned int ti;
290 /* If the pattern rule has deps but no commands, ignore it.
291 Users cancel built-in rules by redefining them without commands. */
292 if (rule->deps != 0 && rule->cmds == 0)
293 continue;
295 /* If this rule is in use by a parent pattern_search,
296 don't use it here. */
297 if (rule->in_use)
299 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
300 continue;
303 for (ti = 0; ti < rule->num; ++ti)
305 const char *target = rule->targets[ti];
306 const char *suffix = rule->suffixes[ti];
307 int check_lastslash;
309 /* Rules that can match any filename and are not terminal
310 are ignored if we're recursing, so that they cannot be
311 intermediate files. */
312 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
313 continue;
315 if (rule->lens[ti] > namelen)
316 /* It can't possibly match. */
317 continue;
319 /* From the lengths of the filename and the pattern parts,
320 find the stem: the part of the filename that matches the %. */
321 stem = filename + (suffix - target - 1);
322 stemlen = namelen - rule->lens[ti] + 1;
324 /* Set CHECK_LASTSLASH if FILENAME contains a directory
325 prefix and the target pattern does not contain a slash. */
327 check_lastslash = 0;
328 if (lastslash)
330 #ifdef VMS
331 check_lastslash = (strchr (target, ']') == 0
332 && strchr (target, ':') == 0);
333 #else
334 check_lastslash = strchr (target, '/') == 0;
335 #ifdef HAVE_DOS_PATHS
336 /* Didn't find it yet: check for DOS-type directories. */
337 if (check_lastslash)
339 char *b = strchr (target, '\\');
340 check_lastslash = !(b || (target[0] && target[1] == ':'));
342 #endif
343 #endif
345 if (check_lastslash)
347 /* If so, don't include the directory prefix in STEM here. */
348 if (pathlen > stemlen)
349 continue;
350 stemlen -= pathlen;
351 stem += pathlen;
354 /* Check that the rule pattern matches the text before the stem. */
355 if (check_lastslash)
357 if (stem > (lastslash + 1)
358 && !strneq (target, lastslash + 1, stem - lastslash - 1))
359 continue;
361 else if (stem > filename
362 && !strneq (target, filename, stem - filename))
363 continue;
365 /* Check that the rule pattern matches the text after the stem.
366 We could test simply use streq, but this way we compare the
367 first two characters immediately. This saves time in the very
368 common case where the first character matches because it is a
369 period. */
370 if (*suffix != stem[stemlen]
371 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
372 continue;
374 /* Record if we match a rule that not all filenames will match. */
375 if (target[1] != '\0')
376 specific_rule_matched = 1;
378 /* A rule with no dependencies and no commands exists solely to set
379 specific_rule_matched when it matches. Don't try to use it. */
380 if (rule->deps == 0 && rule->cmds == 0)
381 continue;
383 /* Record this rule in TRYRULES and the index of the matching
384 target in MATCHES. If several targets of the same rule match,
385 that rule will be in TRYRULES more than once. */
386 tryrules[nrules].rule = rule;
387 tryrules[nrules].matches = ti;
388 tryrules[nrules].checked_lastslash = check_lastslash;
389 ++nrules;
393 /* Bail out early if we haven't found any rules. */
394 if (nrules == 0)
395 goto done;
397 /* If we have found a matching rule that won't match all filenames,
398 retroactively reject any non-"terminal" rules that do always match. */
399 if (specific_rule_matched)
400 for (ri = 0; ri < nrules; ++ri)
401 if (!tryrules[ri].rule->terminal)
403 unsigned int j;
404 for (j = 0; j < tryrules[ri].rule->num; ++j)
405 if (tryrules[ri].rule->targets[j][1] == '\0')
407 tryrules[ri].rule = 0;
408 break;
412 /* We are going to do second expansion so initialize file variables
413 for the rule. */
414 initialize_file_variables (file, 0);
416 /* Try each rule once without intermediate files, then once with them. */
417 for (intermed_ok = 0; intermed_ok < 2; ++intermed_ok)
419 pat = deplist;
421 /* Try each pattern rule till we find one that applies. If it does,
422 expand its dependencies (as substituted) and chain them in DEPS. */
423 for (ri = 0; ri < nrules; ri++)
425 struct dep *dep;
426 int check_lastslash;
427 unsigned int failed = 0;
428 int file_variables_set = 0;
429 unsigned int deps_found = 0;
430 /* NPTR points to the part of the prereq we haven't processed. */
431 const char *nptr = 0;
432 const char *dir = NULL;
433 int order_only = 0;
434 unsigned int matches;
436 rule = tryrules[ri].rule;
438 /* RULE is nil when we discover that a rule, already placed in
439 TRYRULES, should not be applied. */
440 if (rule == 0)
441 continue;
443 /* Reject any terminal rules if we're looking to make intermediate
444 files. */
445 if (intermed_ok && rule->terminal)
446 continue;
448 /* From the lengths of the filename and the matching pattern parts,
449 find the stem: the part of the filename that matches the %. */
450 matches = tryrules[ri].matches;
451 stem = filename + (rule->suffixes[matches]
452 - rule->targets[matches]) - 1;
453 stemlen = (namelen - rule->lens[matches]) + 1;
454 check_lastslash = tryrules[ri].checked_lastslash;
455 if (check_lastslash)
457 stem += pathlen;
458 stemlen -= pathlen;
460 /* We need to add the directory prefix, so set it up. */
461 if (! pathdir)
463 pathdir = alloca (pathlen + 1);
464 memcpy (pathdir, filename, pathlen);
465 pathdir[pathlen] = '\0';
467 dir = pathdir;
470 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
471 (int) stemlen, stem));
473 strncpy (stem_str, stem, stemlen);
474 stem_str[stemlen] = '\0';
476 /* If there are no prerequisites, then this rule matches. */
477 if (rule->deps == 0)
478 break;
480 /* Temporary assign STEM to file->stem (needed to set file
481 variables below). */
482 file->stem = stem_str;
484 /* Mark this rule as in use so a recursive pattern_search won't try
485 to use it. */
486 rule->in_use = 1;
488 /* Try each prerequisite; see if it exists or can be created. We'll
489 build a list of prereq info in DEPLIST. Due to 2nd expansion we
490 may have to process multiple prereqs for a single dep entry. */
492 pat = deplist;
493 dep = rule->deps;
494 nptr = dep_name (dep);
495 while (1)
497 struct dep *dl, *d;
498 char *p;
500 /* If we're out of name to parse, start the next prereq. */
501 if (! nptr)
503 dep = dep->next;
504 if (dep == 0)
505 break;
506 nptr = dep_name (dep);
509 /* If we don't need a second expansion, just replace the %. */
510 if (! dep->need_2nd_expansion)
512 dep_simple = *dep;
513 dep_simple.next = 0;
514 p = strchr (nptr, '%');
515 if (p == 0)
516 dep_simple.name = nptr;
517 else
519 char *o = depname;
520 if (check_lastslash)
522 memcpy (o, filename, pathlen);
523 o += pathlen;
525 memcpy (o, nptr, p - nptr);
526 o += p - nptr;
527 memcpy (o, stem_str, stemlen);
528 o += stemlen;
529 strcpy (o, p + 1);
530 dep_simple.name = strcache_add (depname);
532 dl = &dep_simple;
534 /* We've used up this dep, so next time get a new one. */
535 nptr = 0;
536 ++deps_found;
539 /* We have to perform second expansion on this prereq. In an
540 ideal world we would take the dependency line, substitute the
541 stem, re-expand the whole line and chop it into individual
542 prerequisites. Unfortunately this won't work because of the
543 "check_lastslash" twist. Instead, we will have to go word by
544 word, taking $()'s into account. For each word we will
545 substitute the stem, re-expand, chop it up, and, if
546 check_lastslash != 0, add the directory part to each
547 resulting prerequisite. */
548 else
550 int add_dir = 0;
551 unsigned int len;
553 nptr = get_next_word (nptr, &len);
554 if (nptr == 0)
555 continue;
557 /* See this is a transition to order-only prereqs. */
558 if (! order_only && len == 1 && nptr[0] == '|')
560 order_only = 1;
561 nptr += len;
562 continue;
565 /* If the dependency name has %, substitute the stem. If we
566 just replace % with the stem value then later, when we do
567 the 2nd expansion, we will re-expand this stem value
568 again. This is not good if you have certain characters
569 in your stem (like $).
571 Instead, we will replace % with $* and allow the second
572 expansion to take care of it for us. This way (since $*
573 is a simple variable) there won't be additional
574 re-expansion of the stem. */
576 p = lindex (nptr, nptr + len, '%');
577 if (p == 0)
579 memcpy (depname, nptr, len);
580 depname[len] = '\0';
582 else
584 unsigned int i = p - nptr;
585 memcpy (depname, nptr, i);
586 memcpy (depname + i, "$*", 2);
587 memcpy (depname + i + 2, p + 1, len - i - 1);
588 depname[len + 2 - 1] = '\0';
590 if (check_lastslash)
591 add_dir = 1;
594 /* Set file variables. Note that we cannot do it once at the
595 beginning of the function because the stem value changes
596 for each rule. */
597 if (!file_variables_set)
599 set_file_variables (file);
600 file_variables_set = 1;
603 /* Perform the 2nd expansion. */
604 p = variable_expand_for_file (depname, file);
606 /* Parse the expanded string. */
607 dl = PARSE_FILE_SEQ (&p, struct dep, order_only ? '\0' : '|',
608 add_dir ? dir : NULL, 0);
610 for (d = dl; d != NULL; d = d->next)
612 ++deps_found;
613 if (order_only)
614 d->ignore_mtime = 1;
617 /* Set up for the next word. */
618 nptr += len;
621 /* If there are more than max_pattern_deps prerequisites (due to
622 2nd expansion), reset it and realloc the arrays. */
624 if (deps_found > max_pattern_deps)
626 unsigned int l = pat - deplist;
627 deplist = xrealloc (deplist,
628 deps_found * sizeof (struct patdeps));
629 pat = deplist + l;
630 max_pattern_deps = deps_found;
633 /* Go through the nameseq and handle each as a prereq name. */
634 for (d = dl; d != 0; d = d->next)
636 struct dep *expl_d;
637 int is_rule = d->name == dep_name (dep);
639 if (file_impossible_p (d->name))
641 /* If this prereq has already been ruled "impossible",
642 then the rule fails. Don't bother trying it on the
643 second pass either since we know that will fail. */
644 DBS (DB_IMPLICIT,
645 (is_rule
646 ? _("Rejecting impossible rule prerequisite `%s'.\n")
647 : _("Rejecting impossible implicit prerequisite `%s'.\n"),
648 d->name));
649 tryrules[ri].rule = 0;
651 failed = 1;
652 break;
655 memset (pat, '\0', sizeof (struct patdeps));
656 pat->ignore_mtime = d->ignore_mtime;
658 DBS (DB_IMPLICIT,
659 (is_rule
660 ? _("Trying rule prerequisite `%s'.\n")
661 : _("Trying implicit prerequisite `%s'.\n"), d->name));
663 /* If this prereq is also explicitly mentioned for FILE,
664 skip all tests below since it must be built no matter
665 which implicit rule we choose. */
667 for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
668 if (streq (dep_name (expl_d), d->name))
669 break;
670 if (expl_d != 0)
672 (pat++)->name = d->name;
673 continue;
676 /* The DEP->changed flag says that this dependency resides
677 in a nonexistent directory. So we normally can skip
678 looking for the file. However, if CHECK_LASTSLASH is
679 set, then the dependency file we are actually looking for
680 is in a different directory (the one gotten by prepending
681 FILENAME's directory), so it might actually exist. */
683 /* @@ dep->changed check is disabled. */
684 if (lookup_file (d->name) != 0
685 /*|| ((!dep->changed || check_lastslash) && */
686 || file_exists_p (d->name))
688 (pat++)->name = d->name;
689 continue;
692 /* This code, given FILENAME = "lib/foo.o", dependency name
693 "lib/foo.c", and VPATH=src, searches for
694 "src/lib/foo.c". */
696 const char *vname = vpath_search (d->name, 0);
697 if (vname)
699 DBS (DB_IMPLICIT,
700 (_("Found prerequisite `%s' as VPATH `%s'\n"),
701 d->name, vname));
702 (pat++)->name = d->name;
703 continue;
707 /* We could not find the file in any place we should look.
708 Try to make this dependency as an intermediate file, but
709 only on the second pass. */
711 if (intermed_ok)
713 DBS (DB_IMPLICIT,
714 (_("Looking for a rule with intermediate file `%s'.\n"),
715 d->name));
717 if (int_file == 0)
718 int_file = alloca (sizeof (struct file));
719 memset (int_file, '\0', sizeof (struct file));
720 int_file->name = d->name;
722 if (pattern_search (int_file,
724 depth + 1,
725 recursions + 1))
727 pat->pattern = int_file->name;
728 int_file->name = d->name;
729 pat->file = int_file;
730 (pat++)->name = d->name;
731 int_file = 0;
732 continue;
735 /* If we have tried to find P as an intermediate file
736 and failed, mark that name as impossible so we won't
737 go through the search again later. */
738 if (int_file->variables)
739 free_variable_set (int_file->variables);
740 if (int_file->pat_variables)
741 free_variable_set (int_file->pat_variables);
742 file_impossible (d->name);
745 /* A dependency of this rule does not exist. Therefore, this
746 rule fails. */
747 failed = 1;
748 break;
751 /* Free the ns chain. */
752 if (dl != &dep_simple)
753 free_dep_chain (dl);
755 if (failed)
756 break;
759 /* Reset the stem in FILE. */
761 file->stem = 0;
763 /* This rule is no longer `in use' for recursive searches. */
764 rule->in_use = 0;
766 if (! failed)
767 /* This pattern rule does apply. Stop looking for one. */
768 break;
770 /* This pattern rule does not apply. If some of its dependencies
771 succeeded, free the data structure describing them. */
772 /* free_idep_chain (deps); */
773 deps = 0;
776 /* If we found an applicable rule without intermediate files, don't try
777 with them. */
778 if (ri < nrules)
779 break;
781 rule = 0;
784 /* RULE is nil if the loop went through the list but everything failed. */
785 if (rule == 0)
786 goto done;
788 foundrule = ri;
790 /* If we are recursing, store the pattern that matched FILENAME in
791 FILE->name for use in upper levels. */
793 if (recursions > 0)
794 /* Kludge-o-matic */
795 file->name = rule->targets[tryrules[foundrule].matches];
797 /* DEPLIST lists the prerequisites for the rule we found. This includes the
798 intermediate files, if any. Convert them into entries on the deps-chain
799 of FILE. */
801 while (pat-- > deplist)
803 struct dep *dep;
804 const char *s;
806 if (pat->file != 0)
808 /* If we need to use an intermediate file, make sure it is entered
809 as a target, with the info that was found for it in the recursive
810 pattern_search call. We know that the intermediate file did not
811 already exist as a target; therefore we can assume that the deps
812 and cmds of F below are null before we change them. */
814 struct file *imf = pat->file;
815 struct file *f = lookup_file (imf->name);
817 /* We don't want to delete an intermediate file that happened
818 to be a prerequisite of some (other) target. Mark it as
819 precious. */
820 if (f != 0)
821 f->precious = 1;
822 else
823 f = enter_file (imf->name);
825 f->deps = imf->deps;
826 f->cmds = imf->cmds;
827 f->stem = imf->stem;
828 f->variables = imf->variables;
829 f->pat_variables = imf->pat_variables;
830 f->pat_searched = imf->pat_searched;
831 f->also_make = imf->also_make;
832 f->is_target = 1;
833 f->intermediate = 1;
834 f->tried_implicit = 1;
836 imf = lookup_file (pat->pattern);
837 if (imf != 0 && imf->precious)
838 f->precious = 1;
840 for (dep = f->deps; dep != 0; dep = dep->next)
842 dep->file = enter_file (dep->name);
843 dep->name = 0;
844 dep->file->tried_implicit |= dep->changed;
848 dep = alloc_dep ();
849 dep->ignore_mtime = pat->ignore_mtime;
850 s = strcache_add (pat->name);
851 if (recursions)
852 dep->name = s;
853 else
855 dep->file = lookup_file (s);
856 if (dep->file == 0)
857 dep->file = enter_file (s);
860 if (pat->file == 0 && tryrules[foundrule].rule->terminal)
862 /* If the file actually existed (was not an intermediate file), and
863 the rule that found it was a terminal one, then we want to mark
864 the found file so that it will not have implicit rule search done
865 for it. If we are not entering a `struct file' for it now, we
866 indicate this with the `changed' flag. */
867 if (dep->file == 0)
868 dep->changed = 1;
869 else
870 dep->file->tried_implicit = 1;
873 dep->next = file->deps;
874 file->deps = dep;
877 if (!tryrules[foundrule].checked_lastslash)
879 /* Always allocate new storage, since STEM might be on the stack for an
880 intermediate file. */
881 file->stem = strcache_add_len (stem, stemlen);
882 fullstemlen = stemlen;
884 else
886 int dirlen = (lastslash + 1) - filename;
887 char *sp;
889 /* We want to prepend the directory from
890 the original FILENAME onto the stem. */
891 fullstemlen = dirlen + stemlen;
892 sp = alloca (fullstemlen + 1);
893 memcpy (sp, filename, dirlen);
894 memcpy (sp + dirlen, stem, stemlen);
895 sp[fullstemlen] = '\0';
896 file->stem = strcache_add (sp);
899 file->cmds = rule->cmds;
900 file->is_target = 1;
902 /* Set precious flag. */
904 struct file *f = lookup_file (rule->targets[tryrules[foundrule].matches]);
905 if (f && f->precious)
906 file->precious = 1;
909 /* If this rule builds other targets, too, put the others into FILE's
910 `also_make' member. */
912 if (rule->num > 1)
913 for (ri = 0; ri < rule->num; ++ri)
914 if (ri != tryrules[foundrule].matches)
916 char *nm = alloca (rule->lens[ri] + fullstemlen + 1);
917 char *p = nm;
918 struct file *f;
919 struct dep *new = alloc_dep ();
921 /* GKM FIMXE: handle '|' here too */
922 memcpy (p, rule->targets[ri],
923 rule->suffixes[ri] - rule->targets[ri] - 1);
924 p += rule->suffixes[ri] - rule->targets[ri] - 1;
925 memcpy (p, file->stem, fullstemlen);
926 p += fullstemlen;
927 memcpy (p, rule->suffixes[ri],
928 rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
929 new->name = strcache_add (nm);
930 new->file = enter_file (new->name);
931 new->next = file->also_make;
933 /* Set precious flag. */
934 f = lookup_file (rule->targets[ri]);
935 if (f && f->precious)
936 new->file->precious = 1;
938 /* Set the is_target flag so that this file is not treated as
939 intermediate by the pattern rule search algorithm and
940 file_exists_p cannot pick it up yet. */
941 new->file->is_target = 1;
943 file->also_make = new;
946 done:
947 free (tryrules);
948 free (deplist);
950 return rule != 0;