# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/ohos.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")
import("//foundation/arkui/ace_engine/build/ace_controller.gni")
import("//foundation/arkui/ace_engine/interfaces/napi/kits/napi_lib.gni")

# Build libace static library
template("libace_static") {
  forward_variables_from(invoker, "*")

  ohos_source_set(target_name) {
    if (current_os == "ohos") {
      sanitize = {
        integer_overflow = true
        boundary_sanitize = true
        debug = ace_sanitize_debug
      }
    }

    deps = [ "$ace_root/frameworks/base:ace_base_$platform" ]
    external_deps = []
    if (use_mingw_win || use_mac || use_linux) {
      external_deps += [
        "icu:shared_icui18n",
        "icu:shared_icuuc",
      ]
    }

    if (platform == "ohos_ng" || is_arkui_x) {
      deps += [
        "$ace_root/frameworks/bridge:framework_bridge_ng_$platform",
        "$ace_root/frameworks/core:ace_core_ng_$platform",
      ]
    } else if (platform == "ohos" && !is_asan &&
               ace_engine_feature_enable_libace) {
      deps += [
        "$ace_root/frameworks/bridge:framework_bridge_$platform",
        "$ace_root/frameworks/core:ace_core_$platform",
      ]

      # add napi lib to libace.z.so for auto load in old generation mode
      foreach(module, common_napi_libs) {
        target_names = []
        prefix_dir = []
        module_path = []
        module_name = []

        target_names = string_split(module, "/")
        prefix_dir = target_names[0]
        if (prefix_dir != module) {
          module_path = target_names[1]
        } else {
          module_path = prefix_dir
        }
        module_name = string_replace(module_path, "_", "")

        deps += [ "$ace_root/interfaces/napi/kits/${module_path}:${module_name}_static_${platform}" ]
      }
    } else {
      deps += [
        "$ace_root/frameworks/bridge:framework_bridge_$platform",
        "$ace_root/frameworks/core:ace_core_$platform",
      ]
    }

    if (defined(config.platform_deps)) {
      deps += config.platform_deps
    }

    # build-in ark js engine for preview
    if (defined(config.use_build_in_js_engine) &&
        config.use_build_in_js_engine && defined(config.ark_engine)) {
      if (platform == "ohos_ng" || is_arkui_x) {
        deps += [ "$ace_root/frameworks/bridge/declarative_frontend:declarative_js_engine_ng_ark_$platform" ]
      } else {
        deps += [
          "$ace_root/frameworks/bridge/declarative_frontend:declarative_js_engine_ark_$platform",
          "$ace_root/frameworks/bridge/js_frontend/engine:js_engine_ark_$platform",
        ]
      }
    }
    configs = [ "$ace_root:ace_coverage_config" ]
    if (use_hilog) {
      external_deps += [ "hilog:libhilog" ]
    }
    part_name = ace_engine_part
    subsystem_name = ace_engine_subsystem
  }
}

# build platform engine sources
template("ace_bridge_engine") {
  forward_variables_from(invoker, "*")

  ohos_shared_library(target_name) {
    configs = [ "$ace_root:ace_coverage_config" ]
    deps = []

    assert(defined(platform) && (platform == "ohos" || platform == "ohos_ng"),
           "Only ohos need separated engine lib")

    if (use_hilog) {
      external_deps = [ "hilog:libhilog" ]
    }
    if (build_type == "engine") {
      if (use_js_debug) {
        deps += [ "$ace_root/frameworks/bridge/js_frontend/engine:js_engine_${engine_name}_debug_$platform" ]
      } else {
        deps += [ "$ace_root/frameworks/bridge/js_frontend/engine:js_engine_${engine_name}_$platform" ]
      }
    } else if (build_type == "engine_declarative") {
      deps += [ "$ace_root/frameworks/bridge/declarative_frontend:declarative_js_engine_${engine_name}_$platform" ]
    } else if (build_type == "engine_pa") {
      deps += [ "$ace_root/${pa_engine_path}/engine:js_pa_engine_${engine_name}_$platform" ]
    }

    subsystem_name = ace_engine_subsystem
    part_name = ace_engine_part
  }
}