• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:dicts.bzl", "dicts")
16load("//build/bazel/platforms:rule_utilities.bzl", "ARCH_CONSTRAINT_ATTRS", "get_arch")
17
18# This file contains the implementation for the cc_stub_library rule.
19#
20# TODO(b/207812332):
21# - ndk_api_coverage_parser: https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/coverage.go;l=248-262;drc=master
22
23CcStubInfo = provider(
24    fields = {
25        "stub_map": "The .map file containing library symbols for the specific API version.",
26        "version": "The API version of this library.",
27        "abi_symbol_list": "A plain-text list of all symbols of this library for the specific API version."
28    }
29)
30
31def _cc_stub_gen_impl(ctx):
32    # The name of this target.
33    name = ctx.attr.name
34
35    # All declared outputs of ndkstubgen.
36    out_stub_c = ctx.actions.declare_file("/".join([name, "stub.c"]))
37    out_stub_map = ctx.actions.declare_file("/".join([name, "stub.map"]))
38    out_abi_symbol_list = ctx.actions.declare_file("/".join([name, "abi_symbol_list.txt"]))
39
40    outputs = [out_stub_c, out_stub_map, out_abi_symbol_list]
41
42    arch = get_arch(ctx)
43
44    ndkstubgen_args = ctx.actions.args()
45    ndkstubgen_args.add_all(["--arch", arch])
46    ndkstubgen_args.add_all(["--api", ctx.attr.version])
47    ndkstubgen_args.add_all(["--api-map", ctx.file._api_levels_file])
48    # TODO(b/207812332): This always parses and builds the stub library as a dependency of an APEX. Parameterize this
49    # for non-APEX use cases.
50    ndkstubgen_args.add_all(["--apex", ctx.file.symbol_file])
51    ndkstubgen_args.add_all(outputs)
52    ctx.actions.run(
53        executable = ctx.executable._ndkstubgen,
54        inputs = [
55            ctx.file.symbol_file,
56            ctx.file._api_levels_file,
57        ],
58        outputs = outputs,
59        arguments = [ndkstubgen_args],
60    )
61
62    return [
63        # DefaultInfo.files contains the .stub.c file only so that this target
64        # can be used directly in the srcs of a cc_library.
65        DefaultInfo(files = depset([out_stub_c])),
66        CcStubInfo(
67            stub_map = out_stub_map,
68            abi_symbol_list = out_abi_symbol_list,
69            version = ctx.attr.version,
70        ),
71    ]
72
73cc_stub_gen = rule(
74    implementation = _cc_stub_gen_impl,
75    attrs = dicts.add({
76        # Public attributes
77        "symbol_file": attr.label(mandatory = True, allow_single_file = [".map.txt"]),
78        "version": attr.string(mandatory = True, default = "current"),
79        # Private attributes
80        "_api_levels_file": attr.label(default = "@soong_injection//api_levels:api_levels.json", allow_single_file = True),
81        # TODO(b/199038020): Use //build/soong/cc/ndkstubgen when py_runtime is set up on CI for hermetic python usage.
82        # "_ndkstubgen": attr.label(default = "@make_injection//:host/linux-x86/bin/ndkstubgen", executable = True, cfg = "host", allow_single_file = True),
83        "_ndkstubgen": attr.label(default = "//build/soong/cc/ndkstubgen", executable = True, cfg = "host"),
84    }, ARCH_CONSTRAINT_ATTRS),
85)
86
87