# Copyright 2016 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("gn/flutter_defines.gni")
import("gn/shared_sources.gni")

if (is_fuchsia) {
  import("//build/vulkan/config.gni")
}

if (!defined(is_skia_standalone)) {
  is_skia_standalone = false
}
is_skia_dev_build = is_skia_standalone && !is_official_build

declare_args() {
  skia_use_angle = false
  skia_use_egl = false
  skia_use_expat = true
  skia_use_fontconfig = is_linux
  skia_use_freetype = is_android || is_fuchsia || is_linux
  skia_use_icu = !is_fuchsia && !is_ios && !is_win  # TODO: Windows
  skia_use_libjpeg_turbo = true
  skia_use_libpng = true
  skia_use_libwebp = !is_fuchsia
  skia_use_lua = is_skia_dev_build && !is_ios
  skia_use_piex = !is_win
  skia_use_zlib = true
  skia_use_metal = false
  skia_use_libheif = is_skia_dev_build
  skia_use_skcms = is_skia_dev_build

  skia_android_serial = ""
  skia_enable_discrete_gpu = true
  skia_enable_effects = true
  skia_enable_flutter_defines = false
  skia_enable_fontmgr_empty = false
  skia_enable_gpu = true
  skia_enable_pdf = true
  skia_enable_spirv_validation = is_skia_dev_build && is_debug
  skia_enable_tools = is_skia_dev_build
  skia_enable_vulkan_debug_layers = is_skia_dev_build && is_debug
  skia_vulkan_header = ""
  skia_vulkan_sdk = getenv("VULKAN_SDK")
  skia_qt_path = getenv("QT_PATH")
  skia_compile_processors = false
  skia_lex = false

  skia_jumper_clang = ""
  skia_jumper_objdump = ""
  skia_jumper_ccache = ""

  skia_skqp_enable_driver_correctness_workarounds = false
  skia_skqp_global_error_tolerance = 8
}
declare_args() {
  skia_use_dng_sdk = !is_fuchsia && skia_use_libjpeg_turbo && skia_use_zlib
  skia_use_sfntly = skia_use_icu
  skia_enable_atlas_text = is_skia_dev_build && skia_enable_gpu

  if (is_android) {
    skia_use_vulkan = defined(ndk_api) && ndk_api >= 24
  } else if (is_fuchsia) {
    skia_use_vulkan = fuchsia_use_vulkan
  } else {
    skia_use_vulkan = skia_vulkan_sdk != ""
  }

  if (is_ios) {
    skia_ios_identity = ".*Google.*"
    skia_ios_profile = "Google Development"
  }
}
declare_args() {
  skia_tools_vulkan_header_dir = ""
  if (skia_use_vulkan) {
    # When buliding on Android we get the header via the NDK so no need for any extra path.
    if (is_fuchsia) {
      skia_tools_vulkan_header_dir = "$fuchsia_vulkan_sdk/include"
    } else if (is_linux || is_win) {
      skia_tools_vulkan_header_dir = "$skia_vulkan_sdk/include"
    }
  }
}

if (defined(skia_settings)) {
  import(skia_settings)
}

# Our tools require static linking (they use non-exported symbols).
skia_enable_tools = skia_enable_tools && !is_component_build

fontmgr_android_enabled = skia_use_expat && skia_use_freetype

skia_public_includes = [
  "include/android",
  "include/c",
  "include/codec",
  "include/config",
  "include/core",
  "include/effects",
  "include/encode",
  "include/gpu",
  "include/gpu/gl",
  "include/atlastext",
  "include/pathops",
  "include/ports",
  "include/svg",
  "include/utils",
  "include/utils/mac",
]

if (skia_use_vulkan) {
  skia_public_includes += [ "include/gpu/vk" ]
}
if (skia_enable_atlas_text) {
  skia_public_includes += [ "include/atlastext" ]
}
if (skia_use_metal) {
  skia_public_includes += [ "include/gpu/mtl" ]
}

# Skia public API, generally provided by :skia.
config("skia_public") {
  include_dirs = skia_public_includes
  if (skia_tools_vulkan_header_dir != "") {
    include_dirs += [ skia_tools_vulkan_header_dir ]
  }
  defines = []
  if (is_component_build) {
    defines += [ "SKIA_DLL" ]
  }
  if (is_fuchsia || is_linux) {
    defines += [ "SK_SAMPLES_FOR_X" ]
  }
  if (skia_enable_flutter_defines) {
    defines += flutter_defines
  }
  if (!skia_enable_gpu) {
    defines += [ "SK_SUPPORT_GPU=0" ]
  }
  if (skia_enable_atlas_text) {
    defines += [ "SK_SUPPORT_ATLAS_TEXT=1" ]
  }
}

# Skia internal APIs, used by Skia itself and a few test tools.
config("skia_private") {
  visibility = [ ":*" ]

  include_dirs = [
    "include/private",
    "src/c",
    "src/codec",
    "src/core",
    "src/effects",
    "src/fonts",
    "src/image",
    "src/images",
    "src/lazy",
    "src/opts",
    "src/pathops",
    "src/pdf",
    "src/ports",
    "src/sfnt",
    "src/shaders",
    "src/shaders/gradients",
    "src/sksl",
    "src/utils",
    "src/utils/win",
    "src/xml",
    "third_party/gif",
  ]

  defines = [ "SK_GAMMA_APPLY_TO_A8" ]
  if (is_android) {
    defines += [
      "SK_GAMMA_EXPONENT=1.4",
      "SK_GAMMA_CONTRAST=0.0",
    ]
  }
  if (is_official_build || is_android) {
    # TODO(bsalomon): it'd be nice to make Android normal.
    defines += [ "SK_ALLOW_STATIC_GLOBAL_INITIALIZERS=0" ]
  }
  libs = []
  lib_dirs = []
  if (skia_enable_gpu) {
    include_dirs += [ "src/gpu" ]
    if (is_skia_dev_build && skia_use_vulkan) {
      include_dirs += [ "tools/gpu/vk" ]
    }
  }
  if (skia_use_angle) {
    defines += [ "SK_ANGLE" ]
  }
  if (skia_enable_discrete_gpu) {
    defines += [ "SK_ENABLE_DISCRETE_GPU" ]
  }
  if (!is_official_build) {
    defines += [ "GR_TEST_UTILS=1" ]
  }
}

# Any code that's linked into Skia-the-library should use this config via += skia_library_configs.
config("skia_library") {
  visibility = [ ":*" ]
  defines = [ "SKIA_IMPLEMENTATION=1" ]
}

skia_library_configs = [
  ":skia_public",
  ":skia_private",
  ":skia_library",
]

# Use for CPU-specific Skia code that needs particular compiler flags.
template("opts") {
  if (invoker.enabled) {
    source_set(target_name) {
      forward_variables_from(invoker, "*")
      configs += skia_library_configs
    }
  } else {
    # If not enabled, a phony empty target that swallows all otherwise unused variables.
    source_set(target_name) {
      forward_variables_from(invoker,
                             "*",
                             [
                               "sources",
                               "cflags",
                             ])
    }
  }
}

is_x86 = current_cpu == "x64" || current_cpu == "x86"

opts("none") {
  enabled = !is_x86 && current_cpu != "arm" && current_cpu != "arm64"
  sources = skia_opts.none_sources
  cflags = []
}

opts("armv7") {
  enabled = current_cpu == "arm"
  sources = skia_opts.armv7_sources + skia_opts.neon_sources
  cflags = []
}

opts("arm64") {
  enabled = current_cpu == "arm64"
  sources = skia_opts.arm64_sources
  cflags = []
}

opts("crc32") {
  enabled = current_cpu == "arm64"
  sources = skia_opts.crc32_sources
  cflags = [ "-march=armv8-a+crc" ]
}

opts("sse2") {
  enabled = is_x86
  sources = skia_opts.sse2_sources
  if (!is_clang && is_win) {
    defines = [ "SK_CPU_SSE_LEVEL=SK_CPU_SSE_LEVEL_SSE2" ]
  } else {
    cflags = [ "-msse2" ]
  }
}

