home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / make3_60.lzh / MAKE3_60 / RULE.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  14KB  |  525 lines

  1. /* Pattern and suffix rule internals 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. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "rule.h"
  25.  
  26. static void freerule ();
  27. static int new_pattern_rule ();
  28.  
  29. /* Chain of all pattern rules.  */
  30.  
  31. struct rule *pattern_rules;
  32.  
  33. /* Pointer to last rule in the chain, so we can add onto the end.  */
  34.  
  35. struct rule *last_pattern_rule;
  36.  
  37. /* Number of rules in the chain.  */
  38.  
  39. unsigned int num_pattern_rules;
  40.  
  41. /* Maximum number of dependencies of any pattern rule.  */
  42.  
  43. unsigned int max_pattern_deps;
  44.  
  45. /* Maximum length of the name of a dependencies of any pattern rule.  */
  46.  
  47. unsigned int max_pattern_dep_length;
  48.  
  49. /* Pointer to structure for the file .SUFFIXES
  50.    whose dependencies are the suffixes to be searched.  */
  51.  
  52. struct file *suffix_file;
  53.  
  54. /* Maximum length of a suffix.  */
  55.  
  56. unsigned int maxsuffix;
  57.  
  58. /* Compute the maximum dependency length and maximum number of
  59.    dependencies of all implicit rules.  Also sets the subdir
  60.    flag for a rule when appropriate, possibly removing the rule
  61.    completely when appropriate.  */
  62.  
  63. void
  64. count_implicit_rule_limits ()
  65. {
  66.   char *name;
  67.   unsigned int namelen;
  68.   register struct rule *rule, *lastrule;
  69.  
  70.   num_pattern_rules = 0;
  71.   
  72.   name = 0;
  73.   namelen = 0;
  74.   rule = lastrule = pattern_rules;
  75.   while (rule != 0)
  76.     {
  77.       unsigned int ndeps = 0;
  78.       register struct dep *dep;
  79.       
  80.       ++num_pattern_rules;
  81.       
  82.       for (dep = rule->deps; dep != 0; dep = dep->next)
  83.     {
  84.       unsigned int len = strlen (dep->name);
  85.       char *p = rindex (dep->name, '/');
  86.       char *p2 = p != 0 ? index (dep->name, '%') : 0;
  87.  
  88.       ndeps++;
  89.  
  90.       if (len > max_pattern_dep_length)
  91.         max_pattern_dep_length = len;
  92.  
  93.       if (p != 0 && p2 > p)
  94.         {
  95.           if (p == dep->name)
  96.         ++p;
  97.           if (p - dep->name > namelen)
  98.         {
  99.           if (name != 0)
  100.             free (name);
  101.           namelen = p - dep->name;
  102.           name = (char *) xmalloc (namelen + 1);
  103.         }
  104.           bcopy (dep->name, name, p - dep->name);
  105.           name[p - dep->name] = '\0';
  106.  
  107.           if (!dir_file_exists_p (name, "."))
  108.         {
  109.           if (*name == '/')
  110.             {
  111.               freerule (rule, lastrule);
  112.               rule = lastrule;
  113.               goto end_main_loop;
  114.             }
  115.           else
  116.             rule->subdir = 1;
  117.         }
  118.         }
  119.     }
  120.  
  121.       if (ndeps > max_pattern_deps)
  122.     max_pattern_deps = ndeps;
  123.  
  124.     end_main_loop:;
  125.       lastrule = rule;
  126.       rule = rule->next;
  127.     }
  128.   
  129.   if (name != 0)
  130.     free (name);
  131. }
  132.  
  133. /* Convert old-style suffix rules to pattern rules.
  134.    All rules for the suffixes on the .SUFFIXES list
  135.    are converted and added to the chain of pattern rules.  */
  136.  
  137. void
  138. convert_to_pattern ()
  139. {
  140.   register struct dep *d, *d2, *newd;
  141.   register struct file *f;
  142.   register char *rulename;
  143.   register unsigned int slen, s2len;
  144.   register char *name, **names;
  145.  
  146.   /* Compute maximum length of all the suffixes.  */
  147.  
  148.   maxsuffix = 0;
  149.   for (d = suffix_file->deps; d != 0; d = d->next)
  150.     {
  151.       register unsigned int namelen = strlen (dep_name (d));
  152.       if (namelen > maxsuffix)
  153.     maxsuffix = namelen;
  154.     }
  155.  
  156.   rulename = (char *) alloca ((maxsuffix * 2) + 1);
  157.  
  158.   for (d = suffix_file->deps; d != 0; d = d->next)
  159.     {
  160.       /* Make a rule that is just the suffix, with no deps or commands.
  161.      This rule exists solely to disqualify match-anything rules.  */
  162.       slen = strlen (dep_name (d));
  163.       name = (char *) xmalloc (1 + slen + 1);
  164.       name[0] = '%';
  165.       bcopy (dep_name (d), name + 1, slen + 1);
  166.       names = (char **) xmalloc (2 * sizeof (char *));
  167.       names[0] = name;
  168.       names[1] = 0;
  169.       create_pattern_rule (names, (char **) 0, 0, (struct dep *) 0,
  170.                (struct commands *) 0, 0);
  171.  
  172.       f = d->file;
  173.       if (f->cmds != 0)
  174.     {
  175.       /* Record a pattern for this suffix's null-suffix rule.  */
  176.       newd = (struct dep *) xmalloc (sizeof (struct dep));
  177.       /* Construct this again rather than using the contents
  178.          of NAME (above), since that may have been freed by
  179.          create_pattern_rule.  */
  180.       newd->name = (char *) xmalloc (1 + slen + 1);
  181.       newd->name[0] = '%';
  182.       bcopy (dep_name (d), newd->name + 1, slen + 1);
  183.       newd->next = 0;
  184.       names = (char **) xmalloc (2 * sizeof (char *));
  185.       names[0] = savestring ("%", 1);
  186.       names[1] = 0;
  187.       create_pattern_rule (names, (char **) 0, 0, newd, f->cmds, 0);
  188.     }
  189.  
  190.       /* Record a pattern for each of this suffix's two-suffix rules.  */
  191.       bcopy (dep_name (d), rulename, slen);
  192.       for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  193.     {
  194.       s2len = strlen (dep_name (d2));
  195.  
  196.       if (slen == s2len && streq (dep_name (d), dep_name (d2)))
  197.         continue;
  198.  
  199.       bcopy (dep_name (d2), rulename + slen, s2len + 1);
  200.       f = lookup_file (rulename);
  201.       if (f == 0 || f->cmds == 0 || f->deps != 0)
  202.         continue;
  203.  
  204.       if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
  205.         /* The suffix rule `.X.a:' is converted
  206.            to the pattern rule `(%.o): %.X'.  */
  207.         name = savestring ("(%.o)", 5);
  208.       else
  209.         {
  210.           /* The suffix rule `.X.Y:' is converted
  211.          to the pattern rule `%.Y: %.X'.  */
  212.           name = (char *) xmalloc (1 + s2len + 1);
  213.           name[0] = '%';
  214.           bcopy (dep_name (d2), name + 1, s2len + 1);
  215.         }
  216.       names = (char **) xmalloc (2 * sizeof (char *));
  217.       names[0] = name;
  218.       names[1] = 0;
  219.       newd = (struct dep *) xmalloc (sizeof (struct dep));
  220.       newd->next = 0;
  221.       /* Construct this again (see comment above).  */
  222.       newd->name = (char *) xmalloc (1 + slen + 1);
  223.       newd->name[0] = '%';
  224.       bcopy (dep_name (d), newd->name + 1, slen + 1);
  225.       create_pattern_rule (names, (char **) 0, 0, newd, f->cmds, 0);
  226.     }
  227.     }
  228. }
  229.  
  230.  
  231. /* Install the pattern rule RULE (whose fields have been filled in)
  232.    at the end of the list (so that any rules previously defined
  233.    will take precedence).  If this rule duplicates a previous one
  234.    (identical target and dependents), the old one is replaced
  235.    if OVERRIDE is nonzero, otherwise this new one is thrown out.
  236.    When an old rule is replaced, the new one is put at the end of the
  237.    list.  Return nonzero if RULE is used; zero if not.  */
  238.  
  239. static int
  240. new_pattern_rule (rule, override)
  241.      register struct rule *rule;
  242.      int override;
  243. {
  244.   register struct rule *r, *lastrule;
  245.   register unsigned int i, j;
  246.  
  247.   rule->subdir = 0;
  248.   rule->in_use = 0;
  249.   rule->terminal = 0;
  250.  
  251.   rule->next = 0;
  252.  
  253.   /* Search for an identical rule.  */
  254.   lastrule = pattern_rules;
  255.   for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
  256.     for (i = 0; rule->targets[i] != 0; ++i)
  257.       for (j = 0; r->targets[j] != 0; ++j)
  258.     if (streq (rule->targets[i], r->targets[j]))
  259.       {
  260.         register struct dep *d, *d2;
  261.         for (d = rule->deps, d2 = r->deps;
  262.          d != 0 && d2 != 0; d = d->next, d2 = d2->next)
  263.           if (!streq (dep_name (d), dep_name (d2)))
  264.         break;
  265.         if (d == 0 && d2 == 0)
  266.           /* All the dependencies matched.  */
  267.           if (override)
  268.         {
  269.           /* Remove the old rule.  */
  270.           freerule (r, lastrule);
  271.           /* Install the new one.  */
  272.           if (pattern_rules == 0)
  273.             pattern_rules = rule;
  274.           else
  275.             last_pattern_rule->next = rule;
  276.           last_pattern_rule = rule;
  277.  
  278.           /* We got one.  Stop looking.  */
  279.           goto matched;
  280.         }
  281.           else
  282.         {
  283.           /* The old rule stays intact.  Destroy the new one.  */
  284.           freerule (rule, (struct rule *) 0);
  285.           return 0;
  286.         }
  287.       }
  288.  
  289.  matched:;
  290.  
  291.   if (r == 0)
  292.     {
  293.       /* There was no rule to replace.  */
  294.       if (pattern_rules == 0)
  295.     pattern_rules = rule;
  296.       else
  297.     last_pattern_rule->next = rule;
  298.       last_pattern_rule = rule;
  299.     }
  300.  
  301.   return 1;
  302. }
  303.  
  304.  
  305. /* Install an implicit pattern rule based on the three text strings
  306.    in the structure P points to.  These strings come from one of
  307.    the arrays of default implicit pattern rules.
  308.    TERMINAL specifies what the `terminal' field of the rule should be.  */
  309.  
  310. void
  311. install_pattern_rule (p, terminal)
  312.      struct pspec *p;
  313.      int terminal;
  314. {
  315.   register struct rule *r;
  316.   char *ptr;
  317.  
  318.   r = (struct rule *) xmalloc (sizeof (struct rule));
  319.  
  320.   r->targets = (char **) xmalloc (2 * sizeof (char *));
  321.   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
  322.   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
  323.  
  324.   r->targets[1] = 0;
  325.   r->suffixes[1] = 0;
  326.   r->lens[1] = 0;
  327.  
  328.   r->lens[0] = strlen (p->target);
  329.   /* These will all be string literals, but we malloc space for
  330.      them anyway because somebody might want to free them later on.  */
  331.   r->targets[0] = savestring (p->target, r->lens[0]);
  332.   r->suffixes[0] = find_percent (r->targets[0]);
  333.   if (r->suffixes[0] == 0)
  334.     /* Programmer-out-to-lunch error.  */
  335.     abort ();
  336.   else
  337.     ++r->suffixes[0];
  338.  
  339.   ptr = p->dep;
  340.   r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
  341.                                                        sizeof (struct dep)),
  342.                        sizeof (struct dep));
  343.  
  344.   if (new_pattern_rule (r, 0))
  345.     {
  346.       r->terminal = terminal;
  347.       r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
  348.       r->cmds->filename = 0;
  349.       r->cmds->lineno = 0;
  350.       /* These will all be string literals, but we malloc space for them
  351.      anyway because somebody might want to free them later.  */
  352.       r->cmds->commands = savestring (p->commands, strlen (p->commands));
  353.       r->cmds->command_lines = 0;
  354.     }
  355. }
  356.  
  357.  
  358. /* Free all the storage used in RULE and take it out of the
  359.    pattern_rules chain.  LASTRULE is the rule whose next pointer
  360.    points to RULE.  */
  361.  
  362. static void
  363. freerule (rule, lastrule)
  364.      register struct rule *rule, *lastrule;
  365. {
  366.   struct rule *next = rule->next;
  367.   register unsigned int i;
  368.  
  369.   for (i = 0; rule->targets[i] != 0; ++i)
  370.     free (rule->targets[i]);
  371.  
  372.   free ((char *) rule->targets);
  373.   free ((char *) rule->suffixes);
  374.   free ((char *) rule->lens);
  375.  
  376.   /* We can't free the storage for the commands because there
  377.      are ways that they could be in more than one place:
  378.        * If the commands came from a suffix rule, they could also be in
  379.        the `struct file's for other suffix rules or plain targets given
  380.        on the same makefile line.
  381.        * If two suffixes that together make a two-suffix rule were each
  382.        given twice in the .SUFFIXES list, and in the proper order, two
  383.        identical pattern rules would be created and the second one would
  384.        be discarded here, but both would contain the same `struct commands'
  385.        pointer from the `struct file' for the suffix rule.  */
  386.  
  387.   free ((char *) rule);
  388.  
  389.   if (lastrule == 0)
  390.     return;
  391.  
  392.   if (pattern_rules == rule)
  393.     if (lastrule != pattern_rules)
  394.       abort ();
  395.     else
  396.       pattern_rules = next;
  397.   else
  398.     lastrule->next = next;
  399.   if (last_pattern_rule == rule)
  400.     last_pattern_rule = lastrule;
  401. }
  402.  
  403. /* Create a new pattern rule with the targets in the nil-terminated
  404.    array TARGETS.  If TARGET_PERCENTS is not nil, it is an array of
  405.    pointers into the elements of TARGETS, where the `%'s are.
  406.    The new rule has dependencies DEPS and commands from COMMANDS.
  407.    It is a terminal rule if TERMINAL is nonzero.  This rule overrides
  408.    identical rules with different commands if OVERRIDE is nonzero.
  409.  
  410.    The storage for TARGETS and its elements is used and must not be freed
  411.    until the rule is destroyed.  The storage for TARGET_PERCENTS is not used;
  412.    it may be freed.  */
  413.  
  414. void
  415. create_pattern_rule (targets, target_percents,
  416.              terminal, deps, commands, override)
  417.      char **targets, **target_percents;
  418.      int terminal;
  419.      struct dep *deps;
  420.      struct commands *commands;
  421.      int override;
  422. {
  423.   register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
  424.   register unsigned int max_targets, i;
  425.  
  426.   r->cmds = commands;
  427.   r->deps = deps;
  428.   r->targets = targets;
  429.  
  430.   max_targets = 2;
  431.   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
  432.   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
  433.   for (i = 0; targets[i] != 0; ++i)
  434.     {
  435.       if (i == max_targets - 1)
  436.     {
  437.       max_targets += 5;
  438.       r->lens = (unsigned int *)
  439.         xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
  440.       r->suffixes = (char **)
  441.         xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
  442.     }
  443.       r->lens[i] = strlen (targets[i]);
  444.       r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
  445.             : target_percents[i]) + 1;
  446.       if (r->suffixes[i] == 0)
  447.     abort ();
  448.     }
  449.  
  450.   if (i < max_targets - 1)
  451.     {
  452.       r->lens = (unsigned int *) xrealloc ((char *) r->lens,
  453.                        (i + 1) * sizeof (unsigned int));
  454.       r->suffixes = (char **) xrealloc ((char *) r->suffixes,
  455.                     (i + 1) * sizeof (char *));
  456.     }
  457.  
  458.   if (new_pattern_rule (r, override))
  459.     r->terminal = terminal;
  460. }
  461.  
  462. /* Print the data base of rules.  */
  463.  
  464. void
  465. print_rule_data_base ()
  466. {
  467.   register unsigned int rules, terminal, subdir;
  468.   register struct rule *r;
  469.   register struct dep *d;
  470.   register unsigned int i;
  471.  
  472.   puts ("\n# Implicit Rules");
  473.  
  474.   rules = terminal = subdir = 0;
  475.   for (r = pattern_rules; r != 0; r = r->next)
  476.     {
  477.       ++rules;
  478.  
  479.       putchar ('\n');
  480.       for (i = 0; r->targets[i] != 0; ++i)
  481.     {
  482.       fputs (r->targets[i], stdout);
  483.       if (r->targets[i + 1] != 0)
  484.         putchar (' ');
  485.       else
  486.         putchar (':');
  487.     }
  488.       if (r->terminal)
  489.     {
  490.       ++terminal;
  491.       putchar (':');
  492.     }
  493.  
  494.       for (d = r->deps; d != 0; d = d->next)
  495.     printf (" %s", dep_name (d));
  496.       putchar ('\n');
  497.  
  498.       if (r->subdir)
  499.     {
  500.       ++subdir;
  501.       puts ("#  references nonexistent subdirectory.");
  502.     }
  503.  
  504.       if (r->cmds != 0)
  505.     print_commands (r->cmds);
  506.     }
  507.  
  508.   if (rules == 0)
  509.     puts ("\n# No implicit rules.");
  510.   else
  511.     {
  512.       printf ("\n# %u implicit rules, %u", rules, terminal);
  513. #ifndef    NO_FLOAT
  514.       printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
  515. #endif
  516.       puts (" terminal.");
  517.  
  518.       printf ("# %u", subdir);
  519. #ifndef    NO_FLOAT
  520.       printf (" (%.1f%%)", (double) subdir / (double) rules * 100.0);
  521. #endif
  522.       puts (" reference nonexistent subdirectories.");
  523.     }
  524. }
  525.