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.
6 Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
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
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.
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. */
24 #include "coretypes.h"
31 #include "c-incpath.h"
32 #include "cppdefault.h"
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. */
38 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
39 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
41 # if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__
42 # define INO_T_EQ(A, B) 0
44 # define INO_T_EQ(A, B) ((A) == (B))
46 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
49 static const char dir_separator_str
[] = { DIR_SEPARATOR
, 0 };
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
*,
57 struct cpp_dir
*, int);
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
};
65 /* Free an element of the include chain, possibly giving a reason. */
67 free_path (struct cpp_dir
*path
, int reason
)
73 fprintf (stderr
, _("ignoring duplicate directory \"%s\"\n"), path
->name
);
74 if (reason
== REASON_DUP_SYS
)
76 _(" as it is a non-system directory that duplicates a system directory\n"));
80 fprintf (stderr
, _("ignoring nonexistent directory \"%s\"\n"),
93 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
94 append all the names to the search path CHAIN. */
96 add_env_var_paths (const char *env_var
, int chain
)
100 GET_ENVIRONMENT (q
, env_var
);
105 for (p
= q
; *q
; p
= q
+ 1)
108 while (*q
!= 0 && *q
!= PATH_SEPARATOR
)
112 path
= xstrdup (".");
115 path
= XNEWVEC (char, q
- p
+ 1);
116 memcpy (path
, p
, q
- p
);
120 add_path (path
, chain
, chain
== SYSTEM
, false);
124 /* Append the standard include chain defined in cppdefault.c. */
126 add_standard_paths (const char *sysroot
, const char *iprefix
,
127 const char *imultilib
, int cxx_stdinc
)
129 const struct default_include
*p
;
130 int relocated
= cpp_relocated();
133 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0)
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
++)
140 if (!p
->cplusplus
|| cxx_stdinc
)
142 /* Should we be translating sysrooted dirs too? Assume
143 that iprefix and sysroot are mutually exclusive, for
145 if (sysroot
&& p
->add_sysroot
)
147 if (!strncmp (p
->fname
, cpp_GCC_INCLUDE_DIR
, len
))
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);
158 for (p
= cpp_include_defaults
; p
->fname
; p
++)
160 if (!p
->cplusplus
|| cxx_stdinc
)
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)
170 static const char *relocated_prefix
;
171 /* If this path starts with the configure-time prefix,
172 but the compiler has been relocated, replace it
173 with the run-time prefix. The run-time exec prefix
174 is GCC_EXEC_PREFIX. Compute the path from there back
175 to the toplevel prefix. */
176 if (!relocated_prefix
)
179 /* Make relative prefix expects the first argument
180 to be a program, not a directory. */
181 dummy
= concat (gcc_exec_prefix
, "dummy", NULL
);
183 = make_relative_prefix (dummy
,
187 str
= concat (relocated_prefix
,
188 p
->fname
+ cpp_PREFIX_len
,
190 str
= update_path (str
, p
->component
);
193 str
= update_path (p
->fname
, p
->component
);
195 if (p
->multilib
&& imultilib
)
196 str
= concat (str
, dir_separator_str
, imultilib
, NULL
);
198 add_path (str
, SYSTEM
, p
->cxx_aware
, false);
203 /* For each duplicate path in chain HEAD, keep just the first one.
204 Remove each path in chain HEAD that also exists in chain SYSTEM.
205 Set the NEXT pointer of the last path in the resulting chain to
206 JOIN, unless it duplicates JOIN in which case the last path is
207 removed. Return the head of the resulting chain. Any of HEAD,
208 JOIN and SYSTEM can be NULL. */
210 static struct cpp_dir
*
211 remove_duplicates (cpp_reader
*pfile
, struct cpp_dir
*head
,
212 struct cpp_dir
*system
, struct cpp_dir
*join
,
215 struct cpp_dir
**pcur
, *tmp
, *cur
;
218 for (pcur
= &head
; *pcur
; )
220 int reason
= REASON_QUIET
;
224 if (stat (cur
->name
, &st
))
226 /* Dirs that don't exist are silently ignored, unless verbose. */
228 cpp_errno (pfile
, CPP_DL_ERROR
, cur
->name
);
231 /* If -Wmissing-include-dirs is given, warn. */
232 cpp_options
*opts
= cpp_get_options (pfile
);
233 if (opts
->warn_missing_include_dirs
&& cur
->user_supplied_p
)
234 cpp_errno (pfile
, CPP_DL_WARNING
, cur
->name
);
235 reason
= REASON_NOENT
;
238 else if (!S_ISDIR (st
.st_mode
))
239 cpp_error_with_line (pfile
, CPP_DL_ERROR
, 0, 0,
240 "%s: not a directory", cur
->name
);
243 INO_T_COPY (cur
->ino
, st
.st_ino
);
244 cur
->dev
= st
.st_dev
;
246 /* Remove this one if it is in the system chain. */
247 reason
= REASON_DUP_SYS
;
248 for (tmp
= system
; tmp
; tmp
= tmp
->next
)
249 if (INO_T_EQ (tmp
->ino
, cur
->ino
) && tmp
->dev
== cur
->dev
250 && cur
->construct
== tmp
->construct
)
255 /* Duplicate of something earlier in the same chain? */
257 for (tmp
= head
; tmp
!= cur
; tmp
= tmp
->next
)
258 if (INO_T_EQ (cur
->ino
, tmp
->ino
) && cur
->dev
== tmp
->dev
259 && cur
->construct
== tmp
->construct
)
263 /* Last in the chain and duplicate of JOIN? */
264 && !(cur
->next
== NULL
&& join
265 && INO_T_EQ (cur
->ino
, join
->ino
)
266 && cur
->dev
== join
->dev
267 && cur
->construct
== join
->construct
))
269 /* Unique, so keep this directory. */
276 /* Remove this entry from the chain. */
278 free_path (cur
, verbose
? reason
: REASON_QUIET
);
285 /* Merge the four include chains together in the order quote, bracket,
286 system, after. Remove duplicate dirs (as determined by
289 We can't just merge the lists and then uniquify them because then
290 we may lose directories from the <> search path that should be
291 there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is
292 however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
293 written -iquote bar -Ifoo -Iquux. */
296 merge_include_chains (cpp_reader
*pfile
, int verbose
)
298 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
299 resulting SYSTEM chain. */
301 tails
[SYSTEM
]->next
= heads
[AFTER
];
303 heads
[SYSTEM
] = heads
[AFTER
];
304 heads
[SYSTEM
] = remove_duplicates (pfile
, heads
[SYSTEM
], 0, 0, verbose
);
306 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
307 join it to SYSTEM. */
308 heads
[BRACKET
] = remove_duplicates (pfile
, heads
[BRACKET
], heads
[SYSTEM
],
309 heads
[SYSTEM
], verbose
);
311 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
312 join it to BRACKET. */
313 heads
[QUOTE
] = remove_duplicates (pfile
, heads
[QUOTE
], heads
[SYSTEM
],
314 heads
[BRACKET
], verbose
);
316 /* If verbose, print the list of dirs to search. */
321 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
322 for (p
= heads
[QUOTE
];; p
= p
->next
)
324 if (p
== heads
[BRACKET
])
325 fprintf (stderr
, _("#include <...> search starts here:\n"));
328 fprintf (stderr
, " %s\n", p
->name
);
330 fprintf (stderr
, _("End of search list.\n"));
334 /* Use given -I paths for #include "..." but not #include <...>, and
335 don't search the directory of the present file for #include "...".
336 (Note that -I. -I- is not the same as the default setup; -I. uses
337 the compiler's working dir.) */
339 split_quote_chain (void)
341 heads
[QUOTE
] = heads
[BRACKET
];
342 tails
[QUOTE
] = tails
[BRACKET
];
343 heads
[BRACKET
] = NULL
;
344 tails
[BRACKET
] = NULL
;
345 /* This is NOT redundant. */
346 quote_ignores_source_dir
= true;
349 /* Add P to the chain specified by CHAIN. */
352 add_cpp_dir_path (cpp_dir
*p
, int chain
)
355 tails
[chain
]->next
= p
;
361 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
364 add_path (char *path
, int chain
, int cxx_aware
, bool user_supplied_p
)
368 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
369 /* Convert all backslashes to slashes. The native CRT stat()
370 function does not recognize a directory that ends in a backslash
371 (unless it is a drive root dir, such "c:\"). Forward slashes,
372 trailing or otherwise, cause no problems for stat(). */
374 for (c
= path
; *c
; c
++)
375 if (*c
== '\\') *c
= '/';
381 if (chain
== SYSTEM
|| chain
== AFTER
)
382 p
->sysp
= 1 + !cxx_aware
;
386 p
->user_supplied_p
= user_supplied_p
;
388 add_cpp_dir_path (p
, chain
);
391 /* Exported function to handle include chain merging, duplicate
392 removal, and registration with cpplib. */
394 register_include_chains (cpp_reader
*pfile
, const char *sysroot
,
395 const char *iprefix
, const char *imultilib
,
396 int stdinc
, int cxx_stdinc
, int verbose
)
398 static const char *const lang_env_vars
[] =
399 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
400 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
401 cpp_options
*cpp_opts
= cpp_get_options (pfile
);
402 size_t idx
= (cpp_opts
->objc
? 2: 0);
404 if (cpp_opts
->cplusplus
)
409 /* CPATH and language-dependent environment variables may add to the
411 add_env_var_paths ("CPATH", BRACKET
);
412 add_env_var_paths (lang_env_vars
[idx
], SYSTEM
);
414 target_c_incpath
.extra_pre_includes (sysroot
, iprefix
, stdinc
);
416 /* Finally chain on the standard directories. */
418 add_standard_paths (sysroot
, iprefix
, imultilib
, cxx_stdinc
);
420 target_c_incpath
.extra_includes (sysroot
, iprefix
, stdinc
);
422 merge_include_chains (pfile
, verbose
);
424 cpp_set_include_chains (pfile
, heads
[QUOTE
], heads
[BRACKET
],
425 quote_ignores_source_dir
);
427 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
428 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED
,
429 const char *iprefix ATTRIBUTE_UNUSED
,
430 int stdinc ATTRIBUTE_UNUSED
)
435 #ifndef TARGET_EXTRA_INCLUDES
436 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
438 #ifndef TARGET_EXTRA_PRE_INCLUDES
439 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
442 struct target_c_incpath_s target_c_incpath
= { TARGET_EXTRA_PRE_INCLUDES
, TARGET_EXTRA_INCLUDES
};