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