Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 1 | # Copyright 2015 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 | def _impl(ctx): |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 16 | outjar = ctx.outputs.jar |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 18 | src_list = "" |
| 19 | for src in ctx.files.srcs: |
| 20 | if src.path.endswith(".srcjar"): |
| 21 | src_list += " " + src.path |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 22 | |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 23 | java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo] |
| 24 | jar_path = "%s/bin/jar" % java_runtime.java_home |
| 25 | |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 26 | cmd = [ |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 27 | "for sj in %s; do %s xf $sj; done" % (src_list, jar_path), |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 28 | "dir=$(find . -type d -name java)", |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 29 | "[ -n \"$dir\" -a -d \"$dir\" ] && %s cf %s -C $dir ." % (jar_path, outjar.path), |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 30 | ] |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 31 | |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 32 | ctx.action( |
| 33 | inputs = ctx.files.srcs, |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 34 | outputs = [outjar], |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 35 | progress_message = "Generating source jar for %s" % ctx.attr.name, |
| 36 | command = ";\n".join(cmd), |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 37 | tools = java_runtime.files, |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 38 | ) |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 39 | |
Carmelo Cascone | 6a1ae71 | 2018-08-10 12:19:47 -0700 | [diff] [blame] | 40 | def _impl_alt(ctx): |
| 41 | ending = "-src.jar" |
| 42 | src_files = [] |
| 43 | out_files = [] |
| 44 | if len(ctx.files.srcs) == 0: |
| 45 | fail("Cannot generate source jars from and empty input") |
| 46 | for src in ctx.files.srcs: |
| 47 | if src.path.endswith(ending): |
| 48 | prefix = src.path[:-len(ending)] |
| 49 | src_files.append(src) |
| 50 | out_file = ctx.actions.declare_file(prefix + ".srcjar") |
| 51 | out_files.append(out_file) |
| 52 | if len(src_files) == 0: |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 53 | fail("None of the given input files is a valid source jar (%s)" % |
| 54 | ", ".join([s.path for s in ctx.files.srcs])) |
Carmelo Cascone | 6a1ae71 | 2018-08-10 12:19:47 -0700 | [diff] [blame] | 55 | for i in range(len(src_files)): |
| 56 | cmd = "cp %s %s" % (src_files[i].path, out_files[i].path) |
| 57 | ctx.actions.run_shell( |
| 58 | inputs = [src_files[i]], |
| 59 | outputs = [out_files[i]], |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 60 | progress_message = "Generating source jar %s" % out_files[i].basename, |
| 61 | command = cmd, |
Carmelo Cascone | 6a1ae71 | 2018-08-10 12:19:47 -0700 | [diff] [blame] | 62 | ) |
| 63 | return DefaultInfo(files = depset(out_files)) |
| 64 | |
| 65 | """ |
| 66 | Creates a single source jar file from a set of .srcjar files. |
| 67 | |
| 68 | Args: |
| 69 | srcs: List of source files. Only files ending with .srcjar will be considered. |
| 70 | """ |
| 71 | |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 72 | java_sources = rule( |
| 73 | attrs = { |
| 74 | "srcs": attr.label_list(allow_files = True), |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 75 | "_jdk": attr.label( |
| 76 | default = Label("@bazel_tools//tools/jdk:current_java_runtime"), |
| 77 | providers = [java_common.JavaRuntimeInfo], |
| 78 | ), |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 79 | }, |
| 80 | implementation = _impl, |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 81 | outputs = {"jar": "%{name}.jar"}, |
Carmelo Cascone | 6a1ae71 | 2018-08-10 12:19:47 -0700 | [diff] [blame] | 82 | ) |
| 83 | |
| 84 | """ |
| 85 | Returns a collection of source jar files ending with .srcjar from the given list of java_library or file labels. |
| 86 | Input files are expected to end with "-src.jar". |
| 87 | |
| 88 | Args: |
| 89 | srcs: List of java_library labels. |
| 90 | |
| 91 | """ |
| 92 | |
| 93 | java_sources_alt = rule( |
| 94 | attrs = { |
| 95 | "srcs": attr.label_list(allow_files = True), |
| 96 | }, |
| 97 | implementation = _impl_alt, |
| 98 | ) |