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