Various minor updates and code cleanups.
[make.git] / rule.c
blob152e7e6624d5620ffd5fcad41a50bbfaf505603a
1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93, 1998 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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"
28 static void freerule PARAMS ((struct rule *rule, struct rule *lastrule));
30 /* Chain of all pattern rules. */
32 struct rule *pattern_rules;
34 /* Pointer to last rule in the chain, so we can add onto the end. */
36 struct rule *last_pattern_rule;
38 /* Number of rules in the chain. */
40 unsigned int num_pattern_rules;
42 /* Maximum number of target patterns of any pattern rule. */
44 unsigned int max_pattern_targets;
46 /* Maximum number of dependencies of any pattern rule. */
48 unsigned int max_pattern_deps;
50 /* Maximum length of the name of a dependencies of any pattern rule. */
52 unsigned int max_pattern_dep_length;
54 /* Pointer to structure for the file .SUFFIXES
55 whose dependencies are the suffixes to be searched. */
57 struct file *suffix_file;
59 /* Maximum length of a suffix. */
61 unsigned int maxsuffix;
63 /* Compute the maximum dependency length and maximum number of
64 dependencies of all implicit rules. Also sets the subdir
65 flag for a rule when appropriate, possibly removing the rule
66 completely when appropriate. */
68 void
69 count_implicit_rule_limits (void)
71 char *name;
72 int namelen;
73 register struct rule *rule, *lastrule;
75 num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
76 max_pattern_dep_length = 0;
78 name = 0;
79 namelen = 0;
80 rule = pattern_rules;
81 lastrule = 0;
82 while (rule != 0)
84 unsigned int ndeps = 0;
85 register struct dep *dep;
86 struct rule *next = rule->next;
87 unsigned int ntargets;
89 ++num_pattern_rules;
91 ntargets = 0;
92 while (rule->targets[ntargets] != 0)
93 ++ntargets;
95 if (ntargets > max_pattern_targets)
96 max_pattern_targets = ntargets;
98 for (dep = rule->deps; dep != 0; dep = dep->next)
100 unsigned int len = strlen (dep->name);
102 #ifdef VMS
103 char *p = strrchr (dep->name, ']');
104 char *p2;
105 if (p == 0)
106 p = strrchr (dep->name, ':');
107 p2 = p != 0 ? strchr (dep->name, '%') : 0;
108 #else
109 char *p = strrchr (dep->name, '/');
110 char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
111 #endif
112 ndeps++;
114 if (len > max_pattern_dep_length)
115 max_pattern_dep_length = len;
117 if (p != 0 && p2 > p)
119 /* There is a slash before the % in the dep name.
120 Extract the directory name. */
121 if (p == dep->name)
122 ++p;
123 if (p - dep->name > namelen)
125 if (name != 0)
126 free (name);
127 namelen = p - dep->name;
128 name = (char *) xmalloc (namelen + 1);
130 bcopy (dep->name, name, p - dep->name);
131 name[p - dep->name] = '\0';
133 /* In the deps of an implicit rule the `changed' flag
134 actually indicates that the dependency is in a
135 nonexistent subdirectory. */
137 dep->changed = !dir_file_exists_p (name, "");
139 else
140 /* This dependency does not reside in a subdirectory. */
141 dep->changed = 0;
144 if (ndeps > max_pattern_deps)
145 max_pattern_deps = ndeps;
147 lastrule = rule;
148 rule = next;
151 if (name != 0)
152 free (name);
155 /* Create a pattern rule from a suffix rule.
156 TARGET is the target suffix; SOURCE is the source suffix.
157 CMDS are the commands.
158 If TARGET is nil, it means the target pattern should be `(%.o)'.
159 If SOURCE is nil, it means there should be no deps. */
161 static void
162 convert_suffix_rule (char *target, char *source, struct commands *cmds)
164 char *targname, *targpercent, *depname;
165 char **names, **percents;
166 struct dep *deps;
167 unsigned int len;
169 if (target == 0)
170 /* Special case: TARGET being nil means we are defining a
171 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
173 #ifdef VMS
174 targname = savestring ("(%.obj)", 7);
175 #else
176 targname = savestring ("(%.o)", 5);
177 #endif
178 targpercent = targname + 1;
180 else
182 /* Construct the target name. */
183 len = strlen (target);
184 targname = xmalloc (1 + len + 1);
185 targname[0] = '%';
186 bcopy (target, targname + 1, len + 1);
187 targpercent = targname;
190 names = (char **) xmalloc (2 * sizeof (char *));
191 percents = (char **) alloca (2 * sizeof (char *));
192 names[0] = targname;
193 percents[0] = targpercent;
194 names[1] = percents[1] = 0;
196 if (source == 0)
197 deps = 0;
198 else
200 /* Construct the dependency name. */
201 len = strlen (source);
202 depname = xmalloc (1 + len + 1);
203 depname[0] = '%';
204 bcopy (source, depname + 1, len + 1);
205 deps = (struct dep *) xmalloc (sizeof (struct dep));
206 deps->next = 0;
207 deps->name = depname;
208 deps->ignore_mtime = 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 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
480 register unsigned int max_targets, i;
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);