• 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("//developtools/hiperf/hiperf.gni")
16
17function_disable_define = []
18
19config("hiperf_inner_config") {
20  visibility = [ ":*" ]
21  ldflags = []
22  cflags = code_check_flag
23  defines = function_disable_define
24
25  if (hiperf_code_analyze && is_ohos) {
26    cflags += code_analyze_flag
27    cflags -= [ "-Werror" ]
28  }
29
30  if (is_mingw) {
31    # lld: error: unable to find library -latomic
32    # lld: error: unable to find library -ldl
33    # lld: error: unable to find library -lrt
34    ldflags += [
35      "-Wl,--whole-archive",
36      "-lpthread",
37      "-Wl,--no-whole-archive",
38    ]
39  } else if (is_linux) {
40    ldflags += [
41      "-Wl,--whole-archive",
42      "-lpthread",
43      "-latomic",
44      "-ldl",
45      "-lrt",
46      "-Wl,--no-whole-archive",
47    ]
48  }
49
50  include_dirs = [ "${hiperf_path}/include" ]
51
52  # debug link
53  # ldflags += [ "-v"]
54
55  if (hiperf_debug) {
56    defines += [
57      "HIPERF_DEBUG",
58      "HIPERF_DEBUG_PRINTF",  # if u want to see printf in the log ?
59    ]
60  } else {
61    # some value just for debug, but also can be opt by clang ,don't as error
62    # but maybe we need do some opt
63    cflags += [ "-Wno-unused-variable" ]
64  }
65
66  if (product_name == "Hi3516DV300") {
67    defines += [ "LITTLE_MEMORY" ]
68  }
69
70  if (hiperf_check_time) {
71    defines += [ "HIPERF_DEBUG_TIME" ]
72  }
73
74  cflags += [ "-std=c++17" ]
75  if (is_linux) {
76    cflags += [ "-g" ]  # we need debug info when it crash.
77  }
78  defines += [ "is_mingw=${is_mingw}" ]
79  defines += [ "is_linux=${is_linux}" ]
80  defines += [ "is_ohos=${is_ohos}" ]
81  defines += [ "is_double_framework=${is_double_framework}" ]
82  if (hiperf_target_host) {
83    defines += [ "target_cpu_${host_cpu}" ]
84  } else {
85    defines += [ "target_cpu_${target_cpu}" ]
86  }
87
88  if (is_mingw) {
89    cflags += [ "-includeMingW64Fix.h" ]
90    cflags += [ "-Wno-inconsistent-dllimport" ]  # in mingw some sec api will
91                                                 # overwrite by utilsecurec
92    defines += [ "WIN32_LEAN_AND_MEAN" ]
93    defines += [ "__LITTLE_ENDIAN_BITFIELD" ]
94
95    include_dirs += [
96      "${hiperf_path}/include/nonlinux/",
97      "//kernel/linux/linux-5.10/include/uapi",
98    ]
99  }
100
101  if (hiperf_test_coverage && is_ohos) {
102    cflags += [
103      "-fprofile-arcs",
104      "-ftest-coverage",
105    ]
106    ldflags += [ "--coverage" ]
107  }
108}
109
110sources_platform_common = [
111  "./src/perf_file_format.cpp",
112  "./src/command.cpp",
113  "./src/subcommand.cpp",
114  "./src/option.cpp",
115  "./src/utilities.cpp",
116  "./src/symbols_file.cpp",
117  "./src/virtual_runtime.cpp",
118  "./src/virtual_thread.cpp",
119  "./src/perf_file_reader.cpp",
120  "./src/perf_event_record.cpp",
121  "./src/dwarf_encoding.cpp",
122  "./src/elf_file.cpp",
123  "./src/elf_header.cpp",
124  "./src/section_header.cpp",
125  "./src/program_header.cpp",
126  "./src/elf_symbol.cpp",
127  "./src/subcommand_help.cpp",
128  "./src/subcommand_dump.cpp",
129  "./src/subcommand_report.cpp",
130  "./src/report.cpp",
131  "./src/report_json_file.cpp",
132  "./src/register.cpp",
133  "./src/callstack.cpp",
134]
135
136if (hiperf_debug) {
137  sources_platform_common += [
138    "./src/debug_logger.cpp",
139    "./src/option_debug.cpp",
140  ]
141}
142
143sources_platform_linux = [
144  "./src/perf_events.cpp",
145  "./src/tracked_command.cpp",
146  "./src/ring_buffer.cpp",
147  "./src/perf_file_writer.cpp",
148  "./src/subcommand_stat.cpp",
149  "./src/subcommand_record.cpp",
150  "./src/subcommand_list.cpp",
151]
152
153common_deps = [
154  ":support_elf",
155  ":support_libunwind",
156  ":support_protobuf",
157  "//third_party/bounds_checking_function:libsec_static",
158  "//third_party/zlib:libz",
159]
160
161if (!hiperf_use_libunwind) {
162  common_deps -= [ ":support_libunwind" ]
163}
164
165if (is_ohos) {
166  common_deps += [ "//third_party/bounds_checking_function:libsec_shared" ]
167  common_deps -= [ "//third_party/bounds_checking_function:libsec_static" ]
168}
169
170if (hiperf_target_static) {
171  common_deps -= [ ":support_protobuf" ]
172}
173
174common_configs = [
175  ":hiperf_inner_config",
176  "//utils/native/base:utils_config",
177  "//third_party/googletest:gtest_config",
178]
179
180config("hiperf_syspara_config") {
181  defines = [ "CONFIG_HAS_SYSPARA" ]
182}
183
184if (is_ohos && hiperf_use_syspara) {
185  common_configs += [ ":hiperf_syspara_config" ]
186  common_deps += [
187    "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
188  ]
189}
190
191ohos_source_set("hiperf_platform_common") {
192  use_exceptions = true
193  public_deps = common_deps
194  public_configs = common_configs
195  defines = []
196
197  if (is_ohos) {
198    external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
199  } else {
200    defines += [ "CONFIG_NO_HILOG" ]
201  }
202  sources = sources_platform_common
203}
204
205config("platform_linux_config") {
206  defines = [ "SUPPORT_PERF_EVENT" ]
207}
208
209ohos_source_set("hiperf_platform_linux") {
210  use_exceptions = true
211  public_deps = common_deps
212  public_configs = common_configs
213
214  public_configs += [ ":platform_linux_config" ]
215  configs = [ "interfaces/innerkits/native:hiperf_client_config" ]
216
217  sources = sources_platform_linux
218}
219
220config("unwind_config") {
221  defines = [ "HAVE_LIBUNWIND=1" ]
222}
223
224ohos_source_set("support_libunwind") {
225  public_configs = common_configs
226  public_configs += [ ":unwind_config" ]
227  if (hiperf_target_host) {
228    public_deps = [ "//third_party/libunwind:unwind_source_${host_cpu}" ]
229  } else {
230    public_deps = [ "//third_party/libunwind:unwind_source_${target_cpu}" ]
231  }
232}
233
234config("elf_config") {
235}
236ohos_source_set("support_elf") {
237  public_configs = common_configs
238  public_configs += [ ":elf_config" ]
239}
240
241config("protobuf_config") {
242  defines = [ "HAVE_PROTOBUF=1" ]
243  cflags = [ "-Wno-unused-parameter" ]  # proto buf head build error
244  include_dirs = [
245    "//third_party/protobuf/src",
246    "//third_party/protobuf/src/google",
247    "//third_party/protobuf/src/google/protobuf",
248  ]
249}
250
251ohos_source_set("support_protobuf") {
252  use_exceptions = true
253
254  #protobuf
255  public_configs = common_configs
256  public_configs += [ ":protobuf_config" ]
257  public_deps = [
258    ":proto_file_cpp",
259    "//third_party/bounds_checking_function:libsec_static",
260  ]
261
262  sources = [ "./src/report_protobuf_file.cpp" ]
263}
264
265#protobuf {
266proto_file_defines = [
267  # add your proto file here
268  "report_sample",
269]
270
271proto_base_dir = "proto"
272proto_out_dir = "$target_gen_dir" + "/" + proto_base_dir
273
274proto_file_codegen = []
275proto_file_sources = []
276
277foreach(proto_file, proto_file_defines) {
278  proto_file_codegen += [
279    "$proto_out_dir" + "/" + "$proto_file.pb.h",
280    "$proto_out_dir" + "/" + "$proto_file.pb.cc",
281  ]
282  proto_file_sources += [ "$proto_base_dir" + "/" + "$proto_file.proto" ]
283}
284
285# this is so bad , but someone config the protoc's subsystem_name
286# the better way is build system need provider host tools path or prebuild tools path
287protoc_subsystem_out_path = "developtools/profiler"
288
289if (default_toolchain == current_toolchain) {
290  #if target build
291  host_out_path = "/" + get_label_info(host_toolchain, "name")
292} else {
293  #if host build (for some linke mingw)
294  host_out_path = "/../" + get_label_info(host_toolchain, "name")
295}
296host_protoc_path =
297    root_out_dir + host_out_path + "/" + protoc_subsystem_out_path + "/protoc"
298
299action("hiperf_host_build_proto") {
300  deps = [ "//third_party/protobuf:protoc(//build/toolchain/linux:clang_x64)" ]
301  args = []
302  outputs = proto_file_codegen
303  sources = []
304  script = "proto/build_proto.sh"
305
306  args += [ rebase_path(host_protoc_path) ]
307  args += [
308    "--proto_path",
309    rebase_path(proto_base_dir),
310  ]
311  args += [
312    "--cpp_out",
313    rebase_path(proto_out_dir),
314  ]
315
316  foreach(proto_file_source, proto_file_sources) {
317    #tell gn to check which files as source time
318    sources += [ rebase_path(proto_file_source) ]
319    args += [ rebase_path(proto_file_source) ]
320  }
321}
322
323config("proto_file_cpp_config") {
324  include_dirs = [ proto_out_dir ]
325}
326
327ohos_source_set("proto_file_cpp") {
328  cflags = []
329
330  deps = [ ":hiperf_host_build_proto" ]
331  public_deps = [ "//third_party/protobuf:protobuf_lite_static" ]
332
333  sources = proto_file_codegen
334  public_configs = [ ":proto_file_cpp_config" ]
335}
336
337#protobuf }
338
339ohos_executable("hiperf") {
340  install_enable = true
341  sources = [ "./src/main.cpp" ]
342  deps = [
343    ":hiperf_platform_common",
344    ":hiperf_platform_linux",
345  ]
346
347  if (hiperf_target_static) {
348    static_link = true
349  }
350
351  if (is_linux || is_mingw) {
352    # ld.lld: error: attempted static link of dynamic object hiviewdfx/hilog_native/libhilog.so
353    static_link = true
354  }
355
356  subsystem_name = "developtools"
357  part_name = "hiperf"
358}
359
360ohos_executable("hiperf_host") {
361  sources = [ "./src/main.cpp" ]
362  deps = [ ":hiperf_platform_common" ]
363
364  if (use_musl) {
365    static_link = true
366  }
367
368  subsystem_name = "developtools"
369  part_name = "hiperf"
370}
371
372ohos_source_set("hiperf_platform_host") {
373  sources = [ "./src/hiperf_libreport.cpp" ]
374  public_deps = [ ":hiperf_platform_common" ]
375}
376
377ohos_shared_library("hiperf_host_lib") {
378  public_deps = [ ":hiperf_platform_host" ]
379  output_name = "hiperf_report"
380
381  ldflags = [ "-static-libstdc++" ]
382
383  subsystem_name = "developtools"
384  part_name = "hiperf"
385}
386
387ohos_executable("hiperf_host_lib_demo") {
388  sources = [ "./src/hiperf_libreport_demo.cpp" ]
389  deps = [ ":hiperf_host_lib" ]
390  include_dirs = [ "${hiperf_path}/include" ]
391
392  subsystem_name = "developtools"
393  part_name = "hiperf"
394}
395
396ohos_copy("hiperf_host_python") {
397  sources = [ "./script" ]
398  outputs = [ target_out_dir + "/host/" ]
399
400  module_source_dir = target_out_dir + "/$target_name"
401  module_install_name = ""
402  subsystem_name = "developtools"
403  part_name = "hiperf"
404}
405
406ohos_source_set("hiperf_code_analyze") {
407  deps = [
408    ":hiperf_platform_common",
409    ":hiperf_platform_linux",
410  ]
411}
412
413group("hiperf_target") {
414  if (hiperf_target_host) {
415    deps = [ ":hiperf(${host_toolchain})" ]
416  } else {
417    deps = [ ":hiperf" ]
418  }
419}
420
421group("hiperf_test_target") {
422  testonly = true
423  deps = [ "test:hiperf_test" ]
424}
425
426group("hiperf_target_all") {
427  if (is_double_framework) {
428    deps = [ ":hiperf_target" ]
429  } else {
430    deps = [
431      ":hiperf_host(//build/toolchain/linux:clang_x64)",  # host linux
432      ":hiperf_host(//build/toolchain/mingw:mingw_x86_64)",  # host mingw
433      ":hiperf_host_lib(//build/toolchain/linux:clang_x64)",  # host linux
434      ":hiperf_host_lib(//build/toolchain/mingw:mingw_x86_64)",  # host mingw
435      ":hiperf_host_lib_demo(//build/toolchain/linux:clang_x64)",  # host linux
436      ":hiperf_host_python",
437      ":hiperf_target",
438      "interfaces/innerkits/native:hiperf_client",  # c++ api
439    ]
440    if (is_ohos) {
441      deps += [ "interfaces/kits/js/napi:hiperf_client_napi" ]  # js api
442    }
443  }
444}
445
446group("hiperf_demo") {
447  if (hiperf_target_host) {
448    deps = [ "demo/cpp:hiperf_demo(${host_toolchain})" ]
449  } else {
450    deps = [ "demo/cpp:hiperf_demo" ]
451  }
452}
453
454group("hiperf_example_cmd") {
455  if (hiperf_target_host) {
456    deps = [ "demo/cpp:hiperf_example_cmd(${host_toolchain})" ]
457  } else {
458    deps = [ "demo/cpp:hiperf_example_cmd" ]
459  }
460}
461
462group("hiperf_all") {
463  testonly = true
464  if (hiperf_code_analyze) {
465    deps = [ ":hiperf_code_analyze" ]
466  } else {
467    deps = [
468      ":hiperf_example_cmd",
469      ":hiperf_target_all",
470    ]
471    if (!is_double_framework) {
472      deps += [
473        ":hiperf_demo",
474        ":hiperf_test_target",
475      ]
476    }
477  }
478}
479