[zircon] [gn] Add LTO variants

Bug: BLD-325
Test: gn gen && ninja
Change-Id: I5d2a2e3c408c76f72632401af7f281f9312bf045
diff --git a/zircon/public/gn/config/lto/BUILD.gn b/zircon/public/gn/config/lto/BUILD.gn
new file mode 100644
index 0000000..d883598
--- /dev/null
+++ b/zircon/public/gn/config/lto/BUILD.gn
@@ -0,0 +1,37 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+config("lto") {
+  compiler_flags = [
+    "-flto",
+
+    # Enable whole-program devirtualization and virtual constant propagation.
+    "-fwhole-program-vtables",
+  ]
+}
+
+declare_args() {
+  # Number of parallel ThinLTO jobs.
+  thinlto_jobs = 8
+
+  # ThinLTO cache directory path.
+  thinlto_cache_dir = rebase_path("$root_out_dir/thinlto-cache", root_build_dir)
+}
+
+config("thinlto") {
+  compiler_flags = [ "-flto=thin" ]
+  ldflags = [
+    # The ThinLTO driver launches a number of threads in parallel whose
+    # number is by default equivalent to the number of cores.  We need
+    # to limit the parallelism to avoid aggressive competition between
+    # different linker jobs.
+    "-Wl,--thinlto-jobs=$thinlto_jobs",
+
+    # Set the ThinLTO cache directory which is used to cache native
+    # object files for ThinLTO incremental builds.  This directory is
+    # not managed by Ninja and has to be cleaned manually, but it is
+    # periodically garbage-collected by the ThinLTO driver.
+    "-Wl,--thinlto-cache-dir=$thinlto_cache_dir",
+  ]
+}
diff --git a/zircon/public/gn/config/standard.gni b/zircon/public/gn/config/standard.gni
index 3a9b6b2..2f64a38 100644
--- a/zircon/public/gn/config/standard.gni
+++ b/zircon/public/gn/config/standard.gni
@@ -150,6 +150,18 @@
       }
     }
   },
+  {
+    variant = {
+      name = "lto"
+      configs = [ "$zx/public/gn/config/lto" ]
+    }
+  },
+  {
+    variant = {
+      name = "thinlto"
+      configs = [ "$zx/public/gn/config/lto:thinlto" ]
+    }
+  },
 ]
 
 # This list is appended to the $variants list for the build.  It provides