home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / make-3.71-src.lha / src / amiga / make-3.71 / implicit.c < prev    next >
C/C++ Source or Header  |  1994-04-21  |  19KB  |  591 lines

  1. /* Implicit rule searching for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "rule.h"
  21. #include "dep.h"
  22. #include "file.h"
  23.  
  24. static int pattern_search ();
  25.  
  26. /* For a FILE which has no commands specified, try to figure out some
  27.    from the implicit pattern rules.
  28.    Returns 1 if a suitable implicit rule was found,
  29.    after modifying FILE to contain the appropriate commands and deps,
  30.    or returns 0 if no implicit rule was found.  */
  31.  
  32. int
  33. try_implicit_rule (file, depth)
  34.      struct file *file;
  35.      unsigned int depth;
  36. {
  37.   DEBUGPR ("Looking for an implicit rule for `%s'.\n");
  38.  
  39.   /* The order of these searches was previously reversed.  My logic now is
  40.      that since the non-archive search uses more information in the target
  41.      (the archive search omits the archive name), it is more specific and
  42.      should come first.  */
  43.  
  44.   if (pattern_search (file, 0, depth, 0))
  45.     return 1;
  46.  
  47. #ifndef    NO_ARCHIVES
  48.   /* If this is an archive member reference, use just the
  49.      archive member name to search for implicit rules.  */
  50.   if (ar_name (file->name))
  51.     {
  52.       DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
  53.       if (pattern_search (file, 1, depth, 0))
  54.     return 1;
  55.     }
  56. #endif
  57.  
  58.   return 0;
  59. }
  60.  
  61. #define DEBUGP2(msg, a1, a2)                              \
  62.   do {                                          \
  63.     if (debug_flag)                                  \
  64.       { print_spaces (depth); printf (msg, a1, a2); fflush (stdout); }          \
  65.   } while (0)
  66.  
  67. /* Search the pattern rules for a rule with an existing dependency to make
  68.    FILE.  If a rule is found, the appropriate commands and deps are put in FILE
  69.    and 1 is returned.  If not, 0 is returned.
  70.  
  71.    If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)".  A rule for
  72.    "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
  73.    directory and filename parts.
  74.  
  75.    If an intermediate file is found by pattern search, the intermediate file
  76.    is set up as a target by the recursive call and is also made a dependency
  77.    of FILE.
  78.  
  79.    DEPTH is used for debugging messages.  */
  80.  
  81. static int
  82. pattern_search (file, archive, depth, recursions)
  83.      struct file *file;
  84.      int archive;
  85.      unsigned int depth;
  86.      unsigned int recursions;
  87. {
  88.   /* Filename we are searching for a rule for.  */
  89.   char *filename = archive ? index (file->name, '(') : file->name;
  90.  
  91.   /* Length of FILENAME.  */
  92.   unsigned int namelen = strlen (filename);
  93.  
  94.   /* The last slash in FILENAME (or nil if there is none).  */
  95.   char *lastslash;
  96.  
  97.   /* This is a file-object used as an argument in
  98.      recursive calls.  It never contains any data
  99.      except during a recursive call.  */
  100.   struct file *intermediate_file = 0;
  101.  
  102.   /* List of dependencies found recursively.  */
  103.   struct file **intermediate_files
  104.     = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
  105.  
  106.   /* List of the patterns used to find intermediate files.  */
  107.   char **intermediate_patterns
  108.     = (char **) alloca (max_pattern_deps * sizeof (char *));
  109.  
  110.   /* This buffer records all the dependencies actually found for a rule.  */
  111.   char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
  112.   /* Number of dep names now in FOUND_FILES.  */
  113.   unsigned int deps_found;
  114.  
  115.   /* Names of possible dependencies are constructed in this buffer.  */
  116.   register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
  117.  
  118.   /* The start and length of the stem of FILENAME for the current rule.  */
  119.   register char *stem;
  120.   register unsigned int stemlen;
  121.  
  122.   /* Buffer in which we store all the rules that are possibly applicable.  */
  123.   struct rule **tryrules
  124.     = (struct rule **) alloca (num_pattern_rules * max_pattern_targets
  125.                    * sizeof (struct rule *));
  126.  
  127.   /* Number of valid elements in TRYRULES.  */
  128.   unsigned int nrules;
  129.  
  130.   /* The numbers of the rule targets of each rule
  131.      in TRYRULES that matched the target file.  */
  132.   unsigned int *matches
  133.     = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
  134.  
  135.   /* Each element is nonzero if LASTSLASH was used in
  136.      matching the corresponding element of TRYRULES.  */
  137.   char *checked_lastslash
  138.     = (char *) alloca (num_pattern_rules * sizeof (char));
  139.  
  140.   /* The index in TRYRULES of the rule we found.  */
  141.   unsigned int foundrule;
  142.  
  143.   /* Nonzero if should consider intermediate files as dependencies.  */
  144.   int intermed_ok;
  145.  
  146.   /* Nonzero if we have matched a pattern-rule target
  147.      that is not just `%'.  */
  148.   int specific_rule_matched = 0;
  149.  
  150.   register unsigned int i;
  151.   register struct rule *rule;
  152.   register struct dep *dep;
  153.  
  154.   char *p;
  155.  
  156. #ifndef    NO_ARCHIVES
  157.   if (archive || ar_name (filename))
  158.     lastslash = 0;
  159.   else
  160. #endif
  161.     {
  162.       /* Set LASTSLASH to point at the last slash in FILENAME
  163.      but not counting any slash at the end.  (foo/bar/ counts as
  164.      bar/ in directory foo/, not empty in directory foo/bar/.)  */
  165.       lastslash = rindex (filename, '/');
  166.       if (lastslash != 0 && lastslash[1] == '\0')
  167.     lastslash = 0;
  168.     }
  169.  
  170.   /* First see which pattern rules match this target
  171.      and may be considered.  Put them in TRYRULES.  */
  172.  
  173.   nrules = 0;
  174.   for (rule = pattern_rules; rule != 0; rule = rule->next)
  175.     {
  176.       /* If the pattern rule has deps but no commands, ignore it.
  177.      Users cancel built-in rules by redefining them without commands.  */
  178.       if (rule->deps != 0 && rule->cmds == 0)
  179.     continue;
  180.  
  181.       /* If this rule is in use by a parent pattern_search,
  182.      don't use it here.  */
  183.       if (rule->in_use)
  184.     {
  185.       DEBUGP2 ("Avoiding implicit rule recursion.%s%s\n", "", "");
  186.       continue;
  187.     }
  188.  
  189.       for (i = 0; rule->targets[i] != 0; ++i)
  190.     {
  191.       char *target = rule->targets[i];
  192.       char *suffix = rule->suffixes[i];
  193.       int check_lastslash;
  194.  
  195.       /* Rules that can match any filename and are not terminal
  196.          are ignored if we're recursing, so that they cannot be
  197.          intermediate files.  */
  198.       if (recursions > 0 && target[1] == '\0' && !rule->terminal)
  199.         continue;
  200.  
  201.       if (rule->lens[i] > namelen)
  202.         /* It can't possibly match.  */
  203.         continue;
  204.  
  205.       /* From the lengths of the filename and the pattern parts,
  206.          find the stem: the part of the filename that matches the %.  */
  207.       stem = filename + (suffix - target - 1);
  208.       stemlen = namelen - rule->lens[i] + 1;
  209.  
  210.       /* Set CHECK_LASTSLASH if FILENAME contains a directory
  211.          prefix and the target pattern does not contain a slash.  */
  212.  
  213.       check_lastslash = lastslash != 0 && index (target, '/') == 0;
  214.       if (check_lastslash)
  215.         {
  216.           /* In that case, don't include the
  217.          directory prefix in STEM here.  */
  218.           unsigned int difference = lastslash - filename + 1;
  219.           if (difference > stemlen)
  220.         continue;
  221.           stemlen -= difference;
  222.           stem += difference;
  223.         }
  224.  
  225.       /* Check that the rule pattern matches the text before the stem.  */
  226.       if (check_lastslash)
  227.         {
  228.           if (stem > (lastslash + 1)
  229.           && strncmp (target, lastslash + 1, stem - lastslash - 1))
  230.         continue;
  231.         }
  232.       else if (stem > filename
  233.            && strncmp (target, filename, stem - filename))
  234.         continue;
  235.  
  236.       /* Check that the rule pattern matches the text after the stem.
  237.          We could test simply use streq, but this way we compare the
  238.          first two characters immediately.  This saves time in the very
  239.          common case where the first character matches because it is a
  240.          period.  */
  241.       if (*suffix != stem[stemlen]
  242.           || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
  243.         continue;
  244.  
  245.       /* Record if we match a rule that not all filenam