c-incpath.c revision 132718
1132718Skan/* Set up combined include path chain for the preprocessor.
2132718Skan   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3132718Skan   1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4132718Skan
5132718Skan   Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
6132718Skan
7132718SkanThis program is free software; you can redistribute it and/or modify it
8132718Skanunder the terms of the GNU General Public License as published by the
9132718SkanFree Software Foundation; either version 2, or (at your option) any
10132718Skanlater version.
11132718Skan
12132718SkanThis program is distributed in the hope that it will be useful,
13132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
14132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132718SkanGNU General Public License for more details.
16132718Skan
17132718SkanYou should have received a copy of the GNU General Public License
18132718Skanalong with this program; if not, write to the Free Software
19132718SkanFoundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20132718Skan
21132718Skan#include "config.h"
22132718Skan#include "system.h"
23132718Skan#include "coretypes.h"
24132718Skan#include "tm.h"
25132718Skan#include "cpplib.h"
26132718Skan#include "prefix.h"
27132718Skan#include "intl.h"
28132718Skan#include "c-incpath.h"
29132718Skan#include "cppdefault.h"
30132718Skan
31132718Skan/* Windows does not natively support inodes, and neither does MSDOS.
32132718Skan   Cygwin's emulation can generate non-unique inodes, so don't use it.
33132718Skan   VMS has non-numeric inodes.  */
34132718Skan#ifdef VMS
35132718Skan# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
36132718Skan# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
37132718Skan#else
38132718Skan# if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
39132718Skan#  define INO_T_EQ(A, B) 0
40132718Skan# else
41132718Skan#  define INO_T_EQ(A, B) ((A) == (B))
42132718Skan# endif
43132718Skan# define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
44132718Skan#endif
45132718Skan
46132718Skanstatic void add_env_var_paths (const char *, int);
47132718Skanstatic void add_standard_paths (const char *, const char *, int);
48132718Skanstatic void free_path (struct cpp_dir *, int);
49132718Skanstatic void merge_include_chains (cpp_reader *, int);
50132718Skanstatic struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
51132718Skan					   struct cpp_dir *,
52132718Skan					   struct cpp_dir *, int);
53132718Skan
54132718Skan/* Include chains heads and tails.  */
55132718Skanstatic struct cpp_dir *heads[4];
56132718Skanstatic struct cpp_dir *tails[4];
57132718Skanstatic bool quote_ignores_source_dir;
58132718Skanenum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
59132718Skan
60132718Skan/* Free an element of the include chain, possibly giving a reason.  */
61132718Skanstatic void
62132718Skanfree_path (struct cpp_dir *path, int reason)
63132718Skan{
64132718Skan  switch (reason)
65132718Skan    {
66132718Skan    case REASON_DUP:
67132718Skan    case REASON_DUP_SYS:
68132718Skan      fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
69132718Skan      if (reason == REASON_DUP_SYS)
70132718Skan	fprintf (stderr,
71132718Skan _("  as it is a non-system directory that duplicates a system directory\n"));
72132718Skan      break;
73132718Skan
74132718Skan    case REASON_NOENT:
75132718Skan      fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
76132718Skan	       path->name);
77132718Skan      break;
78132718Skan
79132718Skan    case REASON_QUIET:
80132718Skan    default:
81132718Skan      break;
82132718Skan    }
83132718Skan
84132718Skan  free (path->name);
85132718Skan  free (path);
86132718Skan}
87132718Skan
88132718Skan/* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
89132718Skan   append all the names to the search path CHAIN.  */
90132718Skanstatic void
91132718Skanadd_env_var_paths (const char *env_var, int chain)
92132718Skan{
93132718Skan  char *p, *q, *path;
94132718Skan
95132718Skan  GET_ENVIRONMENT (q, env_var);
96132718Skan
97132718Skan  if (!q)
98132718Skan    return;
99132718Skan
100132718Skan  for (p = q; *q; p = q + 1)
101132718Skan    {
102132718Skan      q = p;
103132718Skan      while (*q != 0 && *q != PATH_SEPARATOR)
104132718Skan	q++;
105132718Skan
106132718Skan      if (p == q)
107132718Skan	path = xstrdup (".");
108132718Skan      else
109132718Skan	{
110132718Skan	  path = xmalloc (q - p + 1);
111132718Skan	  memcpy (path, p, q - p);
112132718Skan	  path[q - p] = '\0';
113132718Skan	}
114132718Skan
115132718Skan      add_path (path, chain, chain == SYSTEM);
116132718Skan    }
117132718Skan}
118132718Skan
119132718Skan/* Append the standard include chain defined in cppdefault.c.  */
120132718Skanstatic void
121132718Skanadd_standard_paths (const char *sysroot, const char *iprefix, int cxx_stdinc)
122132718Skan{
123132718Skan  const struct default_include *p;
124132718Skan  size_t len;
125132718Skan
126132718Skan  if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
127132718Skan    {
128132718Skan      /* Look for directories that start with the standard prefix.
129132718Skan	 "Translate" them, ie. replace /usr/local/lib/gcc... with
130132718Skan	 IPREFIX and search them first.  */
131132718Skan      for (p = cpp_include_defaults; p->fname; p++)
132132718Skan	{
133132718Skan	  if (!p->cplusplus || cxx_stdinc)
134132718Skan	    {
135132718Skan	      /* Should we be translating sysrooted dirs too?  Assume
136132718Skan		 that iprefix and sysroot are mutually exclusive, for
137132718Skan		 now.  */
138132718Skan	      if (sysroot && p->add_sysroot)
139132718Skan		continue;
140132718Skan	      if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
141132718Skan		{
142132718Skan		  char *str = concat (iprefix, p->fname + len, NULL);
143132718Skan		  add_path (str, SYSTEM, p->cxx_aware);
144132718Skan		}
145132718Skan	    }
146132718Skan	}
147132718Skan    }
148132718Skan
149132718Skan  for (p = cpp_include_defaults; p->fname; p++)
150132718Skan    {
151132718Skan      if (!p->cplusplus || cxx_stdinc)
152132718Skan	{
153132718Skan	  char *str;
154132718Skan
155132718Skan	  /* Should this directory start with the sysroot?  */
156132718Skan	  if (sysroot && p->add_sysroot)
157132718Skan	    str = concat (sysroot, p->fname, NULL);
158132718Skan	  else
159132718Skan	    str = update_path (p->fname, p->component);
160132718Skan
161132718Skan	  add_path (str, SYSTEM, p->cxx_aware);
162132718Skan	}
163132718Skan    }
164132718Skan}
165132718Skan
166132718Skan/* For each duplicate path in chain HEAD, keep just the first one.
167132718Skan   Remove each path in chain HEAD that also exists in chain SYSTEM.
168132718Skan   Set the NEXT pointer of the last path in the resulting chain to
169132718Skan   JOIN, unless it duplicates JOIN in which case the last path is
170132718Skan   removed.  Return the head of the resulting chain.  Any of HEAD,
171132718Skan   JOIN and SYSTEM can be NULL.  */
172132718Skanstatic struct cpp_dir *
173132718Skanremove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
174132718Skan		   struct cpp_dir *system, struct cpp_dir *join,
175132718Skan		   int verbose)
176132718Skan{
177132718Skan  struct cpp_dir **pcur, *tmp, *cur;
178132718Skan  struct stat st;
179132718Skan
180132718Skan  for (pcur = &head; *pcur; )
181132718Skan    {
182132718Skan      int reason = REASON_QUIET;
183132718Skan
184132718Skan      cur = *pcur;
185132718Skan
186132718Skan      if (stat (cur->name, &st))
187132718Skan	{
188132718Skan	  /* Dirs that don't exist are silently ignored, unless verbose.  */
189132718Skan	  if (errno != ENOENT)
190132718Skan	    cpp_errno (pfile, CPP_DL_ERROR, cur->name);
191132718Skan	  else
192132718Skan	    reason = REASON_NOENT;
193132718Skan	}
194132718Skan      else if (!S_ISDIR (st.st_mode))
195132718Skan	cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
196132718Skan			     "%s: not a directory", cur->name);
197132718Skan      else
198132718Skan	{
199132718Skan	  INO_T_COPY (cur->ino, st.st_ino);
200132718Skan	  cur->dev  = st.st_dev;
201132718Skan
202132718Skan	  /* Remove this one if it is in the system chain.  */
203132718Skan	  reason = REASON_DUP_SYS;
204132718Skan	  for (tmp = system; tmp; tmp = tmp->next)
205132718Skan	    if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev)
206132718Skan	      break;
207132718Skan
208132718Skan	  if (!tmp)
209132718Skan	    {
210132718Skan	      /* Duplicate of something earlier in the same chain?  */
211132718Skan	      reason = REASON_DUP;
212132718Skan	      for (tmp = head; tmp != cur; tmp = tmp->next)
213132718Skan		if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev)
214132718Skan		  break;
215132718Skan
216132718Skan	      if (tmp == cur
217132718Skan		  /* Last in the chain and duplicate of JOIN?  */
218132718Skan		  && !(cur->next == NULL && join
219132718Skan		       && INO_T_EQ (cur->ino, join->ino)
220132718Skan		       && cur->dev == join->dev))
221132718Skan		{
222132718Skan		  /* Unique, so keep this directory.  */
223132718Skan		  pcur = &cur->next;
224132718Skan		  continue;
225132718Skan		}
226132718Skan	    }
227132718Skan	}
228132718Skan
229132718Skan      /* Remove this entry from the chain.  */
230132718Skan      *pcur = cur->next;
231132718Skan      free_path (cur, verbose ? reason: REASON_QUIET);
232132718Skan    }
233132718Skan
234132718Skan  *pcur = join;
235132718Skan  return head;
236132718Skan}
237132718Skan
238132718Skan/* Merge the four include chains together in the order quote, bracket,
239132718Skan   system, after.  Remove duplicate dirs (as determined by
240132718Skan   INO_T_EQ()).
241132718Skan
242132718Skan   We can't just merge the lists and then uniquify them because then
243132718Skan   we may lose directories from the <> search path that should be
244132718Skan   there; consider -Ifoo -Ibar -I- -Ifoo -Iquux.  It is however safe
245132718Skan   to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
246132718Skan   -Iquux.  */
247132718Skanstatic void
248132718Skanmerge_include_chains (cpp_reader *pfile, int verbose)
249132718Skan{
250132718Skan  /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
251132718Skan     resulting SYSTEM chain.  */
252132718Skan  if (heads[SYSTEM])
253132718Skan    tails[SYSTEM]->next = heads[AFTER];
254132718Skan  else
255132718Skan    heads[SYSTEM] = heads[AFTER];
256132718Skan  heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
257132718Skan
258132718Skan  /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
259132718Skan     join it to SYSTEM.  */
260132718Skan  heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
261132718Skan				      heads[SYSTEM], verbose);
262132718Skan
263132718Skan  /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
264132718Skan     join it to BRACKET.  */
265132718Skan  heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
266132718Skan				    heads[BRACKET], verbose);
267132718Skan
268132718Skan  /* If verbose, print the list of dirs to search.  */
269132718Skan  if (verbose)
270132718Skan    {
271132718Skan      struct cpp_dir *p;
272132718Skan
273132718Skan      fprintf (stderr, _("#include \"...\" search starts here:\n"));
274132718Skan      for (p = heads[QUOTE];; p = p->next)
275132718Skan	{
276132718Skan	  if (p == heads[BRACKET])
277132718Skan	    fprintf (stderr, _("#include <...> search starts here:\n"));
278132718Skan	  if (!p)
279132718Skan	    break;
280132718Skan	  fprintf (stderr, " %s\n", p->name);
281132718Skan	}
282132718Skan      fprintf (stderr, _("End of search list.\n"));
283132718Skan    }
284132718Skan}
285132718Skan
286132718Skan/* Use given -I paths for #include "..." but not #include <...>, and
287132718Skan   don't search the directory of the present file for #include "...".
288132718Skan   (Note that -I. -I- is not the same as the default setup; -I. uses
289132718Skan   the compiler's working dir.)  */
290132718Skanvoid
291132718Skansplit_quote_chain (void)
292132718Skan{
293132718Skan  heads[QUOTE] = heads[BRACKET];
294132718Skan  tails[QUOTE] = tails[BRACKET];
295132718Skan  heads[BRACKET] = NULL;
296132718Skan  tails[BRACKET] = NULL;
297132718Skan  /* This is NOT redundant.  */
298132718Skan  quote_ignores_source_dir = true;
299132718Skan}
300132718Skan
301132718Skan/* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
302132718Skan   NUL-terminated.  */
303132718Skanvoid
304132718Skanadd_path (char *path, int chain, int cxx_aware)
305132718Skan{
306132718Skan  struct cpp_dir *p;
307132718Skan
308132718Skan#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
309132718Skan  /* Convert all backslashes to slashes.  The native CRT stat()
310132718Skan     function does not recognise a directory that ends in a backslash
311132718Skan     (unless it is a drive root dir, such "c:\").  Forward slashes,
312132718Skan     trailing or otherwise, cause no problems for stat().  */
313132718Skan  char* c;
314132718Skan  for (c = path; *c; c++)
315132718Skan    if (*c == '\\') *c = '/';
316132718Skan#endif
317132718Skan
318132718Skan  p = xmalloc (sizeof (struct cpp_dir));
319132718Skan  p->next = NULL;
320132718Skan  p->name = path;
321132718Skan  if (chain == SYSTEM || chain == AFTER)
322132718Skan    p->sysp = 1 + !cxx_aware;
323132718Skan  else
324132718Skan    p->sysp = 0;
325132718Skan
326132718Skan  if (tails[chain])
327132718Skan    tails[chain]->next = p;
328132718Skan  else
329132718Skan    heads[chain] = p;
330132718Skan  tails[chain] = p;
331132718Skan}
332132718Skan
333132718Skan/* Exported function to handle include chain merging, duplicate
334132718Skan   removal, and registration with cpplib.  */
335132718Skanvoid
336132718Skanregister_include_chains (cpp_reader *pfile, const char *sysroot,
337132718Skan			 const char *iprefix, int stdinc, int cxx_stdinc,
338132718Skan			 int verbose)
339132718Skan{
340132718Skan  static const char *const lang_env_vars[] =
341132718Skan    { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
342132718Skan      "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
343132718Skan  cpp_options *cpp_opts = cpp_get_options (pfile);
344132718Skan  size_t idx = (cpp_opts->objc ? 2: 0);
345132718Skan
346132718Skan  if (cpp_opts->cplusplus)
347132718Skan    idx++;
348132718Skan  else
349132718Skan    cxx_stdinc = false;
350132718Skan
351132718Skan  /* CPATH and language-dependent environment variables may add to the
352132718Skan     include chain.  */
353132718Skan  add_env_var_paths ("CPATH", BRACKET);
354132718Skan  add_env_var_paths (lang_env_vars[idx], SYSTEM);
355132718Skan
356132718Skan  /* Finally chain on the standard directories.  */
357132718Skan  if (stdinc)
358132718Skan    add_standard_paths (sysroot, iprefix, cxx_stdinc);
359132718Skan
360132718Skan  merge_include_chains (pfile, verbose);
361132718Skan
362132718Skan  cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
363132718Skan			  quote_ignores_source_dir);
364132718Skan}
365