• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
120# compile the smartperf host trace_streamer using this.
121sources_platform_with_ts_common = [
122  "./src/dwarf_encoding.cpp",
123  "./src/option.cpp",
124  "./src/perf_event_record.cpp",
125  "./src/perf_file_format.cpp",
126  "./src/perf_file_reader.cpp",
127  "./src/register.cpp",
128  "./src/report.cpp",
129  "./src/subcommand.cpp",
130  "./src/symbols_file.cpp",
131  "./src/unique_stack_table.cpp",
132  "./src/utilities.cpp",
133  "./src/virtual_runtime.cpp",
134  "./src/virtual_thread.cpp",
135]
136
137if (hiperf_debug) {
138  sources_platform_with_ts_common += [
139    "./src/debug_logger.cpp",
140  ]
141}
142
143sources_platform_common = [
144  "./src/command.cpp",
145  "./src/command_reporter.cpp",
146  "./src/ipc_utilities.cpp",
147  "./src/report_json_file.cpp",
148  "./src/subcommand_dump.cpp",
149  "./src/subcommand_help.cpp",
150  "./src/subcommand_report.cpp",
151]
152
153sources_platform_common += sources_platform_with_ts_common
154
155if (is_ohos) {
156  sources_platform_common += [ "./src/callstack.cpp" ]
157}
158
159if (hiperf_debug) {
160  sources_platform_common += [
161    "./src/option_debug.cpp",
162  ]
163}
164
165sources_platform_linux = [
166  "./src/perf_events.cpp",
167  "./src/tracked_command.cpp",
168  "./src/ring_buffer.cpp",
169  "./src/perf_file_writer.cpp",
170  "./src/subcommand_stat.cpp",
171  "./src/subcommand_record.cpp",
172  "./src/subcommand_list.cpp",
173  "./src/spe_decoder.cpp",
174  "./src/perf_pipe.cpp",
175]
176
177common_deps = [
178  ":support_elf",
179  ":support_protobuf",
180]
181
182common_configs = [ ":hiperf_inner_config" ]
183
184if (!hiperf_independent_compilation) {
185  common_configs += [ "../../commonlibrary/c_utils/base:utils_config" ]
186}
187
188if (hiperf_target_static) {
189  common_deps -= [ ":support_protobuf" ]
190}
191
192config("hiperf_syspara_config") {
193  defines = [ "CONFIG_HAS_SYSPARA" ]
194}
195
196config("libunwinder_config") {
197  defines = [ "HAVE_LIBUNWINDER=1" ]
198}
199
200if (hiperf_use_libunwinder) {
201  common_configs += [ ":libunwinder_config" ]
202}
203
204if (is_ohos && hiperf_use_syspara) {
205  common_configs += [ ":hiperf_syspara_config" ]
206}
207
208ohos_source_set("hiperf_platform_common") {
209  part_name = "hiperf"
210  subsystem_name = "developtools"
211  use_exceptions = true
212  public_deps = common_deps
213  public_configs = common_configs
214  defines = []
215
216  if (is_ohos) {
217    external_deps = [
218      "cJSON:cjson",
219      "c_utils:utils",
220      "config_policy:configpolicy_util",
221      "faultloggerd:libunwinder",
222      "hilog:libhilog",
223      "hisysevent:libhisysevent",
224      "init:libbegetutil",
225      "ipc:ipc_core",
226      "samgr:samgr_proxy",
227    ]
228    defines += [ "CONFIG_HAS_CCM" ]
229    if (bundle_framework_enable) {
230      external_deps += [
231        "bundle_framework:appexecfwk_base",
232        "bundle_framework:appexecfwk_core",
233      ]
234      defines += [ "BUNDLE_FRAMEWORK_ENABLE" ]
235    }
236    if (ability_base_enable) {
237      external_deps += [ "ability_base:extractortool" ]
238      defines += [ "ENABLE_HAP_EXTRACTOR" ]
239    }
240  } else {
241    external_deps = [ "faultloggerd:unwinder_host" ]
242    defines += [ "CONFIG_NO_HILOG" ]
243  }
244  external_deps += [
245    "bounds_checking_function:libsec_shared",
246    "zlib:libz",
247  ]
248  if (hiperf_independent_compilation) {
249    external_deps += [
250      "hisysevent:libhisysevent",
251      "protobuf:protobuf_lite",
252    ]
253  }
254  sources = sources_platform_common
255}
256
257config("platform_linux_config") {
258  defines = [ "SUPPORT_PERF_EVENT" ]
259}
260
261ohos_source_set("hiperf_platform_linux") {
262  part_name = "hiperf"
263  subsystem_name = "developtools"
264  use_exceptions = true
265  public_deps = common_deps
266  public_configs = common_configs
267
268  if (is_ohos) {
269    external_deps = [
270      "cJSON:cjson",
271      "faultloggerd:libunwinder",
272      "hilog:libhilog",
273      "init:libbegetutil",
274    ]
275    defines = [ "CONFIG_HAS_CCM" ]
276  } else {
277    external_deps = [ "faultloggerd:unwinder_host" ]
278  }
279  external_deps += [
280    "bounds_checking_function:libsec_shared",
281    "c_utils:utils",
282    "zlib:libz",
283  ]
284  if (hiperf_independent_compilation) {
285    external_deps += [
286      "hisysevent:libhisysevent",
287      "protobuf:protobuf_lite",
288    ]
289  }
290  public_configs += [ ":platform_linux_config" ]
291  configs = [ "interfaces/innerkits/native/hiperf_client:hiperf_client_config" ]
292
293  sources = sources_platform_linux
294}
295
296config("elf_config") {
297}
298ohos_source_set("support_elf") {
299  part_name = "hiperf"
300  subsystem_name = "developtools"
301  public_configs = common_configs
302  public_configs += [ ":elf_config" ]
303}
304
305config("protobuf_config") {
306  defines = [ "HAVE_PROTOBUF=1" ]
307  if (!hiperf_independent_compilation) {
308    include_dirs = [ "//third_party/protobuf/src" ]
309  }
310}
311
312ohos_source_set("support_protobuf") {
313  part_name = "hiperf"
314  subsystem_name = "developtools"
315  use_exceptions = true
316
317  #protobuf
318  public_configs = common_configs
319  public_configs += [ ":protobuf_config" ]
320  public_deps = [ ":proto_file_cpp" ]
321
322  if (is_ohos) {
323    external_deps = [ "faultloggerd:libunwinder" ]
324  } else {
325    external_deps = [ "faultloggerd:unwinder_host" ]
326  }
327  external_deps += [ "bounds_checking_function:libsec_shared" ]
328  if (hiperf_independent_compilation) {
329    external_deps += [
330      "c_utils:utils",
331      "protobuf:protobuf_lite",
332    ]
333  }
334  sources = [ "./src/report_protobuf_file.cpp" ]
335}
336
337#protobuf {
338proto_file_defines = [
339  # add your proto file here
340  "report_sample",
341]
342
343proto_base_dir = "proto"
344proto_out_dir = "$target_gen_dir" + "/" + proto_base_dir
345
346proto_file_codegen = []
347proto_file_sources = []
348
349foreach(proto_file, proto_file_defines) {
350  proto_file_codegen += [
351    "$proto_out_dir" + "/" + "$proto_file.pb.h",
352    "$proto_out_dir" + "/" + "$proto_file.pb.cc",
353  ]
354  proto_file_sources += [ "$proto_base_dir" + "/" + "$proto_file.proto" ]
355}
356
357# this is so bad , but someone config the protoc's subsystem_name
358# the better way is build system need provider host tools path or prebuild tools path
359protoc_subsystem_out_path = "thirdparty/protobuf"
360
361if (default_toolchain == current_toolchain) {
362  #if target build
363  host_out_path = "/" + get_label_info(host_toolchain, "name")
364} else {
365  #if host build (for some linke mingw)
366  host_out_path = "/../" + get_label_info(host_toolchain, "name")
367}
368host_protoc_path =
369    root_out_dir + host_out_path + "/" + protoc_subsystem_out_path + "/protoc"
370
371action("hiperf_host_build_proto") {
372  external_deps =
373      [ "protobuf:protoc(//build/toolchain/linux:clang_${host_cpu})" ]
374  args = []
375  outputs = proto_file_codegen
376  sources = []
377  script = "proto/build_proto.sh"
378
379  args += [ rebase_path(host_protoc_path) ]
380  args += [
381    "--proto_path",
382    rebase_path(proto_base_dir),
383  ]
384  args += [
385    "--cpp_out",
386    rebase_path(proto_out_dir),
387  ]
388
389  foreach(proto_file_source, proto_file_sources) {
390    #tell gn to check which files as source time
391    sources += [ rebase_path(proto_file_source) ]
392    args += [ rebase_path(proto_file_source) ]
393  }
394}
395
396config("proto_file_cpp_config") {
397  include_dirs = [ proto_out_dir ]
398  if ("$proto_out_dir" == "*src_test*") {
399    include_dirs +=
400        [ "../../out/default/src_test/gen/developtools/hiperf/proto" ]
401  } else if ("$proto_out_dir" == "*test*") {
402    include_dirs += [ "../../out/default/test/gen/developtools/hiperf/proto" ]
403  } else {
404    include_dirs += [ "../../out/default/src/gen/developtools/hiperf/proto" ]
405  }
406}
407
408ohos_source_set("proto_file_cpp") {
409  part_name = "hiperf"
410  subsystem_name = "developtools"
411  cflags = []
412
413  deps = [ ":hiperf_host_build_proto" ]
414  external_deps = [ "protobuf:protobuf_lite_static" ]
415  if (hiperf_independent_compilation) {
416    external_deps += [ "c_utils:utils" ]
417  }
418  sources = proto_file_codegen
419  public_configs = [ ":proto_file_cpp_config" ]
420}
421
422#protobuf }
423
424ohos_executable("hiperf") {
425  install_enable = true
426  sources = [ "./src/main.cpp" ]
427  deps = [
428    ":hiperf_etc",
429    ":hiperf_platform_common",
430    ":hiperf_platform_linux",
431  ]
432
433  if (hiperf_target_static) {
434    static_link = true
435  }
436
437  if (is_linux || is_mingw) {
438    # ld.lld: error: attempted static link of dynamic object hiviewdfx/hilog_native/libhilog.so
439    static_link = true
440  }
441
442  external_deps = [
443    "abseil-cpp:absl_container",
444    "abseil-cpp:absl_cord",
445    "abseil-cpp:absl_log",
446    "abseil-cpp:absl_strings",
447    "bounds_checking_function:libsec_shared",
448    "c_utils:utils",
449    "faultloggerd:libunwinder",
450    "hilog:libhilog",
451    "ipc:ipc_single",
452  ]
453
454  if (hiperf_independent_compilation) {
455    external_deps += [
456      "hisysevent:libhisysevent",
457      "protobuf:protobuf_lite",
458    ]
459  }
460  subsystem_name = "developtools"
461  part_name = "hiperf"
462}
463
464ohos_executable("hiperf_host") {
465  sources = [ "./src/main.cpp" ]
466
467  deps = [ ":hiperf_platform_common" ]
468
469  external_deps = [
470    "bounds_checking_function:libsec_shared",
471    "protobuf:protobuf_lite",
472  ]
473
474  if (is_ohos) {
475    sources += [ "./src/spe_decoder.cpp" ]
476    external_deps += [
477      "faultloggerd:libunwinder",
478      "hilog:libhilog",
479    ]
480  } else {
481    external_deps += [ "faultloggerd:unwinder_host" ]
482  }
483
484  if (hiperf_independent_compilation) {
485    external_deps += [
486      "c_utils:utils",
487      "hisysevent:libhisysevent",
488    ]
489  }
490  subsystem_name = "developtools"
491  part_name = "hiperf"
492}
493
494ohos_prebuilt_etc("hiperf.para") {
495  source = "etc/hiperf.para"
496  install_images = [
497    "system",
498    "updater",
499  ]
500  module_install_dir = "etc/param"
501  part_name = "hiperf"
502  subsystem_name = "developtools"
503}
504
505ohos_prebuilt_etc("hiperf.para.dac") {
506  source = "etc/hiperf.para.dac"
507  install_images = [
508    "system",
509    "updater",
510  ]
511  module_install_dir = "etc/param"
512  part_name = "hiperf"
513  subsystem_name = "developtools"
514}
515
516ohos_prebuilt_etc("hiperf.cfg") {
517  source = "etc/hiperf.cfg"
518  relative_install_dir = "init"
519  subsystem_name = "developtools"
520  part_name = "hiperf"
521}
522
523group("hiperf_etc") {
524  deps = [
525    ":hiperf.cfg",
526    ":hiperf.para",
527    ":hiperf.para.dac",
528  ]
529}
530
531ohos_source_set("hiperf_platform_host") {
532  part_name = "hiperf"
533  subsystem_name = "developtools"
534  sources = [ "./src/hiperf_libreport.cpp" ]
535  public_deps = [ ":hiperf_platform_common" ]
536  external_deps = [
537    "bounds_checking_function:libsec_shared",
538    "faultloggerd:unwinder_host",
539  ]
540  if (hiperf_independent_compilation) {
541    external_deps += [
542      "c_utils:utils",
543      "protobuf:protobuf_lite",
544    ]
545  }
546}
547
548ohos_shared_library("hiperf_host_lib") {
549  if (!hiperf_independent_compilation) {
550    public_deps = [ ":hiperf_platform_host" ]
551    ldflags = [ "-static-libstdc++" ]
552  }
553  output_name = "hiperf_report"
554
555  subsystem_name = "developtools"
556  part_name = "hiperf"
557}
558
559ohos_executable("hiperf_host_lib_demo") {
560  sources = [ "./src/hiperf_libreport_demo.cpp" ]
561
562  if (!hiperf_independent_compilation) {
563    deps = [ ":hiperf_host_lib" ]
564    include_dirs = [ "${hiperf_path}/include" ]
565  }
566
567  subsystem_name = "developtools"
568  part_name = "hiperf"
569}
570
571ohos_copy("hiperf_host_python") {
572  sources = [ "./script" ]
573  outputs = [ target_out_dir + "/host/" ]
574
575  module_source_dir = target_out_dir + "/$target_name"
576  module_install_name = ""
577  subsystem_name = "developtools"
578  part_name = "hiperf"
579}
580
581ohos_source_set("hiperf_code_analyze") {
582  part_name = "hiperf"
583  deps = [
584    ":hiperf_platform_common",
585    ":hiperf_platform_linux",
586  ]
587  subsystem_name = "developtools"
588  part_name = "hiperf"
589}
590
591group("hiperf_target") {
592  if (hiperf_target_host) {
593    deps = [ ":hiperf(${host_toolchain})" ]
594  } else {
595    deps = [ ":hiperf" ]
596  }
597}
598
599group("hiperf_test_target") {
600  testonly = true
601  deps = [ "test:hiperf_test" ]
602}
603
604group("hiperf_target_all") {
605  if (!is_emulator) {
606    if (is_double_framework) {
607      deps = [ ":hiperf_target" ]
608    } else {
609      deps = []
610      if (!use_libfuzzer) {
611        if (!hiperf_independent_compilation) {
612          defines += [ "IS_UNINDEPENDENT" ]
613          deps += [
614            ":hiperf_host(//build/toolchain/linux:clang_${host_cpu})",  # host_linux
615            ":hiperf_host_lib(//build/toolchain/linux:clang_${host_cpu})",  # host_linux
616            ":hiperf_host_lib_demo(//build/toolchain/linux:clang_${host_cpu})",  # host_linux
617          ]
618        }
619        deps += [ ":hiperf_host_python" ]
620        if (!is_tsan && !hiperf_independent_compilation) {
621          deps += [
622            ":hiperf_host(//build/toolchain/mingw:mingw_x86_64)",  # host mingw
623            ":hiperf_host_lib(//build/toolchain/mingw:mingw_x86_64)",  # host_mingw
624          ]
625        }
626      }
627      deps += [
628        ":hiperf_target",
629        "interfaces/innerkits/native/hiperf_client:hiperf_client",  # c++ api
630      ]
631      if (is_ohos) {
632        deps += [ "interfaces/kits/js/napi:hiperf_client_napi" ]  # js api
633      }
634    }
635  }
636}
637
638group("hiperf_demo") {
639  if (hiperf_target_host) {
640    deps = [ "demo/cpp:hiperf_demo(${host_toolchain})" ]
641  } else {
642    deps = [ "demo/cpp:hiperf_demo" ]
643  }
644}
645
646group("hiperf_example_cmd") {
647  if (hiperf_target_host) {
648    deps = [ "demo/cpp:hiperf_example_cmd(${host_toolchain})" ]
649  } else {
650    deps = [ "demo/cpp:hiperf_example_cmd" ]
651  }
652}
653
654group("hiperf_all") {
655  testonly = true
656  if (hiperf_code_analyze) {
657    deps = [ ":hiperf_code_analyze" ]
658  } else {
659    deps = [
660      ":hiperf_example_cmd",
661      ":hiperf_target_all",
662    ]
663    if (!is_double_framework) {
664      deps += [
665        ":hiperf_demo",
666        ":hiperf_test_target",
667      ]
668    }
669  }
670}
671