forked from antonovvk/bazel_rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.bzl
91 lines (79 loc) · 2.28 KB
/
config.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
def _gen_config_impl(ctx):
output = ctx.outputs.out
content = ""
for k, v in ctx.attr.values.items():
content += "#define %s %s\n" % (k, v)
ctx.file_action(
output = output,
content = content
)
gen_config_rule = rule(
implementation = _gen_config_impl,
attrs = {
"values": attr.string_dict(mandatory=True),
"file": attr.string(mandatory=True),
},
outputs = {"out": "%{file}"},
output_to_genfiles = True,
)
def cc_gen_config(name, file, values, visibility=None):
config = gen_config_rule(
name = name + "_impl",
file = file,
values = values,
)
native.cc_library(
name = name,
hdrs = [file],
visibility = visibility,
)
def _fix_config_impl(ctx):
input = ctx.file.file
output = ctx.outputs.out
script = ""
for k, v in ctx.attr.values.items():
if ctx.attr.cmake:
script += "s/\#cmakedefine[\s]+%s([\s]+\$\{%s\})?/\#define %s %s/g;" % (k, k, k, v)
else:
script += "s/\@%s\@/%s/g;" % (k, v)
if ctx.attr.cmake:
script += "s/\#cmakedefine[\s]+([^\s]+)([\s]+\$\{.+\})?//g"
else:
script += "s/\@[^\@]*\@/0/g"
ctx.action(
inputs = [input],
outputs = [output],
progress_message = "Configuring %s" % input.short_path,
command = "perl -pe '%s' < %s > %s" % (script, input.path, output.path)
)
fix_config_rule = rule(
implementation = _fix_config_impl,
attrs = {
"file": attr.label(
mandatory=True,
allow_files=True,
single_file=True
),
"cmake": attr.bool(default=False, mandatory=False),
"output": attr.string(mandatory=True),
"values": attr.string_dict(mandatory=True),
},
outputs = {"out": "%{output}"},
output_to_genfiles = True,
)
def cc_fix_config(name, files, values, cmake=False, visibility=None):
hdrs = []
for input, output in files.items():
fix_config_rule(
name = input + "_impl",
file = input,
cmake = cmake,
output = output,
values = values,
)
hdrs.append(output)
native.cc_library(
name = name,
hdrs = hdrs,
visibility = visibility,
)