diff options
author | Alix <agespino@google.com> | 2023-02-27 20:14:26 +0000 |
---|---|---|
committer | Alix <agespino@google.com> | 2023-03-03 22:46:39 +0000 |
commit | ad04fe8cea74945b153012f58b3cf4d73c61b653 (patch) | |
tree | c539a15a2f4458068537dd6ecedc95a8ae6de79c /rules/kotlin | |
parent | b37da0ebf3b04c73bc7da06428d5dafa0a61f9c0 (diff) | |
download | bazel-ad04fe8cea74945b153012f58b3cf4d73c61b653.tar.gz |
resource_strip_prefix support for kotlin rules
created macro for kt_jvm_library to replicate resource_strip_prefix
behavior that is in bazel java_library
Test: built kotlinx_coroutines locally and compared jar file to soong
Bug: 268519061
Change-Id: I22bec2ef94ad8bdde9172dd16715cea2f2eb5f15
Diffstat (limited to 'rules/kotlin')
-rw-r--r-- | rules/kotlin/kt_jvm_library.bzl | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/rules/kotlin/kt_jvm_library.bzl b/rules/kotlin/kt_jvm_library.bzl new file mode 100644 index 00000000..80433a55 --- /dev/null +++ b/rules/kotlin/kt_jvm_library.bzl @@ -0,0 +1,87 @@ +""" +Copyright (C) 2023 The Android Open Source Project + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +load("@rules_kotlin//kotlin:jvm_library.bzl", _kt_jvm_library = "kt_jvm_library") + +def _kotlin_resources_impl(ctx): + output_file = ctx.actions.declare_file("kt_resources.jar") + + args = ctx.actions.args() + args.add("cvf") + args.add(output_file.path) + args.add("-C") + args.add(ctx.attr.resource_strip_prefix) + args.add(".") + + ctx.actions.run( + outputs = [output_file], + inputs = ctx.files.srcs, + executable = ctx.executable._jar, + arguments = [args], + ) + + return [DefaultInfo(files = depset([output_file]))] + +kotlin_resources = rule( + doc = """ + Package srcs into a jar, with the option of stripping a path prefix + """, + implementation = _kotlin_resources_impl, + attrs = { + "srcs": attr.label_list(allow_files = True), + "resource_strip_prefix": attr.string( + doc = """The path prefix to strip from resources. + If specified, this path prefix is stripped from every fil + in the resources attribute. It is an error for a resource + file not to be under this directory. If not specified + (the default), the path of resource file is determined + according to the same logic as the Java package of source + files. For example, a source file at stuff/java/foo/bar/a.txt + will be located at foo/bar/a.txt.""", + ), + "_jar": attr.label(default = "@bazel_tools//tools/jdk:jar", executable = True, cfg = "exec"), + }, +) + +def kt_jvm_library( + name, + deps = None, + resources = None, + resource_strip_prefix = None, + **kwargs): + "Bazel macro wrapping for kt_jvm_library" + + if resource_strip_prefix != None: + java_import_name = name + "resources" + kt_res_jar_name = name + "resources_jar" + native.java_import( + name = java_import_name, + jars = [":" + kt_res_jar_name], + ) + + kotlin_resources( + name = kt_res_jar_name, + srcs = resources, + resource_strip_prefix = resource_strip_prefix, + ) + + deps = deps + [":" + java_import_name] + + _kt_jvm_library( + name = name, + deps = deps, + **kwargs + ) |