forked from Vertexwahn/rules_qt6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt.bzl
337 lines (300 loc) · 12 KB
/
qt.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""qt common rules"""
def _gen_ui_header(ctx):
info = ctx.toolchains["@rules_qt//tools:toolchain_type"].qtinfo
args = [ctx.file.ui_file.path, "-o", ctx.outputs.ui_header.path]
exec_requirements = {}
for elem in ctx.attr.tags:
exec_requirements[elem] = "1"
ctx.actions.run(
inputs = [ctx.file.ui_file],
outputs = [ctx.outputs.ui_header],
arguments = args,
executable = info.uic_path,
execution_requirements = exec_requirements,
)
return [OutputGroupInfo(ui_header = depset([ctx.outputs.ui_header]))]
gen_ui_header = rule(
implementation = _gen_ui_header,
attrs = {
"ui_file": attr.label(allow_single_file = True, mandatory = True),
"ui_header": attr.output(),
},
toolchains = ["@rules_qt//tools:toolchain_type"],
)
def qt_ui_library(name, ui, deps, target_compatible_with = [], **kwargs):
"""Compiles a QT UI file and makes a library for it.
Args:
name: A name for the rule.
ui: The ui file to compile.
deps: cc_library dependencies for the library.
target_compatible_with: A list of constraint_values that must be satisfied by the target
platform in order for this toolchain to be selected for a target building for that platform.
**kwargs: extra args pass to cc_library
"""
gen_ui_header(
name = "%s_uic" % name,
ui_file = ui,
ui_header = "ui_%s.h" % ui.split(".")[0],
target_compatible_with = target_compatible_with,
tags = ["local"],
)
native.cc_library(
name = name,
hdrs = [":%s_uic" % name],
deps = deps,
target_compatible_with = target_compatible_with,
**kwargs
)
def _gencpp(ctx):
info = ctx.toolchains["@rules_qt//tools:toolchain_type"].qtinfo
resource_files = [(f, ctx.actions.declare_file(f.path)) for f in ctx.files.files]
for target_file, output in resource_files:
ctx.actions.symlink(
output = output,
target_file = target_file,
)
args = ["--name", ctx.attr.resource_name, "--output", ctx.outputs.cpp.path, ctx.file.qrc.path]
exec_requirements = {}
for elem in ctx.attr.tags:
exec_requirements[elem] = "1"
ctx.actions.run(
inputs = [resource for _, resource in resource_files] + [ctx.file.qrc],
outputs = [ctx.outputs.cpp],
arguments = args,
executable = info.rcc_path,
execution_requirements = exec_requirements,
)
return [OutputGroupInfo(cpp = depset([ctx.outputs.cpp]))]
gencpp = rule(
implementation = _gencpp,
attrs = {
"resource_name": attr.string(),
"files": attr.label_list(allow_files = True, mandatory = False),
"qrc": attr.label(allow_single_file = True, mandatory = True),
"cpp": attr.output(),
},
toolchains = ["@rules_qt//tools:toolchain_type"],
)
def _gencpp2(ctx):
info = ctx.toolchains["@rules_qt//tools:toolchain_type"].qtinfo
resource_files = ctx.files.files
args = ["--name", ctx.attr.resource_name, "--output", ctx.outputs.cpp.path, ctx.file.qrc.path]
exec_requirements = {}
for elem in ctx.attr.tags:
exec_requirements[elem] = "1"
ctx.actions.run(
inputs = [resource for resource in resource_files] + [ctx.file.qrc],
outputs = [ctx.outputs.cpp],
arguments = args,
executable = info.rcc_path,
execution_requirements = exec_requirements,
)
return [OutputGroupInfo(cpp = depset([ctx.outputs.cpp]))]
gencpp2 = rule(
implementation = _gencpp2,
attrs = {
"resource_name": attr.string(),
"files": attr.label_list(allow_files = True, mandatory = False),
"qrc": attr.label(allow_single_file = True, mandatory = True),
"cpp": attr.output(),
},
toolchains = ["@rules_qt//tools:toolchain_type"],
)
# generate a qrc file that lists each of the input files.
def _genqrc(ctx):
qrc_output = ctx.outputs.qrc
qrc_content = "<RCC>\n <qresource prefix=\\\"/\\\">"
for f in ctx.files.files:
qrc_content += "\n <file>%s</file>" % f.path
qrc_content += "\n </qresource>\n</RCC>"
cmd = ["echo", "\"%s\"" % qrc_content, ">", qrc_output.path]
exec_requirements = {}
for elem in ctx.attr.tags:
exec_requirements[elem] = "1"
ctx.actions.run_shell(
command = " ".join(cmd),
outputs = [qrc_output],
execution_requirements = exec_requirements,
)
return [OutputGroupInfo(qrc = depset([qrc_output]))]
genqrc = rule(
implementation = _genqrc,
attrs = {
"files": attr.label_list(allow_files = True, mandatory = True),
"qrc": attr.output(),
},
)
def qt_resource_via_qrc(name, qrc_file, files, target_compatible_with = [], **kwargs):
"""Creates a cc_library containing the contents of all input files using qt's `rcc` tool.
Args:
name: rule name
qrc_file: qt qrc file
files: a list of files to be included in the resource bundle
target_compatible_with: A list of constraint_values that must be satisfied by the target
platform in order for this toolchain to be selected for a target building for that platform.
**kwargs: extra args to pass to the cc_library
"""
# every resource cc_library that is linked into the same binary needs a
# unique 'name'.
rsrc_name = native.package_name().replace("/", "_") + "_" + name
outfile = name + "_gen.cpp"
gencpp2(
name = name + "_gen",
resource_name = rsrc_name,
files = files,
qrc = qrc_file,
cpp = outfile,
target_compatible_with = target_compatible_with,
tags = ["local"],
)
native.cc_library(
name = name,
srcs = [outfile],
alwayslink = 1,
target_compatible_with = target_compatible_with,
**kwargs
)
def qt_resource(name, files, target_compatible_with = [], **kwargs):
"""Creates a cc_library containing the contents of all input files using qt's `rcc` tool.
Args:
name: rule name
files: a list of files to be included in the resource bundle
target_compatible_with: A list of constraint_values that must be satisfied by the target
platform in order for this toolchain to be selected for a target building for that platform.
**kwargs: extra args to pass to the cc_library
"""
qrc_file = name + "_qrc.qrc"
genqrc(name = name + "_qrc", files = files, qrc = qrc_file, target_compatible_with = target_compatible_with)
# every resource cc_library that is linked into the same binary needs a
# unique 'name'.
rsrc_name = native.package_name().replace("/", "_") + "_" + name
outfile = name + "_gen.cpp"
gencpp(
name = name + "_gen",
resource_name = rsrc_name,
files = files,
qrc = qrc_file,
cpp = outfile,
target_compatible_with = target_compatible_with,
tags = [
"local",
],
)
native.cc_library(
name = name,
srcs = [outfile],
alwayslink = 1,
target_compatible_with = target_compatible_with,
**kwargs
)
def qt_cc_library(name, srcs, hdrs, normal_hdrs = [], deps = None, copts = [], target_compatible_with = [], **kwargs):
"""Compiles a QT library and generates the MOC for it.
Args:
name: A name for the rule.
srcs: The cpp files to compile.
hdrs: The header files that the MOC compiles to src.
normal_hdrs: Headers which are not sources for generated code.
deps: cc_library dependencies for the library.
copts: cc_library copts
target_compatible_with: A list of constraint_values that must be satisfied by the target
platform in order for this toolchain to be selected for a target building for that platform.
**kwargs: Any additional arguments are passed to the cc_library rule.
"""
_moc_srcs = []
for hdr in hdrs:
header_path = "%s/%s" % (native.package_name(), hdr) if len(native.package_name()) > 0 else hdr
moc_name = "%s_moc" % hdr.replace(".", "_")
native.genrule(
name = moc_name,
srcs = [hdr],
outs = [moc_name + ".cc"],
cmd = select({
"@platforms//os:linux": "$(location @qt_linux_x86_64//:moc) $(locations %s) -o $@ -f'%s'" % (hdr, header_path),
"@platforms//os:windows": "$(location @qt_windows_x86_64//:moc) $(locations %s) -o $@ -f'%s'" % (hdr, header_path),
"@rules_qt//:osx_x86_64": "$(location @qt_mac_x86_64//:moc) $(locations %s) -o $@ -f'%s'" % (hdr, header_path),
"@rules_qt//:osx_arm64": "$(location @qt_mac_aarch64//:moc) $(locations %s) -o $@ -f'%s'" % (hdr, header_path),
}),
tools = select({
"@platforms//os:linux": ["@qt_linux_x86_64//:moc"],
"@platforms//os:windows": ["@qt_windows_x86_64//:moc"],
"@rules_qt//:osx_arm64": ["@qt_mac_aarch64//:moc"],
"@rules_qt//:osx_x86_64": ["@qt_mac_x86_64//:moc"],
}),
target_compatible_with = target_compatible_with,
)
_moc_srcs.append(":" + moc_name)
native.cc_library(
name = name,
srcs = srcs + _moc_srcs,
hdrs = hdrs + normal_hdrs,
deps = deps,
copts = copts + select({
"@platforms//os:windows": [],
"//conditions:default": ["-fPIC"],
}),
target_compatible_with = target_compatible_with,
**kwargs
)
qt_plugin_data = select({
"@platforms//os:linux": ["@qt_linux_x86_64//:plugin_files", "@qt_linux_x86_64//:qml_files"],
"@rules_qt//:osx_x86_64": ["@qt_mac_x86_64//:plugin_files", "@qt_mac_x86_64//:qml_files"],
"@rules_qt//:osx_arm64": ["@qt_mac_aarch64//:plugin_files", "@qt_mac_aarch64//:qml_files"],
"@platforms//os:windows": ["@qt_windows_x86_64//:plugin_files", "@qt_windows_x86_64//:qml_files"],
})
def update_dict(source, env):
result = {}
result.update(source)
result.update(env)
return result
LINUX_ENV_DATA = {
"QT_QPA_PLATFORM_PLUGIN_PATH": "external/qt_linux_x86_64/plugins/platforms",
"QML2_IMPORT_PATH": "external/qt_linux_x86_64/qml",
"QT_PLUGIN_PATH": "external/qt_linux_x86_64/plugins",
}
MAC_X64_ENV_DATA = {
"QT_QPA_PLATFORM_PLUGIN_PATH": "external/qt_mac_x86_64/share/qt/plugins/platforms",
"QML2_IMPORT_PATH": "external/qt_mac_x86_64/qml",
"QT_PLUGIN_PATH": "external/qt_mac_x86_64/share/qt/plugins",
}
WINDOWS_ENV_DATA = {
"QT_QPA_PLATFORM_PLUGIN_PATH": "external/qt_windows_x86_64/plugins/platforms",
"QML2_IMPORT_PATH": "external/qt_windows_x86_64/qml",
"QT_PLUGIN_PATH": "external/qt_windows_x86_64/plugins",
}
MAC_M1_ENV_DATA = {
"QT_QPA_PLATFORM_PLUGIN_PATH": "external/qt_mac_aarch64/share/qt/plugins/platforms",
"QML2_IMPORT_PATH": "external/qt_mac_aarch64/qml",
"QT_PLUGIN_PATH": "external/qt_mac_aarch64/share/qt/plugins",
}
def qt_cc_binary(name, srcs, deps = None, copts = [], data = [], env = {}, **kwargs):
""" cc_binary which depend on qt_cc_library or want to use qt tools
Args:
name: A name for the rule.
srcs: The cpp files to compile.
deps: cc_library dependencies for the library.
copts: cc_library copts
data: which data need to depend
env: environment value
**kwargs: Any additional arguments are passed to the cc_library rule.
"""
linux_env_data = update_dict(LINUX_ENV_DATA, env)
mac_x64_env_data = update_dict(MAC_X64_ENV_DATA, env)
windows_env_data = update_dict(WINDOWS_ENV_DATA, env)
mac_m1_env_data = update_dict(MAC_M1_ENV_DATA, env)
native.cc_binary(
name = name,
srcs = srcs,
deps = deps,
copts = copts + select({
"@platforms//os:windows": [],
"//conditions:default": ["-fPIC"],
}),
data = qt_plugin_data + data,
env = select({
"@platforms//os:linux": linux_env_data,
"@rules_qt//:osx_x86_64": mac_x64_env_data,
"@rules_qt//:osx_arm64": mac_m1_env_data,
"@platforms//os:windows": windows_env_data,
}),
**kwargs
)