# Copyright 2020 Google LLC
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("../../gn/skia.gni")
import("../../gn/toolchain/wasm.gni")
import("canvaskit.gni")

# These targets depend on components that are only declared if
# `skia_enable_tools` is true.
if (skia_enable_tools) {
  skia_component("viewer_wasm") {
    testonly = true
    include_dirs = [ "../.." ]
    sources = [
      "../../modules/svg/src/SkSVGAttribute.cpp",
      "../../modules/svg/src/SkSVGAttributeParser.cpp",
      "../../modules/svg/src/SkSVGCircle.cpp",
      "../../modules/svg/src/SkSVGClipPath.cpp",
      "../../modules/svg/src/SkSVGContainer.cpp",
      "../../modules/svg/src/SkSVGDOM.cpp",
      "../../modules/svg/src/SkSVGEllipse.cpp",
      "../../modules/svg/src/SkSVGFe.cpp",
      "../../modules/svg/src/SkSVGFeColorMatrix.cpp",
      "../../modules/svg/src/SkSVGFeComposite.cpp",
      "../../modules/svg/src/SkSVGFeTurbulence.cpp",
      "../../modules/svg/src/SkSVGFilter.cpp",
      "../../modules/svg/src/SkSVGFilterContext.cpp",
      "../../modules/svg/src/SkSVGGradient.cpp",
      "../../modules/svg/src/SkSVGLine.cpp",
      "../../modules/svg/src/SkSVGLinearGradient.cpp",
      "../../modules/svg/src/SkSVGNode.cpp",
      "../../modules/svg/src/SkSVGPath.cpp",
      "../../modules/svg/src/SkSVGPattern.cpp",
      "../../modules/svg/src/SkSVGPoly.cpp",
      "../../modules/svg/src/SkSVGRadialGradient.cpp",
      "../../modules/svg/src/SkSVGRect.cpp",
      "../../modules/svg/src/SkSVGRenderContext.cpp",
      "../../modules/svg/src/SkSVGSVG.cpp",
      "../../modules/svg/src/SkSVGShape.cpp",
      "../../modules/svg/src/SkSVGStop.cpp",
      "../../modules/svg/src/SkSVGText.cpp",
      "../../modules/svg/src/SkSVGTransformableNode.cpp",
      "../../modules/svg/src/SkSVGUse.cpp",
      "../../modules/svg/src/SkSVGValue.cpp",
      "../../tools/viewer/SKPSlide.cpp",
      "../../tools/viewer/SvgSlide.cpp",
    ]
  }

  skia_component("gm_wasm") {
    testonly = true
    include_dirs = [ "../.." ]
    deps = [
      "../..:hash_and_encode",
      "../..:tool_utils",
      "../../modules/svg:svg",
    ]
    defines = [ "SK_GANESH" ]
  }
}

skia_component("debugger") {
  include_dirs = [ "../.." ]
  sources = [
    "../../tools/SkSharingProc.cpp",
    "../../tools/UrlDataManager.cpp",
    "../../tools/debugger/DebugCanvas.cpp",
    "../../tools/debugger/DebugLayerManager.cpp",
    "../../tools/debugger/DrawCommand.cpp",
    "../../tools/debugger/JsonWriteBuffer.cpp",
  ]
  defines = [ "SK_GANESH" ]
}

action("create_notomono_cpp") {
  script = "../../tools/embed_resources.py"

  inputs = [ "fonts/NotoMono-Regular.ttf" ]

  output_path =
      "$root_out_dir/modules/canvaskit/fonts/NotoMono-Regular.ttf.ninja.cpp"

  outputs = [ output_path ]

  args = [
    "--name=SK_EMBEDDED_FONTS",
    "--input",
    rebase_path("fonts/NotoMono-Regular.ttf"),
    "--output",
    rebase_path(output_path),
    "--align=4",
  ]
}

