-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.bzl
More file actions
237 lines (197 loc) · 9.96 KB
/
Copy pathrules.bzl
File metadata and controls
237 lines (197 loc) · 9.96 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
""
load("@bazel_skylib//lib:sets.bzl", "sets")
load("@bazel_utilities//toolchains:extras_filegroups.bzl", "filegroup_translate_to_starlark")
load("@bazel_utilities//toolchains:hosts.bzl", "get_host_infos_from_rctx", "split_host_name")
load("@bazel_utilities//toolchains:registry.bzl", "get_archive_from_registry")
load("@bazel_winlibs//:registry.bzl", "WINLIBS_REGISTRY")
def _winlibs_compiler_archive_impl(rctx):
host_os, _, host_name = get_host_infos_from_rctx(rctx.os.name, rctx.os.arch)
if rctx.attr.override_host_name != "" and rctx.attr.override_host_name != "local":
host_os, _, host_name = split_host_name(rctx.attr.override_host_name)
registry = json.decode(rctx.attr.registry_json)
archive = get_archive_from_registry(registry, "MinGW", rctx.attr.winlibs_version)
substitutions = {
"%{rctx_name}": rctx.name,
"%{rctx_path}": "external/{}/".format(rctx.name),
"%{host_name}": host_name,
"%{clang_version}": archive["details"]["clang_version"],
"%{gcc_version}": archive["details"]["gcc_version"],
}
rctx.template(
"BUILD.bazel",
Label("//templates:BUILD.compiler.bazel.tpl"),
substitutions
)
host_archive = archive["archives"][host_name]
rctx.download_and_extract(
url = host_archive["url"],
sha256 = host_archive["sha256"],
stripPrefix = host_archive["strip_prefix"],
)
winlibs_compiler_archive = repository_rule(
implementation = _winlibs_compiler_archive_impl,
attrs = {
'override_host_name': attr.string(default = "local"),
'winlibs_version': attr.string(default = "latest"),
'registry_json': attr.string(mandatory = True),
},
)
def _winlibs_impl(rctx):
host_os, _, host_name = get_host_infos_from_rctx(rctx.os.name, rctx.os.arch)
if rctx.attr.override_host_name != "" and rctx.attr.override_host_name != "local":
host_os, _, host_name = split_host_name(rctx.attr.override_host_name)
registry = json.decode(rctx.attr.registry_json)
archive = get_archive_from_registry(registry, "MinGW", rctx.attr.winlibs_version)
toolchain_path = "external/{}/".format(rctx.name)
compiler_package = ""
compiler_full_package = "@@{}//".format(rctx.name)
compiler_package_path = toolchain_path
if rctx.attr.compiler_archive_package != None and rctx.attr.compiler_archive_package != "":
compiler_package = "@@{}//".format(rctx.attr.compiler_archive_package.repo_name)
compiler_full_package = compiler_package
compiler_package_path = rctx.attr.compiler_archive_package.workspace_root + "/"
substitutions = {
"%{rctx_name}": rctx.name,
"%{toolchain_path}": toolchain_path,
"%{host_name}": host_name,
"%{toolchain_id}": "winlibs_{}".format(rctx.attr.winlibs_version),
"%{clang_version}": archive["details"]["clang_version"],
"%{gcc_version}": archive["details"]["gcc_version"],
"%{compiler_package}": compiler_package,
"%{compiler_full_package}": compiler_full_package,
"%{compiler_package_path}": compiler_package_path,
"%{exec_compatible_with}": json.encode(rctx.attr.exec_compatible_with),
"%{target_compatible_with}": json.encode(rctx.attr.target_compatible_with),
"%{toolchain_builtin_includedirs_isystem}": json.encode(rctx.attr.toolchain_builtin_includedirs_isystem),
"%{toolchain_builtin_includedirs}": json.encode(rctx.attr.toolchain_builtin_includedirs),
"%{copts}": json.encode(rctx.attr.copts),
"%{conlyopts}": json.encode(rctx.attr.conlyopts),
"%{cxxopts}": json.encode(rctx.attr.cxxopts),
"%{linkopts}": json.encode(rctx.attr.linkopts),
"%{defines}": json.encode(rctx.attr.defines),
"%{includedirs}": json.encode(rctx.attr.includedirs),
"%{linkdirs}": json.encode(rctx.attr.linkdirs),
"%{linklibs}": json.encode(rctx.attr.linklibs),
# dbg / opt
"%{dbg_copts}": json.encode(rctx.attr.dbg_copts),
"%{dbg_linkopts}": json.encode(rctx.attr.dbg_linkopts),
"%{opt_copts}": json.encode(rctx.attr.opt_copts),
"%{opt_linkopts}": json.encode(rctx.attr.opt_linkopts),
"%{toolchain_extras_filegroups}": json.encode(filegroup_translate_to_starlark(rctx.attr.toolchain_extras_filegroups), ),
}
rctx.template(
"BUILD.bazel",
Label("//templates:BUILD.bazel.tpl"),
substitutions
)
rctx.template(
"vscode.bzl",
Label("//templates:vscode.bzl.tpl"),
substitutions
)
if rctx.attr.compiler_archive_package == None or rctx.attr.compiler_archive_package == "":
host_archive = archive["archives"][host_name]
rctx.download_and_extract(
url = host_archive["url"],
sha256 = host_archive["sha256"],
stripPrefix = host_archive["strip_prefix"],
)
winlibs_toolchain = repository_rule(
implementation = _winlibs_impl,
attrs = {
'override_host_name': attr.string(default = "local"),
'winlibs_version': attr.string(default = "latest"),
'registry_json': attr.string(default = json.encode(WINLIBS_REGISTRY)),
'exec_compatible_with': attr.string_list(default = []),
'target_compatible_with': attr.string_list(default = []),
'toolchain_builtin_includedirs_isystem': attr.string_list(default = []),
'toolchain_builtin_includedirs': attr.string_list(default = []),
'copts': attr.string_list(default = []),
'conlyopts': attr.string_list(default = []),
'cxxopts': attr.string_list(default = []),
'linkopts': attr.string_list(default = []),
'defines': attr.string_list(default = []),
'includedirs': attr.string_list(default = []),
'linkdirs': attr.string_list(default = []),
'linklibs': attr.string_list(default = []),
# dbg / opt
'dbg_copts': attr.string_list(default = []),
'dbg_linkopts': attr.string_list(default = []),
'opt_copts': attr.string_list(default = []),
'opt_linkopts': attr.string_list(default = []),
'toolchain_extras_filegroups': attr.label_list(default = []),
'compiler_archive_package': attr.label(default = None),
},
)
def _winlibs_toolchain_extension_impl(module_ctx):
toolchain_versions_list = [
(toolchain.override_host_name, toolchain.winlibs_version)
for mod in module_ctx.modules
for toolchain in mod.tags.winlibs_toolchain
]
if len(toolchain_versions_list) == 0:
toolchain_versions_list.append(("local", "latest"))
toolchain_versions_list = sets.to_list(sets.make(toolchain_versions_list))
winlibs_registry = WINLIBS_REGISTRY
for toolchains_version in toolchain_versions_list:
winlibs_compiler_archive(
name = "archive_winlibs-{}-{}".format(toolchains_version[0], toolchains_version[1]),
winlibs_version = toolchains_version[1],
registry_json = json.encode(winlibs_registry),
override_host_name = toolchains_version[0],
)
for mod in module_ctx.modules:
for toolchain in mod.tags.winlibs_toolchain:
winlibs_toolchain(
name = toolchain.name,
winlibs_version = toolchain.winlibs_version,
exec_compatible_with = toolchain.exec_compatible_with,
target_compatible_with = toolchain.target_compatible_with,
toolchain_builtin_includedirs_isystem = toolchain.toolchain_builtin_includedirs_isystem,
toolchain_builtin_includedirs = toolchain.toolchain_builtin_includedirs,
copts = toolchain.copts,
conlyopts = toolchain.conlyopts,
cxxopts = toolchain.cxxopts,
linkopts = toolchain.linkopts,
defines = toolchain.defines,
includedirs = toolchain.includedirs,
linkdirs = toolchain.linkdirs,
linklibs = toolchain.linklibs,
# dbg / opt
dbg_copts = toolchain.dbg_copts,
dbg_linkopts = toolchain.dbg_linkopts,
opt_copts = toolchain.opt_copts,
opt_linkopts = toolchain.opt_linkopts,
toolchain_extras_filegroups = toolchain.toolchain_extras_filegroups,
compiler_archive_package = "@archive_winlibs-{}-{}".format(toolchain.override_host_name, toolchain.winlibs_version),
override_host_name = toolchain.override_host_name,
)
winlibs_toolchain_extension = module_extension(
implementation = _winlibs_toolchain_extension_impl,
tag_classes = {
"winlibs_toolchain": tag_class(attrs = {
'override_host_name': attr.string(default = "local"),
'name': attr.string(mandatory = True),
'winlibs_version': attr.string(default = "latest"),
'compiler_archive_package': attr.label(default = None),
'exec_compatible_with': attr.string_list(default = []),
'target_compatible_with': attr.string_list(default = []),
'toolchain_builtin_includedirs_isystem': attr.string_list(default = []),
'toolchain_builtin_includedirs': attr.string_list(default = []),
'copts': attr.string_list(default = []),
'conlyopts': attr.string_list(default = []),
'cxxopts': attr.string_list(default = []),
'linkopts': attr.string_list(default = []),
'defines': attr.string_list(default = []),
'includedirs': attr.string_list(default = []),
'linkdirs': attr.string_list(default = []),
'linklibs': attr.string_list(default = []),
# dbg / opt
'dbg_copts': attr.string_list(default = []),
'dbg_linkopts': attr.string_list(default = []),
'opt_copts': attr.string_list(default = []),
'opt_linkopts': attr.string_list(default = []),
'toolchain_extras_filegroups': attr.label_list(default = []),
}),
},
)