aboutsummaryrefslogtreecommitdiff
path: root/examples/vendor/constant_gen/defs.bzl
blob: e54fcee5b995d804581af5d181f2cb3c5b2cf5f8 (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
"""A trivial rule to turn a string into a C++ constant."""

# Copyright 2020 Google LLC
#
# 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
#
# https://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.

def _constant_gen_impl(ctx):
    # Turn text into a C++ constant.
    outputs = [ctx.outputs.src_out]
    ctx.actions.run(
        mnemonic = "GenerateConstant",
        progress_message = "Generating %s" % ctx.attr.var,
        outputs = outputs,
        executable = ctx.executable._generator,
        arguments = [ctx.outputs.src_out.path, ctx.attr.var, ctx.attr.text],
    )
    return [DefaultInfo(files = depset(outputs))]

_constant_gen = rule(
    implementation = _constant_gen_impl,
    attrs = {
        "src_out": attr.output(mandatory = True),
        "text": attr.string(mandatory = True),
        "var": attr.string(mandatory = False),
        "_generator": attr.label(
            default = Label("@rules_license//examples/vendor/constant_gen:constant_generator"),
            executable = True,
            allow_files = True,
            cfg = "exec",
        ),
    },
)

def constant_gen(name, text, var):
    # Generate the code
    _constant_gen(
        name = name + "_src_",
        src_out = name + "_src_.cc",
        text = text,
        var = var,
        applicable_licenses = ["@rules_license//examples/vendor/constant_gen:license_for_emitted_code"],
    )

    # And turn it into a library we can link against
    native.cc_library(
        name = name,
        srcs = [name + "_src_"],
        applicable_licenses = ["@rules_license//examples/vendor/constant_gen:license_for_emitted_code"],
    )