Mention amiga port
[make.git] / implicit.c
blob2ccf354148ff84eb6a2c2003707139b4ba7114be
1 /* Implicit rule searching for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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;
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 #endif
171 if (lastslash != 0 && lastslash[1] == '\0')
172 lastslash = 0;
175 /* First see which pattern rules match this target
176 and may be considered. Put them in TRYRULES. */
178 nrules = 0;
179 for (rule = pattern_rules; rule != 0; rule = rule->next)
181 /* If the pattern rule has deps but no commands, ignore it.
182 Users cancel built-in rules by redefining them without commands. */
183 if (rule->deps != 0 && rule->cmds == 0)
184 continue;
186 /* If this rule is in use by a parent pattern_search,
187 don't use it here. */
188 if (rule->in_use)
190 DEBUGP2 ("Avoiding implicit rule recursion.%s%s\n", "", "");
191 continue;
194 for (i = 0; rule->targets[i] != 0; ++i)
196 char *target = rule->targets[i];
197 char *suffix = rule->suffixes[i];
198 int check_lastslash;
200 /* Rules that can match any filename and are not terminal
201 are ignored if we're recursing, so that they cannot be
202 intermediate files. */
203 if (recursions > 0 && target[1] == '\0' && !rule->terminal)
204 continue;
206 if (rule->lens[i] > namelen)
207 /* It can't possibly match. */
208 continue;
210 /* From the lengths of the filename and the pattern parts,
211 find the stem: the part of the filename that matches the %. */
212 stem = filename + (suffix - target - 1);
213 stemlen = namelen - rule->lens[i] + 1;
215 /* Set CHECK_LASTSLASH if FILENAME contains a directory
216 prefix and the target pattern does not contain a slash. */
218 #ifdef VMS
219 check_lastslash = lastslash != 0 && index (target, ']') == 0;
220 #else
221 check_lastslash = lastslash != 0 && index (target, '/') == 0;
222 #endif
223 if (check_lastslash)
225 /* In that case, don't include the
226 directory prefix in STEM here. */
227 unsigned int difference = lastslash - filename + 1;
228 if (difference > stemlen)
229 continue;
230 stemlen -= difference;
231 stem += difference;
234 /* Check that the rule pattern matches the text before the stem. */
235 if (check_lastslash)
237 if (stem > (lastslash + 1)
238 && strncmp (target, lastslash + 1, stem - lastslash - 1))
239 continue;
241 else if (stem > filename
242 && strncmp (target, filename, stem - filename))
243 continue;
245 /* Check that the rule pattern matches the text after the stem.
246 We could test simply use streq, but this way we compare the
247 first two characters immediately. This saves time in the very
248 common case where the first character matches because it is a
249 period. */
250 if (*suffix != stem[stemlen]
251 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
252 continue;
254 /* Record if we match a rule that not all filenames will match. */
255 if (target[1] != '\0')
256 specific_rule_matched = 1;
258 /* A rule with no dependencies and no commands exists solely to set
259 specific_rule_matched when it matches. Don't try to use it. */
260 if (rule->deps == 0 && rule->cmds == 0)
261 continue;
263 /* Record this rule in TRYRULES and the index of the matching
264 target in MATCHES. If several targets of the same rule match,
265 that rule will be in TRYRULES more than once. */
266 tryrules[nrules] = rule;
267 matches[nrules] = i;
268 checked_lastslash[nrules] = check_lastslash;
269 ++nrules;
273 /* If we have found a matching rule that won't match all filenames,
274 retroactively reject any non-"terminal" rules that do always match. */
275 if (specific_rule_matched)
276 for (i = 0; i < nrules; ++i)
277 if (!tryrules[i]->terminal)
279 register unsigned int j;
280 for (j = 0; tryrules[i]->targets[j] != 0; ++j)
281 if (tryrules[i]->targets[j][1] == '\0')
282 break;
283 if (tryrules[i]->targets[j] != 0)
284 tryrules[i] = 0;
287 /* Try each rule once without intermediate files, then once with them. */
288 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
290 /* Try each pattern rule till we find one that applies.
291 If it does, copy the names of its dependencies (as substituted)
292 and store them in FOUND_FILES. DEPS_FOUND is the number of them. */
294 for (i = 0; i < nrules; i++)
296 int check_lastslash;
298 rule = tryrules[i];
300 /* RULE is nil when we discover that a rule,
301 already placed in TRYRULES, should not be applied. */
302 if (rule == 0)
303 continue;
305 /* Reject any terminal rules if we're
306 looking to make intermediate files. */
307 if (intermed_ok && rule->terminal)
308 continue;
310 /* Mark this rule as in use so a recursive
311 pattern_search won't try to use it. */
312 rule->in_use = 1;
314 /* From the lengths of the filename and the matching pattern parts,
315 find the stem: the part of the filename that matches the %. */
316 stem = filename
317 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
318 stemlen = namelen - rule->lens[matches[i]] + 1;
319 check_lastslash = checked_lastslash[i];
320 if (check_lastslash)
322 stem += lastslash - filename + 1;
323 stemlen -= (lastslash - filename) + 1;
326 DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
327 (int) stemlen, stem);
329 /* Try each dependency; see if it "exists". */
331 deps_found = 0;
332 for (dep = rule->deps; dep != 0; dep = dep->next)
334 /* If the dependency name has a %, substitute the stem. */
335 p = index (dep_name (dep), '%');
336 if (p != 0)
338 register unsigned int i;
339 if (check_lastslash)
341 /* Copy directory name from the original FILENAME. */
342 i = lastslash - filename + 1;
343 bcopy (filename, depname, i);
345 else
346 i = 0;
347 bcopy (dep_name (dep), depname + i, p - dep_name (dep));
348 i += p - dep_name (dep);
349 bcopy (stem, depname + i, stemlen);
350 i += stemlen;
351 strcpy (depname + i, p + 1);
352 p = depname;
354 else
355 p = dep_name (dep);
357 /* P is now the actual dependency name as substituted. */
359 if (file_impossible_p (p))
361 /* If this dependency has already been ruled
362 "impossible", then the rule fails and don't
363 bother trying it on the second pass either
364 since we know that will fail too. */
365 DEBUGP2 ("Rejecting impossible %s dependency `%s'.\n",
366 p == depname ? "implicit" : "rule", p);
367 tryrules[i] = 0;
368 break;
371 intermediate_files[deps_found] = 0;
373 DEBUGP2 ("Trying %s dependency `%s'.\n",
374 p == depname ? "implicit" : "rule", p);
376 /* The DEP->changed flag says that this dependency resides in a
377 nonexistent directory. So we normally can skip looking for
378 the file. However, if CHECK_LASTSLASH is set, then the
379 dependency file we are actually looking for is in a different
380 directory (the one gotten by prepending FILENAME's directory),
381 so it might actually exist. */
383 if ((!dep->changed || check_lastslash)
384 && (lookup_file (p) != 0 || file_exists_p (p)))
386 found_files[deps_found++] = savestring (p, strlen (p));
387 continue;
389 /* This code, given FILENAME = "lib/foo.o", dependency name
390 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
391 if (vpath_search (&p, (time_t *) 0))
393 DEBUGP2 ("Found dependency as `%s'.%s\n", p, "");
394 found_files[deps_found++] = p;
395 continue;
398 /* We could not find the file in any place we should look.
399 Try to make this dependency as an intermediate file,
400 but only on the second pass. */
402 if (intermed_ok)
404 if (intermediate_file == 0)
405 intermediate_file
406 = (struct file *) alloca (sizeof (struct file));
408 DEBUGP2 ("Looking for a rule with %s file `%s'.\n",
409 "intermediate", p);
411 bzero ((char *) intermediate_file, sizeof (struct file));
412 intermediate_file->name = p;
413 if (pattern_search (intermediate_file, 0, depth + 1,
414 recursions + 1))
416 p = savestring (p, strlen (p));
417 intermediate_patterns[deps_found]
418 = intermediate_file->name;
419 intermediate_file->name = p;
420 intermediate_files[deps_found] = intermediate_file;
421 intermediate_file = 0;
422 /* Allocate an extra copy to go in FOUND_FILES,
423 because every elt of FOUND_FILES is consumed
424 or freed later. */
425 found_files[deps_found] = savestring (p, strlen (p));
426 ++deps_found;
427 continue;
430 /* If we have tried to find P as an intermediate
431 file and failed, mark that name as impossible
432 so we won't go through the search again later. */
433 file_impossible (p);
436 /* A dependency of this rule does not exist.
437 Therefore, this rule fails. */
438 break;
441 /* This rule is no longer `in use' for recursive searches. */
442 rule->in_use = 0;
444 if (dep != 0)
446 /* This pattern rule does not apply.
447 If some of its dependencies succeeded,
448 free the data structure describing them. */
449 while (deps_found-- > 0)
451 register struct file *f = intermediate_files[deps_found];
452 free (found_files[deps_found]);
453 if (f != 0
454 && (f->stem < f->name
455 || f->stem > f->name + strlen (f->name)))
456 free (f->stem);
459 else
460 /* This pattern rule does apply. Stop looking for one. */
461 break;
464 /* If we found an applicable rule without
465 intermediate files, don't try with them. */
466 if (i < nrules)
467 break;
469 rule = 0;
472 /* RULE is nil if the loop went all the way
473 through the list and everything failed. */
474 if (rule == 0)
475 return 0;
477 foundrule = i;
479 /* If we are recursing, store the pattern that matched
480 FILENAME in FILE->name for use in upper levels. */
482 if (recursions > 0)
483 /* Kludge-o-matic */
484 file->name = rule->targets[matches[foundrule]];
486 /* FOUND_FILES lists the dependencies for the rule we found.
487 This includes the intermediate files, if any.
488 Convert them into entries on the deps-chain of FILE. */
490 while (deps_found-- > 0)
492 register char *s;
494 if (intermediate_files[deps_found] != 0)
496 /* If we need to use an intermediate file,
497 make sure it is entered as a target, with the info that was
498 found for it in the recursive pattern_search call.
499 We know that the intermediate file did not already exist as
500 a target; therefore we can assume that the deps and cmds
501 of F below are null before we change them. */
503 struct file *imf = intermediate_files[deps_found];
504 register struct file *f = enter_file (imf->name);
505 f->deps = imf->deps;
506 f->cmds = imf->cmds;
507 f->stem = imf->stem;
508 imf = lookup_file (intermediate_patterns[deps_found]);
509 if (imf != 0 && imf->precious)
510 f->precious = 1;
511 f->intermediate = 1;
512 f->tried_implicit = 1;
513 for (dep = f->deps; dep != 0; dep = dep->next)
515 dep->file = enter_file (dep->name);
516 dep->name = 0;
517 dep->file->tried_implicit |= dep->changed;
519 num_intermediates++;
522 dep = (struct dep *) xmalloc (sizeof (struct dep));
523 s = found_files[deps_found];
524 if (recursions == 0)
526 dep->name = 0;
527 dep->file = lookup_file (s);
528 if (dep->file == 0)
529 /* enter_file consumes S's storage. */
530 dep->file = enter_file (s);
531 else
532 /* A copy of S is already allocated in DEP->file->name.
533 So we can free S. */
534 free (s);
536 else
538 dep->name = s;
539 dep->file = 0;
540 dep->changed = 0;
542 if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
544 /* If the file actually existed (was not an intermediate file),
545 and the rule that found it was a terminal one, then we want
546 to mark the found file so that it will not have implicit rule
547 search done for it. If we are not entering a `struct file' for
548 it now, we indicate this with the `changed' flag. */
549 if (dep->file == 0)
550 dep->changed = 1;
551 else
552 dep->file->tried_implicit = 1;
554 dep->next = file->deps;
555 file->deps = dep;
558 if (!checked_lastslash[foundrule])
559 /* Always allocate new storage, since STEM might be
560 on the stack for an intermediate file. */
561 file->stem = savestring (stem, stemlen);
562 else
564 /* We want to prepend the directory from
565 the original FILENAME onto the stem. */
566 file->stem = (char *) xmalloc (((lastslash + 1) - filename)
567 + stemlen + 1);
568 bcopy (filename, file->stem, (lastslash + 1) - filename);
569 bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
570 file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
573 file->cmds = rule->cmds;
575 /* Put the targets other than the one that
576 matched into FILE's `also_make' member. */
578 /* If there was only one target, there is nothing to do. */
579 if (rule->targets[1] != 0)
580 for (i = 0; rule->targets[i] != 0; ++i)
581 if (i != matches[foundrule])
583 struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
584 new->name = p = (char *) xmalloc (rule->lens[i] + stemlen + 1);
585 bcopy (rule->targets[i], p,
586 rule->suffixes[i] - rule->targets[i] - 1);
587 p += rule->suffixes[i] - rule->targets[i] - 1;
588 bcopy (stem, p, stemlen);
589 p += stemlen;
590 bcopy (rule->suffixes[i], p,
591 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
592 new->file = enter_file (new->name);
593 new->next = file->also_make;
594 file->also_make = new;
598 return 1;