home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / IMPLICIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  18.8 KB  |  610 lines

  1. /* Implicit rule searching for GNU Make.
  2. Copyright (C) 1988, 1989, 1990 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 1, 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. /*
  20.  * MS-DOS port (c) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>
  21.  *
  22.  * To this port, the same copying conditions apply as to the
  23.  * original release.
  24.  *
  25.  * IMPORTANT:
  26.  * This file is not identical to the original GNU release!
  27.  * You should have received this code as patch to the official
  28.  * GNU release.
  29.  *
  30.  * MORE IMPORTANT:
  31.  * This port comes with ABSOLUTELY NO WARRANTY.
  32.  *
  33.  * $Header: e:/gnu/make/RCS/implicit.c'v 3.58.0.1 90/07/17 00:59:17 tho Exp $
  34.  */
  35.  
  36. #include "make.h"
  37. #include "rule.h"
  38. #include "dep.h"
  39. #include "file.h"
  40.  
  41. #ifdef MSDOS
  42. static  int pattern_search (struct file *file, char *name, unsigned int depth, unsigned int recursions);
  43. #else /* not MSDOS */
  44. static int pattern_search ();
  45. #endif /* not MSDOS */
  46.  
  47. /* For a FILE which has no commands specified, try to figure out some
  48.    from the implicit pattern rules.
  49.    Returns 1 if a suitable implicit rule was found,
  50.    after modifying FILE to contain the appropriate commands and deps,
  51.    or returns 0 if no implicit rule was found.  */
  52.  
  53. int
  54. try_implicit_rule (file, depth)
  55.      struct file *file;
  56.      unsigned int depth;
  57. {
  58.   register char *filename;
  59.  
  60.   DEBUGPR ("Looking for an implicit rule for `%s'.\n");
  61.  
  62. #ifndef    NO_ARCHIVES
  63.   /* If this is an archive member reference, use just the
  64.      archive member name to search for implicit rules.  */
  65.   if (ar_name (file->name))
  66.     {
  67.       filename = index (file->name, '(');
  68.       DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
  69.       if (pattern_search (file, filename, depth, 0))
  70.     return 1;
  71.     }
  72. #endif
  73.  
  74.   return pattern_search (file, (char *) 0, depth, 0);
  75. }
  76.  
  77. #define DEBUGP2(msg, a1, a2) \
  78.   if (debug_flag) \
  79.     { print_spaces (depth); printf (msg, a1, a2); fflush (stdout);  } else
  80.  
  81. /* Search the pattern rules for a rule with an existing dependency to make
  82.    NAME.  If NAME is nil, FILE->name is used.  If a rule is found, the
  83.    appropriate commands and deps are put in FILE and 1 is returned.  If not,
  84.    0 is returned.
  85.  
  86.    If an intermediate file is found by pattern search, the intermediate file
  87.    is set up as a target by the recursive call and is also made a dependency
  88.    of FILE.
  89.  
  90.    DEPTH is used for debugging messages.  */
  91.  
  92. static int
  93. pattern_search (file, name, depth, recursions)
  94.      struct file *file;
  95.      char *name;
  96.      unsigned int depth;
  97.      unsigned int recursions;
  98. {
  99.   /* Filename we are searching for a rule for.  */
  100.   char *filename = name != 0 ? name : file->name;
  101.  
  102.   /* Length of FILENAME.  */
  103.   unsigned int namelen = strlen (filename);
  104.  
  105.   /* The last slash in FILENAME (or nil if there is none).  */
  106.   char *lastslash;
  107.  
  108.   /* This is a file-object used as an argument in
  109.      recursive calls.  It never contains any data
  110.      except during a recursive call.  */
  111.   struct file *intermediate_file = 0;
  112.  
  113.   /* List of dependencies found recursively.  */
  114.   struct file **intermediate_files
  115.     = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
  116.  
  117.   /* List of the patterns used to find intermediate files.  */
  118.   char **intermediate_patterns
  119.     = (char **) alloca (max_pattern_deps * sizeof (char *));
  120.  
  121.   /* This buffer records all the dependencies actually found for a rule.  */
  122.   char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
  123.   /* Number of dep names now in FOUND_FILES.  */
  124.   unsigned int deps_found;
  125.  
  126.   /* Names of possible dependencies are constructed in this buffer.  */
  127.   register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
  128.  
  129.   /* The start and length of the stem of FILENAME for the current rule.  */
  130.   register char *stem;
  131.   register unsigned int stemlen;
  132.  
  133.   /* Buffer in which we store all the rules that are possibly applicable.  */
  134.   struct rule **tryrules
  135.     = (struct rule **) alloca (num_pattern_rules * sizeof (struct rule *));
  136.  
  137.   /* Number of valid elements in TRYRULES.  */
  138.   unsigned int nrules;
  139.  
  140.   /* The numbers of the rule targets of each rule
  141.      in TRYRULES that matched the target file.  */
  142.   unsigned int *matches
  143.     = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
  144.  
  145.   /* Each element is nonzero if LASTSLASH was used in
  146.      matching the corresponding element of TRYRULES.  */
  147.   char *checked_lastslash
  148.     = (char *) alloca (num_pattern_rules * sizeof (char));
  149.  
  150.   /* The index in TRYRULES of the rule we found.  */
  151.   unsigned int foundrule;
  152.  
  153.   /* Nonzero if should consider intermediate files as dependencies.  */
  154.   int intermed_ok;
  155.  
  156.   /* Nonzero if we have matched a pattern-rule target
  157.      that is not just `%'.  */
  158.   int specific_rule_matched = 0;
  159.  
  160.   register unsigned int i;
  161.   register struct rule *rule;
  162.   register struct dep *dep;
  163.  
  164.   char *p;
  165.  
  166. #ifndef    NO_ARCHIVES
  167.   if (ar_name (filename))
  168.     lastslash = 0;
  169.   else
  170. #endif
  171.     {
  172.       /* Set LASTSLASH to point at the last slash in FILENAME
  173.      but not counting any slash at the end.  (foo/bar/ counts as
  174.      bar/ in directory foo/, not empty in directory foo/bar/.)  */
  175.       lastslash = rindex (filename, '/');
  176.       if (lastslash != 0 && lastslash[1] == '\0')
  177.     lastslash = 0;
  178.     }
  179.  
  180.   /* First see which pattern rules match this target
  181.      and may be considered.  Put them in TRYRULES.  */
  182.  
  183.   nrules = 0;
  184.   for (rule = pattern_rules; rule != 0; rule = rule->next)
  185.     {
  186.       int specific_rule_may_have_matched = 0;
  187.       int check_lastslash;
  188.  
  189.       /* If the pattern rule has deps but no commands, ignore it.
  190.      Users cancel built-in rules by redefining them without commands.  */
  191.       if (rule->deps != 0 && rule->cmds == 0)
  192.     continue;
  193.  
  194.       /* If this rule is in use by a parent pattern_search,
  195.      don't use it here.  */
  196.       if (rule->in_use)
  197.     {
  198.       DEBUGP2 ("Avoiding implicit rule recursion.\n", 0, 0);
  199.       continue;
  200.     }
  201.  
  202.       for (i = 0; rule->targets[i] != 0; ++i)
  203.     {
  204.       char *target = rule->targets[i];
  205.       char *suffix = rule->suffixes[i];
  206.  
  207.       /* Rules that can match any filename and are not terminal
  208.          are ignored if we're recursing, so that they cannot be
  209.          intermediate files.  */
  210.       if (recursions > 0 && target[1] == '\0' && !rule->terminal)
  211.         continue;
  212.  
  213.       if (rule->lens[i] > namelen)
  214.         /* It can't possibly match.  */
  215.         continue;
  216.  
  217.       /* From the lengths of the filename and the pattern parts,
  218.          find the stem: the part of the filename that matches the %.  */
  219.       stem = filename + (suffix - target - 1);
  220.       stemlen = namelen - rule->lens[i] + 1;
  221.  
  222.       /* Set CHECK_LASTSLASH if FILENAME contains a directory
  223.          prefix and the target pattern does not contain a slash.  */
  224.  
  225.       check_lastslash = lastslash != 0 && index (target, '/') == 0;
  226.       if (check_lastslash)
  227.         {
  228.           /* In that case, don't include the
  229.          directory prefix in STEM here.  */
  230.           unsigned int difference = lastslash - filename + 1;
  231.           if (difference > stemlen)
  232.         continue;
  233.           stemlen -= difference;
  234.           stem += difference;
  235.         }
  236.  
  237.       /* Check that the rule pattern matches the text before the stem.  */
  238.       if (check_lastslash)
  239.         {
  240.           if (stem > (lastslash + 1)
  241.           && strncmp (target, lastslash + 1, stem - lastslash - 1))
  242.         continue;
  243.         }
  244.       else if (stem > filename
  245.            && strncmp (target, filename, stem - filename))
  246.         continue;
  247.  
  248.       /* Check that the rule pattern matches the text after the stem.
  249.          We could test simply use streq, but this way we compare the
  250.          first two characters immediately.  This saves time in the very
  251.          common case where the first character matches because it is a
  252.          period.  */
  253.       if (*suffix != stem[stemlen]
  254.           || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
  255.         continue;
  256.  
  257.       /* Record if we match a rule that not all filenames will match.  */
  258.       if (target[1] != '\0')
  259.         specific_rule_may_have_matched = 1;
  260.  
  261.       /* We have a matching target.  Don't search for any more.  */
  262.       break;
  263.     }
  264.  
  265.       /* None of the targets matched.  */
  266.       if (rule->targets[i] == 0)
  267.     continue;
  268.  
  269.       specific_rule_matched |= specific_rule_may_have_matched;
  270.  
  271.       /* A rule with no dependencies and no commands exists solely to set
  272.      specific_rule_matched when it matches.  Don't try to use it.  */
  273.       if (rule->deps == 0 && rule->cmds == 0)
  274.     continue;
  275.  
  276.       /* Record this rule in TRYRULES and the index
  277.      of the (first) matching target in MATCHES.  */
  278.       tryrules[nrules] = rule;
  279.       matches[nrules] = i;
  280.       checked_lastslash[nrules] = check_lastslash;
  281.       ++nrules;
  282.     }
  283.  
  284.   /* If we have found a matching rule that won't match all filenames,
  285.      retroactively reject any "terminal" rules that do always match.  */
  286.   if (specific_rule_matched)
  287.     for (i = 0; i < nrules; ++i)
  288.       if (!tryrules[i]->terminal)
  289.     {
  290.       register unsigned int j;
  291.       for (j = 0; tryrules[i]->targets[j] != 0; ++j)
  292.         if (tryrules[i]->targets[j][1] == '\0')
  293.           break;
  294.       if (tryrules[i]->targets[j] != 0)
  295.         tryrules[i] = 0;
  296.     }
  297.  
  298.   /* Try each rule once without intermediate files, then once with them.  */
  299.   for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
  300.     {
  301.       /* Try each pattern rule till we find one that applies.
  302.      If it does, copy the names of its dependencies (as substituted)
  303.      and store them in FOUND_FILES.  DEPS_FOUND is the number of them.  */
  304.  
  305.       for (i = 0; i < nrules; i++)
  306.     {
  307.       int check_lastslash;
  308.  
  309.       rule = tryrules[i];
  310.  
  311.       /* RULE is nil when we discover that a rule,
  312.          already placed in TRYRULES, should not be applied.  */
  313.       if (rule == 0)
  314.         continue;
  315.  
  316.       /* Reject any terminal rules if we're
  317.          looking to make intermediate files.  */
  318.       if (intermed_ok && rule->terminal)
  319.         continue;
  320.  
  321.       /* Mark this rule as in use so a recursive
  322.          pattern_search won't try to use it.  */
  323.       rule->in_use = 1;
  324.  
  325.       /* From the lengths of the filename and the matching pattern parts,
  326.          find the stem: the part of the filename that matches the %.  */
  327.       stem = filename
  328.         + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
  329.       stemlen = namelen - rule->lens[matches[i]] + 1;
  330.       check_lastslash = (lastslash != 0
  331.                  && index (rule->targets[matches[i]], '/') == 0);
  332.       if (check_lastslash)
  333.         {
  334.           stem += lastslash - filename + 1;
  335.           stemlen -= (lastslash - filename) + 1;
  336.         }
  337.  
  338.       DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
  339.            stemlen, stem);
  340.  
  341.       /* Try each dependency; see if it "exists".  */
  342.  
  343.       deps_found = 0;
  344.       for (dep = rule->deps; dep != 0; dep = dep->next)
  345.         {
  346.           /* If the dependency name has a %, substitute the stem.  */
  347.           p = index (dep_name (dep), '%');
  348.           if (p != 0)
  349.         {
  350.           register unsigned int i;
  351.           if (check_lastslash)
  352.             {
  353.               i = lastslash - filename + 1;
  354.               bcopy (filename, depname, i);
  355.             }
  356.           else
  357.             i = 0;
  358.           bcopy (dep_name (dep), depname + i, p - dep_name (dep));
  359.           i += p - dep_name (dep);
  360.           bcopy (stem, depname + i, stemlen);
  361.           i += stemlen;
  362.           strcpy (depname + i, p + 1);
  363.           p = depname;
  364.         }
  365.           else
  366.         p = dep_name (dep);
  367.  
  368.           /* P is now the actual dependency name as substituted.  */
  369.  
  370.           if (file_impossible_p (p))
  371.         {
  372.           /* If this dependency has already been ruled
  373.              "impossible", then the rule fails and don't
  374.              bother trying it on the second pass either
  375.              since we know that will fail too.  */
  376.           DEBUGP2 ("Rejecting impossible %s dependent `%s'.\n",
  377.                p == depname ? "implicit" : "rule", p);
  378.           tryrules[i] = 0;
  379.           break;
  380.         }
  381.  
  382.           intermediate_files[deps_found] = 0;
  383.  
  384.           DEBUGP2 ("Trying %s dependency `%s'.\n",
  385.                p == depname ? "implicit" : "rule", p);
  386.           if (!rule->subdir && lookup_file (p) != 0 || file_exists_p (p))
  387.         {
  388.           found_files[deps_found++] = savestring (p, strlen (p));
  389.           continue;
  390.         }
  391.           /* This code, given FILENAME = "lib/foo.o", dependency name
  392.          "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
  393.           if (vpath_search (&p))
  394.         {
  395.           DEBUGP2 ("Found dependent as `%s'.\n", p, 0);
  396.           found_files[deps_found++] = p;
  397.           continue;
  398.         }
  399.  
  400.           /* We could not find the file in any place we should look.
  401.          Try to make this dependency as an intermediate file,
  402.          but only on the second pass.  */
  403.  
  404.           if (intermed_ok && p == depname)
  405.         {
  406.           if (intermediate_file == 0)
  407.             intermediate_file
  408.               = (struct file *) alloca (sizeof (struct file));
  409.  
  410.           DEBUGP2 ("Looking for a rule with intermediate file `%s'.\n",
  411.                p, 0);
  412.  
  413.           bzero ((char *) intermediate_file, sizeof (struct file));
  414.           intermediate_file->name = p;
  415.           if (pattern_search (intermediate_file, (char *) 0,
  416.                       depth + 1, recursions + 1))
  417.             {
  418.               p = savestring (p, strlen (p));
  419.               intermediate_patterns[deps_found]
  420.             = intermediate_file->name;
  421.               found_files[deps_found] = p;
  422.               intermediate_file->name = p;
  423.               intermediate_files[deps_found] = intermediate_file;
  424.               intermediate_file = 0;
  425.               ++deps_found;
  426.               continue;
  427.             }
  428.  
  429.           /* If we have tried to find P as an intermediate
  430.              file and failed, mark that name as impossible
  431.              so we won't go through the search again later.  */
  432.           file_impossible (p);
  433.         }
  434.  
  435.           /* A dependency of this rule does not exist.
  436.          Therefore, this rule fails.  */
  437.           break;
  438.         }
  439.  
  440.       /* This rule is no longer `in use' for recursive searches.  */
  441.       rule->in_use = 0;
  442.  
  443.       if (dep != 0)
  444.         {
  445.           /* This pattern rule does not apply.
  446.          If some of its dependencies succeeded,
  447.          free the data structure describing them.  */
  448.           while (deps_found-- > 0)
  449.         {
  450.           register struct file *f = intermediate_files[deps_found];
  451.           free (found_files[deps_found]);
  452.           if (f != 0
  453.               && (f->stem < f->name
  454.               || f->stem > f->name + strlen (f->name)))
  455.             free (f->stem);
  456.         }
  457.         }
  458.       else
  459.         /* This pattern rule does apply.  Stop looking for one.  */
  460.         break;
  461.     }
  462.  
  463.       /* If we found an applicable rule without
  464.      intermediate files, don't try with them.  */
  465.       if (i < nrules)
  466.     break;
  467.  
  468.       rule = 0;
  469.     }
  470.  
  471.   /* RULE is nil if the loop went all the way
  472.      through the list and everything failed.  */
  473.   if (rule == 0)
  474.     return 0;
  475.  
  476.   foundrule = i;
  477.  
  478.   /* If we are recursing, store the pattern that matched
  479.      FILENAME in FILE->name for use in upper levels.  */
  480.  
  481.   if (recursions > 0)
  482.     /* Kludge-o-matic */
  483.     file->name = rule->targets[matches[foundrule]];
  484.  
  485.   /* FOUND_FILES lists the dependencies for the rule we found.
  486.      This includes the intermediate files, if any.
  487.      Convert them into entries on the deps-chain of FILE.  */
  488.  
  489.   while (deps_found-- > 0)
  490.     {
  491.       register char *s;
  492.  
  493.       if (intermediate_files[deps_found] != 0)
  494.     {
  495.       /* If we need to use an intermediate file,
  496.          make sure it is entered as a target, with the info that was
  497.          found for it in the recursive pattern_search call.
  498.          We know that the intermediate file did not already exist as
  499.          a target; therefore we can assume that the deps and cmds
  500.          of F below are null before we change them.  */
  501.  
  502.       struct file *imf = intermediate_files[deps_found];
  503.       register struct file *f = enter_file (imf->name);
  504.       f->deps = imf->deps;
  505.       f->cmds = imf->cmds;
  506.       f->stem = imf->stem;
  507.       imf = lookup_file (intermediate_patterns[deps_found]);
  508.       if (imf != 0 && imf->precious)
  509.         f->precious = 1;
  510.       f->intermediate = 1;
  511.       f->tried_implicit = 1;
  512.       for (dep = f->deps; dep != 0; dep = dep->next)
  513.         {
  514.           dep->file = enter_file (dep->name);
  515.           dep->name = 0;
  516.           dep->file->tried_implicit |= dep->changed;
  517.         }
  518.       num_intermediates++;
  519.     }
  520.  
  521.       dep = (struct dep *) xmalloc (sizeof (struct dep));
  522.       s = found_files[deps_found];
  523.       if (recursions == 0)
  524.     {
  525.       dep->name = 0;
  526.       dep->file = enter_file (s);    
  527.     }
  528.       else
  529.     {
  530.       dep->name = s;
  531.       dep->file = 0;
  532.       dep->changed = 0;
  533.     }
  534.       if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
  535.     {
  536.       /* If the file actually existed (was not an intermediate file),
  537.          and the rule that found it was a terminal one, then we want
  538.          to mark the found file so that it will not have implicit rule
  539.          search done for it.  If we are not entering a `struct file' for
  540.          it now, we indicate this with the `changed' flag.  */
  541.       if (dep->file == 0)
  542.         dep->changed = 1;
  543.       else
  544.         dep->file->tried_implicit = 1;
  545.     }
  546.       dep->next = file->deps;
  547.       file->deps = dep;
  548.     }
  549.  
  550.   uniquize_deps (file->deps);
  551.  
  552.   if (!checked_lastslash[foundrule])
  553.     file->stem = stem[stemlen] == '\0' ? stem : savestring (stem, stemlen);
  554.   else
  555.     {
  556.       file->stem = (char *) xmalloc (((lastslash + 1) - filename)
  557.                      + stemlen + 1);
  558.       bcopy (filename, file->stem, (lastslash + 1) - filename);
  559.       bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
  560.       file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
  561.     }
  562.  
  563.   file->cmds = rule->cmds;
  564.  
  565.   /* Put the targets other than the one that
  566.      matched into FILE's `also_make' member.  */
  567.  
  568.   /* If there was only one target, there is nothing to do.  */
  569.   if (rule->targets[1] != 0)
  570.     {
  571.       unsigned int max_targets = 2;
  572.       register unsigned int idx;
  573.  
  574.       file->also_make = (char **) xmalloc (2 * sizeof (char *));
  575.  
  576.       idx = 0;
  577.       for (i = 0; rule->targets[i] != 0; ++i)
  578.     if (i == matches[foundrule])
  579.       continue;
  580.     else
  581.       {
  582.         if (i == max_targets - 1)
  583.           {
  584.         max_targets += 5;
  585.         file->also_make
  586.           = (char **) xrealloc ((char *) file->also_make,
  587.                     max_targets * sizeof (char *));
  588.           }
  589.  
  590.         p = file->also_make[idx++] = (char *) xmalloc (rule->lens[i] +
  591.                                stemlen + 1);
  592.         bcopy (rule->targets[i], p,
  593.            rule->suffixes[i] - rule->targets[i] - 1);
  594.         p += rule->suffixes[i] - rule->targets[i] - 1;
  595.         bcopy (stem, p, stemlen);
  596.         p += stemlen;
  597.         bcopy (rule->suffixes[i], p,
  598.            rule->lens[i]
  599.            - (rule->suffixes[i] - rule->targets[i] - 1) + 1);
  600.       }
  601.  
  602.       file->also_make[idx] = 0;
  603.       if (idx < max_targets - 1)
  604.     file->also_make = (char **) xrealloc ((char *) file->also_make,
  605.                           (idx + 1) * sizeof (char *));
  606.     }
  607.  
  608.   return 1;
  609. }
  610.