xref: /aosp_15_r20/build/bazel/rules/cc/generate_toc.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A function to generate table of contents files of symbols from a shared library."""
16
17CcTocInfo = provider(
18    "Information about the table of contents of a shared library",
19    fields = {
20        "toc": "The single file for the table of contents",
21    },
22)
23
24def generate_toc(ctx, name, input_file):
25    so_name = "lib" + name + ".so"
26    toc_name = so_name + ".toc"
27    out_file = ctx.actions.declare_file(toc_name)
28    d_file = ctx.actions.declare_file(toc_name + ".d")
29    ctx.actions.run(
30        env = {
31            "CLANG_BIN": ctx.executable._readelf.dirname,
32        },
33        inputs = [input_file],
34        tools = [
35            ctx.executable._readelf,
36        ],
37        outputs = [out_file, d_file],
38        executable = ctx.executable._toc_script,
39        arguments = [
40            # Only Linux shared libraries for now.
41            "--elf",
42            "-i",
43            input_file.path,
44            "-o",
45            out_file.path,
46            "-d",
47            d_file.path,
48        ],
49        mnemonic = "GenerateToc",
50    )
51    return CcTocInfo(toc = out_file)
52