Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tools/install/install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def _python_version(ctx):
"""Returns a string a containing the major.minor version number of the
current Python toolchain."""
py_cc_toolchain = ctx.toolchains[_PY_CC_TOOLCHAIN_TYPE].py_cc_toolchain
return py_cc_toolchain.python_version
version = py_cc_toolchain.python_version
major_minor = version.split(".")[:2]
return ".".join(major_minor)

#------------------------------------------------------------------------------
def _output_path(ctx, input_file, strip_prefix = [], ignore_errors = False):
Expand Down
18 changes: 13 additions & 5 deletions tools/workspace/python/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
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 _python_version_txt(ctx):
_PY_CC_TOOLCHAIN_TYPE = "@rules_python//python/cc:toolchain_type"

def _python_version(ctx):
"""Returns a string a containing the major.minor version number of the
current Python toolchain."""
py_cc_toolchain = ctx.toolchains[_PY_CC_TOOLCHAIN_TYPE].py_cc_toolchain
version = py_cc_toolchain.python_version
major_minor = version.split(".")[:2]
return ".".join(major_minor)

def _python_version_txt_impl(ctx):
"""Implementation of the python_version_txt() rule, below."""
output = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = output,
content = py_cc_toolchain.python_version,
content = _python_version(ctx),
is_executable = False,
)
return [DefaultInfo(
Expand All @@ -20,7 +28,7 @@ def _python_version_txt(ctx):
)]

python_version_txt = rule(
implementation = _python_version_txt,
implementation = _python_version_txt_impl,
toolchains = [_PY_CC_TOOLCHAIN_TYPE],
doc = """Generates a text file containing the major.minor version number of
the current Python toolchain, without any newlines.""",
Expand Down