1# Copyright (c) 2023 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14import("//third_party/FreeBSD/FreeBSD.gni")
15import("//third_party/optimized-routines/optimized-routines.gni")
16import("$build_root/ark.gni")
17import("$build_root/config/clang/clang.gni")
18import("$build_root/third_party_gn/musl/musl_src.gni")
19
20_libs_path_prefix = "."
21if (is_asan) {
22  asan = "-asan"
23} else {
24  asan = ""
25}
26musl_linker_so_out_dir =
27    "${root_out_dir}/common/common/libc/${_libs_path_prefix}"
28musl_linker_so_out_path =
29    "${musl_linker_so_out_dir}/ld-musl-${musl_arch}${asan}.so.1"
30musl_linker_so_out_dir_for_qemu = "${root_out_dir}/${so_dir_for_qemu}/lib"
31musl_linker_so_out_path_for_qemu =
32    "${musl_linker_so_out_dir_for_qemu}/ld-musl-${musl_arch}${asan}.so.1"
33
34template("musl_libs") {
35  no_default_deps = true
36
37  forward_variables_from(invoker, [ "*" ])
38  musl_ported_root = "${target_out_dir}/${musl_ported_dir}"
39  _libs_out_dir = "usr/lib/${musl_target_triple}"
40
41  porting_deps = [
42    "$build_root/third_party_gn/musl:create_alltypes_h_file",
43    "$build_root/third_party_gn/musl:create_porting_src",
44    "$build_root/third_party_gn/musl:create_syscall_h_file",
45    "$build_root/third_party_gn/musl:create_version_h_file",
46  ]
47
48  group(target_name) {
49    deps = [
50      ":soft_create_linker",
51      ":soft_libc_musl_shared",
52      ":soft_libc_musl_static",
53      ":soft_musl_crt_libs",
54    ]
55  }
56
57  group("soft_shared_libs") {
58    deps = [
59      ":musl_headers",
60      ":soft_libc_musl_shared",
61      ":soft_libcrypt",
62      ":soft_libdl",
63      ":soft_libm",
64      ":soft_libpthread",
65      ":soft_libresolv",
66      ":soft_librt",
67      ":soft_libutil",
68      ":soft_libxnet",
69      ":soft_musl_crt_libs",
70    ]
71  }
72
73  group("soft_static_libs") {
74    deps = [
75      ":musl_headers",
76      ":soft_libc_musl_static",
77      ":soft_libcrypt",
78      ":soft_libdl",
79      ":soft_libm",
80      ":soft_libpthread",
81      ":soft_libresolv",
82      ":soft_librt",
83      ":soft_libutil",
84      ":soft_libxnet",
85      ":soft_musl_crt_libs",
86    ]
87  }
88
89  group("soft_musl_crt_libs") {
90    deps = [ ":soft_musl_crt_install_action" ]
91  }
92
93  # part of default_compiler_configs from build/config/BUILDCONFIG.gn
94  musl_inherited_configs = [
95    "$build_root/config/compiler:compiler",
96    "$build_root/config/compiler:ark_code",
97    "$build_root/config/compiler:default_include_dirs",
98    "$build_root/config/compiler:default_optimization",
99    "$build_root/config/compiler:runtime_config",
100  ]
101
102  config("soft_musl_config") {
103    configs = [ "$build_root/config/compiler:compiler_cpu_abi" ]
104
105    include_dirs = [
106      "$musl_ported_root/arch/generic",
107      "$musl_ported_root/arch/${musl_arch}",
108      "$musl_ported_root/src/include",
109      "$musl_ported_root/src/internal",
110      "$musl_ported_root/include",
111      "${target_out_dir}/${musl_inc_out_dir}",
112    ]
113
114    cflags_basic = [
115      "-Wl,-z,relro,-z,now,-z,noexecstack",
116      "-Wall",
117      "--target=${musl_target_triple}",
118    ]
119
120    if (musl_arch == "arm") {
121      cflags_basic += [ "-mtp=cp15" ]
122    } else if (musl_arch == "aarch64") {
123    }
124
125    cflags_auto = [
126      "-Qunused-arguments",
127      "-Werror=pointer-arith",
128      "-Werror=pointer-sign",
129      "-Werror=implicit-int",
130      "-fno-omit-frame-pointer",
131      "-Werror=implicit-function-declaration",
132      "-fdata-sections",
133      "-ffunction-sections",
134      "-fasynchronous-unwind-tables",
135      "-funwind-tables",
136      "-pipe",
137      "-g",
138      "-D_XOPEN_SOURCE=700",
139      "-Wno-int-conversion",
140    ]
141
142    if (!is_asan) {
143      cflags_auto += [
144        "-DHOOK_ENABLE",
145        "-DOHOS_SOCKET_HOOK_ENABLE",
146      ]
147    }
148
149    cflags_auto += [ "-DRESERVE_SIGNAL_STACK" ]
150    cflags_auto += [ "-DDFX_SIGNAL_LIBC" ]
151
152    cflags_c99fse = [
153      "-std=c99",
154      "-nostdinc",
155      "-Wa,--noexecstack",
156    ]
157
158    cflags_all = cflags_basic + cflags_c99fse + cflags_auto
159
160    cflags = cflags_all
161
162    defines = [ "BROKEN_VFP_ASM" ]
163    defines += [ "FEATURE_ATEXIT_CB_PROTECT" ]
164
165    if (is_standard_system) {
166      defines += [
167        "OHOS_DNS_PROXY_BY_NETSYS=1",
168        "OHOS_PERMISSION_INTERNET=1",
169        "OHOS_DISABLE_IPV6=0",
170      ]
171    }
172
173    if (!is_standard_system && defined(musl_enable_musl_log)) {
174      if (musl_enable_musl_log) {
175        defines += [ "ENABLE_MUSL_LOG" ]
176      }
177    }
178
179    dynamic_list = rebase_path("$musl_ported_root/dynamic.list")
180
181    ldflags = cflags_all
182    ldflags += [
183      "-Wl,--dynamic-list=${dynamic_list}",
184      "-Wl,--exclude-libs=ALL",
185      "-Wl,--no-undefined",
186      "-Wl,--gc-sections",
187      "-Wl,--sort-common",
188      "-Wl,--sort-section,alignment",
189      "-fuse-ld=lld",
190      "--target=${musl_target_triple}",
191    ]
192    if (current_cpu == "arm" || current_cpu == "arm64") {
193      ldflags += [ "-Wl,--hash-style=both" ]
194    } else {
195      ldflags += [ "-Wl,--hash-style=sysv" ]
196    }
197    asmflags = cflags
198  }
199
200  config("soft_hook") {
201    defines = []
202    configs = []
203
204    if (is_posix) {
205      configs += [ "$build_root/config/posix:runtime_config" ]
206    }
207
208    cflags_cc = []
209
210    defines = [
211      "__GNU_SOURCE=1",  # Necessary for clone().
212      "CHROMIUM_CXX_TWEAK_INLINES",  # Saves binary size.
213    ]
214
215    defines += [
216      "__MUSL__",
217      "_LIBCPP_HAS_MUSL_LIBC",
218      "__BUILD_LINUX_WITH_CLANG",
219    ]
220
221    if (!is_asan) {
222      defines += [
223        "HOOK_ENABLE",
224        "OHOS_SOCKET_HOOK_ENABLE",
225      ]
226    }
227    ldflags = [ "-nostdlib" ]
228
229    libs = []
230
231    if (is_component_build) {
232      defines += [ "COMPONENT_BUILD" ]
233    }
234  }
235
236  config("soft_jemalloc") {
237    configs = []
238
239    include_dirs = [
240      "$musl_ported_root/arch/${musl_arch}",
241      "$musl_ported_root/arch/generic",
242      "$musl_ported_root/src/internal",
243      "$musl_ported_root/src/include",
244      "$musl_ported_root/include",
245      "${target_out_dir}/${musl_inc_out_dir}",
246      "${clang_base_path}/lib/clang/${clang_version}/include",
247    ]
248
249    cflags = [
250      "--target=${musl_target_triple}",
251      "-D_GNU_SOURCE",
252      "-D_REENTRANT",
253      "-Wall",
254      "-Wshorten-64-to-32",
255      "-Wsign-compare",
256      "-Wundef",
257      "-Wno-format-zero-length",
258      "-pipe",
259      "-g3",
260      "-fvisibility=hidden",
261      "-O3",
262      "-funroll-loops",
263      "-funwind-tables",
264
265      # The following flags are for avoiding errors when compiling.
266      "-Wno-unused-parameter",
267      "-Wno-unused-function",
268      "-Wno-missing-field-initializers",
269
270      "-U_FORTIFY_SOURCE",
271
272      "-DOHOS_ENABLE_TCACHE",  # For jemalloc 5.X
273      "-DJEMALLOC_TCACHE",  # For jemalloc 4.X
274      "-DOHOS_LG_TCACHE_MAXCLASS_DEFAULT=16",
275      "-DOHOS_NUM_ARENAS=2",  # For jemalloc 5.X
276      "-DOHOS_MAX_ARENAS=2",  # For jemalloc 4.X
277      "-DOHOS_TCACHE_NSLOTS_SMALL_MAX=8",
278      "-DOHOS_TCACHE_NSLOTS_LARGE=16",
279    ]
280
281    if (is_debug || musl_secure_level > 1) {
282      cflags += [ "-DOHOS_TCACHE_NSLOTS_RANDOM" ]
283    }
284
285    if (musl_arch == "arm") {
286      cflags += [
287        "-march=armv7-a",
288        "-DOHOS_LG_CHUNK_DEFAULT=19",  # For jemalloc 4.X
289      ]
290    } else if (musl_arch == "aarch64") {
291      cflags += [
292        "-march=armv8",
293        "-DOHOS_LG_CHUNK_DEFAULT=19",  # For jemalloc 4.X
294      ]
295    } else if (musl_arch == "x86_64") {
296      cflags += [ "-march=x86-64" ]
297    }
298
299    include_dirs += [
300      "//third_party",
301      "//third_party/musl/src/include/",
302      "//third_party/jemalloc/include/",
303      "//third_party/jemalloc/include/jemalloc/internal",
304      "//third_party/jemalloc/include/jemalloc",
305      "//third_party/FreeBSD/contrib/libexecinfo",
306    ]
307  }
308
309  source_set("soft_musl_crt") {
310    sources = [
311      "$musl_ported_root/crt/${musl_arch}/crti.s",
312      "$musl_ported_root/crt/${musl_arch}/crtn.s",
313      "$musl_ported_root/crt/Scrt1.c",
314      "$musl_ported_root/crt/crt1.c",
315      "$musl_ported_root/crt/crtplus.c",
316      "$musl_ported_root/crt/rcrt1.c",
317    ]
318
319    defines = [ "CRT" ]
320
321    configs -= musl_inherited_configs
322    configs += [ ":soft_musl_config" ]
323    cflags = [
324      "-fPIC",
325      "-fno-stack-protector",
326      "-ffreestanding",
327    ]
328
329    deps = porting_deps
330
331    asmflags = cflags
332  }
333
334  source_set("soft_musl_src") {
335    sources_orig = []
336    sources = []
337    include_dirs = []
338    sources_orig = musl_src_arch_file + musl_src_file
339    sources_orig -= musl_src_filterout
340    sources_orig -= [
341      "src/string/mempcpy.c",
342      "src/string/memset.c",
343      "src/env/__init_tls.c",
344      "src/env/__libc_start_main.c",
345      "src/env/__stack_chk_fail.c",
346      "src/stdlib/qsort.c",
347      "src/stdlib/qsort_nr.c",
348      "src/string/strncpy.c",
349    ]
350
351    freebsd_files = [
352      "//third_party/FreeBSD/contrib/gdtoa/strtod.c",
353      "//third_party/FreeBSD/contrib/gdtoa/gethex.c",
354      "//third_party/FreeBSD/contrib/gdtoa/smisc.c",
355      "//third_party/FreeBSD/contrib/gdtoa/misc.c",
356      "//third_party/FreeBSD/contrib/gdtoa/strtord.c",
357      "//third_party/FreeBSD/contrib/gdtoa/hexnan.c",
358      "//third_party/FreeBSD/contrib/gdtoa/gmisc.c",
359      "//third_party/FreeBSD/contrib/gdtoa/hd_init.c",
360      "//third_party/FreeBSD/contrib/gdtoa/strtodg.c",
361      "//third_party/FreeBSD/contrib/gdtoa/ulp.c",
362      "//third_party/FreeBSD/contrib/gdtoa/strtof.c",
363      "//third_party/FreeBSD/contrib/gdtoa/sum.c",
364      "//third_party/FreeBSD/lib/libc/gdtoa/glue.c",
365      "//third_party/FreeBSD/lib/libc/stdio/parsefloat.c",
366      "//third_party/FreeBSD/contrib/tcp_wrappers/strcasecmp.c",
367      "//third_party/FreeBSD/lib/libc/gen/arc4random.c",
368      "//third_party/FreeBSD/lib/libc/gen/arc4random_uniform.c",
369      "//third_party/FreeBSD/lib/libc/stdlib/qsort.c",
370      "//third_party/FreeBSD/lib/libc/stdlib/strtoimax.c",
371      "//third_party/FreeBSD/lib/libc/stdlib/strtoul.c",
372      "//third_party/FreeBSD/lib/libc/stdlib/strtoumax.c",
373      "//third_party/musl/third_party/openbsd/lib/libc/string/strncpy.c",
374    ]
375
376    if (musl_arch == "arm") {
377      sources_orig -= [
378        "src/thread/${musl_arch}/__set_thread_area.c",
379        "src/string/arm/memcpy.S",
380        "src/string/memchr.c",
381        "src/string/strcmp.c",
382        "src/string/strlen.c",
383        "src/math/sincosf.c",
384        "src/math/expf.c",
385        "src/math/exp2f.c",
386        "src/math/exp2l.c",
387        "src/math/exp2.c",
388        "src/math/log.c",
389        "src/math/logl.c",
390        "src/math/log2.c",
391        "src/math/log2f.c",
392        "src/math/log2l.c",
393        "src/math/logf.c",
394        "src/math/log_data.c",
395        "src/math/logf_data.c",
396        "src/math/log2_data.c",
397        "src/math/log2f_data.c",
398        "src/math/exp2f_data.c",
399        "src/math/pow.c",
400        "src/math/powf.c",
401        "src/math/powl.c",
402        "src/math/sinf.c",
403        "src/math/cosf.c",
404        "src/linux/flock.c",
405      ]
406    } else if (musl_arch == "aarch64") {
407      sources_orig -= [
408        "src/thread/${musl_arch}/__set_thread_area.s",
409        "src/string/memcpy.c",
410        "src/string/memmove.c",
411        "src/string/memchr.c",
412        "src/string/memcmp.c",
413        "src/string/strcpy.c",
414        "src/string/strcmp.c",
415        "src/string/strlen.c",
416        "src/string/stpcpy.c",
417        "src/string/strchr.c",
418        "src/string/strrchr.c",
419        "src/string/strnlen.c",
420        "src/string/strncmp.c",
421        "src/math/sincosf.c",
422        "src/math/sinf.c",
423        "src/math/cosf.c",
424        "src/math/cos.c",
425        "src/math/exp.c",
426        "src/math/exp2.c",
427        "src/math/exp2f.c",
428        "src/math/expf.c",
429        "src/math/log.c",
430        "src/math/log10.c",
431        "src/math/log2.c",
432        "src/math/log2f.c",
433        "src/math/logb.c",
434        "src/math/logf.c",
435        "src/math/sin.c",
436        "src/math/sincos.c",
437        "src/math/pow.c",
438        "src/math/powf.c",
439        "src/linux/flock.c",
440      ]
441    } else if (musl_arch == "x86_64") {
442      sources_orig -= [
443        "src/thread/${musl_arch}/__set_thread_area.s",
444        "src/linux/flock.c",
445      ]
446    }
447
448    defines = []
449    defines += [ "FEATURE_ICU_LOCALE" ]
450
451    # There are two ways to implement cxa_thread_atexit_impl:
452    # - CXA_THREAD_USE_TSD(default): use pthread_key_xxx to implement cxa_thread_atexit_impl.
453    # - CXA_THREAD_USE_TLS: put dtors in pthread to implement cxa_thread_atexit_impl.
454    defines += [ "CXA_THREAD_USE_TSD" ]
455
456    if (musl_arch == "arm") {
457      defines += [ "MUSL_ARM_ARCH" ]
458    }
459    if (musl_arch == "aarch64") {
460      defines += [ "MUSL_AARCH64_ARCH" ]
461    }
462    if (musl_arch == "x86_64") {
463      defines += [ "MUSL_X86_64_ARCH" ]
464    }
465    if (musl_secure_level > 0) {
466      defines += [ "MALLOC_FREELIST_HARDENED" ]
467    }
468    if (musl_secure_level > 1) {
469      defines += [ "MALLOC_FREELIST_QUARANTINE" ]
470    }
471    if (musl_secure_level > 2) {
472      defines += [ "MALLOC_RED_ZONE" ]
473    }
474    if (is_debug || musl_secure_level >= 3) {
475      defines += [ "MALLOC_SECURE_ALL" ]
476    }
477
478    if (musl_iterate_and_stats_api) {
479      defines += [ "MUSL_ITERATE_AND_STATS_API" ]
480    }
481
482    foreach(s, sources_orig) {
483      sources += [ "$musl_ported_root/${s}" ]
484    }
485    if (musl_arch == "arm") {
486      sources += [
487        "$OPTRTDIR/math/cosf.c",
488        "$OPTRTDIR/math/exp2.c",
489        "$OPTRTDIR/math/exp2f.c",
490        "$OPTRTDIR/math/exp2f_data.c",
491        "$OPTRTDIR/math/expf.c",
492        "$OPTRTDIR/math/log.c",
493        "$OPTRTDIR/math/log2.c",
494        "$OPTRTDIR/math/log2_data.c",
495        "$OPTRTDIR/math/log2f.c",
496        "$OPTRTDIR/math/log2f_data.c",
497        "$OPTRTDIR/math/log_data.c",
498        "$OPTRTDIR/math/logf.c",
499        "$OPTRTDIR/math/logf_data.c",
500        "$OPTRTDIR/math/pow.c",
501        "$OPTRTDIR/math/powf.c",
502        "$OPTRTDIR/math/sincosf.c",
503        "$OPTRTDIR/math/sincosf_data.c",
504        "$OPTRTDIR/math/sinf.c",
505        "$OPTRTDIR/string/arm/memchr.S",
506        "$OPTRTDIR/string/arm/memcpy.S",
507        "$OPTRTDIR/string/arm/memset.S",
508        "$OPTRTDIR/string/arm/strcmp.S",
509        "$OPTRTDIR/string/arm/strlen-armv6t2.S",
510      ]
511      sources += freebsd_files
512      asmflags = [
513        "-D__memcpy_arm = memcpy",
514        "-D__memchr_arm = memchr",
515        "-D__memset_arm = memset",
516        "-D__strcmp_arm = strcmp",
517        "-D__strlen_armv6t2 = strlen",
518      ]
519    } else if (musl_arch == "aarch64") {
520      sources += freebsd_files
521      if (defined(ARM_FEATURE_SVE)) {
522        sources += [
523          "$OPTRTDIR/string/aarch64/memchr-sve.S",
524          "$OPTRTDIR/string/aarch64/memcmp-sve.S",
525          "$OPTRTDIR/string/aarch64/memcpy.S",
526          "$OPTRTDIR/string/aarch64/memset.S",
527          "$OPTRTDIR/string/aarch64/stpcpy-sve.S",
528          "$OPTRTDIR/string/aarch64/strchr-sve.S",
529          "$OPTRTDIR/string/aarch64/strchrnul-sve.S",
530          "$OPTRTDIR/string/aarch64/strcmp-sve.S",
531          "$OPTRTDIR/string/aarch64/strcpy-sve.S",
532          "$OPTRTDIR/string/aarch64/strlen-sve.S",
533          "$OPTRTDIR/string/aarch64/strncmp-sve.S",
534          "$OPTRTDIR/string/aarch64/strnlen-sve.S",
535          "$OPTRTDIR/string/aarch64/strrchr-sve.S",
536        ]
537        asmflags = [
538          "-D__memcpy_aarch64 = memcpy",
539          "-D__memset_aarch64 = memset",
540          "-D__memcmp_aarch64_sve = memcmp",
541          "-D__memchr_aarch64_sve = memchr",
542          "-D__strcmp_aarch64_sve = strcmp",
543          "-D__strlen_aarch64_sve = strlen",
544          "-D__strcpy_aarch64_sve = strcpy",
545          "-D__stpcpy_aarch64_sve = stpcpy",
546          "-D__strchr_aarch64_sve = strchr",
547          "-D__strrchr_aarch64_sve = strrchr",
548          "-D__strchrnul_aarch64_sve = strchrnul",
549          "-D__strnlen_aarch64_sve = strnlen",
550          "-D__strncmp_aarch64_sve = strncmp",
551        ]
552      } else if (defined(ARM_FEATURE_MTE)) {
553        sources += [
554          "$OPTRTDIR/string/aarch64/memchr-mte.S",
555          "$OPTRTDIR/string/aarch64/memcmp.S",
556          "$OPTRTDIR/string/aarch64/memcpy.S",
557          "$OPTRTDIR/string/aarch64/memset.S",
558          "$OPTRTDIR/string/aarch64/stpcpy-mte.S",
559          "$OPTRTDIR/string/aarch64/strchr-mte.S",
560          "$OPTRTDIR/string/aarch64/strchrnul-mte.S",
561          "$OPTRTDIR/string/aarch64/strcmp-mte.S",
562          "$OPTRTDIR/string/aarch64/strcpy-mte.S",
563          "$OPTRTDIR/string/aarch64/strlen-mte.S",
564          "$OPTRTDIR/string/aarch64/strncmp-mte.S",
565          "$OPTRTDIR/string/aarch64/strnlen.S",
566          "$OPTRTDIR/string/aarch64/strrchr-mte.S",
567        ]
568        asmflags = [
569          "-D__memcpy_aarch64 = memcpy",
570          "-D__memset_aarch64 = memset",
571          "-D__memcmp_aarch64 = memcmp",
572          "-D__memchr_aarch64_mte = memchr",
573          "-D__strcmp_aarch64_mte = strcmp",
574          "-D__strlen_aarch64_mte = strlen",
575          "-D__strcpy_aarch64_mte = strcpy",
576          "-D__stpcpy_aarch64_mte = stpcpy",
577          "-D__strchr_aarch64_mte = strchr",
578          "-D__strrchr_aarch64_mte = strrchr",
579          "-D__strchrnul_aarch64_mte = strchrnul",
580          "-D__strnlen_aarch64 = strnlen",
581          "-D__strncmp_aarch64_mte = strncmp",
582        ]
583      } else {
584        sources += [
585          "$OPTRTDIR/string/aarch64/memchr.S",
586          "$OPTRTDIR/string/aarch64/memcmp.S",
587          "$OPTRTDIR/string/aarch64/memcpy.S",
588          "$OPTRTDIR/string/aarch64/memset.S",
589          "$OPTRTDIR/string/aarch64/stpcpy.S",
590          "$OPTRTDIR/string/aarch64/strchr.S",
591          "$OPTRTDIR/string/aarch64/strchrnul.S",
592          "$OPTRTDIR/string/aarch64/strcmp.S",
593          "$OPTRTDIR/string/aarch64/strcpy.S",
594          "$OPTRTDIR/string/aarch64/strlen.S",
595          "$OPTRTDIR/string/aarch64/strncmp.S",
596          "$OPTRTDIR/string/aarch64/strnlen.S",
597          "$OPTRTDIR/string/aarch64/strrchr.S",
598        ]
599        asmflags = [
600          "-D__memmove_aarch64 = memmove",
601          "-D__memcpy_aarch64 = memcpy",
602          "-D__memchr_aarch64 = memchr",
603          "-D__memset_aarch64 = memset",
604          "-D__memcmp_aarch64 = memcmp",
605          "-D__strcmp_aarch64 = strcmp",
606          "-D__strlen_aarch64 = strlen",
607          "-D__strcpy_aarch64 = strcpy",
608          "-D__stpcpy_aarch64 = stpcpy",
609          "-D__strchr_aarch64 = strchr",
610          "-D__strrchr_aarch64 = strrchr",
611          "-D__strchrnul_aarch64 = strchrnul",
612          "-D__strnlen_aarch64 = strnlen",
613          "-D__strncmp_aarch64 = strncmp",
614        ]
615      }
616    }
617
618    cflags = [
619      "-O3",
620      "-fPIC",
621      "-fstack-protector-strong",
622    ]
623
624    configs -= musl_inherited_configs
625    configs += [ ":soft_musl_config" ]
626    if (musl_arch == "aarch64") {
627      include_dirs += [ "//third_party/FreeBSD/lib/libc/aarch64" ]
628    } else if (musl_arch == "arm") {
629      include_dirs += [ "//third_party/FreeBSD/lib/libc/arm" ]
630    }
631    include_dirs += [ "//third_party/FreeBSD/lib/libc/include" ]
632    include_dirs += [ "//third_party/FreeBSD/contrib/libexecinfo" ]
633    include_dirs += [ "//third_party/FreeBSD/crypto/openssh/openbsd-compat" ]
634
635    if (!defined(defines)) {
636      defines = []
637    }
638    if (musl_target_os == "linux" && product_path != "" &&
639        product_path != rebase_path("//productdefine/common/products")) {
640      _product_config = read_file("${product_path}/config.json", "json")
641      if (defined(_product_config.device_stack_size)) {
642        defines += [ "TARGET_STACK_SIZE=${_product_config.device_stack_size}" ]
643      }
644      if (defined(_product_config.device_guard_size)) {
645        defines += [ "TARGET_GUARD_SIZE=${_product_config.device_guard_size}" ]
646      }
647    }
648    external_deps = []
649    external_deps += [ "FreeBSD:libc_static" ]
650    deps = porting_deps
651  }
652
653  source_set("soft_musl_src_optimize") {
654    sources = []
655    sources_orig = []
656
657    if (musl_arch == "aarch64") {
658      sources_orig += [
659        "src/math/cos.c",
660        "src/math/exp.c",
661        "src/math/exp2.c",
662        "src/math/exp2f.c",
663        "src/math/expf.c",
664        "src/math/log.c",
665        "src/math/log10.c",
666        "src/math/log2.c",
667        "src/math/log2f.c",
668        "src/math/logb.c",
669        "src/math/logf.c",
670        "src/math/sin.c",
671        "src/math/sincos.c",
672        "src/math/pow.c",
673        "src/math/powf.c",
674      ]
675    }
676
677    foreach(s, sources_orig) {
678      sources += [ "$musl_ported_root/${s}" ]
679    }
680
681    if (musl_arch == "aarch64") {
682      sources += [
683        "$OPTRTDIR/math/cosf.c",
684        "$OPTRTDIR/math/sincosf.c",
685        "$OPTRTDIR/math/sincosf_data.c",
686        "$OPTRTDIR/math/sinf.c",
687      ]
688    }
689
690    configs -= musl_inherited_configs
691    configs += [ ":soft_musl_config" ]
692    cflags = [
693      "-mllvm",
694      "-instcombine-max-iterations=0",
695      "-O3",
696      "-ffp-contract=fast",
697      "-fPIC",
698      "-fstack-protector-strong",
699    ]
700
701    deps = porting_deps
702  }
703
704  source_set("soft_musl_src_nossp") {
705    sources = []
706    sources_orig = [
707      "src/env/__init_tls.c",
708      "src/env/__libc_start_main.c",
709      "src/env/__stack_chk_fail.c",
710      "src/string/mempcpy.c",
711    ]
712
713    defines = []
714    if (musl_iterate_and_stats_api) {
715      defines += [ "MUSL_ITERATE_AND_STATS_API" ]
716    }
717
718    if (musl_arch == "arm") {
719      sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.c" ]
720    } else if (musl_arch == "aarch64") {
721      sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.s" ]
722    } else if (musl_arch == "x86_64") {
723      sources_orig += [
724        "src/string/memset.c",
725        "src/thread/${musl_arch}/__set_thread_area.s",
726      ]
727    }
728
729    foreach(s, sources_orig) {
730      sources += [ "$musl_ported_root/${s}" ]
731    }
732
733    configs -= musl_inherited_configs
734    configs += [ ":soft_musl_config" ]
735    cflags = [
736      "-O3",
737      "-fno-stack-protector",
738      "-ffreestanding",
739      "-fPIC",
740    ]
741
742    deps = porting_deps
743  }
744
745  source_set("soft_musl_ldso") {
746    sources = []
747    sources_orig = musl_src_ldso
748
749    foreach(s, sources_orig) {
750      sources += [ "$musl_ported_root/${s}" ]
751    }
752
753    configs -= musl_inherited_configs
754    configs += [ ":soft_musl_config" ]
755    cflags = [
756      "-fno-stack-protector",
757      "-ffreestanding",
758      "-fPIC",
759      "-O3",
760    ]
761    if (is_asan) {
762      defines = [
763        "NSLIST_DEFAULT_SIZE=1600",
764        "DSOLIST_DEFAULT_SIZE=1600",
765        "INHERIT_DEFAULT_SIZE=1600",
766      ]
767    } else {
768      defines = [
769        "HANDLE_RANDOMIZATION",
770        "LOAD_ORDER_RANDOMIZATION",
771      ]
772    }
773
774    deps = porting_deps
775  }
776
777  source_set("soft_musl_hook") {
778    sources = [
779      "//third_party/musl/porting/linux/user/src/hook/malloc_common.c",
780      "//third_party/musl/porting/linux/user/src/hook/memory_trace.c",
781      "//third_party/musl/porting/linux/user/src/hook/musl_preinit.c",
782      "//third_party/musl/porting/linux/user/src/hook/musl_preinit_common.c",
783      "//third_party/musl/porting/linux/user/src/hook/musl_socket_preinit.c",
784      "//third_party/musl/porting/linux/user/src/hook/musl_socket_preinit_common.c",
785      "//third_party/musl/porting/linux/user/src/hook/socket_common.c",
786    ]
787
788    deps = [
789      "$build_root/third_party_gn/musl:create_alltypes_h_file",
790      "$build_root/third_party_gn/musl:create_porting_src",
791      "$build_root/third_party_gn/musl:create_syscall_h_file",
792      "$build_root/third_party_gn/musl:create_version_h_file",
793      "$build_root/third_party_gn/musl:musl_copy_inc_bits",
794      "$build_root/third_party_gn/musl:musl_copy_inc_fortify",
795      "$build_root/third_party_gn/musl:musl_copy_inc_root",
796      "$build_root/third_party_gn/musl:musl_copy_inc_sys",
797    ]
798
799    configs -= musl_inherited_configs
800
801    configs += [
802      "$build_root/config/compiler:compiler",
803      ":soft_hook",
804    ]
805    cflags = [
806      "-mllvm",
807      "--instcombine-max-iterations=0",
808      "-ffp-contract=fast",
809      "-O3",
810      "-Wno-int-conversion",
811    ]
812  }
813
814  source_set("soft_musl_jemalloc") {
815    sources = [ "./porting/linux/user/src/malloc/jemalloc/jemalloc.c" ]
816
817    deps = [
818      "$build_root/third_party_gn/musl:create_alltypes_h_file",
819      "$build_root/third_party_gn/musl:create_porting_src",
820      "$build_root/third_party_gn/musl:create_syscall_h_file",
821      "$build_root/third_party_gn/musl:create_version_h_file",
822      "$build_root/third_party_gn/musl:musl_copy_inc_bits",
823      "$build_root/third_party_gn/musl:musl_copy_inc_root",
824      "$build_root/third_party_gn/musl:musl_copy_inc_sys",
825    ]
826
827    configs -= musl_inherited_configs
828
829    configs += [ ":soft_jemalloc" ]
830  }
831
832  static_library("soft_libc_musl_static") {
833    configs -= musl_inherited_configs
834
835    output_dir = "${target_out_dir}/${_libs_out_dir}"
836    deps = [
837      ":soft_musl_hook",
838      ":soft_musl_ldso",
839      ":soft_musl_src",
840      ":soft_musl_src_nossp",
841      ":soft_musl_src_optimize",
842    ]
843    external_deps = []
844    external_deps = [ "FreeBSD:libc_static" ]
845    output_name = "libc"
846    complete_static_lib = true
847  }
848
849  static_library("soft_libm") {
850    complete_static_lib = true
851    configs -= musl_inherited_configs
852    output_name = "libm"
853    output_dir = "${target_out_dir}/${_libs_out_dir}"
854  }
855
856  static_library("soft_librt") {
857    complete_static_lib = true
858    configs -= musl_inherited_configs
859    output_name = "librt"
860    output_dir = "${target_out_dir}/${_libs_out_dir}"
861  }
862
863  static_library("soft_libpthread") {
864    complete_static_lib = true
865    configs -= musl_inherited_configs
866    output_name = "libpthread"
867    output_dir = "${target_out_dir}/${_libs_out_dir}"
868  }
869
870  static_library("soft_libcrypt") {
871    complete_static_lib = true
872    configs -= musl_inherited_configs
873    output_name = "libcrypt"
874    output_dir = "${target_out_dir}/${_libs_out_dir}"
875  }
876
877  static_library("soft_libutil") {
878    complete_static_lib = true
879    configs -= musl_inherited_configs
880    output_name = "libutil"
881    output_dir = "${target_out_dir}/${_libs_out_dir}"
882  }
883
884  static_library("soft_libresolv") {
885    complete_static_lib = true
886    configs -= musl_inherited_configs
887    output_name = "libresolv"
888    output_dir = "${target_out_dir}/${_libs_out_dir}"
889  }
890
891  static_library("soft_libxnet") {
892    complete_static_lib = true
893    configs -= musl_inherited_configs
894    output_name = "libxnet"
895    output_dir = "${target_out_dir}/${_libs_out_dir}"
896  }
897
898  static_library("soft_libdl") {
899    complete_static_lib = true
900    configs -= musl_inherited_configs
901    output_name = "libdl"
902    output_dir = "${target_out_dir}/${_libs_out_dir}"
903  }
904
905  shared_library("soft_libc_musl_shared") {
906    deps = []
907    output_dir = "${target_out_dir}/${_libs_out_dir}"
908
909    musl_lib_path = rebase_path("$output_dir")
910
911    libclang_rt_path = rebase_path(
912            "${runtime_lib_path}/${musl_target_triple}/${_libs_path_prefix}")
913
914    libc_map_path = rebase_path("$musl_ported_root/libc.map.txt")
915
916    if (current_cpu == "mipsel") {
917      libs = [ "atomic" ]
918      deps += [ ":mipsel_atomic_linker" ]
919    }
920    ldflags = [
921      "-Wl,--version-script=${libc_map_path}",
922      "-lpthread",
923      "-ldl",
924      "-L${musl_lib_path}",
925      "-lunwind",
926      "-lclang_rt.builtins",
927      "-L${libclang_rt_path}",
928      "-Wl,-e,_dlstart",
929      "-nostdlib",
930    ]
931
932    configs -= [ "$build_root/config:default_libs" ]
933    configs -= musl_inherited_configs
934    configs += [ ":soft_musl_config" ]
935
936    deps += [
937      ":soft_libdl",
938      ":soft_libpthread",
939      ":soft_musl_crt_install_action",
940      ":soft_musl_hook",
941      ":soft_musl_ldso",
942      ":soft_musl_src",
943      ":soft_musl_src_nossp",
944      ":soft_musl_src_optimize",
945    ]
946
947    output_name = "libc"
948    output_extension = "so"
949  }
950
951  action_foreach("soft_musl_crt_install_action") {
952    redir = "${root_out_dir}/obj"
953    script = "$musl_ported_root/scripts/install.py"
954    sources = [
955      "${redir}/$musl_ported_root/crt/${musl_arch}/soft_musl_crt/crti.o",
956      "${redir}/$musl_ported_root/crt/${musl_arch}/soft_musl_crt/crtn.o",
957      "${redir}/$musl_ported_root/crt/soft_musl_crt/Scrt1.o",
958      "${redir}/$musl_ported_root/crt/soft_musl_crt/crt1.o",
959      "${redir}/$musl_ported_root/crt/soft_musl_crt/rcrt1.o",
960    ]
961
962    outputs = [ "${root_build_dir}/obj/arkcompiler/toolchain/build/third_party_gn/musl/${_libs_out_dir}/{{source_file_part}}" ]
963
964    ldpath = []
965    if (is_mac) {
966      ldpath += [ "${clang_base_path}/bin/ld64.lld" ]
967    } else if (is_win) {
968      ldpath += [ "${clang_base_path}/bin/lld-link.exe" ]
969    } else {
970      ldpath += [ "${clang_base_path}/bin/ld.lld" ]
971    }
972
973    args = [
974      "--input",
975      "{{source}}",
976    ]
977    args += [ "--output" ] + rebase_path(outputs, root_build_dir)
978    args += [ "--ldpath" ] + rebase_path(ldpath, root_build_dir)
979    args += [ "--crtplus" ] + rebase_path(
980                [ "${redir}/$musl_ported_root/crt/soft_musl_crt/crtplus.o" ],
981                root_build_dir)
982
983    deps = [ ":soft_musl_crt" ]
984  }
985
986  copy("soft_create_linker") {
987    deps = [ ":soft_libc_musl_shared" ]
988
989    # _libc_shared_outputs = get_target_outputs(":soft_libc_musl_shared")
990    _libc_shared_so = "${target_out_dir}/${_libs_out_dir}/libc.so"
991    sources = [ _libc_shared_so ]
992    outputs = [ musl_linker_so_out_path ]
993  }
994
995  copy("soft_create_linker_for_qemu") {
996    deps = [ ":soft_create_linker" ]
997
998    sources = [ musl_linker_so_out_path ]
999    outputs = [ musl_linker_so_out_path_for_qemu ]
1000  }
1001
1002  copy("mipsel_atomic_linker") {
1003    # need install libatomic1-mipsel-cross first
1004    atomic_shared_so = "/usr/mipsel-linux-gnu/lib/libatomic.so.1.2.0"
1005    sources = [ atomic_shared_so ]
1006
1007    atomic_linker_so = "${target_out_dir}/${_libs_out_dir}/libatomic.so"
1008    outputs = [ atomic_linker_so ]
1009  }
1010}
1011