xref: /aosp_15_r20/external/pigweed/pw_unit_test/facade_test.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# 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  # Controls whether to build and run facade tests. Facade tests add
23  # considerably to build time, so they are disabled by default.
24  pw_unit_test_FACADE_TESTS_ENABLED = false
25
26  # Pigweed uses this internally to manage toolchain generation for facade
27  # tests. This should NEVER be set manually, or depended on as stable API.
28  pw_unit_test_FACADE_TEST_NAME = ""
29}
30
31# Create a facade test. This allows you to, for a single unit test, replace
32# backends for the purpose of testing logic in a facade. To test a single
33# facade, multiple backends may need to be replaced (e.g. to test logging, you
34# can't be using the logging test runner).
35#
36# Note: pw_facade_test names MUST be globally unique, as they all are enumerated
37# to GN's output directory.
38# (e.g. `out/stm32f429i_disc1_size_optimized.tokenizer_facade_test`)
39#
40# WARNING: Facade tests can be very costly, as ALL the test/target dependencies
41#   will be rebuilt in a completely new toolchain context. This may seem
42#   wasteful, but is the only technically correct solution.
43#
44# Args:
45#   build_args: (required) Toolchain build arguments to override in the
46#     generated subtoolchain.
47#   toolchain_suffix: (optional) The suffix to use when generating a
48#     subtoolchain for the currently active toolchain. This must be globally
49#     unique as two tests with the same toolchain_suffix will generate the same
50#     toolchain name, which is illegal.
51template("pw_facade_test") {
52  assert(
53      defined(invoker.build_args),
54      "A facade test with no `defaults` is just a more expensive pw_unit_test!")
55  assert(
56      target_name != "test",
57      "This is a dangerous name, facade tests must have globally unique names!")
58
59  # Only try to generate a facade test for toolchains created by
60  # generate_toolchain. Checking if pw_toolchain_SCOPE has the "name" member
61  # is a reliable way to do this since it's only ever set by generate_toolchain.
62  if (pw_unit_test_FACADE_TESTS_ENABLED && defined(pw_toolchain_SCOPE.name)) {
63    if (defined(invoker.toolchain_suffix)) {
64      _subtoolchain_suffix = invoker.toolchain_suffix
65    } else {
66      _subtoolchain_suffix =
67          get_label_info(":$target_name", "label_no_toolchain")
68      _subtoolchain_suffix = string_replace(_subtoolchain_suffix, "//", "")
69      _subtoolchain_suffix = string_replace(_subtoolchain_suffix, "/", "-")
70      _subtoolchain_suffix = string_replace(_subtoolchain_suffix, ":", "--")
71    }
72
73    # Generate a subtoolchain for this test unless we're already in the
74    # context of a subtoolchain that was generated for this test.
75    if (pw_unit_test_FACADE_TEST_NAME != _subtoolchain_suffix) {
76      # If this branch is hit, we're still in the context of the parent
77      # toolchain, and we should generate the subtoolchain for this test.
78      _current_toolchain_name = get_label_info(current_toolchain, "name")
79      _subtoolchain_name = "${_current_toolchain_name}.${_subtoolchain_suffix}"
80      pw_generate_subtoolchain(_subtoolchain_name) {
81        build_args = {
82          pw_unit_test_FACADE_TEST_NAME = _subtoolchain_suffix
83          forward_variables_from(invoker.build_args, "*")
84        }
85      }
86      not_needed(invoker, "*")
87
88      # This target acts as a somewhat strange passthrough. In this toolchain,
89      # it refers to a test group that depends on a test of the same name in the
90      # context of another toolchain. In the subtoolchain, this same target name
91      # refers to a concrete test. It's like Inception.
92      pw_test_group(target_name) {
93        tests = [ ":$target_name(:$_subtoolchain_name)" ]
94      }
95    } else {
96      # In this branch, we instantiate the actual pw_test target that can be
97      # run.
98      pw_test(target_name) {
99        forward_variables_from(invoker, "*", [ "build_args" ])
100      }
101    }
102  } else {
103    # No-op target for non-pigweed toolchains.
104    not_needed(invoker, "*")
105    pw_test_group(target_name) {
106      enable_if = false
107    }
108  }
109}
110