-
Notifications
You must be signed in to change notification settings - Fork 11
/
rules.bzl
110 lines (94 loc) · 2.97 KB
/
rules.bzl
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
load("@rules_pkg//pkg:providers.bzl", "PackageVariablesInfo")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_bats//:rules.bzl", "bats_test")
# Taken from Bazel repository
def _basic_naming_impl(ctx):
values = {}
# Copy attributes from the rule to the provider
values["product_name"] = ctx.attr.product_name
values["version"] = ctx.attr.version
values["revision"] = ctx.attr.revision
values["compilation_mode"] = ctx.var.get("COMPILATION_MODE")
return PackageVariablesInfo(values = values)
### RUSTC BUILD ARG RULE ###
# A rule to inject variables from the build file into package names.
basic_naming = rule(
implementation = _basic_naming_impl,
attrs = {
"product_name": attr.string(
doc = "Placeholder for our final product name.",
),
"revision": attr.string(
doc = "Placeholder for our release revision.",
),
"version": attr.string(
doc = "Placeholder for our release version.",
),
},
)
# Taken from https://github.com/bazelbuild/rules_rust/issues/801
def _rustc_flags_file_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name + ".txt")
cfg_lines = [
"--cfg\nfeature=\"%s\"" % flag.label.name
for flag in ctx.attr.flags
if flag[BuildSettingInfo].value
]
ctx.actions.write(
output = out,
content = "\n".join(cfg_lines),
)
return [DefaultInfo(files = depset([out]))]
rustc_flags_file = rule(
implementation = _rustc_flags_file_impl,
attrs = {
"flags": attr.label_list(),
},
)
### END RUSTC BUILD ARG RULE ###
### RUN MAKE RULE ###
# Bazel rule to _run_ Makefile commands
#
# Note: This rule is not meant to be used in other projects
def _run_make(ctx):
executable = ctx.actions.declare_file("run-make.sh")
# TODO: This is dumb, fix
cmds = ["cd docker && make -f %s %s" % (ctx.file.src.short_path, ctx.attr.cmd)]
ctx.actions.write(
content = "#!/bin/bash\n" + " && ".join(cmds),
output = executable,
is_executable = True,
)
return executable
def _run_make_impl(ctx):
executable = _run_make(ctx)
runfiles = ctx.runfiles(files = ctx.files.data)
transitive_runfiles = []
for runfiles_attr in (
ctx.attr.data,
):
for target in runfiles_attr:
transitive_runfiles.append(target[DefaultInfo].default_runfiles)
runfiles = runfiles.merge_all(transitive_runfiles)
return DefaultInfo(
executable = executable,
runfiles = runfiles,
)
run_make = rule(
attrs = {
"src": attr.label(
allow_single_file = True,
doc = "Makefile file name",
),
"data": attr.label_list(
allow_files = True,
doc = "Data dependencies",
),
"cmd": attr.string(
doc = "Makefile command",
),
},
executable = True,
implementation = _run_make_impl,
)
### END RUN MAKE RULE ###