Updated for autoconf 2.57, automake 1.7.6, and gettext 0.12.1.
[make.git] / implicit.c
blob944d57a970782bcec82f14bc78277e8bcea824e2
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 "filedef.h"
22 #include "rule.h"
23 #include "dep.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 (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 /* Search the pattern rules for a rule with an existing dependency to make
65 FILE. If a rule is found, the appropriate commands and deps are put in FILE
66 and 1 is returned. If not, 0 is returned.
68 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
69 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
70 directory and filename parts.
72 If an intermediate file is found by pattern search, the intermediate file
73 is set up as a target by the recursive call and is also made a dependency
74 of FILE.
76 DEPTH is used for debugging messages. */
78 static int
79 pattern_search (struct file *file, int archive,
80 unsigned int depth, unsigned int recursions)
82 /* Filename we are searching for a rule for. */
83 char *filename = archive ? strchr (file->name, '(') : file->name;
85 /* Length of FILENAME. */
86 unsigned int namelen = strlen (filename);
88 /* The last slash in FILENAME (or nil if there is none). */
89 char *lastslash;
91 /* This is a file-object used as an argument in
92 recursive calls. It never contains any data
93 except during a recursive call. */
94 struct file *intermediate_file = 0;
96 /* List of dependencies found recursively. */
97 struct file **intermediate_files
98 = (struct file **) xmalloc (max_pattern_deps * sizeof (struct file *));
100 /* List of the patterns used to find intermediate files. */
101 char **intermediate_patterns
102 = (char **) alloca (max_pattern_deps * sizeof (char *));
104 /* This buffer records all the dependencies actually found for a rule. */
105 char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
106 /* Number of dep names now in FOUND_FILES. */
107 unsigned int deps_found = 0;
109 /* Names of possible dependencies are constructed in this buffer. */
110 register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
112 /* The start and length of the stem of FILENAME for the current rule. */
113 register char *stem = 0;
114 register unsigned int stemlen = 0;
115 register unsigned int fullstemlen = 0;
117 /* Buffer in which we store all the rules that are possibly applicable. */
118 struct rule **tryrules
119 = (struct rule **) xmalloc (num_pattern_rules * max_pattern_targets
120 * sizeof (struct rule *));
122 /* Number of valid elements in TRYRULES. */
123 unsigned int nrules;
125 /* The numbers of the rule targets of each rule
126 in TRYRULES that matched the target file. */
127 unsigned int *matches
128 = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
130 /* Each element is nonzero if LASTSLASH was used in
131 matching the corresponding element of TRYRULES. */
132 char *checked_lastslash
133 = (char *) alloca (num_pattern_rules * sizeof (char));
135 /* The index in TRYRULES of the rule we found. */
136 unsigned int foundrule;
138 /* Nonzero if should consider intermediate files as dependencies. */
139 int intermed_ok;
141 /* Nonzero if we have matched a pattern-rule target
142 that is not just `%'. */
143 int specific_rule_matched = 0;
145 register unsigned int i = 0; /* uninit checks OK */
146 register struct rule *rule;
147 register struct dep *dep;
149 char *p, *vp;
151 #ifndef NO_ARCHIVES
152 if (archive || ar_name (filename))
153 lastslash = 0;
154 else
155 #endif
157 /* Set LASTSLASH to point at the last slash in FILENAME
158 but not counting any slash at the end. (foo/bar/ counts as
159 bar/ in directory foo/, not empty in directory foo/bar/.) */
160 #ifdef VMS
161 lastslash = strrchr (filename, ']');
162 if (lastslash == 0)
163 lastslash = strrchr (filename, ':');
164 #else
165 lastslash = strrchr (filename, '/');
166 #ifdef HAVE_DOS_PATHS
167 /* Handle backslashes (possibly mixed with forward slashes)
168 and the case of "d:file". */
170 char *bslash = strrchr (filename, '\\');
171 if (lastslash == 0 || bslash > lastslash)
172 lastslash = bslash;
173 if (lastslash == 0 && filename[0] && filename[1] == ':')
174 lastslash = filename + 1;
176 #endif
177 #endif
178 if (lastslash != 0 && lastslash[1] == '\0')
179 lastslash = 0;
182 /* First see which pattern rules match this target
183 and may be considered. Put them in TRYRULES. */
185 nrules = 0;
186 for (rule = pattern_rules; rule != 0; rule = rule->next)
188 /* If the pattern rule has deps but no commands, ignore it.
189 Users cancel built-in rules by redefining them without commands. */
190 if (rule->deps != 0 && rule->cmds == 0)
191 continue;
193 /* If this rule is in use by a parent pattern_search,
194 don't use it here. */
195 if (rule->in_use)
197 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
198 continue;
201 for (i = 0; rule->targets[i] != 0; ++i)
203 char *target = rule->targets[i];
204 char *suffix = rule->suffixes[i];
205 int check_lastslash;
207 /* Rules that can match any filename and are not terminal
208 are ignored if we're recursing, so that they cannot be
209 intermediate files. */
210 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
211 continue;
213 if (rule->lens[i] > namelen)
214 /* It can't possibly match. */
215 continue;
217 /* From the lengths of the filename and the pattern parts,
218 find the stem: the part of the filename that matches the %. */
219 stem = filename + (suffix - target - 1);
220 stemlen = namelen - rule->lens[i] + 1;
222 /* Set CHECK_LASTSLASH if FILENAME contains a directory
223 prefix and the target pattern does not contain a slash. */
225 #ifdef VMS
226 check_lastslash = lastslash != 0
227 && ((strchr (target, ']') == 0)
228 && (strchr (target, ':') == 0));
229 #else
230 check_lastslash = lastslash != 0 && strchr (target, '/') == 0;
231 #endif
232 if (check_lastslash)
234 /* In that case, don't include the
235 directory prefix in STEM here. */
236 unsigned int difference = lastslash - filename + 1;
237 if (difference > stemlen)
238 continue;
239 stemlen -= difference;
240 stem += difference;
243 /* Check that the rule pattern matches the text before the stem. */
244 if (check_lastslash)
246 if (stem > (lastslash + 1)
247 && !strneq (target, lastslash + 1, stem - lastslash - 1))
248 continue;
250 else if (stem > filename
251 && !strneq (target, filename, stem - filename))
252 continue;
254 /* Check that the rule pattern matches the text after the stem.
255 We could test simply use streq, but this way we compare the
256 first two characters immediately. This saves time in the very
257 common case where the first character matches because it is a
258 period. */
259 if (*suffix != stem[stemlen]
260 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
261 continue;
263 /* Record if we match a rule that not all filenames will match. */
264 if (target[1] != '\0')
265 specific_rule_matched = 1;
267 /* A rule with no dependencies and no commands exists solely to set
268 specific_rule_matched when it matches. Don't try to use it. */
269 if (rule->deps == 0 && rule->cmds == 0)
270 continue;
272 /* Record this rule in TRYRULES and the index of the matching
273 target in MATCHES. If several targets of the same rule match,
274 that rule will be in TRYRULES more than once. */
275 tryrules[nrules] = rule;
276 matches[nrules] = i;
277 checked_lastslash[nrules] = check_lastslash;
278 ++nrules;
282 /* If we have found a matching rule that won't match all filenames,
283 retroactively reject any non-"terminal" rules that do always match. */
284 if (specific_rule_matched)
285 for (i = 0; i < nrules; ++i)
286 if (!tryrules[i]->terminal)
288 register unsigned int j;
289 for (j = 0; tryrules[i]->targets[j] != 0; ++j)
290 if (tryrules[i]->targets[j][1] == '\0')
291 break;
292 if (tryrules[i]->targets[j] != 0)
293 tryrules[i] = 0;
296 /* Try each rule once without intermediate files, then once with them. */
297 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
299 /* Try each pattern rule till we find one that applies.
300 If it does, copy the names of its dependencies (as substituted)
301 and store them in FOUND_FILES. DEPS_FOUND is the number of them. */
303 for (i = 0; i < nrules; i++)
305 int check_lastslash;
307 rule = tryrules[i];
309 /* RULE is nil when we discover that a rule,
310 already placed in TRYRULES, should not be applied. */
311 if (rule == 0)
312 continue;
314 /* Reject any terminal rules if we're
315 looking to make intermediate files. */
316 if (intermed_ok && rule->terminal)
317 continue;
319 /* Mark this rule as in use so a recursive
320 pattern_search won't try to use it. */
321 rule->in_use = 1;
323 /* From the lengths of the filename and the matching pattern parts,
324 find the stem: the part of the filename that matches the %. */
325 stem = filename
326 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
327 stemlen = namelen - rule->lens[matches[i]] + 1;
328 check_lastslash = checked_lastslash[i];
329 if (check_lastslash)
331 stem += lastslash - filename + 1;
332 stemlen -= (lastslash - filename) + 1;
335 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
336 (int) stemlen, stem));
338 /* Try each dependency; see if it "exists". */
340 deps_found = 0;
341 for (dep = rule->deps; dep != 0; dep = dep->next)
343 /* If the dependency name has a %, substitute the stem. */
344 p = strchr (dep_name (dep), '%');
345 if (p != 0)
347 register unsigned int i;
348 if (check_lastslash)
350 /* Copy directory name from the original FILENAME. */
351 i = lastslash - filename + 1;
352 bcopy (filename, depname, i);
354 else
355 i = 0;
356 bcopy (dep_name (dep), depname + i, p - dep_name (dep));
357 i += p - dep_name (dep);
358 bcopy (stem, depname + i, stemlen);
359 i += stemlen;
360 strcpy (depname + i, p + 1);
361 p = depname;
363 else
364 p = dep_name (dep);
366 /* P is now the actual dependency name as substituted. */
368 if (file_impossible_p (p))
370 /* If this dependency has already been ruled
371 "impossible", then the rule fails and don't
372 bother trying it on the second pass either
373 since we know that will fail too. */
374 DBS (DB_IMPLICIT,
375 (p == depname
376 ? _("Rejecting impossible implicit prerequisite `%s'.\n")
377 : _("Rejecting impossible rule prerequisite `%s'.\n"),
378 p));
379 tryrules[i] = 0;
380 break;
383 intermediate_files[deps_found] = 0;
385 DBS (DB_IMPLICIT,
386 (p == depname
387 ? _("Trying implicit prerequisite `%s'.\n")
388 : _("Trying rule prerequisite `%s'.\n"), p));
390 /* The DEP->changed flag says that this dependency resides in a
391 nonexistent directory. So we normally can skip looking for
392 the file. However, if CHECK_LASTSLASH is set, then the
393 dependency file we are actually looking for is in a different
394 directory (the one gotten by prepending FILENAME's directory),
395 so it might actually exist. */
397 if (lookup_file (p) != 0
398 || ((!dep->changed || check_lastslash) && file_exists_p (p)))
400 found_files[deps_found++] = xstrdup (p);
401 continue;
403 /* This code, given FILENAME = "lib/foo.o", dependency name
404 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
405 vp = p;
406 if (vpath_search (&vp, (FILE_TIMESTAMP *) 0))
408 DBS (DB_IMPLICIT,
409 (_("Found prerequisite `%s' as VPATH `%s'\n"), p, vp));
410 strcpy (vp, p);
411 found_files[deps_found++] = vp;
412 continue;
415 /* We could not find the file in any place we should look.
416 Try to make this dependency as an intermediate file,
417 but only on the second pass. */
419 if (intermed_ok)
421 if (intermediate_file == 0)
422 intermediate_file
423 = (struct file *) alloca (sizeof (struct file));
425 DBS (DB_IMPLICIT,
426 (_("Looking for a rule with intermediate file `%s'.\n"),
427 p));
429 bzero ((char *) intermediate_file, sizeof (struct file));
430 intermediate_file->name = p;
431 if (pattern_search (intermediate_file, 0, depth + 1,
432 recursions + 1))
434 p = xstrdup (p);
435 intermediate_patterns[deps_found]
436 = intermediate_file->name;
437 intermediate_file->name = p;
438 intermediate_files[deps_found] = intermediate_file;
439 intermediate_file = 0;
440 /* Allocate an extra copy to go in FOUND_FILES,
441 because every elt of FOUND_FILES is consumed
442 or freed later. */
443 found_files[deps_found] = xstrdup (p);
444 ++deps_found;
445 continue;
448 /* If we have tried to find P as an intermediate
449 file and failed, mark that name as impossible
450 so we won't go through the search again later. */
451 file_impossible (p);
454 /* A dependency of this rule does not exist.
455 Therefore, this rule fails. */
456 break;
459 /* This rule is no longer `in use' for recursive searches. */
460 rule->in_use = 0;
462 if (dep != 0)
464 /* This pattern rule does not apply.
465 If some of its dependencies succeeded,
466 free the data structure describing them. */
467 while (deps_found-- > 0)
469 register struct file *f = intermediate_files[deps_found];
470 free (found_files[deps_found]);
471 if (f != 0
472 && (f->stem < f->name
473 || f->stem > f->name + strlen (f->name)))
474 free (f->stem);
477 else
478 /* This pattern rule does apply. Stop looking for one. */
479 break;
482 /* If we found an applicable rule without
483 intermediate files, don't try with them. */
484 if (i < nrules)
485 break;
487 rule = 0;
490 /* RULE is nil if the loop went all the way
491 through the list and everything failed. */
492 if (rule == 0)
493 goto done;
495 foundrule = i;
497 /* If we are recursing, store the pattern that matched
498 FILENAME in FILE->name for use in upper levels. */
500 if (recursions > 0)
501 /* Kludge-o-matic */
502 file->name = rule->targets[matches[foundrule]];
504 /* FOUND_FILES lists the dependencies for the rule we found.
505 This includes the intermediate files, if any.
506 Convert them into entries on the deps-chain of FILE. */
508 while (deps_found-- > 0)
510 register char *s;
512 if (intermediate_files[deps_found] != 0)
514 /* If we need to use an intermediate file,
515 make sure it is entered as a target, with the info that was
516 found for it in the recursive pattern_search call.
517 We know that the intermediate file did not already exist as
518 a target; therefore we can assume that the deps and cmds
519 of F below are null before we change them. */
521 struct file *imf = intermediate_files[deps_found];
522 register struct file *f = enter_file (imf->name);
523 f->deps = imf->deps;
524 f->cmds = imf->cmds;
525 f->stem = imf->stem;
526 f->also_make = imf->also_make;
527 imf = lookup_file (intermediate_patterns[deps_found]);
528 if (imf != 0 && imf->precious)
529 f->precious = 1;
530 f->intermediate = 1;
531 f->tried_implicit = 1;
532 for (dep = f->deps; dep != 0; dep = dep->next)
534 dep->file = enter_file (dep->name);
535 /* enter_file uses dep->name _if_ we created a new file. */
536 if (dep->name != dep->file->name)
537 free (dep->name);
538 dep->name = 0;
539 dep->file->tried_implicit |= dep->changed;
543 dep = (struct dep *) xmalloc (sizeof (struct dep));
544 dep->ignore_mtime = 0;
545 s = found_files[deps_found];
546 if (recursions == 0)
548 dep->name = 0;
549 dep->file = lookup_file (s);
550 if (dep->file == 0)
551 /* enter_file consumes S's storage. */
552 dep->file = enter_file (s);
553 else
554 /* A copy of S is already allocated in DEP->file->name.
555 So we can free S. */
556 free (s);
558 else
560 dep->name = s;
561 dep->file = 0;
562 dep->changed = 0;
564 if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
566 /* If the file actually existed (was not an intermediate file),
567 and the rule that found it was a terminal one, then we want
568 to mark the found file so that it will not have implicit rule
569 search done for it. If we are not entering a `struct file' for
570 it now, we indicate this with the `changed' flag. */
571 if (dep->file == 0)
572 dep->changed = 1;
573 else
574 dep->file->tried_implicit = 1;
576 dep->next = file->deps;
577 file->deps = dep;
580 if (!checked_lastslash[foundrule])
582 /* Always allocate new storage, since STEM might be
583 on the stack for an intermediate file. */
584 file->stem = savestring (stem, stemlen);
585 fullstemlen = stemlen;
587 else
589 int dirlen = (lastslash + 1) - filename;
591 /* We want to prepend the directory from
592 the original FILENAME onto the stem. */
593 fullstemlen = dirlen + stemlen;
594 file->stem = (char *) xmalloc (fullstemlen + 1);
595 bcopy (filename, file->stem, dirlen);
596 bcopy (stem, file->stem + dirlen, stemlen);
597 file->stem[fullstemlen] = '\0';
600 file->cmds = rule->cmds;
602 /* If this rule builds other targets, too, put the others into FILE's
603 `also_make' member. */
605 if (rule->targets[1] != 0)
606 for (i = 0; rule->targets[i] != 0; ++i)
607 if (i != matches[foundrule])
609 struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
610 /* GKM FIMXE: handle '|' here too */
611 new->ignore_mtime = 0;
612 new->name = p = (char *) xmalloc (rule->lens[i] + fullstemlen + 1);
613 bcopy (rule->targets[i], p,
614 rule->suffixes[i] - rule->targets[i] - 1);
615 p += rule->suffixes[i] - rule->targets[i] - 1;
616 bcopy (file->stem, p, fullstemlen);
617 p += fullstemlen;
618 bcopy (rule->suffixes[i], p,
619 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
620 new->file = enter_file (new->name);
621 new->next = file->also_make;
622 file->also_make = new;
625 done:
626 free (intermediate_files);
627 free (tryrules);
629 return rule != 0;