* Ignore CR in CRLF line terminators for compatibility with DOSsy
[make.git] / implicit.c
blobdfe19da4c4db365f428480373f47b171bd9a5b3f
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, 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"
25 static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth,
26 unsigned int recursions));
28 /* For a FILE which has no commands specified, try to figure out some
29 from the implicit pattern rules.
30 Returns 1 if a suitable implicit rule was found,
31 after modifying FILE to contain the appropriate commands and deps,
32 or returns 0 if no implicit rule was found. */
34 int
35 try_implicit_rule (file, depth)
36 struct file *file;
37 unsigned int depth;
39 DEBUGPR ("Looking for an implicit rule for `%s'.\n");
41 /* The order of these searches was previously reversed. My logic now is
42 that since the non-archive search uses more information in the target
43 (the archive search omits the archive name), it is more specific and
44 should come first. */
46 if (pattern_search (file, 0, depth, 0))
47 return 1;
49 #ifndef NO_ARCHIVES
50 /* If this is an archive member reference, use just the
51 archive member name to search for implicit rules. */
52 if (ar_name (file->name))
54 DEBUGPR ("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;
63 #define DEBUGP2(msg, a1, a2) \
64 do { \
65 if (debug_flag) \
66 { print_spaces (depth); printf (msg, a1, a2); fflush (stdout); } \
67 } while (0)
69 /* Search the pattern rules for a rule with an existing dependency to make
70 FILE. If a rule is found, the appropriate commands and deps are put in FILE
71 and 1 is returned. If not, 0 is returned.
73 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
74 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
75 directory and filename parts.
77 If an intermediate file is found by pattern search, the intermediate file
78 is set up as a target by the recursive call and is also made a dependency
79 of FILE.
81 DEPTH is used for debugging messages. */
83 static int
84 pattern_search (file, archive, depth, recursions)
85 struct file *file;
86 int archive;
87 unsigned int depth;
88 unsigned int recursions;
90 /* Filename we are searching for a rule for. */
91 char *filename = archive ? index (file->name, '(') : file->name;
93 /* Length of FILENAME. */
94 unsigned int namelen = strlen (filename);
96 /* The last slash in FILENAME (or nil if there is none). */
97 char *lastslash;
99 /* This is a file-object used as an argument in
100 recursive calls. It never contains any data
101 except during a recursive call. */
102 struct file *intermediate_file = 0;
104 /* List of dependencies found recursively. */
105 struct file **intermediate_files
106 = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
108 /* List of the patterns used to find intermediate files. */
109 char **intermediate_patterns
110 = (char **) alloca (max_pattern_deps * sizeof (char *));
112 /* This buffer records all the dependencies actually found for a rule. */
113 char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
114 /* Number of dep names now in FOUND_FILES. */
115 unsigned int deps_found = 0;
117 /* Names of possible dependencies are constructed in this buffer. */
118 register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
120 /* The start and length of the stem of FILENAME for the current rule. */
121 register char *stem = 0;
122 register unsigned int stemlen = 0;
124 /* Buffer in which we store all the rules that are possibly applicable. */
125 struct rule **tryrules
126 = (struct rule **) alloca (num_pattern_rules * max_pattern_targets
127 * sizeof (struct rule *));
129 /* Number of valid elements in TRYRULES. */
130 unsigned int nrules;
132 /* The numbers of the rule targets of each rule
133 in TRYRULES that matched the target file. */
134 unsigned int *matches
135 = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
137 /* Each element is nonzero if LASTSLASH was used in
138 matching the corresponding element of TRYRULES. */
139 char *checked_lastslash
140 = (char *) alloca (num_pattern_rules * sizeof (char));
142 /* The index in TRYRULES of the rule we found. */
143 unsigned int foundrule;
145 /* Nonzero if should consider intermediate files as dependencies. */
146 int intermed_ok;
148 /* Nonzero if we have matched a pattern-rule target
149 that is not just `%'. */
150 int specific_rule_matched = 0;
152 register unsigned int i = 0; /* uninit checks OK */
153 register struct rule *rule;
154 register struct dep *dep;
156 char *p, *vp;
158 #ifndef NO_ARCHIVES
159 if (archive || ar_name (filename))
160 lastslash = 0;
161 else
162 #endif
164 /* Set LASTSLASH to point at the last slash in FILENAME
165 but not counting any slash at the end. (foo/bar/ counts as
166 bar/ in directory foo/, not empty in directory foo/bar/.) */
167 #ifdef VMS
168 lastslash = rindex (filename, ']');
169 #else
170 lastslash = rindex (filename, '/');
171 #ifdef __MSDOS__
172 /* Handle backslashes (possibly mixed with forward slashes)
173 and the case of "d:file". */
175 char *bslash = rindex (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 DEBUGP2 ("Avoiding implicit rule recursion.%s%s\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 && index (target, ']') == 0;
232 #else
233 check_lastslash = lastslash != 0 && index (target, '/') == 0;
234 #endif
235 if (check_lastslash)
237 /* In that case, don't include the
238 directory prefix in STEM here. */
239 unsigned int difference = lastslash - filename + 1;
240 if (difference > stemlen)
241 continue;
242 stemlen -= difference;
243 stem += difference;
246 /* Check that the rule pattern matches the text before the stem. */
247 if (check_lastslash)
249 if (stem > (lastslash + 1)
250 && strncmp (target, lastslash + 1, stem - lastslash - 1))
251 continue;
253 else if (stem > filename
254 && strncmp (target, filename, stem - filename))
255 continue;
257 /* Check that the rule pattern matches the text after the stem.
258 We could test simply use streq, but this way we compare the
259 first two characters immediately. This saves time in the very
260 common case where the first character matches because it is a
261 period. */
262 if (*suffix != stem[stemlen]
263 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
264 continue;
266 /* Record if we match a rule that not all filenames will match. */
267 if (target[1] != '\0')
268 specific_rule_matched = 1;
270 /* A rule with no dependencies and no commands exists solely to set
271 specific_rule_matched when it matches. Don't try to use it. */
272 if (rule->deps == 0 && rule->cmds == 0)
273 continue;
275 /* Record this rule in TRYRULES and the index of the matching
276 target in MATCHES. If several targets of the same rule match,
277 that rule will be in TRYRULES more than once. */
278 tryrules[nrules] = rule;
279 matches[nrules] = i;
280 checked_lastslash[nrules] = check_lastslash;
281 ++nrules;
285 /* If we have found a matching rule that won't match all filenames,
286 retroactively reject any non-"terminal" rules that do always match. */
287 if (specific_rule_matched)
288 for (i = 0; i < nrules; ++i)
289 if (!tryrules[i]->terminal)
291 register unsigned int j;
292 for (j = 0; tryrules[i]->targets[j] != 0; ++j)
293 if (tryrules[i]->targets[j][1] == '\0')
294 break;
295 if (tryrules[i]->targets[j] != 0)
296 tryrules[i] = 0;
299 /* Try each rule once without intermediate files, then once with them. */
300 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
302 /* Try each pattern rule till we find one that applies.
303 If it does, copy the names of its dependencies (as substituted)
304 and store them in FOUND_FILES. DEPS_FOUND is the number of them. */
306 for (i = 0; i < nrules; i++)
308 int check_lastslash;
310 rule = tryrules[i];
312 /* RULE is nil when we discover that a rule,
313 already placed in TRYRULES, should not be applied. */
314 if (rule == 0)
315 continue;
317 /* Reject any terminal rules if we're
318 looking to make intermediate files. */
319 if (intermed_ok && rule->terminal)
320 continue;
322 /* Mark this rule as in use so a recursive
323 pattern_search won't try to use it. */
324 rule->in_use = 1;
326 /* From the lengths of the filename and the matching pattern parts,
327 find the stem: the part of the filename that matches the %. */
328 stem = filename
329 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
330 stemlen = namelen - rule->lens[matches[i]] + 1;
331 check_lastslash = checked_lastslash[i];
332 if (check_lastslash)
334 stem += lastslash - filename + 1;
335 stemlen -= (lastslash - filename) + 1;
338 DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
339 (int) stemlen, stem);
341 /* Try each dependency; see if it "exists". */
343 deps_found = 0;
344 for (dep = rule->deps; dep != 0; dep = dep->next)
346 struct file *fp;
348 /* If the dependency name has a %, substitute the stem. */
349 p = index (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 DEBUGP2 ("Rejecting impossible %s dependency `%s'.\n",
380 p == depname ? "implicit" : "rule", p);
381 tryrules[i] = 0;
382 break;
385 intermediate_files[deps_found] = 0;
387 DEBUGP2 ("Trying %s dependency `%s'.\n",
388 p == depname ? "implicit" : "rule", 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. */
396 /* If we find a file but the intermediate flag is set, then it
397 was put here by a .INTERMEDIATE: rule so ignore it. */
399 if ((!dep->changed || check_lastslash)
400 && (((fp = lookup_file (p)) != 0 && !fp->intermediate)
401 || file_exists_p (p)))
403 found_files[deps_found++] = savestring (p, strlen (p));
404 continue;
406 /* This code, given FILENAME = "lib/foo.o", dependency name
407 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
408 vp = p;
409 if (vpath_search (&vp, (FILE_TIMESTAMP *) 0))
411 DEBUGP2 ("Found dependency `%s' as VPATH `%s'\n", p, vp);
412 strcpy(vp, p);
413 found_files[deps_found++] = vp;
414 continue;
417 /* We could not find the file in any place we should look.
418 Try to make this dependency as an intermediate file,
419 but only on the second pass. */
421 if (intermed_ok)
423 if (intermediate_file == 0)
424 intermediate_file
425 = (struct file *) alloca (sizeof (struct file));
427 DEBUGP2 ("Looking for a rule with %s file `%s'.\n",
428 "intermediate", p);
430 bzero ((char *) intermediate_file, sizeof (struct file));
431 intermediate_file->name = p;
432 if (pattern_search (intermediate_file, 0, depth + 1,
433 recursions + 1))
435 p = savestring (p, strlen (p));
436 intermediate_patterns[deps_found]
437 = intermediate_file->name;
438 intermediate_file->name = p;
439 intermediate_files[deps_found] = intermediate_file;
440 intermediate_file = 0;
441 /* Allocate an extra copy to go in FOUND_FILES,
442 because every elt of FOUND_FILES is consumed
443 or freed later. */
444 found_files[deps_found] = savestring (p, strlen (p));
445 ++deps_found;
446 continue;
449 /* If we have tried to find P as an intermediate
450 file and failed, mark that name as impossible
451 so we won't go through the search again later. */
452 file_impossible (p);
455 /* A dependency of this rule does not exist.
456 Therefore, this rule fails. */
457 break;
460 /* This rule is no longer `in use' for recursive searches. */
461 rule->in_use = 0;
463 if (dep != 0)
465 /* This pattern rule does not apply.
466 If some of its dependencies succeeded,
467 free the data structure describing them. */
468 while (deps_found-- > 0)
470 register struct file *f = intermediate_files[deps_found];
471 free (found_files[deps_found]);
472 if (f != 0
473 && (f->stem < f->name
474 || f->stem > f->name + strlen (f->name)))
475 free (f->stem);
478 else
479 /* This pattern rule does apply. Stop looking for one. */
480 break;
483 /* If we found an applicable rule without
484 intermediate files, don't try with them. */
485 if (i < nrules)
486 break;
488 rule = 0;
491 /* RULE is nil if the loop went all the way
492 through the list and everything failed. */
493 if (rule == 0)
494 return 0;
496 foundrule = i;
498 /* If we are recursing, store the pattern that matched
499 FILENAME in FILE->name for use in upper levels. */
501 if (recursions > 0)
502 /* Kludge-o-matic */
503 file->name = rule->targets[matches[foundrule]];
505 /* FOUND_FILES lists the dependencies for the rule we found.
506 This includes the intermediate files, if any.
507 Convert them into entries on the deps-chain of FILE. */
509 while (deps_found-- > 0)
511 register char *s;
513 if (intermediate_files[deps_found] != 0)
515 /* If we need to use an intermediate file,
516 make sure it is entered as a target, with the info that was
517 found for it in the recursive pattern_search call.
518 We know that the intermediate file did not already exist as
519 a target; therefore we can assume that the deps and cmds
520 of F below are null before we change them. */
522 struct file *imf = intermediate_files[deps_found];
523 register struct file *f = enter_file (imf->name);
524 f->deps = imf->deps;
525 f->cmds = imf->cmds;
526 f->stem = imf->stem;
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 dep->name = 0;
536 dep->file->tried_implicit |= dep->changed;
538 num_intermediates++;
541 dep = (struct dep *) xmalloc (sizeof (struct dep));
542 s = found_files[deps_found];
543 if (recursions == 0)
545 dep->name = 0;
546 dep->file = lookup_file (s);
547 if (dep->file == 0)
548 /* enter_file consumes S's storage. */
549 dep->file = enter_file (s);
550 else
551 /* A copy of S is already allocated in DEP->file->name.
552 So we can free S. */
553 free (s);
555 else
557 dep->name = s;
558 dep->file = 0;
559 dep->changed = 0;
561 if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
563 /* If the file actually existed (was not an intermediate file),
564 and the rule that found it was a terminal one, then we want
565 to mark the found file so that it will not have implicit rule
566 search done for it. If we are not entering a `struct file' for
567 it now, we indicate this with the `changed' flag. */
568 if (dep->file == 0)
569 dep->changed = 1;
570 else
571 dep->file->tried_implicit = 1;
573 dep->next = file->deps;
574 file->deps = dep;
577 if (!checked_lastslash[foundrule])
578 /* Always allocate new storage, since STEM might be
579 on the stack for an intermediate file. */
580 file->stem = savestring (stem, stemlen);
581 else
583 /* We want to prepend the directory from
584 the original FILENAME onto the stem. */
585 file->stem = (char *) xmalloc (((lastslash + 1) - filename)
586 + stemlen + 1);
587 bcopy (filename, file->stem, (lastslash + 1) - filename);
588 bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
589 file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
592 file->cmds = rule->cmds;
594 /* Put the targets other than the one that
595 matched into FILE's `also_make' member. */
597 /* If there was only one target, there is nothing to do. */
598 if (rule->targets[1] != 0)
599 for (i = 0; rule->targets[i] != 0; ++i)
600 if (i != matches[foundrule])
602 struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
603 new->name = p = (char *) xmalloc (rule->lens[i] + stemlen + 1);
604 bcopy (rule->targets[i], p,
605 rule->suffixes[i] - rule->targets[i] - 1);
606 p += rule->suffixes[i] - rule->targets[i] - 1;
607 bcopy (stem, p, stemlen);
608 p += stemlen;
609 bcopy (rule->suffixes[i], p,
610 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
611 new->file = enter_file (new->name);
612 new->next = file->also_make;
613 file->also_make = new;
617 return 1;