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.
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "coretypes.h"
28 #include "c-incpath.h"
29 #include "cppdefault.h"
31 /* Windows does not natively support inodes, and neither does MSDOS.
32 Cygwin's emulation can generate non-unique inodes, so don't use it.
33 VMS has non-numeric inodes. */
35 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
36 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
38 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
39 # define INO_T_EQ(A, B) 0
41 # define INO_T_EQ(A, B) ((A) == (B))
43 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
46 static void add_env_var_paths
PARAMS ((const char *, int));
47 static void add_standard_paths
PARAMS ((const char *, const char *, int));
48 static void free_path
PARAMS ((struct cpp_path
*, int));
49 static void merge_include_chains
PARAMS ((cpp_reader
*, int));
50 static struct cpp_path
*
51 remove_duplicates
PARAMS ((cpp_reader
*, struct cpp_path
*,
52 struct cpp_path
*, struct cpp_path
*, int));
54 /* Include chains heads and tails. */
55 static struct cpp_path
*heads
[4];
56 static struct cpp_path
*tails
[4];
57 static bool quote_ignores_source_dir
;
58 enum { REASON_QUIET
= 0, REASON_NOENT
, REASON_DUP
, REASON_DUP_SYS
};
60 /* Free an element of the include chain, possibly giving a reason. */
62 free_path (path
, reason
)
63 struct cpp_path
*path
;
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 (env_var
, chain
)
99 GET_ENVIRONMENT (q
, env_var
);
104 for (p
= q
; *q
; p
= q
+ 1)
107 while (*q
!= 0 && *q
!= PATH_SEPARATOR
)
111 path
= xstrdup (".");
114 path
= xmalloc (q
- p
+ 1);
115 memcpy (path
, p
, q
- p
);
119 add_path (path
, chain
, chain
== SYSTEM
);
123 /* Append the standard include chain defined in cppdefault.c. */
125 add_standard_paths (sysroot
, iprefix
, cxx_stdinc
)
126 const char *sysroot
, *iprefix
;
129 const struct default_include
*p
;
132 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0)
134 /* Look for directories that start with the standard prefix.
135 "Translate" them, ie. replace /usr/local/lib/gcc... with
136 IPREFIX and search them first. */
137 for (p
= cpp_include_defaults
; p
->fname
; p
++)
139 if (!p
->cplusplus
|| cxx_stdinc
)
141 /* Should we be translating sysrooted dirs too? Assume
142 that iprefix and sysroot are mutually exclusive, for
144 if (sysroot
&& p
->add_sysroot
)
146 if (!strncmp (p
->fname
, cpp_GCC_INCLUDE_DIR
, len
))
148 char *str
= concat (iprefix
, p
->fname
+ len
, NULL
);
149 add_path (str
, SYSTEM
, p
->cxx_aware
);
155 for (p
= cpp_include_defaults
; p
->fname
; p
++)
157 if (!p
->cplusplus
|| cxx_stdinc
)
161 /* Should this directory start with the sysroot? */
162 if (sysroot
&& p
->add_sysroot
)
163 str
= concat (sysroot
, p
->fname
, NULL
);
165 str
= update_path (p
->fname
, p
->component
);
167 add_path (str
, SYSTEM
, p
->cxx_aware
);
172 /* For each duplicate path in chain HEAD, keep just the first one.
173 Remove each path in chain HEAD that also exists in chain SYSTEM.
174 Set the NEXT pointer of the last path in the resulting chain to
175 JOIN, unless it duplicates JOIN in which case the last path is
176 removed. Return the head of the resulting chain. Any of HEAD,
177 JOIN and SYSTEM can be NULL. */
178 static struct cpp_path
*
179 remove_duplicates (pfile
, head
, system
, join
, verbose
)
181 struct cpp_path
*head
;
182 struct cpp_path
*system
;
183 struct cpp_path
*join
;
186 struct cpp_path
**pcur
, *tmp
, *cur
;
189 for (pcur
= &head
; *pcur
; )
191 int reason
= REASON_QUIET
;
194 cpp_simplify_path (cur
->name
);
196 if (stat (cur
->name
, &st
))
198 /* Dirs that don't exist are silently ignored, unless verbose. */
200 cpp_errno (pfile
, DL_ERROR
, cur
->name
);
202 reason
= REASON_NOENT
;
204 else if (!S_ISDIR (st
.st_mode
))
205 cpp_error_with_line (pfile
, DL_ERROR
, 0, 0,
206 "%s: not a directory", cur
->name
);
209 INO_T_COPY (cur
->ino
, st
.st_ino
);
210 cur
->dev
= st
.st_dev
;
212 /* Remove this one if it is in the system chain. */
213 reason
= REASON_DUP_SYS
;
214 for (tmp
= system
; tmp
; tmp
= tmp
->next
)
215 if (INO_T_EQ (tmp
->ino
, cur
->ino
) && tmp
->dev
== cur
->dev
)
220 /* Dupicate 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
)
227 /* Last in the chain and duplicate of JOIN? */
228 && !(cur
->next
== NULL
&& join
229 && INO_T_EQ (cur
->ino
, join
->ino
)
230 && cur
->dev
== join
->dev
))
232 /* Unique, so keep this directory. */
239 /* Remove this entry from the chain. */
241 free_path (cur
, verbose
? reason
: REASON_QUIET
);
248 /* Merge the four include chains together in the order quote, bracket,
249 system, after. Remove duplicate dirs (as determined by
252 We can't just merge the lists and then uniquify them because then
253 we may lose directories from the <> search path that should be
254 there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however safe
255 to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
258 merge_include_chains (pfile
, verbose
)
262 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
263 resulting SYSTEM chain. */
265 tails
[SYSTEM
]->next
= heads
[AFTER
];
267 heads
[SYSTEM
] = heads
[AFTER
];
268 heads
[SYSTEM
] = remove_duplicates (pfile
, heads
[SYSTEM
], 0, 0, verbose
);
270 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
271 join it to SYSTEM. */
272 heads
[BRACKET
] = remove_duplicates (pfile
, heads
[BRACKET
], heads
[SYSTEM
],
273 heads
[SYSTEM
], verbose
);
275 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
276 join it to BRACKET. */
277 heads
[QUOTE
] = remove_duplicates (pfile
, heads
[QUOTE
], heads
[SYSTEM
],
278 heads
[BRACKET
], verbose
);
280 /* If verbose, print the list of dirs to search. */
285 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
286 for (p
= heads
[QUOTE
];; p
= p
->next
)
288 if (p
== heads
[BRACKET
])
289 fprintf (stderr
, _("#include <...> search starts here:\n"));
292 fprintf (stderr
, " %s\n", p
->name
);
294 fprintf (stderr
, _("End of search list.\n"));
298 /* Use given -I paths for #include "..." but not #include <...>, and
299 don't search the directory of the present file for #include "...".
300 (Note that -I. -I- is not the same as the default setup; -I. uses
301 the compiler's working dir.) */
305 heads
[QUOTE
] = heads
[BRACKET
];
306 tails
[QUOTE
] = tails
[BRACKET
];
307 heads
[BRACKET
] = NULL
;
308 tails
[BRACKET
] = NULL
;
309 /* This is NOT redundant. */
310 quote_ignores_source_dir
= true;
313 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
316 add_path (path
, chain
, cxx_aware
)
323 p
= (struct cpp_path
*) xmalloc (sizeof (struct cpp_path
));
326 if (chain
== SYSTEM
|| chain
== AFTER
)
327 p
->sysp
= 1 + !cxx_aware
;
332 tails
[chain
]->next
= p
;
338 /* Exported function to handle include chain merging, duplicate
339 removal, and registration with cpplib. */
341 register_include_chains (pfile
, sysroot
, iprefix
,
342 stdinc
, cxx_stdinc
, verbose
)
344 const char *sysroot
, *iprefix
;
345 int stdinc
, cxx_stdinc
, verbose
;
347 static const char *const lang_env_vars
[] =
348 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
349 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
350 cpp_options
*cpp_opts
= cpp_get_options (pfile
);
351 size_t idx
= (cpp_opts
->objc
? 2: 0);
353 if (cpp_opts
->cplusplus
)
358 /* CPATH and language-dependent environment variables may add to the
360 add_env_var_paths ("CPATH", BRACKET
);
361 add_env_var_paths (lang_env_vars
[idx
], SYSTEM
);
363 /* Finally chain on the standard directories. */
365 add_standard_paths (sysroot
, iprefix
, cxx_stdinc
);
367 merge_include_chains (pfile
, verbose
);
369 cpp_set_include_chains (pfile
, heads
[QUOTE
], heads
[BRACKET
],
370 quote_ignores_source_dir
);