* New config.sub and config.guess
[make.git] / vpath.c
blob3401327513421945a353af512a288464f61989e1
1 /* Implementation of pattern-matching file search paths for GNU Make.
2 Copyright (C) 1988,89,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "filedef.h"
22 #include "variable.h"
23 #ifdef WINDOWS32
24 #include "pathstuff.h"
25 #endif
28 /* Structure used to represent a selective VPATH searchpath. */
30 struct vpath
32 struct vpath *next; /* Pointer to next struct in the linked list. */
33 char *pattern; /* The pattern to match. */
34 char *percent; /* Pointer into `pattern' where the `%' is. */
35 unsigned int patlen;/* Length of the pattern. */
36 char **searchpath; /* Null-terminated list of directories. */
37 unsigned int maxlen;/* Maximum length of any entry in the list. */
40 /* Linked-list of all selective VPATHs. */
42 static struct vpath *vpaths;
44 /* Structure for the general VPATH given in the variable. */
46 static struct vpath *general_vpath;
48 /* Structure for GPATH given in the variable. */
50 static struct vpath *gpaths;
52 static int selective_vpath_search PARAMS ((struct vpath *path, char **file, FILE_TIMESTAMP *mtime_ptr));
54 /* Reverse the chain of selective VPATH lists so they
55 will be searched in the order given in the makefiles
56 and construct the list from the VPATH variable. */
58 void
59 build_vpath_lists ()
61 register struct vpath *new = 0;
62 register struct vpath *old, *nexto;
63 register char *p;
65 /* Reverse the chain. */
66 for (old = vpaths; old != 0; old = nexto)
68 nexto = old->next;
69 old->next = new;
70 new = old;
73 vpaths = new;
75 /* If there is a VPATH variable with a nonnull value, construct the
76 general VPATH list from it. We use variable_expand rather than just
77 calling lookup_variable so that it will be recursively expanded. */
80 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
81 int save = warn_undefined_variables_flag;
82 warn_undefined_variables_flag = 0;
84 p = variable_expand ("$(strip $(VPATH))");
86 warn_undefined_variables_flag = save;
89 if (*p != '\0')
91 /* Save the list of vpaths. */
92 struct vpath *save_vpaths = vpaths;
94 /* Empty `vpaths' so the new one will have no next, and `vpaths'
95 will still be nil if P contains no existing directories. */
96 vpaths = 0;
98 /* Parse P. */
99 construct_vpath_list ("%", p);
101 /* Store the created path as the general path,
102 and restore the old list of vpaths. */
103 general_vpath = vpaths;
104 vpaths = save_vpaths;
107 /* If there is a GPATH variable with a nonnull value, construct the
108 GPATH list from it. We use variable_expand rather than just
109 calling lookup_variable so that it will be recursively expanded. */
112 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
113 int save = warn_undefined_variables_flag;
114 warn_undefined_variables_flag = 0;
116 p = variable_expand ("$(strip $(GPATH))");
118 warn_undefined_variables_flag = save;
121 if (*p != '\0')
123 /* Save the list of vpaths. */
124 struct vpath *save_vpaths = vpaths;
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 ("%", 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 (pattern, dirpath)
161 char *pattern, *dirpath;
163 register unsigned int elem;
164 register char *p;
165 register char **vpath;
166 register unsigned int maxvpath;
167 unsigned int maxelem;
168 char *percent = NULL;
170 if (pattern != 0)
172 pattern = xstrdup (pattern);
173 percent = find_percent (pattern);
176 if (dirpath == 0)
178 /* Remove matching listings. */
179 register struct vpath *path, *lastpath;
181 lastpath = 0;
182 path = vpaths;
183 while (path != 0)
185 struct vpath *next = path->next;
187 if (pattern == 0
188 || (((percent == 0 && path->percent == 0)
189 || (percent - pattern == path->percent - path->pattern))
190 && streq (pattern, path->pattern)))
192 /* Remove it from the linked list. */
193 if (lastpath == 0)
194 vpaths = path->next;
195 else
196 lastpath->next = next;
198 /* Free its unused storage. */
199 free (path->pattern);
200 free ((char *) path->searchpath);
201 free ((char *) path);
203 else
204 lastpath = path;
206 path = next;
209 if (pattern != 0)
210 free (pattern);
211 return;
214 #ifdef WINDOWS32
215 convert_vpath_to_windows32(dirpath, ';');
216 #endif
218 /* Figure out the maximum number of VPATH entries and put it in
219 MAXELEM. We start with 2, one before the first separator and one
220 nil (the list terminator) and increment our estimated number for
221 each separator or blank we find. */
222 maxelem = 2;
223 p = dirpath;
224 while (*p != '\0')
225 if (*p++ == PATH_SEPARATOR_CHAR || isblank (*p))
226 ++maxelem;
228 vpath = (char **) xmalloc (maxelem * sizeof (char *));
229 maxvpath = 0;
231 /* Skip over any initial separators and blanks. */
232 p = dirpath;
233 while (*p == PATH_SEPARATOR_CHAR || isblank (*p))
234 ++p;
236 elem = 0;
237 while (*p != '\0')
239 char *v;
240 unsigned int len;
242 /* Find the end of this entry. */
243 v = p;
244 while (*p != '\0' && *p != PATH_SEPARATOR_CHAR && !isblank (*p))
245 ++p;
247 len = p - v;
248 /* Make sure there's no trailing slash,
249 but still allow "/" as a directory. */
250 #ifdef __MSDOS__
251 /* We need also to leave alone a trailing slash in "d:/". */
252 if (len > 3 || (len > 1 && v[1] != ':'))
253 #endif
254 if (len > 1 && p[-1] == '/')
255 --len;
257 if (len > 1 || *v != '.')
259 v = savestring (v, len);
261 /* Verify that the directory actually exists. */
263 if (dir_file_exists_p (v, ""))
265 /* It does. Put it in the list. */
266 vpath[elem++] = dir_name (v);
267 free (v);
268 if (len > maxvpath)
269 maxvpath = len;
271 else
272 /* The directory does not exist. Omit from the list. */
273 free (v);
276 /* Skip over separators and blanks between entries. */
277 while (*p == PATH_SEPARATOR_CHAR || isblank (*p))
278 ++p;
281 if (elem > 0)
283 struct vpath *path;
284 /* ELEM is now incremented one element past the last
285 entry, to where the nil-pointer terminator goes.
286 Usually this is maxelem - 1. If not, shrink down. */
287 if (elem < (maxelem - 1))
288 vpath = (char **) xrealloc ((char *) vpath,
289 (elem + 1) * sizeof (char *));
291 /* Put the nil-pointer terminator on the end of the VPATH list. */
292 vpath[elem] = 0;
294 /* Construct the vpath structure and put it into the linked list. */
295 path = (struct vpath *) xmalloc (sizeof (struct vpath));
296 path->searchpath = vpath;
297 path->maxlen = maxvpath;
298 path->next = vpaths;
299 vpaths = path;
301 /* Set up the members. */
302 path->pattern = pattern;
303 path->percent = percent;
304 path->patlen = strlen (pattern);
306 else
308 /* There were no entries, so free whatever space we allocated. */
309 free ((char *) vpath);
310 if (pattern != 0)
311 free (pattern);
315 /* Search the GPATH list for a pathname string that matches the one passed
316 in. If it is found, return 1. Otherwise we return 0. */
319 gpath_search (file, len)
320 char *file;
321 int len;
323 register char **gp;
325 if (gpaths && (len <= gpaths->maxlen))
326 for (gp = gpaths->searchpath; *gp != NULL; ++gp)
327 if (strneq (*gp, file, len) && (*gp)[len] == '\0')
328 return 1;
330 return 0;
333 /* Search the VPATH list whose pattern matches *FILE for a directory
334 where the name pointed to by FILE exists. If it is found, we set *FILE to
335 the newly malloc'd name of the existing file, *MTIME_PTR (if MTIME_PTR is
336 not NULL) to its modtime (or zero if no stat call was done), and return 1.
337 Otherwise we return 0. */
340 vpath_search (file, mtime_ptr)
341 char **file;
342 FILE_TIMESTAMP *mtime_ptr;
344 register struct vpath *v;
346 /* If there are no VPATH entries or FILENAME starts at the root,
347 there is nothing we can do. */
349 if (**file == '/'
350 #if defined (WINDOWS32) || defined (__MSDOS__)
351 || **file == '\\'
352 || (*file)[1] == ':'
353 #endif
354 || (vpaths == 0 && general_vpath == 0))
355 return 0;
357 for (v = vpaths; v != 0; v = v->next)
358 if (pattern_matches (v->pattern, v->percent, *file))
359 if (selective_vpath_search (v, file, mtime_ptr))
360 return 1;
362 if (general_vpath != 0
363 && selective_vpath_search (general_vpath, file, mtime_ptr))
364 return 1;
366 return 0;
370 /* Search the given VPATH list for a directory where the name pointed
371 to by FILE exists. If it is found, we set *FILE to the newly malloc'd
372 name of the existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to
373 its modtime (or zero if no stat call was done), and we return 1.
374 Otherwise we return 0. */
376 static int
377 selective_vpath_search (path, file, mtime_ptr)
378 struct vpath *path;
379 char **file;
380 FILE_TIMESTAMP *mtime_ptr;
382 int not_target;
383 char *name, *n;
384 char *filename;
385 register char **vpath = path->searchpath;
386 unsigned int maxvpath = path->maxlen;
387 register unsigned int i;
388 unsigned int flen, vlen, name_dplen;
389 int exists = 0;
391 /* Find out if *FILE is a target.
392 If and only if it is NOT a target, we will accept prospective
393 files that don't exist but are mentioned in a makefile. */
395 struct file *f = lookup_file (*file);
396 not_target = f == 0 || !f->is_target;
399 flen = strlen (*file);
401 /* Split *FILE into a directory prefix and a name-within-directory.
402 NAME_DPLEN gets the length of the prefix; FILENAME gets the
403 pointer to the name-within-directory and FLEN is its length. */
405 n = strrchr (*file, '/');
406 #if defined (WINDOWS32) || defined (__MSDOS__)
407 /* We need the rightmost slash or backslash. */
409 char *bslash = strrchr(*file, '\\');
410 if (!n || bslash > n)
411 n = bslash;
413 #endif
414 name_dplen = n != 0 ? n - *file : 0;
415 filename = name_dplen > 0 ? n + 1 : *file;
416 if (name_dplen > 0)
417 flen -= name_dplen + 1;
419 /* Allocate enough space for the biggest VPATH entry,
420 a slash, the directory prefix that came with *FILE,
421 another slash (although this one may not always be
422 necessary), the filename, and a null terminator. */
423 name = (char *) xmalloc (maxvpath + 1 + name_dplen + 1 + flen + 1);
425 /* Try each VPATH entry. */
426 for (i = 0; vpath[i] != 0; ++i)
428 int exists_in_cache = 0;
430 n = name;
432 /* Put the next VPATH entry into NAME at N and increment N past it. */
433 vlen = strlen (vpath[i]);
434 bcopy (vpath[i], n, vlen);
435 n += vlen;
437 /* Add the directory prefix already in *FILE. */
438 if (name_dplen > 0)
440 #ifndef VMS
441 *n++ = '/';
442 #endif
443 bcopy (*file, n, name_dplen);
444 n += name_dplen;
447 #if defined (WINDOWS32) || defined (__MSDOS__)
448 /* Cause the next if to treat backslash and slash alike. */
449 if (n != name && n[-1] == '\\' )
450 n[-1] = '/';
451 #endif
452 /* Now add the name-within-directory at the end of NAME. */
453 #ifndef VMS
454 if (n != name && n[-1] != '/')
456 *n = '/';
457 bcopy (filename, n + 1, flen + 1);
459 else
460 #endif
461 bcopy (filename, n, flen + 1);
463 /* Check if the file is mentioned in a makefile. If *FILE is not
464 a target, that is enough for us to decide this file exists.
465 If *FILE is a target, then the file must be mentioned in the
466 makefile also as a target to be chosen.
468 The restriction that *FILE must not be a target for a
469 makefile-mentioned file to be chosen was added by an
470 inadequately commented change in July 1990; I am not sure off
471 hand what problem it fixes.
473 In December 1993 I loosened this restriction to allow a file
474 to be chosen if it is mentioned as a target in a makefile. This
475 seem logical. */
477 struct file *f = lookup_file (name);
478 if (f != 0)
479 exists = not_target || f->is_target;
482 if (!exists)
484 /* That file wasn't mentioned in the makefile.
485 See if it actually exists. */
487 #ifdef VMS
488 exists_in_cache = exists = dir_file_exists_p (vpath[i], filename);
489 #else
490 /* Clobber a null into the name at the last slash.
491 Now NAME is the name of the directory to look in. */
492 *n = '\0';
494 /* We know the directory is in the hash table now because either
495 construct_vpath_list or the code just above put it there.
496 Does the file we seek exist in it? */
497 exists_in_cache = exists = dir_file_exists_p (name, filename);
498 #endif
501 if (exists)
503 /* The file is in the directory cache.
504 Now check that it actually exists in the filesystem.
505 The cache may be out of date. When vpath thinks a file
506 exists, but stat fails for it, confusion results in the
507 higher levels. */
509 struct stat st;
511 #ifndef VMS
512 /* Put the slash back in NAME. */
513 *n = '/';
514 #endif
516 if (!exists_in_cache /* Makefile-mentioned file need not exist. */
517 || stat (name, &st) == 0) /* Does it really exist? */
519 /* We have found a file.
520 Store the name we found into *FILE for the caller. */
522 *file = savestring (name, (n + 1 - name) + flen);
524 if (mtime_ptr != 0)
525 /* Store the modtime into *MTIME_PTR for the caller.
526 If we have had no need to stat the file here,
527 we record a zero modtime to indicate this. */
528 *mtime_ptr = (exists_in_cache
529 ? FILE_TIMESTAMP_STAT_MODTIME (st)
530 : (FILE_TIMESTAMP) 0);
532 free (name);
533 return 1;
535 else
536 exists = 0;
540 free (name);
541 return 0;
544 /* Print the data base of VPATH search paths. */
546 void
547 print_vpath_data_base ()
549 register unsigned int nvpaths;
550 register struct vpath *v;
552 puts (_("\n# VPATH Search Paths\n"));
554 nvpaths = 0;
555 for (v = vpaths; v != 0; v = v->next)
557 register unsigned int i;
559 ++nvpaths;
561 printf ("vpath %s ", v->pattern);
563 for (i = 0; v->searchpath[i] != 0; ++i)
564 printf ("%s%c", v->searchpath[i],
565 v->searchpath[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
568 if (vpaths == 0)
569 puts (_("# No `vpath' search paths."));
570 else
571 printf (_("\n# %u `vpath' search paths.\n"), nvpaths);
573 if (general_vpath == 0)
574 puts (_("\n# No general (`VPATH' variable) search path."));
575 else
577 register char **path = general_vpath->searchpath;
578 register unsigned int i;
580 fputs (_("\n# General (`VPATH' variable) search path:\n# "), stdout);
582 for (i = 0; path[i] != 0; ++i)
583 printf ("%s%c", path[i],
584 path[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);