]>
Commit | Line | Data |
---|---|---|
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, | |
2b6dd222 JM |
3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
4 | Free Software Foundation, Inc. | |
5793b276 NB |
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 | |
366ccddb | 20 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
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 |
49 | static const char dir_separator_str[] = { DIR_SEPARATOR, 0 }; |
50 | ||
35b1a6fa | 51 | static void add_env_var_paths (const char *, int); |
2b6dd222 | 52 | static void add_standard_paths (const char *, const char *, const char *, int); |
8f9b4009 | 53 | static void free_path (struct cpp_dir *, int); |
35b1a6fa | 54 | static void merge_include_chains (cpp_reader *, int); |
8f9b4009 NB |
55 | static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *, |
56 | struct cpp_dir *, | |
57 | struct cpp_dir *, int); | |
5793b276 NB |
58 | |
59 | /* Include chains heads and tails. */ | |
8f9b4009 NB |
60 | static struct cpp_dir *heads[4]; |
61 | static struct cpp_dir *tails[4]; | |
5793b276 NB |
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 | |
8f9b4009 | 67 | free_path (struct cpp_dir *path, int reason) |
5793b276 NB |
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 | ||
fad205ff | 89 | free (path->name); |
5793b276 NB |
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 | |
35b1a6fa | 96 | add_env_var_paths (const char *env_var, int chain) |
5793b276 NB |
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 | { | |
cceb1885 | 115 | path = XNEWVEC (char, q - p + 1); |
5793b276 NB |
116 | memcpy (path, p, q - p); |
117 | path[q - p] = '\0'; | |
118 | } | |
119 | ||
b02398bd | 120 | add_path (path, chain, chain == SYSTEM, false); |
5793b276 NB |
121 | } |
122 | } | |
123 | ||
124 | /* Append the standard include chain defined in cppdefault.c. */ | |
125 | static void | |
2b6dd222 JM |
126 | add_standard_paths (const char *sysroot, const char *iprefix, |
127 | const char *imultilib, int cxx_stdinc) | |
5793b276 NB |
128 | { |
129 | const struct default_include *p; | |
611f0729 | 130 | size_t len; |
5793b276 | 131 | |
611f0729 AM |
132 | if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0) |
133 | { | |
134 | /* Look for directories that start with the standard prefix. | |
89dbed81 | 135 | "Translate" them, i.e. replace /usr/local/lib/gcc... with |
611f0729 AM |
136 | IPREFIX and search them first. */ |
137 | for (p = cpp_include_defaults; p->fname; p++) | |
138 | { | |
139 | if (!p->cplusplus || cxx_stdinc) | |
140 | { | |
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)) | |
147 | { | |
148 | char *str = concat (iprefix, p->fname + len, NULL); | |
2b6dd222 JM |
149 | if (p->multilib && imultilib) |
150 | str = concat (str, dir_separator_str, imultilib, NULL); | |
b02398bd | 151 | add_path (str, SYSTEM, p->cxx_aware, false); |
611f0729 AM |
152 | } |
153 | } | |
154 | } | |
155 | } | |
5793b276 NB |
156 | |
157 | for (p = cpp_include_defaults; p->fname; p++) | |
158 | { | |
159 | if (!p->cplusplus || cxx_stdinc) | |
160 | { | |
161 | char *str; | |
162 | ||
163 | /* Should this directory start with the sysroot? */ | |
164 | if (sysroot && p->add_sysroot) | |
165 | str = concat (sysroot, p->fname, NULL); | |
5793b276 NB |
166 | else |
167 | str = update_path (p->fname, p->component); | |
168 | ||
2b6dd222 JM |
169 | if (p->multilib && imultilib) |
170 | str = concat (str, dir_separator_str, imultilib, NULL); | |
171 | ||
b02398bd | 172 | add_path (str, SYSTEM, p->cxx_aware, false); |
5793b276 NB |
173 | } |
174 | } | |
175 | } | |
176 | ||
177 | /* For each duplicate path in chain HEAD, keep just the first one. | |
178 | Remove each path in chain HEAD that also exists in chain SYSTEM. | |
179 | Set the NEXT pointer of the last path in the resulting chain to | |
180 | JOIN, unless it duplicates JOIN in which case the last path is | |
181 | removed. Return the head of the resulting chain. Any of HEAD, | |
182 | JOIN and SYSTEM can be NULL. */ | |
4bed3787 | 183 | |
8f9b4009 NB |
184 | static struct cpp_dir * |
185 | remove_duplicates (cpp_reader *pfile, struct cpp_dir *head, | |
186 | struct cpp_dir *system, struct cpp_dir *join, | |
35b1a6fa | 187 | int verbose) |
5793b276 | 188 | { |
8f9b4009 | 189 | struct cpp_dir **pcur, *tmp, *cur; |
5793b276 NB |
190 | struct stat st; |
191 | ||
192 | for (pcur = &head; *pcur; ) | |
193 | { | |
194 | int reason = REASON_QUIET; | |
195 | ||
196 | cur = *pcur; | |
5793b276 NB |
197 | |
198 | if (stat (cur->name, &st)) | |
199 | { | |
200 | /* Dirs that don't exist are silently ignored, unless verbose. */ | |
201 | if (errno != ENOENT) | |
0527bc4e | 202 | cpp_errno (pfile, CPP_DL_ERROR, cur->name); |
5793b276 | 203 | else |
b02398bd | 204 | { |
9cf737f8 | 205 | /* If -Wmissing-include-dirs is given, warn. */ |
b02398bd BE |
206 | cpp_options *opts = cpp_get_options (pfile); |
207 | if (opts->warn_missing_include_dirs && cur->user_supplied_p) | |
208 | cpp_errno (pfile, CPP_DL_WARNING, cur->name); | |
209 | reason = REASON_NOENT; | |
210 | } | |
5793b276 NB |
211 | } |
212 | else if (!S_ISDIR (st.st_mode)) | |
0527bc4e | 213 | cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0, |
5793b276 NB |
214 | "%s: not a directory", cur->name); |
215 | else | |
216 | { | |
217 | INO_T_COPY (cur->ino, st.st_ino); | |
218 | cur->dev = st.st_dev; | |
219 | ||
220 | /* Remove this one if it is in the system chain. */ | |
221 | reason = REASON_DUP_SYS; | |
222 | for (tmp = system; tmp; tmp = tmp->next) | |
8748c00f MR |
223 | if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev |
224 | && cur->construct == tmp->construct) | |
5793b276 NB |
225 | break; |
226 | ||
227 | if (!tmp) | |
228 | { | |
4d6922ee | 229 | /* Duplicate of something earlier in the same chain? */ |
5793b276 NB |
230 | reason = REASON_DUP; |
231 | for (tmp = head; tmp != cur; tmp = tmp->next) | |
8748c00f MR |
232 | if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev |
233 | && cur->construct == tmp->construct) | |
5793b276 NB |
234 | break; |
235 | ||
236 | if (tmp == cur | |
237 | /* Last in the chain and duplicate of JOIN? */ | |
238 | && !(cur->next == NULL && join | |
239 | && INO_T_EQ (cur->ino, join->ino) | |
8748c00f MR |
240 | && cur->dev == join->dev |
241 | && cur->construct == join->construct)) | |
5793b276 NB |
242 | { |
243 | /* Unique, so keep this directory. */ | |
244 | pcur = &cur->next; | |
245 | continue; | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | /* Remove this entry from the chain. */ | |
251 | *pcur = cur->next; | |
252 | free_path (cur, verbose ? reason: REASON_QUIET); | |
253 | } | |
254 | ||
255 | *pcur = join; | |
256 | return head; | |
257 | } | |
258 | ||
259 | /* Merge the four include chains together in the order quote, bracket, | |
260 | system, after. Remove duplicate dirs (as determined by | |
261 | INO_T_EQ()). | |
262 | ||
263 | We can't just merge the lists and then uniquify them because then | |
264 | we may lose directories from the <> search path that should be | |
4bed3787 MS |
265 | there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is |
266 | however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if | |
267 | written -iquote bar -Ifoo -Iquux. */ | |
268 | ||
5793b276 | 269 | static void |
35b1a6fa | 270 | merge_include_chains (cpp_reader *pfile, int verbose) |
5793b276 NB |
271 | { |
272 | /* Join the SYSTEM and AFTER chains. Remove duplicates in the | |
273 | resulting SYSTEM chain. */ | |
274 | if (heads[SYSTEM]) | |
275 | tails[SYSTEM]->next = heads[AFTER]; | |
276 | else | |
277 | heads[SYSTEM] = heads[AFTER]; | |
278 | heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose); | |
279 | ||
280 | /* Remove duplicates from BRACKET that are in itself or SYSTEM, and | |
281 | join it to SYSTEM. */ | |
282 | heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM], | |
283 | heads[SYSTEM], verbose); | |
284 | ||
285 | /* Remove duplicates from QUOTE that are in itself or SYSTEM, and | |
286 | join it to BRACKET. */ | |
287 | heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM], | |
288 | heads[BRACKET], verbose); | |
289 | ||
290 | /* If verbose, print the list of dirs to search. */ | |
291 | if (verbose) | |
292 | { | |
8f9b4009 | 293 | struct cpp_dir *p; |
5793b276 NB |
294 | |
295 | fprintf (stderr, _("#include \"...\" search starts here:\n")); | |
296 | for (p = heads[QUOTE];; p = p->next) | |
297 | { | |
298 | if (p == heads[BRACKET]) | |
299 | fprintf (stderr, _("#include <...> search starts here:\n")); | |
300 | if (!p) | |
301 | break; | |
302 | fprintf (stderr, " %s\n", p->name); | |
303 | } | |
304 | fprintf (stderr, _("End of search list.\n")); | |
305 | } | |
306 | } | |
307 | ||
308 | /* Use given -I paths for #include "..." but not #include <...>, and | |
309 | don't search the directory of the present file for #include "...". | |
310 | (Note that -I. -I- is not the same as the default setup; -I. uses | |
311 | the compiler's working dir.) */ | |
312 | void | |
35b1a6fa | 313 | split_quote_chain (void) |
5793b276 NB |
314 | { |
315 | heads[QUOTE] = heads[BRACKET]; | |
316 | tails[QUOTE] = tails[BRACKET]; | |
317 | heads[BRACKET] = NULL; | |
318 | tails[BRACKET] = NULL; | |
319 | /* This is NOT redundant. */ | |
320 | quote_ignores_source_dir = true; | |
321 | } | |
322 | ||
94d1613b MS |
323 | /* Add P to the chain specified by CHAIN. */ |
324 | ||
325 | void | |
326 | add_cpp_dir_path (cpp_dir *p, int chain) | |
327 | { | |
328 | if (tails[chain]) | |
329 | tails[chain]->next = p; | |
330 | else | |
331 | heads[chain] = p; | |
332 | tails[chain] = p; | |
333 | } | |
334 | ||
5793b276 NB |
335 | /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and |
336 | NUL-terminated. */ | |
337 | void | |
b02398bd | 338 | add_path (char *path, int chain, int cxx_aware, bool user_supplied_p) |
5793b276 | 339 | { |
94d1613b | 340 | cpp_dir *p; |
5793b276 | 341 | |
421563d5 DS |
342 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
343 | /* Convert all backslashes to slashes. The native CRT stat() | |
35fd3193 | 344 | function does not recognize a directory that ends in a backslash |
421563d5 DS |
345 | (unless it is a drive root dir, such "c:\"). Forward slashes, |
346 | trailing or otherwise, cause no problems for stat(). */ | |
347 | char* c; | |
348 | for (c = path; *c; c++) | |
349 | if (*c == '\\') *c = '/'; | |
350 | #endif | |
351 | ||
cceb1885 | 352 | p = XNEW (cpp_dir); |
5793b276 NB |
353 | p->next = NULL; |
354 | p->name = path; | |
355 | if (chain == SYSTEM || chain == AFTER) | |
82f6e3eb | 356 | p->sysp = 1 + !cxx_aware; |
5793b276 NB |
357 | else |
358 | p->sysp = 0; | |
94d1613b | 359 | p->construct = 0; |
b02398bd | 360 | p->user_supplied_p = user_supplied_p; |
5793b276 | 361 | |
b02398bd | 362 | add_cpp_dir_path (p, chain); |
5793b276 NB |
363 | } |
364 | ||
365 | /* Exported function to handle include chain merging, duplicate | |
366 | removal, and registration with cpplib. */ | |
367 | void | |
35b1a6fa | 368 | register_include_chains (cpp_reader *pfile, const char *sysroot, |
2b6dd222 JM |
369 | const char *iprefix, const char *imultilib, |
370 | int stdinc, int cxx_stdinc, int verbose) | |
5793b276 NB |
371 | { |
372 | static const char *const lang_env_vars[] = | |
373 | { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH", | |
374 | "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" }; | |
375 | cpp_options *cpp_opts = cpp_get_options (pfile); | |
376 | size_t idx = (cpp_opts->objc ? 2: 0); | |
377 | ||
378 | if (cpp_opts->cplusplus) | |
379 | idx++; | |
380 | else | |
381 | cxx_stdinc = false; | |
382 | ||
383 | /* CPATH and language-dependent environment variables may add to the | |
384 | include chain. */ | |
385 | add_env_var_paths ("CPATH", BRACKET); | |
386 | add_env_var_paths (lang_env_vars[idx], SYSTEM); | |
3dd53121 AP |
387 | |
388 | target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc); | |
5793b276 NB |
389 | |
390 | /* Finally chain on the standard directories. */ | |
391 | if (stdinc) | |
2b6dd222 | 392 | add_standard_paths (sysroot, iprefix, imultilib, cxx_stdinc); |
5793b276 | 393 | |
3dd53121 | 394 | target_c_incpath.extra_includes (sysroot, iprefix, stdinc); |
94d1613b | 395 | |
5793b276 NB |
396 | merge_include_chains (pfile, verbose); |
397 | ||
398 | cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET], | |
399 | quote_ignores_source_dir); | |
400 | } | |
3dd53121 AP |
401 | #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES) |
402 | static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED, | |
403 | const char *iprefix ATTRIBUTE_UNUSED, | |
404 | int stdinc ATTRIBUTE_UNUSED) | |
405 | { | |
406 | } | |
407 | #endif | |
94d1613b MS |
408 | |
409 | #ifndef TARGET_EXTRA_INCLUDES | |
3dd53121 | 410 | #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int |
94d1613b | 411 | #endif |
3dd53121 AP |
412 | #ifndef TARGET_EXTRA_PRE_INCLUDES |
413 | #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int | |
414 | #endif | |
415 | ||
416 | struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES }; | |
417 |