• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 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"""
16Proguard
17"""
18
19load("//java/common:java_semantics.bzl", "semantics")
20load("//java/common:proguard_spec_info.bzl", "ProguardSpecInfo")
21
22visibility("private")
23
24def _filter_provider(provider, *attrs):
25    return [dep[provider] for attr in attrs for dep in attr if provider in dep]
26
27def _validate_spec(ctx, spec_file):
28    validated_proguard_spec = ctx.actions.declare_file(
29        "validated_proguard/%s/%s_valid" % (ctx.label.name, spec_file.path),
30    )
31
32    toolchain = semantics.find_java_toolchain(ctx)
33
34    args = ctx.actions.args()
35    args.add("--path", spec_file)
36    args.add("--output", validated_proguard_spec)
37
38    ctx.actions.run(
39        mnemonic = "ValidateProguard",
40        progress_message = "Validating proguard configuration %{input}",
41        executable = toolchain.proguard_allowlister,
42        arguments = [args],
43        inputs = [spec_file],
44        outputs = [validated_proguard_spec],
45        toolchain = Label(semantics.JAVA_TOOLCHAIN_TYPE),
46    )
47
48    return validated_proguard_spec
49
50def validate_proguard_specs(ctx, proguard_specs = [], transitive_attrs = []):
51    """
52    Creates actions that validate Proguard specification and returns ProguardSpecProvider.
53
54    Use transtive_attrs parameter to collect Proguard validations from `deps`,
55    `runtime_deps`, `exports`, `plugins`, and `exported_plugins` attributes.
56
57    Args:
58      ctx: (RuleContext) Used to register the actions.
59      proguard_specs: (list[File]) List of Proguard specs files.
60      transitive_attrs: (list[list[Target]])  Attributes to collect transitive
61        proguard validations from.
62    Returns:
63      (ProguardSpecProvider) A ProguardSpecProvider.
64    """
65    proguard_validations = _filter_provider(ProguardSpecInfo, *transitive_attrs)
66    return ProguardSpecInfo(
67        depset(
68            [_validate_spec(ctx, spec_file) for spec_file in proguard_specs],
69            transitive = [validation.specs for validation in proguard_validations],
70        ),
71    )
72