]>
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, | |
3 | 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. | |
4 | ||
5 | Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
94d1613b MS |
24 | #include "machmode.h" |
25 | #include "target.h" | |
5793b276 NB |
26 | #include "tm.h" |
27 | #include "cpplib.h" | |
28 | #include "prefix.h" | |
29 | #include "intl.h" | |
30 | #include "c-incpath.h" | |
31 | #include "cppdefault.h" | |
32 | ||
33 | /* Windows does not natively support inodes, and neither does MSDOS. | |
34 | Cygwin's emulation can generate non-unique inodes, so don't use it. | |
35 | VMS has non-numeric inodes. */ | |
36 | #ifdef VMS | |
37 | # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A))) | |
38 | # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC)) | |
39 | #else | |
40 | # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__ | |
41 | # define INO_T_EQ(A, B) 0 | |
42 | # else | |
43 | # define INO_T_EQ(A, B) ((A) == (B)) | |
44 | # endif | |
45 | # define INO_T_COPY(DEST, SRC) (DEST) = (SRC) | |
46 | #endif | |
47 | ||
35b1a6fa AJ |
48 | static void add_env_var_paths (const char *, int); |
49 | static void add_standard_paths (const char *, const char *, int); | |
8f9b4009 | 50 | static void free_path (struct cpp_dir *, int); |
35b1a6fa | 51 | static void merge_include_chains (cpp_reader *, int); |
8f9b4009 NB |
52 | static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *, |
53 | struct cpp_dir *, | |
54 | struct cpp_dir *, int); | |
5793b276 NB |
55 | |
56 | /* Include chains heads and tails. */ | |
8f9b4009 NB |
57 | static struct cpp_dir *heads[4]; |
58 | static struct cpp_dir *tails[4]; | |
5793b276 NB |
59 | static bool quote_ignores_source_dir; |
60 | enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS }; | |
61 | ||
62 | /* Free an element of the include chain, possibly giving a reason. */ | |
63 | static void | |
8f9b4009 | 64 | free_path (struct cpp_dir *path, int reason) |
5793b276 NB |
65 | { |
66 | switch (reason) | |
67 | { | |
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; | |
75 | ||
76 | case REASON_NOENT: | |
77 | fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), | |
78 | path->name); | |
79 | break; | |
80 | ||
81 | case REASON_QUIET: | |
82 | default: | |
83 | break; | |
84 | } | |
85 | ||
fad205ff | 86 | free (path->name); |
5793b276 NB |
87 | free (path); |
88 | } | |
89 | ||
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 | |
35b1a6fa | 93 | add_env_var_paths (const char *env_var, int chain) |
5793b276 NB |
94 | { |
95 | char *p, *q, *path; | |
96 | ||
97 | GET_ENVIRONMENT (q, env_var); | |
98 | ||
99 | if (!q) | |
100 | return; | |
101 | ||
102 | for (p = q; *q; p = q + 1) | |
103 | { | |
104 | q = p; | |
105 | while (*q != 0 && *q != PATH_SEPARATOR) | |
106 | q++; | |
107 | ||
108 | if (p == q) | |
109 | path = xstrdup ("."); | |
110 | else | |
111 | { | |
112 | path = xmalloc (q - p + 1); | |
113 | memcpy (path, p, q - p); | |
114 | path[q - p] = '\0'; | |
115 | } | |
116 | ||
117 | add_path (path, chain, chain == SYSTEM); | |
118 | } | |
119 | } | |
120 | ||
121 | /* Append the standard include chain defined in cppdefault.c. */ | |
122 | static void | |
35b1a6fa | 123 | add_standard_paths (const char *sysroot, const char *iprefix, int cxx_stdinc) |
5793b276 NB |
124 | { |
125 | const struct default_include *p; | |
611f0729 | 126 | size_t len; |
5793b276 | 127 | |
611f0729 AM |
128 | if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0) |
129 | { | |
130 | /* Look for directories that start with the standard prefix. | |
131 | "Translate" them, ie. replace /usr/local/lib/gcc... with | |
132 | IPREFIX and search them first. */ | |
133 | for (p = cpp_include_defaults; p->fname; p++) | |
134 | { | |
135 | if (!p->cplusplus || cxx_stdinc) | |
136 | { | |
137 | /* Should we be translating sysrooted dirs too? Assume | |
138 | that iprefix and sysroot are mutually exclusive, for | |
139 | now. */ | |
140 | if (sysroot && p->add_sysroot) | |
141 | continue; | |
142 | if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len)) | |
143 | { | |
144 | char *str = concat (iprefix, p->fname + len, NULL); | |
145 | add_path (str, SYSTEM, p->cxx_aware); | |
146 | } | |
147 | } | |
148 | } | |
149 | } | |
5793b276 NB |
150 | |
151 | for (p = cpp_include_defaults; p->fname; p++) | |
152 | { | |
153 | if (!p->cplusplus || cxx_stdinc) | |
154 | { | |
155 | char *str; | |
156 | ||
157 | /* Should this directory start with the sysroot? */ | |
158 | if (sysroot && p->add_sysroot) | |
159 | str = concat (sysroot, p->fname, NULL); | |
5793b276 NB |
160 | else |
161 | str = update_path (p->fname, p->component); | |
162 | ||
163 | add_path (str, SYSTEM, p->cxx_aware); | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | /* For each duplicate path in chain HEAD, keep just the first one. | |
169 | Remove each path in chain HEAD that also exists in chain SYSTEM. | |
170 | Set the NEXT pointer of the last path in the resulting chain to | |
171 | JOIN, unless it duplicates JOIN in which case the last path is | |
172 | removed. Return the head of the resulting chain. Any of HEAD, | |
173 | JOIN and SYSTEM can be NULL. */ | |
8f9b4009 NB |
174 | static struct cpp_dir * |
175 | remove_duplicates (cpp_reader *pfile, struct cpp_dir *head, | |
176 | struct cpp_dir *system, struct cpp_dir *join, | |
35b1a6fa | 177 | int verbose) |
5793b276 | 178 | { |
8f9b4009 | 179 | struct cpp_dir **pcur, *tmp, *cur; |
5793b276 NB |
180 | struct stat st; |
181 | ||
182 | for (pcur = &head; *pcur; ) | |
183 | { | |
184 | int reason = REASON_QUIET; | |
185 | ||
186 | cur = *pcur; | |
5793b276 NB |
187 | |
188 | if (stat (cur->name, &st)) | |
189 | { | |
190 | /* Dirs that don't exist are silently ignored, unless verbose. */ | |
191 | if (errno != ENOENT) | |
0527bc4e | 192 | cpp_errno (pfile, CPP_DL_ERROR, cur->name); |
5793b276 NB |
193 | else |
194 | reason = REASON_NOENT; | |
195 | } | |
196 | else if (!S_ISDIR (st.st_mode)) | |
0527bc4e | 197 | cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0, |
5793b276 NB |
198 | "%s: not a directory", cur->name); |
199 | else | |
200 | { | |
201 | INO_T_COPY (cur->ino, st.st_ino); | |
202 | cur->dev = st.st_dev; | |
203 | ||
204 | /* Remove this one if it is in the system chain. */ | |
205 | reason = REASON_DUP_SYS; | |
206 | for (tmp = system; tmp; tmp = tmp->next) | |
207 | if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev) | |
208 | break; | |
209 | ||
210 | if (!tmp) | |
211 | { | |
4d6922ee | 212 | /* Duplicate of something earlier in the same chain? */ |
5793b276 NB |
213 | reason = REASON_DUP; |
214 | for (tmp = head; tmp != cur; tmp = tmp->next) | |
215 | if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev) | |
216 | break; | |
217 | ||
218 | if (tmp == cur | |
219 | /* Last in the chain and duplicate of JOIN? */ | |
220 | && !(cur->next == NULL && join | |
221 | && INO_T_EQ (cur->ino, join->ino) | |
222 | && cur->dev == join->dev)) | |
223 | { | |
224 | /* Unique, so keep this directory. */ | |
225 | pcur = &cur->next; | |
226 | continue; | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | /* Remove this entry from the chain. */ | |
232 | *pcur = cur->next; | |
233 | free_path (cur, verbose ? reason: REASON_QUIET); | |
234 | } | |
235 | ||
236 | *pcur = join; | |
237 | return head; | |
238 | } | |
239 | ||
240 | /* Merge the four include chains together in the order quote, bracket, | |
241 | system, after. Remove duplicate dirs (as determined by | |
242 | INO_T_EQ()). | |
243 | ||
244 | We can't just merge the lists and then uniquify them because then | |
245 | we may lose directories from the <> search path that should be | |
246 | there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however safe | |
247 | to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo | |
248 | -Iquux. */ | |
249 | static void | |
35b1a6fa | 250 | merge_include_chains (cpp_reader *pfile, int verbose) |
5793b276 NB |
251 | { |
252 | /* Join the SYSTEM and AFTER chains. Remove duplicates in the | |
253 | resulting SYSTEM chain. */ | |
254 | if (heads[SYSTEM]) | |
255 | tails[SYSTEM]->next = heads[AFTER]; | |
256 | else | |
257 | heads[SYSTEM] = heads[AFTER]; | |
258 | heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose); | |
259 | ||
260 | /* Remove duplicates from BRACKET that are in itself or SYSTEM, and | |
261 | join it to SYSTEM. */ | |
262 | heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM], | |
263 | heads[SYSTEM], verbose); | |
264 | ||
265 | /* Remove duplicates from QUOTE that are in itself or SYSTEM, and | |
266 | join it to BRACKET. */ | |
267 | heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM], | |
268 | heads[BRACKET], verbose); | |
269 | ||
270 | /* If verbose, print the list of dirs to search. */ | |
271 | if (verbose) | |
272 | { | |
8f9b4009 | 273 | struct cpp_dir *p; |
5793b276 NB |
274 | |
275 | fprintf (stderr, _("#include \"...\" search starts here:\n")); | |
276 | for (p = heads[QUOTE];; p = p->next) | |
277 | { | |
278 | if (p == heads[BRACKET]) | |
279 | fprintf (stderr, _("#include <...> search starts here:\n")); | |
280 | if (!p) | |
281 | break; | |
282 | fprintf (stderr, " %s\n", p->name); | |
283 | } | |
284 | fprintf (stderr, _("End of search list.\n")); | |
285 | } | |
286 | } | |
287 | ||
288 | /* Use given -I paths for #include "..." but not #include <...>, and | |
289 | don't search the directory of the present file for #include "...". | |
290 | (Note that -I. -I- is not the same as the default setup; -I. uses | |
291 | the compiler's working dir.) */ | |
292 | void | |
35b1a6fa | 293 | split_quote_chain (void) |
5793b276 NB |
294 | { |
295 | heads[QUOTE] = heads[BRACKET]; | |
296 | tails[QUOTE] = tails[BRACKET]; | |
297 | heads[BRACKET] = NULL; | |
298 | tails[BRACKET] = NULL; | |
299 | /* This is NOT redundant. */ | |
300 | quote_ignores_source_dir = true; | |
301 | } | |
302 | ||
94d1613b MS |
303 | /* Add P to the chain specified by CHAIN. */ |
304 | ||
305 | void | |
306 | add_cpp_dir_path (cpp_dir *p, int chain) | |
307 | { | |
308 | if (tails[chain]) | |
309 | tails[chain]->next = p; | |
310 | else | |
311 | heads[chain] = p; | |
312 | tails[chain] = p; | |
313 | } | |
314 | ||
5793b276 NB |
315 | /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and |
316 | NUL-terminated. */ | |
317 | void | |
35b1a6fa | 318 | add_path (char *path, int chain, int cxx_aware) |
5793b276 | 319 | { |
94d1613b | 320 | cpp_dir *p; |
5793b276 | 321 | |
94d1613b | 322 | p = xmalloc (sizeof (cpp_dir)); |
5793b276 NB |
323 | p->next = NULL; |
324 | p->name = path; | |
325 | if (chain == SYSTEM || chain == AFTER) | |
82f6e3eb | 326 | p->sysp = 1 + !cxx_aware; |
5793b276 NB |
327 | else |
328 | p->sysp = 0; | |
94d1613b | 329 | p->construct = 0; |
5793b276 NB |
330 | |
331 | if (tails[chain]) | |
332 | tails[chain]->next = p; | |
333 | else | |
334 | heads[chain] = p; | |
335 | tails[chain] = p; | |
336 | } | |
337 | ||
338 | /* Exported function to handle include chain merging, duplicate | |
339 | removal, and registration with cpplib. */ | |
340 | void | |
35b1a6fa AJ |
341 | register_include_chains (cpp_reader *pfile, const char *sysroot, |
342 | const char *iprefix, int stdinc, int cxx_stdinc, | |
343 | int verbose) | |
5793b276 NB |
344 | { |
345 | static const char *const lang_env_vars[] = | |
346 | { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH", | |
347 | "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" }; | |
348 | cpp_options *cpp_opts = cpp_get_options (pfile); | |
349 | size_t idx = (cpp_opts->objc ? 2: 0); | |
350 | ||
351 | if (cpp_opts->cplusplus) | |
352 | idx++; | |
353 | else | |
354 | cxx_stdinc = false; | |
355 | ||
356 | /* CPATH and language-dependent environment variables may add to the | |
357 | include chain. */ | |
358 | add_env_var_paths ("CPATH", BRACKET); | |
359 | add_env_var_paths (lang_env_vars[idx], SYSTEM); | |
360 | ||
361 | /* Finally chain on the standard directories. */ | |
362 | if (stdinc) | |
363 | add_standard_paths (sysroot, iprefix, cxx_stdinc); | |
364 | ||
94d1613b MS |
365 | target_c_incpath.extra_includes (stdinc); |
366 | ||
5793b276 NB |
367 | merge_include_chains (pfile, verbose); |
368 | ||
369 | cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET], | |
370 | quote_ignores_source_dir); | |
371 | } | |
94d1613b MS |
372 | |
373 | #ifndef TARGET_EXTRA_INCLUDES | |
374 | static void hook_void_int(int u ATTRIBUTE_UNUSED) { } | |
375 | ||
376 | struct target_c_incpath_s target_c_incpath = { hook_void_int }; | |
377 | #endif |