MSVC toolchain changes for building TensorFlow

--
Change-Id: If41817e3889f118a849a29cfb03ac0f7e07d5557
Reviewed-on: https://bazel-review.googlesource.com/#/c/3582
MOS_MIGRATED_REVID=121924420
diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL
index 34c6e85..74a8d4e 100644
--- a/tools/cpp/CROSSTOOL
+++ b/tools/cpp/CROSSTOOL
@@ -563,7 +563,10 @@
   target_libc: "msvcrt140"
   default_python_version: "python2.7"
   cxx_builtin_include_directory: "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/INCLUDE"
-  cxx_builtin_include_directory: "C:/Program Files (x86)/Windows Kits/10/include/10.0.10240.0/ucrt"
+  cxx_builtin_include_directory: "C:/Program Files (x86)/Windows Kits/10/include/"
+  cxx_builtin_include_directory: "C:/Program Files (x86)/Windows Kits/8.1/include/"
+  cxx_builtin_include_directory: "C:/Program Files (x86)/GnuWin32/include/"
+  cxx_builtin_include_directory: "C:/python_27_amd64/files/include"
   tool_path {
     name: "ar"
     path: "wrapper/bin/msvc_link.bat"
@@ -609,7 +612,7 @@
   needsPic: false
 
   compiler_flag: "-m64"
-
+  compiler_flag: "/D__inline__=__inline"
   # TODO(pcloudy): Review those flags below, they should be defined by cl.exe
   compiler_flag: "/DOS_WINDOWS=OS_WINDOWS"
   compiler_flag: "/DCOMPILER_MSVC"
@@ -620,26 +623,17 @@
   compiler_flag: "/DNOMINMAX"
   compiler_flag: "/DPRAGMA_SUPPORTED"
   # Platform defines.
-  compiler_flag: "/D_WIN32"
-  compiler_flag: "/DWIN32"
-  compiler_flag: "/D_WIN32_WINNT=0x0502"
-  compiler_flag: "/DWINVER=0x0502"
-  compiler_flag: "/D_MSC_VER=1900"
-  compiler_flag: "/D_WINDOWS"
+  compiler_flag: "/D_WIN32_WINNT=0x0600"
   # Turn off warning messages.
   compiler_flag: "/D_CRT_SECURE_NO_DEPRECATE"
   compiler_flag: "/D_CRT_SECURE_NO_WARNINGS"
   compiler_flag: "/D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS"
   # Use math constants (M_PI, etc.) from the math library
   compiler_flag: "/D_USE_MATH_DEFINES"
-  # Don't bloat namespace with incompatible winsock versions.
-  compiler_flag: "/DWIN32_LEAN_AND_MEAN"
 
   # Useful options to have on for compilation.
   # Suppress startup banner.
   compiler_flag: "/nologo"
-  # Treat warnings as errors.
-  compiler_flag: "/WX"
   # Increase the capacity of object files to 2^32 sections.
   compiler_flag: "/bigobj"
   # Allocate 500MB for precomputed headers.
diff --git a/tools/cpp/wrapper/bin/pydir/msvc_cl.py b/tools/cpp/wrapper/bin/pydir/msvc_cl.py
index 9a867f6..b8d4c8a 100644
--- a/tools/cpp/wrapper/bin/pydir/msvc_cl.py
+++ b/tools/cpp/wrapper/bin/pydir/msvc_cl.py
@@ -34,6 +34,7 @@
     ('-O0', ['/Od']),
     ('-Os', ['/O1']),
     ('-O2', ['/O2']),
+    ('-g0', []),
     ('-g', ['/MTd']),
     ('-fexceptions', ['/U_HAS_EXCEPTIONS', '/D_HAS_EXCEPTIONS=1', '/EHsc']),
     ('-fomit-frame-pointer', ['/Oy']),
@@ -71,6 +72,7 @@
     ('-B(.+)', []),
     ('-static', []),
     ('-shared', []),
+    ('-std=(.+)', []),
 ]
 
 
diff --git a/tools/cpp/wrapper/bin/pydir/msvc_link.py b/tools/cpp/wrapper/bin/pydir/msvc_link.py
index 8c61fcb..e081ab6 100644
--- a/tools/cpp/wrapper/bin/pydir/msvc_link.py
+++ b/tools/cpp/wrapper/bin/pydir/msvc_link.py
@@ -15,6 +15,7 @@
 """Wrapper script for executing the Microsoft Linker."""
 
 import os
+import shutil
 import sys
 import msvc_tools
 
@@ -28,6 +29,7 @@
     (('rcs.*', '(.+)'), ['/OUT:$PATH0']),
     (('-o', '(.+)'), ['/OUT:$PATH0']),
     ('-B(.+)', []),
+    ('-lpthread', []),
     ('-l(.+)', ['lib$0.so']),
     ('-L(.+)', ['/LIBPATH:$PATH0']),
     ('-static', []),
@@ -68,9 +70,14 @@
 
     # Find the output file name.
     name = ''
+    self.output_dll_file = None
     for arg in parser.options:
       if '/OUT:' in arg:
         name = arg[5:]
+        # if output file ends with .so.exe, we generate dll library.
+        if name.endswith('.so.exe'):
+          default_args.append('/DLL')
+          self.output_dll_file = os.path.normpath(name[0:-7])
         break
     if not name:
       raise msvc_tools.Error('No output file name specified!')
