Fix bug #2846.
[make.git] / rule.c
blob1d4a089cfe7b0fe5b2130e5b0f6e08d56f2a9c57
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 /* Chain of all pattern-specific variables. */
56 static struct pattern_var *pattern_vars;
58 /* Pointer to last struct in the chain, so we can add onto the end. */
60 static struct pattern_var *last_pattern_var;
62 /* Pointer to structure for the file .SUFFIXES
63 whose dependencies are the suffixes to be searched. */
65 struct file *suffix_file;
67 /* Maximum length of a suffix. */
69 unsigned int maxsuffix;
71 /* Compute the maximum dependency length and maximum number of
72 dependencies of all implicit rules. Also sets the subdir
73 flag for a rule when appropriate, possibly removing the rule
74 completely when appropriate. */
76 void
77 count_implicit_rule_limits (void)
79 char *name;
80 unsigned int namelen;
81 register struct rule *rule, *lastrule;
83 num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
84 max_pattern_dep_length = 0;
86 name = 0;
87 namelen = 0;
88 rule = pattern_rules;
89 lastrule = 0;
90 while (rule != 0)
92 unsigned int ndeps = 0;
93 register struct dep *dep;
94 struct rule *next = rule->next;
95 unsigned int ntargets;
97 ++num_pattern_rules;
99 ntargets = 0;
100 while (rule->targets[ntargets] != 0)
101 ++ntargets;
103 if (ntargets > max_pattern_targets)
104 max_pattern_targets = ntargets;
106 for (dep = rule->deps; dep != 0; dep = dep->next)
108 unsigned int len = strlen (dep->name);
110 #ifdef VMS
111 char *p = strrchr (dep->name, ']');
112 char *p2;
113 if (p == 0)
114 p = strrchr (dep->name, ':');
115 p2 = p != 0 ? strchr (dep->name, '%') : 0;
116 #else
117 char *p = strrchr (dep->name, '/');
118 char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
119 #endif
120 ndeps++;
122 if (len > max_pattern_dep_length)
123 max_pattern_dep_length = len;
125 if (p != 0 && p2 > p)
127 /* There is a slash before the % in the dep name.
128 Extract the directory name. */
129 if (p == dep->name)
130 ++p;
131 if (p - dep->name > namelen)
133 if (name != 0)
134 free (name);
135 namelen = p - dep->name;
136 name = (char *) xmalloc (namelen + 1);
138 bcopy (dep->name, name, p - dep->name);
139 name[p - dep->name] = '\0';
141 /* In the deps of an implicit rule the `changed' flag
142 actually indicates that the dependency is in a
143 nonexistent subdirectory. */
145 dep->changed = !dir_file_exists_p (name, "");
146 #ifdef VMS
147 if (dep->changed && strchr (name, ':') != 0)
148 #else
149 if (dep->changed && *name == '/')
150 #endif
152 /* The name is absolute and the directory does not exist.
153 This rule can never possibly match, since this dependency
154 can never possibly exist. So just remove the rule from
155 the list. */
156 freerule (rule, lastrule);
157 --num_pattern_rules;
158 goto end_main_loop;
161 else
162 /* This dependency does not reside in a subdirectory. */
163 dep->changed = 0;
166 if (ndeps > max_pattern_deps)
167 max_pattern_deps = ndeps;
169 lastrule = rule;
170 end_main_loop:
171 rule = next;
174 if (name != 0)
175 free (name);
178 /* Create a pattern rule from a suffix rule.
179 TARGET is the target suffix; SOURCE is the source suffix.
180 CMDS are the commands.
181 If TARGET is nil, it means the target pattern should be `(%.o)'.
182 If SOURCE is nil, it means there should be no deps. */
184 static void
185 convert_suffix_rule (char *target, char *source, struct commands *cmds)
187 char *targname, *targpercent, *depname;
188 char **names, **percents;
189 struct dep *deps;
190 unsigned int len;
192 if (target == 0)
193 /* Special case: TARGET being nil means we are defining a
194 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
196 #ifdef VMS
197 targname = savestring ("(%.obj)", 7);
198 #else
199 targname = savestring ("(%.o)", 5);
200 #endif
201 targpercent = targname + 1;
203 else
205 /* Construct the target name. */
206 len = strlen (target);
207 targname = xmalloc (1 + len + 1);
208 targname[0] = '%';
209 bcopy (target, targname + 1, len + 1);
210 targpercent = targname;
213 names = (char **) xmalloc (2 * sizeof (char *));
214 percents = (char **) alloca (2 * sizeof (char *));
215 names[0] = targname;
216 percents[0] = targpercent;
217 names[1] = percents[1] = 0;
219 if (source == 0)
220 deps = 0;
221 else
223 /* Construct the dependency name. */
224 len = strlen (source);
225 depname = xmalloc (1 + len + 1);
226 depname[0] = '%';
227 bcopy (source, depname + 1, len + 1);
228 deps = (struct dep *) xmalloc (sizeof (struct dep));
229 deps->next = 0;
230 deps->name = depname;
231 deps->ignore_mtime = 0;
234 create_pattern_rule (names, percents, 0, deps, cmds, 0);
237 /* Convert old-style suffix rules to pattern rules.
238 All rules for the suffixes on the .SUFFIXES list
239 are converted and added to the chain of pattern rules. */
241 void
242 convert_to_pattern (void)
244 register struct dep *d, *d2;
245 register struct file *f;
246 register char *rulename;
247 register unsigned int slen, s2len;
249 /* Compute maximum length of all the suffixes. */
251 maxsuffix = 0;
252 for (d = suffix_file->deps; d != 0; d = d->next)
254 register unsigned int namelen = strlen (dep_name (d));
255 if (namelen > maxsuffix)
256 maxsuffix = namelen;
259 rulename = (char *) alloca ((maxsuffix * 2) + 1);
261 for (d = suffix_file->deps; d != 0; d = d->next)
263 /* Make a rule that is just the suffix, with no deps or commands.
264 This rule exists solely to disqualify match-anything rules. */
265 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
267 f = d->file;
268 if (f->cmds != 0)
269 /* Record a pattern for this suffix's null-suffix rule. */
270 convert_suffix_rule ("", dep_name (d), f->cmds);
272 /* Record a pattern for each of this suffix's two-suffix rules. */
273 slen = strlen (dep_name (d));
274 bcopy (dep_name (d), rulename, slen);
275 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
277 s2len = strlen (dep_name (d2));
279 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
280 continue;
282 bcopy (dep_name (d2), rulename + slen, s2len + 1);
283 f = lookup_file (rulename);
284 if (f == 0 || f->cmds == 0)
285 continue;
287 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
288 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
289 It also generates a normal `%.a: %.X' rule below. */
290 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
291 dep_name (d),
292 f->cmds);
294 /* The suffix rule `.X.Y:' is converted
295 to the pattern rule `%.Y: %.X'. */
296 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
302 /* Install the pattern rule RULE (whose fields have been filled in)
303 at the end of the list (so that any rules previously defined
304 will take precedence). If this rule duplicates a previous one
305 (identical target and dependencies), the old one is replaced
306 if OVERRIDE is nonzero, otherwise this new one is thrown out.
307 When an old rule is replaced, the new one is put at the end of the
308 list. Return nonzero if RULE is used; zero if not. */
311 new_pattern_rule (struct rule *rule, int override)
313 register struct rule *r, *lastrule;
314 register unsigned int i, j;
316 rule->in_use = 0;
317 rule->terminal = 0;
319 rule->next = 0;
321 /* Search for an identical rule. */
322 lastrule = 0;
323 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
324 for (i = 0; rule->targets[i] != 0; ++i)
326 for (j = 0; r->targets[j] != 0; ++j)
327 if (!streq (rule->targets[i], r->targets[j]))
328 break;
329 if (r->targets[j] == 0)
330 /* All the targets matched. */
332 register struct dep *d, *d2;
333 for (d = rule->deps, d2 = r->deps;
334 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
335 if (!streq (dep_name (d), dep_name (d2)))
336 break;
337 if (d == 0 && d2 == 0)
339 /* All the dependencies matched. */
340 if (override)
342 /* Remove the old rule. */
343 freerule (r, lastrule);
344 /* Install the new one. */
345 if (pattern_rules == 0)
346 pattern_rules = rule;
347 else
348 last_pattern_rule->next = rule;
349 last_pattern_rule = rule;
351 /* We got one. Stop looking. */
352 goto matched;
354 else
356 /* The old rule stays intact. Destroy the new one. */
357 freerule (rule, (struct rule *) 0);
358 return 0;
364 matched:;
366 if (r == 0)
368 /* There was no rule to replace. */
369 if (pattern_rules == 0)
370 pattern_rules = rule;
371 else
372 last_pattern_rule->next = rule;
373 last_pattern_rule = rule;
376 return 1;
380 /* Install an implicit pattern rule based on the three text strings
381 in the structure P points to. These strings come from one of
382 the arrays of default implicit pattern rules.
383 TERMINAL specifies what the `terminal' field of the rule should be. */
385 void
386 install_pattern_rule (struct pspec *p, int terminal)
388 register struct rule *r;
389 char *ptr;
391 r = (struct rule *) xmalloc (sizeof (struct rule));
393 r->targets = (char **) xmalloc (2 * sizeof (char *));
394 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
395 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
397 r->targets[1] = 0;
398 r->suffixes[1] = 0;
399 r->lens[1] = 0;
401 r->lens[0] = strlen (p->target);
402 /* These will all be string literals, but we malloc space for
403 them anyway because somebody might want to free them later on. */
404 r->targets[0] = savestring (p->target, r->lens[0]);
405 r->suffixes[0] = find_percent (r->targets[0]);
406 if (r->suffixes[0] == 0)
407 /* Programmer-out-to-lunch error. */
408 abort ();
409 else
410 ++r->suffixes[0];
412 ptr = p->dep;
413 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
414 sizeof (struct dep), 1),
415 sizeof (struct dep));
417 if (new_pattern_rule (r, 0))
419 r->terminal = terminal;
420 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
421 r->cmds->fileinfo.filenm = 0;
422 r->cmds->fileinfo.lineno = 0;
423 /* These will all be string literals, but we malloc space for them
424 anyway because somebody might want to free them later. */
425 r->cmds->commands = xstrdup (p->commands);
426 r->cmds->command_lines = 0;
431 /* Free all the storage used in RULE and take it out of the
432 pattern_rules chain. LASTRULE is the rule whose next pointer
433 points to RULE. */
435 static void
436 freerule (struct rule *rule, struct rule *lastrule)
438 struct rule *next = rule->next;
439 register unsigned int i;
440 register struct dep *dep;
442 for (i = 0; rule->targets[i] != 0; ++i)
443 free (rule->targets[i]);
445 dep = rule->deps;
446 while (dep)
448 struct dep *t;
450 t = dep->next;
451 /* We might leak dep->name here, but I'm not sure how to fix this: I
452 think that pointer might be shared (e.g., in the file hash?) */
453 free ((char *) dep);
454 dep = t;
457 free ((char *) rule->targets);
458 free ((char *) rule->suffixes);
459 free ((char *) rule->lens);
461 /* We can't free the storage for the commands because there
462 are ways that they could be in more than one place:
463 * If the commands came from a suffix rule, they could also be in
464 the `struct file's for other suffix rules or plain targets given
465 on the same makefile line.
466 * If two suffixes that together make a two-suffix rule were each
467 given twice in the .SUFFIXES list, and in the proper order, two
468 identical pattern rules would be created and the second one would
469 be discarded here, but both would contain the same `struct commands'
470 pointer from the `struct file' for the suffix rule. */
472 free ((char *) rule);
474 if (pattern_rules == rule)
475 if (lastrule != 0)
476 abort ();
477 else
478 pattern_rules = next;
479 else if (lastrule != 0)
480 lastrule->next = next;
481 if (last_pattern_rule == rule)
482 last_pattern_rule = lastrule;
485 /* Create a new pattern rule with the targets in the nil-terminated
486 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
487 pointers into the elements of TARGETS, where the `%'s are.
488 The new rule has dependencies DEPS and commands from COMMANDS.
489 It is a terminal rule if TERMINAL is nonzero. This rule overrides
490 identical rules with different commands if OVERRIDE is nonzero.
492 The storage for TARGETS and its elements is used and must not be freed
493 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
494 it may be freed. */
496 void
497 create_pattern_rule (char **targets, char **target_percents,
498 int terminal, struct dep *deps,
499 struct commands *commands, int override)
501 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
502 register unsigned int max_targets, i;
504 r->cmds = commands;
505 r->deps = deps;
506 r->targets = targets;
508 max_targets = 2;
509 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
510 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
511 for (i = 0; targets[i] != 0; ++i)
513 if (i == max_targets - 1)
515 max_targets += 5;
516 r->lens = (unsigned int *)
517 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
518 r->suffixes = (char **)
519 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
521 r->lens[i] = strlen (targets[i]);
522 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
523 : target_percents[i]) + 1;
524 if (r->suffixes[i] == 0)
525 abort ();
528 if (i < max_targets - 1)
530 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
531 (i + 1) * sizeof (unsigned int));
532 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
533 (i + 1) * sizeof (char *));
536 if (new_pattern_rule (r, override))
537 r->terminal = terminal;
540 /* Create a new pattern-specific variable struct. */
542 struct pattern_var *
543 create_pattern_var (char *target, char *suffix)
545 register struct pattern_var *p = 0;
546 unsigned int len = strlen(target);
548 /* Look to see if this pattern already exists in the list. */
549 for (p = pattern_vars; p != NULL; p = p->next)
550 if (p->len == len && !strcmp(p->target, target))
551 break;
553 if (p == 0)
555 p = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
556 if (last_pattern_var != 0)
557 last_pattern_var->next = p;
558 else
559 pattern_vars = p;
560 last_pattern_var = p;
561 p->next = 0;
562 p->target = target;
563 p->len = len;
564 p->suffix = suffix + 1;
565 p->vars = create_new_variable_set();
568 return p;
571 /* Look up a target in the pattern-specific variable list. */
573 struct pattern_var *
574 lookup_pattern_var (char *target)
576 struct pattern_var *p;
577 unsigned int targlen = strlen(target);
579 for (p = pattern_vars; p != 0; p = p->next)
581 char *stem;
582 unsigned int stemlen;
584 if (p->len > targlen)
585 /* It can't possibly match. */
586 continue;
588 /* From the lengths of the filename and the pattern parts,
589 find the stem: the part of the filename that matches the %. */
590 stem = target + (p->suffix - p->target - 1);
591 stemlen = targlen - p->len + 1;
593 /* Compare the text in the pattern before the stem, if any. */
594 if (stem > target && !strneq (p->target, target, stem - target))
595 continue;
597 /* Compare the text in the pattern after the stem, if any.
598 We could test simply using streq, but this way we compare the
599 first two characters immediately. This saves time in the very
600 common case where the first character matches because it is a
601 period. */
602 if (*p->suffix == stem[stemlen]
603 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
604 break;
607 return p;
610 /* Print the data base of rules. */
612 static void /* Useful to call from gdb. */
613 print_rule (struct rule *r)
615 register unsigned int i;
616 register struct dep *d;
618 for (i = 0; r->targets[i] != 0; ++i)
620 fputs (r->targets[i], stdout);
621 if (r->targets[i + 1] != 0)
622 putchar (' ');
623 else
624 putchar (':');
626 if (r->terminal)
627 putchar (':');
629 for (d = r->deps; d != 0; d = d->next)
630 printf (" %s", dep_name (d));
631 putchar ('\n');
633 if (r->cmds != 0)
634 print_commands (r->cmds);
637 void
638 print_rule_data_base (void)
640 register unsigned int rules, terminal;
641 register struct rule *r;
643 puts (_("\n# Implicit Rules"));
645 rules = terminal = 0;
646 for (r = pattern_rules; r != 0; r = r->next)
648 ++rules;
650 putchar ('\n');
651 print_rule (r);
653 if (r->terminal)
654 ++terminal;
657 if (rules == 0)
658 puts (_("\n# No implicit rules."));
659 else
661 printf (_("\n# %u implicit rules, %u"), rules, terminal);
662 #ifndef NO_FLOAT
663 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
664 #else
666 int f = (terminal * 1000 + 5) / rules;
667 printf (" (%d.%d%%)", f/10, f%10);
669 #endif
670 puts (_(" terminal."));
673 if (num_pattern_rules != rules)
675 /* This can happen if a fatal error was detected while reading the
676 makefiles and thus count_implicit_rule_limits wasn't called yet. */
677 if (num_pattern_rules != 0)
678 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
679 num_pattern_rules, rules);
682 puts (_("\n# Pattern-specific variable values"));
685 struct pattern_var *p;
687 rules = 0;
688 for (p = pattern_vars; p != 0; p = p->next)
690 ++rules;
692 printf ("\n%s :\n", p->target);
693 print_variable_set (p->vars->set, "# ");
696 if (rules == 0)
697 puts (_("\n# No pattern-specific variable values."));
698 else
700 printf (_("\n# %u pattern-specific variable values"), rules);