|  | 
|  | 1 | +"""Tests for swift_binary's additional_linker_inputs attribute.""" | 
|  | 2 | + | 
|  | 3 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "unittest") | 
|  | 4 | + | 
|  | 5 | +def _swift_binary_additional_linker_inputs_test_impl(ctx): | 
|  | 6 | +    env = analysistest.begin(ctx) | 
|  | 7 | + | 
|  | 8 | +    actions = analysistest.target_actions(env) | 
|  | 9 | +    link_actions = [ | 
|  | 10 | +        action | 
|  | 11 | +        for action in actions | 
|  | 12 | +        if action.mnemonic == "CppLink" | 
|  | 13 | +    ] | 
|  | 14 | + | 
|  | 15 | +    if not link_actions: | 
|  | 16 | +        unittest.fail( | 
|  | 17 | +            env, | 
|  | 18 | +            "Expected to find a CppLink action but found none. Available actions: {}".format( | 
|  | 19 | +                [action.mnemonic for action in actions], | 
|  | 20 | +            ), | 
|  | 21 | +        ) | 
|  | 22 | +        return analysistest.end(env) | 
|  | 23 | + | 
|  | 24 | +    if len(link_actions) != 1: | 
|  | 25 | +        unittest.fail( | 
|  | 26 | +            env, | 
|  | 27 | +            "Expected exactly one CppLink action, but found {}".format(len(link_actions)), | 
|  | 28 | +        ) | 
|  | 29 | +        return analysistest.end(env) | 
|  | 30 | + | 
|  | 31 | +    link_action = link_actions[0] | 
|  | 32 | + | 
|  | 33 | +    expected_inputs = ctx.attr.expected_additional_inputs | 
|  | 34 | +    expected_linkopts = ctx.attr.expected_linkopts | 
|  | 35 | + | 
|  | 36 | +    action_input_paths = set([input.short_path for input in link_action.inputs.to_list()]) | 
|  | 37 | + | 
|  | 38 | +    if expected_inputs: | 
|  | 39 | +        missing_inputs = set(expected_inputs) - action_input_paths | 
|  | 40 | +        if missing_inputs: | 
|  | 41 | +            unittest.fail( | 
|  | 42 | +                env, | 
|  | 43 | +                "Missing expected additional linker inputs: {}. Available inputs: {}".format( | 
|  | 44 | +                    sorted(missing_inputs), | 
|  | 45 | +                    sorted(action_input_paths), | 
|  | 46 | +                ), | 
|  | 47 | +            ) | 
|  | 48 | + | 
|  | 49 | +    if expected_linkopts: | 
|  | 50 | +        missing_linkopts = [] | 
|  | 51 | +        for expected_linkopt in expected_linkopts: | 
|  | 52 | +            # Use substring match since -Wl, options may be grouped with other flags | 
|  | 53 | +            found = False | 
|  | 54 | +            for arg in link_action.argv: | 
|  | 55 | +                if expected_linkopt in arg: | 
|  | 56 | +                    found = True | 
|  | 57 | +                    break | 
|  | 58 | +            if not found: | 
|  | 59 | +                missing_linkopts.append(expected_linkopt) | 
|  | 60 | + | 
|  | 61 | +        if missing_linkopts: | 
|  | 62 | +            unittest.fail( | 
|  | 63 | +                env, | 
|  | 64 | +                "Missing expected linkopts: {}. Link arguments were: {}".format( | 
|  | 65 | +                    missing_linkopts, | 
|  | 66 | +                    link_action.argv, | 
|  | 67 | +                ), | 
|  | 68 | +            ) | 
|  | 69 | + | 
|  | 70 | +    return analysistest.end(env) | 
|  | 71 | + | 
|  | 72 | +swift_binary_additional_linker_inputs_test = analysistest.make( | 
|  | 73 | +    _swift_binary_additional_linker_inputs_test_impl, | 
|  | 74 | +    attrs = { | 
|  | 75 | +        "expected_additional_inputs": attr.string_list( | 
|  | 76 | +        ), | 
|  | 77 | +        "expected_linkopts": attr.string_list( | 
|  | 78 | +        ), | 
|  | 79 | +    }, | 
|  | 80 | +) | 
|  | 81 | + | 
|  | 82 | +def additional_linker_inputs_test_suite(name, tags = []): | 
|  | 83 | +    all_tags = [name] + tags | 
|  | 84 | + | 
|  | 85 | +    swift_binary_additional_linker_inputs_test( | 
|  | 86 | +        name = "{}_with_additional_inputs".format(name), | 
|  | 87 | +        target_under_test = "//test/fixtures/linking:bin_with_additional_linker_inputs", | 
|  | 88 | +        expected_additional_inputs = ["test/fixtures/linking/test_data.bin"], | 
|  | 89 | +        expected_linkopts = ["-Wl,-sectcreate,__TEXT,__test_section"], | 
|  | 90 | +        tags = all_tags, | 
|  | 91 | +    ) | 
|  | 92 | + | 
|  | 93 | +    swift_binary_additional_linker_inputs_test( | 
|  | 94 | +        name = "{}_without_additional_inputs".format(name), | 
|  | 95 | +        target_under_test = "//test/fixtures/linking:bin_without_additional_linker_inputs", | 
|  | 96 | +        expected_additional_inputs = [], | 
|  | 97 | +        expected_linkopts = [], | 
|  | 98 | +        tags = all_tags, | 
|  | 99 | +    ) | 
|  | 100 | + | 
|  | 101 | +    native.test_suite( | 
|  | 102 | +        name = name, | 
|  | 103 | +        tags = all_tags, | 
|  | 104 | +    ) | 
0 commit comments