(\startcontents): Leave ^ catcode as other.
[make.git] / vpath.c
blob3e2a01192f6a825d0ec271e3ac3fb08fa5ab8014
1 /* Implementation of pattern-matching file search paths for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95, 96 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, 675 Mass Ave, Cambridge, MA 02139, 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 char *pattern; /* The pattern to match. */
33 char *percent; /* Pointer into `pattern' where the `%' is. */
34 unsigned int patlen;/* Length of the pattern. */
35 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;
51 static int selective_vpath_search PARAMS ((struct vpath *path, char **file, time_t *mtime_ptr));
53 /* Reverse the chain of selective VPATH lists so they
54 will be searched in the order given in the makefiles
55 and construct the list from the VPATH variable. */
57 void
58 build_vpath_lists ()
60 register struct vpath *new = 0;
61 register struct vpath *old, *nexto;
62 register char *p;
64 /* Reverse the chain. */
65 for (old = vpaths; old != 0; old = nexto)
67 nexto = old->next;
68 old->next = new;
69 new = old;
72 vpaths = new;
74 /* If there is a VPATH variable with a nonnull value, construct the
75 general VPATH list from it. We use variable_expand rather than just
76 calling lookup_variable so that it will be recursively expanded. */
79 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
80 int save = warn_undefined_variables_flag;
81 warn_undefined_variables_flag = 0;
83 p = variable_expand ("$(strip $(VPATH))");
85 warn_undefined_variables_flag = save;
88 if (*p != '\0')
90 /* Save the list of vpaths. */
91 struct vpath *save_vpaths = vpaths;
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 ("%", 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;
125 /* Empty `vpaths' so the new one will have no next, and `vpaths'
126 will still be nil if P contains no existing directories. */
127 vpaths = 0;
129 /* Parse P. */
130 construct_vpath_list ("%", p);
132 /* Store the created path as the GPATH,
133 and restore the old list of vpaths. */
134 gpaths = vpaths;
135 vpaths = save_vpaths;
139 /* Construct the VPATH listing for the pattern and searchpath given.
141 This function is called to generate selective VPATH lists and also for
142 the general VPATH list (which is in fact just a selective VPATH that
143 is applied to everything). The returned pointer is either put in the
144 linked list of all selective VPATH lists or in the GENERAL_VPATH
145 variable.
147 If SEARCHPATH is nil, remove all previous listings with the same
148 pattern. If PATTERN is nil, remove all VPATH listings.
149 Existing and readable directories that are not "." given in the
150 searchpath separated by colons are loaded into the directory hash
151 table if they are not there already and put in the VPATH searchpath
152 for the given pattern with trailing slashes stripped off if present
153 (and if the directory is not the root, "/").
154 The length of the longest entry in the list is put in the structure as well.
155 The new entry will be at the head of the VPATHS chain. */
157 void
158 construct_vpath_list (pattern, dirpath)
159 char *pattern, *dirpath;
161 register unsigned int elem;
162 register char *p;
163 register char **vpath;
164 register unsigned int maxvpath;
165 unsigned int maxelem;
166 char *percent;
168 if (pattern != 0)
170 pattern = savestring (pattern, strlen (pattern));
171 percent = find_percent (pattern);
174 if (dirpath == 0)
176 /* Remove matching listings. */
177 register struct vpath *path, *lastpath;
179 lastpath = 0;
180 path = vpaths;
181 while (path != 0)
183 struct vpath *next = path->next;
185 if (pattern == 0
186 || (((percent == 0 && path->percent == 0)
187 || (percent - pattern == path->percent - path->pattern))
188 && streq (pattern, path->pattern)))
190 /* Remove it from the linked list. */
191 if (lastpath == 0)
192 vpaths = path->next;
193 else
194 lastpath->next = next;
196 /* Free its unused storage. */
197 free (path->pattern);
198 free ((char *) path->searchpath);
199 free ((char *) path);
201 else
202 lastpath = path;
204 path = next;
207 if (pattern != 0)
208 free (pattern);
209 return;
212 #ifdef WINDOWS32
213 convert_vpath_to_windows32(dirpath, ';');
214 #endif
216 /* Figure out the maximum number of VPATH entries and
217 put it in MAXELEM. We start with 2, one before the
218 first colon and one nil, the list terminator and
219 increment our estimated number for each colon or blank we find. */
220 maxelem = 2;
221 p = dirpath;
222 while (*p != '\0')
223 if (*p++ == PATH_SEPARATOR_CHAR || isblank (*p))
224 ++maxelem;
226 vpath = (char **) xmalloc (maxelem * sizeof (char *));
227 maxvpath = 0;
229 /* Skip over any initial colons and blanks. */
230 p = dirpath;
231 while (*p == PATH_SEPARATOR_CHAR || isblank (*p))
232 ++p;
234 elem = 0;
235 while (*p != '\0')
237 char *v;
238 unsigned int len;
240 /* Find the end of this entry. */
241 v = p;
242 while (*p != '\0' && *p != PATH_SEPARATOR_CHAR && !isblank (*p))
243 ++p;
245 len = p - v;
246 /* Make sure there's no trailing slash,
247 but still allow "/" as a directory. */
248 #ifdef __MSDOS__
249 /* We need also to leave alone a trailing slash in "d:/". */
250 if (len > 3 || (len > 1 && v[1] != ':'))
251 #endif
252 if (len > 1 && p[-1] == '/')
253 --len;
255 if (len > 1 || *v != '.')
257 v = savestring (v, len);
259 /* Verify that the directory actually exists. */
261 if (dir_file_exists_p (v, ""))
263 /* It does. Put it in the list. */
264 vpath[elem++] = dir_name (v);
265 free (v);
266 if (len > maxvpath)
267 maxvpath = len;
269 else
270 /* The directory does not exist. Omit from the list. */
271 free (v);
274 /* Skip over colons and blanks between entries. */
275 while (*p == PATH_SEPARATOR_CHAR || isblank (*p))
276 ++p;
279 if (elem > 0)
281 struct vpath *path;
282 /* ELEM is now incremented one element past the last
283 entry, to where the nil-pointer terminator goes.
284 Usually this is maxelem - 1. If not, shrink down. */
285 if (elem < (maxelem - 1))
286 vpath = (char **) xrealloc ((char *) vpath,
287 (elem + 1) * sizeof (char *));
289 /* Put the nil-pointer terminator on the end of the VPATH list. */
290 vpath[elem] = 0;
292 /* Construct the vpath structure and put it into the linked list. */
293 path = (struct vpath *) xmalloc (sizeof (struct vpath));
294 path->searchpath = vpath;
295 path->maxlen = maxvpath;
296 path->next = vpaths;
297 vpaths = path;
299 /* Set up the members. */
300 path->pattern = pattern;
301 path->percent = percent;
302 path->patlen = strlen (pattern);
304 else
306 /* There were no entries, so free whatever space we allocated. */
307 free ((char *) vpath);
308 if (pattern != 0)
309 free (pattern);
313 /* Search the GPATH list for a directory where the name pointed to by FILE
314 exists. If it is found, we set *FILE to the newly malloc'd name of the
315 existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or
316 zero if no stat call was done), and return 1. Otherwise we return 0. */
319 gpath_search (file, mtime_ptr)
320 char **file;
321 time_t *mtime_ptr;
323 if (gpaths != 0
324 && selective_vpath_search (gpaths, file, mtime_ptr))
325 return 1;
327 return 0;
330 /* Search the VPATH list whose pattern matches *FILE for a directory
331 where the name pointed to by FILE exists. If it is found, we set *FILE to
332 the newly malloc'd name of the existing file, *MTIME_PTR (if MTIME_PTR is
333 not NULL) to its modtime (or zero if no stat call was done), and return 1.
334 Otherwise we return 0. */
337 vpath_search (file, mtime_ptr)
338 char **file;
339 time_t *mtime_ptr;
341 register struct vpath *v;
343 /* If there are no VPATH entries or FILENAME starts at the root,
344 there is nothing we can do. */
346 if (**file == '/'
347 #if defined (WINDOWS32) || defined (__MSDOS__)
348 || **file == '\\'
349 || (*file)[1] == ':'
350 #endif
351 || (vpaths == 0 && general_vpath == 0))
352 return 0;
354 for (v = vpaths; v != 0; v = v->next)
355 if (pattern_matches (v->pattern, v->percent, *file))
356 if (selective_vpath_search (v, file, mtime_ptr))
357 return 1;
359 if (general_vpath != 0
360 && selective_vpath_search (general_vpath, file, mtime_ptr))
361 return 1;
363 return 0;
367 /* Search the given VPATH list for a directory where the name pointed
368 to by FILE exists. If it is found, we set *FILE to the newly malloc'd
369 name of the existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to
370 its modtime (or zero if no stat call was done), and we return 1.
371 Otherwise we return 0. */
373 static int
374 selective_vpath_search (path, file, mtime_ptr)
375 struct vpath *path;
376 char **file;
377 time_t *mtime_ptr;
379 int not_target;
380 char *name, *n;
381 char *filename;
382 register char **vpath = path->searchpath;
383 unsigned int maxvpath = path->maxlen;
384 register unsigned int i;
385 unsigned int flen, vlen, name_dplen;
386 int exists = 0;
388 /* Find out if *FILE is a target.
389 If and only if it is NOT a target, we will accept prospective
390 files that don't exist but are mentioned in a makefile. */
392 struct file *f = lookup_file (*file);
393 not_target = f == 0 || !f->is_target;
396 flen = strlen (*file);
398 /* Split *FILE into a directory prefix and a name-within-directory.
399 NAME_DPLEN gets the length of the prefix; FILENAME gets the
400 pointer to the name-within-directory and FLEN is its length. */
402 n = rindex (*file, '/');
403 #if defined (WINDOWS32) || defined (__MSDOS__)
404 /* We need the rightmost slash or backslash. */
406 char *bslash = rindex(*file, '\\');
407 if (!n || bslash > n)
408 n = bslash;
410 #endif
411 name_dplen = n != 0 ? n - *file : 0;
412 filename = name_dplen > 0 ? n + 1 : *file;
413 if (name_dplen > 0)
414 flen -= name_dplen + 1;
416 /* Allocate enough space for the biggest VPATH entry,
417 a slash, the directory prefix that came with *FILE,
418 another slash (although this one may not always be
419 necessary), the filename, and a null terminator. */
420 name = (char *) xmalloc (maxvpath + 1 + name_dplen + 1 + flen + 1);
422 /* Try each VPATH entry. */
423 for (i = 0; vpath[i] != 0; ++i)
425 int exists_in_cache = 0;
427 n = name;
429 /* Put the next VPATH entry into NAME at N and increment N past it. */
430 vlen = strlen (vpath[i]);
431 bcopy (vpath[i], n, vlen);
432 n += vlen;
434 /* Add the directory prefix already in *FILE. */
435 if (name_dplen > 0)
437 *n++ = '/';
438 bcopy (*file, n, name_dplen);
439 n += name_dplen;
442 #if defined (WINDOWS32) || defined (__MSDOS__)
443 /* Cause the next if to treat backslash and slash alike. */
444 if (n != name && n[-1] == '\\' )
445 n[-1] = '/';
446 #endif
447 /* Now add the name-within-directory at the end of NAME. */
448 if (n != name && n[-1] != '/')
450 *n = '/';
451 bcopy (filename, n + 1, flen + 1);
453 else
454 bcopy (filename, n, flen + 1);
456 /* Check if the file is mentioned in a makefile. If *FILE is not
457 a target, that is enough for us to decide this file exists.
458 If *FILE is a target, then the file must be mentioned in the
459 makefile also as a target to be chosen.
461 The restriction that *FILE must not be a target for a
462 makefile-mentioned file to be chosen was added by an
463 inadequately commented change in July 1990; I am not sure off
464 hand what problem it fixes.
466 In December 1993 I loosened of this restriction to allow a file
467 to be chosen if it is mentioned as a target in a makefile. This
468 seem logical. */
470 struct file *f = lookup_file (name);
471 if (f != 0)
472 exists = not_target || f->is_target;
475 if (!exists)
477 /* That file wasn't mentioned in the makefile.
478 See if it actually exists. */
480 /* Clobber a null into the name at the last slash.
481 Now NAME is the name of the directory to look in. */
482 *n = '\0';
484 /* We know the directory is in the hash table now because either
485 construct_vpath_list or the code just above put it there.
486 Does the file we seek exist in it? */
487 exists_in_cache = exists = dir_file_exists_p (name, filename);
490 if (exists)
492 /* The file is in the directory cache.
493 Now check that it actually exists in the filesystem.
494 The cache may be out of date. When vpath thinks a file
495 exists, but stat fails for it, confusion results in the
496 higher levels. */
498 struct stat st;
500 /* Put the slash back in NAME. */
501 *n = '/';
503 if (!exists_in_cache /* Makefile-mentioned file need not exist. */
504 || stat (name, &st) == 0) /* Does it really exist? */
506 /* We have found a file.
507 Store the name we found into *FILE for the caller. */
509 *file = savestring (name, (n + 1 - name) + flen);
511 if (mtime_ptr != 0)
512 /* Store the modtime into *MTIME_PTR for the caller.
513 If we have had no need to stat the file here,
514 we record a zero modtime to indicate this. */
515 *mtime_ptr = exists_in_cache ? st.st_mtime : (time_t) 0;
517 free (name);
518 return 1;
520 else
521 exists = 0;
525 free (name);
526 return 0;
529 /* Print the data base of VPATH search paths. */
531 void
532 print_vpath_data_base ()
534 register unsigned int nvpaths;
535 register struct vpath *v;
537 puts ("\n# VPATH Search Paths\n");
539 nvpaths = 0;
540 for (v = vpaths; v != 0; v = v->next)
542 register unsigned int i;
544 ++nvpaths;
546 printf ("vpath %s ", v->pattern);
548 for (i = 0; v->searchpath[i] != 0; ++i)
549 printf ("%s%c", v->searchpath[i],
550 v->searchpath[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);
553 if (vpaths == 0)
554 puts ("# No `vpath' search paths.");
555 else
556 printf ("\n# %u `vpath' search paths.\n", nvpaths);
558 if (general_vpath == 0)
559 puts ("\n# No general (`VPATH' variable) search path.");
560 else
562 register char **path = general_vpath->searchpath;
563 register unsigned int i;
565 fputs ("\n# General (`VPATH' variable) search path:\n# ", stdout);
567 for (i = 0; path[i] != 0; ++i)
568 printf ("%s%c", path[i],
569 path[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR);