Imported Upstream version 2.9.0
[debian/cc1111] / support / cpp / c-incpath.c
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, 2006
4    Free Software Foundation, Inc.
5
6    Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "prefix.h"
26 #include "intl.h"
27 #include "c-incpath.h"
28 #include "cppdefault.h"
29
30 /* Windows does not natively support inodes, and neither does MSDOS.
31    Cygwin's emulation can generate non-unique inodes, so don't use it.
32    VMS has non-numeric inodes.  */
33 #ifdef VMS
34 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
35 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
36 #else
37 # if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__
38 #  define INO_T_EQ(A, B) 0
39 # else
40 #  define INO_T_EQ(A, B) ((A) == (B))
41 # endif
42 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
43 #endif
44
45 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
46
47 static void add_env_var_paths (const char *, int);
48 static void add_standard_paths (const char *, const char *, const char *, int);
49 static void free_path (struct cpp_dir *, int);
50 static void merge_include_chains (cpp_reader *, int);
51 static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
52                                            struct cpp_dir *,
53                                            struct cpp_dir *, int);
54
55 /* Include chains heads and tails.  */
56 static struct cpp_dir *heads[4];
57 static struct cpp_dir *tails[4];
58 static bool quote_ignores_source_dir;
59 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
60
61 /* Free an element of the include chain, possibly giving a reason.  */
62 static void
63 free_path (struct cpp_dir *path, int reason)
64 {
65   switch (reason)
66     {
67     case REASON_DUP:
68     case REASON_DUP_SYS:
69       fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
70       if (reason == REASON_DUP_SYS)
71         fprintf (stderr,
72  _("  as it is a non-system directory that duplicates a system directory\n"));
73       break;
74
75     case REASON_NOENT:
76       fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
77                path->name);
78       break;
79
80     case REASON_QUIET:
81     default:
82       break;
83     }
84
85   free (path->name);
86   free (path);
87 }
88
89 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
90    append all the names to the search path CHAIN.  */
91 static void
92 add_env_var_paths (const char *env_var, int chain)
93 {
94   char *p, *q, *path;
95
96   GET_ENVIRONMENT (q, env_var);
97
98   if (!q)
99     return;
100
101   for (p = q; *q; p = q + 1)
102     {
103       q = p;
104       while (*q != 0 && *q != PATH_SEPARATOR)
105         q++;
106
107       if (p == q)
108         path = xstrdup (".");
109       else
110         {
111           path = XNEWVEC (char, q - p + 1);
112           memcpy (path, p, q - p);
113           path[q - p] = '\0';
114         }
115
116       add_path (path, chain, chain == SYSTEM, false);
117     }
118 }
119
120 /* Append the standard include chain defined in cppdefault.c.  */
121 static void
122 add_standard_paths (const char *sysroot, const char *iprefix,
123                     const char *imultilib, int cxx_stdinc)
124 {
125   const struct default_include *p;
126   size_t len;
127
128   if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
129     {
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++)
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                   if (p->multilib && imultilib)
146                     str = concat (str, dir_separator_str, imultilib, NULL);
147                   add_path (str, SYSTEM, p->cxx_aware, false);
148                 }
149             }
150         }
151     }
152
153   for (p = cpp_include_defaults; p->fname; p++)
154     {
155       if (!p->cplusplus || cxx_stdinc)
156         {
157           char *str;
158
159           /* Should this directory start with the sysroot?  */
160           if (sysroot && p->add_sysroot)
161             str = concat (sysroot, p->fname, NULL);
162           else
163             str = update_path (p->fname, p->component);
164
165           if (p->multilib && imultilib)
166             str = concat (str, dir_separator_str, imultilib, NULL);
167
168           add_path (str, SYSTEM, p->cxx_aware, false);
169         }
170     }
171 }
172
173 /* For each duplicate path in chain HEAD, keep just the first one.
174    Remove each path in chain HEAD that also exists in chain SYSTEM.
175    Set the NEXT pointer of the last path in the resulting chain to
176    JOIN, unless it duplicates JOIN in which case the last path is
177    removed.  Return the head of the resulting chain.  Any of HEAD,
178    JOIN and SYSTEM can be NULL.  */
179
180 static struct cpp_dir *
181 remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
182                    struct cpp_dir *system, struct cpp_dir *join,
183                    int verbose)
184 {
185   struct cpp_dir **pcur, *tmp, *cur;
186   struct stat st;
187
188   for (pcur = &head; *pcur; )
189     {
190       int reason = REASON_QUIET;
191
192       cur = *pcur;
193
194       if (stat (cur->name, &st))
195         {
196           /* Dirs that don't exist are silently ignored, unless verbose.  */
197           if (errno != ENOENT)
198             cpp_errno (pfile, CPP_DL_ERROR, cur->name);
199           else
200             {
201               /* If -Wmissing-include-dirs is given, warn.  */
202               cpp_options *opts = cpp_get_options (pfile);
203               if (opts->warn_missing_include_dirs && cur->user_supplied_p)
204                 cpp_errno (pfile, CPP_DL_WARNING, cur->name);
205               reason = REASON_NOENT;
206             }
207         }
208       else if (!S_ISDIR (st.st_mode))
209         cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
210                              "%s: not a directory", cur->name);
211       else
212         {
213           INO_T_COPY (cur->ino, st.st_ino);
214           cur->dev  = st.st_dev;
215
216           /* Remove this one if it is in the system chain.  */
217           reason = REASON_DUP_SYS;
218           for (tmp = system; tmp; tmp = tmp->next)
219            if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev
220                && cur->construct == tmp->construct)
221               break;
222
223           if (!tmp)
224             {
225               /* Duplicate of something earlier in the same chain?  */
226               reason = REASON_DUP;
227               for (tmp = head; tmp != cur; tmp = tmp->next)
228                if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev
229                    && cur->construct == tmp->construct)
230                   break;
231
232               if (tmp == cur
233                   /* Last in the chain and duplicate of JOIN?  */
234                   && !(cur->next == NULL && join
235                        && INO_T_EQ (cur->ino, join->ino)
236                       && cur->dev == join->dev
237                       && cur->construct == join->construct))
238                 {
239                   /* Unique, so keep this directory.  */
240                   pcur = &cur->next;
241                   continue;
242                 }
243             }
244         }
245
246       /* Remove this entry from the chain.  */
247       *pcur = cur->next;
248       free_path (cur, verbose ? reason: REASON_QUIET);
249     }
250
251   *pcur = join;
252   return head;
253 }
254
255 /* Merge the four include chains together in the order quote, bracket,
256    system, after.  Remove duplicate dirs (as determined by
257    INO_T_EQ()).
258
259    We can't just merge the lists and then uniquify them because then
260    we may lose directories from the <> search path that should be
261    there; consider -iquote foo -iquote bar -Ifoo -Iquux.  It is
262    however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
263    written -iquote bar -Ifoo -Iquux.  */
264
265 static void
266 merge_include_chains (cpp_reader *pfile, int verbose)
267 {
268   /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
269      resulting SYSTEM chain.  */
270   if (heads[SYSTEM])
271     tails[SYSTEM]->next = heads[AFTER];
272   else
273     heads[SYSTEM] = heads[AFTER];
274   heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
275
276   /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
277      join it to SYSTEM.  */
278   heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
279                                       heads[SYSTEM], verbose);
280
281   /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
282      join it to BRACKET.  */
283   heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
284                                     heads[BRACKET], verbose);
285
286   /* If verbose, print the list of dirs to search.  */
287   if (verbose)
288     {
289       struct cpp_dir *p;
290
291       fprintf (stderr, _("#include \"...\" search starts here:\n"));
292       for (p = heads[QUOTE];; p = p->next)
293         {
294           if (p == heads[BRACKET])
295             fprintf (stderr, _("#include <...> search starts here:\n"));
296           if (!p)
297             break;
298           fprintf (stderr, " %s\n", p->name);
299         }
300       fprintf (stderr, _("End of search list.\n"));
301     }
302 }
303
304 /* Use given -I paths for #include "..." but not #include <...>, and
305    don't search the directory of the present file for #include "...".
306    (Note that -I. -I- is not the same as the default setup; -I. uses
307    the compiler's working dir.)  */
308 void
309 split_quote_chain (void)
310 {
311   heads[QUOTE] = heads[BRACKET];
312   tails[QUOTE] = tails[BRACKET];
313   heads[BRACKET] = NULL;
314   tails[BRACKET] = NULL;
315   /* This is NOT redundant.  */
316   quote_ignores_source_dir = true;
317 }
318
319 /* Add P to the chain specified by CHAIN.  */
320
321 void
322 add_cpp_dir_path (cpp_dir *p, int chain)
323 {
324   if (tails[chain])
325     tails[chain]->next = p;
326   else
327     heads[chain] = p;
328   tails[chain] = p;
329 }
330
331 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
332    NUL-terminated.  */
333 void
334 add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)
335 {
336   cpp_dir *p;
337
338 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
339   /* Convert all backslashes to slashes.  The native CRT stat()
340      function does not recognize a directory that ends in a backslash
341      (unless it is a drive root dir, such "c:\").  Forward slashes,
342      trailing or otherwise, cause no problems for stat().  */
343   char* c;
344   for (c = path; *c; c++)
345     if (*c == '\\') *c = '/';
346 #endif
347
348   p = XNEW (cpp_dir);
349   p->next = NULL;
350   p->name = path;
351   if (chain == SYSTEM || chain == AFTER)
352     p->sysp = 1 + !cxx_aware;
353   else
354     p->sysp = 0;
355   p->construct = 0;
356   p->user_supplied_p = user_supplied_p;
357
358   add_cpp_dir_path (p, chain);
359 }
360
361 /* Exported function to handle include chain merging, duplicate
362    removal, and registration with cpplib.  */
363 void
364 register_include_chains (cpp_reader *pfile, const char *sysroot,
365                          const char *iprefix, const char *imultilib,
366                          int stdinc, int cxx_stdinc, int verbose)
367 {
368   static const char *const lang_env_vars[] =
369     { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
370       "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
371   cpp_options *cpp_opts = cpp_get_options (pfile);
372   size_t idx = (cpp_opts->objc ? 2: 0);
373
374   if (cpp_opts->cplusplus)
375     idx++;
376   else
377     cxx_stdinc = false;
378
379   /* CPATH and language-dependent environment variables may add to the
380      include chain.  */
381   add_env_var_paths ("CPATH", BRACKET);
382   add_env_var_paths (lang_env_vars[idx], SYSTEM);
383
384   target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc);
385
386   /* Finally chain on the standard directories.  */
387   if (stdinc)
388     add_standard_paths (sysroot, iprefix, imultilib, cxx_stdinc);
389
390   target_c_incpath.extra_includes (sysroot, iprefix, stdinc);
391
392   merge_include_chains (pfile, verbose);
393
394   cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
395                           quote_ignores_source_dir);
396 }
397 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
398 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED,
399                                            const char *iprefix ATTRIBUTE_UNUSED,
400                                            int stdinc ATTRIBUTE_UNUSED)
401 {
402 }
403 #endif
404
405 #ifndef TARGET_EXTRA_INCLUDES
406 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
407 #endif
408 #ifndef TARGET_EXTRA_PRE_INCLUDES
409 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
410 #endif
411
412 struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES };
413