-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[build] Use py_cc_toolchain for configuring pybind11 (#22346)
The py_cc_toolchain is a rules_python concept that bridges the Python and C++ toolchains by providing cc_library targets for the headers and libraries for compiling C code against the Python interpreter selected by the Python toolchain. This commit changes our python repository to define that new kind of toolchain and our pydrake bindings to use it. The toolchains are now encapsulated as `@python//:all`, moved from `//tools/py_toolchain`. Important note for future bzlmod users: because our python repository rule can fail when run on a system without our default interpreter, our MODULE.bazel will no longer register our default python toolchain automatically. Downstream Bazel projects using Drake as a module will need to opt-in to the local toolchain. We provide new labels in //tools/workspace/python as a single point of control for depending on Python. The comments in python/repository.bzl provide an overview of how the pieces all fit together. The legacy libraries `@python` and `@python//:python_direct_link` are deprecated.
- Loading branch information
1 parent
6a7293d
commit a1dec15
Showing
16 changed files
with
220 additions
and
103 deletions.
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
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
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
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
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 |
---|---|---|
@@ -1,6 +1,42 @@ | ||
# This file exists to make our directory into a Bazel package, so that our | ||
# neighboring *.bzl file can be loaded elsewhere. | ||
|
||
load("//tools/lint:lint.bzl", "add_lint_tests") | ||
load(":defs.bzl", "current_py_cc_libpython", "python_version_txt") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
# Provides a text file containing the major.minor version number of the current | ||
# Python toolchain, without any newlines. This file may be used as srcs or data | ||
# for any other rule whose action needs to know the current python version. | ||
python_version_txt( | ||
name = "python_version.txt", | ||
) | ||
|
||
# Provides a single point of control within Drake for how to compile a native | ||
# C/C++ Python module (e.g., for pybind11) for the current Python toolchain. | ||
# This may be used like it was a cc_library target that listed hdrs= and | ||
# includes= for the current Python's header files. | ||
alias( | ||
name = "cc_headers", | ||
actual = "@rules_python//python/cc:current_py_cc_headers", | ||
) | ||
|
||
# Provides a single point of control within Drake for how to link a native | ||
# C/C++ Python module (e.g., for pybind11) for the current Python toolchain. | ||
# This may be used like it was a cc_library target that listed linkopts= for | ||
# any libraries use by a Python module. Note that this is intended for linking | ||
# native modules, and does NOT link the libpython embedded runtime; for that, | ||
# use cc_libpython below. | ||
alias( | ||
name = "cc_libs", | ||
actual = "@rules_python//python/cc:current_py_cc_libs", | ||
) | ||
|
||
# Provides a single point of control within Drake for how to link a C/C++ | ||
# executable that embeds the current Python toolchain's interpreter (e.g., for | ||
# Python unit tests which are implemented as C++ programs). This alias may be | ||
# used like it was a cc_library target that listed linkopts= for the libpython | ||
# embedded runtime. | ||
current_py_cc_libpython( | ||
name = "cc_libpython", | ||
) | ||
|
||
add_lint_tests() |
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,55 @@ | ||
load("@rules_cc//cc/common:cc_common.bzl", "cc_common") | ||
load("//tools/skylark:cc.bzl", "CcInfo") | ||
|
||
_PY_CC_TOOLCHAIN_TYPE = "@rules_python//python/cc:toolchain_type" | ||
|
||
# These rules are intended for use only by our neighboring BUILD.bazel file. | ||
# The comments in that file explain how and why to use these rules' output. | ||
|
||
def _current_py_cc_libpython(ctx): | ||
py_cc_toolchain = ctx.toolchains[_PY_CC_TOOLCHAIN_TYPE].py_cc_toolchain | ||
linkopts = ["-lpython{}".format(py_cc_toolchain.python_version)] | ||
return [ | ||
CcInfo( | ||
compilation_context = None, | ||
linking_context = cc_common.create_linking_context( | ||
linker_inputs = depset( | ||
direct = [ | ||
cc_common.create_linker_input( | ||
owner = ctx.label, | ||
user_link_flags = depset(linkopts), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
] | ||
|
||
current_py_cc_libpython = rule( | ||
implementation = _current_py_cc_libpython, | ||
toolchains = [_PY_CC_TOOLCHAIN_TYPE], | ||
provides = [CcInfo], | ||
doc = """Provides the linker flags for how to link a C/C++ executable | ||
that embeds a Python interpreter (e.g., for unit testing), based on the | ||
current Python toolchain. Use this rule like a cc_library.""", | ||
) | ||
|
||
def _python_version_txt(ctx): | ||
py_cc_toolchain = ctx.toolchains[_PY_CC_TOOLCHAIN_TYPE].py_cc_toolchain | ||
output = ctx.actions.declare_file(ctx.label.name) | ||
ctx.actions.write( | ||
output = output, | ||
content = py_cc_toolchain.python_version, | ||
is_executable = False, | ||
) | ||
return [DefaultInfo( | ||
files = depset([output]), | ||
data_runfiles = ctx.runfiles(files = [output]), | ||
)] | ||
|
||
python_version_txt = rule( | ||
implementation = _python_version_txt, | ||
toolchains = [_PY_CC_TOOLCHAIN_TYPE], | ||
doc = """Generates a text file containing the major.minor version number of | ||
the current Python toolchain, without any newlines.""", | ||
) |
Oops, something went wrong.