1""" 2Copyright (C) 2021 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 17"""A macro to generate table of contents files of symbols from a shared library.""" 18 19CcTocInfo = provider( 20 "Information about the table of contents of a shared library", 21 fields = { 22 "toc": "The single file for the table of contents", 23 }, 24) 25 26def _shared_library_toc_impl(ctx): 27 so_name = "lib" + ctx.attr.name + ".so" 28 toc_name = so_name + ".toc" 29 out_file = ctx.actions.declare_file(toc_name) 30 d_file = ctx.actions.declare_file(toc_name + ".d") 31 ctx.actions.run( 32 env = { 33 "CLANG_BIN": ctx.executable._readelf.dirname, 34 }, 35 inputs = ctx.files.src, 36 tools = [ 37 ctx.executable._readelf, 38 ], 39 outputs = [out_file, d_file], 40 executable = ctx.executable._toc_script, 41 arguments = [ 42 # Only Linux shared libraries for now. 43 "--elf", 44 "-i", 45 ctx.files.src[0].path, 46 "-o", 47 out_file.path, 48 "-d", 49 d_file.path, 50 ], 51 ) 52 53 return [ 54 CcTocInfo(toc = out_file), 55 DefaultInfo(files = depset([out_file])), 56 ] 57 58shared_library_toc = rule( 59 implementation = _shared_library_toc_impl, 60 attrs = { 61 "src": attr.label( 62 # TODO(b/217908237): reenable allow_single_file 63 # allow_single_file = True, 64 mandatory = True, 65 ), 66 "_toc_script": attr.label( 67 cfg = "host", 68 executable = True, 69 allow_single_file = True, 70 default = "//build/soong/scripts:toc.sh", 71 ), 72 "_readelf": attr.label( 73 cfg = "host", 74 executable = True, 75 allow_single_file = True, 76 default = "//prebuilts/clang/host/linux-x86:llvm-readelf", 77 ), 78 }, 79 toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], 80) 81