-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.bzl
124 lines (107 loc) · 3.15 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def _set_header_impl(ctx):
output = ctx.outputs.out
ctx.actions.write(
output = output,
content = ctx.attr.data
)
set_header_rule = rule(
implementation = _set_header_impl,
attrs = {
"data": attr.string(mandatory=True),
"file": attr.string(mandatory=True),
},
outputs = {"out": "%{file}"},
output_to_genfiles = True,
)
def cc_set_header(name, file, data, visibility=None):
config = set_header_rule(
name = name + "_impl",
file = file,
data = data,
)
native.cc_library(
name = name,
hdrs = [file],
visibility = visibility,
)
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.actions.write(
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():
v = v.replace('\\', '\\\\').replace('/', '\\/')
if ctx.attr.cmake:
script += r"s/\#cmakedefine\s+%s\b.*/\#define %s %s/g;" % (k, k, v)
script += r"s/\#cmakedefine01\s+%s\b.*/\#define %s 1/g;" % (k, k)
script += r"s/\$\{%s\}/%s/g;" % (k, v)
script += r"s/\@%s\@/%s/g;" % (k, v)
if ctx.attr.cmake:
script += r"s/\#cmakedefine[\s]+(\w+).*/\/* #undef \1 *\//g;"
script += r"s/\#cmakedefine01[\s]+(\w+).*/\#define \1 0/g;"
script += r"s/\$\{\w+\}//g;"
script += r"s/\@[^\@]*\@/0/g"
ctx.actions.run_shell(
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_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,
)