• 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
14import("//build/config/clang/clang.gni")
15
16template("rust_bindgen") {
17  assert(defined(invoker.header),
18         "Must specify the C header file to make bindings for.")
19  _target_name = target_name
20  if (defined(invoker.visibility)) {
21    _visibility = invoker.visibility
22  }
23  _testonly = false
24  if (defined(invoker.testonly)) {
25    _testonly = invoker.testonly
26  }
27  _deps = []
28  if (defined(invoker.deps)) {
29    _deps += invoker.deps
30  }
31  action(_target_name) {
32    sources = [ invoker.header ]
33    configs = default_compiler_configs
34    if (defined(invoker.configs)) {
35      configs += invoker.configs
36    }
37    testonly = _testonly
38    if (defined(_visibility)) {
39      visibility = _visibility
40    }
41
42    if (!ohos_indep_compiler_enable) {
43      if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
44        subsystem_name = invoker.subsystem_name
45        part_name = invoker.part_name
46      } else if (defined(invoker.part_name)) {
47        part_name = invoker.part_name
48        _part_subsystem_info_file =
49            "$root_build_dir/build_configs/parts_info/part_subsystem.json"
50        _arguments = [
51          "--part-name",
52          part_name,
53          "--part-subsystem-info-file",
54          rebase_path(_part_subsystem_info_file, root_build_dir),
55        ]
56        get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
57        subsystem_name =
58            exec_script(get_subsystem_script, _arguments, "trim string")
59      } else if (defined(invoker.subsystem_name)) {
60        subsystem_name = invoker.subsystem_name
61        part_name = subsystem_name
62      } else {
63        subsystem_name = "build"
64        part_name = "build_framework"
65      }
66    }
67
68    if (ohos_indep_compiler_enable) {
69      ohos_bindgen_target = "rust_bindgen:bindgen($host_toolchain)"
70      ohos_bindgen_obj_dir = "//binarys/third_party/rust/crates/cxx/innerapis/cxxbridge/clang_x64/libs"
71      ohos_bindgen_executable = "${ohos_bindgen_obj_dir}/bindgen"
72    } else {
73      ohos_bindgen_target = "//third_party/rust/crates/bindgen/bindgen-cli:bindgen($host_toolchain)"
74      ohos_bindgen_obj_dir = get_label_info(ohos_bindgen_target, "root_out_dir")
75      ohos_bindgen_executable =
76          "${ohos_bindgen_obj_dir}/${subsystem_name}/${part_name}/bindgen"
77    }
78
79    llvm_config_path = "$default_clang_base_path/bin/llvm-config"
80    clang_path = "$default_clang_base_path/bin/clang"
81
82    output_dir = "$target_gen_dir"
83    out_gen_rs = "$output_dir/${target_name}.rs"
84    script = rebase_path("//build/templates/rust/rust_bindgen.py")
85    inputs = [ ohos_bindgen_executable ]
86    depfile = "$target_out_dir/${target_name}.d"
87    outputs = [ out_gen_rs ]
88    if (ohos_indep_compiler_enable) {
89      external_deps = [ ohos_bindgen_target ]
90      deps = _deps
91    } else {
92      deps = [ ohos_bindgen_target ]
93      deps += _deps
94    }
95    args = [
96      "--exe",
97      rebase_path(ohos_bindgen_executable),
98      "--llvm-config-path",
99      rebase_path(llvm_config_path),
100      "--clang-path",
101      rebase_path(clang_path),
102      "--header",
103      rebase_path(invoker.header, root_build_dir),
104      "--ld-library-path",
105      rebase_path(clang_base_path + "/lib", root_build_dir),
106      "--depfile",
107      rebase_path(depfile, root_build_dir),
108      "--output",
109      rebase_path(out_gen_rs, root_build_dir),
110    ]
111    args += [
112      "--",
113      "{{cflags}}",
114      "{{cflags_c}}",
115      "{{defines}}",
116      "{{include_dirs}}",
117      "-fvisibility=default",
118      "-fparse-all-comments",
119    ]
120    if (defined(invoker.enable_c_plus_plus) &&
121        invoker.enable_c_plus_plus == true) {
122      args += [ "-x" ]
123      args += [ "c++" ]
124    }
125  }
126}
127