VirtualBox

source: kBuild/vendor/gnumake/3.82-cvs/rule.c

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

Importing the make-3-82 CVS tag with --auto-props but no keywords.

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