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

if (!defined(is_skia_standalone)) {
  is_skia_standalone = false
}

is_skia_dev_build = is_skia_standalone && !is_official_build

declare_args() {
  skia_android_serial = ""
  skia_compile_processors = false
  skia_enable_api_available_macro = true
  skia_enable_android_utils = is_skia_dev_build
  skia_enable_skgpu_v2 = false
  skia_enable_skgpu_v1 = true
  skia_enable_discrete_gpu = true
  skia_enable_flutter_defines = false
  skia_enable_fontmgr_empty = false
  skia_enable_fontmgr_fuchsia = is_fuchsia
  skia_enable_fontmgr_win = is_win
  skia_enable_gpu = true
  skia_enable_pdf = true
  skia_enable_skottie = !(is_win && is_component_build)
  skia_enable_skrive = true
  skia_enable_sksl = true
  skia_enable_skvm_jit_when_possible = is_skia_dev_build
  skia_enable_svg = !is_component_build
  skia_enable_tools = is_skia_dev_build
  skia_enable_gpu_debug_layers = is_skia_dev_build && is_debug
  skia_enable_winuwp = false
  skia_generate_workarounds = false
  skia_include_multiframe_procs = false
  skia_lex = false
  skia_libgifcodec_path = "third_party/externals/libgifcodec"
  skia_pdf_subset_harfbuzz =
      false  # TODO: set skia_pdf_subset_harfbuzz to skia_use_harfbuzz.
  skia_qt_path = getenv("QT_PATH")
  skia_skqp_global_error_tolerance = 0
  skia_tools_require_resources = false
  skia_update_fuchsia_sdk = false
  skia_use_angle = false
  skia_use_dawn = false
  skia_use_direct3d = false
  skia_use_egl = false
  skia_use_expat = true
  skia_use_experimental_xform = false
  skia_use_ffmpeg = false
  skia_use_fixed_gamma_text = is_android
  skia_use_fontconfig = is_linux
  skia_use_fonthost_mac = is_mac || is_ios
  skia_use_freetype = is_android || is_fuchsia || is_linux
  skia_use_harfbuzz = true
  skia_use_gl = !is_fuchsia
  skia_use_icu = !is_fuchsia
  skia_use_libheif = is_skia_dev_build
  skia_use_libjpeg_turbo_decode = true
  skia_use_libjpeg_turbo_encode = true
  skia_use_libpng_decode = true
  skia_use_libpng_encode = true
  skia_use_libwebp_decode = true
  skia_use_libwebp_encode = true
  skia_use_lua = is_skia_dev_build && !is_ios
  skia_use_metal = false
  skia_use_ndk_images = is_android && defined(ndk_api) && ndk_api >= 30
  skia_use_piex = !is_win
  skia_use_sfml = false
  skia_use_webgl = false
  skia_use_wuffs = false
  skia_use_x11 = is_linux
  skia_use_xps = true
  skia_use_zlib = true

  skia_vtune_path = ""

  if (is_ios) {
    skia_ios_identity = ".*Google.*"
    skia_ios_profile = "Google Development"
  }

  if (is_mac) {
    skia_gl_standard = "gl"
  } else if (is_ios) {
    skia_gl_standard = "gles"
  } else {
    skia_gl_standard = ""
  }

  if (is_android) {
    skia_use_vulkan = defined(ndk_api) && ndk_api >= 24
  } else if (is_fuchsia) {
    skia_use_vulkan = true
  } else {
    skia_use_vulkan = false
  }

  skia_build_fuzzers = is_clang && is_linux && target_cpu == "x64"
  skia_use_libfuzzer_defaults = true
}

declare_args() {
  skia_compile_sksl_tests = skia_compile_processors
  skia_enable_fontmgr_android = skia_use_expat && skia_use_freetype
  skia_enable_fontmgr_custom_directory = skia_use_freetype && !is_fuchsia
  skia_enable_fontmgr_custom_embedded = skia_use_freetype && !is_fuchsia
  skia_enable_fontmgr_custom_empty = skia_use_freetype
  skia_enable_fontmgr_fontconfig = skia_use_freetype && skia_use_fontconfig
  skia_enable_fontmgr_win_gdi = is_win && !skia_enable_winuwp
  skia_enable_fontmgr_FontConfigInterface =
      skia_use_freetype && skia_use_fontconfig
  skia_enable_spirv_validation = is_skia_dev_build && is_debug && !skia_use_dawn
  skia_use_dng_sdk =
      !is_fuchsia && skia_use_libjpeg_turbo_decode && skia_use_zlib
  skia_use_libgifcodec = !skia_use_wuffs
  skia_use_sfntly = skia_use_icu
  skia_enable_vulkan_debug_layers = skia_enable_gpu_debug_layers
  skia_enable_direct3d_debug_layer = skia_enable_gpu_debug_layers
  skia_enable_metal_debug_info = skia_enable_gpu_debug_layers
  skia_use_vma = skia_use_vulkan
}

