1# Copyright (C) 2021 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("./hiperf.gni") 16 17declare_args() { 18 hiperf_feature_mingw_uapi_dir = 19 "../../kernel/linux/patches/linux-5.10/prebuilts/usr/include" 20} 21 22function_disable_define = [] 23 24config("hiperf_inner_config") { 25 visibility = [ 26 ":*", 27 "./test/:*", 28 ] 29 ldflags = [] 30 cflags = code_check_flag 31 defines = function_disable_define 32 33 if (hiperf_code_analyze && is_ohos) { 34 cflags += code_analyze_flag 35 cflags -= [ "-Werror" ] 36 } 37 38 if (build_variant == "user") { 39 cflags += [ "-DIS_RELEASE_VERSION" ] 40 } 41 42 if (is_mingw) { 43 # lld: error: unable to find library -latomic 44 # lld: error: unable to find library -ldl 45 # lld: error: unable to find library -lrt 46 ldflags += [ 47 "-Wl,--whole-archive", 48 "-lpthread", 49 "-Wl,--no-whole-archive", 50 ] 51 } else if (is_linux) { 52 ldflags += [ 53 "-Wl,--whole-archive", 54 "-lpthread", 55 "-latomic", 56 "-ldl", 57 "-lrt", 58 "-Wl,--no-whole-archive", 59 ] 60 } 61 62 include_dirs = [ "${hiperf_path}/include" ] 63 64 # debug link 65 # ldflags += [ "-v"] 66 67 if (hiperf_debug) { 68 defines += [ 69 "HIPERF_DEBUG", 70 "HIPERF_DEBUG_PRINTF", # if u want to see printf in the log ? 71 ] 72 } 73 74 if (hiperf_check_time) { 75 defines += [ "HIPERF_DEBUG_TIME" ] 76 } 77 78 cflags += [ 79 "-std=c++17", 80 "-fvisibility=hidden", 81 "-fdata-sections", 82 "-ffunction-sections", 83 "-Os", 84 ] 85 cflags_cc = [ 86 "-fvisibility-inlines-hidden", 87 "-Os", 88 ] 89 defines += [ "is_mingw=${is_mingw}" ] 90 defines += [ "is_linux=${is_linux}" ] 91 defines += [ "is_ohos=${is_ohos}" ] 92 defines += [ "is_emulator=${is_emulator}" ] 93 defines += [ "is_double_framework=${is_double_framework}" ] 94 if (hiperf_target_host) { 95 defines += [ "target_cpu_${host_cpu}" ] 96 } else { 97 defines += [ "target_cpu_${target_cpu}" ] 98 } 99 100 if (is_mingw) { 101 cflags += [ "-includeMingW64Fix.h" ] 102 defines += [ "WIN32_LEAN_AND_MEAN" ] 103 defines += [ "__LITTLE_ENDIAN_BITFIELD" ] 104 105 include_dirs += [ 106 "${hiperf_path}/include/nonlinux/", 107 hiperf_feature_mingw_uapi_dir, 108 ] 109 } 110 111 if (hiperf_test_coverage && is_ohos) { 112 cflags += [ 113 "-fprofile-arcs", 114 "-ftest-coverage", 115 ] 116 ldflags += [ "--coverage" ] 117 } 118} 119 120sources_platform_common = [ 121 "./src/command.cpp", 122 "./src/command_reporter.cpp", 123 "./src/dwarf_encoding.cpp", 124 "./src/ipc_utilities.cpp", 125 "./src/option.cpp", 126 "./src/perf_event_record.cpp", 127 "./src/perf_file_format.cpp", 128 "./src/perf_file_reader.cpp", 129 "./src/register.cpp", 130 "./src/report.cpp", 131 "./src/report_json_file.cpp", 132 "./src/subcommand.cpp", 133 "./src/subcommand_dump.cpp", 134 "./src/subcommand_help.cpp", 135 "./src/subcommand_report.cpp", 136 "./src/symbols_file.cpp", 137 "./src/unique_stack_table.cpp", 138 "./src/utilities.cpp", 139 "./src/virtual_runtime.cpp", 140 "./src/virtual_thread.cpp", 141] 142 143if (is_ohos) { 144 sources_platform_common += [ "./src/callstack.cpp" ] 145} 146 147if (hiperf_debug) { 148 sources_platform_common += [ 149 "./src/debug_logger.cpp", 150 "./src/option_debug.cpp", 151 ] 152} 153 154sources_platform_linux = [ 155 "./src/perf_events.cpp", 156 "./src/tracked_command.cpp", 157 "./src/ring_buffer.cpp", 158 "./src/perf_file_writer.cpp", 159 "./src/subcommand_stat.cpp", 160 "./src/subcommand_record.cpp", 161 "./src/subcommand_list.cpp", 162 "./src/spe_decoder.cpp", 163] 164 165common_deps = [ 166 ":support_elf", 167 ":support_protobuf", 168] 169 170common_configs = [ ":hiperf_inner_config" ] 171 172if (!hiperf_independent_compilation) { 173 common_configs += [ "../../commonlibrary/c_utils/base:utils_config" ] 174} 175 176if (hiperf_target_static) { 177 common_deps -= [ ":support_protobuf" ] 178} 179 180config("hiperf_syspara_config") { 181 defines = [ "CONFIG_HAS_SYSPARA" ] 182} 183 184config("libunwinder_config") { 185 defines = [ "HAVE_LIBUNWINDER=1" ] 186} 187 188if (hiperf_use_libunwinder) { 189 common_configs += [ ":libunwinder_config" ] 190} 191 192if (is_ohos && hiperf_use_syspara) { 193 common_configs += [ ":hiperf_syspara_config" ] 194} 195 196ohos_source_set("hiperf_platform_common") { 197 part_name = "hiperf" 198 subsystem_name = "developtools" 199 use_exceptions = true 200 public_deps = common_deps 201 public_configs = common_configs 202 defines = [] 203 204 if (is_ohos) { 205 external_deps = [ 206 "c_utils:utils", 207 "faultloggerd:libunwinder", 208 "hilog:libhilog", 209 "hisysevent:libhisysevent", 210 "init:libbegetutil", 211 "ipc:ipc_core", 212 "samgr:samgr_proxy", 213 ] 214 if (bundle_framework_enable) { 215 external_deps += [ 216 "bundle_framework:appexecfwk_base", 217 "bundle_framework:appexecfwk_core", 218 ] 219 defines += [ "BUNDLE_FRAMEWORK_ENABLE" ] 220 } 221 if (ability_base_enable) { 222 external_deps += [ "ability_base:extractortool" ] 223 defines += [ "ENABLE_HAP_EXTRACTOR" ] 224 } 225 } else { 226 external_deps = [ "faultloggerd:unwinder_host" ] 227 defines += [ "CONFIG_NO_HILOG" ] 228 } 229 external_deps += [ 230 "bounds_checking_function:libsec_shared", 231 "zlib:libz", 232 ] 233 if (hiperf_independent_compilation) { 234 external_deps += [ 235 "hisysevent:libhisysevent", 236 "protobuf:protobuf_lite", 237 ] 238 } 239 sources = sources_platform_common 240} 241 242config("platform_linux_config") { 243 defines = [ "SUPPORT_PERF_EVENT" ] 244} 245 246ohos_source_set("hiperf_platform_linux") { 247 part_name = "hiperf" 248 subsystem_name = "developtools" 249 use_exceptions = true 250 public_deps = common_deps 251 public_configs = common_configs 252 253 if (is_ohos) { 254 external_deps = [ 255 "faultloggerd:libunwinder", 256 "hilog:libhilog", 257 "init:libbegetutil", 258 ] 259 } else { 260 external_deps = [ "faultloggerd:unwinder_host" ] 261 } 262 external_deps += [ 263 "bounds_checking_function:libsec_shared", 264 "c_utils:utils", 265 "zlib:libz", 266 ] 267 if (hiperf_independent_compilation) { 268 external_deps += [ 269 "hisysevent:libhisysevent", 270 "protobuf:protobuf_lite", 271 ] 272 } 273 public_configs += [ ":platform_linux_config" ] 274 configs = [ "interfaces/innerkits/native:hiperf_client_config" ] 275 276 sources = sources_platform_linux 277} 278 279config("elf_config") { 280} 281ohos_source_set("support_elf") { 282 part_name = "hiperf" 283 subsystem_name = "developtools" 284 public_configs = common_configs 285 public_configs += [ ":elf_config" ] 286} 287 288config("protobuf_config") { 289 defines = [ "HAVE_PROTOBUF=1" ] 290 if (!hiperf_independent_compilation) { 291 include_dirs = [ "//third_party/protobuf/src" ] 292 } 293} 294 295ohos_source_set("support_protobuf") { 296 part_name = "hiperf" 297 subsystem_name = "developtools" 298 use_exceptions = true 299 300 #protobuf 301 public_configs = common_configs 302 public_configs += [ ":protobuf_config" ] 303 public_deps = [ ":proto_file_cpp" ] 304 305 if (is_ohos) { 306 external_deps = [ "faultloggerd:libunwinder" ] 307 } else { 308 external_deps = [ "faultloggerd:unwinder_host" ] 309 } 310 external_deps += [ "bounds_checking_function:libsec_shared" ] 311 if (hiperf_independent_compilation) { 312 external_deps += [ 313 "c_utils:utils", 314 "protobuf:protobuf_lite", 315 ] 316 } 317 sources = [ "./src/report_protobuf_file.cpp" ] 318} 319 320#protobuf { 321proto_file_defines = [ 322 # add your proto file here 323 "report_sample", 324] 325 326proto_base_dir = "proto" 327proto_out_dir = "$target_gen_dir" + "/" + proto_base_dir 328 329proto_file_codegen = [] 330proto_file_sources = [] 331 332foreach(proto_file, proto_file_defines) { 333 proto_file_codegen += [ 334 "$proto_out_dir" + "/" + "$proto_file.pb.h", 335 "$proto_out_dir" + "/" + "$proto_file.pb.cc", 336 ] 337 proto_file_sources += [ "$proto_base_dir" + "/" + "$proto_file.proto" ] 338} 339 340# this is so bad , but someone config the protoc's subsystem_name 341# the better way is build system need provider host tools path or prebuild tools path 342protoc_subsystem_out_path = "thirdparty/protobuf" 343 344if (default_toolchain == current_toolchain) { 345 #if target build 346 host_out_path = "/" + get_label_info(host_toolchain, "name") 347} else { 348 #if host build (for some linke mingw) 349 host_out_path = "/../" + get_label_info(host_toolchain, "name") 350} 351host_protoc_path = 352 root_out_dir + host_out_path + "/" + protoc_subsystem_out_path + "/protoc" 353 354action("hiperf_host_build_proto") { 355 external_deps = 356 [ "protobuf:protoc(//build/toolchain/linux:clang_${host_cpu})" ] 357 args = [] 358 outputs = proto_file_codegen 359 sources = [] 360 script = "proto/build_proto.sh" 361 362 args += [ rebase_path(host_protoc_path) ] 363 args += [ 364 "--proto_path", 365 rebase_path(proto_base_dir), 366 ] 367 args += [ 368 "--cpp_out", 369 rebase_path(proto_out_dir), 370 ] 371 372 foreach(proto_file_source, proto_file_sources) { 373 #tell gn to check which files as source time 374 sources += [ rebase_path(proto_file_source) ] 375 args += [ rebase_path(proto_file_source) ] 376 } 377} 378 379config("proto_file_cpp_config") { 380 include_dirs = [ proto_out_dir ] 381 if ("$proto_out_dir" == "*src_test*") { 382 include_dirs += 383 [ "../../out/default/src_test/gen/developtools/hiperf/proto" ] 384 } else if ("$proto_out_dir" == "*test*") { 385 include_dirs += [ "../../out/default/test/gen/developtools/hiperf/proto" ] 386 } else { 387 include_dirs += [ "../../out/default/src/gen/developtools/hiperf/proto" ] 388 } 389} 390 391ohos_source_set("proto_file_cpp") { 392 part_name = "hiperf" 393 subsystem_name = "developtools" 394 cflags = [] 395 396 deps = [ ":hiperf_host_build_proto" ] 397 external_deps = [ "protobuf:protobuf_lite_static" ] 398 if (hiperf_independent_compilation) { 399 external_deps += [ "c_utils:utils" ] 400 } 401 sources = proto_file_codegen 402 public_configs = [ ":proto_file_cpp_config" ] 403} 404 405#protobuf } 406 407ohos_executable("hiperf") { 408 install_enable = true 409 sources = [ "./src/main.cpp" ] 410 deps = [ 411 ":hiperf_etc", 412 ":hiperf_platform_common", 413 ":hiperf_platform_linux", 414 ] 415 416 if (hiperf_target_static) { 417 static_link = true 418 } 419 420 if (is_linux || is_mingw) { 421 # ld.lld: error: attempted static link of dynamic object hiviewdfx/hilog_native/libhilog.so 422 static_link = true 423 } 424 425 external_deps = [ 426 "bounds_checking_function:libsec_shared", 427 "c_utils:utils", 428 "faultloggerd:libunwinder", 429 "hilog:libhilog", 430 "ipc:ipc_single", 431 ] 432 433 if (hiperf_independent_compilation) { 434 external_deps += [ 435 "hisysevent:libhisysevent", 436 "protobuf:protobuf_lite", 437 ] 438 } 439 subsystem_name = "developtools" 440 part_name = "hiperf" 441} 442 443ohos_executable("hiperf_host") { 444 sources = [ "./src/main.cpp" ] 445 deps = [ ":hiperf_platform_common" ] 446 447 external_deps = [ 448 "bounds_checking_function:libsec_shared", 449 "faultloggerd:unwinder_host", 450 ] 451 452 if (hiperf_independent_compilation) { 453 external_deps += [ 454 "c_utils:utils", 455 "hisysevent:libhisysevent", 456 ] 457 } 458 subsystem_name = "developtools" 459 part_name = "hiperf" 460} 461 462ohos_prebuilt_etc("hiperf.para") { 463 source = "etc/hiperf.para" 464 install_images = [ 465 "system", 466 "updater", 467 ] 468 module_install_dir = "etc/param" 469 part_name = "hiperf" 470 subsystem_name = "developtools" 471} 472 473ohos_prebuilt_etc("hiperf.para.dac") { 474 source = "etc/hiperf.para.dac" 475 install_images = [ 476 "system", 477 "updater", 478 ] 479 module_install_dir = "etc/param" 480 part_name = "hiperf" 481 subsystem_name = "developtools" 482} 483 484ohos_prebuilt_etc("hiperf.cfg") { 485 source = "etc/hiperf.cfg" 486 relative_install_dir = "init" 487 subsystem_name = "developtools" 488 part_name = "hiperf" 489} 490 491group("hiperf_etc") { 492 deps = [ 493 ":hiperf.cfg", 494 ":hiperf.para", 495 ":hiperf.para.dac", 496 ] 497} 498 499ohos_source_set("hiperf_platform_host") { 500 part_name = "hiperf" 501 subsystem_name = "developtools" 502 sources = [ "./src/hiperf_libreport.cpp" ] 503 public_deps = [ ":hiperf_platform_common" ] 504 external_deps = [ 505 "bounds_checking_function:libsec_shared", 506 "faultloggerd:unwinder_host", 507 ] 508 if (hiperf_independent_compilation) { 509 external_deps += [ 510 "c_utils:utils", 511 "protobuf:protobuf_lite", 512 ] 513 } 514} 515 516ohos_shared_library("hiperf_host_lib") { 517 public_deps = [ ":hiperf_platform_host" ] 518 output_name = "hiperf_report" 519 520 ldflags = [ "-static-libstdc++" ] 521 522 subsystem_name = "developtools" 523 part_name = "hiperf" 524} 525 526ohos_executable("hiperf_host_lib_demo") { 527 sources = [ "./src/hiperf_libreport_demo.cpp" ] 528 deps = [ ":hiperf_host_lib" ] 529 include_dirs = [ "${hiperf_path}/include" ] 530 531 subsystem_name = "developtools" 532 part_name = "hiperf" 533} 534 535ohos_copy("hiperf_host_python") { 536 sources = [ "./script" ] 537 outputs = [ target_out_dir + "/host/" ] 538 539 module_source_dir = target_out_dir + "/$target_name" 540 module_install_name = "" 541 subsystem_name = "developtools" 542 part_name = "hiperf" 543} 544 545ohos_source_set("hiperf_code_analyze") { 546 part_name = "hiperf" 547 deps = [ 548 ":hiperf_platform_common", 549 ":hiperf_platform_linux", 550 ] 551 subsystem_name = "developtools" 552 part_name = "hiperf" 553} 554 555group("hiperf_target") { 556 if (hiperf_target_host) { 557 deps = [ ":hiperf(${host_toolchain})" ] 558 } else { 559 deps = [ ":hiperf" ] 560 } 561} 562 563group("hiperf_test_target") { 564 testonly = true 565 deps = [ "test:hiperf_test" ] 566} 567 568group("hiperf_target_all") { 569 if (!is_emulator) { 570 if (is_double_framework) { 571 deps = [ ":hiperf_target" ] 572 } else { 573 deps = [] 574 if (!use_libfuzzer) { 575 if (!hiperf_independent_compilation) { 576 deps += [ 577 ":hiperf_host(//build/toolchain/linux:clang_${host_cpu})", # host_linux 578 ":hiperf_host_lib(//build/toolchain/linux:clang_${host_cpu})", # host_linux 579 ":hiperf_host_lib_demo(//build/toolchain/linux:clang_${host_cpu})", # host_linux 580 ] 581 } 582 deps += [ ":hiperf_host_python" ] 583 if (!is_tsan && !hiperf_independent_compilation) { 584 deps += [ 585 ":hiperf_host(//build/toolchain/mingw:mingw_x86_64)", # host mingw 586 ":hiperf_host_lib(//build/toolchain/mingw:mingw_x86_64)", # host_mingw 587 ] 588 } 589 } 590 deps += [ 591 ":hiperf_target", 592 "interfaces/innerkits/native:hiperf_client", # c++ api 593 ] 594 if (is_ohos) { 595 deps += [ "interfaces/kits/js/napi:hiperf_client_napi" ] # js api 596 } 597 } 598 } 599} 600 601group("hiperf_demo") { 602 if (hiperf_target_host) { 603 deps = [ "demo/cpp:hiperf_demo(${host_toolchain})" ] 604 } else { 605 deps = [ "demo/cpp:hiperf_demo" ] 606 } 607} 608 609group("hiperf_example_cmd") { 610 if (hiperf_target_host) { 611 deps = [ "demo/cpp:hiperf_example_cmd(${host_toolchain})" ] 612 } else { 613 deps = [ "demo/cpp:hiperf_example_cmd" ] 614 } 615} 616 617group("hiperf_all") { 618 testonly = true 619 if (hiperf_code_analyze) { 620 deps = [ ":hiperf_code_analyze" ] 621 } else { 622 deps = [ 623 ":hiperf_example_cmd", 624 ":hiperf_target_all", 625 ] 626 if (!is_double_framework) { 627 deps += [ 628 ":hiperf_demo", 629 ":hiperf_test_target", 630 ] 631 } 632 } 633} 634