[PATCH] fixincludes: fix fixincludes for MinGW

2016-09-22 Thread Tadek Kijkowski
The fixincl executable uses system function to call applyfix or to
direcly patch a header file, with parameters enclosed in single
quotes. This problem is that MinGW system function just calls cmd.exe,
which doesn't strip quotes from parameters and completely ignores
quotes for embedding spaces in parameters. The MinGW system function
also doesn't allow for newlines in executed command parameters. As a
result fixincludes doesn't wotk at on when trying to build a cross
compiler with mingw as host.

On MinGW host, this patch adds system_with_shell function, which
transforms command passed to system function is following way:
- Enclose entire command in double quotes
- Prepend shell executable name, taken from environment variable SHELL
- Replace each occurence of newline with '$'\n'' sequence which is
understood by bash and ksh (it is assumed that all newlines are
embedded in command parameters enclosed in single quotes)

2016-09-23 Tadek Kijkowski (tkijkow...@gmail.com)

* fixincl.c: Added system_with_shell for MinGW host.


Index: fixincludes/fixincl.c
===
--- fixincludes/fixincl.c   (revision 240386)
+++ fixincludes/fixincl.c   (working copy)
@@ -170,7 +170,111 @@
   exit (EXIT_SUCCESS);
 }

+#ifdef __MINGW32__

