* Oops. Fix a problem running submakes like $(MAKE) $(MFLAGS).
[make.git] / rule.c
blob4983325fdeb8c5970ffb92029fad966fb3caae09
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 ()
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 = rindex (dep->name, ']');
112 #else
113 char *p = rindex (dep->name, '/');
114 #endif
115 char *p2 = p != 0 ? index (dep->name, '%') : 0;
116 ndeps++;
118 if (len > max_pattern_dep_length)
119 max_pattern_dep_length = len;
121 if (p != 0 && p2 > p)
123 /* There is a slash before the % in the dep name.
124 Extract the directory name. */
125 if (p == dep->name)
126 ++p;
127 if (p - dep->name > namelen)
129 if (name != 0)
130 free (name);
131 namelen = p - dep->name;
132 name = (char *) xmalloc (namelen + 1);
134 bcopy (dep->name, name, p - dep->name);
135 name[p - dep->name] = '\0';
137 /* In the deps of an implicit rule the `changed' flag
138 actually indicates that the dependency is in a
139 nonexistent subdirectory. */
141 dep->changed = !dir_file_exists_p (name, "");
142 #ifdef VMS
143 if (dep->changed && *name == ']')
144 #else
145 if (dep->changed && *name == '/')
146 #endif
148 /* The name is absolute and the directory does not exist.
149 This rule can never possibly match, since this dependency
150 can never possibly exist. So just remove the rule from
151 the list. */
152 freerule (rule, lastrule);
153 --num_pattern_rules;
154 goto end_main_loop;
157 else
158 /* This dependency does not reside in a subdirectory. */
159 dep->changed = 0;
162 if (ndeps > max_pattern_deps)
163 max_pattern_deps = ndeps;
165 lastrule = rule;
166 end_main_loop:
167 rule = next;
170 if (name != 0)
171 free (name);
174 /* Create a pattern rule from a suffix rule.
175 TARGET is the target suffix; SOURCE is the source suffix.
176 CMDS are the commands.
177 If TARGET is nil, it means the target pattern should be `(%.o)'.
178 If SOURCE is nil, it means there should be no deps. */
180 static void
181 convert_suffix_rule (target, source, cmds)
182 char *target, *source;
183 struct commands *cmds;
185 char *targname, *targpercent, *depname;
186 char **names, **percents;
187 struct dep *deps;
188 unsigned int len;
190 if (target == 0)
191 /* Special case: TARGET being nil means we are defining a
192 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
194 #ifdef VMS
195 targname = savestring ("(%.obj)", 7);
196 #else
197 targname = savestring ("(%.o)", 5);
198 #endif
199 targpercent = targname + 1;
201 else
203 /* Construct the target name. */
204 len = strlen (target);
205 targname = xmalloc (1 + len + 1);
206 targname[0] = '%';
207 bcopy (target, targname + 1, len + 1);
208 targpercent = targname;
211 names = (char **) xmalloc (2 * sizeof (char *));
212 percents = (char **) alloca (2 * sizeof (char *));
213 names[0] = targname;
214 percents[0] = targpercent;
215 names[1] = percents[1] = 0;
217 if (source == 0)
218 deps = 0;
219 else
221 /* Construct the dependency name. */
222 len = strlen (source);
223 depname = xmalloc (1 + len + 1);
224 depname[0] = '%';
225 bcopy (source, depname + 1, len + 1);
226 deps = (struct dep *) xmalloc (sizeof (struct dep));
227 deps->next = 0;
228 deps->name = depname;
231 create_pattern_rule (names, percents, 0, deps, cmds, 0);
234 /* Convert old-style suffix rules to pattern rules.
235 All rules for the suffixes on the .SUFFIXES list
236 are converted and added to the chain of pattern rules. */
238 void
239 convert_to_pattern ()
241 register struct dep *d, *d2;
242 register struct file *f;
243 register char *rulename;
244 register unsigned int slen, s2len;
246 /* Compute maximum length of all the suffixes. */
248 maxsuffix = 0;
249 for (d = suffix_file->deps; d != 0; d = d->next)
251 register unsigned int namelen = strlen (dep_name (d));
252 if (namelen > maxsuffix)
253 maxsuffix = namelen;
256 rulename = (char *) alloca ((maxsuffix * 2) + 1);
258 for (d = suffix_file->deps; d != 0; d = d->next)
260 /* Make a rule that is just the suffix, with no deps or commands.
261 This rule exists solely to disqualify match-anything rules. */
262 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
264 f = d->file;
265 if (f->cmds != 0)
266 /* Record a pattern for this suffix's null-suffix rule. */
267 convert_suffix_rule ("", dep_name (d), f->cmds);
269 /* Record a pattern for each of this suffix's two-suffix rules. */
270 slen = strlen (dep_name (d));
271 bcopy (dep_name (d), rulename, slen);
272 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
274 s2len = strlen (dep_name (d2));
276 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
277 continue;
279 bcopy (dep_name (d2), rulename + slen, s2len + 1);
280 f = lookup_file (rulename);
281 if (f == 0 || f->cmds == 0)
282 continue;
284 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
285 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
286 It also generates a normal `%.a: %.X' rule below. */
287 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
288 dep_name (d),
289 f->cmds);
291 /* The suffix rule `.X.Y:' is converted
292 to the pattern rule `%.Y: %.X'. */
293 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
299 /* Install the pattern rule RULE (whose fields have been filled in)
300 at the end of the list (so that any rules previously defined
301 will take precedence). If this rule duplicates a previous one
302 (identical target and dependencies), the old one is replaced
303 if OVERRIDE is nonzero, otherwise this new one is thrown out.
304 When an old rule is replaced, the new one is put at the end of the
305 list. Return nonzero if RULE is used; zero if not. */
308 new_pattern_rule (rule, override)
309 register struct rule *rule;
310 int override;
312 register struct rule *r, *lastrule;
313 register unsigned int i, j;
315 rule->in_use = 0;
316 rule->terminal = 0;
318 rule->next = 0;
320 /* Search for an identical rule. */
321 lastrule = 0;
322 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
323 for (i = 0; rule->targets[i] != 0; ++i)
325 for (j = 0; r->targets[j] != 0; ++j)
326 if (!streq (rule->targets[i], r->targets[j]))
327 break;
328 if (r->targets[j] == 0)
329 /* All the targets matched. */
331 register struct dep *d, *d2;
332 for (d = rule->deps, d2 = r->deps;
333 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
334 if (!streq (dep_name (d), dep_name (d2)))
335 break;
336 if (d == 0 && d2 == 0)
338 /* All the dependencies matched. */
339 if (override)
341 /* Remove the old rule. */
342 freerule (r, lastrule);
343 /* Install the new one. */
344 if (pattern_rules == 0)
345 pattern_rules = rule;
346 else
347 last_pattern_rule->next = rule;
348 last_pattern_rule = rule;
350 /* We got one. Stop looking. */
351 goto matched;
353 else
355 /* The old rule stays intact. Destroy the new one. */
356 freerule (rule, (struct rule *) 0);
357 return 0;
363 matched:;
365 if (r == 0)
367 /* There was no rule to replace. */
368 if (pattern_rules == 0)
369 pattern_rules = rule;
370 else
371 last_pattern_rule->next = rule;
372 last_pattern_rule = rule;
375 return 1;
379 /* Install an implicit pattern rule based on the three text strings
380 in the structure P points to. These strings come from one of
381 the arrays of default implicit pattern rules.
382 TERMINAL specifies what the `terminal' field of the rule should be. */
384 void
385 install_pattern_rule (p, terminal)
386 struct pspec *p;
387 int terminal;
389 register struct rule *r;
390 char *ptr;
392 r = (struct rule *) xmalloc (sizeof (struct rule));
394 r->targets = (char **) xmalloc (2 * sizeof (char *));
395 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
396 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
398 r->targets[1] = 0;
399 r->suffixes[1] = 0;
400 r->lens[1] = 0;
402 r->lens[0] = strlen (p->target);
403 /* These will all be string literals, but we malloc space for
404 them anyway because somebody might want to free them later on. */
405 r->targets[0] = savestring (p->target, r->lens[0]);
406 r->suffixes[0] = find_percent (r->targets[0]);
407 if (r->suffixes[0] == 0)
408 /* Programmer-out-to-lunch error. */
409 abort ();
410 else
411 ++r->suffixes[0];
413 ptr = p->dep;
414 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
415 sizeof (struct dep), 1),
416 sizeof (struct dep));
418 if (new_pattern_rule (r, 0))
420 r->terminal = terminal;
421 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
422 r->cmds->fileinfo.filenm = 0;
423 r->cmds->fileinfo.lineno = 0;
424 /* These will all be string literals, but we malloc space for them
425 anyway because somebody might want to free them later. */
426 r->cmds->commands = xstrdup (p->commands);
427 r->cmds->command_lines = 0;
432 /* Free all the storage used in RULE and take it out of the
433 pattern_rules chain. LASTRULE is the rule whose next pointer
434 points to RULE. */
436 static void
437 freerule (rule, lastrule)
438 register struct rule *rule, *lastrule;
440 struct rule *next = rule->next;
441 register unsigned int i;
442 register struct dep *dep;
444 for (i = 0; rule->targets[i] != 0; ++i)
445 free (rule->targets[i]);
447 dep = rule->deps;
448 while (dep)
450 struct dep *t;
452 t = dep->next;
453 /* We might leak dep->name here, but I'm not sure how to fix this: I
454 think that pointer might be shared (e.g., in the file hash?) */
455 free ((char *) dep);
456 dep = t;
459 free ((char *) rule->targets);
460 free ((char *) rule->suffixes);
461 free ((char *) rule->lens);
463 /* We can't free the storage for the commands because there
464 are ways that they could be in more than one place:
465 * If the commands came from a suffix rule, they could also be in
466 the `struct file's for other suffix rules or plain targets given
467 on the same makefile line.
468 * If two suffixes that together make a two-suffix rule were each
469 given twice in the .SUFFIXES list, and in the proper order, two
470 identical pattern rules would be created and the second one would
471 be discarded here, but both would contain the same `struct commands'
472 pointer from the `struct file' for the suffix rule. */
474 free ((char *) rule);
476 if (pattern_rules == rule)
477 if (lastrule != 0)
478 abort ();
479 else
480 pattern_rules = next;
481 else if (lastrule != 0)
482 lastrule->next = next;
483 if (last_pattern_rule == rule)
484 last_pattern_rule = lastrule;
487 /* Create a new pattern rule with the targets in the nil-terminated
488 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
489 pointers into the elements of TARGETS, where the `%'s are.
490 The new rule has dependencies DEPS and commands from COMMANDS.
491 It is a terminal rule if TERMINAL is nonzero. This rule overrides
492 identical rules with different commands if OVERRIDE is nonzero.
494 The storage for TARGETS and its elements is used and must not be freed
495 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
496 it may be freed. */
498 void
499 create_pattern_rule (targets, target_percents,
500 terminal, deps, commands, override)
501 char **targets, **target_percents;
502 int terminal;
503 struct dep *deps;
504 struct commands *commands;
505 int override;
507 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
508 register unsigned int max_targets, i;
510 r->cmds = commands;
511 r->deps = deps;
512 r->targets = targets;
514 max_targets = 2;
515 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
516 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
517 for (i = 0; targets[i] != 0; ++i)
519 if (i == max_targets - 1)
521 max_targets += 5;
522 r->lens = (unsigned int *)
523 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
524 r->suffixes = (char **)
525 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
527 r->lens[i] = strlen (targets[i]);
528 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
529 : target_percents[i]) + 1;
530 if (r->suffixes[i] == 0)
531 abort ();
534 if (i < max_targets - 1)
536 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
537 (i + 1) * sizeof (unsigned int));
538 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
539 (i + 1) * sizeof (char *));
542 if (new_pattern_rule (r, override))
543 r->terminal = terminal;
546 /* Create a new pattern-specific variable struct. */
548 struct pattern_var *
549 create_pattern_var (target, suffix)
550 char *target, *suffix;
552 register struct pattern_var *p = 0;
553 unsigned int len = strlen(target);
555 /* Look to see if this pattern already exists in the list. */
556 for (p = pattern_vars; p != NULL; p = p->next)
557 if (p->len == len && !strcmp(p->target, target))
558 break;
560 if (p == 0)
562 p = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
563 if (last_pattern_var != 0)
564 last_pattern_var->next = p;
565 else
566 pattern_vars = p;
567 last_pattern_var = p;
568 p->next = 0;
569 p->target = target;
570 p->len = len;
571 p->suffix = suffix + 1;
572 p->vars = create_new_variable_set();
575 return p;
578 /* Look up a target in the pattern-specific variable list. */
580 struct pattern_var *
581 lookup_pattern_var (target)
582 char *target;
584 struct pattern_var *p;
585 unsigned int targlen = strlen(target);
587 for (p = pattern_vars; p != 0; p = p->next)
589 char *stem;
590 unsigned int stemlen;
592 if (p->len > targlen)
593 /* It can't possibly match. */
594 continue;
596 /* From the lengths of the filename and the pattern parts,
597 find the stem: the part of the filename that matches the %. */
598 stem = target + (p->suffix - p->target - 1);
599 stemlen = targlen - p->len + 1;
601 /* Compare the text in the pattern before the stem, if any. */
602 if (stem > target && !strneq (p->target, target, stem - target))
603 continue;
605 /* Compare the text in the pattern after the stem, if any.
606 We could test simply use streq, but this way we compare the
607 first two characters immediately. This saves time in the very
608 common case where the first character matches because it is a
609 period. */
610 if (*p->suffix == stem[stemlen]
611 && (*p->suffix == '\0'|| streq (&p->suffix[1], &stem[stemlen+1])))
612 break;
615 return p;
618 /* Print the data base of rules. */
620 static void /* Useful to call from gdb. */
621 print_rule (r)
622 struct rule *r;
624 register unsigned int i;
625 register struct dep *d;
627 for (i = 0; r->targets[i] != 0; ++i)
629 fputs (r->targets[i], stdout);
630 if (r->targets[i + 1] != 0)
631 putchar (' ');
632 else
633 putchar (':');
635 if (r->terminal)
636 putchar (':');
638 for (d = r->deps; d != 0; d = d->next)
639 printf (" %s", dep_name (d));
640 putchar ('\n');
642 if (r->cmds != 0)
643 print_commands (r->cmds);
646 void
647 print_rule_data_base ()
649 register unsigned int rules, terminal;
650 register struct rule *r;
652 puts ("\n# Implicit Rules");
654 rules = terminal = 0;
655 for (r = pattern_rules; r != 0; r = r->next)
657 ++rules;
659 putchar ('\n');
660 print_rule (r);
662 if (r->terminal)
663 ++terminal;
666 if (rules == 0)
667 puts (_("\n# No implicit rules."));
668 else
670 printf (_("\n# %u implicit rules, %u"), rules, terminal);
671 #ifndef NO_FLOAT
672 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
673 #else
675 int f = (terminal * 1000 + 5) / rules;
676 printf (" (%d.%d%%)", f/10, f%10);
678 #endif
679 puts (_(" terminal."));
682 if (num_pattern_rules != rules)
684 /* This can happen if a fatal error was detected while reading the
685 makefiles and thus count_implicit_rule_limits wasn't called yet. */
686 if (num_pattern_rules != 0)
687 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
688 num_pattern_rules, rules);
691 puts (_("\n# Pattern-specific variable values"));
694 struct pattern_var *p;
696 rules = 0;
697 for (p = pattern_vars; p != 0; p = p->next)
699 ++rules;
701 printf ("\n%s :\n", p->target);
702 print_variable_set (p->vars->set, "# ");
705 if (rules == 0)
706 puts (_("\n# No pattern-specific variable values."));
707 else
709 printf (_("\n# %u pattern-specific variable values"), rules);