VirtualBox

source: kBuild/vendor/gnumake/2007-05-23/vpath.c

Last change on this file was 900, checked in by bird, 17 years ago

Load /home/bird/src/Gnu/make/2007-05-23 into vendor/gnumake/current.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette