-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathflags.bzl
More file actions
66 lines (50 loc) · 1.41 KB
/
flags.bzl
File metadata and controls
66 lines (50 loc) · 1.41 KB
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
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo", "string_flag")
def _string_flag_file_impl(ctx):
flavor_value = ctx.attr.flag[BuildSettingInfo].value
output_file = ctx.actions.declare_file(ctx.outputs.output.basename)
ctx.actions.write(output_file, flavor_value)
return [DefaultInfo(files = depset([output_file]))]
string_flag_file = rule(
implementation = _string_flag_file_impl,
attrs = {
"flag": attr.label(),
},
outputs = {
"output": "%{name}_output.txt",
},
)
def string_flag_output(name, flag, default = ""):
"""
Given the following BUILD usage
```starlark
load("@envoy_toolshed//:flags.bzl", "string_flag_output")
string_flag_output(
name = "soup",
flag = "flavor",
default = "brocoli",
)
genrule(
name = "dinner",
outs = ["dinner.txt"],
cmd = "echo \"Soup: $$(cat $(location :soup))\" > $@",
srcs = [":soup"],
)
```
You can specify the flavour like so:
```console
$ bazel build //:dinner --//:flavor=carrot
```
Which with above example will create a file containing:
```text
Soup: carrot
```
`default` is optional and defaults to an empty string.
"""
string_flag(
name = flag,
build_setting_default = default,
)
string_flag_file(
name = name,
flag = ":%s" % flag,
)