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