Skip to content

Commit

Permalink
On Windows, have the autodetecting Python toolchain revert to reading…
Browse files Browse the repository at this point in the history
… --python_path

This works around the fact that the autodetecting toolchain doesn't run under Windows (#7844). We'll have to add a Windows toolchain in the future, and then remove --python_path (and also address the EnsurePythonPathOption behavior in blaze_util_windows.cc).

RELNOTES: None
PiperOrigin-RevId: 241051815
  • Loading branch information
brandjon authored and copybara-github committed Mar 29, 2019
1 parent f8de2ec commit 54b1eb9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,19 @@ private static PyRuntimeInfo initRuntimeFromToolchain(
"The Python toolchain does not provide a runtime for Python version %s",
version.name()));
}

// Hack around the fact that the autodetecting Python toolchain, which is automatically
// registered, does not yet support windows. In this case, we want to return null so that
// BazelPythonSemantics falls back on --python_path. See toolchain.bzl.
// TODO(#7844): Remove this hack when the autodetecting toolchain has a windows implementation.
if (py2RuntimeInfo != null
&& py2RuntimeInfo.getInterpreterPathString() != null
&& py2RuntimeInfo
.getInterpreterPathString()
.equals("/_magic_pyruntime_sentinel_do_not_use")) {
return null;
}

return result;
}

Expand Down
1 change: 1 addition & 0 deletions tools/python/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ constraint_setting(name = "py3_interpreter_path")
define_autodetecting_toolchain(
name = "autodetecting_toolchain",
pywrapper_template = "pywrapper_template.txt",
windows_config_setting = "@bazel_tools//src/conditions:windows",
)
27 changes: 25 additions & 2 deletions tools/python/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ register_toolchains("//my_pkg:my_toolchain")
# toolchain, based on the "py" wrapper (e.g. "py -2" and "py -3"). Use select()
# in the template attr of the _generate_*wrapper targets.

def define_autodetecting_toolchain(name, pywrapper_template):
def define_autodetecting_toolchain(
name,
pywrapper_template,
windows_config_setting):
"""Defines the autodetecting Python toolchain.
For use only by @bazel_tools//tools/python:BUILD; see the documentation
Expand All @@ -127,6 +130,9 @@ def define_autodetecting_toolchain(name, pywrapper_template):
"autodetecting_toolchain". This param is present only to make the
BUILD file more readable.
pywrapper_template: The label of the pywrapper_template.txt file.
windows_config_setting: The label of a config_setting that matches when
the platform is windows, in which case the toolchain is configured
in a way that triggers a workaround for #7844.
"""
if native.package_name() != "tools/python":
fail("define_autodetecting_toolchain() is private to " +
Expand Down Expand Up @@ -163,9 +169,26 @@ def define_autodetecting_toolchain(name, pywrapper_template):
visibility = ["//visibility:private"],
)

# This is a dummy runtime whose interpreter_path triggers the native rule
# logic to use the legacy behavior on Windows.
# TODO(#7844): Remove this target.
native.py_runtime(
name = "_sentinel_py2_runtime",
interpreter_path = "/_magic_pyruntime_sentinel_do_not_use",
python_version = "PY2",
visibility = ["//visibility:private"],
)

py_runtime_pair(
name = "_autodetecting_py_runtime_pair",
py2_runtime = ":_autodetecting_py2_runtime",
py2_runtime = select({
# If we're on windows, inject the sentinel to tell native rule logic
# that we attempted to use the autodetecting toolchain and need to
# switch back to legacy behavior.
# TODO(#7844): Remove this hack.
windows_config_setting: ":_sentinel_py2_runtime",
"//conditions:default": ":_autodetecting_py2_runtime",
}),
py3_runtime = ":_autodetecting_py3_runtime",
visibility = ["//visibility:public"],
)
Expand Down

0 comments on commit 54b1eb9

Please sign in to comment.