Formerly make.texinfo.~98~
[make.git] / rule.c
blob5e2d19080dc12355f98beba107eb8f151032b077
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 dependencies of any pattern rule. */
42 unsigned int max_pattern_deps;
44 /* Maximum length of the name of a dependencies of any pattern rule. */
46 unsigned int max_pattern_dep_length;
48 /* Pointer to structure for the file .SUFFIXES
49 whose dependencies are the suffixes to be searched. */
51 struct file *suffix_file;
53 /* Maximum length of a suffix. */
55 unsigned int maxsuffix;
57 /* Compute the maximum dependency length and maximum number of
58 dependencies of all implicit rules. Also sets the subdir
59 flag for a rule when appropriate, possibly removing the rule
60 completely when appropriate. */
62 void
63 count_implicit_rule_limits ()
65 char *name;
66 unsigned int namelen;
67 register struct rule *rule, *lastrule;
69 num_pattern_rules = 0;
71 name = 0;
72 namelen = 0;
73 rule = pattern_rules;
74 lastrule = 0;
75 while (rule != 0)
77 unsigned int ndeps = 0;
78 register struct dep *dep;
79 struct rule *next = rule->next;
81 ++num_pattern_rules;
83 for (dep = rule->deps; dep != 0; dep = dep->next)
85 unsigned int len = strlen (dep->name);
86 char *p = rindex (dep->name, '/');
87 char *p2 = p != 0 ? index (dep->name, '%') : 0;
89 ndeps++;
91 if (len > max_pattern_dep_length)
92 max_pattern_dep_length = len;
94 if (p != 0 && p2 > p)
96 /* There is a slash before the % in the dep name.
97 Extract the directory name. */
98 if (p == dep->name)
99 ++p;
100 if (p - dep->name > namelen)
102 if (name != 0)
103 free (name);
104 namelen = p - dep->name;
105 name = (char *) xmalloc (namelen + 1);
107 bcopy (dep->name, name, p - dep->name);
108 name[p - dep->name] = '\0';
110 /* In the deps of an implicit rule the `changed' flag
111 actually indicates that the dependency is in a
112 nonexistent subdirectory. */
114 dep->changed = !dir_file_exists_p (name, "");
115 if (dep->changed && *name == '/')
117 /* The name is absolute and the directory does not exist.
118 This rule can never possibly match, since this dependency
119 can never possibly exist. So just remove the rule from
120 the list. */
121 freerule (rule, lastrule);
122 --num_pattern_rules;
123 goto end_main_loop;
126 else
127 /* This dependency does not reside in a subdirectory. */
128 dep->changed = 0;
131 if (ndeps > max_pattern_deps)
132 max_pattern_deps = ndeps;
134 lastrule = rule;
135 end_main_loop:
136 rule = next;
139 if (name != 0)
140 free (name);
143 /* Create a pattern rule from a suffix rule.
144 TARGET is the target suffix; SOURCE is the source suffix.
145 CMDS are the commands.
146 If TARGET is nil, it means the target pattern should be `(%.o)'.
147 If SOURCE is nil, it means there should be no deps. */
149 static void
150 convert_suffix_rule (target, source, cmds)
151 char *target, *source;
152 struct commands *cmds;
154 char *targname, *depname;
155 char **names, **percents;
156 struct dep *deps;
157 unsigned int len;
159 if (target == 0)
160 /* Special case: TARGET being nil means we are defining a
161 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
162 targname = savestring ("(%.o)", 5);
163 else
165 /* Construct the target name. */
166 len = strlen (target);
167 targname = xmalloc (1 + len + 1);
168 targname[0] = '%';
169 bcopy (target, targname + 1, len + 1);
172 names = (char **) xmalloc (2 * sizeof (char *));
173 percents = (char **) xmalloc (2 * sizeof (char *));
174 names[0] = percents[0] = targname;
175 names[1] = percents[1] = 0;
177 if (source == 0)
178 deps = 0;
179 else
181 /* Construct the dependency name. */
182 len = strlen (source);
183 depname = xmalloc (1 + len + 1);
184 depname[0] = '%';
185 bcopy (source, depname + 1, len + 1);
186 deps = (struct dep *) xmalloc (sizeof (struct dep));
187 deps->next = 0;
188 deps->name = depname;
191 create_pattern_rule (names, percents, 0, deps, cmds, 0);
194 /* Convert old-style suffix rules to pattern rules.
195 All rules for the suffixes on the .SUFFIXES list
196 are converted and added to the chain of pattern rules. */
198 void
199 convert_to_pattern ()
201 register struct dep *d, *d2;
202 register struct file *f;
203 register char *rulename;
204 register unsigned int slen, s2len;
206 /* Compute maximum length of all the suffixes. */
208 maxsuffix = 0;
209 for (d = suffix_file->deps; d != 0; d = d->next)
211 register unsigned int namelen = strlen (dep_name (d));
212 if (namelen > maxsuffix)
213 maxsuffix = namelen;
216 rulename = (char *) alloca ((maxsuffix * 2) + 1);
218 for (d = suffix_file->deps; d != 0; d = d->next)
220 /* Make a rule that is just the suffix, with no deps or commands.
221 This rule exists solely to disqualify match-anything rules. */
222 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
224 f = d->file;
225 if (f->cmds != 0)
226 /* Record a pattern for this suffix's null-suffix rule. */
227 convert_suffix_rule ("", dep_name (d), f->cmds);
229 /* Record a pattern for each of this suffix's two-suffix rules. */
230 slen = strlen (dep_name (d));
231 bcopy (dep_name (d), rulename, slen);
232 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
234 s2len = strlen (dep_name (d2));
236 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
237 continue;
239 bcopy (dep_name (d2), rulename + slen, s2len + 1);
240 f = lookup_file (rulename);
241 if (f == 0 || f->cmds == 0)
242 continue;
244 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
245 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
246 It also generates a normal `%.a: %.X' rule below. */
247 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
248 dep_name (d),
249 f->cmds);
251 /* The suffix rule `.X.Y:' is converted
252 to the pattern rule `%.Y: %.X'. */
253 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
259 /* Install the pattern rule RULE (whose fields have been filled in)
260 at the end of the list (so that any rules previously defined
261 will take precedence). If this rule duplicates a previous one
262 (identical target and dependencies), the old one is replaced
263 if OVERRIDE is nonzero, otherwise this new one is thrown out.
264 When an old rule is replaced, the new one is put at the end of the
265 list. Return nonzero if RULE is used; zero if not. */
268 new_pattern_rule (rule, override)
269 register struct rule *rule;
270 int override;
272 register struct rule *r, *lastrule;
273 register unsigned int i, j;
275 rule->in_use = 0;
276 rule->terminal = 0;
278 rule->next = 0;
280 /* Search for an identical rule. */
281 lastrule = 0;
282 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
283 for (i = 0; rule->targets[i] != 0; ++i)
285 for (j = 0; r->targets[j] != 0; ++j)
286 if (!streq (rule->targets[i], r->targets[j]))
287 break;
288 if (r->targets[j] == 0)
289 /* All the targets matched. */
291 register struct dep *d, *d2;
292 for (d = rule->deps, d2 = r->deps;
293 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
294 if (!streq (dep_name (d), dep_name (d2)))
295 break;
296 if (d == 0 && d2 == 0)
297 /* All the dependencies matched. */
298 if (override)
300 /* Remove the old rule. */
301 freerule (r, lastrule);
302 /* Install the new one. */
303 if (pattern_rules == 0)
304 pattern_rules = rule;
305 else
306 last_pattern_rule->next = rule;
307 last_pattern_rule = rule;
309 /* We got one. Stop looking. */
310 goto matched;
312 else
314 /* The old rule stays intact. Destroy the new one. */
315 freerule (rule, (struct rule *) 0);
316 return 0;
321 matched:;
323 if (r == 0)
325 /* There was no rule to replace. */
326 if (pattern_rules == 0)
327 pattern_rules = rule;
328 else
329 last_pattern_rule->next = rule;
330 last_pattern_rule = rule;
333 return 1;
337 /* Install an implicit pattern rule based on the three text strings
338 in the structure P points to. These strings come from one of
339 the arrays of default implicit pattern rules.
340 TERMINAL specifies what the `terminal' field of the rule should be. */
342 void
343 install_pattern_rule (p, terminal)
344 struct pspec *p;
345 int terminal;
347 register struct rule *r;
348 char *ptr;
350 r = (struct rule *) xmalloc (sizeof (struct rule));
352 r->targets = (char **) xmalloc (2 * sizeof (char *));
353 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
354 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
356 r->targets[1] = 0;
357 r->suffixes[1] = 0;
358 r->lens[1] = 0;
360 r->lens[0] = strlen (p->target);
361 /* These will all be string literals, but we malloc space for
362 them anyway because somebody might want to free them later on. */
363 r->targets[0] = savestring (p->target, r->lens[0]);
364 r->suffixes[0] = find_percent (r->targets[0]);
365 if (r->suffixes[0] == 0)
366 /* Programmer-out-to-lunch error. */
367 abort ();
368 else
369 ++r->suffixes[0];
371 ptr = p->dep;
372 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
373 sizeof (struct dep), 1),
374 sizeof (struct dep));
376 if (new_pattern_rule (r, 0))
378 r->terminal = terminal;
379 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
380 r->cmds->filename = 0;
381 r->cmds->lineno = 0;
382 /* These will all be string literals, but we malloc space for them
383 anyway because somebody might want to free them later. */
384 r->cmds->commands = savestring (p->commands, strlen (p->commands));
385 r->cmds->command_lines = 0;
390 /* Free all the storage used in RULE and take it out of the
391 pattern_rules chain. LASTRULE is the rule whose next pointer
392 points to RULE. */
394 static void
395 freerule (rule, lastrule)
396 register struct rule *rule, *lastrule;
398 struct rule *next = rule->next;
399 register unsigned int i;
401 for (i = 0; rule->targets[i] != 0; ++i)
402 free (rule->targets[i]);
404 free ((char *) rule->targets);
405 free ((char *) rule->suffixes);
406 free ((char *) rule->lens);
408 /* We can't free the storage for the commands because there
409 are ways that they could be in more than one place:
410 * If the commands came from a suffix rule, they could also be in
411 the `struct file's for other suffix rules or plain targets given
412 on the same makefile line.
413 * If two suffixes that together make a two-suffix rule were each
414 given twice in the .SUFFIXES list, and in the proper order, two
415 identical pattern rules would be created and the second one would
416 be discarded here, but both would contain the same `struct commands'
417 pointer from the `struct file' for the suffix rule. */
419 free ((char *) rule);
421 if (pattern_rules == rule)
422 if (lastrule != 0)
423 abort ();
424 else
425 pattern_rules = next;
426 else if (lastrule != 0)
427 lastrule->next = next;
428 if (last_pattern_rule == rule)
429 last_pattern_rule = lastrule;
432 /* Create a new pattern rule with the targets in the nil-terminated
433 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
434 pointers into the elements of TARGETS, where the `%'s are.
435 The new rule has dependencies DEPS and commands from COMMANDS.
436 It is a terminal rule if TERMINAL is nonzero. This rule overrides
437 identical rules with different commands if OVERRIDE is nonzero.
439 The storage for TARGETS and its elements is used and must not be freed
440 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
441 it may be freed. */
443 void
444 create_pattern_rule (targets, target_percents,
445 terminal, deps, commands, override)
446 char **targets, **target_percents;
447 int terminal;
448 struct dep *deps;
449 struct commands *commands;
450 int override;
452 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
453 register unsigned int max_targets, i;
455 r->cmds = commands;
456 r->deps = deps;
457 r->targets = targets;
459 max_targets = 2;
460 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
461 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
462 for (i = 0; targets[i] != 0; ++i)
464 if (i == max_targets - 1)
466 max_targets += 5;
467 r->lens = (unsigned int *)
468 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
469 r->suffixes = (char **)
470 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
472 r->lens[i] = strlen (targets[i]);
473 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
474 : target_percents[i]) + 1;
475 if (r->suffixes[i] == 0)
476 abort ();
479 if (i < max_targets - 1)
481 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
482 (i + 1) * sizeof (unsigned int));
483 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
484 (i + 1) * sizeof (char *));
487 if (new_pattern_rule (r, override))
488 r->terminal = terminal;
491 /* Print the data base of rules. */
493 void
494 print_rule_data_base ()
496 register unsigned int rules, terminal;
497 register struct rule *r;
498 register struct dep *d;
499 register unsigned int i;
501 puts ("\n# Implicit Rules");
503 rules = terminal = 0;
504 for (r = pattern_rules; r != 0; r = r->next)
506 ++rules;
508 putchar ('\n');
509 for (i = 0; r->targets[i] != 0; ++i)
511 fputs (r->targets[i], stdout);
512 if (r->targets[i + 1] != 0)
513 putchar (' ');
514 else
515 putchar (':');
517 if (r->terminal)
519 ++terminal;
520 putchar (':');
523 for (d = r->deps; d != 0; d = d->next)
524 printf (" %s", dep_name (d));
525 putchar ('\n');
527 if (r->cmds != 0)
528 print_commands (r->cmds);
531 if (rules == 0)
532 puts ("\n# No implicit rules.");
533 else
535 printf ("\n# %u implicit rules, %u", rules, terminal);
536 #ifndef NO_FLOAT
537 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
538 #endif
539 puts (" terminal.");
542 if (num_pattern_rules != rules)
543 fatal ("BUG: num_pattern_rules wrong! %u != %u",
544 num_pattern_rules, rules);