declare_args() {
  # skia_fontmgr_factory should define SkFontMgr::Factory()
  if (skia_enable_fontmgr_empty) {
    skia_fontmgr_factory = ":fontmgr_empty_factory"
  } else if (is_android && skia_enable_fontmgr_android) {
    skia_fontmgr_factory = ":fontmgr_android_factory"
  } else if (is_win && skia_enable_fontmgr_win) {
    skia_fontmgr_factory = ":fontmgr_win_factory"
  } else if ((is_mac || is_ios) && skia_use_fonthost_mac) {
    skia_fontmgr_factory = ":fontmgr_mac_ct_factory"
  } else if (skia_enable_fontmgr_fontconfig) {
    skia_fontmgr_factory = ":fontmgr_fontconfig_factory"
  } else if (skia_enable_fontmgr_custom_directory) {
    skia_fontmgr_factory = ":fontmgr_custom_directory_factory"
  } else if (skia_enable_fontmgr_custom_embedded) {
    skia_fontmgr_factory = ":fontmgr_custom_embedded_factory"
  } else if (skia_enable_fontmgr_custom_empty) {
    skia_fontmgr_factory = ":fontmgr_custom_empty_factory"
  } else {
    #"src/ports/SkFontMgr_FontConfigInterface_factory.cpp" #WontFix
    #"src/ports/SkFontMgr_win_gdi_factory.cpp" # WontFix
    skia_fontmgr_factory = ":fontmgr_empty_factory"
  }
}

# Our tools require static linking (they use non-exported symbols), and the GPU backend.
skia_enable_tools = skia_enable_tools && !is_component_build && skia_enable_gpu

# The GPU build requires SkSL
skia_enable_sksl = skia_enable_sksl || skia_enable_gpu

# Skia's targets may be built inside other gn build systems.
# Skia builds other project's build targets inside its build.
# This is easier if the built-in target types remain generic.
# Place Skia target specific configs in skia_target_default_configs.
# These will be applied by the 'skia_*' templates.
# In the Skia build these configs apply many warnings as errors.
# Other projects may optionally set these configs as they see fit.
template("skia_target") {
  target(invoker._skia_target_type, target_name) {
    # set_defaults(invoker._skia_target_type) might not exist or set configs
    if (!defined(configs)) {
      configs = []
    }

    # Explicit configs instead of set_defaults("skia_target")
    # Allows template("skia_*") below to avoid the configs dance.
    if (defined(skia_target_default_configs)) {
      configs += skia_target_default_configs
    }

    # "*" clobbers the current scope; append to existing configs
    forward_variables_from(invoker, "*", [ "configs" ])
    if (defined(invoker.configs)) {
      configs += invoker.configs
    }
  }
}

template("skia_executable") {
  skia_target(target_name) {
    assert(!defined(configs), "No set_defaults(skia_target)")
    _skia_target_type = "executable"
    forward_variables_from(invoker, "*")
  }
}
template("skia_source_set") {
  skia_target(target_name) {
    assert(!defined(configs), "No set_defaults(skia_target)")
    _skia_target_type = "source_set"
    forward_variables_from(invoker, "*")
  }
}
template("skia_static_library") {
  skia_target(target_name) {
    assert(!defined(configs), "No set_defaults(skia_target)")
    _skia_target_type = "static_library"
    forward_variables_from(invoker, "*")
  }
}
template("skia_shared_library") {
  skia_target(target_name) {
    assert(!defined(configs), "No set_defaults(skia_target)")
    _skia_target_type = "shared_library"
    forward_variables_from(invoker, "*")
  }
}
template("skia_component") {
  skia_target(target_name) {
    assert(!defined(configs), "No set_defaults(skia_target)")
    _skia_target_type = "component"
    forward_variables_from(invoker, "*")
  }
}