More maintainer rules.
[make.git] / rule.c
blob322ed23ded1878574b511ae93f07ae3b11ef8bdb
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 /* Pointer to structure for the file .SUFFIXES
55 whose dependencies are the suffixes to be searched. */
57 struct file *suffix_file;
59 /* Maximum length of a suffix. */
61 unsigned int maxsuffix;
63 /* Compute the maximum dependency length and maximum number of
64 dependencies of all implicit rules. Also sets the subdir
65 flag for a rule when appropriate, possibly removing the rule
66 completely when appropriate. */
68 void
69 count_implicit_rule_limits (void)
71 char *name;
72 int namelen;
73 register struct rule *rule, *lastrule;
75 num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
76 max_pattern_dep_length = 0;
78 name = 0;
79 namelen = 0;
80 rule = pattern_rules;
81 lastrule = 0;
82 while (rule != 0)
84 unsigned int ndeps = 0;
85 register struct dep *dep;
86 struct rule *next = rule->next;
87 unsigned int ntargets;
89 ++num_pattern_rules;
91 ntargets = 0;
92 while (rule->targets[ntargets] != 0)
93 ++ntargets;
95 if (ntargets > max_pattern_targets)
96 max_pattern_targets = ntargets;
98 for (dep = rule->deps; dep != 0; dep = dep->next)
100 unsigned int len = strlen (dep->name);
102 #ifdef VMS
103 char *p = strrchr (dep->name, ']');
104 char *p2;
105 if (p == 0)
106 p = strrchr (dep->name, ':');
107 p2 = p != 0 ? strchr (dep->name, '%') : 0;
108 #else
109 char *p = strrchr (dep->name, '/');
110 char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
111 #endif
112 ndeps++;
114 if (len > max_pattern_dep_length)
115 max_pattern_dep_length = len;
117 if (p != 0 && p2 > p)
119 /* There is a slash before the % in the dep name.
120 Extract the directory name. */
121 if (p == dep->name)
122 ++p;
123 if (p - dep->name > namelen)
125 if (name != 0)
126 free (name);
127 namelen = p - dep->name;
128 name = (char *) xmalloc (namelen + 1);
130 bcopy (dep->name, name, p - dep->name);
131 name[p - dep->name] = '\0';
133 /* In the deps of an implicit rule the `changed' flag
134 actually indicates that the dependency is in a
135 nonexistent subdirectory. */
137 dep->changed = !dir_file_exists_p (name, "");
138 #ifdef VMS
139 if (dep->changed && strchr (name, ':') != 0)
140 #else
141 if (dep->changed && *name == '/')
142 #endif
144 /* The name is absolute and the directory does not exist.
145 This rule can never possibly match, since this dependency
146 can never possibly exist. So just remove the rule from
147 the list. */
148 freerule (rule, lastrule);
149 --num_pattern_rules;
150 goto end_main_loop;
153 else
154 /* This dependency does not reside in a subdirectory. */
155 dep->changed = 0;
158 if (ndeps > max_pattern_deps)
159 max_pattern_deps = ndeps;
161 lastrule = rule;
162 end_main_loop:
163 rule = next;
166 if (name != 0)
167 free (name);
170 /* Create a pattern rule from a suffix rule.
171 TARGET is the target suffix; SOURCE is the source suffix.
172 CMDS are the commands.
173 If TARGET is nil, it means the target pattern should be `(%.o)'.
174 If SOURCE is nil, it means there should be no deps. */
176 static void
177 convert_suffix_rule (char *target, char *source, struct commands *cmds)
179 char *targname, *targpercent, *depname;
180 char **names, **percents;
181 struct dep *deps;
182 unsigned int len;
184 if (target == 0)
185 /* Special case: TARGET being nil means we are defining a
186 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
188 #ifdef VMS
189 targname = savestring ("(%.obj)", 7);
190 #else
191 targname = savestring ("(%.o)", 5);
192 #endif
193 targpercent = targname + 1;
195 else
197 /* Construct the target name. */
198 len = strlen (target);
199 targname = xmalloc (1 + len + 1);
200 targname[0] = '%';
201 bcopy (target, targname + 1, len + 1);
202 targpercent = targname;
205 names = (char **) xmalloc (2 * sizeof (char *));
206 percents = (char **) alloca (2 * sizeof (char *));
207 names[0] = targname;
208 percents[0] = targpercent;
209 names[1] = percents[1] = 0;
211 if (source == 0)
212 deps = 0;
213 else
215 /* Construct the dependency name. */
216 len = strlen (source);
217 depname = xmalloc (1 + len + 1);
218 depname[0] = '%';
219 bcopy (source, depname + 1, len + 1);
220 deps = (struct dep *) xmalloc (sizeof (struct dep));
221 deps->next = 0;
222 deps->name = depname;
223 deps->ignore_mtime = 0;
226 create_pattern_rule (names, percents, 0, deps, cmds, 0);
229 /* Convert old-style suffix rules to pattern rules.
230 All rules for the suffixes on the .SUFFIXES list
231 are converted and added to the chain of pattern rules. */
233 void
234 convert_to_pattern (void)
236 register struct dep *d, *d2;
237 register struct file *f;
238 register char *rulename;
239 register unsigned int slen, s2len;
241 /* Compute maximum length of all the suffixes. */
243 maxsuffix = 0;
244 for (d = suffix_file->deps; d != 0; d = d->next)
246 register unsigned int namelen = strlen (dep_name (d));
247 if (namelen > maxsuffix)
248 maxsuffix = namelen;
251 rulename = (char *) alloca ((maxsuffix * 2) + 1);
253 for (d = suffix_file->deps; d != 0; d = d->next)
255 /* Make a rule that is just the suffix, with no deps or commands.
256 This rule exists solely to disqualify match-anything rules. */
257 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
259 f = d->file;
260 if (f->cmds != 0)
261 /* Record a pattern for this suffix's null-suffix rule. */
262 convert_suffix_rule ("", dep_name (d), f->cmds);
264 /* Record a pattern for each of this suffix's two-suffix rules. */
265 slen = strlen (dep_name (d));
266 bcopy (dep_name (d), rulename, slen);
267 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
269 s2len = strlen (dep_name (d2));
271 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
272 continue;
274 bcopy (dep_name (d2), rulename + slen, s2len + 1);
275 f = lookup_file (rulename);
276 if (f == 0 || f->cmds == 0)
277 continue;
279 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
280 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
281 It also generates a normal `%.a: %.X' rule below. */
282 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
283 dep_name (d),
284 f->cmds);
286 /* The suffix rule `.X.Y:' is converted
287 to the pattern rule `%.Y: %.X'. */
288 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
294 /* Install the pattern rule RULE (whose fields have been filled in)
295 at the end of the list (so that any rules previously defined
296 will take precedence). If this rule duplicates a previous one
297 (identical target and dependencies), the old one is replaced
298 if OVERRIDE is nonzero, otherwise this new one is thrown out.
299 When an old rule is replaced, the new one is put at the end of the
300 list. Return nonzero if RULE is used; zero if not. */
303 new_pattern_rule (struct rule *rule, int override)
305 register struct rule *r, *lastrule;
306 register unsigned int i, j;
308 rule->in_use = 0;
309 rule->terminal = 0;
311 rule->next = 0;
313 /* Search for an identical rule. */
314 lastrule = 0;
315 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
316 for (i = 0; rule->targets[i] != 0; ++i)
318 for (j = 0; r->targets[j] != 0; ++j)
319 if (!streq (rule->targets[i], r->targets[j]))
320 break;
321 if (r->targets[j] == 0)
322 /* All the targets matched. */
324 register struct dep *d, *d2;
325 for (d = rule->deps, d2 = r->deps;
326 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
327 if (!streq (dep_name (d), dep_name (d2)))
328 break;
329 if (d == 0 && d2 == 0)
331 /* All the dependencies matched. */
332 if (override)
334 /* Remove the old rule. */
335 freerule (r, lastrule);
336 /* Install the new one. */
337 if (pattern_rules == 0)
338 pattern_rules = rule;
339 else
340 last_pattern_rule->next = rule;
341 last_pattern_rule = rule;
343 /* We got one. Stop looking. */
344 goto matched;
346 else
348 /* The old rule stays intact. Destroy the new one. */
349 freerule (rule, (struct rule *) 0);
350 return 0;
356 matched:;
358 if (r == 0)
360 /* There was no rule to replace. */
361 if (pattern_rules == 0)
362 pattern_rules = rule;
363 else
364 last_pattern_rule->next = rule;
365 last_pattern_rule = rule;
368 return 1;
372 /* Install an implicit pattern rule based on the three text strings
373 in the structure P points to. These strings come from one of
374 the arrays of default implicit pattern rules.
375 TERMINAL specifies what the `terminal' field of the rule should be. */
377 void
378 install_pattern_rule (struct pspec *p, int terminal)
380 register struct rule *r;
381 char *ptr;
383 r = (struct rule *) xmalloc (sizeof (struct rule));
385 r->targets = (char **) xmalloc (2 * sizeof (char *));
386 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
387 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
389 r->targets[1] = 0;
390 r->suffixes[1] = 0;
391 r->lens[1] = 0;
393 r->lens[0] = strlen (p->target);
394 /* These will all be string literals, but we malloc space for
395 them anyway because somebody might want to free them later on. */
396 r->targets[0] = savestring (p->target, r->lens[0]);
397 r->suffixes[0] = find_percent (r->targets[0]);
398 if (r->suffixes[0] == 0)
399 /* Programmer-out-to-lunch error. */
400 abort ();
401 else
402 ++r->suffixes[0];
404 ptr = p->dep;
405 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
406 sizeof (struct dep), 1),
407 sizeof (struct dep));
409 if (new_pattern_rule (r, 0))
411 r->terminal = terminal;
412 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
413 r->cmds->fileinfo.filenm = 0;
414 r->cmds->fileinfo.lineno = 0;
415 /* These will all be string literals, but we malloc space for them
416 anyway because somebody might want to free them later. */
417 r->cmds->commands = xstrdup (p->commands);
418 r->cmds->command_lines = 0;
423 /* Free all the storage used in RULE and take it out of the
424 pattern_rules chain. LASTRULE is the rule whose next pointer
425 points to RULE. */
427 static void
428 freerule (struct rule *rule, struct rule *lastrule)
430 struct rule *next = rule->next;
431 register unsigned int i;
432 register struct dep *dep;
434 for (i = 0; rule->targets[i] != 0; ++i)
435 free (rule->targets[i]);
437 dep = rule->deps;
438 while (dep)
440 struct dep *t;
442 t = dep->next;
443 /* We might leak dep->name here, but I'm not sure how to fix this: I
444 think that pointer might be shared (e.g., in the file hash?) */
445 free ((char *) dep);
446 dep = t;
449 free ((char *) rule->targets);
450 free ((char *) rule->suffixes);
451 free ((char *) rule->lens);
453 /* We can't free the storage for the commands because there
454 are ways that they could be in more than one place:
455 * If the commands came from a suffix rule, they could also be in
456 the `struct file's for other suffix rules or plain targets given
457 on the same makefile line.
458 * If two suffixes that together make a two-suffix rule were each
459 given twice in the .SUFFIXES list, and in the proper order, two
460 identical pattern rules would be created and the second one would
461 be discarded here, but both would contain the same `struct commands'
462 pointer from the `struct file' for the suffix rule. */
464 free ((char *) rule);
466 if (pattern_rules == rule)
467 if (lastrule != 0)
468 abort ();
469 else
470 pattern_rules = next;
471 else if (lastrule != 0)
472 lastrule->next = next;
473 if (last_pattern_rule == rule)
474 last_pattern_rule = lastrule;
477 /* Create a new pattern rule with the targets in the nil-terminated
478 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
479 pointers into the elements of TARGETS, where the `%'s are.
480 The new rule has dependencies DEPS and commands from COMMANDS.
481 It is a terminal rule if TERMINAL is nonzero. This rule overrides
482 identical rules with different commands if OVERRIDE is nonzero.
484 The storage for TARGETS and its elements is used and must not be freed
485 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
486 it may be freed. */
488 void
489 create_pattern_rule (char **targets, char **target_percents,
490 int terminal, struct dep *deps,
491 struct commands *commands, int override)
493 register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
494 register unsigned int max_targets, i;
496 r->cmds = commands;
497 r->deps = deps;
498 r->targets = targets;
500 max_targets = 2;
501 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
502 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
503 for (i = 0; targets[i] != 0; ++i)
505 if (i == max_targets - 1)
507 max_targets += 5;
508 r->lens = (unsigned int *)
509 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
510 r->suffixes = (char **)
511 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
513 r->lens[i] = strlen (targets[i]);
514 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
515 : target_percents[i]) + 1;
516 if (r->suffixes[i] == 0)
517 abort ();
520 if (i < max_targets - 1)
522 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
523 (i + 1) * sizeof (unsigned int));
524 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
525 (i + 1) * sizeof (char *));
528 if (new_pattern_rule (r, override))
529 r->terminal = terminal;
532 /* Print the data base of rules. */
534 static void /* Useful to call from gdb. */
535 print_rule (struct rule *r)
537 register unsigned int i;
538 register struct dep *d;
540 for (i = 0; r->targets[i] != 0; ++i)
542 fputs (r->targets[i], stdout);
543 if (r->targets[i + 1] != 0)
544 putchar (' ');
545 else
546 putchar (':');
548 if (r->terminal)
549 putchar (':');
551 for (d = r->deps; d != 0; d = d->next)
552 printf (" %s", dep_name (d));
553 putchar ('\n');
555 if (r->cmds != 0)
556 print_commands (r->cmds);
559 void
560 print_rule_data_base (void)
562 register unsigned int rules, terminal;
563 register struct rule *r;
565 puts (_("\n# Implicit Rules"));
567 rules = terminal = 0;
568 for (r = pattern_rules; r != 0; r = r->next)
570 ++rules;
572 putchar ('\n');
573 print_rule (r);
575 if (r->terminal)
576 ++terminal;
579 if (rules == 0)
580 puts (_("\n# No implicit rules."));
581 else
583 printf (_("\n# %u implicit rules, %u"), rules, terminal);
584 #ifndef NO_FLOAT
585 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
586 #else
588 int f = (terminal * 1000 + 5) / rules;
589 printf (" (%d.%d%%)", f/10, f%10);
591 #endif
592 puts (_(" terminal."));
595 if (num_pattern_rules != rules)
597 /* This can happen if a fatal error was detected while reading the
598 makefiles and thus count_implicit_rule_limits wasn't called yet. */
599 if (num_pattern_rules != 0)
600 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
601 num_pattern_rules, rules);