opts("ssse3") {
  enabled = is_x86
  sources = skia_opts.ssse3_sources
  if (!is_clang && is_win) {
    defines = [ "SK_CPU_SSE_LEVEL=SK_CPU_SSE_LEVEL_SSSE3" ]
  } else {
    cflags = [ "-mssse3" ]
  }
}

opts("sse41") {
  enabled = is_x86
  sources = skia_opts.sse41_sources
  if (!is_clang && is_win) {
    defines = [ "SK_CPU_SSE_LEVEL=SK_CPU_SSE_LEVEL_SSE41" ]
  } else {
    cflags = [ "-msse4.1" ]
  }
}

opts("sse42") {
  enabled = is_x86
  sources = skia_opts.sse42_sources
  if (!is_clang && is_win) {
    defines = [ "SK_CPU_SSE_LEVEL=SK_CPU_SSE_LEVEL_SSE42" ]
  } else {
    cflags = [ "-msse4.2" ]
  }
}

opts("avx") {
  enabled = is_x86
  sources = skia_opts.avx_sources
  if (!is_clang && is_win) {
    cflags = [ "/arch:AVX" ]
  } else {
    cflags = [ "-mavx" ]
  }
}

# Any feature of Skia that requires third-party code should be optional and use this template.
template("optional") {
  if (invoker.enabled) {
    config(target_name + "_public") {
      if (defined(invoker.public_defines)) {
        defines = invoker.public_defines
      }
    }
    source_set(target_name) {
      forward_variables_from(invoker,
                             "*",
                             [
                               "public_defines",
                               "sources_when_disabled",
                               "configs_to_remove",
                             ])
      all_dependent_configs = [ ":" + target_name + "_public" ]
      configs += skia_library_configs
      if (defined(invoker.configs_to_remove)) {
        configs -= invoker.configs_to_remove
      }
    }
  } else {
    source_set(target_name) {
      forward_variables_from(invoker,
                             "*",
                             [
                               "public_defines",
                               "deps",
                               "libs",
                               "sources",
                               "sources_when_disabled",
                               "configs_to_remove",
                             ])
      if (defined(invoker.sources_when_disabled)) {
        sources = invoker.sources_when_disabled
      }
      configs += skia_library_configs
    }
  }
}

optional("effects") {
  enabled = skia_enable_effects
  deps = [
    ":compile_processors",
  ]
  sources =
      skia_effects_sources + [ "src/ports/SkGlobalInitialization_default.cpp" ]
  sources_when_disabled = [ "src/ports/SkGlobalInitialization_none.cpp" ]
}

optional("fontmgr_android") {
  enabled = fontmgr_android_enabled

  deps = [
    ":typeface_freetype",
    "//third_party/expat",
  ]
  sources = [
    "src/ports/SkFontMgr_android.cpp",
    "src/ports/SkFontMgr_android_factory.cpp",
    "src/ports/SkFontMgr_android_parser.cpp",
  ]
}

optional("fontmgr_custom") {
  enabled = is_linux && skia_use_freetype && !skia_use_fontconfig

  deps = [
    ":typeface_freetype",
  ]
  sources = [
    "src/ports/SkFontMgr_custom.cpp",
    "src/ports/SkFontMgr_custom.h",
    "src/ports/SkFontMgr_custom_directory.cpp",
    "src/ports/SkFontMgr_custom_directory_factory.cpp",
    "src/ports/SkFontMgr_custom_embedded.cpp",
    "src/ports/SkFontMgr_custom_empty.cpp",
  ]
}

optional("fontmgr_empty") {
  enabled = skia_enable_fontmgr_empty
  sources = [
    "src/ports/SkFontMgr_empty_factory.cpp",
  ]
}

optional("fontmgr_fontconfig") {
  enabled = skia_use_freetype && skia_use_fontconfig

  deps = [
    ":typeface_freetype",
    "//third_party:fontconfig",
  ]
  sources = [
    "src/ports/SkFontConfigInterface.cpp",
    "src/ports/SkFontConfigInterface_direct.cpp",
    "src/ports/SkFontConfigInterface_direct_factory.cpp",
    "src/ports/SkFontMgr_FontConfigInterface.cpp",
    "src/ports/SkFontMgr_fontconfig.cpp",
    "src/ports/SkFontMgr_fontconfig_factory.cpp",
  ]
}

optional("fontmgr_fuchsia") {
  enabled = is_fuchsia && skia_use_freetype

  deps = [
    ":typeface_freetype",
  ]
  sources = [
    "src/ports/SkFontMgr_custom.cpp",
    "src/ports/SkFontMgr_custom_empty.cpp",
    "src/ports/SkFontMgr_custom_empty_factory.cpp",
  ]
}

if (skia_lex) {
  executable("sksllex") {
    sources = [
      "src/sksl/lex/Main.cpp",
      "src/sksl/lex/NFA.cpp",
      "src/sksl/lex/RegexNode.cpp",
      "src/sksl/lex/RegexParser.cpp",
    ]
    include_dirs = [ "src/sksl/lex" ]
  }

  action("run_sksllex") {
    script = "gn/run_sksllex.py"
    deps = [
      ":sksllex(//gn/toolchain:$host_toolchain)",
    ]
    sources = [
      "src/sksl/lex/layout.lex",
      "src/sksl/lex/sksl.lex",
    ]

    # GN insists its outputs should go somewhere underneath target_out_dir, so we trick it with a
    # path that starts with target_out_dir and then uses ".." to back up into the src dir.
    outputs = [
      "$target_out_dir/" +
          rebase_path("src/sksl/lex/SkSLLexer.h", target_out_dir),
      # the script also modifies the corresponding .cpp file, but if we tell GN that it gets
      # confused due to the same file being named by two different paths
    ]
    sksllex_path = "$root_out_dir/"
    sksllex_path += "sksllex"
    if (host_os == "win") {
      sksllex_path += ".exe"
    }
    args = [
      rebase_path(sksllex_path),
      rebase_path("bin/clang-format"),
      rebase_path("src"),
    ]
  }
} else {
  group("run_sksllex") {
  }
}

if (skia_compile_processors) {
  executable("skslc") {
    defines = [ "SKSL_STANDALONE" ]
    sources = [
      "src/sksl/SkSLMain.cpp",
    ]
    sources += skia_sksl_sources
    include_dirs = [
      "src/gpu",
      "src/sksl",
    ]
    deps = [
      ":run_sksllex",
      "//third_party/spirv-tools",
    ]
  }

  skia_gpu_processor_outputs = []
  foreach(src, skia_gpu_processor_sources) {
    dir = get_path_info(src, "dir")
    name = get_path_info(src, "name")

    # GN insists its outputs should go somewhere underneath target_out_dir, so we trick it with a
    # path that starts with target_out_dir and then uses ".." to back up into the src dir.
    skia_gpu_processor_outputs += [
      "$target_out_dir/" + rebase_path("$dir/$name.h", target_out_dir),
      # the script also modifies the corresponding .cpp file, but if we tell GN that it gets
      # confused due to the same file being named by two different paths
    ]
  }

  action("create_sksl_enums") {
    script = "gn/create_sksl_enums.py"
    sources = [
      "include/private/GrSharedEnums.h",
    ]
    outputs = [
      "$target_out_dir/" +
          rebase_path("src/sksl/sksl_enums.inc", target_out_dir),
    ]
    args = [
      rebase_path(sources[0]),
      rebase_path(outputs[0]),
    ]
  }

  action("compile_processors") {
    script = "gn/compile_processors.py"
    deps = [
      ":create_sksl_enums",
      ":skslc(//gn/toolchain:$host_toolchain)",
    ]
    sources = skia_gpu_processor_sources
    outputs = skia_gpu_processor_outputs
    skslc_path = "$root_out_dir/"
    if (host_toolchain != default_toolchain_name) {
      skslc_path += "$host_toolchain/"
    }
    skslc_path += "skslc"
    if (host_os == "win") {
      skslc_path += ".exe"
    }
    args = [
      rebase_path(skslc_path),
      rebase_path("bin/clang-format"),
    ]
    args += rebase_path(skia_gpu_processor_sources)
  }
} else {
  skia_gpu_processor_outputs = []
  group("compile_processors") {
  }
}

