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 sources_all += [ 122 "${target_gen_dir}/" + name + "_proxy.cpp", 123 "${target_gen_dir}/" + name + "_stub.cpp", 124 ] 125 sources_proxy += [ "${target_gen_dir}/" + name + "_proxy.cpp" ] 126 sources_stub += [ "${target_gen_dir}/" + name + "_stub.cpp" ] 127 if (defined(invoker.client_enable) && invoker.client_enable) { 128 sources_all += [ "${target_gen_dir}/" + name + "_client.cpp" ] 129 sources_proxy += [ "${target_gen_dir}/" + name + "_client.cpp" ] 130 } 131 } 132 133 # callback sources 134 foreach(idl_full_name, idl_callback_list) { 135 idl_name = get_path_info(idl_full_name, "file") 136 i = 0 137 name = string_replace(idl_name, ".idl", "") 138 foreach(s, str_upper_list) { 139 name = string_replace(name, s, "_" + str_lower_list[i]) 140 i = i + 1 141 } 142 143 # first letter 'i' 144 name_split = [] 145 name_split = string_split(name, "_i_") 146 if (name_split[0] == "") { 147 name = string_replace(name, "_i_", "", 1) 148 } 149 name_split = [] 150 name_split = string_split(name, "_") 151 if (name_split[0] == "") { 152 name = string_replace(name, "_", "", 1) 153 } 154 out_full_name = string_replace(idl_full_name, idl_name, name) 155 156 sources_all += [ 157 "${target_gen_dir}/" + out_full_name + "_proxy.cpp", 158 "${target_gen_dir}/" + out_full_name + "_stub.cpp", 159 ] 160 sources_proxy += [ "${target_gen_dir}/" + out_full_name + "_stub.cpp" ] 161 sources_stub += [ "${target_gen_dir}/" + out_full_name + "_proxy.cpp" ] 162 } 163 164 # common sources 165 foreach(idl_full_name, idl_common_list) { 166 idl_name = get_path_info(idl_full_name, "file") 167 i = 0 168 name = string_replace(idl_name, ".idl", "") 169 foreach(s, str_upper_list) { 170 name = string_replace(name, s, "_" + str_lower_list[i]) 171 i = i + 1 172 } 173 174 # first letter 'i' 175 name_split = [] 176 name_split = string_split(name, "_i_") 177 if (name_split[0] == "") { 178 name = string_replace(name, "_i_", "i", 1) 179 } 180 name_split = [] 181 name_split = string_split(name, "_") 182 if (name_split[0] == "") { 183 name = string_replace(name, "_", "", 1) 184 } 185 out_full_name = string_replace(idl_full_name, idl_name, name) 186 187 sources_all += [ "${target_gen_dir}/" + out_full_name + ".cpp" ] 188 sources_proxy += [ "${target_gen_dir}/" + out_full_name + ".cpp" ] 189 sources_stub += [ "${target_gen_dir}/" + out_full_name + ".cpp" ] 190 } 191 192 action("$target_name") { 193 inputs = idl_list 194 deps = idl_build_deps 195 script = "//build/config/components/idl_tool/idl.py" 196 args = [ 197 "--src-idl", 198 string_join(",", idl_list), 199 "--dst-path", 200 rebase_path(target_gen_dir), 201 "--idl-tool-path", 202 rebase_path(idl_build_path), 203 "--dst-file", 204 string_join(",", rebase_path(sources_all)), 205 "--language", 206 language, 207 ] 208 if (defined(invoker.log_domainid)) { 209 args += [ 210 "--log-domainid", 211 invoker.log_domainid, 212 ] 213 } 214 if (defined(invoker.log_tag)) { 215 args += [ 216 "--log-tag", 217 invoker.log_tag, 218 ] 219 } 220 if (defined(invoker.hitrace)) { 221 args += [ 222 "--hitrace", 223 invoker.hitrace, 224 ] 225 } 226 if (defined(invoker.client_enable) && invoker.client_enable) { 227 args += [ 228 "--client-enable", 229 "true", 230 ] 231 } 232 outputs = sources_all 233 } 234 235 idl_headers_config = target_name + "_idl_headers_config" 236 config("$idl_headers_config") { 237 include_dirs = [ target_gen_dir ] 238 if (defined(invoker.sub_include)) { 239 include_dirs += invoker.sub_include 240 } 241 } 242 243 if (invoker.target_type == "source_set" && defined(invoker.sources)) { 244 source_set_client = target_name + "_source_set_proxy" 245 source_set_server = target_name + "_source_set_stub" 246 action_target_name = ":" + target_name 247 248 # build client source_set 249 ohos_source_set(source_set_client) { 250 sources = [] 251 if (defined(invoker.sources_cpp)) { 252 sources += invoker.sources_cpp 253 } 254 sources += sources_proxy 255 if (defined(invoker.configs)) { 256 configs = invoker.configs 257 } 258 public_configs = [ ":$idl_headers_config" ] 259 deps = [ action_target_name ] 260 if (is_standard_system) { 261 external_deps = [ "c_utils:utils" ] 262 if (defined(invoker.hitrace)) { 263 external_deps += [ "hitrace:hitrace_meter" ] 264 } 265 if (defined(invoker.log_domainid)) { 266 external_deps += [ "hilog:libhilog" ] 267 } 268 if (defined(invoker.sequenceable_ext_deps)) { 269 external_deps += invoker.sequenceable_ext_deps 270 } 271 if (language == "c") { 272 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 273 } else if (language == "cpp") { 274 external_deps += [ "ipc:ipc_single" ] 275 } 276 } else { 277 external_deps = [ "hilog:libhilog" ] 278 } 279 if (defined(invoker.subsystem_name)) { 280 subsystem_name = invoker.subsystem_name 281 } 282 if (defined(invoker.part_name)) { 283 part_name = invoker.part_name 284 } 285 if (defined(invoker.innerapi_tags)) { 286 innerapi_tags = invoker.innerapi_tags 287 } 288 if (defined(invoker.sanitize)) { 289 sanitize = invoker.sanitize 290 } else { 291 sanitize = { 292 cfi = true 293 cfi_cross_dso = true 294 debug = false 295 } 296 } 297 if (defined(invoker.cflags)) { 298 cflags = invoker.cflags 299 } 300 if (defined(invoker.cflags_cc)) { 301 cflags_cc = invoker.cflags_cc 302 } 303 if (defined(invoker.remove_configs)) { 304 remove_configs = invoker.remove_configs 305 } 306 } 307 308 # build server source_set 309 ohos_source_set(source_set_server) { 310 sources = [] 311 if (defined(invoker.sources_cpp)) { 312 sources += invoker.sources_cpp 313 } 314 sources += sources_stub 315 if (defined(invoker.configs)) { 316 configs = invoker.configs 317 } 318 public_configs = [ ":$idl_headers_config" ] 319 deps = [ action_target_name ] 320 if (is_standard_system) { 321 external_deps = [ "c_utils:utils" ] 322 if (defined(invoker.hitrace)) { 323 external_deps += [ "hitrace:hitrace_meter" ] 324 } 325 if (defined(invoker.log_domainid)) { 326 external_deps += [ "hilog:libhilog" ] 327 } 328 if (defined(invoker.sequenceable_ext_deps)) { 329 external_deps += invoker.sequenceable_ext_deps 330 } 331 if (language == "c") { 332 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 333 } else if (language == "cpp") { 334 external_deps += [ "ipc:ipc_single" ] 335 } 336 } else { 337 external_deps = [ "hilog:libhilog" ] 338 } 339 if (defined(invoker.subsystem_name)) { 340 subsystem_name = invoker.subsystem_name 341 } 342 if (defined(invoker.part_name)) { 343 part_name = invoker.part_name 344 } 345 if (defined(invoker.sanitize)) { 346 sanitize = invoker.sanitize 347 } else { 348 sanitize = { 349 cfi = true 350 cfi_cross_dso = true 351 debug = false 352 } 353 } 354 if (defined(invoker.cflags)) { 355 cflags = invoker.cflags 356 } 357 if (defined(invoker.cflags_cc)) { 358 cflags_cc = invoker.cflags_cc 359 } 360 if (defined(invoker.remove_configs)) { 361 remove_configs = invoker.remove_configs 362 } 363 } 364 } 365 366 # build so 367 if ((language == "c" || language == "cpp" || 368 invoker.target_type == "shared_library") && defined(invoker.sources)) { 369 lib_client = "lib" + target_name + "_proxy" 370 lib_server = "lib" + target_name + "_stub" 371 action_target_name = ":" + target_name 372 373 # build client so 374 ohos_shared_library(lib_client) { 375 sources = [] 376 if (defined(invoker.sources_cpp)) { 377 sources += invoker.sources_cpp 378 } 379 sources += sources_proxy 380 if (defined(invoker.configs)) { 381 configs = invoker.configs 382 } 383 public_configs = [ ":$idl_headers_config" ] 384 deps = [ action_target_name ] 385 if (is_standard_system) { 386 public_deps = [] 387 if (defined(invoker.sequenceable_pub_deps)) { 388 public_deps += invoker.sequenceable_pub_deps 389 } 390 external_deps = [ "c_utils:utils" ] 391 if (defined(invoker.hitrace)) { 392 external_deps += [ "hitrace:hitrace_meter" ] 393 } 394 if (defined(invoker.log_domainid)) { 395 external_deps += [ "hilog:libhilog" ] 396 } 397 if (defined(invoker.sequenceable_ext_deps)) { 398 external_deps += invoker.sequenceable_ext_deps 399 } 400 if (language == "c") { 401 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 402 } else if (language == "cpp") { 403 external_deps += [ "ipc:ipc_single" ] 404 } 405 } else { 406 external_deps = [ "hilog:libhilog" ] 407 } 408 if (defined(invoker.subsystem_name)) { 409 subsystem_name = invoker.subsystem_name 410 } 411 if (defined(invoker.part_name)) { 412 part_name = invoker.part_name 413 } 414 if (defined(invoker.innerapi_tags)) { 415 innerapi_tags = invoker.innerapi_tags 416 } 417 if (defined(invoker.sanitize)) { 418 sanitize = invoker.sanitize 419 } else { 420 sanitize = { 421 cfi = true 422 cfi_cross_dso = true 423 debug = false 424 } 425 } 426 if (defined(invoker.cflags)) { 427 cflags = invoker.cflags 428 } 429 if (defined(invoker.cflags_cc)) { 430 cflags_cc = invoker.cflags_cc 431 } 432 if (defined(invoker.remove_configs)) { 433 remove_configs = invoker.remove_configs 434 } 435 } 436 437 # build server so 438 ohos_shared_library(lib_server) { 439 sources = [] 440 if (defined(invoker.sources_cpp)) { 441 sources += invoker.sources_cpp 442 } 443 sources += sources_stub 444 if (defined(invoker.configs)) { 445 configs = invoker.configs 446 } 447 public_configs = [ ":$idl_headers_config" ] 448 deps = [ action_target_name ] 449 if (is_standard_system) { 450 public_deps = [] 451 if (defined(invoker.sequenceable_pub_deps)) { 452 public_deps += invoker.sequenceable_pub_deps 453 } 454 external_deps = [ "c_utils:utils" ] 455 if (defined(invoker.hitrace)) { 456 external_deps += [ "hitrace:hitrace_meter" ] 457 } 458 if (defined(invoker.log_domainid)) { 459 external_deps += [ "hilog:libhilog" ] 460 } 461 if (defined(invoker.sequenceable_ext_deps)) { 462 external_deps += invoker.sequenceable_ext_deps 463 } 464 if (language == "c") { 465 external_deps += [ "hdf_core:libhdf_ipc_adapter" ] 466 } else if (language == "cpp") { 467 external_deps += [ "ipc:ipc_single" ] 468 } 469 } else { 470 external_deps = [ "hilog:libhilog" ] 471 } 472 if (defined(invoker.subsystem_name)) { 473 subsystem_name = invoker.subsystem_name 474 } 475 if (defined(invoker.part_name)) { 476 part_name = invoker.part_name 477 } 478 if (defined(invoker.sanitize)) { 479 sanitize = invoker.sanitize 480 } else { 481 sanitize = { 482 cfi = true 483 cfi_cross_dso = true 484 debug = false 485 } 486 } 487 if (defined(invoker.cflags)) { 488 cflags = invoker.cflags 489 } 490 if (defined(invoker.cflags_cc)) { 491 cflags_cc = invoker.cflags_cc 492 } 493 if (defined(invoker.remove_configs)) { 494 remove_configs = invoker.remove_configs 495 } 496 } 497 498 # generate code and shared library 499 group("$target_name" + "_idl_target") { 500 deps = [ 501 ":$lib_client", 502 ":$lib_server", 503 ] 504 } 505 } 506} 507