2025-06-09 00:16:18 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2025-06-09 09:24:31 +08:00
|
|
|
namespace SPC\util\executor;
|
2025-06-09 00:16:18 +08:00
|
|
|
|
2025-06-09 01:48:48 +08:00
|
|
|
use Closure;
|
2025-06-09 00:16:18 +08:00
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
use SPC\exception\WrongUsageException;
|
|
|
|
|
use SPC\store\FileSystem;
|
|
|
|
|
|
2025-06-09 01:48:48 +08:00
|
|
|
/**
|
|
|
|
|
* Unix-like OS cmake command executor.
|
|
|
|
|
*/
|
2025-06-09 00:16:18 +08:00
|
|
|
class UnixCMakeExecutor extends Executor
|
|
|
|
|
{
|
2025-06-09 09:26:39 +08:00
|
|
|
protected ?string $build_dir = null;
|
2025-06-09 00:16:18 +08:00
|
|
|
|
|
|
|
|
protected array $configure_args = [];
|
|
|
|
|
|
|
|
|
|
protected ?array $custom_default_args = null;
|
|
|
|
|
|
2025-06-09 01:07:30 +08:00
|
|
|
protected int $steps = 3;
|
|
|
|
|
|
2025-06-09 01:33:21 +08:00
|
|
|
protected bool $reset = true;
|
|
|
|
|
|
2025-06-09 01:07:30 +08:00
|
|
|
public function build(string $build_pos = '..'): void
|
2025-06-09 00:16:18 +08:00
|
|
|
{
|
|
|
|
|
// set cmake dir
|
2025-06-09 09:26:39 +08:00
|
|
|
$this->initBuildDir();
|
2025-06-09 01:33:21 +08:00
|
|
|
|
|
|
|
|
if ($this->reset) {
|
2025-06-09 09:26:39 +08:00
|
|
|
FileSystem::resetDir($this->build_dir);
|
2025-06-09 01:33:21 +08:00
|
|
|
}
|
2025-06-09 00:16:18 +08:00
|
|
|
|
|
|
|
|
// prepare shell
|
2025-06-09 11:12:34 +08:00
|
|
|
$shell = shell()->cd($this->build_dir)->initializeEnv($this->library);
|
2025-06-09 00:16:18 +08:00
|
|
|
|
|
|
|
|
// config
|
2025-06-09 10:24:06 +08:00
|
|
|
$this->steps >= 1 && $shell->exec("cmake {$this->getConfigureArgs()} {$this->getDefaultCMakeArgs()} {$build_pos}");
|
2025-06-09 00:16:18 +08:00
|
|
|
|
|
|
|
|
// make
|
2025-06-09 10:24:06 +08:00
|
|
|
$this->steps >= 2 && $shell->exec("cmake --build . -j {$this->library->getBuilder()->concurrency}");
|
2025-06-09 00:16:18 +08:00
|
|
|
|
|
|
|
|
// install
|
2025-06-09 10:24:06 +08:00
|
|
|
$this->steps >= 3 && $shell->exec('make install');
|
2025-06-09 00:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add optional library configuration.
|
|
|
|
|
* This method checks if a library is available and adds the corresponding arguments to the CMake configuration.
|
|
|
|
|
*
|
2025-06-09 00:34:25 +08:00
|
|
|
* @param string $name library name to check
|
|
|
|
|
* @param \Closure|string $true_args arguments to use if the library is available (allow closure, returns string)
|
|
|
|
|
* @param string $false_args arguments to use if the library is not available
|
2025-06-09 00:16:18 +08:00
|
|
|
* @return $this
|
|
|
|
|
*/
|
2025-06-09 00:34:25 +08:00
|
|
|
public function optionalLib(string $name, \Closure|string $true_args, string $false_args = ''): static
|
2025-06-09 00:16:18 +08:00
|
|
|
{
|
2025-06-09 00:34:25 +08:00
|
|
|
if ($get = $this->library->getBuilder()->getLib($name)) {
|
|
|
|
|
$args = $true_args instanceof \Closure ? $true_args($get) : $true_args;
|
|
|
|
|
} else {
|
|
|
|
|
$args = $false_args;
|
|
|
|
|
}
|
|
|
|
|
$this->addConfigureArgs($args);
|
2025-06-09 00:16:18 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add configure args.
|
|
|
|
|
*/
|
|
|
|
|
public function addConfigureArgs(...$args): static
|
|
|
|
|
{
|
2025-06-09 01:15:48 +08:00
|
|
|
$this->configure_args = [...$this->configure_args, ...$args];
|
2025-06-09 00:16:18 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 01:48:48 +08:00
|
|
|
/**
|
|
|
|
|
* To build steps.
|
|
|
|
|
*
|
|
|
|
|
* @param int $step Step number, accept 1-3
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2025-06-09 01:07:30 +08:00
|
|
|
public function toStep(int $step): static
|
|
|
|
|
{
|
|
|
|
|
$this->steps = $step;
|
2025-06-09 01:08:24 +08:00
|
|
|
return $this;
|
2025-06-09 01:07:30 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-09 00:16:18 +08:00
|
|
|
/**
|
|
|
|
|
* Set custom CMake build directory.
|
|
|
|
|
*
|
|
|
|
|
* @param string $dir custom CMake build directory
|
|
|
|
|
*/
|
2025-06-09 09:26:39 +08:00
|
|
|
public function setBuildDir(string $dir): static
|
2025-06-09 00:16:18 +08:00
|
|
|
{
|
2025-06-09 09:26:39 +08:00
|
|
|
$this->build_dir = $dir;
|
2025-06-09 00:16:18 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the custom default args.
|
|
|
|
|
*/
|
|
|
|
|
public function setCustomDefaultArgs(...$args): static
|
|
|
|
|
{
|
|
|
|
|
$this->custom_default_args = $args;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 01:48:48 +08:00
|
|
|
/**
|
|
|
|
|
* Set the reset status.
|
|
|
|
|
* If we set it to false, it will not clean and create the specified cmake working directory.
|
|
|
|
|
*/
|
2025-06-09 01:33:21 +08:00
|
|
|
public function setReset(bool $reset): static
|
|
|
|
|
{
|
|
|
|
|
$this->reset = $reset;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 00:16:18 +08:00
|
|
|
/**
|
|
|
|
|
* Get configure argument line.
|
|
|
|
|
*/
|
|
|
|
|
private function getConfigureArgs(): string
|
|
|
|
|
{
|
|
|
|
|
return implode(' ', $this->configure_args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
private function getDefaultCMakeArgs(): string
|
|
|
|
|
{
|
|
|
|
|
return implode(' ', $this->custom_default_args ?? [
|
|
|
|
|
'-DCMAKE_BUILD_TYPE=Release',
|
|
|
|
|
"-DCMAKE_INSTALL_PREFIX={$this->library->getBuildRootPath()}",
|
|
|
|
|
'-DCMAKE_INSTALL_BINDIR=bin',
|
|
|
|
|
'-DCMAKE_INSTALL_LIBDIR=lib',
|
2025-06-09 12:49:33 +08:00
|
|
|
'-DCMAKE_INSTALL_INCLUDEDIR=include',
|
2025-06-09 01:07:30 +08:00
|
|
|
'-DBUILD_SHARED_LIBS=OFF',
|
2025-06-09 00:16:18 +08:00
|
|
|
"-DCMAKE_TOOLCHAIN_FILE={$this->makeCmakeToolchainFile()}",
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the CMake build directory.
|
|
|
|
|
* If the directory is not set, it defaults to the library's source directory with '/build' appended.
|
|
|
|
|
*/
|
2025-06-09 09:26:39 +08:00
|
|
|
private function initBuildDir(): void
|
2025-06-09 00:16:18 +08:00
|
|
|
{
|
2025-06-09 09:26:39 +08:00
|
|
|
if ($this->build_dir === null) {
|
|
|
|
|
$this->build_dir = "{$this->library->getSourceDir()}/build";
|
2025-06-09 00:16:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-09 01:48:48 +08:00
|
|
|
* Generate cmake toolchain file for current spc instance, and return the file path.
|
|
|
|
|
*
|
2025-06-09 00:16:18 +08:00
|
|
|
* @return string CMake toolchain file path
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
*/
|
|
|
|
|
private function makeCmakeToolchainFile(): string
|
|
|
|
|
{
|
|
|
|
|
static $created;
|
|
|
|
|
if (isset($created)) {
|
|
|
|
|
return $created;
|
|
|
|
|
}
|
|
|
|
|
$os = PHP_OS_FAMILY;
|
|
|
|
|
$target_arch = arch2gnu(php_uname('m'));
|
|
|
|
|
$cflags = getenv('SPC_DEFAULT_C_FLAGS');
|
|
|
|
|
$cc = getenv('CC');
|
|
|
|
|
$cxx = getenv('CCX');
|
|
|
|
|
logger()->debug("making cmake tool chain file for {$os} {$target_arch} with CFLAGS='{$cflags}'");
|
|
|
|
|
$root = BUILD_ROOT_PATH;
|
|
|
|
|
$ccLine = '';
|
|
|
|
|
if ($cc) {
|
|
|
|
|
$ccLine = 'SET(CMAKE_C_COMPILER ' . $cc . ')';
|
|
|
|
|
}
|
|
|
|
|
$cxxLine = '';
|
|
|
|
|
if ($cxx) {
|
|
|
|
|
$cxxLine = 'SET(CMAKE_CXX_COMPILER ' . $cxx . ')';
|
|
|
|
|
}
|
|
|
|
|
$toolchain = <<<CMAKE
|
|
|
|
|
{$ccLine}
|
|
|
|
|
{$cxxLine}
|
|
|
|
|
SET(CMAKE_C_FLAGS "{$cflags}")
|
|
|
|
|
SET(CMAKE_CXX_FLAGS "{$cflags}")
|
|
|
|
|
SET(CMAKE_FIND_ROOT_PATH "{$root}")
|
|
|
|
|
SET(CMAKE_PREFIX_PATH "{$root}")
|
|
|
|
|
SET(CMAKE_INSTALL_PREFIX "{$root}")
|
|
|
|
|
SET(CMAKE_INSTALL_LIBDIR "lib")
|
|
|
|
|
|
|
|
|
|
set(PKG_CONFIG_EXECUTABLE "{$root}/bin/pkg-config")
|
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
|
|
|
|
set(CMAKE_EXE_LINKER_FLAGS "-ldl -lpthread -lm -lutil")
|
|
|
|
|
CMAKE;
|
2025-06-09 01:52:23 +08:00
|
|
|
// Whoops, linux may need CMAKE_AR sometimes
|
2025-06-09 00:16:18 +08:00
|
|
|
if (PHP_OS_FAMILY === 'Linux') {
|
|
|
|
|
$toolchain .= "\nSET(CMAKE_AR \"ar\")";
|
|
|
|
|
}
|
|
|
|
|
FileSystem::writeFile(SOURCE_PATH . '/toolchain.cmake', $toolchain);
|
|
|
|
|
return $created = realpath(SOURCE_PATH . '/toolchain.cmake');
|
|
|
|
|
}
|
|
|
|
|
}
|