1132718Skan/* Set up combined include path chain for the preprocessor. 2132718Skan Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3169689Skan 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 4169689Skan Free Software Foundation, Inc. 5132718Skan 6132718Skan Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003. 7132718Skan 8132718SkanThis program is free software; you can redistribute it and/or modify it 9132718Skanunder the terms of the GNU General Public License as published by the 10132718SkanFree Software Foundation; either version 2, or (at your option) any 11132718Skanlater version. 12132718Skan 13132718SkanThis program is distributed in the hope that it will be useful, 14132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 15132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16132718SkanGNU General Public License for more details. 17132718Skan 18132718SkanYou should have received a copy of the GNU General Public License 19132718Skanalong with this program; if not, write to the Free Software 20169689SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 21132718Skan 22132718Skan#include "config.h" 23132718Skan#include "system.h" 24132718Skan#include "coretypes.h" 25169689Skan#include "machmode.h" 26169689Skan#include "target.h" 27132718Skan#include "tm.h" 28132718Skan#include "cpplib.h" 29132718Skan#include "prefix.h" 30132718Skan#include "intl.h" 31132718Skan#include "c-incpath.h" 32132718Skan#include "cppdefault.h" 33132718Skan 34132718Skan/* Windows does not natively support inodes, and neither does MSDOS. 35132718Skan Cygwin's emulation can generate non-unique inodes, so don't use it. 36132718Skan VMS has non-numeric inodes. */ 37132718Skan#ifdef VMS 38132718Skan# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A))) 39132718Skan# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC)) 40132718Skan#else 41169689Skan# if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__ 42132718Skan# define INO_T_EQ(A, B) 0 43132718Skan# else 44132718Skan# define INO_T_EQ(A, B) ((A) == (B)) 45132718Skan# endif 46132718Skan# define INO_T_COPY(DEST, SRC) (DEST) = (SRC) 47132718Skan#endif 48132718Skan 49169689Skanstatic const char dir_separator_str[] = { DIR_SEPARATOR, 0 }; 50169689Skan 51132718Skanstatic void add_env_var_paths (const char *, int); 52169689Skanstatic void add_standard_paths (const char *, const char *, const char *, int); 53132718Skanstatic void free_path (struct cpp_dir *, int); 54132718Skanstatic void merge_include_chains (cpp_reader *, int); 55132718Skanstatic struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *, 56132718Skan struct cpp_dir *, 57132718Skan struct cpp_dir *, int); 58132718Skan 59132718Skan/* Include chains heads and tails. */ 60132718Skanstatic struct cpp_dir *heads[4]; 61132718Skanstatic struct cpp_dir *tails[4]; 62132718Skanstatic bool quote_ignores_source_dir; 63132718Skanenum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS }; 64132718Skan 65132718Skan/* Free an element of the include chain, possibly giving a reason. */ 66132718Skanstatic void 67132718Skanfree_path (struct cpp_dir *path, int reason) 68132718Skan{ 69132718Skan switch (reason) 70132718Skan { 71132718Skan case REASON_DUP: 72132718Skan case REASON_DUP_SYS: 73132718Skan fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name); 74132718Skan if (reason == REASON_DUP_SYS) 75260011Spfg fprintf (stderr, "%s", 76132718Skan _(" as it is a non-system directory that duplicates a system directory\n")); 77132718Skan break; 78132718Skan 79132718Skan case REASON_NOENT: 80132718Skan fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), 81132718Skan path->name); 82132718Skan break; 83132718Skan 84132718Skan case REASON_QUIET: 85132718Skan default: 86132718Skan break; 87132718Skan } 88132718Skan 89132718Skan free (path->name); 90132718Skan free (path); 91132718Skan} 92132718Skan 93132718Skan/* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and 94132718Skan append all the names to the search path CHAIN. */ 95132718Skanstatic void 96132718Skanadd_env_var_paths (const char *env_var, int chain) 97132718Skan{ 98132718Skan char *p, *q, *path; 99132718Skan 100132718Skan GET_ENVIRONMENT (q, env_var); 101132718Skan 102132718Skan if (!q) 103132718Skan return; 104132718Skan 105132718Skan for (p = q; *q; p = q + 1) 106132718Skan { 107132718Skan q = p; 108132718Skan while (*q != 0 && *q != PATH_SEPARATOR) 109132718Skan q++; 110132718Skan 111132718Skan if (p == q) 112132718Skan path = xstrdup ("."); 113132718Skan else 114132718Skan { 115169689Skan path = XNEWVEC (char, q - p + 1); 116132718Skan memcpy (path, p, q - p); 117132718Skan path[q - p] = '\0'; 118132718Skan } 119132718Skan 120169689Skan add_path (path, chain, chain == SYSTEM, false); 121132718Skan } 122132718Skan} 123132718Skan 124132718Skan/* Append the standard include chain defined in cppdefault.c. */ 125132718Skanstatic void 126169689Skanadd_standard_paths (const char *sysroot, const char *iprefix, 127169689Skan const char *imultilib, int cxx_stdinc) 128132718Skan{ 129132718Skan const struct default_include *p; 130132718Skan size_t len; 131132718Skan 132132718Skan if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0) 133132718Skan { 134132718Skan /* Look for directories that start with the standard prefix. 135169689Skan "Translate" them, i.e. replace /usr/local/lib/gcc... with 136132718Skan IPREFIX and search them first. */ 137132718Skan for (p = cpp_include_defaults; p->fname; p++) 138132718Skan { 139132718Skan if (!p->cplusplus || cxx_stdinc) 140132718Skan { 141132718Skan /* Should we be translating sysrooted dirs too? Assume 142132718Skan that iprefix and sysroot are mutually exclusive, for 143132718Skan now. */ 144132718Skan if (sysroot && p->add_sysroot) 145132718Skan continue; 146132718Skan if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len)) 147132718Skan { 148132718Skan char *str = concat (iprefix, p->fname + len, NULL); 149169689Skan if (p->multilib && imultilib) 150169689Skan str = concat (str, dir_separator_str, imultilib, NULL); 151169689Skan add_path (str, SYSTEM, p->cxx_aware, false); 152132718Skan } 153132718Skan } 154132718Skan } 155132718Skan } 156132718Skan 157132718Skan for (p = cpp_include_defaults; p->fname; p++) 158132718Skan { 159132718Skan if (!p->cplusplus || cxx_stdinc) 160132718Skan { 161132718Skan char *str; 162132718Skan 163132718Skan /* Should this directory start with the sysroot? */ 164132718Skan if (sysroot && p->add_sysroot) 165132718Skan str = concat (sysroot, p->fname, NULL); 166132718Skan else 167132718Skan str = update_path (p->fname, p->component); 168132718Skan 169169689Skan if (p->multilib && imultilib) 170169689Skan str = concat (str, dir_separator_str, imultilib, NULL); 171169689Skan 172169689Skan add_path (str, SYSTEM, p->cxx_aware, false); 173132718Skan } 174132718Skan } 175132718Skan} 176132718Skan 177132718Skan/* For each duplicate path in chain HEAD, keep just the first one. 178132718Skan Remove each path in chain HEAD that also exists in chain SYSTEM. 179132718Skan Set the NEXT pointer of the last path in the resulting chain to 180132718Skan JOIN, unless it duplicates JOIN in which case the last path is 181132718Skan removed. Return the head of the resulting chain. Any of HEAD, 182132718Skan JOIN and SYSTEM can be NULL. */ 183169689Skan 184132718Skanstatic struct cpp_dir * 185132718Skanremove_duplicates (cpp_reader *pfile, struct cpp_dir *head, 186132718Skan struct cpp_dir *system, struct cpp_dir *join, 187132718Skan int verbose) 188132718Skan{ 189132718Skan struct cpp_dir **pcur, *tmp, *cur; 190132718Skan struct stat st; 191132718Skan 192132718Skan for (pcur = &head; *pcur; ) 193132718Skan { 194132718Skan int reason = REASON_QUIET; 195132718Skan 196132718Skan cur = *pcur; 197132718Skan 198132718Skan if (stat (cur->name, &st)) 199132718Skan { 200132718Skan /* Dirs that don't exist are silently ignored, unless verbose. */ 201132718Skan if (errno != ENOENT) 202132718Skan cpp_errno (pfile, CPP_DL_ERROR, cur->name); 203132718Skan else 204169689Skan { 205169689Skan /* If -Wmissing-include-dirs is given, warn. */ 206169689Skan cpp_options *opts = cpp_get_options (pfile); 207169689Skan if (opts->warn_missing_include_dirs && cur->user_supplied_p) 208169689Skan cpp_errno (pfile, CPP_DL_WARNING, cur->name); 209169689Skan reason = REASON_NOENT; 210169689Skan } 211132718Skan } 212132718Skan else if (!S_ISDIR (st.st_mode)) 213132718Skan cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0, 214132718Skan "%s: not a directory", cur->name); 215132718Skan else 216132718Skan { 217132718Skan INO_T_COPY (cur->ino, st.st_ino); 218132718Skan cur->dev = st.st_dev; 219132718Skan 220132718Skan /* Remove this one if it is in the system chain. */ 221132718Skan reason = REASON_DUP_SYS; 222132718Skan for (tmp = system; tmp; tmp = tmp->next) 223169689Skan if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev 224169689Skan && cur->construct == tmp->construct) 225132718Skan break; 226132718Skan 227132718Skan if (!tmp) 228132718Skan { 229132718Skan /* Duplicate of something earlier in the same chain? */ 230132718Skan reason = REASON_DUP; 231132718Skan for (tmp = head; tmp != cur; tmp = tmp->next) 232169689Skan if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev 233169689Skan && cur->construct == tmp->construct) 234132718Skan break; 235132718Skan 236132718Skan if (tmp == cur 237132718Skan /* Last in the chain and duplicate of JOIN? */ 238132718Skan && !(cur->next == NULL && join 239132718Skan && INO_T_EQ (cur->ino, join->ino) 240169689Skan && cur->dev == join->dev 241169689Skan && cur->construct == join->construct)) 242132718Skan { 243132718Skan /* Unique, so keep this directory. */ 244132718Skan pcur = &cur->next; 245132718Skan continue; 246132718Skan } 247132718Skan } 248132718Skan } 249132718Skan 250132718Skan /* Remove this entry from the chain. */ 251132718Skan *pcur = cur->next; 252132718Skan free_path (cur, verbose ? reason: REASON_QUIET); 253132718Skan } 254132718Skan 255132718Skan *pcur = join; 256132718Skan return head; 257132718Skan} 258132718Skan 259132718Skan/* Merge the four include chains together in the order quote, bracket, 260132718Skan system, after. Remove duplicate dirs (as determined by 261132718Skan INO_T_EQ()). 262132718Skan 263132718Skan We can't just merge the lists and then uniquify them because then 264132718Skan we may lose directories from the <> search path that should be 265169689Skan there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is 266169689Skan however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if 267169689Skan written -iquote bar -Ifoo -Iquux. */ 268169689Skan 269132718Skanstatic void 270132718Skanmerge_include_chains (cpp_reader *pfile, int verbose) 271132718Skan{ 272132718Skan /* Join the SYSTEM and AFTER chains. Remove duplicates in the 273132718Skan resulting SYSTEM chain. */ 274132718Skan if (heads[SYSTEM]) 275132718Skan tails[SYSTEM]->next = heads[AFTER]; 276132718Skan else 277132718Skan heads[SYSTEM] = heads[AFTER]; 278132718Skan heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose); 279132718Skan 280132718Skan /* Remove duplicates from BRACKET that are in itself or SYSTEM, and 281132718Skan join it to SYSTEM. */ 282132718Skan heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM], 283132718Skan heads[SYSTEM], verbose); 284132718Skan 285132718Skan /* Remove duplicates from QUOTE that are in itself or SYSTEM, and 286132718Skan join it to BRACKET. */ 287132718Skan heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM], 288132718Skan heads[BRACKET], verbose); 289132718Skan 290132718Skan /* If verbose, print the list of dirs to search. */ 291132718Skan if (verbose) 292132718Skan { 293132718Skan struct cpp_dir *p; 294132718Skan 295260011Spfg fprintf (stderr, "%s", _("#include \"...\" search starts here:\n")); 296132718Skan for (p = heads[QUOTE];; p = p->next) 297132718Skan { 298132718Skan if (p == heads[BRACKET]) 299260011Spfg fprintf (stderr, "%s", _("#include <...> search starts here:\n")); 300132718Skan if (!p) 301132718Skan break; 302132718Skan fprintf (stderr, " %s\n", p->name); 303132718Skan } 304260011Spfg fprintf (stderr, "%s", _("End of search list.\n")); 305132718Skan } 306132718Skan} 307132718Skan 308132718Skan/* Use given -I paths for #include "..." but not #include <...>, and 309132718Skan don't search the directory of the present file for #include "...". 310132718Skan (Note that -I. -I- is not the same as the default setup; -I. uses 311132718Skan the compiler's working dir.) */ 312132718Skanvoid 313132718Skansplit_quote_chain (void) 314132718Skan{ 315132718Skan heads[QUOTE] = heads[BRACKET]; 316132718Skan tails[QUOTE] = tails[BRACKET]; 317132718Skan heads[BRACKET] = NULL; 318132718Skan tails[BRACKET] = NULL; 319132718Skan /* This is NOT redundant. */ 320132718Skan quote_ignores_source_dir = true; 321132718Skan} 322132718Skan 323169689Skan/* Add P to the chain specified by CHAIN. */ 324169689Skan 325169689Skanvoid 326169689Skanadd_cpp_dir_path (cpp_dir *p, int chain) 327169689Skan{ 328169689Skan if (tails[chain]) 329169689Skan tails[chain]->next = p; 330169689Skan else 331169689Skan heads[chain] = p; 332169689Skan tails[chain] = p; 333169689Skan} 334169689Skan 335132718Skan/* Add PATH to the include chain CHAIN. PATH must be malloc-ed and 336132718Skan NUL-terminated. */ 337132718Skanvoid 338169689Skanadd_path (char *path, int chain, int cxx_aware, bool user_supplied_p) 339132718Skan{ 340169689Skan cpp_dir *p; 341132718Skan 342132718Skan#if defined (HAVE_DOS_BASED_FILE_SYSTEM) 343132718Skan /* Convert all backslashes to slashes. The native CRT stat() 344169689Skan function does not recognize a directory that ends in a backslash 345132718Skan (unless it is a drive root dir, such "c:\"). Forward slashes, 346132718Skan trailing or otherwise, cause no problems for stat(). */ 347132718Skan char* c; 348132718Skan for (c = path; *c; c++) 349132718Skan if (*c == '\\') *c = '/'; 350132718Skan#endif 351132718Skan 352169689Skan p = XNEW (cpp_dir); 353132718Skan p->next = NULL; 354132718Skan p->name = path; 355132718Skan if (chain == SYSTEM || chain == AFTER) 356132718Skan p->sysp = 1 + !cxx_aware; 357132718Skan else 358132718Skan p->sysp = 0; 359169689Skan p->construct = 0; 360169689Skan p->user_supplied_p = user_supplied_p; 361132718Skan 362169689Skan add_cpp_dir_path (p, chain); 363132718Skan} 364132718Skan 365132718Skan/* Exported function to handle include chain merging, duplicate 366132718Skan removal, and registration with cpplib. */ 367132718Skanvoid 368132718Skanregister_include_chains (cpp_reader *pfile, const char *sysroot, 369169689Skan const char *iprefix, const char *imultilib, 370169689Skan int stdinc, int cxx_stdinc, int verbose) 371132718Skan{ 372132718Skan static const char *const lang_env_vars[] = 373132718Skan { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH", 374132718Skan "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" }; 375132718Skan cpp_options *cpp_opts = cpp_get_options (pfile); 376132718Skan size_t idx = (cpp_opts->objc ? 2: 0); 377132718Skan 378132718Skan if (cpp_opts->cplusplus) 379132718Skan idx++; 380132718Skan else 381132718Skan cxx_stdinc = false; 382132718Skan 383132718Skan /* CPATH and language-dependent environment variables may add to the 384132718Skan include chain. */ 385132718Skan add_env_var_paths ("CPATH", BRACKET); 386132718Skan add_env_var_paths (lang_env_vars[idx], SYSTEM); 387132718Skan 388169689Skan target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc); 389169689Skan 390132718Skan /* Finally chain on the standard directories. */ 391132718Skan if (stdinc) 392169689Skan add_standard_paths (sysroot, iprefix, imultilib, cxx_stdinc); 393132718Skan 394169689Skan target_c_incpath.extra_includes (sysroot, iprefix, stdinc); 395169689Skan 396132718Skan merge_include_chains (pfile, verbose); 397132718Skan 398132718Skan cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET], 399132718Skan quote_ignores_source_dir); 400132718Skan} 401169689Skan#if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES) 402169689Skanstatic void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED, 403169689Skan const char *iprefix ATTRIBUTE_UNUSED, 404169689Skan int stdinc ATTRIBUTE_UNUSED) 405169689Skan{ 406169689Skan} 407169689Skan#endif 408169689Skan 409169689Skan#ifndef TARGET_EXTRA_INCLUDES 410169689Skan#define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int 411169689Skan#endif 412169689Skan#ifndef TARGET_EXTRA_PRE_INCLUDES 413169689Skan#define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int 414169689Skan#endif 415169689Skan 416169689Skanstruct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES }; 417169689Skan 418