skia_wasm_lib("canvaskit") {
  deps = [ "../..:skia" ]
  if (skia_canvaskit_enable_paragraph) {
    deps += [
      "../../modules/skparagraph:skparagraph",
      "../../modules/skunicode:skunicode",
    ]
  }
  if (skia_canvaskit_enable_bidi) {
    deps += [ "../../modules/skunicode:skunicode" ]
  }
  if (skia_enable_skottie) {
    deps += [
      "../../modules/skottie:skottie",
      "../../modules/skresources:skresources",
      "../../modules/sksg:sksg",
    ]
  }
  if (skia_canvaskit_include_viewer) {
    deps += [ ":viewer_wasm" ]
  }
  if (skia_canvaskit_enable_embedded_font) {
    deps += [ ":create_notomono_cpp" ]
  }
  if (skia_canvaskit_enable_pathops) {
    deps += [ "../..:pathops" ]
  }
  sources = [
    "WasmCommon.h",
    "canvaskit_bindings.cpp",
  ]
  if (skia_canvaskit_enable_paragraph) {
    sources += [
      "paragraph_bindings.cpp",
      "paragraph_bindings_gen.cpp",
    ]
  }
  if (skia_canvaskit_enable_bidi) {
    sources += [
      "bidi_bindings.cpp",
      "bidi_bindings_gen.cpp",
    ]
  }
  if (skia_enable_skottie) {
    sources += [
      "../../modules/skottie/utils/SkottieUtils.cpp",
      "../../modules/skottie/utils/TextEditor.cpp",
      "../../modules/skresources/src/SkResources.cpp",
      "skottie_bindings.cpp",
    ]
  }
  if (skia_canvaskit_include_viewer) {
    sources += [ "viewer_bindings.cpp" ]
    # TODO(kjlubick): remove this
  }
  if (skia_canvaskit_enable_debugger) {
    sources += [ "debugger_bindings.cpp" ]
    deps += [ ":debugger" ]
  }
  if (skia_canvaskit_enable_embedded_font) {
    sources += [
      "$root_out_dir/modules/canvaskit/fonts/NotoMono-Regular.ttf.ninja.cpp",
    ]
  }
  if (skia_canvaskit_enable_rt_shader) {
    sources += [ "../../tools/sksltrace/SkSLTraceUtils.cpp" ]
  }

  ldflags = []
  if (is_debug) {
    ldflags += [
      "-O0",
      "-sASSERTIONS=1",
      "-sGL_ASSERTIONS=1",
      "-g3",
      "--pre-js",
      rebase_path("debug.js"),
    ]
  } else {
    externs_path = rebase_path("externs.js")
    ldflags += [
      "-Oz",
      "--closure=1",
      "--pre-js",
      rebase_path("release.js"),
      "--closure-args=--externs=$externs_path",
    ]
  }
  if (skia_canvaskit_profile_build) {
    ldflags += [
      "--profiling-funcs",
      "--closure=0",
    ]
  }
  ldflags += [ "-fno-rtti" ]

  if (skia_canvaskit_enable_webgpu) {
    ldflags += [
      "--pre-js",
      rebase_path("cpu.js"),
      "--pre-js",
      rebase_path("webgpu.js"),
      "-sUSE_WEBGL2=0",
      "-sUSE_WEBGPU=1",
      "-sASYNCIFY",

      # Modules from html5_webgpu for JS<->WASM interop
      "-sEXPORTED_RUNTIME_METHODS=WebGPU,JsValStore",

      # TODO(skbug.com/40044518): The closure compiler doesn't play well with optimized builds and ASYNCIFY.
      "--closure=0",
    ]
  } else if (skia_canvaskit_enable_webgl) {
    ldflags += [
      "-lGL",
      "--pre-js",
      rebase_path("cpu.js"),
      "--pre-js",
      rebase_path("webgl.js"),
      "-sUSE_WEBGL2=1",
      "-sMAX_WEBGL_VERSION=2",
    ]
  } else {
    ldflags += [
      "--pre-js",
      rebase_path("cpu.js"),
      "-sUSE_WEBGL2=0",
    ]
  }

  ldflags += [
    "-std=c++20",
    "--bind",
    "--no-entry",
    "--pre-js",
    rebase_path("preamble.js"),
    "--pre-js",
    rebase_path("color.js"),
    "--pre-js",
    rebase_path("memory.js"),
    "--pre-js",
    rebase_path("util.js"),
    "--pre-js",
    rebase_path("interface.js"),
  ]

  if (skia_canvaskit_enable_matrix_helper) {
    ldflags += [
      "--pre-js",
      rebase_path("matrix.js"),
    ]
  }

  if (skia_canvaskit_enable_paragraph) {
    ldflags += [
      "--pre-js",
      rebase_path("paragraph.js"),
    ]
  }

  if (skia_canvaskit_enable_bidi) {
    ldflags += [
      "--pre-js",
      rebase_path("bidi.js"),
    ]
  }

  if (skia_enable_skottie) {
    ldflags += [
      "--pre-js",
      rebase_path("skottie.js"),
    ]
  }

  if (skia_canvaskit_enable_pathops) {
    ldflags += [
      "--pre-js",
      rebase_path("pathops.js"),
    ]
  }

  if (skia_canvaskit_enable_font) {
    ldflags += [
      "--pre-js",
      rebase_path("font.js"),
    ]
  }

  if (skia_canvaskit_enable_skp_serialization) {
    ldflags += [
      "--pre-js",
      rebase_path("skp.js"),
    ]
  }

  if (skia_canvaskit_enable_rt_shader) {
    ldflags += [
      "--pre-js",
      rebase_path("rt_shader.js"),
    ]
  }

  if (skia_canvaskit_enable_debugger) {
    ldflags += [
      "--pre-js",
      rebase_path("debugger.js"),
    ]
  }

  if (skia_canvaskit_enable_canvas_bindings) {
    ldflags += [
      "--pre-js",
      rebase_path("htmlcanvas/preamble.js"),
      "--pre-js",
      rebase_path("htmlcanvas/util.js"),
      "--pre-js",
      rebase_path("htmlcanvas/color.js"),
      "--pre-js",
      rebase_path("htmlcanvas/font.js"),
      "--pre-js",
      rebase_path("htmlcanvas/canvas2dcontext.js"),
      "--pre-js",
      rebase_path("htmlcanvas/htmlcanvas.js"),
      "--pre-js",
      rebase_path("htmlcanvas/htmlimage.js"),
      "--pre-js",
      rebase_path("htmlcanvas/imagedata.js"),
      "--pre-js",
      rebase_path("htmlcanvas/lineargradient.js"),
      "--pre-js",
      rebase_path("htmlcanvas/path2d.js"),
      "--pre-js",
      rebase_path("htmlcanvas/pattern.js"),
      "--pre-js",
      rebase_path("htmlcanvas/radialgradient.js"),
      "--pre-js",
      rebase_path("htmlcanvas/postamble.js"),
    ]
  }

  ldflags += [
    "--pre-js",
    rebase_path("postamble.js"),
    "-sALLOW_MEMORY_GROWTH",
    "-sDISABLE_EXCEPTION_CATCHING",
    "-sNODEJS_CATCH_EXIT=0",
    "-sDYNAMIC_EXECUTION=0",
    "-sEXPORT_NAME=CanvasKitInit",
    "-sEXPORTED_FUNCTIONS=[_malloc,_free]",
    "-sFORCE_FILESYSTEM=0",
    "-sFILESYSTEM=0",
    "-sMODULARIZE",
    "-sNO_EXIT_RUNTIME=1",
    "-sINITIAL_MEMORY=128MB",
    "-sWASM",
    "-sSTRICT=1",
    "-sEXPORTED_RUNTIME_METHODS=HEAP32,HEAPU8,HEAPU16,HEAPU32,HEAPF32",

    # INCOMING_MODULE_JS_API is set to empty by default.
    # https://github.com/emscripten-core/emscripten/blob/8dc91db45bf96c174531006839472a3924d105aa/src/settings.js#L1204
    "-sINCOMING_MODULE_JS_API=onRuntimeInitialized,locateFile",
  ]

  defines = [
    "SK_FORCE_8_BYTE_ALIGNMENT",
    "EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0",
  ]
  if (is_debug) {
    defines += [ "SK_DEBUG" ]
  } else {
    defines += [ "SK_RELEASE" ]
  }
  if (!is_debug && !skia_canvaskit_force_tracing) {
    defines += [ "SK_DISABLE_TRACING" ]
  }
  if (skia_canvaskit_enable_embedded_font) {
    defines += [ "CK_EMBED_FONT" ]
  }
  if (skia_use_icu && skia_use_harfbuzz) {
    deps += [
      "../../modules/skshaper",
      "../../modules/skunicode",
    ]
  }
  if (skia_canvaskit_enable_paragraph) {
    defines += [ "CK_INCLUDE_PARAGRAPH" ]
  }
  if (skia_canvaskit_enable_bidi) {
    defines += [ "CK_ENABLE_BIDI" ]
  }
  if (skia_canvaskit_enable_skp_serialization) {
    defines += [ "CK_SERIALIZE_SKP" ]
  }
  if (skia_enable_ganesh) {
    defines += [ "SK_GANESH" ]
    if (skia_canvaskit_enable_webgl) {
      defines += [
        "SK_GL",
        "CK_ENABLE_WEBGL",
      ]
    }
  }

  if (skia_canvaskit_enable_pathops) {
    defines += [ "CK_INCLUDE_PATHOPS" ]
  }
  if (skia_canvaskit_enable_rt_shader) {
    defines += [
      "CK_INCLUDE_RUNTIME_EFFECT",
      "CK_DEBUG_TRACE_JSON",
    ]
  }
  if (!skia_canvaskit_enable_alias_font) {
    defines += [ "CK_NO_ALIAS_FONT" ]
  }
  if (skia_canvaskit_legacy_draw_vertices_blend_mode) {
    # https://github.com/flutter/flutter/issues/98531
    defines += [ "SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER" ]
  }
  if (!skia_canvaskit_enable_font) {
    defines += [ "CK_NO_FONTS" ]
  }
}
