# Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Template to build HAP package.
#
# Variables:
#   output:   Output hap file, required.
#   sources:  List of files in the HAP project.
#   entry_libs: List of shared library files in the output directory will be copied to entry/lib.
#   entry_raw_resources: List of raw resource files in the output directory will be copied to entry/resource.
#   update_har: List of har files to update before build hap, optional.
#   auto_signed: Whether hap package is auto-signed or not, default is false, optional.
template("ohos_hap") {
  action(target_name) {
    script = "//arkweb/build/hap/build_hap.py"
    if (!is_arkweb_ext) {
      script = "//arkweb/build/hap/build_hap_rk.py"
    }
    forward_variables_from(invoker, [ "deps" ])

    inputs = [ script ]
    if (defined(invoker.sources)) {
      sources = invoker.sources
    } else {
      sources = []
    }
    inputs += sources

    assert(defined(invoker.output), "output is required for ohos_hap.")
    outputs = [ invoker.output ]

    args = [
      "--abi",
      current_cpu,
      "--sdk-root",
      rebase_path("//ohos_sdk", root_build_dir),
      "--node-root",
      rebase_path("//third_party/node/linux/node-linux-x64/", root_build_dir),
    ]

    _project_root_dir =
        rebase_path(get_path_info(target_name, "dir"), root_build_dir)
    depfile = "$target_gen_dir/$target_name.d"

    args += [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--root",
      _project_root_dir,
      "--output",
      rebase_path(invoker.output, root_build_dir),
    ]

    if (defined(invoker.entry_libs) || defined(invoker.entry_raw_resources)) {
      args += [
        "--override",
        rebase_path(root_out_dir, root_build_dir),
      ]
    }

    if (defined(invoker.entry_libs)) {
      foreach(_lib, invoker.entry_libs) {
        args += [
          "--entry-libs",
          _lib,
        ]
      }
    }

    if (defined(invoker.entry_raw_resources)) {
      foreach(_res, invoker.entry_raw_resources) {
        args += [
          "--entry-rawfiles",
          _res,
        ]
      }
    }

    if (defined(invoker.update_har)) {
      foreach(_har, invoker.update_har) {
        args += [
          "--update-har",
          _har,
        ]
      }
    }

    if (defined(invoker.auto_signed) && invoker.auto_signed) {
      args += [ "--auto-signed" ]
    }

    if (is_arkweb_ext && defined(invoker.is_component) && invoker.is_component) {
      args += [ "--component" ]
    }
  }
}

# Template to build HAR package.
#
# Variables:
#   module:   Har module name.
#   output:   Output hap file, required.
#   sources:  List of files in the HAP project.
#   entry_libs: list of shared library files in the output directory will be copied to entry/lib.
#   entry_raw_resources: list of raw resource files in the output directory will be copied to entry/resource.
template("ohos_har") {
  action(target_name) {
    script = "//arkweb/build/hap/build_hap.py"
    if (!is_arkweb_ext) {
      script = "//arkweb/build/hap/build_hap_rk.py"
    }

    forward_variables_from(invoker, [ "deps" ])

    inputs = [ script ]
    if (defined(invoker.sources)) {
      sources = invoker.sources
    } else {
      sources = []
    }
    inputs += sources

    assert(defined(invoker.module), "module is required for ohos_har.")

    assert(defined(invoker.output), "output is required for ohos_har.")
    outputs = [ invoker.output ]

    args = [
      "--har",
      invoker.module,
      "--sdk-root",
      rebase_path("//ohos_sdk", root_build_dir),
      "--node-root",
      rebase_path("//third_party/node/linux/node-linux-x64/", root_build_dir),
    ]

    _project_root_dir =
        rebase_path(get_path_info(target_name, "dir"), root_build_dir)
    depfile = "$target_gen_dir/$target_name.d"

    args += [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--root",
      _project_root_dir,
      "--output",
      rebase_path(invoker.output, root_build_dir),
    ]
  }
}