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(":cc_binary_test.bzl", "strip_test_assert_flags") 18load(":cc_prebuilt_binary.bzl", "cc_prebuilt_binary") 19 20def _cc_prebuilt_binary_basic_test_impl(ctx): 21 env = analysistest.begin(ctx) 22 target = analysistest.target_under_test(env) 23 outs = target[DefaultInfo].files.to_list() 24 asserts.true( 25 env, 26 len(outs) == 1, 27 "expected there to be 1 output but got:\n" + str(outs), 28 ) 29 return analysistest.end(env) 30 31_cc_prebuilt_binary_basic_test = analysistest.make(_cc_prebuilt_binary_basic_test_impl) 32 33def _cc_prebuilt_binary_simple_test(): 34 name = "cc_prebuilt_binary_simple" 35 cc_prebuilt_binary( 36 name = name, 37 src = "bin", 38 tags = ["manual"], 39 ) 40 test_name = name + "_test" 41 _cc_prebuilt_binary_basic_test( 42 name = test_name, 43 target_under_test = name, 44 ) 45 return test_name 46 47def _cc_prebuilt_binary_stripping_flags_test_impl(ctx): 48 env = analysistest.begin(ctx) 49 actions = analysistest.target_actions(env) 50 strip_acts = [a for a in actions if a.mnemonic == "CcStrip"] 51 has_strip = len(strip_acts) > 0 52 asserts.true( 53 env, 54 has_strip, 55 "expected to find an action with CcStrip mnemonic in:\n%s" % actions, 56 ) 57 if has_strip: 58 strip_test_assert_flags(env, strip_acts[0], ctx.attr.strip_flags) 59 return analysistest.end(env) 60 61__cc_prebuilt_binary_stripping_flags_test = analysistest.make( 62 _cc_prebuilt_binary_stripping_flags_test_impl, 63 attrs = dict( 64 strip_flags = attr.string_list(), 65 ), 66) 67 68def _cc_prebuilt_binary_stripping_flags_test(**kwargs): 69 __cc_prebuilt_binary_stripping_flags_test( 70 target_compatible_with = ["//build/bazel_common_rules/platforms/os:android"], 71 **kwargs 72 ) 73 74def _cc_prebuilt_binary_strip_keep_symbols_test(): 75 name = "cc_prebuilt_binary_strip_keep_symbols" 76 cc_prebuilt_binary( 77 name = name, 78 src = "bin", 79 keep_symbols = True, 80 tags = ["manual"], 81 ) 82 test_name = name + "_test" 83 _cc_prebuilt_binary_stripping_flags_test( 84 name = test_name, 85 target_under_test = name, 86 strip_flags = [ 87 "--keep-symbols", 88 "--add-gnu-debuglink", 89 ], 90 ) 91 return test_name 92 93def _cc_prebuilt_binary_strip_keep_symbols_and_debug_frame_test(): 94 name = "cc_prebuilt_binary_strip_keep_symbols_and_debug_frame" 95 cc_prebuilt_binary( 96 name = name, 97 src = "bin", 98 keep_symbols_and_debug_frame = True, 99 tags = ["manual"], 100 ) 101 test_name = name + "_test" 102 _cc_prebuilt_binary_stripping_flags_test( 103 name = test_name, 104 target_under_test = name, 105 strip_flags = [ 106 "--keep-symbols-and-debug-frame", 107 "--add-gnu-debuglink", 108 ], 109 ) 110 return test_name 111 112def _cc_prebuilt_binary_strip_keep_symbols_list_test(): 113 name = "cc_prebuilt_binary_strip_keep_symbols_list" 114 symbols = ["foo", "bar", "baz"] 115 cc_prebuilt_binary( 116 name = name, 117 src = "bin", 118 keep_symbols_list = symbols, 119 tags = ["manual"], 120 ) 121 test_name = name + "_test" 122 _cc_prebuilt_binary_stripping_flags_test( 123 name = test_name, 124 target_under_test = name, 125 strip_flags = [ 126 "-k" + ",".join(symbols), 127 "--add-gnu-debuglink", 128 ], 129 ) 130 return test_name 131 132def _cc_prebuilt_binary_strip_all_test(): 133 name = "cc_prebuilt_binary_strip_all" 134 cc_prebuilt_binary( 135 name = name, 136 src = "bin", 137 all = True, 138 tags = ["manual"], 139 ) 140 test_name = name + "_test" 141 _cc_prebuilt_binary_stripping_flags_test( 142 name = test_name, 143 target_under_test = name, 144 strip_flags = [ 145 "--add-gnu-debuglink", 146 ], 147 ) 148 return test_name 149 150def _cc_prebuilt_binary_no_stripping_action_test_impl(ctx): 151 env = analysistest.begin(ctx) 152 actions = analysistest.target_actions(env) 153 mnemonics = [a.mnemonic for a in actions] 154 has_strip = "CcStrip" in mnemonics 155 asserts.false( 156 env, 157 has_strip, 158 "expected no action with CcStrip mnemonic in:\n%s" % actions, 159 ) 160 return analysistest.end(env) 161 162__cc_prebuilt_binary_no_stripping_action_test = analysistest.make( 163 _cc_prebuilt_binary_no_stripping_action_test_impl, 164) 165 166def _cc_prebuilt_binary_no_stripping_action_test(**kwargs): 167 __cc_prebuilt_binary_no_stripping_action_test( 168 target_compatible_with = ["//build/bazel_common_rules/platforms/os:android"], 169 **kwargs 170 ) 171 172def _cc_prebuilt_binary_strip_none_test(): 173 name = "cc_prebuilt_binary_strip_none" 174 cc_prebuilt_binary( 175 name = name, 176 src = "bin", 177 none = True, 178 tags = ["manual"], 179 ) 180 test_name = name + "_test" 181 _cc_prebuilt_binary_no_stripping_action_test( 182 name = test_name, 183 target_under_test = name, 184 ) 185 return test_name 186 187def _cc_prebuilt_binary_host_test(**kwargs): 188 __cc_prebuilt_binary_no_stripping_action_test( 189 target_compatible_with = select({ 190 "//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"], 191 "//conditions:default": [], 192 }), 193 **kwargs 194 ) 195 196def _cc_prebuilt_binary_no_strip_host_test(): 197 name = "cc_prebuilt_binary_no_strip_host" 198 cc_prebuilt_binary( 199 name = name, 200 src = "bin", 201 tags = ["manual"], 202 ) 203 test_name = name + "_test" 204 _cc_prebuilt_binary_host_test( 205 name = test_name, 206 target_under_test = name, 207 ) 208 return test_name 209 210def cc_prebuilt_binary_test_suite(name): 211 native.test_suite( 212 name = name, 213 tests = [ 214 _cc_prebuilt_binary_simple_test(), 215 _cc_prebuilt_binary_strip_none_test(), 216 _cc_prebuilt_binary_strip_keep_symbols_test(), 217 _cc_prebuilt_binary_strip_keep_symbols_and_debug_frame_test(), 218 _cc_prebuilt_binary_strip_keep_symbols_list_test(), 219 _cc_prebuilt_binary_strip_all_test(), 220 _cc_prebuilt_binary_no_strip_host_test(), 221 ], 222 ) 223