Skip to content

Add support for hermetic Python #29

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

Merged
merged 1 commit into from
Sep 17, 2021
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ Then, in your `BUILD` file:
```starlark
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
```

## Hermetic Python

To configure `pybind11_bazel` for hermetic Python, `python_configure` can take
the target providing the Python runtime as an argument:

```starlark
python_configure(
name = "local_config_python",
python_interpreter_target = "@python_interpreter//:python_bin",
)
```
13 changes: 11 additions & 2 deletions python_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ def _read_dir(repository_ctx, src_dir):
result = find_result.stdout
return result

def _genrule(src_dir, genrule_name, command, outs):
def _genrule(src_dir, genrule_name, command, outs, local):
"""Returns a string with a genrule.

Genrule executes the given command and produces the given outputs.
"""
return (
"genrule(\n" +
' name = "' +
genrule_name + '",\n' +
genrule_name + '",\n' + (
" local = 1,\n" if local else "") +
" outs = [\n" +
outs +
"\n ],\n" +
Expand Down Expand Up @@ -149,11 +150,15 @@ def _symlink_genrule_for_dir(
genrule_name,
" && ".join(command),
"\n".join(outs),
local = True,
)
return genrule

def _get_python_bin(repository_ctx):
"""Gets the python bin path."""
if repository_ctx.attr.python_interpreter_target != None:
return str(repository_ctx.path(repository_ctx.attr.python_interpreter_target))

python_bin = repository_ctx.os.environ.get(_PYTHON_BIN_PATH)
if python_bin != None:
return python_bin
Expand Down Expand Up @@ -391,6 +396,7 @@ python_configure = repository_rule(
],
attrs = {
"python_version": attr.string(default=""),
"python_interpreter_target": attr.label(),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

would suggest library target for completeness

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean to substitute the check at _get_python_lib? What would this target look like? As far as I understand, we should be getting the correct value by querying the interpreter.

)
"""Detects and configures the local Python.
Expand All @@ -409,4 +415,7 @@ Args:
If set to "2", will build for Python 2 (`which python2`).
By default, will build for whatever Python version is returned by
`which python`.
python_interpreter_target: If set to a target providing a Python binary,
will configure for that Python version instead of the installed
binary. This configuration takes precedence over python_version.
"""