aboutsummaryrefslogtreecommitdiff
path: root/toolchains/clang/host/linux-x86/cc_toolchain_features_linker_alignment_test.bzl
blob: 633697b2702897ecf7fdf49d4441de70c557d270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""Copyright (C) 2023 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

def _test_linker_alignment_flag_impl(ctx):
    """ Checks that linker alignment flag is present.

    This test checks that the linker alignment flag is present for
    arm and arm 64 targets but it is not present for x86 and x86_64 targets.
    """

    env = analysistest.begin(ctx)
    actions = analysistest.target_actions(env)

    for action in actions:
        if action.mnemonic in ctx.attr.expected_action_mnemonics:
            for flag in ctx.attr.expected_flags:
                asserts.true(
                    env,
                    flag in action.argv,
                    "%s action did not contain %s flag" % (
                        action.mnemonic,
                        flag,
                    ),
                )
            for flag in ctx.attr.no_expected_flags:
                asserts.false(
                    env,
                    flag in action.argv,
                    "%s action contained unexpected flag %s" % (
                        action.mnemonic,
                        flag,
                    ),
                )
        else:
            for flag in ctx.attr.expected_flags:
                if action.argv != None:
                    asserts.false(
                        env,
                        flag in action.argv,
                        "%s action contained unexpected flag %s" % (
                            action.mnemonic,
                            flag,
                        ),
                    )

    return analysistest.end(env)

test_attrs = {
    "expected_flags": attr.string_list(
        doc = "Flags expected to be supplied to the command line",
    ),
    "expected_action_mnemonics": attr.string_list(
        doc = "Mnemonics for the actions that should have expected_flags",
    ),
    "no_expected_flags": attr.string_list(
        doc = "Flags not expected to be supplied to the command line",
    ),
}

linker_alignment_flag_arm_test = analysistest.make(
    impl = _test_linker_alignment_flag_impl,
    attrs = test_attrs,
    config_settings = {
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_arm_for_testing",
    },
)

linker_alignment_flag_arm64_test = analysistest.make(
    impl = _test_linker_alignment_flag_impl,
    attrs = test_attrs,
    config_settings = {
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_arm64_for_testing",
    },
)

custom_linker_alignment_flag_arm64_test = analysistest.make(
    impl = _test_linker_alignment_flag_impl,
    attrs = test_attrs,
    config_settings = {
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_arm64_for_testing_custom_linker_alignment",
    },
)

custom_linker_alignment_flag_x86_64_test = analysistest.make(
    impl = _test_linker_alignment_flag_impl,
    attrs = test_attrs,
    config_settings = {
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_x86_64_for_testing_custom_linker_alignment",
    },
)

linker_alignment_flag_x86_test = analysistest.make(
    impl = _test_linker_alignment_flag_impl,
    attrs = test_attrs,
    config_settings = {
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_x86_for_testing",
    },
)

linker_alignment_flag_x86_64_test = analysistest.make(
    impl = _test_linker_alignment_flag_impl,
    attrs = test_attrs,
    config_settings = {
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_x86_64_for_testing",
    },
)

def _test_linker_alignment_flag_arm():
    """ Checks that max-page-size flag is not present for arm targets.
    """
    name = "linker_alignment_flag_arm"
    test_name = name + "_test"

    native.cc_binary(
        name = name,
        srcs = ["foo.cpp"],
        tags = ["manual"],
    )

    linker_alignment_flag_arm_test(
        name = test_name,
        target_under_test = name,
        expected_action_mnemonics = ["CppLink"],
        expected_flags = [],
        no_expected_flags = [
            "-Wl,-z,max-page-size=4096",
            "-Wl,-z,max-page-size=16384",
            "-Wl,-z,max-page-size=65536",
        ],
    )
    return test_name

def _test_linker_alignment_flag_arm64():
    """ Checks that max-page-size flag is present for arm64 targets.
    """
    name = "linker_alignment_flag_arm64"
    test_name = name + "_test"

    native.cc_binary(
        name = name,
        srcs = ["foo.cpp"],
        tags = ["manual"],
    )

    linker_alignment_flag_arm64_test(
        name = test_name,
        target_under_test = name,
        expected_action_mnemonics = ["CppLink"],
        expected_flags = [
            "-Wl,-z,max-page-size=4096",
        ],
        no_expected_flags = [],
    )
    return test_name

def _test_custom_linker_alignment_flag_arm64():
    """ Checks that max-page-size flag has the custom alignment for arm64.
    """
    name = "custom_linker_alignment_flag_arm64"
    test_name = name + "_test"

    native.cc_binary(
        name = name,
        srcs = ["foo.cpp"],
        tags = ["manual"],
    )

    custom_linker_alignment_flag_arm64_test(
        name = test_name,
        target_under_test = name,
        expected_action_mnemonics = ["CppLink"],
        expected_flags = [
            "-Wl,-z,max-page-size=16384",
        ],
        no_expected_flags = [],
    )
    return test_name

def _test_linker_alignment_flag_x86():
    """ Checks that max-page-size flag is not present for x86.
    """
    name = "linker_alignment_flag_x86"
    test_name = name + "_test"

    native.cc_binary(
        name = name,
        srcs = ["foo.cpp"],
        tags = ["manual"],
    )

    linker_alignment_flag_x86_test(
        name = test_name,
        target_under_test = name,
        expected_action_mnemonics = ["CppCompile", "CppLink"],
        expected_flags = [],
        no_expected_flags = [
            "-Wl,-z,max-page-size=4096",
            "-Wl,-z,max-page-size=16384",
            "-Wl,-z,max-page-size=65536",
        ],
    )
    return test_name

def _test_linker_alignment_flag_x86_64():
    """ Checks that max-page-size flag is present for x86_64.
    """
    name = "linker_alignment_flag_x86_64"
    test_name = name + "_test"

    native.cc_binary(
        name = name,
        srcs = ["foo.cpp"],
        tags = ["manual"],
    )

    linker_alignment_flag_x86_64_test(
        name = test_name,
        target_under_test = name,
        expected_action_mnemonics = ["CppLink"],
        expected_flags = [
            "-Wl,-z,max-page-size=4096",
        ],
        no_expected_flags = [],
    )
    return test_name

def _test_custom_linker_alignment_flag_x86_64():
    """ Checks that max-page-size flag has the custom alignment for x86_64.
    """
    name = "custom_linker_alignment_flag_x86_64"
    test_name = name + "_test"

    native.cc_binary(
        name = name,
        srcs = ["foo.cpp"],
        tags = ["manual"],
    )

    custom_linker_alignment_flag_x86_64_test(
        name = test_name,
        target_under_test = name,
        expected_action_mnemonics = ["CppLink"],
        expected_flags = [
            "-Wl,-z,max-page-size=65536",
        ],
        no_expected_flags = [],
    )
    return test_name

def cc_toolchain_features_linker_alignment_test_suite(name):
    native.test_suite(
        name = name,
        tests = [
            _test_linker_alignment_flag_arm(),
            _test_linker_alignment_flag_arm64(),
            _test_custom_linker_alignment_flag_arm64(),
            _test_linker_alignment_flag_x86(),
            _test_linker_alignment_flag_x86_64(),
            _test_custom_linker_alignment_flag_x86_64(),
        ],
    )