1 | /* Pattern and suffix rule internals for GNU Make. |
---|
2 | Copyright (C) 1988,89,90,91,92,93, 1998 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 "dep.h" |
---|
22 | #include "filedef.h" |
---|
23 | #include "job.h" |
---|
24 | #include "commands.h" |
---|
25 | #include "variable.h" |
---|
26 | #include "rule.h" |
---|
27 | |
---|
28 | static void freerule PARAMS ((struct rule *rule, struct rule *lastrule)); |
---|
29 | |
---|
30 | /* Chain of all pattern rules. */ |
---|
31 | |
---|
32 | struct rule *pattern_rules; |
---|
33 | |
---|
34 | /* Pointer to last rule in the chain, so we can add onto the end. */ |
---|
35 | |
---|
36 | struct rule *last_pattern_rule; |
---|
37 | |
---|
38 | /* Number of rules in the chain. */ |
---|
39 | |
---|
40 | unsigned int num_pattern_rules; |
---|
41 | |
---|
42 | /* Maximum number of target patterns of any pattern rule. */ |
---|
43 | |
---|
44 | unsigned int max_pattern_targets; |
---|
45 | |
---|
46 | /* Maximum number of dependencies of any pattern rule. */ |
---|
47 | |
---|
48 | unsigned int max_pattern_deps; |
---|
49 | |
---|
50 | /* Maximum length of the name of a dependencies of any pattern rule. */ |
---|
51 | |
---|
52 | unsigned int max_pattern_dep_length; |
---|
53 | |
---|
54 | /* Chain of all pattern-specific variables. */ |
---|
55 | |
---|
56 | static struct pattern_var *pattern_vars; |
---|
57 | |
---|
58 | /* Pointer to last struct in the chain, so we can add onto the end. */ |
---|
59 | |
---|
60 | static struct pattern_var *last_pattern_var; |
---|
61 | |
---|
62 | /* Pointer to structure for the file .SUFFIXES |
---|
63 | whose dependencies are the suffixes to be searched. */ |
---|
64 | |
---|
65 | struct file *suffix_file; |
---|
66 | |
---|
67 | /* Maximum length of a suffix. */ |
---|
68 | |
---|
69 | unsigned int maxsuffix; |
---|
70 | |
---|
71 | /* Compute the maximum dependency length and maximum number of |
---|
72 | dependencies of all implicit rules. Also sets the subdir |
---|
73 | flag for a rule when appropriate, possibly removing the rule |
---|
74 | completely when appropriate. */ |
---|
75 | |
---|
76 | void |
---|
77 | count_implicit_rule_limits () |
---|
78 | { |
---|
79 | char *name; |
---|
80 | unsigned int namelen; |
---|
81 | register struct rule *rule, *lastrule; |
---|
82 | |
---|
83 | num_pattern_rules = max_pattern_targets = max_pattern_deps = 0; |
---|
84 | max_pattern_dep_length = 0; |
---|
85 | |
---|
86 | name = 0; |
---|
87 | namelen = 0; |
---|
88 | rule = pattern_rules; |
---|
89 | lastrule = 0; |
---|
90 | while (rule != 0) |
---|
91 | { |
---|
92 | unsigned int ndeps = 0; |
---|
93 | register struct dep *dep; |
---|
94 | struct rule *next = rule->next; |
---|
95 | unsigned int ntargets; |
---|
96 | |
---|
97 | ++num_pattern_rules; |
---|
98 | |
---|
99 | ntargets = 0; |
---|
100 | while (rule->targets[ntargets] != 0) |
---|
101 | ++ntargets; |
---|
102 | |
---|
103 | if (ntargets > max_pattern_targets) |
---|
104 | max_pattern_targets = ntargets; |
---|
105 | |
---|
106 | for (dep = rule->deps; dep != 0; dep = dep->next) |
---|
107 | { |
---|
108 | unsigned int len = strlen (dep->name); |
---|
109 | |
---|
110 | #ifdef VMS |
---|
111 | char *p = strrchr (dep->name, ']'); |
---|
112 | char *p2; |
---|
113 | if (p == 0) |
---|
114 | p = strrchr (dep->name, ':'); |
---|
115 | p2 = p != 0 ? strchr (dep->name, '%') : 0; |
---|
116 | #else |
---|
117 | char *p = strrchr (dep->name, '/'); |
---|
118 | char *p2 = p != 0 ? strchr (dep->name, '%') : 0; |
---|
119 | #endif |
---|
120 | ndeps++; |
---|
121 | |
---|
122 | if (len > max_pattern_dep_length) |
---|
123 | max_pattern_dep_length = len; |
---|
124 | |
---|
125 | if (p != 0 && p2 > p) |
---|
126 | { |
---|
127 | /* There is a slash before the % in the dep name. |
---|
128 | Extract the directory name. */ |
---|
129 | if (p == dep->name) |
---|
130 | ++p; |
---|
131 | if (p - dep->name > namelen) |
---|
132 | { |
---|
133 | if (name != 0) |
---|
134 | free (name); |
---|
135 | namelen = p - dep->name; |
---|
136 | name = (char *) xmalloc (namelen + 1); |
---|
137 | } |
---|
138 | bcopy (dep->name, name, p - dep->name); |
---|
139 | name[p - dep->name] = '\0'; |
---|
140 | |
---|
141 | /* In the deps of an implicit rule the `changed' flag |
---|
142 | actually indicates that the dependency is in a |
---|
143 | nonexistent subdirectory. */ |
---|
144 | |
---|
145 | dep->changed = !dir_file_exists_p (name, ""); |
---|
146 | #ifdef VMS |
---|
147 | if (dep->changed && strchr (name, ':') != 0) |
---|
148 | #else |
---|
149 | if (dep->changed && *name == '/') |
---|
150 | #endif |
---|
151 | { |
---|
152 | /* The name is absolute and the directory does not exist. |
---|
153 | This rule can never possibly match, since this dependency |
---|
154 | can never possibly exist. So just remove the rule from |
---|
155 | the list. */ |
---|
156 | freerule (rule, lastrule); |
---|
157 | --num_pattern_rules; |
---|
158 | goto end_main_loop; |
---|
159 | } |
---|
160 | } |
---|
161 | else |
---|
162 | /* This dependency does not reside in a subdirectory. */ |
---|
163 | dep->changed = 0; |
---|
164 | } |
---|
165 | |
---|
166 | if (ndeps > max_pattern_deps) |
---|
167 | max_pattern_deps = ndeps; |
---|
168 | |
---|
169 | lastrule = rule; |
---|
170 | end_main_loop: |
---|
171 | rule = next; |
---|
172 | } |
---|
173 | |
---|
174 | if (name != 0) |
---|
175 | free (name); |
---|
176 | } |
---|
177 | |
---|
178 | /* Create a pattern rule from a suffix rule. |
---|
179 | TARGET is the target suffix; SOURCE is the source suffix. |
---|
180 | CMDS are the commands. |
---|
181 | If TARGET is nil, it means the target pattern should be `(%.o)'. |
---|
182 | If SOURCE is nil, it means there should be no deps. */ |
---|
183 | |
---|
184 | static void |
---|
185 | convert_suffix_rule (target, source, cmds) |
---|
186 | char *target, *source; |
---|
187 | struct commands *cmds; |
---|
188 | { |
---|
189 | char *targname, *targpercent, *depname; |
---|
190 | char **names, **percents; |
---|
191 | struct dep *deps; |
---|
192 | unsigned int len; |
---|
193 | |
---|
194 | if (target == 0) |
---|
195 | /* Special case: TARGET being nil means we are defining a |
---|
196 | `.X.a' suffix rule; the target pattern is always `(%.o)'. */ |
---|
197 | { |
---|
198 | #ifdef VMS |
---|
199 | targname = savestring ("(%.obj)", 7); |
---|
200 | #else |
---|
201 | targname = savestring ("(%.o)", 5); |
---|
202 | #endif |
---|
203 | targpercent = targname + 1; |
---|
204 | } |
---|
205 | else |
---|
206 | { |
---|
207 | /* Construct the target name. */ |
---|
208 | len = strlen (target); |
---|
209 | targname = xmalloc (1 + len + 1); |
---|
210 | targname[0] = '%'; |
---|
211 | bcopy (target, targname + 1, len + 1); |
---|
212 | targpercent = targname; |
---|
213 | } |
---|
214 | |
---|
215 | names = (char **) xmalloc (2 * sizeof (char *)); |
---|
216 | percents = (char **) alloca (2 * sizeof (char *)); |
---|
217 | names[0] = targname; |
---|
218 | percents[0] = targpercent; |
---|
219 | names[1] = percents[1] = 0; |
---|
220 | |
---|
221 | if (source == 0) |
---|
222 | deps = 0; |
---|
223 | else |
---|
224 | { |
---|
225 | /* Construct the dependency name. */ |
---|
226 | len = strlen (source); |
---|
227 | depname = xmalloc (1 + len + 1); |
---|
228 | depname[0] = '%'; |
---|
229 | bcopy (source, depname + 1, len + 1); |
---|
230 | deps = (struct dep *) xmalloc (sizeof (struct dep)); |
---|
231 | deps->next = 0; |
---|
232 | deps->name = depname; |
---|
233 | } |
---|
234 | |
---|
235 | create_pattern_rule (names, percents, 0, deps, cmds, 0); |
---|
236 | } |
---|
237 | |
---|
238 | /* Convert old-style suffix rules to pattern rules. |
---|
239 | All rules for the suffixes on the .SUFFIXES list |
---|
240 | are converted and added to the chain of pattern rules. */ |
---|
241 | |
---|
242 | void |
---|
243 | convert_to_pattern () |
---|
244 | { |
---|
245 | register struct dep *d, *d2; |
---|
246 | register struct file *f; |
---|
247 | register char *rulename; |
---|
248 | register unsigned int slen, s2len; |
---|
249 | |
---|
250 | /* Compute maximum length of all the suffixes. */ |
---|
251 | |
---|
252 | maxsuffix = 0; |
---|
253 | for (d = suffix_file->deps; d != 0; d = d->next) |
---|
254 | { |
---|
255 | register unsigned int namelen = strlen (dep_name (d)); |
---|
256 | if (namelen > maxsuffix) |
---|
257 | maxsuffix = namelen; |
---|
258 | } |
---|
259 | |
---|
260 | rulename = (char *) alloca ((maxsuffix * 2) + 1); |
---|
261 | |
---|
262 | for (d = suffix_file->deps; d != 0; d = d->next) |
---|
263 | { |
---|
264 | /* Make a rule that is just the suffix, with no deps or commands. |
---|
265 | This rule exists solely to disqualify match-anything rules. */ |
---|
266 | convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0); |
---|
267 | |
---|
268 | f = d->file; |
---|
269 | if (f->cmds != 0) |
---|
270 | /* Record a pattern for this suffix's null-suffix rule. */ |
---|
271 | convert_suffix_rule ("", dep_name (d), f->cmds); |
---|
272 | |
---|
273 | /* Record a pattern for each of this suffix's two-suffix rules. */ |
---|
274 | slen = strlen (dep_name (d)); |
---|
275 | bcopy (dep_name (d), rulename, slen); |
---|
276 | for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next) |
---|
277 | { |
---|
278 | s2len = strlen (dep_name (d2)); |
---|
279 | |
---|
280 | if (slen == s2len && streq (dep_name (d), dep_name (d2))) |
---|
281 | continue; |
---|
282 | |
---|
283 | bcopy (dep_name (d2), rulename + slen, s2len + 1); |
---|
284 | f = lookup_file (rulename); |
---|
285 | if (f == 0 || f->cmds == 0) |
---|
286 | continue; |
---|
287 | |
---|
288 | if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a') |
---|
289 | /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'. |
---|
290 | It also generates a normal `%.a: %.X' rule below. */ |
---|
291 | convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */ |
---|
292 | dep_name (d), |
---|
293 | f->cmds); |
---|
294 | |
---|
295 | /* The suffix rule `.X.Y:' is converted |
---|
296 | to the pattern rule `%.Y: %.X'. */ |
---|
297 | convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds); |
---|
298 | } |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | /* Install the pattern rule RULE (whose fields have been filled in) |
---|
304 | at the end of the list (so that any rules previously defined |
---|
305 | will take precedence). If this rule duplicates a previous one |
---|
306 | (identical target and dependencies), the old one is replaced |
---|
307 | if OVERRIDE is nonzero, otherwise this new one is thrown out. |
---|
308 | When an old rule is replaced, the new one is put at the end of the |
---|
309 | list. Return nonzero if RULE is used; zero if not. */ |
---|
310 | |
---|
311 | int |
---|
312 | new_pattern_rule (rule, override) |
---|
313 | register struct rule *rule; |
---|
314 | int override; |
---|
315 | { |
---|
316 | register struct rule *r, *lastrule; |
---|
317 | register unsigned int i, j; |
---|
318 | |
---|
319 | rule->in_use = 0; |
---|
320 | rule->terminal = 0; |
---|
321 | |
---|
322 | rule->next = 0; |
---|
323 | |
---|
324 | /* Search for an identical rule. */ |
---|
325 | lastrule = 0; |
---|
326 | for (r = pattern_rules; r != 0; lastrule = r, r = r->next) |
---|
327 | for (i = 0; rule->targets[i] != 0; ++i) |
---|
328 | { |
---|
329 | for (j = 0; r->targets[j] != 0; ++j) |
---|
330 | if (!streq (rule->targets[i], r->targets[j])) |
---|
331 | break; |
---|
332 | if (r->targets[j] == 0) |
---|
333 | /* All the targets matched. */ |
---|
334 | { |
---|
335 | register struct dep *d, *d2; |
---|
336 | for (d = rule->deps, d2 = r->deps; |
---|
337 | d != 0 && d2 != 0; d = d->next, d2 = d2->next) |
---|
338 | if (!streq (dep_name (d), dep_name (d2))) |
---|
339 | break; |
---|
340 | if (d == 0 && d2 == 0) |
---|
341 | { |
---|
342 | /* All the dependencies matched. */ |
---|
343 | if (override) |
---|
344 | { |
---|
345 | /* Remove the old rule. */ |
---|
346 | freerule (r, lastrule); |
---|
347 | /* Install the new one. */ |
---|
348 | if (pattern_rules == 0) |
---|
349 | pattern_rules = rule; |
---|
350 | else |
---|
351 | last_pattern_rule->next = rule; |
---|
352 | last_pattern_rule = rule; |
---|
353 | |
---|
354 | /* We got one. Stop looking. */ |
---|
355 | goto matched; |
---|
356 | } |
---|
357 | else |
---|
358 | { |
---|
359 | /* The old rule stays intact. Destroy the new one. */ |
---|
360 | freerule (rule, (struct rule *) 0); |
---|
361 | return 0; |
---|
362 | } |
---|
363 | } |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | matched:; |
---|
368 | |
---|
369 | if (r == 0) |
---|
370 | { |
---|
371 | /* There was no rule to replace. */ |
---|
372 | if (pattern_rules == 0) |
---|
373 | pattern_rules = rule; |
---|
374 | else |
---|
375 | last_pattern_rule->next = rule; |
---|
376 | last_pattern_rule = rule; |
---|
377 | } |
---|
378 | |
---|
379 | return 1; |
---|
380 | } |
---|
381 | |
---|
382 | |
---|
383 | /* Install an implicit pattern rule based on the three text strings |
---|
384 | in the structure P points to. These strings come from one of |
---|
385 | the arrays of default implicit pattern rules. |
---|
386 | TERMINAL specifies what the `terminal' field of the rule should be. */ |
---|
387 | |
---|
388 | void |
---|
389 | install_pattern_rule (p, terminal) |
---|
390 | struct pspec *p; |
---|
391 | int terminal; |
---|
392 | { |
---|
393 | register struct rule *r; |
---|
394 | char *ptr; |
---|
395 | |
---|
396 | r = (struct rule *) xmalloc (sizeof (struct rule)); |
---|
397 | |
---|
398 | r->targets = (char **) xmalloc (2 * sizeof (char *)); |
---|
399 | r->suffixes = (char **) xmalloc (2 * sizeof (char *)); |
---|
400 | r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int)); |
---|
401 | |
---|
402 | r->targets[1] = 0; |
---|
403 | r->suffixes[1] = 0; |
---|
404 | r->lens[1] = 0; |
---|
405 | |
---|
406 | r->lens[0] = strlen (p->target); |
---|
407 | /* These will all be string literals, but we malloc space for |
---|
408 | them anyway because somebody might want to free them later on. */ |
---|
409 | r->targets[0] = savestring (p->target, r->lens[0]); |
---|
410 | r->suffixes[0] = find_percent (r->targets[0]); |
---|
411 | if (r->suffixes[0] == 0) |
---|
412 | /* Programmer-out-to-lunch error. */ |
---|
413 | abort (); |
---|
414 | else |
---|
415 | ++r->suffixes[0]; |
---|
416 | |
---|
417 | ptr = p->dep; |
---|
418 | r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0', |
---|
419 | sizeof (struct dep), 1), |
---|
420 | sizeof (struct dep)); |
---|
421 | |
---|
422 | if (new_pattern_rule (r, 0)) |
---|
423 | { |
---|
424 | r->terminal = terminal; |
---|
425 | r->cmds = (struct commands *) xmalloc (sizeof (struct commands)); |
---|
426 | r->cmds->fileinfo.filenm = 0; |
---|
427 | r->cmds->fileinfo.lineno = 0; |
---|
428 | /* These will all be string literals, but we malloc space for them |
---|
429 | anyway because somebody might want to free them later. */ |
---|
430 | r->cmds->commands = xstrdup (p->commands); |
---|
431 | r->cmds->command_lines = 0; |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | |
---|
436 | /* Free all the storage used in RULE and take it out of the |
---|
437 | pattern_rules chain. LASTRULE is the rule whose next pointer |
---|
438 | points to RULE. */ |
---|
439 | |
---|
440 | static void |
---|
441 | freerule (rule, lastrule) |
---|
442 | register struct rule *rule, *lastrule; |
---|
443 | { |
---|
444 | struct rule *next = rule->next; |
---|
445 | register unsigned int i; |
---|
446 | register struct dep *dep; |
---|
447 | |
---|
448 | for (i = 0; rule->targets[i] != 0; ++i) |
---|
449 | free (rule->targets[i]); |
---|
450 | |
---|
451 | dep = rule->deps; |
---|
452 | while (dep) |
---|
453 | { |
---|
454 | struct dep *t; |
---|
455 | |
---|
456 | t = dep->next; |
---|
457 | /* We might leak dep->name here, but I'm not sure how to fix this: I |
---|
458 | think that pointer might be shared (e.g., in the file hash?) */ |
---|
459 | free ((char *) dep); |
---|
460 | dep = t; |
---|
461 | } |
---|
462 | |
---|
463 | free ((char *) rule->targets); |
---|
464 | free ((char *) rule->suffixes); |
---|
465 | free ((char *) rule->lens); |
---|
466 | |
---|
467 | /* We can't free the storage for the commands because there |
---|
468 | are ways that they could be in more than one place: |
---|
469 | * If the commands came from a suffix rule, they could also be in |
---|
470 | the `struct file's for other suffix rules or plain targets given |
---|
471 | on the same makefile line. |
---|
472 | * If two suffixes that together make a two-suffix rule were each |
---|
473 | given twice in the .SUFFIXES list, and in the proper order, two |
---|
474 | identical pattern rules would be created and the second one would |
---|
475 | be discarded here, but both would contain the same `struct commands' |
---|
476 | pointer from the `struct file' for the suffix rule. */ |
---|
477 | |
---|
478 | free ((char *) rule); |
---|
479 | |
---|
480 | if (pattern_rules == rule) |
---|
481 | if (lastrule != 0) |
---|
482 | abort (); |
---|
483 | else |
---|
484 | pattern_rules = next; |
---|
485 | else if (lastrule != 0) |
---|
486 | lastrule->next = next; |
---|
487 | if (last_pattern_rule == rule) |
---|
488 | last_pattern_rule = lastrule; |
---|
489 | } |
---|
490 | |
---|
491 | /* Create a new pattern rule with the targets in the nil-terminated |
---|
492 | array TARGETS. If TARGET_PERCENTS is not nil, it is an array of |
---|
493 | pointers into the elements of TARGETS, where the `%'s are. |
---|
494 | The new rule has dependencies DEPS and commands from COMMANDS. |
---|
495 | It is a terminal rule if TERMINAL is nonzero. This rule overrides |
---|
496 | identical rules with different commands if OVERRIDE is nonzero. |
---|
497 | |
---|
498 | The storage for TARGETS and its elements is used and must not be freed |
---|
499 | until the rule is destroyed. The storage for TARGET_PERCENTS is not used; |
---|
500 | it may be freed. */ |
---|
501 | |
---|
502 | void |
---|
503 | create_pattern_rule (targets, target_percents, |
---|
504 | terminal, deps, commands, override) |
---|
505 | char **targets, **target_percents; |
---|
506 | int terminal; |
---|
507 | struct dep *deps; |
---|
508 | struct commands *commands; |
---|
509 | int override; |
---|
510 | { |
---|
511 | register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule)); |
---|
512 | register unsigned int max_targets, i; |
---|
513 | |
---|
514 | r->cmds = commands; |
---|
515 | r->deps = deps; |
---|
516 | r->targets = targets; |
---|
517 | |
---|
518 | max_targets = 2; |
---|
519 | r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int)); |
---|
520 | r->suffixes = (char **) xmalloc (2 * sizeof (char *)); |
---|
521 | for (i = 0; targets[i] != 0; ++i) |
---|
522 | { |
---|
523 | if (i == max_targets - 1) |
---|
524 | { |
---|
525 | max_targets += 5; |
---|
526 | r->lens = (unsigned int *) |
---|
527 | xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int)); |
---|
528 | r->suffixes = (char **) |
---|
529 | xrealloc ((char *) r->suffixes, max_targets * sizeof (char *)); |
---|
530 | } |
---|
531 | r->lens[i] = strlen (targets[i]); |
---|
532 | r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i]) |
---|
533 | : target_percents[i]) + 1; |
---|
534 | if (r->suffixes[i] == 0) |
---|
535 | abort (); |
---|
536 | } |
---|
537 | |
---|
538 | if (i < max_targets - 1) |
---|
539 | { |
---|
540 | r->lens = (unsigned int *) xrealloc ((char *) r->lens, |
---|
541 | (i + 1) * sizeof (unsigned int)); |
---|
542 | r->suffixes = (char **) xrealloc ((char *) r->suffixes, |
---|
543 | (i + 1) * sizeof (char *)); |
---|
544 | } |
---|
545 | |
---|
546 | if (new_pattern_rule (r, override)) |
---|
547 | r->terminal = terminal; |
---|
548 | } |
---|
549 | |
---|
550 | /* Create a new pattern-specific variable struct. */ |
---|
551 | |
---|
552 | struct pattern_var * |
---|
553 | create_pattern_var (target, suffix) |
---|
554 | char *target, *suffix; |
---|
555 | { |
---|
556 | register struct pattern_var *p = 0; |
---|
557 | unsigned int len = strlen(target); |
---|
558 | |
---|
559 | /* Look to see if this pattern already exists in the list. */ |
---|
560 | for (p = pattern_vars; p != NULL; p = p->next) |
---|
561 | if (p->len == len && !strcmp(p->target, target)) |
---|
562 | break; |
---|
563 | |
---|
564 | if (p == 0) |
---|
565 | { |
---|
566 | p = (struct pattern_var *) xmalloc (sizeof (struct pattern_var)); |
---|
567 | if (last_pattern_var != 0) |
---|
568 | last_pattern_var->next = p; |
---|
569 | else |
---|
570 | pattern_vars = p; |
---|
571 | last_pattern_var = p; |
---|
572 | p->next = 0; |
---|
573 | p->target = target; |
---|
574 | p->len = len; |
---|
575 | p->suffix = suffix + 1; |
---|
576 | p->vars = create_new_variable_set(); |
---|
577 | } |
---|
578 | |
---|
579 | return p; |
---|
580 | } |
---|
581 | |
---|
582 | /* Look up a target in the pattern-specific variable list. */ |
---|
583 | |
---|
584 | struct pattern_var * |
---|
585 | lookup_pattern_var (target) |
---|
586 | char *target; |
---|
587 | { |
---|
588 | struct pattern_var *p; |
---|
589 | unsigned int targlen = strlen(target); |
---|
590 | |
---|
591 | for (p = pattern_vars; p != 0; p = p->next) |
---|
592 | { |
---|
593 | char *stem; |
---|
594 | unsigned int stemlen; |
---|
595 | |
---|
596 | if (p->len > targlen) |
---|
597 | /* It can't possibly match. */ |
---|
598 | continue; |
---|
599 | |
---|
600 | /* From the lengths of the filename and the pattern parts, |
---|
601 | find the stem: the part of the filename that matches the %. */ |
---|
602 | stem = target + (p->suffix - p->target - 1); |
---|
603 | stemlen = targlen - p->len + 1; |
---|
604 | |
---|
605 | /* Compare the text in the pattern before the stem, if any. */ |
---|
606 | if (stem > target && !strneq (p->target, target, stem - target)) |
---|
607 | continue; |
---|
608 | |
---|
609 | /* Compare the text in the pattern after the stem, if any. |
---|
610 | We could test simply using streq, but this way we compare the |
---|
611 | first two characters immediately. This saves time in the very |
---|
612 | common case where the first character matches because it is a |
---|
613 | period. */ |
---|
614 | if (*p->suffix == stem[stemlen] |
---|
615 | && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1]))) |
---|
616 | break; |
---|
617 | } |
---|
618 | |
---|
619 | return p; |
---|
620 | } |
---|
621 | |
---|
622 | /* Print the data base of rules. */ |
---|
623 | |
---|
624 | static void /* Useful to call from gdb. */ |
---|
625 | print_rule (r) |
---|
626 | struct rule *r; |
---|
627 | { |
---|
628 | register unsigned int i; |
---|
629 | register struct dep *d; |
---|
630 | |
---|
631 | for (i = 0; r->targets[i] != 0; ++i) |
---|
632 | { |
---|
633 | fputs (r->targets[i], stdout); |
---|
634 | if (r->targets[i + 1] != 0) |
---|
635 | putchar (' '); |
---|
636 | else |
---|
637 | putchar (':'); |
---|
638 | } |
---|
639 | if (r->terminal) |
---|
640 | putchar (':'); |
---|
641 | |
---|
642 | for (d = r->deps; d != 0; d = d->next) |
---|
643 | printf (" %s", dep_name (d)); |
---|
644 | putchar ('\n'); |
---|
645 | |
---|
646 | if (r->cmds != 0) |
---|
647 | print_commands (r->cmds); |
---|
648 | } |
---|
649 | |
---|
650 | void |
---|
651 | print_rule_data_base () |
---|
652 | { |
---|
653 | register unsigned int rules, terminal; |
---|
654 | register struct rule *r; |
---|
655 | |
---|
656 | puts ("\n# Implicit Rules"); |
---|
657 | |
---|
658 | rules = terminal = 0; |
---|
659 | for (r = pattern_rules; r != 0; r = r->next) |
---|
660 | { |
---|
661 | ++rules; |
---|
662 | |
---|
663 | putchar ('\n'); |
---|
664 | print_rule (r); |
---|
665 | |
---|
666 | if (r->terminal) |
---|
667 | ++terminal; |
---|
668 | } |
---|
669 | |
---|
670 | if (rules == 0) |
---|
671 | puts (_("\n# No implicit rules.")); |
---|
672 | else |
---|
673 | { |
---|
674 | printf (_("\n# %u implicit rules, %u"), rules, terminal); |
---|
675 | #ifndef NO_FLOAT |
---|
676 | printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0); |
---|
677 | #else |
---|
678 | { |
---|
679 | int f = (terminal * 1000 + 5) / rules; |
---|
680 | printf (" (%d.%d%%)", f/10, f%10); |
---|
681 | } |
---|
682 | #endif |
---|
683 | puts (_(" terminal.")); |
---|
684 | } |
---|
685 | |
---|
686 | if (num_pattern_rules != rules) |
---|
687 | { |
---|
688 | /* This can happen if a fatal error was detected while reading the |
---|
689 | makefiles and thus count_implicit_rule_limits wasn't called yet. */ |
---|
690 | if (num_pattern_rules != 0) |
---|
691 | fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"), |
---|
692 | num_pattern_rules, rules); |
---|
693 | } |
---|
694 | |
---|
695 | puts (_("\n# Pattern-specific variable values")); |
---|
696 | |
---|
697 | { |
---|
698 | struct pattern_var *p; |
---|
699 | |
---|
700 | rules = 0; |
---|
701 | for (p = pattern_vars; p != 0; p = p->next) |
---|
702 | { |
---|
703 | ++rules; |
---|
704 | |
---|
705 | printf ("\n%s :\n", p->target); |
---|
706 | print_variable_set (p->vars->set, "# "); |
---|
707 | } |
---|
708 | |
---|
709 | if (rules == 0) |
---|
710 | puts (_("\n# No pattern-specific variable values.")); |
---|
711 | else |
---|
712 | { |
---|
713 | printf (_("\n# %u pattern-specific variable values"), rules); |
---|
714 | } |
---|
715 | } |
---|
716 | } |
---|