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