/
native_toolchain.py
403 lines (321 loc) · 14.4 KB
/
native_toolchain.py
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
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from dataclasses import dataclass
from typing import Any, Tuple, Union
from pants.backend.native.config.environment import (
Assembler,
CCompiler,
CppCompiler,
CppToolchain,
CToolchain,
Linker,
)
from pants.backend.native.subsystems.binaries.binutils import Binutils
from pants.backend.native.subsystems.binaries.gcc import GCC
from pants.backend.native.subsystems.binaries.llvm import LLVM
from pants.backend.native.subsystems.libc_dev import LibcDev
from pants.backend.native.subsystems.native_build_step import ToolchainVariant
from pants.backend.native.subsystems.xcode_cli_tools import XCodeCLITools
from pants.engine.platform import Platform
from pants.engine.rules import RootRule, rule
from pants.engine.selectors import Get
from pants.subsystem.subsystem import Subsystem
from pants.util.enums import match
from pants.util.memo import memoized_property
class NativeToolchain(Subsystem):
"""Abstraction over platform-specific tools to compile and link native code.
When this subsystem is consumed, Pants will download and unpack archives (if necessary) which
together provide an appropriate "native toolchain" for the host platform: a compiler and linker,
usually. This subsystem exposes the toolchain through `@rule`s, which tasks then request during
setup or execution (synchronously, for now).
NB: Currently, on OSX, Pants will find and invoke the XCode command-line tools, or error out with
installation instructions if the XCode tools could not be found.
"""
options_scope = "native-toolchain"
@classmethod
def subsystem_dependencies(cls):
return super().subsystem_dependencies() + (
Binutils.scoped(cls),
GCC.scoped(cls),
LibcDev.scoped(cls),
LLVM.scoped(cls),
XCodeCLITools.scoped(cls),
)
@memoized_property
def _binutils(self):
return Binutils.scoped_instance(self)
@memoized_property
def _gcc(self):
return GCC.scoped_instance(self)
@memoized_property
def _llvm(self):
return LLVM.scoped_instance(self)
@memoized_property
def _xcode_cli_tools(self):
return XCodeCLITools.scoped_instance(self)
@memoized_property
def _libc_dev(self):
return LibcDev.scoped_instance(self)
@dataclass(frozen=True)
class LibcObjects:
crti_object_paths: Any
class LinkerWrapperMixin:
def for_compiler(self, compiler):
"""Return a Linker object which is intended to be compatible with the given `compiler`."""
return (
self.linker
# TODO(#6143): describe why the compiler needs to be first on the PATH!
.sequence(compiler, exclude_list_fields=["extra_args", "path_entries"])
.prepend_field("path_entries", compiler.path_entries)
.copy(exe_filename=compiler.exe_filename)
)
@dataclass(frozen=True)
class GCCLinker(LinkerWrapperMixin):
linker: Linker
@dataclass(frozen=True)
class LLVMLinker(LinkerWrapperMixin):
linker: Linker
@dataclass(frozen=True)
class GCCCToolchain:
c_toolchain: CToolchain
@dataclass(frozen=True)
class GCCCppToolchain:
cpp_toolchain: CppToolchain
@dataclass(frozen=True)
class LLVMCToolchain:
c_toolchain: CToolchain
@dataclass(frozen=True)
class LLVMCppToolchain:
cpp_toolchain: CppToolchain
@rule
async def select_libc_objects(platform: Platform, native_toolchain: NativeToolchain) -> LibcObjects:
# We use lambdas here to avoid searching for libc on osx, where it will fail.
paths = match(
platform,
{
Platform.darwin: lambda: [],
Platform.linux: lambda: native_toolchain._libc_dev.get_libc_objects(),
},
)()
return LibcObjects(paths)
@rule
async def select_assembler(platform: Platform, native_toolchain: NativeToolchain) -> Assembler:
if platform == Platform.darwin:
assembler = await Get(Assembler, XCodeCLITools, native_toolchain._xcode_cli_tools)
else:
assembler = await Get(Assembler, Binutils, native_toolchain._binutils)
return assembler
@dataclass(frozen=True)
class BaseLinker:
"""A Linker which is not specific to any compiler yet.
This represents Linker objects provided by subsystems, but may need additional information to be
usable by a specific compiler.
"""
linker: Linker
# TODO: select the appropriate `Platform` in the `@rule` decl using variants!
@rule
async def select_base_linker(platform: Platform, native_toolchain: NativeToolchain) -> BaseLinker:
if platform == Platform.darwin:
# TODO(#5663): turn this into LLVM when lld works.
linker = await Get(Linker, XCodeCLITools, native_toolchain._xcode_cli_tools)
else:
linker = await Get(Linker, Binutils, native_toolchain._binutils)
base_linker = BaseLinker(linker=linker)
return base_linker
@rule
async def select_gcc_linker(native_toolchain: NativeToolchain) -> GCCLinker:
base_linker = await Get(BaseLinker, NativeToolchain, native_toolchain)
linker = base_linker.linker
libc_objects = await Get(LibcObjects, NativeToolchain, native_toolchain)
linker_with_libc = linker.append_field("extra_object_files", libc_objects.crti_object_paths)
return GCCLinker(linker_with_libc)
@rule
def select_llvm_linker(base_linker: BaseLinker) -> LLVMLinker:
return LLVMLinker(base_linker.linker)
@dataclass(frozen=True)
class GCCInstallLocationForLLVM:
"""This class is convertible into a list of command line arguments for clang and clang++.
This is only used on Linux. The option --gcc-toolchain stops clang from searching for another
gcc on the host system. The option appears to only exist on Linux clang and clang++.
"""
toolchain_dir: Any
@property
def as_clang_argv(self):
# TODO(#6143): describe exactly what this argument does to the clang/clang++ invocation!
return [f"--gcc-toolchain={self.toolchain_dir}"]
@rule
def select_gcc_install_location(gcc: GCC) -> GCCInstallLocationForLLVM:
return GCCInstallLocationForLLVM(gcc.select())
@rule
async def select_llvm_c_toolchain(
platform: Platform, native_toolchain: NativeToolchain
) -> LLVMCToolchain:
provided_clang = await Get(CCompiler, LLVM, native_toolchain._llvm)
if platform == Platform.darwin:
xcode_clang = await Get(CCompiler, XCodeCLITools, native_toolchain._xcode_cli_tools)
joined_c_compiler = provided_clang.sequence(xcode_clang)
else:
gcc_install = await Get(GCCInstallLocationForLLVM, GCC, native_toolchain._gcc)
provided_gcc = await Get(CCompiler, GCC, native_toolchain._gcc)
joined_c_compiler = (
provided_clang.sequence(provided_gcc).append_field(
"extra_args", gcc_install.as_clang_argv
)
# We need g++'s version of the GLIBCXX library to be able to run.
.prepend_field("runtime_library_dirs", provided_gcc.runtime_library_dirs)
)
working_c_compiler = joined_c_compiler.prepend_field("extra_args", ["-x", "c", "-std=c11"])
llvm_linker_wrapper = await Get(LLVMLinker, NativeToolchain, native_toolchain)
working_linker = llvm_linker_wrapper.for_compiler(working_c_compiler)
return LLVMCToolchain(CToolchain(working_c_compiler, working_linker))
@rule
async def select_llvm_cpp_toolchain(
platform: Platform, native_toolchain: NativeToolchain
) -> LLVMCppToolchain:
provided_clangpp = await Get(CppCompiler, LLVM, native_toolchain._llvm)
# On OSX, we use the libc++ (LLVM) C++ standard library implementation. This is feature-complete
# for OSX, but not for Linux (see https://libcxx.llvm.org/ for more info).
if platform == Platform.darwin:
xcode_clangpp = await Get(CppCompiler, XCodeCLITools, native_toolchain._xcode_cli_tools)
joined_cpp_compiler = provided_clangpp.sequence(xcode_clangpp)
extra_llvm_linking_library_dirs: Tuple[str, ...] = tuple()
linker_extra_args: Tuple[str, ...] = tuple()
else:
gcc_install = await Get(GCCInstallLocationForLLVM, GCC, native_toolchain._gcc)
provided_gpp = await Get(CppCompiler, GCC, native_toolchain._gcc)
joined_cpp_compiler = (
provided_clangpp.sequence(provided_gpp)
# NB: we use g++'s headers on Linux, and therefore their C++ standard
# library.
.copy(include_dirs=provided_gpp.include_dirs).append_field(
"extra_args", gcc_install.as_clang_argv
)
# We need g++'s version of the GLIBCXX library to be able to run.
.prepend_field("runtime_library_dirs", provided_gpp.runtime_library_dirs)
)
extra_llvm_linking_library_dirs = (
provided_gpp.runtime_library_dirs + provided_clangpp.runtime_library_dirs
)
# Ensure we use libstdc++, provided by g++, during the linking stage.
linker_extra_args = ("-stdlib=libstdc++",)
working_cpp_compiler = joined_cpp_compiler.prepend_field(
"extra_args",
[
"-x",
"c++",
"-std=c++11",
# This flag is intended to avoid using any of the headers from our LLVM distribution's C++
# stdlib implementation, or any from the host system, and instead, use include dirs from the
# XCodeCLITools or GCC.
# TODO(#6143): Determine precisely what this flag does and why it's necessary.
"-nostdinc++",
],
)
llvm_linker_wrapper = await Get(LLVMLinker, NativeToolchain, native_toolchain)
working_linker = (
llvm_linker_wrapper.for_compiler(working_cpp_compiler)
.append_field("linking_library_dirs", extra_llvm_linking_library_dirs)
.prepend_field("extra_args", linker_extra_args)
)
return LLVMCppToolchain(CppToolchain(working_cpp_compiler, working_linker))
@rule
async def select_gcc_c_toolchain(
platform: Platform, native_toolchain: NativeToolchain
) -> GCCCToolchain:
provided_gcc = await Get(CCompiler, GCC, native_toolchain._gcc)
if platform == Platform.darwin:
# GCC needs access to some headers that are only provided by the XCode toolchain
# currently (e.g. "_stdio.h"). These headers are unlikely to change across versions, so this is
# probably safe.
xcode_clang = await Get(CCompiler, XCodeCLITools, native_toolchain._xcode_cli_tools)
joined_c_compiler = provided_gcc.sequence(xcode_clang)
else:
joined_c_compiler = provided_gcc
# GCC needs an assembler, so we provide that (platform-specific) tool here.
assembler = await Get(Assembler, NativeToolchain, native_toolchain)
working_c_compiler = joined_c_compiler.sequence(assembler).prepend_field(
"extra_args", ["-x", "c", "-std=c11"]
)
gcc_linker_wrapper = await Get(GCCLinker, NativeToolchain, native_toolchain)
working_linker = gcc_linker_wrapper.for_compiler(working_c_compiler)
return GCCCToolchain(CToolchain(working_c_compiler, working_linker))
@rule
async def select_gcc_cpp_toolchain(
platform: Platform, native_toolchain: NativeToolchain
) -> GCCCppToolchain:
provided_gpp = await Get(CppCompiler, GCC, native_toolchain._gcc)
if platform == Platform.darwin:
# GCC needs access to some headers that are only provided by the XCode toolchain
# currently (e.g. "_stdio.h"). These headers are unlikely to change across versions, so this is
# probably safe.
# TODO: we should be providing all of these (so we can eventually phase out XCodeCLITools
# entirely).
xcode_clangpp = await Get(CppCompiler, XCodeCLITools, native_toolchain._xcode_cli_tools)
joined_cpp_compiler = provided_gpp.sequence(xcode_clangpp)
else:
joined_cpp_compiler = provided_gpp
# GCC needs an assembler, so we provide that (platform-specific) tool here.
assembler = await Get(Assembler, NativeToolchain, native_toolchain)
working_cpp_compiler = joined_cpp_compiler.sequence(assembler).prepend_field(
"extra_args",
[
"-x",
"c++",
"-std=c++11",
# This flag is intended to avoid using any of the headers from our LLVM distribution's C++
# stdlib implementation, or any from the host system, and instead, use include dirs from the
# XCodeCLITools or GCC.
# TODO(#6143): Determine precisely what this flag does and why it's necessary.
"-nostdinc++",
],
)
gcc_linker_wrapper = await Get(GCCLinker, NativeToolchain, native_toolchain)
working_linker = gcc_linker_wrapper.for_compiler(working_cpp_compiler)
return GCCCppToolchain(CppToolchain(working_cpp_compiler, working_linker))
@dataclass(frozen=True)
class ToolchainVariantRequest:
toolchain: NativeToolchain
variant: ToolchainVariant
@rule
async def select_c_toolchain(toolchain_variant_request: ToolchainVariantRequest) -> CToolchain:
use_gcc = toolchain_variant_request.variant == ToolchainVariant.gnu
toolchain_resolved: Union[GCCCToolchain, LLVMCToolchain]
if use_gcc:
toolchain_resolved = await Get(
GCCCToolchain, NativeToolchain, toolchain_variant_request.toolchain
)
else:
toolchain_resolved = await Get(
LLVMCToolchain, NativeToolchain, toolchain_variant_request.toolchain
)
return toolchain_resolved.c_toolchain
@rule
async def select_cpp_toolchain(toolchain_variant_request: ToolchainVariantRequest) -> CppToolchain:
use_gcc = toolchain_variant_request.variant == ToolchainVariant.gnu
toolchain_resolved: Union[GCCCppToolchain, LLVMCppToolchain]
if use_gcc:
toolchain_resolved = await Get(
GCCCppToolchain, NativeToolchain, toolchain_variant_request.toolchain
)
else:
toolchain_resolved = await Get(
LLVMCppToolchain, NativeToolchain, toolchain_variant_request.toolchain
)
return toolchain_resolved.cpp_toolchain
def create_native_toolchain_rules():
return [
select_libc_objects,
select_assembler,
select_base_linker,
select_gcc_linker,
select_llvm_linker,
select_gcc_install_location,
select_llvm_c_toolchain,
select_llvm_cpp_toolchain,
select_gcc_c_toolchain,
select_gcc_cpp_toolchain,
select_c_toolchain,
select_cpp_toolchain,
RootRule(NativeToolchain),
RootRule(ToolchainVariantRequest),
]