2003-06-19 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / c-incpath.c
blobb350a21e9428c1e5d5abcca367d24143baeefad3
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
10 later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "prefix.h"
27 #include "intl.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. */
34 #ifdef VMS
35 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
36 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
37 #else
38 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
39 # define INO_T_EQ(A, B) 0
40 # else
41 # define INO_T_EQ(A, B) ((A) == (B))
42 # endif
43 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
44 #endif
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 struct cpp_path *
51 remove_duplicates PARAMS ((cpp_reader *, struct cpp_path *,
52 struct cpp_path *, struct cpp_path *, int));
54 /* Include chains heads and tails. */
55 static struct cpp_path *heads[4];
56 static struct cpp_path *tails[4];
57 static bool quote_ignores_source_dir;
58 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
60 /* Free an element of the include chain, possibly giving a reason. */
61 static void
62 free_path (path, reason)
63 struct cpp_path *path;
64 int reason;
66 switch (reason)
68 case REASON_DUP:
69 case REASON_DUP_SYS:
70 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
71 if (reason == REASON_DUP_SYS)
72 fprintf (stderr,
73 _(" as it is a non-system directory that duplicates a system directory\n"));
74 break;
76 case REASON_NOENT:
77 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
78 path->name);
79 break;
81 case REASON_QUIET:
82 default:
83 break;
86 free (path->name);
87 free (path);
90 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
91 append all the names to the search path CHAIN. */
92 static void
93 add_env_var_paths (env_var, chain)
94 const char *env_var;
95 int chain;
97 char *p, *q, *path;
99 GET_ENVIRONMENT (q, env_var);
101 if (!q)
102 return;
104 for (p = q; *q; p = q + 1)
106 q = p;
107 while (*q != 0 && *q != PATH_SEPARATOR)
108 q++;
110 if (p == q)
111 path = xstrdup (".");
112 else
114 path = xmalloc (q - p + 1);
115 memcpy (path, p, q - p);
116 path[q - p] = '\0';
119 add_path (path, chain, chain == SYSTEM);
123 /* Append the standard include chain defined in cppdefault.c. */
124 static void
125 add_standard_paths (sysroot, iprefix, cxx_stdinc)
126 const char *sysroot, *iprefix;
127 int cxx_stdinc;
129 const struct default_include *p;
130 size_t len;
132 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
134 /* Look for directories that start with the standard prefix.
135 "Translate" them, ie. replace /usr/local/lib/gcc... with
136 IPREFIX and search them first. */
137 for (p = cpp_include_defaults; p->fname; p++)
139 if (!p->cplusplus || cxx_stdinc)
141 /* Should we be translating sysrooted dirs too? Assume
142 that iprefix and sysroot are mutually exclusive, for
143 now. */
144 if (sysroot && p->add_sysroot)
145 continue;
146 if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
148 char *str = concat (iprefix, p->fname + len, NULL);
149 add_path (str, SYSTEM, p->cxx_aware);
155 for (p = cpp_include_defaults; p->fname; p++)
157 if (!p->cplusplus || cxx_stdinc)
159 char *str;
161 /* Should this directory start with the sysroot? */
162 if (sysroot && p->add_sysroot)
163 str = concat (sysroot, p->fname, NULL);
164 else
165 str = update_path (p->fname, p->component);
167 add_path (str, SYSTEM, p->cxx_aware);
172 /* For each duplicate path in chain HEAD, keep just the first one.
173 Remove each path in chain HEAD that also exists in chain SYSTEM.
174 Set the NEXT pointer of the last path in the resulting chain to
175 JOIN, unless it duplicates JOIN in which case the last path is
176 removed. Return the head of the resulting chain. Any of HEAD,
177 JOIN and SYSTEM can be NULL. */
178 static struct cpp_path *
179 remove_duplicates (pfile, head, system, join, verbose)
180 cpp_reader *pfile;
181 struct cpp_path *head;
182 struct cpp_path *system;
183 struct cpp_path *join;
184 int verbose;
186 struct cpp_path **pcur, *tmp, *cur;
187 struct stat st;
189 for (pcur = &head; *pcur; )
191 int reason = REASON_QUIET;
193 cur = *pcur;
194 cpp_simplify_path (cur->name);
196 if (stat (cur->name, &st))
198 /* Dirs that don't exist are silently ignored, unless verbose. */
199 if (errno != ENOENT)
200 cpp_errno (pfile, DL_ERROR, cur->name);
201 else
202 reason = REASON_NOENT;
204 else if (!S_ISDIR (st.st_mode))
205 cpp_error_with_line (pfile, DL_ERROR, 0, 0,
206 "%s: not a directory", cur->name);
207 else
209 INO_T_COPY (cur->ino, st.st_ino);
210 cur->dev = st.st_dev;
212 /* Remove this one if it is in the system chain. */
213 reason = REASON_DUP_SYS;
214 for (tmp = system; tmp; tmp = tmp->next)
215 if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev)
216 break;
218 if (!tmp)
220 /* Dupicate of something earlier in the same chain? */
221 reason = REASON_DUP;
222 for (tmp = head; tmp != cur; tmp = tmp->next)
223 if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev)
224 break;
226 if (tmp == cur
227 /* Last in the chain and duplicate of JOIN? */
228 && !(cur->next == NULL && join
229 && INO_T_EQ (cur->ino, join->ino)
230 && cur->dev == join->dev))
232 /* Unique, so keep this directory. */
233 pcur = &cur->next;
234 continue;
239 /* Remove this entry from the chain. */
240 *pcur = cur->next;
241 free_path (cur, verbose ? reason: REASON_QUIET);
244 *pcur = join;
245 return head;
248 /* Merge the four include chains together in the order quote, bracket,
249 system, after. Remove duplicate dirs (as determined by
250 INO_T_EQ()).
252 We can't just merge the lists and then uniquify them because then
253 we may lose directories from the <> search path that should be
254 there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however safe
255 to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
256 -Iquux. */
257 static void
258 merge_include_chains (pfile, verbose)
259 cpp_reader *pfile;
260 int verbose;
262 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
263 resulting SYSTEM chain. */
264 if (heads[SYSTEM])
265 tails[SYSTEM]->next = heads[AFTER];
266 else
267 heads[SYSTEM] = heads[AFTER];
268 heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
270 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
271 join it to SYSTEM. */
272 heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
273 heads[SYSTEM], verbose);
275 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
276 join it to BRACKET. */
277 heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
278 heads[BRACKET], verbose);
280 /* If verbose, print the list of dirs to search. */
281 if (verbose)
283 struct cpp_path *p;
285 fprintf (stderr, _("#include \"...\" search starts here:\n"));
286 for (p = heads[QUOTE];; p = p->next)
288 if (p == heads[BRACKET])
289 fprintf (stderr, _("#include <...> search starts here:\n"));
290 if (!p)
291 break;
292 fprintf (stderr, " %s\n", p->name);
294 fprintf (stderr, _("End of search list.\n"));
298 /* Use given -I paths for #include "..." but not #include <...>, and
299 don't search the directory of the present file for #include "...".
300 (Note that -I. -I- is not the same as the default setup; -I. uses
301 the compiler's working dir.) */
302 void
303 split_quote_chain ()
305 heads[QUOTE] = heads[BRACKET];
306 tails[QUOTE] = tails[BRACKET];
307 heads[BRACKET] = NULL;
308 tails[BRACKET] = NULL;
309 /* This is NOT redundant. */
310 quote_ignores_source_dir = true;
313 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
314 NUL-terminated. */
315 void
316 add_path (path, chain, cxx_aware)
317 char *path;
318 int chain;
319 int cxx_aware;
321 struct cpp_path *p;
323 p = (struct cpp_path *) xmalloc (sizeof (struct cpp_path));
324 p->next = NULL;
325 p->name = path;
326 if (chain == SYSTEM || chain == AFTER)
327 p->sysp = 1 + !cxx_aware;
328 else
329 p->sysp = 0;
331 if (tails[chain])
332 tails[chain]->next = p;
333 else
334 heads[chain] = p;
335 tails[chain] = p;
338 /* Exported function to handle include chain merging, duplicate
339 removal, and registration with cpplib. */
340 void
341 register_include_chains (pfile, sysroot, iprefix,
342 stdinc, cxx_stdinc, verbose)
343 cpp_reader *pfile;
344 const char *sysroot, *iprefix;
345 int stdinc, cxx_stdinc, verbose;
347 static const char *const lang_env_vars[] =
348 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
349 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
350 cpp_options *cpp_opts = cpp_get_options (pfile);
351 size_t idx = (cpp_opts->objc ? 2: 0);
353 if (cpp_opts->cplusplus)
354 idx++;
355 else
356 cxx_stdinc = false;
358 /* CPATH and language-dependent environment variables may add to the
359 include chain. */
360 add_env_var_paths ("CPATH", BRACKET);
361 add_env_var_paths (lang_env_vars[idx], SYSTEM);
363 /* Finally chain on the standard directories. */
364 if (stdinc)
365 add_standard_paths (sysroot, iprefix, cxx_stdinc);
367 merge_include_chains (pfile, verbose);
369 cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
370 quote_ignores_source_dir);