1 | /* Implicit rule searching for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
|
---|
4 | Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
10 | version.
|
---|
11 |
|
---|
12 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License along with
|
---|
17 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "make.h"
|
---|
20 | #include "filedef.h"
|
---|
21 | #include "rule.h"
|
---|
22 | #include "dep.h"
|
---|
23 | #include "debug.h"
|
---|
24 | #include "variable.h"
|
---|
25 | #include "job.h" /* struct child, used inside commands.h */
|
---|
26 | #include "commands.h" /* set_file_variables */
|
---|
27 |
|
---|
28 | static int pattern_search (struct file *file, int archive,
|
---|
29 | unsigned int depth, unsigned int recursions);
|
---|
30 | |
---|
31 |
|
---|
32 | /* For a FILE which has no commands specified, try to figure out some
|
---|
33 | from the implicit pattern rules.
|
---|
34 | Returns 1 if a suitable implicit rule was found,
|
---|
35 | after modifying FILE to contain the appropriate commands and deps,
|
---|
36 | or returns 0 if no implicit rule was found. */
|
---|
37 |
|
---|
38 | int
|
---|
39 | try_implicit_rule (struct file *file, unsigned int depth)
|
---|
40 | {
|
---|
41 | DBF (DB_IMPLICIT, _("Looking for an implicit rule for `%s'.\n"));
|
---|
42 |
|
---|
43 | /* The order of these searches was previously reversed. My logic now is
|
---|
44 | that since the non-archive search uses more information in the target
|
---|
45 | (the archive search omits the archive name), it is more specific and
|
---|
46 | should come first. */
|
---|
47 |
|
---|
48 | if (pattern_search (file, 0, depth, 0))
|
---|
49 | return 1;
|
---|
50 |
|
---|
51 | #ifndef NO_ARCHIVES
|
---|
52 | /* If this is an archive member reference, use just the
|
---|
53 | archive member name to search for implicit rules. */
|
---|
54 | if (ar_name (file->name))
|
---|
55 | {
|
---|
56 | DBF (DB_IMPLICIT,
|
---|
57 | _("Looking for archive-member implicit rule for `%s'.\n"));
|
---|
58 | if (pattern_search (file, 1, depth, 0))
|
---|
59 | return 1;
|
---|
60 | }
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | return 0;
|
---|
64 | }
|
---|
65 | |
---|
66 |
|
---|
67 |
|
---|
68 | #ifdef CONFIG_WITH_ALLOC_CACHES
|
---|
69 | struct alloccache idep_cache;
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | /* Struct idep captures information about implicit prerequisites
|
---|
73 | that come from implicit rules. */
|
---|
74 | struct idep
|
---|
75 | {
|
---|
76 | struct idep *next; /* struct dep -compatible interface */
|
---|
77 | const char *name; /* name of the prerequisite */
|
---|
78 | struct file *intermediate_file; /* intermediate file, 0 otherwise */
|
---|
79 | const char *intermediate_pattern; /* pattern for intermediate file */
|
---|
80 | unsigned char had_stem; /* had % substituted with stem */
|
---|
81 | unsigned char ignore_mtime; /* ignore_mtime flag */
|
---|
82 | };
|
---|
83 |
|
---|
84 | static void
|
---|
85 | free_idep_chain (struct idep *p)
|
---|
86 | {
|
---|
87 | struct idep *n;
|
---|
88 |
|
---|
89 | for (; p != 0; p = n)
|
---|
90 | {
|
---|
91 | n = p->next;
|
---|
92 | #ifndef CONFIG_WITH_ALLOC_CACHES
|
---|
93 | free (p);
|
---|
94 | #else
|
---|
95 | alloccache_free (&idep_cache, p);
|
---|
96 | #endif
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | /* Scans the BUFFER for the next word with whitespace as a separator.
|
---|
102 | Returns the pointer to the beginning of the word. LENGTH hold the
|
---|
103 | length of the word. */
|
---|
104 |
|
---|
105 | static char *
|
---|
106 | get_next_word (const char *buffer, unsigned int *length)
|
---|
107 | {
|
---|
108 | const char *p = buffer, *beg;
|
---|
109 | char c;
|
---|
110 |
|
---|
111 | /* Skip any leading whitespace. */
|
---|
112 | while (isblank ((unsigned char)*p))
|
---|
113 | ++p;
|
---|
114 |
|
---|
115 | beg = p;
|
---|
116 | c = *(p++);
|
---|
117 |
|
---|
118 | if (c == '\0')
|
---|
119 | {
|
---|
120 | if (length) /* bird: shut up gcc. */
|
---|
121 | *length = 0;
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /* We already found the first value of "c", above. */
|
---|
127 | while (1)
|
---|
128 | {
|
---|
129 | char closeparen;
|
---|
130 | int count;
|
---|
131 |
|
---|
132 | switch (c)
|
---|
133 | {
|
---|
134 | case '\0':
|
---|
135 | case ' ':
|
---|
136 | case '\t':
|
---|
137 | goto done_word;
|
---|
138 |
|
---|
139 | case '$':
|
---|
140 | c = *(p++);
|
---|
141 | if (c == '$')
|
---|
142 | break;
|
---|
143 |
|
---|
144 | /* This is a variable reference, so read it to the matching
|
---|
145 | close paren. */
|
---|
146 |
|
---|
147 | if (c == '(')
|
---|
148 | closeparen = ')';
|
---|
149 | else if (c == '{')
|
---|
150 | closeparen = '}';
|
---|
151 | else
|
---|
152 | /* This is a single-letter variable reference. */
|
---|
153 | break;
|
---|
154 |
|
---|
155 | for (count = 0; *p != '\0'; ++p)
|
---|
156 | {
|
---|
157 | if (*p == c)
|
---|
158 | ++count;
|
---|
159 | else if (*p == closeparen && --count < 0)
|
---|
160 | {
|
---|
161 | ++p;
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | break;
|
---|
166 |
|
---|
167 | case '|':
|
---|
168 | goto done;
|
---|
169 |
|
---|
170 | default:
|
---|
171 | break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | c = *(p++);
|
---|
175 | }
|
---|
176 | done_word:
|
---|
177 | --p;
|
---|
178 |
|
---|
179 | done:
|
---|
180 | if (length)
|
---|
181 | *length = p - beg;
|
---|
182 |
|
---|
183 | return (char *)beg;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Search the pattern rules for a rule with an existing dependency to make
|
---|
187 | FILE. If a rule is found, the appropriate commands and deps are put in FILE
|
---|
188 | and 1 is returned. If not, 0 is returned.
|
---|
189 |
|
---|
190 | If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
|
---|
191 | "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
|
---|
192 | directory and filename parts.
|
---|
193 |
|
---|
194 | If an intermediate file is found by pattern search, the intermediate file
|
---|
195 | is set up as a target by the recursive call and is also made a dependency
|
---|
196 | of FILE.
|
---|
197 |
|
---|
198 | DEPTH is used for debugging messages. */
|
---|
199 |
|
---|
200 | static int
|
---|
201 | pattern_search (struct file *file, int archive,
|
---|
202 | unsigned int depth, unsigned int recursions)
|
---|
203 | {
|
---|
204 | /* Filename we are searching for a rule for. */
|
---|
205 | const char *filename = archive ? strchr (file->name, '(') : file->name;
|
---|
206 |
|
---|
207 | /* Length of FILENAME. */
|
---|
208 | unsigned int namelen = strlen (filename);
|
---|
209 |
|
---|
210 | /* The last slash in FILENAME (or nil if there is none). */
|
---|
211 | char *lastslash;
|
---|
212 |
|
---|
213 | /* This is a file-object used as an argument in
|
---|
214 | recursive calls. It never contains any data
|
---|
215 | except during a recursive call. */
|
---|
216 | struct file *intermediate_file = 0;
|
---|
217 |
|
---|
218 | /* This linked list records all the prerequisites actually
|
---|
219 | found for a rule along with some other useful information
|
---|
220 | (see struct idep for details). */
|
---|
221 | struct idep* deps = 0;
|
---|
222 |
|
---|
223 | /* 1 if we need to remove explicit prerequisites, 0 otherwise. */
|
---|
224 | unsigned int remove_explicit_deps = 0;
|
---|
225 |
|
---|
226 | /* Names of possible dependencies are constructed in this buffer. */
|
---|
227 | char *depname = alloca (namelen + max_pattern_dep_length);
|
---|
228 |
|
---|
229 | /* The start and length of the stem of FILENAME for the current rule. */
|
---|
230 | const char *stem = 0;
|
---|
231 | unsigned int stemlen = 0;
|
---|
232 | unsigned int fullstemlen = 0;
|
---|
233 |
|
---|
234 | /* Buffer in which we store all the rules that are possibly applicable. */
|
---|
235 | struct rule **tryrules = xmalloc (num_pattern_rules * max_pattern_targets
|
---|
236 | * sizeof (struct rule *));
|
---|
237 |
|
---|
238 | /* Number of valid elements in TRYRULES. */
|
---|
239 | unsigned int nrules;
|
---|
240 |
|
---|
241 | /* The numbers of the rule targets of each rule
|
---|
242 | in TRYRULES that matched the target file. */
|
---|
243 | unsigned int *matches = alloca (num_pattern_rules * sizeof (unsigned int));
|
---|
244 |
|
---|
245 | /* Each element is nonzero if LASTSLASH was used in
|
---|
246 | matching the corresponding element of TRYRULES. */
|
---|
247 | char *checked_lastslash = alloca (num_pattern_rules * sizeof (char));
|
---|
248 |
|
---|
249 | /* The index in TRYRULES of the rule we found. */
|
---|
250 | unsigned int foundrule;
|
---|
251 |
|
---|
252 | /* Nonzero if should consider intermediate files as dependencies. */
|
---|
253 | int intermed_ok;
|
---|
254 |
|
---|
255 | /* Nonzero if we have matched a pattern-rule target
|
---|
256 | that is not just `%'. */
|
---|
257 | int specific_rule_matched = 0;
|
---|
258 |
|
---|
259 | unsigned int ri; /* uninit checks OK */
|
---|
260 | struct rule *rule;
|
---|
261 | struct dep *dep, *expl_d;
|
---|
262 |
|
---|
263 | struct idep *d;
|
---|
264 | struct idep **id_ptr;
|
---|
265 | struct dep **d_ptr;
|
---|
266 |
|
---|
267 | PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
|
---|
268 |
|
---|
269 | #ifdef CONFIG_WITH_ALLOC_CACHES
|
---|
270 | if (!idep_cache.size)
|
---|
271 | alloccache_init (&idep_cache, sizeof (struct idep), "idep", NULL, NULL);
|
---|
272 | #endif
|
---|
273 |
|
---|
274 | #ifndef NO_ARCHIVES
|
---|
275 | if (archive || ar_name (filename))
|
---|
276 | lastslash = 0;
|
---|
277 | else
|
---|
278 | #endif
|
---|
279 | {
|
---|
280 | /* Set LASTSLASH to point at the last slash in FILENAME
|
---|
281 | but not counting any slash at the end. (foo/bar/ counts as
|
---|
282 | bar/ in directory foo/, not empty in directory foo/bar/.) */
|
---|
283 | #ifdef VMS
|
---|
284 | lastslash = strrchr (filename, ']');
|
---|
285 | if (lastslash == 0)
|
---|
286 | lastslash = strrchr (filename, ':');
|
---|
287 | #else
|
---|
288 | lastslash = strrchr (filename, '/');
|
---|
289 | #ifdef HAVE_DOS_PATHS
|
---|
290 | /* Handle backslashes (possibly mixed with forward slashes)
|
---|
291 | and the case of "d:file". */
|
---|
292 | {
|
---|
293 | char *bslash = strrchr (filename, '\\');
|
---|
294 | if (lastslash == 0 || bslash > lastslash)
|
---|
295 | lastslash = bslash;
|
---|
296 | if (lastslash == 0 && filename[0] && filename[1] == ':')
|
---|
297 | lastslash = (char *)filename + 1;
|
---|
298 | }
|
---|
299 | #endif
|
---|
300 | #endif
|
---|
301 | if (lastslash != 0 && lastslash[1] == '\0')
|
---|
302 | lastslash = 0;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /* First see which pattern rules match this target
|
---|
306 | and may be considered. Put them in TRYRULES. */
|
---|
307 |
|
---|
308 | nrules = 0;
|
---|
309 | for (rule = pattern_rules; rule != 0; rule = rule->next)
|
---|
310 | {
|
---|
311 | unsigned int ti;
|
---|
312 |
|
---|
313 | /* If the pattern rule has deps but no commands, ignore it.
|
---|
314 | Users cancel built-in rules by redefining them without commands. */
|
---|
315 | if (rule->deps != 0 && rule->cmds == 0)
|
---|
316 | continue;
|
---|
317 |
|
---|
318 | /* If this rule is in use by a parent pattern_search,
|
---|
319 | don't use it here. */
|
---|
320 | if (rule->in_use)
|
---|
321 | {
|
---|
322 | DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
|
---|
323 | continue;
|
---|
324 | }
|
---|
325 |
|
---|
326 | for (ti = 0; ti < rule->num; ++ti)
|
---|
327 | {
|
---|
328 | const char *target = rule->targets[ti];
|
---|
329 | const char *suffix = rule->suffixes[ti];
|
---|
330 | int check_lastslash;
|
---|
331 |
|
---|
332 | /* Rules that can match any filename and are not terminal
|
---|
333 | are ignored if we're recursing, so that they cannot be
|
---|
334 | intermediate files. */
|
---|
335 | if (recursions > 0 && target[1] == '\0' && !rule->terminal)
|
---|
336 | continue;
|
---|
337 |
|
---|
338 | if (rule->lens[ti] > namelen)
|
---|
339 | /* It can't possibly match. */
|
---|
340 | continue;
|
---|
341 |
|
---|
342 | /* From the lengths of the filename and the pattern parts,
|
---|
343 | find the stem: the part of the filename that matches the %. */
|
---|
344 | stem = filename + (suffix - target - 1);
|
---|
345 | stemlen = namelen - rule->lens[ti] + 1;
|
---|
346 |
|
---|
347 | /* Set CHECK_LASTSLASH if FILENAME contains a directory
|
---|
348 | prefix and the target pattern does not contain a slash. */
|
---|
349 |
|
---|
350 | check_lastslash = 0;
|
---|
351 | if (lastslash)
|
---|
352 | {
|
---|
353 | #ifdef VMS
|
---|
354 | check_lastslash = (strchr (target, ']') == 0
|
---|
355 | && strchr (target, ':') == 0);
|
---|
356 | #else
|
---|
357 | check_lastslash = strchr (target, '/') == 0;
|
---|
358 | #ifdef HAVE_DOS_PATHS
|
---|
359 | /* Didn't find it yet: check for DOS-type directories. */
|
---|
360 | if (check_lastslash)
|
---|
361 | {
|
---|
362 | char *b = strchr (target, '\\');
|
---|
363 | check_lastslash = !(b || (target[0] && target[1] == ':'));
|
---|
364 | }
|
---|
365 | #endif
|
---|
366 | #endif
|
---|
367 | }
|
---|
368 | if (check_lastslash)
|
---|
369 | {
|
---|
370 | /* If so, don't include the directory prefix in STEM here. */
|
---|
371 | unsigned int difference = lastslash - filename + 1;
|
---|
372 | if (difference > stemlen)
|
---|
373 | continue;
|
---|
374 | stemlen -= difference;
|
---|
375 | stem += difference;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* Check that the rule pattern matches the text before the stem. */
|
---|
379 | if (check_lastslash)
|
---|
380 | {
|
---|
381 | if (stem > (lastslash + 1)
|
---|
382 | && !strneq (target, lastslash + 1, stem - lastslash - 1))
|
---|
383 | continue;
|
---|
384 | }
|
---|
385 | else if (stem > filename
|
---|
386 | && !strneq (target, filename, stem - filename))
|
---|
387 | continue;
|
---|
388 |
|
---|
389 | /* Check that the rule pattern matches the text after the stem.
|
---|
390 | We could test simply use streq, but this way we compare the
|
---|
391 | first two characters immediately. This saves time in the very
|
---|
392 | common case where the first character matches because it is a
|
---|
393 | period. */
|
---|
394 | if (*suffix != stem[stemlen]
|
---|
395 | || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
|
---|
396 | continue;
|
---|
397 |
|
---|
398 | /* Record if we match a rule that not all filenames will match. */
|
---|
399 | if (target[1] != '\0')
|
---|
400 | specific_rule_matched = 1;
|
---|
401 |
|
---|
402 | /* A rule with no dependencies and no commands exists solely to set
|
---|
403 | specific_rule_matched when it matches. Don't try to use it. */
|
---|
404 | if (rule->deps == 0 && rule->cmds == 0)
|
---|
405 | continue;
|
---|
406 |
|
---|
407 | /* Record this rule in TRYRULES and the index of the matching
|
---|
408 | target in MATCHES. If several targets of the same rule match,
|
---|
409 | that rule will be in TRYRULES more than once. */
|
---|
410 | tryrules[nrules] = rule;
|
---|
411 | matches[nrules] = ti;
|
---|
412 | checked_lastslash[nrules] = check_lastslash;
|
---|
413 | ++nrules;
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | /* If we have found a matching rule that won't match all filenames,
|
---|
418 | retroactively reject any non-"terminal" rules that do always match. */
|
---|
419 | if (specific_rule_matched)
|
---|
420 | for (ri = 0; ri < nrules; ++ri)
|
---|
421 | if (!tryrules[ri]->terminal)
|
---|
422 | {
|
---|
423 | unsigned int j;
|
---|
424 | for (j = 0; j < tryrules[ri]->num; ++j)
|
---|
425 | if (tryrules[ri]->targets[j][1] == '\0')
|
---|
426 | {
|
---|
427 | tryrules[ri] = 0;
|
---|
428 | break;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | /* We are going to do second expansion so initialize file variables
|
---|
433 | for the rule. */
|
---|
434 | initialize_file_variables (file, 0);
|
---|
435 |
|
---|
436 | /* Try each rule once without intermediate files, then once with them. */
|
---|
437 | for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
|
---|
438 | {
|
---|
439 | /* Try each pattern rule till we find one that applies.
|
---|
440 | If it does, expand its dependencies (as substituted)
|
---|
441 | and chain them in DEPS. */
|
---|
442 |
|
---|
443 | for (ri = 0; ri < nrules; ri++)
|
---|
444 | {
|
---|
445 | struct file *f;
|
---|
446 | unsigned int failed = 0;
|
---|
447 | int check_lastslash;
|
---|
448 | int file_variables_set = 0;
|
---|
449 |
|
---|
450 | rule = tryrules[ri];
|
---|
451 |
|
---|
452 | remove_explicit_deps = 0;
|
---|
453 |
|
---|
454 | /* RULE is nil when we discover that a rule,
|
---|
455 | already placed in TRYRULES, should not be applied. */
|
---|
456 | if (rule == 0)
|
---|
457 | continue;
|
---|
458 |
|
---|
459 | /* Reject any terminal rules if we're
|
---|
460 | looking to make intermediate files. */
|
---|
461 | if (intermed_ok && rule->terminal)
|
---|
462 | continue;
|
---|
463 |
|
---|
464 | /* Mark this rule as in use so a recursive
|
---|
465 | pattern_search won't try to use it. */
|
---|
466 | rule->in_use = 1;
|
---|
467 |
|
---|
468 | /* From the lengths of the filename and the matching pattern parts,
|
---|
469 | find the stem: the part of the filename that matches the %. */
|
---|
470 | stem = filename
|
---|
471 | + (rule->suffixes[matches[ri]] - rule->targets[matches[ri]]) - 1;
|
---|
472 | stemlen = namelen - rule->lens[matches[ri]] + 1;
|
---|
473 | check_lastslash = checked_lastslash[ri];
|
---|
474 | if (check_lastslash)
|
---|
475 | {
|
---|
476 | stem += lastslash - filename + 1;
|
---|
477 | stemlen -= (lastslash - filename) + 1;
|
---|
478 | }
|
---|
479 |
|
---|
480 | DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
|
---|
481 | (int) stemlen, stem));
|
---|
482 |
|
---|
483 | strncpy (stem_str, stem, stemlen);
|
---|
484 | stem_str[stemlen] = '\0';
|
---|
485 |
|
---|
486 | /* Temporary assign STEM to file->stem (needed to set file
|
---|
487 | variables below). */
|
---|
488 | file->stem = stem_str;
|
---|
489 |
|
---|
490 | /* Try each dependency; see if it "exists". */
|
---|
491 |
|
---|
492 | for (dep = rule->deps; dep != 0; dep = dep->next)
|
---|
493 | {
|
---|
494 | unsigned int len;
|
---|
495 | char *p;
|
---|
496 | char *p2;
|
---|
497 | unsigned int order_only = 0; /* Set if '|' was seen. */
|
---|
498 |
|
---|
499 | /* In an ideal world we would take the dependency line,
|
---|
500 | substitute the stem, re-expand the whole line and chop it
|
---|
501 | into individual prerequisites. Unfortunately this won't work
|
---|
502 | because of the "check_lastslash" twist. Instead, we will
|
---|
503 | have to go word by word, taking $()'s into account, for each
|
---|
504 | word we will substitute the stem, re-expand, chop it up, and,
|
---|
505 | if check_lastslash != 0, add the directory part to each
|
---|
506 | resulting prerequisite. */
|
---|
507 |
|
---|
508 | p = get_next_word (dep->name, &len);
|
---|
509 |
|
---|
510 | while (1)
|
---|
511 | {
|
---|
512 | int add_dir = 0;
|
---|
513 | int had_stem = 0;
|
---|
514 |
|
---|
515 | if (p == 0)
|
---|
516 | break; /* No more words */
|
---|
517 |
|
---|
518 | /* Is there a pattern in this prerequisite? */
|
---|
519 |
|
---|
520 | for (p2 = p; p2 < p + len && *p2 != '%'; ++p2)
|
---|
521 | ;
|
---|
522 |
|
---|
523 | if (dep->need_2nd_expansion)
|
---|
524 | {
|
---|
525 | /* If the dependency name has %, substitute the stem.
|
---|
526 |
|
---|
527 | Watch out, we are going to do something tricky
|
---|
528 | here. If we just replace % with the stem value,
|
---|
529 | later, when we do the second expansion, we will
|
---|
530 | re-expand this stem value once again. This is not
|
---|
531 | good especially if you have certain characters in
|
---|
532 | your stem (like $).
|
---|
533 |
|
---|
534 | Instead, we will replace % with $* and allow the
|
---|
535 | second expansion to take care of it for us. This way
|
---|
536 | (since $* is a simple variable) there won't be
|
---|
537 | additional re-expansion of the stem. */
|
---|
538 |
|
---|
539 | if (p2 < p + len)
|
---|
540 | {
|
---|
541 | unsigned int i = p2 - p;
|
---|
542 | memcpy (depname, p, i);
|
---|
543 | memcpy (depname + i, "$*", 2);
|
---|
544 | memcpy (depname + i + 2, p2 + 1, len - i - 1);
|
---|
545 | depname[len + 2 - 1] = '\0';
|
---|
546 |
|
---|
547 | if (check_lastslash)
|
---|
548 | add_dir = 1;
|
---|
549 |
|
---|
550 | had_stem = 1;
|
---|
551 | }
|
---|
552 | else
|
---|
553 | {
|
---|
554 | memcpy (depname, p, len);
|
---|
555 | depname[len] = '\0';
|
---|
556 | }
|
---|
557 |
|
---|
558 | /* Set file variables. Note that we cannot do it once
|
---|
559 | at the beginning of the function because of the stem
|
---|
560 | value. */
|
---|
561 | if (!file_variables_set)
|
---|
562 | {
|
---|
563 | #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
|
---|
564 | set_file_variables (file, 0 /* real call */);
|
---|
565 | #else
|
---|
566 | set_file_variables (file);
|
---|
567 | #endif
|
---|
568 | file_variables_set = 1;
|
---|
569 | }
|
---|
570 |
|
---|
571 | p2 = variable_expand_for_file (depname, file);
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | if (p2 < p + len)
|
---|
576 | {
|
---|
577 | unsigned int i = p2 - p;
|
---|
578 | memcpy (depname, p, i);
|
---|
579 | memcpy (depname + i, stem_str, stemlen);
|
---|
580 | memcpy (depname + i + stemlen, p2 + 1, len - i - 1);
|
---|
581 | depname[len + stemlen - 1] = '\0';
|
---|
582 |
|
---|
583 | if (check_lastslash)
|
---|
584 | add_dir = 1;
|
---|
585 |
|
---|
586 | had_stem = 1;
|
---|
587 | }
|
---|
588 | else
|
---|
589 | {
|
---|
590 | memcpy (depname, p, len);
|
---|
591 | depname[len] = '\0';
|
---|
592 | }
|
---|
593 |
|
---|
594 | p2 = depname;
|
---|
595 | }
|
---|
596 |
|
---|
597 | /* Parse the dependencies. */
|
---|
598 |
|
---|
599 | while (1)
|
---|
600 | {
|
---|
601 | id_ptr = &deps;
|
---|
602 |
|
---|
603 | for (; *id_ptr; id_ptr = &(*id_ptr)->next)
|
---|
604 | ;
|
---|
605 |
|
---|
606 | #ifndef CONFIG_WITH_ALLOC_CACHES
|
---|
607 | *id_ptr = (struct idep *)
|
---|
608 | multi_glob (
|
---|
609 | parse_file_seq (&p2,
|
---|
610 | order_only ? '\0' : '|',
|
---|
611 | sizeof (struct idep),
|
---|
612 | 1), sizeof (struct idep));
|
---|
613 | #else
|
---|
614 | *id_ptr = (struct idep *)
|
---|
615 | multi_glob (
|
---|
616 | parse_file_seq (&p2,
|
---|
617 | order_only ? '\0' : '|',
|
---|
618 | &idep_cache, 1),
|
---|
619 | &idep_cache);
|
---|
620 | #endif
|
---|
621 |
|
---|
622 | /* @@ It would be nice to teach parse_file_seq or
|
---|
623 | multi_glob to add prefix. This would save us some
|
---|
624 | reallocations. */
|
---|
625 |
|
---|
626 | if (order_only || add_dir || had_stem)
|
---|
627 | {
|
---|
628 | unsigned long l = lastslash - filename + 1;
|
---|
629 |
|
---|
630 | for (d = *id_ptr; d != 0; d = d->next)
|
---|
631 | {
|
---|
632 | if (order_only)
|
---|
633 | d->ignore_mtime = 1;
|
---|
634 |
|
---|
635 | if (add_dir)
|
---|
636 | {
|
---|
637 | char *n = alloca (strlen (d->name) + l + 1);
|
---|
638 | memcpy (n, filename, l);
|
---|
639 | memcpy (n+l, d->name, strlen (d->name) + 1);
|
---|
640 | d->name = strcache_add (n);
|
---|
641 | }
|
---|
642 |
|
---|
643 | if (had_stem)
|
---|
644 | d->had_stem = 1;
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 | if (!order_only && *p2)
|
---|
649 | {
|
---|
650 | ++p2;
|
---|
651 | order_only = 1;
|
---|
652 | continue;
|
---|
653 | }
|
---|
654 |
|
---|
655 | break;
|
---|
656 | }
|
---|
657 |
|
---|
658 | p += len;
|
---|
659 | p = get_next_word (p, &len);
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 | /* Reset the stem in FILE. */
|
---|
664 |
|
---|
665 | file->stem = 0;
|
---|
666 |
|
---|
667 | /* @@ This loop can be combined with the previous one. I do
|
---|
668 | it separately for now for transparency.*/
|
---|
669 |
|
---|
670 | for (d = deps; d != 0; d = d->next)
|
---|
671 | {
|
---|
672 | const char *name = d->name;
|
---|
673 |
|
---|
674 | if (file_impossible_p (name))
|
---|
675 | {
|
---|
676 | /* If this dependency has already been ruled "impossible",
|
---|
677 | then the rule fails and don't bother trying it on the
|
---|
678 | second pass either since we know that will fail too. */
|
---|
679 | DBS (DB_IMPLICIT,
|
---|
680 | (d->had_stem
|
---|
681 | ? _("Rejecting impossible implicit prerequisite `%s'.\n")
|
---|
682 | : _("Rejecting impossible rule prerequisite `%s'.\n"),
|
---|
683 | name));
|
---|
684 | tryrules[ri] = 0;
|
---|
685 |
|
---|
686 | failed = 1;
|
---|
687 | break;
|
---|
688 | }
|
---|
689 |
|
---|
690 | DBS (DB_IMPLICIT,
|
---|
691 | (d->had_stem
|
---|
692 | ? _("Trying implicit prerequisite `%s'.\n")
|
---|
693 | : _("Trying rule prerequisite `%s'.\n"), name));
|
---|
694 |
|
---|
695 | /* If this prerequisite also happened to be explicitly mentioned
|
---|
696 | for FILE skip all the test below since it it has to be built
|
---|
697 | anyway, no matter which implicit rule we choose. */
|
---|
698 |
|
---|
699 | for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
|
---|
700 | if (streq (dep_name (expl_d), name))
|
---|
701 | break;
|
---|
702 | if (expl_d != 0)
|
---|
703 | continue;
|
---|
704 |
|
---|
705 | /* The DEP->changed flag says that this dependency resides in a
|
---|
706 | nonexistent directory. So we normally can skip looking for
|
---|
707 | the file. However, if CHECK_LASTSLASH is set, then the
|
---|
708 | dependency file we are actually looking for is in a different
|
---|
709 | directory (the one gotten by prepending FILENAME's directory),
|
---|
710 | so it might actually exist. */
|
---|
711 |
|
---|
712 | /* @@ dep->changed check is disabled. */
|
---|
713 | if (((f = lookup_file (name)) != 0 && f->is_target)
|
---|
714 | /*|| ((!dep->changed || check_lastslash) && */
|
---|
715 | || file_exists_p (name))
|
---|
716 | continue;
|
---|
717 |
|
---|
718 | /* This code, given FILENAME = "lib/foo.o", dependency name
|
---|
719 | "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */
|
---|
720 | {
|
---|
721 | const char *vname = vpath_search (name, 0);
|
---|
722 | if (vname)
|
---|
723 | {
|
---|
724 | DBS (DB_IMPLICIT,
|
---|
725 | (_("Found prerequisite `%s' as VPATH `%s'\n"),
|
---|
726 | name, vname));
|
---|
727 | continue;
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 |
|
---|
732 | /* We could not find the file in any place we should look. Try
|
---|
733 | to make this dependency as an intermediate file, but only on
|
---|
734 | the second pass. */
|
---|
735 |
|
---|
736 | if (intermed_ok)
|
---|
737 | {
|
---|
738 | if (intermediate_file == 0)
|
---|
739 | intermediate_file = alloca (sizeof (struct file));
|
---|
740 |
|
---|
741 | DBS (DB_IMPLICIT,
|
---|
742 | (_("Looking for a rule with intermediate file `%s'.\n"),
|
---|
743 | name));
|
---|
744 |
|
---|
745 | memset (intermediate_file, '\0', sizeof (struct file));
|
---|
746 | intermediate_file->name = name;
|
---|
747 | if (pattern_search (intermediate_file,
|
---|
748 | 0,
|
---|
749 | depth + 1,
|
---|
750 | recursions + 1))
|
---|
751 | {
|
---|
752 | d->intermediate_pattern = intermediate_file->name;
|
---|
753 | intermediate_file->name = strcache_add (name);
|
---|
754 | d->intermediate_file = intermediate_file;
|
---|
755 | intermediate_file = 0;
|
---|
756 |
|
---|
757 | continue;
|
---|
758 | }
|
---|
759 |
|
---|
760 | /* If we have tried to find P as an intermediate
|
---|
761 | file and failed, mark that name as impossible
|
---|
762 | so we won't go through the search again later. */
|
---|
763 | if (intermediate_file->variables)
|
---|
764 | free_variable_set (intermediate_file->variables);
|
---|
765 | file_impossible (name);
|
---|
766 | }
|
---|
767 |
|
---|
768 | /* A dependency of this rule does not exist. Therefore,
|
---|
769 | this rule fails. */
|
---|
770 | failed = 1;
|
---|
771 | break;
|
---|
772 | }
|
---|
773 |
|
---|
774 | /* This rule is no longer `in use' for recursive searches. */
|
---|
775 | rule->in_use = 0;
|
---|
776 |
|
---|
777 | if (failed)
|
---|
778 | {
|
---|
779 | /* This pattern rule does not apply. If some of its
|
---|
780 | dependencies succeeded, free the data structure
|
---|
781 | describing them. */
|
---|
782 | free_idep_chain (deps);
|
---|
783 | deps = 0;
|
---|
784 | }
|
---|
785 | else
|
---|
786 | /* This pattern rule does apply. Stop looking for one. */
|
---|
787 | break;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /* If we found an applicable rule without
|
---|
791 | intermediate files, don't try with them. */
|
---|
792 | if (ri < nrules)
|
---|
793 | break;
|
---|
794 |
|
---|
795 | rule = 0;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /* RULE is nil if the loop went all the way
|
---|
799 | through the list and everything failed. */
|
---|
800 | if (rule == 0)
|
---|
801 | goto done;
|
---|
802 |
|
---|
803 | foundrule = ri;
|
---|
804 |
|
---|
805 | /* If we are recursing, store the pattern that matched
|
---|
806 | FILENAME in FILE->name for use in upper levels. */
|
---|
807 |
|
---|
808 | if (recursions > 0)
|
---|
809 | /* Kludge-o-matic */
|
---|
810 | file->name = rule->targets[matches[foundrule]];
|
---|
811 |
|
---|
812 | /* FOUND_FILES lists the dependencies for the rule we found.
|
---|
813 | This includes the intermediate files, if any.
|
---|
814 | Convert them into entries on the deps-chain of FILE. */
|
---|
815 |
|
---|
816 | if (remove_explicit_deps)
|
---|
817 | {
|
---|
818 | /* Remove all the dependencies that didn't come from
|
---|
819 | this implicit rule. */
|
---|
820 |
|
---|
821 | dep = file->deps;
|
---|
822 | while (dep != 0)
|
---|
823 | {
|
---|
824 | struct dep *next = dep->next;
|
---|
825 | free_dep (dep);
|
---|
826 | dep = next;
|
---|
827 | }
|
---|
828 | file->deps = 0;
|
---|
829 | }
|
---|
830 |
|
---|
831 | expl_d = file->deps; /* We will add them at the end. */
|
---|
832 | d_ptr = &file->deps;
|
---|
833 |
|
---|
834 | for (d = deps; d != 0; d = d->next)
|
---|
835 | {
|
---|
836 | const char *s;
|
---|
837 |
|
---|
838 | if (d->intermediate_file != 0)
|
---|
839 | {
|
---|
840 | /* If we need to use an intermediate file,
|
---|
841 | make sure it is entered as a target, with the info that was
|
---|
842 | found for it in the recursive pattern_search call.
|
---|
843 | We know that the intermediate file did not already exist as
|
---|
844 | a target; therefore we can assume that the deps and cmds
|
---|
845 | of F below are null before we change them. */
|
---|
846 |
|
---|
847 | struct file *imf = d->intermediate_file;
|
---|
848 | register struct file *f = lookup_file (imf->name);
|
---|
849 |
|
---|
850 | /* We don't want to delete an intermediate file that happened
|
---|
851 | to be a prerequisite of some (other) target. Mark it as
|
---|
852 | precious. */
|
---|
853 | if (f != 0)
|
---|
854 | f->precious = 1;
|
---|
855 | else
|
---|
856 | f = enter_file (strcache_add (imf->name));
|
---|
857 |
|
---|
858 | f->deps = imf->deps;
|
---|
859 | f->cmds = imf->cmds;
|
---|
860 | f->stem = imf->stem;
|
---|
861 | f->also_make = imf->also_make;
|
---|
862 | f->is_target = 1;
|
---|
863 |
|
---|
864 | if (!f->precious)
|
---|
865 | {
|
---|
866 | imf = lookup_file (d->intermediate_pattern);
|
---|
867 | if (imf != 0 && imf->precious)
|
---|
868 | f->precious = 1;
|
---|
869 | }
|
---|
870 |
|
---|
871 | f->intermediate = 1;
|
---|
872 | f->tried_implicit = 1;
|
---|
873 | for (dep = f->deps; dep != 0; dep = dep->next)
|
---|
874 | {
|
---|
875 | dep->file = enter_file (dep->name);
|
---|
876 | dep->name = 0;
|
---|
877 | dep->file->tried_implicit |= dep->changed;
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | dep = alloc_dep ();
|
---|
882 | dep->ignore_mtime = d->ignore_mtime;
|
---|
883 | s = d->name; /* Hijacking the name. */
|
---|
884 | d->name = 0;
|
---|
885 | if (recursions == 0)
|
---|
886 | {
|
---|
887 | dep->file = lookup_file (s);
|
---|
888 | if (dep->file == 0)
|
---|
889 | dep->file = enter_file (s);
|
---|
890 | }
|
---|
891 | else
|
---|
892 | dep->name = s;
|
---|
893 |
|
---|
894 | if (d->intermediate_file == 0 && tryrules[foundrule]->terminal)
|
---|
895 | {
|
---|
896 | /* If the file actually existed (was not an intermediate file),
|
---|
897 | and the rule that found it was a terminal one, then we want
|
---|
898 | to mark the found file so that it will not have implicit rule
|
---|
899 | search done for it. If we are not entering a `struct file' for
|
---|
900 | it now, we indicate this with the `changed' flag. */
|
---|
901 | if (dep->file == 0)
|
---|
902 | dep->changed = 1;
|
---|
903 | else
|
---|
904 | dep->file->tried_implicit = 1;
|
---|
905 | }
|
---|
906 |
|
---|
907 | *d_ptr = dep;
|
---|
908 | d_ptr = &dep->next;
|
---|
909 | }
|
---|
910 |
|
---|
911 | *d_ptr = expl_d;
|
---|
912 |
|
---|
913 | if (!checked_lastslash[foundrule])
|
---|
914 | {
|
---|
915 | /* Always allocate new storage, since STEM might be
|
---|
916 | on the stack for an intermediate file. */
|
---|
917 | file->stem = strcache_add_len (stem, stemlen);
|
---|
918 | fullstemlen = stemlen;
|
---|
919 | }
|
---|
920 | else
|
---|
921 | {
|
---|
922 | int dirlen = (lastslash + 1) - filename;
|
---|
923 | char *sp;
|
---|
924 |
|
---|
925 | /* We want to prepend the directory from
|
---|
926 | the original FILENAME onto the stem. */
|
---|
927 | fullstemlen = dirlen + stemlen;
|
---|
928 | sp = alloca (fullstemlen + 1);
|
---|
929 | memcpy (sp, filename, dirlen);
|
---|
930 | memcpy (sp + dirlen, stem, stemlen);
|
---|
931 | sp[fullstemlen] = '\0';
|
---|
932 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
933 | file->stem = strcache_add (sp);
|
---|
934 | #else
|
---|
935 | file->stem = strcache_add_len (sp, fullstemlen);
|
---|
936 | #endif
|
---|
937 | }
|
---|
938 |
|
---|
939 | file->cmds = rule->cmds;
|
---|
940 | file->is_target = 1;
|
---|
941 |
|
---|
942 | /* Set precious flag. */
|
---|
943 | {
|
---|
944 | struct file *f = lookup_file (rule->targets[matches[foundrule]]);
|
---|
945 | if (f && f->precious)
|
---|
946 | file->precious = 1;
|
---|
947 | }
|
---|
948 |
|
---|
949 | /* If this rule builds other targets, too, put the others into FILE's
|
---|
950 | `also_make' member. */
|
---|
951 |
|
---|
952 | if (rule->num > 1)
|
---|
953 | for (ri = 0; ri < rule->num; ++ri)
|
---|
954 | if (ri != matches[foundrule])
|
---|
955 | {
|
---|
956 | char *p = alloca (rule->lens[ri] + fullstemlen + 1);
|
---|
957 | struct file *f;
|
---|
958 | struct dep *new = alloc_dep ();
|
---|
959 |
|
---|
960 | /* GKM FIMXE: handle '|' here too */
|
---|
961 | memcpy (p, rule->targets[ri],
|
---|
962 | rule->suffixes[ri] - rule->targets[ri] - 1);
|
---|
963 | p += rule->suffixes[ri] - rule->targets[ri] - 1;
|
---|
964 | memcpy (p, file->stem, fullstemlen);
|
---|
965 | p += fullstemlen;
|
---|
966 | memcpy (p, rule->suffixes[ri],
|
---|
967 | rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
|
---|
968 | new->name = strcache_add (p);
|
---|
969 | new->file = enter_file (new->name);
|
---|
970 | new->next = file->also_make;
|
---|
971 |
|
---|
972 | /* Set precious flag. */
|
---|
973 | f = lookup_file (rule->targets[ri]);
|
---|
974 | if (f && f->precious)
|
---|
975 | new->file->precious = 1;
|
---|
976 |
|
---|
977 | /* Set the is_target flag so that this file is not treated
|
---|
978 | as intermediate by the pattern rule search algorithm and
|
---|
979 | file_exists_p cannot pick it up yet. */
|
---|
980 | new->file->is_target = 1;
|
---|
981 |
|
---|
982 | file->also_make = new;
|
---|
983 | }
|
---|
984 |
|
---|
985 | done:
|
---|
986 | free_idep_chain (deps);
|
---|
987 | free (tryrules);
|
---|
988 |
|
---|
989 | return rule != 0;
|
---|
990 | }
|
---|