# 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.

template("rust_cxx") {
  assert(defined(invoker.sources),
         "Must specify the Rust file to use as input.")

  action_foreach(target_name) {
    sources = invoker.sources

    output_h = "{{source_gen_dir}}/{{source_file_part}}.h"
    output_cc = "{{source_gen_dir}}/{{source_file_part}}.cc"
    if (ohos_indep_compiler_enable) {
      cxxbridge_out_dir = get_label_info("rust_cxx:cxxbridge($host_toolchain)",
                                         "target_out_dir")
      cxxbridge_executable =
          rebase_path("${cxxbridge_out_dir}/clang_x64/libs/cxxbridge")
    } else {
      cxxbridge_out_dir =
          get_label_info("rust_cxx:cxxbridge($host_toolchain)", "root_out_dir")
      cxxbridge_executable =
          rebase_path("${cxxbridge_out_dir}/thirdparty/rust_cxx/cxxbridge")
    }

    script = "//build/templates/rust/rust_cxxbridge.py"
    inputs = [
      cxxbridge_executable,
      script,
    ]
    external_deps = [ "rust_cxx:cxxbridge($host_toolchain)" ]
    outputs = [
      output_h,
      output_cc,
    ]

    args = [
      "--cxxbridge",
      rebase_path(cxxbridge_executable, root_build_dir),
      "--cc",
      output_cc,
      "--header",
      output_h,
      "--",
      "{{source}}",
    ]
  }
}