github.com/ajguerrer/rules_go@v0.20.3/extras/bindata.bzl (about) 1 # Copyright 2018 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 load( 16 "@io_bazel_rules_go//go:def.bzl", 17 "go_context", 18 ) 19 load( 20 "@io_bazel_rules_go//go/private:rules/rule.bzl", 21 "go_rule", 22 ) 23 24 def _bindata_impl(ctx): 25 go = go_context(ctx) 26 out = go.declare_file(go, ext = ".go") 27 arguments = ctx.actions.args() 28 arguments.add_all([ 29 "-o", 30 out, 31 "-pkg", 32 ctx.attr.package, 33 "-prefix", 34 ctx.label.package, 35 ]) 36 if not ctx.attr.compress: 37 arguments.add("-nocompress") 38 if not ctx.attr.metadata: 39 arguments.add("-nometadata") 40 if not ctx.attr.memcopy: 41 arguments.add("-nomemcopy") 42 if not ctx.attr.modtime: 43 arguments.add_all(["-modtime", "0"]) 44 if ctx.attr.extra_args: 45 arguments.add_all(ctx.attr.extra_args) 46 srcs = [f.path for f in ctx.files.srcs] 47 if ctx.attr.strip_external and any([f.startswith("external/") for f in srcs]): 48 arguments.add("-prefix", ctx.label.workspace_root + "/" + ctx.label.package) 49 arguments.add_all(srcs) 50 ctx.actions.run( 51 inputs = ctx.files.srcs, 52 outputs = [out], 53 mnemonic = "GoBindata", 54 executable = ctx.executable._bindata, 55 arguments = [arguments], 56 ) 57 return [ 58 DefaultInfo( 59 files = depset([out]), 60 ), 61 ] 62 63 bindata = go_rule( 64 _bindata_impl, 65 attrs = { 66 "srcs": attr.label_list(allow_files = True), 67 "package": attr.string(mandatory = True), 68 "compress": attr.bool(default = True), 69 "metadata": attr.bool(default = False), 70 "memcopy": attr.bool(default = True), 71 "modtime": attr.bool(default = False), 72 "strip_external": attr.bool(default = False), 73 "extra_args": attr.string_list(), 74 "_bindata": attr.label( 75 executable = True, 76 cfg = "host", 77 default = "@com_github_kevinburke_go_bindata//go-bindata:go-bindata", 78 ), 79 }, 80 )