Patch to allow sysroot-relative include options

Joseph S. Myers
Thu Mar 22 21:27:00 GMT 2007

The GNU linker has a feature that if you pass a -L option starting with 
'=', it will be interpreted relative to the sysroot.  So you can use 
-L=/usr/X11R6/lib and the linker will search in /usr/X11R6/lib within the 
sysroot, without you needing to know where the sysroot is or vary your 
command line according to its location.

I've applied to mainline this patch to add the same feature to the 
compiler's include options (-I, -isystem, -idirafter, -iquote), so you can 
use -I=/usr/include/libxml2, for example.

Bootstrapped with no regressions on i686-pc-linux-gnu; the feature itself 
tested with a cross to arm-none-linux-gnueabi, including relocation of the 

The conversion of '=' to the sysroot happens quite late, because the 
-isysroot option is passed by the driver to cc1 after the user's -I 
options, so the '=' needs to be left until that point.

2007-03-22  Joseph Myers  <>

	* c-incpath.c (add_sysroot_to_chain): New.
	(merge_include_chains): Add sysroot argument.  Call
	add_sysroot_to_chain if sysrooted.
	(register_include_chains): Update call to merge_include_chains.
	* doc/cppopts.texi: Document use of '=' in include directory

Index: c-incpath.c
--- c-incpath.c	(revision 123133)
+++ c-incpath.c	(working copy)
@@ -51,7 +51,8 @@
 static void add_env_var_paths (const char *, int);
 static void add_standard_paths (const char *, const char *, const char *, int);
 static void free_path (struct cpp_dir *, int);
-static void merge_include_chains (cpp_reader *, int);
+static void merge_include_chains (const char *, cpp_reader *, int);
+static void add_sysroot_to_chain (const char *, int);
 static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
 					   struct cpp_dir *,
 					   struct cpp_dir *, int);
@@ -282,6 +283,19 @@
   return head;
+/* Add SYSROOT to any user-supplied paths in CHAIN starting with
+   "=".  */
+static void
+add_sysroot_to_chain (const char *sysroot, int chain)
+  struct cpp_dir *p;
+  for (p = heads[chain]; p != NULL; p = p->next)
+    if (p->name[0] == '=' && p->user_supplied_p)
+      p->name = concat (sysroot, p->name + 1, NULL);
 /* Merge the four include chains together in the order quote, bracket,
    system, after.  Remove duplicate dirs (as determined by
@@ -293,8 +307,17 @@
    written -iquote bar -Ifoo -Iquux.  */
 static void
-merge_include_chains (cpp_reader *pfile, int verbose)
+merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
+  /* Add the sysroot to user-supplied paths starting with "=".  */
+  if (sysroot)
+    {
+      add_sysroot_to_chain (sysroot, QUOTE);
+      add_sysroot_to_chain (sysroot, BRACKET);
+      add_sysroot_to_chain (sysroot, SYSTEM);
+      add_sysroot_to_chain (sysroot, AFTER);
+    }
   /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
      resulting SYSTEM chain.  */
   if (heads[SYSTEM])
@@ -419,7 +442,7 @@
   target_c_incpath.extra_includes (sysroot, iprefix, stdinc);
-  merge_include_chains (pfile, verbose);
+  merge_include_chains (sysroot, pfile, verbose);
   cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
Index: doc/cppopts.texi
--- doc/cppopts.texi	(revision 123133)
+++ doc/cppopts.texi	(working copy)
@@ -65,6 +65,8 @@
 (@pxref{System Headers})
 @end ifset
+If @var{dir} begins with @code{=}, then the @code{=} will be replaced
+by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}.
 @item -o @var{file}
 @opindex o
@@ -464,6 +466,8 @@
 Search @var{dir} for header files, but do it @emph{after} all
 directories specified with @option{-I} and the standard system directories
 have been exhausted.  @var{dir} is treated as a system include directory.
+If @var{dir} begins with @code{=}, then the @code{=} will be replaced
+by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}.
 @item -iprefix @var{prefix}
 @opindex iprefix
@@ -499,6 +503,8 @@
 @ifset cppmanual
 @xref{System Headers}.
 @end ifset
+If @var{dir} begins with @code{=}, then the @code{=} will be replaced
+by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}.
 @item -iquote @var{dir}
 @opindex iquote
@@ -509,6 +515,8 @@
 @ifset cppmanual
 @xref{Search Path}.
 @end ifset
+If @var{dir} begins with @code{=}, then the @code{=} will be replaced
+by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}.
 @item -fdollars-in-identifiers
 @opindex fdollars-in-identifiers

Joseph S. Myers

More information about the Gcc-patches mailing list