Skip to main content

C++ multi-file merge & code golf / minifier tool

Project description

CPPGolf

cppgolf is a C/C++ multi-file merge and code-golf/minifier tool.

Install

pip install cppgolf

libclang-backed features such as symbol renaming, type renaming, and static deduplication are optional at runtime. The base CLI and text transforms work without clang.cindex.

CLI

cppgolf solution.cpp
cppgolf solution.cpp -o golf.cpp
cppgolf solution.cpp --merge-only -I include/
cppgolf solution.cpp -DDEBUG=1 --inject-define --merge-only
cppgolf solution.cpp -I include/ --rename-functions --stats
cppgolf solution.cpp -DBOTZONE_PIKAFISH_STANDALONE=1 -DZSTD_DISABLE_ASM
cppgolf solution.cpp --no-rename

Common options

Option Meaning
-o FILE Write output to a file instead of stdout.
-I DIR Add an include search directory. Can be repeated.
-D MACRO[=VALUE] Pass a macro definition to libclang-backed optional passes. Can be repeated.
--inject-define Write -D macro definitions into the generated source code.
--merge-only Only merge files. Skip all later transforms and compression passes.
--no-merge Skip inlining #include "...".
--no-strip-comments Keep comments.
--no-compress-ws Keep whitespace formatting.
--no-std-ns Do not add using namespace std;.
--no-typedefs Do not add shortcuts such as ll / ld.
--no-rename Disable symbol renaming.
--no-win-lean Do not inject Windows header conflict guards.
--keep-main-return Keep the trailing return 0; in main.
--keep-endl Keep endl.
--keep-inline Keep inline.
--dedup-statics Deduplicate merged static definitions with libclang.
--aggressive Remove braces from single-statement if / for / while.
--shortcuts Insert #define shortcuts for frequent cout / cin.
--rename-functions Also rename user-defined functions and methods.
--rename-type Add typedef aliases for long user-defined type names.
--stats Print size reduction statistics to stderr.

Python API

from pathlib import Path

from cppgolf import process

result, merged_size = process(
    Path("solution.cpp"),
    include_dirs=[],
    defines=["BOTZONE_PIKAFISH_STANDALONE=1"],
    inject_defines=False,
    merge_only=False,
    rename_symbols=False,
)

print(merged_size)
print(result)

Individual passes can also be used directly:

from cppgolf import compress_whitespace, strip_comments
from cppgolf.transforms import golf_typedefs

code = Path("a.cpp").read_text()
code = strip_comments(code)
code = golf_typedefs(code)
code = compress_whitespace(code)

Features

  • Merge local #include "..." files recursively and deduplicate top-level system headers.
  • Strip comments while preserving strings, character literals, and raw strings.
  • Apply safe default text transforms such as std:: removal, typedef shortcuts, endl replacement, inline removal, and trailing return 0; removal in main.
  • Compress whitespace at token granularity while preserving preprocessor line structure.
  • Optionally rename symbols and user-defined types with libclang.

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

cppgolf-0.1.9.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

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

cppgolf-0.1.9-py3-none-any.whl (41.9 kB view details)

Uploaded Python 3

File details

Details for the file cppgolf-0.1.9.tar.gz.

File metadata

  • Download URL: cppgolf-0.1.9.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for cppgolf-0.1.9.tar.gz
Algorithm Hash digest
SHA256 1e331fd552a5738676ddb20f8548be73e54e756f18043abea1a8ca6f7a948235
MD5 2c01c4e0b2f09717625cbe0a55bd46a3
BLAKE2b-256 4e53708eedeee5ab0dd19401a915587a18243bdf13bf85fc47d3a7c9caa26b0b

See more details on using hashes here.

File details

Details for the file cppgolf-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: cppgolf-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 41.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for cppgolf-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5ba9516c5d86f1f6163e1987000b6d1af2842f6f145e261683991062baa3170c
MD5 5e5541fcf61704a5d27d46c331ff5761
BLAKE2b-256 3ddd27ed5df0c721e9170896f7a2f99b766745bf350071802453c6519fd2093d

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