On BSD, realpath(3) doesn't fail if the target doesn't exist, so test
[make.git] / implicit.c
blob060a80c525f9813561279e980963b3532608c458
1 /* Implicit rule searching for GNU Make.
2 Copyright (C) 1988-2012 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
8 version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include "make.h"
18 #include "filedef.h"
19 #include "rule.h"
20 #include "dep.h"
21 #include "debug.h"
22 #include "variable.h"
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. */
35 int
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
43 should come first. */
45 if (pattern_search (file, 0, depth, 0))
46 return 1;
48 #ifndef NO_ARCHIVES
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))
53 DBF (DB_IMPLICIT,
54 _("Looking for archive-member implicit rule for '%s'.\n"));
55 if (pattern_search (file, 1, depth, 0))
56 return 1;
58 #endif
60 return 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. */
68 static const char *
69 get_next_word (const char *buffer, unsigned int *length)
71 const char *p = buffer, *beg;
72 char c;
74 /* Skip any leading whitespace. */
75 while (isblank ((unsigned char)*p))
76 ++p;
78 beg = p;
79 c = *(p++);
81 if (c == '\0')
82 return 0;
85 /* We already found the first value of "c", above. */
86 while (1)
88 char closeparen;
89 int count;
91 switch (c)
93 case '\0':
94 case ' ':
95 case '\t':
96 goto done_word;
98 case '$':
99 c = *(p++);
100 if (c == '$')
101 break;
103 /* This is a variable reference, so read it to the matching
104 close paren. */
106 if (c == '(')
107 closeparen = ')';
108 else if (c == '{')
109 closeparen = '}';
110 else
111 /* This is a single-letter variable reference. */
112 break;
114 for (count = 0; *p != '\0'; ++p)
116 if (*p == c)
117 ++count;
118 else if (*p == closeparen && --count < 0)
120 ++p;
121 break;
124 break;
126 case '|':
127 goto done;
129 default:
130 break;
133 c = *(p++);
135 done_word:
136 --p;
138 done:
139 if (length)
140 *length = p - beg;
142 return beg;
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.
150 struct patdeps
152 const char *name;
153 const char *pattern;
154 struct file *file;
155 unsigned int ignore_mtime : 1;
158 /* This structure stores information about pattern rules that we need
159 to try.
161 struct tryrule
163 struct rule *rule;
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.*/
172 unsigned int order;
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
197 of FILE.
199 DEPTH is used for debugging messages. */
201 static int
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 struct patdeps *deplist
221 = xmalloc (max_pattern_deps * sizeof (struct patdeps));
222 struct patdeps *pat = deplist;
224 /* All the prerequisites actually found for a rule, after expansion. */
225 struct dep *deps;
227 /* Names of possible dependencies are constructed in this buffer. */
228 char *depname = alloca (namelen + max_pattern_dep_length);
230 /* The start and length of the stem of FILENAME for the current rule. */
231 const char *stem = 0;
232 unsigned int stemlen = 0;
233 unsigned int fullstemlen = 0;
235 /* Buffer in which we store all the rules that are possibly applicable. */
236 struct tryrule *tryrules = xmalloc (num_pattern_rules * max_pattern_targets
237 * sizeof (struct tryrule));
239 /* Number of valid elements in TRYRULES. */
240 unsigned int nrules;
242 /* The index in TRYRULES of the rule we found. */
243 unsigned int foundrule;
245 /* Nonzero if should consider intermediate files as dependencies. */
246 int intermed_ok;
248 /* Nonzero if we have initialized file variables for this target. */
249 int file_vars_initialized = 0;
251 /* Nonzero if we have matched a pattern-rule target
252 that is not just '%'. */
253 int specific_rule_matched = 0;
255 struct dep dep_simple;
257 unsigned int ri; /* uninit checks OK */
258 struct rule *rule;
260 char *pathdir = NULL;
261 unsigned long pathlen;
263 PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
265 #ifndef NO_ARCHIVES
266 if (archive || ar_name (filename))
267 lastslash = 0;
268 else
269 #endif
271 /* Set LASTSLASH to point at the last slash in FILENAME
272 but not counting any slash at the end. (foo/bar/ counts as
273 bar/ in directory foo/, not empty in directory foo/bar/.) */
274 #ifdef VMS
275 lastslash = strrchr (filename, ']');
276 if (lastslash == 0)
277 lastslash = strrchr (filename, ':');
278 #else
279 lastslash = strrchr (filename, '/');
280 #ifdef HAVE_DOS_PATHS
281 /* Handle backslashes (possibly mixed with forward slashes)
282 and the case of "d:file". */
284 char *bslash = strrchr (filename, '\\');
285 if (lastslash == 0 || bslash > lastslash)
286 lastslash = bslash;
287 if (lastslash == 0 && filename[0] && filename[1] == ':')
288 lastslash = filename + 1;
290 #endif
291 #endif
292 if (lastslash != 0 && lastslash[1] == '\0')
293 lastslash = 0;
296 pathlen = lastslash - filename + 1;
298 /* First see which pattern rules match this target and may be considered.
299 Put them in TRYRULES. */
301 nrules = 0;
302 for (rule = pattern_rules; rule != 0; rule = rule->next)
304 unsigned int ti;
306 /* If the pattern rule has deps but no commands, ignore it.
307 Users cancel built-in rules by redefining them without commands. */
308 if (rule->deps != 0 && rule->cmds == 0)
309 continue;
311 /* If this rule is in use by a parent pattern_search,
312 don't use it here. */
313 if (rule->in_use)
315 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
316 continue;
319 for (ti = 0; ti < rule->num; ++ti)
321 const char *target = rule->targets[ti];
322 const char *suffix = rule->suffixes[ti];
323 int check_lastslash;
325 /* Rules that can match any filename and are not terminal
326 are ignored if we're recursing, so that they cannot be
327 intermediate files. */
328 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
329 continue;
331 if (rule->lens[ti] > namelen)
332 /* It can't possibly match. */
333 continue;
335 /* From the lengths of the filename and the pattern parts,
336 find the stem: the part of the filename that matches the %. */
337 stem = filename + (suffix - target - 1);
338 stemlen = namelen - rule->lens[ti] + 1;
340 /* Set CHECK_LASTSLASH if FILENAME contains a directory
341 prefix and the target pattern does not contain a slash. */
343 check_lastslash = 0;
344 if (lastslash)
346 #ifdef VMS
347 check_lastslash = (strchr (target, ']') == 0
348 && strchr (target, ':') == 0);
349 #else
350 check_lastslash = strchr (target, '/') == 0;
351 #ifdef HAVE_DOS_PATHS
352 /* Didn't find it yet: check for DOS-type directories. */
353 if (check_lastslash)
355 char *b = strchr (target, '\\');
356 check_lastslash = !(b || (target[0] && target[1] == ':'));
358 #endif
359 #endif
361 if (check_lastslash)
363 /* If so, don't include the directory prefix in STEM here. */
364 if (pathlen > stemlen)
365 continue;
366 stemlen -= pathlen;
367 stem += pathlen;
370 /* Check that the rule pattern matches the text before the stem. */
371 if (check_lastslash)
373 if (stem > (lastslash + 1)
374 && !strneq (target, lastslash + 1, stem - lastslash - 1))
375 continue;
377 else if (stem > filename
378 && !strneq (target, filename, stem - filename))
379 continue;
381 /* Check that the rule pattern matches the text after the stem.
382 We could test simply use streq, but this way we compare the
383 first two characters immediately. This saves time in the very
384 common case where the first character matches because it is a
385 period. */
386 if (*suffix != stem[stemlen]
387 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
388 continue;
390 /* Record if we match a rule that not all filenames will match. */
391 if (target[1] != '\0')
392 specific_rule_matched = 1;
394 /* A rule with no dependencies and no commands exists solely to set
395 specific_rule_matched when it matches. Don't try to use it. */
396 if (rule->deps == 0 && rule->cmds == 0)
397 continue;
399 /* Record this rule in TRYRULES and the index of the matching
400 target in MATCHES. If several targets of the same rule match,
401 that rule will be in TRYRULES more than once. */
402 tryrules[nrules].rule = rule;
403 tryrules[nrules].matches = ti;
404 tryrules[nrules].stemlen = stemlen + (check_lastslash ? pathlen : 0);
405 tryrules[nrules].order = nrules;
406 tryrules[nrules].checked_lastslash = check_lastslash;
407 ++nrules;
411 /* Bail out early if we haven't found any rules. */
412 if (nrules == 0)
413 goto done;
415 /* Sort the rules to place matches with the shortest stem first. This
416 way the most specific rules will be tried first. */
417 if (nrules > 1)
418 qsort (tryrules, nrules, sizeof (struct tryrule), stemlen_compare);
420 /* If we have found a matching rule that won't match all filenames,
421 retroactively reject any non-"terminal" rules that do always match. */
422 if (specific_rule_matched)
423 for (ri = 0; ri < nrules; ++ri)
424 if (!tryrules[ri].rule->terminal)
426 unsigned int j;
427 for (j = 0; j < tryrules[ri].rule->num; ++j)
428 if (tryrules[ri].rule->targets[j][1] == '\0')
430 tryrules[ri].rule = 0;
431 break;
435 /* Try each rule once without intermediate files, then once with them. */
436 for (intermed_ok = 0; intermed_ok < 2; ++intermed_ok)
438 pat = deplist;
440 /* Try each pattern rule till we find one that applies. If it does,
441 expand its dependencies (as substituted) and chain them in DEPS. */
442 for (ri = 0; ri < nrules; ri++)
444 struct dep *dep;
445 int check_lastslash;
446 unsigned int failed = 0;
447 int file_variables_set = 0;
448 unsigned int deps_found = 0;
449 /* NPTR points to the part of the prereq we haven't processed. */
450 const char *nptr = 0;
451 const char *dir = NULL;
452 int order_only = 0;
453 unsigned int matches;
455 rule = tryrules[ri].rule;
457 /* RULE is nil when we discover that a rule, already placed in
458 TRYRULES, should not be applied. */
459 if (rule == 0)
460 continue;
462 /* Reject any terminal rules if we're looking to make intermediate
463 files. */
464 if (intermed_ok && rule->terminal)
465 continue;
467 /* From the lengths of the filename and the matching pattern parts,
468 find the stem: the part of the filename that matches the %. */
469 matches = tryrules[ri].matches;
470 stem = filename + (rule->suffixes[matches]
471 - rule->targets[matches]) - 1;
472 stemlen = (namelen - rule->lens[matches]) + 1;
473 check_lastslash = tryrules[ri].checked_lastslash;
474 if (check_lastslash)
476 stem += pathlen;
477 stemlen -= pathlen;
479 /* We need to add the directory prefix, so set it up. */
480 if (! pathdir)
482 pathdir = alloca (pathlen + 1);
483 memcpy (pathdir, filename, pathlen);
484 pathdir[pathlen] = '\0';
486 dir = pathdir;
489 if (stemlen > GET_PATH_MAX)
491 DBS (DB_IMPLICIT, (_("Stem too long: '%.*s'.\n"),
492 (int) stemlen, stem));
493 continue;
496 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem '%.*s'.\n"),
497 (int) stemlen, stem));
499 strncpy (stem_str, stem, stemlen);
500 stem_str[stemlen] = '\0';
502 /* If there are no prerequisites, then this rule matches. */
503 if (rule->deps == 0)
504 break;
506 /* Temporary assign STEM to file->stem (needed to set file
507 variables below). */
508 file->stem = stem_str;
510 /* Mark this rule as in use so a recursive pattern_search won't try
511 to use it. */
512 rule->in_use = 1;
514 /* Try each prerequisite; see if it exists or can be created. We'll
515 build a list of prereq info in DEPLIST. Due to 2nd expansion we
516 may have to process multiple prereqs for a single dep entry. */
518 pat = deplist;
519 dep = rule->deps;
520 nptr = dep_name (dep);
521 while (1)
523 struct dep *dl, *d;
524 char *p;
526 /* If we're out of name to parse, start the next prereq. */
527 if (! nptr)
529 dep = dep->next;
530 if (dep == 0)
531 break;
532 nptr = dep_name (dep);
535 /* If we don't need a second expansion, just replace the %. */
536 if (! dep->need_2nd_expansion)
538 dep_simple = *dep;
539 dep_simple.next = 0;
540 p = strchr (nptr, '%');
541 if (p == 0)
542 dep_simple.name = nptr;
543 else
545 char *o = depname;
546 if (check_lastslash)
548 memcpy (o, filename, pathlen);
549 o += pathlen;
551 memcpy (o, nptr, p - nptr);
552 o += p - nptr;
553 memcpy (o, stem_str, stemlen);
554 o += stemlen;
555 strcpy (o, p + 1);
556 dep_simple.name = strcache_add (depname);
558 dl = &dep_simple;
560 /* We've used up this dep, so next time get a new one. */
561 nptr = 0;
562 ++deps_found;
565 /* We have to perform second expansion on this prereq. In an
566 ideal world we would take the dependency line, substitute the
567 stem, re-expand the whole line and chop it into individual
568 prerequisites. Unfortunately this won't work because of the
569 "check_lastslash" twist. Instead, we will have to go word by
570 word, taking $()'s into account. For each word we will
571 substitute the stem, re-expand, chop it up, and, if
572 check_lastslash != 0, add the directory part to each
573 resulting prerequisite. */
574 else
576 int add_dir = 0;
577 unsigned int len;
579 nptr = get_next_word (nptr, &len);
580 if (nptr == 0)
581 continue;
583 /* See this is a transition to order-only prereqs. */
584 if (! order_only && len == 1 && nptr[0] == '|')
586 order_only = 1;
587 nptr += len;
588 continue;
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, '%');
603 if (p == 0)
605 memcpy (depname, nptr, len);
606 depname[len] = '\0';
608 else
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';
616 if (check_lastslash)
617 add_dir = 1;
620 /* Initialize and set file variables if we haven't already
621 done so. */
622 if (!file_vars_initialized)
624 initialize_file_variables (file, 0);
625 set_file_variables (file);
626 file_vars_initialized = 1;
628 /* Update the stem value in $* for this rule. */
629 else if (!file_variables_set)
631 define_variable_for_file (
632 "*", 1, file->stem, o_automatic, 0, file);
633 file_variables_set = 1;
636 /* Perform the 2nd expansion. */
637 p = variable_expand_for_file (depname, file);
639 /* Parse the expanded string. */
640 dl = PARSE_FILE_SEQ (&p, struct dep, order_only ? '\0' : '|',
641 add_dir ? dir : NULL, 0);
643 for (d = dl; d != NULL; d = d->next)
645 ++deps_found;
646 if (order_only)
647 d->ignore_mtime = 1;
650 /* Set up for the next word. */
651 nptr += len;
654 /* If there are more than max_pattern_deps prerequisites (due to
655 2nd expansion), reset it and realloc the arrays. */
657 if (deps_found > max_pattern_deps)
659 unsigned int l = pat - deplist;
660 deplist = xrealloc (deplist,
661 deps_found * sizeof (struct patdeps));
662 pat = deplist + l;
663 max_pattern_deps = deps_found;
666 /* Go through the nameseq and handle each as a prereq name. */
667 for (d = dl; d != 0; d = d->next)
669 struct dep *expl_d;
670 int is_rule = d->name == dep_name (dep);
672 if (file_impossible_p (d->name))
674 /* If this prereq has already been ruled "impossible",
675 then the rule fails. Don't bother trying it on the
676 second pass either since we know that will fail. */
677 DBS (DB_IMPLICIT,
678 (is_rule
679 ? _("Rejecting impossible rule prerequisite '%s'.\n")
680 : _("Rejecting impossible implicit prerequisite '%s'.\n"),
681 d->name));
682 tryrules[ri].rule = 0;
684 failed = 1;
685 break;
688 memset (pat, '\0', sizeof (struct patdeps));
689 pat->ignore_mtime = d->ignore_mtime;
691 DBS (DB_IMPLICIT,
692 (is_rule
693 ? _("Trying rule prerequisite '%s'.\n")
694 : _("Trying implicit prerequisite '%s'.\n"), d->name));
696 /* If this prereq is also explicitly mentioned for FILE,
697 skip all tests below since it must be built no matter
698 which implicit rule we choose. */
700 for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
701 if (streq (dep_name (expl_d), d->name))
702 break;
703 if (expl_d != 0)
705 (pat++)->name = d->name;
706 continue;
709 /* The DEP->changed flag says that this dependency resides
710 in a nonexistent directory. So we normally can skip
711 looking for the file. However, if CHECK_LASTSLASH is
712 set, then the dependency file we are actually looking for
713 is in a different directory (the one gotten by prepending
714 FILENAME's directory), so it might actually exist. */
716 /* @@ dep->changed check is disabled. */
717 if (lookup_file (d->name) != 0
718 /*|| ((!dep->changed || check_lastslash) && */
719 || file_exists_p (d->name))
721 (pat++)->name = d->name;
722 continue;
725 /* This code, given FILENAME = "lib/foo.o", dependency name
726 "lib/foo.c", and VPATH=src, searches for
727 "src/lib/foo.c". */
729 const char *vname = vpath_search (d->name, 0, NULL, NULL);
730 if (vname)
732 DBS (DB_IMPLICIT,
733 (_("Found prerequisite '%s' as VPATH '%s'\n"),
734 d->name, vname));
735 (pat++)->name = d->name;
736 continue;
740 /* We could not find the file in any place we should look.
741 Try to make this dependency as an intermediate file, but
742 only on the second pass. */
744 if (intermed_ok)
746 DBS (DB_IMPLICIT,
747 (_("Looking for a rule with intermediate file '%s'.\n"),
748 d->name));
750 if (int_file == 0)
751 int_file = alloca (sizeof (struct file));
752 memset (int_file, '\0', sizeof (struct file));
753 int_file->name = d->name;
755 if (pattern_search (int_file,
757 depth + 1,
758 recursions + 1))
760 pat->pattern = int_file->name;
761 int_file->name = d->name;
762 pat->file = int_file;
763 (pat++)->name = d->name;
764 int_file = 0;
765 continue;
768 /* If we have tried to find P as an intermediate file
769 and failed, mark that name as impossible so we won't
770 go through the search again later. */
771 if (int_file->variables)
772 free_variable_set (int_file->variables);
773 if (int_file->pat_variables)
774 free_variable_set (int_file->pat_variables);
775 file_impossible (d->name);
778 /* A dependency of this rule does not exist. Therefore, this
779 rule fails. */
780 failed = 1;
781 break;
784 /* Free the ns chain. */
785 if (dl != &dep_simple)
786 free_dep_chain (dl);
788 if (failed)
789 break;
792 /* Reset the stem in FILE. */
794 file->stem = 0;
796 /* This rule is no longer 'in use' for recursive searches. */
797 rule->in_use = 0;
799 if (! failed)
800 /* This pattern rule does apply. Stop looking for one. */
801 break;
803 /* This pattern rule does not apply. If some of its dependencies
804 succeeded, free the data structure describing them. */
805 /* free_idep_chain (deps); */
806 deps = 0;
809 /* If we found an applicable rule without intermediate files, don't try
810 with them. */
811 if (ri < nrules)
812 break;
814 rule = 0;
817 /* RULE is nil if the loop went through the list but everything failed. */
818 if (rule == 0)
819 goto done;
821 foundrule = ri;
823 /* If we are recursing, store the pattern that matched FILENAME in
824 FILE->name for use in upper levels. */
826 if (recursions > 0)
827 /* Kludge-o-matic */
828 file->name = rule->targets[tryrules[foundrule].matches];
830 /* DEPLIST lists the prerequisites for the rule we found. This includes the
831 intermediate files, if any. Convert them into entries on the deps-chain
832 of FILE. */
834 while (pat-- > deplist)
836 struct dep *dep;
837 const char *s;
839 if (pat->file != 0)
841 /* If we need to use an intermediate file, make sure it is entered
842 as a target, with the info that was found for it in the recursive
843 pattern_search call. We know that the intermediate file did not
844 already exist as a target; therefore we can assume that the deps
845 and cmds of F below are null before we change them. */
847 struct file *imf = pat->file;
848 struct file *f = lookup_file (imf->name);
850 /* We don't want to delete an intermediate file that happened
851 to be a prerequisite of some (other) target. Mark it as
852 precious. */
853 if (f != 0)
854 f->precious = 1;
855 else
856 f = enter_file (imf->name);
858 f->deps = imf->deps;
859 f->cmds = imf->cmds;
860 f->stem = imf->stem;
861 f->variables = imf->variables;
862 f->pat_variables = imf->pat_variables;
863 f->pat_searched = imf->pat_searched;
864 f->also_make = imf->also_make;
865 f->is_target = 1;
866 f->intermediate = 1;
867 f->tried_implicit = 1;
869 imf = lookup_file (pat->pattern);
870 if (imf != 0 && imf->precious)
871 f->precious = 1;
873 for (dep = f->deps; dep != 0; dep = dep->next)
875 dep->file = enter_file (dep->name);
876 dep->name = 0;
877 dep->file->tried_implicit |= dep->changed;
881 dep = alloc_dep ();
882 dep->ignore_mtime = pat->ignore_mtime;
883 s = strcache_add (pat->name);
884 if (recursions)
885 dep->name = s;
886 else
888 dep->file = lookup_file (s);
889 if (dep->file == 0)
890 dep->file = enter_file (s);
893 if (pat->file == 0 && tryrules[foundrule].rule->terminal)
895 /* If the file actually existed (was not an intermediate file), and
896 the rule that found it was a terminal one, then we want to mark
897 the found file so that it will not have implicit rule search done
898 for it. If we are not entering a 'struct file' for it now, we
899 indicate this with the 'changed' flag. */
900 if (dep->file == 0)
901 dep->changed = 1;
902 else
903 dep->file->tried_implicit = 1;
906 dep->next = file->deps;
907 file->deps = dep;
910 if (!tryrules[foundrule].checked_lastslash)
912 /* Always allocate new storage, since STEM might be on the stack for an
913 intermediate file. */
914 file->stem = strcache_add_len (stem, stemlen);
915 fullstemlen = stemlen;
917 else
919 int dirlen = (lastslash + 1) - filename;
920 char *sp;
922 /* We want to prepend the directory from
923 the original FILENAME onto the stem. */
924 fullstemlen = dirlen + stemlen;
925 sp = alloca (fullstemlen + 1);
926 memcpy (sp, filename, dirlen);
927 memcpy (sp + dirlen, stem, stemlen);
928 sp[fullstemlen] = '\0';
929 file->stem = strcache_add (sp);
932 file->cmds = rule->cmds;
933 file->is_target = 1;
935 /* Set precious flag. */
937 struct file *f = lookup_file (rule->targets[tryrules[foundrule].matches]);
938 if (f && f->precious)
939 file->precious = 1;
942 /* If this rule builds other targets, too, put the others into FILE's
943 'also_make' member. */
945 if (rule->num > 1)
946 for (ri = 0; ri < rule->num; ++ri)
947 if (ri != tryrules[foundrule].matches)
949 char *nm = alloca (rule->lens[ri] + fullstemlen + 1);
950 char *p = nm;
951 struct file *f;
952 struct dep *new = alloc_dep ();
954 /* GKM FIMXE: handle '|' here too */
955 memcpy (p, rule->targets[ri],
956 rule->suffixes[ri] - rule->targets[ri] - 1);
957 p += rule->suffixes[ri] - rule->targets[ri] - 1;
958 memcpy (p, file->stem, fullstemlen);
959 p += fullstemlen;
960 memcpy (p, rule->suffixes[ri],
961 rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
962 new->name = strcache_add (nm);
963 new->file = enter_file (new->name);
964 new->next = file->also_make;
966 /* Set precious flag. */
967 f = lookup_file (rule->targets[ri]);
968 if (f && f->precious)
969 new->file->precious = 1;
971 /* Set the is_target flag so that this file is not treated as
972 intermediate by the pattern rule search algorithm and
973 file_exists_p cannot pick it up yet. */
974 new->file->is_target = 1;
976 file->also_make = new;
979 done:
980 free (tryrules);
981 free (deplist);
983 return rule != 0;