c-incpath.c revision 225736
1260684Skaiw/* Set up combined include path chain for the preprocessor. 2260684Skaiw Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3260684Skaiw 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 4260684Skaiw Free Software Foundation, Inc. 5260684Skaiw 6260684Skaiw Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003. 7260684Skaiw 8260684SkaiwThis program is free software; you can redistribute it and/or modify it 9260684Skaiwunder the terms of the GNU General Public License as published by the 10260684SkaiwFree Software Foundation; either version 2, or (at your option) any 11260684Skaiwlater version. 12260684Skaiw 13260684SkaiwThis program is distributed in the hope that it will be useful, 14260684Skaiwbut WITHOUT ANY WARRANTY; without even the implied warranty of 15260684SkaiwMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16260684SkaiwGNU General Public License for more details. 17260684Skaiw 18260684SkaiwYou should have received a copy of the GNU General Public License 19260684Skaiwalong with this program; if not, write to the Free Software 20260684SkaiwFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 21260684Skaiw 22260684Skaiw#include "config.h" 23260684Skaiw#include "system.h" 24260684Skaiw#include "coretypes.h" 25260684Skaiw#include "machmode.h" 26260684Skaiw#include "target.h" 27260684Skaiw#include "tm.h" 28260684Skaiw#include "cpplib.h" 29260684Skaiw#include "prefix.h" 30260684Skaiw#include "intl.h" 31260684Skaiw#include "c-incpath.h" 32260684Skaiw#include "cppdefault.h" 33260684Skaiw 34260684Skaiw/* Windows does not natively support inodes, and neither does MSDOS. 35260684Skaiw Cygwin's emulation can generate non-unique inodes, so don't use it. 36260684Skaiw VMS has non-numeric inodes. */ 37260684Skaiw#ifdef VMS 38276371Semaste# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A))) 39260684Skaiw# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC)) 40260684Skaiw#else 41260684Skaiw# if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__ 42260684Skaiw# define INO_T_EQ(A, B) 0 43260684Skaiw# else 44260684Skaiw# define INO_T_EQ(A, B) ((A) == (B)) 45260684Skaiw# endif 46260684Skaiw# define INO_T_COPY(DEST, SRC) (DEST) = (SRC) 47260684Skaiw#endif 48260684Skaiw 49260684Skaiwstatic const char dir_separator_str[] = { DIR_SEPARATOR, 0 }; 50260684Skaiw 51260684Skaiwstatic void add_env_var_paths (const char *, int); 52260684Skaiwstatic void add_standard_paths (const char *, const char *, const char *, int); 53260684Skaiwstatic void free_path (struct cpp_dir *, int); 54260684Skaiwstatic void merge_include_chains (cpp_reader *, int); 55260684Skaiwstatic struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *, 56260684Skaiw struct cpp_dir *, 57260684Skaiw struct cpp_dir *, int); 58260684Skaiw 59260684Skaiw/* Include chains heads and tails. */ 60260684Skaiwstatic struct cpp_dir *heads[4]; 61260684Skaiwstatic struct cpp_dir *tails[4]; 62260684Skaiwstatic bool quote_ignores_source_dir; 63260684Skaiwenum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS }; 64260684Skaiw 65260684Skaiw/* Free an element of the include chain, possibly giving a reason. */ 66260684Skaiwstatic void 67260684Skaiwfree_path (struct cpp_dir *path, int reason) 68260684Skaiw{ 69260684Skaiw switch (reason) 70260684Skaiw { 71260684Skaiw case REASON_DUP: 72260684Skaiw case REASON_DUP_SYS: 73260684Skaiw fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name); 74260684Skaiw if (reason == REASON_DUP_SYS) 75260684Skaiw fprintf (stderr, 76260684Skaiw _(" as it is a non-system directory that duplicates a system directory\n")); 77260684Skaiw break; 78260684Skaiw 79260684Skaiw case REASON_NOENT: 80260684Skaiw fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), 81260684Skaiw path->name); 82260684Skaiw break; 83260684Skaiw 84260684Skaiw case REASON_QUIET: 85260684Skaiw default: 86260684Skaiw break; 87260684Skaiw } 88260684Skaiw 89260684Skaiw free (path->name); 90260684Skaiw free (path); 91260684Skaiw} 92260684Skaiw 93260684Skaiw/* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and 94260684Skaiw append all the names to the search path CHAIN. */ 95260684Skaiwstatic void 96260684Skaiwadd_env_var_paths (const char *env_var, int chain) 97260684Skaiw{ 98260684Skaiw char *p, *q, *path; 99260684Skaiw 100260684Skaiw GET_ENVIRONMENT (q, env_var); 101260684Skaiw 102260684Skaiw if (!q) 103260684Skaiw return; 104260684Skaiw 105260684Skaiw for (p = q; *q; p = q + 1) 106260684Skaiw { 107260684Skaiw q = p; 108260684Skaiw while (*q != 0 && *q != PATH_SEPARATOR) 109260684Skaiw q++; 110260684Skaiw 111260684Skaiw if (p == q) 112260684Skaiw path = xstrdup ("."); 113276371Semaste else 114260684Skaiw { 115260684Skaiw path = XNEWVEC (char, q - p + 1); 116260684Skaiw memcpy (path, p, q - p); 117260684Skaiw path[q - p] = '\0'; 118260684Skaiw } 119260684Skaiw 120260684Skaiw add_path (path, chain, chain == SYSTEM, false); 121260684Skaiw } 122260684Skaiw} 123260684Skaiw 124260684Skaiw/* Append the standard include chain defined in cppdefault.c. */ 125260684Skaiwstatic void 126260684Skaiwadd_standard_paths (const char *sysroot, const char *iprefix, 127260684Skaiw const char *imultilib, int cxx_stdinc) 128260684Skaiw{ 129260684Skaiw const struct default_include *p; 130260684Skaiw size_t len; 131260684Skaiw 132260684Skaiw if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0) 133260684Skaiw { 134260684Skaiw /* Look for directories that start with the standard prefix. 135260684Skaiw "Translate" them, i.e. replace /usr/local/lib/gcc... with 136260684Skaiw IPREFIX and search them first. */ 137260684Skaiw for (p = cpp_include_defaults; p->fname; p++) 138260684Skaiw { 139260684Skaiw if (!p->cplusplus || cxx_stdinc) 140260684Skaiw { 141260684Skaiw /* Should we be translating sysrooted dirs too? Assume 142260684Skaiw that iprefix and sysroot are mutually exclusive, for 143260684Skaiw now. */ 144260684Skaiw if (sysroot && p->add_sysroot) 145260684Skaiw continue; 146260684Skaiw if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len)) 147260684Skaiw { 148260684Skaiw char *str = concat (iprefix, p->fname + len, NULL); 149260684Skaiw if (p->multilib && imultilib) 150260684Skaiw str = concat (str, dir_separator_str, imultilib, NULL); 151260684Skaiw add_path (str, SYSTEM, p->cxx_aware, false); 152260684Skaiw } 153260684Skaiw } 154260684Skaiw } 155260684Skaiw } 156260684Skaiw 157260684Skaiw for (p = cpp_include_defaults; p->fname; p++) 158260684Skaiw { 159260684Skaiw if (!p->cplusplus || cxx_stdinc) 160260684Skaiw { 161260684Skaiw char *str; 162260684Skaiw 163260684Skaiw /* Should this directory start with the sysroot? */ 164260684Skaiw if (sysroot && p->add_sysroot) 165260684Skaiw str = concat (sysroot, p->fname, NULL); 166260684Skaiw else 167260684Skaiw str = update_path (p->fname, p->component); 168260684Skaiw 169260684Skaiw if (p->multilib && imultilib) 170260684Skaiw str = concat (str, dir_separator_str, imultilib, NULL); 171260684Skaiw 172260684Skaiw add_path (str, SYSTEM, p->cxx_aware, false); 173260684Skaiw } 174260684Skaiw } 175260684Skaiw} 176260684Skaiw 177260684Skaiw/* For each duplicate path in chain HEAD, keep just the first one. 178260684Skaiw Remove each path in chain HEAD that also exists in chain SYSTEM. 179260684Skaiw Set the NEXT pointer of the last path in the resulting chain to 180260684Skaiw JOIN, unless it duplicates JOIN in which case the last path is 181260684Skaiw removed. Return the head of the resulting chain. Any of HEAD, 182260684Skaiw JOIN and SYSTEM can be NULL. */ 183260684Skaiw 184260684Skaiwstatic struct cpp_dir * 185260684Skaiwremove_duplicates (cpp_reader *pfile, struct cpp_dir *head, 186260684Skaiw struct cpp_dir *system, struct cpp_dir *join, 187260684Skaiw int verbose) 188260684Skaiw{ 189260684Skaiw struct cpp_dir **pcur, *tmp, *cur; 190260684Skaiw struct stat st; 191260684Skaiw 192260684Skaiw for (pcur = &head; *pcur; ) 193260684Skaiw { 194260684Skaiw int reason = REASON_QUIET; 195276371Semaste 196260684Skaiw cur = *pcur; 197260684Skaiw 198260684Skaiw if (stat (cur->name, &st)) 199260684Skaiw { 200260684Skaiw /* Dirs that don't exist are silently ignored, unless verbose. */ 201260684Skaiw if (errno != ENOENT) 202260684Skaiw cpp_errno (pfile, CPP_DL_ERROR, cur->name); 203260684Skaiw else 204276371Semaste { 205260684Skaiw /* If -Wmissing-include-dirs is given, warn. */ 206260684Skaiw cpp_options *opts = cpp_get_options (pfile); 207276371Semaste if (opts->warn_missing_include_dirs && cur->user_supplied_p) 208260684Skaiw cpp_errno (pfile, CPP_DL_WARNING, cur->name); 209260684Skaiw reason = REASON_NOENT; 210260684Skaiw } 211260684Skaiw } 212260684Skaiw else if (!S_ISDIR (st.st_mode)) 213260684Skaiw cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0, 214260684Skaiw "%s: not a directory", cur->name); 215260684Skaiw else 216260684Skaiw { 217260684Skaiw INO_T_COPY (cur->ino, st.st_ino); 218260684Skaiw cur->dev = st.st_dev; 219260684Skaiw 220260684Skaiw /* Remove this one if it is in the system chain. */ 221260684Skaiw reason = REASON_DUP_SYS; 222260684Skaiw for (tmp = system; tmp; tmp = tmp->next) 223260684Skaiw if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev 224260684Skaiw && cur->construct == tmp->construct) 225260684Skaiw break; 226260684Skaiw 227260684Skaiw if (!tmp) 228260684Skaiw { 229260684Skaiw /* Duplicate of something earlier in the same chain? */ 230260684Skaiw reason = REASON_DUP; 231260684Skaiw for (tmp = head; tmp != cur; tmp = tmp->next) 232260684Skaiw if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev 233260684Skaiw && cur->construct == tmp->construct) 234260684Skaiw break; 235260684Skaiw 236260684Skaiw if (tmp == cur 237260684Skaiw /* Last in the chain and duplicate of JOIN? */ 238260684Skaiw && !(cur->next == NULL && join 239260684Skaiw && INO_T_EQ (cur->ino, join->ino) 240260684Skaiw && cur->dev == join->dev 241260684Skaiw && cur->construct == join->construct)) 242260684Skaiw { 243260684Skaiw /* Unique, so keep this directory. */ 244260684Skaiw pcur = &cur->next; 245260684Skaiw continue; 246260684Skaiw } 247260684Skaiw } 248260684Skaiw } 249260684Skaiw 250260684Skaiw /* Remove this entry from the chain. */ 251260684Skaiw *pcur = cur->next; 252276371Semaste free_path (cur, verbose ? reason: REASON_QUIET); 253260684Skaiw } 254260684Skaiw 255260684Skaiw *pcur = join; 256260684Skaiw return head; 257276371Semaste} 258260684Skaiw 259260684Skaiw/* Merge the four include chains together in the order quote, bracket, 260260684Skaiw system, after. Remove duplicate dirs (as determined by 261260684Skaiw INO_T_EQ()). 262260684Skaiw 263260684Skaiw We can't just merge the lists and then uniquify them because then 264260684Skaiw we may lose directories from the <> search path that should be 265260684Skaiw there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is 266260684Skaiw however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if 267260684Skaiw written -iquote bar -Ifoo -Iquux. */ 268260684Skaiw 269260684Skaiwstatic void 270260684Skaiwmerge_include_chains (cpp_reader *pfile, int verbose) 271260684Skaiw{ 272260684Skaiw /* Join the SYSTEM and AFTER chains. Remove duplicates in the 273260684Skaiw resulting SYSTEM chain. */ 274260684Skaiw if (heads[SYSTEM]) 275260684Skaiw tails[SYSTEM]->next = heads[AFTER]; 276260684Skaiw else 277276371Semaste heads[SYSTEM] = heads[AFTER]; 278276371Semaste heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose); 279276371Semaste 280276371Semaste /* Remove duplicates from BRACKET that are in itself or SYSTEM, and 281260684Skaiw join it to SYSTEM. */ 282260684Skaiw heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM], 283260684Skaiw heads[SYSTEM], verbose); 284260684Skaiw 285260684Skaiw /* Remove duplicates from QUOTE that are in itself or SYSTEM, and 286260684Skaiw join it to BRACKET. */ 287260684Skaiw heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM], 288260684Skaiw heads[BRACKET], verbose); 289260684Skaiw 290260684Skaiw /* If verbose, print the list of dirs to search. */ 291276371Semaste if (verbose) 292260684Skaiw { 293276371Semaste struct cpp_dir *p; 294276371Semaste 295260684Skaiw fprintf (stderr, _("#include \"...\" search starts here:\n")); 296260684Skaiw for (p = heads[QUOTE];; p = p->next) 297260684Skaiw { 298260684Skaiw if (p == heads[BRACKET]) 299260684Skaiw fprintf (stderr, _("#include <...> search starts here:\n")); 300260684Skaiw if (!p) 301260684Skaiw break; 302260684Skaiw fprintf (stderr, " %s\n", p->name); 303260684Skaiw } 304260684Skaiw fprintf (stderr, _("End of search list.\n")); 305260684Skaiw } 306260684Skaiw} 307260684Skaiw 308260684Skaiw/* Use given -I paths for #include "..." but not #include <...>, and 309260684Skaiw don't search the directory of the present file for #include "...". 310260684Skaiw (Note that -I. -I- is not the same as the default setup; -I. uses 311260684Skaiw the compiler's working dir.) */ 312260684Skaiwvoid 313260684Skaiwsplit_quote_chain (void) 314260684Skaiw{ 315260684Skaiw heads[QUOTE] = heads[BRACKET]; 316260684Skaiw tails[QUOTE] = tails[BRACKET]; 317260684Skaiw heads[BRACKET] = NULL; 318276371Semaste tails[BRACKET] = NULL; 319276371Semaste /* This is NOT redundant. */ 320260684Skaiw quote_ignores_source_dir = true; 321260684Skaiw} 322260684Skaiw 323260684Skaiw/* Add P to the chain specified by CHAIN. */ 324260684Skaiw 325260684Skaiwvoid 326260684Skaiwadd_cpp_dir_path (cpp_dir *p, int chain) 327260684Skaiw{ 328260684Skaiw if (tails[chain]) 329276371Semaste tails[chain]->next = p; 330260684Skaiw else 331260684Skaiw heads[chain] = p; 332276371Semaste tails[chain] = p; 333260684Skaiw} 334260684Skaiw 335260684Skaiw/* Add PATH to the include chain CHAIN. PATH must be malloc-ed and 336260684Skaiw NUL-terminated. */ 337260684Skaiwvoid 338260684Skaiwadd_path (char *path, int chain, int cxx_aware, bool user_supplied_p) 339260684Skaiw{ 340260684Skaiw cpp_dir *p; 341260684Skaiw 342260684Skaiw#if defined (HAVE_DOS_BASED_FILE_SYSTEM) 343260684Skaiw /* Convert all backslashes to slashes. The native CRT stat() 344260684Skaiw function does not recognize a directory that ends in a backslash 345260684Skaiw (unless it is a drive root dir, such "c:\"). Forward slashes, 346260684Skaiw trailing or otherwise, cause no problems for stat(). */ 347276371Semaste char* c; 348276371Semaste for (c = path; *c; c++) 349276371Semaste if (*c == '\\') *c = '/'; 350276371Semaste#endif 351260684Skaiw 352260684Skaiw p = XNEW (cpp_dir); 353260684Skaiw p->next = NULL; 354260684Skaiw p->name = path; 355260684Skaiw if (chain == SYSTEM || chain == AFTER) 356276371Semaste p->sysp = 1 + !cxx_aware; 357260684Skaiw else 358260684Skaiw p->sysp = 0; 359260684Skaiw p->construct = 0; 360260684Skaiw p->user_supplied_p = user_supplied_p; 361260684Skaiw 362260684Skaiw add_cpp_dir_path (p, chain); 363260684Skaiw} 364260684Skaiw 365260684Skaiw/* Exported function to handle include chain merging, duplicate 366260684Skaiw removal, and registration with cpplib. */ 367260684Skaiwvoid 368260684Skaiwregister_include_chains (cpp_reader *pfile, const char *sysroot, 369260684Skaiw const char *iprefix, const char *imultilib, 370260684Skaiw int stdinc, int cxx_stdinc, int verbose) 371260684Skaiw{ 372260684Skaiw static const char *const lang_env_vars[] = 373260684Skaiw { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH", 374260684Skaiw "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" }; 375260684Skaiw cpp_options *cpp_opts = cpp_get_options (pfile); 376260684Skaiw size_t idx = (cpp_opts->objc ? 2: 0); 377260684Skaiw 378260684Skaiw if (cpp_opts->cplusplus) 379260684Skaiw idx++; 380260684Skaiw else 381260684Skaiw cxx_stdinc = false; 382260684Skaiw 383260684Skaiw /* CPATH and language-dependent environment variables may add to the 384260684Skaiw include chain. */ 385260684Skaiw add_env_var_paths ("CPATH", BRACKET); 386260684Skaiw add_env_var_paths (lang_env_vars[idx], SYSTEM); 387260684Skaiw 388260684Skaiw target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc); 389260684Skaiw 390260684Skaiw /* Finally chain on the standard directories. */ 391260684Skaiw if (stdinc) 392260684Skaiw add_standard_paths (sysroot, iprefix, imultilib, cxx_stdinc); 393260684Skaiw 394260684Skaiw target_c_incpath.extra_includes (sysroot, iprefix, stdinc); 395260684Skaiw 396260684Skaiw merge_include_chains (pfile, verbose); 397260684Skaiw 398260684Skaiw cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET], 399260684Skaiw quote_ignores_source_dir); 400260684Skaiw} 401260684Skaiw#if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES) 402260684Skaiwstatic void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED, 403276371Semaste const char *iprefix ATTRIBUTE_UNUSED, 404276371Semaste int stdinc ATTRIBUTE_UNUSED) 405260684Skaiw{ 406260684Skaiw} 407260684Skaiw#endif 408260684Skaiw 409260684Skaiw#ifndef TARGET_EXTRA_INCLUDES 410260684Skaiw#define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int 411260684Skaiw#endif 412260684Skaiw#ifndef TARGET_EXTRA_PRE_INCLUDES 413260684Skaiw#define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int 414260684Skaiw#endif 415260684Skaiw 416260684Skaiwstruct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES }; 417260684Skaiw 418260684Skaiw