jvolkman / rules_pycross

Bazel + Python rules for cross-platform external dependencies
Apache License 2.0
60 stars 23 forks source link
bazel bazel-rules python

rules_pycross - Python + cross platform

Use your Poetry or PDM or UV lock files with Bazel and enabling cross-platform builds.

Features:

Notice: UV is still experimental

See the examples.

Why?

The current Bazel rules for working with Python external dependencies have a couple of issues that make cross-platform usage difficult (see https://github.com/bazelbuild/rules_python/issues/260):

How?

A pip install operation can be roughly broken down into these parts:

  1. determine the environment (OS and Python version/implementation)
  2. resolve the dependencies of the package to install, some of which may be platform-specific (optionally constrained by a pre-compiled lock file)
  3. figure out which files to download - either pre-built wheels matching the current platform or sdists to build locally
  4. download sdists and wheels
  5. build and install sdists; install wheels

rules_pycross attempts to deconstruct this operation into its constituent parts and glue them together with Bazel:

  1. pycross_target_environment is used to specify target environments ahead of time provided with ABI, platform, and implementation parameters (similar to pip's --abi, --platform, and --implementation flags). These environments are selected using Bazel's own platform/constraint system.
  2. pycross_lock_file generates a "lock" .bzl file from an input poetry.lock. This .bzl file contains a mix of http_file repositories and pycross_* targets.
  3. pycross_wheel_build builds sdist.tar.gz archives into Python wheels. This is a build action, not a WORKSPACE operation.
  4. pycross_wheel_library "installs" (extracts) a Python wheel - either downloaded or built from an sdist - and provides it as a py_library.

See the generated docs.