rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

feat: compile source files at build time

Open rickeylev opened this issue 1 year ago • 1 comments

This implements precompiling: performing Python source to byte code compilation at build time. This allows improved program startup time by allowing the byte code compilation step to be skipped at runtime.

Precompiling is disabled by default, for now. A subsequent release will enable it by default. This allows the necessary flags and attributes to become available so users can opt-out prior to it being enabled by default. Similarly, //python:features.bzl is introduced to allow feature detection.

This implementation is made to serve a variety of use cases, so there are several attributes and flags to control behavior. The main use cases being served are:

  • Large mono-repos that need to incrementally enable/disable precompiling.
  • Remote execution builds, where persistent workers aren't easily available.
  • Environments where toolchains are custom defined instead of using the ones created by rules_python.

To that end, there are several attributes and flags to control behavior, and the toolchains allow customizing the tools used.

Fixes https://github.com/bazelbuild/rules_python/issues/1761

rickeylev avatar May 15 '24 21:05 rickeylev

My apologies for the very large PR. The core logic in common_bazel.bzl#maybe_precompile isn't too big, but there's a lot of supporting code to define a toolchain, flags, attributes, precompiler tools, and etc.

I'll work on the CI failures. It looks like I'll need to add extra logic to gate these behind bazel 7 and pystar.

rickeylev avatar May 15 '24 21:05 rickeylev