* Fix PR/1791.
[make.git] / implicit.c
blobcce179570975e30487563db1ff876394b1a659a7
1 /* Implicit rule searching for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,97,2000 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
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "rule.h"
22 #include "dep.h"
23 #include "filedef.h"
24 #include "debug.h"
26 static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth,
27 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 (file, depth)
37 struct file *file;
38 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 /* Search the pattern rules for a rule with an existing dependency to make
67 FILE. If a rule is found, the appropriate commands and deps are put in FILE
68 and 1 is returned. If not, 0 is returned.
70 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
71 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
72 directory and filename parts.
74 If an intermediate file is found by pattern search, the intermediate file
75 is set up as a target by the recursive call and is also made a dependency
76 of FILE.
78 DEPTH is used for debugging messages. */
80 static int
81 pattern_search (file, archive, depth, recursions)
82 struct file *file;
83 int archive;
84 unsigned int depth;
85 unsigned int recursions;
87 /* Filename we are searching for a rule for. */
88 char *filename = archive ? strchr (file->name, '(') : file->name;
90 /* Length of FILENAME. */
91 unsigned int namelen = strlen (filename);
93 /* The last slash in FILENAME (or nil if there is none). */
94 char *lastslash;
96 /* This is a file-object used as an argument in
97 recursive calls. It never contains any data
98 except during a recursive call. */
99 struct file *intermediate_file = 0;
101 /* List of dependencies found recursively. */
102 struct file **intermediate_files
103 = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
105 /* List of the patterns used to find intermediate files. */
106 char **intermediate_patterns
107 = (char **) alloca (max_pattern_deps * sizeof (char *));
109 /* This buffer records all the dependencies actually found for a rule. */
110 char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
111 /* Number of dep names now in FOUND_FILES. */
112 unsigned int deps_found = 0;
114 /* Names of possible dependencies are constructed in this buffer. */
115 register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
117 /* The start and length of the stem of FILENAME for the current rule. */
118 register char *stem = 0;
119 register unsigned int stemlen = 0;
120 register unsigned int fullstemlen = 0;
122 /* Buffer in which we store all the rules that are possibly applicable. */
123 struct rule **tryrules
124 = (struct rule **) alloca (num_pattern_rules * max_pattern_targets
125 * sizeof (struct rule *));
127 /* Number of valid elements in TRYRULES. */
128 unsigned int nrules;
130 /* The numbers of the rule targets of each rule
131 in TRYRULES that matched the target file. */
132 unsigned int *matches
133 = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
135 /* Each element is nonzero if LASTSLASH was used in
136 matching the corresponding element of TRYRULES. */
137 char *checked_lastslash
138 = (char *) alloca (num_pattern_rules * sizeof (char));
140 /* The index in TRYRULES of the rule we found. */
141 unsigned int foundrule;
143 /* Nonzero if should consider intermediate files as dependencies. */
144 int intermed_ok;
146 /* Nonzero if we have matched a pattern-rule target
147 that is not just `%'. */
148 int specific_rule_matched = 0;
150 register unsigned int i = 0; /* uninit checks OK */
151 register struct rule *rule;
152 register struct dep *dep;
154 char *p, *vp;
156 #ifndef NO_ARCHIVES
157 if (archive || ar_name (filename))
158 lastslash = 0;
159 else
160 #endif
162 /* Set LASTSLASH to point at the last slash in FILENAME
163 but not counting any slash at the end. (foo/bar/ counts as
164 bar/ in directory foo/, not empty in directory foo/bar/.) */
165 #ifdef VMS
166 lastslash = strrchr (filename, ']');
167 if (lastslash == 0)
168 lastslash = strrchr (filename, ':');
169 #else
170 lastslash = strrchr (filename, '/');
171 #if defined(__MSDOS__) || defined(WINDOWS32)
172 /* Handle backslashes (possibly mixed with forward slashes)
173 and the case of "d:file". */
175 char *bslash = strrchr (filename, '\\');
176 if (lastslash == 0 || bslash > lastslash)
177 lastslash = bslash;
178 if (lastslash == 0 && filename[0] && filename[1] == ':')
179 lastslash = filename + 1;
181 #endif
182 #endif
183 if (lastslash != 0 && lastslash[1] == '\0')
184 lastslash = 0;
187 /* First see which pattern rules match this target
188 and may be considered. Put them in TRYRULES. */
190 nrules = 0;
191 for (rule = pattern_rules; rule != 0; rule = rule->next)
193 /* If the pattern rule has deps but no commands, ignore it.
194 Users cancel built-in rules by redefining them without commands. */
195 if (rule->deps != 0 && rule->cmds == 0)
196 continue;
198 /* If this rule is in use by a parent pattern_search,
199 don't use it here. */
200 if (rule->in_use)
202 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
203 continue;
206 for (i = 0; rule->targets[i] != 0; ++i)
208 char *target = rule->targets[i];
209 char *suffix = rule->suffixes[i];
210 int check_lastslash;
212 /* Rules that can match any filename and are not terminal
213 are ignored if we're recursing, so that they cannot be
214 intermediate files. */
215 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
216 continue;
218 if (rule->lens[i] > namelen)
219 /* It can't possibly match. */
220 continue;
222 /* From the lengths of the filename and the pattern parts,
223 find the stem: the part of the filename that matches the %. */
224 stem = filename + (suffix - target - 1);
225 stemlen = namelen - rule->lens[i] + 1;
227 /* Set CHECK_LASTSLASH if FILENAME contains a directory
228 prefix and the target pattern does not contain a slash. */
230 #ifdef VMS
231 check_lastslash = lastslash != 0
232 && ((strchr (target, ']') == 0)
233 && (strchr (target, ':') == 0));
234 #else
235 check_lastslash = lastslash != 0 && strchr (target, '/') == 0;
236 #endif
237 if (check_lastslash)
239 /* In that case, don't include the
240 directory prefix in STEM here. */
241 unsigned int difference = lastslash - filename + 1;
242 if (difference > stemlen)
243 continue;
244 stemlen -= difference;
245 stem += difference;
248 /* Check that the rule pattern matches the text before the stem. */
249 if (check_lastslash)
251 if (stem > (lastslash + 1)
252 && !strneq (target, lastslash + 1, stem - lastslash - 1))
253 continue;
255 else if (stem > filename
256 && !strneq (target, filename, stem - filename))
257 continue;
259 /* Check that the rule pattern matches the text after the stem.
260 We could test simply use streq, but this way we compare the
261 first two characters immediately. This saves time in the very
262 common case where the first character matches because it is a
263 period. */
264 if (*suffix != stem[stemlen]
265 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
266 continue;
268 /* Record if we match a rule that not all filenames will match. */
269 if (target[1] != '\0')
270 specific_rule_matched = 1;
272 /* A rule with no dependencies and no commands exists solely to set
273 specific_rule_matched when it matches. Don't try to use it. */
274 if (rule->deps == 0 && rule->cmds == 0)
275 continue;
277 /* Record this rule in TRYRULES and the index of the matching
278 target in MATCHES. If several targets of the same rule match,
279 that rule will be in TRYRULES more than once. */
280 tryrules[nrules] = rule;
281 matches[nrules] = i;
282 checked_lastslash[nrules] = check_lastslash;
283 ++nrules;
287 /* If we have found a matching rule that won't match all filenames,
288 retroactively reject any non-"terminal" rules that do always match. */
289 if (specific_rule_matched)
290 for (i = 0; i < nrules; ++i)
291 if (!tryrules[i]->terminal)
293 register unsigned int j;
294 for (j = 0; tryrules[i]->targets[j] != 0; ++j)
295 if (tryrules[i]->targets[j][1] == '\0')
296 break;
297 if (tryrules[i]->targets[j] != 0)
298 tryrules[i] = 0;
301 /* Try each rule once without intermediate files, then once with them. */
302 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
304 /* Try each pattern rule till we find one that applies.
305 If it does, copy the names of its dependencies (as substituted)
306 and store them in FOUND_FILES. DEPS_FOUND is the number of them. */
308 for (i = 0; i < nrules; i++)
310 int check_lastslash;
312 rule = tryrules[i];
314 /* RULE is nil when we discover that a rule,
315 already placed in TRYRULES, should not be applied. */
316 if (rule == 0)
317 continue;
319 /* Reject any terminal rules if we're
320 looking to make intermediate files. */
321 if (intermed_ok && rule->terminal)
322 continue;
324 /* Mark this rule as in use so a recursive
325 pattern_search won't try to use it. */
326 rule->in_use = 1;
328 /* From the lengths of the filename and the matching pattern parts,
329 find the stem: the part of the filename that matches the %. */
330 stem = filename
331 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
332 stemlen = namelen - rule->lens[matches[i]] + 1;
333 check_lastslash = checked_lastslash[i];
334 if (check_lastslash)
336 stem += lastslash - filename + 1;
337 stemlen -= (lastslash - filename) + 1;
340 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
341 (int) stemlen, stem));
343 /* Try each dependency; see if it "exists". */
345 deps_found = 0;
346 for (dep = rule->deps; dep != 0; dep = dep->next)
348 /* If the dependency name has a %, substitute the stem. */
349 p = strchr (dep_name (dep), '%');
350 if (p != 0)
352 register unsigned int i;
353 if (check_lastslash)
355 /* Copy directory name from the original FILENAME. */
356 i = lastslash - filename + 1;
357 bcopy (filename, depname, i);
359 else
360 i = 0;
361 bcopy (dep_name (dep), depname + i, p - dep_name (dep));
362 i += p - dep_name (dep);
363 bcopy (stem, depname + i, stemlen);
364 i += stemlen;
365 strcpy (depname + i, p + 1);
366 p = depname;
368 else
369 p = dep_name (dep);
371 /* P is now the actual dependency name as substituted. */
373 if (file_impossible_p (p))
375 /* If this dependency has already been ruled
376 "impossible", then the rule fails and don't
377 bother trying it on the second pass either
378 since we know that will fail too. */
379 DBS (DB_IMPLICIT,
380 (p == depname
381 ? _("Rejecting impossible implicit prerequisite `%s'.\n")
382 : _("Rejecting impossible rule prerequisite `%s'.\n"),
383 p));
384 tryrules[i] = 0;
385 break;
388 intermediate_files[deps_found] = 0;
390 DBS (DB_IMPLICIT,
391 (p == depname
392 ? _("Trying implicit prerequisite `%s'.\n")
393 : _("Trying rule prerequisite `%s'.\n"), p));
395 /* The DEP->changed flag says that this dependency resides in a
396 nonexistent directory. So we normally can skip looking for
397 the file. However, if CHECK_LASTSLASH is set, then the
398 dependency file we are actually looking for is in a different
399 directory (the one gotten by prepending FILENAME's directory),
400 so it might actually exist. */
402 if ((!dep->changed || check_lastslash)
403 && (lookup_file (p) != 0 || file_exists_p (p)))
405 found_files[deps_found++] = xstrdup (p);
406 continue;
408 /* This code, given FILENAME = "lib/foo.o", dependency name
409 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
410 vp = p;
411 if (vpath_search (&vp, (FILE_TIMESTAMP *) 0))
413 DBS (DB_IMPLICIT,
414 (_("Found prerequisite `%s' as VPATH `%s'\n"), p, vp));
415 strcpy (vp, p);
416 found_files[deps_found++] = vp;
417 continue;
420 /* We could not find the file in any place we should look.
421 Try to make this dependency as an intermediate file,
422 but only on the second pass. */
424 if (intermed_ok)
426 if (intermediate_file == 0)
427 intermediate_file
428 = (struct file *) alloca (sizeof (struct file));
430 DBS (DB_IMPLICIT,
431 (_("Looking for a rule with intermediate file `%s'.\n"),
432 p));
434 bzero ((char *) intermediate_file, sizeof (struct file));
435 intermediate_file->name = p;
436 if (pattern_search (intermediate_file, 0, depth + 1,
437 recursions + 1))
439 p = xstrdup (p);
440 intermediate_patterns[deps_found]
441 = intermediate_file->name;
442 intermediate_file->name = p;
443 intermediate_files[deps_found] = intermediate_file;
444 intermediate_file = 0;
445 /* Allocate an extra copy to go in FOUND_FILES,
446 because every elt of FOUND_FILES is consumed
447 or freed later. */
448 found_files[deps_found] = xstrdup (p);
449 ++deps_found;
450 continue;
453 /* If we have tried to find P as an intermediate
454 file and failed, mark that name as impossible
455 so we won't go through the search again later. */
456 file_impossible (p);
459 /* A dependency of this rule does not exist.
460 Therefore, this rule fails. */
461 break;
464 /* This rule is no longer `in use' for recursive searches. */
465 rule->in_use = 0;
467 if (dep != 0)
469 /* This pattern rule does not apply.
470 If some of its dependencies succeeded,
471 free the data structure describing them. */
472 while (deps_found-- > 0)
474 register struct file *f = intermediate_files[deps_found];
475 free (found_files[deps_found]);
476 if (f != 0
477 && (f->stem < f->name
478 || f->stem > f->name + strlen (f->name)))
479 free (f->stem);
482 else
483 /* This pattern rule does apply. Stop looking for one. */
484 break;
487 /* If we found an applicable rule without
488 intermediate files, don't try with them. */
489 if (i < nrules)
490 break;
492 rule = 0;
495 /* RULE is nil if the loop went all the way
496 through the list and everything failed. */
497 if (rule == 0)
498 return 0;
500 foundrule = i;
502 /* If we are recursing, store the pattern that matched
503 FILENAME in FILE->name for use in upper levels. */
505 if (recursions > 0)
506 /* Kludge-o-matic */
507 file->name = rule->targets[matches[foundrule]];
509 /* FOUND_FILES lists the dependencies for the rule we found.
510 This includes the intermediate files, if any.
511 Convert them into entries on the deps-chain of FILE. */
513 while (deps_found-- > 0)
515 register char *s;
517 if (intermediate_files[deps_found] != 0)
519 /* If we need to use an intermediate file,
520 make sure it is entered as a target, with the info that was
521 found for it in the recursive pattern_search call.
522 We know that the intermediate file did not already exist as
523 a target; therefore we can assume that the deps and cmds
524 of F below are null before we change them. */
526 struct file *imf = intermediate_files[deps_found];
527 register struct file *f = enter_file (imf->name);
528 f->deps = imf->deps;
529 f->cmds = imf->cmds;
530 f->stem = imf->stem;
531 f->also_make = imf->also_make;
532 imf = lookup_file (intermediate_patterns[deps_found]);
533 if (imf != 0 && imf->precious)
534 f->precious = 1;
535 f->intermediate = 1;
536 f->tried_implicit = 1;
537 for (dep = f->deps; dep != 0; dep = dep->next)
539 dep->file = enter_file (dep->name);
540 /* enter_file uses dep->name _if_ we created a new file. */
541 if (dep->name != dep->file->name)
542 free (dep->name);
543 dep->name = 0;
544 dep->file->tried_implicit |= dep->changed;
548 dep = (struct dep *) xmalloc (sizeof (struct dep));
549 s = found_files[deps_found];
550 if (recursions == 0)
552 dep->name = 0;
553 dep->file = lookup_file (s);
554 if (dep->file == 0)
555 /* enter_file consumes S's storage. */
556 dep->file = enter_file (s);
557 else
558 /* A copy of S is already allocated in DEP->file->name.
559 So we can free S. */
560 free (s);
562 else
564 dep->name = s;
565 dep->file = 0;
566 dep->changed = 0;
568 if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
570 /* If the file actually existed (was not an intermediate file),
571 and the rule that found it was a terminal one, then we want
572 to mark the found file so that it will not have implicit rule
573 search done for it. If we are not entering a `struct file' for
574 it now, we indicate this with the `changed' flag. */
575 if (dep->file == 0)
576 dep->changed = 1;
577 else
578 dep->file->tried_implicit = 1;
580 dep->next = file->deps;
581 file->deps = dep;
584 if (!checked_lastslash[foundrule])
586 /* Always allocate new storage, since STEM might be
587 on the stack for an intermediate file. */
588 file->stem = savestring (stem, stemlen);
589 fullstemlen = stemlen;
591 else
593 int dirlen = (lastslash + 1) - filename;
595 /* We want to prepend the directory from
596 the original FILENAME onto the stem. */
597 fullstemlen = dirlen + stemlen;
598 file->stem = (char *) xmalloc (fullstemlen + 1);
599 bcopy (filename, file->stem, dirlen);
600 bcopy (stem, file->stem + dirlen, stemlen);
601 file->stem[fullstemlen] = '\0';
604 file->cmds = rule->cmds;
606 /* If this rule builds other targets, too, put the others into FILE's
607 `also_make' member. */
609 if (rule->targets[1] != 0)
610 for (i = 0; rule->targets[i] != 0; ++i)
611 if (i != matches[foundrule])
613 struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
614 new->name = p = (char *) xmalloc (rule->lens[i] + fullstemlen + 1);
615 bcopy (rule->targets[i], p,
616 rule->suffixes[i] - rule->targets[i] - 1);
617 p += rule->suffixes[i] - rule->targets[i] - 1;
618 bcopy (file->stem, p, fullstemlen);
619 p += fullstemlen;
620 bcopy (rule->suffixes[i], p,
621 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
622 new->file = enter_file (new->name);
623 new->next = file->also_make;
624 file->also_make = new;
628 return 1;