2382c8bb创建于 2025年9月26日历史提交
# Copyright (c) 2023 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/components/ets_frontend/es2abc_config.gni")
import("//build/config/python.gni")
import("//build/ohos/notice/notice.gni")
import("//build/templates/common/collect_target.gni")
import("//build/templates/metadata/module_info.gni")

# Generate .abc files from .ts files
#
# Variables
#   sources: Paths to .ts file to compile
#
# Example
#   ohos_abc("foo_abc") {
#     sources = [ "example.ts" ]
#     subsystem_name = "example"
#     part_name = "example"
#   }
template("ohos_abc") {
  assert(defined(invoker.subsystem_name), "subsystem_name is required")
  assert(defined(invoker.part_name), "part_name is required")

  _test_target = defined(invoker.testonly) && invoker.testonly
  subsystem_name = invoker.subsystem_name
  part_name = invoker.part_name

  if (is_use_check_deps && !_test_target) {
    _check_target = "${target_name}__check"
    target_path = get_label_info(":${target_name}", "label_no_toolchain")
    check_target(_check_target) {
      module_deps = []
      module_ex_deps = []
      if (defined(invoker.deps)) {
        module_deps += invoker.deps
      }
      if (defined(invoker.public_deps)) {
        module_deps += invoker.public_deps
      }
      if (defined(invoker.external_deps)) {
        module_ex_deps += invoker.external_deps
      }
      if (defined(invoker.public_external_deps)) {
        module_ex_deps += invoker.public_external_deps
      }
    }
  }

  if (is_standard_system) {
    output_dir = "${root_out_dir}/${subsystem_name}/${part_name}"
  } else {
    output_dir = "${root_out_dir}"
  }

  target_label = get_label_info(":${target_name}", "label_with_toolchain")
  target_toolchain = get_label_info(target_label, "toolchain")

  if (target_toolchain == "${current_toolchain}") {
    ohos_abc_target = target_name
    module_info_target = "${target_name}_info"
    generate_module_info(module_info_target) {
      forward_variables_from(invoker, [ "testonly" ])
      module_name = ohos_abc_target
      if (!defined(invoker.module_type)) {
        module_type = "unknown"
      } else {
        module_type = invoker.module_type
      }
      module_source_dir = "$root_out_dir"
      if (defined(output_dir)) {
        module_source_dir = output_dir
      }

      if (defined(invoker.symlink_target_name)) {
        symlink_target_name = invoker.symlink_target_name
      }

      module_install_name = "${ohos_abc_target}.abc"
      if (defined(invoker.output_name)) {
        module_install_name = "${invoker.output_name}.abc"
      }

      module_install_images = [ "system" ]
      if (defined(invoker.install_images)) {
        module_install_images = []
        module_install_images += invoker.install_images
      }

      module_install_dir = "etc/abc"
      if (defined(invoker.module_install_dir)) {
        module_install_dir = invoker.module_install_dir
      }

      install_enable = true
      if (defined(invoker.install_enable)) {
        install_enable = invoker.install_enable
      }

      if (defined(invoker.relative_install_dir)) {
        relative_install_dir = invoker.relative_install_dir
      }

      notice = "$target_out_dir/$ohos_abc_target.notice.txt"
    }
  }

  if (!_test_target) {
    module_label = get_label_info(":${target_name}", "label_with_toolchain")
    _collect_target = "${target_name}__collect"
    collect_module_target(_collect_target) {
      forward_variables_from(invoker, [ "install_images" ])
    }

    _notice_target = "${target_name}__notice"
    ohos_abc_target = target_name
    collect_notice(_notice_target) {
      forward_variables_from(invoker, [ "testonly" ])
      if (defined(invoker.license_as_sources)) {
        license_as_sources = invoker.license_as_sources
      }
      if (defined(invoker.license_file)) {
        license_file = invoker.license_file
      }
      module_name = ohos_abc_target
      module_source_dir = get_label_info(":${ohos_abc_target}", "dir")
    }
  }

  action_with_pydeps(target_name) {
    forward_variables_from(invoker, [ "testonly" ])
    if (!defined(deps)) {
      deps = []
    }
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (is_arkui_x) {
      deps += es2abc_build_deps
    } else {
      if (!defined(external_deps)) {
        external_deps = []
      }
      if (host_toolchain == toolchain_mac) {
        external_deps = ["ets_frontend:es2panda($toolchain_mac)"]
      } else if (host_toolchain == toolchain_win) {
        external_deps = ["ets_frontend:es2panda(${host_toolchain})"]
      } else {
        external_deps = ["ets_frontend:es2panda($toolchain_linux)"]
      }
    }
    if (is_use_check_deps && !_test_target) {
      deps += [ ":$_check_target" ]
    }
    if (target_toolchain == "${current_toolchain}") {
      deps += [ ":$module_info_target" ]
    }
    if (!_test_target) {
      deps += [
        ":${_collect_target}",
        ":${_notice_target}",
      ]
    }

    if (defined(invoker.output_name)) {
      output_file = "${output_dir}/${invoker.output_name}.abc"
    } else {
      output_file = "${output_dir}/${target_name}.abc"
    }
    script = "//build/scripts/ohos_abc.py"
    sources = invoker.sources
    args = [
      "--outputs",
      rebase_path(output_file),
      "--es2abc",
      rebase_path(es2abc_build_path),
      "--sources",
    ]
    args += rebase_path(sources, root_build_dir)
    if (defined(invoker.merge_abc) && invoker.merge_abc) {
      args += [ "--merge-abc" ]
    }
    if (defined(invoker.disable_module) && invoker.disable_module) {
      args += [ "--module" ]
    }
    outputs = [ output_file ]

    install_module_info = {
      module_def = target_label
      module_info_file =
          rebase_path(get_label_info(module_def, "target_out_dir"),
                      root_build_dir) + "/${target_name}_module_info.json"
      toolchain = current_toolchain
      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
      subsystem_name = subsystem_name
      part_name = part_name
    }
    metadata = {
      install_modules = [ install_module_info ]
    }
  }
}