+/* Count number of \c needle character instances in string */
+static int
+count_chars ( char* str, char needle )
+{
+  int instances = 0;
+
+  while (str)
+{
+   str = strchr (str, needle);
+   if (str)
+ {
+   ++str;
+   ++instances;
+ }
+}
+
+  return instances;
+}
+
+/* On Mingw32 system(3) will just start cmd by default.
+   Try using unix style shell passed via SHELL env. variable.
+ */
+
+/* Call system(3) function, but prepend ${SHELL} -c to the command,
+   replace newlines with '$'\n'' and enclose command with double quotes.
+ */
+static int
+system_with_shell ( char* s )
+{
+  static const char z_shell_start_args[] = " -c \"";
+  static const char z_shell_end_args[] = "\"";
+  static const char z_shell_newline[] = "'$'\\n''";
+
+  char* env_shell;
+  char* long_cmd;
+  char* cmd_endp;
+  size_t cmd_size;
+  int sys_result;
+  int newline_cnt;
+  char* nl_scan;
+
+  /* If SHELL variable is not defined just call standard shell function */
+  env_shell = getenv ("SHELL");
+  if (env_shell == NULL)
+return system (s);
+
+  /* Count number of newlines in command */
+  newline_cnt = count_chars(s, '\n');
+
+  /* Allocate enough memory to fit newly created command string */
+  cmd_size = strlen (env_shell) + (sizeof (z_shell_start_args) - 1)
+   + strlen (s) + newline_cnt * (sizeof(z_shell_newline) - 1 - 1)
+   + (sizeof (z_shell_end_args) - 1) + 1;
+
+  long_cmd = XNEWVEC (char, cmd_size);
+
+  /* Start with ${SHELL} -c " */
+  strcpy (long_cmd, env_shell);
+  strcat (long_cmd, z_shell_start_args);
+
+  /* End pointer for appending pieces of command while replacing newlines */
+  cmd_endp = long_cmd + strlen(long_cmd);
+  nl_scan = s;
+  while (nl_scan != NULL)
+{
+  char* next_nl = strchr (nl_scan, '\n');
+  if (next_nl)
+{
+  /* Append part of command between newlines */
+  size_t part_len = next_nl - nl_scan;
+  memcpy(cmd_endp, nl_scan, part_len);
+  cmd_endp += part_len;
+
+  /* Append newline replacement */
+  memcpy(cmd_endp, z_shell_newline, sizeof(z_shell_newline));
+  cmd_endp += sizeof(z_shell_newline) - 1;
+
+  /* Skip newline in src */
+  ++next_nl;
+}
+  else
+{
+  /* Last portion */
+  strcpy (cmd_endp, nl_scan);
+}
+  nl_scan = next_nl;
+}
+
+  /* Closing quote */
+  strcat (long_cmd, z_shell_end_args);
+
+  sys_result = system (long_cmd);
+
+  free ( (void*) long_cmd);
+
+  return sys_result;
+}
+
+#define system system_with_shell
+
+#endif /* defined(__MINGW32__) */
+
+
 static void
 do_version (void)
 {


[PATCH] gcc: Fix sysroot relative paths for MinGW

2016-09-22 Thread Tadek Kijkowski
Prevent paths relative to sysroot directory from being transformed to
Windows form with MSYS prefix.
See: http://www.mingw.org/wiki/Posix_path_conversion

2016-09-23  Tadek Kijkowski  

* gcc/Makefile.in: Fix sysroot relative paths for MinGW


Index: gcc/Makefile.in
===
--- gcc/Makefile.in(revision 240386)
+++ gcc/Makefile.in(working copy)
@@ -603,6 +603,18 @@
 # UNSORTED
 # 

+# MSYS will zealously translate all paths to Windows form,
+# so /usr/include becomes c:/msysX/usr/include.
+# If sysroot is specified this is undesirable, so this function converts
+# /usr/include to //usr\include, which will become /usr/include
+# again when passed to gcc.
+ifneq ($(and @TARGET_SYSTEM_ROOT@,$(filter %-mingw32,$(host))),)
+sysroot_relative_path = $(if $(2),$$(echo '$(1)' | tr '/' '\\' | sed
's,^\\,//,'),$(1))
+else
+sysroot_relative_path = $(1)
+endif
+
+
 # Directory in which the compiler finds libraries etc.
 libsubdir = 
$(libdir)/gcc/$(real_target_noncanonical)/$(version)$(accel_dir_suffix)
 # Directory in which the compiler finds executables
@@ -2751,14 +2763,14 @@
 PREPROCESSOR_DEFINES = \
   -DGCC_INCLUDE_DIR=\"$(libsubdir)/include\" \
   -DFIXED_INCLUDE_DIR=\"$(libsubdir)/include-fixed\" \
-  -DGPLUSPLUS_INCLUDE_DIR=\"$(gcc_gxx_include_dir)\" \
+  -DGPLUSPLUS_INCLUDE_DIR=\"$(call
sysroot_relative_path,$(gcc_gxx_include_dir),$(filter-out
0,$(gcc_gxx_include_dir_add_sysroot)))\" \
   -DGPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT=$(gcc_gxx_include_dir_add_sysroot) \
-  
-DGPLUSPLUS_TOOL_INCLUDE_DIR=\"$(gcc_gxx_include_dir)/$(target_noncanonical)\"
\
-  -DGPLUSPLUS_BACKWARD_INCLUDE_DIR=\"$(gcc_gxx_include_dir)/backward\" \
+  -DGPLUSPLUS_TOOL_INCLUDE_DIR=\"$(call
sysroot_relative_path,$(gcc_gxx_include_dir)/$(target_noncanonical),$(filter-out
0,$(gcc_gxx_include_dir_add_sysroot)))\" \
+  -DGPLUSPLUS_BACKWARD_INCLUDE_DIR=\"$(call
sysroot_relative_path,$(gcc_gxx_include_dir)/backward,$(filter-out
0,$(gcc_gxx_include_dir_add_sysroot)))\" \
   -DLOCAL_INCLUDE_DIR=\"$(local_includedir)\" \
   -DCROSS_INCLUDE_DIR=\"$(CROSS_SYSTEM_HEADER_DIR)\" \
   -DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\" \
-  -DNATIVE_SYSTEM_HEADER_DIR=\"$(NATIVE_SYSTEM_HEADER_DIR)\" \
+  -DNATIVE_SYSTEM_HEADER_DIR=\"$(call
sysroot_relative_path,$(NATIVE_SYSTEM_HEADER_DIR),yes)\" \
   -DPREFIX=\"$(prefix)/\" \
   -DSTANDARD_EXEC_PREFIX=\"$(libdir)/gcc/\" \
   @TARGET_SYSTEM_ROOT_DEFINE@


Re: [PATCH] fixincludes: fix fixincludes for MinGW

2016-09-29 Thread Tadek Kijkowski
Hold on. I have much improved version almost ready. It passes all the tests now.

