Fix some Savannah bugs.
[make.git] / rule.c
blobdd0ebcb612864329b5faec14db7d1f266235724a
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;
211 create_pattern_rule (names, percents, 0, deps, cmds, 0);
214 /* Convert old-style suffix rules to pattern rules.
215 All rules for the suffixes on the .SUFFIXES list
216 are converted and added to the chain of pattern rules. */
218 void
219 convert_to_pattern (void)
221 register struct dep *d, *d2;
222 register struct file *f;
223 register char *rulename;
224 register unsigned int slen, s2len;
226 /* Compute maximum length of all the suffixes. */
228 maxsuffix = 0;
229 for (d = suffix_file->deps; d != 0; d = d->next)
231 register unsigned int namelen = strlen (dep_name (d));
232 if (namelen > maxsuffix)
233 maxsuffix = namelen;
236 rulename = (char *) alloca ((maxsuffix * 2) + 1);
238 for (d = suffix_file->deps; d != 0; d = d->next)
240 /* Make a rule that is just the suffix, with no deps or commands.
241 This rule exists solely to disqualify match-anything rules. */
242 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
244 f = d->file;
245 if (f->cmds != 0)
246 /* Record a pattern for this suffix's null-suffix rule. */
247 convert_suffix_rule ("", dep_name (d), f->cmds);
249 /* Record a pattern for each of this suffix's two-suffix rules. */
250 slen = strlen (dep_name (d));
251 bcopy (dep_name (d), rulename, slen);
252 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
254 s2len = strlen (dep_name (d2));
256 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
257 continue;
259 bcopy (dep_name (d2), rulename + slen, s2len + 1);
260 f = lookup_file (rulename);
261 if (f == 0 || f->cmds == 0)
262 continue;
264 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
265 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
266 It also generates a normal `%.a: %.X' rule below. */
267 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
268 dep_name (d),
269 f->cmds);
271 /* The suffix rule `.X.Y:' is converted
272 to the pattern rule `%.Y: %.X'. */
273 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
279 /* Install the pattern rule RULE (whose fields have been filled in)
280 at the end of the list (so that any rules previously defined
281 will take precedence). If this rule duplicates a previous one
282 (identical target and dependencies), the old one is replaced
283 if OVERRIDE is nonzero, otherwise this new one is thrown out.
284 When an old rule is replaced, the new one is put at the end of the
285 list. Return nonzero if RULE is used; zero if not. */
288 new_pattern_rule (struct rule *rule, int override)
290 register struct rule *r, *lastrule;
291 register unsigned int i, j;
293 rule->in_use = 0;
294 rule->terminal = 0;
296 rule->next = 0;
298 /* Search for an identical rule. */
299 lastrule = 0;
300 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
301 for (i = 0; rule->targets[i] != 0; ++i)
303 for (j = 0; r->targets[j] != 0; ++j)
304 if (!streq (rule->targets[i], r->targets[j]))
305 break;
306 if (r->targets[j] == 0)
307 /* All the targets matched. */
309 register struct dep *d, *d2;
310 for (d = rule->deps, d2 = r->deps;
311 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
312 if (!streq (dep_name (d), dep_name (d2)))
313 break;
314 if (d == 0 && d2 == 0)
316 /* All the dependencies matched. */
317 if (override)
319 /* Remove the old rule. */
320 freerule (r, lastrule);
321 /* Install the new one. */
322 if (pattern_rules == 0)
323 pattern_rules = rule;
324 else
325 last_pattern_rule->next = rule;
326 last_pattern_rule = rule;
328 /* We got one. Stop looking. */
329 goto matched;
331 else
333 /* The old rule stays intact. Destroy the new one. */
334 freerule (rule, (struct rule *) 0);
335 return 0;
341 matched:;
343 if (r == 0)
345 /* There was no rule to replace. */
346 if (pattern_rules == 0)
347 pattern_rules = rule;
348 else
349 last_pattern_rule->next = rule;
350 last_pattern_rule = rule;
353 return 1;
357 /* Install an implicit pattern rule based on the three text strings
358 in the structure P points to. These strings come from one of
359 the arrays of default implicit pattern rules.
360 TERMINAL specifies what the `terminal' field of the rule should be. */
362 void
363 install_pattern_rule (struct pspec *p, int terminal)
365 register struct rule *r;
366 char *ptr;
368 r = (struct rule *) xmalloc (sizeof (struct rule));
370 r->targets = (char **) xmalloc (2 * sizeof (char *));
371 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
372 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
374 r->targets[1] = 0;
375 r->suffixes[1] = 0;
376 r->lens[1] = 0;
378 r->lens[0] = strlen (p->target);
379 /* These will all be string literals, but we malloc space for
380 them anyway because somebody might want to free them later on. */
381 r->targets[0] = savestring (p->target, r->lens[0]);
382 r->suffixes[0] = find_percent (r->targets[0]);
383 if (r->suffixes[0] == 0)
384 /* Programmer-out-to-lunch error. */
385 abort ();
386 else
387 ++r->suffixes[0];
389 ptr = p->dep;
390 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
391 sizeof (struct dep), 1),
392 sizeof (struct dep));
394 if (new_pattern_rule (r, 0))
396 r->terminal = terminal;
397 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
398 r->cmds->fileinfo.filenm = 0;
399 r->cmds->fileinfo.lineno = 0;
400 /* These will all be string literals, but we malloc space for them
401 anyway because somebody might want to free them later. */
402 r->cmds->commands = xstrdup (p->commands);
403 r->cmds->command_lines = 0;
408 /* Free all the storage used in RULE and take it out of the
409 pattern_rules chain. LASTRULE is the rule whose next pointer
410 points to RULE. */
412 static void
413 freerule (struct rule *rule, struct rule *lastrule)
415 struct rule *next = rule->next;
416 register unsigned int i;
417 register struct dep *dep;
419 for (i = 0; rule->targets[i] != 0; ++i)
420 free (rule->targets[i]);
422 dep = rule->deps;
423 while (dep)
425 struct dep *t;
427 t = dep->next;
428 /* We might leak dep->name here, but I'm not sure how to fix this: I
429 think that pointer might be shared (e.g., in the file hash?) */
430 free ((char *) dep);
431 dep = t;
434 free ((char *) rule->targets);
435 free ((char *) rule->suffixes);
436 free ((char *) rule->lens);
438 /* We can't free the storage for the commands because there
439 are ways that they could be in more than one place:
440 * If the commands came from a suffix rule, they could also be in
441 the `struct file's for other suffix rules or plain targets given
442 on the same makefile line.
443 * If two suffixes that together make a two-suffix rule were each
444 given twice in the .SUFFIXES list, and in the proper order, two
445 identical pattern rules would be created and the second one would
446 be discarded here, but both would contain the same `struct commands'
447 pointer from the `struct file' for the suffix rule. */
449 free ((char *) rule);
451 if (pattern_rules == rule)
452 if (lastrule != 0)
453 abort ();
454 else
455 pattern_rules = next;
456 else if (lastrule != 0)
457 lastrule->next = next;
458 if (last_pattern_rule == rule)
459 last_pattern_rule = lastrule;
462 /* Create a new pattern rule with the targets in the nil-terminated
463 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
464 pointers into the elements of TARGETS, where the `%'s are.
465 The new rule has dependencies DEPS and commands from COMMANDS.
466 It is a terminal rule if TERMINAL is nonzero. This rule overrides
467 identical rules with different commands if OVERRIDE is nonzero.
469 The storage for TARGETS and its elements is used and must not be freed
470 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
471 it may be freed. */
473 void
474 create_pattern_rule (char **targets, char **target_percents,
475 int terminal, struct dep *deps,
476 struct commands *commands, int override)
478 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
479 register unsigned int max_targets, i;
481 r->cmds = commands;
482 r->deps = deps;
483 r->targets = targets;
485 max_targets = 2;
486 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
487 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
488 for (i = 0; targets[i] != 0; ++i)
490 if (i == max_targets - 1)
492 max_targets += 5;
493 r->lens = (unsigned int *)
494 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
495 r->suffixes = (char **)
496 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
498 r->lens[i] = strlen (targets[i]);
499 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
500 : target_percents[i]) + 1;
501 if (r->suffixes[i] == 0)
502 abort ();
505 if (i < max_targets - 1)
507 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
508 (i + 1) * sizeof (unsigned int));
509 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
510 (i + 1) * sizeof (char *));
513 if (new_pattern_rule (r, override))
514 r->terminal = terminal;
517 /* Print the data base of rules. */
519 static void /* Useful to call from gdb. */
520 print_rule (struct rule *r)
522 register unsigned int i;
523 register struct dep *d;
525 for (i = 0; r->targets[i] != 0; ++i)
527 fputs (r->targets[i], stdout);
528 if (r->targets[i + 1] != 0)
529 putchar (' ');
530 else
531 putchar (':');
533 if (r->terminal)
534 putchar (':');
536 for (d = r->deps; d != 0; d = d->next)
537 printf (" %s", dep_name (d));
538 putchar ('\n');
540 if (r->cmds != 0)
541 print_commands (r->cmds);
544 void
545 print_rule_data_base (void)
547 register unsigned int rules, terminal;
548 register struct rule *r;
550 puts (_("\n# Implicit Rules"));
552 rules = terminal = 0;
553 for (r = pattern_rules; r != 0; r = r->next)
555 ++rules;
557 putchar ('\n');
558 print_rule (r);
560 if (r->terminal)
561 ++terminal;
564 if (rules == 0)
565 puts (_("\n# No implicit rules."));
566 else
568 printf (_("\n# %u implicit rules, %u"), rules, terminal);
569 #ifndef NO_FLOAT
570 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
571 #else
573 int f = (terminal * 1000 + 5) / rules;
574 printf (" (%d.%d%%)", f/10, f%10);
576 #endif
577 puts (_(" terminal."));
580 if (num_pattern_rules != rules)
582 /* This can happen if a fatal error was detected while reading the
583 makefiles and thus count_implicit_rule_limits wasn't called yet. */
584 if (num_pattern_rules != 0)
585 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
586 num_pattern_rules, rules);