xref: /aosp_15_r20/external/pigweed/pw_perf_test/perf_test.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 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_build/target_types.gni")
18import("$dir_pw_build/test_info.gni")
19import("$dir_pw_compilation_testing/negative_compilation_test.gni")
20import("$dir_pw_toolchain/host_clang/toolchains.gni")
21import("$dir_pw_unit_test/test.gni")
22
23declare_args() {
24  # Chooses the backend for how the framework calculates time
25  pw_perf_test_TIMER_INTERFACE_BACKEND = ""
26
27  # Chooses the EventHandler for running the perf tests
28  pw_perf_test_MAIN_FUNCTION = "$dir_pw_perf_test:logging_main"
29
30  # Chooses the executable template for performance tests
31  pw_perf_test_EXECUTABLE_TARGET_TYPE = "pw_executable"
32}
33
34# Creates a library and an executable target for a unit test with pw_unit_test.
35#
36# <target_name>.lib contains the provided test sources as a library, which can
37# then be linked into a test executable.
38# <target_name> is a standalone executable which contains only the test sources
39# specified in the pw_perf_test_template.
40#
41# Targets defined using this template will produce test metadata with a
42# `test_type` of "perf_test" and an additional `test_directory` value describing
43# the location of the test binary within the build output.
44#
45# Args:
46#   - The following args have the same meaning as for `pw_test`:
47#         enable_if
48#         tags
49#         extra_metadata
50#
51#   - All of the regular `executable` target args are accepted.
52#
53template("pw_perf_test") {
54  _test_target_name = target_name
55
56  _test_is_enabled = !defined(invoker.enable_if) || invoker.enable_if
57
58  _test_output_dir = "${target_out_dir}/test"
59  if (defined(invoker.output_dir)) {
60    _test_output_dir = invoker.output_dir
61  }
62
63  _test_main = pw_perf_test_MAIN_FUNCTION
64  if (defined(invoker.test_main)) {
65    _test_main = invoker.test_main
66  }
67
68  pw_internal_disableable_target("$target_name.lib") {
69    target_type = "pw_source_set"
70    enable_if = _test_is_enabled
71    forward_variables_from(invoker, "*", [ "metadata" ])
72
73    if (!defined(deps)) {
74      deps = []
75    }
76    deps += [ dir_pw_perf_test ]
77  }
78
79  _test_metadata = "${target_name}.metadata"
80  _extra_metadata = {
81    forward_variables_from(invoker, [ "extra_metadata" ])
82    test_directory = rebase_path(_test_output_dir, root_build_dir)
83  }
84  pw_test_info(_test_metadata) {
85    test_type = "perf_test"
86    test_name = _test_target_name
87    forward_variables_from(invoker, [ "tags" ])
88    extra_metadata = _extra_metadata
89  }
90
91  pw_internal_disableable_target(_test_target_name) {
92    target_type = pw_perf_test_EXECUTABLE_TARGET_TYPE
93    enable_if = _test_is_enabled
94
95    deps = [
96      ":$_test_metadata",
97      ":$_test_target_name.lib",
98    ]
99    if (_test_main != "") {
100      deps += [ _test_main ]
101    }
102
103    output_dir = _test_output_dir
104  }
105}
106