Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Clang Build Extension

Gitter Build Status Coverage Status

clang-build-ext Version clang-build-ext Python Versions clang-build-ext Downloads Per Day clang-build-ext Downloads Per Week clang-build-ext Downloads Per Month

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 clang-build-ext to your build dependencies in pyproject.toml:

[build-system]
requires = ["setuptools", "clang-build-ext"]
build-backend = "setuptools.build_meta"

Register the custom build commands in 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,
    },
)

Then build with:

python -m build

LLVM Toolchain

The compiler uses the full LLVM toolchain:

Tool Command
C compiler clang
C++ compiler clang++
Linker clang -fuse-ld=lld / clang++ -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.

* matches within a single directory, while ** recurses to any depth. Given this tree:

src/module/
├── module.c
├── util/
│   └── util.c
└── codec/
    └── formats/
        └── png.c
Pattern Matches
src/module/*.c module.c
src/module/*/*.c util/util.c
src/module/**/*.c module.c, util/util.c, codec/formats/png.c

Use * when a flat directory is exactly what you want — it will not silently pick up sources added in a subdirectory later:

setup(
    ...,
    ext_modules=[
        Extension("myext", ["src/module/*.c"]),
    ],
    libraries=[
        ("mylib", {"sources": ["src/lib/*.c"]}),
    ],
    cmdclass={
        "build_ext": ClangBuildExt,
        "build_clib": ClangBuildClib,
    },
)

Use ** to pick up an entire source tree regardless of how deeply it is nested:

setup(
    ...,
    ext_modules=[
        Extension("myext", ["src/module/**/*.c"]),
    ],
    libraries=[
        ("mylib", {"sources": ["src/lib/**/*.c"]}),
    ],
    cmdclass={
        "build_ext": ClangBuildExt,
        "build_clib": ClangBuildClib,
    },
)

**/*.c already covers the top level, so pairing it with *.c is unnecessary. Should you list overlapping patterns anyway, each source is compiled once: duplicates are removed, keeping the order in which they were first matched.

Only the sources entry is glob-expanded. Every other build_info key of a build_clib library (macros, include_dirs, cflags, obj_deps) is passed through to setuptools unchanged.

C++ Extensions

Sources that setuptools detects as C++ (.cc, .cpp, .cxx) are compiled with the clang++ driver, and any extension containing one is linked with it too, so no extra configuration is required to mix C and C++ in one project:

setup(
    ...,
    ext_modules=[
        Extension("myext", ["src/module/*.cpp"],
                  include_dirs=["include"],
                  extra_compile_args=["-std=c++20"]),
    ],
    libraries=[
        ("mylib", {"sources": ["src/lib/*.cpp"],
                   "include_dirs": ["include"],
                   "macros": [("MYLIB_BUILD", "1")],
                   "cflags": ["-std=c++20"]}),
    ],
    cmdclass={
        "build_ext": ClangBuildExt,
        "build_clib": ClangBuildClib,
    },
)

Note that clang++ links against libc++ rather than libstdc++. When the toolchain comes from karellen-llvm-clang, libc++.so.1 lives in the package's library directory and is recorded as a plain DT_NEEDED, so the resulting extension needs that directory on the loader path (LD_LIBRARY_PATH, an ld.so.conf entry, or an explicit -Wl,-rpath in extra_link_args) at import time.

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:

  1. Compiles with --save-temps=obj -fno-discard-value-names to produce .bc (bitcode) files alongside object files
  2. Includes .bc files in static libraries created via build_clib
  3. After linking, extracts .bc files from all linked objects and static libraries and embeds them into the output binary as .drakon.<name> ELF sections (marked noload,readonly)

Enable via environment variable or setup.cfg:

DRAKON=1 python -m build
# setup.cfg
[build_ext]
drakon = 1

Thin Static Libraries

Thin static libraries store references to object files rather than copies, reducing build artifact size during development.

Enable via environment variable or setup.cfg:

THIN=1 python -m build
# setup.cfg
[build_ext]
thin = 1

Both options can be combined:

DRAKON=1 THIN=1 python -m build
# setup.cfg
[build_ext]
drakon = 1
thin = 1

The build_clib command inherits drakon and thin settings from build_ext automatically.

Setuptools Compatibility

clang-build-ext supports setuptools 68 and newer, and is tested against the versions on either side of each API change that affects it:

setuptools Change
70.1 build_meta.get_requires_for_build_wheel stops requiring wheel; earlier versions need it installed for a --no-isolation build
72.2 UnixCCompiler gains the separate C++ executables (compiler_cxx, compiler_so_cxx, linker_so_cxx, linker_exe_cxx)
75.9 new_compiler moves from distutils.ccompiler to distutils.compilers.C.base
81.0 new_compiler drops the dry_run parameter

Release files for clang-build-ext 0.0.6.dev20260914163909

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clang-build-ext 0.0.6.dev20260914163909
File Size Uploaded
clang_build_ext-0.0.6.dev20260914163909.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for clang-build-ext 0.0.6.dev20260914163909
File Interpreter ABI Platform
clang_build_ext-0.0.6.dev20260914163909-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / clang_build_ext-0.0.6.dev20260914163909.tar.gz

Download URL clang_build_ext-0.0.6.dev20260914163909.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b503b9fd7de637cb0c65fd93896a8a8f3baa225e120ab946ae7393c077e07552
BLAKE2b-256 checksum
How to use checksums
81bd34c2ef69d0f8c89a61a092c44f7455498257a375c8ef1356b4a9f0935869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / clang_build_ext-0.0.6.dev20260914163909-py3-none-any.whl

Download URL clang_build_ext-0.0.6.dev20260914163909-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33bd4fa0012fe606fb604535713b6fdb0c0fd64c75777ee484937dd2c645e40d
BLAKE2b-256 checksum
How to use checksums
d5f0bdc2bfba663a6eae72a92e502b64a5fd48c32a60ea2081176e534863766f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page