1# Copyright (c) 2023-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_root = "//binarys/foundation/ability/idl_tool/innerapis/idl/clang_x64" 18} else { 19 idl_root = "//foundation/ability/idl_tool" 20} 21 22idl_build_deps = "" 23idl_out_root = "" 24 25build_root = "//build" 26 27if (host_cpu == "arm64") { 28 toolchain_linux = "$build_root/toolchain/linux:clang_arm64" 29 toolchain_mac = "$build_root/toolchain/mac:clang_arm64" 30} else { 31 toolchain_linux = "$build_root/toolchain/linux:clang_x64" 32 toolchain_mac = "$build_root/toolchain/mac:clang_x64" 33} 34toolchain_win = "$build_root/toolchain/mingw:mingw_x86_64" 35 36if (host_toolchain == toolchain_mac) { 37 idl_out_root = get_label_info("$idl_root:idl($toolchain_mac)", "root_out_dir") 38 idl_build_deps = [ "$idl_root:idl($toolchain_mac)" ] 39} else if (host_toolchain == toolchain_win) { 40 idl_out_root = get_label_info("$idl_root:idl($toolchain_win)", "root_out_dir") 41 idl_build_deps = [ "$idl_root:idl($toolchain_win)" ] 42} else { 43 idl_out_root = 44 get_label_info("$idl_root:idl($toolchain_linux)", "root_out_dir") 45 idl_build_deps = [ "$idl_root:idl($toolchain_linux)" ] 46} 47 48if (ohos_indep_compiler_enable) { 49 idl_build_path = 50 idl_out_root + 51 "/obj/binarys/foundation/ability/idl_tool/innerapis/idl/clang_x64/libs" 52} else { 53 idl_build_path = idl_out_root + "/ability/idl_tool" 54} 55 56template("idl_gen_interface") { 57 not_needed(invoker, [ "dst_file" ]) 58 59 # idl sources 60 idl_list = [] 61 idl_callback_list = [] 62 idl_common_list = [] 63 64 if (defined(invoker.sources)) { 65 not_needed(invoker, [ "src_idl" ]) 66 67 # sources support multiple idl files 68 idl_list += rebase_path(invoker.sources) 69 } else { 70 # src_idl support single idl file 71 assert(defined(invoker.src_idl), "src_idl is required") 72 idl_list += [ invoker.src_idl ] 73 } 74 75 if (defined(invoker.sources_callback)) { 76 idl_callback_list += invoker.sources_callback 77 } 78 if (defined(invoker.sources_common)) { 79 idl_common_list += invoker.sources_common 80 } 81 82 # language, default cpp, support c/cpp/rust 83 language = "cpp" 84 if (defined(invoker.language)) { 85 assert(invoker.language == "c" || invoker.language == "cpp" || 86 invoker.language == "rust", 87 "the language must be set to 'c' or 'cpp' or 'rust', default 'cpp'") 88 language = invoker.language 89 } 90 91 # idl name transform 92 str_upper = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" 93 str_lower = "a b c d e f g h i j k l m n o p q r s t u v w x y z" 94 str_upper_list = string_split(str_upper, " ") 95 str_lower_list = string_split(str_lower, " ") 96 sources_all = [] 97 sources_proxy = [] 98 sources_stub = [] 99 100 # sources 101 foreach(idl_full_name, idl_list) { 102 name = get_path_info(idl_full_name, "name") 103 i = 0 104 foreach(s, str_upper_list) { 105 name = string_replace(name, s, "_" + str_lower_list[i]) 106 i = i + 1 107 } 108 109 # first letter 'i' 110 name_split = [] 111 name_split = string_split(name, "_i_") 112 if (name_split[0] == "") { 113 name = string_replace(name, "_i_", "", 1) 114 } 115 name_split = [] 116 name_split = string_split(name, "_") 117 if (name_split[0] == "") { 118 name = string_replace(name, "_", "", 1) 119 } 120 121 # second letter of result is '_' 122 name_split = [] 123 name_split = string_split(name, "_") 124 if (filter_include(str_lower_list, [ name_split[0] ]) != []) { 125 name = string_replace(name, "_", "", 1) 126 } 127 128 sources_all += [ 129 "${target_gen_dir}/" + name + "_proxy.cpp", 130 "${target_gen_dir}/" + name + "_stub.cpp", 131 ] 132 sources_proxy += [ "${target_gen_dir}/" + name + "_proxy.cpp" ] 133 sources_stub += [ "${target_gen_dir}/" + name + "_stub.cpp" ] 134 if (defined(invoker.client_enable) && invoker.client_enable) { 135 sources_all += [ "${target_gen_dir}/" + name + "_client.cpp" ] 136 sources_proxy += [ "${target_gen_dir}/" + name + "_client.cpp" ] 137 } 138 } 139 140 # callback sources 141 foreach(idl_full_name, idl_callback_list) { 142 idl_name = get_path_info(idl_full_name, "file") 143 i = 0 144 name = string_replace(idl_name, ".idl", "") 145 foreach(s, str_upper_list) { 146 name = string_replace(name, s, "_" + str_lower_list[i]) 147 i = i + 1 148 } 149 150 # first letter 'i' 151 name_split = [] 152 name_split = string_split(name, "_i_") 153 if (name_split[0] == "") { 154 name = string_replace(name, "_i_", "", 1) 155 } 156 name_split = [] 157 name_split = string_split(name, "_") 158 if (name_split[0] == "") { 159 name = string_replace(name, "_", "", 1) 160 } 161 162 # second letter of result is '_' 163 name_split = [] 164 name_split = string_split(name, "_") 165 if (filter_include(str_lower_list, [ name_split[0] ]) != []) { 166 name = string_replace(name, "_", "", 1) 167 } 168 169 out_full_name = string_replace(idl_full_name, idl_name, name) 170 171 sources_all += [ 172 "${target_gen_dir}/" + out_full_name + "_proxy.cpp", 173 "${target_gen_dir}/" + out_full_name + "_stub.cpp", 174 ] 175 sources_proxy += [ "${target_gen_dir}/" + out_full_name + "_stub.cpp" ] 176 sources_stub += [ "${target_gen_dir}/" + out_full_name + "_proxy.cpp" ] 177 } 178 179 # common sources 180 foreach(idl_full_name, idl_common_list) { 181 idl_name = get_path_info(idl_full_name, "file") 182 i = 0 183 name = string_replace(idl_name, ".idl", "") 184 foreach(s, str_upper_list) { 185 name = string_replace(name, s, "_" + str_lower_list[i]) 186 i = i + 1 187 } 188 189 # first letter 'i' 190 name_split = [] 191 name_split = string_split(name, "_i_") 192 if (name_split[0] == "") { 193 name = string_replace(name, "_i_", "i", 1) 194 } 195 name_split = [] 196 name_split = string_split(name, "_") 197 if (name_split[0] == "") { 198 name = string_replace(name, "_", "", 1) 199 } 200 201 # second letter of result is '_' 202 name_split = [] 203 name_split = string_split(name, "_") 204 if (filter_include(str_lower_list, [ name_split[0] ]) != []) { 205 name = string_replace(name, "_", "", 1) 206 } 207 208 out_full_name = string_replace(idl_full_name, idl_name, name) 209 210 sources_all += [ "${target_gen_dir}/" + out_full_name + ".cpp" ] 211 sources_proxy += [ "${target_gen_dir}/" + out_full_name + ".cpp" ] 212 sources_stub += [ "${target_gen_dir}/" + out_full_name + ".cpp" ] 213 } 214 215 action("$target_name") { 216 inputs = idl_list 217 if (defined(invoker.inputs)) { 218 inputs += invoker.inputs 219 } 220 if (is_arkui_x) { 221 deps = idl_build_deps 222 } else { 223 if (!defined(external_deps)) { 224 external_deps = [] 225 } 226 external_deps += [ "idl_tool:idl(${host_toolchain})" ] 227 } 228 229 script = "//build/config/components/idl_tool/idl.py" 230 args = [ 231 "--src-idl", 232 string_join(",", idl_list), 233 "--dst-path", 234 rebase_path(target_gen_dir), 235 "--idl-tool-path", 236 rebase_path(idl_build_path), 237 "--dst-file", 238 string_join(",", rebase_path(sources_all)), 239 "--language", 240 language, 241 ] 242 if (defined(invoker.log_domainid)) { 243 args += [ 244 "--log-domainid", 245 invoker.log_domainid, 246 ] 247 } 248 if (defined(invoker.log_tag)) { 249 args += [ 250 "--log-tag", 251 invoker.log_tag, 252 ] 253 } 254 if (defined(invoker.hitrace)) { 255 args += [ 256 "--hitrace", 257 invoker.hitrace, 258 ] 259 } 260 if (defined(invoker.client_enable) && invoker.client_enable) { 261 args += [ 262 "--client-enable", 263 "true", 264 ] 265 } 266 outputs = sources_all 267 } 268 269 idl_headers_config = target_name + "_idl_headers_config" 270 config("$idl_headers_config") { 271 include_dirs = [ target_gen_dir ] 272 if (defined(invoker.sub_include)) { 273 include_dirs += invoker.sub_include 274 } 275 } 276 277 if (invoker.target_type == "source_set" && defined(invoker.sources)) { 278 source_set_client = target_name + "_source_set_proxy" 279 source_set_server = target_name + "_source_set_stub" 280 action_target_name = ":" + target_name 281 282 # build client source_set 283 ohos_source_set(source_set_client) { 284 sources = [] 285 if (defined(invoker.sources_cpp)) { 286 sources += invoker.sources_cpp 287 } 288 sources += sources_proxy 289 if (defined(invoker.configs)) { 290 configs = invoker.configs 291 } 292 public_configs = [ ":$idl_headers_config" ] 293 deps = [ action_target_name ] 294 if (is_standard_system) { 295 external_deps = [ "c_utils:utils" ] 296 if (defined(invoker.hitrace)) { 297 external_deps += [ "hitrace:hitrace_meter" ] 298 } 299 if (defined(invoker.log_domainid)) { 300 external_deps += [ "hilog:libhilog" ] 301 } 302 if (defined(invoker.sequenceable_ext_deps)) { 303 external_deps += invoker.sequenceable_ext_deps 304 } 305 if (language == "c") { 306 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 307 } else if (language == "cpp") { 308 external_deps += [ "ipc:ipc_single" ] 309 } 310 } else { 311 external_deps = [ "hilog:libhilog" ] 312 } 313 if (defined(invoker.subsystem_name)) { 314 subsystem_name = invoker.subsystem_name 315 } 316 if (defined(invoker.part_name)) { 317 part_name = invoker.part_name 318 } 319 if (defined(invoker.innerapi_tags)) { 320 innerapi_tags = invoker.innerapi_tags 321 } 322 if (defined(invoker.sanitize)) { 323 sanitize = invoker.sanitize 324 } else { 325 sanitize = { 326 cfi = true 327 cfi_cross_dso = true 328 debug = false 329 } 330 } 331 if (defined(invoker.cflags)) { 332 cflags = invoker.cflags 333 } 334 if (defined(invoker.cflags_cc)) { 335 cflags_cc = invoker.cflags_cc 336 } 337 if (defined(invoker.remove_configs)) { 338 remove_configs = invoker.remove_configs 339 } 340 } 341 342 # build server source_set 343 ohos_source_set(source_set_server) { 344 sources = [] 345 if (defined(invoker.sources_cpp)) { 346 sources += invoker.sources_cpp 347 } 348 sources += sources_stub 349 if (defined(invoker.configs)) { 350 configs = invoker.configs 351 } 352 public_configs = [ ":$idl_headers_config" ] 353 deps = [ action_target_name ] 354 if (is_standard_system) { 355 external_deps = [ "c_utils:utils" ] 356 if (defined(invoker.hitrace)) { 357 external_deps += [ "hitrace:hitrace_meter" ] 358 } 359 if (defined(invoker.log_domainid)) { 360 external_deps += [ "hilog:libhilog" ] 361 } 362 if (defined(invoker.sequenceable_ext_deps)) { 363 external_deps += invoker.sequenceable_ext_deps 364 } 365 if (language == "c") { 366 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 367 } else if (language == "cpp") { 368 external_deps += [ "ipc:ipc_single" ] 369 } 370 } else { 371 external_deps = [ "hilog:libhilog" ] 372 } 373 if (defined(invoker.subsystem_name)) { 374 subsystem_name = invoker.subsystem_name 375 } 376 if (defined(invoker.part_name)) { 377 part_name = invoker.part_name 378 } 379 if (defined(invoker.sanitize)) { 380 sanitize = invoker.sanitize 381 } else { 382 sanitize = { 383 cfi = true 384 cfi_cross_dso = true 385 debug = false 386 } 387 } 388 if (defined(invoker.cflags)) { 389 cflags = invoker.cflags 390 } 391 if (defined(invoker.cflags_cc)) { 392 cflags_cc = invoker.cflags_cc 393 } 394 if (defined(invoker.remove_configs)) { 395 remove_configs = invoker.remove_configs 396 } 397 } 398 } 399 400 # build so 401 if ((language == "c" || language == "cpp" || 402 invoker.target_type == "shared_library") && defined(invoker.sources)) { 403 lib_client = "lib" + target_name + "_proxy" 404 lib_server = "lib" + target_name + "_stub" 405 action_target_name = ":" + target_name 406 407 # build client so 408 ohos_shared_library(lib_client) { 409 sources = [] 410 if (defined(invoker.sources_cpp)) { 411 sources += invoker.sources_cpp 412 } 413 sources += sources_proxy 414 if (defined(invoker.configs)) { 415 configs = invoker.configs 416 } 417 public_configs = [ ":$idl_headers_config" ] 418 deps = [ action_target_name ] 419 if (is_standard_system) { 420 public_deps = [] 421 if (defined(invoker.sequenceable_pub_deps)) { 422 public_deps += invoker.sequenceable_pub_deps 423 } 424 external_deps = [ "c_utils:utils" ] 425 if (defined(invoker.hitrace)) { 426 external_deps += [ "hitrace:hitrace_meter" ] 427 } 428 if (defined(invoker.log_domainid)) { 429 external_deps += [ "hilog:libhilog" ] 430 } 431 if (defined(invoker.sequenceable_ext_deps)) { 432 external_deps += invoker.sequenceable_ext_deps 433 } 434 if (language == "c") { 435 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 436 } else if (language == "cpp") { 437 external_deps += [ "ipc:ipc_single" ] 438 } 439 } else { 440 external_deps = [ "hilog:libhilog" ] 441 } 442 if (defined(invoker.subsystem_name)) { 443 subsystem_name = invoker.subsystem_name 444 } 445 if (defined(invoker.part_name)) { 446 part_name = invoker.part_name 447 } 448 if (defined(invoker.innerapi_tags)) { 449 innerapi_tags = invoker.innerapi_tags 450 } 451 if (defined(invoker.sanitize)) { 452 sanitize = invoker.sanitize 453 } else { 454 sanitize = { 455 cfi = true 456 cfi_cross_dso = true 457 debug = false 458 } 459 } 460 if (defined(invoker.cflags)) { 461 cflags = invoker.cflags 462 } 463 if (defined(invoker.cflags_cc)) { 464 cflags_cc = invoker.cflags_cc 465 } 466 if (defined(invoker.remove_configs)) { 467 remove_configs = invoker.remove_configs 468 } 469 } 470 471 # build server so 472 ohos_shared_library(lib_server) { 473 sources = [] 474 if (defined(invoker.sources_cpp)) { 475 sources += invoker.sources_cpp 476 } 477 sources += sources_stub 478 if (defined(invoker.configs)) { 479 configs = invoker.configs 480 } 481 public_configs = [ ":$idl_headers_config" ] 482 deps = [ action_target_name ] 483 if (is_standard_system) { 484 public_deps = [] 485 if (defined(invoker.sequenceable_pub_deps)) { 486 public_deps += invoker.sequenceable_pub_deps 487 } 488 external_deps = [ "c_utils:utils" ] 489 if (defined(invoker.hitrace)) { 490 external_deps += [ "hitrace:hitrace_meter" ] 491 } 492 if (defined(invoker.log_domainid)) { 493 external_deps += [ "hilog:libhilog" ] 494 } 495 if (defined(invoker.sequenceable_ext_deps)) { 496 external_deps += invoker.sequenceable_ext_deps 497 } 498 if (language == "c") { 499 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 500 } else if (language == "cpp") { 501 external_deps += [ "ipc:ipc_single" ] 502 } 503 } else { 504 external_deps = [ "hilog:libhilog" ] 505 } 506 if (defined(invoker.subsystem_name)) { 507 subsystem_name = invoker.subsystem_name 508 } 509 if (defined(invoker.part_name)) { 510 part_name = invoker.part_name 511 } 512 if (defined(invoker.sanitize)) { 513 sanitize = invoker.sanitize 514 } else { 515 sanitize = { 516 cfi = true 517 cfi_cross_dso = true 518 debug = false 519 } 520 } 521 if (defined(invoker.cflags)) { 522 cflags = invoker.cflags 523 } 524 if (defined(invoker.cflags_cc)) { 525 cflags_cc = invoker.cflags_cc 526 } 527 if (defined(invoker.remove_configs)) { 528 remove_configs = invoker.remove_configs 529 } 530 } 531 532 # generate code and shared library 533 group("$target_name" + "_idl_target") { 534 deps = [ 535 ":$lib_client", 536 ":$lib_server", 537 ] 538 } 539 } 540} 541