1*882aa7c7SMatt Gilbride# Copyright 2019 Google LLC 2*882aa7c7SMatt Gilbride# 3*882aa7c7SMatt Gilbride# Redistribution and use in source and binary forms, with or without 4*882aa7c7SMatt Gilbride# modification, are permitted provided that the following conditions are 5*882aa7c7SMatt Gilbride# met: 6*882aa7c7SMatt Gilbride# 7*882aa7c7SMatt Gilbride# * Redistributions of source code must retain the above copyright 8*882aa7c7SMatt Gilbride# notice, this list of conditions and the following disclaimer. 9*882aa7c7SMatt Gilbride# * Redistributions in binary form must reproduce the above 10*882aa7c7SMatt Gilbride# copyright notice, this list of conditions and the following disclaimer 11*882aa7c7SMatt Gilbride# in the documentation and/or other materials provided with the 12*882aa7c7SMatt Gilbride# distribution. 13*882aa7c7SMatt Gilbride# * Neither the name of Google LLC nor the names of its 14*882aa7c7SMatt Gilbride# contributors may be used to endorse or promote products derived from 15*882aa7c7SMatt Gilbride# this software without specific prior written permission. 16*882aa7c7SMatt Gilbride# 17*882aa7c7SMatt Gilbride# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*882aa7c7SMatt Gilbride# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*882aa7c7SMatt Gilbride# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20*882aa7c7SMatt Gilbride# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21*882aa7c7SMatt Gilbride# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22*882aa7c7SMatt Gilbride# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23*882aa7c7SMatt Gilbride# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*882aa7c7SMatt Gilbride# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*882aa7c7SMatt Gilbride# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*882aa7c7SMatt Gilbride# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27*882aa7c7SMatt Gilbride# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*882aa7c7SMatt Gilbride 29*882aa7c7SMatt Gilbridedef java_tests(name, srcs, runtime_deps, size): 30*882aa7c7SMatt Gilbride classNames = [] 31*882aa7c7SMatt Gilbride for src in srcs: 32*882aa7c7SMatt Gilbride # convert .java file path to fully qualified class name 33*882aa7c7SMatt Gilbride className = src[(src.index("/com/") + 1):-5].replace("/", ".") 34*882aa7c7SMatt Gilbride classNames.append(className) 35*882aa7c7SMatt Gilbride native.java_test( 36*882aa7c7SMatt Gilbride name = className, 37*882aa7c7SMatt Gilbride test_class = className, 38*882aa7c7SMatt Gilbride runtime_deps = runtime_deps, 39*882aa7c7SMatt Gilbride size = size, 40*882aa7c7SMatt Gilbride ) 41*882aa7c7SMatt Gilbride if classNames: 42*882aa7c7SMatt Gilbride native.test_suite( 43*882aa7c7SMatt Gilbride name = name, 44*882aa7c7SMatt Gilbride tests = classNames, 45*882aa7c7SMatt Gilbride ) 46*882aa7c7SMatt Gilbride 47*882aa7c7SMatt Gilbridedef google_java_format(name, srcs, formatter): 48*882aa7c7SMatt Gilbride native.genrule( 49*882aa7c7SMatt Gilbride name = name, 50*882aa7c7SMatt Gilbride outs = ["%s.sh" % name], 51*882aa7c7SMatt Gilbride srcs = srcs, 52*882aa7c7SMatt Gilbride # TODO: this may fail if list of files is too long (exceeds max command line limit in shell). 53*882aa7c7SMatt Gilbride # Split the command into multiple executions if this ever fails (good enough for now) 54*882aa7c7SMatt Gilbride cmd = "echo ' $(location %s) --replace $(SRCS)' > $@" % formatter, 55*882aa7c7SMatt Gilbride executable = True, 56*882aa7c7SMatt Gilbride tools = [formatter], 57*882aa7c7SMatt Gilbride local = 1, 58*882aa7c7SMatt Gilbride ) 59*882aa7c7SMatt Gilbride 60*882aa7c7SMatt Gilbridedef _google_java_format_verification_impl(ctx): 61*882aa7c7SMatt Gilbride src_files = [src.path for src in ctx.files.srcs] 62*882aa7c7SMatt Gilbride output_file = ctx.outputs.output_file 63*882aa7c7SMatt Gilbride formatter = ctx.executable.formatter 64*882aa7c7SMatt Gilbride 65*882aa7c7SMatt Gilbride ctx.actions.run_shell( 66*882aa7c7SMatt Gilbride inputs = ctx.files.srcs, 67*882aa7c7SMatt Gilbride arguments = ["--dry-run", "--set-exit-if-changed"] + src_files, 68*882aa7c7SMatt Gilbride tools = [formatter], 69*882aa7c7SMatt Gilbride command = "%s $@ > %s" % (formatter.path, output_file.path), 70*882aa7c7SMatt Gilbride outputs = [output_file], 71*882aa7c7SMatt Gilbride progress_message = 72*882aa7c7SMatt Gilbride "If this target fails check the list of files that must be formatted in %s" % output_file.path, 73*882aa7c7SMatt Gilbride ) 74*882aa7c7SMatt Gilbride 75*882aa7c7SMatt Gilbridegoogle_java_format_verification = rule( 76*882aa7c7SMatt Gilbride attrs = { 77*882aa7c7SMatt Gilbride "srcs": attr.label_list(allow_files = True), 78*882aa7c7SMatt Gilbride "formatter": attr.label( 79*882aa7c7SMatt Gilbride executable = True, 80*882aa7c7SMatt Gilbride cfg = "host", 81*882aa7c7SMatt Gilbride ), 82*882aa7c7SMatt Gilbride }, 83*882aa7c7SMatt Gilbride outputs = {"output_file": "%{name}.txt"}, 84*882aa7c7SMatt Gilbride implementation = _google_java_format_verification_impl, 85*882aa7c7SMatt Gilbride) 86