2016-09-30 0:15 GMT+02:00 Bruce Korb :
> I usually try to catch emails with "fixincludes" in the title.
> Can I please get a copy of the original patch?  Thanks.
>
>
> On 09/29/16 11:44, Jeff Law wrote:
>>
>> On 09/22/2016 11:26 PM, Tadek Kijkowski wrote:
>>>
>>> The fixincl executable uses system function to call applyfix or to
>>> direcly patch a header file, with parameters enclosed in single
>>> quotes. This problem is that MinGW system function just calls cmd.exe,
>>> which doesn't strip quotes from parameters and completely ignores
>>> quotes for embedding spaces in parameters. The MinGW system function
>>> also doesn't allow for newlines in executed command parameters. As a
>>> result fixincludes doesn't wotk at on when trying to build a cross
>>> compiler with mingw as host.
>>>
>>> On MinGW host, this patch adds system_with_shell function, which
>>> transforms command passed to system function is following way:
>>> - Enclose entire command in double quotes
>>> - Prepend shell executable name, taken from environment variable SHELL
>>> - Replace each occurence of newline with '$'\n'' sequence which is
>>> understood by bash and ksh (it is assumed that all newlines are
>>> embedded in command parameters enclosed in single quotes)
>>>
>>> 2016-09-23 Tadek Kijkowski (tkijkow...@gmail.com)
>>>
>>> * fixincl.c: Added system_with_shell for MinGW host.
>>>
>>>
>>> Index: fixincludes/fixincl.c
>>> ===
>>> --- fixincludes/fixincl.c   (revision 240386)
>>> +++ fixincludes/fixincl.c   (working copy)
>>> @@ -170,7 +170,111 @@
>>>exit (EXIT_SUCCESS);
>>>  }
>>>
>>> +#ifdef __MINGW32__
>>>
>>> +/* Count number of \c needle character instances in string */
>>> +static int
>>> +count_chars ( char* str, char needle )
>>
>> Formatting it.  This should be:
>>
>> count_chars (char* str, char needle)
>>
>> Note the lack of horizontal whitespace after the open paren or before
>> the close paren.  Similarly for system_with_shell declaration below.
>>
>> Wouldn't this be better named "count_occurrences_of_char" or
>> "count_instances_of_char"?
>>
>>
>>
>>
>>> +
>>> +  /* Allocate enough memory to fit newly created command string */
>>> +  cmd_size = strlen (env_shell) + (sizeof (z_shell_start_args) - 1)
>>> +   + strlen (s) + newline_cnt * (sizeof(z_shell_newline) - 1
>>> - 1)
>>> +   + (sizeof (z_shell_end_args) - 1) + 1;
>>
>> Why use sizeof (foo) - 1 rather than just strlen (foo) here?  Note that
>> GCC can compute the string length as a compile time constant, so you're
>> not gaining any performance by using sizeof here and strlen seems clearer.
>>
>>
>>
>>> +
>>> +  long_cmd = XNEWVEC (char, cmd_size);
>>> +
>>> +  /* Start with ${SHELL} -c " */
>>> +  strcpy (long_cmd, env_shell);
>>> +  strcat (long_cmd, z_shell_start_args);
>>> +
>>> +  /* End pointer for appending pieces of command while replacing
>>> newlines */
>>> +  cmd_endp = long_cmd + strlen(long_cmd);
>>
>> Formatting nit.  Space between function name and its argument list.
>>
>>
>>> +  nl_scan = s;
>>> +  while (nl_scan != NULL)
>>> +{
>>> +  char* next_nl = strchr (nl_scan, '\n');
>>
>> Formatting nit.  char *next_nl.
>>
>>
>>> +  if (next_nl)
>>> +{
>>> +  /* Append part of command between newlines */
>>> +  size_t part_len = next_nl - nl_scan;
>>> +  memcpy(cmd_endp, nl_scan, part_len);
>>
>> Formatting nit, space between function name and its argument list.
>>
>>> +  cmd_endp += part_len;
>>> +
>>> +  /* Append newline replacement */
>>> +  memcpy(cmd_endp, z_shell_newline, sizeof(z_shell_newline));
>>
>> Smilarly, space between functio nname and its argument list.
>>
>>> +  cmd_endp += sizeof(z_shell_newline) - 1;
>>
>> Here too.
>>
>>> +
>>> +  free ( (void*) long_cmd);
>>
>> free (long_cmd);
>>
>> Can you fix those various (minor) issue and resubmit.  I think it'll be
>> OK for the trunk with those changes.
>>
>> jeff
>>
>>
>


Re: [PATCH] fixincludes: fix fixincludes for MinGW

2016-09-29 Thread Tadek Kijkowski
The fixincl executable uses system function to call applyfix or to
direcly patch a header file, with parameters enclosed in single
quotes. The problem is that MinGW system function just calls cmd.exe,
which doesn't strip quotes from parameters and completely ignores
quotes for embedding spaces in parameters. The MinGW system function
also doesn't allow embedding newlines in executed command parameters.
As a result fixincludes doesn't work at all when trying to build a cross
compiler with mingw as host.

On MinGW host, this patch adds system_with_shell function, which
transforms command passed to system function is following way:
* Enclose entire command in double quotes
* Prepend shell executable name, taken from environment variable SHELL
* Replace each occurence of newline with '$'\n'' sequence which is
understood by bash and ksh
* Escape special characters

Second attempt:

Moved system_with_shell to fixlib.c. Escape some special characters
located between single quotes as enclosing single-quoted string in double
quotes nullifies single quotes. Fixed passing file name to apply_fix.
Fixed "test" tests and "shell" fixes, also for DJGPP.

Convert line all endings to unix in check.sh to avoid false positives.

Followed Jeff's and Bruce's advices:
Fixed formatting.
Call system_with_shell explicitly. If it's not needed it just expands
to "system".

On MinGW (mingw64 actually) all fixinclude tests pass.
On DJGPP all but two tests pass - if you manage to get it to compile at all.

2016-09-30  Tadek Kijkowski  

* check.tpl: Convert line endings to unix on test outputs
* fixfixes.c: Fixed passing file name to apply_fix when
SEPARATE_FIX_PROC is defined
* fixincl.c: Use system_with_shell, fixes for MinGW and DJGPP
* fixlib.c, fixlib.h: Added system_with_shell and fix_path_separators

Index: fixincludes/check.tpl
===
--- fixincludes/check.tpl (revision 240643)
+++ fixincludes/check.tpl (working copy)
@@ -123,6 +123,11 @@
 exec < ${TESTDIR}/LIST
 while read f
 do
+  if [ -n "$MSYSTEM" -o -n "$DJGPP" ]
+  then
+# On MinGW and DJGPP convert line endings to avoid false positives
+mv $f $f.dos; tr -d '\r' < $f.dos > $f; rm $f.dos
+  fi
   if [ ! -f ${TESTBASE}/$f ]
   then
 echo "Newly fixed header:  $f" >&2
Index: fixincludes/fixfixes.c
===
--- fixincludes/fixfixes.c (revision 240643)
+++ fixincludes/fixfixes.c (working copy)
@@ -790,7 +790,8 @@
   return EXIT_FAILURE;
 }

-  apply_fix (pFix, argv[1]);
+  /* Second parameter of apply_fix is file name */
+  apply_fix (pFix, argv[2]);
   fclose (stdout);
   fclose (stdin);
   unlink (argv[4]);
Index: fixincludes/fixincl.c
===
--- fixincludes/fixincl.c (revision 240643)
+++ fixincludes/fixincl.c (working copy)
@@ -74,9 +74,12 @@
 #endif /* DO_STATS */

 const char incl_quote_pat[] = "^[ \t]*#[ \t]*include[ \t]*\"[^/]";
-tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
 regex_t incl_quote_re;

+#ifndef SEPARATE_FIX_PROC
+tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
+#endif
+
 static void do_version (void) ATTRIBUTE_NORETURN;
 char *load_file (const char *);
 void run_compiles (void);
@@ -188,7 +191,7 @@
   puts (zBuf + 5);
   exit (strcmp (run_shell (zBuf), program_id));
 #else
-  exit (system (zBuf));
+  exit (system_with_shell (zBuf));
 #endif
 }

