/
cxx_toolchain.bzl
309 lines (290 loc) · 17.6 KB
/
cxx_toolchain.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
load("@prelude//:is_full_meta_repo.bzl", "is_full_meta_repo")
load("@prelude//cxx:cxx_toolchain_types.bzl", "AsCompilerInfo", "AsmCompilerInfo", "BinaryUtilitiesInfo", "CCompilerInfo", "CudaCompilerInfo", "CvtresCompilerInfo", "CxxCompilerInfo", "CxxObjectFormat", "DepTrackingMode", "DistLtoToolsInfo", "HipCompilerInfo", "LinkerInfo", "PicBehavior", "RcCompilerInfo", "ShlibInterfacesMode", "StripFlagsInfo", "cxx_toolchain_infos")
load("@prelude//cxx:cxx_utility.bzl", "cxx_toolchain_allow_cache_upload_args")
load("@prelude//cxx:debug.bzl", "SplitDebugMode")
load("@prelude//cxx:headers.bzl", "HeaderMode", "HeadersAsRawHeadersMode")
load("@prelude//cxx:linker.bzl", "LINKERS", "is_pdb_generated")
load("@prelude//linking:link_info.bzl", "LinkOrdering", "LinkStyle")
load("@prelude//linking:lto.bzl", "LtoMode", "lto_compiler_flags")
load("@prelude//utils:utils.bzl", "flatten", "value_or")
load("@prelude//decls/cxx_rules.bzl", "cxx_rules")
def cxx_toolchain_impl(ctx):
c_compiler = _get_maybe_wrapped_msvc(ctx.attrs.c_compiler[RunInfo], ctx.attrs.c_compiler_type or ctx.attrs.compiler_type, ctx.attrs._msvc_hermetic_exec[RunInfo])
lto_mode = LtoMode(ctx.attrs.lto_mode)
if lto_mode != LtoMode("none"):
object_format = "bitcode"
else:
object_format = ctx.attrs.object_format if ctx.attrs.object_format else "native"
c_lto_flags = lto_compiler_flags(lto_mode)
c_info = CCompilerInfo(
compiler = c_compiler,
compiler_type = ctx.attrs.c_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.c_compiler_flags, c_lto_flags),
preprocessor = c_compiler,
preprocessor_flags = cmd_args(ctx.attrs.c_preprocessor_flags),
dep_files_processor = ctx.attrs._dep_files_processor[RunInfo],
allow_cache_upload = ctx.attrs.c_compiler_allow_cache_upload,
)
cxx_compiler = _get_maybe_wrapped_msvc(ctx.attrs.cxx_compiler[RunInfo], ctx.attrs.cxx_compiler_type or ctx.attrs.compiler_type, ctx.attrs._msvc_hermetic_exec[RunInfo])
cxx_info = CxxCompilerInfo(
compiler = cxx_compiler,
compiler_type = ctx.attrs.cxx_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.cxx_compiler_flags, c_lto_flags),
preprocessor = cxx_compiler,
preprocessor_flags = cmd_args(ctx.attrs.cxx_preprocessor_flags),
dep_files_processor = ctx.attrs._dep_files_processor[RunInfo],
allow_cache_upload = ctx.attrs.cxx_compiler_allow_cache_upload,
)
asm_info = AsmCompilerInfo(
compiler = ctx.attrs.asm_compiler[RunInfo],
compiler_type = ctx.attrs.asm_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.asm_compiler_flags),
preprocessor_flags = cmd_args(ctx.attrs.asm_preprocessor_flags),
dep_files_processor = ctx.attrs._dep_files_processor[RunInfo],
) if ctx.attrs.asm_compiler else None
as_info = AsCompilerInfo(
compiler = ctx.attrs.assembler[RunInfo],
compiler_type = ctx.attrs.assembler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.assembler_flags),
preprocessor_flags = cmd_args(ctx.attrs.assembler_preprocessor_flags),
dep_files_processor = ctx.attrs._dep_files_processor[RunInfo],
) if ctx.attrs.assembler else None
cuda_info = CudaCompilerInfo(
compiler = ctx.attrs.cuda_compiler[RunInfo],
compiler_type = ctx.attrs.cuda_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.cuda_compiler_flags),
preprocessor_flags = cmd_args(ctx.attrs.cuda_preprocessor_flags),
dep_files_processor = ctx.attrs._dep_files_processor[RunInfo],
) if ctx.attrs.cuda_compiler else None
hip_info = HipCompilerInfo(
compiler = ctx.attrs.hip_compiler[RunInfo],
compiler_type = ctx.attrs.hip_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.hip_compiler_flags),
preprocessor_flags = cmd_args(ctx.attrs.hip_preprocessor_flags),
) if ctx.attrs.hip_compiler else None
cvtres_info = CvtresCompilerInfo(
compiler = ctx.attrs.cvtres_compiler[RunInfo],
compiler_type = ctx.attrs.cvtres_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.cvtres_compiler_flags),
preprocessor_flags = cmd_args(ctx.attrs.cvtres_preprocessor_flags),
) if ctx.attrs.cvtres_compiler else None
rc_info = RcCompilerInfo(
compiler = ctx.attrs.rc_compiler[RunInfo],
compiler_type = ctx.attrs.rc_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.rc_compiler_flags),
preprocessor_flags = cmd_args(ctx.attrs.rc_preprocessor_flags),
) if ctx.attrs.rc_compiler else None
linker_info = LinkerInfo(
archiver = ctx.attrs.archiver[RunInfo],
archiver_flags = cmd_args(ctx.attrs.archiver_flags),
archiver_supports_argfiles = ctx.attrs.archiver_supports_argfiles,
archiver_type = ctx.attrs.archiver_type,
archive_contents = ctx.attrs.archive_contents,
archive_objects_locally = False,
binary_extension = value_or(ctx.attrs.binary_extension, ""),
generate_linker_maps = ctx.attrs.generate_linker_maps,
is_pdb_generated = is_pdb_generated(ctx.attrs.linker_type, ctx.attrs.linker_flags),
link_binaries_locally = not value_or(ctx.attrs.cache_links, True),
link_libraries_locally = False,
link_style = LinkStyle(ctx.attrs.link_style),
link_weight = ctx.attrs.link_weight,
link_ordering = ctx.attrs.link_ordering,
linker = ctx.attrs.linker[RunInfo],
linker_flags = cmd_args(ctx.attrs.linker_flags, c_lto_flags),
post_linker_flags = cmd_args(ctx.attrs.post_linker_flags),
lto_mode = lto_mode,
mk_shlib_intf = ctx.attrs.shared_library_interface_producer,
object_file_extension = ctx.attrs.object_file_extension or "o",
shlib_interfaces = ShlibInterfacesMode(ctx.attrs.shared_library_interface_mode),
independent_shlib_interface_linker_flags = ctx.attrs.shared_library_interface_flags,
requires_archives = value_or(ctx.attrs.requires_archives, True),
requires_objects = value_or(ctx.attrs.requires_objects, False),
sanitizer_runtime_enabled = ctx.attrs.sanitizer_runtime_enabled,
sanitizer_runtime_files = flatten([runtime_file[DefaultInfo].default_outputs for runtime_file in ctx.attrs.sanitizer_runtime_files]),
supports_distributed_thinlto = ctx.attrs.supports_distributed_thinlto,
shared_dep_runtime_ld_flags = ctx.attrs.shared_dep_runtime_ld_flags,
shared_library_name_default_prefix = _get_shared_library_name_default_prefix(ctx),
shared_library_name_format = _get_shared_library_name_format(ctx),
shared_library_versioned_name_format = _get_shared_library_versioned_name_format(ctx),
static_dep_runtime_ld_flags = ctx.attrs.static_dep_runtime_ld_flags,
static_library_extension = ctx.attrs.static_library_extension or "a",
static_pic_dep_runtime_ld_flags = ctx.attrs.static_pic_dep_runtime_ld_flags,
type = ctx.attrs.linker_type,
use_archiver_flags = ctx.attrs.use_archiver_flags,
)
utilities_info = BinaryUtilitiesInfo(
nm = ctx.attrs.nm[RunInfo],
objcopy = ctx.attrs.objcopy_for_shared_library_interface[RunInfo],
objdump = ctx.attrs.objdump[RunInfo] if ctx.attrs.objdump else None,
ranlib = ctx.attrs.ranlib[RunInfo] if ctx.attrs.ranlib else None,
strip = ctx.attrs.strip[RunInfo],
dwp = None,
bolt_msdk = None,
)
strip_flags_info = StripFlagsInfo(
strip_debug_flags = ctx.attrs.strip_debug_flags,
strip_non_global_flags = ctx.attrs.strip_non_global_flags,
strip_all_flags = ctx.attrs.strip_all_flags,
)
platform_name = ctx.attrs.platform_name or ctx.attrs.name
return [
DefaultInfo(),
] + cxx_toolchain_infos(
platform_name = platform_name,
linker_info = linker_info,
binary_utilities_info = utilities_info,
bolt_enabled = value_or(ctx.attrs.bolt_enabled, False),
c_compiler_info = c_info,
cxx_compiler_info = cxx_info,
asm_compiler_info = asm_info,
as_compiler_info = as_info,
cuda_compiler_info = cuda_info,
hip_compiler_info = hip_info,
cvtres_compiler_info = cvtres_info,
rc_compiler_info = rc_info,
header_mode = _get_header_mode(ctx),
llvm_link = ctx.attrs.llvm_link[RunInfo] if ctx.attrs.llvm_link else None,
object_format = CxxObjectFormat(object_format),
headers_as_raw_headers_mode = HeadersAsRawHeadersMode(ctx.attrs.headers_as_raw_headers_mode) if ctx.attrs.headers_as_raw_headers_mode != None else None,
conflicting_header_basename_allowlist = ctx.attrs.conflicting_header_basename_exemptions,
mk_hmap = ctx.attrs._mk_hmap[RunInfo],
mk_comp_db = ctx.attrs._mk_comp_db,
pic_behavior = PicBehavior(ctx.attrs.pic_behavior),
split_debug_mode = SplitDebugMode(ctx.attrs.split_debug_mode),
strip_flags_info = strip_flags_info,
# TODO(T138705365): Turn on dep files by default
use_dep_files = value_or(ctx.attrs.use_dep_files, _get_default_use_dep_files(platform_name)),
clang_remarks = ctx.attrs.clang_remarks,
clang_trace = value_or(ctx.attrs.clang_trace, False),
cpp_dep_tracking_mode = DepTrackingMode(ctx.attrs.cpp_dep_tracking_mode),
cuda_dep_tracking_mode = DepTrackingMode(ctx.attrs.cuda_dep_tracking_mode),
dumpbin_toolchain_path = ctx.attrs._dumpbin_toolchain_path[DefaultInfo].default_outputs[0] if ctx.attrs._dumpbin_toolchain_path else None,
)
def cxx_toolchain_extra_attributes(is_toolchain_rule):
dep_type = attrs.exec_dep if is_toolchain_rule else attrs.dep
return {
"archiver": dep_type(providers = [RunInfo]),
"archiver_supports_argfiles": attrs.bool(default = False),
"asm_compiler": attrs.option(dep_type(providers = [RunInfo]), default = None),
"asm_preprocessor": attrs.option(dep_type(providers = [RunInfo]), default = None),
"assembler": dep_type(providers = [RunInfo]),
"assembler_preprocessor": attrs.option(dep_type(providers = [RunInfo]), default = None),
"bolt_enabled": attrs.bool(default = False),
"c_compiler": dep_type(providers = [RunInfo]),
"clang_remarks": attrs.option(attrs.string(), default = None),
"clang_trace": attrs.option(attrs.bool(), default = None),
"cpp_dep_tracking_mode": attrs.enum(DepTrackingMode.values(), default = "makefile"),
"cuda_compiler": attrs.option(dep_type(providers = [RunInfo]), default = None),
"cuda_dep_tracking_mode": attrs.enum(DepTrackingMode.values(), default = "makefile"),
"cvtres_compiler": attrs.option(dep_type(providers = [RunInfo]), default = None),
"cxx_compiler": dep_type(providers = [RunInfo]),
"generate_linker_maps": attrs.bool(default = False),
"hip_compiler": attrs.option(dep_type(providers = [RunInfo]), default = None),
"link_ordering": attrs.enum(LinkOrdering.values(), default = "preorder"),
"link_weight": attrs.int(default = 1),
"linker": dep_type(providers = [RunInfo]),
"llvm_link": attrs.option(dep_type(providers = [RunInfo]), default = None),
"lto_mode": attrs.enum(LtoMode.values(), default = "none"),
"nm": dep_type(providers = [RunInfo]),
"objcopy_for_shared_library_interface": dep_type(providers = [RunInfo]),
"objdump": attrs.option(dep_type(providers = [RunInfo]), default = None),
"object_format": attrs.enum(CxxObjectFormat.values(), default = "native"),
"pic_behavior": attrs.enum(PicBehavior.values(), default = "supported"),
# A placeholder tool that can be used to set up toolchain constraints.
# Useful when fat and thin toolchahins share the same underlying tools via `command_alias()`,
# which requires setting up separate platform-specific aliases with the correct constraints.
"placeholder_tool": attrs.option(dep_type(providers = [RunInfo]), default = None),
# Used for resolving any 'platform_*' attributes.
"platform_name": attrs.option(attrs.string(), default = None),
"private_headers_symlinks_enabled": attrs.bool(default = True),
"public_headers_symlinks_enabled": attrs.bool(default = True),
"ranlib": attrs.option(dep_type(providers = [RunInfo]), default = None),
"rc_compiler": attrs.option(dep_type(providers = [RunInfo]), default = None),
"requires_objects": attrs.bool(default = False),
"sanitizer_runtime_enabled": attrs.bool(default = False),
"sanitizer_runtime_files": attrs.set(attrs.dep(), sorted = True, default = []), # Use `attrs.dep()` as it's not a tool, always propagate target platform
"shared_library_interface_mode": attrs.enum(ShlibInterfacesMode.values(), default = "disabled"),
"shared_library_interface_producer": attrs.option(dep_type(providers = [RunInfo]), default = None),
"split_debug_mode": attrs.enum(SplitDebugMode.values(), default = "none"),
"strip": dep_type(providers = [RunInfo]),
"supports_distributed_thinlto": attrs.bool(default = False),
"use_archiver_flags": attrs.bool(default = True),
"use_dep_files": attrs.option(attrs.bool(), default = None),
"_dep_files_processor": dep_type(providers = [RunInfo], default = "prelude//cxx/tools:dep_file_processor"),
"_dist_lto_tools": attrs.default_only(dep_type(providers = [DistLtoToolsInfo], default = "prelude//cxx/dist_lto/tools:dist_lto_tools")),
# TODO(scottcao): Figure out a slightly better way to integrate this. In theory, this is only needed for clang toolchain.
# If we were using msvc, we should be able to use dumpbin directly.
"_dumpbin_toolchain_path": attrs.default_only(attrs.option(dep_type(providers = [DefaultInfo]), default = select({
"DEFAULT": None,
"ovr_config//os:windows": select({
# Unfortunately, it seems like an unresolved select when resolve exec platforms causes the whole resolution
# to fail, so I need a DEFAULT here when some target without cpu constraint tries to configure against the
# windows exec platform.
"DEFAULT": None,
"ovr_config//cpu:x86_32": "fbsource//arvr/third-party/toolchains/visual_studio:cl_x86_and_tools",
"ovr_config//cpu:x86_64": "fbsource//arvr/third-party/toolchains/visual_studio:cl_x64_and_tools",
}),
}) if is_full_meta_repo() else None)),
"_mk_comp_db": attrs.default_only(dep_type(providers = [RunInfo], default = "prelude//cxx/tools:make_comp_db")),
# FIXME: prelude// should be standalone (not refer to fbsource//)
"_mk_hmap": attrs.default_only(dep_type(providers = [RunInfo], default = "prelude//cxx/tools:hmap_wrapper")),
"_msvc_hermetic_exec": attrs.default_only(dep_type(providers = [RunInfo], default = "prelude//windows/tools:msvc_hermetic_exec")),
} | cxx_toolchain_allow_cache_upload_args()
def _cxx_toolchain_inheriting_target_platform_attrs():
attrs = dict(cxx_rules.cxx_toolchain.attrs)
attrs.update(cxx_toolchain_extra_attributes(is_toolchain_rule = True))
return attrs
cxx_toolchain_inheriting_target_platform = rule(
impl = cxx_toolchain_impl,
attrs = _cxx_toolchain_inheriting_target_platform_attrs(),
is_toolchain_rule = True,
)
_APPLE_PLATFORM_NAME_PREFIXES = [
"iphonesimulator",
"iphoneos",
"maccatalyst",
"macosx",
"watchos",
"watchsimulator",
"appletvos",
"appletvsimulator",
]
def _get_default_use_dep_files(platform_name: str) -> bool:
# All Apple platforms use Clang which supports the standard dep files format
for apple_platform_name_prefix in _APPLE_PLATFORM_NAME_PREFIXES:
if apple_platform_name_prefix in platform_name:
return True
return False
def _get_header_mode(ctx: AnalysisContext) -> HeaderMode:
if ctx.attrs.use_header_map:
if ctx.attrs.private_headers_symlinks_enabled or ctx.attrs.public_headers_symlinks_enabled:
return HeaderMode("symlink_tree_with_header_map")
else:
return HeaderMode("header_map_only")
else:
return HeaderMode("symlink_tree_only")
def _get_shared_library_name_default_prefix(ctx: AnalysisContext) -> str:
extension = ctx.attrs.shared_library_extension
return "" if extension == "dll" else "lib"
def _get_shared_library_name_format(ctx: AnalysisContext) -> str:
linker_type = ctx.attrs.linker_type
extension = ctx.attrs.shared_library_extension
if extension == "":
extension = LINKERS[linker_type].default_shared_library_extension
return "{}." + extension
def _get_shared_library_versioned_name_format(ctx: AnalysisContext) -> str:
linker_type = ctx.attrs.linker_type
extension_format = ctx.attrs.shared_library_versioned_extension_format.replace("%s", "{}")
if extension_format == "":
extension_format = LINKERS[linker_type].default_shared_library_versioned_extension_format
return "{}." + extension_format
def _get_maybe_wrapped_msvc(compiler: RunInfo, compiler_type: str, msvc_hermetic_exec: RunInfo) -> RunInfo:
if compiler_type == "windows":
return RunInfo(args = cmd_args(msvc_hermetic_exec, compiler))
return compiler