mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-04 23:35:40 +08:00
initial commit for macOS support
This commit is contained in:
68
src/SPC/builder/traits/UnixSystemUtilTrait.php
Normal file
68
src/SPC/builder/traits/UnixSystemUtilTrait.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SPC\builder\traits;
|
||||
|
||||
/**
|
||||
* Unix 系统的工具函数 Trait,适用于 Linux、macOS
|
||||
*/
|
||||
trait UnixSystemUtilTrait
|
||||
{
|
||||
/**
|
||||
* 生成 toolchain.cmake,用于 cmake 构建
|
||||
*
|
||||
* @param string $os 操作系统代号
|
||||
* @param string $target_arch 目标架构
|
||||
* @param string $cflags CFLAGS 参数
|
||||
* @param null|string $cc CC 参数(默认空)
|
||||
* @param null|string $cxx CXX 参数(默认空)
|
||||
*/
|
||||
public static function makeCmakeToolchainFile(
|
||||
string $os,
|
||||
string $target_arch,
|
||||
string $cflags,
|
||||
?string $cc = null,
|
||||
?string $cxx = null
|
||||
): string {
|
||||
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 ' . self::findCommand($cc) . ')';
|
||||
}
|
||||
$cxxLine = '';
|
||||
if ($cxx) {
|
||||
$cxxLine = 'SET(CMAKE_CXX_COMPILER ' . self::findCommand($cxx) . ')';
|
||||
}
|
||||
$toolchain = <<<CMAKE
|
||||
SET(CMAKE_SYSTEM_NAME {$os})
|
||||
SET(CMAKE_SYSTEM_PROCESSOR {$target_arch})
|
||||
{$ccLine}
|
||||
{$cxxLine}
|
||||
SET(CMAKE_C_FLAGS "{$cflags}")
|
||||
SET(CMAKE_CXX_FLAGS "{$cflags}")
|
||||
SET(CMAKE_FIND_ROOT_PATH "{$root}")
|
||||
CMAKE;
|
||||
file_put_contents(SOURCE_PATH . '/toolchain.cmake', $toolchain);
|
||||
return realpath(SOURCE_PATH . '/toolchain.cmake');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name 命令名称
|
||||
* @param array $paths 寻找的目标路径(如果不传入,则使用环境变量 PATH)
|
||||
* @return null|string 找到了返回命令路径,找不到返回 null
|
||||
*/
|
||||
public static function findCommand(string $name, array $paths = []): ?string
|
||||
{
|
||||
if (!$paths) {
|
||||
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
|
||||
}
|
||||
foreach ($paths as $path) {
|
||||
if (file_exists($path . DIRECTORY_SEPARATOR . $name)) {
|
||||
return $path . DIRECTORY_SEPARATOR . $name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user