@@ -275,6 +278,11 @@
   /* NULL as the first argument to `tempnam' causes it to DTRT
  wrt the temporary directory where the file will be created.  */
   pz_temp_file = tempnam( NULL, "fxinc" );
+
+#if defined(__MINGW32__)
+  fix_path_separators (pz_temp_file);
+#endif
+
 # endif

   signal (SIGQUIT, SIG_IGN);
@@ -566,7 +574,27 @@
   free ((void *) pz_res);
   return res;
 }
+#elif defined(__MINGW32__) || defined(__DJGPP__)
+static int
+test_test (tTestDesc* p_test, char* pz_test_file)
+{
+  tSCC cmd_fmt[] =
+#if defined(__DJGPP__)
+"file=%s; test %s >/dev/null 2>/dev/null";
 #else
+"file=%s; test %s > /dev/null 2>&1";
+#endif
+  int res;
+
+  char *cmd_buf = XNEWVEC (char, strlen(cmd_fmt) +
strlen(pz_test_file) + strlen(p_test->pz_test_text));
+
+  sprintf (cmd_buf, cmd_fmt, pz_test_file, p_test->pz_test_text);
+  res = system_with_shell (cmd_buf);
+
+  free (cmd_buf);
+  return res ? SKIP_FIX : APPLY_FIX;
+}
+#else
 /*
  *  IF we are in MS-DOS land, then whatever shell-type test is required
  *  will, by definition, fail
@@ -887,7 +915,7 @@
   else /* NOT an "internal" fix: */
 {
   size_t parg_size;
-#ifdef __MSDOS__
+#if defined(__MSDOS__) && !defined(__DJGPP__)
 

Fwd: [PATCH] gcc: Fix sysroot relative paths for MinGW

2016-09-29 Thread Tadek Kijkowski
Can I have plain-text mode, please, gmail?

-- Forwarded message --
From: Tadek Kijkowski 
Date: 2016-09-30 5:16 GMT+02:00
Subject: Re: [PATCH] gcc: Fix sysroot relative paths for MinGW
To: Jeff Law 
CC: gcc-patches@gcc.gnu.org


2016-09-29 21:16 GMT+02:00 Jeff Law :
> On 09/23/2016 12:31 AM, Tadek Kijkowski wrote:
>>
>> Prevent paths relative to sysroot directory from being transformed to
>> Windows form with MSYS prefix.
>> See: http://www.mingw.org/wiki/Posix_path_conversion
>>
>> 2016-09-23  Tadek Kijkowski  
>>
>> * gcc/Makefile.in: Fix sysroot relative paths for MinGW
>>
>>
>> Index: gcc/Makefile.in
>> ===
>> --- gcc/Makefile.in(revision 240386)
>> +++ gcc/Makefile.in(working copy)
>> @@ -603,6 +603,18 @@
>>  # UNSORTED
>>  # 
>>
>> +# MSYS will zealously translate all paths to Windows form,
>> +# so /usr/include becomes c:/msysX/usr/include.
>> +# If sysroot is specified this is undesirable, so this function converts
>> +# /usr/include to //usr\include, which will become /usr/include
>> +# again when passed to gcc.
>> +ifneq ($(and @TARGET_SYSTEM_ROOT@,$(filter %-mingw32,$(host))),)
>> +sysroot_relative_path = $(if $(2),$$(echo '$(1)' | tr '/' '\\' | sed
>> 's,^\\,//,'),$(1))
>> +else
>> +sysroot_relative_path = $(1)
>> +endif
>
> I'd really like to see the documentation here improved.
>
> There's no mention of the second argument's purpose.  And one has to parse
> the ifneq line to understand what it's doing.  Those kinds of things should
> be made clear in a comment.
>

OK. I'll expand it.

>
>> +
>> +
>>  # Directory in which the compiler finds libraries etc.
>>  libsubdir =
>> $(libdir)/gcc/$(real_target_noncanonical)/$(version)$(accel_dir_suffix)
>>  # Directory in which the compiler finds executables
>> @@ -2751,14 +2763,14 @@
>>  PREPROCESSOR_DEFINES = \
>>-DGCC_INCLUDE_DIR=\"$(libsubdir)/include\" \
>>-DFIXED_INCLUDE_DIR=\"$(libsubdir)/include-fixed\" \
>> -  -DGPLUSPLUS_INCLUDE_DIR=\"$(gcc_gxx_include_dir)\" \
>> +  -DGPLUSPLUS_INCLUDE_DIR=\"$(call
>> sysroot_relative_path,$(gcc_gxx_include_dir),$(filter-out
>> 0,$(gcc_gxx_include_dir_add_sysroot)))\" \
>
> So why the $(filter-out 0, )?
>
> I'd really like to avoid being too clever here and write this code in the
> most straightforward way possible.
>

