Skip to content

fix(local_runtime): Improve local_runtime usability in macos / windows #3148

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

laramiel
Copy link

@laramiel laramiel commented Aug 6, 2025

local_runtime fails to handle many variations of python install on windows and MacOS, such as:

  • LDLIBRARY on MacOS may refer to a file under PYTHONFRAMEWORKPREFIX, not LIBDIR
  • LDLIBRARY on Windows refers to pythonXY.dll, not the linkable pythonXY.lib
  • LIBDIR may not be correctly set on Windows.
  • On windows, interpreter_path needs to be normalized. Other paths also require this.
  • SHLIB_SUFFIX does not indicate the correct suffix.

For examples, see

See: https://docs.python.org/3/extending/windows.html

In order to resolve this the shared library resolution has been moved into get_local_runtime_info.py, which now does the following:

  • Constructs a list of paths to search based on LIBDIR, LIBPL, PYTHONFRAMEWORKPREFIX, and the executable directory.
  • Constructs a list of libraries to search based on INSTSONAME, LDLIBRARY, pythonXY.lib, etc.
  • Checks to see which files exist, partitioning the result into a list of "dynamic_libraries" and "static_libraries"

On windows and macos, since SHLIB_SUFFIX does not always indicate the filenames needed searching, this has been removed from local_runtime_repo_setup and replaced with an explicit file.

On windows the interpreter_path and other search paths are now normalized ( \ converted to /).

Additional logging added to local_runtime_repo.

Example get_local_runtime_info.py outputs:

rules_python:local_runtime_repo(@@local_python3) INFO: GetPythonInfo result:
  INSTSONAME: Python.framework/Versions/3.12/Python
  LDLIBRARY: Python.framework/Versions/3.12/Python
  LIBDIR: /Library/Frameworks/Python.framework/Versions/3.12/lib
  MULTIARCH: darwin
  PY3LIBRARY: <empty string>
  PYTHONFRAMEWORKPREFIX: /Library/Frameworks
  base_executable: /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12
  implementation_name: cpython
  include: /Library/Frameworks/Python.framework/Versions/3.12/include/python3.12
  major: 3
  micro: 10
  minor: 12

Here we see the MacOS framework issue, namely LDLIBRARY is not under LIBDIR, but directly under frameworks.
The new code adds PYTHONFRAMEWORKPREFIX to the search path, so the library is found.

rules_python:local_runtime_repo(@@local_python3)  INFO: GetPythonInfo result:
  INSTSONAME: None
  LDLIBRARY: python313.dll
  LIBDIR: T:\build_temp\home\python_4379bdec28bdff81a567a01c9b8cf10e3856c8c966e4fe53945bedea6338b416\tools\libs
  MULTIARCH: None
  PY3LIBRARY: python313.lib
  base_executable: T:\build_temp\home\python_4379bdec28bdff81a567a01c9b8cf10e3856c8c966e4fe53945bedea6338b416\tools\python.exe
  implementation_name: cpython
  include: T:\build_temp\home\python_4379bdec28bdff81a567a01c9b8cf10e3856c8c966e4fe53945bedea6338b416\tools\Include
  major: 3
  micro: 1
  minor: 13

Here we see that under windows the python313.dll is reported; python313.lib is added via PY3LIBRARY.

Fixes #3055

local_runtime fails to handle many variations of python install on windows and MacOS, such as:
* LDLIBRARY on MacOS may refer to a file under PYTHONFRAMEWORKPREFIX, not LIBDIR
* LDLIBRARY on Windows refers to pythonXY.dll, not the linkable pythonXY.lib
* LIBDIR may not be correctly set on Windows.
* On windows, interpreter_path needs to be normalized.  Other paths also require this.
* SHLIB_SUFFIX does not indicate the correct suffix.

For examples, see
See: bazel-contrib#3055
See: https://docs.python.org/3/extending/windows.html

Example get_local_runtime_info.py outputs:

