- Fix broken handling of order-only prereqs in secondary expansion
[make.git] / implicit.c
blob0bb6f56dd4ea2f6ed3fa9fa97bab383ad6e6400f
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 /* Search the pattern rules for a rule with an existing dependency to make
161 FILE. If a rule is found, the appropriate commands and deps are put in FILE
162 and 1 is returned. If not, 0 is returned.
164 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
165 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
166 directory and filename parts.
168 If an intermediate file is found by pattern search, the intermediate file
169 is set up as a target by the recursive call and is also made a dependency
170 of FILE.
172 DEPTH is used for debugging messages. */
174 static int
175 pattern_search (struct file *file, int archive,
176 unsigned int depth, unsigned int recursions)
178 /* Filename we are searching for a rule for. */
179 const char *filename = archive ? strchr (file->name, '(') : file->name;
181 /* Length of FILENAME. */
182 unsigned int namelen = strlen (filename);
184 /* The last slash in FILENAME (or nil if there is none). */
185 char *lastslash;
187 /* This is a file-object used as an argument in
188 recursive calls. It never contains any data
189 except during a recursive call. */
190 struct file *int_file = 0;
192 /* List of dependencies found recursively. */
193 struct patdeps *deplist
194 = xmalloc (max_pattern_deps * sizeof (struct patdeps));
195 struct patdeps *pat = deplist;
197 /* All the prerequisites actually found for a rule, after expansion. */
198 struct dep *deps;
200 /* Names of possible dependencies are constructed in this buffer. */
201 char *depname = alloca (namelen + max_pattern_dep_length);
203 /* The start and length of the stem of FILENAME for the current rule. */
204 const char *stem = 0;
205 unsigned int stemlen = 0;
206 unsigned int fullstemlen = 0;
208 /* Buffer in which we store all the rules that are possibly applicable. */
209 struct rule **tryrules = xmalloc (num_pattern_rules * max_pattern_targets
210 * sizeof (struct rule *));
212 /* Number of valid elements in TRYRULES. */
213 unsigned int nrules;
215 /* The numbers of the rule targets of each rule
216 in TRYRULES that matched the target file. */
217 unsigned int *matches = alloca (num_pattern_rules * sizeof (unsigned int));
219 /* Each element is nonzero if LASTSLASH was used in
220 matching the corresponding element of TRYRULES. */
221 char *checked_lastslash = alloca (num_pattern_rules * sizeof (char));
223 /* The index in TRYRULES of the rule we found. */
224 unsigned int foundrule;
226 /* Nonzero if should consider intermediate files as dependencies. */
227 int intermed_ok;
229 /* Nonzero if we have matched a pattern-rule target
230 that is not just `%'. */
231 int specific_rule_matched = 0;
233 struct dep dep_simple;
235 unsigned int ri; /* uninit checks OK */
236 struct rule *rule;
238 char *pathdir = NULL;
239 unsigned long pathlen;
241 PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
243 #ifndef NO_ARCHIVES
244 if (archive || ar_name (filename))
245 lastslash = 0;
246 else
247 #endif
249 /* Set LASTSLASH to point at the last slash in FILENAME
250 but not counting any slash at the end. (foo/bar/ counts as
251 bar/ in directory foo/, not empty in directory foo/bar/.) */
252 #ifdef VMS
253 lastslash = strrchr (filename, ']');
254 if (lastslash == 0)
255 lastslash = strrchr (filename, ':');
256 #else
257 lastslash = strrchr (filename, '/');
258 #ifdef HAVE_DOS_PATHS
259 /* Handle backslashes (possibly mixed with forward slashes)
260 and the case of "d:file". */
262 char *bslash = strrchr (filename, '\\');
263 if (lastslash == 0 || bslash > lastslash)
264 lastslash = bslash;
265 if (lastslash == 0 && filename[0] && filename[1] == ':')
266 lastslash = filename + 1;
268 #endif
269 #endif
270 if (lastslash != 0 && lastslash[1] == '\0')
271 lastslash = 0;
274 pathlen = lastslash - filename + 1;
276 /* First see which pattern rules match this target and may be considered.
277 Put them in TRYRULES. */
279 nrules = 0;
280 for (rule = pattern_rules; rule != 0; rule = rule->next)
282 unsigned int ti;
284 /* If the pattern rule has deps but no commands, ignore it.
285 Users cancel built-in rules by redefining them without commands. */
286 if (rule->deps != 0 && rule->cmds == 0)
287 continue;
289 /* If this rule is in use by a parent pattern_search,
290 don't use it here. */
291 if (rule->in_use)
293 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
294 continue;
297 for (ti = 0; ti < rule->num; ++ti)
299 const char *target = rule->targets[ti];
300 const char *suffix = rule->suffixes[ti];
301 int check_lastslash;
303 /* Rules that can match any filename and are not terminal
304 are ignored if we're recursing, so that they cannot be
305 intermediate files. */
306 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
307 continue;
309 if (rule->lens[ti] > namelen)
310 /* It can't possibly match. */
311 continue;
313 /* From the lengths of the filename and the pattern parts,
314 find the stem: the part of the filename that matches the %. */
315 stem = filename + (suffix - target - 1);
316 stemlen = namelen - rule->lens[ti] + 1;
318 /* Set CHECK_LASTSLASH if FILENAME contains a directory
319 prefix and the target pattern does not contain a slash. */
321 check_lastslash = 0;
322 if (lastslash)
324 #ifdef VMS
325 check_lastslash = (strchr (target, ']') == 0
326 && strchr (target, ':') == 0);
327 #else
328 check_lastslash = strchr (target, '/') == 0;
329 #ifdef HAVE_DOS_PATHS
330 /* Didn't find it yet: check for DOS-type directories. */
331 if (check_lastslash)
333 char *b = strchr (target, '\\');
334 check_lastslash = !(b || (target[0] && target[1] == ':'));
336 #endif
337 #endif
339 if (check_lastslash)
341 /* If so, don't include the directory prefix in STEM here. */
342 if (pathlen > stemlen)
343 continue;
344 stemlen -= pathlen;
345 stem += pathlen;
348 /* Check that the rule pattern matches the text before the stem. */
349 if (check_lastslash)
351 if (stem > (lastslash + 1)
352 && !strneq (target, lastslash + 1, stem - lastslash - 1))
353 continue;
355 else if (stem > filename
356 && !strneq (target, filename, stem - filename))
357 continue;
359 /* Check that the rule pattern matches the text after the stem.
360 We could test simply use streq, but this way we compare the
361 first two characters immediately. This saves time in the very
362 common case where the first character matches because it is a
363 period. */
364 if (*suffix != stem[stemlen]
365 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
366 continue;
368 /* Record if we match a rule that not all filenames will match. */
369 if (target[1] != '\0')
370 specific_rule_matched = 1;
372 /* A rule with no dependencies and no commands exists solely to set
373 specific_rule_matched when it matches. Don't try to use it. */
374 if (rule->deps == 0 && rule->cmds == 0)
375 continue;
377 /* Record this rule in TRYRULES and the index of the matching
378 target in MATCHES. If several targets of the same rule match,
379 that rule will be in TRYRULES more than once. */
380 tryrules[nrules] = rule;
381 matches[nrules] = ti;
382 checked_lastslash[nrules] = check_lastslash;
383 ++nrules;
387 /* If we have found a matching rule that won't match all filenames,
388 retroactively reject any non-"terminal" rules that do always match. */
389 if (specific_rule_matched)
390 for (ri = 0; ri < nrules; ++ri)
391 if (!tryrules[ri]->terminal)
393 unsigned int j;
394 for (j = 0; j < tryrules[ri]->num; ++j)
395 if (tryrules[ri]->targets[j][1] == '\0')
397 tryrules[ri] = 0;
398 break;
402 /* We are going to do second expansion so initialize file variables
403 for the rule. */
404 initialize_file_variables (file, 0);
406 /* Try each rule once without intermediate files, then once with them. */
407 for (intermed_ok = 0; intermed_ok < 2; ++intermed_ok)
409 pat = deplist;
411 /* Try each pattern rule till we find one that applies. If it does,
412 expand its dependencies (as substituted) and chain them in DEPS. */
413 for (ri = 0; ri < nrules; ri++)
415 struct dep *dep;
416 int check_lastslash;
417 unsigned int failed = 0;
418 int file_variables_set = 0;
419 unsigned int deps_found = 0;
420 /* NPTR points to the part of the prereq we haven't processed. */
421 const char *nptr = 0;
422 const char *dir = NULL;
423 int order_only = 0;
425 rule = tryrules[ri];
427 /* RULE is nil when we discover that a rule, already placed in
428 TRYRULES, should not be applied. */
429 if (rule == 0)
430 continue;
432 /* Reject any terminal rules if we're looking to make intermediate
433 files. */
434 if (intermed_ok && rule->terminal)
435 continue;
437 /* From the lengths of the filename and the matching pattern parts,
438 find the stem: the part of the filename that matches the %. */
439 stem = filename + (rule->suffixes[matches[ri]]
440 - rule->targets[matches[ri]]) - 1;
441 stemlen = (namelen - rule->lens[matches[ri]]) + 1;
442 check_lastslash = checked_lastslash[ri];
443 if (check_lastslash)
445 stem += pathlen;
446 stemlen -= pathlen;
448 /* We need to add the directory prefix, so set it up. */
449 if (! pathdir)
451 pathdir = alloca (pathlen + 1);
452 memcpy (pathdir, filename, pathlen);
453 pathdir[pathlen] = '\0';
455 dir = pathdir;
458 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
459 (int) stemlen, stem));
461 strncpy (stem_str, stem, stemlen);
462 stem_str[stemlen] = '\0';
464 /* If there are no prerequisites, then this rule matches. */
465 if (rule->deps == 0)
466 break;
468 /* Temporary assign STEM to file->stem (needed to set file
469 variables below). */
470 file->stem = stem_str;
472 /* Mark this rule as in use so a recursive pattern_search won't try
473 to use it. */
474 rule->in_use = 1;
476 /* Try each prerequisite; see if it exists or can be created. We'll
477 build a list of prereq info in DEPLIST. Due to 2nd expansion we
478 may have to process multiple prereqs for a single dep entry. */
480 pat = deplist;
481 dep = rule->deps;
482 nptr = dep_name (dep);
483 while (1)
485 struct dep *dl, *d;
486 char *p;
488 /* If we're out of name to parse, start the next prereq. */
489 if (! nptr)
491 dep = dep->next;
492 if (dep == 0)
493 break;
494 nptr = dep_name (dep);
497 /* If we don't need a second expansion, just replace the %. */
498 if (! dep->need_2nd_expansion)
500 dep_simple = *dep;
501 dep_simple.next = 0;
502 p = strchr (nptr, '%');
503 if (p == 0)
504 dep_simple.name = nptr;
505 else
507 char *o = depname;
508 if (check_lastslash)
510 memcpy (o, filename, pathlen);
511 o += pathlen;
513 memcpy (o, nptr, p - nptr);
514 o += p - nptr;
515 memcpy (o, stem_str, stemlen);
516 o += stemlen;
517 strcpy (o, p + 1);
518 dep_simple.name = strcache_add (depname);
520 dl = &dep_simple;
522 /* We've used up this dep, so next time get a new one. */
523 nptr = 0;
524 ++deps_found;
527 /* We have to perform second expansion on this prereq. In an
528 ideal world we would take the dependency line, substitute the
529 stem, re-expand the whole line and chop it into individual
530 prerequisites. Unfortunately this won't work because of the
531 "check_lastslash" twist. Instead, we will have to go word by
532 word, taking $()'s into account. For each word we will
533 substitute the stem, re-expand, chop it up, and, if
534 check_lastslash != 0, add the directory part to each
535 resulting prerequisite. */
536 else
538 int add_dir = 0;
539 unsigned int len;
541 nptr = get_next_word (nptr, &len);
542 if (nptr == 0)
543 continue;
545 /* See this is a transition to order-only prereqs. */
546 if (! order_only && len == 1 && nptr[0] == '|')
548 order_only = 1;
549 nptr += len;
550 continue;
553 /* If the dependency name has %, substitute the stem. If we
554 just replace % with the stem value then later, when we do
555 the 2nd expansion, we will re-expand this stem value
556 again. This is not good if you have certain characters
557 in your stem (like $).
559 Instead, we will replace % with $* and allow the second
560 expansion to take care of it for us. This way (since $*
561 is a simple variable) there won't be additional
562 re-expansion of the stem. */
564 p = lindex (nptr, nptr + len, '%');
565 if (p == 0)
567 memcpy (depname, nptr, len);
568 depname[len] = '\0';
570 else
572 unsigned int i = p - nptr;
573 memcpy (depname, nptr, i);
574 memcpy (depname + i, "$*", 2);
575 memcpy (depname + i + 2, p + 1, len - i - 1);
576 depname[len + 2 - 1] = '\0';
578 if (check_lastslash)
579 add_dir = 1;
582 /* Set file variables. Note that we cannot do it once at the
583 beginning of the function because the stem value changes
584 for each rule. */
585 if (!file_variables_set)
587 set_file_variables (file);
588 file_variables_set = 1;
591 /* Perform the 2nd expansion. */
592 p = variable_expand_for_file (depname, file);
594 /* Parse the expanded string. */
595 dl = PARSE_FILE_SEQ (&p, struct dep, order_only ? '\0' : '|',
596 add_dir ? dir : NULL, 0);
598 for (d = dl; d != NULL; d = d->next)
600 ++deps_found;
601 if (order_only)
602 d->ignore_mtime = 1;
605 /* Set up for the next word. */
606 nptr += len;
609 /* If there are more than max_pattern_deps prerequisites (due to
610 2nd expansion), reset it and realloc the arrays. */
612 if (deps_found > max_pattern_deps)
614 unsigned int l = pat - deplist;
615 deplist = xrealloc (deplist,
616 deps_found * sizeof (struct patdeps));
617 pat = deplist + l;
618 max_pattern_deps = deps_found;
621 /* Go through the nameseq and handle each as a prereq name. */
622 for (d = dl; d != 0; d = d->next)
624 struct dep *expl_d;
625 int is_rule = d->name == dep_name (dep);
627 if (file_impossible_p (d->name))
629 /* If this prereq has already been ruled "impossible",
630 then the rule fails. Don't bother trying it on the
631 second pass either since we know that will fail. */
632 DBS (DB_IMPLICIT,
633 (is_rule
634 ? _("Rejecting impossible rule prerequisite `%s'.\n")
635 : _("Rejecting impossible implicit prerequisite `%s'.\n"),
636 d->name));
637 tryrules[ri] = 0;
639 failed = 1;
640 break;
643 memset (pat, '\0', sizeof (struct patdeps));
644 pat->ignore_mtime = d->ignore_mtime;
646 DBS (DB_IMPLICIT,
647 (is_rule
648 ? _("Trying rule prerequisite `%s'.\n")
649 : _("Trying implicit prerequisite `%s'.\n"), d->name));
651 /* If this prereq is also explicitly mentioned for FILE,
652 skip all tests below since it must be built no matter
653 which implicit rule we choose. */
655 for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
656 if (streq (dep_name (expl_d), d->name))
657 break;
658 if (expl_d != 0)
660 (pat++)->name = d->name;
661 continue;
664 /* The DEP->changed flag says that this dependency resides
665 in a nonexistent directory. So we normally can skip
666 looking for the file. However, if CHECK_LASTSLASH is
667 set, then the dependency file we are actually looking for
668 is in a different directory (the one gotten by prepending
669 FILENAME's directory), so it might actually exist. */
671 /* @@ dep->changed check is disabled. */
672 if (lookup_file (d->name) != 0
673 /*|| ((!dep->changed || check_lastslash) && */
674 || file_exists_p (d->name))
676 (pat++)->name = d->name;
677 continue;
680 /* This code, given FILENAME = "lib/foo.o", dependency name
681 "lib/foo.c", and VPATH=src, searches for
682 "src/lib/foo.c". */
684 const char *vname = vpath_search (d->name, 0);
685 if (vname)
687 DBS (DB_IMPLICIT,
688 (_("Found prerequisite `%s' as VPATH `%s'\n"),
689 d->name, vname));
690 (pat++)->name = d->name;
691 continue;
695 /* We could not find the file in any place we should look.
696 Try to make this dependency as an intermediate file, but
697 only on the second pass. */
699 if (intermed_ok)
701 DBS (DB_IMPLICIT,
702 (_("Looking for a rule with intermediate file `%s'.\n"),
703 d->name));
705 if (int_file == 0)
706 int_file = alloca (sizeof (struct file));
707 memset (int_file, '\0', sizeof (struct file));
708 int_file->name = d->name;
710 if (pattern_search (int_file,
712 depth + 1,
713 recursions + 1))
715 pat->pattern = int_file->name;
716 int_file->name = d->name;
717 pat->file = int_file;
718 (pat++)->name = d->name;
719 int_file = 0;
720 continue;
723 /* If we have tried to find P as an intermediate file
724 and failed, mark that name as impossible so we won't
725 go through the search again later. */
726 if (int_file->variables)
727 free_variable_set (int_file->variables);
728 if (int_file->pat_variables)
729 free_variable_set (int_file->pat_variables);
730 file_impossible (d->name);
733 /* A dependency of this rule does not exist. Therefore, this
734 rule fails. */
735 failed = 1;
736 break;
739 /* Free the ns chain. */
740 if (dl != &dep_simple)
741 free_dep_chain (dl);
743 if (failed)
744 break;
747 /* Reset the stem in FILE. */
749 file->stem = 0;
751 /* This rule is no longer `in use' for recursive searches. */
752 rule->in_use = 0;
754 if (! failed)
755 /* This pattern rule does apply. Stop looking for one. */
756 break;
758 /* This pattern rule does not apply. If some of its dependencies
759 succeeded, free the data structure describing them. */
760 /* free_idep_chain (deps); */
761 deps = 0;
764 /* If we found an applicable rule without intermediate files, don't try
765 with them. */
766 if (ri < nrules)
767 break;
769 rule = 0;
772 /* RULE is nil if the loop went through the list but everything failed. */
773 if (rule == 0)
774 goto done;
776 foundrule = ri;
778 /* If we are recursing, store the pattern that matched FILENAME in
779 FILE->name for use in upper levels. */
781 if (recursions > 0)
782 /* Kludge-o-matic */
783 file->name = rule->targets[matches[foundrule]];
785 /* DEPLIST lists the prerequisites for the rule we found. This includes the
786 intermediate files, if any. Convert them into entries on the deps-chain
787 of FILE. */
789 while (pat-- > deplist)
791 struct dep *dep;
792 const char *s;
794 if (pat->file != 0)
796 /* If we need to use an intermediate file, make sure it is entered
797 as a target, with the info that was found for it in the recursive
798 pattern_search call. We know that the intermediate file did not
799 already exist as a target; therefore we can assume that the deps
800 and cmds of F below are null before we change them. */
802 struct file *imf = pat->file;
803 struct file *f = lookup_file (imf->name);
805 /* We don't want to delete an intermediate file that happened
806 to be a prerequisite of some (other) target. Mark it as
807 precious. */
808 if (f != 0)
809 f->precious = 1;
810 else
811 f = enter_file (imf->name);
813 f->deps = imf->deps;
814 f->cmds = imf->cmds;
815 f->stem = imf->stem;
816 f->variables = imf->variables;
817 f->pat_variables = imf->pat_variables;
818 f->pat_searched = imf->pat_searched;
819 f->also_make = imf->also_make;
820 f->is_target = 1;
821 f->intermediate = 1;
822 f->tried_implicit = 1;
824 imf = lookup_file (pat->pattern);
825 if (imf != 0 && imf->precious)
826 f->precious = 1;
828 for (dep = f->deps; dep != 0; dep = dep->next)
830 dep->file = enter_file (dep->name);
831 dep->name = 0;
832 dep->file->tried_implicit |= dep->changed;
836 dep = alloc_dep ();
837 dep->ignore_mtime = pat->ignore_mtime;
838 s = strcache_add (pat->name);
839 if (recursions)
840 dep->name = s;
841 else
843 dep->file = lookup_file (s);
844 if (dep->file == 0)
845 dep->file = enter_file (s);
848 if (pat->file == 0 && tryrules[foundrule]->terminal)
850 /* If the file actually existed (was not an intermediate file), and
851 the rule that found it was a terminal one, then we want to mark
852 the found file so that it will not have implicit rule search done
853 for it. If we are not entering a `struct file' for it now, we
854 indicate this with the `changed' flag. */
855 if (dep->file == 0)
856 dep->changed = 1;
857 else
858 dep->file->tried_implicit = 1;
861 dep->next = file->deps;
862 file->deps = dep;
865 if (!checked_lastslash[foundrule])
867 /* Always allocate new storage, since STEM might be on the stack for an
868 intermediate file. */
869 file->stem = strcache_add_len (stem, stemlen);
870 fullstemlen = stemlen;
872 else
874 int dirlen = (lastslash + 1) - filename;
875 char *sp;
877 /* We want to prepend the directory from
878 the original FILENAME onto the stem. */
879 fullstemlen = dirlen + stemlen;
880 sp = alloca (fullstemlen + 1);
881 memcpy (sp, filename, dirlen);
882 memcpy (sp + dirlen, stem, stemlen);
883 sp[fullstemlen] = '\0';
884 file->stem = strcache_add (sp);
887 file->cmds = rule->cmds;
888 file->is_target = 1;
890 /* Set precious flag. */
892 struct file *f = lookup_file (rule->targets[matches[foundrule]]);
893 if (f && f->precious)
894 file->precious = 1;
897 /* If this rule builds other targets, too, put the others into FILE's
898 `also_make' member. */
900 if (rule->num > 1)
901 for (ri = 0; ri < rule->num; ++ri)
902 if (ri != matches[foundrule])
904 char *nm = alloca (rule->lens[ri] + fullstemlen + 1);
905 char *p = nm;
906 struct file *f;
907 struct dep *new = alloc_dep ();
909 /* GKM FIMXE: handle '|' here too */
910 memcpy (p, rule->targets[ri],
911 rule->suffixes[ri] - rule->targets[ri] - 1);
912 p += rule->suffixes[ri] - rule->targets[ri] - 1;
913 memcpy (p, file->stem, fullstemlen);
914 p += fullstemlen;
915 memcpy (p, rule->suffixes[ri],
916 rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
917 new->name = strcache_add (nm);
918 new->file = enter_file (new->name);
919 new->next = file->also_make;
921 /* Set precious flag. */
922 f = lookup_file (rule->targets[ri]);
923 if (f && f->precious)
924 new->file->precious = 1;
926 /* Set the is_target flag so that this file is not treated as
927 intermediate by the pattern rule search algorithm and
928 file_exists_p cannot pick it up yet. */
929 new->file->is_target = 1;
931 file->also_make = new;
934 done:
935 free (tryrules);
936 free (deplist);
938 return rule != 0;