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 if (defined(invoker.external_deps) && invoker.external_deps != []) { 185 innersdk_dir_name = "sdk/${target_os}-${target_cpu}" 186 innersdk_base_dir = "//${innersdk_dir_name}" 187 188 component_override_map = rebase_path( 189 "${root_build_dir}/build_configs/component_override_map.json") 190 external_deps_script = 191 rebase_path("//build/templates/common/external_deps_handler.py") 192 external_deps_temp_file = "$target_gen_dir/${_part_name}__${target_name}_external_deps_temp.json" 193 arguments = [ "--external-deps" ] 194 arguments += invoker.external_deps 195 arguments += [ 196 "--parts-src-flag-file", 197 rebase_path("$root_build_dir/build_configs/parts_src_flag.json", 198 root_build_dir), 199 "--external-deps-temp-file", 200 rebase_path(external_deps_temp_file, root_build_dir), 201 "--sdk-base-dir", 202 rebase_path("${innersdk_base_dir}", root_build_dir), 203 "--sdk-dir-name", 204 "${innersdk_dir_name}", 205 "--current-toolchain", 206 current_toolchain, 207 "--innerkits-adapter-info-file", 208 rebase_path("//build/ohos/inner_kits_adapter.json", root_build_dir), 209 "--component-override-map", 210 component_override_map, 211 ] 212 213 handler_result = exec_script(external_deps_script, arguments, "string") 214 if (handler_result != "") { 215 print(handler_result) 216 } 217 218 external_deps_info = read_file(external_deps_temp_file, "json") 219 if (defined(external_deps_info.deps)) { 220 deps += external_deps_info.deps 221 } 222 if (defined(external_deps_info.libs)) { 223 libs += external_deps_info.libs 224 } 225 if (defined(external_deps_info.include_dirs)) { 226 include_dirs += external_deps_info.include_dirs 227 } 228 } 229 } 230} 231 232template("moduletest") { 233 executable(target_name) { 234 output_dir = "${root_out_dir}/test/moduletest" 235 forward_variables_from(invoker, "*") 236 if (!defined(include_dirs)) { 237 include_dirs = [] 238 } 239 include_dirs += test_common_include_dirs 240 if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { 241 include_dirs += test_include_dirs 242 } 243 if (!defined(deps)) { 244 deps = [] 245 } 246 if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { 247 deps += [ "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main" ] 248 } 249 if (!defined(configs)) { 250 configs = [] 251 } 252 cflags = [ "-Wno-error" ] 253 ldflags = [] 254 if (defined(invoker.ldflags)) { 255 ldflags += invoker.ldflags 256 } 257 if (ohos_build_compiler != "clang") { 258 ldflags += [ "-lstdc++" ] 259 } 260 if (ohos_kernel_type == "linux") { 261 ldflags += [ 262 "-lm", 263 "-pthread", 264 ] 265 } 266 } 267} 268 269template("subsystem_test") { 270 assert(defined(invoker.test_components), "Test Components is required.") 271 group(target_name) { 272 deps = [] 273 if (defined(invoker.test_components)) { 274 deps += invoker.test_components 275 } 276 } 277} 278 279template("fuzztest") { 280 executable(target_name) { 281 output_dir = "${root_out_dir}/test/fuzztest" 282 forward_variables_from(invoker, "*") 283 if (!defined(include_dirs)) { 284 include_dirs = [] 285 } 286 include_dirs += test_common_include_dirs 287 include_dirs += [ 288 "//test/tools/Secodefuzz/", 289 "//test/tools/Secodefuzz/common", 290 ] 291 if (ohos_kernel_type == "liteos_a") { 292 include_dirs += test_include_dirs 293 } 294 if (!defined(deps)) { 295 deps = [] 296 } 297 if (ohos_kernel_type == "liteos_a") { 298 deps += [ 299 "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main", 300 "//test/tools/Secodefuzz:secodefuzz", 301 ] 302 } 303 if (!defined(configs)) { 304 configs = [] 305 } 306 cflags = [ "-Wno-error" ] 307 ldflags = [] 308 if (defined(invoker.ldflags)) { 309 ldflags += invoker.ldflags 310 } 311 if (ohos_build_compiler != "clang") { 312 ldflags += [ "-lstdc++" ] 313 } 314 _fuzztest_output_dir = "$root_build_dir/fuzztest" 315 rebase_path(_fuzztest_output_dir, root_build_dir) 316 } 317} 318