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