adachsoft / code-operations-cpp
CMake-based C++ code operations with GCC and Clang runtime discovery.
Requires
- php: ^8.3
- adachsoft/code-operations-contracts: ^0.2
- adachsoft/command-executor-lib: ^2.4
Requires (Dev)
- adachsoft/php-code-style: ^0.9.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
CMake-based C++ implementation of the shared adachsoft/code-operations-contracts API. It discovers GCC and Clang compilers, configures and builds projects, runs CTest suites, and executes named CMake targets.
Requirements
- PHP 8.3 or later
- CMake 3.20 or later (
ctest --test-dirrequires CMake 3.20) - A supported C++ compiler available in
PATH:g++,g++-<N>,clang++, orclang++-<N> - CTest available in
PATH
Installation
Install the library and its runtime dependencies with Composer:
composer require adachsoft/code-operations-cpp
Usage
<?php
declare(strict_types=1);
use AdachSoft\CodeOperationsContracts\ExecutionContextDto;
use AdachSoft\CodeOperationsContracts\RuntimeVo;
use AdachSoft\CodeOperationsCpp\CppCodeOperations;
use AdachSoft\CodeOperationsCpp\CppRuntimeResolver;
use AdachSoft\CommandExecutorLib\SimpleCommandRunner;
$runner = new SimpleCommandRunner();
$runtimeResolver = new CppRuntimeResolver($runner);
$operations = new CppCodeOperations($runtimeResolver, $runner);
$context = new ExecutionContextDto(
'/path/to/cmake-project',
new RuntimeVo('cpp', 'gcc-13'),
);
$validation = $operations->validate($context);
$tests = $operations->test($context, 'smoke');
$application = $operations->run($context, 'app', ['--version']);
validate() configures the project in Debug mode and builds all targets. test() builds the project before running CTest; its optional filter is passed to ctest -R. run() builds the requested CMake target and executes its binary with the project directory as the working directory.
Compiler selection
When no compiler list is supplied, CppRuntimeResolver searches directories in PATH for g++, g++-<N>, clang++, and clang++-<N>. Duplicate executables are removed by canonical real path. GCC runtimes are ordered before Clang runtimes; versions within each family are ordered from newest to oldest. The default selection is the first runtime in that order.
An optional RuntimeVo version is matched as a prefix of the runtime identifier. For example, gcc, gcc-13, and clang-18.1 can select matching identifiers such as gcc-13.3.0 and clang-18.1.3. The resolver sets CXX to the selected compiler and sets CC to its matching C compiler when that executable exists.
requiredVersion() always returns null: CMake projects do not declare a required compiler version, so callers can select a compiler explicitly when needed.
Build directory and diagnostics
Build artifacts are kept outside the project by default, under:
<sys_get_temp_dir()>/code-operations-cpp/<sha1(realpath(project))>/<runtime-id>
Pass a custom buildRoot to CppCodeOperations to change the root directory.
ExecutionContextDto::$paths filters returned diagnostics to the listed project-relative files or directories and their descendants. CMake still configures and builds the complete project; the path list does not limit compilation. CMake, compiler, linker, and CTest output is combined and truncated to the configured maximum output size, retaining its end and adding a truncation marker when necessary.