• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2024 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/ohos.gni")
15
16hdf_fwk_path = "//drivers/hdf_core/framework"
17idl_tool_path = "//foundation/ability/idl_tool/idl_tool_2"
18
19idl_root = "//foundation/ability/idl_tool"
20idl_build_deps = ""
21idl_out_root = ""
22
23build_root = "//build"
24toolchain_linux = "$build_root/toolchain/linux:clang_x64"
25if (host_cpu == "arm64") {
26  toolchain_mac = "$build_root/toolchain/mac:clang_arm64"
27} else {
28  toolchain_mac = "$build_root/toolchain/mac:clang_x64"
29}
30toolchain_win = "$build_root/toolchain/mingw:mingw_x86_64"
31
32if (host_toolchain == toolchain_mac) {
33  idl_out_root = get_label_info("$idl_root:idl($toolchain_mac)", "root_out_dir")
34  idl_build_deps = [ "$idl_root:idl($toolchain_mac)" ]
35} else if (host_toolchain == toolchain_win) {
36  idl_out_root = get_label_info("$idl_root:idl($toolchain_win)", "root_out_dir")
37  idl_build_deps = [ "$idl_root:idl($toolchain_win)" ]
38} else {
39  idl_out_root =
40      get_label_info("$idl_root:idl($toolchain_linux)", "root_out_dir")
41  idl_build_deps = [ "$idl_root:idl($toolchain_linux)" ]
42}
43
44idl_build_path = idl_out_root + "/ability/idl_tool"
45
46template("hdi") {
47  assert(defined(invoker.sources), "sources must be set")
48  assert(defined(invoker.language), "language must be set")
49  assert(defined(invoker.subsystem_name), "subsystem_name must be set")
50  assert(defined(invoker.part_name), "part_name must be set")
51
52  # the module_name is an obsolete option
53  if (defined(invoker.module_name)) {
54    print(invoker.module_name)
55  }
56
57  # system type
58  system = "full"
59
60  # generate mode, the default value is "ipc", the optional values are "ipc" or "passthrough"
61  mode = "ipc"
62  if (defined(invoker.mode)) {
63    assert(invoker.mode == "ipc" || invoker.mode == "passthrough",
64           "hdi mode must be 'ipc' or 'passthrough'")
65    mode = invoker.mode
66  }
67
68  assert(invoker.language == "c" || invoker.language == "cpp",
69         "the language must be set to 'c' or 'cpp'")
70  language = invoker.language
71
72  imports = []
73  if (defined(invoker.imports)) {
74    imports += invoker.imports
75  }
76
77  root_package = "ohos.hdi"
78  root_path = rebase_path("//drivers/interface")
79  if (defined(invoker.root)) {
80    package_path_map = string_split(invoker.root, ":")
81    root_package = package_path_map[0]
82    root_path = rebase_path(package_path_map[1])
83  }
84  root_package_path = "${root_package}:${root_path}"
85
86  # set base directory of hdi files, set this parameter to your component name if you are using external idl files.
87  if (defined(invoker.base_dir)) {
88    root_path += invoker.base_dir
89  }
90
91  sources_gen_dir = get_path_info("${root_path}/", "gen_dir")
92  get_build_info_args = [
93    "-s",
94    system,
95    "-m",
96    mode,
97    "-l",
98    invoker.language,
99    "-o",
100    sources_gen_dir,
101    "-r",
102    root_package_path,
103  ]
104  foreach(idl_file, invoker.sources) {
105    get_build_info_args += [ "-f" ]
106    get_build_info_args += [ rebase_path(idl_file) ]
107  }
108
109  foreach(import_info, imports) {
110    get_build_info_args += [
111      "--import",
112      import_info,
113    ]
114  }
115
116  hdi_build_info = exec_script("$idl_tool_path/build_hdi_files_info.py",
117                               get_build_info_args,
118                               "json")
119  assert(defined(hdi_build_info.include_dirs), "missing include_dirs")
120  assert(defined(hdi_build_info.out_dir), "out_dir")
121  assert(defined(hdi_build_info.version), "missing version")
122  assert(defined(hdi_build_info.sources), "missing sources")
123  assert(defined(hdi_build_info.proxy_sources), "missing proxy_sources")
124  assert(defined(hdi_build_info.stub_sources), "missing stub_sources")
125  assert(defined(hdi_build_info.proxy_deps), "missing proxy_deps")
126  assert(defined(hdi_build_info.stub_deps), "missing stub_deps")
127  assert(defined(hdi_build_info.header_deps), "missing header_deps")
128
129  idl_headers_config = "$target_name" + "_idl_headers_config"
130  config("$idl_headers_config") {
131    include_dirs = hdi_build_info.include_dirs
132  }
133
134  action("idl_gen") {
135    deps = idl_build_deps
136    script = "/usr/bin/env"
137    if (defined(ohos_lite)) {
138      script = "//build/lite/run_shell_cmd.py"
139    }
140
141    idl_sources = invoker.sources
142    inputs = invoker.sources
143    outputs = hdi_build_info.sources
144
145    args = [
146      rebase_path("${idl_build_path}") + "/idl",
147      "--intf-type",
148      "hdi",
149      "--system",
150      system,
151      "--mode",
152      mode,
153      "-d",
154      rebase_path(hdi_build_info.out_dir),
155    ]
156
157    if (language == "c") {
158      args += [ "--gen-c" ]
159    } else if (language == "cpp") {
160      args += [ "--gen-cpp" ]
161    }
162
163    foreach(idl_file, idl_sources) {
164      args += [ "-c" ]
165      args += [ rebase_path(idl_file) ]
166    }
167    args += [
168      "-r",
169      root_package_path,
170    ]
171  }
172
173  lib_client = "lib" + target_name + "_proxy" + "_" + hdi_build_info.version
174  ohos_shared_library(lib_client) {
175    if (defined(invoker.sources)) {
176      sources = hdi_build_info.proxy_sources
177      public_configs = [ ":$idl_headers_config" ]
178      deps = [ ":idl_gen" ]
179      if (is_standard_system) {
180        public_deps = []
181        if (defined(invoker.sequenceable_pub_deps)) {
182          public_deps += invoker.sequenceable_pub_deps
183        }
184
185        public_deps += hdi_build_info.proxy_deps
186
187        external_deps = [
188          "c_utils:utils",
189          "hdf_core:libhdf_ipc_adapter",
190          "hdf_core:libhdi",
191          "hdf_core:libpub_utils",
192          "hilog:libhilog",
193        ]
194        if (defined(invoker.sequenceable_ext_deps)) {
195          external_deps += invoker.sequenceable_ext_deps
196        }
197        if (invoker.language == "c") {
198          external_deps += [ "hdf_core:libhdf_ipc_adapter" ]
199        } else if (invoker.language == "cpp") {
200          external_deps += [ "ipc:ipc_single" ]
201        }
202      } else {
203        external_deps = [ "hilog:libhilog" ]
204      }
205
206      public_external_deps = [
207        "hdf_core:libhdf_ipc_adapter",
208        "hdf_core:libhdf_utils",
209      ]
210
211      if (defined(invoker.innerapi_tags)) {
212        innerapi_tags = invoker.innerapi_tags
213      }
214      shlib_type = "hdi_proxy"
215      if (defined(invoker.install_images)) {
216        install_images = invoker.install_images
217      } else {
218        install_images = [ system_base_dir ]
219      }
220      subsystem_name = invoker.subsystem_name
221      partname_list = string_split(invoker.part_name, "_")
222      if (partname_list[0] == "drivers") {
223        part_name = invoker.part_name
224      } else {
225        part_name = invoker.part_name + "_interface"
226      }
227
228      if (defined(invoker.stack_protector_ret)) {
229        stack_protector_ret = invoker.stack_protector_ret
230      }
231
232      if (defined(invoker.sanitize)) {
233        sanitize = invoker.sanitize
234      }
235
236      if (defined(invoker.cflags)) {
237        cflags = invoker.cflags
238      }
239
240      if (defined(invoker.cflags_cc)) {
241        cflags_cc = invoker.cflags_cc
242      }
243
244      if (defined(invoker.branch_protector_ret)) {
245        branch_protector_ret = invoker.branch_protector_ret
246      }
247    }
248  }
249
250  if (mode == "ipc") {
251    lib_server = "lib" + target_name + "_stub" + "_" + hdi_build_info.version
252    ohos_shared_library(lib_server) {
253      if (defined(invoker.sources)) {
254        sources = hdi_build_info.stub_sources
255        public_configs = [ ":$idl_headers_config" ]
256
257        deps = [ ":idl_gen" ]
258        if (is_standard_system) {
259          public_deps = []
260          if (defined(invoker.sequenceable_pub_deps)) {
261            public_deps += invoker.sequenceable_pub_deps
262          }
263
264          public_deps += hdi_build_info.stub_deps
265
266          external_deps = [
267            "c_utils:utils",
268            "hdf_core:libhdf_ipc_adapter",
269            "hdf_core:libhdi",
270            "hilog:libhilog",
271          ]
272          if (defined(invoker.sequenceable_ext_deps)) {
273            external_deps += invoker.sequenceable_ext_deps
274          }
275          if (invoker.language == "c") {
276            external_deps += [
277              "hdf_core:libhdf_ipc_adapter",
278              "hdf_core:libhdf_utils",
279            ]
280          } else if (invoker.language == "cpp") {
281            external_deps += [ "ipc:ipc_single" ]
282          }
283        } else {
284          external_deps = [ "hilog:libhilog" ]
285        }
286
287        public_external_deps = [
288          "hdf_core:libhdf_ipc_adapter",
289          "hdf_core:libhdf_utils",
290        ]
291
292        shlib_type = "hdi_stub"
293        install_images = [ chipset_base_dir ]
294        if (defined(invoker.install_images)) {
295          foreach(img_name, invoker.install_images) {
296            if (img_name == updater_base_dir) {
297              install_images += [ updater_vendor_base_dir ]
298            }
299          }
300        }
301        subsystem_name = invoker.subsystem_name
302        part_name = invoker.part_name
303
304        if (defined(invoker.stack_protector_ret)) {
305          stack_protector_ret = invoker.stack_protector_ret
306        }
307
308        if (defined(invoker.sanitize)) {
309          sanitize = invoker.sanitize
310        }
311
312        if (defined(invoker.cflags)) {
313          cflags = invoker.cflags
314        }
315
316        if (defined(invoker.cflags_cc)) {
317          cflags_cc = invoker.cflags_cc
318        }
319
320        if (defined(invoker.branch_protector_ret)) {
321          branch_protector_ret = invoker.branch_protector_ret
322        }
323      }
324    }
325  }
326
327  # generate code and shared library
328  group("$target_name" + "_idl_target") {
329    deps = [ ":$lib_client" ]
330    if (mode == "ipc") {
331      deps += [ ":$lib_server" ]
332    }
333  }
334
335  # only generate code and provide header file path
336  # usage example: external_deps = [ "drivers_interface_xxx:xxx_idl_headers" ]
337  # this target has been replaced by 'idl_headers_target', please use 'idl_headers_target'
338  group("$target_name" + "_idl_headers") {
339    public_configs = [ ":$idl_headers_config" ]
340    deps = [ ":idl_gen" ]
341    public_external_deps = [
342      "hdf_core:libhdf_ipc_adapter",
343      "hdf_core:libhdf_utils",
344    ]
345  }
346
347  # only generate code and provide header file path
348  # usage example: external_deps = [ "drivers_interface_xxx:xxx_idl_headers_1.0" ]
349  idl_headers_target = target_name + "_idl_headers_" + hdi_build_info.version
350  group(idl_headers_target) {
351    public_configs = [ ":$idl_headers_config" ]
352    deps = [ ":idl_gen" ]
353    public_deps = hdi_build_info.header_deps
354    public_external_deps = [
355      "hdf_core:libhdf_ipc_adapter",
356      "hdf_core:libhdf_utils",
357    ]
358  }
359}
360