Enable ASAN build

Requires Clang 3.1 or higher to be installed.

R=epoger@google.com

Review URL: https://codereview.chromium.org/18205007

git-svn-id: http://skia.googlecode.com/svn/trunk@9891 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/common_conditions.gypi b/gyp/common_conditions.gypi
index 4aa503d..6fabfdc 100644
--- a/gyp/common_conditions.gypi
+++ b/gyp/common_conditions.gypi
@@ -210,6 +210,15 @@
               }],
             ],
           }],
+          [ 'skia_asan_build', {
+            'cflags': [
+              '-fsanitize=address',
+              '-fno-omit-frame-pointer',
+            ],
+            'ldflags': [
+              '-fsanitize=address',
+            ],
+          }],
         ],
       },
     ],
diff --git a/gyp/common_variables.gypi b/gyp/common_variables.gypi
index 3a42fdc..67049e3 100644
--- a/gyp/common_variables.gypi
+++ b/gyp/common_variables.gypi
@@ -142,6 +142,7 @@
     'skia_static_initializers%': '<(skia_static_initializers)',
     'ios_sdk_version%': '6.0',
     'skia_win_debuggers_path%': '<(skia_win_debuggers_path)',
+    'skia_asan_build%': 0,
 
     # These are referenced by our .gypi files that list files (e.g. core.gypi)
     #
diff --git a/tools/asan_build b/tools/asan_build
new file mode 100755
index 0000000..49817f6
--- /dev/null
+++ b/tools/asan_build
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# Build Skia with Address Sanitizer.
+#
+# Address Sanitizer is available in LLVM (and Clang) 3.1 and above, as well as
+# GCC 4.8.  For now, this script assumes the use of Clang 3.2 or newer, which
+# uses different flag syntax from 3.1.
+#
+# For more information, see:
+# https://code.google.com/p/address-sanitizer/wiki/AddressSanitizer
+
+makeVars="$@"
+
+export CC="$(which clang)"
+export CXX="$(which clang++)"
+export LINK="$(which clang)"
+
+noClang="Couldn't find Clang on this machine!"
+if [[ -z "${CC}" ]]; then
+  echo "${noClang}"
+  exit 1
+fi
+if [[ -z "${CXX}" ]]; then
+  echo "${noClang}"
+  exit 1
+fi
+if [[ -z "${LINK}" ]]; then
+  echo "${noClang}"
+  exit 1
+fi
+
+export GYP_DEFINES="skia_asan_build=1 ${GYP_DEFINES}"
+
+python gyp_skia
+if [[ "$?" != "0" ]]; then
+  exit 1
+fi
+
+make ${makeVars}
+if [[ "$?" != "0" ]]; then
+  exit 1
+fi
\ No newline at end of file