github.com/ajguerrer/rules_go@v0.20.3/extras/embed_data.bzl (about) 1 # Copyright 2017 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/private:context.bzl", #TODO: This ought to be def 17 "go_context", 18 ) 19 load( 20 "@io_bazel_rules_go//go/private:rules/rule.bzl", 21 "go_rule", 22 ) 23 24 def _go_embed_data_impl(ctx): 25 go = go_context(ctx) 26 if ctx.attr.src and ctx.attr.srcs: 27 fail("%s: src and srcs attributes cannot both be specified" % ctx.label) 28 if ctx.attr.src and ctx.attr.flatten: 29 fail("%s: src and flatten attributes cannot both be specified" % ctx.label) 30 31 args = ctx.actions.args() 32 if ctx.attr.src: 33 srcs = [ctx.file.src] 34 else: 35 srcs = ctx.files.srcs 36 args.add("-multi") 37 38 if ctx.attr.package: 39 package = ctx.attr.package 40 else: 41 _, _, package = ctx.label.package.rpartition("/") 42 if package == "": 43 fail("%s: must provide package attribute for go_embed_data rules in the repository root directory" % ctx.label) 44 45 out = go.declare_file(go, ext = ".go") 46 args.add_all([ 47 "-workspace", 48 ctx.workspace_name, 49 "-label", 50 str(ctx.label), 51 "-out", 52 out, 53 "-package", 54 package, 55 "-var", 56 ctx.attr.var, 57 ]) 58 if ctx.attr.flatten: 59 args.add("-flatten") 60 if ctx.attr.string: 61 args.add("-string") 62 if ctx.attr.unpack: 63 args.add("-unpack") 64 args.add("-multi") 65 args.add_all(srcs) 66 67 library = go.new_library(go, srcs = srcs) 68 source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented()) 69 70 ctx.actions.run( 71 outputs = [out], 72 inputs = srcs, 73 executable = ctx.executable._embed, 74 arguments = [args], 75 mnemonic = "GoSourcesData", 76 ) 77 return [ 78 DefaultInfo(files = depset([out])), 79 library, 80 source, 81 ] 82 83 go_embed_data = go_rule( 84 implementation = _go_embed_data_impl, 85 attrs = { 86 "package": attr.string(), 87 "var": attr.string(default = "Data"), 88 "src": attr.label(allow_single_file = True), 89 "srcs": attr.label_list(allow_files = True), 90 "flatten": attr.bool(), 91 "unpack": attr.bool(), 92 "string": attr.bool(), 93 "_embed": attr.label( 94 default = "@io_bazel_rules_go//go/tools/builders:embed", 95 executable = True, 96 cfg = "host", 97 ), 98 }, 99 ) 100 """See go/extras.rst#go_embed_data for full documentation."""