1 /* Set up combined include path chain for the preprocessor.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "coretypes.h"
28 #include "c-incpath.h"
29 #include "cppdefault.h"
31 /* Windows does not natively support inodes, and neither does MSDOS.
32 Cygwin's emulation can generate non-unique inodes, so don't use it.
33 VMS has non-numeric inodes. */
35 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
36 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
38 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
39 # define INO_T_EQ(A, B) 0
41 # define INO_T_EQ(A, B) ((A) == (B))
43 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
46 static void add_env_var_paths PARAMS ((const char *, int));
47 static void add_standard_paths PARAMS ((const char *, const char *, int));
48 static void free_path PARAMS ((struct cpp_path *, int));
49 static void merge_include_chains PARAMS ((cpp_reader *, int));
50 static int remove_component_p PARAMS ((const char *));
51 static struct cpp_path *
52 remove_duplicates PARAMS ((cpp_reader *, struct cpp_path *,
53 struct cpp_path *, struct cpp_path *, int));
55 /* Include chains heads and tails. */
56 static struct cpp_path *heads[4];
57 static struct cpp_path *tails[4];
58 static bool quote_ignores_source_dir;
59 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
61 /* Free an element of the include chain, possibly giving a reason. */
63 free_path (path, reason)
64 struct cpp_path *path;
71 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
72 if (reason == REASON_DUP_SYS)
74 _(" as it is a non-system directory that duplicates a system directory\n"));
78 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
87 free ((PTR) path->name);
91 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
92 append all the names to the search path CHAIN. */
94 add_env_var_paths (env_var, chain)
100 GET_ENVIRONMENT (q, env_var);
105 for (p = q; *q; p = q + 1)
108 while (*q != 0 && *q != PATH_SEPARATOR)
112 path = xstrdup (".");
115 path = xmalloc (q - p + 1);
116 memcpy (path, p, q - p);
120 add_path (path, chain, chain == SYSTEM);
124 /* Append the standard include chain defined in cppdefault.c. */
126 add_standard_paths (sysroot, iprefix, cxx_stdinc)
127 const char *sysroot, *iprefix;
130 const struct default_include *p;
134 len = cpp_GCC_INCLUDE_DIR_len;
136 for (p = cpp_include_defaults; p->fname; p++)
138 if (!p->cplusplus || cxx_stdinc)
142 /* Should this directory start with the sysroot? */
143 if (sysroot && p->add_sysroot)
144 str = concat (sysroot, p->fname, NULL);
145 /* Does this directory start with the prefix? If so, search
146 "translated" versions of GNU directories. These have
147 /usr/local/lib/gcc... replaced by iprefix. */
148 else if (len && !strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
149 str = concat (iprefix, p->fname + len, NULL);
151 str = update_path (p->fname, p->component);
153 add_path (str, SYSTEM, p->cxx_aware);
158 /* For each duplicate path in chain HEAD, keep just the first one.
159 Remove each path in chain HEAD that also exists in chain SYSTEM.
160 Set the NEXT pointer of the last path in the resulting chain to
161 JOIN, unless it duplicates JOIN in which case the last path is
162 removed. Return the head of the resulting chain. Any of HEAD,
163 JOIN and SYSTEM can be NULL. */
164 static struct cpp_path *
165 remove_duplicates (pfile, head, system, join, verbose)
167 struct cpp_path *head;
168 struct cpp_path *system;
169 struct cpp_path *join;
172 struct cpp_path **pcur, *tmp, *cur;
175 for (pcur = &head; *pcur; )
177 int reason = REASON_QUIET;
180 simplify_path (cur->name);
182 if (stat (cur->name, &st))
184 /* Dirs that don't exist are silently ignored, unless verbose. */
186 cpp_errno (pfile, DL_ERROR, cur->name);
188 reason = REASON_NOENT;
190 else if (!S_ISDIR (st.st_mode))
191 cpp_error_with_line (pfile, DL_ERROR, 0, 0,
192 "%s: not a directory", cur->name);
195 INO_T_COPY (cur->ino, st.st_ino);
196 cur->dev = st.st_dev;
198 /* Remove this one if it is in the system chain. */
199 reason = REASON_DUP_SYS;
200 for (tmp = system; tmp; tmp = tmp->next)
201 if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev)
206 /* Dupicate of something earlier in the same chain? */
208 for (tmp = head; tmp != cur; tmp = tmp->next)
209 if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev)
213 /* Last in the chain and duplicate of JOIN? */
214 && !(cur->next == NULL && join
215 && INO_T_EQ (cur->ino, join->ino)
216 && cur->dev == join->dev))
218 /* Unique, so keep this directory. */
225 /* Remove this entry from the chain. */
227 free_path (cur, verbose ? reason: REASON_QUIET);
234 /* Merge the four include chains together in the order quote, bracket,
235 system, after. Remove duplicate dirs (as determined by
238 We can't just merge the lists and then uniquify them because then
239 we may lose directories from the <> search path that should be
240 there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however safe
241 to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
244 merge_include_chains (pfile, verbose)
248 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
249 resulting SYSTEM chain. */
251 tails[SYSTEM]->next = heads[AFTER];
253 heads[SYSTEM] = heads[AFTER];
254 heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
256 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
257 join it to SYSTEM. */
258 heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
259 heads[SYSTEM], verbose);
261 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
262 join it to BRACKET. */
263 heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
264 heads[BRACKET], verbose);
266 /* If verbose, print the list of dirs to search. */
271 fprintf (stderr, _("#include \"...\" search starts here:\n"));
272 for (p = heads[QUOTE];; p = p->next)
274 if (p == heads[BRACKET])
275 fprintf (stderr, _("#include <...> search starts here:\n"));
278 fprintf (stderr, " %s\n", p->name);
280 fprintf (stderr, _("End of search list.\n"));
284 /* Use given -I paths for #include "..." but not #include <...>, and
285 don't search the directory of the present file for #include "...".
286 (Note that -I. -I- is not the same as the default setup; -I. uses
287 the compiler's working dir.) */
291 heads[QUOTE] = heads[BRACKET];
292 tails[QUOTE] = tails[BRACKET];
293 heads[BRACKET] = NULL;
294 tails[BRACKET] = NULL;
295 /* This is NOT redundant. */
296 quote_ignores_source_dir = true;
299 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
302 add_path (path, chain, cxx_aware)
309 p = (struct cpp_path *) xmalloc (sizeof (struct cpp_path));
312 if (chain == SYSTEM || chain == AFTER)
313 p->sysp = 1 + (cxx_aware != 0);
318 tails[chain]->next = p;
324 /* Exported function to handle include chain merging, duplicate
325 removal, and registration with cpplib. */
327 register_include_chains (pfile, sysroot, iprefix,
328 stdinc, cxx_stdinc, verbose)
330 const char *sysroot, *iprefix;
331 int stdinc, cxx_stdinc, verbose;
333 static const char *const lang_env_vars[] =
334 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
335 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
336 cpp_options *cpp_opts = cpp_get_options (pfile);
337 size_t idx = (cpp_opts->objc ? 2: 0);
339 if (cpp_opts->cplusplus)
344 /* CPATH and language-dependent environment variables may add to the
346 add_env_var_paths ("CPATH", BRACKET);
347 add_env_var_paths (lang_env_vars[idx], SYSTEM);
349 /* Finally chain on the standard directories. */
351 add_standard_paths (sysroot, iprefix, cxx_stdinc);
353 merge_include_chains (pfile, verbose);
355 cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
356 quote_ignores_source_dir);
359 /* Returns true if it is safe to remove the final component of path,
360 when it is followed by a ".." component. We use lstat to avoid
361 symlinks if we have it. If not, we can still catch errors with
364 remove_component_p (path)
371 result = lstat (path, &s);
373 result = stat (path, &s);
376 /* There's no guarantee that errno will be unchanged, even on
377 success. Cygwin's lstat(), for example, will often set errno to
378 ENOSYS. In case of success, reset errno to zero. */
382 return result == 0 && S_ISDIR (s.st_mode);
385 /* Simplify a path name in place, deleting redundant components. This
386 reduces OS overhead and guarantees that equivalent paths compare
387 the same (modulo symlinks).
390 foo/bar/../quux foo/quux
394 //quux //quux (POSIX allows leading // as a namespace escape)
396 Guarantees no trailing slashes. All transforms reduce the length
397 of the string. Returns PATH. errno is 0 if no error occurred;
398 nonzero if an error occurred when using stat () or lstat (). */
401 char *path ATTRIBUTE_UNUSED;
405 char *base, *orig_base;
409 /* Don't overflow the empty path by putting a '.' in it below. */
413 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
414 /* Convert all backslashes to slashes. */
415 for (from = path; *from; from++)
416 if (*from == '\\') *from = '/';
418 /* Skip over leading drive letter if present. */
419 if (ISALPHA (path[0]) && path[1] == ':')
420 from = to = &path[2];
427 /* Remove redundant leading /s. */
436 /* 3 or more initial /s are equivalent to 1 /. */
437 while (*++from == '/');
439 /* On some hosts // differs from /; Posix allows this. */
444 base = orig_base = to;
464 else if (from[1] == '.' && (from[2] == '/' || from[2] == '\0'))
466 /* Don't simplify if there was no previous component. */
467 if (absolute && orig_base == to)
472 /* Don't simplify if the previous component was "../",
473 or if an error has already occurred with (l)stat. */
474 if (base != to && errno == 0)
476 /* We don't back up if it's a symlink. */
478 if (remove_component_p (path))
480 while (to > base && *to != '/')
490 /* Add the component separator. */
494 /* Copy this component until the trailing null or '/'. */
495 while (*from != '\0' && *from != '/')
502 /* Change the empty string to "." so that it is not treated as stdin.