1994-03-23 22:12:55 +08:00
|
|
|
|
/* Implementation of pattern-matching file search paths for GNU Make.
|
2013-05-17 13:46:11 +08:00
|
|
|
|
Copyright (C) 1988-2013 Free Software Foundation, Inc.
|
1991-06-04 05:37:27 +09:00
|
|
|
|
This file is part of GNU Make.
|
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
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
|
2007-07-05 03:35:15 +08:00
|
|
|
|
Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
version.
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
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.
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2007-07-05 03:35:15 +08:00
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2013-01-21 00:01:01 +08:00
|
|
|
|
#include "makeint.h"
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#include "filedef.h"
|
1991-06-04 05:37:27 +09:00
|
|
|
|
#include "variable.h"
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#include "pathstuff.h"
|
|
|
|
|
#endif
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Structure used to represent a selective VPATH searchpath. */
|
|
|
|
|
|
|
|
|
|
struct vpath
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
struct vpath *next; /* Pointer to next struct in the linked list. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *pattern;/* The pattern to match. */
|
2012-03-04 08:24:20 +08:00
|
|
|
|
const char *percent;/* Pointer into 'pattern' where the '%' is. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
unsigned int patlen;/* Length of the pattern. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char **searchpath; /* Null-terminated list of directories. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
unsigned int maxlen;/* Maximum length of any entry in the list. */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Linked-list of all selective VPATHs. */
|
|
|
|
|
|
|
|
|
|
static struct vpath *vpaths;
|
|
|
|
|
|
|
|
|
|
/* Structure for the general VPATH given in the variable. */
|
|
|
|
|
|
|
|
|
|
static struct vpath *general_vpath;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
|
|
|
|
/* Structure for GPATH given in the variable. */
|
|
|
|
|
|
|
|
|
|
static struct vpath *gpaths;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Reverse the chain of selective VPATH lists so they will be searched in the
|
|
|
|
|
order given in the makefiles and construct the list from the VPATH
|
|
|
|
|
variable. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
build_vpath_lists ()
|
|
|
|
|
{
|
|
|
|
|
register struct vpath *new = 0;
|
|
|
|
|
register struct vpath *old, *nexto;
|
|
|
|
|
register char *p;
|
|
|
|
|
|
|
|
|
|
/* Reverse the chain. */
|
|
|
|
|
for (old = vpaths; old != 0; old = nexto)
|
|
|
|
|
{
|
|
|
|
|
nexto = old->next;
|
|
|
|
|
old->next = new;
|
|
|
|
|
new = old;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vpaths = new;
|
|
|
|
|
|
|
|
|
|
/* If there is a VPATH variable with a nonnull value, construct the
|
|
|
|
|
general VPATH list from it. We use variable_expand rather than just
|
|
|
|
|
calling lookup_variable so that it will be recursively expanded. */
|
1993-06-26 04:03:51 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
|
|
|
|
int save = warn_undefined_variables_flag;
|
|
|
|
|
warn_undefined_variables_flag = 0;
|
|
|
|
|
|
1993-07-09 09:30:13 +08:00
|
|
|
|
p = variable_expand ("$(strip $(VPATH))");
|
1993-06-26 04:03:51 +08:00
|
|
|
|
|
|
|
|
|
warn_undefined_variables_flag = save;
|
|
|
|
|
}
|
|
|
|
|
|
1991-06-04 05:37:27 +09:00
|
|
|
|
if (*p != '\0')
|
|
|
|
|
{
|
1993-07-09 09:30:13 +08:00
|
|
|
|
/* Save the list of vpaths. */
|
|
|
|
|
struct vpath *save_vpaths = vpaths;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char gp[] = "%";
|
1993-07-09 09:30:13 +08:00
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* Empty 'vpaths' so the new one will have no next, and 'vpaths'
|
2013-05-17 14:29:46 +08:00
|
|
|
|
will still be nil if P contains no existing directories. */
|
1993-07-09 09:30:13 +08:00
|
|
|
|
vpaths = 0;
|
|
|
|
|
|
|
|
|
|
/* Parse P. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
construct_vpath_list (gp, p);
|
1993-07-09 09:30:13 +08:00
|
|
|
|
|
|
|
|
|
/* Store the created path as the general path,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
and restore the old list of vpaths. */
|
1993-07-09 09:30:13 +08:00
|
|
|
|
general_vpath = vpaths;
|
|
|
|
|
vpaths = save_vpaths;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
|
|
|
|
/* If there is a GPATH variable with a nonnull value, construct the
|
|
|
|
|
GPATH list from it. We use variable_expand rather than just
|
|
|
|
|
calling lookup_variable so that it will be recursively expanded. */
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
|
|
|
|
int save = warn_undefined_variables_flag;
|
|
|
|
|
warn_undefined_variables_flag = 0;
|
|
|
|
|
|
|
|
|
|
p = variable_expand ("$(strip $(GPATH))");
|
|
|
|
|
|
|
|
|
|
warn_undefined_variables_flag = save;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*p != '\0')
|
|
|
|
|
{
|
|
|
|
|
/* Save the list of vpaths. */
|
|
|
|
|
struct vpath *save_vpaths = vpaths;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char gp[] = "%";
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* Empty 'vpaths' so the new one will have no next, and 'vpaths'
|
2013-05-17 14:29:46 +08:00
|
|
|
|
will still be nil if P contains no existing directories. */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
vpaths = 0;
|
|
|
|
|
|
|
|
|
|
/* Parse P. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
construct_vpath_list (gp, p);
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
|
|
|
|
/* Store the created path as the GPATH,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
and restore the old list of vpaths. */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
gpaths = vpaths;
|
|
|
|
|
vpaths = save_vpaths;
|
|
|
|
|
}
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 17:47:41 +08:00
|
|
|
|
/* Construct the VPATH listing for the PATTERN and DIRPATH given.
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
This function is called to generate selective VPATH lists and also for
|
|
|
|
|
the general VPATH list (which is in fact just a selective VPATH that
|
|
|
|
|
is applied to everything). The returned pointer is either put in the
|
|
|
|
|
linked list of all selective VPATH lists or in the GENERAL_VPATH
|
|
|
|
|
variable.
|
|
|
|
|
|
2009-12-11 17:47:41 +08:00
|
|
|
|
If DIRPATH is nil, remove all previous listings with the same
|
1997-08-28 04:30:54 +08:00
|
|
|
|
pattern. If PATTERN is nil, remove all VPATH listings. Existing
|
2009-12-11 17:47:41 +08:00
|
|
|
|
and readable directories that are not "." given in the DIRPATH
|
2013-01-21 00:01:01 +08:00
|
|
|
|
separated by the path element separator (defined in makeint.h) are
|
1997-08-28 04:30:54 +08:00
|
|
|
|
loaded into the directory hash table if they are not there already
|
|
|
|
|
and put in the VPATH searchpath for the given pattern with trailing
|
|
|
|
|
slashes stripped off if present (and if the directory is not the
|
|
|
|
|
root, "/"). The length of the longest entry in the list is put in
|
|
|
|
|
the structure as well. The new entry will be at the head of the
|
|
|
|
|
VPATHS chain. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
void
|
2002-10-15 05:54:04 +08:00
|
|
|
|
construct_vpath_list (char *pattern, char *dirpath)
|
1991-06-04 05:37:27 +09:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
unsigned int elem;
|
|
|
|
|
char *p;
|
|
|
|
|
const char **vpath;
|
|
|
|
|
unsigned int maxvpath;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
unsigned int maxelem;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *percent = NULL;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
if (pattern != 0)
|
2006-11-19 04:53:44 +08:00
|
|
|
|
percent = find_percent (pattern);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
if (dirpath == 0)
|
|
|
|
|
{
|
|
|
|
|
/* Remove matching listings. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
struct vpath *path, *lastpath;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
1993-01-09 06:07:33 +08:00
|
|
|
|
lastpath = 0;
|
|
|
|
|
path = vpaths;
|
|
|
|
|
while (path != 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
struct vpath *next = path->next;
|
|
|
|
|
|
|
|
|
|
if (pattern == 0
|
|
|
|
|
|| (((percent == 0 && path->percent == 0)
|
|
|
|
|
|| (percent - pattern == path->percent - path->pattern))
|
|
|
|
|
&& streq (pattern, path->pattern)))
|
|
|
|
|
{
|
|
|
|
|
/* Remove it from the linked list. */
|
|
|
|
|
if (lastpath == 0)
|
|
|
|
|
vpaths = path->next;
|
|
|
|
|
else
|
|
|
|
|
lastpath->next = next;
|
|
|
|
|
|
|
|
|
|
/* Free its unused storage. */
|
2010-07-19 15:10:53 +08:00
|
|
|
|
/* MSVC erroneously warns without a cast here. */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
free ((void *)path->searchpath);
|
|
|
|
|
free (path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
lastpath = path;
|
1993-01-09 06:07:33 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
path = next;
|
|
|
|
|
}
|
1993-01-09 06:07:33 +08:00
|
|
|
|
|
1991-06-04 05:37:27 +09:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
2013-05-17 14:29:46 +08:00
|
|
|
|
convert_vpath_to_windows32 (dirpath, ';');
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
/* Skip over any initial separators and blanks. */
|
|
|
|
|
while (*dirpath == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*dirpath))
|
|
|
|
|
++dirpath;
|
|
|
|
|
|
1997-08-28 04:30:54 +08:00
|
|
|
|
/* Figure out the maximum number of VPATH entries and put it in
|
|
|
|
|
MAXELEM. We start with 2, one before the first separator and one
|
|
|
|
|
nil (the list terminator) and increment our estimated number for
|
|
|
|
|
each separator or blank we find. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
maxelem = 2;
|
1992-11-17 06:49:14 +08:00
|
|
|
|
p = dirpath;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
while (*p != '\0')
|
2000-06-07 13:43:37 +08:00
|
|
|
|
if (*p++ == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*p))
|
1991-06-04 05:37:27 +09:00
|
|
|
|
++maxelem;
|
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
vpath = xmalloc (maxelem * sizeof (const char *));
|
1991-06-04 05:37:27 +09:00
|
|
|
|
maxvpath = 0;
|
|
|
|
|
|
1992-11-11 04:29:48 +08:00
|
|
|
|
elem = 0;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
p = dirpath;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
while (*p != '\0')
|
|
|
|
|
{
|
|
|
|
|
char *v;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
|
|
/* Find the end of this entry. */
|
|
|
|
|
v = p;
|
2007-12-22 18:55:30 +08:00
|
|
|
|
while (*p != '\0'
|
|
|
|
|
#if defined(HAVE_DOS_PATHS) && (PATH_SEPARATOR_CHAR == ':')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Platforms whose PATH_SEPARATOR_CHAR is ':' and which
|
|
|
|
|
also define HAVE_DOS_PATHS would like us to recognize
|
|
|
|
|
colons after the drive letter in the likes of
|
|
|
|
|
"D:/foo/bar:C:/xyzzy". */
|
|
|
|
|
&& (*p != PATH_SEPARATOR_CHAR
|
|
|
|
|
|| (p == v + 1 && (p[1] == '/' || p[1] == '\\')))
|
2007-12-22 18:55:30 +08:00
|
|
|
|
#else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
&& *p != PATH_SEPARATOR_CHAR
|
2007-12-22 18:55:30 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
&& !isblank ((unsigned char)*p))
|
|
|
|
|
++p;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
len = p - v;
|
|
|
|
|
/* Make sure there's no trailing slash,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
but still allow "/" as a directory. */
|
2007-12-22 18:55:30 +08:00
|
|
|
|
#if defined(__MSDOS__) || defined(__EMX__) || defined(HAVE_DOS_PATHS)
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* We need also to leave alone a trailing slash in "d:/". */
|
|
|
|
|
if (len > 3 || (len > 1 && v[1] != ':'))
|
|
|
|
|
#endif
|
1991-06-04 05:37:27 +09:00
|
|
|
|
if (len > 1 && p[-1] == '/')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
--len;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
/* Put the directory on the vpath list. */
|
1993-01-26 09:51:23 +08:00
|
|
|
|
if (len > 1 || *v != '.')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
vpath[elem++] = dir_name (strcache_add_len (v, len));
|
|
|
|
|
if (len > maxvpath)
|
|
|
|
|
maxvpath = len;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
1992-11-11 04:29:48 +08:00
|
|
|
|
|
1997-08-28 04:30:54 +08:00
|
|
|
|
/* Skip over separators and blanks between entries. */
|
2000-06-07 13:43:37 +08:00
|
|
|
|
while (*p == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*p))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
++p;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elem > 0)
|
|
|
|
|
{
|
|
|
|
|
struct vpath *path;
|
|
|
|
|
/* ELEM is now incremented one element past the last
|
2013-05-17 14:29:46 +08:00
|
|
|
|
entry, to where the nil-pointer terminator goes.
|
|
|
|
|
Usually this is maxelem - 1. If not, shrink down. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
if (elem < (maxelem - 1))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
vpath = xrealloc (vpath, (elem+1) * sizeof (const char *));
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
/* Put the nil-pointer terminator on the end of the VPATH list. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
vpath[elem] = NULL;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
/* Construct the vpath structure and put it into the linked list. */
|
2006-04-10 06:09:24 +08:00
|
|
|
|
path = xmalloc (sizeof (struct vpath));
|
1991-06-04 05:37:27 +09:00
|
|
|
|
path->searchpath = vpath;
|
|
|
|
|
path->maxlen = maxvpath;
|
|
|
|
|
path->next = vpaths;
|
|
|
|
|
vpaths = path;
|
|
|
|
|
|
|
|
|
|
/* Set up the members. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
path->pattern = strcache_add (pattern);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
path->patlen = strlen (pattern);
|
2007-03-20 11:02:26 +08:00
|
|
|
|
path->percent = percent ? path->pattern + (percent - pattern) : 0;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
else
|
2006-11-19 04:53:44 +08:00
|
|
|
|
/* There were no entries, so free whatever space we allocated. */
|
2010-07-19 15:10:53 +08:00
|
|
|
|
/* MSVC erroneously warns without a cast here. */
|
|
|
|
|
free ((void *)vpath);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
1997-08-19 02:11:04 +08:00
|
|
|
|
/* Search the GPATH list for a pathname string that matches the one passed
|
|
|
|
|
in. If it is found, return 1. Otherwise we return 0. */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
|
|
|
|
int
|
2006-11-19 04:53:44 +08:00
|
|
|
|
gpath_search (const char *file, unsigned int len)
|
1997-04-07 15:21:16 +08:00
|
|
|
|
{
|
1997-08-19 02:11:04 +08:00
|
|
|
|
if (gpaths && (len <= gpaths->maxlen))
|
2013-07-22 05:52:13 +08:00
|
|
|
|
{
|
|
|
|
|
const char **gp;
|
|
|
|
|
for (gp = gpaths->searchpath; *gp != NULL; ++gp)
|
|
|
|
|
if (strneq (*gp, file, len) && (*gp)[len] == '\0')
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Search the given VPATH list for a directory where the name pointed to by
|
|
|
|
|
FILE exists. If it is found, we return a cached name of the existing file
|
|
|
|
|
and set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
|
2009-11-13 00:42:36 +08:00
|
|
|
|
stat call was done). Also set the matching directory index in PATH_INDEX
|
|
|
|
|
if it is not NULL. Otherwise we return NULL. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
static const char *
|
|
|
|
|
selective_vpath_search (struct vpath *path, const char *file,
|
2009-11-13 00:42:36 +08:00
|
|
|
|
FILE_TIMESTAMP *mtime_ptr, unsigned int* path_index)
|
1991-06-04 05:37:27 +09:00
|
|
|
|
{
|
|
|
|
|
int not_target;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
char *name;
|
|
|
|
|
const char *n;
|
|
|
|
|
const char *filename;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char **vpath = path->searchpath;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
unsigned int maxvpath = path->maxlen;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
unsigned int i;
|
2013-07-22 05:52:13 +08:00
|
|
|
|
unsigned int flen, name_dplen;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
int exists = 0;
|
|
|
|
|
|
|
|
|
|
/* Find out if *FILE is a target.
|
|
|
|
|
If and only if it is NOT a target, we will accept prospective
|
|
|
|
|
files that don't exist but are mentioned in a makefile. */
|
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
struct file *f = lookup_file (file);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
not_target = f == 0 || !f->is_target;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
flen = strlen (file);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
/* Split *FILE into a directory prefix and a name-within-directory.
|
2007-03-20 11:02:26 +08:00
|
|
|
|
NAME_DPLEN gets the length of the prefix; FILENAME gets the pointer to
|
|
|
|
|
the name-within-directory and FLEN is its length. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
n = strrchr (file, '/');
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* We need the rightmost slash or backslash. */
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
const char *bslash = strrchr (file, '\\');
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (!n || bslash > n)
|
|
|
|
|
n = bslash;
|
|
|
|
|
}
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#endif
|
2007-03-20 11:02:26 +08:00
|
|
|
|
name_dplen = n != 0 ? n - file : 0;
|
|
|
|
|
filename = name_dplen > 0 ? n + 1 : file;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
if (name_dplen > 0)
|
|
|
|
|
flen -= name_dplen + 1;
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Get enough space for the biggest VPATH entry, a slash, the directory
|
|
|
|
|
prefix that came with FILE, another slash (although this one may not
|
|
|
|
|
always be necessary), the filename, and a null terminator. */
|
|
|
|
|
name = alloca (maxvpath + 1 + name_dplen + 1 + flen + 1);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
/* Try each VPATH entry. */
|
|
|
|
|
for (i = 0; vpath[i] != 0; ++i)
|
|
|
|
|
{
|
1993-02-09 06:50:00 +08:00
|
|
|
|
int exists_in_cache = 0;
|
2013-07-22 05:52:13 +08:00
|
|
|
|
char *p = name;
|
|
|
|
|
unsigned int vlen = strlen (vpath[i]);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Put the next VPATH entry into NAME at P and increment P past it. */
|
|
|
|
|
memcpy (p, vpath[i], vlen);
|
|
|
|
|
p += vlen;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
/* Add the directory prefix already in *FILE. */
|
|
|
|
|
if (name_dplen > 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#ifndef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
*p++ = '/';
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
memcpy (p, file, name_dplen);
|
|
|
|
|
p += name_dplen;
|
|
|
|
|
}
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* Cause the next if to treat backslash and slash alike. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
if (p != name && p[-1] == '\\' )
|
2013-05-17 14:29:46 +08:00
|
|
|
|
p[-1] = '/';
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#endif
|
1991-06-04 05:37:27 +09:00
|
|
|
|
/* Now add the name-within-directory at the end of NAME. */
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#ifndef VMS
|
2007-03-20 11:02:26 +08:00
|
|
|
|
if (p != name && p[-1] != '/')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
*p = '/';
|
|
|
|
|
memcpy (p + 1, filename, flen + 1);
|
|
|
|
|
}
|
1993-03-25 04:13:56 +08:00
|
|
|
|
else
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
memcpy (p, filename, flen + 1);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
1993-12-23 12:42:29 +08:00
|
|
|
|
/* Check if the file is mentioned in a makefile. If *FILE is not
|
2013-05-17 14:29:46 +08:00
|
|
|
|
a target, that is enough for us to decide this file exists.
|
|
|
|
|
If *FILE is a target, then the file must be mentioned in the
|
|
|
|
|
makefile also as a target to be chosen.
|
1993-12-23 12:42:29 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
The restriction that *FILE must not be a target for a
|
|
|
|
|
makefile-mentioned file to be chosen was added by an
|
|
|
|
|
inadequately commented change in July 1990; I am not sure off
|
|
|
|
|
hand what problem it fixes.
|
1993-12-23 12:42:29 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
In December 1993 I loosened this restriction to allow a file
|
|
|
|
|
to be chosen if it is mentioned as a target in a makefile. This
|
|
|
|
|
seem logical.
|
2006-02-07 00:21:59 +08:00
|
|
|
|
|
|
|
|
|
Special handling for -W / -o: make sure we preserve the special
|
|
|
|
|
values here. Actually this whole thing is a little bogus: I think
|
|
|
|
|
we should ditch the name/hname thing and look into the renamed
|
|
|
|
|
capability that already exists for files: that is, have a new struct
|
|
|
|
|
file* entry for the VPATH-found file, and set the renamed field if
|
|
|
|
|
we use it.
|
|
|
|
|
*/
|
1993-12-23 12:42:29 +08:00
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
struct file *f = lookup_file (name);
|
|
|
|
|
if (f != 0)
|
2006-02-07 00:21:59 +08:00
|
|
|
|
{
|
|
|
|
|
exists = not_target || f->is_target;
|
|
|
|
|
if (exists && mtime_ptr
|
|
|
|
|
&& (f->last_mtime == OLD_MTIME || f->last_mtime == NEW_MTIME))
|
|
|
|
|
{
|
|
|
|
|
*mtime_ptr = f->last_mtime;
|
|
|
|
|
mtime_ptr = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
1993-12-23 12:42:29 +08:00
|
|
|
|
}
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
if (!exists)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* That file wasn't mentioned in the makefile.
|
|
|
|
|
See if it actually exists. */
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#ifdef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
exists_in_cache = exists = dir_file_exists_p (vpath[i], filename);
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Clobber a null into the name at the last slash.
|
|
|
|
|
Now NAME is the name of the directory to look in. */
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
|
|
/* We know the directory is in the hash table now because either
|
|
|
|
|
construct_vpath_list or the code just above put it there.
|
|
|
|
|
Does the file we seek exist in it? */
|
|
|
|
|
exists_in_cache = exists = dir_file_exists_p (name, filename);
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
if (exists)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* The file is in the directory cache.
|
|
|
|
|
Now check that it actually exists in the filesystem.
|
|
|
|
|
The cache may be out of date. When vpath thinks a file
|
|
|
|
|
exists, but stat fails for it, confusion results in the
|
|
|
|
|
higher levels. */
|
1993-02-09 06:50:00 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
struct stat st;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#ifndef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Put the slash back in NAME. */
|
|
|
|
|
*p = '/';
|
1997-08-28 04:30:54 +08:00
|
|
|
|
#endif
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (exists_in_cache) /* Makefile-mentioned file need not exist. */
|
|
|
|
|
{
|
2003-01-30 13:22:52 +08:00
|
|
|
|
int e;
|
|
|
|
|
|
|
|
|
|
EINTRLOOP (e, stat (name, &st)); /* Does it really exist? */
|
|
|
|
|
if (e != 0)
|
|
|
|
|
{
|
|
|
|
|
exists = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2006-02-07 00:21:59 +08:00
|
|
|
|
|
|
|
|
|
/* Store the modtime into *MTIME_PTR for the caller. */
|
|
|
|
|
if (mtime_ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
*mtime_ptr = FILE_TIMESTAMP_STAT_MODTIME (name, st);
|
|
|
|
|
mtime_ptr = 0;
|
|
|
|
|
}
|
2003-01-30 13:22:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We have found a file.
|
2007-03-20 11:02:26 +08:00
|
|
|
|
If we get here and mtime_ptr hasn't been set, record
|
2006-02-07 00:21:59 +08:00
|
|
|
|
UNKNOWN_MTIME to indicate this. */
|
2003-01-30 13:22:52 +08:00
|
|
|
|
if (mtime_ptr != 0)
|
2006-02-07 00:21:59 +08:00
|
|
|
|
*mtime_ptr = UNKNOWN_MTIME;
|
2003-01-30 13:22:52 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Store the name we found and return it. */
|
|
|
|
|
|
2009-11-13 00:42:36 +08:00
|
|
|
|
if (path_index)
|
|
|
|
|
*path_index = i;
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
return strcache_add_len (name, (p + 1 - name) + flen);
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Search the VPATH list whose pattern matches FILE for a directory where FILE
|
|
|
|
|
exists. If it is found, return the cached name of an existing file, and
|
|
|
|
|
set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
|
2009-11-13 00:42:36 +08:00
|
|
|
|
stat call was done). Also set the matching directory index in VPATH_INDEX
|
|
|
|
|
and PATH_INDEX if they are not NULL. Otherwise we return 0. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
|
|
|
|
|
const char *
|
2009-11-13 00:42:36 +08:00
|
|
|
|
vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr,
|
|
|
|
|
unsigned int* vpath_index, unsigned int* path_index)
|
2007-03-20 11:02:26 +08:00
|
|
|
|
{
|
|
|
|
|
struct vpath *v;
|
|
|
|
|
|
|
|
|
|
/* If there are no VPATH entries or FILENAME starts at the root,
|
|
|
|
|
there is nothing we can do. */
|
|
|
|
|
|
|
|
|
|
if (file[0] == '/'
|
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
|
|
|
|
|| file[0] == '\\' || file[1] == ':'
|
|
|
|
|
#endif
|
|
|
|
|
|| (vpaths == 0 && general_vpath == 0))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2009-11-13 00:42:36 +08:00
|
|
|
|
if (vpath_index)
|
|
|
|
|
{
|
|
|
|
|
*vpath_index = 0;
|
|
|
|
|
*path_index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
for (v = vpaths; v != 0; v = v->next)
|
2009-11-13 00:42:36 +08:00
|
|
|
|
{
|
|
|
|
|
if (pattern_matches (v->pattern, v->percent, file))
|
|
|
|
|
{
|
|
|
|
|
const char *p = selective_vpath_search (
|
|
|
|
|
v, file, mtime_ptr, path_index);
|
|
|
|
|
if (p)
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vpath_index)
|
|
|
|
|
++*vpath_index;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
|
|
|
|
|
if (general_vpath != 0)
|
|
|
|
|
{
|
2009-11-13 00:42:36 +08:00
|
|
|
|
const char *p = selective_vpath_search (
|
|
|
|
|
general_vpath, file, mtime_ptr, path_index);
|
2007-03-20 11:02:26 +08:00
|
|
|
|
if (p)
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
1991-06-04 05:37:27 +09:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-11-13 00:42:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
/* Print the data base of VPATH search paths. */
|
|
|
|
|
|
|
|
|
|
void
|
2002-10-15 05:54:04 +08:00
|
|
|
|
print_vpath_data_base (void)
|
1991-06-04 05:37:27 +09:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
unsigned int nvpaths;
|
|
|
|
|
struct vpath *v;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
1999-07-28 14:23:37 +08:00
|
|
|
|
puts (_("\n# VPATH Search Paths\n"));
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
nvpaths = 0;
|
|
|
|
|
for (v = vpaths; v != 0; v = v->next)
|
|
|
|
|
{
|
|
|
|
|
register unsigned int i;
|
|
|
|
|
|
|
|
|
|
++nvpaths;
|
|
|
|
|
|
|
|
|
|
printf ("vpath %s ", v->pattern);
|
|
|
|
|
|
|
|
|
|
for (i = 0; v->searchpath[i] != 0; ++i)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf ("%s%c", v->searchpath[i],
|
|
|
|
|
v->searchpath[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vpaths == 0)
|
2012-03-04 08:24:20 +08:00
|
|
|
|
puts (_("# No 'vpath' search paths."));
|
1991-06-04 05:37:27 +09:00
|
|
|
|
else
|
2012-03-04 08:24:20 +08:00
|
|
|
|
printf (_("\n# %u 'vpath' search paths.\n"), nvpaths);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
if (general_vpath == 0)
|
2012-03-04 08:24:20 +08:00
|
|
|
|
puts (_("\n# No general ('VPATH' variable) search path."));
|
1991-06-04 05:37:27 +09:00
|
|
|
|
else
|
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char **path = general_vpath->searchpath;
|
|
|
|
|
unsigned int i;
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
fputs (_("\n# General ('VPATH' variable) search path:\n# "), stdout);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
|
|
|
|
|
for (i = 0; path[i] != 0; ++i)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf ("%s%c", path[i],
|
|
|
|
|
path[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
|
1991-06-04 05:37:27 +09:00
|
|
|
|
}
|
|
|
|
|
}
|