Update copyright and license notices on all files.
[make/kirr.git] / rule.c
blobc41deca49e9084187fdacb7c24db977ac3d5caf1
1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18 #include "make.h"
19 #include "dep.h"
20 #include "filedef.h"
21 #include "job.h"
22 #include "commands.h"
23 #include "variable.h"
24 #include "rule.h"
26 static void freerule PARAMS ((struct rule *rule, struct rule *lastrule));
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 (void)
69 char *name;
70 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);
100 #ifdef VMS
101 char *p = strrchr (dep->name, ']');
102 char *p2;
103 if (p == 0)
104 p = strrchr (dep->name, ':');
105 p2 = p != 0 ? strchr (dep->name, '%') : 0;
106 #else
107 char *p = strrchr (dep->name, '/');
108 char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
109 #endif
110 ndeps++;
112 if (len > max_pattern_dep_length)
113 max_pattern_dep_length = len;
115 if (p != 0 && p2 > p)
117 /* There is a slash before the % in the dep name.
118 Extract the directory name. */
119 if (p == dep->name)
120 ++p;
121 if (p - dep->name > namelen)
123 if (name != 0)
124 free (name);
125 namelen = p - dep->name;
126 name = (char *) xmalloc (namelen + 1);
128 bcopy (dep->name, name, p - dep->name);
129 name[p - dep->name] = '\0';
131 /* In the deps of an implicit rule the `changed' flag
132 actually indicates that the dependency is in a
133 nonexistent subdirectory. */
135 dep->changed = !dir_file_exists_p (name, "");
137 else
138 /* This dependency does not reside in a subdirectory. */
139 dep->changed = 0;
142 if (ndeps > max_pattern_deps)
143 max_pattern_deps = ndeps;
145 lastrule = rule;
146 rule = next;
149 if (name != 0)
150 free (name);
153 /* Create a pattern rule from a suffix rule.
154 TARGET is the target suffix; SOURCE is the source suffix.
155 CMDS are the commands.
156 If TARGET is nil, it means the target pattern should be `(%.o)'.
157 If SOURCE is nil, it means there should be no deps. */
159 static void
160 convert_suffix_rule (char *target, char *source, struct commands *cmds)
162 char *targname, *targpercent, *depname;
163 char **names, **percents;
164 struct dep *deps;
165 unsigned int len;
167 if (target == 0)
168 /* Special case: TARGET being nil means we are defining a
169 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
171 #ifdef VMS
172 targname = savestring ("(%.obj)", 7);
173 #else
174 targname = savestring ("(%.o)", 5);
175 #endif
176 targpercent = targname + 1;
178 else
180 /* Construct the target name. */
181 len = strlen (target);
182 targname = xmalloc (1 + len + 1);
183 targname[0] = '%';
184 bcopy (target, targname + 1, len + 1);
185 targpercent = targname;
188 names = (char **) xmalloc (2 * sizeof (char *));
189 percents = (char **) alloca (2 * sizeof (char *));
190 names[0] = targname;
191 percents[0] = targpercent;
192 names[1] = percents[1] = 0;
194 if (source == 0)
195 deps = 0;
196 else
198 /* Construct the dependency name. */
199 len = strlen (source);
200 depname = xmalloc (1 + len + 1);
201 depname[0] = '%';
202 bcopy (source, depname + 1, len + 1);
203 deps = (struct dep *) xmalloc (sizeof (struct dep));
204 deps->next = 0;
205 deps->name = depname;
206 deps->ignore_mtime = 0;
207 deps->staticpattern = 0;
208 deps->need_2nd_expansion = 0;
211 create_pattern_rule (names, percents, 0, deps, cmds, 0);
214 /* Convert old-style suffix rules to pattern rules.
215 All rules for the suffixes on the .SUFFIXES list
216 are converted and added to the chain of pattern rules. */
218 void
219 convert_to_pattern (void)
221 register struct dep *d, *d2;
222 register struct file *f;
223 register char *rulename;
224 register unsigned int slen, s2len;
226 /* Compute maximum length of all the suffixes. */
228 maxsuffix = 0;
229 for (d = suffix_file->deps; d != 0; d = d->next)
231 register unsigned int namelen = strlen (dep_name (d));
232 if (namelen > maxsuffix)
233 maxsuffix = namelen;
236 rulename = (char *) alloca ((maxsuffix * 2) + 1);
238 for (d = suffix_file->deps; d != 0; d = d->next)
240 /* Make a rule that is just the suffix, with no deps or commands.
241 This rule exists solely to disqualify match-anything rules. */
242 convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
244 f = d->file;
245 if (f->cmds != 0)
246 /* Record a pattern for this suffix's null-suffix rule. */
247 convert_suffix_rule ("", dep_name (d), f->cmds);
249 /* Record a pattern for each of this suffix's two-suffix rules. */
250 slen = strlen (dep_name (d));
251 bcopy (dep_name (d), rulename, slen);
252 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
254 s2len = strlen (dep_name (d2));
256 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
257 continue;
259 bcopy (dep_name (d2), rulename + slen, s2len + 1);
260 f = lookup_file (rulename);
261 if (f == 0 || f->cmds == 0)
262 continue;
264 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
265 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
266 It also generates a normal `%.a: %.X' rule below. */
267 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
268 dep_name (d),
269 f->cmds);
271 /* The suffix rule `.X.Y:' is converted
272 to the pattern rule `%.Y: %.X'. */
273 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
279 /* Install the pattern rule RULE (whose fields have been filled in)
280 at the end of the list (so that any rules previously defined
281 will take precedence). If this rule duplicates a previous one
282 (identical target and dependencies), the old one is replaced
283 if OVERRIDE is nonzero, otherwise this new one is thrown out.
284 When an old rule is replaced, the new one is put at the end of the
285 list. Return nonzero if RULE is used; zero if not. */
288 new_pattern_rule (struct rule *rule, int override)
290 register struct rule *r, *lastrule;
291 register unsigned int i, j;
293 rule->in_use = 0;
294 rule->terminal = 0;
296 rule->next = 0;
298 /* Search for an identical rule. */
299 lastrule = 0;
300 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
301 for (i = 0; rule->targets[i] != 0; ++i)
303 for (j = 0; r->targets[j] != 0; ++j)
304 if (!streq (rule->targets[i], r->targets[j]))
305 break;
306 if (r->targets[j] == 0)
307 /* All the targets matched. */
309 register struct dep *d, *d2;
310 for (d = rule->deps, d2 = r->deps;
311 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
312 if (!streq (dep_name (d), dep_name (d2)))
313 break;
314 if (d == 0 && d2 == 0)
316 /* All the dependencies matched. */
317 if (override)
319 /* Remove the old rule. */
320 freerule (r, lastrule);
321 /* Install the new one. */
322 if (pattern_rules == 0)
323 pattern_rules = rule;
324 else
325 last_pattern_rule->next = rule;
326 last_pattern_rule = rule;
328 /* We got one. Stop looking. */
329 goto matched;
331 else
333 /* The old rule stays intact. Destroy the new one. */
334 freerule (rule, (struct rule *) 0);
335 return 0;
341 matched:;
343 if (r == 0)
345 /* There was no rule to replace. */
346 if (pattern_rules == 0)
347 pattern_rules = rule;
348 else
349 last_pattern_rule->next = rule;
350 last_pattern_rule = rule;
353 return 1;
357 /* Install an implicit pattern rule based on the three text strings
358 in the structure P points to. These strings come from one of
359 the arrays of default implicit pattern rules.
360 TERMINAL specifies what the `terminal' field of the rule should be. */
362 void
363 install_pattern_rule (struct pspec *p, int terminal)
365 register struct rule *r;
366 char *ptr;
368 r = (struct rule *) xmalloc (sizeof (struct rule));
370 r->targets = (char **) xmalloc (2 * sizeof (char *));
371 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
372 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
374 r->targets[1] = 0;
375 r->suffixes[1] = 0;
376 r->lens[1] = 0;
378 r->lens[0] = strlen (p->target);
379 /* These will all be string literals, but we malloc space for
380 them anyway because somebody might want to free them later on. */
381 r->targets[0] = savestring (p->target, r->lens[0]);
382 r->suffixes[0] = find_percent (r->targets[0]);
383 if (r->suffixes[0] == 0)
384 /* Programmer-out-to-lunch error. */
385 abort ();
386 else
387 ++r->suffixes[0];
389 ptr = p->dep;
390 r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
391 sizeof (struct dep), 1),
392 sizeof (struct dep));
394 if (new_pattern_rule (r, 0))
396 r->terminal = terminal;
397 r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
398 r->cmds->fileinfo.filenm = 0;
399 r->cmds->fileinfo.lineno = 0;
400 /* These will all be string literals, but we malloc space for them
401 anyway because somebody might want to free them later. */
402 r->cmds->commands = xstrdup (p->commands);
403 r->cmds->command_lines = 0;
408 /* Free all the storage used in RULE and take it out of the
409 pattern_rules chain. LASTRULE is the rule whose next pointer
410 points to RULE. */
412 static void
413 freerule (struct rule *rule, struct rule *lastrule)
415 struct rule *next = rule->next;
416 register unsigned int i;
417 register struct dep *dep;
419 for (i = 0; rule->targets[i] != 0; ++i)
420 free (rule->targets[i]);
422 dep = rule->deps;
423 while (dep)
425 struct dep *t;
427 t = dep->next;
428 /* We might leak dep->name here, but I'm not sure how to fix this: I
429 think that pointer might be shared (e.g., in the file hash?) */
430 free ((char *) dep);
431 dep = t;
434 free ((char *) rule->targets);
435 free ((char *) rule->suffixes);
436 free ((char *) rule->lens);
438 /* We can't free the storage for the commands because there
439 are ways that they could be in more than one place:
440 * If the commands came from a suffix rule, they could also be in
441 the `struct file's for other suffix rules or plain targets given
442 on the same makefile line.
443 * If two suffixes that together make a two-suffix rule were each
444 given twice in the .SUFFIXES list, and in the proper order, two
445 identical pattern rules would be created and the second one would
446 be discarded here, but both would contain the same `struct commands'
447 pointer from the `struct file' for the suffix rule. */
449 free ((char *) rule);
451 if (pattern_rules == rule)
452 if (lastrule != 0)
453 abort ();
454 else
455 pattern_rules = next;
456 else if (lastrule != 0)
457 lastrule->next = next;
458 if (last_pattern_rule == rule)
459 last_pattern_rule = lastrule;
462 /* Create a new pattern rule with the targets in the nil-terminated
463 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
464 pointers into the elements of TARGETS, where the `%'s are.
465 The new rule has dependencies DEPS and commands from COMMANDS.
466 It is a terminal rule if TERMINAL is nonzero. This rule overrides
467 identical rules with different commands if OVERRIDE is nonzero.
469 The storage for TARGETS and its elements is used and must not be freed
470 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
471 it may be freed. */
473 void
474 create_pattern_rule (char **targets, char **target_percents,
475 int terminal, struct dep *deps,
476 struct commands *commands, int override)
478 unsigned int max_targets, i;
479 struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
481 r->cmds = commands;
482 r->deps = deps;
483 r->targets = targets;
485 max_targets = 2;
486 r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
487 r->suffixes = (char **) xmalloc (2 * sizeof (char *));
488 for (i = 0; targets[i] != 0; ++i)
490 if (i == max_targets - 1)
492 max_targets += 5;
493 r->lens = (unsigned int *)
494 xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
495 r->suffixes = (char **)
496 xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
498 r->lens[i] = strlen (targets[i]);
499 r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
500 : target_percents[i]) + 1;
501 if (r->suffixes[i] == 0)
502 abort ();
505 if (i < max_targets - 1)
507 r->lens = (unsigned int *) xrealloc ((char *) r->lens,
508 (i + 1) * sizeof (unsigned int));
509 r->suffixes = (char **) xrealloc ((char *) r->suffixes,
510 (i + 1) * sizeof (char *));
513 if (new_pattern_rule (r, override))
514 r->terminal = terminal;
517 /* Print the data base of rules. */
519 static void /* Useful to call from gdb. */
520 print_rule (struct rule *r)
522 register unsigned int i;
523 register struct dep *d;
525 for (i = 0; r->targets[i] != 0; ++i)
527 fputs (r->targets[i], stdout);
528 if (r->targets[i + 1] != 0)
529 putchar (' ');
530 else
531 putchar (':');
533 if (r->terminal)
534 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 void
545 print_rule_data_base (void)
547 register unsigned int rules, terminal;
548 register struct rule *r;
550 puts (_("\n# Implicit Rules"));
552 rules = terminal = 0;
553 for (r = pattern_rules; r != 0; r = r->next)
555 ++rules;
557 putchar ('\n');
558 print_rule (r);
560 if (r->terminal)
561 ++terminal;
564 if (rules == 0)
565 puts (_("\n# No implicit rules."));
566 else
568 printf (_("\n# %u implicit rules, %u"), rules, terminal);
569 #ifndef NO_FLOAT
570 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
571 #else
573 int f = (terminal * 1000 + 5) / rules;
574 printf (" (%d.%d%%)", f/10, f%10);
576 #endif
577 puts (_(" terminal."));
580 if (num_pattern_rules != rules)
582 /* This can happen if a fatal error was detected while reading the
583 makefiles and thus count_implicit_rule_limits wasn't called yet. */
584 if (num_pattern_rules != 0)
585 fatal (NILF, _("BUG: num_pattern_rules wrong! %u != %u"),
586 num_pattern_rules, rules);