blob: 1381a057dcf756b1f180e9dec990248b6149903f [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):
Carmelo Casconed33d3b42019-06-18 12:12:36 -070016 outjar = 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
Carmelo Casconed33d3b42019-06-18 12:12:36 -070023 java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo]
24 jar_path = "%s/bin/jar" % java_runtime.java_home
25
Ray Milkey5063f5b2018-08-15 16:22:30 -070026 cmd = [
Carmelo Casconed33d3b42019-06-18 12:12:36 -070027 "for sj in %s; do %s xf $sj; done" % (src_list, jar_path),
Ray Milkey5063f5b2018-08-15 16:22:30 -070028 "dir=$(find . -type d -name java)",
Carmelo Casconed33d3b42019-06-18 12:12:36 -070029 "[ -n \"$dir\" -a -d \"$dir\" ] && %s cf %s -C $dir ." % (jar_path, outjar.path),
Ray Milkey5063f5b2018-08-15 16:22:30 -070030 ]
Thomas Vachuska50ac0982018-07-19 10:17:37 -070031
Ray Milkey5063f5b2018-08-15 16:22:30 -070032 ctx.action(
33 inputs = ctx.files.srcs,
Carmelo Casconed33d3b42019-06-18 12:12:36 -070034 outputs = [outjar],
Ray Milkey5063f5b2018-08-15 16:22:30 -070035 progress_message = "Generating source jar for %s" % ctx.attr.name,
36 command = ";\n".join(cmd),
Carmelo Casconed33d3b42019-06-18 12:12:36 -070037 tools = java_runtime.files,
Ray Milkey5063f5b2018-08-15 16:22:30 -070038 )
Thomas Vachuska50ac0982018-07-19 10:17:37 -070039
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070040def _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 Milkey5063f5b2018-08-15 16:22:30 -070053 fail("None of the given input files is a valid source jar (%s)" %
54 ", ".join([s.path for s in ctx.files.srcs]))
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070055 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 Milkey5063f5b2018-08-15 16:22:30 -070060 progress_message = "Generating source jar %s" % out_files[i].basename,
61 command = cmd,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070062 )
63 return DefaultInfo(files = depset(out_files))
64
65"""
66Creates a single source jar file from a set of .srcjar files.
67
68Args:
69 srcs: List of source files. Only files ending with .srcjar will be considered.
70"""
71
Thomas Vachuska50ac0982018-07-19 10:17:37 -070072java_sources = rule(
73 attrs = {
74 "srcs": attr.label_list(allow_files = True),
Carmelo Casconed33d3b42019-06-18 12:12:36 -070075 "_jdk": attr.label(
76 default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
77 providers = [java_common.JavaRuntimeInfo],
78 ),
Thomas Vachuska50ac0982018-07-19 10:17:37 -070079 },
80 implementation = _impl,
Ray Milkey5063f5b2018-08-15 16:22:30 -070081 outputs = {"jar": "%{name}.jar"},
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070082)
83
84"""
85Returns a collection of source jar files ending with .srcjar from the given list of java_library or file labels.
86Input files are expected to end with "-src.jar".
87
88Args:
89 srcs: List of java_library labels.
90
91"""
92
93java_sources_alt = rule(
94 attrs = {
95 "srcs": attr.label_list(allow_files = True),
96 },
97 implementation = _impl_alt,
98)