• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import("//build/config/clang/clang.gni")
2import("//build/ohos.gni")
3import("musl_src.gni")
4template("static_and_shared_libs_template") {
5  if (defined(invoker.musl_use_gwp_asan)) {
6    __use_gwp_asan = invoker.musl_use_gwp_asan
7  } else {
8    __use_gwp_asan = musl_use_gwp_asan
9  }
10  __use_flto = invoker.musl_use_flto
11
12  config("gwp_asan_config_${target_name}") {
13    if (__use_gwp_asan) {
14      defines = [ "USE_GWP_ASAN" ]
15      if (!defined(libs)) {
16        libs = [ rebase_path(libgwp_asan_file) ]
17      } else {
18        libs += [ rebase_path(libgwp_asan_file) ]
19      }
20    }
21  }
22
23  config("soft_hook_config_${target_name}") {
24    defines = []
25    configs = [ "//build/config/coverage:default_coverage" ]
26
27    if (is_posix) {
28      configs += [ "//build/config/posix:runtime_library" ]
29    }
30
31    cflags_cc = []
32
33    defines = [
34      "__GNU_SOURCE=1",  # Necessary for clone().
35      "CHROMIUM_CXX_TWEAK_INLINES",  # Saves binary size.
36    ]
37
38    defines += [
39      "__MUSL__",
40      "_LIBCPP_HAS_MUSL_LIBC",
41      "__BUILD_LINUX_WITH_CLANG",
42    ]
43
44    if (!is_asan && musl_arch != "mips") {
45      defines += [
46        "HOOK_ENABLE",
47        "OHOS_SOCKET_HOOK_ENABLE",
48      ]
49    }
50
51    if (musl_use_mutex_wait_opt) {
52      defines += [ "USE_MUTEX_WAIT_OPT" ]
53    }
54
55    if (!is_llvm_build && !is_asan && musl_arch != "mips") {
56      defines += [ "OHOS_FDTRACK_HOOK_ENABLE" ]
57    }
58
59    ldflags = [ "-nostdlib" ]
60
61    libs = []
62
63    if (__use_gwp_asan) {
64      defines += [ "USE_GWP_ASAN" ]
65      libs += [ rebase_path(libgwp_asan_file) ]
66    }
67
68    if (is_component_build) {
69      defines += [ "COMPONENT_BUILD" ]
70    }
71  }
72
73  if (!is_llvm_build) {
74    if (target_name == "static") {
75      # parameterbase_noflto is a library without the compilation option `-flto` enabled
76      param_base_external_deps = [ "init:parameterbase_noflto" ]
77    } else {
78      param_base_external_deps = [ "init:parameterbase" ]
79    }
80  }
81
82  if (target_name == "static") {
83    # libc_static_noflto is a library without the compilation option `-flto` enabled
84    freebsd_external_deps = [ "FreeBSD:libc_static_noflto" ]
85  } else {
86    freebsd_external_deps = [ "FreeBSD:libc_static" ]
87  }
88
89  cur_gwp_asan_config = "gwp_asan_config_${target_name}"
90  cur_soft_hook_config = "soft_hook_config_${target_name}"
91
92  source_set("soft_musl_hook_${target_name}") {
93    sources = [
94      "./src/hook/linux/malloc_common.c",
95      "./src/hook/linux/memory_trace.c",
96      "./src/hook/linux/musl_fdtrack.c",
97      "./src/hook/linux/musl_fdtrack_load.c",
98      "./src/hook/linux/musl_preinit.c",
99      "./src/hook/linux/musl_preinit_common.c",
100      "./src/hook/linux/musl_socket_preinit.c",
101      "./src/hook/linux/musl_socket_preinit_common.c",
102      "./src/hook/linux/socket_common.c",
103      "./src/hook/linux/musl_opt.c"
104    ]
105
106    deps = [
107      "//third_party/musl:create_alltypes_h",
108      "//third_party/musl:create_porting_src",
109      "//third_party/musl:create_syscall_h",
110      "//third_party/musl:create_version_h",
111      "//third_party/musl:musl_copy_inc_bits",
112      "//third_party/musl:musl_copy_inc_fortify",
113      "//third_party/musl:musl_copy_inc_root",
114      "//third_party/musl:musl_copy_inc_sys",
115    ]
116    if (use_musl && !is_llvm_build) {
117      defines = [ "OHOS_ENABLE_PARAMETER" ]
118      external_deps = param_base_external_deps
119    }
120
121    if (musl_unit_test_flag) {
122      defines += [ "UNIT_TEST_STATIC" ]
123    }
124    if (musl_use_jemalloc) {
125      defines += [ "USE_JEMALLOC" ]
126      if (musl_use_jemalloc_dfx_intf) {
127        defines += [ "USE_JEMALLOC_DFX_INTF" ]
128      }
129      if (musl_use_jemalloc_recycle_func) {
130        defines += [ "USE_JEMALLOC_RECYCLE_FUNC" ]
131      }
132      include_dirs = [ "${musl_malloc_plugin}/include/jemalloc" ]
133    }
134
135    configs -= musl_inherited_configs
136
137    if (defined(include_dirs)) {
138      include_dirs += [
139        "${target_out_dir}/${musl_ported_dir}/src/internal",
140        "${target_out_dir}/${musl_ported_dir}/arch/${musl_arch}"
141      ]
142    } else {
143      include_dirs = [
144        "${target_out_dir}/${musl_ported_dir}/src/internal",
145        "${target_out_dir}/${musl_ported_dir}/arch/${musl_arch}"
146      ]
147    }
148
149    configs += [
150      "//build/config/compiler:compiler",
151      ":$cur_soft_hook_config",
152      ":stack_config_musl",
153    ]
154
155    cflags = [
156      "-mllvm",
157      "--instcombine-max-iterations=0",
158      "-ffp-contract=fast",
159      "-O3",
160      "-Wno-int-conversion",
161    ]
162
163    if (use_hwasan) {
164      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
165      cflags += [
166        "-mllvm",
167        "-hwasan-instrument-check-enabled=true",
168      ]
169    }
170
171    if (!__use_flto) {
172      cflags_c = [ "-fno-lto" ]
173    }
174  }
175
176  source_set("soft_musl_src_${target_name}") {
177    forward_variables_from(invoker, [ "*" ])
178    sources_orig = []
179    sources = []
180    external_deps = []
181
182    sources_orig = musl_src_arch_file + musl_src_file
183    sources_orig -= musl_src_filterout
184    sources_orig -= [
185      "src/string/mempcpy.c",
186      "src/string/memset.c",
187      "src/env/__init_tls.c",
188      "src/env/__libc_start_main.c",
189      "src/env/__stack_chk_fail.c",
190      "src/stdlib/qsort.c",
191      "src/stdlib/qsort_nr.c",
192      "src/string/strncpy.c",
193    ]
194
195    sources += [
196      "third_party/openbsd/gnu/lib/libexecinfo/backtrace.c",
197      "third_party/openbsd/lib/libc/string/strcasestr.c",
198    ]
199
200    if (musl_arch == "arm") {
201      if (!is_llvm_build) {
202        sources_orig -= [
203          "src/thread/${musl_arch}/__set_thread_area.c",
204          "src/string/arm/memcpy.S",
205          "src/string/memchr.c",
206          "src/string/strcmp.c",
207          "src/string/strlen.c",
208          "src/math/sincosf.c",
209          "src/math/expf.c",
210          "src/math/exp2f.c",
211          "src/math/exp2l.c",
212          "src/math/exp2.c",
213          "src/math/log.c",
214          "src/math/logl.c",
215          "src/math/log2.c",
216          "src/math/log2f.c",
217          "src/math/log2l.c",
218          "src/math/logf.c",
219          "src/math/log_data.c",
220          "src/math/logf_data.c",
221          "src/math/log2_data.c",
222          "src/math/log2f_data.c",
223          "src/math/exp2f_data.c",
224          "src/math/pow.c",
225          "src/math/powf.c",
226          "src/math/powl.c",
227          "src/math/sinf.c",
228          "src/math/cosf.c",
229          "src/linux/flock.c",
230        ]
231      } else {
232        sources_orig -= [ "src/thread/${musl_arch}/__set_thread_area.c" ]
233        sources_orig += [ "src/string/memset.c" ]
234      }
235    } else if (musl_arch == "aarch64") {
236      sources_orig -= [
237        "src/thread/${musl_arch}/__set_thread_area.s",
238        "src/string/memcpy.c",
239        "src/string/memmove.c",
240        "src/string/memchr.c",
241        "src/string/memcmp.c",
242        "src/string/strcpy.c",
243        "src/string/strcmp.c",
244        "src/string/strlen.c",
245        "src/string/stpcpy.c",
246        "src/string/strchr.c",
247        "src/string/strrchr.c",
248        "src/string/strnlen.c",
249        "src/string/strncmp.c",
250        "src/math/sincosf.c",
251        "src/math/sinf.c",
252        "src/math/cosf.c",
253        "src/math/cos.c",
254        "src/math/exp.c",
255        "src/math/exp2.c",
256        "src/math/exp2f.c",
257        "src/math/expf.c",
258        "src/math/log.c",
259        "src/math/log10.c",
260        "src/math/log2.c",
261        "src/math/log2f.c",
262        "src/math/logb.c",
263        "src/math/logf.c",
264        "src/math/sin.c",
265        "src/math/sincos.c",
266        "src/math/pow.c",
267        "src/math/powf.c",
268        "src/math/frexpl.c",
269        "src/linux/flock.c",
270      ]
271
272      if (is_llvm_build) {
273        sources_orig += [
274          "src/string/memcpy.c",
275          "src/string/memmove.c",
276          "src/string/memchr.c",
277          "src/string/memcmp.c",
278          "src/string/memset.c",
279          "src/string/strcpy.c",
280          "src/string/strcmp.c",
281          "src/string/strlen.c",
282          "src/string/stpcpy.c",
283          "src/string/strchr.c",
284          "src/string/strrchr.c",
285          "src/string/strnlen.c",
286          "src/string/strncmp.c",
287        ]
288      }
289    } else if (musl_arch == "x86_64") {
290      sources_orig -= [
291        "src/thread/${musl_arch}/__set_thread_area.s",
292        "src/linux/flock.c",
293      ]
294    } else if (musl_arch == "riscv64") {
295      sources_orig -= [
296        "src/thread/${musl_arch}/__set_thread_area.s",
297        "src/math/copysign.c",
298        "src/math/copysignf.c",
299      ]
300    } else if (musl_arch == "loongarch64") {
301      sources_orig -= [ "src/thread/${musl_arch}/__set_thread_area.s" ]
302    }
303    if (musl_ld128_flag) {
304      if (musl_arch == "x86_64") {
305        sources_orig -= [
306          "src/math/x86_64/exp2l.s",
307          "src/math/x86_64/expl.s",
308          "src/math/x86_64/expm1l.s",
309          "src/math/x86_64/fabsl.s",
310          "src/math/x86_64/floorl.s",
311          "src/math/x86_64/log1pl.s",
312          "src/math/x86_64/log2l.s",
313          "src/math/x86_64/log10l.s",
314          "src/math/x86_64/logl.s",
315          "src/math/x86_64/sqrtl.s",
316          "src/math/acoshl.c",
317          "src/math/asinhl.c",
318          "src/math/coshl.c",
319          "src/math/sinhl.c",
320          "src/math/erfl.c",
321          "src/math/powl.c",
322          "src/math/lgammal.c",
323          "src/math/tanhl.c",
324        ]
325      } else if (musl_arch == "aarch64") {
326        sources_orig -= [
327          "src/math/acoshl.c",
328          "src/math/asinhl.c",
329          "src/math/coshl.c",
330          "src/math/sinhl.c",
331          "src/math/expl.c",
332          "src/math/expm1l.c",
333          "src/math/erfl.c",
334          "src/math/logl.c",
335          "src/math/log1pl.c",
336          "src/math/log2l.c",
337          "src/math/log10l.c",
338          "src/math/powl.c",
339          "src/math/lgammal.c",
340          "src/math/tanhl.c",
341        ]
342      }
343    }
344
345    defines = []
346    defines += [ "FEATURE_ICU_LOCALE" ]
347    if (musl_use_icu_locale == true) {
348      defines += [ "FEATURE_ICU_LOCALE_TMP" ]
349    }
350
351    # There are two ways to implement cxa_thread_atexit_impl:
352    # - CXA_THREAD_USE_TSD(default): use pthread_key_xxx to implement cxa_thread_atexit_impl.
353    # - CXA_THREAD_USE_TLS: put dtors in pthread to implement cxa_thread_atexit_impl.
354    defines += [ "CXA_THREAD_USE_TSD" ]
355
356    if (musl_arch == "arm") {
357      defines += [ "MUSL_ARM_ARCH" ]
358    }
359    if (musl_arch == "aarch64") {
360      defines += [ "MUSL_AARCH64_ARCH" ]
361    }
362    if (musl_arch == "x86_64") {
363      defines += [ "MUSL_X86_64_ARCH" ]
364    }
365    if (musl_secure_level > 0) {
366      defines += [ "MALLOC_FREELIST_HARDENED" ]
367    }
368    if (musl_secure_level > 1) {
369      defines += [ "MALLOC_FREELIST_QUARANTINE" ]
370    }
371    if (musl_secure_level > 2) {
372      defines += [ "MALLOC_RED_ZONE" ]
373    }
374    if (is_debug || musl_secure_level >= 3) {
375      defines += [ "MALLOC_SECURE_ALL" ]
376    }
377
378    if (musl_iterate_and_stats_api) {
379      defines += [ "MUSL_ITERATE_AND_STATS_API" ]
380    }
381
382    foreach(s, sources_orig) {
383      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
384    }
385
386    cflags = [
387      "-O3",
388      "-fPIC",
389      "-fstack-protector-strong",
390    ]
391
392    if (!(use_libfuzzer || is_mac || is_asan || is_tsan || use_clang_coverage) && __use_flto) {
393      cflags += [ "-flto" ]
394    } else {
395      not_needed([ "__use_flto" ])
396    }
397
398    if (musl_use_jemalloc) {
399      defines += [ "USE_JEMALLOC" ]
400      if (musl_use_jemalloc_dfx_intf) {
401        defines += [ "USE_JEMALLOC_DFX_INTF" ]
402      }
403      if (musl_use_jemalloc_recycle_func) {
404        defines += [ "USE_JEMALLOC_RECYCLE_FUNC" ]
405      }
406      include_dirs = [ "${musl_malloc_plugin}/include/jemalloc" ]
407    }
408
409    if (musl_use_mutex_wait_opt) {
410      defines += [ "USE_MUTEX_WAIT_OPT" ]
411    }
412
413    configs -= musl_inherited_configs
414    configs += [
415      "//build/config/components/musl:soft_musl_config",
416      ":$cur_gwp_asan_config",
417      ":stack_config_musl",
418    ]
419
420    if (use_hwasan) {
421      defines += [ "HWASAN_REMOVE_CLEANUP" ]
422      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
423      cflags += [
424        "-mllvm",
425        "-hwasan-instrument-check-enabled=true",
426      ]
427    }
428
429    if (!defined(defines)) {
430      defines = []
431    }
432    if (musl_target_os == "linux" && product_path != "" &&
433        product_path != rebase_path("//productdefine/common/products")) {
434      _product_config = read_file("${product_path}/config.json", "json")
435      if (defined(_product_config.device_stack_size)) {
436        defines += [ "TARGET_STACK_SIZE=${_product_config.device_stack_size}" ]
437      }
438      if (defined(_product_config.device_guard_size)) {
439        defines += [ "TARGET_GUARD_SIZE=${_product_config.device_guard_size}" ]
440      }
441    }
442
443    deps = porting_deps
444
445    external_deps += freebsd_external_deps
446    if (use_musl && !is_llvm_build) {
447      defines += [ "OHOS_ENABLE_PARAMETER" ]
448      external_deps += param_base_external_deps
449    }
450  }
451
452  source_set("soft_musl_ldso_${target_name}") {
453    sources = []
454    sources_orig = musl_src_ldso
455
456    foreach(s, sources_orig) {
457      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
458    }
459
460    configs -= musl_inherited_configs
461    configs += [
462      "//build/config/components/musl:soft_musl_config",
463      ":$cur_gwp_asan_config",
464      ":stack_config_musl",
465    ]
466    cflags = [
467      "-fPIC",
468      "-ffreestanding",
469      "-fno-stack-protector",
470      "-O3",
471    ]
472
473    if (use_hwasan) {
474      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
475      cflags += [
476        "-mllvm",
477        "-hwasan-instrument-check-enabled=true",
478      ]
479    }
480
481    if (is_asan || is_tsan) {
482      defines = [
483        "NSLIST_DEFAULT_SIZE=1600",
484        "DSOLIST_DEFAULT_SIZE=1600",
485        "INHERIT_DEFAULT_SIZE=1600",
486      ]
487      if (is_asan) {
488        defines += [ "IS_ASAN" ]
489      }
490      if (is_tsan) {
491        defines += [ "IS_TSAN" ]
492      }
493    } else {
494      defines = [
495        "HANDLE_RANDOMIZATION",
496        "LOAD_ORDER_RANDOMIZATION",
497      ]
498    }
499
500    if (musl_arch == "aarch64") {
501      defines += [ "BTI_SUPPORT" ]
502    }
503
504    deps = porting_deps
505    if (use_musl && !is_llvm_build) {
506      defines += [ "OHOS_ENABLE_PARAMETER" ]
507      external_deps = param_base_external_deps
508    }
509
510    if (musl_unit_test_flag) {
511      defines += [
512        "UNIT_TEST_STATIC",
513        "DL_NOMMU_SUPPORT=1",
514      ]
515    }
516  }
517}
518
519template("musl_libs") {
520  no_default_deps = true
521
522  forward_variables_from(invoker, [ "*" ])
523
524  _libs_path_prefix = "."
525  _libs_out_dir = "usr/lib/${musl_target_triple}/${musl_target_multilib}"
526
527  group(target_name) {
528    deps = [
529      ":soft_create_linker",
530      ":soft_libc_musl_shared",
531      ":soft_libc_musl_static",
532      ":soft_musl_crt_libs",
533    ]
534  }
535
536  group("soft_musl_libs") {
537    deps = [
538      ":musl_headers",
539      ":soft_libc_musl_shared",
540      ":soft_libc_musl_static",
541      ":soft_libcrypt",
542      ":soft_libdl",
543      ":soft_libm",
544      ":soft_libpthread",
545      ":soft_libresolv",
546      ":soft_librt",
547      ":soft_libutil",
548      ":soft_libxnet",
549      ":soft_musl_crt_libs",
550    ]
551  }
552
553  group("soft_shared_libs") {
554    deps = [
555      ":musl_headers",
556      ":soft_libc_musl_shared",
557      ":soft_libcrypt",
558      ":soft_libdl",
559      ":soft_libm",
560      ":soft_libpthread",
561      ":soft_libresolv",
562      ":soft_librt",
563      ":soft_libutil",
564      ":soft_libxnet",
565      ":soft_musl_crt_libs",
566    ]
567  }
568
569  group("soft_static_libs") {
570    deps = [
571      ":musl_headers",
572      ":soft_libc_musl_static",
573      ":soft_libcrypt",
574      ":soft_libdl",
575      ":soft_libm",
576      ":soft_libpthread",
577      ":soft_libresolv",
578      ":soft_librt",
579      ":soft_libutil",
580      ":soft_libxnet",
581      ":soft_musl_crt_libs",
582    ]
583  }
584
585  group("soft_musl_crt_libs") {
586    deps = [ ":soft_musl_crt_install_action" ]
587  }
588
589  config("soft_jemalloc") {
590    configs = [ "//build/config/coverage:default_coverage" ]
591
592    include_dirs = [
593      "${target_out_dir}/${musl_ported_dir}/arch/${musl_arch}",
594      "${target_out_dir}/${musl_ported_dir}/arch/generic",
595      "${target_out_dir}/${musl_ported_dir}/src/internal",
596      "${target_out_dir}/${musl_ported_dir}/src/include",
597      "${target_out_dir}/${musl_ported_dir}/include",
598      "${target_out_dir}/${musl_inc_out_dir}",
599      "${clang_base_path}/lib/clang/${clang_version}/include",
600    ]
601
602    cflags = [
603      "--target=${musl_target_triple}",
604      "-D_GNU_SOURCE",
605      "-D_REENTRANT",
606      "-Wall",
607      "-Wshorten-64-to-32",
608      "-Wsign-compare",
609      "-Wundef",
610      "-Wno-format-zero-length",
611      "-pipe",
612      "-g3",
613      "-fvisibility=hidden",
614      "-O3",
615      "-funroll-loops",
616
617      # The following flags are for avoiding errors when compiling.
618      "-Wno-unused-parameter",
619      "-Wno-unused-function",
620      "-Wno-missing-field-initializers",
621      "-Wno-compare-distinct-pointer-types",
622      "-Wno-excess-initializers",
623      "-Wno-extra-tokens",
624      "-Wno-format",
625      "-Wno-implicit-function-declaration",
626      "-Wno-incompatible-pointer-types",
627      "-Wno-macro-redefined",
628      "-Wno-many-braces-around-scalar-init",
629      "-Wno-missing-braces",
630      "-Wno-unknown-pragmas",
631      "-Wno-unused-label",
632      "-Wno-unused-variable",
633      "-Wno-shorten-64-to-32",
634      "-Wno-sign-compare",
635      "-Wno-string-conversion",
636
637      "-U_FORTIFY_SOURCE",
638
639      "-DOHOS_ENABLE_TCACHE",  # For jemalloc 5.X
640      "-DJEMALLOC_TCACHE",  # For jemalloc 4.X
641      "-DOHOS_LG_TCACHE_MAXCLASS_DEFAULT=16",
642      "-DOHOS_NUM_ARENAS=2",  # For jemalloc 5.X
643      "-DOHOS_MAX_ARENAS=2",  # For jemalloc 4.X
644      "-DOHOS_TCACHE_NSLOTS_SMALL_MAX=8",
645      "-DOHOS_TCACHE_NSLOTS_LARGE=16",
646    ]
647
648    if (use_hwasan) {
649      cflags += [
650        "-mllvm",
651        "-hwasan-instrument-check-enabled=true",
652      ]
653    }
654
655    if (is_llvm_build) {
656      cflags += [ "-fno-unwind-tables" ]
657    } else {
658      cflags += [ "-funwind-tables" ]
659    }
660
661    if (is_debug || musl_secure_level > 1) {
662      cflags += [ "-DOHOS_TCACHE_NSLOTS_RANDOM" ]
663    }
664
665    if (musl_arch == "arm") {
666      cflags += [
667        "-march=armv7-a",
668        "-DOHOS_LG_CHUNK_DEFAULT=19",  # For jemalloc 4.X
669      ]
670    } else if (musl_arch == "aarch64") {
671      cflags += [
672        "-march=armv8",
673        "-DOHOS_LG_CHUNK_DEFAULT=19",  # For jemalloc 4.X
674      ]
675    } else if (musl_arch == "x86_64") {
676      cflags += [ "-march=x86-64" ]
677    } else if (musl_arch == "mips") {
678      cflags += [ "-march=mips32r2" ]
679    } else if (musl_arch == "riscv64") {
680      cflags += [ "-march=rv64gc" ]
681    }
682
683    include_dirs += [
684      "//third_party/musl/src/include/",
685      "${musl_malloc_plugin}/include/",
686      "${musl_malloc_plugin}/include/jemalloc/internal",
687      "${musl_malloc_plugin}/include/jemalloc",
688    ]
689  }
690
691  source_set("soft_musl_crt") {
692    sources = [
693      "${target_out_dir}/${musl_ported_dir}/crt/Scrt1.c",
694      "${target_out_dir}/${musl_ported_dir}/crt/crt1.c",
695      "${target_out_dir}/${musl_ported_dir}/crt/crtplus.c",
696      "${target_out_dir}/${musl_ported_dir}/crt/rcrt1.c",
697    ]
698    if (musl_arch == "riscv64") {
699      sources += [
700        "${target_out_dir}/${musl_ported_dir}/crt/crti.c",
701        "${target_out_dir}/${musl_ported_dir}/crt/crtn.c",
702      ]
703    } else if (musl_arch == "loongarch64") {
704      sources += [
705        "${target_out_dir}/${musl_ported_dir}/crt/crti.c",
706        "${target_out_dir}/${musl_ported_dir}/crt/crtn.c",
707      ]
708    } else {
709      sources += [
710        "${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/crti.s",
711        "${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/crtn.s",
712      ]
713    }
714
715    defines = [ "CRT" ]
716
717    configs -= musl_inherited_configs
718    configs += [
719      "//build/config/components/musl:soft_musl_config",
720      ":gwp_asan_config_shared",
721      ":stack_config_musl",
722    ]
723    cflags = [
724      "-fPIC",
725      "-ffreestanding",
726      "-fno-stack-protector",
727    ]
728    if (musl_arch == "aarch64") {
729      cflags += [
730        "-mbranch-protection=bti",
731        "-mmark-bti-property",
732      ]
733    }
734
735    deps = porting_deps
736
737    asmflags = cflags
738  }
739
740  source_set("soft_musl_src_strncpy") {
741    sources = []
742    sources += [ "third_party/openbsd/lib/libc/string/strncpy.c" ]
743    configs -= musl_inherited_configs
744    configs += [
745      "//build/config/components/musl:soft_musl_config",
746      ":gwp_asan_config_shared",
747      ":stack_config_musl",
748    ]
749    cflags = [
750      "-O2",
751      "-fPIC",
752      "-fstack-protector-strong",
753    ]
754
755    if (use_hwasan) {
756      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
757      cflags += [
758        "-mllvm",
759        "-hwasan-instrument-check-enabled=true",
760      ]
761    }
762
763    deps = porting_deps
764  }
765
766  source_set("soft_musl_src_optimize") {
767    sources = []
768    sources_orig = []
769
770    if (musl_arch == "aarch64") {
771      sources_orig += [
772        "src/math/cos.c",
773        "src/math/exp.c",
774        "src/math/exp2.c",
775        "src/math/exp2f.c",
776        "src/math/expf.c",
777        "src/math/log.c",
778        "src/math/log10.c",
779        "src/math/log2.c",
780        "src/math/log2f.c",
781        "src/math/logb.c",
782        "src/math/logf.c",
783        "src/math/sin.c",
784        "src/math/sincos.c",
785        "src/math/pow.c",
786        "src/math/powf.c",
787      ]
788
789      if (is_llvm_build) {
790        sources_orig += [
791          "src/math/cosf.c",
792          "src/math/sincosf.c",
793          "src/math/sinf.c",
794        ]
795      }
796    }
797
798    foreach(s, sources_orig) {
799      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
800    }
801
802    configs -= musl_inherited_configs
803    configs += [
804      "//build/config/components/musl:soft_musl_config",
805      ":gwp_asan_config_shared",
806      ":stack_config_musl",
807    ]
808    cflags = [
809      "-mllvm",
810      "-instcombine-max-iterations=0",
811      "-ffp-contract=fast",
812      "-O3",
813      "-fPIC",
814      "-fstack-protector-strong",
815    ]
816
817    if (use_hwasan) {
818      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
819      cflags += [
820        "-mllvm",
821        "-hwasan-instrument-check-enabled=true",
822      ]
823    }
824
825    deps = porting_deps
826  }
827
828  source_set("soft_musl_src_nossp") {
829    sources = []
830    sources_orig = [
831      "src/string/mempcpy.c",
832      "src/env/__init_tls.c",
833      "src/env/__libc_start_main.c",
834      "src/env/__stack_chk_fail.c",
835    ]
836
837    defines = []
838    if (musl_iterate_and_stats_api) {
839      defines += [ "MUSL_ITERATE_AND_STATS_API" ]
840    }
841
842    if (musl_arch == "arm") {
843      sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.c" ]
844    } else if (musl_arch == "aarch64") {
845      sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.s" ]
846    } else if (musl_arch == "x86_64") {
847      sources_orig += [
848        "src/thread/${musl_arch}/__set_thread_area.s",
849        "src/string/memset.c",
850      ]
851    } else if (musl_arch == "mips") {
852      sources_orig += [ "src/string/memset.c" ]
853    } else if (musl_arch == "riscv64") {
854      sources_orig += [
855        "src/thread/${musl_arch}/__set_thread_area.s",
856        "src/string/memset.c",
857      ]
858    } else if (musl_arch == "loongarch64") {
859      sources_orig += [
860        "src/thread/${musl_arch}/__set_thread_area.s",
861        "src/string/memset.c",
862      ]
863    }
864
865    foreach(s, sources_orig) {
866      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
867    }
868
869    configs -= musl_inherited_configs
870    configs += [
871      "//build/config/components/musl:soft_musl_config",
872      ":gwp_asan_config_shared",
873      ":stack_config_musl",
874    ]
875    cflags = [
876      "-O3",
877      "-fPIC",
878      "-ffreestanding",
879      "-fno-stack-protector",
880    ]
881
882    if (use_hwasan) {
883      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
884      cflags += [
885        "-mllvm",
886        "-hwasan-instrument-check-enabled=true",
887      ]
888    }
889
890    deps = porting_deps
891  }
892
893  source_set("soft_musl_jemalloc") {
894    sources = [ "${musl_malloc_plugin}/src/static.c" ]
895
896    defines = []
897
898    deps = [
899      "//third_party/musl:create_alltypes_h",
900      "//third_party/musl:create_porting_src",
901      "//third_party/musl:create_syscall_h",
902      "//third_party/musl:create_version_h",
903      "//third_party/musl:musl_copy_inc_bits",
904      "//third_party/musl:musl_copy_inc_root",
905      "//third_party/musl:musl_copy_inc_sys",
906    ]
907
908    if (use_musl && !is_llvm_build) {
909      defines += [ "OHOS_ENABLE_PARAMETER" ]
910      external_deps = [ "init:parameterbase" ]
911    }
912
913    if (musl_guard_jemalloc_tsd || is_asan || use_hwasan) {
914      defines += [ "GUARD_JEMALLOC_TSD" ]
915    }
916
917    configs -= musl_inherited_configs
918
919    configs += [
920      ":soft_jemalloc",
921      ":stack_config_musl",
922    ]
923  }
924
925  source_set("soft_musl_src_ld128") {
926    sources = []
927    if (musl_arch == "x86_64") {
928      sources += [
929        "src/math/ceill.c",
930        "src/math/exp2l.c",
931        "src/math/fabsl.c",
932        "src/math/floorl.c",
933        "src/math/sqrtl.c",
934        "src/math/truncl.c",
935      ]
936    }
937
938    configs -= musl_inherited_configs
939    configs += [
940      "//build/config/components/musl:soft_musl_config",
941      ":gwp_asan_config_shared",
942      ":stack_config_musl",
943    ]
944    cflags = [
945      "-mllvm",
946      "-instcombine-max-iterations=0",
947      "-ffp-contract=fast",
948      "-O3",
949      "-fPIC",
950      "-fstack-protector-strong",
951    ]
952
953    if (use_hwasan) {
954      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
955      cflags += [
956        "-mllvm",
957        "-hwasan-instrument-check-enabled=true",
958      ]
959    }
960
961    deps = porting_deps
962    external_deps = [ "FreeBSD:ld128_static" ]
963  }
964
965  static_library("soft_libc_musl_static") {
966    output_name = "libc"
967    complete_static_lib = true
968
969    configs -= musl_inherited_configs
970
971    output_dir = "${target_out_dir}/${_libs_out_dir}"
972    deps = [
973      ":soft_musl_crt_install_action",
974      ":soft_musl_hook_static",
975      ":soft_musl_src_nossp",
976      ":soft_musl_src_optimize",
977      ":soft_musl_src_static",
978      ":soft_musl_src_strncpy",
979    ]
980    external_deps = []
981    if (use_musl && !is_llvm_build) {
982      external_deps += [ "init:parameterbase_noflto" ]
983    }
984    if (!is_llvm_build) {
985      external_deps += [ "optimized_routines:optimized_static" ]
986    }
987
988    if (musl_arch == "aarch64" && !is_llvm_build) {
989      external_deps += [ "optimized_routines:optimize_math" ]
990    }
991    if (musl_use_jemalloc) {
992      deps += [ ":soft_musl_jemalloc" ]
993    }
994
995    if (musl_ld128_flag) {
996      deps += [ ":soft_musl_src_ld128" ]
997    }
998  }
999
1000  static_library("soft_musl_ldso_static_library") {
1001    output_name = "libc_ldso"
1002    complete_static_lib = true
1003
1004    configs -= musl_inherited_configs
1005
1006    output_dir = "${target_out_dir}/${_libs_out_dir}"
1007    deps = [ ":soft_musl_ldso_static" ]
1008  }
1009  static_library("soft_libm") {
1010    complete_static_lib = true
1011    configs -= musl_inherited_configs
1012    output_name = "libm"
1013    output_dir = "${target_out_dir}/${_libs_out_dir}"
1014  }
1015
1016  static_library("soft_librt") {
1017    complete_static_lib = true
1018    configs -= musl_inherited_configs
1019    output_name = "librt"
1020    output_dir = "${target_out_dir}/${_libs_out_dir}"
1021  }
1022
1023  static_library("soft_libpthread") {
1024    complete_static_lib = true
1025    configs -= musl_inherited_configs
1026    configs += [ ":stack_config_musl" ]
1027    output_name = "libpthread"
1028    output_dir = "${target_out_dir}/${_libs_out_dir}"
1029  }
1030
1031  static_library("soft_libcrypt") {
1032    complete_static_lib = true
1033    configs -= musl_inherited_configs
1034    output_name = "libcrypt"
1035    output_dir = "${target_out_dir}/${_libs_out_dir}"
1036  }
1037
1038  static_library("soft_libutil") {
1039    complete_static_lib = true
1040    configs -= musl_inherited_configs
1041    output_name = "libutil"
1042    output_dir = "${target_out_dir}/${_libs_out_dir}"
1043  }
1044
1045  static_library("soft_libresolv") {
1046    complete_static_lib = true
1047    configs -= musl_inherited_configs
1048    output_name = "libresolv"
1049    output_dir = "${target_out_dir}/${_libs_out_dir}"
1050  }
1051
1052  static_library("soft_libxnet") {
1053    complete_static_lib = true
1054    configs -= musl_inherited_configs
1055    output_name = "libxnet"
1056    output_dir = "${target_out_dir}/${_libs_out_dir}"
1057  }
1058
1059  static_library("soft_libdl") {
1060    complete_static_lib = true
1061    configs -= musl_inherited_configs
1062    configs += [ ":stack_config_musl" ]
1063    output_name = "libdl"
1064    output_dir = "${target_out_dir}/${_libs_out_dir}"
1065  }
1066
1067  shared_library("soft_libc_musl_shared") {
1068    output_dir = "${target_out_dir}/${_libs_out_dir}"
1069    output_name = "libc"
1070    output_extension = "so"
1071
1072    if (musl_is_legacy) {
1073      libclang_rt_path = rebase_path(
1074              "${runtime_lib_path}/${musl_target_triple}/nanlegacy/${_libs_path_prefix}")
1075    } else {
1076      libclang_rt_path = rebase_path(
1077              "${runtime_lib_path}/${musl_target_triple}/${_libs_path_prefix}")
1078    }
1079
1080    musl_lib_path = rebase_path("${target_out_dir}/${_libs_out_dir}")
1081
1082    ldflags = []
1083    if (!musl_unit_test_flag) {
1084      libc_map_path =
1085          rebase_path("${target_out_dir}/${musl_ported_dir}/libc.map.txt")
1086      ldflags += [ "-Wl,--version-script=${libc_map_path}" ]
1087    }
1088
1089    ldflags += [
1090      "-nostdlib",
1091      "-Wl,-e,_dlstart",
1092      "-L${libclang_rt_path}",
1093      "-lclang_rt.builtins",
1094      "-L${musl_lib_path}",
1095      "-ldl",
1096      "-lpthread",
1097    ]
1098
1099    if (!is_llvm_build) {
1100      ldflags += [ "-lunwind" ]
1101    }
1102
1103    configs -= musl_inherited_configs
1104    configs -= [ "//build/config:default_libs" ]
1105    configs += [
1106      "//build/config/components/musl:soft_musl_config",
1107      ":gwp_asan_config_shared",
1108    ]
1109
1110    deps = [
1111      ":soft_libdl",
1112      ":soft_libpthread",
1113      ":soft_musl_crt_install_action",
1114      ":soft_musl_hook_shared",
1115      ":soft_musl_ldso_shared",
1116      ":soft_musl_src_nossp",
1117      ":soft_musl_src_optimize",
1118      ":soft_musl_src_shared",
1119      ":soft_musl_src_strncpy",
1120    ]
1121
1122    external_deps = []
1123    if (use_musl && !is_llvm_build) {
1124      external_deps += [ "init:parameterbase" ]
1125    }
1126
1127    if (!is_llvm_build) {
1128      external_deps += [ "optimized_routines:optimized_static" ]
1129    }
1130
1131    if (musl_arch == "aarch64" && !is_llvm_build) {
1132      external_deps += [ "optimized_routines:optimize_math" ]
1133    }
1134
1135    if (musl_use_jemalloc) {
1136      deps += [ ":soft_musl_jemalloc" ]
1137    }
1138
1139    if (musl_ld128_flag) {
1140      deps += [ ":soft_musl_src_ld128" ]
1141    }
1142
1143    configs += [ ":stack_config_musl" ]
1144  }
1145
1146  action_foreach("soft_musl_crt_install_action") {
1147    redir = "${root_out_dir}/obj"
1148    script = "${target_out_dir}/${musl_ported_dir}/scripts/install.py"
1149    sources = [
1150      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/Scrt1.o",
1151      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crt1.o",
1152      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/rcrt1.o",
1153    ]
1154
1155    if (musl_arch == "riscv64") {
1156      sources += [
1157        "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crti.o",
1158        "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crtn.o",
1159      ]
1160    } else if (musl_arch == "loongarch64") {
1161      sources += [
1162        "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crti.o",
1163        "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crtn.o",
1164      ]
1165    } else {
1166      sources += [
1167        "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/soft_musl_crt/crti.o",
1168        "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/soft_musl_crt/crtn.o",
1169      ]
1170    }
1171
1172    outputs = [ "${root_build_dir}/obj/third_party/musl/${_libs_out_dir}/{{source_file_part}}" ]
1173
1174    ldpath = []
1175    if (is_mac) {
1176      ldpath += [ "${clang_base_path}/bin/ld64.lld" ]
1177    } else if (is_win) {
1178      ldpath += [ "${clang_base_path}/bin/lld-link.exe" ]
1179    } else {
1180      ldpath += [ "${clang_base_path}/bin/ld.lld" ]
1181    }
1182
1183    args = [
1184      "--input",
1185      "{{source}}",
1186    ]
1187    args += [ "--output" ] + rebase_path(outputs, root_build_dir)
1188    args += [ "--ldpath" ] + rebase_path(ldpath, root_build_dir)
1189    args += [ "--crtplus" ] + rebase_path(
1190                [ "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crtplus.o" ],
1191                root_build_dir)
1192
1193    deps = [ ":soft_musl_crt" ]
1194  }
1195
1196  copy("soft_create_linker") {
1197    deps = [ ":soft_libc_musl_shared" ]
1198
1199    # _libc_shared_outputs = get_target_outputs(":soft_libc_musl_shared")
1200    _libc_shared_so = "${target_out_dir}/${_libs_out_dir}/libc.so"
1201
1202    # Note that build framework puts the "tsan" lib in the "asan" directory(like /system/asan/lib64).
1203    # We need to take advantage of the ability of the asan version of libc to find the lib from the asan directory.
1204    sources = [ _libc_shared_so ]
1205    if (is_asan || is_tsan) {
1206      asan = "-asan"
1207    } else {
1208      asan = ""
1209    }
1210    _muls_linker_so = "${root_out_dir}/common/common/libc/${_libs_path_prefix}/ld-musl-${musl_arch}${asan}.so.1"
1211    outputs = [ _muls_linker_so ]
1212  }
1213}
1214