optional("gpu") {
  enabled = skia_enable_gpu
  deps = [
    ":compile_processors",
    ":run_sksllex",
  ]
  public_defines = []

  sources = skia_gpu_sources + skia_sksl_sources + skia_gpu_processor_outputs

  # These paths need to be absolute to match the ones produced by shared_sources.gni.
  sources -= get_path_info([ "src/gpu/gl/GrGLMakeNativeInterface_none.cpp" ],
                           "abspath")
  libs = []
  if (is_android) {
    sources += [ "src/gpu/gl/android/GrGLMakeNativeInterface_android.cpp" ]

    # this lib is required to link against AHardwareBuffer
    if (defined(ndk_api) && ndk_api >= 26) {
      libs += [ "android" ]
    }
  } else if (skia_use_egl) {
    sources += [ "src/gpu/gl/egl/GrGLMakeNativeInterface_egl.cpp" ]
    libs += [ "EGL" ]
  } else if (is_linux) {
    sources += [ "src/gpu/gl/glx/GrGLMakeNativeInterface_glx.cpp" ]
    libs += [
      "GL",
      "GLU",
    ]
  } else if (is_mac) {
    sources += [ "src/gpu/gl/mac/GrGLMakeNativeInterface_mac.cpp" ]
  } else if (is_ios) {
    sources += [ "src/gpu/gl/iOS/GrGLMakeNativeInterface_iOS.cpp" ]
  } else if (is_win) {
    sources += [ "src/gpu/gl/win/GrGLMakeNativeInterface_win.cpp" ]
    libs += [ "OpenGL32.lib" ]
  } else {
    sources += [ "src/gpu/gl/GrGLMakeNativeInterface_none.cpp" ]
  }

  if (skia_use_vulkan) {
    public_defines += [ "SK_VULKAN" ]
    sources += skia_vk_sources
    if (skia_enable_vulkan_debug_layers) {
      public_defines += [ "SK_ENABLE_VK_LAYERS" ]
    }
    if (skia_vulkan_header != "") {
      public_defines += [ "SK_VULKAN_HEADER=\"$skia_vulkan_header\"" ]
    } else {
      if (is_skia_dev_build) {
        public_defines += [ "SK_VULKAN_HEADER=\"GrVulkanDefines.h\"" ]
      }
    }
  }
  if (skia_enable_spirv_validation) {
    deps += [ "//third_party/spirv-tools" ]
    public_defines += [ "SK_ENABLE_SPIRV_VALIDATION" ]
  }

  cflags_objcc = []
  if (skia_use_metal) {
    public_defines += [ "SK_METAL" ]
    sources += skia_metal_sources
    libs += [ "Metal.framework" ]
    cflags_objcc += [ "-fobjc-arc" ]
  }

  if (skia_enable_atlas_text) {
    sources += skia_atlas_text_sources
  }
}

optional("heif") {
  enabled = skia_use_libheif
  public_defines = [ "SK_HAS_HEIF_LIBRARY" ]

  deps = []

  sources = [
    "src/codec/SkHeifCodec.cpp",
  ]
}

optional("jpeg") {
  enabled = skia_use_libjpeg_turbo
  public_defines = [ "SK_HAS_JPEG_LIBRARY" ]

  deps = [
    "//third_party/libjpeg-turbo:libjpeg",
  ]
  sources = [
    "src/codec/SkJpegCodec.cpp",
    "src/codec/SkJpegDecoderMgr.cpp",
    "src/codec/SkJpegUtility.cpp",
    "src/images/SkJPEGWriteUtility.cpp",
    "src/images/SkJpegEncoder.cpp",
  ]
}

optional("pdf") {
  enabled = skia_use_zlib && skia_enable_pdf
  public_defines = [ "SK_SUPPORT_PDF" ]

  deps = [
    "//third_party/zlib",
  ]
  sources = skia_pdf_sources
  sources_when_disabled = [ "src/pdf/SkDocument_PDF_None.cpp" ]

  if (skia_use_sfntly) {
    deps += [ "//third_party/sfntly" ]
    public_defines += [ "SK_PDF_USE_SFNTLY" ]
  }
}

optional("png") {
  enabled = skia_use_libpng
  public_defines = [ "SK_HAS_PNG_LIBRARY" ]

  deps = [
    "//third_party/libpng",
  ]
  sources = [
    "src/codec/SkIcoCodec.cpp",
    "src/codec/SkPngCodec.cpp",
    "src/images/SkPngEncoder.cpp",
  ]
}

optional("raw") {
  enabled = skia_use_dng_sdk && skia_use_libjpeg_turbo && skia_use_piex
  public_defines = [ "SK_CODEC_DECODES_RAW" ]

  deps = [
    "//third_party/dng_sdk",
    "//third_party/libjpeg-turbo:libjpeg",
    "//third_party/piex",
  ]

  # SkRawCodec catches any exceptions thrown by dng_sdk, insulating the rest of
  # Skia.
  configs_to_remove = [ "//gn:no_exceptions" ]

  sources = [
    "src/codec/SkRawAdapterCodec.cpp",
    "src/codec/SkRawCodec.cpp",
  ]
}

optional("skcms") {
  enabled = skia_use_skcms

  deps = [
    "//third_party/skcms",
  ]
  sources = [
    # TODO
  ]
}

optional("typeface_freetype") {
  enabled = skia_use_freetype

  deps = [
    "//third_party/freetype2",
  ]
  sources = [
    "src/ports/SkFontHost_FreeType.cpp",
    "src/ports/SkFontHost_FreeType_common.cpp",
  ]
}

optional("webp") {
  enabled = skia_use_libwebp
  public_defines = [ "SK_HAS_WEBP_LIBRARY" ]

  deps = [
    "//third_party/libwebp",
  ]
  sources = [
    "src/codec/SkWebpAdapterCodec.cpp",
    "src/codec/SkWebpCodec.cpp",
    "src/images/SkWebpEncoder.cpp",
  ]
}

optional("xml") {
  enabled = skia_use_expat
  public_defines = [ "SK_XML" ]

  deps = [
    "//third_party/expat",
  ]
  sources = [
    "src/svg/SkSVGCanvas.cpp",
    "src/svg/SkSVGDevice.cpp",
    "src/xml/SkDOM.cpp",
    "src/xml/SkXMLParser.cpp",
    "src/xml/SkXMLWriter.cpp",
  ]
}

