Formerly file.c.~24~
[make.git] / rule.c
blob6614dd0151441cd12247a34850d46de137f2473f
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)),
374 sizeof (struct dep),
377 if (new_pattern_rule (r, 0))
379 r->terminal = terminal;
380 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
381 r->cmds->filename = 0;
382 r->cmds->lineno = 0;
383 /* These will all be string literals, but we malloc space for them
384 anyway because somebody might want to free them later. */
385 r->cmds->commands = savestring (p->commands, strlen (p->commands));
386 r->cmds->command_lines = 0;
391 /* Free all the storage used in RULE and take it out of the
392 pattern_rules chain. LASTRULE is the rule whose next pointer
393 points to RULE. */
395 static void
396 freerule (rule, lastrule)
397 register struct rule *rule, *lastrule;
399 struct rule *next = rule->next;
400 register unsigned int i;
402 for (i = 0; rule->targets[i] != 0; ++i)
403 free (rule->targets[i]);
405 free ((char *) rule->targets);
406 free ((char *) rule->suffixes);
407 free ((char *) rule->lens);
409 /* We can't free the storage for the commands because there
410 are ways that they could be in more than one place:
411 * If the commands came from a suffix rule, they could also be in
412 the `struct file's for other suffix rules or plain targets given
413 on the same makefile line.
414 * If two suffixes that together make a two-suffix rule were each
415 given twice in the .SUFFIXES list, and in the proper order, two
416 identical pattern rules would be created and the second one would
417 be discarded here, but both would contain the same `struct commands'
418 pointer from the `struct file' for the suffix rule. */
420 free ((char *) rule);
422 if (pattern_rules == rule)
423 if (lastrule != 0)
424 abort ();
425 else
426 pattern_rules = next;
427 else if (lastrule != 0)
428 lastrule->next = next;
429 if (last_pattern_rule == rule)
430 last_pattern_rule = lastrule;
433 /* Create a new pattern rule with the targets in the nil-terminated
434 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
435 pointers into the elements of TARGETS, where the `%'s are.
436 The new rule has dependencies DEPS and commands from COMMANDS.
437 It is a terminal rule if TERMINAL is nonzero. This rule overrides
438 identical rules with different commands if OVERRIDE is nonzero.
440 The storage for TARGETS and its elements is used and must not be freed
441 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
442 it may be freed. */
444 void
445 create_pattern_rule (targets, target_percents,
446 terminal, deps, commands, override)
447 char **targets, **target_percents;
448 int terminal;
449 struct dep *deps;
450 struct commands *commands;
451 int override;
453 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
454 register unsigned int max_targets, i;
456 r->cmds = commands;
457 r->deps = deps;
458 r->targets = targets;
460 max_targets = 2;
461 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
462 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
463 for (i = 0; targets[i] != 0; ++i)
465 if (i == max_targets - 1)
467 max_targets += 5;
468 r->lens = (unsigned int *)
469 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
470 r->suffixes = (char **)
471 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
473 r->lens[i] = strlen (targets[i]);
474 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
475 : target_percents[i]) + 1;
476 if (r->suffixes[i] == 0)
477 abort ();
480 if (i < max_targets - 1)
482 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
483 (i + 1) * sizeof (unsigned int));
484 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
485 (i + 1) * sizeof (char *));
488 if (new_pattern_rule (r, override))
489 r->terminal = terminal;
492 /* Print the data base of rules. */
494 void
495 print_rule_data_base ()
497 register unsigned int rules, terminal;
498 register struct rule *r;
499 register struct dep *d;
500 register unsigned int i;
502 puts ("\n# Implicit Rules");
504 rules = terminal = 0;
505 for (r = pattern_rules; r != 0; r = r->next)
507 ++rules;
509 putchar ('\n');
510 for (i = 0; r->targets[i] != 0; ++i)
512 fputs (r->targets[i], stdout);
513 if (r->targets[i + 1] != 0)
514 putchar (' ');
515 else
516 putchar (':');
518 if (r->terminal)
520 ++terminal;
521 putchar (':');
524 for (d = r->deps; d != 0; d = d->next)
525 printf (" %s", dep_name (d));
526 putchar ('\n');
528 if (r->cmds != 0)
529 print_commands (r->cmds);
532 if (rules == 0)
533 puts ("\n# No implicit rules.");
534 else
536 printf ("\n# %u implicit rules, %u", rules, terminal);
537 #ifndef NO_FLOAT
538 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
539 #endif
540 puts (" terminal.");
543 if (num_pattern_rules != rules)
544 fatal ("BUG: num_pattern_rules wrong! %u != %u",
545 num_pattern_rules, rules);