xref: /aosp_15_r20/external/cronet/build/config/android/android_nocompile.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2020 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/android/rules.gni")
6
7declare_args() {
8  # Used by tests to enable generating build files for GN targets which should
9  # not compile.
10  enable_android_nocompile_tests = false
11}
12
13# Defines a test suite which checks that the 'test targets' fail to compile. The
14# test suite runs 'gn gen' with a custom output directory and attempts to compile
15# each test target.
16#
17# All of the tests should be defined in the same dedicated BUILD.gn file in order
18# to minimize the number of targets that are processed by 'gn gen'.
19#
20# Variables
21#   tests: List of test configurations. A test configuration has the following
22#     keys:
23#     'target': The GN target which should not compile when
24#       enable_android_nocompile_tests=true The target should compile when
25#       enable_android_nocompile_tests=false.
26#     'expected_compile_output_regex': Error message regex to search for when compile fails.
27#     'nocompile_sources': Source files which do not compile. This ensures that
28#       the test suite is re-run when one of these files change (as the test
29#       targets might not depend of the files when
30#       enable_android_nocompile_tests=false).
31template("android_nocompile_test_suite") {
32  assert(!enable_android_nocompile_tests)
33
34  action(target_name) {
35    testonly = true
36    script = "//build/android/gyp/nocompile_test.py"
37    pool = "//build/config/android:nocompile_pool"
38
39    _tests = invoker.tests
40    _test0 = _tests[0]
41    _test0_dir = get_label_info(_test0["target"], "dir")
42    _test0_target_out_dir = get_label_info(_test0["target"], "target_out_dir")
43    foreach(_test_config, _tests) {
44      assert(
45          _test0_dir == get_label_info(_test_config["target"], "dir"),
46          "To avoid running 'gn gen' for each test, all tests in an android_nocompile_test_suite() should be declared in same BUILD.gn file")
47    }
48
49    deps = []
50    if (defined(invoker.deps)) {
51      deps += invoker.deps
52    }
53
54    sources = []
55    if (defined(invoker.sources)) {
56      sources += invoker.sources
57    }
58
59    # Depend on compile_java Python scripts so that the action is re-run whenever the script is
60    # modified.
61    _pydeps = [ "//build/android/gyp/compile_java.pydeps" ]
62    if (defined(invoker.pydeps)) {
63      _pydeps += invoker.pydeps
64    }
65
66    inputs = []
67    foreach(_pydeps_file, _pydeps) {
68      _pydeps_file_lines = []
69      _pydeps_file_lines = read_file(_pydeps_file, "list lines")
70      _pydeps_entries = []
71      _pydeps_entries = filter_exclude(_pydeps_file_lines, [ "#*" ])
72      _pydeps_file_dir = get_path_info(_pydeps_file, "dir")
73      inputs += rebase_path(_pydeps_entries, ".", _pydeps_file_dir)
74    }
75
76    _json_test_configs = []
77    foreach(_test_config, _tests) {
78      _test = _test_config["target"]
79      deps += [ _test ]
80      sources += _test_config["nocompile_sources"]
81      _dep_dir = get_label_info(_test, "dir")
82      _dep_name = get_label_info(_test, "name")
83      _json_test_configs += [
84        {
85          target = "${_dep_dir}:${_dep_name}"
86          expect_regex = _test_config["expected_compile_output_regex"]
87        },
88      ]
89    }
90
91    _config_path = "$target_gen_dir/${target_name}.nocompile_config"
92    write_file(_config_path, _json_test_configs, "json")
93
94    # Compute output directory for no-compile tests based on the directory containing test
95    # targets instead of based on the test suite target name. This avoids calling 'gn gen' for each
96    # android_nocompile_test_suite() for test suites whose tests are declared in the same BUILD.gn
97    # file.
98    _out_dir = "${_test0_target_out_dir}/nocompile_out"
99
100    _stamp_path = "${target_gen_dir}/${target_name}.stamp"
101    args = [
102      "--gn-args-path",
103      "args.gn",
104      "--out-dir",
105      rebase_path(_out_dir, root_build_dir),
106      "--test-configs-path",
107      rebase_path(_config_path, root_build_dir),
108      "--stamp",
109      rebase_path(_stamp_path, root_build_dir),
110    ]
111    inputs += [ _config_path ]
112    outputs = [ _stamp_path ]
113  }
114}
115