1e4b17023SJohn Marino /* Set up combined include path chain for the preprocessor.
2e4b17023SJohn Marino Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3e4b17023SJohn Marino 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2012
4e4b17023SJohn Marino Free Software Foundation, Inc.
5e4b17023SJohn Marino
6e4b17023SJohn Marino Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
7e4b17023SJohn Marino
8e4b17023SJohn Marino This program is free software; you can redistribute it and/or modify it
9e4b17023SJohn Marino under the terms of the GNU General Public License as published by the
10e4b17023SJohn Marino Free Software Foundation; either version 3, or (at your option) any
11e4b17023SJohn Marino later version.
12e4b17023SJohn Marino
13e4b17023SJohn Marino This program is distributed in the hope that it will be useful,
14e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
15e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16e4b17023SJohn Marino GNU General Public License for more details.
17e4b17023SJohn Marino
18e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19e4b17023SJohn Marino along with this program; see the file COPYING3. If not see
20e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
21e4b17023SJohn Marino
22e4b17023SJohn Marino #include "config.h"
23e4b17023SJohn Marino #include "system.h"
24e4b17023SJohn Marino #include "coretypes.h"
25e4b17023SJohn Marino #include "machmode.h"
26e4b17023SJohn Marino #include "target.h"
27e4b17023SJohn Marino #include "tm.h"
28e4b17023SJohn Marino #include "cpplib.h"
29e4b17023SJohn Marino #include "prefix.h"
30e4b17023SJohn Marino #include "intl.h"
31e4b17023SJohn Marino #include "incpath.h"
32e4b17023SJohn Marino #include "cppdefault.h"
33e4b17023SJohn Marino
34e4b17023SJohn Marino /* Microsoft Windows does not natively support inodes.
35e4b17023SJohn Marino VMS has non-numeric inodes. */
36e4b17023SJohn Marino #ifdef VMS
37e4b17023SJohn Marino # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
38e4b17023SJohn Marino # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
39e4b17023SJohn Marino #elif !defined (HOST_LACKS_INODE_NUMBERS)
40e4b17023SJohn Marino # define INO_T_EQ(A, B) ((A) == (B))
41e4b17023SJohn Marino # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
42e4b17023SJohn Marino #endif
43e4b17023SJohn Marino
44e4b17023SJohn Marino #if defined INO_T_EQ
45e4b17023SJohn Marino #define DIRS_EQ(A, B) ((A)->dev == (B)->dev \
46e4b17023SJohn Marino && INO_T_EQ((A)->ino, (B)->ino))
47e4b17023SJohn Marino #else
48e4b17023SJohn Marino #define DIRS_EQ(A, B) (!filename_cmp ((A)->canonical_name, (B)->canonical_name))
49e4b17023SJohn Marino #endif
50e4b17023SJohn Marino
51e4b17023SJohn Marino static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
52e4b17023SJohn Marino
53e4b17023SJohn Marino static void add_env_var_paths (const char *, int);
54e4b17023SJohn Marino static void add_standard_paths (const char *, const char *, const char *, int);
55e4b17023SJohn Marino static void free_path (struct cpp_dir *, int);
56e4b17023SJohn Marino static void merge_include_chains (const char *, cpp_reader *, int);
57e4b17023SJohn Marino static void add_sysroot_to_chain (const char *, int);
58e4b17023SJohn Marino static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
59e4b17023SJohn Marino struct cpp_dir *,
60e4b17023SJohn Marino struct cpp_dir *, int);
61e4b17023SJohn Marino
62e4b17023SJohn Marino /* Include chains heads and tails. */
63e4b17023SJohn Marino static struct cpp_dir *heads[4];
64e4b17023SJohn Marino static struct cpp_dir *tails[4];
65e4b17023SJohn Marino static bool quote_ignores_source_dir;
66e4b17023SJohn Marino enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
67e4b17023SJohn Marino
68e4b17023SJohn Marino /* Free an element of the include chain, possibly giving a reason. */
69e4b17023SJohn Marino static void
free_path(struct cpp_dir * path,int reason)70e4b17023SJohn Marino free_path (struct cpp_dir *path, int reason)
71e4b17023SJohn Marino {
72e4b17023SJohn Marino switch (reason)
73e4b17023SJohn Marino {
74e4b17023SJohn Marino case REASON_DUP:
75e4b17023SJohn Marino case REASON_DUP_SYS:
76e4b17023SJohn Marino fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
77e4b17023SJohn Marino if (reason == REASON_DUP_SYS)
78e4b17023SJohn Marino fprintf (stderr,
79e4b17023SJohn Marino _(" as it is a non-system directory that duplicates a system directory\n"));
80e4b17023SJohn Marino break;
81e4b17023SJohn Marino
82e4b17023SJohn Marino case REASON_NOENT:
83e4b17023SJohn Marino fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
84e4b17023SJohn Marino path->name);
85e4b17023SJohn Marino break;
86e4b17023SJohn Marino
87e4b17023SJohn Marino case REASON_QUIET:
88e4b17023SJohn Marino default:
89e4b17023SJohn Marino break;
90e4b17023SJohn Marino }
91e4b17023SJohn Marino
92e4b17023SJohn Marino free (path->name);
93e4b17023SJohn Marino free (path);
94e4b17023SJohn Marino }
95e4b17023SJohn Marino
96e4b17023SJohn Marino /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
97e4b17023SJohn Marino append all the names to the search path CHAIN. */
98e4b17023SJohn Marino static void
add_env_var_paths(const char * env_var,int chain)99e4b17023SJohn Marino add_env_var_paths (const char *env_var, int chain)
100e4b17023SJohn Marino {
101e4b17023SJohn Marino char *p, *q, *path;
102e4b17023SJohn Marino
103e4b17023SJohn Marino q = getenv (env_var);
104e4b17023SJohn Marino
105e4b17023SJohn Marino if (!q)
106e4b17023SJohn Marino return;
107e4b17023SJohn Marino
108e4b17023SJohn Marino for (p = q; *q; p = q + 1)
109e4b17023SJohn Marino {
110e4b17023SJohn Marino q = p;
111e4b17023SJohn Marino while (*q != 0 && *q != PATH_SEPARATOR)
112e4b17023SJohn Marino q++;
113e4b17023SJohn Marino
114e4b17023SJohn Marino if (p == q)
115e4b17023SJohn Marino path = xstrdup (".");
116e4b17023SJohn Marino else
117e4b17023SJohn Marino {
118e4b17023SJohn Marino path = XNEWVEC (char, q - p + 1);
119e4b17023SJohn Marino memcpy (path, p, q - p);
120e4b17023SJohn Marino path[q - p] = '\0';
121e4b17023SJohn Marino }
122e4b17023SJohn Marino
123e4b17023SJohn Marino add_path (path, chain, chain == SYSTEM, false);
124e4b17023SJohn Marino }
125e4b17023SJohn Marino }
126e4b17023SJohn Marino
127e4b17023SJohn Marino /* Append the standard include chain defined in cppdefault.c. */
128e4b17023SJohn Marino static void
add_standard_paths(const char * sysroot,const char * iprefix,const char * imultilib,int cxx_stdinc)129e4b17023SJohn Marino add_standard_paths (const char *sysroot, const char *iprefix,
130e4b17023SJohn Marino const char *imultilib, int cxx_stdinc)
131e4b17023SJohn Marino {
132e4b17023SJohn Marino const struct default_include *p;
133e4b17023SJohn Marino int relocated = cpp_relocated();
134e4b17023SJohn Marino size_t len;
135e4b17023SJohn Marino
136e4b17023SJohn Marino if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
137e4b17023SJohn Marino {
138e4b17023SJohn Marino /* Look for directories that start with the standard prefix.
139e4b17023SJohn Marino "Translate" them, i.e. replace /usr/local/lib/gcc... with
140e4b17023SJohn Marino IPREFIX and search them first. */
141e4b17023SJohn Marino for (p = cpp_include_defaults; p->fname; p++)
142e4b17023SJohn Marino {
143e4b17023SJohn Marino if (!p->cplusplus || cxx_stdinc)
144e4b17023SJohn Marino {
145e4b17023SJohn Marino /* Should we be translating sysrooted dirs too? Assume
146e4b17023SJohn Marino that iprefix and sysroot are mutually exclusive, for
147e4b17023SJohn Marino now. */
148e4b17023SJohn Marino if (sysroot && p->add_sysroot)
149e4b17023SJohn Marino continue;
150e4b17023SJohn Marino if (!filename_ncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
151e4b17023SJohn Marino {
152e4b17023SJohn Marino char *str = concat (iprefix, p->fname + len, NULL);
153*5ce9237cSJohn Marino if (p->multilib == 1 && imultilib)
154e4b17023SJohn Marino str = concat (str, dir_separator_str, imultilib, NULL);
155*5ce9237cSJohn Marino else if (p->multilib == 2)
156*5ce9237cSJohn Marino {
157*5ce9237cSJohn Marino if (!imultiarch)
158*5ce9237cSJohn Marino continue;
159*5ce9237cSJohn Marino str = concat (str, dir_separator_str, imultiarch, NULL);
160*5ce9237cSJohn Marino }
161e4b17023SJohn Marino add_path (str, SYSTEM, p->cxx_aware, false);
162e4b17023SJohn Marino }
163e4b17023SJohn Marino }
164e4b17023SJohn Marino }
165e4b17023SJohn Marino }
166e4b17023SJohn Marino
167e4b17023SJohn Marino for (p = cpp_include_defaults; p->fname; p++)
168e4b17023SJohn Marino {
169e4b17023SJohn Marino if (!p->cplusplus || cxx_stdinc)
170e4b17023SJohn Marino {
171e4b17023SJohn Marino char *str;
172e4b17023SJohn Marino
173e4b17023SJohn Marino /* Should this directory start with the sysroot? */
174e4b17023SJohn Marino if (sysroot && p->add_sysroot)
175e4b17023SJohn Marino {
176e4b17023SJohn Marino char *sysroot_no_trailing_dir_separator = xstrdup (sysroot);
177e4b17023SJohn Marino size_t sysroot_len = strlen (sysroot);
178e4b17023SJohn Marino
179e4b17023SJohn Marino if (sysroot_len > 0 && sysroot[sysroot_len - 1] == DIR_SEPARATOR)
180e4b17023SJohn Marino sysroot_no_trailing_dir_separator[sysroot_len - 1] = '\0';
181e4b17023SJohn Marino str = concat (sysroot_no_trailing_dir_separator, p->fname, NULL);
182e4b17023SJohn Marino free (sysroot_no_trailing_dir_separator);
183e4b17023SJohn Marino }
184e4b17023SJohn Marino else if (!p->add_sysroot && relocated
185e4b17023SJohn Marino && !filename_ncmp (p->fname, cpp_PREFIX, cpp_PREFIX_len))
186e4b17023SJohn Marino {
187e4b17023SJohn Marino static const char *relocated_prefix;
188e4b17023SJohn Marino /* If this path starts with the configure-time prefix,
189e4b17023SJohn Marino but the compiler has been relocated, replace it
190e4b17023SJohn Marino with the run-time prefix. The run-time exec prefix
191e4b17023SJohn Marino is GCC_EXEC_PREFIX. Compute the path from there back
192e4b17023SJohn Marino to the toplevel prefix. */
193e4b17023SJohn Marino if (!relocated_prefix)
194e4b17023SJohn Marino {
195e4b17023SJohn Marino char *dummy;
196e4b17023SJohn Marino /* Make relative prefix expects the first argument
197e4b17023SJohn Marino to be a program, not a directory. */
198e4b17023SJohn Marino dummy = concat (gcc_exec_prefix, "dummy", NULL);
199e4b17023SJohn Marino relocated_prefix
200e4b17023SJohn Marino = make_relative_prefix (dummy,
201e4b17023SJohn Marino cpp_EXEC_PREFIX,
202e4b17023SJohn Marino cpp_PREFIX);
203e4b17023SJohn Marino }
204e4b17023SJohn Marino str = concat (relocated_prefix,
205e4b17023SJohn Marino p->fname + cpp_PREFIX_len,
206e4b17023SJohn Marino NULL);
207e4b17023SJohn Marino str = update_path (str, p->component);
208e4b17023SJohn Marino }
209e4b17023SJohn Marino else
210e4b17023SJohn Marino str = update_path (p->fname, p->component);
211e4b17023SJohn Marino
212*5ce9237cSJohn Marino if (p->multilib == 1 && imultilib)
213e4b17023SJohn Marino str = concat (str, dir_separator_str, imultilib, NULL);
214*5ce9237cSJohn Marino else if (p->multilib == 2)
215*5ce9237cSJohn Marino {
216*5ce9237cSJohn Marino if (!imultiarch)
217*5ce9237cSJohn Marino continue;
218*5ce9237cSJohn Marino str = concat (str, dir_separator_str, imultiarch, NULL);
219*5ce9237cSJohn Marino }
220e4b17023SJohn Marino
221e4b17023SJohn Marino add_path (str, SYSTEM, p->cxx_aware, false);
222e4b17023SJohn Marino }
223e4b17023SJohn Marino }
224e4b17023SJohn Marino }
225e4b17023SJohn Marino
226e4b17023SJohn Marino /* For each duplicate path in chain HEAD, keep just the first one.
227e4b17023SJohn Marino Remove each path in chain HEAD that also exists in chain SYSTEM.
228e4b17023SJohn Marino Set the NEXT pointer of the last path in the resulting chain to
229e4b17023SJohn Marino JOIN, unless it duplicates JOIN in which case the last path is
230e4b17023SJohn Marino removed. Return the head of the resulting chain. Any of HEAD,
231e4b17023SJohn Marino JOIN and SYSTEM can be NULL. */
232e4b17023SJohn Marino
233e4b17023SJohn Marino static struct cpp_dir *
remove_duplicates(cpp_reader * pfile,struct cpp_dir * head,struct cpp_dir * system,struct cpp_dir * join,int verbose)234e4b17023SJohn Marino remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
235e4b17023SJohn Marino struct cpp_dir *system, struct cpp_dir *join,
236e4b17023SJohn Marino int verbose)
237e4b17023SJohn Marino {
238e4b17023SJohn Marino struct cpp_dir **pcur, *tmp, *cur;
239e4b17023SJohn Marino struct stat st;
240e4b17023SJohn Marino
241e4b17023SJohn Marino for (pcur = &head; *pcur; )
242e4b17023SJohn Marino {
243e4b17023SJohn Marino int reason = REASON_QUIET;
244e4b17023SJohn Marino
245e4b17023SJohn Marino cur = *pcur;
246e4b17023SJohn Marino
247e4b17023SJohn Marino if (stat (cur->name, &st))
248e4b17023SJohn Marino {
249e4b17023SJohn Marino /* Dirs that don't exist are silently ignored, unless verbose. */
250e4b17023SJohn Marino if (errno != ENOENT)
251e4b17023SJohn Marino cpp_errno (pfile, CPP_DL_ERROR, cur->name);
252e4b17023SJohn Marino else
253e4b17023SJohn Marino {
254e4b17023SJohn Marino /* If -Wmissing-include-dirs is given, warn. */
255e4b17023SJohn Marino cpp_options *opts = cpp_get_options (pfile);
256e4b17023SJohn Marino if (opts->warn_missing_include_dirs && cur->user_supplied_p)
257e4b17023SJohn Marino cpp_errno (pfile, CPP_DL_WARNING, cur->name);
258e4b17023SJohn Marino reason = REASON_NOENT;
259e4b17023SJohn Marino }
260e4b17023SJohn Marino }
261e4b17023SJohn Marino else if (!S_ISDIR (st.st_mode))
262e4b17023SJohn Marino cpp_error_with_line (pfile, CPP_DL_WARNING, 0, 0,
263e4b17023SJohn Marino "%s: not a directory", cur->name);
264e4b17023SJohn Marino else
265e4b17023SJohn Marino {
266e4b17023SJohn Marino #if defined (INO_T_COPY)
267e4b17023SJohn Marino INO_T_COPY (cur->ino, st.st_ino);
268e4b17023SJohn Marino cur->dev = st.st_dev;
269e4b17023SJohn Marino #endif
270e4b17023SJohn Marino
271e4b17023SJohn Marino /* Remove this one if it is in the system chain. */
272e4b17023SJohn Marino reason = REASON_DUP_SYS;
273e4b17023SJohn Marino for (tmp = system; tmp; tmp = tmp->next)
274e4b17023SJohn Marino if (DIRS_EQ (tmp, cur) && cur->construct == tmp->construct)
275e4b17023SJohn Marino break;
276e4b17023SJohn Marino
277e4b17023SJohn Marino if (!tmp)
278e4b17023SJohn Marino {
279e4b17023SJohn Marino /* Duplicate of something earlier in the same chain? */
280e4b17023SJohn Marino reason = REASON_DUP;
281e4b17023SJohn Marino for (tmp = head; tmp != cur; tmp = tmp->next)
282e4b17023SJohn Marino if (DIRS_EQ (cur, tmp) && cur->construct == tmp->construct)
283e4b17023SJohn Marino break;
284e4b17023SJohn Marino
285e4b17023SJohn Marino if (tmp == cur
286e4b17023SJohn Marino /* Last in the chain and duplicate of JOIN? */
287e4b17023SJohn Marino && !(cur->next == NULL && join
288e4b17023SJohn Marino && DIRS_EQ (cur, join)
289e4b17023SJohn Marino && cur->construct == join->construct))
290e4b17023SJohn Marino {
291e4b17023SJohn Marino /* Unique, so keep this directory. */
292e4b17023SJohn Marino pcur = &cur->next;
293e4b17023SJohn Marino continue;
294e4b17023SJohn Marino }
295e4b17023SJohn Marino }
296e4b17023SJohn Marino }
297e4b17023SJohn Marino
298e4b17023SJohn Marino /* Remove this entry from the chain. */
299e4b17023SJohn Marino *pcur = cur->next;
300e4b17023SJohn Marino free_path (cur, verbose ? reason: REASON_QUIET);
301e4b17023SJohn Marino }
302e4b17023SJohn Marino
303e4b17023SJohn Marino *pcur = join;
304e4b17023SJohn Marino return head;
305e4b17023SJohn Marino }
306e4b17023SJohn Marino
307e4b17023SJohn Marino /* Add SYSROOT to any user-supplied paths in CHAIN starting with
308e4b17023SJohn Marino "=". */
309e4b17023SJohn Marino
310e4b17023SJohn Marino static void
add_sysroot_to_chain(const char * sysroot,int chain)311e4b17023SJohn Marino add_sysroot_to_chain (const char *sysroot, int chain)
312e4b17023SJohn Marino {
313e4b17023SJohn Marino struct cpp_dir *p;
314e4b17023SJohn Marino
315e4b17023SJohn Marino for (p = heads[chain]; p != NULL; p = p->next)
316e4b17023SJohn Marino if (p->name[0] == '=' && p->user_supplied_p)
317e4b17023SJohn Marino p->name = concat (sysroot, p->name + 1, NULL);
318e4b17023SJohn Marino }
319e4b17023SJohn Marino
320e4b17023SJohn Marino /* Merge the four include chains together in the order quote, bracket,
321e4b17023SJohn Marino system, after. Remove duplicate dirs (determined in
322e4b17023SJohn Marino system-specific manner).
323e4b17023SJohn Marino
324e4b17023SJohn Marino We can't just merge the lists and then uniquify them because then
325e4b17023SJohn Marino we may lose directories from the <> search path that should be
326e4b17023SJohn Marino there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is
327e4b17023SJohn Marino however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
328e4b17023SJohn Marino written -iquote bar -Ifoo -Iquux. */
329e4b17023SJohn Marino
330e4b17023SJohn Marino static void
merge_include_chains(const char * sysroot,cpp_reader * pfile,int verbose)331e4b17023SJohn Marino merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
332e4b17023SJohn Marino {
333e4b17023SJohn Marino /* Add the sysroot to user-supplied paths starting with "=". */
334e4b17023SJohn Marino if (sysroot)
335e4b17023SJohn Marino {
336e4b17023SJohn Marino add_sysroot_to_chain (sysroot, QUOTE);
337e4b17023SJohn Marino add_sysroot_to_chain (sysroot, BRACKET);
338e4b17023SJohn Marino add_sysroot_to_chain (sysroot, SYSTEM);
339e4b17023SJohn Marino add_sysroot_to_chain (sysroot, AFTER);
340e4b17023SJohn Marino }
341e4b17023SJohn Marino
342e4b17023SJohn Marino /* Join the SYSTEM and AFTER chains. Remove duplicates in the
343e4b17023SJohn Marino resulting SYSTEM chain. */
344e4b17023SJohn Marino if (heads[SYSTEM])
345e4b17023SJohn Marino tails[SYSTEM]->next = heads[AFTER];
346e4b17023SJohn Marino else
347e4b17023SJohn Marino heads[SYSTEM] = heads[AFTER];
348e4b17023SJohn Marino heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
349e4b17023SJohn Marino
350e4b17023SJohn Marino /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
351e4b17023SJohn Marino join it to SYSTEM. */
352e4b17023SJohn Marino heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
353e4b17023SJohn Marino heads[SYSTEM], verbose);
354e4b17023SJohn Marino
355e4b17023SJohn Marino /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
356e4b17023SJohn Marino join it to BRACKET. */
357e4b17023SJohn Marino heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
358e4b17023SJohn Marino heads[BRACKET], verbose);
359e4b17023SJohn Marino
360e4b17023SJohn Marino /* If verbose, print the list of dirs to search. */
361e4b17023SJohn Marino if (verbose)
362e4b17023SJohn Marino {
363e4b17023SJohn Marino struct cpp_dir *p;
364e4b17023SJohn Marino
365e4b17023SJohn Marino fprintf (stderr, _("#include \"...\" search starts here:\n"));
366e4b17023SJohn Marino for (p = heads[QUOTE];; p = p->next)
367e4b17023SJohn Marino {
368e4b17023SJohn Marino if (p == heads[BRACKET])
369e4b17023SJohn Marino fprintf (stderr, _("#include <...> search starts here:\n"));
370e4b17023SJohn Marino if (!p)
371e4b17023SJohn Marino break;
372e4b17023SJohn Marino fprintf (stderr, " %s\n", p->name);
373e4b17023SJohn Marino }
374e4b17023SJohn Marino fprintf (stderr, _("End of search list.\n"));
375e4b17023SJohn Marino }
376e4b17023SJohn Marino }
377e4b17023SJohn Marino
378e4b17023SJohn Marino /* Use given -I paths for #include "..." but not #include <...>, and
379e4b17023SJohn Marino don't search the directory of the present file for #include "...".
380e4b17023SJohn Marino (Note that -I. -I- is not the same as the default setup; -I. uses
381e4b17023SJohn Marino the compiler's working dir.) */
382e4b17023SJohn Marino void
split_quote_chain(void)383e4b17023SJohn Marino split_quote_chain (void)
384e4b17023SJohn Marino {
385e4b17023SJohn Marino if (heads[QUOTE])
386e4b17023SJohn Marino free_path (heads[QUOTE], REASON_QUIET);
387e4b17023SJohn Marino if (tails[QUOTE])
388e4b17023SJohn Marino free_path (tails[QUOTE], REASON_QUIET);
389e4b17023SJohn Marino heads[QUOTE] = heads[BRACKET];
390e4b17023SJohn Marino tails[QUOTE] = tails[BRACKET];
391e4b17023SJohn Marino heads[BRACKET] = NULL;
392e4b17023SJohn Marino tails[BRACKET] = NULL;
393e4b17023SJohn Marino /* This is NOT redundant. */
394e4b17023SJohn Marino quote_ignores_source_dir = true;
395e4b17023SJohn Marino }
396e4b17023SJohn Marino
397e4b17023SJohn Marino /* Add P to the chain specified by CHAIN. */
398e4b17023SJohn Marino
399e4b17023SJohn Marino void
add_cpp_dir_path(cpp_dir * p,int chain)400e4b17023SJohn Marino add_cpp_dir_path (cpp_dir *p, int chain)
401e4b17023SJohn Marino {
402e4b17023SJohn Marino if (tails[chain])
403e4b17023SJohn Marino tails[chain]->next = p;
404e4b17023SJohn Marino else
405e4b17023SJohn Marino heads[chain] = p;
406e4b17023SJohn Marino tails[chain] = p;
407e4b17023SJohn Marino }
408e4b17023SJohn Marino
409e4b17023SJohn Marino /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
410e4b17023SJohn Marino NUL-terminated. */
411e4b17023SJohn Marino void
add_path(char * path,int chain,int cxx_aware,bool user_supplied_p)412e4b17023SJohn Marino add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)
413e4b17023SJohn Marino {
414e4b17023SJohn Marino cpp_dir *p;
415e4b17023SJohn Marino
416e4b17023SJohn Marino #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
417e4b17023SJohn Marino /* Remove unnecessary trailing slashes. On some versions of MS
418e4b17023SJohn Marino Windows, trailing _forward_ slashes cause no problems for stat().
419e4b17023SJohn Marino On newer versions, stat() does not recognize a directory that ends
420e4b17023SJohn Marino in a '\\' or '/', unless it is a drive root dir, such as "c:/",
421e4b17023SJohn Marino where it is obligatory. */
422e4b17023SJohn Marino int pathlen = strlen (path);
423e4b17023SJohn Marino char* end = path + pathlen - 1;
424e4b17023SJohn Marino /* Preserve the lead '/' or lead "c:/". */
425e4b17023SJohn Marino char* start = path + (pathlen > 2 && path[1] == ':' ? 3 : 1);
426e4b17023SJohn Marino
427e4b17023SJohn Marino for (; end > start && IS_DIR_SEPARATOR (*end); end--)
428e4b17023SJohn Marino *end = 0;
429e4b17023SJohn Marino #endif
430e4b17023SJohn Marino
431e4b17023SJohn Marino p = XNEW (cpp_dir);
432e4b17023SJohn Marino p->next = NULL;
433e4b17023SJohn Marino p->name = path;
434e4b17023SJohn Marino #ifndef INO_T_EQ
435e4b17023SJohn Marino p->canonical_name = lrealpath (path);
436e4b17023SJohn Marino #endif
437e4b17023SJohn Marino if (chain == SYSTEM || chain == AFTER)
438e4b17023SJohn Marino p->sysp = 1 + !cxx_aware;
439e4b17023SJohn Marino else
440e4b17023SJohn Marino p->sysp = 0;
441e4b17023SJohn Marino p->construct = 0;
442e4b17023SJohn Marino p->user_supplied_p = user_supplied_p;
443e4b17023SJohn Marino
444e4b17023SJohn Marino add_cpp_dir_path (p, chain);
445e4b17023SJohn Marino }
446e4b17023SJohn Marino
447e4b17023SJohn Marino /* Exported function to handle include chain merging, duplicate
448e4b17023SJohn Marino removal, and registration with cpplib. */
449e4b17023SJohn Marino void
register_include_chains(cpp_reader * pfile,const char * sysroot,const char * iprefix,const char * imultilib,int stdinc,int cxx_stdinc,int verbose)450e4b17023SJohn Marino register_include_chains (cpp_reader *pfile, const char *sysroot,
451e4b17023SJohn Marino const char *iprefix, const char *imultilib,
452e4b17023SJohn Marino int stdinc, int cxx_stdinc, int verbose)
453e4b17023SJohn Marino {
454e4b17023SJohn Marino static const char *const lang_env_vars[] =
455e4b17023SJohn Marino { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
456e4b17023SJohn Marino "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
457e4b17023SJohn Marino cpp_options *cpp_opts = cpp_get_options (pfile);
458e4b17023SJohn Marino size_t idx = (cpp_opts->objc ? 2: 0);
459e4b17023SJohn Marino
460e4b17023SJohn Marino if (cpp_opts->cplusplus)
461e4b17023SJohn Marino idx++;
462e4b17023SJohn Marino else
463e4b17023SJohn Marino cxx_stdinc = false;
464e4b17023SJohn Marino
465e4b17023SJohn Marino /* CPATH and language-dependent environment variables may add to the
466e4b17023SJohn Marino include chain. */
467e4b17023SJohn Marino add_env_var_paths ("CPATH", BRACKET);
468e4b17023SJohn Marino add_env_var_paths (lang_env_vars[idx], SYSTEM);
469e4b17023SJohn Marino
470e4b17023SJohn Marino target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc);
471e4b17023SJohn Marino
472e4b17023SJohn Marino /* Finally chain on the standard directories. */
473e4b17023SJohn Marino if (stdinc)
474e4b17023SJohn Marino add_standard_paths (sysroot, iprefix, imultilib, cxx_stdinc);
475e4b17023SJohn Marino
476e4b17023SJohn Marino target_c_incpath.extra_includes (sysroot, iprefix, stdinc);
477e4b17023SJohn Marino
478e4b17023SJohn Marino merge_include_chains (sysroot, pfile, verbose);
479e4b17023SJohn Marino
480e4b17023SJohn Marino cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
481e4b17023SJohn Marino quote_ignores_source_dir);
482e4b17023SJohn Marino }
483e4b17023SJohn Marino
484e4b17023SJohn Marino /* Return the current chain of cpp dirs. */
485e4b17023SJohn Marino
486e4b17023SJohn Marino struct cpp_dir *
get_added_cpp_dirs(int chain)487e4b17023SJohn Marino get_added_cpp_dirs (int chain)
488e4b17023SJohn Marino {
489e4b17023SJohn Marino return heads[chain];
490e4b17023SJohn Marino }
491e4b17023SJohn Marino
492e4b17023SJohn Marino #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
hook_void_charptr_charptr_int(const char * sysroot ATTRIBUTE_UNUSED,const char * iprefix ATTRIBUTE_UNUSED,int stdinc ATTRIBUTE_UNUSED)493e4b17023SJohn Marino static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED,
494e4b17023SJohn Marino const char *iprefix ATTRIBUTE_UNUSED,
495e4b17023SJohn Marino int stdinc ATTRIBUTE_UNUSED)
496e4b17023SJohn Marino {
497e4b17023SJohn Marino }
498e4b17023SJohn Marino #endif
499e4b17023SJohn Marino
500e4b17023SJohn Marino #ifndef TARGET_EXTRA_INCLUDES
501e4b17023SJohn Marino #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
502e4b17023SJohn Marino #endif
503e4b17023SJohn Marino #ifndef TARGET_EXTRA_PRE_INCLUDES
504e4b17023SJohn Marino #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
505e4b17023SJohn Marino #endif
506e4b17023SJohn Marino
507e4b17023SJohn Marino struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES };
508e4b17023SJohn Marino
509