Skip to content

Conversation

@deustis
Copy link

@deustis deustis commented Jan 8, 2026

Initial Bazel configuration for Pybind11 and S2Point bindings.
#522

The S2 Geometry library is transitioning from SWIG-based bindings to pybind11-based bindings.

During this migration:

  • SWIG bindings (s2geometry): The current production bindings, built with CMake. Use import s2geometry to access these.
  • pybind11 bindings (s2geometry_pybind): The new bindings under development, built with Bazel. Use import s2geometry_pybind to access these.

Once the pybind11 bindings are feature-complete and stable, the SWIG bindings will be deprecated and the pybind11 package will be renamed to s2geometry to become the primary Python API.

This PR adds Bazel configuration for building and testing the bindings on the local toolchain. For distribution we'll need to generate a wheel for compatibility with different OS and python versions.

@google-cla
Copy link

google-cla bot commented Jan 8, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@deustis deustis mentioned this pull request Jan 8, 2026
@jmr
Copy link
Member

jmr commented Jan 8, 2026

Looks like a good start. I'll have a closer look tomorrow.

How much are you planning on wrapping?

@deustis
Copy link
Author

deustis commented Jan 8, 2026

Looks like a good start. I'll have a closer look tomorrow.

Great!

How much are you planning on wrapping?

For my purposes I mainly need S2Cell and its dependencies. But I'm happy to do more to get this to whatever you would consider a viable MVP. Open to suggestions there?

@deustis deustis force-pushed the deustis/pybind11_bazel_setup branch from 6535f67 to 7b92d38 Compare January 8, 2026 16:55

## Development

Bazel can be used for development and testing.
Copy link
Member

Choose a reason for hiding this comment

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

What's the CMake / pybind11 story? Say something about that here.

Copy link
Author

Choose a reason for hiding this comment

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

Added some explanation about this. Currently CMake only builds the SWIG package. We'll probably need to update CMake to build the wheel for the final distribution but I'll save that for another PR if that's alright with you?


```
python/
├── bindings/ # Dir for C++ pybind11 bindings
Copy link
Member

Choose a reason for hiding this comment

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

Why is there both bindings/ and s2geometry_pybind/?

It seems like one should do, and everything could probably go directly in python/, but I'm not sure.

Copy link
Author

Choose a reason for hiding this comment

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

I believe we do do need the s2geometry_pybind to match the name of the python package. Open to suggestions on naming there.

The bindings and tests directories are not necessary. We can certainly flatten one or both of those into the python parent if you prefer. Happy to go with whatever layout you prefer here.

│ └── ... # Bindings for additional classes
├── s2geometry_pybind/ # Dir for Python package
│ └── __init__.py # Package initialization
├── tests/ # Dir for Python unit tests
Copy link
Member

Choose a reason for hiding this comment

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

tests/ subdir also seems inconsistent with the rest of the project. Why not keep tests with the code?

Copy link
Author

Choose a reason for hiding this comment

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

Sure, we can drop. Just let me know what layout you prefer in the comment above.

package(default_visibility = ["//python:__subpackages__"])

pybind_extension(
name = "s2geometry_bindings",
Copy link
Member

Choose a reason for hiding this comment

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

Should this be public?

Copy link
Author

Choose a reason for hiding this comment

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

Add the underscore prefix to indicate it is a private module


pybind_extension(
name = "s2geometry_bindings",
srcs = ["module.cc"],
Copy link
Member

Choose a reason for hiding this comment

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

Does it make any sense to have fine-grained targets? In google3, we have fine-grained targets for the S2 library, which really helps compilation and development time. We don't use them for PyClif mostly because the C++ targets were sharded after the pyclif ones were added.

Do we just accept that it's monolithic in Python?

Copy link
Author

@deustis deustis Jan 12, 2026

Choose a reason for hiding this comment

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

We could consider adding some submodules. How about if we put the "core" classes directly under the top-level package (e.g. s2geometry.S2Cell), but add submodules for some of the utility classes (e.g. s2geometry.regions.S2CellCovering). Open to suggestions on how to break this up.

Once we align on the python module structure, I think we would then define one pybind_extension rule per module, plus separate pybind_library rules for each class.

@deustis
Copy link
Author

deustis commented Jan 12, 2026

@jmr, I made most of the changes you suggested. Let me know what you prefer for the directory layout and how to organize the python modules and I'll make those changes as well.

@deustis deustis requested a review from jmr January 12, 2026 17:00
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

Successfully merging this pull request may close these issues.

2 participants