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