VirtualBox

source: kBuild/vendor/gnumake/2007-05-23/rule.c

Last change on this file was 900, checked in by bird, 17 years ago

Load /home/bird/src/Gnu/make/2007-05-23 into vendor/gnumake/current.

  • Property svn:eol-style set to native
File size: 14.5 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 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
383 sizeof (struct dep), 1),
384 sizeof (struct dep));
385
386 if (new_pattern_rule (r, 0))
387 {
388 r->terminal = terminal;
389 r->cmds = xmalloc (sizeof (struct commands));
390 r->cmds->fileinfo.filenm = 0;
391 r->cmds->fileinfo.lineno = 0;
392 /* These will all be string literals, but we malloc space for them
393 anyway because somebody might want to free them later. */
394 r->cmds->commands = xstrdup (p->commands);
395 r->cmds->command_lines = 0;
396 }
397}
398
399
400/* Free all the storage used in RULE and take it out of the
401 pattern_rules chain. LASTRULE is the rule whose next pointer
402 points to RULE. */
403
404static void
405freerule (struct rule *rule, struct rule *lastrule)
406{
407 struct rule *next = rule->next;
408 struct dep *dep;
409
410 dep = rule->deps;
411 while (dep)
412 {
413 struct dep *t = dep->next;
414 free_dep (dep);
415 dep = t;
416 }
417
418 free (rule->targets);
419 free (rule->suffixes);
420 free (rule->lens);
421
422 /* We can't free the storage for the commands because there
423 are ways that they could be in more than one place:
424 * If the commands came from a suffix rule, they could also be in
425 the `struct file's for other suffix rules or plain targets given
426 on the same makefile line.
427 * If two suffixes that together make a two-suffix rule were each
428 given twice in the .SUFFIXES list, and in the proper order, two
429 identical pattern rules would be created and the second one would
430 be discarded here, but both would contain the same `struct commands'
431 pointer from the `struct file' for the suffix rule. */
432
433 free (rule);
434
435 if (pattern_rules == rule)
436 if (lastrule != 0)
437 abort ();
438 else
439 pattern_rules = next;
440 else if (lastrule != 0)
441 lastrule->next = next;
442 if (last_pattern_rule == rule)
443 last_pattern_rule = lastrule;
444}
445
446
447/* Create a new pattern rule with the targets in the nil-terminated array
448 TARGETS. TARGET_PERCENTS is an array of pointers to the % in each element
449 of TARGETS. N is the number of items in the array (not counting the nil
450 element). The new rule has dependencies DEPS and commands from COMMANDS.
451 It is a terminal rule if TERMINAL is nonzero. This rule overrides
452 identical rules with different commands if OVERRIDE is nonzero.
453
454 The storage for TARGETS and its elements and TARGET_PERCENTS is used and
455 must not be freed until the rule is destroyed. */
456
457void
458create_pattern_rule (const char **targets, const char **target_percents,
459 unsigned int n, int terminal, struct dep *deps,
460 struct commands *commands, int override)
461{
462 unsigned int i;
463 struct rule *r = xmalloc (sizeof (struct rule));
464
465 r->num = n;
466 r->cmds = commands;
467 r->deps = deps;
468 r->targets = targets;
469 r->suffixes = target_percents;
470 r->lens = xmalloc (n * sizeof (unsigned int));
471
472 for (i = 0; i < n; ++i)
473 {
474 r->lens[i] = strlen (targets[i]);
475 assert (r->suffixes[i] != NULL);
476 ++r->suffixes[i];
477 }
478
479 if (new_pattern_rule (r, override))
480 r->terminal = terminal;
481}
482
483
484/* Print the data base of rules. */
485
486static void /* Useful to call from gdb. */
487print_rule (struct rule *r)
488{
489 unsigned int i;
490 struct dep *d;
491
492 for (i = 0; i < r->num; ++i)
493 {
494 fputs (r->targets[i], stdout);
495 putchar ((i + 1 == r->num) ? ':' : ' ');
496 }
497 if (r->terminal)
498 putchar (':');
499
500 for (d = r->deps; d != 0; d = d->next)
501 printf (" %s", dep_name (d));
502 putchar ('\n');
503
504 if (r->cmds != 0)
505 print_commands (r->cmds);
506}
507
508void
509print_rule_data_base (void)
510{
511 unsigned int rules, terminal;
512 struct rule *r;
513
514 puts (_("\n# Implicit Rules"));
515
516 rules = terminal = 0;
517 for (r = pattern_rules; r != 0; r = r->next)
518 {
519 ++rules;
520
521 putchar ('\n');
522 print_rule (r);
523
524 if (r->terminal)
525 ++terminal;
526 }
527
528 if (rules == 0)
529 puts (_("\n# No implicit rules."));
530 else
531 {
532 printf (_("\n# %u implicit rules, %u"), rules, terminal);
533#ifndef NO_FLOAT
534 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
535#else
536 {
537 int f = (terminal * 1000 + 5) / rules;
538 printf (" (%d.%d%%)", f/10, f%10);
539 }
540#endif
541 puts (_(" terminal."));
542 }
543
544 if (num_pattern_rules != rules)
545 {
546 /* This can happen if a fatal error was detected while reading the
547 makefiles and thus count_implicit_rule_limits wasn't called yet. */
548 if (num_pattern_rules != 0)
549 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
550 num_pattern_rules, rules);
551 }
552}
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