[fuzz] Add libfuzzer binaries to BUILD.gn

For the longest time, the libfuzzer binaries used by oss-fuzz
were just hacked onto the BUILD.gn file. This removes that patch
and makes them buildable from Skia proper.

After this, there should not be any modifications oss-fuzz needs
to do to a Skia checkout before it builds and runs.

Of note, oss-fuzz will define skia_use_libfuzzer_defaults to be
false so it can control those flags with more finesse (e.g.
fuzz with ASAN, fuzz with hong fuzz instead of libfuzzer). I
added on skia_use_libfuzzer_defaults so that a normal developer
gets something that works by default.

Bug: skia:10713
Change-Id: I024f5805060cba8f8560e1c2569b9309fb49a564
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/316536
Commit-Queue: Kevin Lubick <kjlubick@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index d2ee891..152f8fa 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -2674,6 +2674,363 @@
       deps += [ "modules/canvaskit:viewer_wasm" ]
     }
   }
+
+  if (skia_build_fuzzers) {
+    template("libfuzzer_app") {
+      output_dir = root_build_dir
+      executable(target_name) {
+        check_includes = false
+        forward_variables_from(invoker, "*", [ "is_shared_library" ])
+        configs += [ ":skia_private" ]
+        sources += [
+          "fuzz/Fuzz.cpp",
+          "fuzz/FuzzCommon.cpp",
+        ]
+        deps += [
+          ":flags",
+          ":gpu_tool_utils",
+          ":skia",
+        ]
+        defines = [ "SK_BUILD_FOR_LIBFUZZER" ]
+        if (skia_use_libfuzzer_defaults) {
+          cflags = [ "-fsanitize=fuzzer" ]
+          ldflags = [ "-fsanitize=fuzzer" ]
+        }
+        testonly = true
+      }
+    }
+
+    libfuzzer_app("region_deserialize") {
+      sources = [ "fuzz/oss_fuzz/FuzzRegionDeserialize.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("image_filter_deserialize") {
+      include_dirs = [
+        "tools",
+        "tools/fonts",
+      ]
+      sources = [
+        "fuzz/oss_fuzz/FuzzImageFilterDeserialize.cpp",
+        "tools/Resources.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [ ":experimental_svg_model" ]
+    }
+
+    libfuzzer_app("region_set_path") {
+      sources = [ "fuzz/oss_fuzz/FuzzRegionSetPath.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("textblob_deserialize") {
+      include_dirs = [
+        "tools",
+        "tools/fonts",
+      ]
+      sources = [
+        "fuzz/oss_fuzz/FuzzTextBlobDeserialize.cpp",
+        "tools/Resources.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [ ":experimental_svg_model" ]
+    }
+
+    libfuzzer_app("path_deserialize") {
+      sources = [ "fuzz/oss_fuzz/FuzzPathDeserialize.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("image_decode") {
+      sources = [ "fuzz/oss_fuzz/FuzzImage.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("animated_image_decode") {
+      sources = [ "fuzz/oss_fuzz/FuzzAnimatedImage.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("api_create_ddl") {
+      include_dirs = [
+        "include",
+        "include/gpu",
+      ]
+      sources = [
+        "fuzz/FuzzCreateDDL.cpp",
+        "fuzz/oss_fuzz/FuzzAPICreateDDL.cpp",
+        "tools/Resources.cpp",
+        "tools/UrlDataManager.cpp",
+        "tools/debugger/DebugCanvas.cpp",
+        "tools/debugger/DebugLayerManager.cpp",
+        "tools/debugger/DrawCommand.cpp",
+        "tools/debugger/JsonWriteBuffer.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [
+        ":experimental_svg_model",
+        "//third_party/libpng",
+      ]
+    }
+
+    libfuzzer_app("api_draw_functions") {
+      sources = [
+        "fuzz/FuzzDrawFunctions.cpp",
+        "fuzz/oss_fuzz/FuzzDrawFunctions.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("api_gradients") {
+      sources = [
+        "fuzz/FuzzGradients.cpp",
+        "fuzz/oss_fuzz/FuzzGradients.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("api_image_filter") {
+      include_dirs = [
+        "tools",
+        "tools/debugger",
+      ]
+      sources = [
+        "fuzz/FuzzCanvas.cpp",
+        "fuzz/oss_fuzz/FuzzAPIImageFilter.cpp",
+        "tools/UrlDataManager.cpp",
+        "tools/debugger/DebugCanvas.cpp",
+        "tools/debugger/DebugLayerManager.cpp",
+        "tools/debugger/DrawCommand.cpp",
+        "tools/debugger/JsonWriteBuffer.cpp",
+      ]
+      deps = [ "//third_party/libpng" ]
+    }
+
+    libfuzzer_app("api_path_measure") {
+      sources = [
+        "fuzz/FuzzPathMeasure.cpp",
+        "fuzz/oss_fuzz/FuzzPathMeasure.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("api_pathop") {
+      sources = [
+        "fuzz/FuzzPathop.cpp",
+        "fuzz/oss_fuzz/FuzzPathop.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("api_raster_n32_canvas") {
+      include_dirs = [
+        "tools",
+        "tools/debugger",
+        "tools/fonts",
+      ]
+      sources = [
+        "fuzz/FuzzCanvas.cpp",
+        "fuzz/oss_fuzz/FuzzRasterN32Canvas.cpp",
+        "tools/Resources.cpp",
+        "tools/UrlDataManager.cpp",
+        "tools/debugger/DebugCanvas.cpp",
+        "tools/debugger/DebugLayerManager.cpp",
+        "tools/debugger/DrawCommand.cpp",
+        "tools/debugger/JsonWriteBuffer.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [
+        ":experimental_svg_model",
+        "//third_party/libpng",
+      ]
+    }
+
+    if (skia_use_gl) {
+      libfuzzer_app("api_mock_gpu_canvas") {
+        include_dirs = [
+          "tools",
+          "tools/debugger",
+          "tools/fonts",
+        ]
+        sources = [
+          "fuzz/FuzzCanvas.cpp",
+          "fuzz/oss_fuzz/FuzzMockGPUCanvas.cpp",
+          "tools/LsanSuppressions.cpp",
+          "tools/Resources.cpp",
+          "tools/UrlDataManager.cpp",
+          "tools/debugger/DebugCanvas.cpp",
+          "tools/debugger/DebugLayerManager.cpp",
+          "tools/debugger/DrawCommand.cpp",
+          "tools/debugger/JsonWriteBuffer.cpp",
+          "tools/fonts/TestFontMgr.cpp",
+          "tools/fonts/TestSVGTypeface.cpp",
+          "tools/fonts/TestTypeface.cpp",
+        ]
+        deps = [
+          ":experimental_svg_model",
+          "//third_party/libpng",
+        ]
+      }
+    }
+
+    libfuzzer_app("api_null_canvas") {
+      include_dirs = [
+        "tools",
+        "tools/debugger",
+        "tools/fonts",
+      ]
+      sources = [
+        "fuzz/FuzzCanvas.cpp",
+        "fuzz/oss_fuzz/FuzzNullCanvas.cpp",
+        "tools/Resources.cpp",
+        "tools/UrlDataManager.cpp",
+        "tools/debugger/DebugCanvas.cpp",
+        "tools/debugger/DebugLayerManager.cpp",
+        "tools/debugger/DrawCommand.cpp",
+        "tools/debugger/JsonWriteBuffer.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [
+        ":experimental_svg_model",
+        "//third_party/libpng",
+      ]
+    }
+
+    libfuzzer_app("api_svg_canvas") {
+      include_dirs = [
+        "include",
+        "include/svg",
+      ]
+      sources = [
+        "fuzz/FuzzCanvas.cpp",
+        "fuzz/oss_fuzz/FuzzAPISVGCanvas.cpp",
+        "tools/Resources.cpp",
+        "tools/UrlDataManager.cpp",
+        "tools/debugger/DebugCanvas.cpp",
+        "tools/debugger/DebugLayerManager.cpp",
+        "tools/debugger/DrawCommand.cpp",
+        "tools/debugger/JsonWriteBuffer.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [
+        ":experimental_svg_model",
+        "//third_party/libpng",
+      ]
+    }
+
+    libfuzzer_app("png_encoder") {
+      sources = [
+        "fuzz/FuzzEncoders.cpp",
+        "fuzz/oss_fuzz/FuzzPNGEncoder.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("jpeg_encoder") {
+      sources = [
+        "fuzz/FuzzEncoders.cpp",
+        "fuzz/oss_fuzz/FuzzJPEGEncoder.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("webp_encoder") {
+      sources = [
+        "fuzz/FuzzEncoders.cpp",
+        "fuzz/oss_fuzz/FuzzWEBPEncoder.cpp",
+      ]
+      deps = []
+    }
+
+    libfuzzer_app("skottie_json") {
+      sources = [
+        "modules/skottie/fuzz/FuzzSkottieJSON.cpp",
+        "tools/Resources.cpp",
+        "tools/fonts/TestFontMgr.cpp",
+        "tools/fonts/TestSVGTypeface.cpp",
+        "tools/fonts/TestTypeface.cpp",
+      ]
+      deps = [
+        ":experimental_svg_model",
+        "modules/skottie:skottie",
+      ]
+    }
+
+    libfuzzer_app("skjson") {
+      sources = [ "fuzz/oss_fuzz/FuzzJSON.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("api_polyutils") {
+      sources = [
+        "fuzz/FuzzPolyUtils.cpp",
+        "fuzz/oss_fuzz/FuzzPolyUtils.cpp",
+      ]
+      deps = [ ":skia" ]
+    }
+
+    libfuzzer_app("android_codec") {
+      sources = [ "fuzz/oss_fuzz/FuzzAndroidCodec.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("image_decode_incremental") {
+      sources = [ "fuzz/oss_fuzz/FuzzIncrementalImage.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("sksl2glsl") {
+      sources = [ "fuzz/oss_fuzz/FuzzSKSL2GLSL.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("sksl2spirv") {
+      sources = [ "fuzz/oss_fuzz/FuzzSKSL2SPIRV.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("sksl2metal") {
+      sources = [ "fuzz/oss_fuzz/FuzzSKSL2Metal.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("sksl2pipeline") {
+      sources = [ "fuzz/oss_fuzz/FuzzSKSL2Pipeline.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("skdescriptor_deserialize") {
+      sources = [ "fuzz/oss_fuzz/FuzzSkDescriptorDeserialize.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("svg_dom") {
+      sources = [ "fuzz/oss_fuzz/FuzzSVG.cpp" ]
+      deps = [ ":experimental_svg_model" ]
+    }
+
+    libfuzzer_app("skruntimeeffect") {
+      sources = [ "fuzz/oss_fuzz/FuzzSkRuntimeEffect.cpp" ]
+      deps = []
+    }
+
+    libfuzzer_app("skp") {
+      sources = [ "fuzz/oss_fuzz/FuzzSKP.cpp" ]
+      deps = []
+    }
+  }
 }
 
 if (is_ios && skia_use_metal && !skia_enable_flutter_defines) {