1# Copyright (c) 2022 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/rust/rustc_toolchain.gni") 15import("//build/templates/cxx/cxx.gni") 16import("//build/templates/rust/ohos_rust_library.gni") 17 18allowAllLints = [ 19 "--cap-lints", 20 "allow", 21] 22rustcOhosLints = [ 23 "-A", 24 "deprecated", 25 "-D", 26 "missing-docs", 27 "-D", 28 "warnings", 29] 30rustcVendorLints = [ 31 "-A", 32 "deprecated", 33 "-D", 34 "warnings", 35] 36rustcAllWarningLints = [ 37 "-W", 38 "non_ascii_idents", 39 "-W", 40 "ambiguous_associated_items", 41 "-W", 42 "arithmetic_overflow", 43 "-W", 44 "bindings_with_variant_name", 45 "-W", 46 "cenum_impl_drop_cast", 47 "-W", 48 "conflicting_repr_hints", 49 "-W", 50 "deprecated_cfg_attr_crate_type_name", 51 "-W", 52 "enum_intrinsics_non_enums", 53 "-W", 54 "ill_formed_attribute_input", 55 "-W", 56 "implied_bounds_entailment", 57 "-W", 58 "incomplete_include", 59 "-W", 60 "ineffective_unstable_trait_impl", 61 "-W", 62 "invalid_atomic_ordering", 63 "-W", 64 "invalid_type_param_default", 65 "-W", 66 "let_underscore_lock", 67 "-W", 68 "macro_expanded_macro_exports_accessed_by_absolute_paths", 69 "-W", 70 "missing_fragment_specifier", 71 "-W", 72 "mutable_transmutes", 73 "-W", 74 "named_asm_labels", 75 "-W", 76 "no_mangle_const_items", 77 "-W", 78 "order_dependent_trait_objects", 79 "-W", 80 "overflowing_literals", 81 "-W", 82 "patterns_in_fns_without_body", 83 "-W", 84 "proc_macro_back_compat", 85 "-W", 86 "proc_macro_derive_resolution_fallback", 87 "-W", 88 "pub_use_of_private_extern_crate", 89 "-W", 90 "soft_unstable", 91 "-W", 92 "test_unstable_lint", 93 "-W", 94 "text_direction_codepoint_in_comment", 95 "-W", 96 "text_direction_codepoint_in_literal", 97 "-W", 98 "unconditional_panic", 99 "-W", 100 "unknown_crate_types", 101 "-W", 102 "useless_deprecated", 103] 104clippyOhosLints = [ 105 "-A", 106 "clippy::type-complexity", 107 "-A", 108 "clippy::unnecessary-wraps", 109 "-A", 110 "clippy::unusual-byte-groupings", 111 "-A", 112 "clippy::upper-case-acronyms", 113 "-A", 114 "clippy::let_and_return", 115 "-A", 116 "clippy::unnecessary-cast", 117] 118clippyVendorLints = [ 119 "-A", 120 "clippy::complexity", 121 "-A", 122 "clippy::perf", 123 "-A", 124 "clippy::style", 125] 126clippyAllWarningLints = [ 127 "-W", 128 "clippy::all", 129 "-W", 130 "clippy::pedantic", 131 "-W", 132 "clippy::restriction", 133] 134 135template("rust_target") { 136 assert(!defined(invoker.crate_root) || 137 !(defined(invoker.generate_crate_root) && invoker.generate_crate_root)) 138 139 _crate_name = target_name 140 if (defined(invoker.crate_name)) { 141 _crate_name = invoker.crate_name 142 } 143 _crate_type = "" 144 if (defined(invoker.crate_type)) { 145 _crate_type = invoker.crate_type 146 } 147 _deps = [] 148 if (defined(invoker.deps)) { 149 _deps += invoker.deps 150 } 151 152 _rustflags = [ "-Zstack-protector=all" ] 153 154 if (defined(invoker.rustflags)) { 155 _rustflags += invoker.rustflags 156 } 157 158 _public_deps = [] 159 if (defined(invoker.public_deps)) { 160 _public_deps += invoker.public_deps 161 } 162 163 if (defined(invoker.output_dir) && invoker.output_dir != "") { 164 _out_dir = invoker.output_dir 165 } else { 166 _out_dir = target_out_dir 167 } 168 169 if (defined(invoker.features)) { 170 foreach(i, invoker.features) { 171 _rustflags += [ "--cfg=feature=\"${i}\"" ] 172 } 173 } 174 _rustenv = [ "OUT_DIR=" + rebase_path(_out_dir) ] 175 if (defined(invoker.rustenv)) { 176 _rustenv += invoker.rustenv 177 } 178 179 assert(defined(invoker.sources), "sources must be listed") 180 181 _rust_deps = _deps 182 _rust_public_deps = _public_deps 183 184 _edition = rust_default_edition 185 if (defined(invoker.edition)) { 186 _edition = invoker.edition 187 } 188 _rustflags += [ string_join("", 189 [ 190 "--edition=", 191 _edition, 192 ]) ] 193 if (invoker.target_type == "rust_proc_macro") { 194 _rustflags += [ 195 "--extern", 196 "proc_macro", 197 ] 198 } 199 200 if (is_asan) { 201 if (use_hwasan) { 202 _rustflags += [ 203 "-Clink-arg=-fsanitize=hwaddress", 204 "-Clink-arg=-shared-libasan", 205 ] 206 } else { 207 _rustflags += [ 208 "-Clink-arg=-fsanitize=address", 209 "-Clink-arg=-shared-libasan", 210 ] 211 } 212 } 213 214 if (is_tsan && !is_mingw && !is_win) { 215 _rustflags += [ 216 "-Clink-arg=-fsanitize=thread", 217 "-Clink-arg=-shared-libasan", 218 ] 219 } 220 221 target(invoker.target_type, "${target_name}") { 222 forward_variables_from(invoker, 223 "*", 224 [ 225 "features", 226 "deps", 227 "public_deps", 228 "rustflags", 229 "rustenv", 230 231 # "configs", 232 "output_dir", 233 "crate_type", 234 ]) 235 crate_name = _crate_name 236 237 deps = _rust_deps 238 public_deps = _rust_public_deps 239 rustflags = _rustflags 240 rustenv = _rustenv 241 crate_type = _crate_type 242 if (target_type == "rust_proc_macro") { 243 output_dir = _out_dir 244 } 245 if (!defined(output_name) || output_name == "") { 246 output_name = crate_name 247 } 248 if (use_clang_coverage == true) { 249 if (!defined(ldflags)) { 250 ldflags = [] 251 } 252 ldflags += [ "-Wl,--undefined-version" ] 253 rustflags += [ 254 "-Cinstrument-coverage", 255 "-Clink-dead-code", 256 "-Zprofile", 257 "-Ccodegen-units=1", 258 ] 259 } 260 } 261} 262 263template("ohos_rust_executable") { 264 _target_name = target_name 265 _rustflags = [] 266 rust_target("$_target_name") { 267 target_type = "ohos_executable" 268 forward_variables_from(invoker, "*") 269 if (!defined(invoker.crate_name)) { 270 crate_name = _target_name 271 } 272 crate_type = "bin" 273 if (defined(invoker.crate_type)) { 274 assert(invoker.crate_type == crate_type, 275 "crate_type should be $crate_type or use default value.") 276 } 277 configs = [] 278 if (!defined(deps)) { 279 deps = [] 280 } 281 if (defined(invoker.rustc_lints)) { 282 rustc_lints = invoker.rustc_lints 283 } 284 if (defined(invoker.clippy_lints)) { 285 clippy_lints = invoker.clippy_lints 286 } 287 288 if (defined(rustc_codecheck) && rustc_codecheck) { 289 rustc_lints = "allWarning" 290 clippy_lints = "allWarning" 291 } 292 293 if (!defined(rustc_lints) && !defined(clippy_lints)) { 294 file_path = 295 get_path_info(get_path_info(invoker.sources, "dir"), "abspath") 296 file_path_split = string_split(file_path[0], "/") 297 source_dir_begin = file_path_split[2] 298 299 if (defined(invoker.install_images)) { 300 install_images = [] 301 install_images = invoker.install_images 302 } 303 304 if (source_dir_begin == "third_party") { 305 _rustflags += allowAllLints 306 } else if (source_dir_begin == "prebuilts") { 307 _rustflags += allowAllLints 308 } else if (source_dir_begin == "vendor" && 309 file_path_split[3] == "open_source") { 310 _rustflags += allowAllLints 311 } else if (source_dir_begin == "vendor") { 312 _rustflags += rustcVendorLints 313 _rustflags += clippyVendorLints 314 } else if (source_dir_begin == "device") { 315 _rustflags += rustcVendorLints 316 _rustflags += clippyVendorLints 317 } else { 318 _rustflags += rustcOhosLints 319 _rustflags += clippyOhosLints 320 } 321 } 322 323 if (defined(rustc_lints)) { 324 if (rustc_lints == "openharmony") { 325 _rustflags += rustcOhosLints 326 } else if (rustc_lints == "vendor") { 327 _rustflags += rustcVendorLints 328 } else if (rustc_lints == "none") { 329 _rustflags += allowAllLints 330 } else if (rustc_lints == "allWarning") { 331 _rustflags += rustcAllWarningLints 332 } 333 } 334 if (defined(clippy_lints)) { 335 if (clippy_lints == "openharmony") { 336 _rustflags += clippyOhosLints 337 } else if (clippy_lints == "vendor") { 338 _rustflags += clippyVendorLints 339 } else if (clippy_lints == "none") { 340 _rustflags += allowAllLints 341 } else if (clippy_lints == "allWarning") { 342 _rustflags += clippyAllWarningLints 343 } 344 } 345 if (!defined(rustflags)) { 346 rustflags = _rustflags 347 } else { 348 rustflags += _rustflags 349 } 350 if (!defined(rust_static_link) || !rust_static_link) { 351 rustflags += [ "-Cprefer-dynamic" ] 352 } 353 } 354} 355 356template("ohos_rust_shared_library") { 357 _target_name = target_name 358 _rustflags = [] 359 rust_target("$_target_name") { 360 target_type = "ohos_rust_library" 361 forward_variables_from(invoker, "*") 362 if (!defined(invoker.crate_name)) { 363 crate_name = _target_name 364 } 365 crate_type = "dylib" 366 if (defined(invoker.crate_type)) { 367 assert(invoker.crate_type == crate_type, 368 "crate_type should be $crate_type or use default value.") 369 } 370 371 if (defined(invoker.output_extension)) { 372 module_output_extension = "." + invoker.output_extension 373 } else { 374 module_output_extension = dylib_extension 375 } 376 377 if (defined(invoker.rustc_lints)) { 378 rustc_lints = invoker.rustc_lints 379 } 380 if (defined(invoker.clippy_lints)) { 381 clippy_lints = invoker.clippy_lints 382 } 383 384 if (defined(rustc_codecheck) && rustc_codecheck) { 385 rustc_lints = "allWarning" 386 clippy_lints = "allWarning" 387 } 388 389 if (!defined(rustc_lints) && !defined(clippy_lints)) { 390 file_path = 391 get_path_info(get_path_info(invoker.sources, "dir"), "abspath") 392 file_path_split = string_split(file_path[0], "/") 393 source_dir_begin = file_path_split[2] 394 395 if (source_dir_begin == "third_party") { 396 _rustflags += allowAllLints 397 } else if (source_dir_begin == "prebuilts") { 398 _rustflags += allowAllLints 399 } else if (source_dir_begin == "vendor" && 400 file_path_split[3] == "open_source") { 401 _rustflags += allowAllLints 402 } else if (source_dir_begin == "vendor") { 403 _rustflags += rustcVendorLints 404 _rustflags += clippyVendorLints 405 } else if (source_dir_begin == "device") { 406 _rustflags += rustcVendorLints 407 _rustflags += clippyVendorLints 408 } else { 409 _rustflags += rustcOhosLints 410 _rustflags += clippyOhosLints 411 } 412 } 413 414 if (defined(invoker.install_images)) { 415 install_images = [] 416 install_images = invoker.install_images 417 } 418 419 if (defined(rustc_lints)) { 420 if (rustc_lints == "openharmony") { 421 _rustflags += rustcOhosLints 422 } else if (rustc_lints == "vendor") { 423 _rustflags += rustcVendorLints 424 } else if (rustc_lints == "none") { 425 _rustflags += allowAllLints 426 } else if (rustc_lints == "allWarning") { 427 _rustflags += rustcAllWarningLints 428 } 429 } 430 if (defined(clippy_lints)) { 431 if (clippy_lints == "openharmony") { 432 _rustflags += clippyOhosLints 433 } else if (clippy_lints == "vendor") { 434 _rustflags += clippyVendorLints 435 } else if (clippy_lints == "none") { 436 _rustflags += allowAllLints 437 } else if (clippy_lints == "allWarning") { 438 _rustflags += clippyAllWarningLints 439 } 440 } 441 if (!defined(rustflags)) { 442 rustflags = _rustflags 443 } else { 444 rustflags += _rustflags 445 } 446 } 447} 448 449template("ohos_rust_static_library") { 450 _target_name = target_name 451 _rustflags = [] 452 rust_target("$_target_name") { 453 target_type = "ohos_rust_library" 454 forward_variables_from(invoker, "*") 455 if (!defined(invoker.crate_name)) { 456 crate_name = _target_name 457 } 458 crate_type = "rlib" 459 if (defined(invoker.crate_type)) { 460 assert(invoker.crate_type == crate_type, 461 "crate_type should be $crate_type or use default value.") 462 } 463 module_output_extension = rlib_extension 464 install_enable = false 465 466 if (defined(invoker.rustc_lints)) { 467 rustc_lints = invoker.rustc_lints 468 } 469 if (defined(invoker.clippy_lints)) { 470 clippy_lints = invoker.clippy_lints 471 } 472 473 if (defined(rustc_codecheck) && rustc_codecheck) { 474 rustc_lints = "allWarning" 475 clippy_lints = "allWarning" 476 } 477 478 if (!defined(rustc_lints) && !defined(clippy_lints)) { 479 file_path = 480 get_path_info(get_path_info(invoker.sources, "dir"), "abspath") 481 file_path_split = string_split(file_path[0], "/") 482 source_dir_begin = file_path_split[2] 483 484 if (source_dir_begin == "third_party") { 485 _rustflags += allowAllLints 486 } else if (source_dir_begin == "prebuilts") { 487 _rustflags += allowAllLints 488 } else if (source_dir_begin == "vendor" && 489 file_path_split[3] == "open_source") { 490 _rustflags += allowAllLints 491 } else if (source_dir_begin == "vendor") { 492 _rustflags += rustcVendorLints 493 _rustflags += clippyVendorLints 494 } else if (source_dir_begin == "device") { 495 _rustflags += rustcVendorLints 496 _rustflags += clippyVendorLints 497 } else { 498 _rustflags += rustcOhosLints 499 _rustflags += clippyOhosLints 500 } 501 } 502 503 if (defined(invoker.install_images)) { 504 install_images = [] 505 install_images = invoker.install_images 506 } 507 508 if (defined(rustc_lints)) { 509 if (rustc_lints == "openharmony") { 510 _rustflags += rustcOhosLints 511 } else if (rustc_lints == "vendor") { 512 _rustflags += rustcVendorLints 513 } else if (rustc_lints == "none") { 514 _rustflags += allowAllLints 515 } else if (rustc_lints == "allWarning") { 516 _rustflags += rustcAllWarningLints 517 } 518 } 519 if (defined(clippy_lints)) { 520 if (clippy_lints == "openharmony") { 521 _rustflags += clippyOhosLints 522 } else if (clippy_lints == "vendor") { 523 _rustflags += clippyVendorLints 524 } else if (clippy_lints == "none") { 525 _rustflags += allowAllLints 526 } else if (clippy_lints == "allWarning") { 527 _rustflags += clippyAllWarningLints 528 } 529 } 530 if (!defined(rustflags)) { 531 rustflags = _rustflags 532 } else { 533 rustflags += _rustflags 534 } 535 } 536} 537 538template("ohos_rust_shared_ffi") { 539 _target_name = target_name 540 _rustflags = [] 541 rust_target("$_target_name") { 542 target_type = "ohos_shared_library" 543 forward_variables_from(invoker, "*") 544 if (!defined(invoker.crate_name)) { 545 crate_name = _target_name 546 } 547 crate_type = "cdylib" 548 if (defined(invoker.crate_type)) { 549 assert(invoker.crate_type == crate_type, 550 "crate_type should be $crate_type or use default value.") 551 } 552 553 if (!defined(deps)) { 554 deps = [] 555 } 556 557 if (defined(invoker.rustc_lints)) { 558 rustc_lints = invoker.rustc_lints 559 } 560 if (defined(invoker.clippy_lints)) { 561 clippy_lints = invoker.clippy_lints 562 } 563 564 if (defined(rustc_codecheck) && rustc_codecheck) { 565 rustc_lints = "allWarning" 566 clippy_lints = "allWarning" 567 } 568 569 if (!defined(rustc_lints) && !defined(clippy_lints)) { 570 file_path = 571 get_path_info(get_path_info(invoker.sources, "dir"), "abspath") 572 file_path_split = string_split(file_path[0], "/") 573 source_dir_begin = file_path_split[2] 574 575 if (source_dir_begin == "third_party") { 576 _rustflags += allowAllLints 577 } else if (source_dir_begin == "prebuilts") { 578 _rustflags += allowAllLints 579 } else if (source_dir_begin == "vendor" && 580 file_path_split[3] == "open_source") { 581 _rustflags += allowAllLints 582 } else if (source_dir_begin == "vendor") { 583 _rustflags += rustcVendorLints 584 _rustflags += clippyVendorLints 585 } else if (source_dir_begin == "device") { 586 _rustflags += rustcVendorLints 587 _rustflags += clippyVendorLints 588 } else { 589 _rustflags += rustcOhosLints 590 _rustflags += clippyOhosLints 591 } 592 } 593 594 if (defined(invoker.install_images)) { 595 install_images = [] 596 install_images = invoker.install_images 597 } 598 599 if (defined(rustc_lints)) { 600 if (rustc_lints == "openharmony") { 601 _rustflags += rustcOhosLints 602 } else if (rustc_lints == "vendor") { 603 _rustflags += rustcVendorLints 604 } else if (rustc_lints == "none") { 605 _rustflags += allowAllLints 606 } else if (rustc_lints == "allWarning") { 607 _rustflags += rustcAllWarningLints 608 } 609 } 610 if (defined(clippy_lints)) { 611 if (clippy_lints == "openharmony") { 612 _rustflags += clippyOhosLints 613 } else if (clippy_lints == "vendor") { 614 _rustflags += clippyVendorLints 615 } else if (clippy_lints == "none") { 616 _rustflags += allowAllLints 617 } else if (clippy_lints == "allWarning") { 618 _rustflags += clippyAllWarningLints 619 } 620 } 621 if (!defined(rustflags)) { 622 rustflags = _rustflags 623 } else { 624 rustflags += _rustflags 625 } 626 } 627} 628 629template("ohos_rust_static_ffi") { 630 _target_name = target_name 631 _rustflags = [] 632 rust_target("$_target_name") { 633 target_type = "ohos_static_library" 634 forward_variables_from(invoker, "*") 635 if (!defined(invoker.crate_name)) { 636 crate_name = _target_name 637 } 638 crate_type = "staticlib" 639 if (defined(invoker.crate_type)) { 640 assert(invoker.crate_type == crate_type, 641 "crate_type should be $crate_type or use default value.") 642 } 643 if (!defined(deps)) { 644 deps = [] 645 } 646 if (defined(invoker.rustc_lints)) { 647 rustc_lints = invoker.rustc_lints 648 } 649 if (defined(invoker.clippy_lints)) { 650 clippy_lints = invoker.clippy_lints 651 } 652 653 if (defined(rustc_codecheck) && rustc_codecheck) { 654 rustc_lints = "allWarning" 655 clippy_lints = "allWarning" 656 } 657 658 if (!defined(rustc_lints) && !defined(clippy_lints)) { 659 file_path = 660 get_path_info(get_path_info(invoker.sources, "dir"), "abspath") 661 file_path_split = string_split(file_path[0], "/") 662 source_dir_begin = file_path_split[2] 663 664 if (source_dir_begin == "third_party") { 665 _rustflags += allowAllLints 666 } else if (source_dir_begin == "prebuilts") { 667 _rustflags += allowAllLints 668 } else if (source_dir_begin == "vendor" && 669 file_path_split[3] == "open_source") { 670 _rustflags += allowAllLints 671 } else if (source_dir_begin == "vendor") { 672 _rustflags += rustcVendorLints 673 _rustflags += clippyVendorLints 674 } else if (source_dir_begin == "device") { 675 _rustflags += rustcVendorLints 676 _rustflags += clippyVendorLints 677 } else { 678 _rustflags += rustcOhosLints 679 _rustflags += clippyOhosLints 680 } 681 } 682 683 if (defined(invoker.install_images)) { 684 install_images = [] 685 install_images = invoker.install_images 686 } 687 688 if (defined(rustc_lints)) { 689 if (rustc_lints == "openharmony") { 690 _rustflags += rustcOhosLints 691 } else if (rustc_lints == "vendor") { 692 _rustflags += rustcVendorLints 693 } else if (rustc_lints == "none") { 694 _rustflags += allowAllLints 695 } else if (rustc_lints == "allWarning") { 696 _rustflags += rustcAllWarningLints 697 } 698 } 699 if (defined(clippy_lints)) { 700 if (clippy_lints == "openharmony") { 701 _rustflags += clippyOhosLints 702 } else if (clippy_lints == "vendor") { 703 _rustflags += clippyVendorLints 704 } else if (clippy_lints == "none") { 705 _rustflags += allowAllLints 706 } else if (clippy_lints == "allWarning") { 707 _rustflags += clippyAllWarningLints 708 } 709 } 710 if (!defined(rustflags)) { 711 rustflags = _rustflags 712 } else { 713 rustflags += _rustflags 714 } 715 } 716} 717 718template("ohos_rust_proc_macro") { 719 assert(!defined(invoker.output_dir), 720 "output_dir is not allowed to be defined.") 721 _test_target = defined(invoker.testonly) && invoker.testonly 722 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 723 subsystem_name = invoker.subsystem_name 724 part_name = invoker.part_name 725 } else if (defined(invoker.part_name)) { 726 part_name = invoker.part_name 727 _part_subsystem_info_file = 728 "$root_build_dir/build_configs/parts_info/part_subsystem.json" 729 _arguments = [ 730 "--part-name", 731 part_name, 732 "--part-subsystem-info-file", 733 rebase_path(_part_subsystem_info_file, root_build_dir), 734 ] 735 get_subsystem_script = "//build/templates/common/get_subsystem_name.py" 736 subsystem_name = 737 exec_script(get_subsystem_script, _arguments, "trim string") 738 if (is_use_check_deps && !_test_target) { 739 skip_check_subsystem = true 740 } 741 } else if (defined(invoker.subsystem_name)) { 742 subsystem_name = invoker.subsystem_name 743 part_name = subsystem_name 744 } else { 745 subsystem_name = "build" 746 part_name = "build_framework" 747 } 748 assert(subsystem_name != "") 749 assert(part_name != "") 750 if (is_use_check_deps && !_test_target) { 751 _check_target = "${target_name}__check" 752 target_path = get_label_info(":${target_name}", "label_no_toolchain") 753 check_target(_check_target) { 754 module_deps = [] 755 module_ex_deps = [] 756 if (defined(invoker.deps)) { 757 module_deps += invoker.deps 758 } 759 if (defined(invoker.public_deps)) { 760 module_deps += invoker.public_deps 761 } 762 if (defined(invoker.external_deps)) { 763 module_ex_deps = invoker.external_deps 764 } 765 if (defined(invoker.external_deps)) { 766 module_ex_deps += invoker.external_deps 767 } 768 if (defined(invoker.public_external_deps)) { 769 module_ex_deps += invoker.public_external_deps 770 } 771 } 772 } 773 if (check_deps) { 774 deps_data = { 775 } 776 module_label = get_label_info(":${target_name}", "label_with_toolchain") 777 module_deps = [] 778 if (defined(invoker.deps)) { 779 foreach(dep, invoker.deps) { 780 module_deps += [ get_label_info(dep, "label_no_toolchain") ] 781 } 782 } 783 module_ex_deps = [] 784 if (defined(invoker.external_deps) && invoker.external_deps != []) { 785 module_ex_deps = invoker.external_deps 786 } 787 deps_data = { 788 part_name = part_name 789 module_label = module_label 790 deps = module_deps 791 external_deps = module_ex_deps 792 } 793 write_file("${root_out_dir}/deps_files/${part_name}__${target_name}.json", 794 deps_data, 795 "json") 796 } 797 798 if (is_standard_system) { 799 output_dir = "${root_out_dir}/${subsystem_name}/${part_name}" 800 } else { 801 output_dir = "${root_out_dir}" 802 } 803 804 if (!_test_target) { 805 module_label = get_label_info(":${target_name}", "label_with_toolchain") 806 _collect_target = "${target_name}__collect" 807 collect_module_target(_collect_target) { 808 forward_variables_from(invoker, [ "install_images" ]) 809 } 810 811 _notice_target = "${target_name}__notice" 812 _main_target_name = target_name 813 collect_notice(_notice_target) { 814 forward_variables_from(invoker, 815 [ 816 "testonly", 817 "license_as_sources", 818 "license_file", 819 ]) 820 821 module_name = _main_target_name 822 module_source_dir = get_label_info(":${_main_target_name}", "dir") 823 } 824 } 825 826 target_label = get_label_info(":${target_name}", "label_with_toolchain") 827 target_toolchain = get_label_info(target_label, "toolchain") 828 829 if (target_toolchain == "${current_toolchain}") { 830 ohos_module_name = target_name 831 _module_info_target = "${target_name}_info" 832 generate_module_info(_module_info_target) { 833 module_name = ohos_module_name 834 module_type = "lib" 835 module_source_dir = "$root_out_dir" 836 if (defined(output_dir)) { 837 module_source_dir = output_dir 838 } 839 840 module_install_name = ohos_module_name 841 if (defined(invoker.output_name)) { 842 module_install_name = invoker.output_name 843 } 844 845 module_install_images = [ "system" ] 846 if (defined(invoker.install_images)) { 847 module_install_images = [] 848 module_install_images += invoker.install_images 849 } 850 851 module_output_extension = shlib_extension 852 if (defined(invoker.output_extension)) { 853 module_output_extension = "." + invoker.output_extension 854 } 855 856 install_enable = true 857 if (defined(invoker.install_enable)) { 858 install_enable = invoker.install_enable 859 } 860 861 if (defined(invoker.module_install_dir)) { 862 module_install_dir = invoker.module_install_dir 863 } 864 865 if (defined(invoker.relative_install_dir)) { 866 relative_install_dir = invoker.relative_install_dir 867 } 868 869 if (defined(invoker.symlink_target_name)) { 870 symlink_target_name = invoker.symlink_target_name 871 } 872 873 if (defined(invoker.output_prefix_override)) { 874 output_prefix_override = invoker.output_prefix_override 875 } 876 notice = "$target_out_dir/$ohos_module_name.notice.txt" 877 } 878 } 879 880 _rustflags = [] 881 rust_target(target_name) { 882 target_type = "rust_proc_macro" 883 forward_variables_from(invoker, 884 "*", 885 [ 886 "configs", 887 "remove_configs", 888 "no_default_deps", 889 "install_images", 890 "module_install_dir", 891 "relative_install_dir", 892 "symlink_target_name", 893 "output_dir", 894 "install_enable", 895 "version_script", 896 "license_file", 897 "license_as_sources", 898 "use_exceptions", 899 "stl", 900 901 # Sanitizer variables 902 "sanitize", 903 ]) 904 if (!defined(invoker.crate_name)) { 905 crate_name = _target_name 906 } 907 crate_type = "proc-macro" 908 if (defined(invoker.crate_type)) { 909 assert(invoker.crate_type == crate_type, 910 "crate_type should be $crate_type or use default value.") 911 } 912 913 output_dir = output_dir 914 915 if (!defined(inputs)) { 916 inputs = [] 917 } 918 919 if (!defined(ldflags)) { 920 ldflags = [] 921 } 922 923 if (defined(invoker.configs)) { 924 configs += invoker.configs 925 } 926 if (defined(invoker.remove_configs)) { 927 configs -= invoker.remove_configs 928 } 929 930 if (!defined(output_name)) { 931 output_name = target_name 932 } 933 934 if (defined(invoker.no_default_deps)) { 935 no_default_deps = invoker.no_default_deps 936 } 937 938 if (!defined(libs)) { 939 libs = [] 940 } 941 if (!defined(cflags_cc)) { 942 cflags_cc = [] 943 } 944 if (!defined(deps)) { 945 deps = [] 946 } 947 if (is_use_check_deps && !_test_target) { 948 deps += [ ":$_check_target" ] 949 } 950 if (target_toolchain == "${current_toolchain}" && !skip_gen_module_info) { 951 deps += [ ":$_module_info_target" ] 952 } 953 954 if (!_test_target) { 955 deps += [ 956 ":$_notice_target", 957 ":${_collect_target}", 958 ] 959 } 960 if (!defined(include_dirs)) { 961 include_dirs = [] 962 } 963 964 install_module_info = { 965 module_def = target_label 966 module_info_file = 967 rebase_path(get_label_info(module_def, "target_out_dir"), 968 root_build_dir) + "/${target_name}_module_info.json" 969 subsystem_name = subsystem_name 970 part_name = part_name 971 toolchain = current_toolchain 972 toolchain_out_dir = rebase_path(root_out_dir, root_build_dir) 973 } 974 metadata = { 975 install_modules = [ install_module_info ] 976 } 977 if (defined(is_debug) && !is_debug && enable_debug_components != "") { 978 foreach(component_name, debug_components) { 979 if (part_name == component_name) { 980 configs -= default_opt_configs 981 configs += debug_level_configs 982 } 983 } 984 } 985 986 if (defined(invoker.rustc_lints)) { 987 rustc_lints = invoker.rustc_lints 988 } 989 if (defined(invoker.clippy_lints)) { 990 clippy_lints = invoker.clippy_lints 991 } 992 993 if (defined(rustc_codecheck) && rustc_codecheck) { 994 rustc_lints = "allWarning" 995 clippy_lints = "allWarning" 996 } 997 998 if (!defined(rustc_lints) && !defined(clippy_lints)) { 999 file_path = 1000 get_path_info(get_path_info(invoker.sources, "dir"), "abspath") 1001 file_path_split = string_split(file_path[0], "/") 1002 source_dir_begin = file_path_split[2] 1003 1004 if (source_dir_begin == "third_party") { 1005 _rustflags += allowAllLints 1006 } else if (source_dir_begin == "prebuilts") { 1007 _rustflags += allowAllLints 1008 } else if (source_dir_begin == "vendor" && 1009 file_path_split[3] == "open_source") { 1010 _rustflags += allowAllLints 1011 } else if (source_dir_begin == "vendor") { 1012 _rustflags += rustcVendorLints 1013 _rustflags += clippyVendorLints 1014 } else if (source_dir_begin == "device") { 1015 _rustflags += rustcVendorLints 1016 _rustflags += clippyVendorLints 1017 } else { 1018 _rustflags += rustcOhosLints 1019 _rustflags += clippyOhosLints 1020 } 1021 } 1022 1023 if (defined(rustc_lints)) { 1024 if (rustc_lints == "openharmony") { 1025 _rustflags += rustcOhosLints 1026 } else if (rustc_lints == "vendor") { 1027 _rustflags += rustcVendorLints 1028 } else if (rustc_lints == "none") { 1029 _rustflags += allowAllLints 1030 } else if (rustc_lints == "allWarning") { 1031 _rustflags += rustcAllWarningLints 1032 } 1033 } 1034 if (defined(clippy_lints)) { 1035 if (clippy_lints == "openharmony") { 1036 _rustflags += clippyOhosLints 1037 } else if (clippy_lints == "vendor") { 1038 _rustflags += clippyVendorLints 1039 } else if (clippy_lints == "none") { 1040 _rustflags += allowAllLints 1041 } else if (clippy_lints == "allWarning") { 1042 _rustflags += clippyAllWarningLints 1043 } 1044 } 1045 if (!defined(rustflags)) { 1046 rustflags = _rustflags 1047 } else { 1048 rustflags += _rustflags 1049 } 1050 } 1051} 1052