[53] | 1 | /* Implementation of pattern-matching file search paths for GNU Make.
|
---|
[3138] | 2 | Copyright (C) 1988-2016 Free Software Foundation, Inc.
|
---|
[53] | 3 | This file is part of GNU Make.
|
---|
| 4 |
|
---|
[501] | 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
---|
[1989] | 7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 8 | version.
|
---|
[53] | 9 |
|
---|
[501] | 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
[53] | 13 |
|
---|
[501] | 14 | You should have received a copy of the GNU General Public License along with
|
---|
[1989] | 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
[53] | 16 |
|
---|
[3138] | 17 | #include "makeint.h"
|
---|
[53] | 18 | #include "filedef.h"
|
---|
| 19 | #include "variable.h"
|
---|
| 20 | #ifdef WINDOWS32
|
---|
| 21 | #include "pathstuff.h"
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | /* Structure used to represent a selective VPATH searchpath. */
|
---|
| 26 |
|
---|
| 27 | struct vpath
|
---|
| 28 | {
|
---|
[3138] | 29 | struct vpath *next; /* Pointer to next struct in the linked list. */
|
---|
[900] | 30 | const char *pattern;/* The pattern to match. */
|
---|
[3138] | 31 | const char *percent;/* Pointer into 'pattern' where the '%' is. */
|
---|
[53] | 32 | unsigned int patlen;/* Length of the pattern. */
|
---|
[900] | 33 | const char **searchpath; /* Null-terminated list of directories. */
|
---|
[53] | 34 | unsigned int maxlen;/* Maximum length of any entry in the list. */
|
---|
| 35 | };
|
---|
| 36 |
|
---|
| 37 | /* Linked-list of all selective VPATHs. */
|
---|
| 38 |
|
---|
| 39 | static struct vpath *vpaths;
|
---|
| 40 |
|
---|
| 41 | /* Structure for the general VPATH given in the variable. */
|
---|
| 42 |
|
---|
| 43 | static struct vpath *general_vpath;
|
---|
| 44 |
|
---|
| 45 | /* Structure for GPATH given in the variable. */
|
---|
| 46 |
|
---|
| 47 | static struct vpath *gpaths;
|
---|
| 48 | |
---|
| 49 |
|
---|
[900] | 50 |
|
---|
| 51 | /* Reverse the chain of selective VPATH lists so they will be searched in the
|
---|
| 52 | order given in the makefiles and construct the list from the VPATH
|
---|
[53] | 53 | variable. */
|
---|
| 54 |
|
---|
[3138] | 55 | void
|
---|
[53] | 56 | build_vpath_lists (void)
|
---|
| 57 | {
|
---|
| 58 | register struct vpath *new = 0;
|
---|
| 59 | register struct vpath *old, *nexto;
|
---|
| 60 | register char *p;
|
---|
| 61 |
|
---|
| 62 | /* Reverse the chain. */
|
---|
| 63 | for (old = vpaths; old != 0; old = nexto)
|
---|
| 64 | {
|
---|
| 65 | nexto = old->next;
|
---|
| 66 | old->next = new;
|
---|
| 67 | new = old;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | vpaths = new;
|
---|
| 71 |
|
---|
| 72 | /* If there is a VPATH variable with a nonnull value, construct the
|
---|
| 73 | general VPATH list from it. We use variable_expand rather than just
|
---|
| 74 | calling lookup_variable so that it will be recursively expanded. */
|
---|
| 75 |
|
---|
| 76 | {
|
---|
| 77 | /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
---|
| 78 | int save = warn_undefined_variables_flag;
|
---|
| 79 | warn_undefined_variables_flag = 0;
|
---|
| 80 |
|
---|
| 81 | p = variable_expand ("$(strip $(VPATH))");
|
---|
| 82 |
|
---|
| 83 | warn_undefined_variables_flag = save;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if (*p != '\0')
|
---|
| 87 | {
|
---|
| 88 | /* Save the list of vpaths. */
|
---|
[900] | 89 | struct vpath *save_vpaths = vpaths;
|
---|
[53] | 90 | char gp[] = "%";
|
---|
[3138] | 91 |
|
---|
| 92 | /* Empty 'vpaths' so the new one will have no next, and 'vpaths'
|
---|
[53] | 93 | will still be nil if P contains no existing directories. */
|
---|
| 94 | vpaths = 0;
|
---|
| 95 |
|
---|
[900] | 96 | /* Parse P. */
|
---|
[53] | 97 | construct_vpath_list (gp, p);
|
---|
| 98 |
|
---|
[3138] | 99 | /* Store the created path as the general path,
|
---|
[53] | 100 | and restore the old list of vpaths. */
|
---|
| 101 | general_vpath = vpaths;
|
---|
| 102 | vpaths = save_vpaths;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /* If there is a GPATH variable with a nonnull value, construct the
|
---|
| 106 | GPATH list from it. We use variable_expand rather than just
|
---|
| 107 | calling lookup_variable so that it will be recursively expanded. */
|
---|
| 108 |
|
---|
| 109 | {
|
---|
| 110 | /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
---|
| 111 | int save = warn_undefined_variables_flag;
|
---|
| 112 | warn_undefined_variables_flag = 0;
|
---|
| 113 |
|
---|
| 114 | p = variable_expand ("$(strip $(GPATH))");
|
---|
| 115 |
|
---|
| 116 | warn_undefined_variables_flag = save;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | if (*p != '\0')
|
---|
| 120 | {
|
---|
| 121 | /* Save the list of vpaths. */
|
---|
[900] | 122 | struct vpath *save_vpaths = vpaths;
|
---|
[53] | 123 | char gp[] = "%";
|
---|
[3138] | 124 |
|
---|
| 125 | /* Empty 'vpaths' so the new one will have no next, and 'vpaths'
|
---|
[53] | 126 | will still be nil if P contains no existing directories. */
|
---|
| 127 | vpaths = 0;
|
---|
| 128 |
|
---|
[900] | 129 | /* Parse P. */
|
---|
[53] | 130 | construct_vpath_list (gp, p);
|
---|
| 131 |
|
---|
[3138] | 132 | /* Store the created path as the GPATH,
|
---|
[53] | 133 | and restore the old list of vpaths. */
|
---|
| 134 | gpaths = vpaths;
|
---|
| 135 | vpaths = save_vpaths;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[2596] | 138 | |
---|
[53] | 139 |
|
---|
| 140 | /* Construct the VPATH listing for the PATTERN and DIRPATH given.
|
---|
| 141 |
|
---|
| 142 | This function is called to generate selective VPATH lists and also for
|
---|
| 143 | the general VPATH list (which is in fact just a selective VPATH that
|
---|
| 144 | is applied to everything). The returned pointer is either put in the
|
---|
| 145 | linked list of all selective VPATH lists or in the GENERAL_VPATH
|
---|
[2596] | 146 | variable.
|
---|
[53] | 147 |
|
---|
[2596] | 148 | If DIRPATH is nil, remove all previous listings with the same
|
---|
[3138] | 149 | pattern. If PATTERN is nil, remove all VPATH listings. Existing
|
---|
[53] | 150 | and readable directories that are not "." given in the DIRPATH
|
---|
| 151 | separated by the path element separator (defined in makeint.h) are
|
---|
| 152 | loaded into the directory hash table if they are not there already
|
---|
| 153 | and put in the VPATH searchpath for the given pattern with trailing
|
---|
| 154 | slashes stripped off if present (and if the directory is not the
|
---|
| 155 | root, "/"). The length of the longest entry in the list is put in
|
---|
| 156 | the structure as well. The new entry will be at the head of the
|
---|
| 157 | VPATHS chain. */
|
---|
| 158 |
|
---|
| 159 | void
|
---|
[900] | 160 | construct_vpath_list (char *pattern, char *dirpath)
|
---|
| 161 | {
|
---|
| 162 | unsigned int elem;
|
---|
| 163 | char *p;
|
---|
[53] | 164 | const char **vpath;
|
---|
[900] | 165 | unsigned int maxvpath;
|
---|
[53] | 166 | unsigned int maxelem;
|
---|
| 167 | const char *percent = NULL;
|
---|
[900] | 168 |
|
---|
[53] | 169 | if (pattern != 0)
|
---|
| 170 | percent = find_percent (pattern);
|
---|
| 171 |
|
---|
| 172 | if (dirpath == 0)
|
---|
[900] | 173 | {
|
---|
[53] | 174 | /* Remove matching listings. */
|
---|
| 175 | struct vpath *path, *lastpath;
|
---|
| 176 |
|
---|
| 177 | lastpath = 0;
|
---|
[3138] | 178 | path = vpaths;
|
---|
| 179 | while (path != 0)
|
---|
[53] | 180 | {
|
---|
[3138] | 181 | struct vpath *next = path->next;
|
---|
| 182 |
|
---|
| 183 | if (pattern == 0
|
---|
| 184 | || (((percent == 0 && path->percent == 0)
|
---|
| 185 | || (percent - pattern == path->percent - path->pattern))
|
---|
| 186 | && streq (pattern, path->pattern)))
|
---|
| 187 | {
|
---|
| 188 | /* Remove it from the linked list. */
|
---|
| 189 | if (lastpath == 0)
|
---|
| 190 | vpaths = path->next;
|
---|
[53] | 191 | else
|
---|
[3138] | 192 | lastpath->next = next;
|
---|
[2596] | 193 |
|
---|
[3138] | 194 | /* Free its unused storage. */
|
---|
| 195 | /* MSVC erroneously warns without a cast here. */
|
---|
| 196 | free ((void *)path->searchpath);
|
---|
| 197 | free (path);
|
---|
| 198 | }
|
---|
[53] | 199 | else
|
---|
[3138] | 200 | lastpath = path;
|
---|
| 201 |
|
---|
[53] | 202 | path = next;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
[3138] | 207 |
|
---|
[53] | 208 | #ifdef WINDOWS32
|
---|
| 209 | convert_vpath_to_windows32 (dirpath, ';');
|
---|
[900] | 210 | #endif
|
---|
[3138] | 211 |
|
---|
[900] | 212 | /* Skip over any initial separators and blanks. */
|
---|
| 213 | while (STOP_SET (*dirpath, MAP_BLANK|MAP_PATHSEP))
|
---|
[53] | 214 | ++dirpath;
|
---|
| 215 |
|
---|
| 216 | /* Figure out the maximum number of VPATH entries and put it in
|
---|
| 217 | MAXELEM. We start with 2, one before the first separator and one
|
---|
| 218 | nil (the list terminator) and increment our estimated number for
|
---|
| 219 | each separator or blank we find. */
|
---|
| 220 | maxelem = 2;
|
---|
[3138] | 221 | p = dirpath;
|
---|
[53] | 222 | while (*p != '\0')
|
---|
| 223 | if (STOP_SET (*p++, MAP_BLANK|MAP_PATHSEP))
|
---|
[900] | 224 | ++maxelem;
|
---|
[53] | 225 |
|
---|
| 226 | vpath = xmalloc (maxelem * sizeof (const char *));
|
---|
[900] | 227 | maxvpath = 0;
|
---|
[53] | 228 |
|
---|
| 229 | elem = 0;
|
---|
| 230 | p = dirpath;
|
---|
| 231 | while (*p != '\0')
|
---|
| 232 | {
|
---|
| 233 | char *v;
|
---|
| 234 | unsigned int len;
|
---|
| 235 |
|
---|
[1989] | 236 | /* Find the end of this entry. */
|
---|
| 237 | v = p;
|
---|
[3138] | 238 | while (*p != '\0'
|
---|
| 239 | #if defined(HAVE_DOS_PATHS) && (PATH_SEPARATOR_CHAR == ':')
|
---|
| 240 | /* Platforms whose PATH_SEPARATOR_CHAR is ':' and which
|
---|
| 241 | also define HAVE_DOS_PATHS would like us to recognize
|
---|
| 242 | colons after the drive letter in the likes of
|
---|
| 243 | "D:/foo/bar:C:/xyzzy". */
|
---|
[1989] | 244 | && (*p != PATH_SEPARATOR_CHAR
|
---|
[3138] | 245 | || (p == v + 1 && (p[1] == '/' || p[1] == '\\')))
|
---|
[1989] | 246 | #else
|
---|
[3138] | 247 | && *p != PATH_SEPARATOR_CHAR
|
---|
| 248 | #endif
|
---|
[53] | 249 | && !ISBLANK (*p))
|
---|
| 250 | ++p;
|
---|
| 251 |
|
---|
[3138] | 252 | len = p - v;
|
---|
[1989] | 253 | /* Make sure there's no trailing slash,
|
---|
[53] | 254 | but still allow "/" as a directory. */
|
---|
| 255 | #if defined(__MSDOS__) || defined(__EMX__) || defined(HAVE_DOS_PATHS)
|
---|
| 256 | /* We need also to leave alone a trailing slash in "d:/". */
|
---|
| 257 | if (len > 3 || (len > 1 && v[1] != ':'))
|
---|
[3138] | 258 | #endif
|
---|
[53] | 259 | if (len > 1 && p[-1] == '/')
|
---|
[900] | 260 | --len;
|
---|
[53] | 261 |
|
---|
[3138] | 262 | /* Put the directory on the vpath list. */
|
---|
[900] | 263 | if (len > 1 || *v != '.')
|
---|
| 264 | {
|
---|
| 265 | vpath[elem++] = dir_name (strcache_add_len (v, len));
|
---|
[3138] | 266 | if (len > maxvpath)
|
---|
[53] | 267 | maxvpath = len;
|
---|
| 268 | }
|
---|
[3138] | 269 |
|
---|
| 270 | /* Skip over separators and blanks between entries. */
|
---|
[53] | 271 | while (STOP_SET (*p, MAP_BLANK|MAP_PATHSEP))
|
---|
| 272 | ++p;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | if (elem > 0)
|
---|
| 276 | {
|
---|
[3138] | 277 | struct vpath *path;
|
---|
| 278 | /* ELEM is now incremented one element past the last
|
---|
[53] | 279 | entry, to where the nil-pointer terminator goes.
|
---|
[3138] | 280 | Usually this is maxelem - 1. If not, shrink down. */
|
---|
[53] | 281 | if (elem < (maxelem - 1))
|
---|
| 282 | vpath = xrealloc (vpath, (elem+1) * sizeof (const char *));
|
---|
[900] | 283 |
|
---|
[53] | 284 | /* Put the nil-pointer terminator on the end of the VPATH list. */
|
---|
| 285 | vpath[elem] = NULL;
|
---|
[900] | 286 |
|
---|
[53] | 287 | /* Construct the vpath structure and put it into the linked list. */
|
---|
| 288 | path = xmalloc (sizeof (struct vpath));
|
---|
| 289 | path->searchpath = vpath;
|
---|
| 290 | path->maxlen = maxvpath;
|
---|
| 291 | path->next = vpaths;
|
---|
| 292 | vpaths = path;
|
---|
[900] | 293 |
|
---|
[53] | 294 | /* Set up the members. */
|
---|
[900] | 295 | path->pattern = strcache_add (pattern);
|
---|
[53] | 296 | path->patlen = strlen (pattern);
|
---|
| 297 | path->percent = percent ? path->pattern + (percent - pattern) : 0;
|
---|
[900] | 298 | }
|
---|
[2596] | 299 | else
|
---|
| 300 | /* There were no entries, so free whatever space we allocated. */
|
---|
[53] | 301 | /* MSVC erroneously warns without a cast here. */
|
---|
| 302 | free ((void *)vpath);
|
---|
| 303 | }
|
---|
| 304 | |
---|
| 305 |
|
---|
| 306 | /* Search the GPATH list for a pathname string that matches the one passed
|
---|
[900] | 307 | in. If it is found, return 1. Otherwise we return 0. */
|
---|
[53] | 308 |
|
---|
| 309 | int
|
---|
[3138] | 310 | gpath_search (const char *file, unsigned int len)
|
---|
| 311 | {
|
---|
| 312 | if (gpaths && (len <= gpaths->maxlen))
|
---|
| 313 | {
|
---|
| 314 | const char **gp;
|
---|
| 315 | for (gp = gpaths->searchpath; *gp != NULL; ++gp)
|
---|
[53] | 316 | if (strneq (*gp, file, len) && (*gp)[len] == '\0')
|
---|
| 317 | return 1;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | return 0;
|
---|
[900] | 321 | }
|
---|
| 322 | |
---|
| 323 |
|
---|
[2596] | 324 |
|
---|
| 325 | /* Search the given VPATH list for a directory where the name pointed to by
|
---|
[53] | 326 | FILE exists. If it is found, we return a cached name of the existing file
|
---|
[900] | 327 | and set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
|
---|
| 328 | stat call was done). Also set the matching directory index in PATH_INDEX
|
---|
[2596] | 329 | if it is not NULL. Otherwise we return NULL. */
|
---|
[53] | 330 |
|
---|
| 331 | static const char *
|
---|
[900] | 332 | selective_vpath_search (struct vpath *path, const char *file,
|
---|
| 333 | FILE_TIMESTAMP *mtime_ptr, unsigned int* path_index)
|
---|
| 334 | {
|
---|
| 335 | int not_target;
|
---|
[53] | 336 | char *name;
|
---|
[900] | 337 | const char *n;
|
---|
[3138] | 338 | const char *filename;
|
---|
[53] | 339 | const char **vpath = path->searchpath;
|
---|
| 340 | unsigned int maxvpath = path->maxlen;
|
---|
| 341 | unsigned int i;
|
---|
| 342 | unsigned int flen, name_dplen;
|
---|
| 343 | int exists = 0;
|
---|
| 344 |
|
---|
[900] | 345 | /* Find out if *FILE is a target.
|
---|
[53] | 346 | If and only if it is NOT a target, we will accept prospective
|
---|
| 347 | files that don't exist but are mentioned in a makefile. */
|
---|
| 348 | {
|
---|
[900] | 349 | struct file *f = lookup_file (file);
|
---|
[53] | 350 | not_target = f == 0 || !f->is_target;
|
---|
| 351 | }
|
---|
[900] | 352 |
|
---|
| 353 | flen = strlen (file);
|
---|
[53] | 354 |
|
---|
[900] | 355 | /* Split *FILE into a directory prefix and a name-within-directory.
|
---|
[53] | 356 | NAME_DPLEN gets the length of the prefix; FILENAME gets the pointer to
|
---|
| 357 | the name-within-directory and FLEN is its length. */
|
---|
| 358 |
|
---|
[3138] | 359 | n = strrchr (file, '/');
|
---|
[53] | 360 | #ifdef HAVE_DOS_PATHS
|
---|
| 361 | /* We need the rightmost slash or backslash. */
|
---|
| 362 | {
|
---|
| 363 | const char *bslash = strrchr (file, '\\');
|
---|
[900] | 364 | if (!n || bslash > n)
|
---|
| 365 | n = bslash;
|
---|
[53] | 366 | }
|
---|
| 367 | #endif
|
---|
| 368 | name_dplen = n != 0 ? n - file : 0;
|
---|
[900] | 369 | filename = name_dplen > 0 ? n + 1 : file;
|
---|
| 370 | if (name_dplen > 0)
|
---|
| 371 | flen -= name_dplen + 1;
|
---|
| 372 |
|
---|
[53] | 373 | /* Get enough space for the biggest VPATH entry, a slash, the directory
|
---|
| 374 | prefix that came with FILE, another slash (although this one may not
|
---|
| 375 | always be necessary), the filename, and a null terminator. */
|
---|
| 376 | name = alloca (maxvpath + 1 + name_dplen + 1 + flen + 1);
|
---|
| 377 |
|
---|
[3138] | 378 | /* Try each VPATH entry. */
|
---|
| 379 | for (i = 0; vpath[i] != 0; ++i)
|
---|
[53] | 380 | {
|
---|
[900] | 381 | int exists_in_cache = 0;
|
---|
| 382 | char *p = name;
|
---|
| 383 | unsigned int vlen = strlen (vpath[i]);
|
---|
[53] | 384 |
|
---|
| 385 | /* Put the next VPATH entry into NAME at P and increment P past it. */
|
---|
| 386 | memcpy (p, vpath[i], vlen);
|
---|
[3138] | 387 | p += vlen;
|
---|
[53] | 388 |
|
---|
[3138] | 389 | /* Add the directory prefix already in *FILE. */
|
---|
| 390 | if (name_dplen > 0)
|
---|
| 391 | {
|
---|
| 392 | #ifndef VMS
|
---|
| 393 | *p++ = '/';
|
---|
[53] | 394 | #else
|
---|
[3138] | 395 | /* VMS: if this is not in VMS format, treat as Unix format */
|
---|
| 396 | if ((*p != ':') && (*p != ']') && (*p != '>'))
|
---|
| 397 | *p++ = '/';
|
---|
[53] | 398 | #endif
|
---|
| 399 | memcpy (p, file, name_dplen);
|
---|
| 400 | p += name_dplen;
|
---|
[900] | 401 | }
|
---|
[3138] | 402 |
|
---|
[53] | 403 | #ifdef HAVE_DOS_PATHS
|
---|
| 404 | /* Cause the next if to treat backslash and slash alike. */
|
---|
| 405 | if (p != name && p[-1] == '\\' )
|
---|
[900] | 406 | p[-1] = '/';
|
---|
[3138] | 407 | #endif
|
---|
| 408 | /* Now add the name-within-directory at the end of NAME. */
|
---|
| 409 | #ifndef VMS
|
---|
| 410 | if (p != name && p[-1] != '/')
|
---|
[53] | 411 | {
|
---|
[3138] | 412 | *p = '/';
|
---|
| 413 | memcpy (p + 1, filename, flen + 1);
|
---|
| 414 | }
|
---|
| 415 | else
|
---|
| 416 | #else
|
---|
| 417 | /* VMS use a slash if no directory terminator present */
|
---|
| 418 | if (p != name && p[-1] != '/' && p[-1] != ':' &&
|
---|
| 419 | p[-1] != '>' && p[-1] != ']')
|
---|
| 420 | {
|
---|
[53] | 421 | *p = '/';
|
---|
[3138] | 422 | memcpy (p + 1, filename, flen + 1);
|
---|
[53] | 423 | }
|
---|
| 424 | else
|
---|
[3138] | 425 | #endif
|
---|
| 426 | memcpy (p, filename, flen + 1);
|
---|
| 427 |
|
---|
[53] | 428 | /* Check if the file is mentioned in a makefile. If *FILE is not
|
---|
[3138] | 429 | a target, that is enough for us to decide this file exists.
|
---|
| 430 | If *FILE is a target, then the file must be mentioned in the
|
---|
| 431 | makefile also as a target to be chosen.
|
---|
| 432 |
|
---|
[53] | 433 | The restriction that *FILE must not be a target for a
|
---|
[3138] | 434 | makefile-mentioned file to be chosen was added by an
|
---|
| 435 | inadequately commented change in July 1990; I am not sure off
|
---|
| 436 | hand what problem it fixes.
|
---|
[501] | 437 |
|
---|
| 438 | In December 1993 I loosened this restriction to allow a file
|
---|
| 439 | to be chosen if it is mentioned as a target in a makefile. This
|
---|
| 440 | seem logical.
|
---|
| 441 |
|
---|
| 442 | Special handling for -W / -o: make sure we preserve the special
|
---|
| 443 | values here. Actually this whole thing is a little bogus: I think
|
---|
| 444 | we should ditch the name/hname thing and look into the renamed
|
---|
[53] | 445 | capability that already exists for files: that is, have a new struct
|
---|
[3138] | 446 | file* entry for the VPATH-found file, and set the renamed field if
|
---|
| 447 | we use it.
|
---|
[501] | 448 | */
|
---|
| 449 | {
|
---|
| 450 | struct file *f = lookup_file (name);
|
---|
| 451 | if (f != 0)
|
---|
| 452 | {
|
---|
| 453 | exists = not_target || f->is_target;
|
---|
| 454 | if (exists && mtime_ptr
|
---|
| 455 | && (f->last_mtime == OLD_MTIME || f->last_mtime == NEW_MTIME))
|
---|
| 456 | {
|
---|
[53] | 457 | *mtime_ptr = f->last_mtime;
|
---|
| 458 | mtime_ptr = 0;
|
---|
| 459 | }
|
---|
[3138] | 460 | }
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[53] | 463 | if (!exists)
|
---|
| 464 | {
|
---|
[3138] | 465 | /* That file wasn't mentioned in the makefile.
|
---|
| 466 | See if it actually exists. */
|
---|
| 467 |
|
---|
| 468 | #ifdef VMS
|
---|
[53] | 469 | /* For VMS syntax just use the original vpath */
|
---|
[3138] | 470 | if (*p != '/')
|
---|
| 471 | exists_in_cache = exists = dir_file_exists_p (vpath[i], filename);
|
---|
| 472 | else
|
---|
| 473 | #endif
|
---|
| 474 | {
|
---|
| 475 | /* Clobber a null into the name at the last slash.
|
---|
| 476 | Now NAME is the name of the directory to look in. */
|
---|
| 477 | *p = '\0';
|
---|
| 478 | /* We know the directory is in the hash table now because either
|
---|
| 479 | construct_vpath_list or the code just above put it there.
|
---|
[53] | 480 | Does the file we seek exist in it? */
|
---|
| 481 | exists_in_cache = exists = dir_file_exists_p (name, filename);
|
---|
[3138] | 482 | }
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | if (exists)
|
---|
| 486 | {
|
---|
| 487 | /* The file is in the directory cache.
|
---|
[53] | 488 | Now check that it actually exists in the filesystem.
|
---|
[3138] | 489 | The cache may be out of date. When vpath thinks a file
|
---|
[53] | 490 | exists, but stat fails for it, confusion results in the
|
---|
| 491 | higher levels. */
|
---|
[3138] | 492 |
|
---|
| 493 | struct stat st;
|
---|
| 494 |
|
---|
| 495 | #ifndef VMS
|
---|
| 496 | /* Put the slash back in NAME. */
|
---|
| 497 | *p = '/';
|
---|
[53] | 498 | #else
|
---|
| 499 | /* If the slash was removed, put it back */
|
---|
[3138] | 500 | if (*p == 0)
|
---|
| 501 | *p = '/';
|
---|
[53] | 502 | #endif
|
---|
| 503 |
|
---|
| 504 | if (exists_in_cache) /* Makefile-mentioned file need not exist. */
|
---|
| 505 | {
|
---|
| 506 | int e;
|
---|
| 507 |
|
---|
| 508 | EINTRLOOP (e, stat (name, &st)); /* Does it really exist? */
|
---|
| 509 | if (e != 0)
|
---|
[501] | 510 | {
|
---|
| 511 | exists = 0;
|
---|
| 512 | continue;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | /* Store the modtime into *MTIME_PTR for the caller. */
|
---|
| 516 | if (mtime_ptr != 0)
|
---|
[53] | 517 | {
|
---|
| 518 | *mtime_ptr = FILE_TIMESTAMP_STAT_MODTIME (name, st);
|
---|
| 519 | mtime_ptr = 0;
|
---|
[900] | 520 | }
|
---|
[501] | 521 | }
|
---|
[53] | 522 |
|
---|
[501] | 523 | /* We have found a file.
|
---|
[53] | 524 | If we get here and mtime_ptr hasn't been set, record
|
---|
[900] | 525 | UNKNOWN_MTIME to indicate this. */
|
---|
| 526 | if (mtime_ptr != 0)
|
---|
[2596] | 527 | *mtime_ptr = UNKNOWN_MTIME;
|
---|
| 528 |
|
---|
| 529 | /* Store the name we found and return it. */
|
---|
[900] | 530 |
|
---|
[3138] | 531 | if (path_index)
|
---|
[53] | 532 | *path_index = i;
|
---|
| 533 |
|
---|
| 534 | return strcache_add_len (name, (p + 1 - name) + flen);
|
---|
| 535 | }
|
---|
[900] | 536 | }
|
---|
| 537 |
|
---|
| 538 | return 0;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[2596] | 541 |
|
---|
| 542 | /* Search the VPATH list whose pattern matches FILE for a directory where FILE
|
---|
[900] | 543 | exists. If it is found, return the cached name of an existing file, and
|
---|
| 544 | set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
|
---|
[2596] | 545 | stat call was done). Also set the matching directory index in VPATH_INDEX
|
---|
| 546 | and PATH_INDEX if they are not NULL. Otherwise we return 0. */
|
---|
[900] | 547 |
|
---|
| 548 | const char *
|
---|
| 549 | vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr,
|
---|
| 550 | unsigned int* vpath_index, unsigned int* path_index)
|
---|
| 551 | {
|
---|
| 552 | struct vpath *v;
|
---|
| 553 |
|
---|
| 554 | /* If there are no VPATH entries or FILENAME starts at the root,
|
---|
| 555 | there is nothing we can do. */
|
---|
| 556 |
|
---|
| 557 | if (file[0] == '/'
|
---|
| 558 | #ifdef HAVE_DOS_PATHS
|
---|
| 559 | || file[0] == '\\' || file[1] == ':'
|
---|
[2596] | 560 | #endif
|
---|
| 561 | || (vpaths == 0 && general_vpath == 0))
|
---|
| 562 | return 0;
|
---|
| 563 |
|
---|
| 564 | if (vpath_index)
|
---|
| 565 | {
|
---|
[900] | 566 | *vpath_index = 0;
|
---|
[2596] | 567 | *path_index = 0;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | for (v = vpaths; v != 0; v = v->next)
|
---|
| 571 | {
|
---|
| 572 | if (pattern_matches (v->pattern, v->percent, file))
|
---|
| 573 | {
|
---|
| 574 | const char *p = selective_vpath_search (
|
---|
[900] | 575 | v, file, mtime_ptr, path_index);
|
---|
[2596] | 576 | if (p)
|
---|
| 577 | return p;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | if (vpath_index)
|
---|
[900] | 581 | ++*vpath_index;
|
---|
| 582 | }
|
---|
[2596] | 583 |
|
---|
| 584 |
|
---|
[900] | 585 | if (general_vpath != 0)
|
---|
| 586 | {
|
---|
| 587 | const char *p = selective_vpath_search (
|
---|
| 588 | general_vpath, file, mtime_ptr, path_index);
|
---|
| 589 | if (p)
|
---|
| 590 | return p;
|
---|
[2596] | 591 | }
|
---|
| 592 |
|
---|
| 593 | return 0;
|
---|
[53] | 594 | }
|
---|
| 595 |
|
---|
| 596 |
|
---|
| 597 |
|
---|
| 598 | |
---|
| 599 |
|
---|
[900] | 600 | /* Print the data base of VPATH search paths. */
|
---|
| 601 |
|
---|
[53] | 602 | void
|
---|
| 603 | print_vpath_data_base (void)
|
---|
| 604 | {
|
---|
| 605 | unsigned int nvpaths;
|
---|
| 606 | struct vpath *v;
|
---|
| 607 |
|
---|
| 608 | puts (_("\n# VPATH Search Paths\n"));
|
---|
| 609 |
|
---|
| 610 | nvpaths = 0;
|
---|
| 611 | for (v = vpaths; v != 0; v = v->next)
|
---|
| 612 | {
|
---|
| 613 | register unsigned int i;
|
---|
| 614 |
|
---|
[3138] | 615 | ++nvpaths;
|
---|
| 616 |
|
---|
[53] | 617 | printf ("vpath %s ", v->pattern);
|
---|
| 618 |
|
---|
| 619 | for (i = 0; v->searchpath[i] != 0; ++i)
|
---|
[3138] | 620 | printf ("%s%c", v->searchpath[i],
|
---|
[53] | 621 | v->searchpath[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
|
---|
[3138] | 622 | }
|
---|
[53] | 623 |
|
---|
| 624 | if (vpaths == 0)
|
---|
[3138] | 625 | puts (_("# No 'vpath' search paths."));
|
---|
[53] | 626 | else
|
---|
| 627 | printf (_("\n# %u 'vpath' search paths.\n"), nvpaths);
|
---|
[900] | 628 |
|
---|
| 629 | if (general_vpath == 0)
|
---|
[53] | 630 | puts (_("\n# No general ('VPATH' variable) search path."));
|
---|
[3138] | 631 | else
|
---|
[53] | 632 | {
|
---|
| 633 | const char **path = general_vpath->searchpath;
|
---|
[3138] | 634 | unsigned int i;
|
---|
| 635 |
|
---|
[53] | 636 | fputs (_("\n# General ('VPATH' variable) search path:\n# "), stdout);
|
---|
| 637 |
|
---|
| 638 | for (i = 0; path[i] != 0; ++i)
|
---|
| 639 | printf ("%s%c", path[i],
|
---|
| 640 | path[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
|
---|
| 641 | }
|
---|
| 642 | }
|
---|