1import("//build/test.gni")
2import("//third_party/musl/musl_config.gni")
3
4musl_base_dir = "third_party/musl/"
5test_dir = "${musl_base_dir}/libc-test"
6out_test_dir = "${root_out_dir}/obj/${musl_base_dir}/libc-test"
7
8if (current_cpu == "arm") {
9  musl_arch = "arm"
10} else if (current_cpu == "arm64") {
11  musl_arch = "aarch64"
12} else if (current_cpu == "x86_64") {
13  musl_arch = "x86_64"
14}
15
16musl_include_dir =
17    "${root_out_dir}/obj/${musl_base_dir}/usr/include/${musl_arch}-linux-ohos"
18musl_lib_dir =
19    "${root_out_dir}/obj/${musl_base_dir}/usr/lib/${musl_arch}-linux-ohos"
20
21test_lib_dir = "musl/libc-test-lib"
22
23musl_src_base = "${root_out_dir}/obj/${musl_base_dir}/${musl_ported_dir}"
24
25template("test_unittest") {
26  assert(defined(invoker.target_name),
27         "file name is required in target ${target_name}")
28
29  target_name = invoker.target_name
30  target_dir = invoker.target_dir
31
32  if (defined(invoker.cpp_file)) {
33    cpp_file = invoker.cpp_file
34  } else {
35    cpp_file = false
36  }
37
38  if (defined(invoker.musl_unit_test_flag)) {
39    musl_unit_test_flag = invoker.musl_unit_test_flag
40  }
41  target("ohos_executable", "${target_name}") {
42    subsystem_name = "musl"
43    part_name = "libc-test"
44
45    if (target_cpu == "arm64") {
46      defines = [ "_ARM64_" ]
47    }
48
49    cflags = []
50
51    if (cpp_file) {
52      sources = [ "${target_name}.cpp" ]
53      cflags = [ "-std=c++17" ]
54    } else {
55      sources = [ "${target_name}.c" ]
56    }
57
58    include_dirs = [
59      "//${test_dir}/src/common",
60      "//${musl_include_dir}",
61      "//${test_dir}/src/functionalext/common",
62      "//${musl_base_dir}/include/arpa",
63      "//${musl_base_dir}/src/process",
64    ]
65
66    cflags_c = [
67      "-pipe",
68      "-std=c99",
69      "-D_POSIX_C_SOURCE=200809L",
70      "-Wall",
71      "-Wno-unused",
72      "-Wno-unused-function",
73      "-Wno-missing-braces",
74      "-Wno-overflow",
75      "-Wno-unknown-pragmas",
76      "-Wno-unsupported-floating-point-opt",
77      "-Wno-ignored-pragmas",
78      "-fno-builtin",
79      "-frounding-math",
80      "-Werror=implicit-function-declaration",
81      "-Werror=implicit-int",
82      "-Werror=pointer-sign",
83      "-Werror=pointer-arith",
84      "-g",
85      "-D_FILE_OFFSET_BITS=64",
86      "-c",
87      "-o",
88    ]
89
90    if (musl_use_pthread_cancel) {
91      cflags += [ "-DFEATURE_PTHREAD_CANCEL" ]
92    }
93
94    ldflags = [ "-nostdlib" ]
95
96    libs = [ "//${out_test_dir}/src/common/libtest.a" ]
97    if (!musl_unit_test_flag) {
98      libs += [ "${musl_lib_dir}/libc.a" ]
99    }
100
101    if (target_dir == "math") {
102      include_dirs += [
103        "//${test_dir}/src/math/crlibm",
104        "//${test_dir}/src/math/gen",
105        "//${test_dir}/src/math/sanity",
106        "//${test_dir}/src/math/special",
107        "//${test_dir}/src/math/ucb",
108      ]
109
110      if (musl_ld128_flag) {
111        include_dirs += [ "//${test_dir}/src/math/ld128" ]
112        if (!defined(defines)) {
113          defines = []
114        }
115        defines += [ "LD128_ENABLE" ]
116      }
117
118      cflags += [
119        # math/dremf.c:1:9: error: '_GNU_SOURCE' macro redefined [-Werror,-Wmacro-redefined]
120        # cflags_c无法生效此命令
121        # -Wno-error无法屏蔽错误,改为屏蔽警告
122        "-Wno-macro-redefined",
123      ]
124    }
125
126    if (target_dir == "functional") {
127      cflags_c += [
128        # include/${musl_arch}-linux-ohos/endian.h:26:25: error: '&' within '|' [-Werror,-Wbitwise-op-parentheses]
129        "-Wno-error=bitwise-op-parentheses",
130
131        #functional/sscanf.c:73:9: error: magnitude of floating-point constant too large for type 'double'; maximum is 1.7976931348623157E+308 [-Werror,-Wliteral-range]
132        "-Wno-error=literal-range",
133      ]
134
135      cflags += [
136        # functional/strptime.c:3:9: error: '_GNU_SOURCE' macro redefined [-Werror,-Wmacro-redefined]
137        "-Wno-macro-redefined",
138      ]
139
140      if (target_name == "dlopen") {
141        ldflags += [ "-rdynamic" ]
142        #libs += [ "//${root_out_dir}/${test_lib_dir}/libdlopen_dso.so" ]
143      }
144
145      if (target_name == "dlopen_ns") {
146        ldflags += [ "-rdynamic" ]
147        #libs += [ "//${root_out_dir}/${test_lib_dir}/libdlopen_ns_dso.so" ]
148      }
149
150      if (target_name == "dlclose_reset") {
151        ldflags += [ "-rdynamic" ]
152        #libs += [ "//${root_out_dir}/${test_lib_dir}/libdlclose_reset_dso.so" ]
153      }
154
155      if (target_name == "dl_multithread_test") {
156        ldflags += [ "-Wl,-rpath=./" ]
157      }
158
159      if (target_name == "dlopen") {
160        ldflags += [ "-Wl,-rpath=./" ]
161      }
162
163      if (target_name == "tls_align") {
164        ldflags += [ "-Wl,-rpath=./" ]
165        libs += [ "//${root_out_dir}/${test_lib_dir}/libtls_align_dso.so" ]
166      }
167
168      if (target_name == "tls_init") {
169        ldflags += [ "-Wl,-rpath=./" ]
170        libs += [ "//${root_out_dir}/${test_lib_dir}/libtls_init_dso.so" ]
171      }
172
173      if (target_name == "test-malloc-info") {
174        deps = [ "//third_party/libxml2:xml2" ]
175      }
176    }
177
178    if (target_dir == "functionalext/fortify") {
179      if (!defined(defines)) {
180        defines = []
181      }
182      defines += [ "_FORTIFY_SOURCE=2" ]
183    }
184
185    if (target_dir == "functionalext/ldso_randomization") {
186      if (target_name == "ldso_randomization_test") {
187        ldflags += [ "-Wl,--export-dynamic,-rpath=./" ]
188      }
189
190      if (target_name == "ldso_randomization_manual") {
191        ldflags += [ "-Wl,-rpath=./" ]
192      }
193
194      include_dirs += [ "//${test_dir}/src/functionalext/common" ]
195    }
196
197    if (target_dir == "functionalext/symver") {
198      ldflags += [ "-Wl,-rpath=./" ]
199      if (target_name == "dlsym" || target_name == "dlvsym" ||
200          target_name == "dynlink" || target_name == "dynlink_default") {
201        libs += [
202          "//${root_out_dir}/${test_lib_dir}/libdso_easy_symver.so",
203          "//${root_out_dir}/${test_lib_dir}/libdso_hard_symver.so",
204          "//${root_out_dir}/${test_lib_dir}/libdso_no_symver.so",
205          "//${root_out_dir}/${test_lib_dir}/libdso_symver.so",
206        ]
207      }
208    }
209
210    if (target_dir == "musl") {
211      cflags_c += [ "-w" ]
212
213      libs += [
214        "//${musl_lib_dir}/libc.a",
215        "//${musl_lib_dir}/libm.a",
216        "//${musl_lib_dir}/librt.a",
217        "//${musl_lib_dir}/libcrypt.a",
218        "//${musl_lib_dir}/libdl.a",
219        "//${musl_lib_dir}/libresolv.a",
220        "//${musl_lib_dir}/libutil.a",
221        "//${musl_lib_dir}/libpthread.a",
222      ]
223    }
224
225    if (target_dir == "regression") {
226      cflags_c += [
227        # regression/daemon-failure.c:56:24: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
228        "-Wno-string-plus-int",
229        "-Wno-error=string-plus-int",
230      ]
231
232      cflags += [
233        # regression/syscall-sign-extend.c:3:9: error: '_GNU_SOURCE' macro redefined [-Werror,-Wmacro-redefined]
234        "-Wno-macro-redefined",
235      ]
236
237      if (target_name == "tls_get_new-dtv") {
238        ldflags += [ "-Wl,-rpath=./" ]
239        libs +=
240            [ "//${root_out_dir}/${test_lib_dir}/libtls_get_new-dtv_dso.so" ]
241      }
242    }
243
244    if (target_dir == "functionalext/relro") {
245      if (target_name == "dlopen_ext_relro_test") {
246        include_dirs += [ "//${test_dir}/src/functionalext/common" ]
247
248        ldflags += [ "-Wl,-rpath=./" ]
249      }
250    }
251
252    if (target_dir == "functionalext/thread") {
253      if (!defined(defines)) {
254        defines = []
255      }
256      if (musl_target_os == "linux" && product_path != "" &&
257          product_path != rebase_path("//productdefine/common/products")) {
258        _product_config = read_file("${product_path}/config.json", "json")
259        if (target_name == "pthread_stack_size") {
260          if (defined(_product_config.device_stack_size)) {
261            defines +=
262                [ "TARGET_STACK_SIZE=${_product_config.device_stack_size}" ]
263          }
264        }
265        if (target_name == "pthread_guard_size") {
266          if (defined(_product_config.device_guard_size)) {
267            defines +=
268                [ "TARGET_GUARD_SIZE=${_product_config.device_guard_size}" ]
269          }
270        }
271      }
272    }
273
274    if (target_dir == "functionalext/sigchain") {
275      include_dirs += [ "//${musl_base_dir}/include" ]
276    }
277  }
278}
279
280template("test_sharedlib") {
281  assert(defined(invoker.target_name),
282         "shared_lib name is required in target ${target_name}")
283
284  target_name = invoker.target_name
285  if (defined(invoker.cpp_file)) {
286    cpp_file = invoker.cpp_file
287  } else {
288    cpp_file = false
289  }
290
291  if (defined(invoker.global)) {
292    global = invoker.global
293  } else {
294    global = false
295  }
296
297  target("ohos_shared_library", "${target_name}") {
298    include_dirs = [ "//${test_dir}/src/common" ]
299
300    if (cpp_file) {
301      sources = [ "${target_name}.cpp" ]
302      cflags = [ "-std=c++17" ]
303    } else {
304      sources = [ "${target_name}.c" ]
305      cflags = [
306        "-std=c99",
307        "-D_POSIX_C_SOURCE=200809L",
308      ]
309    }
310
311    cflags_common = [
312      "-pipe",
313      "-Wall",
314      "-Wno-unused",
315      "-Wno-unused-function",
316      "-Wno-missing-braces",
317      "-Wno-overflow",
318      "-Wno-unknown-pragmas",
319      "-Wno-unsupported-floating-point-opt",
320      "-fno-builtin",
321      "-frounding-math",
322      "-Werror=implicit-function-declaration",
323      "-Werror=implicit-int",
324      "-Werror=pointer-sign",
325      "-Werror=pointer-arith",
326      "-g",
327      "-D_FILE_OFFSET_BITS=64",
328      "-fPIC",
329      "-DSHARED",
330      "-c",
331      "-o",
332    ]
333
334    cflags_c = cflags
335    cflags_c += cflags_common
336
337    ldflags = [
338      "-shared",
339      "-nostdlib",
340    ]
341
342    if (global) {
343      ldflags += [ "-Wl,-z,global" ]
344    }
345
346    if (defined(invoker.version_script)) {
347      _version_script = rebase_path(invoker.version_script, root_build_dir)
348      ldflags += [ "-Wl,--version-script=${_version_script}" ]
349    }
350
351    if (defined(invoker.deps)) {
352      deps = invoker.deps
353    }
354
355    libs = [ "//${out_test_dir}/src/common/libtest.a" ]
356
357    output_name = "${target_name}"
358
359    output_extension = "so"
360
361    subsystem_name = "musl"
362    part_name = "libc-test-lib"
363  }
364}
365