Ignore:
Timestamp:
May 16, 2005, 6:54:02 PM (20 years ago)
Author:
bird
Message:

Current make snaphot, 2005-05-16.

Location:
branches/GNU/src/gmake
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/gmake

    • Property svn:ignore
      •  

        old new  
        3434README.DOS
        3535README.W32
         36README.OS2
        3637aclocal.m4
        3738autom4te.cache
  • branches/GNU/src/gmake/implicit.c

    r153 r280  
    11/* Implicit rule searching for GNU Make.
    2 Copyright (C) 1988,89,90,91,92,93,94,97,2000 Free Software Foundation, Inc.
     2Copyright (C) 1988,1989,1990,1991,1992,1993,1994,1997,2000,2004,2005 Free Software Foundation, Inc.
    33This file is part of GNU Make.
    44
     
    2323#include "dep.h"
    2424#include "debug.h"
    25 
    26 static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth,
    27                 unsigned int recursions));
     25#include "variable.h"
     26#include "job.h"      /* struct child, used inside commands.h */
     27#include "commands.h" /* set_file_variables */
     28
     29static int
     30pattern_search PARAMS ((struct file *file, int archive,
     31                        unsigned int depth, unsigned int recursions));
    2832
    2933
     
    6468
    6569
     70/* Struct idep captures information about implicit prerequisites
     71   that come from implicit rules. */
     72struct idep
     73{
     74  struct idep *next;              /* struct dep -compatible interface */
     75  char *name;                     /* name of the prerequisite */
     76  struct file *intermediate_file; /* intermediate file, 0 otherwise */
     77  char *intermediate_pattern;     /* pattern for intermediate file */
     78  unsigned char had_stem;         /* had % substituted with stem */
     79  unsigned char ignore_mtime;     /* ignore_mtime flag */
     80};
     81
     82static void
     83free_idep_chain (struct idep* p)
     84{
     85  register struct idep* n;
     86  register struct file *f;
     87
     88  for (; p != 0; p = n)
     89    {
     90      n = p->next;
     91
     92      if (p->name)
     93        {
     94          free (p->name);
     95
     96          f = p->intermediate_file;
     97
     98          if (f != 0
     99              && (f->stem < f->name
     100                  || f->stem > f->name + strlen (f->name)))
     101            free (f->stem);
     102        }
     103
     104      free (p);
     105    }
     106}
     107
     108
     109/* Scans the BUFFER for the next word with whitespace as a separator.
     110   Returns the pointer to the beginning of the word. LENGTH hold the
     111   length of the word.  */
     112
     113static char *
     114get_next_word (char *buffer, unsigned int *length)
     115{
     116  char *p = buffer, *beg;
     117  char c;
     118
     119  /* Skip any leading whitespace.  */
     120  while (isblank ((unsigned char)*p))
     121    ++p;
     122
     123  beg = p;
     124  c = *(p++);
     125
     126  if (c == '\0')
     127    return 0;
     128
     129
     130  /* We already found the first value of "c", above.  */
     131  while (1)
     132    {
     133      char closeparen;
     134      int count;
     135
     136      switch (c)
     137        {
     138        case '\0':
     139        case ' ':
     140        case '\t':
     141          goto done_word;
     142
     143        case '$':
     144          c = *(p++);
     145          if (c == '$')
     146            break;
     147
     148          /* This is a variable reference, so read it to the matching
     149             close paren.  */
     150
     151          if (c == '(')
     152            closeparen = ')';
     153          else if (c == '{')
     154            closeparen = '}';
     155          else
     156            /* This is a single-letter variable reference.  */
     157            break;
     158
     159          for (count = 0; *p != '\0'; ++p)
     160            {
     161              if (*p == c)
     162                ++count;
     163              else if (*p == closeparen && --count < 0)
     164                {
     165                  ++p;
     166                  break;
     167                }
     168            }
     169          break;
     170
     171        case '|':
     172          goto done;
     173
     174        default:
     175          break;
     176        }
     177
     178      c = *(p++);
     179    }
     180 done_word:
     181  --p;
     182
     183 done:
     184  if (length)
     185    *length = p - beg;
     186
     187  return beg;
     188}
     189
    66190/* Search the pattern rules for a rule with an existing dependency to make
    67191   FILE.  If a rule is found, the appropriate commands and deps are put in FILE
     
    96220  struct file *intermediate_file = 0;
    97221
    98   /* List of dependencies found recursively.  */
    99   struct file **intermediate_files
    100     = (struct file **) xmalloc (max_pattern_deps * sizeof (struct file *));
    101 
    102   /* List of the patterns used to find intermediate files.  */
    103   char **intermediate_patterns
    104     = (char **) alloca (max_pattern_deps * sizeof (char *));
    105 
    106   /* This buffer records all the dependencies actually found for a rule.  */
    107   char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
    108   /* Remember whether the associated dep has an "ignore_mtime" flag set.  */
    109   unsigned char *found_files_im = (unsigned char *) alloca (max_pattern_deps * sizeof (unsigned char));
    110   /* Number of dep names now in FOUND_FILES.  */
    111   unsigned int deps_found = 0;
     222  /* This linked list records all the prerequisites actually
     223     found for a rule along with some other useful information
     224     (see struct idep for details). */
     225  struct idep* deps = 0;
     226
     227  /* 1 if we need to remove explicit prerequisites, 0 otherwise. */
     228  unsigned int remove_explicit_deps = 0;
    112229
    113230  /* Names of possible dependencies are constructed in this buffer.  */
     
    149266  register unsigned int i = 0;  /* uninit checks OK */
    150267  register struct rule *rule;
    151   register struct dep *dep;
    152 
    153   char *p, *vp;
     268  register struct dep *dep, *expl_d;
     269
     270  char *p, *vname;
     271
     272  struct idep *d;
     273  struct idep **id_ptr;
     274  struct dep **d_ptr;
     275
     276  PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */
    154277
    155278#ifndef NO_ARCHIVES
     
    298421        }
    299422
     423  /* We are going to do second expansion so initialize file variables
     424     for the rule. */
     425  initialize_file_variables (file, 0);
     426
    300427  /* Try each rule once without intermediate files, then once with them.  */
    301428  for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
    302429    {
    303430      /* Try each pattern rule till we find one that applies.
    304          If it does, copy the names of its dependencies (as substituted)
    305          and store them in FOUND_FILES.  DEPS_FOUND is the number of them.  */
     431         If it does, expand its dependencies (as substituted)
     432         and chain them in DEPS.  */
    306433
    307434      for (i = 0; i < nrules; i++)
    308435        {
     436          struct file *f;
     437          unsigned int failed = 0;
    309438          int check_lastslash;
    310439
    311440          rule = tryrules[i];
     441
     442          remove_explicit_deps = 0;
    312443
    313444          /* RULE is nil when we discover that a rule,
     
    340471                             (int) stemlen, stem));
    341472
     473          strncpy (stem_str, stem, stemlen);
     474          stem_str[stemlen] = '\0';
     475
     476          /* Temporary assign STEM to file->stem and set file variables. */
     477          file->stem = stem_str;
     478          set_file_variables (file);
     479
    342480          /* Try each dependency; see if it "exists".  */
    343481
    344           deps_found = 0;
     482          /* @@ There is always only one dep line for any given implicit
     483                rule. So the loop is not necessary. Can rule->deps be 0?
     484
     485                Watch out for conversion of suffix rules to implicit rules.
     486          */
     487
    345488          for (dep = rule->deps; dep != 0; dep = dep->next)
    346489            {
    347               /* If the dependency name has a %, substitute the stem.  */
    348               p = strchr (dep_name (dep), '%');
    349               if (p != 0)
    350                 {
    351                   register unsigned int i;
    352                   if (check_lastslash)
    353                     {
    354                       /* Copy directory name from the original FILENAME.  */
    355                       i = lastslash - filename + 1;
    356                       bcopy (filename, depname, i);
    357                     }
    358                   else
    359                     i = 0;
    360                   bcopy (dep_name (dep), depname + i, p - dep_name (dep));
    361                   i += p - dep_name (dep);
    362                   bcopy (stem, depname + i, stemlen);
    363                   i += stemlen;
    364                   strcpy (depname + i, p + 1);
    365                   p = depname;
    366                 }
    367               else
    368                 p = dep_name (dep);
    369 
    370               /* P is now the actual dependency name as substituted.  */
    371 
    372               if (file_impossible_p (p))
    373                 {
    374                   /* If this dependency has already been ruled
    375                      "impossible", then the rule fails and don't
    376                      bother trying it on the second pass either
    377                      since we know that will fail too.  */
    378                   DBS (DB_IMPLICIT,
    379                        (p == depname
     490              unsigned int len;
     491              char *p2;
     492              unsigned int order_only = 0; /* Set if '|' was seen. */
     493
     494              /* In an ideal world we would take the dependency line,
     495                 substitute the stem, re-expand the whole line and
     496                 chop it into individual prerequisites. Unfortunately
     497                 this won't work because of the "check_lastslash" twist.
     498                 Instead, we will have to go word by word, taking $()'s
     499                 into account, for each word we will substitute the stem,
     500                 re-expand, chop it up, and, if check_lastslash != 0,
     501                 add the directory part to each resulting prerequisite.  */
     502
     503              p = get_next_word (dep->name, &len);
     504
     505              while (1)
     506                {
     507                  int add_dir = 0;
     508                  int had_stem = 0;
     509
     510                  if (p == 0)
     511                    break; /* No more words */
     512
     513                  /* If the dependency name has %, substitute the stem.
     514                     Watch out, we are going to do something tricky here. If
     515                     we just replace % with the stem value, later, when we do
     516                     the second expansion, we will re-expand this stem value
     517                     once again. This is not good especially if you have
     518                     certain characters in your setm (like $).
     519
     520                     Instead, we will replace % with $* and allow the second
     521                     expansion to take care of it for us. This way (since $*
     522                     is a simple variable) there won't be additional
     523                     re-expansion of the stem.  */
     524
     525                  for (p2 = p; p2 < p + len && *p2 != '%'; ++p2)
     526                    ;
     527
     528                  if (p2 < p + len)
     529                    {
     530                      register unsigned int i = p2 - p;
     531                      bcopy (p, depname, i);
     532                      bcopy ("$*", depname + i, 2);
     533                      bcopy (p2 + 1, depname + i + 2, len - i - 1);
     534                      depname[len + 2 - 1] = '\0';
     535
     536                      if (check_lastslash)
     537                        add_dir = 1;
     538
     539                      had_stem = 1;
     540                    }
     541                  else
     542                    {
     543                      bcopy (p, depname, len);
     544                      depname[len] = '\0';
     545                    }
     546
     547                  p2 = variable_expand_for_file (depname, file);
     548
     549                  /* Parse the dependencies. */
     550
     551                  while (1)
     552                    {
     553                      id_ptr = &deps;
     554
     555                      for (; *id_ptr; id_ptr = &(*id_ptr)->next)
     556                        ;
     557
     558                      *id_ptr = (struct idep *)
     559                        multi_glob (
     560                          parse_file_seq (&p2,
     561                                          order_only ? '\0' : '|',
     562                                          sizeof (struct idep),
     563                                          1), sizeof (struct idep));
     564
     565                      /* @@ It would be nice to teach parse_file_seq or
     566                         multi_glob to add prefix. This would save us
     567                         some reallocations. */
     568
     569                      if (order_only || add_dir || had_stem)
     570                        {
     571                          unsigned long l = lastslash - filename + 1;
     572
     573                          for (d = *id_ptr; d != 0; d = d->next)
     574                            {
     575                              if (order_only)
     576                                d->ignore_mtime = 1;
     577
     578                              if (add_dir)
     579                                {
     580                                  char *p = d->name;
     581
     582                                  d->name = xmalloc (strlen (p) + l + 1);
     583
     584                                  bcopy (filename, d->name, l);
     585                                  bcopy (p, d->name + l, strlen (p) + 1);
     586
     587                                  free (p);
     588                                }
     589
     590                              if (had_stem)
     591                                d->had_stem = 1;
     592                            }
     593                        }
     594
     595                      if (!order_only && *p2)
     596                      {
     597                        ++p2;
     598                        order_only = 1;
     599                        continue;
     600                      }
     601
     602                      break;
     603                    }
     604
     605                  p += len;
     606                  p = get_next_word (p, &len);
     607                }
     608            }
     609
     610          /* Reset the stem in FILE. */
     611
     612          file->stem = 0;
     613
     614          /* @@ This loop can be combined with the previous one. I do
     615             it separately for now for transparency.*/
     616
     617          for (d = deps; d != 0; d = d->next)
     618            {
     619              char *name = d->name;
     620
     621              if (file_impossible_p (name))
     622                {
     623                  /* If this dependency has already been ruled
     624                     "impossible", then the rule fails and don't
     625                     bother trying it on the second pass either
     626                     since we know that will fail too.  */
     627                  DBS (DB_IMPLICIT,
     628                       (d->had_stem
    380629                        ? _("Rejecting impossible implicit prerequisite `%s'.\n")
    381630                        : _("Rejecting impossible rule prerequisite `%s'.\n"),
    382                         p));
    383                   tryrules[i] = 0;
    384                   break;
    385                 }
    386 
    387               intermediate_files[deps_found] = 0;
    388 
    389               DBS (DB_IMPLICIT,
    390                    (p == depname
     631                        name));
     632                  tryrules[i] = 0;
     633
     634                  failed = 1;
     635                  break;
     636                }
     637
     638              DBS (DB_IMPLICIT,
     639                   (d->had_stem
    391640                    ? _("Trying implicit prerequisite `%s'.\n")
    392                     : _("Trying rule prerequisite `%s'.\n"), p));
    393 
    394               /* The DEP->changed flag says that this dependency resides in a
    395                  nonexistent directory.  So we normally can skip looking for
    396                  the file.  However, if CHECK_LASTSLASH is set, then the
    397                  dependency file we are actually looking for is in a different
    398                  directory (the one gotten by prepending FILENAME's directory),
    399                  so it might actually exist.  */
    400 
    401               if (lookup_file (p) != 0
    402                   || ((!dep->changed || check_lastslash) && file_exists_p (p)))
    403                 {
    404                   found_files_im[deps_found] = dep->ignore_mtime;
    405                   found_files[deps_found++] = xstrdup (p);
    406                   continue;
    407                 }
    408               /* This code, given FILENAME = "lib/foo.o", dependency name
    409                  "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
    410               vp = p;
    411               if (vpath_search (&vp, (FILE_TIMESTAMP *) 0))
    412                 {
    413                   DBS (DB_IMPLICIT,
    414                        (_("Found prerequisite `%s' as VPATH `%s'\n"), p, vp));
    415                   strcpy (vp, p);
    416                   found_files_im[deps_found] = dep->ignore_mtime;
    417                   found_files[deps_found++] = vp;
    418                   continue;
    419                 }
    420 
    421               /* We could not find the file in any place we should look.
    422                  Try to make this dependency as an intermediate file,
    423                  but only on the second pass.  */
    424 
    425               if (intermed_ok)
    426                 {
    427                   if (intermediate_file == 0)
    428                     intermediate_file
    429                       = (struct file *) alloca (sizeof (struct file));
    430 
    431                   DBS (DB_IMPLICIT,
     641                    : _("Trying rule prerequisite `%s'.\n"), name));
     642
     643              /* If this prerequisite also happened to be explicitly
     644                 mentioned for FILE skip all the test below since it
     645                 it has to be built anyway, no matter which implicit
     646                 rule we choose. */
     647
     648              for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next)
     649                if (strcmp (dep_name (expl_d), name) == 0) break;
     650
     651              if (expl_d != 0)
     652                continue;
     653
     654
     655
     656              /* The DEP->changed flag says that this dependency resides in a
     657                 nonexistent directory.  So we normally can skip looking for
     658                 the file.  However, if CHECK_LASTSLASH is set, then the
     659                 dependency file we are actually looking for is in a different
     660                 directory (the one gotten by prepending FILENAME's directory),
     661                 so it might actually exist.  */
     662
     663              /* @@ dep->changed check is disabled. */
     664              if (((f = lookup_file (name)) != 0 && f->is_target)
     665                  /*|| ((!dep->changed || check_lastslash) && */
     666                  || file_exists_p (name))
     667                {
     668                  continue;
     669                }
     670
     671              /* This code, given FILENAME = "lib/foo.o", dependency name
     672                 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
     673              vname = name;
     674              if (vpath_search (&vname, (FILE_TIMESTAMP *) 0))
     675                {
     676                  DBS (DB_IMPLICIT,
     677                       (_("Found prerequisite `%s' as VPATH `%s'\n"),
     678                        name,
     679                        vname));
     680
     681                  free (vname);
     682                  continue;
     683                }
     684
     685
     686              /* We could not find the file in any place we should look.
     687                 Try to make this dependency as an intermediate file,
     688                 but only on the second pass.  */
     689
     690              if (intermed_ok)
     691                {
     692                  if (intermediate_file == 0)
     693                    intermediate_file
     694                      = (struct file *) alloca (sizeof (struct file));
     695
     696                  DBS (DB_IMPLICIT,
    432697                       (_("Looking for a rule with intermediate file `%s'.\n"),
    433                         p));
    434 
    435                   bzero ((char *) intermediate_file, sizeof (struct file));
    436                   intermediate_file->name = p;
    437                   if (pattern_search (intermediate_file, 0, depth + 1,
    438                                       recursions + 1))
    439                     {
    440                       p = xstrdup (p);
    441                       intermediate_patterns[deps_found]
    442                         = intermediate_file->name;
    443                       intermediate_file->name = p;
    444                       intermediate_files[deps_found] = intermediate_file;
    445                       intermediate_file = 0;
    446                       found_files_im[deps_found] = dep->ignore_mtime;
    447                       /* Allocate an extra copy to go in FOUND_FILES,
    448                          because every elt of FOUND_FILES is consumed
    449                          or freed later.  */
    450                       found_files[deps_found++] = xstrdup (p);
    451                       continue;
    452                     }
    453 
    454                   /* If we have tried to find P as an intermediate
    455                      file and failed, mark that name as impossible
    456                      so we won't go through the search again later.  */
    457                   file_impossible (p);
    458                 }
    459 
    460               /* A dependency of this rule does not exist.
    461                  Therefore, this rule fails.  */
    462               break;
    463             }
    464 
    465           /* This rule is no longer `in use' for recursive searches.  */
     698                        name));
     699
     700                  bzero ((char *) intermediate_file, sizeof (struct file));
     701                  intermediate_file->name = name;
     702                  if (pattern_search (intermediate_file,
     703                                      0,
     704                                      depth + 1,
     705                                      recursions + 1))
     706                    {
     707                      d->intermediate_file = intermediate_file;
     708                      d->intermediate_pattern = intermediate_file->name;
     709
     710                      intermediate_file->name = xstrdup (name);
     711                      intermediate_file = 0;
     712
     713                      continue;
     714                    }
     715
     716                  /* If we have tried to find P as an intermediate
     717                     file and failed, mark that name as impossible
     718                     so we won't go through the search again later.  */
     719                  file_impossible (name);
     720                }
     721
     722              /* A dependency of this rule does not exist. Therefore,
     723                 this rule fails.  */
     724              failed = 1;
     725              break;
     726            }
     727
     728          /* This rule is no longer `in use' for recursive searches.  */
    466729          rule->in_use = 0;
    467730
    468           if (dep != 0)
    469             {
    470               /* This pattern rule does not apply.
    471                  If some of its dependencies succeeded,
    472                  free the data structure describing them.  */
    473               while (deps_found-- > 0)
    474                 {
    475                   register struct file *f = intermediate_files[deps_found];
    476                   free (found_files[deps_found]);
    477                   if (f != 0
    478                       && (f->stem < f->name
    479                           || f->stem > f->name + strlen (f->name)))
    480                     free (f->stem);
    481                 }
    482             }
     731          if (failed)
     732            {
     733              /* This pattern rule does not apply. If some of its
     734                 dependencies succeeded, free the data structure
     735                 describing them.  */
     736              free_idep_chain (deps);
     737              deps = 0;
     738            }
    483739          else
    484740            /* This pattern rule does apply.  Stop looking for one.  */
     
    512768     Convert them into entries on the deps-chain of FILE.  */
    513769
    514   while (deps_found-- > 0)
     770  if (remove_explicit_deps)
     771    {
     772      /* Remove all the dependencies that didn't come from
     773         this implicit rule. */
     774
     775      dep = file->deps;
     776      while (dep != 0)
     777        {
     778          struct dep *next = dep->next;
     779          free (dep->name);
     780          free ((char *)dep);
     781          dep = next;
     782        }
     783      file->deps = 0;
     784  }
     785
     786  expl_d = file->deps; /* We will add them at the end. */
     787  d_ptr = &file->deps;
     788
     789  for (d = deps; d != 0; d = d->next)
    515790    {
    516791      register char *s;
    517792
    518       if (intermediate_files[deps_found] != 0)
     793      if (d->intermediate_file != 0)
    519794        {
    520795          /* If we need to use an intermediate file,
     
    525800             of F below are null before we change them.  */
    526801
    527           struct file *imf = intermediate_files[deps_found];
    528           register struct file *f = enter_file (imf->name);
     802          struct file *imf = d->intermediate_file;
     803          register struct file *f = lookup_file (imf->name);
     804
     805          /* We don't want to delete an intermediate file that happened
     806             to be a prerequisite of some (other) target. Mark it as
     807             precious.  */
     808          if (f != 0)
     809            f->precious = 1;
     810          else
     811            f = enter_file (imf->name);
     812
    529813          f->deps = imf->deps;
    530814          f->cmds = imf->cmds;
    531815          f->stem = imf->stem;
    532816          f->also_make = imf->also_make;
    533           imf = lookup_file (intermediate_patterns[deps_found]);
    534           if (imf != 0 && imf->precious)
    535             f->precious = 1;
     817          f->is_target = 1;
     818
     819          if (!f->precious)
     820            {
     821              imf = lookup_file (d->intermediate_pattern);
     822              if (imf != 0 && imf->precious)
     823                f->precious = 1;
     824            }
     825
    536826          f->intermediate = 1;
    537827          f->tried_implicit = 1;
     
    548838
    549839      dep = (struct dep *) xmalloc (sizeof (struct dep));
    550       dep->ignore_mtime = found_files_im[deps_found];
    551       s = found_files[deps_found];
     840      dep->ignore_mtime = d->ignore_mtime;
     841      dep->need_2nd_expansion = 0;
     842      s = d->name; /* Hijacking the name. */
     843      d->name = 0;
    552844      if (recursions == 0)
    553845        {
     
    568860          dep->changed = 0;
    569861        }
    570       if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
     862      if (d->intermediate_file == 0 && tryrules[foundrule]->terminal)
    571863        {
    572864          /* If the file actually existed (was not an intermediate file),
     
    580872            dep->file->tried_implicit = 1;
    581873        }
    582       dep->next = file->deps;
    583       file->deps = dep;
     874
     875      *d_ptr = dep;
     876      d_ptr = &dep->next;
    584877    }
     878
     879  *d_ptr = expl_d;
    585880
    586881  if (!checked_lastslash[foundrule])
     
    605900
    606901  file->cmds = rule->cmds;
     902  file->is_target = 1;
    607903
    608904  /* If this rule builds other targets, too, put the others into FILE's
     
    616912          /* GKM FIMXE: handle '|' here too */
    617913          new->ignore_mtime = 0;
     914          new->need_2nd_expansion = 0;
    618915          new->name = p = (char *) xmalloc (rule->lens[i] + fullstemlen + 1);
    619916          bcopy (rule->targets[i], p,
     
    630927
    631928 done:
    632   free (intermediate_files);
     929  free_idep_chain (deps);
    633930  free (tryrules);
    634931
Note: See TracChangeset for help on using the changeset viewer.