component("skia") {
  public_configs = [ ":skia_public" ]
  configs += skia_library_configs

  deps = [
    ":arm64",
    ":armv7",
    ":avx",
    ":crc32",
    ":effects",
    ":fontmgr_android",
    ":fontmgr_custom",
    ":fontmgr_empty",
    ":fontmgr_fontconfig",
    ":fontmgr_fuchsia",
    ":gpu",
    ":heif",
    ":jpeg",
    ":none",
    ":pdf",
    ":png",
    ":raw",
    ":skcms",
    ":sse2",
    ":sse41",
    ":sse42",
    ":ssse3",
    ":webp",
    ":xml",
  ]

  # This file (and all GN files in Skia) are designed to work with an
  # empty sources assignment filter; we handle all that explicitly.
  # We clear the filter here for clients who may have set up a global filter.
  set_sources_assignment_filter([])

  sources = []
  sources += skia_core_sources
  sources += skia_utils_sources
  sources += skia_xps_sources
  sources += [
    "src/android/SkAndroidFrameworkUtils.cpp",
    "src/android/SkAnimatedImage.cpp",
    "src/android/SkBitmapRegionCodec.cpp",
    "src/android/SkBitmapRegionDecoder.cpp",
    "src/codec/SkAndroidCodec.cpp",
    "src/codec/SkBmpBaseCodec.cpp",
    "src/codec/SkBmpCodec.cpp",
    "src/codec/SkBmpMaskCodec.cpp",
    "src/codec/SkBmpRLECodec.cpp",
    "src/codec/SkBmpStandardCodec.cpp",
    "src/codec/SkCodec.cpp",
    "src/codec/SkCodecImageGenerator.cpp",
    "src/codec/SkGifCodec.cpp",
    "src/codec/SkMaskSwizzler.cpp",
    "src/codec/SkMasks.cpp",
    "src/codec/SkSampledCodec.cpp",
    "src/codec/SkSampler.cpp",
    "src/codec/SkStreamBuffer.cpp",
    "src/codec/SkSwizzler.cpp",
    "src/codec/SkWbmpCodec.cpp",
    "src/images/SkImageEncoder.cpp",
    "src/ports/SkDiscardableMemory_none.cpp",
    "src/ports/SkImageGenerator_skia.cpp",
    "src/ports/SkMemory_malloc.cpp",
    "src/ports/SkOSFile_stdio.cpp",
    "src/sfnt/SkOTTable_name.cpp",
    "src/sfnt/SkOTUtils.cpp",
    "src/utils/mac/SkStream_mac.cpp",
    "third_party/gif/SkGifImageReader.cpp",
  ]

  libs = []

  if (is_win) {
    sources += [
      "src/fonts/SkFontMgr_indirect.cpp",
      "src/ports/SkDebug_win.cpp",
      "src/ports/SkFontHost_win.cpp",
      "src/ports/SkFontMgr_win_dw.cpp",
      "src/ports/SkFontMgr_win_dw_factory.cpp",
      "src/ports/SkImageEncoder_WIC.cpp",
      "src/ports/SkImageGeneratorWIC.cpp",
      "src/ports/SkOSFile_win.cpp",
      "src/ports/SkOSLibrary_win.cpp",
      "src/ports/SkScalerContext_win_dw.cpp",
      "src/ports/SkTLS_win.cpp",
      "src/ports/SkTypeface_win_dw.cpp",
    ]
    libs += [
      "FontSub.lib",
      "Gdi32.lib",
      "Ole32.lib",
      "OleAut32.lib",
      "User32.lib",
      "Usp10.lib",
    ]
  } else {
    sources += [
      "src/ports/SkOSFile_posix.cpp",
      "src/ports/SkOSLibrary_posix.cpp",
      "src/ports/SkTLS_pthread.cpp",
    ]
    libs += [ "dl" ]
  }

  if (is_android) {
    deps += [ "//third_party/expat" ]
    if (defined(ndk) && ndk != "") {
      deps += [ "//third_party/cpu-features" ]
    }
    sources += [ "src/ports/SkDebug_android.cpp" ]
    libs += [
      "EGL",
      "GLESv2",
      "log",
    ]
  }

  if (is_linux) {
    sources += [ "src/ports/SkDebug_stdio.cpp" ]
  }

  if (is_mac) {
    sources += [
      "src/ports/SkDebug_stdio.cpp",
      "src/ports/SkFontHost_mac.cpp",
      "src/ports/SkImageEncoder_CG.cpp",
      "src/ports/SkImageGeneratorCG.cpp",
    ]
    libs += [
      # AppKit symbols NSFontWeightXXX may be dlsym'ed.
      "AppKit.framework",
      "ApplicationServices.framework",
      "OpenGL.framework",
    ]
  }

  if (is_ios) {
    sources += [
      "src/ports/SkDebug_stdio.cpp",
      "src/ports/SkFontHost_mac.cpp",
      "src/ports/SkImageEncoder_CG.cpp",
      "src/ports/SkImageGeneratorCG.cpp",
    ]
    libs += [
      "CoreFoundation.framework",
      "CoreGraphics.framework",
      "CoreText.framework",
      "ImageIO.framework",
      "MobileCoreServices.framework",

      # UIKit symbols UIFontWeightXXX may be dlsym'ed.
      "UIKit.framework",
    ]
  }

  if (is_fuchsia) {
    sources += [ "src/ports/SkDebug_stdio.cpp" ]
  }
}

