xref: /aosp_15_r20/external/pigweed/pw_build/rust_test.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 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.
14import("//build_overrides/pigweed.gni")
15
16import("$dir_pw_build/python_action.gni")
17import("$dir_pw_build/rust_executable.gni")
18
19# Note: In general, prefer to import target_types.gni rather than this file.
20#
21# This template sets up a configurable build of source files containing rust
22# unit tests. This wrapper is analogous to pw_test with additions to support
23# rust specific parameters such as rust edition and cargo config features.
24#
25# For more information on the features provided by this template, see the full
26# docs at https://pigweed.dev/pw_build/?highlight=pw_rust_test.
27template("pw_rust_test") {
28  output_dir = "${target_out_dir}/test"
29  if (defined(invoker.output_dir)) {
30    output_dir = invoker.output_dir
31  }
32
33  _actual_target_name = target_name
34  _is_enabled = !defined(invoker.enable_if) || invoker.enable_if
35  if (!_is_enabled) {
36    _actual_target_name = target_name + ".DISABLED"
37
38    # If the target is disabled, create an empty target in its place. Use an
39    # action with the original target's sources as inputs to ensure that the
40    # source files exist (even if they don't compile).
41    pw_python_action(target_name) {
42      script = "$dir_pw_build/py/pw_build/nop.py"
43      stamp = true
44
45      inputs = []
46      if (defined(invoker.sources)) {
47        inputs += invoker.sources
48      }
49      if (defined(invoker.public)) {
50        inputs += invoker.public
51      }
52    }
53  }
54
55  pw_rust_executable(_actual_target_name) {
56    forward_variables_from(invoker, "*")
57
58    if (!defined(rustflags)) {
59      rustflags = []
60    }
61    rustflags += [ "--test" ]
62
63    _extra_metadata = {
64    }
65    if (defined(invoker.extra_metadata)) {
66      _extra_metadata = invoker.extra_metadata
67    }
68
69    metadata = {
70      tests = [
71        {
72          type = "test"
73          test_name = _actual_target_name
74          test_directory = rebase_path(output_dir, root_build_dir)
75          extra_metadata = _extra_metadata
76        },
77      ]
78    }
79  }
80}
81