Hmm... that's partially leftover from the abandoned idea to pass
@TARGET_SYSTEM_ROOT@ as second parameter of sysroot_relative_path.
Sysroot is prepended to GPLUSPLUS_INCLUDE_DIR in runtime only if
$(gcc_gxx_include_dir) is 1.
Since sysroot_relative_path checks for non-empty string the easiest
way was to use filter-out. But I agree this way it's confusing.

How about if I change the sysroot_relative_path function to explicitly
check for 1? But still - since $(if) checks for empty string, it will
have to use filter or filter-out.

# MSYS will zealously translate all paths to Windows form, so
/usr/include becomes c:/msysX/usr/include.
# If sysroot is specified this is undesirable, so this function
converts /usr/include to //usr\include, which will become /usr/include
# again when passed to gcc.
# This function takes two parameters: first parameter is include
directory path, second parameter tells if the path is relative
# to TARGET_SYSTEM_ROOT.
# If TARGET_SYSTEM_ROOT is not configured or host is not MinGW32, this
function always expands to the unmodified first parameter
# if TARGET_SYSTEM_ROOT is configured and host is MinGW32, but second
parameter is not 1, this function again expands to the unmodified
first parameter
# otherwise, it expands to a shell expression which will transform the
first parameter as described above.
ifneq ($(and @TARGET_SYSTEM_ROOT@,$(filter %-mingw32,$(host))),)
sysroot_relative_path = $(if $(filter 1,$(2)),$$(echo '$(1)' | tr '/'
'\\' | sed 's,^\\,//,'),$(1))
else
sysroot_relative_path = $(1)
endif

