blob: de6b7c16848eb5912a9002225b4e584b5513af5f [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("//build_overrides/pigweed.gni")
import("${chip_root}/build/chip/tests.gni")
import("${dir_pw_unit_test}/test.gni")
assert(chip_build_tests)
declare_args() {
# These may be overridden in args.gni to build platform-specific test binaries.
test_executable_output_name = ""
test_executable_output_name_suffix = ""
test_executable_ldflags = []
}
# 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",
# "TestBar.cpp",
# ]
#
# public_deps = [
# "${chip_root}/src/lib/foo", # add dependencies here
# ]
# }
#
template("chip_test_suite") {
_suite_name = target_name
exclude_variables = [ "tests" ]
if (chip_link_tests && chip_device_platform != "darwin") {
# Common library shouldn't have all the individual unit tests, only the common sources.
exclude_variables += [ "test_sources" ]
# NOTE: For `Build on Darwin (clang, python_lib, simulated)` the test_sources must be in common lib.
} else {
# Common library should have all the individual unit tests, in addition to the common sources.
if (!defined(invoker.sources)) {
invoker.sources = []
}
if (defined(invoker.test_sources)) {
invoker.sources += invoker.test_sources
}
}
# Target for the common library. Contains all the common sources, and sometimes all the individual 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, "*", exclude_variables)
output_dir = "${root_out_dir}/lib"
if (!defined(invoker.public_deps)) {
public_deps = []
}
deps = [ dir_pw_unit_test ]
if (current_os != "zephyr" && current_os != "mbed" &&
chip_device_platform != "efr32") {
# Depend on stdio logging, and have it take precedence over the default platform backend
public_deps += [ "${chip_root}/src/platform/logging:stdio" ]
} else {
public_deps += [ "${chip_root}/src/platform/logging:default" ]
}
}
# Build a source_set or a flashable executable for each individual unit test source, which also includes the common files.
if (chip_link_tests) {
tests = []
if (defined(invoker.test_sources)) {
foreach(_test, invoker.test_sources) {
_test_name = string_replace(_test, ".cpp", "")
_test_output_dir = "${root_out_dir}/tests"
if (defined(invoker.output_dir)) {
_test_output_dir = invoker.output_dir
}
pw_test(_test_name) {
# Forward certain variables from the invoker.
forward_variables_from(invoker,
[
"deps",
"public_deps",
"cflags",
"configs",
])
# Link to the common lib for this suite so we get its `sources`.
public_deps += [ ":${_suite_name}.lib" ]
# Set variables that the platform executable may need.
if (test_executable_output_name != "") {
output_name = test_executable_output_name + _test_name +
test_executable_output_name_suffix
}
ldflags = test_executable_ldflags
# Add the individual test source file (e.g. "TestSomething.cpp").
sources = [ _test ]
output_dir = _test_output_dir
}
tests += [ _test_name ]
}
}
group(_suite_name) {
deps = []
# Add each individual unit test.
foreach(_test, tests) {
deps += [ ":${_test}" ]
}
}
if (chip_pw_run_tests) {
group("${_suite_name}_run") {
deps = []
# Add the .run targets created by pw_test.
foreach(_test, tests) {
deps += [ ":${_test}.run" ]
}
}
}
} else {
group(_suite_name) {
deps = [ ":${_suite_name}.lib" ]
}
}
}