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 15load( 16 "//go/private:context.bzl", #TODO: This ought to be def 17 "go_context", 18) 19load( 20 "//go/private:go_toolchain.bzl", 21 "GO_TOOLCHAIN", 22) 23 24_DOC = """**Deprecated**: Will be removed in rules_go 0.39. 25 26`go_embed_data` generates a .go file that contains data from a file or a 27list of files. It should be consumed in the srcs list of one of the 28[core go rules]. 29 30Before using `go_embed_data`, you must add the following snippet to your 31WORKSPACE: 32 33``` bzl 34load("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies") 35 36go_embed_data_dependencies() 37``` 38 39`go_embed_data` accepts the attributes listed below. 40""" 41 42def _go_embed_data_impl(ctx): 43 print("Embedding is now better handled by using rules_go's built-in embedding functionality (https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_library-embedsrcs). The `go_embed_data` rule is deprecated and will be removed in rules_go version 0.39.") 44 45 go = go_context(ctx) 46 if ctx.attr.src and ctx.attr.srcs: 47 fail("%s: src and srcs attributes cannot both be specified" % ctx.label) 48 if ctx.attr.src and ctx.attr.flatten: 49 fail("%s: src and flatten attributes cannot both be specified" % ctx.label) 50 51 args = ctx.actions.args() 52 if ctx.attr.src: 53 srcs = [ctx.file.src] 54 else: 55 srcs = ctx.files.srcs 56 args.add("-multi") 57 58 if ctx.attr.package: 59 package = ctx.attr.package 60 else: 61 _, _, package = ctx.label.package.rpartition("/") 62 if package == "": 63 fail("%s: must provide package attribute for go_embed_data rules in the repository root directory" % ctx.label) 64 65 out = go.declare_file(go, ext = ".go") 66 args.add_all([ 67 "-workspace", 68 ctx.workspace_name, 69 "-label", 70 str(ctx.label), 71 "-out", 72 out, 73 "-package", 74 package, 75 "-var", 76 ctx.attr.var, 77 ]) 78 if ctx.attr.flatten: 79 args.add("-flatten") 80 if ctx.attr.string: 81 args.add("-string") 82 if ctx.attr.unpack: 83 args.add("-unpack") 84 args.add("-multi") 85 args.add_all(srcs) 86 87 library = go.new_library(go, srcs = [out]) 88 source = go.library_to_source(go, {}, library, ctx.coverage_instrumented()) 89 90 ctx.actions.run( 91 outputs = [out], 92 inputs = srcs, 93 executable = ctx.executable._embed, 94 arguments = [args], 95 mnemonic = "GoSourcesData", 96 ) 97 return [ 98 DefaultInfo(files = depset([out])), 99 library, 100 source, 101 ] 102 103go_embed_data = rule( 104 implementation = _go_embed_data_impl, 105 doc = _DOC, 106 attrs = { 107 "package": attr.string( 108 doc = "Go package name for the generated .go file.", 109 ), 110 "var": attr.string( 111 default = "Data", 112 doc = "Name of the variable that will contain the embedded data.", 113 ), 114 "src": attr.label( 115 allow_single_file = True, 116 doc = """A single file to embed. This cannot be used at the same time as `srcs`. 117 The generated file will have a variable of type `[]byte` or `string` with the contents of this file.""", 118 ), 119 "srcs": attr.label_list( 120 allow_files = True, 121 doc = """A list of files to embed. This cannot be used at the same time as `src`. 122 The generated file will have a variable of type `map[string][]byte` or `map[string]string` with the contents 123 of each file. The map keys are relative paths of the files from the repository root. Keys for files in external 124 repositories will be prefixed with `"external/repo/"` where "repo" is the name of the external repository.""", 125 ), 126 "flatten": attr.bool( 127 doc = "If `True` and `srcs` is used, map keys are file base names instead of relative paths.", 128 ), 129 "unpack": attr.bool( 130 doc = "If `True`, sources are treated as archives and their contents will be stored. Supported formats are `.zip` and `.tar`", 131 ), 132 "string": attr.bool( 133 doc = "If `True`, the embedded data will be stored as `string` instead of `[]byte`.", 134 ), 135 "_embed": attr.label( 136 default = "//go/tools/builders:embed", 137 executable = True, 138 cfg = "exec", 139 ), 140 "_go_context_data": attr.label( 141 default = "//:go_context_data", 142 ), 143 }, 144 toolchains = [GO_TOOLCHAIN], 145) 146# See /docs/go/extras/extras.md#go_embed_data for full documentation. 147