zig build
https://codeberg.org/dasimmet/zig-python-sdist
- Zig 92.9%
- Python 6.9%
- C 0.2%
| .forgejo/workflows | add ci | |
| example | rename createSdistStep in example | |
| src | add renamesdist function | |
| .gitignore | start using unittest | |
| build.zig | link renameextlib | |
| build.zig.zon | upgrade zig | |
| LICENSE | add license | |
| pkg-info.zon | refactor pkg-info.zon | |
| README.md | format references | |
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:
addVersionadds__version__and__version_str__symbols to your module.addZigBuiltinadds a__zig_builtin__symbol to your module populated by zig's build information.wrapMainwraps a Zig "JuicyMain" function (fn (std.process.Init) !void) to be used as an entrypoint for your python module, by populating thestd.process.initfrom python's builtinosandsysmodules.
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