# Targets guarded by skia_enable_tools may use //third_party freely.
if (skia_enable_tools) {
  # Used by gn_to_bp.py to list our public include dirs.
  source_set("public") {
    configs += [ ":skia_public" ]
  }

  config("skia.h_config") {
    include_dirs = [ "$target_gen_dir" ]
    if (skia_use_vulkan) {
      # So we can get the header which includes vulkan
      include_dirs += [ "tools/gpu/vk" ]
    }
  }
  action("skia.h") {
    public_configs = [ ":skia.h_config" ]
    skia_h = "$target_gen_dir/skia.h"
    script = "gn/find_headers.py"
    args = [ rebase_path(skia_h, root_build_dir) ] +
           rebase_path(skia_public_includes)
    depfile = "$skia_h.deps"
    outputs = [
      skia_h,
    ]
  }

  if (skia_enable_gpu && target_cpu == "x64") {
    executable("fiddle") {
      libs = []
      sources = [
        "tools/fiddle/draw.cpp",
        "tools/fiddle/fiddle_main.cpp",
      ]

      if (skia_use_egl) {
        sources += [ "tools/fiddle/egl_context.cpp" ]
      } else {
        sources += [ "tools/fiddle/null_context.cpp" ]
      }
      testonly = true
      deps = [
        ":flags",
        ":gpu_tool_utils",
        ":skia",
        ":skia.h",
      ]
    }
  }

  if (skia_enable_gpu) {
    source_set("public_headers_warnings_check") {
      sources = [
        "tools/public_headers_warnings_check.cpp",
      ]
      configs -= [ "//gn:warnings_except_public_headers" ]
      deps = [
        ":skia",
        ":skia.h",
      ]
    }
  }

  template("test_lib") {
    config(target_name + "_config") {
      include_dirs = invoker.public_include_dirs
      if (defined(invoker.public_defines)) {
        defines = invoker.public_defines
      }
    }
    source_set(target_name) {
      forward_variables_from(invoker, "*", [ "public_include_dirs" ])
      public_configs = [
        ":" + target_name + "_config",
        ":skia_private",
      ]

      if (!defined(deps)) {
        deps = []
      }
      deps += [ ":skia" ]
      testonly = true
    }
  }

  template("test_app") {
    if (is_ios) {
      app_name = target_name
      gen_path = target_gen_dir

      action("${app_name}_generate_info_plist") {
        script = "//gn/gen_plist_ios.py"
        outputs = [
          "$gen_path/${app_name}_Info.plist",
        ]
        args = [ rebase_path("$gen_path/$app_name", root_build_dir) ]
      }

      bundle_data("${app_name}_bundle_info_plist") {
        public_deps = [
          ":${app_name}_generate_info_plist",
        ]
        sources = [
          "$gen_path/${app_name}_Info.plist",
        ]
        outputs = [
          "{{bundle_root_dir}}/Info.plist",
        ]
      }

      bundle_ios_data =
          defined(invoker.bundle_ios_data) && invoker.bundle_ios_data

      if (bundle_ios_data) {
        has_skps =
            "True" == exec_script("//gn/checkdir.py",
                                  [ rebase_path("skps", root_build_dir) ],
                                  "trim string")
        bundle_data("${app_name}_bundle_resources") {
          sources = [
            "resources",
          ]
          outputs = [
            # iOS reserves the folders 'Resources' and 'resources' so store one level deeper
            "{{bundle_resources_dir}}/data/resources",
          ]
        }

        if (has_skps) {
          bundle_data("${app_name}_bundle_skps") {
            sources = [
              "skps",
            ]
            outputs = [
              # Store in same folder as resources
              "{{bundle_resources_dir}}/data/skps",
            ]
          }
        }
      }

      executable("${app_name}_generate_executable") {
        forward_variables_from(invoker,
                               "*",
                               [
                                 "output_name",
                                 "visibility",
                                 "is_shared_library",
                               ])
        configs += [ ":skia_private" ]
        testonly = true
        output_name = rebase_path("$gen_path/$app_name", root_build_dir)
      }

      bundle_data("${app_name}_bundle_executable") {
        public_deps = [
          ":${app_name}_generate_executable",
        ]
        sources = [
          "$gen_path/$app_name",
        ]
        outputs = [
          "{{bundle_executable_dir}}/$app_name",
        ]
        testonly = true
      }

      create_bundle("$app_name") {
        product_type = "com.apple.product-type.application"
        testonly = true

        bundle_root_dir = "${root_build_dir}/${target_name}.app"
        bundle_resources_dir = bundle_root_dir
        bundle_executable_dir = bundle_root_dir
        bundle_plugins_dir = bundle_root_dir + "/Plugins"

        deps = [
          ":${app_name}_bundle_executable",
          ":${app_name}_bundle_info_plist",
        ]
        if (bundle_ios_data) {
          deps += [ ":${app_name}_bundle_resources" ]
          if (has_skps) {
            deps += [ ":${app_name}_bundle_skps" ]
          }
        }

        # should only code sign when running on a device, not the simulator
        if (target_cpu != "x64") {
          code_signing_script = "//gn/codesign_ios.py"
          code_signing_sources = [ "$target_gen_dir/$app_name" ]
          code_signing_outputs = [
            "$bundle_root_dir/_CodeSignature/CodeResources",
            "$bundle_root_dir/embedded.mobileprovision",
          ]
          code_signing_args = [
            rebase_path("$bundle_root_dir", root_build_dir),
            skia_ios_identity,
            skia_ios_profile,
          ]
        }
      }
    } else {
      # !is_ios

      if (defined(invoker.is_shared_library) && invoker.is_shared_library) {
        shared_library("lib" + target_name) {
          forward_variables_from(invoker, "*", [ "is_shared_library" ])
          configs += [ ":skia_private" ]
          testonly = true
        }
      } else {
        _executable = target_name
        executable(_executable) {
          forward_variables_from(invoker, "*", [ "is_shared_library" ])
          configs += [ ":skia_private" ]
          testonly = true
        }
      }
      if (is_android && skia_android_serial != "" && defined(_executable)) {
        action("push_" + target_name) {
          script = "gn/push_to_android.py"
          deps = [
            ":" + _executable,
          ]
          _stamp = "$target_gen_dir/$_executable.pushed_$skia_android_serial"
          outputs = [
            _stamp,
          ]
          args = [
            rebase_path("$root_build_dir/$_executable"),
            skia_android_serial,
            rebase_path(_stamp),
          ]
          testonly = true
        }
      }
    }
  }

  test_lib("gpu_tool_utils") {
    public_include_dirs = []
    if (skia_enable_gpu) {
      public_defines = []
      public_include_dirs += [ "tools/gpu" ]

      deps = []
      sources = [
        "tools/gpu/GrContextFactory.cpp",
        "tools/gpu/GrTest.cpp",
        "tools/gpu/TestContext.cpp",
        "tools/gpu/atlastext/GLTestAtlasTextRenderer.cpp",
        "tools/gpu/gl/GLTestContext.cpp",
        "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.cpp",
        "tools/gpu/gl/debug/DebugGLTestContext.cpp",
        "tools/gpu/gl/debug/GrBufferObj.cpp",
        "tools/gpu/gl/debug/GrFrameBufferObj.cpp",
        "tools/gpu/gl/debug/GrProgramObj.cpp",
        "tools/gpu/gl/debug/GrShaderObj.cpp",
        "tools/gpu/gl/debug/GrTextureObj.cpp",
        "tools/gpu/gl/debug/GrTextureUnitObj.cpp",
        "tools/gpu/gl/null/NullGLTestContext.cpp",
        "tools/gpu/mock/MockTestContext.cpp",
      ]
      libs = []

      if (is_android || skia_use_egl) {
        sources += [ "tools/gpu/gl/egl/CreatePlatformGLTestContext_egl.cpp" ]
      } else if (is_ios) {
        sources += [ "tools/gpu/gl/iOS/CreatePlatformGLTestContext_iOS.mm" ]
        libs += [ "OpenGLES.framework" ]
      } else if (is_linux) {
        sources += [ "tools/gpu/gl/glx/CreatePlatformGLTestContext_glx.cpp" ]
        libs += [ "X11" ]
      } else if (is_mac) {
        sources += [ "tools/gpu/gl/mac/CreatePlatformGLTestContext_mac.cpp" ]
      } else if (is_win) {
        sources += [ "tools/gpu/gl/win/CreatePlatformGLTestContext_win.cpp" ]
        libs += [
          "Gdi32.lib",
          "OpenGL32.lib",
        ]
      }

      cflags_objcc = [ "-fobjc-arc" ]

      if (skia_use_angle) {
        deps += [ "//third_party/angle2" ]
        sources += [ "tools/gpu/gl/angle/GLTestContext_angle.cpp" ]
      }
      if (skia_use_vulkan) {
        sources += [ "tools/gpu/vk/VkTestContext.cpp" ]
        sources += [ "tools/gpu/vk/VkTestUtils.cpp" ]
      }
      if (skia_use_metal) {
        sources += [ "tools/gpu/mtl/MtlTestContext.mm" ]
      }
    }
  }

  test_lib("flags") {
    public_include_dirs = [ "tools/flags" ]
    sources = [
      "tools/flags/SkCommandLineFlags.cpp",
    ]
  }
  test_lib("common_flags") {
    public_include_dirs = [ "tools/flags" ]
    sources = [
      "tools/flags/SkCommonFlags.cpp",
      "tools/flags/SkCommonFlagsConfig.cpp",
    ]
    deps = [
      ":flags",
      ":gpu_tool_utils",
    ]
  }

  test_lib("tool_utils") {
    public_include_dirs = [
      "tools",
      "tools/debugger",
      "tools/timer",
      "tools/trace",
    ]
    sources = [
      "tools/AndroidSkDebugToStdOut.cpp",
      "tools/CrashHandler.cpp",
      "tools/LsanSuppressions.cpp",
      "tools/ProcStats.cpp",
      "tools/Resources.cpp",
      "tools/SkRandomScalerContext.cpp",
      "tools/SkTestScalerContext.cpp",
      "tools/UrlDataManager.cpp",
      "tools/debugger/SkDebugCanvas.cpp",
      "tools/debugger/SkDrawCommand.cpp",
      "tools/debugger/SkJsonWriteBuffer.cpp",
      "tools/debugger/SkObjectParser.cpp",
      "tools/picture_utils.cpp",
      "tools/random_parse_path.cpp",
      "tools/sk_tool_utils.cpp",
      "tools/sk_tool_utils_font.cpp",
      "tools/timer/Timer.cpp",
      "tools/trace/SkChromeTracingTracer.cpp",
      "tools/trace/SkChromeTracingTracer.h",
      "tools/trace/SkDebugfTracer.cpp",
      "tools/trace/SkDebugfTracer.h",
      "tools/trace/SkEventTracingPriv.cpp",
      "tools/trace/SkEventTracingPriv.h",
    ]
    libs = []
    if (is_ios) {
      sources += [ "tools/ios_utils.m" ]
      libs += [ "Foundation.framework" ]
    }
    deps = [
      ":common_flags",
      ":flags",
      "//third_party/libpng",
    ]
    public_deps = [
      "//third_party/jsoncpp",
    ]
  }

  import("gn/gm.gni")
  test_lib("gm") {
    public_include_dirs = [ "gm" ]
    sources = gm_sources
    deps = [
      ":experimental_sksg",
      ":flags",
      ":gpu_tool_utils",
      ":skia",
      ":tool_utils",
    ]
  }

  import("gn/tests.gni")
  test_lib("tests") {
    public_include_dirs = [ "tests" ]
    sources = tests_sources + pathops_tests_sources
    if (!fontmgr_android_enabled) {
      sources -= [ "//tests/FontMgrAndroidParserTest.cpp" ]
    }
    deps = [
      ":experimental_sksg",
      ":flags",
      ":skia",
      ":tool_utils",
      "//third_party/libpng",
      "//third_party/zlib",
    ]
    if (skia_use_expat) {
      deps += [ ":experimental_svg_model" ]
    }
    public_deps = [
      ":gpu_tool_utils",  # Test.h #includes headers from this target.
    ]
  }

  import("gn/bench.gni")
  test_lib("bench") {
    public_include_dirs = [ "bench" ]
    sources = bench_sources
    deps = [
      ":flags",
      ":gm",
      ":gpu_tool_utils",
      ":skia",
      ":tool_utils",
    ]
  }

  test_lib("experimental_skottie") {
    public_include_dirs = [ "experimental/skottie" ]
    include_dirs = [ "tools" ]
    sources = [
      "experimental/skottie/Skottie.cpp",
      "experimental/skottie/SkottieAnimator.cpp",
      "experimental/skottie/SkottieParser.cpp",
      "experimental/skottie/SkottieProperties.cpp",
    ]
    deps = [
      ":experimental_sksg",
      ":skia",
      "//third_party/jsoncpp",
    ]
  }

  test_lib("experimental_svg_model") {
    public_include_dirs = [ "experimental/svg/model" ]
    sources = [
      "experimental/svg/model/SkSVGAttribute.cpp",
      "experimental/svg/model/SkSVGAttributeParser.cpp",
      "experimental/svg/model/SkSVGCircle.cpp",
      "experimental/svg/model/SkSVGClipPath.cpp",
      "experimental/svg/model/SkSVGContainer.cpp",
      "experimental/svg/model/SkSVGDOM.cpp",
      "experimental/svg/model/SkSVGEllipse.cpp",
      "experimental/svg/model/SkSVGGradient.cpp",
      "experimental/svg/model/SkSVGLine.cpp",
      "experimental/svg/model/SkSVGLinearGradient.cpp",
      "experimental/svg/model/SkSVGNode.cpp",
      "experimental/svg/model/SkSVGPath.cpp",
      "experimental/svg/model/SkSVGPattern.cpp",
      "experimental/svg/model/SkSVGPoly.cpp",
      "experimental/svg/model/SkSVGRadialGradient.cpp",
      "experimental/svg/model/SkSVGRect.cpp",
      "experimental/svg/model/SkSVGRenderContext.cpp",
      "experimental/svg/model/SkSVGSVG.cpp",
      "experimental/svg/model/SkSVGShape.cpp",
      "experimental/svg/model/SkSVGStop.cpp",
      "experimental/svg/model/SkSVGTransformableNode.cpp",
      "experimental/svg/model/SkSVGUse.cpp",
      "experimental/svg/model/SkSVGValue.cpp",
    ]
    deps = [
      ":skia",
    ]
  }

  test_lib("experimental_sksg") {
    public_include_dirs = [
      "experimental/sksg",
      "experimental/sksg/effects",
      "experimental/sksg/geometry",
      "experimental/sksg/paint",
    ]
    sources = [
      "experimental/sksg/SkSGDraw.cpp",
      "experimental/sksg/SkSGEffectNode.cpp",
      "experimental/sksg/SkSGGeometryNode.cpp",
      "experimental/sksg/SkSGGroup.cpp",
      "experimental/sksg/SkSGImage.cpp",
      "experimental/sksg/SkSGInvalidationController.cpp",
      "experimental/sksg/SkSGNode.cpp",
      "experimental/sksg/SkSGPaintNode.cpp",
      "experimental/sksg/SkSGRenderNode.cpp",
      "experimental/sksg/SkSGScene.cpp",
      "experimental/sksg/effects/SkSGClipEffect.cpp",
      "experimental/sksg/effects/SkSGMaskEffect.cpp",
      "experimental/sksg/effects/SkSGOpacityEffect.cpp",
      "experimental/sksg/effects/SkSGTransform.cpp",
      "experimental/sksg/geometry/SkSGGeometryTransform.cpp",
      "experimental/sksg/geometry/SkSGMerge.cpp",
      "experimental/sksg/geometry/SkSGPath.cpp",
      "experimental/sksg/geometry/SkSGRect.cpp",
      "experimental/sksg/geometry/SkSGText.cpp",
      "experimental/sksg/geometry/SkSGTrimEffect.cpp",
      "experimental/sksg/paint/SkSGColor.cpp",
      "experimental/sksg/paint/SkSGGradient.cpp",
    ]
    deps = [
      ":skia",
    ]
  }

  if (target_cpu != "wasm") {
    test_lib("views") {
      public_include_dirs = [ "include/views" ]
      sources = [
        "src/views/SkEvent.cpp",
        "src/views/SkEventSink.cpp",
        "src/views/SkTouchGesture.cpp",
        "src/views/SkView.cpp",
      ]
    }
  }

  if (skia_use_lua) {
    test_lib("lua") {
      public_include_dirs = []
      sources = [
        "src/utils/SkLua.cpp",
        "src/utils/SkLuaCanvas.cpp",
      ]
      deps = [
        "//third_party/lua",
      ]
    }

    test_app("lua_app") {
      sources = [
        "tools/lua/lua_app.cpp",
      ]
      deps = [
        ":lua",
        ":skia",
        "//third_party/lua",
      ]
    }

    test_app("lua_pictures") {
      sources = [
        "tools/lua/lua_pictures.cpp",
      ]
      deps = [
        ":flags",
        ":lua",
        ":skia",
        ":tool_utils",
        "//third_party/lua",
      ]
    }
  }

  test_app("bookmaker") {
    sources = [
      "tools/bookmaker/bookmaker.cpp",
      "tools/bookmaker/cataloger.cpp",
      "tools/bookmaker/definition.cpp",
      "tools/bookmaker/fiddleParser.cpp",
      "tools/bookmaker/includeParser.cpp",
      "tools/bookmaker/includeWriter.cpp",
      "tools/bookmaker/mdOut.cpp",
      "tools/bookmaker/parserCommon.cpp",
      "tools/bookmaker/selfCheck.cpp",
      "tools/bookmaker/spellCheck.cpp",
    ]
    deps = [
      ":flags",
      ":skia",
      ":tool_utils",
      "//third_party/jsoncpp",
    ]
  }

  if (target_cpu != "wasm") {
    import("gn/samples.gni")
    test_lib("samples") {
      public_include_dirs = [ "samplecode" ]
      include_dirs = [ "experimental" ]
      sources = samples_sources + [
                  # Relocating these files here, so that clients don't try to build them while they're
                  # still in active development. Clang's thread safety analysis gets tripped up by
                  # conditional locks.
                  "src/core/SkThreadedBMPDevice.cpp",
                  "src/core/SkThreadedBMPDevice.h",
                ]
      deps = [
        ":experimental_sksg",
        ":experimental_svg_model",
        ":flags",
        ":gm",
        ":tool_utils",
        ":views",
        ":xml",
      ]

      if (skia_use_lua) {
        sources += [ "samplecode/SampleLua.cpp" ]
        deps += [
          ":lua",
          "//third_party/lua",
        ]
      }
    }
    test_app("dm") {
      sources = [
        "dm/DM.cpp",
        "dm/DMFontMgr.cpp",
        "dm/DMGpuTestProcs.cpp",
        "dm/DMJsonWriter.cpp",
        "dm/DMSrcSink.cpp",
      ]
      include_dirs = [ "tests" ]
      deps = [
        ":common_flags",
        ":experimental_skottie",
        ":experimental_sksg",
        ":experimental_svg_model",
        ":flags",
        ":gm",
        ":gpu_tool_utils",
        ":skia",
        ":tests",
        ":tool_utils",
        "//third_party/jsoncpp",
        "//third_party/libpng",
      ]
    }
  }

  if (!is_win) {
    test_app("remote_demo") {
      sources = [
        "tools/remote_demo.cpp",
      ]
      deps = [
        ":skia",
      ]
    }
  }

  test_app("nanobench") {
    sources = [
      "bench/nanobench.cpp",
    ]
    deps = [
      ":bench",
      ":common_flags",
      ":experimental_sksg",
      ":experimental_svg_model",
      ":flags",
      ":gm",
      ":gpu_tool_utils",
      ":skia",
      ":tool_utils",
      "//third_party/jsoncpp",
    ]
  }

  test_app("skpinfo") {
    sources = [
      "tools/skpinfo.cpp",
    ]
    deps = [
      ":flags",
      ":skia",
    ]
  }

  if (skia_enable_gpu) {
    test_app("skpbench") {
      sources = [
        "tools/skpbench/skpbench.cpp",
      ]
      deps = [
        ":flags",
        ":gpu_tool_utils",
        ":skia",
        ":tool_utils",
      ]
    }
  }

  # We can't yet build ICU on iOS or Windows.
  if (!is_ios && !is_win && target_cpu != "wasm") {
    test_app("sktexttopdf-hb") {
      sources = [
        "tools/shape/SkShaper_harfbuzz.cpp",
        "tools/shape/using_skia_and_harfbuzz.cpp",
      ]
      deps = [
        ":skia",
        "//third_party/harfbuzz",
        "//third_party/icu",
      ]
    }
  }
  test_app("sktexttopdf") {
    sources = [
      "tools/shape/SkShaper_primitive.cpp",
      "tools/shape/using_skia_and_harfbuzz.cpp",
    ]
    deps = [
      ":skia",
    ]
  }

  test_app("create_flutter_test_images") {
    sources = [
      "tools/create_flutter_test_images.cpp",
    ]
    deps = [
      ":skia",
      ":tool_utils",
    ]
  }

  test_app("create_test_font") {
    sources = [
      "tools/create_test_font.cpp",
    ]
    deps = [
      ":skia",
    ]
    assert_no_deps = [
      # tool_utils requires the output of this app.
      ":tool_utils",
    ]
  }

  test_app("get_images_from_skps") {
    sources = [
      "tools/get_images_from_skps.cpp",
    ]
    deps = [
      ":flags",
      ":skia",
      "//third_party/jsoncpp",
    ]
  }

  test_app("colorspaceinfo") {
    sources = [
      "tools/colorspaceinfo.cpp",
    ]
    deps = [
      ":flags",
      ":skia",
      ":tool_utils",
    ]
  }

  if (!is_ios && target_cpu != "wasm") {
    test_app("skiaserve") {
      sources = [
        "tools/skiaserve/Request.cpp",
        "tools/skiaserve/Response.cpp",
        "tools/skiaserve/skiaserve.cpp",
        "tools/skiaserve/urlhandlers/BreakHandler.cpp",
        "tools/skiaserve/urlhandlers/ClipAlphaHandler.cpp",
        "tools/skiaserve/urlhandlers/CmdHandler.cpp",
        "tools/skiaserve/urlhandlers/ColorModeHandler.cpp",
        "tools/skiaserve/urlhandlers/DataHandler.cpp",
        "tools/skiaserve/urlhandlers/DownloadHandler.cpp",
        "tools/skiaserve/urlhandlers/EnableGPUHandler.cpp",
        "tools/skiaserve/urlhandlers/ImgHandler.cpp",
        "tools/skiaserve/urlhandlers/InfoHandler.cpp",
        "tools/skiaserve/urlhandlers/OpBoundsHandler.cpp",
        "tools/skiaserve/urlhandlers/OpsHandler.cpp",
        "tools/skiaserve/urlhandlers/OverdrawHandler.cpp",
        "tools/skiaserve/urlhandlers/PostHandler.cpp",
        "tools/skiaserve/urlhandlers/QuitHandler.cpp",
        "tools/skiaserve/urlhandlers/RootHandler.cpp",
      ]
      deps = [
        ":flags",
        ":gpu_tool_utils",
        ":skia",
        ":tool_utils",
        "//third_party/jsoncpp",
        "//third_party/libmicrohttpd",
        "//third_party/libpng",
      ]
    }
  }

  test_app("fuzz") {
    include_dirs = [
      "tools",
      "tools/debugger",
    ]
    sources = [
      "fuzz/FuzzCanvas.cpp",
      "fuzz/FuzzDrawFunctions.cpp",
      "fuzz/FuzzGradients.cpp",
      "fuzz/FuzzParsePath.cpp",
      "fuzz/FuzzPathop.cpp",
      "fuzz/FuzzScaleToSides.cpp",
      "fuzz/fuzz.cpp",
      "fuzz/oss_fuzz/FuzzRegionDeserialize.cpp",
      "fuzz/oss_fuzz/FuzzRegionSetPath.cpp",
      "tools/UrlDataManager.cpp",
      "tools/debugger/SkDebugCanvas.cpp",
      "tools/debugger/SkDrawCommand.cpp",
      "tools/debugger/SkJsonWriteBuffer.cpp",
      "tools/debugger/SkObjectParser.cpp",
      "tools/picture_utils.cpp",
    ]
    deps = [
      ":flags",
      ":gpu_tool_utils",
      ":skia",
      "//third_party/jsoncpp",
      "//third_party/libpng",
    ]
  }

  test_app("pathops_unittest") {
    sources = pathops_tests_sources + [
                rebase_path("tests/skia_test.cpp"),
                rebase_path("tests/Test.cpp"),
              ]
    deps = [
      ":flags",
      ":gpu_tool_utils",
      ":skia",
      ":tool_utils",
    ]
  }

  test_app("dump_record") {
    sources = [
      "tools/DumpRecord.cpp",
      "tools/dump_record.cpp",
    ]
    deps = [
      ":flags",
      ":skia",
    ]
  }

  test_app("skdiff") {
    sources = [
      "tools/skdiff/skdiff.cpp",
      "tools/skdiff/skdiff_html.cpp",
      "tools/skdiff/skdiff_main.cpp",
      "tools/skdiff/skdiff_utils.cpp",
    ]
    deps = [
      ":skia",
      ":tool_utils",
    ]
  }

  test_app("skp_parser") {
    sources = [
      "tools/skp_parser.cpp",
    ]
    deps = [
      ":skia",
      ":tool_utils",
      "//third_party/jsoncpp",
    ]
  }

  if (!is_win && skia_enable_gpu) {
    test_lib("skqp_lib") {
      public_include_dirs = [ "tools/skqp" ]
      defines =
          [ "SK_SKQP_GLOBAL_ERROR_TOLERANCE=$skia_skqp_global_error_tolerance" ]
      if (skia_skqp_enable_driver_correctness_workarounds) {
        defines += [ "SK_SKQP_ENABLE_DRIVER_CORRECTNESS_WORKAROUNDS" ]
      }
      sources = [
        "dm/DMFontMgr.cpp",
        "dm/DMGpuTestProcs.cpp",
        "tools/skqp/gm_knowledge.cpp",
        "tools/skqp/gm_runner.cpp",
      ]
      deps = [
        ":gm",
        ":gpu_tool_utils",
        ":skia",
        ":tests",
        ":tool_utils",
      ]
    }
    test_app("skqp") {
      sources = [
        "tools/skqp/skqp.cpp",
      ]
      deps = [
        ":skia",
        ":skqp_lib",
        ":tool_utils",
        "//third_party/googletest",
      ]
    }
  }
  if (is_android && skia_enable_gpu) {
    test_app("skqp_app") {
      is_shared_library = true
      sources = [
        "tools/skqp/jni/org_skia_skqp_SkQPRunner.cpp",
      ]
      deps = [
        ":skia",
        ":skqp_lib",
        ":tool_utils",
      ]
      libs = [ "android" ]
    }
  }

  test_app("list_gms") {
    sources = [
      "tools/list_gms.cpp",
    ]
    deps = [
      ":gm",
      ":skia",
    ]
  }
  test_app("list_gpu_unit_tests") {
    sources = [
      "dm/DMGpuTestProcs.cpp",
      "tools/list_gpu_unit_tests.cpp",
    ]
    deps = [
      ":skia",
      ":tests",
    ]
  }

  if (skia_enable_gpu) {
    test_lib("sk_app") {
      public_include_dirs = [ "tools/sk_app" ]
      sources = [
        "tools/sk_app/CommandSet.cpp",
        "tools/sk_app/GLWindowContext.cpp",
        "tools/sk_app/Window.cpp",
      ]
      libs = []

      if (is_android) {
        sources += [
          "tools/sk_app/android/GLWindowContext_android.cpp",
          "tools/sk_app/android/RasterWindowContext_android.cpp",
          "tools/sk_app/android/Window_android.cpp",
          "tools/sk_app/android/main_android.cpp",
          "tools/sk_app/android/surface_glue_android.cpp",
        ]
        libs += [ "android" ]
      } else if (is_linux) {
        sources += [
          "tools/sk_app/unix/GLWindowContext_unix.cpp",
          "tools/sk_app/unix/RasterWindowContext_unix.cpp",
          "tools/sk_app/unix/Window_unix.cpp",
          "tools/sk_app/unix/keysym2ucs.c",
          "tools/sk_app/unix/main_unix.cpp",
        ]
        libs += [
          "GL",
          "X11",
        ]
      } else if (is_win) {
        sources += [
          "tools/sk_app/win/GLWindowContext_win.cpp",
          "tools/sk_app/win/RasterWindowContext_win.cpp",
          "tools/sk_app/win/Window_win.cpp",
          "tools/sk_app/win/main_win.cpp",
        ]
        if (skia_use_angle) {
          sources += [ "tools/sk_app/win/ANGLEWindowContext_win.cpp" ]
        }
      } else if (is_mac) {
        sources += [
          "tools/sk_app/mac/GLWindowContext_mac.cpp",
          "tools/sk_app/mac/RasterWindowContext_mac.cpp",
          "tools/sk_app/mac/Window_mac.cpp",
          "tools/sk_app/mac/main_mac.cpp",
        ]
        libs += [
          "QuartzCore.framework",
          "Cocoa.framework",
          "Foundation.framework",
        ]
      } else if (is_ios) {
        sources += [
          "tools/sk_app/ios/GLWindowContext_ios.cpp",
          "tools/sk_app/ios/RasterWindowContext_ios.cpp",
          "tools/sk_app/ios/Window_ios.cpp",
          "tools/sk_app/ios/main_ios.cpp",
        ]
      }

      if (skia_use_vulkan) {
        sources += [ "tools/sk_app/VulkanWindowContext.cpp" ]
        if (is_android) {
          sources += [ "tools/sk_app/android/VulkanWindowContext_android.cpp" ]
        } else if (is_linux) {
          sources += [ "tools/sk_app/unix/VulkanWindowContext_unix.cpp" ]
          libs += [ "X11-xcb" ]
        } else if (is_win) {
          sources += [ "tools/sk_app/win/VulkanWindowContext_win.cpp" ]
        }
      }

      deps = [
        ":gpu_tool_utils",
        ":skia",
        ":tool_utils",
        ":views",
      ]
      if (is_android) {
        deps += [ "//third_party/native_app_glue" ]
      } else if (is_mac || is_ios) {
        deps += [ "//third_party/libsdl" ]
      }
      if (skia_use_angle) {
        deps += [ "//third_party/angle2" ]
      }
    }
  }

  if (skia_enable_gpu) {
    test_app("viewer") {
      is_shared_library = is_android
      if (is_ios) {
        bundle_ios_data = true
      }
      sources = [
        "tools/viewer/GMSlide.cpp",
        "tools/viewer/ImGuiLayer.cpp",
        "tools/viewer/ImageSlide.cpp",
        "tools/viewer/SKPSlide.cpp",
        "tools/viewer/SampleSlide.cpp",
        "tools/viewer/SkottieSlide.cpp",
        "tools/viewer/SkottieSlide2.cpp",
        "tools/viewer/StatsLayer.cpp",
        "tools/viewer/Viewer.cpp",
      ]
      libs = []

      include_dirs = []
      deps = [
        ":experimental_skottie",
        ":experimental_sksg",
        ":flags",
        ":gm",
        ":gpu_tool_utils",
        ":samples",
        ":sk_app",
        ":skia",
        ":tool_utils",
        ":views",
        "//third_party/imgui",
        "//third_party/jsoncpp",
      ]
    }
  }

  if (skia_enable_gpu && !skia_use_angle && (is_linux || is_win || is_mac)) {
    test_app("HelloWorld") {
      sources = [
        "example/HelloWorld.cpp",
      ]
      libs = []

      include_dirs = []
      deps = [
        ":flags",
        ":gpu_tool_utils",
        ":sk_app",
        ":skia",
        ":tool_utils",
        ":views",
      ]
    }
  }

  if (skia_enable_gpu && (is_linux || is_mac || is_ios)) {
    test_app("SkiaSDLExample") {
      sources = [
        "example/SkiaSDLExample.cpp",
      ]
      libs = []
      include_dirs = []
      deps = [
        ":gpu_tool_utils",
        ":skia",
        "//third_party/libsdl",
      ]
    }
  }

  if (skia_qt_path != "" && (is_win || is_linux || is_mac)) {
    action_foreach("generate_mocs") {
      script = "gn/call.py"
      sources = [
        "tools/mdbviz/MainWindow.h",
      ]
      outputs = [
        "$target_gen_dir/mdbviz/{{source_name_part}}_moc.cpp",
      ]
      args = [
        "$skia_qt_path" + "/bin/moc",
        "{{source}}",
        "-o",
        "gen/mdbviz/{{source_name_part}}_moc.cpp",
      ]
    }
    action_foreach("generate_resources") {
      script = "gn/call.py"
      sources = [
        "tools/mdbviz/resources.qrc",
      ]
      outputs = [
        "$target_gen_dir/mdbviz/{{source_name_part}}_res.cpp",
      ]
      args = [
        "$skia_qt_path" + "/bin/rcc",
        "{{source}}",
        "-o",
        "gen/mdbviz/{{source_name_part}}_res.cpp",
      ]
    }
    test_app("mdbviz") {
      if (is_win) {
        # on Windows we need to disable some exception handling warnings due to the Qt headers
        cflags = [ "/Wv:18" ]  # 18 -> VS2013, 19 -> VS2015, 1910 -> VS2017
      }
      sources = [
        "tools/UrlDataManager.cpp",
        "tools/debugger/SkDebugCanvas.cpp",
        "tools/debugger/SkDrawCommand.cpp",
        "tools/debugger/SkJsonWriteBuffer.cpp",
        "tools/debugger/SkObjectParser.cpp",
        "tools/mdbviz/MainWindow.cpp",
        "tools/mdbviz/Model.cpp",
        "tools/mdbviz/main.cpp",
        "tools/picture_utils.cpp",

        # generated files
        "$target_gen_dir/mdbviz/MainWindow_moc.cpp",
        "$target_gen_dir/mdbviz/resources_res.cpp",
      ]
      lib_dirs = [ "$skia_qt_path/lib" ]
      libs = [
        "Qt5Core.lib",
        "Qt5Gui.lib",
        "Qt5Widgets.lib",
      ]
      include_dirs = [
        "$skia_qt_path/include",
        "$skia_qt_path/include/QtCore",
        "$skia_qt_path/include/QtWidgets",
        "tools",
        "tools/debugger",
      ]
      deps = [
        ":generate_mocs",
        ":generate_resources",
        ":skia",
        "//third_party/jsoncpp",
        "//third_party/libpng",
      ]
    }
  }

  if (is_android && defined(ndk) && ndk != "") {
    copy("gdbserver") {
      sources = [
        "$ndk/$ndk_gdbserver",
      ]
      outputs = [
        "$root_out_dir/gdbserver",
      ]
    }
  }
}

if (skia_jumper_clang != "") {
  action("regen_jumper") {
    script = "src/jumper/build_stages.py"

    inputs = [
      "src/jumper/SkJumper_stages.cpp",
      "src/jumper/SkJumper_stages_lowp.cpp",
    ]

    # GN insists its outputs should go somewhere underneath target_out_dir, so we trick it.
    outputs = [
      "$target_out_dir/" +
          rebase_path("src/jumper/SkJumper_generated.S", target_out_dir),
      "$target_out_dir/" +
          rebase_path("src/jumper/SkJumper_generated_win.S", target_out_dir),
    ]

    args = [
             skia_jumper_clang,
             skia_jumper_objdump,
             skia_jumper_ccache,
           ] + rebase_path(inputs) + rebase_path(outputs)
  }
}