VirtualBox

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

Last change on this file since 285 was 281, checked in by bird, 20 years ago

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

  • Property svn:eol-style set to native
File size: 15.9 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 }
141 else
142 /* This dependency does not reside in a subdirectory. */
143 dep->changed = 0;
144 }
145
146 if (ndeps > max_pattern_deps)
147 max_pattern_deps = ndeps;
148
149 lastrule = rule;
150 rule = next;
151 }
152
153 if (name != 0)
154 free (name);
155}
156
157
158/* Create a pattern rule from a suffix rule.
159 TARGET is the target suffix; SOURCE is the source suffix.
160 CMDS are the commands.
161 If TARGET is nil, it means the target pattern should be `(%.o)'.
162 If SOURCE is nil, it means there should be no deps. */
163
164static void
165convert_suffix_rule (char *target, char *source, struct commands *cmds)
166{
167 char *targname, *targpercent, *depname;
168 char **names, **percents;
169 struct dep *deps;
170 unsigned int len;
171
172 if (target == 0)
173 /* Special case: TARGET being nil means we are defining a
174 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
175 {
176#ifdef VMS
177 targname = savestring ("(%.obj)", 7);
178#else
179 targname = savestring ("(%.o)", 5);
180#endif
181 targpercent = targname + 1;
182 }
183 else
184 {
185 /* Construct the target name. */
186 len = strlen (target);
187 targname = xmalloc (1 + len + 1);
188 targname[0] = '%';
189 bcopy (target, targname + 1, len + 1);
190 targpercent = targname;
191 }
192
193 names = (char **) xmalloc (2 * sizeof (char *));
194 percents = (char **) alloca (2 * sizeof (char *));
195 names[0] = targname;
196 percents[0] = targpercent;
197 names[1] = percents[1] = 0;
198
199 if (source == 0)
200 deps = 0;
201 else
202 {
203 /* Construct the dependency name. */
204 len = strlen (source);
205 depname = xmalloc (1 + len + 1);
206 depname[0] = '%';
207 bcopy (source, depname + 1, len + 1);
208 deps = (struct dep *) xmalloc (sizeof (struct dep));
209 deps->next = 0;
210 deps->name = depname;
211 deps->ignore_mtime = 0;
212 deps->need_2nd_expansion = 0;
213 }
214
215 create_pattern_rule (names, percents, 0, deps, cmds, 0);
216}
217
218/* Convert old-style suffix rules to pattern rules.
219 All rules for the suffixes on the .SUFFIXES list
220 are converted and added to the chain of pattern rules. */
221
222void
223convert_to_pattern (void)
224{
225 register struct dep *d, *d2;
226 register struct file *f;
227 register char *rulename;
228 register unsigned int slen, s2len;
229
230 /* Compute maximum length of all the suffixes. */
231
232 maxsuffix = 0;
233 for (d = suffix_file->deps; d != 0; d = d->next)
234 {
235 register unsigned int namelen = strlen (dep_name (d));
236 if (namelen > maxsuffix)
237 maxsuffix = namelen;
238 }
239
240 rulename = (char *) alloca ((maxsuffix * 2) + 1);
241
242 for (d = suffix_file->deps; d != 0; d = d->next)
243 {
244 /* Make a rule that is just the suffix, with no deps or commands.
245 This rule exists solely to disqualify match-anything rules. */
246 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
247
248 f = d->file;
249 if (f->cmds != 0)
250 /* Record a pattern for this suffix's null-suffix rule. */
251 convert_suffix_rule ("", dep_name (d), f->cmds);
252
253 /* Record a pattern for each of this suffix's two-suffix rules. */
254 slen = strlen (dep_name (d));
255 bcopy (dep_name (d), rulename, slen);
256 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
257 {
258 s2len = strlen (dep_name (d2));
259
260 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
261 continue;
262
263 bcopy (dep_name (d2), rulename + slen, s2len + 1);
264 f = lookup_file (rulename);
265 if (f == 0 || f->cmds == 0)
266 continue;
267
268 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
269 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
270 It also generates a normal `%.a: %.X' rule below. */
271 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
272 dep_name (d),
273 f->cmds);
274
275 /* The suffix rule `.X.Y:' is converted
276 to the pattern rule `%.Y: %.X'. */
277 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
278 }
279 }
280}
281
282
283/* Install the pattern rule RULE (whose fields have been filled in)
284 at the end of the list (so that any rules previously defined
285 will take precedence). If this rule duplicates a previous one
286 (identical target and dependencies), the old one is replaced
287 if OVERRIDE is nonzero, otherwise this new one is thrown out.
288 When an old rule is replaced, the new one is put at the end of the
289 list. Return nonzero if RULE is used; zero if not. */
290
291int
292new_pattern_rule (struct rule *rule, int override)
293{
294 register struct rule *r, *lastrule;
295 register unsigned int i, j;
296
297 rule->in_use = 0;
298 rule->terminal = 0;
299
300 rule->next = 0;
301
302 /* Search for an identical rule. */
303 lastrule = 0;
304 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
305 for (i = 0; rule->targets[i] != 0; ++i)
306 {
307 for (j = 0; r->targets[j] != 0; ++j)
308 if (!streq (rule->targets[i], r->targets[j]))
309 break;
310 if (r->targets[j] == 0)
311 /* All the targets matched. */
312 {
313 register struct dep *d, *d2;
314 for (d = rule->deps, d2 = r->deps;
315 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
316 if (!streq (dep_name (d), dep_name (d2)))
317 break;
318 if (d == 0 && d2 == 0)
319 {
320 /* All the dependencies matched. */
321 if (override)
322 {
323 /* Remove the old rule. */
324 freerule (r, lastrule);
325 /* Install the new one. */
326 if (pattern_rules == 0)
327 pattern_rules = rule;
328 else
329 last_pattern_rule->next = rule;
330 last_pattern_rule = rule;
331
332 /* We got one. Stop looking. */
333 goto matched;
334 }
335 else
336 {
337 /* The old rule stays intact. Destroy the new one. */
338 freerule (rule, (struct rule *) 0);
339 return 0;
340 }
341 }
342 }
343 }
344
345 matched:;
346
347 if (r == 0)
348 {
349 /* There was no rule to replace. */
350 if (pattern_rules == 0)
351 pattern_rules = rule;
352 else
353 last_pattern_rule->next = rule;
354 last_pattern_rule = rule;
355 }
356
357 return 1;
358}
359
360
361/* Install an implicit pattern rule based on the three text strings
362 in the structure P points to. These strings come from one of
363 the arrays of default implicit pattern rules.
364 TERMINAL specifies what the `terminal' field of the rule should be. */
365
366void
367install_pattern_rule (struct pspec *p, int terminal)
368{
369 register struct rule *r;
370 char *ptr;
371
372 r = (struct rule *) xmalloc (sizeof (struct rule));
373
374 r->targets = (char **) xmalloc (2 * sizeof (char *));
375 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
376 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
377
378 r->targets[1] = 0;
379 r->suffixes[1] = 0;
380 r->lens[1] = 0;
381
382 r->lens[0] = strlen (p->target);
383 /* These will all be string literals, but we malloc space for
384 them anyway because somebody might want to free them later on. */
385 r->targets[0] = savestring (p->target, r->lens[0]);
386 r->suffixes[0] = find_percent (r->targets[0]);
387 if (r->suffixes[0] == 0)
388 /* Programmer-out-to-lunch error. */
389 abort ();
390 else
391 ++r->suffixes[0];
392
393 ptr = p->dep;
394 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
395 sizeof (struct dep), 1),
396 sizeof (struct dep));
397
398 if (new_pattern_rule (r, 0))
399 {
400 r->terminal = terminal;
401 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
402 r->cmds->fileinfo.filenm = 0;
403 r->cmds->fileinfo.lineno = 0;
404 /* These will all be string literals, but we malloc space for them
405 anyway because somebody might want to free them later. */
406 r->cmds->commands = xstrdup (p->commands);
407 r->cmds->command_lines = 0;
408 }
409}
410
411
412/* Free all the storage used in RULE and take it out of the
413 pattern_rules chain. LASTRULE is the rule whose next pointer
414 points to RULE. */
415
416static void
417freerule (struct rule *rule, struct rule *lastrule)
418{
419 struct rule *next = rule->next;
420 register unsigned int i;
421 register struct dep *dep;
422
423 for (i = 0; rule->targets[i] != 0; ++i)
424 free (rule->targets[i]);
425
426 dep = rule->deps;
427 while (dep)
428 {
429 struct dep *t;
430
431 t = dep->next;
432 /* We might leak dep->name here, but I'm not sure how to fix this: I
433 think that pointer might be shared (e.g., in the file hash?) */
434 free ((char *) dep);
435 dep = t;
436 }
437
438 free ((char *) rule->targets);
439 free ((char *) rule->suffixes);
440 free ((char *) rule->lens);
441
442 /* We can't free the storage for the commands because there
443 are ways that they could be in more than one place:
444 * If the commands came from a suffix rule, they could also be in
445 the `struct file's for other suffix rules or plain targets given
446 on the same makefile line.
447 * If two suffixes that together make a two-suffix rule were each
448 given twice in the .SUFFIXES list, and in the proper order, two
449 identical pattern rules would be created and the second one would
450 be discarded here, but both would contain the same `struct commands'
451 pointer from the `struct file' for the suffix rule. */
452
453 free ((char *) rule);
454
455 if (pattern_rules == rule)
456 if (lastrule != 0)
457 abort ();
458 else
459 pattern_rules = next;
460 else if (lastrule != 0)
461 lastrule->next = next;
462 if (last_pattern_rule == rule)
463 last_pattern_rule = lastrule;
464}
465
466
467/* Create a new pattern rule with the targets in the nil-terminated
468 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
469 pointers into the elements of TARGETS, where the `%'s are.
470 The new rule has dependencies DEPS and commands from COMMANDS.
471 It is a terminal rule if TERMINAL is nonzero. This rule overrides
472 identical rules with different commands if OVERRIDE is nonzero.
473
474 The storage for TARGETS and its elements is used and must not be freed
475 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
476 it may be freed. */
477
478void
479create_pattern_rule (char **targets, char **target_percents,
480 int terminal, struct dep *deps,
481 struct commands *commands, int override)
482{
483 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
484 register unsigned int max_targets, i;
485
486 r->cmds = commands;
487 r->deps = deps;
488 r->targets = targets;
489
490 max_targets = 2;
491 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
492 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
493 for (i = 0; targets[i] != 0; ++i)
494 {
495 if (i == max_targets - 1)
496 {
497 max_targets += 5;
498 r->lens = (unsigned int *)
499 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
500 r->suffixes = (char **)
501 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
502 }
503 r->lens[i] = strlen (targets[i]);
504 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
505 : target_percents[i]) + 1;
506 if (r->suffixes[i] == 0)
507 abort ();
508 }
509
510 if (i < max_targets - 1)
511 {
512 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
513 (i + 1) * sizeof (unsigned int));
514 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
515 (i + 1) * sizeof (char *));
516 }
517
518 if (new_pattern_rule (r, override))
519 r->terminal = terminal;
520}
521
522
523/* Print the data base of rules. */
524
525static void /* Useful to call from gdb. */
526print_rule (struct rule *r)
527{
528 register unsigned int i;
529 register struct dep *d;
530
531 for (i = 0; r->targets[i] != 0; ++i)
532 {
533 fputs (r->targets[i], stdout);
534 if (r->targets[i + 1] != 0)
535 putchar (' ');
536 else
537 putchar (':');
538 }
539 if (r->terminal)
540 putchar (':');
541
542 for (d = r->deps; d != 0; d = d->next)
543 printf (" %s", dep_name (d));
544 putchar ('\n');
545
546 if (r->cmds != 0)
547 print_commands (r->cmds);
548}
549
550void
551print_rule_data_base (void)
552{
553 register unsigned int rules, terminal;
554 register struct rule *r;
555
556 puts (_("\n# Implicit Rules"));
557
558 rules = terminal = 0;
559 for (r = pattern_rules; r != 0; r = r->next)
560 {
561 ++rules;
562
563 putchar ('\n');
564 print_rule (r);
565
566 if (r->terminal)
567 ++terminal;
568 }
569
570 if (rules == 0)
571 puts (_("\n# No implicit rules."));
572 else
573 {
574 printf (_("\n# %u implicit rules, %u"), rules, terminal);
575#ifndef NO_FLOAT
576 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
577#else
578 {
579 int f = (terminal * 1000 + 5) / rules;
580 printf (" (%d.%d%%)", f/10, f%10);
581 }
582#endif
583 puts (_(" terminal."));
584 }
585
586 if (num_pattern_rules != rules)
587 {
588 /* This can happen if a fatal error was detected while reading the
589 makefiles and thus count_implicit_rule_limits wasn't called yet. */
590 if (num_pattern_rules != 0)
591 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
592 num_pattern_rules, rules);
593 }
594}
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