Skip to content

Commit

Permalink
Make pybind_extension() work on Windows.
Browse files Browse the repository at this point in the history
Fixes #74.
  • Loading branch information
junyer committed Mar 2, 2024
1 parent d4bdf59 commit 3aca775
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
version = "2.11.1.bzl.2",
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.31.0")
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Provided rules:

- `pybind_extension`: Builds a python extension, automatically adding the
required build flags and pybind11 dependencies. It defines a `*.so` target
which can be included as a `data` dependency of a `py_*` target.
required build flags and pybind11 dependencies. It defines a target which
can be included as a `data` dependency of a `py_*` target.
- `pybind_library`: Builds a C++ library, automatically adding the required
build flags and pybind11 dependencies. This library can then be used as a
dependency of a `pybind_extension`. The arguments match a `cc_library`.
Expand Down
27 changes: 25 additions & 2 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"""Build rules for pybind11."""

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

def register_extension_info(**kwargs):
pass

Expand All @@ -24,8 +26,15 @@ PYBIND_DEPS = [
]

# Builds a Python extension module using pybind11.
# This can be directly used in python with the import statement.
# This adds rules for a .so binary file.
# This can be directly used in Python with the import statement.
# Assuming the name NAME, the following targets will be defined:
# 1. NAME.so - the shared/dynamic library for the extension module
# 2. NAME.pyd - a copy of NAME.so named for Python on Windows; see
# https://github.com/pybind/pybind11_bazel/issues/74
# 3. NAME - an alias pointing to either NAME.so or NAME.pyd as per
# the platform OS (not-Windows or Windows, respectively)
# Generally, the user will "depend" on this extension module via the
# data attribute of their py_* target; specifying NAME is preferred.
def pybind_extension(
name,
copts = [],
Expand Down Expand Up @@ -55,6 +64,20 @@ def pybind_extension(
**kwargs
)

copy_file(
name = name + "_copy_so_to_pyd",
src = name + ".so",
out = name + ".pyd",
)

native.alias(
name = name,
actual = select({
"@platforms//os:windows": name + ".pyd",
"//conditions:default": name + ".so",
}),
)

# Builds a pybind11 compatible library. This can be linked to a pybind_extension.
def pybind_library(
name,
Expand Down

0 comments on commit 3aca775

Please sign in to comment.