Merge "Fix coverage build."
diff --git a/buildtools/BUILD.gn b/buildtools/BUILD.gn
index 97ab40a..9d5fad4 100644
--- a/buildtools/BUILD.gn
+++ b/buildtools/BUILD.gn
@@ -724,7 +724,8 @@
       "//gn/standalone/sanitizers:sanitizer_options_link_helper",
     ]
     defines = [ "_LIBCPP_BUILDING_LIBRARY" ]
-    if ((is_linux || is_android) && (is_asan || is_tsan || is_msan)) {
+    if ((is_linux || is_android) && using_sanitizer &&
+        (is_asan || is_tsan || is_msan)) {
       # In {a,t,m}san configurations, operator new and operator delete will be
       # provided by the sanitizer runtime library.  Since libc++ defines these
       # symbols with weak linkage, and the *san runtime uses strong linkage, it
diff --git a/gn/standalone/BUILDCONFIG.gn b/gn/standalone/BUILDCONFIG.gn
index e080700..6f32686 100644
--- a/gn/standalone/BUILDCONFIG.gn
+++ b/gn/standalone/BUILDCONFIG.gn
@@ -55,9 +55,13 @@
   current_cpu = target_cpu
 }
 
-is_cross_compiling =
-    target_cpu != host_cpu || target_os != host_os || target_triplet != ""
-
+declare_args() {
+  # the ossfuzz sanitizer overrides this to true. In that config the
+  # host/target cpu and arch are identical, but we want to build only the
+  # targets with the sanitizer/fuzzer flags
+  is_cross_compiling =
+      target_cpu != host_cpu || target_os != host_os || target_triplet != ""
+}
 default_configs = [
   "//gn/standalone:debug_symbols",
   "//gn/standalone:default",
diff --git a/gn/standalone/sanitizers/BUILD.gn b/gn/standalone/sanitizers/BUILD.gn
index d3374de..574dcfb 100644
--- a/gn/standalone/sanitizers/BUILD.gn
+++ b/gn/standalone/sanitizers/BUILD.gn
@@ -33,61 +33,60 @@
 }
 
 config("sanitizers_cflags") {
-  cflags = []
-  defines = []
   if (using_sanitizer) {
-    cflags += [ "-fno-omit-frame-pointer" ]
-  }
+    cflags = [ "-fno-omit-frame-pointer" ]
+    defines = []
 
-  if (is_asan) {
-    cflags += [ "-fsanitize=address" ]
-    defines += [ "ADDRESS_SANITIZER" ]
-  }
-  if (is_lsan) {
-    cflags += [ "-fsanitize=leak" ]
-    defines += [ "LEAK_SANITIZER" ]
-  }
-  if (is_tsan) {
-    cflags += [ "-fsanitize=thread" ]
-    defines += [
-      "THREAD_SANITIZER",
-      "DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL=1",
-    ]
-  }
-  if (is_msan) {
-    cflags += [
-      "-fsanitize=memory",
-      "-fsanitize-memory-track-origins=2",
-    ]
-    defines += [ "MEMORY_SANITIZER" ]
-  }
-  if (is_ubsan) {
-    cflags += [
-      "-fsanitize=bounds",
-      "-fsanitize=float-divide-by-zero",
-      "-fsanitize=integer-divide-by-zero",
-      "-fsanitize=null",
-      "-fsanitize=object-size",
-      "-fsanitize=return",
-      "-fsanitize=returns-nonnull-attribute",
-      "-fsanitize=shift-exponent",
-      "-fsanitize=signed-integer-overflow",
-      "-fsanitize=unreachable",
-      "-fsanitize=vla-bound",
-    ]
-    defines += [ "UNDEFINED_SANITIZER" ]
-  }
-  if (is_fuzzer) {
-    # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION is also defined by oss-fuzz,
-    # so using the same name.
-    defines += [ "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" ]
-    cflags += [ "-fsanitize=fuzzer-no-link" ]
     if (is_asan) {
-      cflags += [
-        "-mllvm",
-        "-asan-use-private-alias",
+      cflags += [ "-fsanitize=address" ]
+      defines += [ "ADDRESS_SANITIZER" ]
+    }
+    if (is_lsan) {
+      cflags += [ "-fsanitize=leak" ]
+      defines += [ "LEAK_SANITIZER" ]
+    }
+    if (is_tsan) {
+      cflags += [ "-fsanitize=thread" ]
+      defines += [
+        "THREAD_SANITIZER",
+        "DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL=1",
       ]
     }
+    if (is_msan) {
+      cflags += [
+        "-fsanitize=memory",
+        "-fsanitize-memory-track-origins=2",
+      ]
+      defines += [ "MEMORY_SANITIZER" ]
+    }
+    if (is_ubsan) {
+      cflags += [
+        "-fsanitize=bounds",
+        "-fsanitize=float-divide-by-zero",
+        "-fsanitize=integer-divide-by-zero",
+        "-fsanitize=null",
+        "-fsanitize=object-size",
+        "-fsanitize=return",
+        "-fsanitize=returns-nonnull-attribute",
+        "-fsanitize=shift-exponent",
+        "-fsanitize=signed-integer-overflow",
+        "-fsanitize=unreachable",
+        "-fsanitize=vla-bound",
+      ]
+      defines += [ "UNDEFINED_SANITIZER" ]
+    }
+    if (is_fuzzer) {
+      # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION is also defined by oss-fuzz,
+      # so using the same name.
+      defines += [ "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" ]
+      cflags += [ "-fsanitize=fuzzer-no-link" ]
+      if (is_asan) {
+        cflags += [
+          "-mllvm",
+          "-asan-use-private-alias",
+        ]
+      }
+    }
   }
 }
 
@@ -98,25 +97,27 @@
 }
 
 config("sanitizers_ldflags") {
-  visibility = [ ":deps" ]
-  ldflags = []
-  if (is_asan) {
-    ldflags += [ "-fsanitize=address" ]
+  if (using_sanitizer) {
+    visibility = [ ":deps" ]
+    ldflags = []
+    if (is_asan) {
+      ldflags += [ "-fsanitize=address" ]
+    }
+    if (is_lsan) {
+      # This is not a copy/paste mistake. The LSan runtime library has
+      # moved into asan. So in order to make LSan work one has to build
+      # .cc files with -fsanitize=leak but link with -fsanitize=address.
+      ldflags += [ "-fsanitize=address" ]
+    }
+    if (is_tsan) {
+      ldflags += [ "-fsanitize=thread" ]
+    }
+    if (is_msan) {
+      ldflags += [ "-fsanitize=memory" ]
+    }
+    if (is_ubsan) {
+      ldflags += [ "-fsanitize=undefined" ]
+    }
+    configs = [ ":sanitizer_options_link_helper" ]
   }
-  if (is_lsan) {
-    # This is not a copy/paste mistake. The LSan runtime library has
-    # moved into asan. So in order to make LSan work one has to build
-    # .cc files with -fsanitize=leak but link with -fsanitize=address.
-    ldflags += [ "-fsanitize=address" ]
-  }
-  if (is_tsan) {
-    ldflags += [ "-fsanitize=thread" ]
-  }
-  if (is_msan) {
-    ldflags += [ "-fsanitize=memory" ]
-  }
-  if (is_ubsan) {
-    ldflags += [ "-fsanitize=undefined" ]
-  }
-  configs = [ ":sanitizer_options_link_helper" ]
 }
diff --git a/gn/standalone/sanitizers/vars.gni b/gn/standalone/sanitizers/vars.gni
index 9dada78..228908f 100644
--- a/gn/standalone/sanitizers/vars.gni
+++ b/gn/standalone/sanitizers/vars.gni
@@ -47,8 +47,9 @@
 }
 
 declare_args() {
-  using_sanitizer =
-      is_asan || is_lsan || is_tsan || is_msan || is_ubsan || use_libfuzzer
+  # Don't build host artifacts with sanitizers/fuzzers, only target toolchain.
+  using_sanitizer = (is_asan || is_lsan || is_tsan || is_msan || is_ubsan ||
+                     use_libfuzzer) && current_toolchain == default_toolchain
 }
 
 assert(!using_sanitizer || is_clang || is_system_compiler,
diff --git a/infra/oss-fuzz/build_fuzzers b/infra/oss-fuzz/build_fuzzers
index 7f1fe48..95d9383 100755
--- a/infra/oss-fuzz/build_fuzzers
+++ b/infra/oss-fuzz/build_fuzzers
@@ -11,9 +11,9 @@
 
 GN_ARGS="is_clang=true is_debug=false is_fuzzer=true use_libfuzzer=false \
 link_fuzzer=\"-lFuzzingEngine\" is_hermetic_clang=false \
-use_custom_libcxx=false \
-extra_cflags=\"$CFLAGS -Wno-implicit-int-float-conversion\" \
-extra_cxxflags=\"$CXXFLAGS\" extra_ldflags=\"$CXXFLAGS\" \
+use_custom_libcxx=false is_cross_compiling=true \
+extra_target_cflags=\"$CFLAGS -Wno-implicit-int-float-conversion\" \
+extra_target_cxxflags=\"$CXXFLAGS\" extra_target_ldflags=\"$CXXFLAGS\" \
 is_system_compiler=true cc=\"$CC\" cxx=\"$CXX\" linker=\"gold\""
 
 if [ "$SANITIZER" = "address" ]; then