VirtualBox

source: kBuild/trunk/src/kmk/rule.c@ 1878

Last change on this file since 1878 was 1867, checked in by bird, 16 years ago

kmk: commands and file allocation caches.

  • Property svn:eol-style set to native
File size: 14.8 KB
Line 
1/* Pattern and suffix rule internals for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "dep.h"
24#include "filedef.h"
25#include "job.h"
26#include "commands.h"
27#include "variable.h"
28#include "rule.h"
29
30static void freerule (struct rule *rule, struct rule *lastrule);
31
32
33/* Chain of all pattern rules. */
34
35struct rule *pattern_rules;
36
37/* Pointer to last rule in the chain, so we can add onto the end. */
38
39struct rule *last_pattern_rule;
40
41/* Number of rules in the chain. */
42
43unsigned int num_pattern_rules;
44
45/* Maximum number of target patterns of any pattern rule. */
46
47unsigned int max_pattern_targets;
48
49/* Maximum number of dependencies of any pattern rule. */
50
51unsigned int max_pattern_deps;
52
53/* Maximum length of the name of a dependencies of any pattern rule. */
54
55unsigned int max_pattern_dep_length;
56
57/* Pointer to structure for the file .SUFFIXES
58 whose dependencies are the suffixes to be searched. */
59
60struct file *suffix_file;
61
62/* Maximum length of a suffix. */
63
64unsigned int maxsuffix;
65
66
67/* Compute the maximum dependency length and maximum number of
68 dependencies of all implicit rules. Also sets the subdir
69 flag for a rule when appropriate, possibly removing the rule
70 completely when appropriate. */
71
72void
73count_implicit_rule_limits (void)
74{
75 char *name;
76 int namelen;
77 struct rule *rule, *lastrule;
78
79 num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
80 max_pattern_dep_length = 0;
81
82 name = 0;
83 namelen = 0;
84 rule = pattern_rules;
85 lastrule = 0;
86 while (rule != 0)
87 {
88 unsigned int ndeps = 0;
89 struct dep *dep;
90 struct rule *next = rule->next;
91
92 ++num_pattern_rules;
93
94 if (rule->num > max_pattern_targets)
95 max_pattern_targets = rule->num;
96
97 for (dep = rule->deps; dep != 0; dep = dep->next)
98 {
99 unsigned int len = strlen (dep->name);
100
101#ifdef VMS
102 const char *p = strrchr (dep->name, ']');
103 const char *p2;
104 if (p == 0)
105 p = strrchr (dep->name, ':');
106 p2 = p != 0 ? strchr (dep->name, '%') : 0;
107#else
108 const char *p = strrchr (dep->name, '/');
109 const char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
110#endif
111 ndeps++;
112
113 if (len > max_pattern_dep_length)
114 max_pattern_dep_length = len;
115
116 if (p != 0 && p2 > p)
117 {
118 /* There is a slash before the % in the dep name.
119 Extract the directory name. */
120 if (p == dep->name)
121 ++p;
122 if (p - dep->name > namelen)
123 {
124 namelen = p - dep->name;
125 name = xrealloc (name, namelen + 1);
126 }
127 memcpy (name, dep->name, p - dep->name);
128 name[p - dep->name] = '\0';
129
130 /* In the deps of an implicit rule the `changed' flag
131 actually indicates that the dependency is in a
132 nonexistent subdirectory. */
133
134 dep->changed = !dir_file_exists_p (name, "");
135 }
136 else
137 /* This dependency does not reside in a subdirectory. */
138 dep->changed = 0;
139 }
140
141 if (ndeps > max_pattern_deps)
142 max_pattern_deps = ndeps;
143
144 lastrule = rule;
145 rule = next;
146 }
147
148 if (name != 0)
149 free (name);
150}
151
152
153/* Create a pattern rule from a suffix rule.
154 TARGET is the target suffix; SOURCE is the source suffix.
155 CMDS are the commands.
156 If TARGET is nil, it means the target pattern should be `(%.o)'.
157 If SOURCE is nil, it means there should be no deps. */
158
159static void
160convert_suffix_rule (const char *target, const char *source,
161 struct commands *cmds)
162{
163 const char **names, **percents;
164 struct dep *deps;
165
166 names = xmalloc (sizeof (const char *));
167 percents = xmalloc (sizeof (const char *));
168
169 if (target == 0)
170 {
171 /* Special case: TARGET being nil means we are defining a `.X.a' suffix
172 rule; the target pattern is always `(%.o)'. */
173#ifdef VMS
174 *names = strcache_add_len ("(%.obj)", 7);
175#else
176 *names = strcache_add_len ("(%.o)", 5);
177#endif
178 *percents = *names + 1;
179 }
180 else
181 {
182 /* Construct the target name. */
183 unsigned int len = strlen (target);
184 char *p = alloca (1 + len + 1);
185 p[0] = '%';
186 memcpy (p + 1, target, len + 1);
187 *names = strcache_add_len (p, len + 1);
188 *percents = *names;
189 }
190
191 if (source == 0)
192 deps = 0;
193 else
194 {
195 /* Construct the dependency name. */
196 unsigned int len = strlen (source);
197 char *p = alloca (1 + len + 1);
198 p[0] = '%';
199 memcpy (p + 1, source, len + 1);
200 deps = alloc_dep ();
201 deps->name = strcache_add_len (p, len + 1);
202 }
203
204 create_pattern_rule (names, percents, 1, 0, deps, cmds, 0);
205}
206
207/* Convert old-style suffix rules to pattern rules.
208 All rules for the suffixes on the .SUFFIXES list are converted and added to
209 the chain of pattern rules. */
210
211void
212convert_to_pattern (void)
213{
214 struct dep *d, *d2;
215 char *rulename;
216
217 /* We will compute every potential suffix rule (.x.y) from the list of
218 suffixes in the .SUFFIXES target's dependencies and see if it exists.
219 First find the longest of the suffixes. */
220
221 maxsuffix = 0;
222 for (d = suffix_file->deps; d != 0; d = d->next)
223 {
224 unsigned int l = strlen (dep_name (d));
225 if (l > maxsuffix)
226 maxsuffix = l;
227 }
228
229 /* Space to construct the suffix rule target name. */
230 rulename = alloca ((maxsuffix * 2) + 1);
231
232 for (d = suffix_file->deps; d != 0; d = d->next)
233 {
234 unsigned int slen;
235
236 /* Make a rule that is just the suffix, with no deps or commands.
237 This rule exists solely to disqualify match-anything rules. */
238 convert_suffix_rule (dep_name (d), 0, 0);
239
240 if (d->file->cmds != 0)
241 /* Record a pattern for this suffix's null-suffix rule. */
242 convert_suffix_rule ("", dep_name (d), d->file->cmds);
243
244 /* Add every other suffix to this one and see if it exists as a
245 two-suffix rule. */
246 slen = strlen (dep_name (d));
247 memcpy (rulename, dep_name (d), slen);
248
249 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
250 {
251 struct file *f;
252 unsigned int s2len;
253
254 s2len = strlen (dep_name (d2));
255
256 /* Can't build something from itself. */
257 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
258 continue;
259
260 memcpy (rulename + slen, dep_name (d2), s2len + 1);
261 f = lookup_file (rulename);
262 if (f == 0 || f->cmds == 0)
263 continue;
264
265 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
266 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
267 It also generates a normal `%.a: %.X' rule below. */
268 convert_suffix_rule (NULL, /* Indicates `(%.o)'. */
269 dep_name (d),
270 f->cmds);
271
272 /* The suffix rule `.X.Y:' is converted
273 to the pattern rule `%.Y: %.X'. */
274 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
275 }
276 }
277}
278
279
280/* Install the pattern rule RULE (whose fields have been filled in) at the end
281 of the list (so that any rules previously defined will take precedence).
282 If this rule duplicates a previous one (identical target and dependencies),
283 the old one is replaced if OVERRIDE is nonzero, otherwise this new one is
284 thrown out. When an old rule is replaced, the new one is put at the end of
285 the list. Return nonzero if RULE is used; zero if not. */
286
287static int
288new_pattern_rule (struct rule *rule, int override)
289{
290 struct rule *r, *lastrule;
291 unsigned int i, j;
292
293 rule->in_use = 0;
294 rule->terminal = 0;
295
296 rule->next = 0;
297
298 /* Search for an identical rule. */
299 lastrule = 0;
300 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
301 for (i = 0; i < rule->num; ++i)
302 {
303 for (j = 0; j < r->num; ++j)
304 if (!streq (rule->targets[i], r->targets[j]))
305 break;
306 /* If all the targets matched... */
307 if (j == r->num)
308 {
309 struct dep *d, *d2;
310 for (d = rule->deps, d2 = r->deps;
311 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
312 if (!streq (dep_name (d), dep_name (d2)))
313 break;
314 if (d == 0 && d2 == 0)
315 {
316 /* All the dependencies matched. */
317 if (override)
318 {
319 /* Remove the old rule. */
320 freerule (r, lastrule);
321 /* Install the new one. */
322 if (pattern_rules == 0)
323 pattern_rules = rule;
324 else
325 last_pattern_rule->next = rule;
326 last_pattern_rule = rule;
327
328 /* We got one. Stop looking. */
329 goto matched;
330 }
331 else
332 {
333 /* The old rule stays intact. Destroy the new one. */
334 freerule (rule, (struct rule *) 0);
335 return 0;
336 }
337 }
338 }
339 }
340
341 matched:;
342
343 if (r == 0)
344 {
345 /* There was no rule to replace. */
346 if (pattern_rules == 0)
347 pattern_rules = rule;
348 else
349 last_pattern_rule->next = rule;
350 last_pattern_rule = rule;
351 }
352
353 return 1;
354}
355
356
357/* Install an implicit pattern rule based on the three text strings
358 in the structure P points to. These strings come from one of
359 the arrays of default implicit pattern rules.
360 TERMINAL specifies what the `terminal' field of the rule should be. */
361
362void
363install_pattern_rule (struct pspec *p, int terminal)
364{
365 struct rule *r;
366 char *ptr;
367
368 r = xmalloc (sizeof (struct rule));
369
370 r->num = 1;
371 r->targets = xmalloc (sizeof (const char *));
372 r->suffixes = xmalloc (sizeof (const char *));
373 r->lens = xmalloc (sizeof (unsigned int));
374
375 r->lens[0] = strlen (p->target);
376 r->targets[0] = p->target;
377 r->suffixes[0] = find_percent_cached (&r->targets[0]);
378 assert (r->suffixes[0] != NULL);
379 ++r->suffixes[0];
380
381 ptr = p->dep;
382#ifndef CONFIG_WITH_ALLOC_CACHES
383 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
384 sizeof (struct dep), 1),
385 sizeof (struct dep));
386#else
387 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
388 &dep_cache, 1),
389 &dep_cache);
390#endif
391
392 if (new_pattern_rule (r, 0))
393 {
394 r->terminal = terminal;
395#ifndef CONFIG_WITH_ALLOC_CACHES
396 r->cmds = xmalloc (sizeof (struct commands));
397#else
398 r->cmds = alloccache_alloc (&commands_cache);
399#endif
400 r->cmds->fileinfo.filenm = 0;
401 r->cmds->fileinfo.lineno = 0;
402 /* These will all be string literals, but we malloc space for them
403 anyway because somebody might want to free them later. */
404 r->cmds->commands = xstrdup (p->commands);
405 r->cmds->command_lines = 0;
406 }
407}
408
409
410/* Free all the storage used in RULE and take it out of the
411 pattern_rules chain. LASTRULE is the rule whose next pointer
412 points to RULE. */
413
414static void
415freerule (struct rule *rule, struct rule *lastrule)
416{
417 struct rule *next = rule->next;
418 struct dep *dep;
419
420 dep = rule->deps;
421 while (dep)
422 {
423 struct dep *t = dep->next;
424 free_dep (dep);
425 dep = t;
426 }
427
428 free (rule->targets);
429 free (rule->suffixes);
430 free (rule->lens);
431
432 /* We can't free the storage for the commands because there
433 are ways that they could be in more than one place:
434 * If the commands came from a suffix rule, they could also be in
435 the `struct file's for other suffix rules or plain targets given
436 on the same makefile line.
437 * If two suffixes that together make a two-suffix rule were each
438 given twice in the .SUFFIXES list, and in the proper order, two
439 identical pattern rules would be created and the second one would
440 be discarded here, but both would contain the same `struct commands'
441 pointer from the `struct file' for the suffix rule. */
442
443 free (rule);
444
445 if (pattern_rules == rule)
446 if (lastrule != 0)
447 abort ();
448 else
449 pattern_rules = next;
450 else if (lastrule != 0)
451 lastrule->next = next;
452 if (last_pattern_rule == rule)
453 last_pattern_rule = lastrule;
454}
455
456
457/* Create a new pattern rule with the targets in the nil-terminated array
458 TARGETS. TARGET_PERCENTS is an array of pointers to the % in each element
459 of TARGETS. N is the number of items in the array (not counting the nil
460 element). The new rule has dependencies DEPS and commands from COMMANDS.
461 It is a terminal rule if TERMINAL is nonzero. This rule overrides
462 identical rules with different commands if OVERRIDE is nonzero.
463
464 The storage for TARGETS and its elements and TARGET_PERCENTS is used and
465 must not be freed until the rule is destroyed. */
466
467void
468create_pattern_rule (const char **targets, const char **target_percents,
469 unsigned int n, int terminal, struct dep *deps,
470 struct commands *commands, int override)
471{
472 unsigned int i;
473 struct rule *r = xmalloc (sizeof (struct rule));
474
475 r->num = n;
476 r->cmds = commands;
477 r->deps = deps;
478 r->targets = targets;
479 r->suffixes = target_percents;
480 r->lens = xmalloc (n * sizeof (unsigned int));
481
482 for (i = 0; i < n; ++i)
483 {
484 r->lens[i] = strlen (targets[i]);
485 assert (r->suffixes[i] != NULL);
486 ++r->suffixes[i];
487 }
488
489 if (new_pattern_rule (r, override))
490 r->terminal = terminal;
491}
492
493
494/* Print the data base of rules. */
495
496static void /* Useful to call from gdb. */
497print_rule (struct rule *r)
498{
499 unsigned int i;
500 struct dep *d;
501
502 for (i = 0; i < r->num; ++i)
503 {
504 fputs (r->targets[i], stdout);
505 putchar ((i + 1 == r->num) ? ':' : ' ');
506 }
507 if (r->terminal)
508 putchar (':');
509
510 for (d = r->deps; d != 0; d = d->next)
511 printf (" %s", dep_name (d));
512 putchar ('\n');
513
514 if (r->cmds != 0)
515 print_commands (r->cmds);
516}
517
518void
519print_rule_data_base (void)
520{
521 unsigned int rules, terminal;
522 struct rule *r;
523
524 puts (_("\n# Implicit Rules"));
525
526 rules = terminal = 0;
527 for (r = pattern_rules; r != 0; r = r->next)
528 {
529 ++rules;
530
531 putchar ('\n');
532 print_rule (r);
533
534 if (r->terminal)
535 ++terminal;
536 }
537
538 if (rules == 0)
539 puts (_("\n# No implicit rules."));
540 else
541 {
542 printf (_("\n# %u implicit rules, %u"), rules, terminal);
543#ifndef NO_FLOAT
544 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
545#else
546 {
547 int f = (terminal * 1000 + 5) / rules;
548 printf (" (%d.%d%%)", f/10, f%10);
549 }
550#endif
551 puts (_(" terminal."));
552 }
553
554 if (num_pattern_rules != rules)
555 {
556 /* This can happen if a fatal error was detected while reading the
557 makefiles and thus count_implicit_rule_limits wasn't called yet. */
558 if (num_pattern_rules != 0)
559 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
560 num_pattern_rules, rules);
561 }
562}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette