Changeset 503 for trunk/src/gmake/read.c


Ignore:
Timestamp:
Sep 15, 2006, 7:09:38 AM (19 years ago)
Author:
bird
Message:

Untested merge with GNU Make v3.81 (vendor/gnumake/2005-05-16 -> vendor/gnumake/current).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/read.c

    r466 r503  
    11/* Reading and parsing of makefiles for GNU Make.
    22Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 2002 Free Software Foundation, Inc.
     31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
     4Foundation, Inc.
    45This file is part of GNU Make.
    56
    6 GNU Make is free software; you can redistribute it and/or modify
    7 it under the terms of the GNU General Public License as published by
    8 the Free Software Foundation; either version 2, or (at your option)
    9 any later version.
    10 
    11 GNU Make is distributed in the hope that it will be useful,
    12 but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 GNU General Public License for more details.
    15 
    16 You should have received a copy of the GNU General Public License
    17 along with GNU Make; see the file COPYING.  If not, write to
    18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    19 Boston, MA 02111-1307, USA.  */
     7GNU Make is free software; you can redistribute it and/or modify it under the
     8terms of the GNU General Public License as published by the Free Software
     9Foundation; either version 2, or (at your option) any later version.
     10
     11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
     12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     13A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     14
     15You should have received a copy of the GNU General Public License along with
     16GNU Make; see the file COPYING.  If not, write to the Free Software
     17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
    2018
    2119#include "make.h"
     
    144142static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
    145143                        char **startp, unsigned int *length));
     144static void remove_comments PARAMS ((char *line));
     145static char *find_char_unquote PARAMS ((char *string, int stop1,
     146                                        int stop2, int blank, int ignorevars));
    146147
    147148
     
    187188        if (*p != '\0')
    188189          *p++ = '\0';
    189         name = xstrdup (name);
    190         if (eval_makefile (name,
    191                            RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE) < 2)
    192           free (name);
     190        eval_makefile (name, RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
    193191      }
    194192
     
    253251          for (p = default_makefiles; *p != 0; ++p)
    254252            {
    255               struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
    256               d->name = 0;
     253              struct dep *d = alloc_dep ();
    257254              d->file = enter_file (*p);
    258255              d->file->dontcare = 1;
    259               d->ignore_mtime = 0;
    260               d->need_2nd_expansion = 0;
    261256              /* Tell update_goal_chain to bail out as soon as this file is
    262257                 made, and main not to die if we can't make this file.  */
     
    312307  struct ebuffer ebuf;
    313308  const struct floc *curfile;
     309  char *expanded = 0;
     310  char *included = 0;
    314311  int makefile_errno;
    315312  int r;
    316313
    317   ebuf.floc.filenm = filename;
     314  ebuf.floc.filenm = strcache_add (filename);
    318315  ebuf.floc.lineno = 1;
    319316
     
    338335  if (!(flags & RM_NO_TILDE) && filename[0] == '~')
    339336    {
    340       char *expanded = tilde_expand (filename);
     337      expanded = tilde_expand (filename);
    341338      if (expanded != 0)
    342339        filename = expanded;
     
    355352      for (i = 0; include_directories[i] != 0; ++i)
    356353        {
    357           char *name = concat (include_directories[i], "/", filename);
    358           ebuf.fp = fopen (name, "r");
    359           if (ebuf.fp == 0)
    360             free (name);
    361           else
     354          included = concat (include_directories[i], "/", filename);
     355          ebuf.fp = fopen (included, "r");
     356          if (ebuf.fp)
    362357            {
    363               filename = name;
     358              filename = included;
    364359              break;
    365360            }
    366         }
     361          free (included);
     362        }
     363      /* If we're not using it, we already freed it above.  */
     364      if (filename != included)
     365        included = 0;
    367366    }
    368367
    369368  /* Add FILENAME to the chain of read makefiles.  */
    370   deps = (struct dep *) xmalloc (sizeof (struct dep));
     369  deps = alloc_dep ();
    371370  deps->next = read_makefiles;
    372371  read_makefiles = deps;
    373   deps->name = 0;
    374372  deps->file = lookup_file (filename);
    375373  if (deps->file == 0)
    376374    deps->file = enter_file (xstrdup (filename));
    377   if (filename != ebuf.floc.filenm)
    378     free (filename);
    379375  filename = deps->file->name;
    380376  deps->changed = flags;
    381   deps->ignore_mtime = 0;
    382   deps->need_2nd_expansion = 0;
    383377  if (flags & RM_DONTCARE)
    384378    deps->file->dontcare = 1;
     379
     380  if (expanded)
     381    free (expanded);
     382  if (included)
     383    free (included);
    385384
    386385  /* If the makefile can't be found at all, give up entirely.  */
     
    803802
    804803          p = allocated_variable_expand (p2);
     804
     805          /* If no filenames, it's a no-op.  */
    805806          if (*p == '\0')
    806             {
    807               error (fstart,
    808                      _("no file name for `%sinclude'"), noerror ? "-" : "");
    809               continue;
    810             }
     807            {
     808              free (p);
     809              continue;
     810            }
    811811
    812812          /* Parse the list of file names.  */
     
    838838              r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
    839839                                        | (noerror ? RM_DONTCARE : 0)));
    840               if (!r)
    841                 {
    842                   if (!noerror)
    843                     error (fstart, "%s: %s", name, strerror (errno));
    844                   free (name);
    845                 }
     840              if (!r && !noerror)
     841                error (fstart, "%s: %s", name, strerror (errno));
     842              free (name);
    846843            }
    847844
     
    888885        /* Search the line for an unquoted ; that is not after an
    889886           unquoted #.  */
    890         cmdleft = find_char_unquote (line, ';', '#', 0);
     887        cmdleft = find_char_unquote (line, ';', '#', 0, 1);
    891888        if (cmdleft != 0 && *cmdleft == '#')
    892889          {
     
    935932              {
    936933                /* Look for a semicolon in the expanded line.  */
    937                 cmdleft = find_char_unquote (p2, ';', 0, 0);
     934                cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
    938935
    939936                if (cmdleft != 0)
     
    962959              }
    963960
    964             colonp = find_char_unquote(p2, ':', 0, 0);
     961            colonp = find_char_unquote(p2, ':', 0, 0, 0);
    965962#ifdef HAVE_DOS_PATHS
    966963            /* The drive spec brain-damage strikes again...  */
     
    971968                   colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
    972969                   (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
    973               colonp = find_char_unquote(colonp + 1, ':', 0, 0);
     970              colonp = find_char_unquote(colonp + 1, ':', 0, 0, 0);
    974971#endif
    975972            if (colonp != 0)
     
    10851082        /* This is a normal target, _not_ a target-specific variable.
    10861083           Unquote any = in the dependency list.  */
    1087         find_char_unquote (lb_next, '=', 0, 0);
     1084        find_char_unquote (lb_next, '=', 0, 0, 0);
    10881085
    10891086        /* We have some targets, so don't ignore the following commands.  */
     
    11001097            if (cmdleft == 0)
    11011098              {
    1102                 cmdleft = find_char_unquote (p2, ';', 0, 0);
     1099                cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
    11031100                if (cmdleft != 0)
    11041101                  *(cmdleft++) = '\0';
     
    11651162            if (pattern_percent == 0)
    11661163              fatal (fstart, _("target pattern contains no `%%' (target `%s')"), target->name);
    1167             free((char *)target);
     1164            free ((char *)target);
    11681165          }
    11691166        else
     
    11771174        if (beg <= end && *beg != '\0')
    11781175          {
    1179             char *top;
    1180             const char *fromp = beg;
    1181 
    1182             /* Make a copy of the dependency string.  Note if we find '$'.  */
    1183             deps = (struct dep*) xmalloc (sizeof (struct dep));
    1184             deps->next = 0;
    1185             deps->name = top = (char *) xmalloc (end - beg + 2);
    1186             deps->need_2nd_expansion = 0;
    1187             while (fromp <= end)
    1188               {
    1189                 if (*fromp == '$')
    1190                   deps->need_2nd_expansion = 1;
    1191                 *(top++) = *(fromp++);
    1192               }
    1193             *top = '\0';
    1194             deps->file = 0;
     1176            /* Put all the prerequisites here; they'll be parsed later.  */
     1177            deps = alloc_dep ();
     1178            deps->name = savestring (beg, end - beg + 1);
    11951179          }
    11961180        else
     
    13191303
    13201304
     1305/* Remove comments from LINE.
     1306   This is done by copying the text at LINE onto itself.  */
     1307
     1308static void
     1309remove_comments (char *line)
     1310{
     1311  char *comment;
     1312
     1313  comment = find_char_unquote (line, '#', 0, 0, 0);
     1314
     1315  if (comment != 0)
     1316    /* Cut off the line at the #.  */
     1317    *comment = '\0';
     1318}
     1319
    13211320/* Execute a `define' directive.
    13221321   The first line has already been read, and NAME is the name of
     
    18391838      v->origin = origin;
    18401839      v->per_target = 1;
    1841       if (exported)
    1842         v->export = v_export;
     1840      v->export = exported ? v_export : v_default;
    18431841
    18441842      /* If it's not an override, check to see if there was a command-line
     
    19131911      char *name = filenames->name;
    19141912      struct file *f;
    1915       struct dep *d;
    1916       struct dep *this;
     1913      struct dep *this = 0;
    19171914      char *implicit_percent;
    19181915
     
    19201917      free (filenames);
    19211918
    1922       /* Check for .POSIX.  We used to do this in snap_deps() but that's not
    1923          good enough: it doesn't happen until after the makefile is read,
    1924          which means we cannot use its value during parsing.  */
     1919      /* Check for special targets.  Do it here instead of, say, snap_deps()
     1920         so that we can immediately use the value.  */
    19251921
    19261922      if (streq (name, ".POSIX"))
    19271923        posix_pedantic = 1;
     1924      else if (streq (name, ".SECONDEXPANSION"))
     1925        second_expansion = 1;
    19281926
    19291927      implicit_percent = find_percent (name);
     
    19601958        }
    19611959
    1962       /* If there are multiple filenames, copy the chain DEPS
    1963          for all but the last one.  It is not safe for the same deps
    1964          to go in more than one place in the database.  */
    1965       this = nextf != 0 ? copy_dep_chain (deps) : deps;
    1966 
    1967       if (pattern != 0)
    1968         {
    1969           /* If this is an extended static rule:
    1970              `targets: target%pattern: dep%pattern; cmds',
    1971              translate each dependency pattern into a plain filename
    1972              using the target pattern and this target's name.  */
    1973           if (!pattern_matches (pattern, pattern_percent, name))
    1974             {
    1975               /* Give a warning if the rule is meaningless.  */
    1976               error (flocp,
    1977                      _("target `%s' doesn't match the target pattern"), name);
    1978               this = 0;
    1979             }
    1980           else
    1981             /* We use subst_expand to do the work of translating % to $* in
    1982                the dependency line.  */
    1983 
    1984             if (this != 0 && find_percent (this->name) != 0)
    1985               {
    1986                 char *o;
    1987                 char *buffer = variable_expand ("");
    1988 
    1989                 o = subst_expand (buffer, this->name, "%", "$*", 1, 2, 0);
    1990 
    1991                 free (this->name);
    1992                 this->name = savestring (buffer, o - buffer);
    1993                 this->need_2nd_expansion = 1;
    1994               }
    1995         }
     1960      /* If this is a static pattern rule:
     1961         `targets: target%pattern: dep%pattern; cmds',
     1962         make sure the pattern matches this target name.  */
     1963      if (pattern && !pattern_matches (pattern, pattern_percent, name))
     1964        error (flocp, _("target `%s' doesn't match the target pattern"), name);
     1965      else if (deps)
     1966        {
     1967          /* If there are multiple filenames, copy the chain DEPS for all but
     1968             the last one.  It is not safe for the same deps to go in more
     1969             than one place in the database.  */
     1970          this = nextf != 0 ? copy_dep_chain (deps) : deps;
     1971          this->need_2nd_expansion = (second_expansion
     1972                                      && strchr (this->name, '$'));
     1973        }
    19961974
    19971975      if (!two_colon)
     
    20332011            f->cmds = cmds;
    20342012
    2035           /* Defining .SUFFIXES with no dependencies
    2036              clears out the list of suffixes.  */
     2013          /* Defining .SUFFIXES with no dependencies clears out the list of
     2014             suffixes.  */
    20372015          if (f == suffix_file && this == 0)
    20382016            {
    2039               d = f->deps;
    2040               while (d != 0)
    2041                 {
    2042                   struct dep *nextd = d->next;
    2043                   free (d->name);
    2044                   free ((char *)d);
    2045                   d = nextd;
    2046                 }
     2017              free_dep_chain (f->deps);
    20472018              f->deps = 0;
    20482019            }
     
    20592030
    20602031                  if (cmds != 0)
    2061                     {
    2062                       /* This is the rule with commands, so put its deps
    2063                          last. The rationale behind this is that $< expands
    2064                          to the first dep in the chain, and commands use $<
    2065                          expecting to get the dep that rule specifies.
    2066                          However the second expansion algorithm reverses
    2067                          the order thus we need to make it last here.  */
    2068 
    2069                       (*d_ptr)->next = this;
    2070                     }
     2032                    /* This is the rule with commands, so put its deps
     2033                       last. The rationale behind this is that $< expands to
     2034                       the first dep in the chain, and commands use $<
     2035                       expecting to get the dep that rule specifies.  However
     2036                       the second expansion algorithm reverses the order thus
     2037                       we need to make it last here.  */
     2038                    (*d_ptr)->next = this;
    20712039                  else
    20722040                    {
    20732041                      /* This is the rule without commands. Put its
    2074                          dependencies at the end but before dependencies
    2075                          from the rule with commands (if any). This way
    2076                          everything appears in makefile order.  */
     2042                         dependencies at the end but before dependencies from
     2043                         the rule with commands (if any). This way everything
     2044                         appears in makefile order.  */
    20772045
    20782046                      if (f->cmds != 0)
     
    21042072                f->updating = 1;
    21052073            }
    2106 
    2107           /* If this is a static pattern rule, set the file's stem to
    2108              the part of its name that matched the `%' in the pattern,
    2109              so you can use $* in the commands.  */
    2110           if (pattern != 0)
    2111             {
    2112               static char *percent = "%";
    2113               char *buffer = variable_expand ("");
    2114               char *o = patsubst_expand (buffer, name, pattern, percent,
    2115                                          pattern_percent+1, percent+1);
    2116               f->stem = savestring (buffer, o - buffer);
    2117             }
    21182074        }
    21192075      else
    21202076        {
    2121           /* Double-colon.  Make a new record
    2122              even if the file already has one.  */
     2077          /* Double-colon.  Make a new record even if there already is one.  */
    21232078          f = lookup_file (name);
     2079
    21242080          /* Check for both : and :: rules.  Check is_target so
    21252081             we don't lose on default suffix rules or makefiles.  */
     
    21282084                   _("target file `%s' has both : and :: entries"), f->name);
    21292085          f = enter_file (name);
    2130           /* If there was an existing entry and it was a double-colon
    2131              entry, enter_file will have returned a new one, making it the
    2132              prev pointer of the old one, and setting its double_colon
    2133              pointer to the first one.  */
     2086          /* If there was an existing entry and it was a double-colon entry,
     2087             enter_file will have returned a new one, making it the prev
     2088             pointer of the old one, and setting its double_colon pointer to
     2089             the first one.  */
    21342090          if (f->double_colon == 0)
    2135             /* This is the first entry for this name, so we must
    2136                set its double_colon pointer to itself.  */
     2091            /* This is the first entry for this name, so we must set its
     2092               double_colon pointer to itself.  */
    21372093            f->double_colon = f;
    21382094          f->is_target = 1;
     
    21412097        }
    21422098
     2099      /* If this is a static pattern rule, set the stem to the part of its
     2100         name that matched the `%' in the pattern, so you can use $* in the
     2101         commands.  */
     2102      if (pattern)
     2103        {
     2104          static char *percent = "%";
     2105          char *buffer = variable_expand ("");
     2106          char *o = patsubst_expand (buffer, name, pattern, percent,
     2107                                     pattern_percent+1, percent+1);
     2108          f->stem = savestring (buffer, o - buffer);
     2109          if (this)
     2110            {
     2111              this->staticpattern = 1;
     2112              this->stem = xstrdup (f->stem);
     2113            }
     2114        }
     2115
    21432116      /* Free name if not needed further.  */
    21442117      if (f != 0 && name != f->name
     
    21502123
    21512124      /* If this target is a default target, update DEFAULT_GOAL_FILE.  */
    2152       if (strcmp (*default_goal_name, name) == 0
     2125      if (streq (*default_goal_name, name)
    21532126          && (default_goal_file == 0
    2154               || strcmp (default_goal_file->name, name) != 0))
     2127              || ! streq (default_goal_file->name, name)))
    21552128        default_goal_file = f;
    21562129    }
     
    21602133      targets[target_idx] = 0;
    21612134      target_percents[target_idx] = 0;
     2135      if (deps)
     2136        deps->need_2nd_expansion = second_expansion;
    21622137      create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
    21632138      free ((char *) target_percents);
     
    21702145   Quoting backslashes are removed from STRING by compacting it into
    21712146   itself.  Returns a pointer to the first unquoted STOPCHAR if there is
    2172    one, or nil if there are none.  */
    2173 
    2174 char *
    2175 find_char_unquote (char *string, int stop1, int stop2, int blank)
     2147   one, or nil if there are none.  STOPCHARs inside variable references are
     2148   ignored if IGNOREVARS is true.
     2149
     2150   STOPCHAR _cannot_ be '$' if IGNOREVARS is true.  */
     2151
     2152static char *
     2153find_char_unquote (char *string, int stop1, int stop2, int blank,
     2154                   int ignorevars)
    21762155{
    21772156  unsigned int string_len = 0;
    21782157  register char *p = string;
    2179   register int ch;
     2158  register int ch; /* bird */
     2159
     2160  if (ignorevars)
     2161    ignorevars = '$';
    21802162
    21812163  while (1)
    21822164    {
    21832165      if (stop2 && blank)
    2184         while ((ch = *p) != '\0' && ch != stop1 && ch != stop2
     2166        while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
    21852167               && ! isblank ((unsigned char) ch))
    21862168          ++p;
    21872169      else if (stop2)
    2188         while ((ch = *p) != '\0' && ch != stop1 && ch != stop2)
     2170        while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
    21892171          ++p;
    21902172      else if (blank)
    2191         while ((ch = *p) != '\0' && ch != stop1
     2173        while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
    21922174               && ! isblank ((unsigned char) ch))
    21932175          ++p;
    21942176      else
    2195         while ((ch = *p) != '\0' && ch != stop1)
     2177        while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
    21962178          ++p;
    21972179
    21982180      if (ch == '\0')
    21992181        break;
     2182
     2183      /* If we stopped due to a variable reference, skip over its contents.  */
     2184      if (ch == ignorevars)
     2185        {
     2186          char openparen = p[1];
     2187
     2188          p += 2;
     2189
     2190          /* Skip the contents of a non-quoted, multi-char variable ref.  */
     2191          if (openparen == '(' || openparen == '{')
     2192            {
     2193              unsigned int pcount = 1;
     2194              char closeparen = (openparen == '(' ? ')' : '}');
     2195
     2196              while ((ch = *p))
     2197                {
     2198                  if (ch == openparen)
     2199                    ++pcount;
     2200                  else if (ch == closeparen)
     2201                    if (--pcount == 0)
     2202                      {
     2203                        ++p;
     2204                        break;
     2205                      }
     2206                  ++p;
     2207                }
     2208            }
     2209
     2210          /* Skipped the variable reference: look for STOPCHARS again.  */
     2211          continue;
     2212        }
    22002213
    22012214      if (p > string && p[-1] == '\\')
     
    22342247find_percent (char *pattern)
    22352248{
    2236   return find_char_unquote (pattern, '%', 0, 0);
     2249  return find_char_unquote (pattern, '%', 0, 0, 0);
    22372250}
    22382251
     
    22542267parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
    22552268{
    2256   register struct nameseq *new = 0;
    2257   register struct nameseq *new1, *lastnew1;
    2258   register char *p = *stringp;
     2269  struct nameseq *new = 0;
     2270  struct nameseq *new1, *lastnew1;
     2271  char *p = *stringp;
    22592272  char *q;
    22602273  char *name;
     
    22772290      /* Yes, find end of next name.  */
    22782291      q = p;
    2279       p = find_char_unquote (q, stopchar, VMS_COMMA, 1);
     2292      p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
    22802293#ifdef VMS
    22812294        /* convert comma separated list to space separated */
     
    22882301               || isspace ((unsigned char)p[-1])))
    22892302      {
    2290         p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1);
     2303        p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
    22912304      }
    22922305#endif
     
    22992312      while (p != 0 && !isspace ((unsigned char)*p) &&
    23002313             (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
    2301         p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1);
     2314        p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
    23022315#endif
    23032316      if (p == 0)
     
    29092922  dirs[idx] = 0;
    29102923
    2911   /* Now compute the maximum length of any name in it.  */
     2924  /* Now compute the maximum length of any name in it. Also add each
     2925     dir to the .INCLUDE_DIRS variable.  */
    29122926
    29132927  max_incl_len = 0;
     
    29222936      if (len > max_incl_len)
    29232937        max_incl_len = len;
     2938
     2939      /* Append to .INCLUDE_DIRS.   */
     2940      do_variable_definition (NILF, ".INCLUDE_DIRS", dirs[i],
     2941                              o_default, f_append, 0);
    29242942    }
    29252943
Note: See TracChangeset for help on using the changeset viewer.