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