xref: /aosp_15_r20/external/pigweed/pw_compilation_testing/negative_compilation_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/python_action.gni")
18import("$dir_pw_build/target_types.gni")
19
20declare_args() {
21  # Enables or disables negative compilation tests for the current toolchain.
22  # Disabled by default since negative compilation tests increase gn gen time
23  # significantly.
24  pw_compilation_testing_NEGATIVE_COMPILATION_ENABLED = false
25}
26
27# Declares a compilation failure test. If
28# pw_compilation_testing_NEGATIVE_COMPILATION_ENABLED is true, negative
29# complilation tests will be executed in the build. These tests pass if
30# compilation fails when #if block test cases are enabled in the code.
31#
32# Compilation failure tests may also be declared as part of a unit test in
33# pw_test by settin negative_compilation_tests = true.
34template("pw_cc_negative_compilation_test") {
35  assert(defined(invoker.sources) && invoker.sources != [],
36         "pw_cc_negative_compilation_test requires 'sources' to be provided")
37
38  if (pw_compilation_testing_NEGATIVE_COMPILATION_ENABLED) {
39    _out_dir = "$target_gen_dir/$target_name"
40
41    _args = [
42      "--output",
43      rebase_path(_out_dir, root_build_dir),
44      "--base",
45      get_label_info(":$target_name._base", "label_no_toolchain"),
46      "--name",
47      target_name,
48    ]
49
50    # List the source files as both a GN path and a file system path.
51    foreach(file, invoker.sources) {
52      _args += [ get_path_info(file, "abspath") + ";" +
53                 rebase_path(file, root_build_dir) ]
54    }
55
56    # Invoke the generator script, which generates a BUILD.gn file with a GN
57    # action for each NC test. The test names are returned as a list.
58    _tests = exec_script(
59            "$dir_pw_compilation_testing/py/pw_compilation_testing/generator.py",
60            _args,
61            "list lines",
62            invoker.sources)
63
64    # Create a group of the generated NC test targets.
65    group(target_name) {
66      forward_variables_from(invoker, [ "testonly" ])
67      deps = []
68      foreach(test, _tests) {
69        deps += [ "$_out_dir:$target_name.$test.negative_compilation_test" ]
70      }
71    }
72  } else {
73    # If compilation testing is disabled, only compile the base file, which the
74    # negative compilation test targets depend on. Use an action for this target
75    # so that depending on it will not link in any source files.
76    pw_python_action(target_name) {
77      forward_variables_from(invoker, [ "testonly" ])
78      script = "$dir_pw_build/py/pw_build/nop.py"
79      stamp = true
80      deps = [ ":$target_name._base" ]
81    }
82  }
83
84  # The base target is the sources with no tests enabled.
85  pw_source_set(target_name + "._base") {
86    forward_variables_from(invoker, "*")
87    if (!defined(deps)) {
88      deps = []
89    }
90    deps += [ "$dir_pw_compilation_testing:internal_pigweed_use_only" ]
91  }
92}
93