• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2023 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14template("rust_cxx") {
15  assert(defined(invoker.sources),
16         "Must specify the Rust file to use as input.")
17
18  action_foreach(target_name) {
19    sources = invoker.sources
20
21    output_h = "{{source_gen_dir}}/{{source_file_part}}.h"
22    output_cc = "{{source_gen_dir}}/{{source_file_part}}.cc"
23
24    if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
25      subsystem_name = invoker.subsystem_name
26      part_name = invoker.part_name
27    } else if (defined(invoker.part_name)) {
28      part_name = invoker.part_name
29      _part_subsystem_info_file =
30          "$root_build_dir/build_configs/parts_info/part_subsystem.json"
31      _arguments = [
32        "--part-name",
33        part_name,
34        "--part-subsystem-info-file",
35        rebase_path(_part_subsystem_info_file, root_build_dir),
36      ]
37      get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
38      subsystem_name =
39          exec_script(get_subsystem_script, _arguments, "trim string")
40    } else if (defined(invoker.subsystem_name)) {
41      subsystem_name = invoker.subsystem_name
42      part_name = subsystem_name
43    } else {
44      subsystem_name = "build"
45      part_name = "build_framework"
46    }
47
48    cxxbridge_target =
49        "//third_party/rust/crates/cxx/gen/cmd:cxxbridge($host_toolchain)"
50
51    cxxbridge_out_dir = get_label_info(cxxbridge_target, "root_out_dir")
52    cxxbridge_executable = rebase_path(
53            "${cxxbridge_out_dir}/${subsystem_name}/${part_name}/cxxbridge")
54
55    script = "//build/templates/rust/rust_cxxbridge.py"
56    inputs = [
57      cxxbridge_executable,
58      script,
59    ]
60    deps = [ cxxbridge_target ]
61    outputs = [
62      output_h,
63      output_cc,
64    ]
65
66    args = [
67      "--cxxbridge",
68      rebase_path(cxxbridge_executable, root_build_dir),
69      "--cc",
70      output_cc,
71      "--header",
72      output_h,
73      "--",
74      "{{source}}",
75    ]
76  }
77}
78