# Copyright (c) 2024-2025 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/clang/clang.gni")
import("//build/ohos.gni")

is_ohos_standard_system = is_standard_system && !is_arkui_x
use_mingw_win = "${current_os}_${current_cpu}" == "mingw_x86_64"
use_mac = "${current_os}_${current_cpu}" == "mac_x64" ||
          "${current_os}_${current_cpu}" == "mac_arm64"
use_ios = "${current_os}_${current_cpu}" == "ios_x64" ||
          "${current_os}_${current_cpu}" == "ios_arm64"
use_linux = "${current_os}_${current_cpu}" == "linux_x64"
windows_buildtool = "//build/toolchain/mingw:mingw_x86_64"

if (!defined(default_phone_source_dir)) {
  default_phone_source_dir = "/"
}
objcopy_default = "${default_phone_source_dir}/prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/bin/objcopy"
objcopy_mingw = "${default_phone_source_dir}/prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/bin/objcopy"
objcopy_x86_64 = "${default_clang_base_path}/bin/llvm-objcopy"
if ("${current_os}_${current_cpu}" == "mac_arm64") {
  mac_buildtool = "//build/toolchain/mac:clang_arm64"
} else if ("${current_os}_${current_cpu}" == "mac_x64") {
  mac_buildtool = "//build/toolchain/mac:clang_x64"
}
objcopy_clang = "$clang_base_path/bin/llvm-objcopy"
if (is_ohos_standard_system) {
  objcopy_default = "//prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-objcopy"
} else if (is_arkui_x) {
  if (host_os == "mac") {
    objcopy_default = objcopy_clang
  } else if (defined(phone_objcopy)) {
    objcopy_default = phone_objcopy
  }
}

template("gen_obj") {
  name = target_name
  action("gen_obj_" + name) {
    visibility = [ ":*" ]  # Only targets in this file can depend on this.

    if (use_mingw_win) {
      objcopy_tool = objcopy_mingw
      script = "//build/config/components/ace_engine/build_resource_to_bytecode.py"
    } else if (use_mac || target_os == "ios") {
      objcopy_tool = objcopy_clang
      script = "//build/config/components/ace_engine/build_resource_to_bytecode.py"
    } else if (use_linux) {
      objcopy_tool = objcopy_x86_64
      script = "//build/config/components/ace_engine/build_resource_to_bytecode.py"
    } else if (target_cpu == "x86_64") {
      objcopy_tool = objcopy_x86_64
      script = "//build/scripts/run_objcopy.py"
    } else {
      objcopy_tool = objcopy_default
      script = "//build/scripts/run_objcopy.py"
    }

    args = [
      "--objcopy",
      rebase_path(objcopy_tool),
      "--input",
      rebase_path(invoker.input),
      "--output",
      rebase_path(invoker.output),
      "--arch",
      current_cpu,
    ]

    deps = []
    deps += invoker.snapshot_dep

    inputs = [ invoker.input ]
    outputs = [ invoker.output ]
  }

  source_set("gen_obj_src_" + name) {
    sources = [ invoker.output ]
    deps = [ ":gen_obj_" + name ]
  }
}