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.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

clang_build_ext-0.0.4-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: clang_build_ext-0.0.4.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for clang_build_ext-0.0.4.tar.gz
Algorithm Hash digest
SHA256 32d474df2c965af4d261258c1aa49125140d6b7c276a597791319310151490ba
MD5 e1b13960d9a26a3f95ffe8f37a7fb5cf
BLAKE2b-256 faf742a70c97004fa88ec941f6e710341e418896154125505b60c1d6930c590a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clang_build_ext-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f742ff52d6134dc2954d0bf2781e68aa78a340ff8a626502d91028eac1d3e319
MD5 bda47cd01c5581312f7c94a8e04ec894
BLAKE2b-256 fe181e2e70b30b4410d39eba838eb4413d0a0ba4db6e35a8176cef18af535d2e

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