...

PREPROCESSOR_DEFINES = \
  -DGCC_INCLUDE_DIR=\"$(libsubdir)/include\" \
  -DFIXED_INCLUDE_DIR=\"$(libsubdir)/include-fixed\" \
  -DGPLUSPLUS_INCLUDE_DIR=\"$(call
sysroot_relative_path,$(gcc_gxx_include_dir),$(gcc_gxx_include_dir_add_sysroot))\"
\
...
  -DNATIVE_SYSTEM_HEADER_DIR=\"$(call
sysroot_relative_path,$(NATIVE_SYSTEM_HEADER_DIR),1)\" \
...

What do you think?

N.B. I'd prefer to use backticks over "$()", but it could clash if
some include paths already contain backtick expressions.

Tadek


Re: [PATCH] gcc: Fix sysroot relative paths for MinGW

2016-10-08 Thread Tadek Kijkowski

Prevent paths relative to sysroot directory from being transformed to Windows 
form with MSYS prefix.

Second attempt:
Moved most changes to x-mingw32. Only thing added to Makefile.in are new 
variables to ease overriding in included file.
Disabled overriding standard lib and include paths (with /mingw/lib and 
/mingw/include) in mingw32.h when TARGET_SYSTEM_ROOT is defined.

Side notes:

I've tested the patch together with patches from 
github.com/Alexpux/MINGW-packages. One of those patches disables overriding 
NATIVE_SYSTEM_HEADER_DIR in config scripts, so it's complement to this patch.

Target s-selftest in gcc fails on MinGW:
/home/tadek/gcc/gcc-build-mingw32-sysroot/./gcc/xgcc 
-B/home/tadek/gcc/gcc-build-mingw32-sysroot/./gcc/ -xc -S -c /dev/null 
-fself-test
cc1.exe: fatal error: input file 'nul.s' is the same as output file
It's enough to specify any output file, like '-o self-test-result.s' to fix the 
issue. Specifying '-o /dev/null' works as well, but I'm not sure if it's safe 
on all systems. It's a topic for separate patch.

Manual at "https://gcc.gnu.org/install/configure.html"; does mention that path specified with|--with-native-system-header-dir is located within sysroot, but it fails 
to mention that the same applies to ||--with-local-prefix. Changelog: 2016-10-08 Tadek Kijkowski 
 * gcc/Makefile.in, gcc/config/i386/x-mingw32: |Fix sysroot relative paths for MinGW

* gcc/config/i386/mingw32.h: Disable overriding default library and 
include paths when sysroot is defined

