1
2
Fork
You've already forked zig-python-sdist
0
a toolset to build python source distributions for pypi directly using zig build https://codeberg.org/dasimmet/zig-python-sdist
  • Zig 92.9%
  • Python 6.9%
  • C 0.2%
Tobias Simetsreiter a561f8f19e
Some checks are pending
/ Build and Test (Windows) (push) Waiting to run
/ Build and Test (Linux) (push) Successful in 3m41s
link renameextlib
2026年07月07日 13:26:58 +02:00
.forgejo/workflows add ci 2026年06月27日 08:25:22 +02:00
example rename createSdistStep in example 2026年07月05日 16:33:27 +02:00
src add renamesdist function 2026年07月07日 08:17:02 +02:00
.gitignore start using unittest 2026年07月03日 09:19:28 +02:00
build.zig link renameextlib 2026年07月07日 13:26:58 +02:00
build.zig.zon upgrade zig 2026年07月05日 09:24:43 +02:00
LICENSE add license 2026年06月26日 12:00:37 +02:00
pkg-info.zon refactor pkg-info.zon 2026年06月28日 09:59:28 +02:00
README.md format references 2026年07月05日 18:24:22 +02:00

Zig Python Sdist

this is a toolset to build python source distributions for pypi directly using zig build. It's inspired by Loris Cro's talk Deinventing the Wheel.

It allows to add a python distribution to zig packages without dealing with python package metadata the traditional way.

You find a full usage example in the example/ directory.

build.zig api

to generate a python sdist archive, add the zig package to the build.zig.zon file of your existing zig >= 0.16.0 project by running:

zig fetch --save git+https://codeberg.org/dasimmet/zig-python-sdist

then, import the dependency and create a source distribution. You will have to specify all files or directories (subdirectories are not added recursively) necessary to build the extension to add to the sdist archive. the zig-python-sdist dependency is added automatically for you as well as a --fork command line argument to zig build at wheel build time.

constzig_python_sdist=@import("zig_python_sdist");pubfnbuild(b:*std.Build)void{constbuild_ext_step=b.step("bdist-wheel","build the extension module");{// generate a module from cpython headersconstcpython=zig_python_sdist.createCPythonModule(b,.{.target=target,.version=zon.version,});// create a shared library, linked against cpythonconstsdist_dummy=b.addLibrary(.{.name="sdist_dummy",.linkage=.dynamic,.root_module=b.createModule(.{.root_source_file=b.path("sdist_dummy.zig"),.target=target,.optimize=optimize,.imports=&.{.{.name="cpython",.module=cpython},},}),});// install the extension on the "bdist-wheel" top level stepconstsdist_dummy_install=zig_python_sdist.installExtLib(b,sdist_dummy);build_ext_step.dependOn(&sdist_dummy_install.step);}constsdist=zig_python_sdist.createSdistStep(b,.{.name="python-package-name",.version=@import("build.zig.zon).version,.description=b.path("README.md"),.build=.{// the source distribution will build the "bdist-wheel" step on wheel build.args=&.{b.fmt("-Doptimize={s}",.{@tagName(optimize)}),build_ext_step.name,},},// add all files and directories necessary to build to the source distribution.files=&.{.{.src=b.path("build.zig.zon"),.dest="build.zig.zon"},.{.src=b.path("build.zig"),.dest="build.zig"},.{.src=b.path("README.md"),.dest="README.md"},.{.src=b.path("LICENSE"),.dest="LICENSE"},},.directories=&.{.{.src=b.path("src"),.dest="src"},},// tip: when adding dependency directories, pass a `--fork=<path inside sdist>` to// the build arguments.});// the install step only builds the source distributionconstsdist_install=sdist.install();b.getInstallStep().dependOn(&sdist_install.step);}

cpython zig module

the generated cpython module provides a c member holding the translated cpython headers as well as some utility functions to:

  • addVersion adds __version__ and __version_str__ symbols to your module.
  • addZigBuiltin adds a __zig_builtin__ symbol to your module populated by zig's build information.
  • wrapMain wraps a Zig "JuicyMain" function (fn (std.process.Init) !void) to be used as an entrypoint for your python module, by populating the std.process.init from python's builtin os and sys modules.

take a look at example/sdist_dummy.zig

Testing the distribution installation

to test the built source distribution, you can create a VenvInstallTest step that will:

  • create a python virtual environment
  • compile and install your package
  • run python unittest files against the installed package
  • run entrypoints from the installed package to test them

References