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