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.external_deps)) { 155 external_deps = invoker.external_deps 156 } 157 if (defined(invoker.public_external_deps)) { 158 public_external_deps = invoker.public_external_deps 159 } 160 161 if (defined(invoker.defines)) { 162 defines = invoker.defines 163 } 164 if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { 165 deps += [ "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main" ] 166 } 167 if (defined(invoker.configs)) { 168 configs += invoker.configs 169 } 170 skip_processing = true 171 foreach(config, configs) { 172 if (skip_processing && config == "//build/lite/config:language_cpp") { 173 configs -= [ "//build/lite/config:language_cpp" ] 174 skip_processing = false 175 } 176 } 177 configs += [ "//build/lite/config:gtest_feature_cpp" ] 178 if (defined(invoker.output_extension)) { 179 output_extension = invoker.output_extension 180 } 181 cflags = [ "-Wno-error" ] 182 if (defined(invoker.cflags)) { 183 cflags += invoker.cflags 184 } 185 ldflags = [] 186 if (defined(invoker.ldflags)) { 187 ldflags += invoker.ldflags 188 } 189 if (ohos_build_compiler != "clang") { 190 ldflags += [ "-lstdc++" ] 191 } 192 if (ohos_kernel_type == "linux") { 193 ldflags += [ 194 "-lm", 195 "-pthread", 196 ] 197 } 198 } 199} 200 201template("moduletest") { 202 executable(target_name) { 203 output_dir = "${root_out_dir}/test/moduletest" 204 forward_variables_from(invoker, "*") 205 if (!defined(include_dirs)) { 206 include_dirs = [] 207 } 208 include_dirs += test_common_include_dirs 209 if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { 210 include_dirs += test_include_dirs 211 } 212 if (!defined(deps)) { 213 deps = [] 214 } 215 if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { 216 deps += [ "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main" ] 217 } 218 if (!defined(configs)) { 219 configs = [] 220 } 221 cflags = [ "-Wno-error" ] 222 ldflags = [] 223 if (defined(invoker.ldflags)) { 224 ldflags += invoker.ldflags 225 } 226 if (defined(invoker.external_deps)) { 227 external_deps = invoker.external_deps 228 } 229 if (defined(invoker.public_external_deps)) { 230 public_external_deps = invoker.public_external_deps 231 } 232 if (ohos_build_compiler != "clang") { 233 ldflags += [ "-lstdc++" ] 234 } 235 if (ohos_kernel_type == "linux") { 236 ldflags += [ 237 "-lm", 238 "-pthread", 239 ] 240 } 241 skip_processing = true 242 foreach(config, configs) { 243 if (skip_processing && config == "//build/lite/config:language_cpp") { 244 configs -= [ "//build/lite/config:language_cpp" ] 245 skip_processing = false 246 } 247 } 248 configs += [ "//build/lite/config:gtest_feature_cpp" ] 249 } 250} 251 252template("subsystem_test") { 253 assert(defined(invoker.test_components), "Test Components is required.") 254 group(target_name) { 255 deps = [] 256 if (defined(invoker.test_components)) { 257 deps += invoker.test_components 258 } 259 } 260} 261 262template("fuzztest") { 263 executable(target_name) { 264 output_dir = "${root_out_dir}/test/fuzztest" 265 forward_variables_from(invoker, "*") 266 if (!defined(include_dirs)) { 267 include_dirs = [] 268 } 269 include_dirs += test_common_include_dirs 270 include_dirs += [ 271 "//test/tools/Secodefuzz/", 272 "//test/tools/Secodefuzz/common", 273 ] 274 if (ohos_kernel_type == "liteos_a") { 275 include_dirs += test_include_dirs 276 } 277 if (!defined(deps)) { 278 deps = [] 279 } 280 if (ohos_kernel_type == "liteos_a") { 281 deps += [ 282 "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main", 283 "//test/tools/Secodefuzz:secodefuzz", 284 ] 285 } 286 if (!defined(configs)) { 287 configs = [] 288 } 289 cflags = [ "-Wno-error" ] 290 ldflags = [] 291 if (defined(invoker.ldflags)) { 292 ldflags += invoker.ldflags 293 } 294 295 if (defined(invoker.external_deps)) { 296 external_deps = invoker.external_deps 297 } 298 if (defined(invoker.public_external_deps)) { 299 public_external_deps = invoker.public_external_deps 300 } 301 if (ohos_build_compiler != "clang") { 302 ldflags += [ "-lstdc++" ] 303 } 304 _fuzztest_output_dir = "$root_build_dir/fuzztest" 305 rebase_path(_fuzztest_output_dir, root_build_dir) 306 skip_processing = true 307 foreach(config, configs) { 308 if (skip_processing && config == "//build/lite/config:language_cpp") { 309 configs -= [ "//build/lite/config:language_cpp" ] 310 skip_processing = false 311 } 312 } 313 configs += [ "//build/lite/config:gtest_feature_cpp" ] 314 } 315} 316