Ignore:
Timestamp:
Jun 20, 2012, 12:44:52 AM (13 years ago)
Author:
bird
Message:

gnumake/current -> 3.82-cvs.

Location:
vendor/gnumake/current
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • vendor/gnumake/current

    • Property svn:ignore deleted
  • vendor/gnumake/current/implicit.c

    r1989 r2596  
    11/* Implicit rule searching for GNU Make.
    22Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
    4 Foundation, Inc.
     31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
     42010 Free Software Foundation, Inc.
    55This file is part of GNU Make.
    66
     
    4949    return 1;
    5050
    51 #ifndef NO_ARCHIVES
     51#ifndef NO_ARCHIVES
    5252  /* If this is an archive member reference, use just the
    5353     archive member name to search for implicit rules.  */
     
    5757           _("Looking for archive-member implicit rule for `%s'.\n"));
    5858      if (pattern_search (file, 1, depth, 0))
    59         return 1;
     59        return 1;
    6060    }
    6161#endif
     
    6464}
    6565
    66 
    67 
    68 /* Struct idep captures information about implicit prerequisites
    69    that come from implicit rules. */
    70 struct idep
    71 {
    72   struct idep *next;              /* struct dep -compatible interface */
    73   const char *name;               /* name of the prerequisite */
    74   struct file *intermediate_file; /* intermediate file, 0 otherwise */
    75   const char *intermediate_pattern; /* pattern for intermediate file */
    76   unsigned char had_stem;         /* had % substituted with stem */
    77   unsigned char ignore_mtime;     /* ignore_mtime flag */
    78 };
    79 
    80 static void
    81 free_idep_chain (struct idep *p)
    82 {
    83   struct idep *n;
    84 
    85   for (; p != 0; p = n)
    86     {
    87       n = p->next;
    88       free (p);
    89     }
    90 }
    9166
    9267
     
    9570   length of the word.  */
    9671
    97 static char *
     72static const char *
    9873get_next_word (const char *buffer, unsigned int *length)
    9974{
     
    169144    *length = p - beg;
    170145
    171   return (char *)beg;
     146  return beg;
     147}
     148
     149/* This structure stores information about the expanded prerequisites for a
     150   pattern rule.  NAME is always set to the strcache'd name of the prereq.
     151   FILE and PATTERN will be set for intermediate files only.  IGNORE_MTIME is
     152   copied from the prerequisite we expanded.
     153 */
     154struct patdeps
     155  {
     156    const char *name;
     157    const char *pattern;
     158    struct file *file;
     159    unsigned int ignore_mtime : 1;
     160  };
     161
     162/* This structure stores information about pattern rules that we need
     163   to try.
     164*/
     165struct tryrule
     166  {
     167    struct rule *rule;
     168
     169    /* Index of the target in this rule that matched the file. */
     170    unsigned int matches;
     171
     172    /* Stem length for this match. */
     173    unsigned int stemlen;
     174
     175    /* Definition order of this rule. Used to implement stable sort.*/
     176    unsigned int order;
     177
     178    /* Nonzero if the LASTSLASH logic was used in matching this rule. */
     179    char checked_lastslash;
     180  };
     181
     182int
     183stemlen_compare (const void *v1, const void *v2)
     184{
     185  const struct tryrule *r1 = v1;
     186  const struct tryrule *r2 = v2;
     187  int r = r1->stemlen - r2->stemlen;
     188  return r != 0 ? r : (int)(r1->order - r2->order);
    172189}
    173190
     
    197214
    198215  /* The last slash in FILENAME (or nil if there is none).  */
    199   char *lastslash;
     216  const char *lastslash;
    200217
    201218  /* This is a file-object used as an argument in
    202219     recursive calls.  It never contains any data
    203220     except during a recursive call.  */
    204   struct file *intermediate_file = 0;
    205 
    206   /* This linked list records all the prerequisites actually
    207      found for a rule along with some other useful information
    208      (see struct idep for details). */
    209   struct idep* deps = 0;
    210 
    211   /* 1 if we need to remove explicit prerequisites, 0 otherwise. */
    212   unsigned int remove_explicit_deps = 0;
     221  struct file *int_file = 0;
     222
     223  /* List of dependencies found recursively.  */
     224  struct patdeps *deplist
     225    = xmalloc (max_pattern_deps * sizeof (struct patdeps));
     226  struct patdeps *pat = deplist;
     227
     228  /* All the prerequisites actually found for a rule, after expansion. */
     229  struct dep *deps;
    213230
    214231  /* Names of possible dependencies are constructed in this buffer.  */
     
    221238
    222239  /* Buffer in which we store all the rules that are possibly applicable.  */
    223   struct rule **tryrules = xmalloc (num_pattern_rules * max_pattern_targets
    224                                     * sizeof (struct rule *));
     240  struct tryrule *tryrules = xmalloc (num_pattern_rules * max_pattern_targets
     241                                      * sizeof (struct tryrule));
    225242
    226243  /* Number of valid elements in TRYRULES.  */
    227244  unsigned int nrules;
    228245
    229   /* The numbers of the rule targets of each rule
    230      in TRYRULES that matched the target file.  */
    231   unsigned int *matches = alloca (num_pattern_rules * sizeof (unsigned int));
    232 
    233   /* Each element is nonzero if LASTSLASH was used in
    234      matching the corresponding element of TRYRULES.  */
    235   char *checked_lastslash = alloca (num_pattern_rules * sizeof (char));
    236 
    237246  /* The index in TRYRULES of the rule we found.  */
    238247  unsigned int foundrule;
     
    240249  /* Nonzero if should consider intermediate files as dependencies.  */
    241250  int intermed_ok;
     251
     252  /* Nonzero if we have initialized file variables for this target.  */
     253  int file_vars_initialized = 0;
    242254
    243255  /* Nonzero if we have matched a pattern-rule target
     
    245257  int specific_rule_matched = 0;
    246258
     259  struct dep dep_simple;
     260
    247261  unsigned int ri;  /* uninit checks OK */
    248262  struct rule *rule;
    249   struct dep *dep, *expl_d;
    250 
    251   struct idep *d;
    252   struct idep **id_ptr;
    253   struct dep **d_ptr;
     263
     264  char *pathdir = NULL;
     265  unsigned long pathlen;
    254266
    255267  PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
    256268
    257 #ifndef NO_ARCHIVES
     269#ifndef NO_ARCHIVES
    258270  if (archive || ar_name (filename))
    259271    lastslash = 0;
     
    262274    {
    263275      /* Set LASTSLASH to point at the last slash in FILENAME
    264         but not counting any slash at the end.  (foo/bar/ counts as
    265         bar/ in directory foo/, not empty in directory foo/bar/.)  */
     276        but not counting any slash at the end.  (foo/bar/ counts as
     277        bar/ in directory foo/, not empty in directory foo/bar/.)  */
    266278#ifdef VMS
    267279      lastslash = strrchr (filename, ']');
    268280      if (lastslash == 0)
    269         lastslash = strrchr (filename, ':');
     281        lastslash = strrchr (filename, ':');
    270282#else
    271283      lastslash = strrchr (filename, '/');
    272284#ifdef HAVE_DOS_PATHS
    273285      /* Handle backslashes (possibly mixed with forward slashes)
    274         and the case of "d:file".  */
     286        and the case of "d:file".  */
    275287      {
    276         char *bslash = strrchr (filename, '\\');
    277         if (lastslash == 0 || bslash > lastslash)
    278           lastslash = bslash;
    279         if (lastslash == 0 && filename[0] && filename[1] == ':')
    280           lastslash = filename + 1;
     288        char *bslash = strrchr (filename, '\\');
     289        if (lastslash == 0 || bslash > lastslash)
     290          lastslash = bslash;
     291        if (lastslash == 0 && filename[0] && filename[1] == ':')
     292          lastslash = filename + 1;
    281293      }
    282294#endif
    283295#endif
    284296      if (lastslash != 0 && lastslash[1] == '\0')
    285         lastslash = 0;
     297        lastslash = 0;
    286298    }
    287299
    288   /* First see which pattern rules match this target
    289      and may be considered.  Put them in TRYRULES.  */
     300  pathlen = lastslash - filename + 1;
     301
     302  /* First see which pattern rules match this target and may be considered.
     303     Put them in TRYRULES.  */
    290304
    291305  nrules = 0;
     
    295309
    296310      /* If the pattern rule has deps but no commands, ignore it.
    297         Users cancel built-in rules by redefining them without commands.  */
     311        Users cancel built-in rules by redefining them without commands.  */
    298312      if (rule->deps != 0 && rule->cmds == 0)
    299         continue;
     313        continue;
    300314
    301315      /* If this rule is in use by a parent pattern_search,
    302         don't use it here.  */
     316        don't use it here.  */
    303317      if (rule->in_use)
    304         {
    305           DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
    306           continue;
    307         }
     318        {
     319          DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n")));
     320          continue;
     321        }
    308322
    309323      for (ti = 0; ti < rule->num; ++ti)
    310         {
    311           const char *target = rule->targets[ti];
    312           const char *suffix = rule->suffixes[ti];
    313           int check_lastslash;
    314 
    315           /* Rules that can match any filename and are not terminal
    316              are ignored if we're recursing, so that they cannot be
    317              intermediate files.  */
    318           if (recursions > 0 && target[1] == '\0' && !rule->terminal)
    319             continue;
    320 
    321           if (rule->lens[ti] > namelen)
    322             /* It can't possibly match.  */
    323             continue;
    324 
    325           /* From the lengths of the filename and the pattern parts,
    326              find the stem: the part of the filename that matches the %.  */
    327           stem = filename + (suffix - target - 1);
    328           stemlen = namelen - rule->lens[ti] + 1;
    329 
    330           /* Set CHECK_LASTSLASH if FILENAME contains a directory
    331              prefix and the target pattern does not contain a slash.  */
     324        {
     325          const char *target = rule->targets[ti];
     326          const char *suffix = rule->suffixes[ti];
     327          int check_lastslash;
     328
     329          /* Rules that can match any filename and are not terminal
     330             are ignored if we're recursing, so that they cannot be
     331             intermediate files.  */
     332          if (recursions > 0 && target[1] == '\0' && !rule->terminal)
     333            continue;
     334
     335          if (rule->lens[ti] > namelen)
     336            /* It can't possibly match.  */
     337            continue;
     338
     339          /* From the lengths of the filename and the pattern parts,
     340             find the stem: the part of the filename that matches the %.  */
     341          stem = filename + (suffix - target - 1);
     342          stemlen = namelen - rule->lens[ti] + 1;
     343
     344          /* Set CHECK_LASTSLASH if FILENAME contains a directory
     345             prefix and the target pattern does not contain a slash.  */
    332346
    333347          check_lastslash = 0;
     
    349363#endif
    350364            }
    351           if (check_lastslash)
    352             {
    353               /* If so, don't include the directory prefix in STEM here.  */
    354               unsigned int difference = lastslash - filename + 1;
    355               if (difference > stemlen)
    356                 continue;
    357               stemlen -= difference;
    358               stem += difference;
    359             }
    360 
    361           /* Check that the rule pattern matches the text before the stem.  */
    362           if (check_lastslash)
    363             {
    364               if (stem > (lastslash + 1)
    365                   && !strneq (target, lastslash + 1, stem - lastslash - 1))
    366                 continue;
    367             }
    368           else if (stem > filename
    369                    && !strneq (target, filename, stem - filename))
    370             continue;
    371 
    372           /* Check that the rule pattern matches the text after the stem.
    373              We could test simply use streq, but this way we compare the
    374              first two characters immediately.  This saves time in the very
    375              common case where the first character matches because it is a
    376              period.  */
    377           if (*suffix != stem[stemlen]
    378               || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
    379             continue;
    380 
    381           /* Record if we match a rule that not all filenames will match.  */
    382           if (target[1] != '\0')
    383             specific_rule_matched = 1;
    384 
    385           /* A rule with no dependencies and no commands exists solely to set
    386              specific_rule_matched when it matches.  Don't try to use it.  */
    387           if (rule->deps == 0 && rule->cmds == 0)
    388             continue;
    389 
    390           /* Record this rule in TRYRULES and the index of the matching
    391              target in MATCHES.  If several targets of the same rule match,
    392              that rule will be in TRYRULES more than once.  */
    393           tryrules[nrules] = rule;
    394           matches[nrules] = ti;
    395           checked_lastslash[nrules] = check_lastslash;
    396           ++nrules;
    397         }
     365          if (check_lastslash)
     366            {
     367              /* If so, don't include the directory prefix in STEM here.  */
     368              if (pathlen > stemlen)
     369                continue;
     370              stemlen -= pathlen;
     371              stem += pathlen;
     372            }
     373
     374          /* Check that the rule pattern matches the text before the stem.  */
     375          if (check_lastslash)
     376            {
     377              if (stem > (lastslash + 1)
     378                  && !strneq (target, lastslash + 1, stem - lastslash - 1))
     379                continue;
     380            }
     381          else if (stem > filename
     382                   && !strneq (target, filename, stem - filename))
     383            continue;
     384
     385          /* Check that the rule pattern matches the text after the stem.
     386             We could test simply use streq, but this way we compare the
     387             first two characters immediately.  This saves time in the very
     388             common case where the first character matches because it is a
     389             period.  */
     390          if (*suffix != stem[stemlen]
     391              || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
     392            continue;
     393
     394          /* Record if we match a rule that not all filenames will match.  */
     395          if (target[1] != '\0')
     396            specific_rule_matched = 1;
     397
     398          /* A rule with no dependencies and no commands exists solely to set
     399             specific_rule_matched when it matches.  Don't try to use it.  */
     400          if (rule->deps == 0 && rule->cmds == 0)
     401            continue;
     402
     403          /* Record this rule in TRYRULES and the index of the matching
     404             target in MATCHES.  If several targets of the same rule match,
     405             that rule will be in TRYRULES more than once.  */
     406          tryrules[nrules].rule = rule;
     407          tryrules[nrules].matches = ti;
     408          tryrules[nrules].stemlen = stemlen + (check_lastslash ? pathlen : 0);
     409          tryrules[nrules].order = nrules;
     410          tryrules[nrules].checked_lastslash = check_lastslash;
     411          ++nrules;
     412        }
    398413    }
     414
     415  /* Bail out early if we haven't found any rules. */
     416  if (nrules == 0)
     417    goto done;
     418
     419  /* Sort the rules to place matches with the shortest stem first. This
     420     way the most specific rules will be tried first. */
     421  if (nrules > 1)
     422    qsort (tryrules, nrules, sizeof (struct tryrule), stemlen_compare);
    399423
    400424  /* If we have found a matching rule that won't match all filenames,
     
    402426  if (specific_rule_matched)
    403427    for (ri = 0; ri < nrules; ++ri)
    404       if (!tryrules[ri]->terminal)
    405         {
    406           unsigned int j;
    407           for (j = 0; j < tryrules[ri]->num; ++j)
    408             if (tryrules[ri]->targets[j][1] == '\0')
     428      if (!tryrules[ri].rule->terminal)
     429        {
     430          unsigned int j;
     431          for (j = 0; j < tryrules[ri].rule->num; ++j)
     432            if (tryrules[ri].rule->targets[j][1] == '\0')
    409433              {
    410                 tryrules[ri] = 0;
     434                tryrules[ri].rule = 0;
    411435                break;
    412436              }
    413         }
    414 
    415   /* We are going to do second expansion so initialize file variables
    416      for the rule. */
    417   initialize_file_variables (file, 0);
     437        }
    418438
    419439  /* Try each rule once without intermediate files, then once with them.  */
    420   for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
     440  for (intermed_ok = 0; intermed_ok < 2; ++intermed_ok)
    421441    {
    422       /* Try each pattern rule till we find one that applies.
    423          If it does, expand its dependencies (as substituted)
    424          and chain them in DEPS.  */
    425 
     442      pat = deplist;
     443
     444      /* Try each pattern rule till we find one that applies.  If it does,
     445         expand its dependencies (as substituted) and chain them in DEPS.  */
    426446      for (ri = 0; ri < nrules; ri++)
    427         {
    428           struct file *f;
     447        {
     448          struct dep *dep;
     449          int check_lastslash;
    429450          unsigned int failed = 0;
    430           int check_lastslash;
    431451          int file_variables_set = 0;
    432 
    433           rule = tryrules[ri];
    434 
    435           remove_explicit_deps = 0;
    436 
    437           /* RULE is nil when we discover that a rule,
    438              already placed in TRYRULES, should not be applied.  */
    439           if (rule == 0)
    440             continue;
    441 
    442           /* Reject any terminal rules if we're
    443              looking to make intermediate files.  */
    444           if (intermed_ok && rule->terminal)
    445             continue;
    446 
    447           /* Mark this rule as in use so a recursive
    448              pattern_search won't try to use it.  */
    449           rule->in_use = 1;
    450 
    451           /* From the lengths of the filename and the matching pattern parts,
    452              find the stem: the part of the filename that matches the %.  */
    453           stem = filename
    454             + (rule->suffixes[matches[ri]] - rule->targets[matches[ri]]) - 1;
    455           stemlen = namelen - rule->lens[matches[ri]] + 1;
    456           check_lastslash = checked_lastslash[ri];
    457           if (check_lastslash)
    458             {
    459               stem += lastslash - filename + 1;
    460               stemlen -= (lastslash - filename) + 1;
    461             }
    462 
    463           DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
     452          unsigned int deps_found = 0;
     453          /* NPTR points to the part of the prereq we haven't processed.  */
     454          const char *nptr = 0;
     455          const char *dir = NULL;
     456          int order_only = 0;
     457          unsigned int matches;
     458
     459          rule = tryrules[ri].rule;
     460
     461          /* RULE is nil when we discover that a rule, already placed in
     462             TRYRULES, should not be applied.  */
     463          if (rule == 0)
     464            continue;
     465
     466          /* Reject any terminal rules if we're looking to make intermediate
     467             files.  */
     468          if (intermed_ok && rule->terminal)
     469            continue;
     470
     471          /* From the lengths of the filename and the matching pattern parts,
     472             find the stem: the part of the filename that matches the %.  */
     473          matches = tryrules[ri].matches;
     474          stem = filename + (rule->suffixes[matches]
     475                             - rule->targets[matches]) - 1;
     476          stemlen = (namelen - rule->lens[matches]) + 1;
     477          check_lastslash = tryrules[ri].checked_lastslash;
     478          if (check_lastslash)
     479            {
     480              stem += pathlen;
     481              stemlen -= pathlen;
     482
     483              /* We need to add the directory prefix, so set it up.  */
     484              if (! pathdir)
     485                {
     486                  pathdir = alloca (pathlen + 1);
     487                  memcpy (pathdir, filename, pathlen);
     488                  pathdir[pathlen] = '\0';
     489                }
     490              dir = pathdir;
     491            }
     492
     493          DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"),
    464494                             (int) stemlen, stem));
    465495
    466496          strncpy (stem_str, stem, stemlen);
    467497          stem_str[stemlen] = '\0';
     498
     499          /* If there are no prerequisites, then this rule matches.  */
     500          if (rule->deps == 0)
     501            break;
    468502
    469503          /* Temporary assign STEM to file->stem (needed to set file
     
    471505          file->stem = stem_str;
    472506
    473           /* Try each dependency; see if it "exists".  */
    474 
    475           for (dep = rule->deps; dep != 0; dep = dep->next)
    476             {
    477               unsigned int len;
     507          /* Mark this rule as in use so a recursive pattern_search won't try
     508             to use it.  */
     509          rule->in_use = 1;
     510
     511          /* Try each prerequisite; see if it exists or can be created.  We'll
     512             build a list of prereq info in DEPLIST.  Due to 2nd expansion we
     513             may have to process multiple prereqs for a single dep entry.  */
     514
     515          pat = deplist;
     516          dep = rule->deps;
     517          nptr = dep_name (dep);
     518          while (1)
     519            {
     520              struct dep *dl, *d;
    478521              char *p;
    479               char *p2;
    480               unsigned int order_only = 0; /* Set if '|' was seen. */
    481 
    482               /* In an ideal world we would take the dependency line,
    483                  substitute the stem, re-expand the whole line and chop it
    484                  into individual prerequisites. Unfortunately this won't work
    485                  because of the "check_lastslash" twist.  Instead, we will
    486                  have to go word by word, taking $()'s into account, for each
    487                  word we will substitute the stem, re-expand, chop it up, and,
    488                  if check_lastslash != 0, add the directory part to each
     522
     523              /* If we're out of name to parse, start the next prereq.  */
     524              if (! nptr)
     525                {
     526                  dep = dep->next;
     527                  if (dep == 0)
     528                    break;
     529                  nptr = dep_name (dep);
     530                }
     531
     532              /* If we don't need a second expansion, just replace the %.  */
     533              if (! dep->need_2nd_expansion)
     534                {
     535                  dep_simple = *dep;
     536                  dep_simple.next = 0;
     537                  p = strchr (nptr, '%');
     538                  if (p == 0)
     539                    dep_simple.name = nptr;
     540                  else
     541                    {
     542                      char *o = depname;
     543                      if (check_lastslash)
     544                        {
     545                          memcpy (o, filename, pathlen);
     546                          o += pathlen;
     547                        }
     548                      memcpy (o, nptr, p - nptr);
     549                      o += p - nptr;
     550                      memcpy (o, stem_str, stemlen);
     551                      o += stemlen;
     552                      strcpy (o, p + 1);
     553                      dep_simple.name = strcache_add (depname);
     554                    }
     555                  dl = &dep_simple;
     556
     557                  /* We've used up this dep, so next time get a new one.  */
     558                  nptr = 0;
     559                  ++deps_found;
     560                }
     561
     562              /* We have to perform second expansion on this prereq.  In an
     563                 ideal world we would take the dependency line, substitute the
     564                 stem, re-expand the whole line and chop it into individual
     565                 prerequisites.  Unfortunately this won't work because of the
     566                 "check_lastslash" twist.  Instead, we will have to go word by
     567                 word, taking $()'s into account.  For each word we will
     568                 substitute the stem, re-expand, chop it up, and, if
     569                 check_lastslash != 0, add the directory part to each
    489570                 resulting prerequisite.  */
    490 
    491               p = get_next_word (dep->name, &len);
    492 
    493               while (1)
     571              else
    494572                {
    495573                  int add_dir = 0;
    496                   int had_stem = 0;
    497 
     574                  unsigned int len;
     575
     576                  nptr = get_next_word (nptr, &len);
     577                  if (nptr == 0)
     578                    continue;
     579
     580                  /* See this is a transition to order-only prereqs.  */
     581                  if (! order_only && len == 1 && nptr[0] == '|')
     582                    {
     583                      order_only = 1;
     584                      nptr += len;
     585                      continue;
     586                    }
     587
     588                  /* If the dependency name has %, substitute the stem.  If we
     589                     just replace % with the stem value then later, when we do
     590                     the 2nd expansion, we will re-expand this stem value
     591                     again.  This is not good if you have certain characters
     592                     in your stem (like $).
     593
     594                     Instead, we will replace % with $* and allow the second
     595                     expansion to take care of it for us.  This way (since $*
     596                     is a simple variable) there won't be additional
     597                     re-expansion of the stem.  */
     598
     599                  p = lindex (nptr, nptr + len, '%');
    498600                  if (p == 0)
    499                     break; /* No more words */
    500 
    501                   /* Is there a pattern in this prerequisite?  */
    502 
    503                   for (p2 = p; p2 < p + len && *p2 != '%'; ++p2)
    504                     ;
    505 
    506                   if (dep->need_2nd_expansion)
    507                     {
    508                       /* If the dependency name has %, substitute the stem.
    509 
    510                          Watch out, we are going to do something tricky
    511                          here. If we just replace % with the stem value,
    512                          later, when we do the second expansion, we will
    513                          re-expand this stem value once again. This is not
    514                          good especially if you have certain characters in
    515                          your stem (like $).
    516 
    517                          Instead, we will replace % with $* and allow the
    518                          second expansion to take care of it for us. This way
    519                          (since $* is a simple variable) there won't be
    520                          additional re-expansion of the stem.  */
    521 
    522                       if (p2 < p + len)
    523                         {
    524                           unsigned int i = p2 - p;
    525                           memcpy (depname, p, i);
    526                           memcpy (depname + i, "$*", 2);
    527                           memcpy (depname + i + 2, p2 + 1, len - i - 1);
    528                           depname[len + 2 - 1] = '\0';
    529 
    530                           if (check_lastslash)
    531                             add_dir = 1;
    532 
    533                           had_stem = 1;
    534                         }
    535                       else
    536                         {
    537                           memcpy (depname, p, len);
    538                           depname[len] = '\0';
    539                         }
    540 
    541                       /* Set file variables. Note that we cannot do it once
    542                          at the beginning of the function because of the stem
    543                          value.  */
    544                       if (!file_variables_set)
    545                         {
    546                           set_file_variables (file);
    547                           file_variables_set = 1;
    548                         }
    549 
    550                       p2 = variable_expand_for_file (depname, file);
     601                    {
     602                      memcpy (depname, nptr, len);
     603                      depname[len] = '\0';
    551604                    }
    552605                  else
    553606                    {
    554                        if (p2 < p + len)
    555                         {
    556                           unsigned int i = p2 - p;
    557                           memcpy (depname, p, i);
    558                           memcpy (depname + i, stem_str, stemlen);
    559                           memcpy (depname + i + stemlen, p2 + 1, len - i - 1);
    560                           depname[len + stemlen - 1] = '\0';
    561 
    562                           if (check_lastslash)
    563                             add_dir = 1;
    564 
    565                           had_stem = 1;
    566                         }
    567                       else
    568                         {
    569                           memcpy (depname, p, len);
    570                           depname[len] = '\0';
    571                         }
    572 
    573                        p2 = depname;
    574                     }
    575 
    576                   /* Parse the dependencies. */
    577 
    578                   while (1)
    579                     {
    580                       id_ptr = &deps;
    581 
    582                       for (; *id_ptr; id_ptr = &(*id_ptr)->next)
    583                         ;
    584 
    585                       *id_ptr = (struct idep *)
    586                         multi_glob (
    587                           parse_file_seq (&p2,
    588                                           order_only ? '\0' : '|',
    589                                           sizeof (struct idep),
    590                                           1), sizeof (struct idep));
    591 
    592                       /* @@ It would be nice to teach parse_file_seq or
    593                          multi_glob to add prefix. This would save us some
    594                          reallocations. */
    595 
    596                       if (order_only || add_dir || had_stem)
    597                         {
    598                           unsigned long l = lastslash - filename + 1;
    599 
    600                           for (d = *id_ptr; d != 0; d = d->next)
    601                             {
    602                               if (order_only)
    603                                 d->ignore_mtime = 1;
    604 
    605                               if (add_dir)
    606                                 {
    607                                   char *n = alloca (strlen (d->name) + l + 1);
    608                                   memcpy (n, filename, l);
    609                                   memcpy (n+l, d->name, strlen (d->name) + 1);
    610                                   d->name = strcache_add (n);
    611                                 }
    612 
    613                               if (had_stem)
    614                                 d->had_stem = 1;
    615                             }
    616                         }
    617 
    618                       if (!order_only && *p2)
     607                      unsigned int i = p - nptr;
     608                      memcpy (depname, nptr, i);
     609                      memcpy (depname + i, "$*", 2);
     610                      memcpy (depname + i + 2, p + 1, len - i - 1);
     611                      depname[len + 2 - 1] = '\0';
     612
     613                      if (check_lastslash)
     614                        add_dir = 1;
     615                    }
     616
     617                  /* Initialize and set file variables if we haven't already
     618                     done so. */
     619                  if (!file_vars_initialized)
     620                    {
     621                      initialize_file_variables (file, 0);
     622                      set_file_variables (file);
     623                      file_vars_initialized = 1;
     624                    }
     625                  /* Update the stem value in $* for this rule.  */
     626                  else if (!file_variables_set)
     627                    {
     628                      define_variable_for_file (
     629                        "*", 1, file->stem, o_automatic, 0, file);
     630                      file_variables_set = 1;
     631                    }
     632
     633                  /* Perform the 2nd expansion.  */
     634                  p = variable_expand_for_file (depname, file);
     635
     636                  /* Parse the expanded string. */
     637                  dl = PARSE_FILE_SEQ (&p, struct dep, order_only ? '\0' : '|',
     638                                       add_dir ? dir : NULL, 0);
     639
     640                  for (d = dl; d != NULL; d = d->next)
     641                    {
     642                      ++deps_found;
     643                      if (order_only)
     644                        d->ignore_mtime = 1;
     645                    }
     646
     647                  /* Set up for the next word.  */
     648                  nptr += len;
     649                }
     650
     651              /* If there are more than max_pattern_deps prerequisites (due to
     652                 2nd expansion), reset it and realloc the arrays.  */
     653
     654              if (deps_found > max_pattern_deps)
     655                {
     656                  unsigned int l = pat - deplist;
     657                  deplist = xrealloc (deplist,
     658                                      deps_found * sizeof (struct patdeps));
     659                  pat = deplist + l;
     660                  max_pattern_deps = deps_found;
     661                }
     662
     663              /* Go through the nameseq and handle each as a prereq name.  */
     664              for (d = dl; d != 0; d = d->next)
     665                {
     666                  struct dep *expl_d;
     667                  int is_rule = d->name == dep_name (dep);
     668
     669                  if (file_impossible_p (d->name))
     670                    {
     671                      /* If this prereq has already been ruled "impossible",
     672                         then the rule fails.  Don't bother trying it on the
     673                         second pass either since we know that will fail.  */
     674                      DBS (DB_IMPLICIT,
     675                           (is_rule
     676                            ? _("Rejecting impossible rule prerequisite `%s'.\n")
     677                            : _("Rejecting impossible implicit prerequisite `%s'.\n"),
     678                            d->name));
     679                      tryrules[ri].rule = 0;
     680
     681                      failed = 1;
     682                      break;
     683                    }
     684
     685                  memset (pat, '\0', sizeof (struct patdeps));
     686                  pat->ignore_mtime = d->ignore_mtime;
     687
     688                  DBS (DB_IMPLICIT,
     689                       (is_rule
     690                        ? _("Trying rule prerequisite `%s'.\n")
     691                        : _("Trying implicit prerequisite `%s'.\n"), d->name));
     692
     693                  /* If this prereq is also explicitly mentioned for FILE,
     694                     skip all tests below since it must be built no matter
     695                     which implicit rule we choose. */
     696
     697                  for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
     698                    if (streq (dep_name (expl_d), d->name))
     699                      break;
     700                  if (expl_d != 0)
     701                    {
     702                      (pat++)->name = d->name;
     703                      continue;
     704                    }
     705
     706                  /* The DEP->changed flag says that this dependency resides
     707                     in a nonexistent directory.  So we normally can skip
     708                     looking for the file.  However, if CHECK_LASTSLASH is
     709                     set, then the dependency file we are actually looking for
     710                     is in a different directory (the one gotten by prepending
     711                     FILENAME's directory), so it might actually exist.  */
     712
     713                  /* @@ dep->changed check is disabled. */
     714                  if (lookup_file (d->name) != 0
     715                      /*|| ((!dep->changed || check_lastslash) && */
     716                      || file_exists_p (d->name))
     717                    {
     718                      (pat++)->name = d->name;
     719                      continue;
     720                    }
     721
     722                  /* This code, given FILENAME = "lib/foo.o", dependency name
     723                     "lib/foo.c", and VPATH=src, searches for
     724                     "src/lib/foo.c".  */
     725                  {
     726                    const char *vname = vpath_search (d->name, 0, NULL, NULL);
     727                    if (vname)
    619728                      {
    620                         ++p2;
    621                         order_only = 1;
     729                        DBS (DB_IMPLICIT,
     730                             (_("Found prerequisite `%s' as VPATH `%s'\n"),
     731                              d->name, vname));
     732                        (pat++)->name = d->name;
    622733                        continue;
    623734                      }
    624 
    625                       break;
    626                     }
    627 
    628                   p += len;
    629                   p = get_next_word (p, &len);
    630                 }
    631             }
    632 
    633           /* Reset the stem in FILE. */
    634 
    635           file->stem = 0;
    636 
    637           /* @@ This loop can be combined with the previous one. I do
    638              it separately for now for transparency.*/
    639 
    640           for (d = deps; d != 0; d = d->next)
    641             {
    642               const char *name = d->name;
    643 
    644               if (file_impossible_p (name))
    645                 {
    646                   /* If this dependency has already been ruled "impossible",
    647                      then the rule fails and don't bother trying it on the
    648                      second pass either since we know that will fail too.  */
    649                   DBS (DB_IMPLICIT,
    650                        (d->had_stem
    651                         ? _("Rejecting impossible implicit prerequisite `%s'.\n")
    652                         : _("Rejecting impossible rule prerequisite `%s'.\n"),
    653                         name));
    654                   tryrules[ri] = 0;
    655 
     735                  }
     736
     737                  /* We could not find the file in any place we should look.
     738                     Try to make this dependency as an intermediate file, but
     739                     only on the second pass.  */
     740
     741                  if (intermed_ok)
     742                    {
     743                      DBS (DB_IMPLICIT,
     744                           (_("Looking for a rule with intermediate file `%s'.\n"),
     745                            d->name));
     746
     747                      if (int_file == 0)
     748                        int_file = alloca (sizeof (struct file));
     749                      memset (int_file, '\0', sizeof (struct file));
     750                      int_file->name = d->name;
     751
     752                      if (pattern_search (int_file,
     753                                          0,
     754                                          depth + 1,
     755                                          recursions + 1))
     756                        {
     757                          pat->pattern = int_file->name;
     758                          int_file->name = d->name;
     759                          pat->file = int_file;
     760                          (pat++)->name = d->name;
     761                          int_file = 0;
     762                          continue;
     763                        }
     764
     765                      /* If we have tried to find P as an intermediate file
     766                         and failed, mark that name as impossible so we won't
     767                         go through the search again later.  */
     768                      if (int_file->variables)
     769                        free_variable_set (int_file->variables);
     770                      if (int_file->pat_variables)
     771                        free_variable_set (int_file->pat_variables);
     772                      file_impossible (d->name);
     773                    }
     774
     775                  /* A dependency of this rule does not exist. Therefore, this
     776                     rule fails.  */
    656777                  failed = 1;
    657778                  break;
    658779                }
    659780
    660               DBS (DB_IMPLICIT,
    661                    (d->had_stem
    662                     ? _("Trying implicit prerequisite `%s'.\n")
    663                     : _("Trying rule prerequisite `%s'.\n"), name));
    664 
    665               /* If this prerequisite also happened to be explicitly mentioned
    666                  for FILE skip all the test below since it it has to be built
    667                  anyway, no matter which implicit rule we choose. */
    668 
    669               for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
    670                 if (streq (dep_name (expl_d), name))
    671                   break;
    672               if (expl_d != 0)
    673                 continue;
    674 
    675               /* The DEP->changed flag says that this dependency resides in a
    676                  nonexistent directory.  So we normally can skip looking for
    677                  the file.  However, if CHECK_LASTSLASH is set, then the
    678                  dependency file we are actually looking for is in a different
    679                  directory (the one gotten by prepending FILENAME's directory),
    680                  so it might actually exist.  */
    681 
    682               /* @@ dep->changed check is disabled. */
    683               if (((f = lookup_file (name)) != 0 && f->is_target)
    684                   /*|| ((!dep->changed || check_lastslash) && */
    685                   || file_exists_p (name))
    686                 continue;
    687 
    688               /* This code, given FILENAME = "lib/foo.o", dependency name
    689                  "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
    690               {
    691                 const char *vname = vpath_search (name, 0);
    692                 if (vname)
    693                   {
    694                     DBS (DB_IMPLICIT,
    695                          (_("Found prerequisite `%s' as VPATH `%s'\n"),
    696                           name, vname));
    697                     continue;
    698                   }
    699               }
    700 
    701 
    702               /* We could not find the file in any place we should look.  Try
    703                  to make this dependency as an intermediate file, but only on
    704                  the second pass.  */
    705 
    706               if (intermed_ok)
    707                 {
    708                   if (intermediate_file == 0)
    709                     intermediate_file = alloca (sizeof (struct file));
    710 
    711                   DBS (DB_IMPLICIT,
    712                        (_("Looking for a rule with intermediate file `%s'.\n"),
    713                         name));
    714 
    715                   memset (intermediate_file, '\0', sizeof (struct file));
    716                   intermediate_file->name = name;
    717                   if (pattern_search (intermediate_file,
    718                                       0,
    719                                       depth + 1,
    720                                       recursions + 1))
    721                     {
    722                       d->intermediate_pattern = intermediate_file->name;
    723                       intermediate_file->name = strcache_add (name);
    724                       d->intermediate_file = intermediate_file;
    725                       intermediate_file = 0;
    726 
    727                       continue;
    728                     }
    729 
    730                   /* If we have tried to find P as an intermediate
    731                      file and failed, mark that name as impossible
    732                      so we won't go through the search again later.  */
    733                   if (intermediate_file->variables)
    734                     free_variable_set (intermediate_file->variables);
    735                   file_impossible (name);
    736                 }
    737 
    738               /* A dependency of this rule does not exist. Therefore,
    739                  this rule fails.  */
    740               failed = 1;
    741               break;
     781              /* Free the ns chain.  */
     782              if (dl != &dep_simple)
     783                free_dep_chain (dl);
     784
     785              if (failed)
     786                break;
    742787            }
    743788
     789          /* Reset the stem in FILE. */
     790
     791          file->stem = 0;
     792
    744793          /* This rule is no longer `in use' for recursive searches.  */
    745           rule->in_use = 0;
    746 
    747           if (failed)
    748             {
    749               /* This pattern rule does not apply. If some of its
    750                  dependencies succeeded, free the data structure
    751                  describing them.  */
    752               free_idep_chain (deps);
    753               deps = 0;
    754             }
    755           else
    756             /* This pattern rule does apply.  Stop looking for one.  */
    757             break;
    758         }
    759 
    760       /* If we found an applicable rule without
    761          intermediate files, don't try with them.  */
     794          rule->in_use = 0;
     795
     796          if (! failed)
     797            /* This pattern rule does apply.  Stop looking for one.  */
     798            break;
     799
     800          /* This pattern rule does not apply. If some of its dependencies
     801             succeeded, free the data structure describing them.  */
     802          /* free_idep_chain (deps); */
     803          deps = 0;
     804        }
     805
     806      /* If we found an applicable rule without intermediate files, don't try
     807         with them.  */
    762808      if (ri < nrules)
    763         break;
     809        break;
    764810
    765811      rule = 0;
    766812    }
    767813
    768   /* RULE is nil if the loop went all the way
    769      through the list and everything failed.  */
     814  /* RULE is nil if the loop went through the list but everything failed.  */
    770815  if (rule == 0)
    771816    goto done;
     
    773818  foundrule = ri;
    774819
    775   /* If we are recursing, store the pattern that matched
    776      FILENAME in FILE->name for use in upper levels.  */
     820  /* If we are recursing, store the pattern that matched FILENAME in
     821     FILE->name for use in upper levels.  */
    777822
    778823  if (recursions > 0)
    779824    /* Kludge-o-matic */
    780     file->name = rule->targets[matches[foundrule]];
    781 
    782   /* FOUND_FILES lists the dependencies for the rule we found.
    783      This includes the intermediate files, if any.
    784      Convert them into entries on the deps-chain of FILE.  */
    785 
    786   if (remove_explicit_deps)
     825    file->name = rule->targets[tryrules[foundrule].matches];
     826
     827  /* DEPLIST lists the prerequisites for the rule we found.  This includes the
     828     intermediate files, if any.  Convert them into entries on the deps-chain
     829     of FILE.  */
     830
     831  while (pat-- > deplist)
    787832    {
    788       /* Remove all the dependencies that didn't come from
    789          this implicit rule. */
    790 
    791       dep = file->deps;
    792       while (dep != 0)
     833      struct dep *dep;
     834      const char *s;
     835
     836      if (pat->file != 0)
    793837        {
    794           struct dep *next = dep->next;
    795           free_dep (dep);
    796           dep = next;
    797         }
    798       file->deps = 0;
    799   }
    800 
    801   expl_d = file->deps; /* We will add them at the end. */
    802   d_ptr = &file->deps;
    803 
    804   for (d = deps; d != 0; d = d->next)
    805     {
    806       const char *s;
    807 
    808       if (d->intermediate_file != 0)
    809         {
    810           /* If we need to use an intermediate file,
    811              make sure it is entered as a target, with the info that was
    812              found for it in the recursive pattern_search call.
    813              We know that the intermediate file did not already exist as
    814              a target; therefore we can assume that the deps and cmds
    815              of F below are null before we change them.  */
    816 
    817           struct file *imf = d->intermediate_file;
    818           register struct file *f = lookup_file (imf->name);
     838          /* If we need to use an intermediate file, make sure it is entered
     839             as a target, with the info that was found for it in the recursive
     840             pattern_search call.  We know that the intermediate file did not
     841             already exist as a target; therefore we can assume that the deps
     842             and cmds of F below are null before we change them.  */
     843
     844          struct file *imf = pat->file;
     845          struct file *f = lookup_file (imf->name);
    819846
    820847          /* We don't want to delete an intermediate file that happened
     
    824851            f->precious = 1;
    825852          else
    826             f = enter_file (strcache_add (imf->name));
    827 
    828           f->deps = imf->deps;
    829           f->cmds = imf->cmds;
    830           f->stem = imf->stem;
     853            f = enter_file (imf->name);
     854
     855          f->deps = imf->deps;
     856          f->cmds = imf->cmds;
     857          f->stem = imf->stem;
     858          f->variables = imf->variables;
     859          f->pat_variables = imf->pat_variables;
     860          f->pat_searched = imf->pat_searched;
    831861          f->also_make = imf->also_make;
    832862          f->is_target = 1;
    833 
    834           if (!f->precious)
     863          f->intermediate = 1;
     864          f->tried_implicit = 1;
     865
     866          imf = lookup_file (pat->pattern);
     867          if (imf != 0 && imf->precious)
     868            f->precious = 1;
     869
     870          for (dep = f->deps; dep != 0; dep = dep->next)
    835871            {
    836               imf = lookup_file (d->intermediate_pattern);
    837               if (imf != 0 && imf->precious)
    838                 f->precious = 1;
     872              dep->file = enter_file (dep->name);
     873              dep->name = 0;
     874              dep->file->tried_implicit |= dep->changed;
    839875            }
    840 
    841           f->intermediate = 1;
    842           f->tried_implicit = 1;
    843           for (dep = f->deps; dep != 0; dep = dep->next)
    844             {
    845               dep->file = enter_file (dep->name);
    846               dep->name = 0;
    847               dep->file->tried_implicit |= dep->changed;
    848             }
    849         }
     876        }
    850877
    851878      dep = alloc_dep ();
    852       dep->ignore_mtime = d->ignore_mtime;
    853       s = d->name; /* Hijacking the name. */
    854       d->name = 0;
    855       if (recursions == 0)
    856         {
    857           dep->file = lookup_file (s);
    858           if (dep->file == 0)
    859             dep->file = enter_file (s);
    860         }
     879      dep->ignore_mtime = pat->ignore_mtime;
     880      s = strcache_add (pat->name);
     881      if (recursions)
     882        dep->name = s;
    861883      else
    862         dep->name = s;
    863 
    864       if (d->intermediate_file == 0 && tryrules[foundrule]->terminal)
    865         {
    866           /* If the file actually existed (was not an intermediate file),
    867              and the rule that found it was a terminal one, then we want
    868              to mark the found file so that it will not have implicit rule
    869              search done for it.  If we are not entering a `struct file' for
    870              it now, we indicate this with the `changed' flag.  */
    871           if (dep->file == 0)
    872             dep->changed = 1;
    873           else
    874             dep->file->tried_implicit = 1;
    875         }
    876 
    877       *d_ptr = dep;
    878       d_ptr = &dep->next;
     884        {
     885          dep->file = lookup_file (s);
     886          if (dep->file == 0)
     887            dep->file = enter_file (s);
     888        }
     889
     890      if (pat->file == 0 && tryrules[foundrule].rule->terminal)
     891        {
     892          /* If the file actually existed (was not an intermediate file), and
     893             the rule that found it was a terminal one, then we want to mark
     894             the found file so that it will not have implicit rule search done
     895             for it.  If we are not entering a `struct file' for it now, we
     896             indicate this with the `changed' flag.  */
     897          if (dep->file == 0)
     898            dep->changed = 1;
     899          else
     900            dep->file->tried_implicit = 1;
     901        }
     902
     903      dep->next = file->deps;
     904      file->deps = dep;
    879905    }
    880906
    881   *d_ptr = expl_d;
    882 
    883   if (!checked_lastslash[foundrule])
     907  if (!tryrules[foundrule].checked_lastslash)
    884908    {
    885       /* Always allocate new storage, since STEM might be
    886          on the stack for an intermediate file.  */
     909      /* Always allocate new storage, since STEM might be on the stack for an
     910         intermediate file.  */
    887911      file->stem = strcache_add_len (stem, stemlen);
    888912      fullstemlen = stemlen;
     
    894918
    895919      /* We want to prepend the directory from
    896         the original FILENAME onto the stem.  */
     920        the original FILENAME onto the stem.  */
    897921      fullstemlen = dirlen + stemlen;
    898922      sp = alloca (fullstemlen + 1);
     
    908932  /* Set precious flag. */
    909933  {
    910     struct file *f = lookup_file (rule->targets[matches[foundrule]]);
     934    struct file *f = lookup_file (rule->targets[tryrules[foundrule].matches]);
    911935    if (f && f->precious)
    912936      file->precious = 1;
     
    918942  if (rule->num > 1)
    919943    for (ri = 0; ri < rule->num; ++ri)
    920       if (ri != matches[foundrule])
    921         {
    922           char *p = alloca (rule->lens[ri] + fullstemlen + 1);
    923           struct file *f;
    924           struct dep *new = alloc_dep ();
    925 
    926           /* GKM FIMXE: handle '|' here too */
    927           memcpy (p, rule->targets[ri],
     944      if (ri != tryrules[foundrule].matches)
     945        {
     946          char *nm = alloca (rule->lens[ri] + fullstemlen + 1);
     947          char *p = nm;
     948          struct file *f;
     949          struct dep *new = alloc_dep ();
     950
     951          /* GKM FIMXE: handle '|' here too */
     952          memcpy (p, rule->targets[ri],
    928953                  rule->suffixes[ri] - rule->targets[ri] - 1);
    929           p += rule->suffixes[ri] - rule->targets[ri] - 1;
    930           memcpy (p, file->stem, fullstemlen);
    931           p += fullstemlen;
    932           memcpy (p, rule->suffixes[ri],
     954          p += rule->suffixes[ri] - rule->targets[ri] - 1;
     955          memcpy (p, file->stem, fullstemlen);
     956          p += fullstemlen;
     957          memcpy (p, rule->suffixes[ri],
    933958                  rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1);
    934           new->name = strcache_add (p);
    935           new->file = enter_file (new->name);
    936           new->next = file->also_make;
    937 
    938           /* Set precious flag. */
    939           f = lookup_file (rule->targets[ri]);
    940           if (f && f->precious)
     959          new->name = strcache_add (nm);
     960          new->file = enter_file (new->name);
     961          new->next = file->also_make;
     962
     963          /* Set precious flag. */
     964          f = lookup_file (rule->targets[ri]);
     965          if (f && f->precious)
    941966            new->file->precious = 1;
    942967
    943           /* Set the is_target flag so that this file is not treated
    944              as intermediate by the pattern rule search algorithm and
     968          /* Set the is_target flag so that this file is not treated as
     969             intermediate by the pattern rule search algorithm and
    945970             file_exists_p cannot pick it up yet.  */
    946971          new->file->is_target = 1;
    947972
    948           file->also_make = new;
    949         }
     973          file->also_make = new;
     974        }
    950975
    951976 done:
    952   free_idep_chain (deps);
    953977  free (tryrules);
     978  free (deplist);
    954979
    955980  return rule != 0;
Note: See TracChangeset for help on using the changeset viewer.