blob: 1640988016c25f48e13138d5efa22ecaa2629c0f [file] [log] [blame]
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("${chip_root}/build/chip/chip_test.gni")
import("${chip_root}/build/chip/tests.gni")
import("${dir_pw_unit_test}/test.gni")
assert(chip_build_tests)
# Define CHIP unit tests
#
# Simple usage
# chip_test_suite("tests") {
# output_name = "libFooTests"
#
# sources = [
# "Common.h", # add common sources here
# "Common.cpp",
# ]
#
# test_sources = [
# "TestFoo.cpp", # Files are parsed for `CHIP_REGISTER_TEST_SUITE(...)`
# "TestBar.cpp", # and a driver is created automatically
# ]
#
# public_deps = [
# "${chip_root}/src/lib/foo", # add dependencies here
# "${nlunit_test_root}:nlunit-test",
# ]
# }
#
#
# Deprecated usage (writing own driver files):
#
# chip_test_suite("tests") {
# output_name = "libFooTests"
#
# sources = [
# "TestDeclarations.h",
# "TestFoo.cpp",
# "TestBar.cpp",
# ]
#
# public_deps = [
# "${chip_root}/src/lib/foo", # add dependencies here
# "${nlunit_test_root}:nlunit-test",
# ]
#
# tests = [
# "TestFoo", # Assumes TestFooDriver.cpp exists
# "TestBar", # Assumes TestBarDriver.cpp exists
# ]
# }
#
template("chip_test_suite") {
_suite_name = target_name
# Ensures that the common library has sources containing both common
# and individual unit tests.
if (!defined(invoker.sources)) {
invoker.sources = []
}
if (defined(invoker.test_sources)) {
invoker.sources += invoker.test_sources
}
if (chip_build_test_static_libraries) {
_target_type = "static_library"
} else {
_target_type = "source_set"
}
target(_target_type, "${_suite_name}_lib") {
forward_variables_from(invoker, "*", [ "tests" ])
output_dir = "${root_out_dir}/lib"
if (!defined(invoker.public_deps)) {
public_deps = []
}
if (current_os != "zephyr" && current_os != "mbed") {
# Depend on stdio logging, and have it take precedence over the default platform backend
public_deps += [ "${chip_root}/src/platform/logging:force_stdio" ]
}
}
if (chip_link_tests) {
tests = []
if (defined(invoker.test_sources)) {
foreach(_test, invoker.test_sources) {
_test_name = string_replace(_test, ".cpp", "")
_driver_name = "${root_gen_dir}/${_test_name}.driver.cpp"
action("${_test_name}_generate_driver") {
script = "${chip_root}/scripts/gen_test_driver.py"
inputs = [ _test ]
outputs = [ _driver_name ]
args = [
"--input_file=" + rebase_path(_test, root_build_dir),
"--output_file=" + rebase_path(_driver_name, root_build_dir),
]
}
chip_test(_test_name) {
sources = [ _driver_name ]
public_deps = [
":${_suite_name}_lib",
":${_test_name}_generate_driver",
]
}
tests += [ _test_name ]
}
}
if (defined(invoker.tests)) {
foreach(_test, invoker.tests) {
chip_test(_test) {
sources = [ "${_test}Driver.cpp" ]
public_deps = [ ":${_suite_name}_lib" ]
}
tests += [ _test ]
}
}
group(_suite_name) {
deps = []
foreach(_test, tests) {
deps += [ ":${_test}" ]
}
}
if (chip_pw_run_tests) {
group("${_suite_name}_run") {
deps = []
foreach(_test, tests) {
deps += [ ":${_test}_run" ]
}
}
}
} else {
group(_suite_name) {
deps = [ ":${_suite_name}_lib" ]
}
}
}