VirtualBox

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

Last change on this file since 2591 was 2591, checked in by bird, 12 years ago

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

  • Property svn:eol-style set to native
File size: 14.6 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, 2008, 2009,
42010 Free Software Foundation, 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 const char *dname = dep_name (dep);
100 unsigned int len = strlen (dname);
101
102#ifdef VMS
103 const char *p = strrchr (dname, ']');
104 const char *p2;
105 if (p == 0)
106 p = strrchr (dname, ':');
107 p2 = p != 0 ? strchr (dname, '%') : 0;
108#else
109 const char *p = strrchr (dname, '/');
110 const char *p2 = p != 0 ? strchr (dname, '%') : 0;
111#endif
112 ndeps++;
113
114 if (len > max_pattern_dep_length)
115 max_pattern_dep_length = len;
116
117 if (p != 0 && p2 > p)
118 {
119 /* There is a slash before the % in the dep name.
120 Extract the directory name. */
121 if (p == dname)
122 ++p;
123 if (p - dname > namelen)
124 {
125 namelen = p - dname;
126 name = xrealloc (name, namelen + 1);
127 }
128 memcpy (name, dname, p - dname);
129 name[p - dname] = '\0';
130
131 /* In the deps of an implicit rule the `changed' flag
132 actually indicates that the dependency is in a
133 nonexistent subdirectory. */
134
135 dep->changed = !dir_file_exists_p (name, "");
136 }
137 else
138 /* This dependency does not reside in a subdirectory. */
139 dep->changed = 0;
140 }
141
142 if (ndeps > max_pattern_deps)
143 max_pattern_deps = ndeps;
144
145 lastrule = rule;
146 rule = next;
147 }
148
149 if (name != 0)
150 free (name);
151}
152
153
154/* Create a pattern rule from a suffix rule.
155 TARGET is the target suffix; SOURCE is the source suffix.
156 CMDS are the commands.
157 If TARGET is nil, it means the target pattern should be `(%.o)'.
158 If SOURCE is nil, it means there should be no deps. */
159
160static void
161convert_suffix_rule (const char *target, const char *source,
162 struct commands *cmds)
163{
164 const char **names, **percents;
165 struct dep *deps;
166
167 names = xmalloc (sizeof (const char *));
168 percents = xmalloc (sizeof (const char *));
169
170 if (target == 0)
171 {
172 /* Special case: TARGET being nil means we are defining a `.X.a' suffix
173 rule; the target pattern is always `(%.o)'. */
174#ifdef VMS
175 *names = strcache_add_len ("(%.obj)", 7);
176#else
177 *names = strcache_add_len ("(%.o)", 5);
178#endif
179 *percents = *names + 1;
180 }
181 else
182 {
183 /* Construct the target name. */
184 unsigned int len = strlen (target);
185 char *p = alloca (1 + len + 1);
186 p[0] = '%';
187 memcpy (p + 1, target, len + 1);
188 *names = strcache_add_len (p, len + 1);
189 *percents = *names;
190 }
191
192 if (source == 0)
193 deps = 0;
194 else
195 {
196 /* Construct the dependency name. */
197 unsigned int len = strlen (source);
198 char *p = alloca (1 + len + 1);
199 p[0] = '%';
200 memcpy (p + 1, source, len + 1);
201 deps = alloc_dep ();
202 deps->name = strcache_add_len (p, len + 1);
203 }
204
205 create_pattern_rule (names, percents, 1, 0, deps, cmds, 0);
206}
207
208/* Convert old-style suffix rules to pattern rules.
209 All rules for the suffixes on the .SUFFIXES list are converted and added to
210 the chain of pattern rules. */
211
212void
213convert_to_pattern (void)
214{
215 struct dep *d, *d2;
216 char *rulename;
217
218 /* We will compute every potential suffix rule (.x.y) from the list of
219 suffixes in the .SUFFIXES target's dependencies and see if it exists.
220 First find the longest of the suffixes. */
221
222 maxsuffix = 0;
223 for (d = suffix_file->deps; d != 0; d = d->next)
224 {
225 unsigned int l = strlen (dep_name (d));
226 if (l > maxsuffix)
227 maxsuffix = l;
228 }
229
230 /* Space to construct the suffix rule target name. */
231 rulename = alloca ((maxsuffix * 2) + 1);
232
233 for (d = suffix_file->deps; d != 0; d = d->next)
234 {
235 unsigned int slen;
236
237 /* Make a rule that is just the suffix, with no deps or commands.
238 This rule exists solely to disqualify match-anything rules. */
239 convert_suffix_rule (dep_name (d), 0, 0);
240
241 if (d->file->cmds != 0)
242 /* Record a pattern for this suffix's null-suffix rule. */
243 convert_suffix_rule ("", dep_name (d), d->file->cmds);
244
245 /* Add every other suffix to this one and see if it exists as a
246 two-suffix rule. */
247 slen = strlen (dep_name (d));
248 memcpy (rulename, dep_name (d), slen);
249
250 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
251 {
252 struct file *f;
253 unsigned int s2len;
254
255 s2len = strlen (dep_name (d2));
256
257 /* Can't build something from itself. */
258 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
259 continue;
260
261 memcpy (rulename + slen, dep_name (d2), s2len + 1);
262 f = lookup_file (rulename);
263 if (f == 0 || f->cmds == 0)
264 continue;
265
266 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
267 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
268 It also generates a normal `%.a: %.X' rule below. */
269 convert_suffix_rule (NULL, /* Indicates `(%.o)'. */
270 dep_name (d),
271 f->cmds);
272
273 /* The suffix rule `.X.Y:' is converted
274 to the pattern rule `%.Y: %.X'. */
275 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
276 }
277 }
278}
279
280
281/* Install the pattern rule RULE (whose fields have been filled in) at the end
282 of the list (so that any rules previously defined will take precedence).
283 If this rule duplicates a previous one (identical target and dependencies),
284 the old one is replaced if OVERRIDE is nonzero, otherwise this new one is
285 thrown out. When an old rule is replaced, the new one is put at the end of
286 the list. Return nonzero if RULE is used; zero if not. */
287
288static int
289new_pattern_rule (struct rule *rule, int override)
290{
291 struct rule *r, *lastrule;
292 unsigned int i, j;
293
294 rule->in_use = 0;
295 rule->terminal = 0;
296
297 rule->next = 0;
298
299 /* Search for an identical rule. */
300 lastrule = 0;
301 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
302 for (i = 0; i < rule->num; ++i)
303 {
304 for (j = 0; j < r->num; ++j)
305 if (!streq (rule->targets[i], r->targets[j]))
306 break;
307 /* If all the targets matched... */
308 if (j == r->num)
309 {
310 struct dep *d, *d2;
311 for (d = rule->deps, d2 = r->deps;
312 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
313 if (!streq (dep_name (d), dep_name (d2)))
314 break;
315 if (d == 0 && d2 == 0)
316 {
317 /* All the dependencies matched. */
318 if (override)
319 {
320 /* Remove the old rule. */
321 freerule (r, lastrule);
322 /* Install the new one. */
323 if (pattern_rules == 0)
324 pattern_rules = rule;
325 else
326 last_pattern_rule->next = rule;
327 last_pattern_rule = rule;
328
329 /* We got one. Stop looking. */
330 goto matched;
331 }
332 else
333 {
334 /* The old rule stays intact. Destroy the new one. */
335 freerule (rule, (struct rule *) 0);
336 return 0;
337 }
338 }
339 }
340 }
341
342 matched:;
343
344 if (r == 0)
345 {
346 /* There was no rule to replace. */
347 if (pattern_rules == 0)
348 pattern_rules = rule;
349 else
350 last_pattern_rule->next = rule;
351 last_pattern_rule = rule;
352 }
353
354 return 1;
355}
356
357
358/* Install an implicit pattern rule based on the three text strings
359 in the structure P points to. These strings come from one of
360 the arrays of default implicit pattern rules.
361 TERMINAL specifies what the `terminal' field of the rule should be. */
362
363void
364install_pattern_rule (struct pspec *p, int terminal)
365{
366 struct rule *r;
367 char *ptr;
368
369 r = xmalloc (sizeof (struct rule));
370
371 r->num = 1;
372 r->targets = xmalloc (sizeof (const char *));
373 r->suffixes = xmalloc (sizeof (const char *));
374 r->lens = xmalloc (sizeof (unsigned int));
375
376 r->lens[0] = strlen (p->target);
377 r->targets[0] = p->target;
378 r->suffixes[0] = find_percent_cached (&r->targets[0]);
379 assert (r->suffixes[0] != NULL);
380 ++r->suffixes[0];
381
382 ptr = p->dep;
383 r->deps = PARSE_FILE_SEQ (&ptr, struct dep, '\0', NULL, 0);
384
385 if (new_pattern_rule (r, 0))
386 {
387 r->terminal = terminal;
388#ifndef CONFIG_WITH_ALLOC_CACHES
389 r->cmds = xmalloc (sizeof (struct commands));
390#else
391 r->cmds = alloccache_alloc (&commands_cache);
392#endif
393 r->cmds->fileinfo.filenm = 0;
394 r->cmds->fileinfo.lineno = 0;
395 /* These will all be string literals, but we malloc space for them
396 anyway because somebody might want to free them later. */
397 r->cmds->commands = xstrdup (p->commands);
398 r->cmds->command_lines = 0;
399#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
400 r->cmds->refs = 1000;
401#endif
402 }
403}
404
405
406/* Free all the storage used in RULE and take it out of the
407 pattern_rules chain. LASTRULE is the rule whose next pointer
408 points to RULE. */
409
410static void
411freerule (struct rule *rule, struct rule *lastrule)
412{
413 struct rule *next = rule->next;
414
415 free_dep_chain (rule->deps);
416
417 /* MSVC erroneously warns without a cast here. */
418 free ((void *)rule->targets);
419 free ((void *)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#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
482 if (commands != NULL)
483 commands->refs = 1000;
484#endif
485}
486
487
488/* Print the data base of rules. */
489
490static void /* Useful to call from gdb. */
491print_rule (struct rule *r)
492{
493 unsigned int i;
494
495 for (i = 0; i < r->num; ++i)
496 {
497 fputs (r->targets[i], stdout);
498 putchar ((i + 1 == r->num) ? ':' : ' ');
499 }
500 if (r->terminal)
501 putchar (':');
502
503 print_prereqs (r->deps);
504
505 if (r->cmds != 0)
506 print_commands (r->cmds);
507}
508
509void
510print_rule_data_base (void)
511{
512 unsigned int rules, terminal;
513 struct rule *r;
514
515 puts (_("\n# Implicit Rules"));
516
517 rules = terminal = 0;
518 for (r = pattern_rules; r != 0; r = r->next)
519 {
520 ++rules;
521
522 putchar ('\n');
523 print_rule (r);
524
525 if (r->terminal)
526 ++terminal;
527 }
528
529 if (rules == 0)
530 puts (_("\n# No implicit rules."));
531 else
532 {
533 printf (_("\n# %u implicit rules, %u"), rules, terminal);
534#ifndef NO_FLOAT
535 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
536#else
537 {
538 int f = (terminal * 1000 + 5) / rules;
539 printf (" (%d.%d%%)", f/10, f%10);
540 }
541#endif
542 puts (_(" terminal."));
543 }
544
545 if (num_pattern_rules != rules)
546 {
547 /* This can happen if a fatal error was detected while reading the
548 makefiles and thus count_implicit_rule_limits wasn't called yet. */
549 if (num_pattern_rules != 0)
550 fatal (NILF, _("BUG: num_pattern_rules is wrong! %u != %u"),
551 num_pattern_rules, rules);
552 }
553}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use