# Copyright 2024 The Lynx Authors. All rights reserved.
# Licensed under the Apache License Version 2.0 that can be found in the
# LICENSE file in the root directory of this source tree.

import("//build/util/write_cmake_config.gni")
import("../config.gni")
import("Lynx.gni")

# This target is used to contain configs that are required globally.
# This config is added to the basic xxx_source_set template.
# If you are using the basic xxx_source_set template, you do not need to add this config.
config("lynx_public_config") {
  ldflags = []
  include_dirs = []
  cflags = []
  defines = []
  include_dirs += [
    "..",
    "../platform",
    "../core",
    "build/gen",
    "../third_party",
  ]

  if (!lynx_export_symbols) {
    defines += [ "LYNX_NO_EXPORT" ]
  }

  cflags += [
    "-Wno-c99-designator",
    "-Wno-unknown-warning-option",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-variable",
    "-Wno-unused-parameter",
  ]
  defines += [
    "LYNX_ENABLE_E2E_TEST=${enable_e2e_test}",
    "ENABLE_TESTBENCH_REPLAY=${enable_testbench_replay}",
    "ENABLE_TESTBENCH_RECORDER=${enable_testbench_recorder}",
    "INSPECTOR_TEST=${enable_inspector_test}",
    "ENABLE_LEPUSNG_WORKLET=${enable_lepusng_worklet}",
    "ENABLE_NAPI_BINDING=${enable_napi_binding}",
    "DISABLE_NANBOX=${disable_nanbox}",
    "ENABLE_JUST_LEPUSNG=${enable_just_lepusng}",
    "ENABLE_INSPECTOR=${enable_inspector}",
    "LYNX_ENABLE_LOGBOX=${enable_logbox}",
  ]

  if (is_android) {
    if (is_debug) {
      defines += [ "DEBUG_MEMORY" ]
    }
    defines += [
      "GNU_SUPPORT=1",
      "OS_ANDROID=1",
      "OS_POSIX=1",
    ]
  } else if (is_ios) {
    defines += [
      "OS_IOS=1",
      "OS_POSIX=1",
    ]
  } else if (is_mac) {
    if (!enable_unittests && !is_oliver && !is_encoder_ut) {
      defines += [
        "OS_OSX=1",
        "OS_POSIX=1",
      ]
    }
    defines += [ "lynx_EXPORTS" ]
  } else if (is_linux) {
    defines += [ "OS_LINUX=1" ]
  }

  if (use_primjs_napi) {
    defines += [ "USE_PRIMJS_NAPI" ]
  }
  if (use_weak_suffix_napi) {
    defines += [ "USE_WEAK_SUFFIX_NAPI=1" ]
  }

  if (enable_trace == "perfetto") {
    defines += [ "ENABLE_TRACE_PERFETTO=1" ]
  } else if (enable_trace == "systrace") {
    defines += [ "ENABLE_TRACE_SYSTRACE=1" ]
  }

  if (!is_debug) {
    defines += [ "NDEBUG" ]
  }

  defines += [ "RAPIDJSON_HAS_STDSTRING=1" ]

  if (is_android) {
    defines += [
      "JS_ENGINE_TYPE=2",
      "QUICKJS_VER_LYNX",
    ]
  } else {
    if (jsengine_type == "jsc") {
      defines += [ "JS_ENGINE_TYPE=1" ]
    } else if (jsengine_type == "quickjs") {
      defines += [
        "JS_ENGINE_TYPE=2",
        "QUICKJS_VER_LYNX",
      ]
    } else if (jsengine_type == "v8") {
      defines += [ "JS_ENGINE_TYPE=0" ]
    }
  }

  if (is_harmony) {
    defines += [ "OS_HARMONY=1" ]
    if (enable_inspector) {
      defines += [ "EXPORT_SYMBOLS_FOR_DEVTOOL=1" ]
    }

    if (jsengine_type == "jsvm") {
      defines += [ "JS_ENGINE_TYPE=3" ]
    }
  }

  # Attach arguments to turn on Lynx Clang static checker plugin.
  if (enable_lynx_clang_plugin) {
    if (host_os == "linux") {
      plugin_dylib = rebase_path(
              "../../tools/clang/lynx_clang_plugin/release/libLynxClangPlugin.so")
    } else if (host_os == "mac") {
      plugin_dylib = rebase_path(
              "../../tools/clang/lynx_clang_plugin/release/libLynxClangPlugin.dylib")
    } else {
      print(
          "Unable to turn on Lynx Clang plugin on platforms other than MacOS or Linux.")
    }

    # Load plugin Clang args.
    clang_args = [
      "-load",
      plugin_dylib,
      "-add-plugin",
      "lynx_static_analysis",
    ]

    # Arguments for plugin.
    plugin_args = [
      "-config",
      rebase_path(
          "../../tools/clang/lynx_clang_plugin/release/lynx_config.json"),
      "-on_path",
      rebase_path("//"),
      "-output_dir",
      rebase_path("../../lynx_clang_plugin_output"),
    ]

    # Forward all plugin arguments with plugin prefix.
    foreach(arg, plugin_args) {
      clang_args += [
        "-plugin-arg-lynx_static_analysis",
        arg,
      ]
    }

    # Forward all arguments with -Xclang
    foreach(arg, clang_args) {
      cflags += [
        "-Xclang",
        arg,
      ]
    }
  }
}

config("config") {
  include_dirs = [
    ".",
    "../core",
    "//third_party",
  ]

  cflags = [
    "-Wno-c99-designator",
    "-Wno-unknown-warning-option",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-variable",
  ]

  defines = [ "RAPIDJSON_HAS_STDSTRING=1" ]
}

action("find_node_headers") {
  target_dir = rebase_path(node_headers_dst_dir)

  script = "../oliver/copy_node_headers.py"

  args = [ target_dir ]
  outputs = [ "$root_gen_dir/node" ]
}

group("lynx_core_native") {
  deps = [
    "base",
    "renderer/dom:dom",
  ]
}

group("lynx_native") {
  deps = [
    "../gfx",
    "../third_party/rapidjson",
    "animation",
    "animation/basic_animation",
    "animation/lynx_basic_animator:lynx_basic_animator",
    "base",
    "build:build",
    "event",
    "renderer:tasm_group",
    "renderer/css",
    "renderer/data:data",
    "renderer/events",
    "renderer/layout_scheduler",
    "renderer/pipeline",
    "renderer/signal:signal",
    "renderer/starlight",
    "renderer/utils:renderer_utils",
    "resource",
    "runtime/common/bindings/event:runtime_common",
    "runtime/common/bindings/modules:native_modules",
    "runtime/common/bindings/resource:promise",
    "runtime/js/bindings/interceptor:interceptor_factory",
    "runtime/lepus:lepus",
    "runtime/lepus/bindings:bindings",
    "services/event_report:event_report",
    "services/feature_count:feature_count",
    "services/fluency:fluency",
    "services/long_task_timing:long_task_timing",
    "services/performance:performance",
    "services/timing_handler:timing_handler",
    "services/watch_dog:watch_dog",
    "//third_party/modp_b64",
  ]

  if (is_ios) {
    deps += [ "services/fsp_tracing:fsp_tracing" ]
  }

  if (is_ios || is_android) {
    deps += [ "parser" ]
  }

  if (enable_recorder) {
    deps += [ "services/recorder:recorder" ]
  }
}