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