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

use_mingw_win = "${current_os}_${current_cpu}" == "mingw_x86_64"
objcopy_mingw = "//prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/bin/objcopy"
objcopy_clang = "$clang_base_path/bin/llvm-objcopy"
objcopy_x86_64 = "${default_clang_base_path}/bin/llvm-objcopy"
objcopy_default = "//prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/aarch64-linux-android/bin/objcopy"

if (is_standard_system && !is_arkui_x) {
  if (host_cpu == "arm64") {
    objcopy_default =
        "//prebuilts/clang/ohos/linux-aarch64/llvm/bin/llvm-objcopy"
  } else {
    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(aosp_objcopy)) {
    objcopy_default = aosp_objcopy
  }
}

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" ||
            "${current_os}_${current_cpu}" == "linux_arm64"

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 = "$ace_root/build/tools/run_objcopy.py"
      script = "//build/scripts/run_objcopy.py"
    } else {
      objcopy_tool = objcopy_default

      # script = "$ace_root/build/tools/run_objcopy.py"
      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 ]
  }
}