Adding features to MSVC CROSSTOOL

This stops Bazel from passing some of the gcc options to wrapper script.

Options we get rid of:
-c -o -MD -MF -frandom-seed -isystem -iquote -I

--
Change-Id: I416b393c6875a2033d5382a644471c416cd5e8d1
Reviewed-on: https://bazel-review.googlesource.com/#/c/3930
MOS_MIGRATED_REVID=126299789
diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL
index 5a23b58..7562352 100644
--- a/tools/cpp/CROSSTOOL
+++ b/tools/cpp/CROSSTOOL
@@ -662,6 +662,121 @@
 
   linker_flag: "-m64"
 
+  feature {
+    name: 'include_paths'
+    flag_set {
+      action: 'preprocess-assemble'
+      action: 'c-compile'
+      action: 'c++-compile'
+      action: 'c++-header-parsing'
+      action: 'c++-header-preprocessing'
+      action: 'c++-module-compile'
+      flag_group {
+        flag: '/I%{quote_include_paths}'
+      }
+      flag_group {
+        flag: '/I%{include_paths}'
+      }
+      flag_group {
+        flag: '/I%{system_include_paths}'
+      }
+    }
+  }
+
+  feature {
+    name: 'dependency_file'
+    flag_set {
+      action: 'assemble'
+      action: 'preprocess-assemble'
+      action: 'c-compile'
+      action: 'c++-compile'
+      action: 'c++-module-compile'
+      action: 'c++-header-preprocessing'
+      action: 'c++-header-parsing'
+      expand_if_all_available: 'dependency_file'
+      flag_group {
+        flag: '/DEPENDENCY_FILE'
+        flag: '%{dependency_file}'
+      }
+    }
+  }
+
+  # Stop passing -frandom-seed option
+  feature {
+    name: 'random_seed'
+  }
+
+  # This feature is just for enabling flag_set in action_config for -c and -o options during the transitional period
+  feature {
+    name: 'compile_action_flags_in_flag_set'
+  }
+
+  action_config {
+    config_name: 'c-compile'
+    action_name: 'c-compile'
+    tool {
+      tool_path: 'wrapper/bin/msvc_cl.bat'
+    }
+    flag_set {
+      flag_group {
+        flag: '/c'
+        flag: '%{source_file}'
+      }
+    }
+    flag_set {
+      expand_if_all_available: 'output_object_file'
+      flag_group {
+        flag: '/Fo%{output_object_file}'
+      }
+    }
+    flag_set {
+      expand_if_all_available: 'output_assembly_file'
+      flag_group {
+        flag: '/Fa%{output_assembly_file}'
+      }
+    }
+    flag_set {
+      expand_if_all_available: 'output_preprocess_file'
+      flag_group {
+        flag: '/P'
+        flag: '/Fi%{output_preprocess_file}'
+      }
+    }
+  }
+
+  action_config {
+    config_name: 'c++-compile'
+    action_name: 'c++-compile'
+    tool {
+      tool_path: 'wrapper/bin/msvc_cl.bat'
+    }
+    flag_set {
+      flag_group {
+        flag: '/c'
+        flag: '%{source_file}'
+      }
+    }
+    flag_set {
+      expand_if_all_available: 'output_object_file'
+      flag_group {
+        flag: '/Fo%{output_object_file}'
+      }
+    }
+    flag_set {
+      expand_if_all_available: 'output_assembly_file'
+      flag_group {
+        flag: '/Fa%{output_assembly_file}'
+      }
+    }
+    flag_set {
+      expand_if_all_available: 'output_preprocess_file'
+      flag_group {
+        flag: '/P'
+        flag: '/Fi%{output_preprocess_file}'
+      }
+    }
+  }
+
   compilation_mode_flags {
     mode: DBG
     compiler_flag: "/DDEBUG=1"
diff --git a/tools/cpp/wrapper/bin/pydir/msvc_cl.py b/tools/cpp/wrapper/bin/pydir/msvc_cl.py
index b8d4c8a..7983099 100644
--- a/tools/cpp/wrapper/bin/pydir/msvc_cl.py
+++ b/tools/cpp/wrapper/bin/pydir/msvc_cl.py
@@ -26,11 +26,9 @@
     ('-Xcompilation-mode=(dbg|fastbuild|opt)', ['$COMPILATION_MODE']),
     ('-msse', ['/arch:SSE']),
     ('-msse2', ['/arch:SSE2']),
-    ('-c', ['/c']),
     ('-D(.+)', ['/D$0']),
     ('-U(.+)', ['/U$0']),
     ('-E', ['/E']),
-    (('-o', '(.+)'), ['$COMPILE_OUTPUT0']),
     ('-O0', ['/Od']),
     ('-Os', ['/O1']),
     ('-O2', ['/O2']),
@@ -38,18 +36,13 @@
     ('-g', ['/MTd']),
     ('-fexceptions', ['/U_HAS_EXCEPTIONS', '/D_HAS_EXCEPTIONS=1', '/EHsc']),
     ('-fomit-frame-pointer', ['/Oy']),
-    ('-frandom-seed(.+)', []),
     ('-fno-rtti', ['/GR-']),
     ('-frtti', ['/GR']),
     ('-fPIC', []),
 
     # This is unneeded for Windows.
-    (('-isystem', '(.*)'), ['/I$PATH0']),
-    (('-iquote', '(.*)'), ['/I$PATH0']),
-    ('-I(.+)', ['/I$PATH0']),
     (('-include', '(.+)'), ['/FI$PATH0']),
-    ('-MD', []),
-    (('-MF', '(.+)'), ['$GENERATE_DEPS0']),
+    (('/DEPENDENCY_FILE', '(.+)'), ['$GENERATE_DEPS0']),
     ('-w', ['/w']),
     ('-Wall', ['/Wall']),
     ('-Wsign-compare', ['/we4018']),
@@ -68,7 +61,6 @@
     ('-Wno-string-plus-int', []),
     ('-Wl,S', []),  # Stripping is unnecessary since msvc uses pdb files.
     ('-Wl,-rpath(.+)', []),
-    (('-x', r'c\+\+-header'), ['$CREATE_PRECOMPILED_HEADER']),
     ('-B(.+)', []),
     ('-static', []),
     ('-shared', []),
diff --git a/tools/cpp/wrapper/bin/pydir/msvc_tools.py b/tools/cpp/wrapper/bin/pydir/msvc_tools.py
index 9d3162b..f84efc3 100644
--- a/tools/cpp/wrapper/bin/pydir/msvc_tools.py
+++ b/tools/cpp/wrapper/bin/pydir/msvc_tools.py
@@ -19,7 +19,6 @@
 import os
 import re
 import subprocess
-import sys
 
 MAX_PATH = 260  # The maximum number of characters in a Windows path.
 MAX_OPTION_LENGTH = 10  # The maximum length of a compiler/linker option.
@@ -123,10 +122,14 @@
     matched = []
     unmatched = []
     files = []
-    is_pch = False
     while i < len(argv):
       num_matched, action, groups = self._MatchOneArg(argv[i:])
       arg = argv[i]
+      if arg.startswith('/Fo') or arg.startswith('/Fa') or arg.startswith(
+          '/Fi'):
+        self.output_file = arg[3:]
+        self.options.append(
+            '/Fd%s.pdb' % self.NormPath(os.path.splitext(self.output_file)[0]))
       if num_matched == 0:
         # Strip out any .a's that have 0 size, they are header or intermediate
         # dependency libraries and don't contain any code. 0-length files are
@@ -155,13 +158,6 @@
       matched += argv[i:i + num_matched]
       # Handle special options.
       for entry in action:
-        if entry == '$CREATE_PRECOMPILED_HEADER':
-          # The PCH flag comes _first_ on blaze-generated command-lines, so all
-          # we can do is set a flag here since we have not yet parsed any other
-          # options.
-          is_pch = True
-          continue
-
         if entry == '$TARGET_ARCH':
           if arg == '-m32':
             self.target_arch = 'x86'
@@ -205,22 +201,6 @@
                 exit(-1)
               continue
 
-            # Depending on whether we are creating precompiled headers cl.exe
-            # needs different options for specifying the output file.
-            if entry == ('$COMPILE_OUTPUT%d' % g):
-              if is_pch:
-                # Just touch the PCH file so that blaze is happy.
-                with open(value, 'a'):
-                  os.utime(value, None)
-                # Exit since we don't want to actually try to process a PCH.
-                sys.exit(0)
-              else:
-                self.output_file = value
-                self.options.append('/Fo%s' % self.NormPath(value))
-                self.options.append('/Fd%s.pdb' %
-                                    self.NormPath(os.path.splitext(value)[0]))
-              continue
-
             if entry == ('$GENERATE_DEPS%d' % g):
               self.options.append('/showIncludes')
               self.deps_file = value