1""" 2Copyright (C) 2023 The Android Open Source Project 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15""" 16 17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 18load("//build/bazel/rules/cc:cc_prebuilt_library_shared.bzl", "cc_prebuilt_library_shared") 19load("//build/bazel/rules/test_common:paths.bzl", "get_output_and_package_dir_based_path") 20 21_fake_expected_lib = "{[()]}" 22 23def _cc_prebuilt_library_shared_test_impl(ctx): 24 env = analysistest.begin(ctx) 25 target = analysistest.target_under_test(env) 26 expected_lib = ctx.attr.expected_lib 27 cc_info = target[CcInfo] 28 compilation_context = cc_info.compilation_context 29 linker_inputs = cc_info.linking_context.linker_inputs.to_list() 30 libs_to_link = [] 31 for lib in linker_inputs: 32 libs_to_link += lib.libraries 33 34 if expected_lib == _fake_expected_lib: 35 asserts.true( 36 env, 37 len(libs_to_link) == 0, 38 "\nExpected the shared library to be empty, but instead got:\n\t%s\n" % str(libs_to_link), 39 ) 40 else: 41 asserts.true( 42 env, 43 expected_lib in [lib.dynamic_library.basename for lib in libs_to_link], 44 "\nExpected the target to include the shared library %s; but instead got:\n\t%s\n" % (expected_lib, libs_to_link), 45 ) 46 47 # Checking for the expected {,system_}includes 48 assert_template = "\nExpected the %s for " + expected_lib + " to be:\n\t%s\n, but instead got:\n\t%s\n" 49 expand_paths = lambda paths: [get_output_and_package_dir_based_path(env, p) for p in paths] 50 expected_includes = expand_paths(ctx.attr.expected_includes) 51 expected_system_includes = expand_paths(ctx.attr.expected_system_includes) 52 53 includes = compilation_context.includes.to_list() 54 for include in expected_includes: 55 asserts.true(env, include in includes, assert_template % ("includes", expected_includes, includes)) 56 57 system_includes = compilation_context.system_includes.to_list() 58 for include in expected_system_includes: 59 asserts.true(env, include in system_includes, assert_template % ("system_includes", expected_system_includes, system_includes)) 60 61 return analysistest.end(env) 62 63_cc_prebuilt_library_shared_test = analysistest.make( 64 _cc_prebuilt_library_shared_test_impl, 65 attrs = dict( 66 expected_lib = attr.string(default = _fake_expected_lib), 67 expected_includes = attr.string_list(), 68 expected_system_includes = attr.string_list(), 69 ), 70) 71 72def _cc_prebuilt_library_shared_simple(): 73 name = "_cc_prebuilt_library_shared_simple" 74 test_name = name + "_test" 75 lib = "libfoo.so" 76 77 cc_prebuilt_library_shared( 78 name = name, 79 shared_library = lib, 80 tags = ["manual"], 81 ) 82 _cc_prebuilt_library_shared_test( 83 name = test_name, 84 target_under_test = name, 85 expected_lib = lib, 86 ) 87 88 return test_name 89 90def _cc_prebuilt_library_shared_default_library_field(): 91 name = "_cc_prebuilt_library_shared_default_library_field" 92 test_name = name + "_test" 93 lib = None 94 95 cc_prebuilt_library_shared( 96 name = name, 97 shared_library = lib, 98 tags = ["manual"], 99 ) 100 _cc_prebuilt_library_shared_test( 101 name = test_name, 102 target_under_test = name, 103 expected_lib = lib, # We expect the default of _fake_expected_lib 104 ) 105 106 return test_name 107 108def _cc_prebuilt_library_shared_has_all_includes(): 109 name = "_cc_prebuilt_library_shared_has_all_includes" 110 test_name = name + "_test" 111 lib = "libfoo.so" 112 includes = ["includes"] 113 system_includes = ["system_includes"] 114 115 cc_prebuilt_library_shared( 116 name = name, 117 shared_library = lib, 118 export_includes = includes, 119 export_system_includes = system_includes, 120 tags = ["manual"], 121 ) 122 _cc_prebuilt_library_shared_test( 123 name = test_name, 124 target_under_test = name, 125 expected_lib = lib, 126 expected_includes = includes, 127 expected_system_includes = system_includes, 128 ) 129 130 return test_name 131 132def cc_prebuilt_library_shared_test_suite(name): 133 native.test_suite( 134 name = name, 135 tests = [ 136 _cc_prebuilt_library_shared_simple(), 137 _cc_prebuilt_library_shared_default_library_field(), 138 _cc_prebuilt_library_shared_has_all_includes(), 139 ], 140 ) 141