Last of the copyright updates.
[make.git] / rule.c
blob527b5b0cf1f8a161edd56c9a7f4ee0e19c5a6905
1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
19 #include "make.h"
20 #include "dep.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
25 #include "rule.h"
27 static void freerule PARAMS ((struct rule *rule, struct rule *lastrule));
29 /* Chain of all pattern rules. */
31 struct rule *pattern_rules;
33 /* Pointer to last rule in the chain, so we can add onto the end. */
35 struct rule *last_pattern_rule;
37 /* Number of rules in the chain. */
39 unsigned int num_pattern_rules;
41 /* Maximum number of target patterns of any pattern rule. */
43 unsigned int max_pattern_targets;
45 /* Maximum number of dependencies of any pattern rule. */
47 unsigned int max_pattern_deps;
49 /* Maximum length of the name of a dependencies of any pattern rule. */
51 unsigned int max_pattern_dep_length;
53 /* Pointer to structure for the file .SUFFIXES
54 whose dependencies are the suffixes to be searched. */
56 struct file *suffix_file;
58 /* Maximum length of a suffix. */
60 unsigned int maxsuffix;
62 /* Compute the maximum dependency length and maximum number of
63 dependencies of all implicit rules. Also sets the subdir
64 flag for a rule when appropriate, possibly removing the rule
65 completely when appropriate. */
67 void
68 count_implicit_rule_limits (void)
70 char *name;
71 int namelen;
72 register struct rule *rule, *lastrule;
74 num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
75 max_pattern_dep_length = 0;
77 name = 0;
78 namelen = 0;
79 rule = pattern_rules;
80 lastrule = 0;
81 while (rule != 0)
83 unsigned int ndeps = 0;
84 register struct dep *dep;
85 struct rule *next = rule->next;
86 unsigned int ntargets;
88 ++num_pattern_rules;
90 ntargets = 0;
91 while (rule->targets[ntargets] != 0)
92 ++ntargets;
94 if (ntargets > max_pattern_targets)
95 max_pattern_targets = ntargets;
97 for (dep = rule->deps; dep != 0; dep = dep->next)
99 unsigned int len = strlen (dep->name);
101 #ifdef VMS
102 char *p = strrchr (dep->name, ']');
103 char *p2;
104 if (p == 0)
105 p = strrchr (dep->name, ':');
106 p2 = p != 0 ? strchr (dep->name, '%') : 0;
107 #else
108 char *p = strrchr (dep->name, '/');
109 char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
110 #endif
111 ndeps++;
113 if (len > max_pattern_dep_length)
114 max_pattern_dep_length = len;
116 if (p != 0 && p2 > p)
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)
124 if (name != 0)
125 free (name);
126 namelen = p - dep->name;
127 name = (char *) xmalloc (namelen + 1);
129 bcopy (dep->name, name, p - dep->name);
130 name[p - dep->name] = '\0';
132 /* In the deps of an implicit rule the `changed' flag
133 actually indicates that the dependency is in a
134 nonexistent subdirectory. */
136 dep->changed = !dir_file_exists_p (name, "");
138 else
139 /* This dependency does not reside in a subdirectory. */
140 dep->changed = 0;
143 if (ndeps > max_pattern_deps)
144 max_pattern_deps = ndeps;
146 lastrule = rule;
147 rule = next;
150 if (name != 0)
151 free (name);
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. */
160 static void
161 convert_suffix_rule (char *target, char *source, struct commands *cmds)
163 char *targname, *targpercent, *depname;
164 char **names, **percents;
165 struct dep *deps;
166 unsigned int len;
168 if (target == 0)
169 /* Special case: TARGET being nil means we are defining a
170 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
172 #ifdef VMS
173 targname = savestring ("(%.obj)", 7);
174 #else
175 targname = savestring ("(%.o)", 5);
176 #endif
177 targpercent = targname + 1;
179 else
181 /* Construct the target name. */
182 len = strlen (target);
183 targname = xmalloc (1 + len + 1);
184 targname[0] = '%';
185 bcopy (target, targname + 1, len + 1);
186 targpercent = targname;
189 names = (char **) xmalloc (2 * sizeof (char *));
190 percents = (char **) alloca (2 * sizeof (char *));
191 names[0] = targname;
192 percents[0] = targpercent;
193 names[1] = percents[1] = 0;
195 if (source == 0)
196 deps = 0;
197 else
199 /* Construct the dependency name. */
200 len = strlen (source);
201 depname = xmalloc (1 + len + 1);
202 depname[0] = '%';
203 bcopy (source, depname + 1, len + 1);
204 deps = (struct dep *) xmalloc (sizeof (struct dep));
205 deps->next = 0;
206 deps->name = depname;
207 deps->ignore_mtime = 0;
208 deps->staticpattern = 0;
209 deps->need_2nd_expansion = 0;
212 create_pattern_rule (names, percents, 0, deps, cmds, 0);
215 /* Convert old-style suffix rules to pattern rules.
216 All rules for the suffixes on the .SUFFIXES list
217 are converted and added to the chain of pattern rules. */
219 void
220 convert_to_pattern (void)
222 register struct dep *d, *d2;
223 register struct file *f;
224 register char *rulename;
225 register unsigned int slen, s2len;
227 /* Compute maximum length of all the suffixes. */
229 maxsuffix = 0;
230 for (d = suffix_file->deps; d != 0; d = d->next)
232 register unsigned int namelen = strlen (dep_name (d));
233 if (namelen > maxsuffix)
234 maxsuffix = namelen;
237 rulename = (char *) alloca ((maxsuffix * 2) + 1);
239 for (d = suffix_file->deps; d != 0; d = d->next)
241 /* Make a rule that is just the suffix, with no deps or commands.
242 This rule exists solely to disqualify match-anything rules. */
243 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
245 f = d->file;
246 if (f->cmds != 0)
247 /* Record a pattern for this suffix's null-suffix rule. */
248 convert_suffix_rule ("", dep_name (d), f->cmds);
250 /* Record a pattern for each of this suffix's two-suffix rules. */
251 slen = strlen (dep_name (d));
252 bcopy (dep_name (d), rulename, slen);
253 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
255 s2len = strlen (dep_name (d2));
257 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
258 continue;
260 bcopy (dep_name (d2), rulename + slen, s2len + 1);
261 f = lookup_file (rulename);
262 if (f == 0 || f->cmds == 0)
263 continue;
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 ((char *) 0, /* Indicates `(%.o)'. */
269 dep_name (d),
270 f->cmds);
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);
280 /* Install the pattern rule RULE (whose fields have been filled in)
281 at the end of the list (so that any rules previously defined
282 will take precedence). If this rule duplicates a previous one
283 (identical target and dependencies), the old one is replaced
284 if OVERRIDE is nonzero, otherwise this new one is thrown out.
285 When an old rule is replaced, the new one is put at the end of the
286 list. Return nonzero if RULE is used; zero if not. */
289 new_pattern_rule (struct rule *rule, int override)
291 register struct rule *r, *lastrule;
292 register unsigned int i, j;
294 rule->in_use = 0;
295 rule->terminal = 0;
297 rule->next = 0;
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; rule->targets[i] != 0; ++i)
304 for (j = 0; r->targets[j] != 0; ++j)
305 if (!streq (rule->targets[i], r->targets[j]))
306 break;
307 if (r->targets[j] == 0)
308 /* All the targets matched. */
310 register 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)
317 /* All the dependencies matched. */
318 if (override)
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;
329 /* We got one. Stop looking. */
330 goto matched;
332 else
334 /* The old rule stays intact. Destroy the new one. */
335 freerule (rule, (struct rule *) 0);
336 return 0;
342 matched:;
344 if (r == 0)
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;
354 return 1;
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. */
363 void
364 install_pattern_rule (struct pspec *p, int terminal)
366 register struct rule *r;
367 char *ptr;
369 r = (struct rule *) xmalloc (sizeof (struct rule));
371 r->targets = (char **) xmalloc (2 * sizeof (char *));
372 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
373 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
375 r->targets[1] = 0;
376 r->suffixes[1] = 0;
377 r->lens[1] = 0;
379 r->lens[0] = strlen (p->target);
380 /* These will all be string literals, but we malloc space for
381 them anyway because somebody might want to free them later on. */
382 r->targets[0] = savestring (p->target, r->lens[0]);
383 r->suffixes[0] = find_percent (r->targets[0]);
384 if (r->suffixes[0] == 0)
385 /* Programmer-out-to-lunch error. */
386 abort ();
387 else
388 ++r->suffixes[0];
390 ptr = p->dep;
391 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
392 sizeof (struct dep), 1),
393 sizeof (struct dep));
395 if (new_pattern_rule (r, 0))
397 r->terminal = terminal;
398 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
399 r->cmds->fileinfo.filenm = 0;
400 r->cmds->fileinfo.lineno = 0;
401 /* These will all be string literals, but we malloc space for them
402 anyway because somebody might want to free them later. */
403 r->cmds->commands = xstrdup (p->commands);
404 r->cmds->command_lines = 0;
409 /* Free all the storage used in RULE and take it out of the
410 pattern_rules chain. LASTRULE is the rule whose next pointer
411 points to RULE. */
413 static void
414 freerule (struct rule *rule, struct rule *lastrule)
416 struct rule *next = rule->next;
417 register unsigned int i;
418 register struct dep *dep;
420 for (i = 0; rule->targets[i] != 0; ++i)
421 free (rule->targets[i]);
423 dep = rule->deps;
424 while (dep)
426 struct dep *t;
428 t = dep->next;
429 /* We might leak dep->name here, but I'm not sure how to fix this: I
430 think that pointer might be shared (e.g., in the file hash?) */
431 free ((char *) dep);
432 dep = t;
435 free ((char *) rule->targets);
436 free ((char *) rule->suffixes);
437 free ((char *) rule->lens);
439 /* We can't free the storage for the commands because there
440 are ways that they could be in more than one place:
441 * If the commands came from a suffix rule, they could also be in
442 the `struct file's for other suffix rules or plain targets given
443 on the same makefile line.
444 * If two suffixes that together make a two-suffix rule were each
445 given twice in the .SUFFIXES list, and in the proper order, two
446 identical pattern rules would be created and the second one would
447 be discarded here, but both would contain the same `struct commands'
448 pointer from the `struct file' for the suffix rule. */
450 free ((char *) rule);
452 if (pattern_rules == rule)
453 if (lastrule != 0)
454 abort ();
455 else
456 pattern_rules = next;
457 else if (lastrule != 0)
458 lastrule->next = next;
459 if (last_pattern_rule == rule)
460 last_pattern_rule = lastrule;
463 /* Create a new pattern rule with the targets in the nil-terminated
464 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
465 pointers into the elements of TARGETS, where the `%'s are.
466 The new rule has dependencies DEPS and commands from COMMANDS.
467 It is a terminal rule if TERMINAL is nonzero. This rule overrides
468 identical rules with different commands if OVERRIDE is nonzero.
470 The storage for TARGETS and its elements is used and must not be freed
471 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
472 it may be freed. */
474 void
475 create_pattern_rule (char **targets, char **target_percents,
476 int terminal, struct dep *deps,
477 struct commands *commands, int override)
479 unsigned int max_targets, i;
480 struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
482 r->cmds = commands;
483 r->deps = deps;
484 r->targets = targets;
486 max_targets = 2;
487 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
488 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
489 for (i = 0; targets[i] != 0; ++i)
491 if (i == max_targets - 1)
493 max_targets += 5;
494 r->lens = (unsigned int *)
495 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
496 r->suffixes = (char **)
497 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
499 r->lens[i] = strlen (targets[i]);
500 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
501 : target_percents[i]) + 1;
502 if (r->suffixes[i] == 0)
503 abort ();
506 if (i < max_targets - 1)
508 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
509 (i + 1) * sizeof (unsigned int));
510 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
511 (i + 1) * sizeof (char *));
514 if (new_pattern_rule (r, override))
515 r->terminal = terminal;
518 /* Print the data base of rules. */
520 static void /* Useful to call from gdb. */
521 print_rule (struct rule *r)
523 register unsigned int i;
524 register struct dep *d;
526 for (i = 0; r->targets[i] != 0; ++i)
528 fputs (r->targets[i], stdout);
529 if (r->targets[i + 1] != 0)
530 putchar (' ');
531 else
532 putchar (':');
534 if (r->terminal)
535 putchar (':');
537 for (d = r->deps; d != 0; d = d->next)
538 printf (" %s", dep_name (d));
539 putchar ('\n');
541 if (r->cmds != 0)
542 print_commands (r->cmds);
545 void
546 print_rule_data_base (void)
548 register unsigned int rules, terminal;
549 register struct rule *r;
551 puts (_("\n# Implicit Rules"));
553 rules = terminal = 0;
554 for (r = pattern_rules; r != 0; r = r->next)
556 ++rules;
558 putchar ('\n');
559 print_rule (r);
561 if (r->terminal)
562 ++terminal;
565 if (rules == 0)
566 puts (_("\n# No implicit rules."));
567 else
569 printf (_("\n# %u implicit rules, %u"), rules, terminal);
570 #ifndef NO_FLOAT
571 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
572 #else
574 int f = (terminal * 1000 + 5) / rules;
575 printf (" (%d.%d%%)", f/10, f%10);
577 #endif
578 puts (_(" terminal."));
581 if (num_pattern_rules != rules)
583 /* This can happen if a fatal error was detected while reading the
584 makefiles and thus count_implicit_rule_limits wasn't called yet. */
585 if (num_pattern_rules != 0)
586 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
587 num_pattern_rules, rules);