# 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/config/python.gni")
import("//build/ohos/ebpf.gni")
import("//build/ohos_var.gni")

template("ohos_part") {
  assert(defined(invoker.subsystem_name), "subsystem_name is required.")
  assert(defined(invoker.module_list), "module_list is required.")

  part_label = get_label_info(":${target_name}", "label_with_toolchain")
  _deps = []
  foreach(module_label, invoker.module_list) {
    _deps += [ get_label_info(module_label, "label_with_toolchain") ]
  }

  # add sdk dep
  parts_targets_info_file =
      "${root_build_dir}/build_configs/required_parts_targets_list.json"

  parts_targets_info = read_file(parts_targets_info_file, "json")
  foreach(part_info, parts_targets_info) {
    if (part_info.part == part_label) {
      if (defined(part_info.inner_kits)) {
        _deps += [ part_info.inner_kits ]
      }
    }
  }

  part_name = target_name
  if (defined(invoker.origin_name)) {
    origin_part_name = invoker.origin_name
  } else {
    origin_part_name = part_name
  }
  if (defined(invoker.variant)) {
    variant_name = invoker.variant
  } else {
    variant_name = "phone"
  }

  part_modules_info_file = "${target_gen_dir}/${part_name}_modules.json"
  generated_file("${part_name}_info") {
    outputs = [ part_modules_info_file ]
    data_keys = [ "install_modules" ]
    output_conversion = "json"

    part_install_info = {
      part_label = part_label
      part_name = part_name
      origin_part_name = origin_part_name
      variant_name = variant_name
      subsystem_name = invoker.subsystem_name
      part_info_file =
          rebase_path(get_label_info(part_label, "target_out_dir"),
                      root_build_dir) + "/${part_name}_install_modules.json"
      toolchain_label = get_label_info(part_label, "toolchain")
      build_out_dir = rebase_path(root_out_dir, root_build_dir)
    }

    part_host_info = {
      part_label = part_label
      part_name = part_name
      origin_part_name = origin_part_name
      variant_name = variant_name
      subsystem_name = invoker.subsystem_name

      # generated by generate_part_info.py
      part_info_file =
          rebase_path(get_label_info(part_label, "target_out_dir"),
                      root_build_dir) + "/${part_name}_host_modules.json"
      toolchain_label = get_label_info(part_label, "toolchain")
      build_out_dir = rebase_path(root_out_dir, root_build_dir)
    }
    metadata = {
      part_installed_info = [ part_install_info ]
      part_host_info = [ part_host_info ]
    }
    deps = _deps
  }

  # ebpf
  if (ebpf_enable) {
    if (defined(invoker.ebpf_testcase)) {
      collect_ebpf_testcase("${part_name}_ebpf_testcase") {
        ebpf_testcase = invoker.ebpf_testcase
      }
      _deps += [ ":${part_name}_ebpf_testcase" ]
    }
  }

  part_install_modules_file =
      "${target_out_dir}/${part_name}_install_modules.json"
  part_dep_modules_file = "${target_out_dir}/${part_name}_dep_modules.json"
  part_sdk_modules_info_file =
      "${target_gen_dir}/${part_name}_sdk_install_modules.json"
  part_host_modules_file = "${target_out_dir}/${part_name}_host_modules.json"
  action_with_pydeps(target_name) {
    deps = [ ":${part_name}_info" ]
    script = "//build/ohos/generate_part_info.py"
    sources = [ part_modules_info_file ]
    depfile = "${target_gen_dir}/${target_name}.d"
    outputs = [
      part_install_modules_file,
      part_dep_modules_file,
    ]
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--part-name",
      part_name,
      "--origin-part-name",
      origin_part_name,
      "--input-file",
      rebase_path(part_modules_info_file, root_build_dir),
      "--sdk-modules-info-file",
      rebase_path(part_sdk_modules_info_file, root_build_dir),
      "--output-install-file",
      rebase_path(part_install_modules_file, root_build_dir),
      "--output-deps-file",
      rebase_path(part_dep_modules_file, root_build_dir),
      "--output-host-file",
      rebase_path(part_host_modules_file, root_build_dir),
      "--current-toolchain",
      "${current_toolchain}",
      "--host-toolchain",
      "${host_toolchain}",
    ]
  }
}