Add the ability to use different mallocs.

gn gen --args='malloc="tcmalloc"'
gn gen --args='malloc="jemalloc"'

or if the library is in a non-standard directory
gn gen --args='malloc="tcmalloc" extra_ldflags="-L<path-to-library>"'

Change-Id: Icacd837d11392a1971f298ccddd69a5a6781f6cf
Reviewed-on: https://skia-review.googlesource.com/5629
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Mike Klein <mtklein@chromium.org>
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 7af1038..c71ec3f 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -27,6 +27,7 @@
   extra_ldflags = []
 
   cc_wrapper = ""
+  malloc = ""
 }
 
 if (host_os == "win") {
@@ -58,6 +59,7 @@
   cflags_cc = []
   defines = []
   ldflags = []
+  libs = []
 
   if (is_win) {
     cflags += [
@@ -133,6 +135,16 @@
     ldflags += [ "-m32" ]
   }
 
+  if (malloc != "" && !is_win) {
+    cflags += [
+      "-fno-builtin-malloc",
+      "-fno-builtin-calloc",
+      "-fno-builtin-realloc",
+      "-fno-builtin-free",
+    ]
+    libs += [ malloc ]
+  }
+
   if (is_android) {
     asmflags += [
       "--target=$ndk_target",
@@ -166,7 +178,7 @@
       ldflags += [ "-B$ndk/platforms/$ndk_platform/usr/lib64" ]
     }
 
-    libs = [
+    libs += [
       # Order matters here!  Keep these three in exactly this order.
       "c++_static",
       "c++abi",
@@ -192,7 +204,7 @@
       target_cpu,
       "-stdlib=libc++",
     ]
-    libs = [ "objc" ]
+    libs += [ "objc" ]
 
     # We used to link all our iOS tools together, so none actually defines main().
     # Instead they each define their own entry point, which our iOS mega-app called.
@@ -207,7 +219,7 @@
   }
 
   if (is_linux) {
-    libs = [ "pthread" ]
+    libs += [ "pthread" ]
   }
 
   if (sanitize != "") {