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