1*9bb1b549SSpandan Das# Copyright 2017 The Bazel Authors. All rights reserved. 2*9bb1b549SSpandan Das# 3*9bb1b549SSpandan Das# Licensed under the Apache License, Version 2.0 (the "License"); 4*9bb1b549SSpandan Das# you may not use this file except in compliance with the License. 5*9bb1b549SSpandan Das# You may obtain a copy of the License at 6*9bb1b549SSpandan Das# 7*9bb1b549SSpandan Das# http://www.apache.org/licenses/LICENSE-2.0 8*9bb1b549SSpandan Das# 9*9bb1b549SSpandan Das# Unless required by applicable law or agreed to in writing, software 10*9bb1b549SSpandan Das# distributed under the License is distributed on an "AS IS" BASIS, 11*9bb1b549SSpandan Das# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9bb1b549SSpandan Das# See the License for the specific language governing permissions and 13*9bb1b549SSpandan Das# limitations under the License. 14*9bb1b549SSpandan Das 15*9bb1b549SSpandan Dasload( 16*9bb1b549SSpandan Das "//go/private:context.bzl", #TODO: This ought to be def 17*9bb1b549SSpandan Das "go_context", 18*9bb1b549SSpandan Das) 19*9bb1b549SSpandan Dasload( 20*9bb1b549SSpandan Das "//go/private:go_toolchain.bzl", 21*9bb1b549SSpandan Das "GO_TOOLCHAIN", 22*9bb1b549SSpandan Das) 23*9bb1b549SSpandan Das 24*9bb1b549SSpandan Das_DOC = """**Deprecated**: Will be removed in rules_go 0.39. 25*9bb1b549SSpandan Das 26*9bb1b549SSpandan Das`go_embed_data` generates a .go file that contains data from a file or a 27*9bb1b549SSpandan Daslist of files. It should be consumed in the srcs list of one of the 28*9bb1b549SSpandan Das[core go rules]. 29*9bb1b549SSpandan Das 30*9bb1b549SSpandan DasBefore using `go_embed_data`, you must add the following snippet to your 31*9bb1b549SSpandan DasWORKSPACE: 32*9bb1b549SSpandan Das 33*9bb1b549SSpandan Das``` bzl 34*9bb1b549SSpandan Dasload("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies") 35*9bb1b549SSpandan Das 36*9bb1b549SSpandan Dasgo_embed_data_dependencies() 37*9bb1b549SSpandan Das``` 38*9bb1b549SSpandan Das 39*9bb1b549SSpandan Das`go_embed_data` accepts the attributes listed below. 40*9bb1b549SSpandan Das""" 41*9bb1b549SSpandan Das 42*9bb1b549SSpandan Dasdef _go_embed_data_impl(ctx): 43*9bb1b549SSpandan Das 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*9bb1b549SSpandan Das 45*9bb1b549SSpandan Das go = go_context(ctx) 46*9bb1b549SSpandan Das if ctx.attr.src and ctx.attr.srcs: 47*9bb1b549SSpandan Das fail("%s: src and srcs attributes cannot both be specified" % ctx.label) 48*9bb1b549SSpandan Das if ctx.attr.src and ctx.attr.flatten: 49*9bb1b549SSpandan Das fail("%s: src and flatten attributes cannot both be specified" % ctx.label) 50*9bb1b549SSpandan Das 51*9bb1b549SSpandan Das args = ctx.actions.args() 52*9bb1b549SSpandan Das if ctx.attr.src: 53*9bb1b549SSpandan Das srcs = [ctx.file.src] 54*9bb1b549SSpandan Das else: 55*9bb1b549SSpandan Das srcs = ctx.files.srcs 56*9bb1b549SSpandan Das args.add("-multi") 57*9bb1b549SSpandan Das 58*9bb1b549SSpandan Das if ctx.attr.package: 59*9bb1b549SSpandan Das package = ctx.attr.package 60*9bb1b549SSpandan Das else: 61*9bb1b549SSpandan Das _, _, package = ctx.label.package.rpartition("/") 62*9bb1b549SSpandan Das if package == "": 63*9bb1b549SSpandan Das fail("%s: must provide package attribute for go_embed_data rules in the repository root directory" % ctx.label) 64*9bb1b549SSpandan Das 65*9bb1b549SSpandan Das out = go.declare_file(go, ext = ".go") 66*9bb1b549SSpandan Das args.add_all([ 67*9bb1b549SSpandan Das "-workspace", 68*9bb1b549SSpandan Das ctx.workspace_name, 69*9bb1b549SSpandan Das "-label", 70*9bb1b549SSpandan Das str(ctx.label), 71*9bb1b549SSpandan Das "-out", 72*9bb1b549SSpandan Das out, 73*9bb1b549SSpandan Das "-package", 74*9bb1b549SSpandan Das package, 75*9bb1b549SSpandan Das "-var", 76*9bb1b549SSpandan Das ctx.attr.var, 77*9bb1b549SSpandan Das ]) 78*9bb1b549SSpandan Das if ctx.attr.flatten: 79*9bb1b549SSpandan Das args.add("-flatten") 80*9bb1b549SSpandan Das if ctx.attr.string: 81*9bb1b549SSpandan Das args.add("-string") 82*9bb1b549SSpandan Das if ctx.attr.unpack: 83*9bb1b549SSpandan Das args.add("-unpack") 84*9bb1b549SSpandan Das args.add("-multi") 85*9bb1b549SSpandan Das args.add_all(srcs) 86*9bb1b549SSpandan Das 87*9bb1b549SSpandan Das library = go.new_library(go, srcs = [out]) 88*9bb1b549SSpandan Das source = go.library_to_source(go, {}, library, ctx.coverage_instrumented()) 89*9bb1b549SSpandan Das 90*9bb1b549SSpandan Das ctx.actions.run( 91*9bb1b549SSpandan Das outputs = [out], 92*9bb1b549SSpandan Das inputs = srcs, 93*9bb1b549SSpandan Das executable = ctx.executable._embed, 94*9bb1b549SSpandan Das arguments = [args], 95*9bb1b549SSpandan Das mnemonic = "GoSourcesData", 96*9bb1b549SSpandan Das ) 97*9bb1b549SSpandan Das return [ 98*9bb1b549SSpandan Das DefaultInfo(files = depset([out])), 99*9bb1b549SSpandan Das library, 100*9bb1b549SSpandan Das source, 101*9bb1b549SSpandan Das ] 102*9bb1b549SSpandan Das 103*9bb1b549SSpandan Dasgo_embed_data = rule( 104*9bb1b549SSpandan Das implementation = _go_embed_data_impl, 105*9bb1b549SSpandan Das doc = _DOC, 106*9bb1b549SSpandan Das attrs = { 107*9bb1b549SSpandan Das "package": attr.string( 108*9bb1b549SSpandan Das doc = "Go package name for the generated .go file.", 109*9bb1b549SSpandan Das ), 110*9bb1b549SSpandan Das "var": attr.string( 111*9bb1b549SSpandan Das default = "Data", 112*9bb1b549SSpandan Das doc = "Name of the variable that will contain the embedded data.", 113*9bb1b549SSpandan Das ), 114*9bb1b549SSpandan Das "src": attr.label( 115*9bb1b549SSpandan Das allow_single_file = True, 116*9bb1b549SSpandan Das doc = """A single file to embed. This cannot be used at the same time as `srcs`. 117*9bb1b549SSpandan Das The generated file will have a variable of type `[]byte` or `string` with the contents of this file.""", 118*9bb1b549SSpandan Das ), 119*9bb1b549SSpandan Das "srcs": attr.label_list( 120*9bb1b549SSpandan Das allow_files = True, 121*9bb1b549SSpandan Das doc = """A list of files to embed. This cannot be used at the same time as `src`. 122*9bb1b549SSpandan Das The generated file will have a variable of type `map[string][]byte` or `map[string]string` with the contents 123*9bb1b549SSpandan Das of each file. The map keys are relative paths of the files from the repository root. Keys for files in external 124*9bb1b549SSpandan Das repositories will be prefixed with `"external/repo/"` where "repo" is the name of the external repository.""", 125*9bb1b549SSpandan Das ), 126*9bb1b549SSpandan Das "flatten": attr.bool( 127*9bb1b549SSpandan Das doc = "If `True` and `srcs` is used, map keys are file base names instead of relative paths.", 128*9bb1b549SSpandan Das ), 129*9bb1b549SSpandan Das "unpack": attr.bool( 130*9bb1b549SSpandan Das doc = "If `True`, sources are treated as archives and their contents will be stored. Supported formats are `.zip` and `.tar`", 131*9bb1b549SSpandan Das ), 132*9bb1b549SSpandan Das "string": attr.bool( 133*9bb1b549SSpandan Das doc = "If `True`, the embedded data will be stored as `string` instead of `[]byte`.", 134*9bb1b549SSpandan Das ), 135*9bb1b549SSpandan Das "_embed": attr.label( 136*9bb1b549SSpandan Das default = "//go/tools/builders:embed", 137*9bb1b549SSpandan Das executable = True, 138*9bb1b549SSpandan Das cfg = "exec", 139*9bb1b549SSpandan Das ), 140*9bb1b549SSpandan Das "_go_context_data": attr.label( 141*9bb1b549SSpandan Das default = "//:go_context_data", 142*9bb1b549SSpandan Das ), 143*9bb1b549SSpandan Das }, 144*9bb1b549SSpandan Das toolchains = [GO_TOOLCHAIN], 145*9bb1b549SSpandan Das) 146*9bb1b549SSpandan Das# See /docs/go/extras/extras.md#go_embed_data for full documentation. 147