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, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
22 #include "pathstuff.h"
26 /* Structure used to represent a selective VPATH searchpath. */
30 struct vpath
*next
; /* Pointer to next struct in the linked list. */
31 char *pattern
; /* The pattern to match. */
32 char *percent
; /* Pointer into `pattern' where the `%' is. */
33 unsigned int patlen
;/* Length of the pattern. */
34 char **searchpath
; /* Null-terminated list of directories. */
35 unsigned int maxlen
;/* Maximum length of any entry in the list. */
38 /* Linked-list of all selective VPATHs. */
40 static struct vpath
*vpaths
;
42 /* Structure for the general VPATH given in the variable. */
44 static struct vpath
*general_vpath
;
46 /* Structure for GPATH given in the variable. */
48 static struct vpath
*gpaths
;
50 static int selective_vpath_search
PARAMS ((struct vpath
*path
, char **file
, FILE_TIMESTAMP
*mtime_ptr
));
52 /* Reverse the chain of selective VPATH lists so they
53 will be searched in the order given in the makefiles
54 and construct the list from the VPATH variable. */
59 register struct vpath
*new = 0;
60 register struct vpath
*old
, *nexto
;
63 /* Reverse the chain. */
64 for (old
= vpaths
; old
!= 0; old
= nexto
)
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
;
89 /* Save the list of vpaths. */
90 struct vpath
*save_vpaths
= vpaths
;
92 /* Empty `vpaths' so the new one will have no next, and `vpaths'
93 will still be nil if P contains no existing directories. */
97 construct_vpath_list ("%", p
);
99 /* Store the created path as the general path,
100 and restore the old list of vpaths. */
101 general_vpath
= vpaths
;
102 vpaths
= save_vpaths
;
105 /* If there is a GPATH variable with a nonnull value, construct the
106 GPATH list from it. We use variable_expand rather than just
107 calling lookup_variable so that it will be recursively expanded. */
110 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
111 int save
= warn_undefined_variables_flag
;
112 warn_undefined_variables_flag
= 0;
114 p
= variable_expand ("$(strip $(GPATH))");
116 warn_undefined_variables_flag
= save
;
121 /* Save the list of vpaths. */
122 struct vpath
*save_vpaths
= vpaths
;
124 /* Empty `vpaths' so the new one will have no next, and `vpaths'
125 will still be nil if P contains no existing directories. */
129 construct_vpath_list ("%", p
);
131 /* Store the created path as the GPATH,
132 and restore the old list of vpaths. */
134 vpaths
= save_vpaths
;
138 /* Construct the VPATH listing for the pattern and searchpath given.
140 This function is called to generate selective VPATH lists and also for
141 the general VPATH list (which is in fact just a selective VPATH that
142 is applied to everything). The returned pointer is either put in the
143 linked list of all selective VPATH lists or in the GENERAL_VPATH
146 If SEARCHPATH is nil, remove all previous listings with the same
147 pattern. If PATTERN is nil, remove all VPATH listings. Existing
148 and readable directories that are not "." given in the searchpath
149 separated by the path element separator (defined in make.h) are
150 loaded into the directory hash table if they are not there already
151 and put in the VPATH searchpath for the given pattern with trailing
152 slashes stripped off if present (and if the directory is not the
153 root, "/"). The length of the longest entry in the list is put in
154 the structure as well. The new entry will be at the head of the
158 construct_vpath_list (char *pattern
, char *dirpath
)
160 register unsigned int elem
;
162 register char **vpath
;
163 register unsigned int maxvpath
;
164 unsigned int maxelem
;
165 char *percent
= NULL
;
169 pattern
= xstrdup (pattern
);
170 percent
= find_percent (pattern
);
175 /* Remove matching listings. */
176 register struct vpath
*path
, *lastpath
;
182 struct vpath
*next
= path
->next
;
185 || (((percent
== 0 && path
->percent
== 0)
186 || (percent
- pattern
== path
->percent
- path
->pattern
))
187 && streq (pattern
, path
->pattern
)))
189 /* Remove it from the linked list. */
193 lastpath
->next
= next
;
195 /* Free its unused storage. */
196 free (path
->pattern
);
197 free ((char *) path
->searchpath
);
198 free ((char *) path
);
212 convert_vpath_to_windows32(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. */
222 if (*p
++ == PATH_SEPARATOR_CHAR
|| isblank ((unsigned char)*p
))
225 vpath
= (char **) xmalloc (maxelem
* sizeof (char *));
228 /* Skip over any initial separators and blanks. */
230 while (*p
== PATH_SEPARATOR_CHAR
|| isblank ((unsigned char)*p
))
239 /* Find the end of this entry. */
241 while (*p
!= '\0' && *p
!= PATH_SEPARATOR_CHAR
242 && !isblank ((unsigned char)*p
))
246 /* Make sure there's no trailing slash,
247 but still allow "/" as a directory. */
248 #if defined(__MSDOS__) || defined(__EMX__)
249 /* We need also to leave alone a trailing slash in "d:/". */
250 if (len
> 3 || (len
> 1 && v
[1] != ':'))
252 if (len
> 1 && p
[-1] == '/')
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
);
270 /* The directory does not exist. Omit from the list. */
274 /* Skip over separators and blanks between entries. */
275 while (*p
== PATH_SEPARATOR_CHAR
|| isblank ((unsigned char)*p
))
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. */
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
;
299 /* Set up the members. */
300 path
->pattern
= pattern
;
301 path
->percent
= percent
;
302 path
->patlen
= strlen (pattern
);
306 /* There were no entries, so free whatever space we allocated. */
307 free ((char *) vpath
);
313 /* Search the GPATH list for a pathname string that matches the one passed
314 in. If it is found, return 1. Otherwise we return 0. */
317 gpath_search (char *file
, unsigned int len
)
321 if (gpaths
&& (len
<= gpaths
->maxlen
))
322 for (gp
= gpaths
->searchpath
; *gp
!= NULL
; ++gp
)
323 if (strneq (*gp
, file
, len
) && (*gp
)[len
] == '\0')
329 /* Search the VPATH list whose pattern matches *FILE for a directory
330 where the name pointed to by FILE exists. If it is found, we set *FILE to
331 the newly malloc'd name of the existing file, *MTIME_PTR (if MTIME_PTR is
332 not NULL) to its modtime (or zero if no stat call was done), and return 1.
333 Otherwise we return 0. */
336 vpath_search (char **file
, FILE_TIMESTAMP
*mtime_ptr
)
338 register struct vpath
*v
;
340 /* If there are no VPATH entries or FILENAME starts at the root,
341 there is nothing we can do. */
344 #ifdef HAVE_DOS_PATHS
348 || (vpaths
== 0 && general_vpath
== 0))
351 for (v
= vpaths
; v
!= 0; v
= v
->next
)
352 if (pattern_matches (v
->pattern
, v
->percent
, *file
))
353 if (selective_vpath_search (v
, file
, mtime_ptr
))
356 if (general_vpath
!= 0
357 && selective_vpath_search (general_vpath
, file
, mtime_ptr
))
364 /* Search the given VPATH list for a directory where the name pointed
365 to by FILE exists. If it is found, we set *FILE to the newly malloc'd
366 name of the existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to
367 its modtime (or zero if no stat call was done), and we return 1.
368 Otherwise we return 0. */
371 selective_vpath_search (struct vpath
*path
, char **file
,
372 FILE_TIMESTAMP
*mtime_ptr
)
377 register char **vpath
= path
->searchpath
;
378 unsigned int maxvpath
= path
->maxlen
;
379 register unsigned int i
;
380 unsigned int flen
, vlen
, name_dplen
;
383 /* Find out if *FILE is a target.
384 If and only if it is NOT a target, we will accept prospective
385 files that don't exist but are mentioned in a makefile. */
387 struct file
*f
= lookup_file (*file
);
388 not_target
= f
== 0 || !f
->is_target
;
391 flen
= strlen (*file
);
393 /* Split *FILE into a directory prefix and a name-within-directory.
394 NAME_DPLEN gets the length of the prefix; FILENAME gets the
395 pointer to the name-within-directory and FLEN is its length. */
397 n
= strrchr (*file
, '/');
398 #ifdef HAVE_DOS_PATHS
399 /* We need the rightmost slash or backslash. */
401 char *bslash
= strrchr(*file
, '\\');
402 if (!n
|| bslash
> n
)
406 name_dplen
= n
!= 0 ? n
- *file
: 0;
407 filename
= name_dplen
> 0 ? n
+ 1 : *file
;
409 flen
-= name_dplen
+ 1;
411 /* Allocate enough space for the biggest VPATH entry,
412 a slash, the directory prefix that came with *FILE,
413 another slash (although this one may not always be
414 necessary), the filename, and a null terminator. */
415 name
= (char *) xmalloc (maxvpath
+ 1 + name_dplen
+ 1 + flen
+ 1);
417 /* Try each VPATH entry. */
418 for (i
= 0; vpath
[i
] != 0; ++i
)
420 int exists_in_cache
= 0;
424 /* Put the next VPATH entry into NAME at N and increment N past it. */
425 vlen
= strlen (vpath
[i
]);
426 bcopy (vpath
[i
], n
, vlen
);
429 /* Add the directory prefix already in *FILE. */
435 bcopy (*file
, n
, name_dplen
);
439 #ifdef HAVE_DOS_PATHS
440 /* Cause the next if to treat backslash and slash alike. */
441 if (n
!= name
&& n
[-1] == '\\' )
444 /* Now add the name-within-directory at the end of NAME. */
446 if (n
!= name
&& n
[-1] != '/')
449 bcopy (filename
, n
+ 1, flen
+ 1);
453 bcopy (filename
, n
, flen
+ 1);
455 /* Check if the file is mentioned in a makefile. If *FILE is not
456 a target, that is enough for us to decide this file exists.
457 If *FILE is a target, then the file must be mentioned in the
458 makefile also as a target to be chosen.
460 The restriction that *FILE must not be a target for a
461 makefile-mentioned file to be chosen was added by an
462 inadequately commented change in July 1990; I am not sure off
463 hand what problem it fixes.
465 In December 1993 I loosened this restriction to allow a file
466 to be chosen if it is mentioned as a target in a makefile. This
469 Special handling for -W / -o: make sure we preserve the special
470 values here. Actually this whole thing is a little bogus: I think
471 we should ditch the name/hname thing and look into the renamed
472 capability that already exists for files: that is, have a new struct
473 file* entry for the VPATH-found file, and set the renamed field if
477 struct file
*f
= lookup_file (name
);
480 exists
= not_target
|| f
->is_target
;
481 if (exists
&& mtime_ptr
482 && (f
->last_mtime
== OLD_MTIME
|| f
->last_mtime
== NEW_MTIME
))
484 *mtime_ptr
= f
->last_mtime
;
492 /* That file wasn't mentioned in the makefile.
493 See if it actually exists. */
496 exists_in_cache
= exists
= dir_file_exists_p (vpath
[i
], filename
);
498 /* Clobber a null into the name at the last slash.
499 Now NAME is the name of the directory to look in. */
502 /* We know the directory is in the hash table now because either
503 construct_vpath_list or the code just above put it there.
504 Does the file we seek exist in it? */
505 exists_in_cache
= exists
= dir_file_exists_p (name
, filename
);
511 /* The file is in the directory cache.
512 Now check that it actually exists in the filesystem.
513 The cache may be out of date. When vpath thinks a file
514 exists, but stat fails for it, confusion results in the
520 /* Put the slash back in NAME. */
524 if (exists_in_cache
) /* Makefile-mentioned file need not exist. */
528 EINTRLOOP (e
, stat (name
, &st
)); /* Does it really exist? */
535 /* Store the modtime into *MTIME_PTR for the caller. */
538 *mtime_ptr
= FILE_TIMESTAMP_STAT_MODTIME (name
, st
);
543 /* We have found a file.
544 Store the name we found into *FILE for the caller. */
546 *file
= savestring (name
, (n
+ 1 - name
) + flen
);
548 /* If we get here and mtime_ptr hasn't been set, record
549 UNKNOWN_MTIME to indicate this. */
551 *mtime_ptr
= UNKNOWN_MTIME
;
562 /* Print the data base of VPATH search paths. */
565 print_vpath_data_base (void)
567 register unsigned int nvpaths
;
568 register struct vpath
*v
;
570 puts (_("\n# VPATH Search Paths\n"));
573 for (v
= vpaths
; v
!= 0; v
= v
->next
)
575 register unsigned int i
;
579 printf ("vpath %s ", v
->pattern
);
581 for (i
= 0; v
->searchpath
[i
] != 0; ++i
)
582 printf ("%s%c", v
->searchpath
[i
],
583 v
->searchpath
[i
+ 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR
);
587 puts (_("# No `vpath' search paths."));
589 printf (_("\n# %u `vpath' search paths.\n"), nvpaths
);
591 if (general_vpath
== 0)
592 puts (_("\n# No general (`VPATH' variable) search path."));
595 register char **path
= general_vpath
->searchpath
;
596 register unsigned int i
;
598 fputs (_("\n# General (`VPATH' variable) search path:\n# "), stdout
);
600 for (i
= 0; path
[i
] != 0; ++i
)
601 printf ("%s%c", path
[i
],
602 path
[i
+ 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR
);