15f9996aaSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
25f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
35f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
45f9996aaSopenharmony_ci# You may obtain a copy of the License at
55f9996aaSopenharmony_ci#
65f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
95f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
105f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
115f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
125f9996aaSopenharmony_ci# limitations under the License.
135f9996aaSopenharmony_ci
145f9996aaSopenharmony_citemplate("rust_cxx") {
155f9996aaSopenharmony_ci  assert(defined(invoker.sources),
165f9996aaSopenharmony_ci         "Must specify the Rust file to use as input.")
175f9996aaSopenharmony_ci
185f9996aaSopenharmony_ci  action_foreach(target_name) {
195f9996aaSopenharmony_ci    sources = invoker.sources
205f9996aaSopenharmony_ci
215f9996aaSopenharmony_ci    output_h = "{{source_gen_dir}}/{{source_file_part}}.h"
225f9996aaSopenharmony_ci    output_cc = "{{source_gen_dir}}/{{source_file_part}}.cc"
235f9996aaSopenharmony_ci
245f9996aaSopenharmony_ci    if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
255f9996aaSopenharmony_ci      subsystem_name = invoker.subsystem_name
265f9996aaSopenharmony_ci      part_name = invoker.part_name
275f9996aaSopenharmony_ci    } else if (defined(invoker.part_name)) {
285f9996aaSopenharmony_ci      part_name = invoker.part_name
295f9996aaSopenharmony_ci      _part_subsystem_info_file =
305f9996aaSopenharmony_ci          "$root_build_dir/build_configs/parts_info/part_subsystem.json"
315f9996aaSopenharmony_ci      _arguments = [
325f9996aaSopenharmony_ci        "--part-name",
335f9996aaSopenharmony_ci        part_name,
345f9996aaSopenharmony_ci        "--part-subsystem-info-file",
355f9996aaSopenharmony_ci        rebase_path(_part_subsystem_info_file, root_build_dir),
365f9996aaSopenharmony_ci      ]
375f9996aaSopenharmony_ci      get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
385f9996aaSopenharmony_ci      subsystem_name =
395f9996aaSopenharmony_ci          exec_script(get_subsystem_script, _arguments, "trim string")
405f9996aaSopenharmony_ci    } else if (defined(invoker.subsystem_name)) {
415f9996aaSopenharmony_ci      subsystem_name = invoker.subsystem_name
425f9996aaSopenharmony_ci      part_name = subsystem_name
435f9996aaSopenharmony_ci    } else {
445f9996aaSopenharmony_ci      subsystem_name = "build"
455f9996aaSopenharmony_ci      part_name = "build_framework"
465f9996aaSopenharmony_ci    }
475f9996aaSopenharmony_ci
485f9996aaSopenharmony_ci    cxxbridge_target =
495f9996aaSopenharmony_ci        "//third_party/rust/crates/cxx/gen/cmd:cxxbridge($host_toolchain)"
505f9996aaSopenharmony_ci
515f9996aaSopenharmony_ci    cxxbridge_out_dir = get_label_info(cxxbridge_target, "root_out_dir")
525f9996aaSopenharmony_ci    cxxbridge_executable = rebase_path(
535f9996aaSopenharmony_ci            "${cxxbridge_out_dir}/${subsystem_name}/${part_name}/cxxbridge")
545f9996aaSopenharmony_ci
555f9996aaSopenharmony_ci    script = "//build/templates/rust/rust_cxxbridge.py"
565f9996aaSopenharmony_ci    inputs = [
575f9996aaSopenharmony_ci      cxxbridge_executable,
585f9996aaSopenharmony_ci      script,
595f9996aaSopenharmony_ci    ]
605f9996aaSopenharmony_ci    deps = [ cxxbridge_target ]
615f9996aaSopenharmony_ci    outputs = [
625f9996aaSopenharmony_ci      output_h,
635f9996aaSopenharmony_ci      output_cc,
645f9996aaSopenharmony_ci    ]
655f9996aaSopenharmony_ci
665f9996aaSopenharmony_ci    args = [
675f9996aaSopenharmony_ci      "--cxxbridge",
685f9996aaSopenharmony_ci      rebase_path(cxxbridge_executable, root_build_dir),
695f9996aaSopenharmony_ci      "--cc",
705f9996aaSopenharmony_ci      output_cc,
715f9996aaSopenharmony_ci      "--header",
725f9996aaSopenharmony_ci      output_h,
735f9996aaSopenharmony_ci      "--",
745f9996aaSopenharmony_ci      "{{source}}",
755f9996aaSopenharmony_ci    ]
765f9996aaSopenharmony_ci  }
775f9996aaSopenharmony_ci}
78