xref: /aosp_15_r20/external/bazelbuild-rules_go/extras/bindata.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
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"""bindata.bzl provides the bindata rule for embedding data in .go files"""
16
17load(
18    "//go:def.bzl",
19    "go_context",
20)
21load(
22    "//go/private:go_toolchain.bzl",
23    "GO_TOOLCHAIN",
24)
25
26def _bindata_impl(ctx):
27    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 `bindata` rule is deprecated and will be removed in rules_go version 0.39.")
28    go = go_context(ctx)
29    out = go.declare_file(go, ext = ".go")
30    arguments = ctx.actions.args()
31    arguments.add_all([
32        "-o",
33        out,
34        "-pkg",
35        ctx.attr.package,
36        "-prefix",
37        ctx.label.package,
38    ])
39    if not ctx.attr.compress:
40        arguments.add("-nocompress")
41    if not ctx.attr.metadata:
42        arguments.add("-nometadata")
43    if not ctx.attr.memcopy:
44        arguments.add("-nomemcopy")
45    if not ctx.attr.modtime:
46        arguments.add_all(["-modtime", "0"])
47    if ctx.attr.extra_args:
48        arguments.add_all(ctx.attr.extra_args)
49    srcs = [f.path for f in ctx.files.srcs]
50    if ctx.attr.strip_external and any([f.startswith("external/") for f in srcs]):
51        arguments.add("-prefix", ctx.label.workspace_root + "/" + ctx.label.package)
52    arguments.add_all(srcs)
53    ctx.actions.run(
54        inputs = ctx.files.srcs,
55        outputs = [out],
56        mnemonic = "GoBindata",
57        executable = ctx.executable._bindata,
58        arguments = [arguments],
59    )
60    return [
61        DefaultInfo(
62            files = depset([out]),
63        ),
64    ]
65
66bindata = rule(
67    implementation = _bindata_impl,
68    attrs = {
69        "srcs": attr.label_list(allow_files = True),
70        "package": attr.string(mandatory = True),
71        "compress": attr.bool(default = True),
72        "metadata": attr.bool(default = False),
73        "memcopy": attr.bool(default = True),
74        "modtime": attr.bool(default = False),
75        "strip_external": attr.bool(default = False),
76        "extra_args": attr.string_list(),
77        "_bindata": attr.label(
78            executable = True,
79            cfg = "exec",
80            default = "@com_github_kevinburke_go_bindata//go-bindata:go-bindata",
81        ),
82        "_go_context_data": attr.label(
83            default = "//:go_context_data",
84        ),
85    },
86    toolchains = [GO_TOOLCHAIN],
87)
88