• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2020 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("//build/lite/config/subsystem/aafwk/path.gni")
15
16# liteos c test template
17
18test_common_include_dirs = [ "//third_party/googletest/googletest/include" ]
19
20test_include_dirs = [
21  "//commonlibrary/utils_lite/include",
22  "//third_party/bounds_checking_function/include",
23]
24
25template("_lite_testcase_resources") {
26  assert(defined(invoker.testcase_target_name))
27  assert(defined(invoker._output_dir))
28  _deps = []
29  if (defined(invoker.deps)) {
30    _deps += invoker.deps
31  }
32  action(target_name) {
33    if (defined(invoker.testonly)) {
34      testonly = invoker.testonly
35    }
36    deps = _deps
37    inputs = []
38    script = "//build/lite/testfwk/lite_testcase_resource_copy.py"
39    output_file = "$target_out_dir/$target_name.json"
40    outputs = [ output_file ]
41    args = []
42    if (defined(invoker.resource_config_file)) {
43      args += [
44        "--resource-config-file",
45        rebase_path(invoker.resource_config_file, root_build_dir),
46      ]
47      inputs += [ invoker.resource_config_file ]
48    }
49    args += [
50      "--testcase-target-name",
51      invoker.testcase_target_name,
52      "--part-build-out-path",
53      rebase_path(root_out_dir, root_build_dir),
54      "--resource-output-path",
55      rebase_path(invoker._output_dir + "/resource", root_build_dir),
56      "--output-file",
57      rebase_path(output_file, root_build_dir),
58    ]
59  }
60}
61
62template("unittest") {
63  archive_dir_name = "test_info"
64  _output_dir = ""
65  if (defined(invoker.output_dir)) {
66    _output_dir = invoker.output_dir
67  } else {
68    _output_dir = "${root_out_dir}/${archive_dir_name}/unittest"
69  }
70  _deps = []
71  if (defined(invoker.deps)) {
72    _deps += invoker.deps
73  }
74
75  # generate module list file in gn stage
76  # format like: unittest("componentName_test_xx")
77  list_tmp = string_split(target_name, "_test")
78  _part_name = list_tmp[0]
79  _module_list_file = string_join("",
80                                  [
81                                    root_out_dir,
82                                    "/${archive_dir_name}/module_list_files/",
83                                    _part_name,
84                                    "/",
85                                    _part_name,
86                                    "/",
87                                    target_name,
88                                    ".mlf",
89                                  ])
90  _sources_file_search_root_dir = string_join("",
91                                              [
92                                                root_out_dir,
93                                                "/${archive_dir_name}/gen",
94                                              ])
95  _sources = ""
96  foreach(s, invoker.sources) {
97    _sources += s + ","
98  }
99  _arguments = [
100    "--target",
101    target_name,
102    "--target_label",
103    get_label_info(target_name, "label_with_toolchain"),
104    "--source_dir",
105    rebase_path(get_label_info(target_name, "dir"), root_out_dir),
106    "--test_type",
107    "unittest",
108    "--output_dir",
109    rebase_path(_output_dir),
110    "--module_list_file",
111    rebase_path(_module_list_file),
112    "--sources_file_search_root_dir",
113    rebase_path(_sources_file_search_root_dir),
114    "--sources",
115    _sources,
116  ]
117  _gen_module_list_script = "//build/lite/testfwk/gen_module_list_files.py"
118  exec_script(_gen_module_list_script, _arguments)
119
120  # copy testcase resource
121  testcase_target_name = target_name
122  _lite_testcase_resources("${testcase_target_name}_resource_copy") {
123    if (defined(invoker.resource_config_file)) {
124      resource_config_file = invoker.resource_config_file
125    }
126    deps = _deps
127    testonly = true
128  }
129
130  executable(target_name) {
131    if (defined(invoker.output_dir)) {
132      output_dir = invoker.output_dir
133    }
134    if (defined(invoker.sources)) {
135      sources = invoker.sources
136    }
137    if (defined(invoker.include_dirs)) {
138      include_dirs = invoker.include_dirs
139    } else {
140      include_dirs = []
141    }
142    include_dirs += test_common_include_dirs
143    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
144      include_dirs += test_include_dirs
145    }
146    if (defined(invoker.deps)) {
147      deps = invoker.deps
148    } else {
149      deps = []
150    }
151    if (defined(invoker.public_deps)) {
152      public_deps = invoker.public_deps
153    }
154    if (defined(invoker.defines)) {
155      defines = invoker.defines
156    }
157    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
158      deps += [ "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main" ]
159    }
160    if (defined(invoker.configs)) {
161      configs += invoker.configs
162    }
163    if (defined(invoker.output_extension)) {
164      output_extension = invoker.output_extension
165    }
166    cflags = [ "-Wno-error" ]
167    if (defined(invoker.cflags)) {
168      cflags += invoker.cflags
169    }
170    ldflags = []
171    if (defined(invoker.ldflags)) {
172      ldflags += invoker.ldflags
173    }
174    if (ohos_build_compiler != "clang") {
175      ldflags += [ "-lstdc++" ]
176    }
177    if (ohos_kernel_type == "linux") {
178      ldflags += [
179        "-lm",
180        "-pthread",
181      ]
182    }
183  }
184}
185
186template("moduletest") {
187  executable(target_name) {
188    output_dir = "${root_out_dir}/test/moduletest"
189    forward_variables_from(invoker, "*")
190    if (!defined(include_dirs)) {
191      include_dirs = []
192    }
193    include_dirs += test_common_include_dirs
194    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
195      include_dirs += test_include_dirs
196    }
197    if (!defined(deps)) {
198      deps = []
199    }
200    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
201      deps += [ "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main" ]
202    }
203    if (!defined(configs)) {
204      configs = []
205    }
206    cflags = [ "-Wno-error" ]
207    ldflags = []
208    if (defined(invoker.ldflags)) {
209      ldflags += invoker.ldflags
210    }
211    if (ohos_build_compiler != "clang") {
212      ldflags += [ "-lstdc++" ]
213    }
214    if (ohos_kernel_type == "linux") {
215      ldflags += [
216        "-lm",
217        "-pthread",
218      ]
219    }
220  }
221}
222
223template("subsystem_test") {
224  assert(defined(invoker.test_components), "Test Components is required.")
225  group(target_name) {
226    deps = []
227    if (defined(invoker.test_components)) {
228      deps += invoker.test_components
229    }
230  }
231}
232
233template("fuzztest") {
234  executable(target_name) {
235    output_dir = "${root_out_dir}/test/fuzztest"
236    forward_variables_from(invoker, "*")
237    if (!defined(include_dirs)) {
238      include_dirs = []
239    }
240    include_dirs += test_common_include_dirs
241    include_dirs += [
242      "//test/tools/Secodefuzz/",
243      "//test/tools/Secodefuzz/common",
244    ]
245    if (ohos_kernel_type == "liteos_a") {
246      include_dirs += test_include_dirs
247    }
248    if (!defined(deps)) {
249      deps = []
250    }
251    if (ohos_kernel_type == "liteos_a") {
252      deps += [
253        "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main",
254        "//test/tools/Secodefuzz:secodefuzz",
255      ]
256    }
257    if (!defined(configs)) {
258      configs = []
259    }
260    cflags = [ "-Wno-error" ]
261    ldflags = []
262    if (defined(invoker.ldflags)) {
263      ldflags += invoker.ldflags
264    }
265    if (ohos_build_compiler != "clang") {
266      ldflags += [ "-lstdc++" ]
267    }
268    _fuzztest_output_dir = "$root_build_dir/fuzztest"
269    rebase_path(_fuzztest_output_dir, root_build_dir)
270  }
271}
272