@@ -116,8 +123,12 @@
           else:
             default_args.insert(0, 'libcmt.lib')
 
-      return self.RunBinary(tool, default_args + parser.options,
-                            parser.target_arch, parser)
+      ret_code = self.RunBinary(tool, default_args + parser.options,
+                                parser.target_arch, parser)
+      if not ret_code and self.output_dll_file:
+        shutil.copyfile(self.output_dll_file + '.so.exe',
+                        self.output_dll_file + '.dll')
+      return ret_code
 
 
 def main(argv):
diff --git a/tools/cpp/wrapper/bin/pydir/msvc_tools.py b/tools/cpp/wrapper/bin/pydir/msvc_tools.py
index 6e1f07c..9d3162b 100644
--- a/tools/cpp/wrapper/bin/pydir/msvc_tools.py
+++ b/tools/cpp/wrapper/bin/pydir/msvc_tools.py
@@ -32,6 +32,8 @@
 PLATFORM_SDK_PATH = 'C:\\Program Files (x86)\\Windows Kits'
 PLATFORM_SDK_VERSION = '10.0.10240.0'
 TMP_PATH = 'C:\\Windows\\Temp'
+PYTHON_PATH = 'C:\\python_27_amd64\\files'
+GNU_PATH = 'C:\\Program Files (x86)\\GnuWin32'
 
 
 class Error(Exception):
@@ -339,6 +341,8 @@
     path = x86_paths if build_arch == 'x86' else x64_paths
 
     include = [
+        os.path.join(GNU_PATH, 'include'),
+        os.path.join(PYTHON_PATH, 'include'),
         os.path.join(VC_PATH, 'VC\\INCLUDE'),
         os.path.join(VC_PATH, 'VC\\ATLMFC\\INCLUDE'),
         os.path.join(PLATFORM_SDK_PATH, '10\\include', PLATFORM_SDK_VERSION,
@@ -350,29 +354,39 @@
         os.path.join(PLATFORM_SDK_PATH, 'NETFXSDK\\4.6.1\\include\\um'),
     ]
 
+    common_lib_paths = [
+        os.path.join(GNU_PATH, 'bin'),
+        os.path.join(GNU_PATH, 'lib'),
+        os.path.join(PYTHON_PATH, 'libs'),
+    ]
+
     x86_lib_path = [
         os.path.join(VC_PATH, 'VC\\bin'),
         os.path.join(VC_PATH, 'VC\\LIB'),
         os.path.join(VC_PATH, 'VC\\ATLMFC\\LIB'),
-        os.path.join(VC_PATH,
-                     'VC\\redist\\x86\\Microsoft.VC' + VC_VERSION + '.CRT'),
+        os.path.join(VC_PATH, 'VC\\redist\\x86\\Microsoft.VC' + VC_VERSION +
+                     '.CRT'),
         os.path.join(PLATFORM_SDK_PATH, '10\\lib', PLATFORM_SDK_VERSION,
                      'ucrt\\x86'),
         os.path.join(PLATFORM_SDK_PATH, '8.1\\lib\\winv6.3\\um\\x86'),
         os.path.join(PLATFORM_SDK_PATH, 'NETFXSDK\\4.6.1\\lib\\um\\x86'),
-    ]
+        os.path.join(GNU_PATH, 'bin\\x86'),
+        os.path.join(GNU_PATH, 'lib\\x86'),
+    ] + common_lib_paths
 
     x64_lib_path = [
         os.path.join(VC_PATH, 'VC\\bin\\x86_amd64'),
         os.path.join(VC_PATH, 'VC\\LIB\\amd64'),
         os.path.join(VC_PATH, 'VC\\ATLMFC\\LIB\\amd64'),
-        os.path.join(VC_PATH,
-                     'VC\\redist\\x64\\Microsoft.VC' + VC_VERSION + '.CRT'),
+        os.path.join(VC_PATH, 'VC\\redist\\x64\\Microsoft.VC' + VC_VERSION +
+                     '.CRT'),
         os.path.join(PLATFORM_SDK_PATH, '10\\lib', PLATFORM_SDK_VERSION,
                      'ucrt\\x64'),
         os.path.join(PLATFORM_SDK_PATH, '8.1\\lib\\winv6.3\\um\\x64'),
         os.path.join(PLATFORM_SDK_PATH, 'NETFXSDK\\4.6.1\\lib\\um\\x64'),
-    ]
+        os.path.join(GNU_PATH, 'bin\\x64'),
+        os.path.join(GNU_PATH, 'lib\\x64'),
+    ] + common_lib_paths
 
     lib = x86_lib_path if build_arch == 'x86' else x64_lib_path
 
@@ -403,6 +417,8 @@
         '.*warning LNK4044: unrecognized option \'/link\'; ignored.*\n',
         '.*warning LNK4221: This object file does not define any '
         'previously.*\n',
+        # Comment the following line if you want to see warning messages
+        '.*warning C.*\n',
         '\r\n',
         '\n\r',
     ]
@@ -443,6 +459,8 @@
     else:
       cmd = [binary] + args
     # Save stderr output to a temporary in case we need it.
+    # Unconmment the following line to see what exact command is executed.
+    # print "Running: " + " ".join(cmd)
     proc = subprocess.Popen(cmd,
                             env=build_env,
                             stdout=subprocess.PIPE,