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