VirtualBox

source: kBuild/trunk/src/gmake/implicit.c@ 200

Last change on this file since 200 was 154, checked in by bird, 20 years ago

This commit was generated by cvs2svn to compensate for changes in r153,
which included commits to RCS files with non-trunk default branches.

  • Property svn:eol-style set to native
File size: 20.0 KB
Line 
1/* Implicit rule searching for GNU Make.
2Copyright (C) 1988,89,90,91,92,93,94,97,2000 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10GNU Make is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Make; see the file COPYING. If not, write to
17the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, MA 02111-1307, USA. */
19
20#include "make.h"
21#include "filedef.h"
22#include "rule.h"
23#include "dep.h"
24#include "debug.h"
25
26static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth,
27 unsigned int recursions));
28
29
30/* For a FILE which has no commands specified, try to figure out some
31 from the implicit pattern rules.
32 Returns 1 if a suitable implicit rule was found,
33 after modifying FILE to contain the appropriate commands and deps,
34 or returns 0 if no implicit rule was found. */
35
36int
37try_implicit_rule (struct file *file, unsigned int depth)
38{
39 DBF (DB_IMPLICIT, _("Looking for an implicit rule for `%s'.\n"));
40
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. */
45
46 if (pattern_search (file, 0, depth, 0))
47 return 1;
48
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))
53 {
54 DBF (DB_IMPLICIT,
55 _("Looking for archive-member implicit rule for `%s'.\n"));
56 if (pattern_search (file, 1, depth, 0))
57 return 1;
58 }
59#endif
60
61 return 0;
62}
63
64
65
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.
69
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.
73
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.
77
78 DEPTH is used for debugging messages. */
79
80static int
81pattern_search (struct file *file, int archive,
82 unsigned int depth, unsigned int recursions)
83{
84 /* Filename we are searching for a rule for. */
85 char *filename = archive ? strchr (file->name, '(') : file->name;
86
87 /* Length of FILENAME. */
88 unsigned int namelen = strlen (filename);
89
90 /* The last slash in FILENAME (or nil if there is none). */
91 char *lastslash;
92
93 /* This is a file-object used as an argument in
94 recursive calls. It never contains any data
95 except during a recursive call. */
96 struct file *intermediate_file = 0;
97
98 /* List of dependencies found recursively. */
99 struct file **intermediate_files
100 = (struct file **) xmalloc (max_pattern_deps * sizeof (struct file *));
101
102 /* List of the patterns used to find intermediate files. */
103 char **intermediate_patterns
104 = (char **) alloca (max_pattern_deps * sizeof (char *));
105
106 /* This buffer records all the dependencies actually found for a rule. */
107 char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
108 /* Remember whether the associated dep has an "ignore_mtime" flag set. */
109 unsigned char *found_files_im = (unsigned char *) alloca (max_pattern_deps * sizeof (unsigned char));
110 /* Number of dep names now in FOUND_FILES. */
111 unsigned int deps_found = 0;
112
113 /* Names of possible dependencies are constructed in this buffer. */
114 register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
115
116 /* The start and length of the stem of FILENAME for the current rule. */
117 register char *stem = 0;
118 register unsigned int stemlen = 0;
119 register unsigned int fullstemlen = 0;
120
121 /* Buffer in which we store all the rules that are possibly applicable. */
122 struct rule **tryrules
123 = (struct rule **) xmalloc (num_pattern_rules * max_pattern_targets
124 * sizeof (struct rule *));
125
126 /* Number of valid elements in TRYRULES. */
127 unsigned int nrules;
128
129 /* The numbers of the rule targets of each rule
130 in TRYRULES that matched the target file. */
131 unsigned int *matches
132 = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
133
134 /* Each element is nonzero if LASTSLASH was used in
135 matching the corresponding element of TRYRULES. */
136 char *checked_lastslash
137 = (char *) alloca (num_pattern_rules * sizeof (char));
138
139 /* The index in TRYRULES of the rule we found. */
140 unsigned int foundrule;
141
142 /* Nonzero if should consider intermediate files as dependencies. */
143 int intermed_ok;
144
145 /* Nonzero if we have matched a pattern-rule target
146 that is not just `%'. */
147 int specific_rule_matched = 0;
148
149 register unsigned int i = 0; /* uninit checks OK */
150 register struct rule *rule;
151 register struct dep *dep;
152
153 char *p, *vp;
154
155#ifndef NO_ARCHIVES
156 if (archive || ar_name (filename))
157 lastslash = 0;
158 else
159#endif
160 {
161 /* Set LASTSLASH to point at the last slash in FILENAME
162 but not counting any slash at the end. (foo/bar/ counts as
163 bar/ in directory foo/, not empty in directory foo/bar/.) */
164#ifdef VMS
165 lastslash = strrchr (filename, ']');
166 if (lastslash == 0)
167 lastslash = strrchr (filename, ':');
168#else
169 lastslash = strrchr (filename, '/');
170#ifdef HAVE_DOS_PATHS
171 /* Handle backslashes (possibly mixed with forward slashes)
172 and the case of "d:file". */
173 {
174 char *bslash = strrchr (filename, '\\');
175 if (lastslash == 0 || bslash > lastslash)
176 lastslash = bslash;
177 if (lastslash == 0 && filename[0] && filename[1] == ':')
178 lastslash = filename + 1;
179 }
180#endif
181#endif
182 if (lastslash != 0 && lastslash[1] == '\0')
183 lastslash = 0;
184 }
185
186 /* First see which pattern rules match this target
187 and may be considered. Put them in TRYRULES. */
188
189 nrules = 0;
190 for (rule = pattern_rules; rule != 0; rule = rule->next)
191 {
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;
196
197 /* If this rule is in use by a parent pattern_search,
198 don't use it here. */
199 if (rule->in_use)
200 {
201 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
202 continue;
203 }
204
205 for (i = 0; rule->targets[i] != 0; ++i)
206 {
207 char *target = rule->targets[i];
208 char *suffix = rule->suffixes[i];
209 int check_lastslash;
210
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;
216
217 if (rule->lens[i] > namelen)
218 /* It can't possibly match. */
219 continue;
220
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;
225
226 /* Set CHECK_LASTSLASH if FILENAME contains a directory
227 prefix and the target pattern does not contain a slash. */
228
229#ifdef VMS
230 check_lastslash = lastslash != 0
231 && ((strchr (target, ']') == 0)
232 && (strchr (target, ':') == 0));
233#else
234 check_lastslash = lastslash != 0 && strchr (target, '/') == 0;
235#endif
236 if (check_lastslash)
237 {
238 /* In that case, don't include the
239 directory prefix in STEM here. */
240 unsigned int difference = lastslash - filename + 1;
241 if (difference > stemlen)
242 continue;
243 stemlen -= difference;
244 stem += difference;
245 }
246
247 /* Check that the rule pattern matches the text before the stem. */
248 if (check_lastslash)
249 {
250 if (stem > (lastslash + 1)
251 && !strneq (target, lastslash + 1, stem - lastslash - 1))
252 continue;
253 }
254 else if (stem > filename
255 && !strneq (target, filename, stem - filename))
256 continue;
257
258 /* Check that the rule pattern matches the text after the stem.
259 We could test simply use streq, but this way we compare the
260 first two characters immediately. This saves time in the very
261 common case where the first character matches because it is a
262 period. */
263 if (*suffix != stem[stemlen]
264 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
265 continue;
266
267 /* Record if we match a rule that not all filenames will match. */
268 if (target[1] != '\0')
269 specific_rule_matched = 1;
270
271 /* A rule with no dependencies and no commands exists solely to set
272 specific_rule_matched when it matches. Don't try to use it. */
273 if (rule->deps == 0 && rule->cmds == 0)
274 continue;
275
276 /* Record this rule in TRYRULES and the index of the matching
277 target in MATCHES. If several targets of the same rule match,
278 that rule will be in TRYRULES more than once. */
279 tryrules[nrules] = rule;
280 matches[nrules] = i;
281 checked_lastslash[nrules] = check_lastslash;
282 ++nrules;
283 }
284 }
285
286 /* If we have found a matching rule that won't match all filenames,
287 retroactively reject any non-"terminal" rules that do always match. */
288 if (specific_rule_matched)
289 for (i = 0; i < nrules; ++i)
290 if (!tryrules[i]->terminal)
291 {
292 register unsigned int j;
293 for (j = 0; tryrules[i]->targets[j] != 0; ++j)
294 if (tryrules[i]->targets[j][1] == '\0')
295 break;
296 if (tryrules[i]->targets[j] != 0)
297 tryrules[i] = 0;
298 }
299
300 /* Try each rule once without intermediate files, then once with them. */
301 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
302 {
303 /* Try each pattern rule till we find one that applies.
304 If it does, copy the names of its dependencies (as substituted)
305 and store them in FOUND_FILES. DEPS_FOUND is the number of them. */
306
307 for (i = 0; i < nrules; i++)
308 {
309 int check_lastslash;
310
311 rule = tryrules[i];
312
313 /* RULE is nil when we discover that a rule,
314 already placed in TRYRULES, should not be applied. */
315 if (rule == 0)
316 continue;
317
318 /* Reject any terminal rules if we're
319 looking to make intermediate files. */
320 if (intermed_ok && rule->terminal)
321 continue;
322
323 /* Mark this rule as in use so a recursive
324 pattern_search won't try to use it. */
325 rule->in_use = 1;
326
327 /* From the lengths of the filename and the matching pattern parts,
328 find the stem: the part of the filename that matches the %. */
329 stem = filename
330 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
331 stemlen = namelen - rule->lens[matches[i]] + 1;
332 check_lastslash = checked_lastslash[i];
333 if (check_lastslash)
334 {
335 stem += lastslash - filename + 1;
336 stemlen -= (lastslash - filename) + 1;
337 }
338
339 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
340 (int) stemlen, stem));
341
342 /* Try each dependency; see if it "exists". */
343
344 deps_found = 0;
345 for (dep = rule->deps; dep != 0; dep = dep->next)
346 {
347 /* If the dependency name has a %, substitute the stem. */
348 p = strchr (dep_name (dep), '%');
349 if (p != 0)
350 {
351 register unsigned int i;
352 if (check_lastslash)
353 {
354 /* Copy directory name from the original FILENAME. */
355 i = lastslash - filename + 1;
356 bcopy (filename, depname, i);
357 }
358 else
359 i = 0;
360 bcopy (dep_name (dep), depname + i, p - dep_name (dep));
361 i += p - dep_name (dep);
362 bcopy (stem, depname + i, stemlen);
363 i += stemlen;
364 strcpy (depname + i, p + 1);
365 p = depname;
366 }
367 else
368 p = dep_name (dep);
369
370 /* P is now the actual dependency name as substituted. */
371
372 if (file_impossible_p (p))
373 {
374 /* If this dependency has already been ruled
375 "impossible", then the rule fails and don't
376 bother trying it on the second pass either
377 since we know that will fail too. */
378 DBS (DB_IMPLICIT,
379 (p == depname
380 ? _("Rejecting impossible implicit prerequisite `%s'.\n")
381 : _("Rejecting impossible rule prerequisite `%s'.\n"),
382 p));
383 tryrules[i] = 0;
384 break;
385 }
386
387 intermediate_files[deps_found] = 0;
388
389 DBS (DB_IMPLICIT,
390 (p == depname
391 ? _("Trying implicit prerequisite `%s'.\n")
392 : _("Trying rule prerequisite `%s'.\n"), p));
393
394 /* The DEP->changed flag says that this dependency resides in a
395 nonexistent directory. So we normally can skip looking for
396 the file. However, if CHECK_LASTSLASH is set, then the
397 dependency file we are actually looking for is in a different
398 directory (the one gotten by prepending FILENAME's directory),
399 so it might actually exist. */
400
401 if (lookup_file (p) != 0
402 || ((!dep->changed || check_lastslash) && file_exists_p (p)))
403 {
404 found_files_im[deps_found] = dep->ignore_mtime;
405 found_files[deps_found++] = xstrdup (p);
406 continue;
407 }
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))
412 {
413 DBS (DB_IMPLICIT,
414 (_("Found prerequisite `%s' as VPATH `%s'\n"), p, vp));
415 strcpy (vp, p);
416 found_files_im[deps_found] = dep->ignore_mtime;
417 found_files[deps_found++] = vp;
418 continue;
419 }
420
421 /* We could not find the file in any place we should look.
422 Try to make this dependency as an intermediate file,
423 but only on the second pass. */
424
425 if (intermed_ok)
426 {
427 if (intermediate_file == 0)
428 intermediate_file
429 = (struct file *) alloca (sizeof (struct file));
430
431 DBS (DB_IMPLICIT,
432 (_("Looking for a rule with intermediate file `%s'.\n"),
433 p));
434
435 bzero ((char *) intermediate_file, sizeof (struct file));
436 intermediate_file->name = p;
437 if (pattern_search (intermediate_file, 0, depth + 1,
438 recursions + 1))
439 {
440 p = xstrdup (p);
441 intermediate_patterns[deps_found]
442 = intermediate_file->name;
443 intermediate_file->name = p;
444 intermediate_files[deps_found] = intermediate_file;
445 intermediate_file = 0;
446 found_files_im[deps_found] = dep->ignore_mtime;
447 /* Allocate an extra copy to go in FOUND_FILES,
448 because every elt of FOUND_FILES is consumed
449 or freed later. */
450 found_files[deps_found++] = xstrdup (p);
451 continue;
452 }
453
454 /* If we have tried to find P as an intermediate
455 file and failed, mark that name as impossible
456 so we won't go through the search again later. */
457 file_impossible (p);
458 }
459
460 /* A dependency of this rule does not exist.
461 Therefore, this rule fails. */
462 break;
463 }
464
465 /* This rule is no longer `in use' for recursive searches. */
466 rule->in_use = 0;
467
468 if (dep != 0)
469 {
470 /* This pattern rule does not apply.
471 If some of its dependencies succeeded,
472 free the data structure describing them. */
473 while (deps_found-- > 0)
474 {
475 register struct file *f = intermediate_files[deps_found];
476 free (found_files[deps_found]);
477 if (f != 0
478 && (f->stem < f->name
479 || f->stem > f->name + strlen (f->name)))
480 free (f->stem);
481 }
482 }
483 else
484 /* This pattern rule does apply. Stop looking for one. */
485 break;
486 }
487
488 /* If we found an applicable rule without
489 intermediate files, don't try with them. */
490 if (i < nrules)
491 break;
492
493 rule = 0;
494 }
495
496 /* RULE is nil if the loop went all the way
497 through the list and everything failed. */
498 if (rule == 0)
499 goto done;
500
501 foundrule = i;
502
503 /* If we are recursing, store the pattern that matched
504 FILENAME in FILE->name for use in upper levels. */
505
506 if (recursions > 0)
507 /* Kludge-o-matic */
508 file->name = rule->targets[matches[foundrule]];
509
510 /* FOUND_FILES lists the dependencies for the rule we found.
511 This includes the intermediate files, if any.
512 Convert them into entries on the deps-chain of FILE. */
513
514 while (deps_found-- > 0)
515 {
516 register char *s;
517
518 if (intermediate_files[deps_found] != 0)
519 {
520 /* If we need to use an intermediate file,
521 make sure it is entered as a target, with the info that was
522 found for it in the recursive pattern_search call.
523 We know that the intermediate file did not already exist as
524 a target; therefore we can assume that the deps and cmds
525 of F below are null before we change them. */
526
527 struct file *imf = intermediate_files[deps_found];
528 register struct file *f = enter_file (imf->name);
529 f->deps = imf->deps;
530 f->cmds = imf->cmds;
531 f->stem = imf->stem;
532 f->also_make = imf->also_make;
533 imf = lookup_file (intermediate_patterns[deps_found]);
534 if (imf != 0 && imf->precious)
535 f->precious = 1;
536 f->intermediate = 1;
537 f->tried_implicit = 1;
538 for (dep = f->deps; dep != 0; dep = dep->next)
539 {
540 dep->file = enter_file (dep->name);
541 /* enter_file uses dep->name _if_ we created a new file. */
542 if (dep->name != dep->file->name)
543 free (dep->name);
544 dep->name = 0;
545 dep->file->tried_implicit |= dep->changed;
546 }
547 }
548
549 dep = (struct dep *) xmalloc (sizeof (struct dep));
550 dep->ignore_mtime = found_files_im[deps_found];
551 s = found_files[deps_found];
552 if (recursions == 0)
553 {
554 dep->name = 0;
555 dep->file = lookup_file (s);
556 if (dep->file == 0)
557 /* enter_file consumes S's storage. */
558 dep->file = enter_file (s);
559 else
560 /* A copy of S is already allocated in DEP->file->name.
561 So we can free S. */
562 free (s);
563 }
564 else
565 {
566 dep->name = s;
567 dep->file = 0;
568 dep->changed = 0;
569 }
570 if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
571 {
572 /* If the file actually existed (was not an intermediate file),
573 and the rule that found it was a terminal one, then we want
574 to mark the found file so that it will not have implicit rule
575 search done for it. If we are not entering a `struct file' for
576 it now, we indicate this with the `changed' flag. */
577 if (dep->file == 0)
578 dep->changed = 1;
579 else
580 dep->file->tried_implicit = 1;
581 }
582 dep->next = file->deps;
583 file->deps = dep;
584 }
585
586 if (!checked_lastslash[foundrule])
587 {
588 /* Always allocate new storage, since STEM might be
589 on the stack for an intermediate file. */
590 file->stem = savestring (stem, stemlen);
591 fullstemlen = stemlen;
592 }
593 else
594 {
595 int dirlen = (lastslash + 1) - filename;
596
597 /* We want to prepend the directory from
598 the original FILENAME onto the stem. */
599 fullstemlen = dirlen + stemlen;
600 file->stem = (char *) xmalloc (fullstemlen + 1);
601 bcopy (filename, file->stem, dirlen);
602 bcopy (stem, file->stem + dirlen, stemlen);
603 file->stem[fullstemlen] = '\0';
604 }
605
606 file->cmds = rule->cmds;
607
608 /* If this rule builds other targets, too, put the others into FILE's
609 `also_make' member. */
610
611 if (rule->targets[1] != 0)
612 for (i = 0; rule->targets[i] != 0; ++i)
613 if (i != matches[foundrule])
614 {
615 struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
616 /* GKM FIMXE: handle '|' here too */
617 new->ignore_mtime = 0;
618 new->name = p = (char *) xmalloc (rule->lens[i] + fullstemlen + 1);
619 bcopy (rule->targets[i], p,
620 rule->suffixes[i] - rule->targets[i] - 1);
621 p += rule->suffixes[i] - rule->targets[i] - 1;
622 bcopy (file->stem, p, fullstemlen);
623 p += fullstemlen;
624 bcopy (rule->suffixes[i], p,
625 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
626 new->file = enter_file (new->name);
627 new->next = file->also_make;
628 file->also_make = new;
629 }
630
631 done:
632 free (intermediate_files);
633 free (tryrules);
634
635 return rule != 0;
636}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette