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")
17load(
18    "//build/bazel/rules/test_common:flags.bzl",
19    "action_flags_absent_for_mnemonic_test",
20    "action_flags_present_only_for_mnemonic_test",
21)
22
23# Include these different file types to make sure that all actions types are
24# triggered
25test_srcs = [
26    "foo.cpp",
27    "bar.c",
28    "baz.s",
29    "blah.S",
30]
31
32compile_action_mnemonic = "CppCompile"
33link_action_mnemonic = "CppLink"
34
35def test_thin_lto_feature_defaults():
36    name = "thin_lto_feature"
37
38    native.cc_binary(
39        name = name,
40        srcs = test_srcs,
41        features = ["android_thin_lto"],
42        tags = ["manual"],
43    )
44
45    test_name = name + "_test"
46    test_names = [test_name]
47    action_flags_present_only_for_mnemonic_test(
48        name = test_name,
49        target_under_test = name,
50        mnemonics = [compile_action_mnemonic, link_action_mnemonic],
51        expected_flags = [
52            "-flto=thin",
53            "-fsplit-lto-unit",
54        ],
55    )
56    limit_cross_tu_inline_test_name = "limit_cross_tu_inline_feature_enabled_by_default"
57    test_names += [limit_cross_tu_inline_test_name]
58    action_flags_present_only_for_mnemonic_test(
59        name = limit_cross_tu_inline_test_name,
60        target_under_test = name,
61        mnemonics = [link_action_mnemonic],
62        expected_flags = ["-Wl,-plugin-opt,-import-instr-limit=5"],
63    )
64    return test_names
65
66def test_whole_program_vtables_feature():
67    name = "whole_program_vtables_feature"
68    test_name = name + "_test"
69
70    native.cc_binary(
71        name = name,
72        srcs = test_srcs,
73        features = [
74            "android_thin_lto",
75            "android_thin_lto_whole_program_vtables",
76        ],
77        tags = ["manual"],
78    )
79    action_flags_present_only_for_mnemonic_test(
80        name = test_name,
81        target_under_test = name,
82        mnemonics = [link_action_mnemonic],
83        expected_flags = ["-fwhole-program-vtables"],
84    )
85
86    return test_name
87
88def test_whole_program_vtables_requires_thinlto_feature():
89    name = "whole_program_vtables_requires_thinlto"
90    test_name = name + "_test"
91
92    native.cc_binary(
93        name = name,
94        srcs = test_srcs,
95        features = [
96            "android_thin_lto_whole_program_vtables",
97        ],
98        tags = ["manual"],
99    )
100    action_flags_absent_for_mnemonic_test(
101        name = test_name,
102        target_under_test = name,
103        mnemonics = [
104            compile_action_mnemonic,
105            link_action_mnemonic,
106        ],
107        expected_absent_flags = ["-fwhole-program-vtables"],
108    )
109
110    return test_name
111
112def test_limit_cross_tu_inline_requires_thinlto_feature():
113    name = "limit_cross_tu_inline_requires_thinlto"
114    test_name = name + "_test"
115
116    native.cc_binary(
117        name = name,
118        srcs = test_srcs,
119        features = [
120            "android_thin_lto_limit_cross_tu_inline",
121        ],
122        tags = ["manual"],
123    )
124    action_flags_absent_for_mnemonic_test(
125        name = test_name,
126        target_under_test = name,
127        mnemonics = [
128            link_action_mnemonic,
129        ],
130        expected_absent_flags = ["-Wl,-plugin-opt,-import-instr-limit=5"],
131    )
132
133    return test_name
134
135def test_limit_cross_tu_inline_disabled_when_autofdo_enabled():
136    name = "limit_cross_tu_inline_disabled_when_autofdo_enabled"
137    test_name = name + "_test"
138
139    native.cc_binary(
140        name = name,
141        srcs = test_srcs,
142        features = [
143            "android_thin_lto",
144            "autofdo",
145        ],
146        tags = ["manual"],
147    )
148    action_flags_absent_for_mnemonic_test(
149        name = test_name,
150        target_under_test = name,
151        mnemonics = [
152            link_action_mnemonic,
153        ],
154        expected_absent_flags = ["-Wl,-plugin-opt,-import-instr-limit=5"],
155    )
156
157    return test_name
158
159def cc_toolchain_features_lto_test_suite(name):
160    native.test_suite(
161        name = name,
162        tests = [
163            test_whole_program_vtables_feature(),
164            test_whole_program_vtables_requires_thinlto_feature(),
165            test_limit_cross_tu_inline_requires_thinlto_feature(),
166            test_limit_cross_tu_inline_disabled_when_autofdo_enabled(),
167        ] + test_thin_lto_feature_defaults(),
168    )
169