Imported Upstream version 4.0
[platform/upstream/make.git] / rule.c
1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
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.
9
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.
13
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/>.  */
16
17 #include "makeint.h"
18
19 #include <assert.h>
20
21 #include "filedef.h"
22 #include "dep.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "variable.h"
26 #include "rule.h"
27
28 static void freerule (struct rule *rule, struct rule *lastrule);
29 \f
30 /* Chain of all pattern rules.  */
31
32 struct rule *pattern_rules;
33
34 /* Pointer to last rule in the chain, so we can add onto the end.  */
35
36 struct rule *last_pattern_rule;
37
38 /* Number of rules in the chain.  */
39
40 unsigned int num_pattern_rules;
41
42 /* Maximum number of target patterns of any pattern rule.  */
43
44 unsigned int max_pattern_targets;
45
46 /* Maximum number of dependencies of any pattern rule.  */
47
48 unsigned int max_pattern_deps;
49
50 /* Maximum length of the name of a dependencies of any pattern rule.  */
51
52 unsigned int max_pattern_dep_length;
53
54 /* Pointer to structure for the file .SUFFIXES
55    whose dependencies are the suffixes to be searched.  */
56
57 struct file *suffix_file;
58
59 /* Maximum length of a suffix.  */
60
61 unsigned int maxsuffix;
62 \f
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.  */
67
68 void
69 count_implicit_rule_limits (void)
70 {
71   char *name;
72   int namelen;
73   struct rule *rule;
74
75   num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
76   max_pattern_dep_length = 0;
77
78   name = 0;
79   namelen = 0;
80   rule = pattern_rules;
81   while (rule != 0)
82     {
83       unsigned int ndeps = 0;
84       struct dep *dep;
85       struct rule *next = rule->next;
86
87       ++num_pattern_rules;
88
89       if (rule->num > max_pattern_targets)
90         max_pattern_targets = rule->num;
91
92       for (dep = rule->deps; dep != 0; dep = dep->next)
93         {
94           const char *dname = dep_name (dep);
95           unsigned int len = strlen (dname);
96
97 #ifdef VMS
98           const char *p = strrchr (dname, ']');
99           const char *p2;
100           if (p == 0)
101             p = strrchr (dname, ':');
102           p2 = p != 0 ? strchr (dname, '%') : 0;
103 #else
104           const char *p = strrchr (dname, '/');
105           const char *p2 = p != 0 ? strchr (dname, '%') : 0;
106 #endif
107           ndeps++;
108
109           if (len > max_pattern_dep_length)
110             max_pattern_dep_length = len;
111
112           if (p != 0 && p2 > p)
113             {
114               /* There is a slash before the % in the dep name.
115                  Extract the directory name.  */
116               if (p == dname)
117                 ++p;
118               if (p - dname > namelen)
119                 {
120                   namelen = p - dname;
121                   name = xrealloc (name, namelen + 1);
122                 }
123               memcpy (name, dname, p - dname);
124               name[p - dname] = '\0';
125
126               /* In the deps of an implicit rule the 'changed' flag
127                  actually indicates that the dependency is in a
128                  nonexistent subdirectory.  */
129
130               dep->changed = !dir_file_exists_p (name, "");
131             }
132           else
133             /* This dependency does not reside in a subdirectory.  */
134             dep->changed = 0;
135         }
136
137       if (ndeps > max_pattern_deps)
138         max_pattern_deps = ndeps;
139
140       rule = next;
141     }
142
143   if (name != 0)
144     free (name);
145 }
146 \f
147 /* Create a pattern rule from a suffix rule.
148    TARGET is the target suffix; SOURCE is the source suffix.
149    CMDS are the commands.
150    If TARGET is nil, it means the target pattern should be '(%.o)'.
151    If SOURCE is nil, it means there should be no deps.  */
152
153 static void
154 convert_suffix_rule (const char *target, const char *source,
155                      struct commands *cmds)
156 {
157   const char **names, **percents;
158   struct dep *deps;
159
160   names = xmalloc (sizeof (const char *));
161   percents = xmalloc (sizeof (const char *));
162
163   if (target == 0)
164     {
165       /* Special case: TARGET being nil means we are defining a '.X.a' suffix
166          rule; the target pattern is always '(%.o)'.  */
167 #ifdef VMS
168       *names = strcache_add_len ("(%.obj)", 7);
169 #else
170       *names = strcache_add_len ("(%.o)", 5);
171 #endif
172       *percents = *names + 1;
173     }
174   else
175     {
176       /* Construct the target name.  */
177       unsigned int len = strlen (target);
178       char *p = alloca (1 + len + 1);
179       p[0] = '%';
180       memcpy (p + 1, target, len + 1);
181       *names = strcache_add_len (p, len + 1);
182       *percents = *names;
183     }
184
185   if (source == 0)
186     deps = 0;
187   else
188     {
189       /* Construct the dependency name.  */
190       unsigned int len = strlen (source);
191       char *p = alloca (1 + len + 1);
192       p[0] = '%';
193       memcpy (p + 1, source, len + 1);
194       deps = alloc_dep ();
195       deps->name = strcache_add_len (p, len + 1);
196     }
197
198   create_pattern_rule (names, percents, 1, 0, deps, cmds, 0);
199 }
200
201 /* Convert old-style suffix rules to pattern rules.
202    All rules for the suffixes on the .SUFFIXES list are converted and added to
203    the chain of pattern rules.  */
204
205 void
206 convert_to_pattern (void)
207 {
208   struct dep *d, *d2;
209   char *rulename;
210
211   /* We will compute every potential suffix rule (.x.y) from the list of
212      suffixes in the .SUFFIXES target's dependencies and see if it exists.
213      First find the longest of the suffixes.  */
214
215   maxsuffix = 0;
216   for (d = suffix_file->deps; d != 0; d = d->next)
217     {
218       unsigned int l = strlen (dep_name (d));
219       if (l > maxsuffix)
220         maxsuffix = l;
221     }
222
223   /* Space to construct the suffix rule target name.  */
224   rulename = alloca ((maxsuffix * 2) + 1);
225
226   for (d = suffix_file->deps; d != 0; d = d->next)
227     {
228       unsigned int slen;
229
230       /* Make a rule that is just the suffix, with no deps or commands.
231          This rule exists solely to disqualify match-anything rules.  */
232       convert_suffix_rule (dep_name (d), 0, 0);
233
234       if (d->file->cmds != 0)
235         /* Record a pattern for this suffix's null-suffix rule.  */
236         convert_suffix_rule ("", dep_name (d), d->file->cmds);
237
238       /* Add every other suffix to this one and see if it exists as a
239          two-suffix rule.  */
240       slen = strlen (dep_name (d));
241       memcpy (rulename, dep_name (d), slen);
242
243       for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
244         {
245           struct file *f;
246           unsigned int s2len;
247
248           s2len = strlen (dep_name (d2));
249
250           /* Can't build something from itself.  */
251           if (slen == s2len && streq (dep_name (d), dep_name (d2)))
252             continue;
253
254           memcpy (rulename + slen, dep_name (d2), s2len + 1);
255           f = lookup_file (rulename);
256           if (f == 0 || f->cmds == 0)
257             continue;
258
259           if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
260             /* A suffix rule '.X.a:' generates the pattern rule '(%.o): %.X'.
261                It also generates a normal '%.a: %.X' rule below.  */
262             convert_suffix_rule (NULL, /* Indicates '(%.o)'.  */
263                                  dep_name (d),
264                                  f->cmds);
265
266           /* The suffix rule '.X.Y:' is converted
267              to the pattern rule '%.Y: %.X'.  */
268           convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
269         }
270     }
271 }
272
273
274 /* Install the pattern rule RULE (whose fields have been filled in) at the end
275    of the list (so that any rules previously defined will take precedence).
276    If this rule duplicates a previous one (identical target and dependencies),
277    the old one is replaced if OVERRIDE is nonzero, otherwise this new one is
278    thrown out.  When an old rule is replaced, the new one is put at the end of
279    the list.  Return nonzero if RULE is used; zero if not.  */
280
281 static int
282 new_pattern_rule (struct rule *rule, int override)
283 {
284   struct rule *r, *lastrule;
285   unsigned int i, j;
286
287   rule->in_use = 0;
288   rule->terminal = 0;
289
290   rule->next = 0;
291
292   /* Search for an identical rule.  */
293   lastrule = 0;
294   for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
295     for (i = 0; i < rule->num; ++i)
296       {
297         for (j = 0; j < r->num; ++j)
298           if (!streq (rule->targets[i], r->targets[j]))
299             break;
300         /* If all the targets matched...  */
301         if (j == r->num)
302           {
303             struct dep *d, *d2;
304             for (d = rule->deps, d2 = r->deps;
305                  d != 0 && d2 != 0; d = d->next, d2 = d2->next)
306               if (!streq (dep_name (d), dep_name (d2)))
307                 break;
308             if (d == 0 && d2 == 0)
309               {
310                 /* All the dependencies matched.  */
311                 if (override)
312                   {
313                     /* Remove the old rule.  */
314                     freerule (r, lastrule);
315                     /* Install the new one.  */
316                     if (pattern_rules == 0)
317                       pattern_rules = rule;
318                     else
319                       last_pattern_rule->next = rule;
320                     last_pattern_rule = rule;
321
322                     /* We got one.  Stop looking.  */
323                     goto matched;
324                   }
325                 else
326                   {
327                     /* The old rule stays intact.  Destroy the new one.  */
328                     freerule (rule, (struct rule *) 0);
329                     return 0;
330                   }
331               }
332           }
333       }
334
335  matched:;
336
337   if (r == 0)
338     {
339       /* There was no rule to replace.  */
340       if (pattern_rules == 0)
341         pattern_rules = rule;
342       else
343         last_pattern_rule->next = rule;
344       last_pattern_rule = rule;
345     }
346
347   return 1;
348 }
349
350
351 /* Install an implicit pattern rule based on the three text strings
352    in the structure P points to.  These strings come from one of
353    the arrays of default implicit pattern rules.
354    TERMINAL specifies what the 'terminal' field of the rule should be.  */
355
356 void
357 install_pattern_rule (struct pspec *p, int terminal)
358 {
359   struct rule *r;
360   char *ptr;
361
362   r = xmalloc (sizeof (struct rule));
363
364   r->num = 1;
365   r->targets = xmalloc (sizeof (const char *));
366   r->suffixes = xmalloc (sizeof (const char *));
367   r->lens = xmalloc (sizeof (unsigned int));
368
369   r->lens[0] = strlen (p->target);
370   r->targets[0] = p->target;
371   r->suffixes[0] = find_percent_cached (&r->targets[0]);
372   assert (r->suffixes[0] != NULL);
373   ++r->suffixes[0];
374
375   ptr = p->dep;
376   r->deps = PARSE_SIMPLE_SEQ (&ptr, struct dep);
377
378   if (new_pattern_rule (r, 0))
379     {
380       r->terminal = terminal;
381       r->cmds = xmalloc (sizeof (struct commands));
382       r->cmds->fileinfo.filenm = 0;
383       r->cmds->fileinfo.lineno = 0;
384       /* These will all be string literals, but we malloc space for them
385          anyway because somebody might want to free them later.  */
386       r->cmds->commands = xstrdup (p->commands);
387       r->cmds->command_lines = 0;
388       r->cmds->recipe_prefix = RECIPEPREFIX_DEFAULT;
389     }
390 }
391
392
393 /* Free all the storage used in RULE and take it out of the
394    pattern_rules chain.  LASTRULE is the rule whose next pointer
395    points to RULE.  */
396
397 static void
398 freerule (struct rule *rule, struct rule *lastrule)
399 {
400   struct rule *next = rule->next;
401
402   free_dep_chain (rule->deps);
403
404   /* MSVC erroneously warns without a cast here.  */
405   free ((void *)rule->targets);
406   free ((void *)rule->suffixes);
407   free (rule->lens);
408
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.  */
419
420   free (rule);
421
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;
431 }
432 \f
433 /* Create a new pattern rule with the targets in the nil-terminated array
434    TARGETS.  TARGET_PERCENTS is an array of pointers to the % in each element
435    of TARGETS.  N is the number of items in the array (not counting the nil
436    element).  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.
439
440    The storage for TARGETS and its elements and TARGET_PERCENTS is used and
441    must not be freed until the rule is destroyed.  */
442
443 void
444 create_pattern_rule (const char **targets, const char **target_percents,
445                      unsigned int n, int terminal, struct dep *deps,
446                      struct commands *commands, int override)
447 {
448   unsigned int i;
449   struct rule *r = xmalloc (sizeof (struct rule));
450
451   r->num = n;
452   r->cmds = commands;
453   r->deps = deps;
454   r->targets = targets;
455   r->suffixes = target_percents;
456   r->lens = xmalloc (n * sizeof (unsigned int));
457
458   for (i = 0; i < n; ++i)
459     {
460       r->lens[i] = strlen (targets[i]);
461       assert (r->suffixes[i] != NULL);
462       ++r->suffixes[i];
463     }
464
465   if (new_pattern_rule (r, override))
466     r->terminal = terminal;
467 }
468 \f
469 /* Print the data base of rules.  */
470
471 static void                     /* Useful to call from gdb.  */
472 print_rule (struct rule *r)
473 {
474   unsigned int i;
475
476   for (i = 0; i < r->num; ++i)
477     {
478       fputs (r->targets[i], stdout);
479       putchar ((i + 1 == r->num) ? ':' : ' ');
480     }
481   if (r->terminal)
482     putchar (':');
483
484   print_prereqs (r->deps);
485
486   if (r->cmds != 0)
487     print_commands (r->cmds);
488 }
489
490 void
491 print_rule_data_base (void)
492 {
493   unsigned int rules, terminal;
494   struct rule *r;
495
496   puts (_("\n# Implicit Rules"));
497
498   rules = terminal = 0;
499   for (r = pattern_rules; r != 0; r = r->next)
500     {
501       ++rules;
502
503       putchar ('\n');
504       print_rule (r);
505
506       if (r->terminal)
507         ++terminal;
508     }
509
510   if (rules == 0)
511     puts (_("\n# No implicit rules."));
512   else
513     {
514       printf (_("\n# %u implicit rules, %u"), rules, terminal);
515 #ifndef NO_FLOAT
516       printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
517 #else
518       {
519         int f = (terminal * 1000 + 5) / rules;
520         printf (" (%d.%d%%)", f/10, f%10);
521       }
522 #endif
523       puts (_(" terminal."));
524     }
525
526   if (num_pattern_rules != rules)
527     {
528       /* This can happen if a fatal error was detected while reading the
529          makefiles and thus count_implicit_rule_limits wasn't called yet.  */
530       if (num_pattern_rules != 0)
531         fatal (NILF, _("BUG: num_pattern_rules is wrong!  %u != %u"),
532                num_pattern_rules, rules);
533     }
534 }