Add missing load.c file to POTFILES.in
[make.git] / rule.c
blob36eb308a28103577830364930337cf5e1adf958c
1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988-2012 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 it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 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 this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include "make.h"
19 #include <assert.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 (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 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 struct dep *dep;
86 struct rule *next = rule->next;
88 ++num_pattern_rules;
90 if (rule->num > max_pattern_targets)
91 max_pattern_targets = rule->num;
93 for (dep = rule->deps; dep != 0; dep = dep->next)
95 const char *dname = dep_name (dep);
96 unsigned int len = strlen (dname);
98 #ifdef VMS
99 const char *p = strrchr (dname, ']');
100 const char *p2;
101 if (p == 0)
102 p = strrchr (dname, ':');
103 p2 = p != 0 ? strchr (dname, '%') : 0;
104 #else
105 const char *p = strrchr (dname, '/');
106 const char *p2 = p != 0 ? strchr (dname, '%') : 0;
107 #endif
108 ndeps++;
110 if (len > max_pattern_dep_length)
111 max_pattern_dep_length = len;
113 if (p != 0 && p2 > p)
115 /* There is a slash before the % in the dep name.
116 Extract the directory name. */
117 if (p == dname)
118 ++p;
119 if (p - dname > namelen)
121 namelen = p - dname;
122 name = xrealloc (name, namelen + 1);
124 memcpy (name, dname, p - dname);
125 name[p - dname] = '\0';
127 /* In the deps of an implicit rule the 'changed' flag
128 actually indicates that the dependency is in a
129 nonexistent subdirectory. */
131 dep->changed = !dir_file_exists_p (name, "");
133 else
134 /* This dependency does not reside in a subdirectory. */
135 dep->changed = 0;
138 if (ndeps > max_pattern_deps)
139 max_pattern_deps = ndeps;
141 lastrule = rule;
142 rule = next;
145 if (name != 0)
146 free (name);
149 /* Create a pattern rule from a suffix rule.
150 TARGET is the target suffix; SOURCE is the source suffix.
151 CMDS are the commands.
152 If TARGET is nil, it means the target pattern should be '(%.o)'.
153 If SOURCE is nil, it means there should be no deps. */
155 static void
156 convert_suffix_rule (const char *target, const char *source,
157 struct commands *cmds)
159 const char **names, **percents;
160 struct dep *deps;
162 names = xmalloc (sizeof (const char *));
163 percents = xmalloc (sizeof (const char *));
165 if (target == 0)
167 /* Special case: TARGET being nil means we are defining a '.X.a' suffix
168 rule; the target pattern is always '(%.o)'. */
169 #ifdef VMS
170 *names = strcache_add_len ("(%.obj)", 7);
171 #else
172 *names = strcache_add_len ("(%.o)", 5);
173 #endif
174 *percents = *names + 1;
176 else
178 /* Construct the target name. */
179 unsigned int len = strlen (target);
180 char *p = alloca (1 + len + 1);
181 p[0] = '%';
182 memcpy (p + 1, target, len + 1);
183 *names = strcache_add_len (p, len + 1);
184 *percents = *names;
187 if (source == 0)
188 deps = 0;
189 else
191 /* Construct the dependency name. */
192 unsigned int len = strlen (source);
193 char *p = alloca (1 + len + 1);
194 p[0] = '%';
195 memcpy (p + 1, source, len + 1);
196 deps = alloc_dep ();
197 deps->name = strcache_add_len (p, len + 1);
200 create_pattern_rule (names, percents, 1, 0, deps, cmds, 0);
203 /* Convert old-style suffix rules to pattern rules.
204 All rules for the suffixes on the .SUFFIXES list are converted and added to
205 the chain of pattern rules. */
207 void
208 convert_to_pattern (void)
210 struct dep *d, *d2;
211 char *rulename;
213 /* We will compute every potential suffix rule (.x.y) from the list of
214 suffixes in the .SUFFIXES target's dependencies and see if it exists.
215 First find the longest of the suffixes. */
217 maxsuffix = 0;
218 for (d = suffix_file->deps; d != 0; d = d->next)
220 unsigned int l = strlen (dep_name (d));
221 if (l > maxsuffix)
222 maxsuffix = l;
225 /* Space to construct the suffix rule target name. */
226 rulename = alloca ((maxsuffix * 2) + 1);
228 for (d = suffix_file->deps; d != 0; d = d->next)
230 unsigned int slen;
232 /* Make a rule that is just the suffix, with no deps or commands.
233 This rule exists solely to disqualify match-anything rules. */
234 convert_suffix_rule (dep_name (d), 0, 0);
236 if (d->file->cmds != 0)
237 /* Record a pattern for this suffix's null-suffix rule. */
238 convert_suffix_rule ("", dep_name (d), d->file->cmds);
240 /* Add every other suffix to this one and see if it exists as a
241 two-suffix rule. */
242 slen = strlen (dep_name (d));
243 memcpy (rulename, dep_name (d), slen);
245 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
247 struct file *f;
248 unsigned int s2len;
250 s2len = strlen (dep_name (d2));
252 /* Can't build something from itself. */
253 if (slen == s2len && streq (dep_name (d), dep_name (d2)))
254 continue;
256 memcpy (rulename + slen, dep_name (d2), s2len + 1);
257 f = lookup_file (rulename);
258 if (f == 0 || f->cmds == 0)
259 continue;
261 if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
262 /* A suffix rule '.X.a:' generates the pattern rule '(%.o): %.X'.
263 It also generates a normal '%.a: %.X' rule below. */
264 convert_suffix_rule (NULL, /* Indicates '(%.o)'. */
265 dep_name (d),
266 f->cmds);
268 /* The suffix rule '.X.Y:' is converted
269 to the pattern rule '%.Y: %.X'. */
270 convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
276 /* Install the pattern rule RULE (whose fields have been filled in) at the end
277 of the list (so that any rules previously defined will take precedence).
278 If this rule duplicates a previous one (identical target and dependencies),
279 the old one is replaced if OVERRIDE is nonzero, otherwise this new one is
280 thrown out. When an old rule is replaced, the new one is put at the end of
281 the list. Return nonzero if RULE is used; zero if not. */
283 static int
284 new_pattern_rule (struct rule *rule, int override)
286 struct rule *r, *lastrule;
287 unsigned int i, j;
289 rule->in_use = 0;
290 rule->terminal = 0;
292 rule->next = 0;
294 /* Search for an identical rule. */
295 lastrule = 0;
296 for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
297 for (i = 0; i < rule->num; ++i)
299 for (j = 0; j < r->num; ++j)
300 if (!streq (rule->targets[i], r->targets[j]))
301 break;
302 /* If all the targets matched... */
303 if (j == r->num)
305 struct dep *d, *d2;
306 for (d = rule->deps, d2 = r->deps;
307 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
308 if (!streq (dep_name (d), dep_name (d2)))
309 break;
310 if (d == 0 && d2 == 0)
312 /* All the dependencies matched. */
313 if (override)
315 /* Remove the old rule. */
316 freerule (r, lastrule);
317 /* Install the new one. */
318 if (pattern_rules == 0)
319 pattern_rules = rule;
320 else
321 last_pattern_rule->next = rule;
322 last_pattern_rule = rule;
324 /* We got one. Stop looking. */
325 goto matched;
327 else
329 /* The old rule stays intact. Destroy the new one. */
330 freerule (rule, (struct rule *) 0);
331 return 0;
337 matched:;
339 if (r == 0)
341 /* There was no rule to replace. */
342 if (pattern_rules == 0)
343 pattern_rules = rule;
344 else
345 last_pattern_rule->next = rule;
346 last_pattern_rule = rule;
349 return 1;
353 /* Install an implicit pattern rule based on the three text strings
354 in the structure P points to. These strings come from one of
355 the arrays of default implicit pattern rules.
356 TERMINAL specifies what the 'terminal' field of the rule should be. */
358 void
359 install_pattern_rule (struct pspec *p, int terminal)
361 struct rule *r;
362 char *ptr;
364 r = xmalloc (sizeof (struct rule));
366 r->num = 1;
367 r->targets = xmalloc (sizeof (const char *));
368 r->suffixes = xmalloc (sizeof (const char *));
369 r->lens = xmalloc (sizeof (unsigned int));
371 r->lens[0] = strlen (p->target);
372 r->targets[0] = p->target;
373 r->suffixes[0] = find_percent_cached (&r->targets[0]);
374 assert (r->suffixes[0] != NULL);
375 ++r->suffixes[0];
377 ptr = p->dep;
378 r->deps = PARSE_FILE_SEQ (&ptr, struct dep, '\0', NULL, 0);
380 if (new_pattern_rule (r, 0))
382 r->terminal = terminal;
383 r->cmds = xmalloc (sizeof (struct commands));
384 r->cmds->fileinfo.filenm = 0;
385 r->cmds->fileinfo.lineno = 0;
386 /* These will all be string literals, but we malloc space for them
387 anyway because somebody might want to free them later. */
388 r->cmds->commands = xstrdup (p->commands);
389 r->cmds->command_lines = 0;
390 r->cmds->recipe_prefix = RECIPEPREFIX_DEFAULT;
395 /* Free all the storage used in RULE and take it out of the
396 pattern_rules chain. LASTRULE is the rule whose next pointer
397 points to RULE. */
399 static void
400 freerule (struct rule *rule, struct rule *lastrule)
402 struct rule *next = rule->next;
404 free_dep_chain (rule->deps);
406 /* MSVC erroneously warns without a cast here. */
407 free ((void *)rule->targets);
408 free ((void *)rule->suffixes);
409 free (rule->lens);
411 /* We can't free the storage for the commands because there
412 are ways that they could be in more than one place:
413 * If the commands came from a suffix rule, they could also be in
414 the 'struct file's for other suffix rules or plain targets given
415 on the same makefile line.
416 * If two suffixes that together make a two-suffix rule were each
417 given twice in the .SUFFIXES list, and in the proper order, two
418 identical pattern rules would be created and the second one would
419 be discarded here, but both would contain the same 'struct commands'
420 pointer from the 'struct file' for the suffix rule. */
422 free (rule);
424 if (pattern_rules == rule)
425 if (lastrule != 0)
426 abort ();
427 else
428 pattern_rules = next;
429 else if (lastrule != 0)
430 lastrule->next = next;
431 if (last_pattern_rule == rule)
432 last_pattern_rule = lastrule;
435 /* Create a new pattern rule with the targets in the nil-terminated array
436 TARGETS. TARGET_PERCENTS is an array of pointers to the % in each element
437 of TARGETS. N is the number of items in the array (not counting the nil
438 element). The new rule has dependencies DEPS and commands from COMMANDS.
439 It is a terminal rule if TERMINAL is nonzero. This rule overrides
440 identical rules with different commands if OVERRIDE is nonzero.
442 The storage for TARGETS and its elements and TARGET_PERCENTS is used and
443 must not be freed until the rule is destroyed. */
445 void
446 create_pattern_rule (const char **targets, const char **target_percents,
447 unsigned int n, int terminal, struct dep *deps,
448 struct commands *commands, int override)
450 unsigned int i;
451 struct rule *r = xmalloc (sizeof (struct rule));
453 r->num = n;
454 r->cmds = commands;
455 r->deps = deps;
456 r->targets = targets;
457 r->suffixes = target_percents;
458 r->lens = xmalloc (n * sizeof (unsigned int));
460 for (i = 0; i < n; ++i)
462 r->lens[i] = strlen (targets[i]);
463 assert (r->suffixes[i] != NULL);
464 ++r->suffixes[i];
467 if (new_pattern_rule (r, override))
468 r->terminal = terminal;
471 /* Print the data base of rules. */
473 static void /* Useful to call from gdb. */
474 print_rule (struct rule *r)
476 unsigned int i;
478 for (i = 0; i < r->num; ++i)
480 fputs (r->targets[i], stdout);
481 putchar ((i + 1 == r->num) ? ':' : ' ');
483 if (r->terminal)
484 putchar (':');
486 print_prereqs (r->deps);
488 if (r->cmds != 0)
489 print_commands (r->cmds);
492 void
493 print_rule_data_base (void)
495 unsigned int rules, terminal;
496 struct rule *r;
498 puts (_("\n# Implicit Rules"));
500 rules = terminal = 0;
501 for (r = pattern_rules; r != 0; r = r->next)
503 ++rules;
505 putchar ('\n');
506 print_rule (r);
508 if (r->terminal)
509 ++terminal;
512 if (rules == 0)
513 puts (_("\n# No implicit rules."));
514 else
516 printf (_("\n# %u implicit rules, %u"), rules, terminal);
517 #ifndef NO_FLOAT
518 printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
519 #else
521 int f = (terminal * 1000 + 5) / rules;
522 printf (" (%d.%d%%)", f/10, f%10);
524 #endif
525 puts (_(" terminal."));
528 if (num_pattern_rules != rules)
530 /* This can happen if a fatal error was detected while reading the
531 makefiles and thus count_implicit_rule_limits wasn't called yet. */
532 if (num_pattern_rules != 0)
533 fatal (NILF, _("BUG: num_pattern_rules is wrong! %u != %u"),
534 num_pattern_rules, rules);