aboutsummaryrefslogtreecommitdiff
path: root/rules/cc/cc_prebuilt_library_static_test.bzl
blob: 5a386e4ce1ef6c22c6122244ab648f1df07129fe (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
"""
Copyright (C) 2022 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")
load("//build/bazel/rules/cc:cc_prebuilt_library_static.bzl", "cc_prebuilt_library_static")

def _cc_prebuilt_library_static_alwayslink_test_impl(ctx):
    env = analysistest.begin(ctx)
    target = analysistest.target_under_test(env)
    expected_lib = ctx.attr.expected_lib
    cc_info = target[CcInfo]
    linker_inputs = cc_info.linking_context.linker_inputs.to_list()
    libs_to_link = []
    for l in linker_inputs:
        libs_to_link += l.libraries

    has_alwayslink = False
    libs = {}
    for lib_to_link in libs_to_link:
        lib = lib_to_link.static_library.basename
        libs[lib_to_link.static_library] = lib_to_link.alwayslink
        if lib == expected_lib:
            has_alwayslink = lib_to_link.alwayslink
        if has_alwayslink:
            break
    asserts.true(env, has_alwayslink, "\nExpected to find the static library `%s` unconditionally in the linker_input, with alwayslink set:\n\t%s" % (expected_lib, str(libs)))

    return analysistest.end(env)

_cc_prebuilt_library_static_alwayslink_test = analysistest.make(
    _cc_prebuilt_library_static_alwayslink_test_impl,
    attrs = {"expected_lib": attr.string()},
)

def _cc_prebuilt_library_static_given_alwayslink_lib():
    name = "_cc_prebuilt_library_static_given_alwayslink_lib"
    test_name = name + "_test"
    lib = "libfoo.a"

    cc_prebuilt_library_static(
        name = name,
        static_library = lib,
        alwayslink = True,
        tags = ["manual"],
    )

    _cc_prebuilt_library_static_alwayslink_test(
        name = test_name,
        target_under_test = name,
        expected_lib = lib,
    )

    return test_name

def cc_prebuilt_library_static_test_suite(name):
    native.test_suite(
        name = name,
        tests = [
            _cc_prebuilt_library_static_given_alwayslink_lib(),
        ],
    )