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