1# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""cc_test macro for building native tests with Bazel."""
16
17load(
18    "//build/bazel/rules/tradefed:tradefed.bzl",
19    "FILTER_GENERATOR_SUFFIX",
20    "LANGUAGE_CC",
21    "TEST_DEP_SUFFIX",
22    "cc_test_filter_generator",
23    "tradefed_test_suite",
24)
25load(":cc_binary.bzl", "cc_binary")
26
27# TODO(b/244559183): Keep this in sync with cc/test.go#linkerFlags
28_gtest_copts = select({
29    "//build/bazel_common_rules/platforms/os:linux_glibc": ["-DGTEST_OS_LINUX"],
30    "//build/bazel_common_rules/platforms/os:darwin": ["-DGTEST_OS_MAC"],
31    "//build/bazel_common_rules/platforms/os:windows": ["-DGTEST_OS_WINDOWS"],
32    "//conditions:default": ["-DGTEST_OS_LINUX_ANDROID"],
33}) + select({
34    "//build/bazel_common_rules/platforms/os:android": [],
35    "//conditions:default": ["-O0", "-g"],  # here, default == host platform
36}) + [
37    "-DGTEST_HAS_STD_STRING",
38    "-Wno-unused-result",  # TODO(b/244433518): Figure out why this is necessary in the bazel compile action.
39]
40
41def cc_test(
42        name,
43        srcs,
44        copts = [],
45        deps = [],
46        dynamic_deps = [],
47        gtest = True,
48        tags = [],
49        tidy = None,
50        tidy_checks = None,
51        tidy_checks_as_errors = None,
52        tidy_flags = None,
53        tidy_disabled_srcs = None,
54        tidy_timeout_srcs = None,
55        test_config = None,
56        dynamic_config = None,
57        template_test_config = None,
58        template_configs = [],
59        template_install_base = None,
60        runs_on = [],
61        suffix = "",
62        visibility = None,
63        target_compatible_with = None,
64        **kwargs):
65    # NOTE: Keep this in sync with cc/test.go#linkerDeps
66    if gtest:
67        # TODO(b/244433197): handle ctx.useSdk() && ctx.Device() case to link against the ndk variants of the gtest libs.
68        copts = copts + _gtest_copts
69
70    test_filter_generator_name = name + FILTER_GENERATOR_SUFFIX
71    cc_test_filter_generator(
72        name = test_filter_generator_name,
73        srcs = srcs,
74        module_name = name,
75    )
76
77    # A cc_test is essentially the same as a cc_binary. Let's reuse the
78    # implementation for now and factor the common bits out as necessary.
79    test_dep_name = name + TEST_DEP_SUFFIX
80    cc_binary(
81        name = test_dep_name,
82        srcs = srcs,
83        copts = copts,
84        deps = deps,
85        dynamic_deps = dynamic_deps,
86        generate_cc_test = True,
87        tidy = tidy,
88        tidy_checks = tidy_checks,
89        tidy_checks_as_errors = tidy_checks_as_errors,
90        tidy_flags = tidy_flags,
91        tidy_disabled_srcs = tidy_disabled_srcs,
92        tidy_timeout_srcs = tidy_timeout_srcs,
93        tags = tags + ["manual"],
94        suffix = suffix,
95        target_compatible_with = target_compatible_with,
96        visibility = visibility,
97        **kwargs
98    )
99
100    tradefed_test_suite(
101        name = name,
102        test_dep = test_dep_name,
103        test_config = test_config,
104        template_test_config = template_test_config,
105        dynamic_config = dynamic_config,
106        template_configs = template_configs,
107        template_install_base = template_install_base,
108        deviceless_test_config = "//build/make/core:native_host_test_config_template.xml",
109        device_driven_test_config = "//build/make/core:native_test_config_template.xml",
110        host_driven_device_test_config = "//build/make/core:native_host_test_config_template.xml",
111        runs_on = runs_on,
112        test_filter_generator = test_filter_generator_name,
113        tags = tags,
114        suffix = suffix,
115        visibility = visibility,
116        test_language = LANGUAGE_CC,
117    )
118