Add ASAN support to Tulsi

--
PiperOrigin-RevId: 164602275
MOS_MIGRATED_REVID=164602275
diff --git a/src/Tulsi.xcodeproj/project.pbxproj b/src/Tulsi.xcodeproj/project.pbxproj
index 967ef6e..67bb2e5 100644
--- a/src/Tulsi.xcodeproj/project.pbxproj
+++ b/src/Tulsi.xcodeproj/project.pbxproj
@@ -130,6 +130,7 @@
 		AB18CB663B4A10F9BA45D02F /* TulsiCommandlineParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB18C4E9FB80370FC40B500A /* TulsiCommandlineParser.swift */; };
 		AB18CC4A7B1A308E23A1CE6F /* HeadlessTulsiProjectCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB18CFC29644CD1B3B860056 /* HeadlessTulsiProjectCreator.swift */; };
 		D33C204F1EC108CC00867450 /* tulsi_logging.py in Resources */ = {isa = PBXBuildFile; fileRef = D33C204E1EC108CC00867450 /* tulsi_logging.py */; };
+		D3F78C681F391E9700AE0571 /* bazel_options.py in Resources */ = {isa = PBXBuildFile; fileRef = D3F78C671F391E9700AE0571 /* bazel_options.py */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -325,6 +326,7 @@
 		AB18C7D45D34ACB3B8BC1805 /* BazelIntegrationTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BazelIntegrationTestCase.swift; sourceTree = "<group>"; };
 		AB18CFC29644CD1B3B860056 /* HeadlessTulsiProjectCreator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeadlessTulsiProjectCreator.swift; sourceTree = "<group>"; };
 		D33C204E1EC108CC00867450 /* tulsi_logging.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = tulsi_logging.py; sourceTree = "<group>"; };
+		D3F78C671F391E9700AE0571 /* bazel_options.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = bazel_options.py; sourceTree = "<group>"; };
 		F4469ECE1C5BCA5900BCFAA1 /* TulsiVersion.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = TulsiVersion.xcconfig; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -484,6 +486,7 @@
 			children = (
 				3D329D0D1C4831EF00DFBD0F /* bazel_build.py */,
 				D33C204E1EC108CC00867450 /* tulsi_logging.py */,
+				D3F78C671F391E9700AE0571 /* bazel_options.py */,
 				3D156AE11C1C8D9C00183439 /* bazel_clean.sh */,
 				3DFB7C4A1C81F78000376760 /* command_line_splitter.sh */,
 			);
@@ -798,6 +801,7 @@
 				2D9DB34A1E5DECA40021EAF4 /* XCTRunner.entitlements in Resources */,
 				3D329D0E1C4831EF00DFBD0F /* bazel_build.py in Resources */,
 				D33C204F1EC108CC00867450 /* tulsi_logging.py in Resources */,
+				D3F78C681F391E9700AE0571 /* bazel_options.py in Resources */,
 				3D9926911C29F1410094E098 /* bazel_clean.sh in Resources */,
 				3DFB7C4B1C81F78000376760 /* command_line_splitter.sh in Resources */,
 				3DBEFACD1C2A1F7200119556 /* Localizable.strings in Resources */,
diff --git a/src/TulsiGenerator/Scripts/bazel_build.py b/src/TulsiGenerator/Scripts/bazel_build.py
index 75bf867..27a76e3 100755
--- a/src/TulsiGenerator/Scripts/bazel_build.py
+++ b/src/TulsiGenerator/Scripts/bazel_build.py
@@ -31,6 +31,7 @@
 import textwrap
 import time
 import zipfile
+import bazel_options
 import tulsi_logging
 
 
@@ -687,6 +688,9 @@
 
     bazel_command.extend(options.targets)
 
+    extra_options = bazel_options.BazelOptions(os.environ)
+    bazel_command.extend(extra_options.bazel_feature_flags())
+
     return (bazel_command, 0)
 
   def _RunBazelAndPatchOutput(self, command):
diff --git a/src/TulsiGenerator/Scripts/bazel_options.py b/src/TulsiGenerator/Scripts/bazel_options.py
new file mode 100755
index 0000000..5dc736c
--- /dev/null
+++ b/src/TulsiGenerator/Scripts/bazel_options.py
@@ -0,0 +1,42 @@
+# Copyright 2017 The Tulsi Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the 'License');
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an 'AS IS' BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Logic to translate Xcode options to Bazel options."""
+
+
+class BazelOptions(object):
+  """Converts Xcode features into Bazel command line flags."""
+
+  def __init__(self, xcode_env):
+    """Creates a new BazelOptions object.
+
+    Args:
+      xcode_env: A dictionary of Xcode environment variables.
+
+    Returns:
+      A BazelOptions instance.
+    """
+    self.xcode_env = xcode_env
+
+  def bazel_feature_flags(self):
+    """Returns a list of bazel flags for the current Xcode env configuration."""
+    flags = []
+    if self.xcode_env.get('ENABLE_ADDRESS_SANITIZER') == 'YES':
+      flags.extend([
+          '--features=asan',
+          '--define=apple_bundle_clang_rt=1',
+          '--define=apple_swift_sanitize=address',
+      ])
+
+    return flags
diff --git a/src/TulsiGenerator/TulsiXcodeProjectGenerator.swift b/src/TulsiGenerator/TulsiXcodeProjectGenerator.swift
index 9ad62c6..5bbd136 100644
--- a/src/TulsiGenerator/TulsiXcodeProjectGenerator.swift
+++ b/src/TulsiGenerator/TulsiXcodeProjectGenerator.swift
@@ -49,7 +49,8 @@
     let resourceURLs = XcodeProjectGenerator.ResourceSourcePathURLs(
         buildScript: bundle.url(forResource: "bazel_build", withExtension: "py")!,
         cleanScript: bundle.url(forResource: "bazel_clean", withExtension: "sh")!,
-        extraBuildScripts: [bundle.url(forResource: "tulsi_logging", withExtension: "py")!],
+        extraBuildScripts: [bundle.url(forResource: "tulsi_logging", withExtension: "py")!,
+                            bundle.url(forResource: "bazel_options", withExtension: "py")!],
         postProcessor: bundle.url(forResource: "post_processor",
                                              withExtension: "",
                                              subdirectory: "Utilities")!,