blob: 734d048e74fb93239f759c9c851a26577b590b5f [file] [log] [blame]
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# 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.
def _impl(ctx):
jar = ctx.outputs.jar
src_list = ""
for src in ctx.files.srcs:
if src.path.endswith(".srcjar"):
src_list += " " + src.path
cmd = [
"for sj in %s; do jar xf $sj; done" % src_list,
"dir=$(find . -type d -name java)",
"[ -n \"$dir\" -a -d \"$dir\" ] && jar cf %s -C $dir ." % jar.path,
]
ctx.action(
inputs = ctx.files.srcs,
outputs = [jar],
progress_message = "Generating source jar for %s" % ctx.attr.name,
command = ";\n".join(cmd)
)
def _impl_alt(ctx):
ending = "-src.jar"
src_files = []
out_files = []
if len(ctx.files.srcs) == 0:
fail("Cannot generate source jars from and empty input")
for src in ctx.files.srcs:
if src.path.endswith(ending):
prefix = src.path[:-len(ending)]
src_files.append(src)
out_file = ctx.actions.declare_file(prefix + ".srcjar")
out_files.append(out_file)
if len(src_files) == 0:
fail("None of the given input files is a valid source jar (%s)"
% ", ".join([s.path for s in ctx.files.srcs]))
for i in range(len(src_files)):
cmd = "cp %s %s" % (src_files[i].path, out_files[i].path)
ctx.actions.run_shell(
inputs = [src_files[i]],
outputs = [out_files[i]],
progress_message = "Generating source jar %s" % out_files[i].basename,
command = cmd
)
return DefaultInfo(files = depset(out_files))
"""
Creates a single source jar file from a set of .srcjar files.
Args:
srcs: List of source files. Only files ending with .srcjar will be considered.
"""
java_sources = rule(
attrs = {
"srcs": attr.label_list(allow_files = True),
},
implementation = _impl,
outputs = {"jar" : "%{name}.jar"},
)
"""
Returns a collection of source jar files ending with .srcjar from the given list of java_library or file labels.
Input files are expected to end with "-src.jar".
Args:
srcs: List of java_library labels.
"""
java_sources_alt = rule(
attrs = {
"srcs": attr.label_list(allow_files = True),
},
implementation = _impl_alt,
)