• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without modification,
4# are permitted provided that the following conditions are met:
5#
6# 1. Redistributions of source code must retain the above copyright notice, this list of
7#    conditions and the following disclaimer.
8#
9# 2. Redistributions in binary form must reproduce the above copyright notice, this list
10#    of conditions and the following disclaimer in the documentation and/or other materials
11#    provided with the distribution.
12#
13# 3. Neither the name of the copyright holder nor the names of its contributors may be used
14#    to endorse or promote products derived from this software without specific prior written
15#    permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import("//build/config/clang/clang.gni")
30import("//build/ohos.gni")
31import("optimized-routines.gni")
32
33config("optimized_routines_config") {
34  include_dirs = [
35    "//third_party/optimized-routines/math",
36    "//third_party/optimized-routines/math/include",
37  ]
38}
39
40config("optimized_routines_stack_config") {
41  cflags = []
42  cflags_c = []
43
44  if (musl_arch == "aarch64") {
45    cflags += [ "-mbranch-protection=pac-ret+b-key" ]
46    cflags_c += [ "-fno-stack-protector" ]
47  }
48}
49
50if (!(is_lite_system && current_os == "ohos")) {
51  static_library("optimized_static") {
52    output_name = "liboptimize"
53
54    public_configs = [ ":optimized_routines_config" ]
55    configs += [ ":optimized_routines_stack_config" ]
56
57    sources = []
58    defines = []
59    include_dirs = [ "//third_party/optimized-routines/string/include" ]
60
61    if (musl_arch == "arm") {
62      if (is_llvm_build) {
63        defines += [ "__IS_LLVM_BUILD" ]
64      }
65      sources += [
66        "math/cosf.c",
67        "math/exp2.c",
68        "math/exp2f.c",
69        "math/exp2f_data.c",
70        "math/exp_data.c",
71        "math/expf.c",
72        "math/log.c",
73        "math/log2.c",
74        "math/log2_data.c",
75        "math/log2f.c",
76        "math/log2f_data.c",
77        "math/log_data.c",
78        "math/logf.c",
79        "math/logf_data.c",
80        "math/math_err.c",
81        "math/math_errf.c",
82        "math/pow.c",
83        "math/pow_log_data.c",
84        "math/powf.c",
85        "math/powf_log2_data.c",
86        "math/sincosf.c",
87        "math/sincosf_data.c",
88        "math/sinf.c",
89        "string/arm/memchr.S",
90        "string/arm/memcpy.S",
91        "string/arm/memset.S",
92        "string/arm/strcmp.S",
93        "string/arm/strlen-armv6t2.S",
94      ]
95
96      asmflags = [
97        "-D__memcpy_arm = memcpy",
98        "-D__memchr_arm = memchr",
99        "-D__memset_arm = memset",
100        "-D__strcmp_arm = strcmp",
101        "-D__strlen_armv6t2 = strlen",
102      ]
103
104      include_dirs += [ "//third_party/optimized-routines/string/arm" ]
105    } else if (musl_arch == "aarch64") {
106      include_dirs += [ "//third_party/optimized-routines/string/aarch64" ]
107      if (defined(ARM_FEATURE_SVE)) {
108        sources += [
109          "string/aarch64/memchr-sve.S",
110          "string/aarch64/memcmp-sve.S",
111          "string/aarch64/memcpy.S",
112          "string/aarch64/memset.S",
113          "string/aarch64/stpcpy-sve.S",
114          "string/aarch64/strchr-sve.S",
115          "string/aarch64/strchrnul-sve.S",
116          "string/aarch64/strcmp-sve.S",
117          "string/aarch64/strcpy-sve.S",
118          "string/aarch64/strlen-sve.S",
119          "string/aarch64/strncmp-sve.S",
120          "string/aarch64/strnlen-sve.S",
121          "string/aarch64/strrchr-sve.S",
122        ]
123        asmflags = [
124          "-D__memcpy_aarch64 = memcpy",
125          "-D__memset_aarch64 = memset",
126          "-D__memcmp_aarch64_sve = memcmp",
127          "-D__memchr_aarch64_sve = memchr",
128          "-D__strcmp_aarch64_sve = strcmp",
129          "-D__strlen_aarch64_sve = strlen",
130          "-D__strcpy_aarch64_sve = strcpy",
131          "-D__stpcpy_aarch64_sve = stpcpy",
132          "-D__strchr_aarch64_sve = strchr",
133          "-D__strrchr_aarch64_sve = strrchr",
134          "-D__strchrnul_aarch64_sve = strchrnul",
135          "-D__strnlen_aarch64_sve = strnlen",
136          "-D__strncmp_aarch64_sve = strncmp",
137        ]
138      } else if (defined(ARM_FEATURE_MTE)) {
139        sources += [
140          "string/aarch64/memchr-mte.S",
141          "string/aarch64/memcmp.S",
142          "string/aarch64/memcpy.S",
143          "string/aarch64/memset.S",
144          "string/aarch64/stpcpy-mte.S",
145          "string/aarch64/strchr-mte.S",
146          "string/aarch64/strchrnul-mte.S",
147          "string/aarch64/strcmp-mte.S",
148          "string/aarch64/strcpy-mte.S",
149          "string/aarch64/strlen-mte.S",
150          "string/aarch64/strncmp-mte.S",
151          "string/aarch64/strnlen.S",
152          "string/aarch64/strrchr-mte.S",
153        ]
154        asmflags = [
155          "-D__memcpy_aarch64 = memcpy",
156          "-D__memset_aarch64 = memset",
157          "-D__memcmp_aarch64 = memcmp",
158          "-D__memchr_aarch64_mte = memchr",
159          "-D__strcmp_aarch64_mte = strcmp",
160          "-D__strlen_aarch64_mte = strlen",
161          "-D__strcpy_aarch64_mte = strcpy",
162          "-D__stpcpy_aarch64_mte = stpcpy",
163          "-D__strchr_aarch64_mte = strchr",
164          "-D__strrchr_aarch64_mte = strrchr",
165          "-D__strchrnul_aarch64_mte = strchrnul",
166          "-D__strnlen_aarch64 = strnlen",
167          "-D__strncmp_aarch64_mte = strncmp",
168        ]
169      } else {
170        sources += [
171          "string/aarch64/memchr.S",
172          "string/aarch64/memcmp.S",
173          "string/aarch64/memcpy.S",
174          "string/aarch64/memset.S",
175          "string/aarch64/stpcpy.S",
176          "string/aarch64/strchr.S",
177          "string/aarch64/strchrnul.S",
178          "string/aarch64/strcmp.S",
179          "string/aarch64/strcpy.S",
180          "string/aarch64/strlen.S",
181          "string/aarch64/strncmp.S",
182          "string/aarch64/strnlen.S",
183          "string/aarch64/strrchr.S",
184        ]
185        asmflags = [
186          "-D__memmove_aarch64 = memmove",
187          "-D__memcpy_aarch64 = memcpy",
188          "-D__memchr_aarch64 = memchr",
189          "-D__memset_aarch64 = memset",
190          "-D__memcmp_aarch64 = memcmp",
191          "-D__strcmp_aarch64 = strcmp",
192          "-D__strlen_aarch64 = strlen",
193          "-D__strcpy_aarch64 = strcpy",
194          "-D__stpcpy_aarch64 = stpcpy",
195          "-D__strchr_aarch64 = strchr",
196          "-D__strrchr_aarch64 = strrchr",
197          "-D__strchrnul_aarch64 = strchrnul",
198          "-D__strnlen_aarch64 = strnlen",
199          "-D__strncmp_aarch64 = strncmp",
200        ]
201      }
202    }
203
204    cflags = [
205      "-O3",
206      "-fPIC",
207      "-fstack-protector-strong",
208    ]
209    cflags_c = [ "-fno-lto" ]
210
211    configs -= optimized_inherited_configs
212    configs += [ "//build/config/components/musl:soft_musl_config" ]
213    if (use_hwasan) {
214      defines += [ "HWASAN_REMOVE_CLEANUP" ]
215      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
216      cflags += [
217        "-mllvm",
218        "-hwasan-instrument-check-enabled=true",
219      ]
220    }
221  }
222
223  static_library("optimize_math") {
224    output_name = "liboptimize_math"
225    output_extension = "a"
226
227    public_configs = [ ":optimized_routines_config" ]
228    configs += [ ":optimized_routines_stack_config" ]
229    sources = []
230
231    if (musl_arch == "aarch64") {
232      sources += [
233        "math/cosf.c",
234        "math/math_errf.c",
235        "math/sincosf.c",
236        "math/sincosf_data.c",
237        "math/sinf.c",
238      ]
239    }
240
241    cflags = [
242      "-mllvm",
243      "-instcombine-max-iterations=0",
244      "-ffp-contract=fast",
245      "-O3",
246      "-fPIC",
247      "-fstack-protector-strong",
248    ]
249    cflags_c = [ "-fno-lto" ]
250
251    configs -= optimized_inherited_configs
252    configs += [ "//build/config/components/musl:soft_musl_config" ]
253    if (use_hwasan) {
254      configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
255      cflags += [
256        "-mllvm",
257        "-hwasan-instrument-check-enabled=true",
258      ]
259    }
260  }
261}
262