Skip to content
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

Expose libpython from toolchain like interpreter #2262

Open
keith opened this issue Sep 30, 2024 · 4 comments
Open

Expose libpython from toolchain like interpreter #2262

keith opened this issue Sep 30, 2024 · 4 comments

Comments

@keith
Copy link
Member

keith commented Sep 30, 2024

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?

@rickeylev
Copy link
Collaborator

@keith
Copy link
Member Author

keith commented Sep 30, 2024

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,
)

@rickeylev
Copy link
Collaborator

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?)

@keith
Copy link
Member Author

keith commented Oct 3, 2024

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

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

No branches or pull requests

2 participants