Index: gcc/Makefile.in
===
--- gcc/Makefile.in (revision 240805)
+++ gcc/Makefile.in (working copy)
@@ -680,6 +680,15 @@
 exeext = @host_exeext@
 build_exeext = @build_exeext@
 
+

+# This allows overriding include paths in host specific Makefile
+# (config/i386/x-mingw32 overrides those variables and local_includedir).
+gplusplus_includedir = $(gcc_gxx_include_dir)
+gplusplus_tool_includedir = $(gcc_gxx_include_dir)/$(target_noncanonical)
+gplusplus_backward_includedir = $(gcc_gxx_include_dir)/backward
+native_system_includedir = $(NATIVE_SYSTEM_HEADER_DIR)
+
+
 # Directory in which to put man pages.
 mandir = @mandir@
 man1dir = $(mandir)/man1
@@ -2757,14 +2766,14 @@
 PREPROCESSOR_DEFINES = \
   -DGCC_INCLUDE_DIR=\"$(libsubdir)/include\" \
   -DFIXED_INCLUDE_DIR=\"$(libsubdir)/include-fixed\" \
-  -DGPLUSPLUS_INCLUDE_DIR=\"$(gcc_gxx_include_dir)\" \
+  -DGPLUSPLUS_INCLUDE_DIR=\"$(gplusplus_includedir)\" \
   -DGPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT=$(gcc_gxx_include_dir_add_sysroot) \
-  
-DGPLUSPLUS_TOOL_INCLUDE_DIR=\"$(gcc_gxx_include_dir)/$(target_noncanonical)\" \
-  -DGPLUSPLUS_BACKWARD_INCLUDE_DIR=\"$(gcc_gxx_include_dir)/backward\" \
+  -DGPLUSPLUS_TOOL_INCLUDE_DIR=\"$(gplusplus_tool_includedir)\" \
+  -DGPLUSPLUS_BACKWARD_INCLUDE_DIR=\"$(gplusplus_backward_includedir)\" \
   -DLOCAL_INCLUDE_DIR=\"$(local_includedir)\" \
   -DCROSS_INCLUDE_DIR=\"$(CROSS_SYSTEM_HEADER_DIR)\" \
   -DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\" \
-  -DNATIVE_SYSTEM_HEADER_DIR=\"$(NATIVE_SYSTEM_HEADER_DIR)\" \
+  -DNATIVE_SYSTEM_HEADER_DIR=\"$(native_system_includedir)\" \
   -DPREFIX=\"$(prefix)/\" \
   -DSTANDARD_EXEC_PREFIX=\"$(libdir)/gcc/\" \
   @TARGET_SYSTEM_ROOT_DEFINE@
Index: gcc/config/i386/mingw32.h
===
--- gcc/config/i386/mingw32.h   (revision 240805)
+++ gcc/config/i386/mingw32.h   (working copy)
@@ -157,6 +161,8 @@
 fvtable-verify=std:vtv_end.o%s} \
   crtend.o%s"
 
+#if !defined(TARGET_SYSTEM_ROOT)

+
 /* Override startfile prefix defaults.  */
 #ifndef STANDARD_STARTFILE_PREFIX_1
 #define STANDARD_STARTFILE_PREFIX_1 "/mingw/lib/"
@@ -170,6 +176,8 @@
 #undef NATIVE_SYSTEM_HEADER_DIR
 #define NATIVE_SYSTEM_HEADER_DIR "/mingw/include"
 
+#endif /* !defined(TARGET_SYSTEM_ROOT) */

+
 /* Output STRING, a string representing a filename, to FILE.
We canonicalize it to be in Unix format (backslashes are replaced
forward slashes.  */
Index: gcc/config/i386/x-mingw32
===
--- gcc/config/i386/x-mingw32   (revision 240805)
+++ gcc/config/i386/x-mingw32   (working copy)
@@ -16,11 +16,45 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 #
+
+# MSYS will zealously translate all paths to Windows form, so /usr/include 
becomes c:/msysX/usr/include.
+# This is undesirable when TARGET_SYSTEM_ROOT is specified, so this function 
converts /usr/include to //usr\include,
+# which will become /usr/include again when passed to gcc.
+
+# This function takes two parameters: first parameter is include directory 
path, second parameter tells
+#