incpath.c 15 KB

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