Clang-based extension builder
Project description
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).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clang_build_ext-0.0.4.dev20260320015434.tar.gz.
File metadata
- Download URL: clang_build_ext-0.0.4.dev20260320015434.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eedf1629eb24305a3892d016fb9c8f6fa47cfac28893626c101cc20291038b8a
|
|
| MD5 |
f03df6aaa1247b9a07530d7cb3d68c6d
|
|
| BLAKE2b-256 |
53dbfb9ade2028557cbdadd067f27efb9a48c58088177518c5a92ab36f877165
|
File details
Details for the file clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl.
File metadata
- Download URL: clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b87c3086b60c3336b75b2b3a87dc5594305ac547edce13ca0c5300fdc97c2862
|
|
| MD5 |
81fc3c2967eb58799f1466bad7f8a05c
|
|
| BLAKE2b-256 |
748e278196e9ced4aedfb69644203222f131540f6620bed166bd9d9355219f7e
|