1# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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")
15common_source = [
16  "libunwind/src/dwarf/Gexpr.c",
17  "libunwind/src/dwarf/Gfde.c",
18  "libunwind/src/dwarf/Gfind_proc_info-lsb.c",
19  "libunwind/src/dwarf/Gfind_unwind_table.c",
20  "libunwind/src/dwarf/global.c",
21  "libunwind/src/dwarf/Gparser.c",
22  "libunwind/src/dwarf/Gpe.c",
23  "libunwind/src/dwarf/Lexpr.c",
24  "libunwind/src/dwarf/Lfde.c",
25  "libunwind/src/dwarf/Lfind_proc_info-lsb.c",
26  "libunwind/src/dwarf/Lfind_unwind_table.c",
27  "libunwind/src/dwarf/Lparser.c",
28  "libunwind/src/dwarf/Lpe.c",
29  "libunwind/src/mi/backtrace.c",
30  "libunwind/src/mi/dyn-cancel.c",
31  "libunwind/src/mi/dyn-info-list.c",
32  "libunwind/src/mi/dyn-register.c",
33  "libunwind/src/mi/flush_cache.c",
34  "libunwind/src/mi/Gdestroy_addr_space.c",
35  "libunwind/src/mi/Gdyn-extract.c",
36  "libunwind/src/mi/Gdyn-remote.c",
37  "libunwind/src/mi/Gfind_dynamic_proc_info.c",
38  "libunwind/src/mi/Gget_accessors.c",
39  "libunwind/src/mi/Gget_fpreg.c",
40  "libunwind/src/mi/Gget_proc_info_by_ip.c",
41  "libunwind/src/mi/Gget_proc_name.c",
42  "libunwind/src/mi/Gget_reg.c",
43  "libunwind/src/mi/Gput_dynamic_unwind_info.c",
44  "libunwind/src/mi/Gset_cache_size.c",
45  "libunwind/src/mi/Gset_caching_policy.c",
46  "libunwind/src/mi/Gset_fpreg.c",
47  "libunwind/src/mi/Gset_reg.c",
48  "libunwind/src/mi/init.c",
49  "libunwind/src/mi/Ldestroy_addr_space.c",
50  "libunwind/src/mi/Ldyn-extract.c",
51  "libunwind/src/mi/Lfind_dynamic_proc_info.c",
52  "libunwind/src/mi/Lget_fpreg.c",
53  "libunwind/src/mi/Lget_proc_info_by_ip.c",
54  "libunwind/src/mi/Lget_proc_name.c",
55  "libunwind/src/mi/Lget_reg.c",
56  "libunwind/src/mi/Lput_dynamic_unwind_info.c",
57  "libunwind/src/mi/Lset_cache_size.c",
58  "libunwind/src/mi/Lset_caching_policy.c",
59  "libunwind/src/mi/Lset_fpreg.c",
60  "libunwind/src/mi/Lset_reg.c",
61  "libunwind/src/mi/mempool.c",
62  "libunwind/src/mi/strerror.c",
63  "libunwind/src/os-linux.c",
64  "libunwind/src/ptrace/_UPT_access_fpreg.c",
65  "libunwind/src/ptrace/_UPT_access_mem.c",
66  "libunwind/src/ptrace/_UPT_access_reg.c",
67  "libunwind/src/ptrace/_UPT_accessors.c",
68  "libunwind/src/ptrace/_UPT_create.c",
69  "libunwind/src/ptrace/_UPT_destroy.c",
70  "libunwind/src/ptrace/_UPT_find_proc_info.c",
71  "libunwind/src/ptrace/_UPT_get_proc_name.c",
72  "libunwind/src/ptrace/_UPT_put_unwind_info.c",
73  "libunwind/src/ptrace/_UPT_reg_offset.c",
74  "libunwind/src/ptrace/_UPT_resume.c",
75]
76
77# as libc++ is static linked with libunwind.a
78# we remove the Gstep.c for duplicated symbol violation
79if (target_cpu == "arm") {
80  arm_source = [
81    "libunwind/src/arm/Gapply_reg_state.c",
82    "libunwind/src/arm/Gcreate_addr_space.c",
83    "libunwind/src/arm/Gex_tables.c",
84    "libunwind/src/arm/Gget_proc_info.c",
85    "libunwind/src/arm/Gget_save_loc.c",
86    "libunwind/src/arm/Gglobal.c",
87    "libunwind/src/arm/Ginit.c",
88    "libunwind/src/arm/Ginit_local.c",
89    "libunwind/src/arm/Ginit_remote.c",
90    "libunwind/src/arm/Gos-linux.c",
91    "libunwind/src/arm/Greg_states_iterate.c",
92    "libunwind/src/arm/Gregs.c",
93    "libunwind/src/arm/Gresume.c",
94    "libunwind/src/arm/Gstash_frame.c",
95    "libunwind/src/arm/Gstep.c",
96    "libunwind/src/arm/Gtrace.c",
97    "libunwind/src/arm/Lcreate_addr_space.c",
98    "libunwind/src/arm/Lex_tables.c",
99    "libunwind/src/arm/Lget_proc_info.c",
100    "libunwind/src/arm/Lget_save_loc.c",
101    "libunwind/src/arm/Lglobal.c",
102    "libunwind/src/arm/Linit.c",
103    "libunwind/src/arm/Linit_local.c",
104    "libunwind/src/arm/Linit_remote.c",
105    "libunwind/src/arm/Los-linux.c",
106    "libunwind/src/arm/Lregs.c",
107    "libunwind/src/arm/Lresume.c",
108    "libunwind/src/arm/Lstash_frame.c",
109    "libunwind/src/arm/Lstep.c",
110    "libunwind/src/arm/Ltrace.c",
111    "libunwind/src/arm/getcontext.S",
112    "libunwind/src/arm/is_fpreg.c",
113    "libunwind/src/arm/regname.c",
114    "libunwind/src/arm/siglongjmp.S",
115    "libunwind/src/elf32.c",
116  ]
117}
118if (target_cpu == "arm64") {
119  arm64_source = [
120    "libunwind/src/aarch64/Gcreate_addr_space.c",
121    "libunwind/src/aarch64/Gget_proc_info.c",
122    "libunwind/src/aarch64/Gget_save_loc.c",
123    "libunwind/src/aarch64/Gglobal.c",
124    "libunwind/src/aarch64/Ginit.c",
125    "libunwind/src/aarch64/Ginit_local.c",
126    "libunwind/src/aarch64/Ginit_remote.c",
127    "libunwind/src/aarch64/Gis_signal_frame.c",
128    "libunwind/src/aarch64/Gregs.c",
129    "libunwind/src/aarch64/Gresume.c",
130    "libunwind/src/aarch64/Gstash_frame.c",
131    "libunwind/src/aarch64/Gstep.c",
132    "libunwind/src/aarch64/Gtrace.c",
133    "libunwind/src/aarch64/Lcreate_addr_space.c",
134    "libunwind/src/aarch64/Lget_proc_info.c",
135    "libunwind/src/aarch64/Lget_save_loc.c",
136    "libunwind/src/aarch64/Lglobal.c",
137    "libunwind/src/aarch64/Linit.c",
138    "libunwind/src/aarch64/Linit_local.c",
139    "libunwind/src/aarch64/Linit_remote.c",
140    "libunwind/src/aarch64/Lis_signal_frame.c",
141    "libunwind/src/aarch64/Lregs.c",
142    "libunwind/src/aarch64/Lresume.c",
143    "libunwind/src/aarch64/Lstash_frame.c",
144    "libunwind/src/aarch64/Lstep.c",
145    "libunwind/src/aarch64/Ltrace.c",
146    "libunwind/src/aarch64/getcontext.S",
147    "libunwind/src/aarch64/is_fpreg.c",
148    "libunwind/src/aarch64/regname.c",
149    "libunwind/src/elf64.c",
150  ]
151}
152
153if (target_cpu == "x64") {
154  x64_source = [
155    "libunwind/src/elf64.c",
156    "libunwind/src/x86_64/Gcreate_addr_space.c",
157    "libunwind/src/x86_64/Gget_proc_info.c",
158    "libunwind/src/x86_64/Gget_save_loc.c",
159    "libunwind/src/x86_64/Gglobal.c",
160    "libunwind/src/x86_64/Ginit.c",
161    "libunwind/src/x86_64/Ginit_local.c",
162    "libunwind/src/x86_64/Ginit_remote.c",
163    "libunwind/src/x86_64/Gos-linux.c",
164    "libunwind/src/x86_64/Gregs.c",
165    "libunwind/src/x86_64/Gresume.c",
166    "libunwind/src/x86_64/Gstash_frame.c",
167    "libunwind/src/x86_64/Gstep.c",
168    "libunwind/src/x86_64/Gtrace.c",
169    "libunwind/src/x86_64/Lcreate_addr_space.c",
170    "libunwind/src/x86_64/Lget_proc_info.c",
171    "libunwind/src/x86_64/Lget_save_loc.c",
172    "libunwind/src/x86_64/Lglobal.c",
173    "libunwind/src/x86_64/Linit.c",
174    "libunwind/src/x86_64/Linit_local.c",
175    "libunwind/src/x86_64/Linit_remote.c",
176    "libunwind/src/x86_64/Los-linux.c",
177    "libunwind/src/x86_64/Lregs.c",
178    "libunwind/src/x86_64/Lresume.c",
179    "libunwind/src/x86_64/Lstash_frame.c",
180    "libunwind/src/x86_64/Lstep.c",
181    "libunwind/src/x86_64/Ltrace.c",
182    "libunwind/src/x86_64/getcontext.S",
183    "libunwind/src/x86_64/is_fpreg.c",
184    "libunwind/src/x86_64/regname.c",
185    "libunwind/src/x86_64/setcontext.S",
186  ]
187}
188remove_sources = []
189
190ptrace_sources = [
191  "libunwind/src/ptrace/_UPT_access_fpreg.c",
192  "libunwind/src/ptrace/_UPT_access_mem.c",
193  "libunwind/src/ptrace/_UPT_access_reg.c",
194  "libunwind/src/ptrace/_UPT_accessors.c",
195  "libunwind/src/ptrace/_UPT_create.c",
196  "libunwind/src/ptrace/_UPT_destroy.c",
197  "libunwind/src/ptrace/_UPT_find_proc_info.c",
198  "libunwind/src/ptrace/_UPT_get_dyn_info_list_addr.c",
199  "libunwind/src/ptrace/_UPT_get_proc_name.c",
200  "libunwind/src/ptrace/_UPT_put_unwind_info.c",
201  "libunwind/src/ptrace/_UPT_reg_offset.c",
202  "libunwind/src/ptrace/_UPT_resume.c",
203]
204
205libunwind_la_SOURCES_local_nounwind = [
206  "libunwind/src/mi/backtrace.c",
207  "libunwind/src/mi/dyn-cancel.c",
208  "libunwind/src/mi/dyn-info-list.c",
209  "libunwind/src/mi/dyn-register.c",
210  "libunwind/src/mi/Ldyn-extract.c",
211  "libunwind/src/mi/Lfind_dynamic_proc_info.c",
212  "libunwind/src/mi/Lget_proc_info_by_ip.c",
213  "libunwind/src/mi/Lget_proc_name.c",
214  "libunwind/src/mi/Lput_dynamic_unwind_info.c",
215  "libunwind/src/mi/Ldestroy_addr_space.c",
216  "libunwind/src/mi/Lget_reg.c",
217  "libunwind/src/mi/Lset_reg.c",
218  "libunwind/src/mi/Lget_fpreg.c",
219  "libunwind/src/mi/Lset_fpreg.c",
220  "libunwind/src/mi/Lset_caching_policy.c",
221  "libunwind/src/mi/Lset_cache_size.c",
222]
223
224libunwind_dwarf_local_la_SOURCES = [
225  "libunwind/src/dwarf/Lexpr.c",
226  "libunwind/src/dwarf/Lfde.c",
227  "libunwind/src/dwarf/Lparser.c",
228  "libunwind/src/dwarf/Lpe.c",
229  "libunwind/src/dwarf/Lfind_proc_info-lsb.c",
230  "libunwind/src/dwarf/Lfind_unwind_table.c",
231]
232
233# remove local file
234remove_sources += libunwind_la_SOURCES_local_nounwind
235remove_sources += libunwind_dwarf_local_la_SOURCES
236remove_sources += ptrace_sources
237
238if (is_mingw) {
239  common_source += [ "libunwind/src/mingw/pal-single-threaded.c" ]
240}
241
242config("unwind_config_public") {
243  include_dirs = [
244    "libunwind/src",
245    "libunwind/include",
246  ]
247
248  cflags = [
249    "-D_GNU_SOURCE",
250    "-DHAVE_CONFIG_H",
251    "-DCC_IS_CLANG",
252    "-fcommon",
253    "-Werror",
254    "-Wno-absolute-value",
255    "-Wno-header-guard",
256    "-Wno-unused-parameter",
257    "-Wno-unused-variable",
258    "-Wno-unused-result",
259    "-Wno-tautological-constant-out-of-range-compare",
260    "-Wno-bitfield-constant-conversion",
261  ]
262  if (use_wasm) {
263    cflags += [ "-Wno-incompatible-pointer-types" ]
264  }
265  if (is_mingw) {
266    cflags += [
267      "-Wno-attributes",
268      "-Wno-pointer-to-int-cast",
269      "-Wno-implicit-function-declaration",
270    ]
271  }
272
273  if (defined(ohos_lite)) {
274    cflags += [ "-fPIC" ]
275  }
276
277  if (target_cpu == "arm") {
278    include_dirs += [ "libunwind/include/tdep-arm" ]
279    cflags += [
280      "-Wno-inline-asm",
281      "-Wno-shift-count-overflow",
282      "-Wno-tautological-constant-out-of-range-compare",
283      "-Wno-unused-function",
284    ]
285  } else if (target_cpu == "arm64") {
286    include_dirs += [ "libunwind/include/tdep-aarch64" ]
287    cflags += [ "-Wno-incompatible-pointer-types" ]
288  } else if (target_cpu == "x64") {
289    include_dirs += [ "libunwind/include/tdep-x86_64" ]
290  }
291}
292config("unwind_config_remote") {
293  cflags =
294      [ "-Wno-format" ]  # some debug feature will warning in host x64 build
295}
296config("unwind_config_remote_public") {
297  cflags = []
298  include_dirs = []
299  defines = []
300
301  # this is a host tools build
302  # what means host use remote mode to unwind
303  # with dwarf from stack or coredump or something not real target
304  # There is an exception, we can support local unwind for linux.
305  cflags += [ "-DUNW_REMOTE_ONLY" ]
306
307  cflags += [ "-DBUILD_REMOTE" ]
308
309  cflags += [ "-Wno-sometimes-uninitialized" ]  # some value not initialized in
310                                                # host x64 build
311  cflags += [ "-Wno-int-to-void-pointer-cast" ]
312
313  if (is_mingw) {
314    include_dirs += [ "include/mingw" ]
315    cflags += [ "-DMINGW" ]
316  } else if (is_linux) {
317    cflags += [ "-g" ]  # we need debug info when it crash.
318  }
319
320  defines += [ "build_remote=1" ]
321  defines += [ "target_cpu=${target_cpu}" ]
322}
323
324config("unwind_config_arm") {
325  defines = [ "UNW_TARGET_ARM" ]
326}
327
328config("unwind_config_arm64") {
329  defines = [ "UNW_TARGET_ARM64" ]
330}
331
332config("unwind_config_x64") {
333  defines = [ "UNW_TARGET_X86_64" ]
334  defines += [ "UNW_TARGET_X86_64_LINUX" ]
335}
336if (target_cpu == "arm") {
337  ohos_source_set("unwind_source_arm") {
338    subsystem_name = "developtools"
339    part_name = "smartperf_host"
340    configs += [ ":unwind_config_remote" ]
341    public_configs = [
342      ":unwind_config_public",
343      ":unwind_config_remote_public",
344      ":unwind_config_arm",
345    ]
346    sources = common_source
347
348    # there is a main function in this file
349    # for a lib we dont need this
350    arm_source -= [ "src/arm/gen-offsets.c" ]
351
352    # no jump lib
353    arm_source -= [
354      "libunwind/src/arm/getcontext.S",
355      "libunwind/src/arm/siglongjmp.S",
356    ]
357    sources += arm_source
358    sources -= remove_sources
359  }
360}
361if (target_cpu == "arm64") {
362  ohos_source_set("unwind_source_arm64") {
363    subsystem_name = "developtools"
364    part_name = "smartperf_host"
365    configs += [ ":unwind_config_remote" ]
366    public_configs = [
367      ":unwind_config_public",
368      ":unwind_config_remote_public",
369      ":unwind_config_arm64",
370    ]
371    sources = common_source
372    arm64_source -= [ "libunwind/src/aarch64/getcontext.S" ]
373
374    sources += arm64_source
375    sources -= remove_sources
376  }
377}
378if (target_cpu == "x86") {
379  ohos_source_set("unwind_source_x64") {
380    subsystem_name = "developtools"
381    part_name = "smartperf_host"
382    configs += [ ":unwind_config_remote" ]
383    public_configs = [
384      ":unwind_config_public",
385      ":unwind_config_remote_public",
386      ":unwind_config_x64",
387    ]
388    sources = common_source
389
390    # no jump lib
391    x64_source -= [
392      "libunwind/src/x86_64/getcontext.S",
393      "libunwind/src/x86_64/setcontext.S",
394    ]
395    sources += x64_source
396    sources -= remove_sources
397  }
398}
399ohos_source_set("unwind_source") {
400  subsystem_name = "developtools"
401  part_name = "smartperf_host"
402  configs += [ ":unwind_config_public" ]
403  sources = common_source
404  if (target_cpu == "arm") {
405    # as libc++ is static linked with libunwind.a
406    # we remove the Gstep.c for duplicated symbol violation
407    sources += arm_source
408    public_configs = [ ":unwind_config_arm" ]
409  } else if (target_cpu == "arm64") {
410    sources += arm64_source
411    public_configs = [ ":unwind_config_arm64" ]
412  } else if (target_cpu == "x64") {
413    sources += x64_source
414    public_configs = [ ":unwind_config_x64" ]
415  }
416}
417
418source_set("libunwind") {
419  deps = [ ":unwind_source" ]
420  public_configs = [ ":unwind_config_public" ]
421}
422