This release is a pre-release and may not be stable for production use.
Clang Build Extension
Overview
clang-build-ext is a setuptools plugin that builds Python C/C++ extensions using the
LLVM/Clang compiler toolchain instead of the system default compiler.
Either a system-installed LLVM/Clang or the
karellen-llvm-clang package can be used.
The plugin provides drop-in replacements for setuptools' build_ext and build_clib commands,
along with features such as glob pattern expansion in source lists, Drakon IR bytecode embedding,
and thin static library support.
Basic Usage
Add the following to your setup.py:
from setuptools import setup, Extension
from karellen.clang_build_ext import ClangBuildExt, ClangBuildClib
setup(
...,
ext_modules=[Extension("myext", ["src/*.c"])],
cmdclass={
"build_ext": ClangBuildExt,
"build_clib": ClangBuildClib,
},
)
LLVM Toolchain
The compiler uses the full LLVM toolchain:
| Tool | Command |
|---|---|
| C compiler | clang |
| C++ compiler | clang-cpp |
| Linker | clang -fuse-ld=lld / clang-cpp -fuse-ld=lld |
| Archiver | llvm-ar |
| Objcopy | llvm-objcopy |
| Readelf | llvm-readelf |
Features
Glob Pattern Expansion
Source file lists in both extensions and libraries support shell glob patterns. Patterns are
expanded at build time, so you don't need to enumerate every source file in setup.py:
setup(
...,
ext_modules=[
Extension("myext", ["src/module/*.c", "src/module/**/*.c"]),
],
libraries=[
("mylib", {"sources": ["src/lib/*.c", "src/lib/**/*.c"]}),
],
cmdclass={
"build_ext": ClangBuildExt,
"build_clib": ClangBuildClib,
},
)
Drakon Enhancements
Drakon mode embeds LLVM intermediate representation (IR) bytecode into compiled binaries as custom ELF sections. This enables post-compilation IR analysis and transformation of the final binary.
When enabled, the build:
- Compiles with
--save-temps=obj -fno-discard-value-namesto produce.bc(bitcode) files alongside object files - Includes
.bcfiles in static libraries created viabuild_clib - After linking, extracts
.bcfiles from all linked objects and static libraries and embeds them into the output binary as.drakon.<name>ELF sections (markednoload,readonly)
Enable via command line or environment variable:
# Command line
python setup.py build_ext --drakon
python setup.py build_ext -d
# Environment variable
DRAKON=1 python setup.py build_ext
Thin Static Libraries
Thin static libraries store references to object files rather than copies, reducing build artifact size during development.
Enable via command line or environment variable:
# Command line
python setup.py build_ext --thin
python setup.py build_ext -T
# Environment variable
THIN=1 python setup.py build_ext
Both options can be combined:
python setup.py build_ext --drakon --thin
The build_clib command inherits drakon and thin settings from build_ext automatically.
Setuptools Compatibility
clang-build-ext maintains compatibility across setuptools versions, including the API changes
in setuptools 75+ (new C++ compiler executables) and 82+ (removal of the dry_run parameter).
Metadata
Release files for clang-build-ext 0.0.4.dev20260320015434
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| clang_build_ext-0.0.4.dev20260320015434.tar.gz | 13.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.2 kB
Release files / clang_build_ext-0.0.4.dev20260320015434.tar.gz
| Download URL | clang_build_ext-0.0.4.dev20260320015434.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eedf1629eb24305a3892d016fb9c8f6fa47cfac28893626c101cc20291038b8a
|
|
BLAKE2b-256 checksum How to use checksums |
53dbfb9ade2028557cbdadd067f27efb9a48c58088177518c5a92ab36f877165
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl
| Download URL | clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b87c3086b60c3336b75b2b3a87dc5594305ac547edce13ca0c5300fdc97c2862
|
|
BLAKE2b-256 checksum How to use checksums |
748e278196e9ced4aedfb69644203222f131540f6620bed166bd9d9355219f7e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|