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