VirtualBox

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

Last change on this file since 2132 was 2056, checked in by bird, 16 years ago

kmk: some MBs of memory during building by freeing up the chopped up command lines after we're done with them. (Code not perfect, but wtf., it saves me 7 MBs (out of 45), a bunch of faults and turns out to using less cpu time...)

  • Property svn:eol-style set to native
File size: 14.9 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, 2007 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 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
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#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
407 r->cmds->refs = 1000;
408#endif
409 }
410}
411
412
413/* Free all the storage used in RULE and take it out of the
414 pattern_rules chain. LASTRULE is the rule whose next pointer
415 points to RULE. */
416
417static void
418freerule (struct rule *rule, struct rule *lastrule)
419{
420 struct rule *next = rule->next;
421 struct dep *dep;
422
423 dep = rule->deps;
424 while (dep)
425 {
426 struct dep *t = dep->next;
427 free_dep (dep);
428 dep = t;
429 }
430
431 free (rule->targets);
432 free (rule->suffixes);
433 free (rule->lens);
434
435 /* We can't free the storage for the commands because there
436 are ways that they could be in more than one place:
437 * If the commands came from a suffix rule, they could also be in
438 the `struct file's for other suffix rules or plain targets given
439 on the same makefile line.
440 * If two suffixes that together make a two-suffix rule were each
441 given twice in the .SUFFIXES list, and in the proper order, two
442 identical pattern rules would be created and the second one would
443 be discarded here, but both would contain the same `struct commands'
444 pointer from the `struct file' for the suffix rule. */
445
446 free (rule);
447
448 if (pattern_rules == rule)
449 if (lastrule != 0)
450 abort ();
451 else
452 pattern_rules = next;
453 else if (lastrule != 0)
454 lastrule->next = next;
455 if (last_pattern_rule == rule)
456 last_pattern_rule = lastrule;
457}
458
459
460/* Create a new pattern rule with the targets in the nil-terminated array
461 TARGETS. TARGET_PERCENTS is an array of pointers to the % in each element
462 of TARGETS. N is the number of items in the array (not counting the nil
463 element). The new rule has dependencies DEPS and commands from COMMANDS.
464 It is a terminal rule if TERMINAL is nonzero. This rule overrides
465 identical rules with different commands if OVERRIDE is nonzero.
466
467 The storage for TARGETS and its elements and TARGET_PERCENTS is used and
468 must not be freed until the rule is destroyed. */
469
470void
471create_pattern_rule (const char **targets, const char **target_percents,
472 unsigned int n, int terminal, struct dep *deps,
473 struct commands *commands, int override)
474{
475 unsigned int i;
476 struct rule *r = xmalloc (sizeof (struct rule));
477
478 r->num = n;
479 r->cmds = commands;
480 r->deps = deps;
481 r->targets = targets;
482 r->suffixes = target_percents;
483 r->lens = xmalloc (n * sizeof (unsigned int));
484
485 for (i = 0; i < n; ++i)
486 {
487 r->lens[i] = strlen (targets[i]);
488 assert (r->suffixes[i] != NULL);
489 ++r->suffixes[i];
490 }
491
492 if (new_pattern_rule (r, override))
493 r->terminal = terminal;
494#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
495 commands->refs = 1000;
496#endif
497}
498
499
500/* Print the data base of rules. */
501
502static void /* Useful to call from gdb. */
503print_rule (struct rule *r)
504{
505 unsigned int i;
506 struct dep *d;
507
508 for (i = 0; i < r->num; ++i)
509 {
510 fputs (r->targets[i], stdout);
511 putchar ((i + 1 == r->num) ? ':' : ' ');
512 }
513 if (r->terminal)
514 putchar (':');
515
516 for (d = r->deps; d != 0; d = d->next)
517 printf (" %s", dep_name (d));
518 putchar ('\n');
519
520 if (r->cmds != 0)
521 print_commands (r->cmds);
522}
523
524void
525print_rule_data_base (void)
526{
527 unsigned int rules, terminal;
528 struct rule *r;
529
530 puts (_("\n# Implicit Rules"));
531
532 rules = terminal = 0;
533 for (r = pattern_rules; r != 0; r = r->next)
534 {
535 ++rules;
536
537 putchar ('\n');
538 print_rule (r);
539
540 if (r->terminal)
541 ++terminal;
542 }
543
544 if (rules == 0)
545 puts (_("\n# No implicit rules."));
546 else
547 {
548 printf (_("\n# %u implicit rules, %u"), rules, terminal);
549#ifndef NO_FLOAT
550 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
551#else
552 {
553 int f = (terminal * 1000 + 5) / rules;
554 printf (" (%d.%d%%)", f/10, f%10);
555 }
556#endif
557 puts (_(" terminal."));
558 }
559
560 if (num_pattern_rules != rules)
561 {
562 /* This can happen if a fatal error was detected while reading the
563 makefiles and thus count_implicit_rule_limits wasn't called yet. */
564 if (num_pattern_rules != 0)
565 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
566 num_pattern_rules, rules);
567 }
568}
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