Home | History | Annotate | Download | only in make-3.81
      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, 2004, 2005, 2006 Free Software
      4 Foundation, Inc.
      5 This file is part of GNU Make.
      6 
      7 GNU Make is free software; you can redistribute it and/or modify it under the
      8 terms of the GNU General Public License as published by the Free Software
      9 Foundation; either version 2, or (at your option) any later version.
     10 
     11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
     12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     13 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     14 
     15 You should have received a copy of the GNU General Public License along with
     16 GNU Make; see the file COPYING.  If not, write to the Free Software
     17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
     18 
     19 #include "make.h"
     20 #include "dep.h"
     21 #include "filedef.h"
     22 #include "job.h"
     23 #include "commands.h"
     24 #include "variable.h"
     25 #include "rule.h"
     26 
     27 static void freerule PARAMS ((struct rule *rule, struct rule *lastrule));
     28 
     29 /* 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 
     63 /* Compute the maximum dependency length and maximum number of
     65    dependencies of all implicit rules.  Also sets the subdir
     66    flag for a rule when appropriate, possibly removing the rule
     67    completely when appropriate.  */
     68 
     69 void
     70 count_implicit_rule_limits (void)
     71 {
     72   char *name;
     73   int namelen;
     74   register struct rule *rule, *lastrule;
     75 
     76   num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
     77   max_pattern_dep_length = 0;
     78 
     79   name = 0;
     80   namelen = 0;
     81   rule = pattern_rules;
     82   lastrule = 0;
     83   while (rule != 0)
     84     {
     85       unsigned int ndeps = 0;
     86       register struct dep *dep;
     87       struct rule *next = rule->next;
     88       unsigned int ntargets;
     89 
     90       ++num_pattern_rules;
     91 
     92       ntargets = 0;
     93       while (rule->targets[ntargets] != 0)
     94 	++ntargets;
     95 
     96       if (ntargets > max_pattern_targets)
     97 	max_pattern_targets = ntargets;
     98 
     99       for (dep = rule->deps; dep != 0; dep = dep->next)
    100 	{
    101 	  unsigned int len = strlen (dep->name);
    102 
    103 #ifdef VMS
    104 	  char *p = strrchr (dep->name, ']');
    105           char *p2;
    106           if (p == 0)
    107             p = strrchr (dep->name, ':');
    108           p2 = p != 0 ? strchr (dep->name, '%') : 0;
    109 #else
    110 	  char *p = strrchr (dep->name, '/');
    111 	  char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
    112 #endif
    113 	  ndeps++;
    114 
    115 	  if (len > max_pattern_dep_length)
    116 	    max_pattern_dep_length = len;
    117 
    118 	  if (p != 0 && p2 > p)
    119 	    {
    120 	      /* There is a slash before the % in the dep name.
    121 		 Extract the directory name.  */
    122 	      if (p == dep->name)
    123 		++p;
    124 	      if (p - dep->name > namelen)
    125 		{
    126 		  if (name != 0)
    127 		    free (name);
    128 		  namelen = p - dep->name;
    129 		  name = (char *) xmalloc (namelen + 1);
    130 		}
    131 	      bcopy (dep->name, name, p - dep->name);
    132 	      name[p - dep->name] = '\0';
    133 
    134 	      /* In the deps of an implicit rule the `changed' flag
    135 		 actually indicates that the dependency is in a
    136 		 nonexistent subdirectory.  */
    137 
    138 	      dep->changed = !dir_file_exists_p (name, "");
    139 	    }
    140 	  else
    141 	    /* This dependency does not reside in a subdirectory.  */
    142 	    dep->changed = 0;
    143 	}
    144 
    145       if (ndeps > max_pattern_deps)
    146 	max_pattern_deps = ndeps;
    147 
    148       lastrule = rule;
    149       rule = next;
    150     }
    151 
    152   if (name != 0)
    153     free (name);
    154 }
    155 
    156 /* Create a pattern rule from a suffix rule.
    158    TARGET is the target suffix; SOURCE is the source suffix.
    159    CMDS are the commands.
    160    If TARGET is nil, it means the target pattern should be `(%.o)'.
    161    If SOURCE is nil, it means there should be no deps.  */
    162 
    163 static void
    164 convert_suffix_rule (char *target, char *source, struct commands *cmds)
    165 {
    166   char *targname, *targpercent, *depname;
    167   char **names, **percents;
    168   struct dep *deps;
    169   unsigned int len;
    170 
    171   if (target == 0)
    172     /* Special case: TARGET being nil means we are defining a
    173        `.X.a' suffix rule; the target pattern is always `(%.o)'.  */
    174     {
    175 #ifdef VMS
    176       targname = savestring ("(%.obj)", 7);
    177 #else
    178       targname = savestring ("(%.o)", 5);
    179 #endif
    180       targpercent = targname + 1;
    181     }
    182   else
    183     {
    184       /* Construct the target name.  */
    185       len = strlen (target);
    186       targname = xmalloc (1 + len + 1);
    187       targname[0] = '%';
    188       bcopy (target, targname + 1, len + 1);
    189       targpercent = targname;
    190     }
    191 
    192   names = (char **) xmalloc (2 * sizeof (char *));
    193   percents = (char **) alloca (2 * sizeof (char *));
    194   names[0] = targname;
    195   percents[0] = targpercent;
    196   names[1] = percents[1] = 0;
    197 
    198   if (source == 0)
    199     deps = 0;
    200   else
    201     {
    202       /* Construct the dependency name.  */
    203       len = strlen (source);
    204       depname = xmalloc (1 + len + 1);
    205       depname[0] = '%';
    206       bcopy (source, depname + 1, len + 1);
    207       deps = alloc_dep ();
    208       deps->name = depname;
    209     }
    210 
    211   create_pattern_rule (names, percents, 0, deps, cmds, 0);
    212 }
    213 
    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.  */
    217 
    218 void
    219 convert_to_pattern (void)
    220 {
    221   register struct dep *d, *d2;
    222   register struct file *f;
    223   register char *rulename;
    224   register unsigned int slen, s2len;
    225 
    226   /* Compute maximum length of all the suffixes.  */
    227 
    228   maxsuffix = 0;
    229   for (d = suffix_file->deps; d != 0; d = d->next)
    230     {
    231       register unsigned int namelen = strlen (dep_name (d));
    232       if (namelen > maxsuffix)
    233 	maxsuffix = namelen;
    234     }
    235 
    236   rulename = (char *) alloca ((maxsuffix * 2) + 1);
    237 
    238   for (d = suffix_file->deps; d != 0; d = d->next)
    239     {
    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);
    243 
    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);
    248 
    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)
    253 	{
    254 	  s2len = strlen (dep_name (d2));
    255 
    256 	  if (slen == s2len && streq (dep_name (d), dep_name (d2)))
    257 	    continue;
    258 
    259 	  bcopy (dep_name (d2), rulename + slen, s2len + 1);
    260 	  f = lookup_file (rulename);
    261 	  if (f == 0 || f->cmds == 0)
    262 	    continue;
    263 
    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);
    270 
    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);
    274 	}
    275     }
    276 }
    277 
    278 
    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.  */
    286 
    287 int
    288 new_pattern_rule (struct rule *rule, int override)
    289 {
    290   register struct rule *r, *lastrule;
    291   register unsigned int i, j;
    292 
    293   rule->in_use = 0;
    294   rule->terminal = 0;
    295 
    296   rule->next = 0;
    297 
    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)
    302       {
    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.  */
    308 	  {
    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)
    315 	      {
    316 		/* All the dependencies matched.  */
    317 		if (override)
    318 		  {
    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;
    327 
    328 		    /* We got one.  Stop looking.  */
    329 		    goto matched;
    330 		  }
    331 		else
    332 		  {
    333 		    /* The old rule stays intact.  Destroy the new one.  */
    334 		    freerule (rule, (struct rule *) 0);
    335 		    return 0;
    336 		  }
    337 	      }
    338 	  }
    339       }
    340 
    341  matched:;
    342 
    343   if (r == 0)
    344     {
    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;
    351     }
    352 
    353   return 1;
    354 }
    355 
    356 
    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.  */
    361 
    362 void
    363 install_pattern_rule (struct pspec *p, int terminal)
    364 {
    365   register struct rule *r;
    366   char *ptr;
    367 
    368   r = (struct rule *) xmalloc (sizeof (struct rule));
    369 
    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));
    373 
    374   r->targets[1] = 0;
    375   r->suffixes[1] = 0;
    376   r->lens[1] = 0;
    377 
    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];
    388 
    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));
    393 
    394   if (new_pattern_rule (r, 0))
    395     {
    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;
    404     }
    405 }
    406 
    407 
    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.  */
    411 
    412 static void
    413 freerule (struct rule *rule, struct rule *lastrule)
    414 {
    415   struct rule *next = rule->next;
    416   register unsigned int i;
    417   register struct dep *dep;
    418 
    419   for (i = 0; rule->targets[i] != 0; ++i)
    420     free (rule->targets[i]);
    421 
    422   dep = rule->deps;
    423   while (dep)
    424     {
    425       struct dep *t;
    426 
    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       dep->name = 0; /* Make sure free_dep does not free name.  */
    431       free_dep (dep);
    432       dep = t;
    433     }
    434 
    435   free ((char *) rule->targets);
    436   free ((char *) rule->suffixes);
    437   free ((char *) rule->lens);
    438 
    439   /* We can't free the storage for the commands because there
    440      are ways that they could be in more than one place:
    441        * If the commands came from a suffix rule, they could also be in
    442        the `struct file's for other suffix rules or plain targets given
    443        on the same makefile line.
    444        * If two suffixes that together make a two-suffix rule were each
    445        given twice in the .SUFFIXES list, and in the proper order, two
    446        identical pattern rules would be created and the second one would
    447        be discarded here, but both would contain the same `struct commands'
    448        pointer from the `struct file' for the suffix rule.  */
    449 
    450   free ((char *) rule);
    451 
    452   if (pattern_rules == rule)
    453     if (lastrule != 0)
    454       abort ();
    455     else
    456       pattern_rules = next;
    457   else if (lastrule != 0)
    458     lastrule->next = next;
    459   if (last_pattern_rule == rule)
    460     last_pattern_rule = lastrule;
    461 }
    462 
    463 /* Create a new pattern rule with the targets in the nil-terminated
    465    array TARGETS.  If TARGET_PERCENTS is not nil, it is an array of
    466    pointers into the elements of TARGETS, where the `%'s are.
    467    The new rule has dependencies DEPS and commands from COMMANDS.
    468    It is a terminal rule if TERMINAL is nonzero.  This rule overrides
    469    identical rules with different commands if OVERRIDE is nonzero.
    470 
    471    The storage for TARGETS and its elements is used and must not be freed
    472    until the rule is destroyed.  The storage for TARGET_PERCENTS is not used;
    473    it may be freed.  */
    474 
    475 void
    476 create_pattern_rule (char **targets, char **target_percents,
    477 		     int terminal, struct dep *deps,
    478                      struct commands *commands, int override)
    479 {
    480   unsigned int max_targets, i;
    481   struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
    482 
    483   r->cmds = commands;
    484   r->deps = deps;
    485   r->targets = targets;
    486 
    487   max_targets = 2;
    488   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
    489   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
    490   for (i = 0; targets[i] != 0; ++i)
    491     {
    492       if (i == max_targets - 1)
    493 	{
    494 	  max_targets += 5;
    495 	  r->lens = (unsigned int *)
    496 	    xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
    497 	  r->suffixes = (char **)
    498 	    xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
    499 	}
    500       r->lens[i] = strlen (targets[i]);
    501       r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
    502 			: target_percents[i]) + 1;
    503       if (r->suffixes[i] == 0)
    504 	abort ();
    505     }
    506 
    507   if (i < max_targets - 1)
    508     {
    509       r->lens = (unsigned int *) xrealloc ((char *) r->lens,
    510 					   (i + 1) * sizeof (unsigned int));
    511       r->suffixes = (char **) xrealloc ((char *) r->suffixes,
    512 					(i + 1) * sizeof (char *));
    513     }
    514 
    515   if (new_pattern_rule (r, override))
    516     r->terminal = terminal;
    517 }
    518 
    519 /* Print the data base of rules.  */
    521 
    522 static void			/* Useful to call from gdb.  */
    523 print_rule (struct rule *r)
    524 {
    525   register unsigned int i;
    526   register struct dep *d;
    527 
    528   for (i = 0; r->targets[i] != 0; ++i)
    529     {
    530       fputs (r->targets[i], stdout);
    531       if (r->targets[i + 1] != 0)
    532 	putchar (' ');
    533       else
    534 	putchar (':');
    535     }
    536   if (r->terminal)
    537     putchar (':');
    538 
    539   for (d = r->deps; d != 0; d = d->next)
    540     printf (" %s", dep_name (d));
    541   putchar ('\n');
    542 
    543   if (r->cmds != 0)
    544     print_commands (r->cmds);
    545 }
    546 
    547 void
    548 print_rule_data_base (void)
    549 {
    550   register unsigned int rules, terminal;
    551   register struct rule *r;
    552 
    553   puts (_("\n# Implicit Rules"));
    554 
    555   rules = terminal = 0;
    556   for (r = pattern_rules; r != 0; r = r->next)
    557     {
    558       ++rules;
    559 
    560       putchar ('\n');
    561       print_rule (r);
    562 
    563       if (r->terminal)
    564 	++terminal;
    565     }
    566 
    567   if (rules == 0)
    568     puts (_("\n# No implicit rules."));
    569   else
    570     {
    571       printf (_("\n# %u implicit rules, %u"), rules, terminal);
    572 #ifndef	NO_FLOAT
    573       printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
    574 #else
    575       {
    576 	int f = (terminal * 1000 + 5) / rules;
    577 	printf (" (%d.%d%%)", f/10, f%10);
    578       }
    579 #endif
    580       puts (_(" terminal."));
    581     }
    582 
    583   if (num_pattern_rules != rules)
    584     {
    585       /* This can happen if a fatal error was detected while reading the
    586          makefiles and thus count_implicit_rule_limits wasn't called yet.  */
    587       if (num_pattern_rules != 0)
    588         fatal (NILF, _("BUG: num_pattern_rules wrong!  %u != %u"),
    589                num_pattern_rules, rules);
    590     }
    591 }
    592