Auto-download Win SDK on non-Win hosts if 'win' is in target_os in .gclient.

Also, automatically set up a case-insensitive user-space file system mount
for the Windows SDK when on Linux.

This implements https://bugs.chromium.org/p/chromium/issues/detail?id=495204#c82

Bug: 495204
Change-Id: I8c6833570bfffffcfd9b52f31a5ee5e620f46620
Reviewed-on: https://chromium-review.googlesource.com/713999
Reviewed-by: Hans Wennborg <[email protected]>
Commit-Queue: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/master@{#508375}
diff --git a/DEPS b/DEPS
index b2ee7e7..37ce53a 100644
--- a/DEPS
+++ b/DEPS
@@ -721,9 +721,8 @@
     # Update the Windows toolchain if necessary.  Must run before 'clang' below.
     'name': 'win_toolchain',
     'pattern': '.',
-    # TODO(thakis): Put some condition here. Not just host_os == 'win', because
-    # we also need this for (mac|linux) -> win cross builds.
-    'action': ['python', 'src/build/vs_toolchain.py', 'update'],
+    'condition': 'checkout_win',
+    'action': ['python', 'src/build/vs_toolchain.py', 'update', '--force'],
   },
   {
     # Update the Mac toolchain if necessary.
diff --git a/build/vs_toolchain.py b/build/vs_toolchain.py
index e5e2998..d8fe41e 100755
--- a/build/vs_toolchain.py
+++ b/build/vs_toolchain.py
@@ -390,6 +390,30 @@
         depot_tools_win_toolchain):
     import find_depot_tools
     depot_tools_path = find_depot_tools.add_depot_tools_to_path()
+
+    # On Linux, the file system is usually case-sensitive while the Windows
+    # SDK only works on case-insensitive file systems.  If it doesn't already
+    # exist, set up a ciopfs fuse mount to put the SDK in a case-insensitive
+    # part of the file system.
+    toolchain_dir = os.path.join(depot_tools_path, 'win_toolchain', 'vs_files')
+    if sys.platform.startswith('linux') and not os.path.ismount(toolchain_dir):
+      import distutils.spawn
+      ciopfs = distutils.spawn.find_executable('ciopfs')
+      if not ciopfs:
+        # TODO(thakis): Offer to auto-install this?  Or have a
+        # build/install-build-deps-win.sh script and point to that? (Or run
+        # that?)
+        print >>sys.stderr, \
+            "\n\tCouldn't set up case-insensitive mount for Windows SDK."
+        print >>sys.stderr, \
+            "\tPlease run `sudo apt-get install ciopfs` and try again.\n"
+        return 1
+      if not os.path.isdir(toolchain_dir):
+        os.mkdir(toolchain_dir)
+      if not os.path.isdir(toolchain_dir + '.ciopfs'):
+        os.mkdir(toolchain_dir + '.ciopfs')
+      subprocess.check_call([ciopfs, toolchain_dir + '.ciopfs', toolchain_dir])
+
     # Necessary so that get_toolchain_if_necessary.py will put the VS toolkit
     # in the correct directory.
     os.environ['GYP_MSVS_VERSION'] = GetVisualStudioVersion()