• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 The Bazel Authors. All rights reserved.
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"""Custom android_library for use in test.bzl"""
16
17load(
18    "//rules:attrs.bzl",
19    _attrs = "attrs",
20)
21load(
22    "//rules:java.bzl",
23    _java = "java",
24)
25load(
26    "//rules:processing_pipeline.bzl",
27    _ProviderInfo = "ProviderInfo",
28    _processing_pipeline = "processing_pipeline",
29)
30load(
31    "//rules/android_library:attrs.bzl",
32    _BASE_ATTRS = "ATTRS",
33)
34load(
35    "//rules/android_library:impl.bzl",
36    _BASE_PROCESSORS = "PROCESSORS",
37    _finalize = "finalize",
38)
39load(
40    "//rules/android_library:rule.bzl",
41    _make_rule = "make_rule",
42)
43
44CustomProviderInfo = provider(
45    doc = "Custom provider to provide",
46    fields = dict(
47        key = "Some key to provide",
48    ),
49)
50
51def _process_custom_provider(ctx, **_unused_sub_ctxs):
52    return _ProviderInfo(
53        name = "custom_provider_ctx",
54        value = struct(
55            providers = [
56                CustomProviderInfo(
57                    key = ctx.attr.key,
58                ),
59            ],
60        ),
61    )
62
63PROCESSORS = _processing_pipeline.append(
64    _BASE_PROCESSORS,
65    CustomProviderInfoProcessor = _process_custom_provider,
66)
67
68_PROCESSING_PIPELINE = _processing_pipeline.make_processing_pipeline(
69    processors = PROCESSORS,
70    finalize = _finalize,
71)
72
73def _impl(ctx):
74    java_package = _java.resolve_package_from_label(ctx.label, ctx.attr.custom_package)
75    return _processing_pipeline.run(ctx, java_package, _PROCESSING_PIPELINE)
76
77custom_android_library = _make_rule(
78    implementation = _impl,
79    attrs = _attrs.add(_BASE_ATTRS, dict(
80        # Custom attribute to wrap in a provider
81        key = attr.string(),
82    )),
83    additional_providers = [
84        CustomProviderInfo,
85    ],
86)
87