-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathcxx.bzl
266 lines (251 loc) · 13.2 KB
/
cxx.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
# 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//cxx:cxx_toolchain_types.bzl",
"BinaryUtilitiesInfo",
"CCompilerInfo",
"CvtresCompilerInfo",
"CxxCompilerInfo",
"CxxInternalTools",
"CxxPlatformInfo",
"CxxToolchainInfo",
"LinkerInfo",
"LinkerType",
"PicBehavior",
"RcCompilerInfo",
"ShlibInterfacesMode",
)
load("@prelude//cxx:headers.bzl", "HeaderMode")
load("@prelude//cxx:linker.bzl", "is_pdb_generated")
load("@prelude//decls:common.bzl", "buck")
load("@prelude//linking:link_info.bzl", "LinkOrdering", "LinkStyle")
load("@prelude//linking:lto.bzl", "LtoMode")
load("@prelude//os_lookup:defs.bzl", "OsLookup")
CxxToolsInfo = provider(
fields = {
"archiver": provider_field(typing.Any, default = None),
"archiver_type": provider_field(typing.Any, default = None),
"asm_compiler": provider_field(typing.Any, default = None),
"asm_compiler_type": provider_field(typing.Any, default = None),
"compiler": provider_field(typing.Any, default = None),
"compiler_type": provider_field(typing.Any, default = None),
"cvtres_compiler": provider_field(typing.Any, default = None),
"cxx_compiler": provider_field(typing.Any, default = None),
"linker": provider_field(typing.Any, default = None),
"linker_type": LinkerType,
"rc_compiler": provider_field(typing.Any, default = None),
},
)
def _legacy_equivalent_cxx_tools_info_windows(ctx: AnalysisContext, default_toolchain: CxxToolsInfo) -> CxxToolsInfo:
return CxxToolsInfo(
compiler = default_toolchain.compiler if ctx.attrs.compiler == None or ctx.attrs.compiler == "cl.exe" else ctx.attrs.compiler,
compiler_type = default_toolchain.compiler_type if ctx.attrs.compiler_type == None else ctx.attrs.compiler_type,
cxx_compiler = default_toolchain.cxx_compiler if ctx.attrs.compiler == None or ctx.attrs.compiler == "cl.exe" else ctx.attrs.compiler,
asm_compiler = default_toolchain.asm_compiler,
asm_compiler_type = default_toolchain.asm_compiler_type,
rc_compiler = default_toolchain.rc_compiler if ctx.attrs.rc_compiler == None or ctx.attrs.rc_compiler == "rc.exe" else ctx.attrs.rc_compiler,
cvtres_compiler = default_toolchain.cvtres_compiler if ctx.attrs.cvtres_compiler == None or ctx.attrs.cvtres_compiler == "cvtres.exe" else ctx.attrs.cvtres_compiler,
archiver = default_toolchain.archiver,
archiver_type = default_toolchain.archiver_type,
linker = default_toolchain.linker if ctx.attrs.linker == None or ctx.attrs.linker == "link.exe" else ctx.attrs.linker,
linker_type = default_toolchain.linker_type,
)
def _legacy_equivalent_cxx_tools_info_non_windows(ctx: AnalysisContext, default_toolchain: CxxToolsInfo) -> CxxToolsInfo:
return CxxToolsInfo(
compiler = default_toolchain.compiler if ctx.attrs.compiler == None else ctx.attrs.compiler,
compiler_type = default_toolchain.compiler_type if ctx.attrs.compiler_type == None else ctx.attrs.compiler_type,
cxx_compiler = default_toolchain.cxx_compiler if ctx.attrs.cxx_compiler == None else ctx.attrs.cxx_compiler,
asm_compiler = default_toolchain.asm_compiler if ctx.attrs.compiler == None else ctx.attrs.compiler,
asm_compiler_type = default_toolchain.asm_compiler_type if ctx.attrs.compiler_type == None else ctx.attrs.compiler_type,
rc_compiler = default_toolchain.rc_compiler if ctx.attrs.rc_compiler == None else ctx.attrs.rc_compiler,
cvtres_compiler = default_toolchain.cvtres_compiler if ctx.attrs.cvtres_compiler == None else ctx.attrs.cvtres_compiler,
archiver = default_toolchain.archiver,
archiver_type = default_toolchain.archiver_type,
linker = default_toolchain.linker if ctx.attrs.linker == None else ctx.attrs.linker,
linker_type = default_toolchain.linker_type,
)
def _system_cxx_toolchain_impl(ctx: AnalysisContext):
"""
A very simple toolchain that is hardcoded to the current environment.
"""
os = ctx.attrs._target_os_type[OsLookup].platform
arch_name = ctx.attrs._target_os_type[OsLookup].cpu
cxx_tools_info = ctx.attrs._cxx_tools_info[CxxToolsInfo]
cxx_tools_info = _legacy_equivalent_cxx_tools_info_windows(ctx, cxx_tools_info) if os == "windows" else _legacy_equivalent_cxx_tools_info_non_windows(ctx, cxx_tools_info)
target_name = os
if arch_name:
target_name += "-" + arch_name
return _cxx_toolchain_from_cxx_tools_info(ctx, cxx_tools_info, target_name)
def _cxx_tools_info_toolchain_impl(ctx: AnalysisContext):
return _cxx_toolchain_from_cxx_tools_info(ctx, ctx.attrs.cxx_tools_info[CxxToolsInfo])
def _cxx_toolchain_from_cxx_tools_info(ctx: AnalysisContext, cxx_tools_info: CxxToolsInfo, target_name = "x86_64"):
os = ctx.attrs._target_os_type[OsLookup].platform
archiver_supports_argfiles = os != "macos"
additional_linker_flags = ["-fuse-ld=lld"] if os == "linux" and cxx_tools_info.linker != "g++" and cxx_tools_info.cxx_compiler != "g++" else []
if os == "windows":
linker_type = LinkerType("windows")
binary_extension = "exe"
object_file_extension = "obj"
static_library_extension = "lib"
shared_library_name_default_prefix = ""
shared_library_name_format = "{}.dll"
shared_library_versioned_name_format = "{}.dll"
pic_behavior = PicBehavior("not_supported")
else:
binary_extension = ""
object_file_extension = "o"
static_library_extension = "a"
shared_library_name_default_prefix = "lib"
shared_library_name_format = "{}.so"
shared_library_versioned_name_format = "{}.so.{}"
if os == "macos":
linker_type = LinkerType("darwin")
pic_behavior = PicBehavior("always_enabled")
else:
linker_type = LinkerType("gnu")
pic_behavior = PicBehavior("supported")
if cxx_tools_info.compiler_type == "clang":
llvm_link = RunInfo(args = ["llvm-link"])
else:
llvm_link = None
return [
DefaultInfo(),
CxxToolchainInfo(
internal_tools = ctx.attrs._internal_tools[CxxInternalTools],
linker_info = LinkerInfo(
linker = _run_info(cxx_tools_info.linker),
linker_flags = additional_linker_flags + ctx.attrs.link_flags,
post_linker_flags = ctx.attrs.post_link_flags,
archiver = _run_info(cxx_tools_info.archiver),
archiver_type = cxx_tools_info.archiver_type,
archiver_supports_argfiles = archiver_supports_argfiles,
generate_linker_maps = False,
lto_mode = LtoMode("none"),
type = linker_type,
link_binaries_locally = True,
link_libraries_locally = True,
archive_objects_locally = True,
use_archiver_flags = True,
static_dep_runtime_ld_flags = [],
static_pic_dep_runtime_ld_flags = [],
shared_dep_runtime_ld_flags = [],
independent_shlib_interface_linker_flags = [],
shlib_interfaces = ShlibInterfacesMode("disabled"),
link_style = LinkStyle(ctx.attrs.link_style),
link_weight = 1,
binary_extension = binary_extension,
object_file_extension = object_file_extension,
shared_library_name_default_prefix = shared_library_name_default_prefix,
shared_library_name_format = shared_library_name_format,
shared_library_versioned_name_format = shared_library_versioned_name_format,
static_library_extension = static_library_extension,
force_full_hybrid_if_capable = False,
is_pdb_generated = is_pdb_generated(linker_type, ctx.attrs.link_flags),
link_ordering = ctx.attrs.link_ordering,
),
bolt_enabled = False,
binary_utilities_info = BinaryUtilitiesInfo(
nm = RunInfo(args = ["nm"]),
objcopy = RunInfo(args = ["objcopy"]),
objdump = RunInfo(args = ["objdump"]),
ranlib = RunInfo(args = ["ranlib"]),
strip = RunInfo(args = ["strip"]),
dwp = None,
bolt_msdk = None,
),
cxx_compiler_info = CxxCompilerInfo(
compiler = _run_info(cxx_tools_info.cxx_compiler),
preprocessor_flags = [],
compiler_flags = ctx.attrs.cxx_flags,
compiler_type = cxx_tools_info.compiler_type,
),
c_compiler_info = CCompilerInfo(
compiler = _run_info(cxx_tools_info.compiler),
preprocessor_flags = [],
compiler_flags = ctx.attrs.c_flags,
compiler_type = cxx_tools_info.compiler_type,
),
as_compiler_info = CCompilerInfo(
compiler = _run_info(cxx_tools_info.compiler),
compiler_type = cxx_tools_info.compiler_type,
),
asm_compiler_info = CCompilerInfo(
compiler = _run_info(cxx_tools_info.asm_compiler),
compiler_type = cxx_tools_info.asm_compiler_type,
),
cvtres_compiler_info = CvtresCompilerInfo(
compiler = _run_info(cxx_tools_info.cvtres_compiler),
preprocessor_flags = [],
compiler_flags = ctx.attrs.cvtres_flags,
compiler_type = cxx_tools_info.compiler_type,
),
rc_compiler_info = RcCompilerInfo(
compiler = _run_info(cxx_tools_info.rc_compiler),
preprocessor_flags = [],
compiler_flags = ctx.attrs.rc_flags,
compiler_type = cxx_tools_info.compiler_type,
),
header_mode = HeaderMode("symlink_tree_only"),
cpp_dep_tracking_mode = ctx.attrs.cpp_dep_tracking_mode,
pic_behavior = pic_behavior,
llvm_link = llvm_link,
),
CxxPlatformInfo(name = target_name),
]
def _run_info(args):
return None if args == None else RunInfo(args = [args])
system_cxx_toolchain = rule(
impl = _system_cxx_toolchain_impl,
attrs = {
"c_flags": attrs.list(attrs.string(), default = []),
"compiler": attrs.option(attrs.string(), default = None),
"compiler_type": attrs.option(attrs.string(), default = None), # one of CxxToolProviderType
"cpp_dep_tracking_mode": attrs.string(default = "makefile"),
"cvtres_compiler": attrs.option(attrs.string(), default = None),
"cvtres_flags": attrs.list(attrs.string(), default = []),
"cxx_compiler": attrs.option(attrs.string(), default = None),
"cxx_flags": attrs.list(attrs.string(), default = []),
"link_flags": attrs.list(attrs.string(), default = []),
"link_ordering": attrs.option(attrs.enum(LinkOrdering.values()), default = None),
"link_style": attrs.string(default = "shared"),
"linker": attrs.option(attrs.string(), default = None),
"post_link_flags": attrs.list(attrs.string(), default = []),
"rc_compiler": attrs.option(attrs.string(), default = None),
"rc_flags": attrs.list(attrs.string(), default = []),
"_cxx_tools_info": attrs.exec_dep(providers = [CxxToolsInfo], default = "prelude//toolchains/msvc:msvc_tools" if host_info().os.is_windows else "prelude//toolchains/cxx/clang:path_clang_tools"),
"_internal_tools": attrs.default_only(attrs.exec_dep(providers = [CxxInternalTools], default = "prelude//cxx/tools:internal_tools")),
"_target_os_type": buck.target_os_type_arg(),
},
is_toolchain_rule = True,
)
cxx_tools_info_toolchain = rule(
impl = _cxx_tools_info_toolchain_impl,
attrs = {
"c_flags": attrs.list(attrs.string(), default = []),
"cpp_dep_tracking_mode": attrs.string(default = "makefile"),
"cvtres_flags": attrs.list(attrs.string(), default = []),
"cxx_flags": attrs.list(attrs.string(), default = []),
"cxx_tools_info": attrs.exec_dep(providers = [CxxToolsInfo], default = select({
"DEFAULT": "prelude//toolchains/cxx/clang:path_clang_tools",
"config//os:windows": "prelude//toolchains/msvc:msvc_tools",
})),
"link_flags": attrs.list(attrs.string(), default = []),
"link_ordering": attrs.option(attrs.enum(LinkOrdering.values()), default = None),
"link_style": attrs.enum(
LinkStyle.values(),
default = "shared",
doc = """
The default value of the `link_style` attribute for rules that use this toolchain.
""",
),
"post_link_flags": attrs.list(attrs.string(), default = []),
"rc_flags": attrs.list(attrs.string(), default = []),
"_internal_tools": attrs.default_only(attrs.exec_dep(providers = [CxxInternalTools], default = "prelude//cxx/tools:internal_tools")),
"_target_os_type": buck.target_os_type_arg(),
},
is_toolchain_rule = True,
)