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 Free Software Foundation, Inc.
5 Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
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
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "coretypes.h"
30 #include "c-incpath.h"
31 #include "cppdefault.h"
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. */
37 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
38 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
40 # if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__
41 # define INO_T_EQ(A, B) 0
43 # define INO_T_EQ(A, B) ((A) == (B))
45 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
48 static void add_env_var_paths (const char *, int);
49 static void add_standard_paths (const char *, const char *, int);
50 static void free_path (struct cpp_dir
*, int);
51 static void merge_include_chains (cpp_reader
*, int);
52 static struct cpp_dir
*remove_duplicates (cpp_reader
*, struct cpp_dir
*,
54 struct cpp_dir
*, int);
56 /* Include chains heads and tails. */
57 static struct cpp_dir
*heads
[4];
58 static struct cpp_dir
*tails
[4];
59 static bool quote_ignores_source_dir
;
60 enum { REASON_QUIET
= 0, REASON_NOENT
, REASON_DUP
, REASON_DUP_SYS
};
62 /* Free an element of the include chain, possibly giving a reason. */
64 free_path (struct cpp_dir
*path
, int reason
)
70 fprintf (stderr
, _("ignoring duplicate directory \"%s\"\n"), path
->name
);
71 if (reason
== REASON_DUP_SYS
)
73 _(" as it is a non-system directory that duplicates a system directory\n"));
77 fprintf (stderr
, _("ignoring nonexistent directory \"%s\"\n"),
90 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
91 append all the names to the search path CHAIN. */
93 add_env_var_paths (const char *env_var
, int chain
)
97 GET_ENVIRONMENT (q
, env_var
);
102 for (p
= q
; *q
; p
= q
+ 1)
105 while (*q
!= 0 && *q
!= PATH_SEPARATOR
)
109 path
= xstrdup (".");
112 path
= xmalloc (q
- p
+ 1);
113 memcpy (path
, p
, q
- p
);
117 add_path (path
, chain
, chain
== SYSTEM
, false);
121 /* Append the standard include chain defined in cppdefault.c. */
123 add_standard_paths (const char *sysroot
, const char *iprefix
, int cxx_stdinc
)
125 const struct default_include
*p
;
128 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0)
130 /* Look for directories that start with the standard prefix.
131 "Translate" them, i.e. replace /usr/local/lib/gcc... with
132 IPREFIX and search them first. */
133 for (p
= cpp_include_defaults
; p
->fname
; p
++)
135 if (!p
->cplusplus
|| cxx_stdinc
)
137 /* Should we be translating sysrooted dirs too? Assume
138 that iprefix and sysroot are mutually exclusive, for
140 if (sysroot
&& p
->add_sysroot
)
142 if (!strncmp (p
->fname
, cpp_GCC_INCLUDE_DIR
, len
))
144 char *str
= concat (iprefix
, p
->fname
+ len
, NULL
);
145 add_path (str
, SYSTEM
, p
->cxx_aware
, false);
151 for (p
= cpp_include_defaults
; p
->fname
; p
++)
153 if (!p
->cplusplus
|| cxx_stdinc
)
157 /* Should this directory start with the sysroot? */
158 if (sysroot
&& p
->add_sysroot
)
159 str
= concat (sysroot
, p
->fname
, NULL
);
161 str
= update_path (p
->fname
, p
->component
);
163 add_path (str
, SYSTEM
, p
->cxx_aware
, false);
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. */
175 static struct cpp_dir
*
176 remove_duplicates (cpp_reader
*pfile
, struct cpp_dir
*head
,
177 struct cpp_dir
*system
, struct cpp_dir
*join
,
180 struct cpp_dir
**pcur
, *tmp
, *cur
;
183 for (pcur
= &head
; *pcur
; )
185 int reason
= REASON_QUIET
;
189 if (stat (cur
->name
, &st
))
191 /* Dirs that don't exist are silently ignored, unless verbose. */
193 cpp_errno (pfile
, CPP_DL_ERROR
, cur
->name
);
196 /* If -Wmissing-include-dirs is given, warn. */
197 cpp_options
*opts
= cpp_get_options (pfile
);
198 if (opts
->warn_missing_include_dirs
&& cur
->user_supplied_p
)
199 cpp_errno (pfile
, CPP_DL_WARNING
, cur
->name
);
200 reason
= REASON_NOENT
;
203 else if (!S_ISDIR (st
.st_mode
))
204 cpp_error_with_line (pfile
, CPP_DL_ERROR
, 0, 0,
205 "%s: not a directory", cur
->name
);
208 INO_T_COPY (cur
->ino
, st
.st_ino
);
209 cur
->dev
= st
.st_dev
;
211 /* Remove this one if it is in the system chain. */
212 reason
= REASON_DUP_SYS
;
213 for (tmp
= system
; tmp
; tmp
= tmp
->next
)
214 if (INO_T_EQ (tmp
->ino
, cur
->ino
) && tmp
->dev
== cur
->dev
215 && cur
->construct
== tmp
->construct
)
220 /* Duplicate of something earlier in the same chain? */
222 for (tmp
= head
; tmp
!= cur
; tmp
= tmp
->next
)
223 if (INO_T_EQ (cur
->ino
, tmp
->ino
) && cur
->dev
== tmp
->dev
224 && cur
->construct
== tmp
->construct
)
228 /* Last in the chain and duplicate of JOIN? */
229 && !(cur
->next
== NULL
&& join
230 && INO_T_EQ (cur
->ino
, join
->ino
)
231 && cur
->dev
== join
->dev
232 && cur
->construct
== join
->construct
))
234 /* Unique, so keep this directory. */
241 /* Remove this entry from the chain. */
243 free_path (cur
, verbose
? reason
: REASON_QUIET
);
250 /* Merge the four include chains together in the order quote, bracket,
251 system, after. Remove duplicate dirs (as determined by
254 We can't just merge the lists and then uniquify them because then
255 we may lose directories from the <> search path that should be
256 there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is
257 however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
258 written -iquote bar -Ifoo -Iquux. */
261 merge_include_chains (cpp_reader
*pfile
, int verbose
)
263 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
264 resulting SYSTEM chain. */
266 tails
[SYSTEM
]->next
= heads
[AFTER
];
268 heads
[SYSTEM
] = heads
[AFTER
];
269 heads
[SYSTEM
] = remove_duplicates (pfile
, heads
[SYSTEM
], 0, 0, verbose
);
271 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
272 join it to SYSTEM. */
273 heads
[BRACKET
] = remove_duplicates (pfile
, heads
[BRACKET
], heads
[SYSTEM
],
274 heads
[SYSTEM
], verbose
);
276 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
277 join it to BRACKET. */
278 heads
[QUOTE
] = remove_duplicates (pfile
, heads
[QUOTE
], heads
[SYSTEM
],
279 heads
[BRACKET
], verbose
);
281 /* If verbose, print the list of dirs to search. */
286 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
287 for (p
= heads
[QUOTE
];; p
= p
->next
)
289 if (p
== heads
[BRACKET
])
290 fprintf (stderr
, _("#include <...> search starts here:\n"));
293 fprintf (stderr
, " %s\n", p
->name
);
295 fprintf (stderr
, _("End of search list.\n"));
299 /* Use given -I paths for #include "..." but not #include <...>, and
300 don't search the directory of the present file for #include "...".
301 (Note that -I. -I- is not the same as the default setup; -I. uses
302 the compiler's working dir.) */
304 split_quote_chain (void)
306 heads
[QUOTE
] = heads
[BRACKET
];
307 tails
[QUOTE
] = tails
[BRACKET
];
308 heads
[BRACKET
] = NULL
;
309 tails
[BRACKET
] = NULL
;
310 /* This is NOT redundant. */
311 quote_ignores_source_dir
= true;
314 /* Add P to the chain specified by CHAIN. */
317 add_cpp_dir_path (cpp_dir
*p
, int chain
)
320 tails
[chain
]->next
= p
;
326 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
329 add_path (char *path
, int chain
, int cxx_aware
, bool user_supplied_p
)
333 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
334 /* Convert all backslashes to slashes. The native CRT stat()
335 function does not recognize a directory that ends in a backslash
336 (unless it is a drive root dir, such "c:\"). Forward slashes,
337 trailing or otherwise, cause no problems for stat(). */
339 for (c
= path
; *c
; c
++)
340 if (*c
== '\\') *c
= '/';
343 p
= xmalloc (sizeof (cpp_dir
));
346 if (chain
== SYSTEM
|| chain
== AFTER
)
347 p
->sysp
= 1 + !cxx_aware
;
351 p
->user_supplied_p
= user_supplied_p
;
353 add_cpp_dir_path (p
, chain
);
356 /* Exported function to handle include chain merging, duplicate
357 removal, and registration with cpplib. */
359 register_include_chains (cpp_reader
*pfile
, const char *sysroot
,
360 const char *iprefix
, int stdinc
, int cxx_stdinc
,
363 static const char *const lang_env_vars
[] =
364 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
365 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
366 cpp_options
*cpp_opts
= cpp_get_options (pfile
);
367 size_t idx
= (cpp_opts
->objc
? 2: 0);
369 if (cpp_opts
->cplusplus
)
374 /* CPATH and language-dependent environment variables may add to the
376 add_env_var_paths ("CPATH", BRACKET
);
377 add_env_var_paths (lang_env_vars
[idx
], SYSTEM
);
379 target_c_incpath
.extra_pre_includes (sysroot
, iprefix
, stdinc
);
381 /* Finally chain on the standard directories. */
383 add_standard_paths (sysroot
, iprefix
, cxx_stdinc
);
385 target_c_incpath
.extra_includes (sysroot
, iprefix
, stdinc
);
387 merge_include_chains (pfile
, verbose
);
389 cpp_set_include_chains (pfile
, heads
[QUOTE
], heads
[BRACKET
],
390 quote_ignores_source_dir
);
392 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
393 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED
,
394 const char *iprefix ATTRIBUTE_UNUSED
,
395 int stdinc ATTRIBUTE_UNUSED
)
400 #ifndef TARGET_EXTRA_INCLUDES
401 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
403 #ifndef TARGET_EXTRA_PRE_INCLUDES
404 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
407 struct target_c_incpath_s target_c_incpath
= { TARGET_EXTRA_PRE_INCLUDES
, TARGET_EXTRA_INCLUDES
};