PATCH: --with-sysroot support for GCC

Daniel Jacobowitz drow@mvista.com
Mon Jan 27 22:55:00 GMT 2003


On Thu, Jan 16, 2003 at 04:20:57PM -0500, Daniel Jacobowitz wrote:
> It's been two months since I last posted this patch, so here it is again. 
> Updated and retest on i386-linux and a sysrooted mipsel-linux cross
> compiler.  I believe the configury parts have been pretty much approved by
> Alexandre and the cpplib parts by Neil; the gcc.c bits still need approval.
> 
> Sysroot is this really neat way of organizing cross-compilers to full target
> systems (i.e. something which could run a compiler natively).  You just say
> --with-sysroot=/path/to/root when configuring, and in /path/to/root you put
> a copy of the target filesystem.  Binutils will search for libraries and
> startfiles there, GCC will look there for headers, et cetera.
> 
> Since this patch was last posted binutils and GDB have both gotten support
> for --with-sysroot.  As a result if you configure with this patch on top of
> a combined tree, everything will just work.  It's as simple as:
>   $srcdir/configure --with-sysroot=/mysysrootpath --target=mytarget
> 
> OK?

Could someone _please_ review this patch?

> 2003-01-16  Daniel Jacobowitz  <drow@mvista.com>
> 
> 	* Makefile.in (PREPROCESSOR_DEFINES): Add
> 	@TARGET_SYSTEM_ROOT_DEFINE@.
> 	* configure.in (PREFIX_INCLUDE_DIR): Don't define if $with_sysroot
> 	is specified or if building a cross compiler.
> 	(TARGET_SYSTEM_ROOT_DEFINE): Add TARGET_SYSTEM_ROOT_RELOCATABLE
> 	if the sysroot is under $exec_prefix.
> 	* configure: Regenerated.
> 	* cppdefault.h: Use native include paths if TARGET_SYSTEM_ROOT is
> 	defined.
> 	(struct default_include): Add add_sysroot field.
> 	(cpp_SYSROOT): Declare.
> 	* cppdefault.c (cpp_include_defaults): Fill in add_sysroot
> 	field.
> 	(cpp_SYSROOT): New variable.
> 	* cppinit.c (cpp_create_reader): Initialize
> 	CPP_OPTION (pfile, sysroot).
> 	(init_standard_includes): Handle add_sysroot.  Do not
> 	add unrelocated copies of relocated directories.
> 	(COMMAND_LINE_OPTIONS): Add -isysroot.
> 	(cpp_handle_option): Handle -isysroot.
> 	* cpplib.h (struct cpp_options): Add sysroot member.
> 	* gcc.c (The Specs Language): Update description of %I.
> 	(target_system_root_changed): New variable.
> 	(process_command): Conditionalize make_relative_prefix call
> 	on !VMS and TARGET_SYSTEM_ROOT_RELOCATABLE.  Set
> 	target_system_root_changed.
> 	(do_spec_1): Add -isysroot to %I.
> 	* doc/invoke.texi (Spec Files): Update description of %I.
> 	* doc/install.texi (--with-sysroot): Update comment about
> 	relocation.
> 
> Index: gcc/Makefile.in
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/Makefile.in,v
> retrieving revision 1.972
> diff -u -p -r1.972 Makefile.in
> --- gcc/Makefile.in	2003/01/15 01:21:44	1.972
> +++ gcc/Makefile.in	2003/01/16 20:09:25
> @@ -2217,7 +2217,8 @@ PREPROCESSOR_DEFINES = \
>    -DGPLUSPLUS_BACKWARD_INCLUDE_DIR=\"$(gcc_gxx_include_dir)/backward\" \
>    -DLOCAL_INCLUDE_DIR=\"$(local_includedir)\" \
>    -DCROSS_INCLUDE_DIR=\"$(CROSS_SYSTEM_HEADER_DIR)\" \
> -  -DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\"
> +  -DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\" \
> +  @TARGET_SYSTEM_ROOT_DEFINE@
>  
>  LIBCPP_OBJS =	cpplib.o cpplex.o cppmacro.o cppexp.o cppfiles.o cpptrad.o \
>  		cpphash.o cpperror.o cppinit.o cppdefault.o cppmain.o \
> Index: gcc/configure.in
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/configure.in,v
> retrieving revision 1.634
> diff -u -p -r1.634 configure.in
> --- gcc/configure.in	2003/01/15 10:53:22	1.634
> +++ gcc/configure.in	2003/01/16 20:09:25
> @@ -486,6 +486,22 @@ AC_ARG_WITH(sysroot,
>     
>   TARGET_SYSTEM_ROOT_DEFINE='-DTARGET_SYSTEM_ROOT=\"$(TARGET_SYSTEM_ROOT)\"'
>   CROSS_SYSTEM_HEADER_DIR='$(TARGET_SYSTEM_ROOT)$(NATIVE_SYSTEM_HEADER_DIR)'
> +
> + if test "x$exec_prefix" = xNONE; then
> +  if test "x$prefix" = xNONE; then
> +   test_prefix=/usr/local
> +  else
> +   test_prefix=$prefix
> +  fi
> + else
> +  test_prefix=$exec_prefix
> + fi
> + case ${TARGET_SYSTEM_ROOT} in
> + ${test_prefix}*)
> +   t="$TARGET_SYSTEM_ROOT_DEFINE -DTARGET_SYSTEM_ROOT_RELOCATABLE"
> +   TARGET_SYSTEM_ROOT_DEFINE="$t"
> +   ;;
> + esac
>  ], [
>   TARGET_SYSTEM_ROOT=
>   TARGET_SYSTEM_ROOT_DEFINE='-DTARGET_SYSTEM_ROOT=0'
> @@ -2341,7 +2357,8 @@ case "$target" in
>      ;;
>  esac
>  
> -if test "$prefix" != "/usr" && test "x$prefix" != "x$local_prefix" ; then
> +if test x$with_sysroot = x && x$host = x$target \
> +   && test "$prefix" != "/usr" && test "x$prefix" != "x$local_prefix" ; then
>    AC_DEFINE_UNQUOTED(PREFIX_INCLUDE_DIR, "$prefix/include")
>  fi
>  
> Index: gcc/cppdefault.c
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/cppdefault.c,v
> retrieving revision 1.5
> diff -u -p -r1.5 cppdefault.c
> --- gcc/cppdefault.c	2002/12/16 18:19:14	1.5
> +++ gcc/cppdefault.c	2003/01/16 20:09:25
> @@ -35,44 +35,44 @@ const struct default_include cpp_include
>  = {
>  #ifdef GPLUSPLUS_INCLUDE_DIR
>      /* Pick up GNU C++ generic include files.  */
> -    { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
> +    { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1, 0 },
>  #endif
>  #ifdef GPLUSPLUS_TOOL_INCLUDE_DIR
>      /* Pick up GNU C++ target-dependent include files.  */
> -    { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1 },
> +    { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1, 0 },
>  #endif
>  #ifdef GPLUSPLUS_BACKWARD_INCLUDE_DIR
>      /* Pick up GNU C++ backward and deprecated include files.  */
> -    { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1 },
> +    { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1, 0 },
>  #endif
>  #ifdef LOCAL_INCLUDE_DIR
>      /* /usr/local/include comes before the fixincluded header files.  */
> -    { LOCAL_INCLUDE_DIR, 0, 0, 1 },
> +    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1 },
>  #endif
>  #ifdef PREFIX_INCLUDE_DIR
> -    { PREFIX_INCLUDE_DIR, 0, 0, 1 },
> +    { PREFIX_INCLUDE_DIR, 0, 0, 1, 0 },
>  #endif
>  #ifdef GCC_INCLUDE_DIR
>      /* This is the dir for fixincludes and for gcc's private headers.  */
> -    { GCC_INCLUDE_DIR, "GCC", 0, 0 },
> +    { GCC_INCLUDE_DIR, "GCC", 0, 0, 0 },
>  #endif
>  #ifdef CROSS_INCLUDE_DIR
>      /* One place the target system's headers might be.  */
> -    { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
> +    { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0 },
>  #endif
>  #ifdef TOOL_INCLUDE_DIR
>      /* Another place the target system's headers might be.  */
> -    { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
> +    { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1, 0 },
>  #endif
>  #ifdef SYSTEM_INCLUDE_DIR
>      /* Some systems have an extra dir of include files.  */
> -    { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
> +    { SYSTEM_INCLUDE_DIR, 0, 0, 0, 1 },
>  #endif
>  #ifdef STANDARD_INCLUDE_DIR
>      /* /usr/include comes dead last.  */
> -    { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
> +    { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 1 },
>  #endif
> -    { 0, 0, 0, 0 }
> +    { 0, 0, 0, 0, 0 }
>    };
>  #endif /* no INCLUDE_DEFAULTS */
>  
> @@ -82,4 +82,10 @@ const size_t cpp_GCC_INCLUDE_DIR_len = s
>  #else
>  const char cpp_GCC_INCLUDE_DIR[] = "";
>  const size_t cpp_GCC_INCLUDE_DIR_len = 0;
> +#endif
> +
> +#ifdef TARGET_SYSTEM_ROOT
> +const char *cpp_SYSROOT = TARGET_SYSTEM_ROOT;
> +#else
> +const char *cpp_SYSROOT = "";
>  #endif
> Index: gcc/cppdefault.h
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/cppdefault.h,v
> retrieving revision 1.6
> diff -u -p -r1.6 cppdefault.h
> --- gcc/cppdefault.h	2002/05/16 19:02:59	1.6
> +++ gcc/cppdefault.h	2003/01/16 20:09:25
> @@ -34,12 +34,12 @@ Foundation, 59 Temple Place - Suite 330,
>  #define STANDARD_INCLUDE_COMPONENT 0
>  #endif
>  
> -#ifdef CROSS_COMPILE
> -#undef LOCAL_INCLUDE_DIR
> -#undef SYSTEM_INCLUDE_DIR
> -#undef STANDARD_INCLUDE_DIR
> +#if defined (CROSS_COMPILE) && !defined (TARGET_SYSTEM_ROOT)
> +# undef LOCAL_INCLUDE_DIR
> +# undef SYSTEM_INCLUDE_DIR
> +# undef STANDARD_INCLUDE_DIR
>  #else
> -#undef CROSS_INCLUDE_DIR
> +# undef CROSS_INCLUDE_DIR
>  #endif
>  
>  /* This is the default list of directories to search for include files.
> @@ -61,10 +61,14 @@ struct default_include
>    const int cxx_aware;		/* Includes in this directory don't need to
>  				   be wrapped in extern "C" when compiling
>  				   C++.  */
> +  const int add_sysroot;	/* FNAME should be prefixed by
> +				   cpp_SYSROOT.  */
>  };
>  
>  extern const struct default_include cpp_include_defaults[];
>  extern const char cpp_GCC_INCLUDE_DIR[];
>  extern const size_t cpp_GCC_INCLUDE_DIR_len;
> +
> +extern const char *cpp_SYSROOT;
>  
>  #endif /* ! GCC_CPPDEFAULT_H */
> Index: gcc/cppinit.c
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/cppinit.c,v
> retrieving revision 1.265
> diff -u -p -r1.265 cppinit.c
> --- gcc/cppinit.c	2002/12/16 18:19:15	1.265
> +++ gcc/cppinit.c	2003/01/16 20:32:05
> @@ -528,6 +528,7 @@ cpp_create_reader (lang)
>    CPP_OPTION (pfile, operator_names) = 1;
>    CPP_OPTION (pfile, warn_endif_labels) = 1;
>    CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
> +  CPP_OPTION (pfile, sysroot) = cpp_SYSROOT;
>  
>    CPP_OPTION (pfile, pending) =
>      (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
> @@ -756,46 +757,24 @@ init_standard_includes (pfile)
>  {
>    const struct default_include *p;
>    const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
> +  int default_len, specd_len;
> +  char *default_prefix;
>  
>    /* Search "translated" versions of GNU directories.
>       These have /usr/local/lib/gcc... replaced by specd_prefix.  */
> +  default_len = 0;
>    if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
>      {
>        /* Remove the `include' from /usr/local/lib/gcc.../include.
>  	 GCC_INCLUDE_DIR will always end in /include.  */
> -      int default_len = cpp_GCC_INCLUDE_DIR_len;
> -      char *default_prefix = (char *) alloca (default_len + 1);
> -      int specd_len = strlen (specd_prefix);
> +      default_len = cpp_GCC_INCLUDE_DIR_len;
> +      default_prefix = (char *) alloca (default_len + 1);
> +      specd_len = strlen (specd_prefix);
>  
>        memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
>        default_prefix[default_len] = '\0';
> -
> -      for (p = cpp_include_defaults; p->fname; p++)
> -	{
> -	  /* Some standard dirs are only for C++.  */
> -	  if (!p->cplusplus
> -	      || (CPP_OPTION (pfile, cplusplus)
> -		  && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
> -	    {
> -	      /* Does this dir start with the prefix?  */
> -	      if (!strncmp (p->fname, default_prefix, default_len))
> -		{
> -		  /* Yes; change prefix and add to search list.  */
> -		  int flen = strlen (p->fname);
> -		  int this_len = specd_len + flen - default_len;
> -		  char *str = (char *) xmalloc (this_len + 1);
> -		  memcpy (str, specd_prefix, specd_len);
> -		  memcpy (str + specd_len,
> -			  p->fname + default_len,
> -			  flen - default_len + 1);
> -
> -		  append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
> -		}
> -	    }
> -	}
>      }
>  
> -  /* Search ordinary names for GNU include directories.  */
>    for (p = cpp_include_defaults; p->fname; p++)
>      {
>        /* Some standard dirs are only for C++.  */
> @@ -803,7 +782,30 @@ init_standard_includes (pfile)
>  	  || (CPP_OPTION (pfile, cplusplus)
>  	      && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
>  	{
> -	  char *str = update_path (p->fname, p->component);
> +	  char *str;
> +
> +	  /* Should this dir start with the sysroot?  */
> +	  if (p->add_sysroot && CPP_OPTION (pfile, sysroot))
> +	    str = concat (CPP_OPTION (pfile, sysroot), p->fname, NULL);
> +
> +	  /* Does this dir start with the prefix?  */
> +	  else if (default_len
> +		   && !strncmp (p->fname, default_prefix, default_len))
> +	    {
> +	      /* Yes; change prefix and add to search list.  */
> +	      int flen = strlen (p->fname);
> +	      int this_len = specd_len + flen - default_len;
> +
> +	      str = (char *) xmalloc (this_len + 1);
> +	      memcpy (str, specd_prefix, specd_len);
> +	      memcpy (str + specd_len,
> +		      p->fname + default_len,
> +		      flen - default_len + 1);
> +	    }
> +
> +	  else
> +	    str = update_path (p->fname, p->component);
> +
>  	  append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
>  	}
>      }
> @@ -1160,6 +1162,7 @@ new_pending_directive (pend, text, handl
>    DEF_OPT("imacros",                  no_fil, OPT_imacros)                    \
>    DEF_OPT("include",                  no_fil, OPT_include)                    \
>    DEF_OPT("iprefix",                  no_pth, OPT_iprefix)                    \
> +  DEF_OPT("isysroot",                 no_dir, OPT_isysroot)                   \
>    DEF_OPT("isystem",                  no_dir, OPT_isystem)                    \
>    DEF_OPT("iwithprefix",              no_dir, OPT_iwithprefix)                \
>    DEF_OPT("iwithprefixbefore",        no_dir, OPT_iwithprefixbefore)
> @@ -1302,6 +1305,10 @@ cpp_handle_option (pfile, argc, argv)
>  	case OPT_iprefix:
>  	  CPP_OPTION (pfile, include_prefix) = arg;
>  	  CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
> +	  break;
> +
> +	case OPT_isysroot:
> +	  CPP_OPTION (pfile, sysroot) = arg;
>  	  break;
>  
>  	case OPT_A:
> Index: gcc/cpplib.h
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/cpplib.h,v
> retrieving revision 1.240
> diff -u -p -r1.240 cpplib.h
> --- gcc/cpplib.h	2003/01/10 02:21:59	1.240
> +++ gcc/cpplib.h	2003/01/16 20:09:25
> @@ -237,6 +237,10 @@ struct cpp_options
>    const char *include_prefix;
>    unsigned int include_prefix_len;
>  
> +  /* Directory prefix for system include directories in the standard search
> +     path.  */
> +  const char *sysroot;
> +
>    /* The language we're preprocessing.  */
>    enum c_lang lang;
>  
> Index: gcc/gcc.c
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/gcc.c,v
> retrieving revision 1.357
> diff -u -p -r1.357 gcc.c
> --- gcc/gcc.c	2003/01/10 02:22:02	1.357
> +++ gcc/gcc.c	2003/01/16 20:09:25
> @@ -204,6 +204,10 @@ static int report_times;
>  
>  static const char *target_system_root = TARGET_SYSTEM_ROOT;
>  
> +/* Nonzero means pass the updated target_system_root to the compiler.  */
> +
> +static int target_system_root_changed;
> +
>  /* Nonzero means write "temp" files in source directory
>     and use the source file's name in them, and don't delete them.  */
>  
> @@ -445,7 +449,9 @@ or with constant text in a single argume
>   %P	like %p, but puts `__' before and after the name of each macro.
>  	(Except macros that already have __.)
>  	This is for ANSI C.
> - %I	Substitute a -iprefix option made from GCC_EXEC_PREFIX.
> + %I	Substitute any of -iprefix (made from GCC_EXEC_PREFIX), -isysroot
> +	(made from TARGET_SYSTEM_ROOT), and -isystem (made from COMPILER_PATH
> +	and -B options) as necessary.
>   %s     current argument is the name of a library or startup file of some sort.
>          Search for that file in a standard list of directories
>  	and substitute the full name found.
> @@ -3829,14 +3835,23 @@ warranty; not even for MERCHANTABILITY o
>  	      concat (tooldir_prefix, "lib", dir_separator_str, NULL),
>  	      "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL, 1);
>  
> +#if defined(TARGET_SYSTEM_ROOT_RELOCATABLE) && !defined(VMS)
> +  /* If the normal TARGET_SYSTEM_ROOT is inside of $exec_prefix,
> +     then consider it to relocate with the rest of the GCC installation
> +     if GCC_EXEC_PREFIX is set.
> +     ``make_relative_prefix'' is not compiled for VMS, so don't call it.  */
>    if (target_system_root && gcc_exec_prefix)
>      {
>        char *tmp_prefix = make_relative_prefix (argv[0],
>  					       standard_bindir_prefix,
>  					       target_system_root);
>        if (tmp_prefix && access_check (tmp_prefix, F_OK) == 0)
> -	target_system_root = tmp_prefix;
> +	{
> +	  target_system_root = tmp_prefix;
> +	  target_system_root_changed = 1;
> +	}
>      }
> +#endif
>  
>    /* More prefixes are enabled in main, after we read the specs file
>       and determine whether this is cross-compilation or not.  */
> @@ -4742,6 +4757,15 @@ do_spec_1 (spec, inswitch, soft_matched_
>  		  /* Make this a separate argument.  */
>  		  do_spec_1 (" ", 0, NULL);
>  		  do_spec_1 (gcc_exec_prefix, 1, NULL);
> +		  do_spec_1 (" ", 0, NULL);
> +		}
> +
> +	      if (target_system_root_changed)
> +		{
> +		  do_spec_1 ("-isysroot", 1, NULL);
> +		  /* Make this a separate argument.  */
> +		  do_spec_1 (" ", 0, NULL);
> +		  do_spec_1 (target_system_root, 1, NULL);
>  		  do_spec_1 (" ", 0, NULL);
>  		}
>  
> Index: gcc/doc/install.texi
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/doc/install.texi,v
> retrieving revision 1.160
> diff -u -p -r1.160 install.texi
> --- gcc/doc/install.texi	2003/01/03 10:16:30	1.160
> +++ gcc/doc/install.texi	2003/01/16 20:09:25
> @@ -895,16 +895,9 @@ searched in there.  The specified direct
>  install tree, unlike the options @option{--with-headers} and
>  @option{--with-libs} that this option obsoletes.  The default value,
>  in case @option{--with-sysroot} is not given an argument, is
> -@option{$@{gcc_tooldir@}/sys-root}.  Starting the pathname with
> -@option{$@{gcc_tooldir@}/}, in such a way that this variable is not
> -expanded by the shell, but rather by the gcc Makefile, enables the
> -target sysroot directory to be relocatable along with the entire
> -install tree, but it will require the gcc tool directory to be created
> -@emph{before} the build is started.  This directory, as well as its
> -intermediate pathnames, can be easily created by running @command{make
> -install-gcc-tooldir} the gcc build directory.  This is not done
> -automatically to avoid touching the install tree without explicit
> -permission.
> +@option{$@{gcc_tooldir@}/sys-root}.  If the specified directory is a
> +subdirectory of @option{$@{exec_prefix@}}, then it will be found relative to
> +the GCC binaries if the installation tree is moved.
>  
>  @item --with-headers
>  @itemx --with-headers=@var{dir}
> Index: gcc/doc/invoke.texi
> ===================================================================
> RCS file: /big/fsf/rsync/gcc-cvs/gcc/gcc/doc/invoke.texi,v
> retrieving revision 1.224
> diff -u -p -r1.224 invoke.texi
> --- gcc/doc/invoke.texi	2003/01/15 22:50:41	1.224
> +++ gcc/doc/invoke.texi	2003/01/16 20:09:25
> @@ -4949,7 +4949,10 @@ predefined macro, except for macros that
>  C@.
>  
>  @item %I
> -Substitute a @option{-iprefix} option made from @env{GCC_EXEC_PREFIX}.
> +Substitute any of @option{-iprefix} (made from @env{GCC_EXEC_PREFIX}),
> +@option{-isysroot} (made from @env{TARGET_SYSTEM_ROOT}), and
> +@option{-isystem} (made from @env{COMPILER_PATH} and @option{-B} options)
> +as necessary.
>  
>  @item %s
>  Current argument is the name of a library or startup file of some sort.
> 

-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer



More information about the Gcc-patches mailing list