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