home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
make-3.71-src.lha
/
src
/
amiga
/
make-3.71
/
implicit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-21
|
19KB
|
591 lines
/* Implicit rule searching for GNU Make.
Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
This file is part of GNU Make.
GNU Make is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Make is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Make; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "make.h"
#include "rule.h"
#include "dep.h"
#include "file.h"
static int pattern_search ();
/* For a FILE which has no commands specified, try to figure out some
from the implicit pattern rules.
Returns 1 if a suitable implicit rule was found,
after modifying FILE to contain the appropriate commands and deps,
or returns 0 if no implicit rule was found. */
int
try_implicit_rule (file, depth)
struct file *file;
unsigned int depth;
{
DEBUGPR ("Looking for an implicit rule for `%s'.\n");
/* The order of these searches was previously reversed. My logic now is
that since the non-archive search uses more information in the target
(the archive search omits the archive name), it is more specific and
should come first. */
if (pattern_search (file, 0, depth, 0))
return 1;
#ifndef NO_ARCHIVES
/* If this is an archive member reference, use just the
archive member name to search for implicit rules. */
if (ar_name (file->name))
{
DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
if (pattern_search (file, 1, depth, 0))
return 1;
}
#endif
return 0;
}
#define DEBUGP2(msg, a1, a2) \
do { \
if (debug_flag) \
{ print_spaces (depth); printf (msg, a1, a2); fflush (stdout); } \
} while (0)
/* Search the pattern rules for a rule with an existing dependency to make
FILE. If a rule is found, the appropriate commands and deps are put in FILE
and 1 is returned. If not, 0 is returned.
If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for
"(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
directory and filename parts.
If an intermediate file is found by pattern search, the intermediate file
is set up as a target by the recursive call and is also made a dependency
of FILE.
DEPTH is used for debugging messages. */
static int
pattern_search (file, archive, depth, recursions)
struct file *file;
int archive;
unsigned int depth;
unsigned int recursions;
{
/* Filename we are searching for a rule for. */
char *filename = archive ? index (file->name, '(') : file->name;
/* Length of FILENAME. */
unsigned int namelen = strlen (filename);
/* The last slash in FILENAME (or nil if there is none). */
char *lastslash;
/* This is a file-object used as an argument in
recursive calls. It never contains any data
except during a recursive call. */
struct file *intermediate_file = 0;
/* List of dependencies found recursively. */
struct file **intermediate_files
= (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
/* List of the patterns used to find intermediate files. */
char **intermediate_patterns
= (char **) alloca (max_pattern_deps * sizeof (char *));
/* This buffer records all the dependencies actually found for a rule. */
char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
/* Number of dep names now in FOUND_FILES. */
unsigned int deps_found;
/* Names of possible dependencies are constructed in this buffer. */
register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
/* The start and length of the stem of FILENAME for the current rule. */
register char *stem;
register unsigned int stemlen;
/* Buffer in which we store all the rules that are possibly applicable. */
struct rule **tryrules
= (struct rule **) alloca (num_pattern_rules * max_pattern_targets
* sizeof (struct rule *));
/* Number of valid elements in TRYRULES. */
unsigned int nrules;
/* The numbers of the rule targets of each rule
in TRYRULES that matched the target file. */
unsigned int *matches
= (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
/* Each element is nonzero if LASTSLASH was used in
matching the corresponding element of TRYRULES. */
char *checked_lastslash
= (char *) alloca (num_pattern_rules * sizeof (char));
/* The index in TRYRULES of the rule we found. */
unsigned int foundrule;
/* Nonzero if should consider intermediate files as dependencies. */
int intermed_ok;
/* Nonzero if we have matched a pattern-rule target
that is not just `%'. */
int specific_rule_matched = 0;
register unsigned int i;
register struct rule *rule;
register struct dep *dep;
char *p;
#ifndef NO_ARCHIVES
if (archive || ar_name (filename))
lastslash = 0;
else
#endif
{
/* Set LASTSLASH to point at the last slash in FILENAME
but not counting any slash at the end. (foo/bar/ counts as
bar/ in directory foo/, not empty in directory foo/bar/.) */
lastslash = rindex (filename, '/');
if (lastslash != 0 && lastslash[1] == '\0')
lastslash = 0;
}
/* First see which pattern rules match this target
and may be considered. Put them in TRYRULES. */
nrules = 0;
for (rule = pattern_rules; rule != 0; rule = rule->next)
{
/* If the pattern rule has deps but no commands, ignore it.
Users cancel built-in rules by redefining them without commands. */
if (rule->deps != 0 && rule->cmds == 0)
continue;
/* If this rule is in use by a parent pattern_search,
don't use it here. */
if (rule->in_use)
{
DEBUGP2 ("Avoiding implicit rule recursion.%s%s\n", "", "");
continue;
}
for (i = 0; rule->targets[i] != 0; ++i)
{
char *target = rule->targets[i];
char *suffix = rule->suffixes[i];
int check_lastslash;
/* Rules that can match any filename and are not terminal
are ignored if we're recursing, so that they cannot be
intermediate files. */
if (recursions > 0 && target[1] == '\0' && !rule->terminal)
continue;
if (rule->lens[i] > namelen)
/* It can't possibly match. */
continue;
/* From the lengths of the filename and the pattern parts,
find the stem: the part of the filename that matches the %. */
stem = filename + (suffix - target - 1);
stemlen = namelen - rule->lens[i] + 1;
/* Set CHECK_LASTSLASH if FILENAME contains a directory
prefix and the target pattern does not contain a slash. */
check_lastslash = lastslash != 0 && index (target, '/') == 0;
if (check_lastslash)
{
/* In that case, don't include the
directory prefix in STEM here. */
unsigned int difference = lastslash - filename + 1;
if (difference > stemlen)
continue;
stemlen -= difference;
stem += difference;
}
/* Check that the rule pattern matches the text before the stem. */
if (check_lastslash)
{
if (stem > (lastslash + 1)
&& strncmp (target, lastslash + 1, stem - lastslash - 1))
continue;
}
else if (stem > filename
&& strncmp (target, filename, stem - filename))
continue;
/* Check that the rule pattern matches the text after the stem.
We could test simply use streq, but this way we compare the
first two characters immediately. This saves time in the very
common case where the first character matches because it is a
period. */
if (*suffix != stem[stemlen]
|| (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
continue;
/* Record if we match a rule that not all filenam