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