Skip to main content

Clang-based extension builder

Project description

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 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:

  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 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clang_build_ext-0.0.4.dev20260320015434.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file clang_build_ext-0.0.4.dev20260320015434.tar.gz.

File metadata

File hashes

Hashes for clang_build_ext-0.0.4.dev20260320015434.tar.gz
Algorithm Hash digest
SHA256 eedf1629eb24305a3892d016fb9c8f6fa47cfac28893626c101cc20291038b8a
MD5 f03df6aaa1247b9a07530d7cb3d68c6d
BLAKE2b-256 53dbfb9ade2028557cbdadd067f27efb9a48c58088177518c5a92ab36f877165

See more details on using hashes here.

File details

Details for the file clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl.

File metadata

File hashes

Hashes for clang_build_ext-0.0.4.dev20260320015434-py3-none-any.whl
Algorithm Hash digest
SHA256 b87c3086b60c3336b75b2b3a87dc5594305ac547edce13ca0c5300fdc97c2862
MD5 81fc3c2967eb58799f1466bad7f8a05c
BLAKE2b-256 748e278196e9ced4aedfb69644203222f131540f6620bed166bd9d9355219f7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page