blob: 4912888d3336d6fbc0b7453b77655d85538f484d [file] [log] [blame]
Armando Montanez72b93852020-11-10 15:33:22 -08001# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_toolchain/generate_toolchain.gni")
18import("$dir_pw_toolchain/subtoolchain.gni")
19import("$dir_pw_unit_test/test.gni")
20
21declare_args() {
22 # Pigweed uses this internally to manage toolchain generation for facade
23 # tests. This should NEVER be set manually, or depended on as stable API.
24 pw_unit_test_FACADE_TEST_NAME = ""
25}
26
27# Create a facade test. This allows you to, for a single unit test, replace
28# backends for the purpose of testing logic in a facade. To test a single
29# facade, multiple backends may need to be replaced (e.g. to test logging, you
30# can't be using the logging test runner).
31#
32# Note: pw_facade_test names MUST be globally unique, as they all are enumerated
33# to GN's output directory.
34# (e.g. `out/stm32f429i_disc1_size_optimized.tokenizer_facade_test`)
35#
36# WARNING: Facade tests can be very costly, as ALL the test/target dependencies
37# will be rebuilt in a completely new toolchain context. This may seem
38# wasteful, but is the only technically correct solution.
39#
40# Args:
41# build_args: (required) Toolchain build arguments to override in the
42# generated subtoolchain.
43# toolchain_suffix: (optional) The suffix to use when generating a
44# subtoolchain for the currently active toolchain. This must be globally
45# unique as two tests with the same toolchain_suffix will generate the same
46# toolchain name, which is illegal.
47template("pw_facade_test") {
48 assert(
49 defined(invoker.build_args),
50 "A facade test with no `defaults` is just a more expensive pw_unit_test!")
51 assert(
52 target_name != "test",
53 "This is a dangerous name, facade tests must have globally unique names!")
54
55 # Only try to generate a facade test for toolchains created by
56 # generate_toolchain. Checking if pw_toolchain_SCOPE has the "name" member
57 # is a reliable way to do this since it's only ever set by generate_toolchain.
58 if (defined(pw_toolchain_SCOPE.name)) {
59 if (defined(invoker.toolchain_suffix)) {
60 _subtoolchain_suffix = invoker.toolchain_suffix
61 } else {
62 _subtoolchain_suffix =
63 get_label_info(":$target_name", "label_no_toolchain")
64 _subtoolchain_suffix = string_replace(_subtoolchain_suffix, "//", "")
65 _subtoolchain_suffix = string_replace(_subtoolchain_suffix, "/", "-")
66 _subtoolchain_suffix = string_replace(_subtoolchain_suffix, ":", "--")
67 }
68
69 # Generate a subtoolchain for this test unless we're already in the
70 # context of a subtoolchain that was generated for this test.
71 if (pw_unit_test_FACADE_TEST_NAME != _subtoolchain_suffix) {
72 # If this branch is hit, we're still in the context of the parent
73 # toolchain, and we should generate the subtoolchain for this test.
74 _current_toolchain_name = get_label_info(current_toolchain, "name")
75 _subtoolchain_name = "${_current_toolchain_name}.${_subtoolchain_suffix}"
76 pw_generate_subtoolchain(_subtoolchain_name) {
77 build_args = {
78 pw_unit_test_FACADE_TEST_NAME = _subtoolchain_suffix
79 forward_variables_from(invoker.build_args, "*")
80 }
81 }
82 not_needed(invoker, "*")
83
84 # This target acts as a somewhat strange passthrough. In this toolchain,
85 # it refers to a test group that depends on a test of the same name in the
86 # context of another toolchain. In the subtoolchain, this same target name
87 # refers to a concrete test. It's like Inception.
88 pw_test_group(target_name) {
89 tests = [ ":$target_name(:$_subtoolchain_name)" ]
90 }
91 } else {
92 # In this branch, we instantiate the actual pw_test target that can be
93 # run.
94 pw_test(target_name) {
95 forward_variables_from(invoker, "*", [ "build_args" ])
96 }
97 }
98 } else {
99 # Dummy target for non-pigweed toolchains.
100 not_needed(invoker, "*")
101 pw_test_group(target_name) {
102 enable_if = false
103 }
104 }
105}