1# Copyright 2019 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"""native_binary() and native_test() rule implementations. 16 17These rules let you wrap a pre-built binary or script in a conventional binary 18and test rule respectively. They fulfill the same goal as sh_binary and sh_test 19do, but they run the wrapped binary directly, instead of through Bash, so they 20don't depend on Bash and work with --shell_exectuable="". 21""" 22 23load("//rules/private:copy_file_private.bzl", "copy_bash", "copy_cmd") 24 25def _impl_rule(ctx, is_windows): 26 out = ctx.actions.declare_file(ctx.attr.out) 27 if is_windows: 28 copy_cmd(ctx, ctx.file.src, out) 29 else: 30 copy_bash(ctx, ctx.file.src, out) 31 return DefaultInfo( 32 executable = out, 33 files = depset([out]), 34 runfiles = ctx.runfiles( 35 files = [out], 36 collect_data = True, 37 collect_default = True, 38 ), 39 ) 40 41def _impl(ctx): 42 return _impl_rule(ctx, ctx.attr.is_windows) 43 44_ATTRS = { 45 "src": attr.label( 46 executable = True, 47 allow_single_file = True, 48 mandatory = True, 49 cfg = "host", 50 ), 51 "data": attr.label_list(allow_files = True), 52 # "out" is attr.string instead of attr.output, so that it is select()'able. 53 "out": attr.string(mandatory = True), 54 "is_windows": attr.bool(mandatory = True), 55} 56 57_native_binary = rule( 58 implementation = _impl, 59 attrs = _ATTRS, 60 executable = True, 61) 62 63_native_test = rule( 64 implementation = _impl, 65 attrs = _ATTRS, 66 test = True, 67) 68 69def native_binary(name, src, out, data = None, **kwargs): 70 """Wraps a pre-built binary or script with a binary rule. 71 72 You can "bazel run" this rule like any other binary rule, and use it as a tool in genrule.tools for example. You can also augment the binary with runfiles. 73 74 Args: 75 name: The name of the test rule. 76 src: label; path of the pre-built executable 77 out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.) 78 data: list of labels; data dependencies 79 **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-binaries">common attributes for binaries</a>. 80 """ 81 _native_binary( 82 name = name, 83 src = src, 84 out = out, 85 data = data, 86 is_windows = select({ 87 "@bazel_tools//src/conditions:host_windows": True, 88 "//conditions:default": False, 89 }), 90 **kwargs 91 ) 92 93def native_test(name, src, out, data = None, **kwargs): 94 """Wraps a pre-built binary or script with a test rule. 95 96 You can "bazel test" this rule like any other test rule. You can also augment the binary with 97 runfiles. 98 99 Args: 100 name: The name of the test rule. 101 src: label; path of the pre-built executable 102 out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.) 103 data: list of labels; data dependencies 104 **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>. 105 """ 106 _native_test( 107 name = name, 108 src = src, 109 out = out, 110 data = data, 111 is_windows = select({ 112 "@bazel_tools//src/conditions:host_windows": True, 113 "//conditions:default": False, 114 }), 115 **kwargs 116 ) 117