mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-05 15:55:39 +08:00
merge cmake changes from master into icurel
This commit is contained in:
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace SPC\util;
|
||||
|
||||
use SPC\builder\freebsd\library\BSDLibraryBase;
|
||||
use SPC\builder\linux\library\LinuxLibraryBase;
|
||||
use SPC\builder\macos\library\MacOSLibraryBase;
|
||||
use SPC\exception\RuntimeException;
|
||||
use ZM\Logger\ConsoleColor;
|
||||
|
||||
@@ -42,6 +45,10 @@ class UnixShell
|
||||
/* @phpstan-ignore-next-line */
|
||||
logger()->info(ConsoleColor::yellow('[EXEC] ') . ConsoleColor::green($cmd));
|
||||
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
|
||||
$env_str = $this->getEnvString();
|
||||
if (!empty($env_str)) {
|
||||
$cmd = "{$env_str} {$cmd}";
|
||||
}
|
||||
if ($this->cd !== null) {
|
||||
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
|
||||
}
|
||||
@@ -52,6 +59,37 @@ class UnixShell
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init the environment variable that common build will be used.
|
||||
*
|
||||
* @param BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library Library class
|
||||
*/
|
||||
public function initializeEnv(BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library): UnixShell
|
||||
{
|
||||
$this->setEnv([
|
||||
'CFLAGS' => $library->getLibExtraCFlags(),
|
||||
'LDFLAGS' => $library->getLibExtraLdFlags(),
|
||||
'LIBS' => $library->getLibExtraLibs(),
|
||||
'CXXFLAGS' => $library->getLibExtraCXXFlags(),
|
||||
]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function appendEnv(array $env): UnixShell
|
||||
{
|
||||
foreach ($env as $k => $v) {
|
||||
if ($v === '') {
|
||||
continue;
|
||||
}
|
||||
if (!isset($this->env[$k])) {
|
||||
$this->env[$k] = $v;
|
||||
} else {
|
||||
$this->env[$k] = "{$v} {$this->env[$k]}";
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execWithResult(string $cmd, bool $with_log = true): array
|
||||
{
|
||||
if ($with_log) {
|
||||
@@ -80,14 +118,6 @@ class UnixShell
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function execWithEnv(string $cmd): UnixShell
|
||||
{
|
||||
return $this->exec($this->getEnvString() . ' ' . $cmd);
|
||||
}
|
||||
|
||||
private function getEnvString(): string
|
||||
{
|
||||
$str = '';
|
||||
|
||||
20
src/SPC/util/executor/Executor.php
Normal file
20
src/SPC/util/executor/Executor.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SPC\util\executor;
|
||||
|
||||
use SPC\builder\freebsd\library\BSDLibraryBase;
|
||||
use SPC\builder\LibraryBase;
|
||||
use SPC\builder\linux\library\LinuxLibraryBase;
|
||||
use SPC\builder\macos\library\MacOSLibraryBase;
|
||||
|
||||
abstract class Executor
|
||||
{
|
||||
public function __construct(protected BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library) {}
|
||||
|
||||
public static function create(LibraryBase $library): static
|
||||
{
|
||||
return new static($library);
|
||||
}
|
||||
}
|
||||
208
src/SPC/util/executor/UnixCMakeExecutor.php
Normal file
208
src/SPC/util/executor/UnixCMakeExecutor.php
Normal file
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SPC\util\executor;
|
||||
|
||||
use Closure;
|
||||
use SPC\exception\FileSystemException;
|
||||
use SPC\exception\WrongUsageException;
|
||||
use SPC\store\FileSystem;
|
||||
|
||||
/**
|
||||
* Unix-like OS cmake command executor.
|
||||
*/
|
||||
class UnixCMakeExecutor extends Executor
|
||||
{
|
||||
protected ?string $build_dir = null;
|
||||
|
||||
protected array $configure_args = [];
|
||||
|
||||
protected ?array $custom_default_args = null;
|
||||
|
||||
protected int $steps = 3;
|
||||
|
||||
protected bool $reset = true;
|
||||
|
||||
public function build(string $build_pos = '..'): void
|
||||
{
|
||||
// set cmake dir
|
||||
$this->initBuildDir();
|
||||
|
||||
if ($this->reset) {
|
||||
FileSystem::resetDir($this->build_dir);
|
||||
}
|
||||
|
||||
// prepare shell
|
||||
$shell = shell()->cd($this->build_dir)->initializeEnv($this->library);
|
||||
|
||||
// config
|
||||
$this->steps >= 1 && $shell->exec("cmake {$this->getConfigureArgs()} {$this->getDefaultCMakeArgs()} {$build_pos}");
|
||||
|
||||
// make
|
||||
$this->steps >= 2 && $shell->exec("cmake --build . -j {$this->library->getBuilder()->concurrency}");
|
||||
|
||||
// install
|
||||
$this->steps >= 3 && $shell->exec('make install');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add optional library configuration.
|
||||
* This method checks if a library is available and adds the corresponding arguments to the CMake configuration.
|
||||
*
|
||||
* @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
|
||||
* @return $this
|
||||
*/
|
||||
public function optionalLib(string $name, \Closure|string $true_args, string $false_args = ''): static
|
||||
{
|
||||
if ($get = $this->library->getBuilder()->getLib($name)) {
|
||||
$args = $true_args instanceof \Closure ? $true_args($get) : $true_args;
|
||||
} else {
|
||||
$args = $false_args;
|
||||
}
|
||||
$this->addConfigureArgs($args);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configure args.
|
||||
*/
|
||||
public function addConfigureArgs(...$args): static
|
||||
{
|
||||
$this->configure_args = [...$this->configure_args, ...$args];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* To build steps.
|
||||
*
|
||||
* @param int $step Step number, accept 1-3
|
||||
* @return $this
|
||||
*/
|
||||
public function toStep(int $step): static
|
||||
{
|
||||
$this->steps = $step;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set custom CMake build directory.
|
||||
*
|
||||
* @param string $dir custom CMake build directory
|
||||
*/
|
||||
public function setBuildDir(string $dir): static
|
||||
{
|
||||
$this->build_dir = $dir;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the custom default args.
|
||||
*/
|
||||
public function setCustomDefaultArgs(...$args): static
|
||||
{
|
||||
$this->custom_default_args = $args;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the reset status.
|
||||
* If we set it to false, it will not clean and create the specified cmake working directory.
|
||||
*/
|
||||
public function setReset(bool $reset): static
|
||||
{
|
||||
$this->reset = $reset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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',
|
||||
'-DCMAKE_INSTALL_INCLUDEDIR=include',
|
||||
'-DBUILD_SHARED_LIBS=OFF',
|
||||
"-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.
|
||||
*/
|
||||
private function initBuildDir(): void
|
||||
{
|
||||
if ($this->build_dir === null) {
|
||||
$this->build_dir = "{$this->library->getSourceDir()}/build";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate cmake toolchain file for current spc instance, and return the file path.
|
||||
*
|
||||
* @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;
|
||||
// Whoops, linux may need CMAKE_AR sometimes
|
||||
if (PHP_OS_FAMILY === 'Linux') {
|
||||
$toolchain .= "\nSET(CMAKE_AR \"ar\")";
|
||||
}
|
||||
FileSystem::writeFile(SOURCE_PATH . '/toolchain.cmake', $toolchain);
|
||||
return $created = realpath(SOURCE_PATH . '/toolchain.cmake');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user