• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Copyright (C) 2023 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("@rules_kotlin//kotlin:compiler_opt.bzl", "kt_compiler_opt")
18load("@rules_kotlin//kotlin:jvm_library.bzl", _kt_jvm_library = "kt_jvm_library")
19load("//build/bazel/rules/java:rules.bzl", "java_import")
20load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition", "sdk_transition_attrs")
21
22def _kotlin_resources_impl(ctx):
23    output_file = ctx.actions.declare_file("kt_resources.jar")
24
25    args = ctx.actions.args()
26    args.add("cvf")
27    args.add(output_file.path)
28    args.add("-C")
29    args.add(ctx.attr.resource_strip_prefix)
30    args.add(".")
31
32    ctx.actions.run(
33        outputs = [output_file],
34        inputs = ctx.files.srcs,
35        executable = ctx.executable._jar,
36        arguments = [args],
37    )
38
39    return [DefaultInfo(files = depset([output_file]))]
40
41kotlin_resources = rule(
42    doc = """
43    Package srcs into a jar, with the option of stripping a path prefix
44    """,
45    implementation = _kotlin_resources_impl,
46    attrs = {
47        "srcs": attr.label_list(allow_files = True),
48        "resource_strip_prefix": attr.string(
49            doc = """The path prefix to strip from resources.
50                   If specified, this path prefix is stripped from every fil
51                   in the resources attribute. It is an error for a resource
52                   file not to be under this directory. If not specified
53                   (the default), the path of resource file is determined
54                   according to the same logic as the Java package of source
55                   files. For example, a source file at stuff/java/foo/bar/a.txt
56                    will be located at foo/bar/a.txt.""",
57        ),
58        "_jar": attr.label(default = "@bazel_tools//tools/jdk:jar", executable = True, cfg = "exec"),
59    },
60)
61
62# TODO(b/277801336): document these attributes.
63def kt_jvm_library(
64        name,
65        deps = None,
66        resources = None,
67        resource_strip_prefix = None,
68        kotlincflags = None,
69        java_version = None,
70        sdk_version = None,
71        tags = [],
72        target_compatible_with = [],
73        visibility = None,
74        **kwargs):
75    "Bazel macro wrapping for kt_jvm_library"
76    if resource_strip_prefix != None:
77        java_import_name = name + "resources"
78        kt_res_jar_name = name + "resources_jar"
79        java_import(
80            name = java_import_name,
81            jars = [":" + kt_res_jar_name],
82        )
83
84        kotlin_resources(
85            name = kt_res_jar_name,
86            srcs = resources,
87            resource_strip_prefix = resource_strip_prefix,
88        )
89
90        deps = deps + [":" + java_import_name]
91
92    custom_kotlincopts = None
93    if kotlincflags != None:
94        ktcopts_name = name + "_kotlincopts"
95        kt_compiler_opt(
96            name = ktcopts_name,
97            opts = kotlincflags,
98        )
99        custom_kotlincopts = [":" + ktcopts_name]
100
101    lib_name = name + "_private"
102    _kt_jvm_library(
103        name = lib_name,
104        deps = deps,
105        custom_kotlincopts = custom_kotlincopts,
106        tags = tags + ["manual"],
107        target_compatible_with = target_compatible_with,
108        visibility = ["//visibility:private"],
109        **kwargs
110    )
111
112    kt_jvm_library_sdk_transition(
113        name = name,
114        sdk_version = sdk_version,
115        java_version = java_version,
116        exports = lib_name,
117        tags = tags,
118        target_compatible_with = target_compatible_with,
119        visibility = visibility,
120    )
121
122# The list of providers to forward was determined using cquery on one
123# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at
124# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit
125# test ensures that the wrapper's providers and the wrapped rule's do
126# match.
127def _kt_jvm_library_sdk_transition_impl(ctx):
128    return [
129        ctx.attr.exports[0][JavaInfo],
130        ctx.attr.exports[0][InstrumentedFilesInfo],
131        ctx.attr.exports[0][ProguardSpecProvider],
132        ctx.attr.exports[0][OutputGroupInfo],
133        ctx.attr.exports[0][DefaultInfo],
134    ]
135
136kt_jvm_library_sdk_transition = rule(
137    implementation = _kt_jvm_library_sdk_transition_impl,
138    attrs = sdk_transition_attrs,
139    provides = [JavaInfo],
140)
141