Formerly file.c.~24~
[make.git] / vpath.c
blobdb53168dbeaa1cc8d103732711e8af02899093fc
1 /* Copyright (C) 1988, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
2 This file is part of GNU Make.
4 GNU Make is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 GNU Make is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU Make; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include "make.h"
19 #include "file.h"
20 #include "variable.h"
23 /* Structure used to represent a selective VPATH searchpath. */
25 struct vpath
27 struct vpath *next; /* Pointer to next struct in the linked list. */
28 char *pattern; /* The pattern to match. */
29 char *percent; /* Pointer into `pattern' where the `%' is. */
30 unsigned int patlen;/* Length of the pattern. */
31 char **searchpath; /* Null-terminated list of directories. */
32 unsigned int maxlen;/* Maximum length of any entry in the list. */
35 /* Linked-list of all selective VPATHs. */
37 static struct vpath *vpaths;
39 /* Structure for the general VPATH given in the variable. */
41 static struct vpath *general_vpath;
43 static int selective_vpath_search ();
45 /* Reverse the chain of selective VPATH lists so they
46 will be searched in the order given in the makefiles
47 and construct the list from the VPATH variable. */
49 void
50 build_vpath_lists ()
52 register struct vpath *new = 0;
53 register struct vpath *old, *nexto;
54 register char *p;
56 /* Reverse the chain. */
57 for (old = vpaths; old != 0; old = nexto)
59 nexto = old->next;
60 old->next = new;
61 new = old;
64 vpaths = new;
66 /* If there is a VPATH variable with a nonnull value, construct the
67 general VPATH list from it. We use variable_expand rather than just
68 calling lookup_variable so that it will be recursively expanded. */
69 p = variable_expand ("$(VPATH)");
70 if (*p != '\0')
72 construct_vpath_list ("%", p);
73 /* VPATHS will be nil if there have been no previous `vpath'
74 directives and none of the given directories exists. */
75 if (vpaths == 0)
76 general_vpath = 0;
77 else
79 general_vpath = vpaths;
80 /* It was just put into the linked list,
81 but we don't want it there, so we must remove it. */
82 vpaths = general_vpath->next;
87 /* Construct the VPATH listing for the pattern and searchpath given.
89 This function is called to generate selective VPATH lists and also for
90 the general VPATH list (which is in fact just a selective VPATH that
91 is applied to everything). The returned pointer is either put in the
92 linked list of all selective VPATH lists or in the GENERAL_VPATH
93 variable.
95 If SEARCHPATH is nil, remove all previous listings with the same
96 pattern. If PATTERN is nil, remove all VPATH listings.
97 Existing and readable directories that are not "." given in the
98 searchpath separated by colons are loaded into the directory hash
99 table if they are not there already and put in the VPATH searchpath
100 for the given pattern with trailing slashes stripped off if present
101 (and if the directory is not the root, "/").
102 The length of the longest entry in the list is put in the structure as well.
103 The new entry will be at the head of the VPATHS chain. */
105 void
106 construct_vpath_list (pattern, dirpath)
107 char *pattern, *dirpath;
109 register unsigned int elem;
110 register char *p;
111 register char **vpath;
112 register unsigned int maxvpath;
113 unsigned int maxelem;
114 char *percent;
116 if (pattern != 0)
118 pattern = savestring (pattern, strlen (pattern));
119 percent = find_percent (pattern);
122 if (dirpath == 0)
124 /* Remove matching listings. */
125 register struct vpath *path, *lastpath;
127 lastpath = 0;
128 path = vpaths;
129 while (path != 0)
131 struct vpath *next = path->next;
133 if (pattern == 0
134 || (((percent == 0 && path->percent == 0)
135 || (percent - pattern == path->percent - path->pattern))
136 && streq (pattern, path->pattern)))
138 /* Remove it from the linked list. */
139 if (lastpath == 0)
140 vpaths = path->next;
141 else
142 lastpath->next = next;
144 /* Free its unused storage. */
145 free (path->pattern);
146 free ((char *) path->searchpath);
147 free ((char *) path);
150 lastpath = path;
151 path = next;
154 if (pattern != 0)
155 free (pattern);
156 return;
159 /* Figure out the maximum number of VPATH entries and
160 put it in MAXELEM. We start with 2, one before the
161 first colon and one nil, the list terminator and
162 increment our estimated number for each colon or blank we find. */
163 maxelem = 2;
164 p = dirpath;
165 while (*p != '\0')
166 if (*p++ == ':' || isblank (*p))
167 ++maxelem;
169 vpath = (char **) xmalloc (maxelem * sizeof (char *));
170 maxvpath = 0;
172 /* Skip over any initial colons and blanks. */
173 p = dirpath;
174 while (*p == ':' || isblank (*p))
175 ++p;
177 elem = 0;
178 while (*p != '\0')
180 char *v;
181 unsigned int len;
183 /* Find the end of this entry. */
184 v = p;
185 while (*p != '\0' && *p != ':' && !isblank (*p))
186 ++p;
188 len = p - v;
189 /* Make sure there's no trailing slash,
190 but still allow "/" as a directory. */
191 if (len > 1 && p[-1] == '/')
192 --len;
194 if (len > 1 || *v != '.')
196 v = savestring (v, len);
198 /* Verify that the directory actually exists. */
200 if (dir_file_exists_p (v, ""))
202 /* It does. Put it in the list. */
203 vpath[elem++] = dir_name (v);
204 free (v);
205 if (len > maxvpath)
206 maxvpath = len;
208 else
209 /* The directory does not exist. Omit from the list. */
210 free (v);
213 /* Skip over colons and blanks between entries. */
214 while (*p == ':' || isblank (*p))
215 ++p;
218 if (elem > 0)
220 struct vpath *path;
221 /* ELEM is now incremented one element past the last
222 entry, to where the nil-pointer terminator goes.
223 Usually this is maxelem - 1. If not, shrink down. */
224 if (elem < (maxelem - 1))
225 vpath = (char **) xrealloc ((char *) vpath,
226 (elem + 1) * sizeof (char *));
228 /* Put the nil-pointer terminator on the end of the VPATH list. */
229 vpath[elem] = 0;
231 /* Construct the vpath structure and put it into the linked list. */
232 path = (struct vpath *) xmalloc (sizeof (struct vpath));
233 path->searchpath = vpath;
234 path->maxlen = maxvpath;
235 path->next = vpaths;
236 vpaths = path;
238 /* Set up the members. */
239 path->pattern = pattern;
240 path->percent = percent;
241 path->patlen = strlen (pattern);
243 else
244 /* There were no entries, so free whatever space we allocated. */
245 free ((char *) vpath);
248 /* Search the VPATH list whose pattern matches *FILE for a directory
249 where the name pointed to by FILE exists. If it is found, we set *FILE to
250 the newly malloc'd name of the existing file, *MTIME_PTR (if MTIME_PTR is
251 not NULL) to its modtime (or zero if no stat call was done), and return 1.
252 Otherwise we return 0. */
255 vpath_search (file, mtime_ptr)
256 char **file;
257 time_t *mtime_ptr;
259 register struct vpath *v;
261 /* If there are no VPATH entries or FILENAME starts at the root,
262 there is nothing we can do. */
264 if (**file == '/' || (vpaths == 0 && general_vpath == 0))
265 return 0;
267 for (v = vpaths; v != 0; v = v->next)
268 if (pattern_matches (v->pattern, v->percent, *file))
269 if (selective_vpath_search (v, file, mtime_ptr))
270 return 1;
272 if (general_vpath != 0
273 && selective_vpath_search (general_vpath, file, mtime_ptr))
274 return 1;
276 return 0;
280 /* Search the given VPATH list for a directory where the name pointed
281 to by FILE exists. If it is found, we set *FILE to the newly malloc'd
282 name of the existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to
283 its modtime (or zero if no stat call was done), and we return 1.
284 Otherwise we return 0. */
286 static int
287 selective_vpath_search (path, file, mtime_ptr)
288 struct vpath *path;
289 char **file;
290 time_t *mtime_ptr;
292 int not_target;
293 char *name, *n;
294 char *filename;
295 register char **vpath = path->searchpath;
296 unsigned int maxvpath = path->maxlen;
297 register unsigned int i;
298 unsigned int flen, vlen, name_dplen;
299 int exists = 0;
301 /* Find out if *FILE is a target.
302 If and only if it is NOT a target, we will accept prospective
303 files that don't exist but are mentioned in a makefile. */
305 struct file *f = lookup_file (*file);
306 not_target = f == 0 || !f->is_target;
309 flen = strlen (*file);
311 /* Split *FILE into a directory prefix and a name-within-directory.
312 NAME_DPLEN gets the length of the prefix; FILENAME gets the
313 pointer to the name-within-directory and FLEN is its length. */
315 n = rindex (*file, '/');
316 name_dplen = n != 0 ? n - *file : 0;
317 filename = name_dplen > 0 ? n + 1 : *file;
318 if (name_dplen > 0)
319 flen -= name_dplen + 1;
321 /* Allocate enough space for the biggest VPATH entry,
322 a slash, the directory prefix that came with *FILE,
323 another slash (although this one may not always be
324 necessary), the filename, and a null terminator. */
325 name = (char *) alloca (maxvpath + 1 + name_dplen + 1 + flen + 1);
327 /* Try each VPATH entry. */
328 for (i = 0; vpath[i] != 0; ++i)
330 int exists_in_cache = 0;
332 n = name;
334 /* Put the next VPATH entry into NAME at N and increment N past it. */
335 vlen = strlen (vpath[i]);
336 bcopy (vpath[i], n, vlen);
337 n += vlen;
339 /* Add the directory prefix already in *FILE. */
340 if (name_dplen > 0)
342 *n++ = '/';
343 bcopy (*file, n, name_dplen);
344 n += name_dplen;
347 /* Now add the name-within-directory at the end of NAME. */
348 if (n != name && n[-1] != '/')
350 *n = '/';
351 bcopy (filename, n + 1, flen + 1);
353 else
354 bcopy (filename, n, flen + 1);
356 if (not_target)
357 /* Since *FILE is not a target, if the file is
358 mentioned in a makefile, we consider it existent. */
359 exists = lookup_file (name) != 0;
361 if (!exists)
363 /* That file wasn't mentioned in the makefile.
364 See if it actually exists. */
366 /* Clobber a null into the name at the last slash.
367 Now NAME is the name of the directory to look in. */
368 *n = '\0';
370 /* We know the directory is in the hash table now because either
371 construct_vpath_list or the code just above put it there.
372 Does the file we seek exist in it? */
373 exists_in_cache = exists = dir_file_exists_p (name, filename);
376 if (exists)
378 /* The file is in the directory cache.
379 Now check that it actually exists in the filesystem.
380 The cache may be out of date. When vpath thinks a file
381 exists, but stat fails for it, confusion results in the
382 higher levels. */
384 struct stat st;
386 /* Put the slash back in NAME. */
387 *n = '/';
389 if (!exists_in_cache /* Makefile-mentioned file need not exist. */
390 || stat (name, &st) == 0) /* Does it really exist? */
392 /* We have found a file.
393 Store the name we found into *FILE for the caller. */
395 *file = savestring (name, (n + 1 - name) + flen);
397 if (mtime_ptr != 0)
398 /* Store the modtime into *MTIME_PTR for the caller.
399 If we have had no need to stat the file here,
400 we record a zero modtime to indicate this. */
401 *mtime_ptr = exists_in_cache ? st.st_mtime : (time_t) 0;
403 return 1;
408 return 0;
411 /* Print the data base of VPATH search paths. */
413 void
414 print_vpath_data_base ()
416 register unsigned int nvpaths;
417 register struct vpath *v;
419 puts ("\n# VPATH Search Paths\n");
421 nvpaths = 0;
422 for (v = vpaths; v != 0; v = v->next)
424 register unsigned int i;
426 ++nvpaths;
428 printf ("vpath %s ", v->pattern);
430 for (i = 0; v->searchpath[i] != 0; ++i)
431 printf ("%s%c", v->searchpath[i],
432 v->searchpath[i + 1] == 0 ? '\n' : ':');
435 if (vpaths == 0)
436 puts ("# No `vpath' search paths.");
437 else
438 printf ("\n# %u `vpath' search paths.\n", nvpaths);
440 if (general_vpath == 0)
441 puts ("\n# No general (`VPATH' variable) search path.");
442 else
444 register char **path = general_vpath->searchpath;
445 register unsigned int i;
447 fputs ("\n# General (`VPATH' variable) search path:\n# ", stdout);
449 for (i = 0; path[i] != 0; ++i)
450 printf ("%s%c", path[i], path[i + 1] == 0 ? '\n' : ':');