xref: /aosp_15_r20/build/bazel/rules/linker_config.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2022 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
15load("@bazel_skylib//lib:paths.bzl", "paths")
16load("//build/bazel/rules:prebuilt_file.bzl", "PrebuiltFileInfo")
17
18def _linker_config_impl(ctx):
19    output_file = ctx.actions.declare_file(paths.replace_extension(ctx.file.src.basename, ".pb"))
20
21    args = ctx.actions.args()
22    args.add("proto")
23    args.add("-s", ctx.file.src.path)
24    args.add("-o", output_file.path)
25
26    ctx.actions.run(
27        outputs = [output_file],
28        inputs = [ctx.file.src],
29        executable = ctx.executable._conv_linker_config,
30        arguments = [args],
31    )
32
33    return [
34        DefaultInfo(
35            files = depset([output_file]),
36        ),
37        PrebuiltFileInfo(
38            src = output_file,
39            dir = "etc",
40            filename = "linker.config.pb",
41        ),
42    ]
43
44linker_config = rule(
45    doc = """
46    linker_config generates protobuf file from json file. This protobuf file will
47    be used from linkerconfig while generating ld.config.txt. Format of this file
48    can be found from
49    https://android.googlesource.com/platform/system/linkerconfig/+/master/README.md
50    """,
51    implementation = _linker_config_impl,
52    attrs = {
53        "src": attr.label(allow_single_file = [".json"], mandatory = True, doc = "source linker configuration property file"),
54        "_conv_linker_config": attr.label(default = "//build/soong/scripts:conv_linker_config", cfg = "exec", executable = True),
55    },
56)
57