-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rules.bzl
567 lines (535 loc) · 19.9 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
load("//tools/workspace:generate_file.bzl", "generate_file")
load(
"@drake//tools/workspace:cmake_configure_file.bzl",
"cmake_configure_files",
)
load("@vtk_internal//:modules.bzl", "MODULES", "PLATFORM")
load("@vtk_internal//:settings.bzl", "MODULE_SETTINGS")
# You can manually set this to True, to get some feedback during upgrades.
_VERBOSE = False
def _bazelize_module_name(name):
"""Transforms e.g. `VTK::IOCore` => `VTK__IOCore` to make Bazel happy."""
return name.replace(":", "_")
def _bazelize_module_names(names):
"""Maps _bazelize_module_name onto a list of names."""
return [_bazelize_module_name(x) for x in names or []]
def _vtk_cc_module_impl(
*,
module_name,
subdir,
library_name = None,
module_deps_public = [],
module_deps_private = [],
module_deps_ignore = [],
hdrs_extra = [],
hdrs_glob_exclude = [],
hdrs_content = {},
included_cxxs = [],
srcs_extra = [],
srcs_glob_extra = [],
srcs_glob_exclude = [],
srcs_objc_non_arc = [],
cmake_defines_cmakelists = [],
cmake_defines = [],
cmake_undefines = [],
defines_extra = [],
copts_extra = [],
linkopts_extra = [],
deps_extra = [],
visibility = []):
"""See vtk_cc_module() for documentation."""
# Convert module names to bazel-safe names.
module_name = _bazelize_module_name(module_name)
module_deps_public = _bazelize_module_names(module_deps_public)
module_deps_private = _bazelize_module_names(module_deps_private)
# Cull any module deps that should be ignored.
for dep_name in (module_deps_ignore or []):
dep_name = _bazelize_module_name(dep_name)
if dep_name in module_deps_public:
module_deps_public.remove(dep_name)
if dep_name in module_deps_private:
module_deps_private.remove(dep_name)
# Generate the module header, à la CMake GenerateExportHeader.
if library_name != None and "ThirdParty" not in subdir:
module_h = subdir + "/" + library_name + "Module.h"
scream = library_name.upper()
generate_file(
name = module_h,
content = "\n".join([
"#pragma once",
"#include \"vtkABINamespace.h\"",
"#define {scream}_EXPORT",
"#define {scream}_NO_EXPORT",
"#define {scream}_DEPRECATED",
"#define {scream}_DEPRECATED_EXPORT",
"#define {scream}_DEPRECATED_NO_EXPORT",
]).format(scream = scream),
)
hdrs_extra = hdrs_extra + [module_h]
for hdr_filename, hdr_content in (hdrs_content or {}).items():
generate_file(name = hdr_filename, content = hdr_content)
hdrs_extra = hdrs_extra + [hdr_filename]
# Configure template headers (when necessary).
gen_hdrs_lib = []
subst_hdrs = native.glob([
subdir + "/**/*.h.in",
subdir + "/**/*.hpp.in",
subdir + "/**/*.hxx.in",
subdir + "/**/*.txx.in",
], exclude = [
"**/test*",
] + hdrs_glob_exclude)
gen_hdrs = [
# We want to start from, e.g., "Common/Core/vtkDebug.h.in" and generate
# "gen/Common/Core/vtkDebug.h" with CMake substitutions applied, which
# means tacking a directory name on the front and stripping off ".in".
"gen/" + x[:-3]
for x in subst_hdrs
]
if gen_hdrs:
cmake_configure_files(
name = "_genrule_hdrs_" + module_name,
srcs = subst_hdrs,
outs = gen_hdrs,
cmakelists = cmake_defines_cmakelists,
defines = cmake_defines,
undefines = cmake_undefines,
# TODO(jwnimmer-tri) Add opt-out config knob for strictness?
strict = True,
)
native.cc_library(
name = module_name + "_gen_hdrs",
hdrs = gen_hdrs,
strip_include_prefix = "gen/" + subdir,
linkstatic = True,
)
gen_hdrs_lib = [module_name + "_gen_hdrs"]
# Prepare the cc_library options.
hdrs = hdrs_extra + included_cxxs + native.glob([
subdir + "/**/*.h",
subdir + "/**/*.hpp",
subdir + "/**/*.hxx",
subdir + "/**/*.txx",
], exclude = hdrs_glob_exclude, allow_empty = True)
srcs = srcs_extra + native.glob(
[subdir + "/*.cxx"] + srcs_glob_extra,
exclude = included_cxxs + srcs_glob_exclude + [
"**/vtkTest*",
"**/test*",
],
allow_empty = True,
)
copts = ["-w"] + copts_extra
linkopts = linkopts_extra
deps = []
if "ThirdParty" not in subdir:
deps.append(":vtkABINamespace")
deps = deps + gen_hdrs_lib
deps = deps + module_deps_public
deps = deps + module_deps_private
deps = deps + deps_extra
# Deal with objc code.
if PLATFORM["name"] != "linux" and srcs_objc_non_arc:
objc_lib_name = "_" + module_name + "_objc"
native.objc_library(
name = objc_lib_name,
non_arc_srcs = srcs_objc_non_arc,
hdrs = hdrs,
includes = [subdir],
defines = defines_extra,
copts = copts,
linkopts = linkopts,
deps = deps,
)
deps = deps + [objc_lib_name]
# Declare the library using its upstream module name.
native.cc_library(
name = module_name,
srcs = srcs,
hdrs = hdrs,
strip_include_prefix = subdir,
defines = defines_extra,
copts = copts,
linkopts = linkopts,
deps = deps,
linkstatic = True,
)
# Alias the module name into the library name (if given).
if library_name != None:
native.alias(
name = library_name,
actual = module_name,
visibility = visibility,
)
def vtk_cc_module(
*,
module_metadata,
module_settings = None):
"""
Args:
module_metadata: A parsed dict of the `vtk.module` contents, as
returned by the parse_module() function in repository.bzl.
module_settings: (Optional) A dict of customizations for how to
build this module, typically loaded from a `settings.bzl` file.
Keys of module_metadata:
subdir: The directory path, e.g., `Foo/Bar`.
NAME: The module name per VTK, e.g., `VTK::FooBar`.
LIBRARY_NAME: (Optional) The library name per VTK, e.g., `vtkFooBar`.
DEPENDS: (Optional) The public dependencies (by name).
PRIVATE_DEPENDS: (Optional) The private dependencies (by name).
Keys of module_settings (all of which are optional):
module_deps_ignore: Dependencies (either public or private) to ignore.
Typically this is used when the srcs have been trimmed to be
narrower than the full set, i.e., when certain classes are being
omitted from the build. The names here are VTK module names (e.g.,
VTK::FooBar).
hdrs_extra: Adds more headers beyond the default glob() patterns (*.h,
*.hpp, *.hxx, *.txx, *.h.in, *.hpp.in, *.hxx.in, *.txx.in).
Typically this is used for generated headers, since glob() only
matches upstream files, not generated files.
hdrs_glob_exclude: Skips these patterns. Typically this is used to skip
configure headers that need special handling (e.g., `**/*.hxx.in`).
hdrs_content: Map from source tree filename to generated file content.
Useful to create configure headers directly instead of with CMake
template substitution.
included_cxxs: Flags cxxs filenames abused as headers; these files will
be added to hdrs instead of srcs.
srcs_extra: Adds more sources beyond the default glob() pattern
(`{subdir}/*.cxx`). Typically this is either used to add files from
from deeper subdirectories, or to opt back in to a narrow set of
files that would otherwise been excluded by a srcs_glob_exclude.
srcs_glob_extra: Adds to the default sources glob() pattern. Typically
this is used to match files from from deeper subdirectories.
srcs_glob_exclude: Skips these patterns. Typically this is used to skip
classes that are unwanted or not necessary.
srcs_objc_non_arc: Adds the given Objective-C sources to this library.
cmake_defines: When generating a header file (e.g., foo.h.in => foo.h),
uses these definitions. See cmake_configure_file() for details.
cmake_undefines: When generating a header file, sets these definitions
to be undefined. See cmake_configure_file() for details.
defines_extra: Adds `defines = []` to the cc_library.
copts_extra: Adds `copts = []` to the cc_library.
linkopts_extra: Adds `linkopts = []` to the cc_library.
deps_extra: Adds `deps = []` to the cc_library.
visibility: Sets `visibility = []` on the cc_library.
"""
# Delegate to impl, unpacking the module details in the function call.
# This allows us to check the validity of all settings.bzl keywords.
_vtk_cc_module_impl(
module_name = module_metadata["NAME"],
subdir = module_metadata["subdir"],
library_name = module_metadata.get("LIBRARY_NAME"),
module_deps_public = module_metadata.get("DEPENDS"),
module_deps_private = module_metadata.get("PRIVATE_DEPENDS"),
**(module_settings or {})
)
def modules_closure(module_names, *, max_depth = 10):
"""Computes the transitive closure of the dependencies (both public and
private) of the given model names. Both the argument and return value are
a list of module_name strings.
Skylark doesn't have `while` loops, so we need to specify a max search
depth that's sufficient to cover the longest path in the deps graph.
"""
result = []
worklist = list(module_names)
for _ in range(max_depth):
if len(worklist) == 0:
break
next_worklist = []
for name in worklist:
result.append(name)
settings = MODULE_SETTINGS.get(name, {})
ignore = settings.get("module_deps_ignore", [])
for kind in ["DEPENDS", "PRIVATE_DEPENDS"]:
for dep_name in MODULES[name].get(kind, []):
if dep_name in result:
continue
if dep_name in worklist:
continue
if dep_name in next_worklist:
continue
if dep_name in ignore:
continue
next_worklist.append(dep_name)
worklist = next_worklist
if worklist:
fail("Insufficient max_depth")
return sorted(result)
def compile_all_modules():
"""Adds a cc_library rule for all modules in settings.bzl that are marked
with non-default visibility, and also adds private cc_library rules for all
of their required transitive dependency modules (per vtk.module metadata).
"""
names = modules_closure([
name
for name, details in MODULE_SETTINGS.items()
if "visibility" in details
])
for name in names:
vtk_cc_module(
module_metadata = MODULES[name],
module_settings = MODULE_SETTINGS.get(name),
)
# Optionally provide some diagnostics to help with VTK version upgrades.
unused_settings = [
name
for name in MODULE_SETTINGS
if name not in names
]
unused_settings.remove("ABI") # Not a real module.
modules_no_settings = [
name
for name in names
if name not in MODULE_SETTINGS
]
if _VERBOSE and unused_settings:
print("settings.bzl has unused modules: " + str(unused_settings))
if _VERBOSE and modules_no_settings:
print("settings.bzl does not customize: " + str(modules_no_settings))
def generate_abi_namespace():
"""Generates vtkABINamespace.h, mimicking the Common/Core/CMakeLists.txt.
"""
cmake_configure_files(
name = "_vtkABINamespace_genrule",
srcs = ["CMake/vtkABINamespace.h.in"],
outs = ["CMake/vtkABINamespace.h"],
defines = MODULE_SETTINGS["ABI"]["cmake_defines"],
strict = True,
)
native.cc_library(
name = "vtkABINamespace",
hdrs = ["CMake/vtkABINamespace.h"],
strip_include_prefix = "CMake",
linkstatic = True,
visibility = ["//visibility:public"],
)
def generate_common_core_array_dispatch_array_list():
"""Mimics the vtkCreateArrayDispatchArrayList.cmake logic.
Generates an `*.h` file.
"""
name = "common_core_array_dispatch_array_list"
# This is hard-coded to the default upstream options (AOS yes; SOA no).
# We could parameterize this function with options if we ever need more.
content = """
#pragma once
#include "vtkTypeList.h"
#include "vtkAOSDataArrayTemplate.h"
namespace vtkArrayDispatch {
VTK_ABI_NAMESPACE_BEGIN
typedef vtkTypeList::Unique<
vtkTypeList::Create<
vtkAOSDataArrayTemplate<char>,
vtkAOSDataArrayTemplate<double>,
vtkAOSDataArrayTemplate<float>,
vtkAOSDataArrayTemplate<int>,
vtkAOSDataArrayTemplate<long>,
vtkAOSDataArrayTemplate<long long>,
vtkAOSDataArrayTemplate<short>,
vtkAOSDataArrayTemplate<signed char>,
vtkAOSDataArrayTemplate<unsigned char>,
vtkAOSDataArrayTemplate<unsigned int>,
vtkAOSDataArrayTemplate<unsigned long>,
vtkAOSDataArrayTemplate<unsigned long long>,
vtkAOSDataArrayTemplate<unsigned short>,
vtkAOSDataArrayTemplate<vtkIdType>
>
>::Result Arrays;
VTK_ABI_NAMESPACE_END
}
"""
hdr = "Common/Core/vtkArrayDispatchArrayList.h"
generate_file(name = hdr, content = content)
native.filegroup(
name = name,
srcs = [hdr],
)
def _ctype_to_vtk_camel_type(ctype):
"""Converts e.g. "unsigned short" to "vtkUnsignedShort".
"""
if ctype == None:
return None
return "vtk" + "".join([
word.capitalize()
for word in ctype.split(" ")
])
def generate_common_core_type_list_macros():
"""Mimics the vtkCreateTypeListMacros.cmake logic.
Generates an `*.h` file.
"""
name = "common_core_type_list_macros"
max = 99
content = [
"#pragma once",
"#include \"vtkTypeList.h\"",
"#define vtkTypeList_Create_1(t1) \\",
" vtkTypeList::TypeList<t1, vtkTypeList::NullType>",
]
tail_list = []
for i in range(2, max + 1):
tail_list.append("t" + str(i))
tail = ", ".join(tail_list)
content.extend([
"#define vtkTypeList_Create_" + str(i) + "(t1, " + tail + ") \\",
" vtkTypeList::TypeList<t1, \\",
" vtkTypeList_Create_" + str(i - 1) + "(" + tail + ") >",
])
content.append("")
hdr = "Common/Core/vtkTypeListMacros.h"
generate_file(name = hdr, content = "\n".join(content))
native.filegroup(
name = name,
srcs = [hdr],
)
def generate_common_core_vtk_type_arrays():
"""Mimics the vtkTypeArrays.cmake logic, assuming a 64-bit platform.
Generates an `*.h` and `*.cxx` file for each of VTK's primitive types.
"""
name = "common_core_vtk_type_arrays"
result_hdrs = []
result_srcs = []
for vtk_type, preferred_ctype, fallback_ctype in (
("Int8", "char", "signed char"),
("UInt8", "unsigned char", None),
("Int16", "short", None),
("UInt16", "unsigned short", None),
("Int32", "int", None),
("UInt32", "unsigned int", None),
("Int64", "long", "long long"),
("UInt64", "unsigned long", "unsigned long long"),
("Float32", "float", None),
("Float64", "double", None),
):
preferred_ctype_upper = preferred_ctype.replace(" ", "_").upper()
preferred_class = _ctype_to_vtk_camel_type(preferred_ctype)
fallback_class = _ctype_to_vtk_camel_type(fallback_ctype)
srcs = [
"Common/Core/vtkTypedArray.h.in",
"Common/Core/vtkTypedArray.cxx.in",
]
outs = [
"Common/Core/vtkType{}Array.h".format(vtk_type),
"Common/Core/vtkType{}Array.cxx".format(vtk_type),
]
cmake_configure_files(
name = "_common_core_vtk_type_arrays_" + vtk_type,
srcs = srcs,
outs = outs,
defines = [
"VTK_TYPE_NAME={}".format(vtk_type),
"VTK_TYPE_NATIVE=" + """
#if VTK_TYPE_{vtk_type_upper} == VTK_{preferred_ctype_upper}
# include \"{preferred_class}Array.h\"
# define vtkTypeArrayBase {preferred_class}Array
#else
# include \"{fallback_class}Array.h\"
# define vtkTypeArrayBase {fallback_class}Array
#endif
""".format(
vtk_type_upper = vtk_type.upper(),
preferred_ctype_upper = preferred_ctype_upper,
preferred_class = preferred_class,
fallback_class = fallback_class or "_ERROR_",
),
],
strict = True,
)
result_hdrs.append(outs[0])
result_srcs.append(outs[1])
native.filegroup(
name = name + "_hdrs",
srcs = result_hdrs,
)
native.filegroup(
name = name + "_srcs",
srcs = result_srcs,
)
def generate_common_core_array_instantiations():
"""Mimic the instantiation_sources in Common/Core/CMakeLists.
Generates a pile of headers.
"""
name = "common_core_array_instantiations"
result = []
for ctype in (
"char",
"signed char",
"unsigned char",
"short",
"unsigned short",
"int",
"unsigned int",
"long",
"unsigned long",
"long long",
"unsigned long long",
"float",
"double",
):
for stem in (
"vtkSOADataArrayTemplateInstantiate",
"vtkGenericDataArrayValueRangeInstantiate",
):
if "Generic" in stem and "long" not in ctype:
continue
snake = ctype.replace(" ", "_")
out = "Common/Core/{stem}_{snake}.cxx".format(
stem = stem,
snake = snake,
)
cmake_configure_files(
name = "_genrule_" + stem + "_" + snake,
srcs = ["Common/Core/" + stem + ".cxx.in"],
outs = [out],
defines = [
"INSTANTIATION_VALUE_TYPE=" + ctype,
],
strict = True,
)
result.append(out)
native.filegroup(
name = name,
srcs = result,
)
def generate_common_core_sources():
generate_common_core_array_dispatch_array_list()
generate_common_core_type_list_macros()
generate_common_core_vtk_type_arrays()
generate_common_core_array_instantiations()
def cxx_embed(*, src, out, constant_name):
"""Mimics the vtkEncodeString.cmake logic.
Generates an `*.h` file with the contents of a data file.
"""
header = """
#pragma once
VTK_ABI_NAMESPACE_BEGIN
constexpr char {constant_name}[] = R"foo(
""".format(constant_name = constant_name)
footer = """
)foo";
VTK_ABI_NAMESPACE_END
"""
native.genrule(
name = "_genrule_" + out,
srcs = [src],
outs = [out],
cmd = " && ".join([
"(echo '" + header + "' > $@)",
"(cat $< >> $@)",
"(echo '" + footer + "' >> $@)",
]),
)
def _path_stem(src):
"""Returns e.g. "quux" when given "foo/bar/quux.ext".
"""
return src.split("/")[-1].split(".")[0]
def generate_rendering_opengl2_sources():
name = "generated_rendering_opengl2_sources"
hdrs = []
for src in native.glob([
"Rendering/OpenGL2/glsl/*.glsl",
"Rendering/OpenGL2/textures/*.jpg",
]):
stem = _path_stem(src)
hdr = "Rendering/OpenGL2/" + stem + ".h"
cxx_embed(src = src, out = hdr, constant_name = stem)
hdrs.append(hdr)
native.filegroup(name = name, srcs = hdrs)