VirtualBox

source: kBuild/trunk/src/gmake/rule.c@ 191

Last change on this file since 191 was 154, checked in by bird, 20 years ago

This commit was generated by cvs2svn to compensate for changes in r153,
which included commits to RCS files with non-trunk default branches.

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