-
-
Notifications
You must be signed in to change notification settings - Fork 631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
combine Clang, GCC, Binutils, and XCodeCLITools to form the NativeToolchain subsystem and consume it in BuildLocalPythonDistributions for native code #5490
Merged
stuhood
merged 27 commits into
pantsbuild:master
from
cosmicexplorer:dmcclanahan/python-dist-c++-sources
Apr 5, 2018
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
dcf23c9
Introduce Clang, GCC, and Binutils to form a native toolchain
cosmicexplorer 5bad1a8
add some more BUILD files
cosmicexplorer 5765442
reduce platform-specific toolchains to ones we can handle right now
cosmicexplorer 34c5125
delete setup.py modifications from an earlier iteration and clarify c…
cosmicexplorer acb788a
Remove ExecutionEnvironmentMixin and expose path entries instead
cosmicexplorer 93e760d
remove added lines in the centos6 Dockerfile
cosmicexplorer 94eca72
flesh out XCodeCLITools and add "sanity test" with hello world
cosmicexplorer e93a158
refactor the fasthello testproject to test both C and C++
cosmicexplorer 2f1c2a2
move toolchain validation to the NativeToolchain class
cosmicexplorer bbdd5e6
add utility method to wrap subprocess invocations
cosmicexplorer 2e72531
revert validation section -- that can go in a subsequent PR
cosmicexplorer b0ea3ad
refactor to use new has_native_sources prop on python_dist() targets
cosmicexplorer 53a67e8
test C and C++ in python_dist testing
cosmicexplorer 63fca9f
add hello world in C and C++ to backend/native tests
cosmicexplorer 73134c9
add some more docstrings
cosmicexplorer bae1a2d
remove extraneous register.py
cosmicexplorer 192374c
slightly elaborate in docstring
cosmicexplorer 72f592a
remove or clarify some TODOs
cosmicexplorer fbc0b99
fix import order with isort.sh
cosmicexplorer f2b31d1
add a dependency that was definitely there last time i checked
cosmicexplorer 21ae076
...fix lint errors...
cosmicexplorer 9b73f7b
fix toolchain unit test failures and probably fix CI integration
cosmicexplorer 55dab09
fix error message format string in native toolchain test
cosmicexplorer b4feaa2
don't isolate the path when invoking the native toolchain
cosmicexplorer b7a6184
import os when using os in native toolchain test
cosmicexplorer 7053d48
prepend to PATH instead of isolating it when using native toolchain
cosmicexplorer c35ac05
cut back on our use of the toolchain subsystem to pass CI
cosmicexplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
examples/src/python/example/python_distribution/hello/fasthello/c_greet.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <Python.h> | ||
|
||
static PyObject * c_greet(PyObject *self, PyObject *args) { | ||
return Py_BuildValue("s", "Hello from C!"); | ||
} | ||
|
||
static PyMethodDef Methods[] = { | ||
{"c_greet", c_greet, METH_VARARGS, "A greeting in the C language."}, | ||
{NULL, NULL, 0, NULL} | ||
}; | ||
|
||
PyMODINIT_FUNC initc_greet(void) { | ||
(void) Py_InitModule("c_greet", Methods); | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/src/python/example/python_distribution/hello/fasthello/cpp_greet.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <Python.h> | ||
|
||
static PyObject * cpp_greet(PyObject *self, PyObject *args) { | ||
return Py_BuildValue("s", "Hello from C++!"); | ||
} | ||
|
||
static PyMethodDef Methods[] = { | ||
{"cpp_greet", cpp_greet, METH_VARARGS, "A greeting in the C++ language."}, | ||
{NULL, NULL, 0, NULL} | ||
}; | ||
|
||
PyMODINIT_FUNC initcpp_greet(void) { | ||
(void) Py_InitModule("cpp_greet", Methods); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
|
||
|
||
if __name__ == '__main__': | ||
hello.hello() | ||
print(hello.hello()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
examples/src/python/example/python_distribution/hello/fasthello/super_greet.c
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
import os | ||
|
||
from pants.binaries.binary_tool import ExecutablePathProvider, NativeTool | ||
|
||
|
||
class Clang(NativeTool, ExecutablePathProvider): | ||
options_scope = 'clang' | ||
default_version = '6.0.0' | ||
archive_type = 'tgz' | ||
|
||
def path_entries(self): | ||
return [os.path.join(self.select(), 'bin')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/python/pants/backend/native/subsystems/native_toolchain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
from pants.backend.native.subsystems.clang import Clang | ||
from pants.backend.native.subsystems.gcc import GCC | ||
from pants.backend.native.subsystems.platform_specific.darwin.xcode_cli_tools import XCodeCLITools | ||
from pants.backend.native.subsystems.platform_specific.linux.binutils import Binutils | ||
from pants.binaries.binary_tool import ExecutablePathProvider | ||
from pants.subsystem.subsystem import Subsystem | ||
from pants.util.memo import memoized_method | ||
from pants.util.osutil import get_os_name, normalize_os_name | ||
|
||
|
||
class NativeToolchain(Subsystem, ExecutablePathProvider): | ||
"""Abstraction over platform-specific tools to compile and link native code. | ||
|
||
This "native toolchain" subsystem is an abstraction that exposes directories | ||
containing executables to compile and link "native" code (for now, C and C++ | ||
are supported). Consumers of this subsystem can add these directories to their | ||
PATH to invoke subprocesses which use these tools. | ||
|
||
This abstraction is necessary for two reasons. First, because there are | ||
multiple binaries involved in compilation and linking, which often invoke | ||
other binaries that must also be available on the PATH. Second, because unlike | ||
other binary tools in Pants, we can't provide the same package built for both | ||
OSX and Linux, because there is no open-source linker for OSX with a | ||
compatible license. | ||
|
||
So when this subsystem is consumed, Pants will download and unpack archives | ||
(if necessary) which together provide an appropriate "native toolchain" for | ||
the host platform. On OSX, Pants will also find and provide path entries for | ||
the XCode command-line tools, or error out with installation instructions if | ||
the XCode tools could not be found. | ||
""" | ||
|
||
options_scope = 'native-toolchain' | ||
|
||
# This is a list of subsystems which implement `ExecutablePathProvider` and | ||
# can be provided for all supported platforms. | ||
_CROSS_PLATFORM_SUBSYSTEMS = [Clang, GCC] | ||
|
||
# This is a map of {<platform> -> [<subsystem_cls>, ...]}; the key is the | ||
# normalized OS name, and the value is a list of subsystem class objects that | ||
# implement `ExecutablePathProvider`. The native toolchain subsystem will | ||
# declare dependencies only on the subsystems for the platform Pants is | ||
# executing on. | ||
_PLATFORM_SPECIFIC_SUBSYSTEMS = { | ||
'darwin': [XCodeCLITools], | ||
'linux': [Binutils], | ||
} | ||
|
||
class UnsupportedPlatformError(Exception): | ||
"""Thrown if the native toolchain is invoked on an unrecognized platform. | ||
|
||
Note that the native toolchain should work on all of Pants's supported | ||
platforms.""" | ||
|
||
@classmethod | ||
@memoized_method | ||
def _get_platform_specific_subsystems(cls): | ||
"""Return the subsystems used by the native toolchain for this platform.""" | ||
os_name = get_os_name() | ||
normed_os_name = normalize_os_name(os_name) | ||
|
||
subsystems_for_host = cls._PLATFORM_SPECIFIC_SUBSYSTEMS.get(normed_os_name, None) | ||
|
||
if subsystems_for_host is None: | ||
raise cls.UnsupportedPlatformError( | ||
"Pants doesn't support building native code on this platform " | ||
"(uname: '{}').".format(os_name)) | ||
|
||
# NB: path entries for cross-platform subsystems currently take precedence | ||
# over platform-specific ones -- this could be made configurable. | ||
all_subsystems_for_toolchain = cls._CROSS_PLATFORM_SUBSYSTEMS + subsystems_for_host | ||
|
||
return all_subsystems_for_toolchain | ||
|
||
@classmethod | ||
def subsystem_dependencies(cls): | ||
prev = super(NativeToolchain, cls).subsystem_dependencies() | ||
cur_platform_subsystems = cls._get_platform_specific_subsystems() | ||
return prev + tuple(sub.scoped(cls) for sub in cur_platform_subsystems) | ||
|
||
@memoized_method | ||
def _subsystem_instances(self): | ||
cur_platform_subsystems = self._get_platform_specific_subsystems() | ||
return [sub.scoped_instance(self) for sub in cur_platform_subsystems] | ||
|
||
# TODO(cosmicexplorer): We might want to run a very small test suite verifying | ||
# the toolchain can compile and link native code before returning, especially | ||
# since we don't provide the tools on OSX. | ||
def path_entries(self): | ||
combined_path_entries = [] | ||
for subsystem in self._subsystem_instances(): | ||
combined_path_entries.extend(subsystem.path_entries()) | ||
|
||
return combined_path_entries |
Empty file.
11 changes: 11 additions & 0 deletions
11
src/python/pants/backend/native/subsystems/platform_specific/darwin/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_library( | ||
dependencies=[ | ||
'src/python/pants/binaries:binary_util', | ||
'src/python/pants/subsystem', | ||
'src/python/pants/util:dirutil', | ||
], | ||
) |
Empty file.
44 changes: 44 additions & 0 deletions
44
src/python/pants/backend/native/subsystems/platform_specific/darwin/xcode_cli_tools.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
import os | ||
|
||
from pants.binaries.binary_tool import ExecutablePathProvider | ||
from pants.subsystem.subsystem import Subsystem | ||
from pants.util.dirutil import is_executable | ||
|
||
|
||
class XCodeCLITools(Subsystem, ExecutablePathProvider): | ||
"""Subsystem to detect and provide the XCode command line developer tools. | ||
|
||
This subsystem exists to give a useful error message if the tools aren't | ||
installed, and because the install location may not be on the PATH when Pants | ||
is invoked.""" | ||
|
||
options_scope = 'xcode-cli-tools' | ||
|
||
# TODO(cosmicexplorer): make this an option? | ||
_INSTALL_LOCATION = '/usr/bin' | ||
|
||
_REQUIRED_TOOLS = frozenset(['cc', 'c++', 'ld', 'lipo']) | ||
|
||
class XCodeToolsUnavailable(Exception): | ||
"""Thrown if the XCode CLI tools could not be located.""" | ||
|
||
def _check_executables_exist(self): | ||
for filename in self._REQUIRED_TOOLS: | ||
executable_path = os.path.join(self._INSTALL_LOCATION, filename) | ||
if not is_executable(executable_path): | ||
raise self.XCodeToolsUnavailable( | ||
"'{}' is not an executable file, but it is required to build " | ||
"native code on this platform. You may need to install the XCode " | ||
"command line developer tools." | ||
.format(executable_path)) | ||
|
||
def path_entries(self): | ||
self._check_executables_exist() | ||
return [self._INSTALL_LOCATION] |
8 changes: 8 additions & 0 deletions
8
src/python/pants/backend/native/subsystems/platform_specific/linux/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_library( | ||
dependencies=[ | ||
'src/python/pants/binaries:binary_util', | ||
], | ||
) |
Empty file.
19 changes: 19 additions & 0 deletions
19
src/python/pants/backend/native/subsystems/platform_specific/linux/binutils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
import os | ||
|
||
from pants.binaries.binary_tool import ExecutablePathProvider, NativeTool | ||
|
||
|
||
class Binutils(NativeTool, ExecutablePathProvider): | ||
options_scope = 'binutils' | ||
default_version = '2.30' | ||
archive_type = 'tgz' | ||
|
||
def path_entries(self): | ||
return [os.path.join(self.select(), 'bin')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great explanation.