* texinfo.tex (\acronym): New Texinfo command.
[make.git] / implicit.c
blobf7298c6c4494f3be6db522ea0186bd928c214de8
1 /* Implicit rule searching for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,97 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "rule.h"
21 #include "dep.h"
22 #include "filedef.h"
24 static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth,
25 unsigned int recursions));
27 /* For a FILE which has no commands specified, try to figure out some
28 from the implicit pattern rules.
29 Returns 1 if a suitable implicit rule was found,
30 after modifying FILE to contain the appropriate commands and deps,
31 or returns 0 if no implicit rule was found. */
33 int
34 try_implicit_rule (file, depth)
35 struct file *file;
36 unsigned int depth;
38 DEBUGPR ("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 DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
54 if (pattern_search (file, 1, depth, 0))
55 return 1;
57 #endif
59 return 0;
62 #define DEBUGP2(msg, a1, a2) \
63 do { \
64 if (debug_flag) \
65 { print_spaces (depth); printf (msg, a1, a2); fflush (stdout); } \
66 } while (0)
68 /* Search the pattern rules for a rule with an existing dependency to make
69 FILE. If a rule is found, the appropriate commands and deps are put in FILE
70 and 1 is returned. If not, 0 is returned.
72 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
73 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
74 directory and filename parts.
76 If an intermediate file is found by pattern search, the intermediate file
77 is set up as a target by the recursive call and is also made a dependency
78 of FILE.
80 DEPTH is used for debugging messages. */
82 static int
83 pattern_search (file, archive, depth, recursions)
84 struct file *file;
85 int archive;
86 unsigned int depth;
87 unsigned int recursions;
89 /* Filename we are searching for a rule for. */
90 char *filename = archive ? index (file->name, '(') : file->name;
92 /* Length of FILENAME. */
93 unsigned int namelen = strlen (filename);
95 /* The last slash in FILENAME (or nil if there is none). */
96 char *lastslash;
98 /* This is a file-object used as an argument in
99 recursive calls. It never contains any data
100 except during a recursive call. */
101 struct file *intermediate_file = 0;
103 /* List of dependencies found recursively. */
104 struct file **intermediate_files
105 = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
107 /* List of the patterns used to find intermediate files. */
108 char **intermediate_patterns
109 = (char **) alloca (max_pattern_deps * sizeof (char *));
111 /* This buffer records all the dependencies actually found for a rule. */
112 char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
113 /* Number of dep names now in FOUND_FILES. */
114 unsigned int deps_found;
116 /* Names of possible dependencies are constructed in this buffer. */
117 register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
119 /* The start and length of the stem of FILENAME for the current rule. */
120 register char *stem;
121 register unsigned int stemlen;
123 /* Buffer in which we store all the rules that are possibly applicable. */
124 struct rule **tryrules
125 = (struct rule **) alloca (num_pattern_rules * max_pattern_targets
126 * sizeof (struct rule *));
128 /* Number of valid elements in TRYRULES. */
129 unsigned int nrules;
131 /* The numbers of the rule targets of each rule
132 in TRYRULES that matched the target file. */
133 unsigned int *matches
134 = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
136 /* Each element is nonzero if LASTSLASH was used in
137 matching the corresponding element of TRYRULES. */
138 char *checked_lastslash
139 = (char *) alloca (num_pattern_rules * sizeof (char));
141 /* The index in TRYRULES of the rule we found. */
142 unsigned int foundrule;
144 /* Nonzero if should consider intermediate files as dependencies. */
145 int intermed_ok;
147 /* Nonzero if we have matched a pattern-rule target
148 that is not just `%'. */
149 int specific_rule_matched = 0;
151 register unsigned int i;
152 register struct rule *rule;
153 register struct dep *dep;
155 char *p, *vp;
157 #ifndef NO_ARCHIVES
158 if (archive || ar_name (filename))
159 lastslash = 0;
160 else
161 #endif
163 /* Set LASTSLASH to point at the last slash in FILENAME
164 but not counting any slash at the end. (foo/bar/ counts as
165 bar/ in directory foo/, not empty in directory foo/bar/.) */
166 #ifdef VMS
167 lastslash = rindex (filename, ']');
168 #else
169 lastslash = rindex (filename, '/');
170 #ifdef __MSDOS__
171 /* Handle backslashes (possibly mixed with forward slashes)
172 and the case of "d:file". */
174 char *bslash = rindex (filename, '\\');
175 if (lastslash == 0 || bslash > lastslash)
176 lastslash = bslash;
177 if (lastslash == 0 && filename[0] && filename[1] == ':')
178 lastslash = filename + 1;
180 #endif
181 #endif
182 if (lastslash != 0 && lastslash[1] == '\0')
183 lastslash = 0;
186 /* First see which pattern rules match this target
187 and may be considered. Put them in TRYRULES. */
189 nrules = 0;
190 for (rule = pattern_rules; rule != 0; rule = rule->next)
192 /* If the pattern rule has deps but no commands, ignore it.
193 Users cancel built-in rules by redefining them without commands. */
194 if (rule->deps != 0 && rule->cmds == 0)
195 continue;
197 /* If this rule is in use by a parent pattern_search,
198 don't use it here. */
199 if (rule->in_use)
201 DEBUGP2 ("Avoiding implicit rule recursion.%s%s\n", "", "");
202 continue;
205 for (i = 0; rule->targets[i] != 0; ++i)
207 char *target = rule->targets[i];
208 char *suffix = rule->suffixes[i];
209 int check_lastslash;
211 /* Rules that can match any filename and are not terminal
212 are ignored if we're recursing, so that they cannot be
213 intermediate files. */
214 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
215 continue;
217 if (rule->lens[i] > namelen)
218 /* It can't possibly match. */
219 continue;
221 /* From the lengths of the filename and the pattern parts,
222 find the stem: the part of the filename that matches the %. */
223 stem = filename + (suffix - target - 1);
224 stemlen = namelen - rule->lens[i] + 1;
226 /* Set CHECK_LASTSLASH if FILENAME contains a directory
227 prefix and the target pattern does not contain a slash. */
229 #ifdef VMS
230 check_lastslash = lastslash != 0 && index (target, ']') == 0;
231 #else
232 check_lastslash = lastslash != 0 && index (target, '/') == 0;
233 #endif
234 if (check_lastslash)
236 /* In that case, don't include the
237 directory prefix in STEM here. */
238 unsigned int difference = lastslash - filename + 1;
239 if (difference > stemlen)
240 continue;
241 stemlen -= difference;
242 stem += difference;
245 /* Check that the rule pattern matches the text before the stem. */
246 if (check_lastslash)
248 if (stem > (lastslash + 1)
249 && strncmp (target, lastslash + 1, stem - lastslash - 1))
250 continue;
252 else if (stem > filename
253 && strncmp (target, filename, stem - filename))
254 continue;
256 /* Check that the rule pattern matches the text after the stem.
257 We could test simply use streq, but this way we compare the
258 first two characters immediately. This saves time in the very
259 common case where the first character matches because it is a
260 period. */
261 if (*suffix != stem[stemlen]
262 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
263 continue;
265 /* Record if we match a rule that not all filenames will match. */
266 if (target[1] != '\0')
267 specific_rule_matched = 1;
269 /* A rule with no dependencies and no commands exists solely to set
270 specific_rule_matched when it matches. Don't try to use it. */
271 if (rule->deps == 0 && rule->cmds == 0)
272 continue;
274 /* Record this rule in TRYRULES and the index of the matching
275 target in MATCHES. If several targets of the same rule match,
276 that rule will be in TRYRULES more than once. */
277 tryrules[nrules] = rule;
278 matches[nrules] = i;
279 checked_lastslash[nrules] = check_lastslash;
280 ++nrules;
284 /* If we have found a matching rule that won't match all filenames,
285 retroactively reject any non-"terminal" rules that do always match. */
286 if (specific_rule_matched)
287 for (i = 0; i < nrules; ++i)
288 if (!tryrules[i]->terminal)
290 register unsigned int j;
291 for (j = 0; tryrules[i]->targets[j] != 0; ++j)
292 if (tryrules[i]->targets[j][1] == '\0')
293 break;
294 if (tryrules[i]->targets[j] != 0)
295 tryrules[i] = 0;
298 /* Try each rule once without intermediate files, then once with them. */
299 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
301 /* Try each pattern rule till we find one that applies.
302 If it does, copy the names of its dependencies (as substituted)
303 and store them in FOUND_FILES. DEPS_FOUND is the number of them. */
305 for (i = 0; i < nrules; i++)
307 int check_lastslash;
309 rule = tryrules[i];
311 /* RULE is nil when we discover that a rule,
312 already placed in TRYRULES, should not be applied. */
313 if (rule == 0)
314 continue;
316 /* Reject any terminal rules if we're
317 looking to make intermediate files. */
318 if (intermed_ok && rule->terminal)
319 continue;
321 /* Mark this rule as in use so a recursive
322 pattern_search won't try to use it. */
323 rule->in_use = 1;
325 /* From the lengths of the filename and the matching pattern parts,
326 find the stem: the part of the filename that matches the %. */
327 stem = filename
328 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
329 stemlen = namelen - rule->lens[matches[i]] + 1;
330 check_lastslash = checked_lastslash[i];
331 if (check_lastslash)
333 stem += lastslash - filename + 1;
334 stemlen -= (lastslash - filename) + 1;
337 DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
338 (int) stemlen, stem);
340 /* Try each dependency; see if it "exists". */
342 deps_found = 0;
343 for (dep = rule->deps; dep != 0; dep = dep->next)
345 /* If the dependency name has a %, substitute the stem. */
346 p = index (dep_name (dep), '%');
347 if (p != 0)
349 register unsigned int i;
350 if (check_lastslash)
352 /* Copy directory name from the original FILENAME. */
353 i = lastslash - filename + 1;
354 bcopy (filename, depname, i);
356 else
357 i = 0;
358 bcopy (dep_name (dep), depname + i, p - dep_name (dep));
359 i += p - dep_name (dep);
360 bcopy (stem, depname + i, stemlen);
361 i += stemlen;
362 strcpy (depname + i, p + 1);
363 p = depname;
365 else
366 p = dep_name (dep);
368 /* P is now the actual dependency name as substituted. */
370 if (file_impossible_p (p))
372 /* If this dependency has already been ruled
373 "impossible", then the rule fails and don't
374 bother trying it on the second pass either
375 since we know that will fail too. */
376 DEBUGP2 ("Rejecting impossible %s dependency `%s'.\n",
377 p == depname ? "implicit" : "rule", p);
378 tryrules[i] = 0;
379 break;
382 intermediate_files[deps_found] = 0;
384 DEBUGP2 ("Trying %s dependency `%s'.\n",
385 p == depname ? "implicit" : "rule", p);
387 /* The DEP->changed flag says that this dependency resides in a
388 nonexistent directory. So we normally can skip looking for
389 the file. However, if CHECK_LASTSLASH is set, then the
390 dependency file we are actually looking for is in a different
391 directory (the one gotten by prepending FILENAME's directory),
392 so it might actually exist. */
394 if ((!dep->changed || check_lastslash)
395 && (lookup_file (p) != 0 || file_exists_p (p)))
397 found_files[deps_found++] = savestring (p, strlen (p));
398 continue;
400 /* This code, given FILENAME = "lib/foo.o", dependency name
401 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
402 vp = p;
403 if (vpath_search (&vp, (time_t *) 0))
405 DEBUGP2 ("Found dependency `%s' as VPATH `%s'\n", p, vp);
406 strcpy(vp, p);
407 found_files[deps_found++] = vp;
408 continue;
411 /* We could not find the file in any place we should look.
412 Try to make this dependency as an intermediate file,
413 but only on the second pass. */
415 if (intermed_ok)
417 if (intermediate_file == 0)
418 intermediate_file
419 = (struct file *) alloca (sizeof (struct file));
421 DEBUGP2 ("Looking for a rule with %s file `%s'.\n",
422 "intermediate", p);
424 bzero ((char *) intermediate_file, sizeof (struct file));
425 intermediate_file->name = p;
426 if (pattern_search (intermediate_file, 0, depth + 1,
427 recursions + 1))
429 p = savestring (p, strlen (p));
430 intermediate_patterns[deps_found]
431 = intermediate_file->name;
432 intermediate_file->name = p;
433 intermediate_files[deps_found] = intermediate_file;
434 intermediate_file = 0;
435 /* Allocate an extra copy to go in FOUND_FILES,
436 because every elt of FOUND_FILES is consumed
437 or freed later. */
438 found_files[deps_found] = savestring (p, strlen (p));
439 ++deps_found;
440 continue;
443 /* If we have tried to find P as an intermediate
444 file and failed, mark that name as impossible
445 so we won't go through the search again later. */
446 file_impossible (p);
449 /* A dependency of this rule does not exist.
450 Therefore, this rule fails. */
451 break;
454 /* This rule is no longer `in use' for recursive searches. */
455 rule->in_use = 0;
457 if (dep != 0)
459 /* This pattern rule does not apply.
460 If some of its dependencies succeeded,
461 free the data structure describing them. */
462 while (deps_found-- > 0)
464 register struct file *f = intermediate_files[deps_found];
465 free (found_files[deps_found]);
466 if (f != 0
467 && (f->stem < f->name
468 || f->stem > f->name + strlen (f->name)))
469 free (f->stem);
472 else
473 /* This pattern rule does apply. Stop looking for one. */
474 break;
477 /* If we found an applicable rule without
478 intermediate files, don't try with them. */
479 if (i < nrules)
480 break;
482 rule = 0;
485 /* RULE is nil if the loop went all the way
486 through the list and everything failed. */
487 if (rule == 0)
488 return 0;
490 foundrule = i;
492 /* If we are recursing, store the pattern that matched
493 FILENAME in FILE->name for use in upper levels. */
495 if (recursions > 0)
496 /* Kludge-o-matic */
497 file->name = rule->targets[matches[foundrule]];
499 /* FOUND_FILES lists the dependencies for the rule we found.
500 This includes the intermediate files, if any.
501 Convert them into entries on the deps-chain of FILE. */
503 while (deps_found-- > 0)
505 register char *s;
507 if (intermediate_files[deps_found] != 0)
509 /* If we need to use an intermediate file,
510 make sure it is entered as a target, with the info that was
511 found for it in the recursive pattern_search call.
512 We know that the intermediate file did not already exist as
513 a target; therefore we can assume that the deps and cmds
514 of F below are null before we change them. */
516 struct file *imf = intermediate_files[deps_found];
517 register struct file *f = enter_file (imf->name);
518 f->deps = imf->deps;
519 f->cmds = imf->cmds;
520 f->stem = imf->stem;
521 imf = lookup_file (intermediate_patterns[deps_found]);
522 if (imf != 0 && imf->precious)
523 f->precious = 1;
524 f->intermediate = 1;
525 f->tried_implicit = 1;
526 for (dep = f->deps; dep != 0; dep = dep->next)
528 dep->file = enter_file (dep->name);
529 dep->name = 0;
530 dep->file->tried_implicit |= dep->changed;
532 num_intermediates++;
535 dep = (struct dep *) xmalloc (sizeof (struct dep));
536 s = found_files[deps_found];
537 if (recursions == 0)
539 dep->name = 0;
540 dep->file = lookup_file (s);
541 if (dep->file == 0)
542 /* enter_file consumes S's storage. */
543 dep->file = enter_file (s);
544 else
545 /* A copy of S is already allocated in DEP->file->name.
546 So we can free S. */
547 free (s);
549 else
551 dep->name = s;
552 dep->file = 0;
553 dep->changed = 0;
555 if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
557 /* If the file actually existed (was not an intermediate file),
558 and the rule that found it was a terminal one, then we want
559 to mark the found file so that it will not have implicit rule
560 search done for it. If we are not entering a `struct file' for
561 it now, we indicate this with the `changed' flag. */
562 if (dep->file == 0)
563 dep->changed = 1;
564 else
565 dep->file->tried_implicit = 1;
567 dep->next = file->deps;
568 file->deps = dep;
571 if (!checked_lastslash[foundrule])
572 /* Always allocate new storage, since STEM might be
573 on the stack for an intermediate file. */
574 file->stem = savestring (stem, stemlen);
575 else
577 /* We want to prepend the directory from
578 the original FILENAME onto the stem. */
579 file->stem = (char *) xmalloc (((lastslash + 1) - filename)
580 + stemlen + 1);
581 bcopy (filename, file->stem, (lastslash + 1) - filename);
582 bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
583 file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
586 file->cmds = rule->cmds;
588 /* Put the targets other than the one that
589 matched into FILE's `also_make' member. */
591 /* If there was only one target, there is nothing to do. */
592 if (rule->targets[1] != 0)
593 for (i = 0; rule->targets[i] != 0; ++i)
594 if (i != matches[foundrule])
596 struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
597 new->name = p = (char *) xmalloc (rule->lens[i] + stemlen + 1);
598 bcopy (rule->targets[i], p,
599 rule->suffixes[i] - rule->targets[i] - 1);
600 p += rule->suffixes[i] - rule->targets[i] - 1;
601 bcopy (stem, p, stemlen);
602 p += stemlen;
603 bcopy (rule->suffixes[i], p,
604 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
605 new->file = enter_file (new->name);
606 new->next = file->also_make;
607 file->also_make = new;
611 return 1;