Fix Savannah bug #19348: if the user specified
[make.git] / vpath.c
blob4c79fcddbd208b9ae6d8a1c15c7792d480af27a9
1 /* Implementation of pattern-matching file search paths for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
19 #include "make.h"
20 #include "filedef.h"
21 #include "variable.h"
22 #ifdef WINDOWS32
23 #include "pathstuff.h"
24 #endif
27 /* Structure used to represent a selective VPATH searchpath. */
29 struct vpath
31 struct vpath *next; /* Pointer to next struct in the linked list. */
32 const char *pattern;/* The pattern to match. */
33 const char *percent;/* Pointer into `pattern' where the `%' is. */
34 unsigned int patlen;/* Length of the pattern. */
35 const char **searchpath; /* Null-terminated list of directories. */
36 unsigned int maxlen;/* Maximum length of any entry in the list. */
39 /* Linked-list of all selective VPATHs. */
41 static struct vpath *vpaths;
43 /* Structure for the general VPATH given in the variable. */
45 static struct vpath *general_vpath;
47 /* Structure for GPATH given in the variable. */
49 static struct vpath *gpaths;
52 /* Reverse the chain of selective VPATH lists so they will be searched in the
53 order given in the makefiles and construct the list from the VPATH
54 variable. */
56 void
57 build_vpath_lists ()
59 register struct vpath *new = 0;
60 register struct vpath *old, *nexto;
61 register char *p;
63 /* Reverse the chain. */
64 for (old = vpaths; old != 0; old = nexto)
66 nexto = old->next;
67 old->next = new;
68 new = old;
71 vpaths = new;
73 /* If there is a VPATH variable with a nonnull value, construct the
74 general VPATH list from it. We use variable_expand rather than just
75 calling lookup_variable so that it will be recursively expanded. */
78 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
79 int save = warn_undefined_variables_flag;
80 warn_undefined_variables_flag = 0;
82 p = variable_expand ("$(strip $(VPATH))");
84 warn_undefined_variables_flag = save;
87 if (*p != '\0')
89 /* Save the list of vpaths. */
90 struct vpath *save_vpaths = vpaths;
91 char gp[] = "%";
93 /* Empty `vpaths' so the new one will have no next, and `vpaths'
94 will still be nil if P contains no existing directories. */
95 vpaths = 0;
97 /* Parse P. */
98 construct_vpath_list (gp, p);
100 /* Store the created path as the general path,
101 and restore the old list of vpaths. */
102 general_vpath = vpaths;
103 vpaths = save_vpaths;
106 /* If there is a GPATH variable with a nonnull value, construct the
107 GPATH list from it. We use variable_expand rather than just
108 calling lookup_variable so that it will be recursively expanded. */
111 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
112 int save = warn_undefined_variables_flag;
113 warn_undefined_variables_flag = 0;
115 p = variable_expand ("$(strip $(GPATH))");
117 warn_undefined_variables_flag = save;
120 if (*p != '\0')
122 /* Save the list of vpaths. */
123 struct vpath *save_vpaths = vpaths;
124 char gp[] = "%";
126 /* Empty `vpaths' so the new one will have no next, and `vpaths'
127 will still be nil if P contains no existing directories. */
128 vpaths = 0;
130 /* Parse P. */
131 construct_vpath_list (gp, p);
133 /* Store the created path as the GPATH,
134 and restore the old list of vpaths. */
135 gpaths = vpaths;
136 vpaths = save_vpaths;
140 /* Construct the VPATH listing for the pattern and searchpath given.
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
146 variable.
148 If SEARCHPATH is nil, remove all previous listings with the same
149 pattern. If PATTERN is nil, remove all VPATH listings. Existing
150 and readable directories that are not "." given in the searchpath
151 separated by the path element separator (defined in make.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. */
159 void
160 construct_vpath_list (char *pattern, char *dirpath)
162 unsigned int elem;
163 char *p;
164 const char **vpath;
165 unsigned int maxvpath;
166 unsigned int maxelem;
167 const char *percent = NULL;
169 if (pattern != 0)
170 percent = find_percent (pattern);
172 if (dirpath == 0)
174 /* Remove matching listings. */
175 struct vpath *path, *lastpath;
177 lastpath = 0;
178 path = vpaths;
179 while (path != 0)
181 struct vpath *next = path->next;
183 if (pattern == 0
184 || (((percent == 0 && path->percent == 0)
185 || (percent - pattern == path->percent - path->pattern))
186 && streq (pattern, path->pattern)))
188 /* Remove it from the linked list. */
189 if (lastpath == 0)
190 vpaths = path->next;
191 else
192 lastpath->next = next;
194 /* Free its unused storage. */
195 free (path->searchpath);
196 free (path);
198 else
199 lastpath = path;
201 path = next;
204 return;
207 #ifdef WINDOWS32
208 convert_vpath_to_windows32(dirpath, ';');
209 #endif
211 /* Skip over any initial separators and blanks. */
212 while (*dirpath == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*dirpath))
213 ++dirpath;
215 /* Figure out the maximum number of VPATH entries and put it in
216 MAXELEM. We start with 2, one before the first separator and one
217 nil (the list terminator) and increment our estimated number for
218 each separator or blank we find. */
219 maxelem = 2;
220 p = dirpath;
221 while (*p != '\0')
222 if (*p++ == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*p))
223 ++maxelem;
225 vpath = xmalloc (maxelem * sizeof (const char *));
226 maxvpath = 0;
228 elem = 0;
229 p = dirpath;
230 while (*p != '\0')
232 char *v;
233 unsigned int len;
235 /* Find the end of this entry. */
236 v = p;
237 while (*p != '\0' && *p != PATH_SEPARATOR_CHAR
238 && !isblank ((unsigned char)*p))
239 ++p;
241 len = p - v;
242 /* Make sure there's no trailing slash,
243 but still allow "/" as a directory. */
244 #if defined(__MSDOS__) || defined(__EMX__)
245 /* We need also to leave alone a trailing slash in "d:/". */
246 if (len > 3 || (len > 1 && v[1] != ':'))
247 #endif
248 if (len > 1 && p[-1] == '/')
249 --len;
251 /* Put the directory on the vpath list. */
252 if (len > 1 || *v != '.')
254 vpath[elem++] = dir_name (strcache_add_len (v, len));
255 if (len > maxvpath)
256 maxvpath = len;
259 /* Skip over separators and blanks between entries. */
260 while (*p == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*p))
261 ++p;
264 if (elem > 0)
266 struct vpath *path;
267 /* ELEM is now incremented one element past the last
268 entry, to where the nil-pointer terminator goes.
269 Usually this is maxelem - 1. If not, shrink down. */
270 if (elem < (maxelem - 1))
271 vpath = xrealloc (vpath, (elem+1) * sizeof (const char *));
273 /* Put the nil-pointer terminator on the end of the VPATH list. */
274 vpath[elem] = NULL;
276 /* Construct the vpath structure and put it into the linked list. */
277 path = xmalloc (sizeof (struct vpath));
278 path->searchpath = vpath;
279 path->maxlen = maxvpath;
280 path->next = vpaths;
281 vpaths = path;
283 /* Set up the members. */
284 path->pattern = strcache_add (pattern);
285 path->patlen = strlen (pattern);
286 path->percent = percent ? path->pattern + (percent - pattern) : 0;
288 else
289 /* There were no entries, so free whatever space we allocated. */
290 free (vpath);
293 /* Search the GPATH list for a pathname string that matches the one passed
294 in. If it is found, return 1. Otherwise we return 0. */
297 gpath_search (const char *file, unsigned int len)
299 const char **gp;
301 if (gpaths && (len <= gpaths->maxlen))
302 for (gp = gpaths->searchpath; *gp != NULL; ++gp)
303 if (strneq (*gp, file, len) && (*gp)[len] == '\0')
304 return 1;
306 return 0;
310 /* Search the given VPATH list for a directory where the name pointed to by
311 FILE exists. If it is found, we return a cached name of the existing file
312 and set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
313 stat call was done). Otherwise we return NULL. */
315 static const char *
316 selective_vpath_search (struct vpath *path, const char *file,
317 FILE_TIMESTAMP *mtime_ptr)
319 int not_target;
320 char *name;
321 const char *n;
322 const char *filename;
323 const char **vpath = path->searchpath;
324 unsigned int maxvpath = path->maxlen;
325 unsigned int i;
326 unsigned int flen, vlen, name_dplen;
327 int exists = 0;
329 /* Find out if *FILE is a target.
330 If and only if it is NOT a target, we will accept prospective
331 files that don't exist but are mentioned in a makefile. */
333 struct file *f = lookup_file (file);
334 not_target = f == 0 || !f->is_target;
337 flen = strlen (file);
339 /* Split *FILE into a directory prefix and a name-within-directory.
340 NAME_DPLEN gets the length of the prefix; FILENAME gets the pointer to
341 the name-within-directory and FLEN is its length. */
343 n = strrchr (file, '/');
344 #ifdef HAVE_DOS_PATHS
345 /* We need the rightmost slash or backslash. */
347 const char *bslash = strrchr(file, '\\');
348 if (!n || bslash > n)
349 n = bslash;
351 #endif
352 name_dplen = n != 0 ? n - file : 0;
353 filename = name_dplen > 0 ? n + 1 : file;
354 if (name_dplen > 0)
355 flen -= name_dplen + 1;
357 /* Get enough space for the biggest VPATH entry, a slash, the directory
358 prefix that came with FILE, another slash (although this one may not
359 always be necessary), the filename, and a null terminator. */
360 name = alloca (maxvpath + 1 + name_dplen + 1 + flen + 1);
362 /* Try each VPATH entry. */
363 for (i = 0; vpath[i] != 0; ++i)
365 int exists_in_cache = 0;
366 char *p;
368 p = name;
370 /* Put the next VPATH entry into NAME at P and increment P past it. */
371 vlen = strlen (vpath[i]);
372 memcpy (p, vpath[i], vlen);
373 p += vlen;
375 /* Add the directory prefix already in *FILE. */
376 if (name_dplen > 0)
378 #ifndef VMS
379 *p++ = '/';
380 #endif
381 memcpy (p, file, name_dplen);
382 p += name_dplen;
385 #ifdef HAVE_DOS_PATHS
386 /* Cause the next if to treat backslash and slash alike. */
387 if (p != name && p[-1] == '\\' )
388 p[-1] = '/';
389 #endif
390 /* Now add the name-within-directory at the end of NAME. */
391 #ifndef VMS
392 if (p != name && p[-1] != '/')
394 *p = '/';
395 memcpy (p + 1, filename, flen + 1);
397 else
398 #endif
399 memcpy (p, filename, flen + 1);
401 /* Check if the file is mentioned in a makefile. If *FILE is not
402 a target, that is enough for us to decide this file exists.
403 If *FILE is a target, then the file must be mentioned in the
404 makefile also as a target to be chosen.
406 The restriction that *FILE must not be a target for a
407 makefile-mentioned file to be chosen was added by an
408 inadequately commented change in July 1990; I am not sure off
409 hand what problem it fixes.
411 In December 1993 I loosened this restriction to allow a file
412 to be chosen if it is mentioned as a target in a makefile. This
413 seem logical.
415 Special handling for -W / -o: make sure we preserve the special
416 values here. Actually this whole thing is a little bogus: I think
417 we should ditch the name/hname thing and look into the renamed
418 capability that already exists for files: that is, have a new struct
419 file* entry for the VPATH-found file, and set the renamed field if
420 we use it.
423 struct file *f = lookup_file (name);
424 if (f != 0)
426 exists = not_target || f->is_target;
427 if (exists && mtime_ptr
428 && (f->last_mtime == OLD_MTIME || f->last_mtime == NEW_MTIME))
430 *mtime_ptr = f->last_mtime;
431 mtime_ptr = 0;
436 if (!exists)
438 /* That file wasn't mentioned in the makefile.
439 See if it actually exists. */
441 #ifdef VMS
442 exists_in_cache = exists = dir_file_exists_p (vpath[i], filename);
443 #else
444 /* Clobber a null into the name at the last slash.
445 Now NAME is the name of the directory to look in. */
446 *p = '\0';
448 /* We know the directory is in the hash table now because either
449 construct_vpath_list or the code just above put it there.
450 Does the file we seek exist in it? */
451 exists_in_cache = exists = dir_file_exists_p (name, filename);
452 #endif
455 if (exists)
457 /* The file is in the directory cache.
458 Now check that it actually exists in the filesystem.
459 The cache may be out of date. When vpath thinks a file
460 exists, but stat fails for it, confusion results in the
461 higher levels. */
463 struct stat st;
465 #ifndef VMS
466 /* Put the slash back in NAME. */
467 *p = '/';
468 #endif
470 if (exists_in_cache) /* Makefile-mentioned file need not exist. */
472 int e;
474 EINTRLOOP (e, stat (name, &st)); /* Does it really exist? */
475 if (e != 0)
477 exists = 0;
478 continue;
481 /* Store the modtime into *MTIME_PTR for the caller. */
482 if (mtime_ptr != 0)
484 *mtime_ptr = FILE_TIMESTAMP_STAT_MODTIME (name, st);
485 mtime_ptr = 0;
489 /* We have found a file.
490 If we get here and mtime_ptr hasn't been set, record
491 UNKNOWN_MTIME to indicate this. */
492 if (mtime_ptr != 0)
493 *mtime_ptr = UNKNOWN_MTIME;
495 /* Store the name we found and return it. */
497 return strcache_add_len (name, (p + 1 - name) + flen);
501 return 0;
505 /* Search the VPATH list whose pattern matches FILE for a directory where FILE
506 exists. If it is found, return the cached name of an existing file, and
507 set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
508 stat call was done). Otherwise we return 0. */
510 const char *
511 vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr)
513 struct vpath *v;
515 /* If there are no VPATH entries or FILENAME starts at the root,
516 there is nothing we can do. */
518 if (file[0] == '/'
519 #ifdef HAVE_DOS_PATHS
520 || file[0] == '\\' || file[1] == ':'
521 #endif
522 || (vpaths == 0 && general_vpath == 0))
523 return 0;
525 for (v = vpaths; v != 0; v = v->next)
526 if (pattern_matches (v->pattern, v->percent, file))
528 const char *p = selective_vpath_search (v, file, mtime_ptr);
529 if (p)
530 return p;
533 if (general_vpath != 0)
535 const char *p = selective_vpath_search (general_vpath, file, mtime_ptr);
536 if (p)
537 return p;
540 return 0;
543 /* Print the data base of VPATH search paths. */
545 void
546 print_vpath_data_base (void)
548 unsigned int nvpaths;
549 struct vpath *v;
551 puts (_("\n# VPATH Search Paths\n"));
553 nvpaths = 0;
554 for (v = vpaths; v != 0; v = v->next)
556 register unsigned int i;
558 ++nvpaths;
560 printf ("vpath %s ", v->pattern);
562 for (i = 0; v->searchpath[i] != 0; ++i)
563 printf ("%s%c", v->searchpath[i],
564 v->searchpath[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
567 if (vpaths == 0)
568 puts (_("# No `vpath' search paths."));
569 else
570 printf (_("\n# %u `vpath' search paths.\n"), nvpaths);
572 if (general_vpath == 0)
573 puts (_("\n# No general (`VPATH' variable) search path."));
574 else
576 const char **path = general_vpath->searchpath;
577 unsigned int i;
579 fputs (_("\n# General (`VPATH' variable) search path:\n# "), stdout);
581 for (i = 0; path[i] != 0; ++i)
582 printf ("%s%c", path[i],
583 path[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);