Windows: Add static_link_msvcrt and related features

This is to replace using USE_DYNAMIC_CRT env variable to configure
msvcrt linking in CROSSTOOL.

If user applies static_link_msvcrt feature to a specific target,
Bazel will choose the correct options for statically linking msvcrt.
If static_link_msvcrt is not specified, Bazel uses options for dynamically linking
msvcrt by default.

https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx

Change-Id: Ia078dfb528de9ffdd8a11d392db9eb3f34463b09
PiperOrigin-RevId: 170021927
diff --git a/tools/cpp/CROSSTOOL.tpl b/tools/cpp/CROSSTOOL.tpl
index 4952d62..9f5a22d 100644
--- a/tools/cpp/CROSSTOOL.tpl
+++ b/tools/cpp/CROSSTOOL.tpl
@@ -796,47 +796,85 @@
   }
 
   feature {
-    name: 'link_crt_library'
-    flag_set {
-      action: 'c-compile'
-      action: 'c++-compile'
-      flag_group {
-        # The flag is filled by cc_configure.
-        # The default option is /MT, set USE_DYNAMIC_CRT=1 to change it to /MD
-        flag: "%{crt_option}"
-      }
-    }
-    flag_set {
-      action: 'c++-link-executable'
-      action: 'c++-link-dynamic-library'
-      flag_group {
-      # The flag is filled by cc_configure.
-        # The default value is libcmt.lib, set USE_DYNAMIC_CRT=1 to change it to msvcrt.lib
-        flag: "/DEFAULTLIB:%{crt_library}"
-      }
-    }
+    name: 'static_link_msvcrt'
   }
 
   feature {
-    name: 'link_crt_debug_library'
+    name: 'static_link_msvcrt_no_debug'
     flag_set {
       action: 'c-compile'
       action: 'c++-compile'
       flag_group {
-        # The flag is filled by cc_configure.
-        # The default option is /MTd, set USE_DYNAMIC_CRT=1 to change it to /MDd
-        flag: "%{crt_debug_option}"
+        flag: "/MT"
       }
     }
     flag_set {
       action: 'c++-link-executable'
       action: 'c++-link-dynamic-library'
       flag_group {
-        # The flag is filled by cc_configure.
-        # The default value is libcmtd.lib, set USE_DYNAMIC_CRT=1 to change it to msvcrtd.lib
-        flag: "/DEFAULTLIB:%{crt_debug_library}"
+        flag: "/DEFAULTLIB:libcmt.lib"
       }
     }
+    requires: { feature: 'fastbuild'}
+    requires: { feature: 'opt'}
+  }
+
+  feature {
+    name: 'dynamic_link_msvcrt_no_debug'
+    flag_set {
+      action: 'c-compile'
+      action: 'c++-compile'
+      flag_group {
+        flag: "/MD"
+      }
+    }
+    flag_set {
+      action: 'c++-link-executable'
+      action: 'c++-link-dynamic-library'
+      flag_group {
+        flag: "/DEFAULTLIB:msvcrt.lib"
+      }
+    }
+    requires: { feature: 'fastbuild'}
+    requires: { feature: 'opt'}
+  }
+
+  feature {
+    name: 'static_link_msvcrt_debug'
+    flag_set {
+      action: 'c-compile'
+      action: 'c++-compile'
+      flag_group {
+        flag: "/MTd"
+      }
+    }
+    flag_set {
+      action: 'c++-link-executable'
+      action: 'c++-link-dynamic-library'
+      flag_group {
+        flag: "/DEFAULTLIB:libcmtd.lib"
+      }
+    }
+    requires: { feature: 'dbg'}
+  }
+
+  feature {
+    name: 'dynamic_link_msvcrt_debug'
+    flag_set {
+      action: 'c-compile'
+      action: 'c++-compile'
+      flag_group {
+        flag: "/MDd"
+      }
+    }
+    flag_set {
+      action: 'c++-link-executable'
+      action: 'c++-link-dynamic-library'
+      flag_group {
+        flag: "/DEFAULTLIB:msvcrtd.lib"
+      }
+    }
+    requires: { feature: 'dbg'}
   }
 
   feature {
@@ -857,7 +895,6 @@
         flag: "/INCREMENTAL:NO"
       }
     }
-    implies: 'link_crt_debug_library'
     implies: 'generate_pdb_file'
   }
 
@@ -879,7 +916,6 @@
         flag: "/INCREMENTAL:NO"
       }
     }
-    implies: 'link_crt_library'
     implies: 'generate_pdb_file'
   }
 
@@ -892,7 +928,6 @@
         flag: "/O2"
       }
     }
-    implies: 'link_crt_library'
   }
 
   feature {