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, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include "c-incpath.h"
27 #include "cppdefault.h"
29 /* Windows does not natively support inodes, and neither does MSDOS.
30 Cygwin's emulation can generate non-unique inodes, so don't use it.
31 VMS has non-numeric inodes. */
33 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
34 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
36 # if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__
37 # define INO_T_EQ(A, B) 0
39 # define INO_T_EQ(A, B) ((A) == (B))
41 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
44 static void add_env_var_paths (const char *, int);
45 static void add_standard_paths (const char *, const char *, int);
46 static void free_path (struct cpp_dir *, int);
47 static void merge_include_chains (cpp_reader *, int);
48 static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
50 struct cpp_dir *, int);
52 /* Include chains heads and tails. */
53 static struct cpp_dir *heads[4];
54 static struct cpp_dir *tails[4];
55 static bool quote_ignores_source_dir;
56 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
58 /* Free an element of the include chain, possibly giving a reason. */
60 free_path (struct cpp_dir *path, int reason)
66 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
67 if (reason == REASON_DUP_SYS)
69 _(" as it is a non-system directory that duplicates a system directory\n"));
73 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
86 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
87 append all the names to the search path CHAIN. */
89 add_env_var_paths (const char *env_var, int chain)
93 GET_ENVIRONMENT (q, env_var);
98 for (p = q; *q; p = q + 1)
101 while (*q != 0 && *q != PATH_SEPARATOR)
105 path = xstrdup (".");
108 path = xmalloc (q - p + 1);
109 memcpy (path, p, q - p);
113 add_path (path, chain, chain == SYSTEM, false);
117 /* Append the standard include chain defined in cppdefault.c. */
119 add_standard_paths (const char *sysroot, const char *iprefix, int cxx_stdinc)
121 const struct default_include *p;
124 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
126 /* Look for directories that start with the standard prefix.
127 "Translate" them, i.e. replace /usr/local/lib/gcc... with
128 IPREFIX and search them first. */
129 for (p = cpp_include_defaults; p->fname; p++)
131 if (!p->cplusplus || cxx_stdinc)
133 /* Should we be translating sysrooted dirs too? Assume
134 that iprefix and sysroot are mutually exclusive, for
136 if (sysroot && p->add_sysroot)
138 if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
140 char *str = concat (iprefix, p->fname + len, NULL);
141 add_path (str, SYSTEM, p->cxx_aware, false);
147 for (p = cpp_include_defaults; p->fname; p++)
149 if (!p->cplusplus || cxx_stdinc)
153 /* Should this directory start with the sysroot? */
154 if (sysroot && p->add_sysroot)
155 str = concat (sysroot, p->fname, NULL);
157 str = update_path (p->fname, p->component);
159 add_path (str, SYSTEM, p->cxx_aware, false);
164 /* For each duplicate path in chain HEAD, keep just the first one.
165 Remove each path in chain HEAD that also exists in chain SYSTEM.
166 Set the NEXT pointer of the last path in the resulting chain to
167 JOIN, unless it duplicates JOIN in which case the last path is
168 removed. Return the head of the resulting chain. Any of HEAD,
169 JOIN and SYSTEM can be NULL. */
171 static struct cpp_dir *
172 remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
173 struct cpp_dir *system, struct cpp_dir *join,
176 struct cpp_dir **pcur, *tmp, *cur;
179 for (pcur = &head; *pcur; )
181 int reason = REASON_QUIET;
185 if (stat (cur->name, &st))
187 /* Dirs that don't exist are silently ignored, unless verbose. */
189 cpp_errno (pfile, CPP_DL_ERROR, cur->name);
192 /* If -Wmissing-include-dirs is given, warn. */
193 cpp_options *opts = cpp_get_options (pfile);
194 if (opts->warn_missing_include_dirs && cur->user_supplied_p)
195 cpp_errno (pfile, CPP_DL_WARNING, cur->name);
196 reason = REASON_NOENT;
199 else if (!S_ISDIR (st.st_mode))
200 cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
201 "%s: not a directory", cur->name);
204 INO_T_COPY (cur->ino, st.st_ino);
205 cur->dev = st.st_dev;
207 /* Remove this one if it is in the system chain. */
208 reason = REASON_DUP_SYS;
209 for (tmp = system; tmp; tmp = tmp->next)
210 if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev
211 && cur->construct == tmp->construct)
216 /* Duplicate of something earlier in the same chain? */
218 for (tmp = head; tmp != cur; tmp = tmp->next)
219 if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev
220 && cur->construct == tmp->construct)
224 /* Last in the chain and duplicate of JOIN? */
225 && !(cur->next == NULL && join
226 && INO_T_EQ (cur->ino, join->ino)
227 && cur->dev == join->dev
228 && cur->construct == join->construct))
230 /* Unique, so keep this directory. */
237 /* Remove this entry from the chain. */
239 free_path (cur, verbose ? reason: REASON_QUIET);
246 /* Merge the four include chains together in the order quote, bracket,
247 system, after. Remove duplicate dirs (as determined by
250 We can't just merge the lists and then uniquify them because then
251 we may lose directories from the <> search path that should be
252 there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is
253 however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
254 written -iquote bar -Ifoo -Iquux. */
257 merge_include_chains (cpp_reader *pfile, int verbose)
259 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
260 resulting SYSTEM chain. */
262 tails[SYSTEM]->next = heads[AFTER];
264 heads[SYSTEM] = heads[AFTER];
265 heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
267 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
268 join it to SYSTEM. */
269 heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
270 heads[SYSTEM], verbose);
272 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
273 join it to BRACKET. */
274 heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
275 heads[BRACKET], verbose);
277 /* If verbose, print the list of dirs to search. */
282 fprintf (stderr, _("#include \"...\" search starts here:\n"));
283 for (p = heads[QUOTE];; p = p->next)
285 if (p == heads[BRACKET])
286 fprintf (stderr, _("#include <...> search starts here:\n"));
289 fprintf (stderr, " %s\n", p->name);
291 fprintf (stderr, _("End of search list.\n"));
295 /* Use given -I paths for #include "..." but not #include <...>, and
296 don't search the directory of the present file for #include "...".
297 (Note that -I. -I- is not the same as the default setup; -I. uses
298 the compiler's working dir.) */
300 split_quote_chain (void)
302 heads[QUOTE] = heads[BRACKET];
303 tails[QUOTE] = tails[BRACKET];
304 heads[BRACKET] = NULL;
305 tails[BRACKET] = NULL;
306 /* This is NOT redundant. */
307 quote_ignores_source_dir = true;
310 /* Add P to the chain specified by CHAIN. */
313 add_cpp_dir_path (cpp_dir *p, int chain)
316 tails[chain]->next = p;
322 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
325 add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)
329 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
330 /* Convert all backslashes to slashes. The native CRT stat()
331 function does not recognize a directory that ends in a backslash
332 (unless it is a drive root dir, such "c:\"). Forward slashes,
333 trailing or otherwise, cause no problems for stat(). */
335 for (c = path; *c; c++)
336 if (*c == '\\') *c = '/';
339 p = xmalloc (sizeof (cpp_dir));
342 if (chain == SYSTEM || chain == AFTER)
343 p->sysp = 1 + !cxx_aware;
347 p->user_supplied_p = user_supplied_p;
349 add_cpp_dir_path (p, chain);
352 /* Exported function to handle include chain merging, duplicate
353 removal, and registration with cpplib. */
355 register_include_chains (cpp_reader *pfile, const char *sysroot,
356 const char *iprefix, int stdinc, int cxx_stdinc,
359 static const char *const lang_env_vars[] =
360 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
361 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
362 cpp_options *cpp_opts = cpp_get_options (pfile);
363 size_t idx = (cpp_opts->objc ? 2: 0);
365 if (cpp_opts->cplusplus)
370 /* CPATH and language-dependent environment variables may add to the
372 add_env_var_paths ("CPATH", BRACKET);
373 add_env_var_paths (lang_env_vars[idx], SYSTEM);
375 target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc);
377 /* Finally chain on the standard directories. */
379 add_standard_paths (sysroot, iprefix, cxx_stdinc);
381 target_c_incpath.extra_includes (sysroot, iprefix, stdinc);
383 merge_include_chains (pfile, verbose);
385 cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
386 quote_ignores_source_dir);
388 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
389 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED,
390 const char *iprefix ATTRIBUTE_UNUSED,
391 int stdinc ATTRIBUTE_UNUSED)
396 #ifndef TARGET_EXTRA_INCLUDES
397 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
399 #ifndef TARGET_EXTRA_PRE_INCLUDES
400 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
403 struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES };