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

Bump nanobind-bazel to v2.1.0, add stubgen target #1824

Merged
merged 2 commits into from
Jul 30, 2024
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
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ use_repo(pip, "tools_pip_deps")

# -- bazel_dep definitions -- #

bazel_dep(name = "nanobind_bazel", version = "2.0.0", dev_dependency = True)
bazel_dep(name = "nanobind_bazel", version = "2.1.0", dev_dependency = True)
8 changes: 7 additions & 1 deletion bindings/python/google_benchmark/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension")
load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension", "nanobind_stubgen")

py_library(
name = "google_benchmark",
Expand All @@ -15,6 +15,12 @@ nanobind_extension(
deps = ["//:benchmark"],
)

nanobind_stubgen(
name = "benchmark_stubgen",
marker_file = "bindings/python/google_benchmark/py.typed",
module = ":_benchmark",
)

py_test(
name = "example",
srcs = ["example.py"],
Expand Down
38 changes: 30 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def bazel_build(self, ext: BazelExtension) -> None:

bazel_argv = [
"bazel",
"build",
"run",
ext.bazel_target,
f"--symlink_prefix={temp_path / 'bazel-'}",
f"--compilation_mode={'dbg' if self.debug else 'opt'}",
Expand Down Expand Up @@ -127,20 +127,42 @@ def bazel_build(self, ext: BazelExtension) -> None:
else:
suffix = ".abi3.so" if ext.py_limited_api else ".so"

ext_name = ext.target_name + suffix
ext_bazel_bin_path = temp_path / "bazel-bin" / ext.relpath / ext_name
ext_dest_path = Path(self.get_ext_fullpath(ext.name)).with_name(
ext_name
)
shutil.copyfile(ext_bazel_bin_path, ext_dest_path)
# copy the Bazel build artifacts into setuptools' libdir,
# from where the wheel is built.
pkgname = "google_benchmark"
pythonroot = Path("bindings") / "python" / "google_benchmark"
srcdir = temp_path / "bazel-bin" / pythonroot
libdir = Path(self.build_lib) / pkgname
for root, dirs, files in os.walk(srcdir, topdown=True):
# exclude runfiles directories and children.
dirs[:] = [d for d in dirs if "runfiles" not in d]

for f in files:
print(f)
fp = Path(f)
should_copy = False
# we do not want the bare .so file included
# when building for ABI3, so we require a
# full and exact match on the file extension.
if "".join(fp.suffixes) == suffix:
should_copy = True
elif fp.suffix == ".pyi":
should_copy = True
elif Path(root) == srcdir and f == "py.typed":
# copy py.typed, but only at the package root.
should_copy = True

if should_copy:
shutil.copyfile(root / fp, libdir / fp)


setuptools.setup(
cmdclass=dict(build_ext=BuildBazelExtension),
package_data={"google_benchmark": ["py.typed", "*.pyi"]},
ext_modules=[
BazelExtension(
name="google_benchmark._benchmark",
bazel_target="//bindings/python/google_benchmark:_benchmark",
bazel_target="//bindings/python/google_benchmark:benchmark_stubgen",
py_limited_api=py_limited_api,
)
],
Expand Down
Loading