rules_python:local_runtime_repo(@@local_python3) INFO: GetPythonInfo result:
  INSTSONAME: Python.framework/Versions/3.12/Python
  LDLIBRARY: Python.framework/Versions/3.12/Python
  LIBDIR: /Library/Frameworks/Python.framework/Versions/3.12/lib
  MULTIARCH: darwin
  PY3LIBRARY: <empty string>
  PYTHONFRAMEWORKPREFIX: /Library/Frameworks
  base_executable: /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12
  implementation_name: cpython
  include: /Library/Frameworks/Python.framework/Versions/3.12/include/python3.12
  major: 3
  micro: 10
  minor: 12

rules_python:local_runtime_repo(@@local_python3)  INFO: GetPythonInfo result:
  INSTSONAME: None
  LDLIBRARY: python313.dll
  LIBDIR: T:\build_temp\home\python_4379bdec28bdff81a567a01c9b8cf10e3856c8c966e4fe53945bedea6338b416\tools\libs
  MULTIARCH: None
  PY3LIBRARY: python313.lib
  base_executable: T:\build_temp\home\python_4379bdec28bdff81a567a01c9b8cf10e3856c8c966e4fe53945bedea6338b416\tools\python.exe
  implementation_name: cpython
  include: T:\build_temp\home\python_4379bdec28bdff81a567a01c9b8cf10e3856c8c966e4fe53945bedea6338b416\tools\Include
  major: 3
  micro: 1
  minor: 13

On windows and macos, since SHLIB_SUFFIX does not always indicate the filenames needed searching, this has been removed from local_runtime_repo_setup and replaced with an explicit list of files.  In addition, target libraries are searched in multiple locations to handle variations in macos and windows file locations.

On windows the interpreter_path and other search paths are now normalized ( \ converted to /).

On windows the get_local_runtime_info now returns the pythonXY.lib library

Additional logging added to local_runtime_repo.
:1
@laramiel
Copy link
Author

laramiel commented Aug 7, 2025

After integrating this some more with tensorstore, I've reworked the library lookup to handle manylinux and other linux installs with no dynamic libraries exposed.

Copy link
Collaborator

@aignas aignas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider also referencing the issue in the changelog.

This PR is very exciting, but I gave it a quick glance only since it is reasonably big.

I would love to ask the local_python example also be made to work with bzlmod - all of the improvements should work there too.

implementation_name = info["implementation_name"],
os = "@platforms//os:{}".format(repo_utils.get_platforms_os_name(rctx)),
))
)
logger.debug("BUILD.bazel\n{}".format(build_bazel))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to have it? You can always do bazel build <repo>//:BUILD.bazel to see the file. I think this can be removed and the changes reordering the writing of the files could be also reverted for a smaller diff.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that, however this is logged at debug level and given my multi-target ci it's helpful to print it in the log.

Copy link
Collaborator

@rickeylev rickeylev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly, I can edit and commit files via the ui, but can't push to laramiel/main on the command line. Is there something special setup with your repo or branch? I was going to move the example over into local toolchains, but it was a bit hard to refactor that via the ui alone.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The integration tests for local toolchains are in tests/integration/local_toolchains. It should already have workspace and bzlmod stuff setup. I think the only thing to do is remove the target_compatible_with constraint that makes it only run on linux/mac.

# on the system - some systems like version suffix, others don't.""
#
# A typical INSTSONAME is 'libpython3.8.so.1.0' on Linux, or
# 'Python.framework/Versions/3.9/Python' on MacOS.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just verifying: On mac, INSTSONAME might be a relative path to a dir, not the filename?


# Set the prefix and suffix to construct the library name used for linking.
if _IS_DARWIN:
# SHLIB_SUFFIX may be ".so"; always override on darwin to be ".dynlib"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why always override? Because dylib is the idiomatic extension? Or for another reason? Whatever the case, please include it as a comment.

Comment on lines +103 to +104
# SHLIB_SUFFIX on windows is ".dll"; however the compiler needs to
# link with the ".lib".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a reference for this? If so, please include a link. If not, oh well.

config_vars = sysconfig.get_config_vars()

# VERSION is X.Y in Linux/macOS and XY in Windows.
if not config_vars.get("VERSION"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case is VERSION missing? I'd think this would always be present. I known, please comment. If this is just defense, that's fine too; just comment its for defense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix: Local Toolchain on Windows: local_runtime_repo generates interpreter_path without escaping backward slashes
3 participants