rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

Expose libpython from toolchain like `interpreter`

Open keith opened this issue 1 year ago • 4 comments

Currently if you're writing custom rules that depend on python, it can be useful to extract things from the underlying python toolcahin like toolchain.py3_runtime.interpreter. I think it would be useful to also be able to extract the libpython that is included in the toolchain in a similar way, which would be useful for linking to binaries in custom rules, or providing through env vars at runtime. The library is already accessible if you search through the files on the runtime, but being able to access it in a more structured way would be useful for this case.

Thoughts?

keith avatar Sep 30 '24 16:09 keith

Is https://rules-python.readthedocs.io/en/latest/api/rules_python/python/cc/index.html#current_py_cc_libs what you want?

rickeylev avatar Sep 30 '24 23:09 rickeylev

I could use that in my custom rule alongside the toolchain itself, but I was thinking that this might make sense as an attribute on the toolchain. To be more concrete my rule looks like this:

def _impl(ctx):
    ...

    transitive_runfiles = [
        ctx.runfiles(files = py_toolchain.py3_runtime.files.to_list()),
    ]

    libpython = None
    for file in py_toolchain.py3_runtime.files.to_list():
        if file.basename.startswith("libpython"):
            libpython = file.short_path

            # if there are multiple any of them should work and they are likely symlinks to each other
            break

    if not libpython:
        fail("Failed to find libpython")

    return [
        DefaultInfo(executable = ...),
        RunEnvironmentInfo(
            environment = {"PYTHON_LIBRARY": libpython},
        ),
    ]


foo = rule(
    implementation = _impl,
    toolchains = [
        "@bazel_tools//tools/python:toolchain_type",
    ],
    executable = True,
)

keith avatar Sep 30 '24 23:09 keith

Curious -- why do you need to pass the path via an environment variable? To setup LD_LIBRARY_PATH later or something? (I can't recall -- I think you were one of the people doing embedded Python?)

rickeylev avatar Oct 03 '24 20:10 rickeylev

Our case is pretty unique, this is with the mojo programming language which offers python interop, so I need to tie together the actual python toolchain we want it to use by providing it through the env

keith avatar Oct 03 '24 20:10 keith