blob: a45ae6078a6c24790ec60d1574382abcc42fa4bd [file] [log] [blame]
Thomas Vachuska50ac0982018-07-19 10:17:37 -07001# 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
15def _impl(ctx):
Ray Milkey5063f5b2018-08-15 16:22:30 -070016 jar = ctx.outputs.jar
Thomas Vachuska50ac0982018-07-19 10:17:37 -070017
Ray Milkey5063f5b2018-08-15 16:22:30 -070018 src_list = ""
19 for src in ctx.files.srcs:
20 if src.path.endswith(".srcjar"):
21 src_list += " " + src.path
Thomas Vachuska50ac0982018-07-19 10:17:37 -070022
Ray Milkey5063f5b2018-08-15 16:22:30 -070023 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 Vachuska50ac0982018-07-19 10:17:37 -070028
Ray Milkey5063f5b2018-08-15 16:22:30 -070029 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 Vachuska50ac0982018-07-19 10:17:37 -070035
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070036def _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 Milkey5063f5b2018-08-15 16:22:30 -070049 fail("None of the given input files is a valid source jar (%s)" %
50 ", ".join([s.path for s in ctx.files.srcs]))
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070051 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 Milkey5063f5b2018-08-15 16:22:30 -070056 progress_message = "Generating source jar %s" % out_files[i].basename,
57 command = cmd,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070058 )
59 return DefaultInfo(files = depset(out_files))
60
61"""
62Creates a single source jar file from a set of .srcjar files.
63
64Args:
65 srcs: List of source files. Only files ending with .srcjar will be considered.
66"""
67
Thomas Vachuska50ac0982018-07-19 10:17:37 -070068java_sources = rule(
69 attrs = {
70 "srcs": attr.label_list(allow_files = True),
71 },
72 implementation = _impl,
Ray Milkey5063f5b2018-08-15 16:22:30 -070073 outputs = {"jar": "%{name}.jar"},
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070074)
75
76"""
77Returns a collection of source jar files ending with .srcjar from the given list of java_library or file labels.
78Input files are expected to end with "-src.jar".
79
80Args:
81 srcs: List of java_library labels.
82
83"""
84
85java_sources_alt = rule(
86 attrs = {
87 "srcs": attr.label_list(allow_files = True),
88 },
89 implementation = _impl_alt,
90)