aboutsummaryrefslogtreecommitdiff
path: root/extras/embed_data.bzl
blob: fb3519e23a35d6632a2198e6f35baf17cc53d4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
    "//go/private:context.bzl",  #TODO: This ought to be def
    "go_context",
)
load(
    "//go/private:go_toolchain.bzl",
    "GO_TOOLCHAIN",
)

_DOC = """**Deprecated**: Will be removed in rules_go 0.39.

`go_embed_data` generates a .go file that contains data from a file or a
list of files. It should be consumed in the srcs list of one of the
[core go rules].

Before using `go_embed_data`, you must add the following snippet to your
WORKSPACE:

``` bzl
load("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies")

go_embed_data_dependencies()
```

`go_embed_data` accepts the attributes listed below.
"""

def _go_embed_data_impl(ctx):
    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.")

    go = go_context(ctx)
    if ctx.attr.src and ctx.attr.srcs:
        fail("%s: src and srcs attributes cannot both be specified" % ctx.label)
    if ctx.attr.src and ctx.attr.flatten:
        fail("%s: src and flatten attributes cannot both be specified" % ctx.label)

    args = ctx.actions.args()
    if ctx.attr.src:
        srcs = [ctx.file.src]
    else:
        srcs = ctx.files.srcs
        args.add("-multi")

    if ctx.attr.package:
        package = ctx.attr.package
    else:
        _, _, package = ctx.label.package.rpartition("/")
        if package == "":
            fail("%s: must provide package attribute for go_embed_data rules in the repository root directory" % ctx.label)

    out = go.declare_file(go, ext = ".go")
    args.add_all([
        "-workspace",
        ctx.workspace_name,
        "-label",
        str(ctx.label),
        "-out",
        out,
        "-package",
        package,
        "-var",
        ctx.attr.var,
    ])
    if ctx.attr.flatten:
        args.add("-flatten")
    if ctx.attr.string:
        args.add("-string")
    if ctx.attr.unpack:
        args.add("-unpack")
        args.add("-multi")
    args.add_all(srcs)

    library = go.new_library(go, srcs = [out])
    source = go.library_to_source(go, {}, library, ctx.coverage_instrumented())

    ctx.actions.run(
        outputs = [out],
        inputs = srcs,
        executable = ctx.executable._embed,
        arguments = [args],
        mnemonic = "GoSourcesData",
    )
    return [
        DefaultInfo(files = depset([out])),
        library,
        source,
    ]

go_embed_data = rule(
    implementation = _go_embed_data_impl,
    doc = _DOC,
    attrs = {
        "package": attr.string(
            doc = "Go package name for the generated .go file.",
        ),
        "var": attr.string(
            default = "Data",
            doc = "Name of the variable that will contain the embedded data.",
        ),
        "src": attr.label(
            allow_single_file = True,
            doc = """A single file to embed. This cannot be used at the same time as `srcs`.
            The generated file will have a variable of type `[]byte` or `string` with the contents of this file.""",
        ),
        "srcs": attr.label_list(
            allow_files = True,
            doc = """A list of files to embed. This cannot be used at the same time as `src`.
            The generated file will have a variable of type `map[string][]byte` or `map[string]string` with the contents
            of each file. The map keys are relative paths of the files from the repository root. Keys for files in external
            repositories will be prefixed with `"external/repo/"` where "repo" is the name of the external repository.""",
        ),
        "flatten": attr.bool(
            doc = "If `True` and `srcs` is used, map keys are file base names instead of relative paths.",
        ),
        "unpack": attr.bool(
            doc = "If `True`, sources are treated as archives and their contents will be stored. Supported formats are `.zip` and `.tar`",
        ),
        "string": attr.bool(
            doc = "If `True`, the embedded data will be stored as `string` instead of `[]byte`.",
        ),
        "_embed": attr.label(
            default = "//go/tools/builders:embed",
            executable = True,
            cfg = "exec",
        ),
        "_go_context_data": attr.label(
            default = "//:go_context_data",
        ),
    },
    toolchains = [GO_TOOLCHAIN],
)
# See /docs/go/extras/extras.md#go_embed_data for full documentation.