Metadata-Version: 2.4
Name: clap-compiler
Version: 0.38.0
Summary: Zero-setup C/C++ compiler frontend for Windows, education, and automation
Author: CLAP contributors
License-Expression: MIT
Project-URL: Homepage, https://github.com/BRATAN-CORP/clap
Project-URL: Repository, https://github.com/BRATAN-CORP/clap
Project-URL: Documentation, https://github.com/BRATAN-CORP/clap#readme
Project-URL: Issues, https://github.com/BRATAN-CORP/clap/issues
Project-URL: Changelog, https://github.com/BRATAN-CORP/clap/releases
Keywords: c,cpp,compiler,windows,zig,education,ai-agents
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Compilers
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: tomli>=2.0; python_version < "3.11"
Requires-Dist: cmake==4.4.5
Requires-Dist: ninja==1.13.2
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: ruff>=0.8; extra == "dev"
Requires-Dist: jsonschema>=4.23; extra == "dev"
Dynamic: license-file

# 👏 CLAP

**C Like A Python — simple as clap.**

English | [Русский](https://github.com/BRATAN-CORP/clap/blob/main/README.ru.md)

[![CI](https://github.com/BRATAN-CORP/clap/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/BRATAN-CORP/clap/actions/workflows/test.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/BRATAN-CORP/clap/blob/main/LICENSE)
[![Python](https://img.shields.io/badge/python-3.9%2B-blue.svg)](https://www.python.org/)
[![Platforms](https://img.shields.io/badge/platforms-Windows%20%C2%B7%20Linux%20%C2%B7%20macOS-lightgrey.svg)](#platform-support)
[![PyPI](https://img.shields.io/pypi/v/clap-compiler.svg)](https://pypi.org/project/clap-compiler/)

CLAP is a zero-setup C/C++ compiler frontend for people who just want the thing to build.

No afternoon lost to MinGW mirrors. No “please install Visual Studio Build Tools and restart your machine”. No lecture about `PATH`. Install the Python package, run `clap build`, and get a binary.

CLAP does **not** try to turn C into Python. It only makes compiling C feel almost as uneventful as running Python. The mascot logic is equally simple: 👏 for clap, 🐍 for the Python-like lack of ceremony. Under the hood it uses a pinned, checksum-verified Zig/Clang toolchain; Python is the installer, dispatcher, and boring glue.

![CLAP terminal demo](https://raw.githubusercontent.com/BRATAN-CORP/clap/main/docs/assets/terminal-demo.svg)

## What it does

- compiles C as C and C++ as C++ — a small detail that many “simple” tools still manage to break;
- handles mixed C/C++ projects by compiling each file in the right mode and linking with the C++ driver;
- bootstraps a private toolchain on Windows, Linux, and macOS;
- can cross-compile Windows executables from Linux or macOS;
- builds ordinary CMake projects with pinned `cmake` and `ninja` Python packages;
- prints human output by default and structured JSON for editors, scripts, and agents;
- falls back to a system compiler only when you ask for it or when the private toolchain is unavailable.

## Install

### From PyPI

With [pipx](https://pipx.pypa.io/) — recommended for a CLI tool:

```bash
pipx install clap-compiler
```

Or with plain pip:

```bash
python -m pip install clap-compiler
# `python -m clap_compiler` also works after installation
```

The GitHub repository can still be installed directly with `pip install git+https://github.com/BRATAN-CORP/clap.git` when testing unreleased changes.

Python 3.9+ is required. The first private toolchain bootstrap needs internet access; after that `--offline` builds work.

### Package homes

PyPI is the canonical package. The other links are packaging targets or drafts until their maintainers publish them.

| Package manager | Package | Status |
| --- | --- | --- |
| PyPI | [`clap-compiler`](https://pypi.org/project/clap-compiler/) | published: `python -m pip install clap-compiler` |
| npm | [`clap-compiler`](https://www.npmjs.com/package/clap-compiler) | wrapper placeholder |
| Debian/Ubuntu | [`clap-compiler`](https://packages.debian.org/search?keywords=clap-compiler) | `.deb` placeholder |
| Snap Store | [`clap-compiler`](https://snapcraft.io/clap-compiler) | placeholder |
| AUR | [`clap-compiler`](https://aur.archlinux.org/packages/clap-compiler) | placeholder |
| Nix/NixOS | [`clap-compiler`](https://search.nixos.org/packages?query=clap-compiler) | placeholder |
| GitHub Releases | [releases](https://github.com/BRATAN-CORP/clap/releases) | release artifacts land here |

## Quick start

```bash
clap init hello
cd hello
clap run

# other starting points:
# clap init app --template cpp
# clap init app --template mixed
# clap init app --template cmake
```

Or compile a file directly:

```bash
clap run main.c
clap run main.cpp
clap build main.c -o hello
clap build main.c util.c -j 8  # compile objects in parallel
clap run main.c --sanitize address  # debug memory errors
clap build main.c --coverage       # write compiler coverage instrumentation
clap run main.c --coverage-report  # write .gcov files plus text/JSON summaries
clap build main.c --timing         # print toolchain/compile/total timings
clap build main.c --check  # syntax-check without linking
clap build main.c --warnings --warnings-as-errors  # strict warning hygiene
clap build main.c --verbose  # show exact compiler and linker commands
clap build main.c --dry-run  # inspect the build plan without artifacts
clap run main.c -- arguments for the program
```

When no files are passed and no `clap.toml` exists, CLAP conservatively autodiscovers a simple layout: a unique `main.c`/`main.cpp`-style entrypoint plus its sibling source files in the project root or `src/`, or exactly one source file in either location. Ambiguous or recursive multi-directory projects still need explicit files or `clap.toml`; autodiscovery is a zero-config convenience, not a replacement for project configuration.

Useful commands:

```bash
clap doctor       # show the active toolchain and cache
clap doctor --probe  # compile tiny C and C++ health checks
clap bootstrap    # download and verify the private toolchain now
clap bootstrap --warmup  # also precompile the C/C++ runtime cache
clap toolchain list    # show cached toolchains and sizes
clap toolchain info    # show pinned/active toolchain details
clap toolchain prune   # remove stale or incomplete cached toolchains
clap toolchain delete --yes  # remove the current cached toolchain
clap inspect .      # stable JSON project/toolchain metadata for editors and agents
clap vscode         # write VS Code tasks and IntelliSense configuration
clap lock           # record pkg-config dependency versions and flags
clap deps           # check dependency lock, paths, and vcpkg status
clap build        # build without running
clap cmake .      # configure and build a CMake project
clap cmake . -j 8  # parallel CMake build
clap cmake . --release  # CMake Release configuration
clap cmake . --install --prefix out  # build and install into ./out
clap clean --cache  # remove build outputs and cached toolchains
```


## Examples

Small runnable examples live in [`examples/`](https://github.com/BRATAN-CORP/clap/tree/main/examples):

```bash
clap run examples/hello/main.c
clap run examples/mixed/answer.c examples/mixed/main.cpp
clap cmake examples/cmake
clap test examples/tests/test_arithmetic.c examples/tests/test_boolean.c

(cd examples/zero-config && clap run)       # autodiscover main.c + util.c
(cd examples/library && clap build)          # build libanswer.a / answer.lib
```

The mixed example is the important one: `answer.c` is compiled as C, `main.cpp` as C++, and the final link uses the C++ driver.

Compiler flags go in `--flag`; linker-only flags go in `--link-flag` (use `--link-flag=-lm` for values starting with a dash). Portable `-I`/`--include`, `-D`/`--define`, `--library`, `--library-path`, and pkg-config `--package` options are translated into GCC/Clang or MSVC syntax. In Ninja and mixed-language builds linker-only flags are kept out of per-object compile commands and are added only when linking.

### Build profiles

`--profile` selects a small built-in preset without replacing normal project configuration:

```bash
clap build main.c --profile debug
clap build main.c --profile release
clap build main.c --profile windows-x86_64
clap build main.c --profile linux-x86_64
clap build main.c --profile macos-x86_64
clap build main.c --profile macos-arm64
```

`debug` and `release` map to the same portable optimization/debug flags as `--debug` and `--release`. Cross profiles select Zig/Clang target triples (`windows-x86_64` uses `x86_64-windows-gnu`, `linux-x86_64` uses `x86_64-linux-gnu`, and the macOS profiles use the matching macOS triple). Explicit `--target`, `--release`, `--debug`, or `--output` still wins. Profile builds use separate default output names such as `main-windows-x86_64.exe` so a cross build does not overwrite the host binary.



### Objects and libraries

Executables are the default, but direct builds can also emit the artifacts a compiler frontend is expected to produce:

```bash
clap build util.c --artifact object   # util.o / util.obj
clap build util.c --artifact static   # libutil.a / util.lib
clap build util.c --artifact shared   # libutil.so / util.dll / libutil.dylib
clap build util.c --artifact asm      # util.s assembly (Zig/GCC/Clang backends)
clap build util.c --artifact preprocess  # util.i preprocessed source
```

With several sources, `--artifact object` treats `-o` as a directory and writes one object per source. Static archives use `zig ar`, MSVC `lib.exe`, or the system `ar` depending on the active compiler. These modes compose with `--ninja`, so repeated builds stay incremental. Assembly artifacts are portable too. MSVC emits documented assembler listings with `/FA` and `/Fa`; because MSVC cannot suppress the companion object file, CLAP writes that implementation detail to a hidden `.clap-assembly-objects` directory. Preprocessing uses `/P` and `/Fi` on MSVC and `-E` on Zig/GCC/Clang.

## Windows, minus the setup ritual

On a Windows machine with only Python installed:

```powershell
python -m pip install clap-compiler
clap bootstrap
clap run main.c
```

CLAP downloads a pinned Zig archive, checks its SHA-256 digest and exact size, extracts it into the user cache, and invokes it without touching global system state. Concurrent first-run bootstraps share a cache lock instead of downloading or replacing the same toolchain twice. `clap bootstrap --warmup` additionally compiles minimal C and C++ programs so the first real build does not pay the one-time compiler runtime cache cost.

A Windows executable can also be produced from Linux or macOS:

```bash
clap build main.c --target x86_64-windows-gnu
clap cmake . --target x86_64-windows-gnu  # CMake projects can cross too
```

Windows outputs automatically receive `.exe` when the target is Windows. CMake cross-builds use the pinned Zig/Clang-style target triple, set the matching CMake system name and processor, and avoid running try-compile executables on the host.

For project paths longer than the classic Windows `MAX_PATH`, enable Win32 long-path support and invoke CLAP from a shorter working directory with explicit deep input/output paths, for example `clap build C:\deep\project\main.c -o C:\deep\project\main.exe`. CLAP keeps Ninja compiler child processes in a valid short working directory and can execute deep output binaries. Windows itself does not allow a new process to inherit a current directory longer than `MAX_PATH`, so `cd C:\deep\project` followed by `clap ...` remains an operating-system limitation rather than something a compiler frontend can bypass.

## Testing

For a CMake project, `clap test` builds the project and runs CTest. For a direct source file or a `clap.toml` executable project, it builds the executable and runs it, using the program exit code as the test result. Arguments after `--` go to the test executable:

```bash
clap test . -- --verbose
clap test test_math.c test_string.c  # independent test executables
clap test . --junit-xml build/test-results.xml
```

When multiple source files are passed to `clap test`, CLAP builds each file as its own executable in `.clap-tests/`, runs them sequentially, and aggregates pass/fail results. This is intended for independent test binaries; shared production code should be linked through a project configuration or represented in CMake.

`--object-cache` (or `object_cache = true`) enables an opt-in content-addressed object cache for direct GCC/Clang/Zig Ninja builds. Cache entries are keyed by compiler identity, target, language mode, flags, source content, and the content of compiler-reported dependencies; changing a header or system header prevents stale reuse. Relative project dependencies can hit across moved clean checkouts. The cache is capped at 512 MiB by default (`CLAP_OBJECT_CACHE_MAX_BYTES` overrides that), corrupted entries are ignored, and `clap clean --cache` removes it together with private toolchains. MSVC, dry-run/check, assembly, preprocessing, and CMake builds are explicitly outside the initial scope.

Local maintainer measurements for cold, no-op, incremental, and coverage-report builds live in [docs/PERFORMANCE.md](docs/PERFORMANCE.md) and can be refreshed with `python scripts/benchmark.py --format markdown --output docs/PERFORMANCE.md`.

`--junit-xml` writes a JUnit XML report for CI systems. Direct tests include the executable runtime; CMake tests use CTest’s native per-test JUnit output when available, with a simple fallback report if CTest cannot write the file. `--coverage` enables compiler coverage instrumentation for direct GCC/Clang builds. `--coverage-report [DIR]` implies it for direct Ninja builds, runs `gcov` (or `llvm-cov gcov` for Clang), and writes `.gcov` files, `summary.txt`, and `coverage-summary.json`; CMake coverage reporting is not implemented yet. `--timing` prints toolchain, compile, run (when applicable), and total phase timings; JSON output exposes the same values under `timing`.

## CMake projects

If a directory has `CMakeLists.txt`, CLAP can treat it as a project:

```bash
clap build .
clap build . --release
clap cmake . --cmake-target all --cmake-arg -DENABLE_TESTS=OFF
clap test . --debug
clap run .        # build and run a single executable target
clap test .       # build, then run CTest
clap test . -j 8 --ctest-arg=-R --ctest-arg=mytest
clap clean        # remove CLAP build directories
```

The CMake path uses the same compiler selection logic as direct compilation. If a build directory was configured with another compiler or generator, CLAP resets CMake’s cache instead of silently reusing the wrong toolchain. `--release` and `--debug` map to the normal CMake `Release` and `Debug` build types; an exact `--cmake-arg -DCMAKE_BUILD_TYPE=...` can still override them when a project needs another configuration. `cmake` and `ninja` come from pinned Python package dependencies, so CLAP does not download a random “latest” archive in the background.

## Project configuration

`clap.toml` keeps small projects small. Source entries can be exact files or deterministic glob patterns such as `src/**/*.c`:

```toml
[project]
name = "hello"
sources = ["src/**/*.c", "src/util.c"]  # glob patterns are allowed
# exclude_sources = ["src/generated/**", "tests/fixtures/**"]
output = "hello"
flags = ["-Wall"]
includes = ["include"]
defines = ["CONFIG_RELEASE=1"]
libraries = ["m"]
library_paths = ["lib"]
packages = ["zlib"]  # resolved through pkg-config
# vcpkg_root = "vendor/vcpkg"
# vcpkg_triplet = "x64-linux"
link_flags = []
std = "c11"
# artifact = "static"  # exe, object, static, shared, asm, preprocess
# sanitizers = ["address"]
# warnings = true
# warnings_as_errors = false
# compiler = "gcc"   # CLI --compiler overrides this
# ninja = false      # disable automatic incremental Ninja builds
# object_cache = true # reuse compatible direct-build objects across clean builds
# jobs = 4           # parallel compile jobs
# compile_commands = true  # always write editor metadata
# sbom = false             # write SPDX SBOMs for direct builds
# release = true           # optimized project builds by default
# debug = false            # debug info project builds by default
# or separate mixed C/C++ projects:
# c_std = "c11"
# cpp_std = "c++20"
```

Then:

```bash
clap build
clap run
```

Explicit files on the command line still win over config defaults. That is intentional: automation should be able to override a project without editing it.

vcpkg can be enabled with `vcpkg_root`/`vcpkg_triplet` in `clap.toml`, `--vcpkg-root`/`--vcpkg-triplet`, or `VCPKG_ROOT`. For direct builds CLAP adds `installed/<triplet>/include` to include paths and `installed/<triplet>/lib` (or `debug/lib` for debug builds) to library paths; libraries themselves are still explicit through `libraries`/`--library`. For CMake projects CLAP passes the standard `scripts/buildsystems/vcpkg.cmake` toolchain file and triplet, so `find_package` and vcpkg manifest mode work normally.

`clap lock` records resolved pkg-config versions, compile flags, and linker flags in `clap.lock.json`. `clap deps` reports the declared packages, lock status/version drift, manual include/library paths, and vcpkg triplet state in human output or schema-versioned JSON (`schema/deps-v1.json`). When that file exists, direct builds use the locked flags by default and verify that the installed `pkg-config --modversion` still matches; a mismatch stops the build until `clap lock` refreshes the file. `--no-lock` temporarily bypasses the lock. The lock does not make system libraries portable across machines — it makes their resolution explicit and verifiable.

## Machine-readable output

```bash
clap build main.c --json
clap run main.c --json
```

The JSON report includes the primary compiler command, the full `commands` list for multi-step builds, output path, toolchain name/version/source, parsed diagnostics, warnings, stdout/stderr, and the child process result for `run`. It is meant to be useful to editor integrations and coding agents, not just decorative JSON. `clap inspect` follows the versioned schema in [`schema/inspect-v1.json`](https://github.com/BRATAN-CORP/clap/blob/main/schema/inspect-v1.json); schema version 1 is additive-stable, so integrations can rely on the documented fields while ignoring future additions.

`clap vscode` writes mergeable `.vscode/tasks.json`, `c_cpp_properties.json`, and `extensions.json` files. The default **CLAP: Build** task generates `${workspaceFolder}/compile_commands.json`, which the recommended Microsoft C/C++ extension uses for IntelliSense. Existing unrelated VS Code tasks and settings are preserved; CLAP refuses to overwrite malformed existing JSON instead of destroying it.

CLAP uses Ninja automatically for CMake projects and direct builds whenever the bundled Ninja dependency is available. `--ninja` remains accepted as an explicit opt-in, while `--no-ninja` keeps the legacy immediate compile/link behavior:

```bash
clap build main.c                     # incremental by default
clap build main.c --ninja             # explicit Ninja opt-in
clap build main.c --no-ninja          # legacy direct commands
```

This writes `.clap-ninja/build.ninja`, compiles each source once, tracks header dependencies, and reuses unchanged object files on later builds. Add `--compile-commands` to write a `compile_commands.json` database beside the output for clangd and other editor tooling, or `--compile-commands-output path/to/db.json` to choose the exact destination. CMake builds export the same database into the CMake build directory automatically.

## SPDX SBOMs

CLAP can write an SPDX 2.3 software bill of materials after a successful build:

```bash
clap build main.c --sbom
clap cmake . --sbom
clap cmake . --sbom --sbom-output sbom.spdx.json
```

Direct builds record source files, output checksums, compiler identity, linker libraries, and non-standard includes. CMake builds use the CMake File API, so target names, artifacts, sources, target dependencies, and link-line libraries come from CMake's own model instead of a text scan.

One honest boundary: CLAP can report that a target links `openssl` or `z`, but it cannot magically know which upstream package supplied that library if the project does not say so. Conan/vcpkg/system package provenance still has to come from the project or package manager.

## How the toolchain works

CLAP pins Zig `0.17.1` per platform and verifies every downloaded archive before extraction. Archive entries are checked for path traversal and links are rejected. The toolchain lives in the user cache:

| Platform | Cache location |
| --- | --- |
| Windows | `%LOCALAPPDATA%\CLAP` |
| macOS | `~/Library/Caches/CLAP` |
| Linux | `${XDG_CACHE_HOME:-~/.cache}/clap` |

Set `CLAP_CACHE` to use a different location. Use `--system` to prefer an installed compiler, or `--offline` to forbid downloads.

This design is deliberately unglamorous. A compiler is not the sort of thing that should depend on whatever happened to be in `PATH` today.

## Platform support

| Platform | Native build | Windows cross-build | CMake |
| --- | ---: | ---: | ---: |
| Windows | yes | native `.exe` | yes |
| Linux | yes | yes | yes |
| macOS | yes | yes | yes |

Automatic GitHub CI runs Linux unit, packaging, and Linux-to-Windows smoke checks. Native Windows smoke remains available as a manual workflow, while maintainers use `scripts/ci-local.sh` plus real Windows/macOS validation for platform-sensitive changes.

## Why CLAP instead of CMake, Meson, or XMake?

CLAP is not trying to replace large build systems inside mature projects. Its narrow job is the first ten minutes: install one Python package, get a verified compiler toolchain, compile or run ordinary C/C++, and optionally drive an existing CMake project with pinned CMake/Ninja. Use CMake/Meson/XMake when you need their full project model; use CLAP when compiler setup itself is the barrier, especially on Windows or in agent/CI environments.

## Honest limitations

CLAP is not a full replacement for Visual Studio or the Windows SDK. Projects that require MSVC-specific extensions, MFC/ATL, proprietary SDK pieces, or exotic linker setups still need those Microsoft components installed. When they are installed, `clap --system` can discover MSVC through `vswhere` and use the correct `vcvars` environment even outside a Developer PowerShell; `--compiler cl` asks for that toolchain explicitly.

CLAP is also not a new programming language. The “Python” part is about the installation experience, not syntax.

## Development

```bash
python -m pip install -e '.[dev]'
python -m ruff check src tests
python -m pytest -q
```

The active engineering plan lives in [docs/ROADMAP.md](docs/ROADMAP.md); local performance measurements live in [docs/PERFORMANCE.md](docs/PERFORMANCE.md). Please keep changes small enough to explain in one commit message. CLAP itself should stay small too. Release notes live in the [changelog](https://github.com/BRATAN-CORP/clap/blob/main/CHANGELOG.md) and on the [releases page](https://github.com/BRATAN-CORP/clap/releases).

## License

[MIT](https://github.com/BRATAN-CORP/clap/blob/main/LICENSE). Use it, teach with it, automate with it, improve it.
