Introduce a special CPU for Windows and add msys compiler toolchain.

We might revisit what the default toolchain for Windows should be, but
this CL uses msys to simplify bootstrapping.

--
MOS_MIGRATED_REVID=113665255
diff --git a/src/BUILD b/src/BUILD
index 48afe1a..9741c3d 100644
--- a/src/BUILD
+++ b/src/BUILD
@@ -8,6 +8,7 @@
     srcs = select({
         ":darwin": ["//src/main/native:libunix.dylib"],
         ":darwin_x86_64": ["//src/main/native:libunix.dylib"],
+        ":windows": [],
         "//conditions:default": ["//src/main/native:libunix.so"],
     }),
     visibility = [
@@ -186,6 +187,6 @@
 
 config_setting(
     name = "windows",
-    values = {"compiler": "windows_msys64"},
+    values = {"cpu": "x64_windows"},
     visibility = ["//visibility:public"],
 )
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
index 985d5e4..167fc8c 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
@@ -450,6 +450,12 @@
             return "darwin";
           case FREEBSD:
             return "freebsd";
+          case WINDOWS:
+            switch (CPU.getCurrent()) {
+              case X86_64:
+                return "x64_windows";
+            }
+            break; // We only support x64 Windows for now.
           case LINUX:
             switch (CPU.getCurrent()) {
               case X86_32:
diff --git a/src/main/tools/BUILD b/src/main/tools/BUILD
index c212f8e..4950106 100644
--- a/src/main/tools/BUILD
+++ b/src/main/tools/BUILD
@@ -43,6 +43,7 @@
         "//src:darwin": [],
         "//src:darwin_x86_64": [],
         "//src:freebsd": [],
+        "//src:windows": [],
         "//conditions:default": [
             ":process-tools",
             ":network-tools",
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index 8416f3f..fd0a445 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -30,6 +30,7 @@
         ":cc-compiler-darwin",
         ":cc-compiler-freebsd",
         ":cc-compiler-local",
+        ":cc-compiler-x64_windows",
         ":empty",
     ],
 )
@@ -104,6 +105,20 @@
     supports_param_files = 0,
 )
 
+cc_toolchain(
+    name = "cc-compiler-x64_windows",
+    all_files = ":empty",
+    compiler_files = ":empty",
+    cpu = "local",
+    dwp_files = ":empty",
+    dynamic_runtime_libs = [":empty"],
+    linker_files = ":empty",
+    objcopy_files = ":empty",
+    static_runtime_libs = [":empty"],
+    strip_files = ":empty",
+    supports_param_files = 0,
+)
+
 filegroup(
     name = "srcs",
     srcs = glob(["**"]),
diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL
index 0a47bb7..1552fd7 100644
--- a/tools/cpp/CROSSTOOL
+++ b/tools/cpp/CROSSTOOL
@@ -26,6 +26,10 @@
   cpu: "arm"
   toolchain_identifier: "local_linux"
 }
+default_toolchain {
+  cpu: "x64_windows"
+  toolchain_identifier: "local_windows_msys64"
+}
 
 toolchain {
   abi_version: "armeabi-v7a"
@@ -405,7 +409,7 @@
   host_system_name: "local"
   needsPic: false
   target_libc: "local"
-  target_cpu: "k8"
+  target_cpu: "x64_windows"
   target_system_name: "local"
   toolchain_identifier: "local_windows_mingw"
 
@@ -439,7 +443,7 @@
   host_system_name: "local"
   needsPic: false
   target_libc: "local"
-  target_cpu: "k8"
+  target_cpu: "x64_windows"
   target_system_name: "local"
   toolchain_identifier: "local_windows_msys64_mingw64"
 
@@ -472,7 +476,7 @@
   host_system_name: "local"
   needsPic: false
   target_libc: "local"
-  target_cpu: "k8"
+  target_cpu: "x64_windows"
   target_system_name: "local"
   toolchain_identifier: "local_windows_clang"
 
@@ -498,3 +502,39 @@
   tool_path { name: "objdump" path: "C:/Program Files (x86)/LLVM/bin/objdump" }
   tool_path { name: "strip" path: "C:/Program Files (x86)/LLVM/bin/strip" }
 }
+
+toolchain {
+   abi_version: "local"
+   abi_libc_version: "local"
+   builtin_sysroot: ""
+   compiler: "windows_msys64"
+   host_system_name: "local"
+   needsPic: false
+   target_libc: "local"
+   target_cpu: "x64_windows"
+   target_system_name: "local"
+   toolchain_identifier: "local_windows_msys64"
+
+   tool_path { name: "ar" path: "C:/msys64/usr/bin/ar" }
+   tool_path { name: "compat-ld" path: "C:/msys64/usr/bin/ld" }
+   tool_path { name: "cpp" path: "C:/msys64/usr/bin/cpp" }
+   tool_path { name: "dwp" path: "C:/msys64/usr/bin/dwp" }
+   # Use gcc instead of g++ so that C will compile correctly.
+   tool_path { name: "gcc" path: "C:/msys64/usr/bin/gcc" }
+   cxx_flag: "-std=gnu++0x"
+   linker_flag: "-lstdc++"
+   # TODO(bazel-team): In theory, the path here ought to exactly match the path
+   # used by gcc. That works because bazel currently doesn't track files at
+   # absolute locations and has no remote execution, yet. However, this will need
+   # to be fixed, maybe with auto-detection?
+   cxx_builtin_include_directory: "C:/msys64/"
+   cxx_builtin_include_directory: "/usr/"
+   tool_path { name: "gcov" path: "C:/msys64/usr/bin/gcov" }
+   tool_path { name: "ld" path: "C:/msys64/usr/bin/ld" }
+   tool_path { name: "nm" path: "C:/msys64/usr/bin/nm" }
+   tool_path { name: "objcopy" path: "C:/msys64/usr/bin/objcopy" }
+   objcopy_embed_flag: "-I"
+   objcopy_embed_flag: "binary"
+   tool_path { name: "objdump" path: "C:/msys64/usr/bin/objdump" }
+   tool_path { name: "strip" path: "C:/msys64/usr/bin/strip" }
+}