entered into RCS
[make.git] / rule.c
blob027aa293b7ea669ace9dac5c194d9b83a6e0110d
1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "commands.h"
21 #include "dep.h"
22 #include "file.h"
23 #include "variable.h"
24 #include "rule.h"
26 static void freerule ();
28 /* Chain of all pattern rules. */
30 struct rule *pattern_rules;
32 /* Pointer to last rule in the chain, so we can add onto the end. */
34 struct rule *last_pattern_rule;
36 /* Number of rules in the chain. */
38 unsigned int num_pattern_rules;
40 /* Maximum number of target patterns of any pattern rule. */
42 unsigned int max_pattern_targets;
44 /* Maximum number of dependencies of any pattern rule. */
46 unsigned int max_pattern_deps;
48 /* Maximum length of the name of a dependencies of any pattern rule. */
50 unsigned int max_pattern_dep_length;
52 /* Pointer to structure for the file .SUFFIXES
53 whose dependencies are the suffixes to be searched. */
55 struct file *suffix_file;
57 /* Maximum length of a suffix. */
59 unsigned int maxsuffix;
61 /* Compute the maximum dependency length and maximum number of
62 dependencies of all implicit rules. Also sets the subdir
63 flag for a rule when appropriate, possibly removing the rule
64 completely when appropriate. */
66 void
67 count_implicit_rule_limits ()
69 char *name;
70 unsigned int namelen;
71 register struct rule *rule, *lastrule;
73 num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
74 max_pattern_dep_length = 0;
76 name = 0;
77 namelen = 0;
78 rule = pattern_rules;
79 lastrule = 0;
80 while (rule != 0)
82 unsigned int ndeps = 0;
83 register struct dep *dep;
84 struct rule *next = rule->next;
85 unsigned int ntargets;
87 ++num_pattern_rules;
89 ntargets = 0;
90 while (rule->targets[ntargets] != 0)
91 ++ntargets;
93 if (ntargets > max_pattern_targets)
94 max_pattern_targets = ntargets;
96 for (dep = rule->deps; dep != 0; dep = dep->next)
98 unsigned int len = strlen (dep->name);
99 char *p = rindex (dep->name, '/');
100 char *p2 = p != 0 ? index (dep->name, '%') : 0;
102 ndeps++;
104 if (len > max_pattern_dep_length)
105 max_pattern_dep_length = len;
107 if (p != 0 && p2 > p)
109 /* There is a slash before the % in the dep name.
110 Extract the directory name. */
111 if (p == dep->name)
112 ++p;
113 if (p - dep->name > namelen)
115 if (name != 0)
116 free (name);
117 namelen = p - dep->name;
118 name = (char *) xmalloc (namelen + 1);
120 bcopy (dep->name, name, p - dep->name);
121 name[p - dep->name] = '\0';
123 /* In the deps of an implicit rule the `changed' flag
124 actually indicates that the dependency is in a
125 nonexistent subdirectory. */
127 dep->changed = !dir_file_exists_p (name, "");
128 if (dep->changed && *name == '/')
130 /* The name is absolute and the directory does not exist.
131 This rule can never possibly match, since this dependency
132 can never possibly exist. So just remove the rule from
133 the list. */
134 freerule (rule, lastrule);
135 --num_pattern_rules;
136 goto end_main_loop;
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 end_main_loop:
149 rule = next;
152 if (name != 0)
153 free (name);
156 /* Create a pattern rule from a suffix rule.
157 TARGET is the target suffix; SOURCE is the source suffix.
158 CMDS are the commands.
159 If TARGET is nil, it means the target pattern should be `(%.o)'.
160 If SOURCE is nil, it means there should be no deps. */
162 static void
163 convert_suffix_rule (target, source, cmds)
164 char *target, *source;
165 struct commands *cmds;
167 char *targname, *depname;
168 char **names, **percents;
169 struct dep *deps;
170 unsigned int len;
172 if (target == 0)
173 /* Special case: TARGET being nil means we are defining a
174 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
175 targname = savestring ("(%.o)", 5);
176 else
178 /* Construct the target name. */
179 len = strlen (target);
180 targname = xmalloc (1 + len + 1);
181 targname[0] = '%';
182 bcopy (target, targname + 1, len + 1);
185 names = (char **) xmalloc (2 * sizeof (char *));
186 percents = (char **) alloca (2 * sizeof (char *));
187 names[0] = percents[0] = targname;
188 names[1] = percents[1] = 0;
190 if (source == 0)
191 deps = 0;
192 else
194 /* Construct the dependency name. */
195 len = strlen (source);
196 depname = xmalloc (1 + len + 1);
197 depname[0] = '%';
198 bcopy (source, depname + 1, len + 1);
199 deps = (struct dep *) xmalloc (sizeof (struct dep));
200 deps->next = 0;
201 deps->name = depname;
204 create_pattern_rule (names, percents, 0, deps, cmds, 0);
207 /* Convert old-style suffix rules to pattern rules.
208 All rules for the suffixes on the .SUFFIXES list
209 are converted and added to the chain of pattern rules. */
211 void
212 convert_to_pattern ()
214 register struct dep *d, *d2;
215 register struct file *f;
216 register char *rulename;
217 register unsigned int slen, s2len;
219 /* Compute maximum length of all the suffixes. */
221 maxsuffix = 0;
222 for (d = suffix_file->deps; d != 0; d = d->next)
224 register unsigned int namelen = strlen (dep_name (d));
225 if (namelen > maxsuffix)
226 maxsuffix = namelen;
229 rulename = (char *) alloca ((maxsuffix * 2) + 1);
231 for (d = suffix_file->deps; d != 0; d = d->next)
233 /* Make a rule that is just the suffix, with no deps or commands.
234 This rule exists solely to disqualify match-anything rules. */
235 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
237 f = d->file;
238 if (f->cmds != 0)
239 /* Record a pattern for this suffix's null-suffix rule. */
240 convert_suffix_rule ("", dep_name (d), f->cmds);
242 /* Record a pattern for each of this suffix's two-suffix rules. */
243 slen = strlen (dep_name (d));
244 bcopy (dep_name (d), rulename, slen);
245 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
247 s2len = strlen (dep_name (d2));
249 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
250 continue;
252 bcopy (dep_name (d2), rulename + slen, s2len + 1);
253 f = lookup_file (rulename);
254 if (f == 0 || f->cmds == 0)
255 continue;
257 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
258 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
259 It also generates a normal `%.a: %.X' rule below. */
260 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
261 dep_name (d),
262 f->cmds);
264 /* The suffix rule `.X.Y:' is converted
265 to the pattern rule `%.Y: %.X'. */
266 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
272 /* Install the pattern rule RULE (whose fields have been filled in)
273 at the end of the list (so that any rules previously defined
274 will take precedence). If this rule duplicates a previous one
275 (identical target and dependencies), the old one is replaced
276 if OVERRIDE is nonzero, otherwise this new one is thrown out.
277 When an old rule is replaced, the new one is put at the end of the
278 list. Return nonzero if RULE is used; zero if not. */
281 new_pattern_rule (rule, override)
282 register struct rule *rule;
283 int override;
285 register struct rule *r, *lastrule;
286 register unsigned int i, j;
288 rule->in_use = 0;
289 rule->terminal = 0;
291 rule->next = 0;
293 /* Search for an identical rule. */
294 lastrule = 0;
295 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
296 for (i = 0; rule->targets[i] != 0; ++i)
298 for (j = 0; r->targets[j] != 0; ++j)
299 if (!streq (rule->targets[i], r->targets[j]))
300 break;
301 if (r->targets[j] == 0)
302 /* All the targets matched. */
304 register struct dep *d, *d2;
305 for (d = rule->deps, d2 = r->deps;
306 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
307 if (!streq (dep_name (d), dep_name (d2)))
308 break;
309 if (d == 0 && d2 == 0)
310 /* All the dependencies matched. */
311 if (override)
313 /* Remove the old rule. */
314 freerule (r, lastrule);
315 /* Install the new one. */
316 if (pattern_rules == 0)
317 pattern_rules = rule;
318 else
319 last_pattern_rule->next = rule;
320 last_pattern_rule = rule;
322 /* We got one. Stop looking. */
323 goto matched;
325 else
327 /* The old rule stays intact. Destroy the new one. */
328 freerule (rule, (struct rule *) 0);
329 return 0;
334 matched:;
336 if (r == 0)
338 /* There was no rule to replace. */
339 if (pattern_rules == 0)
340 pattern_rules = rule;
341 else
342 last_pattern_rule->next = rule;
343 last_pattern_rule = rule;
346 return 1;
350 /* Install an implicit pattern rule based on the three text strings
351 in the structure P points to. These strings come from one of
352 the arrays of default implicit pattern rules.
353 TERMINAL specifies what the `terminal' field of the rule should be. */
355 void
356 install_pattern_rule (p, terminal)
357 struct pspec *p;
358 int terminal;
360 register struct rule *r;
361 char *ptr;
363 r = (struct rule *) xmalloc (sizeof (struct rule));
365 r->targets = (char **) xmalloc (2 * sizeof (char *));
366 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
367 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
369 r->targets[1] = 0;
370 r->suffixes[1] = 0;
371 r->lens[1] = 0;
373 r->lens[0] = strlen (p->target);
374 /* These will all be string literals, but we malloc space for
375 them anyway because somebody might want to free them later on. */
376 r->targets[0] = savestring (p->target, r->lens[0]);
377 r->suffixes[0] = find_percent (r->targets[0]);
378 if (r->suffixes[0] == 0)
379 /* Programmer-out-to-lunch error. */
380 abort ();
381 else
382 ++r->suffixes[0];
384 ptr = p->dep;
385 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
386 sizeof (struct dep), 1),
387 sizeof (struct dep));
389 if (new_pattern_rule (r, 0))
391 r->terminal = terminal;
392 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
393 r->cmds->filename = 0;
394 r->cmds->lineno = 0;
395 /* These will all be string literals, but we malloc space for them
396 anyway because somebody might want to free them later. */
397 r->cmds->commands = savestring (p->commands, strlen (p->commands));
398 r->cmds->command_lines = 0;
403 /* Free all the storage used in RULE and take it out of the
404 pattern_rules chain. LASTRULE is the rule whose next pointer
405 points to RULE. */
407 static void
408 freerule (rule, lastrule)
409 register struct rule *rule, *lastrule;
411 struct rule *next = rule->next;
412 register unsigned int i;
414 for (i = 0; rule->targets[i] != 0; ++i)
415 free (rule->targets[i]);
417 free ((char *) rule->targets);
418 free ((char *) rule->suffixes);
419 free ((char *) rule->lens);
421 /* We can't free the storage for the commands because there
422 are ways that they could be in more than one place:
423 * If the commands came from a suffix rule, they could also be in
424 the `struct file's for other suffix rules or plain targets given
425 on the same makefile line.
426 * If two suffixes that together make a two-suffix rule were each
427 given twice in the .SUFFIXES list, and in the proper order, two
428 identical pattern rules would be created and the second one would
429 be discarded here, but both would contain the same `struct commands'
430 pointer from the `struct file' for the suffix rule. */
432 free ((char *) rule);
434 if (pattern_rules == rule)
435 if (lastrule != 0)
436 abort ();
437 else
438 pattern_rules = next;
439 else if (lastrule != 0)
440 lastrule->next = next;
441 if (last_pattern_rule == rule)
442 last_pattern_rule = lastrule;
445 /* Create a new pattern rule with the targets in the nil-terminated
446 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
447 pointers into the elements of TARGETS, where the `%'s are.
448 The new rule has dependencies DEPS and commands from COMMANDS.
449 It is a terminal rule if TERMINAL is nonzero. This rule overrides
450 identical rules with different commands if OVERRIDE is nonzero.
452 The storage for TARGETS and its elements is used and must not be freed
453 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
454 it may be freed. */
456 void
457 create_pattern_rule (targets, target_percents,
458 terminal, deps, commands, override)
459 char **targets, **target_percents;
460 int terminal;
461 struct dep *deps;
462 struct commands *commands;
463 int override;
465 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
466 register unsigned int max_targets, i;
468 r->cmds = commands;
469 r->deps = deps;
470 r->targets = targets;
472 max_targets = 2;
473 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
474 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
475 for (i = 0; targets[i] != 0; ++i)
477 if (i == max_targets - 1)
479 max_targets += 5;
480 r->lens = (unsigned int *)
481 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
482 r->suffixes = (char **)
483 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
485 r->lens[i] = strlen (targets[i]);
486 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
487 : target_percents[i]) + 1;
488 if (r->suffixes[i] == 0)
489 abort ();
492 if (i < max_targets - 1)
494 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
495 (i + 1) * sizeof (unsigned int));
496 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
497 (i + 1) * sizeof (char *));
500 if (new_pattern_rule (r, override))
501 r->terminal = terminal;
504 /* Print the data base of rules. */
506 void
507 print_rule_data_base ()
509 register unsigned int rules, terminal;
510 register struct rule *r;
511 register struct dep *d;
512 register unsigned int i;
514 puts ("\n# Implicit Rules");
516 rules = terminal = 0;
517 for (r = pattern_rules; r != 0; r = r->next)
519 ++rules;
521 putchar ('\n');
522 for (i = 0; r->targets[i] != 0; ++i)
524 fputs (r->targets[i], stdout);
525 if (r->targets[i + 1] != 0)
526 putchar (' ');
527 else
528 putchar (':');
530 if (r->terminal)
532 ++terminal;
533 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 if (rules == 0)
545 puts ("\n# No implicit rules.");
546 else
548 printf ("\n# %u implicit rules, %u", rules, terminal);
549 #ifndef NO_FLOAT
550 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
551 #endif
552 puts (" terminal.");
555 if (num_pattern_rules != rules)
556 fatal ("BUG: num_pattern_rules wrong! %u != %u",
557 num_pattern_rules, rules);