1"""Copyright (C) 2022 The Android Open Source Project
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7     http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15
16load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
17
18CC_LIB_NAME = "env_based_flags_test_cc_lib"
19
20def _compile_flags_verification_test_impl(ctx):
21    env = analysistest.begin(ctx)
22    actions = analysistest.target_actions(env)
23    compile_actions = [a for a in actions if a.mnemonic == "CppCompile"]
24    asserts.true(
25        env,
26        len(compile_actions) == 1,
27        "There should be only one compile action: %s" % compile_actions,
28    )
29    compile_action = compile_actions[0]
30
31    for flag in ctx.attr.expected_flags:
32        asserts.true(
33            env,
34            flag in compile_action.argv,
35            "compile action did not contain %s flag [%s]" % (flag, compile_action.argv),
36        )
37
38    return analysistest.end(env)
39
40auto_pattern_initialize_flags_verification_test = analysistest.make(
41    _compile_flags_verification_test_impl,
42    attrs = {
43        "expected_flags": attr.string_list(
44            doc = "Flags expected to be supplied to the command line",
45        ),
46    },
47    config_settings = {
48        "@//build/bazel/toolchains/clang/host/linux-x86:auto_pattern_initialize_env": True,
49    },
50)
51
52auto_zero_initialize_flags_verification_test = analysistest.make(
53    _compile_flags_verification_test_impl,
54    attrs = {
55        "expected_flags": attr.string_list(
56            doc = "Flags expected to be supplied to the command line",
57        ),
58    },
59    config_settings = {
60        "@//build/bazel/toolchains/clang/host/linux-x86:auto_zero_initialize_env": True,
61    },
62)
63
64auto_uninitialize_flags_verification_test = analysistest.make(
65    _compile_flags_verification_test_impl,
66    attrs = {
67        "expected_flags": attr.string_list(
68            doc = "Flags expected to be supplied to the command line",
69        ),
70    },
71    config_settings = {
72        "@//build/bazel/toolchains/clang/host/linux-x86:auto_uninitialize_env": True,
73    },
74)
75
76auto_initialize_default_flags_verification_test = analysistest.make(
77    _compile_flags_verification_test_impl,
78    attrs = {
79        "expected_flags": attr.string_list(
80            doc = "Flags expected to be supplied to the command line",
81        ),
82    },
83)
84
85independent_global_flags_verification_test = analysistest.make(
86    _compile_flags_verification_test_impl,
87    attrs = {
88        "expected_flags": attr.string_list(
89            doc = "Flags expected to be supplied to the command line",
90        ),
91    },
92    config_settings = {
93        "@//build/bazel/toolchains/clang/host/linux-x86:use_ccache_env": True,
94        "@//build/bazel/toolchains/clang/host/linux-x86:llvm_next_env": True,
95        "@//build/bazel/toolchains/clang/host/linux-x86:allow_unknown_warning_option_env": True,
96    },
97)
98
99def test_auto_pattern_initialize_flags():
100    test_name = "auto_pattern_initialize_test"
101
102    auto_pattern_initialize_flags_verification_test(
103        name = test_name,
104        target_under_test = CC_LIB_NAME,
105        expected_flags = [
106            "-ftrivial-auto-var-init=pattern",
107        ],
108    )
109
110    return test_name
111
112def test_auto_uninitialize_flags():
113    test_name = "auto_uninitialize_test"
114
115    auto_uninitialize_flags_verification_test(
116        name = test_name,
117        target_under_test = CC_LIB_NAME,
118        expected_flags = [
119            "-ftrivial-auto-var-init=uninitialized",
120        ],
121    )
122
123    return test_name
124
125def test_auto_zero_initialize_flags():
126    test_name = "auto_zero_initialize_test"
127
128    auto_zero_initialize_flags_verification_test(
129        name = test_name,
130        target_under_test = CC_LIB_NAME,
131        expected_flags = [
132            "-ftrivial-auto-var-init=zero",
133            "-Wno-unused-command-line-argument",
134        ],
135    )
136
137    return test_name
138
139def test_auto_initialize_default_flags():
140    test_name = "auto_initialize_default_test"
141
142    auto_initialize_default_flags_verification_test(
143        name = test_name,
144        target_under_test = CC_LIB_NAME,
145        expected_flags = [
146            "-ftrivial-auto-var-init=zero",
147            "-Wno-unused-command-line-argument",
148            "-g",
149        ],
150    )
151
152    return test_name
153
154def test_independent_global_flags():
155    test_name = "independent_gloabl_flags_test"
156
157    independent_global_flags_verification_test(
158        name = test_name,
159        target_under_test = CC_LIB_NAME,
160        expected_flags = [
161            "-Wno-unused-command-line-argument",
162            "-Wno-error=single-bit-bitfield-constant-conversion",
163            "-Wno-error=unknown-warning-option",
164        ],
165    )
166
167    return test_name
168
169def cc_toolchain_features_env_based_flags_test_suite(name):
170    native.cc_library(
171        name = CC_LIB_NAME,
172        srcs = ["foo.cc"],
173        tags = ["manual"],
174    )
175
176    native.test_suite(
177        name = name,
178        tests = [
179            test_auto_pattern_initialize_flags(),
180            test_auto_uninitialize_flags(),
181            test_auto_zero_initialize_flags(),
182            test_auto_initialize_default_flags(),
183            test_independent_global_flags(),
184        ],
185    )
186