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 (const char *, int);
47 static void add_standard_paths (const char *, const char *, int);
48 static void free_path (struct cpp_dir
*, int);
49 static void merge_include_chains (cpp_reader
*, int);
50 static struct cpp_dir
*remove_duplicates (cpp_reader
*, struct cpp_dir
*,
52 struct cpp_dir
*, int);
54 /* Include chains heads and tails. */
55 static struct cpp_dir
*heads
[4];
56 static struct cpp_dir
*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 (struct cpp_dir
*path
, int reason
)
68 fprintf (stderr
, _("ignoring duplicate directory \"%s\"\n"), path
->name
);
69 if (reason
== REASON_DUP_SYS
)
71 _(" as it is a non-system directory that duplicates a system directory\n"));
75 fprintf (stderr
, _("ignoring nonexistent directory \"%s\"\n"),
88 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
89 append all the names to the search path CHAIN. */
91 add_env_var_paths (const char *env_var
, int chain
)
95 GET_ENVIRONMENT (q
, env_var
);
100 for (p
= q
; *q
; p
= q
+ 1)
103 while (*q
!= 0 && *q
!= PATH_SEPARATOR
)
107 path
= xstrdup (".");
110 path
= xmalloc (q
- p
+ 1);
111 memcpy (path
, p
, q
- p
);
115 add_path (path
, chain
, chain
== SYSTEM
);
119 /* Append the standard include chain defined in cppdefault.c. */
121 add_standard_paths (const char *sysroot
, const char *iprefix
, int cxx_stdinc
)
123 const struct default_include
*p
;
126 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0)
128 /* Look for directories that start with the standard prefix.
129 "Translate" them, ie. replace /usr/local/lib/gcc... with
130 IPREFIX and search them first. */
131 for (p
= cpp_include_defaults
; p
->fname
; p
++)
133 if (!p
->cplusplus
|| cxx_stdinc
)
135 /* Should we be translating sysrooted dirs too? Assume
136 that iprefix and sysroot are mutually exclusive, for
138 if (sysroot
&& p
->add_sysroot
)
140 if (!strncmp (p
->fname
, cpp_GCC_INCLUDE_DIR
, len
))
142 char *str
= concat (iprefix
, p
->fname
+ len
, NULL
);
143 add_path (str
, SYSTEM
, p
->cxx_aware
);
149 for (p
= cpp_include_defaults
; p
->fname
; p
++)
151 if (!p
->cplusplus
|| cxx_stdinc
)
155 /* Should this directory start with the sysroot? */
156 if (sysroot
&& p
->add_sysroot
)
157 str
= concat (sysroot
, p
->fname
, NULL
);
159 str
= update_path (p
->fname
, p
->component
);
161 add_path (str
, SYSTEM
, p
->cxx_aware
);
166 /* For each duplicate path in chain HEAD, keep just the first one.
167 Remove each path in chain HEAD that also exists in chain SYSTEM.
168 Set the NEXT pointer of the last path in the resulting chain to
169 JOIN, unless it duplicates JOIN in which case the last path is
170 removed. Return the head of the resulting chain. Any of HEAD,
171 JOIN and SYSTEM can be NULL. */
172 static struct cpp_dir
*
173 remove_duplicates (cpp_reader
*pfile
, struct cpp_dir
*head
,
174 struct cpp_dir
*system
, struct cpp_dir
*join
,
177 struct cpp_dir
**pcur
, *tmp
, *cur
;
180 for (pcur
= &head
; *pcur
; )
182 int reason
= REASON_QUIET
;
186 if (stat (cur
->name
, &st
))
188 /* Dirs that don't exist are silently ignored, unless verbose. */
190 cpp_errno (pfile
, CPP_DL_ERROR
, cur
->name
);
192 reason
= REASON_NOENT
;
194 else if (!S_ISDIR (st
.st_mode
))
195 cpp_error_with_line (pfile
, CPP_DL_ERROR
, 0, 0,
196 "%s: not a directory", cur
->name
);
199 INO_T_COPY (cur
->ino
, st
.st_ino
);
200 cur
->dev
= st
.st_dev
;
202 /* Remove this one if it is in the system chain. */
203 reason
= REASON_DUP_SYS
;
204 for (tmp
= system
; tmp
; tmp
= tmp
->next
)
205 if (INO_T_EQ (tmp
->ino
, cur
->ino
) && tmp
->dev
== cur
->dev
)
210 /* Duplicate of something earlier in the same chain? */
212 for (tmp
= head
; tmp
!= cur
; tmp
= tmp
->next
)
213 if (INO_T_EQ (cur
->ino
, tmp
->ino
) && cur
->dev
== tmp
->dev
)
217 /* Last in the chain and duplicate of JOIN? */
218 && !(cur
->next
== NULL
&& join
219 && INO_T_EQ (cur
->ino
, join
->ino
)
220 && cur
->dev
== join
->dev
))
222 /* Unique, so keep this directory. */
229 /* Remove this entry from the chain. */
231 free_path (cur
, verbose
? reason
: REASON_QUIET
);
238 /* Merge the four include chains together in the order quote, bracket,
239 system, after. Remove duplicate dirs (as determined by
242 We can't just merge the lists and then uniquify them because then
243 we may lose directories from the <> search path that should be
244 there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however safe
245 to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
248 merge_include_chains (cpp_reader
*pfile
, int verbose
)
250 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
251 resulting SYSTEM chain. */
253 tails
[SYSTEM
]->next
= heads
[AFTER
];
255 heads
[SYSTEM
] = heads
[AFTER
];
256 heads
[SYSTEM
] = remove_duplicates (pfile
, heads
[SYSTEM
], 0, 0, verbose
);
258 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
259 join it to SYSTEM. */
260 heads
[BRACKET
] = remove_duplicates (pfile
, heads
[BRACKET
], heads
[SYSTEM
],
261 heads
[SYSTEM
], verbose
);
263 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
264 join it to BRACKET. */
265 heads
[QUOTE
] = remove_duplicates (pfile
, heads
[QUOTE
], heads
[SYSTEM
],
266 heads
[BRACKET
], verbose
);
268 /* If verbose, print the list of dirs to search. */
273 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
274 for (p
= heads
[QUOTE
];; p
= p
->next
)
276 if (p
== heads
[BRACKET
])
277 fprintf (stderr
, _("#include <...> search starts here:\n"));
280 fprintf (stderr
, " %s\n", p
->name
);
282 fprintf (stderr
, _("End of search list.\n"));
286 /* Use given -I paths for #include "..." but not #include <...>, and
287 don't search the directory of the present file for #include "...".
288 (Note that -I. -I- is not the same as the default setup; -I. uses
289 the compiler's working dir.) */
291 split_quote_chain (void)
293 heads
[QUOTE
] = heads
[BRACKET
];
294 tails
[QUOTE
] = tails
[BRACKET
];
295 heads
[BRACKET
] = NULL
;
296 tails
[BRACKET
] = NULL
;
297 /* This is NOT redundant. */
298 quote_ignores_source_dir
= true;
301 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
304 add_path (char *path
, int chain
, int cxx_aware
)
308 p
= xmalloc (sizeof (struct cpp_dir
));
311 if (chain
== SYSTEM
|| chain
== AFTER
)
312 p
->sysp
= 1 + !cxx_aware
;
317 tails
[chain
]->next
= p
;
323 /* Exported function to handle include chain merging, duplicate
324 removal, and registration with cpplib. */
326 register_include_chains (cpp_reader
*pfile
, const char *sysroot
,
327 const char *iprefix
, int stdinc
, int cxx_stdinc
,
330 static const char *const lang_env_vars
[] =
331 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
332 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
333 cpp_options
*cpp_opts
= cpp_get_options (pfile
);
334 size_t idx
= (cpp_opts
->objc
? 2: 0);
336 if (cpp_opts
->cplusplus
)
341 /* CPATH and language-dependent environment variables may add to the
343 add_env_var_paths ("CPATH", BRACKET
);
344 add_env_var_paths (lang_env_vars
[idx
], SYSTEM
);
346 /* Finally chain on the standard directories. */
348 add_standard_paths (sysroot
, iprefix
, cxx_stdinc
);
350 merge_include_chains (pfile
, verbose
);
352 cpp_set_include_chains (pfile
, heads
[QUOTE
], heads
[BRACKET
],
353 quote_ignores_source_dir
);