add wrong usage exception

This commit is contained in:
crazywhalecc 2023-03-29 21:39:36 +08:00
parent d31e1b00e8
commit b0ac50941f
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
13 changed files with 67 additions and 18 deletions

View File

@ -8,6 +8,7 @@ use SPC\builder\linux\LinuxBuilder;
use SPC\builder\macos\MacOSBuilder; use SPC\builder\macos\MacOSBuilder;
use SPC\builder\windows\WindowsBuilder; use SPC\builder\windows\WindowsBuilder;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
/** /**
@ -36,7 +37,7 @@ class BuilderProvider
cxx: $input->getOption('cxx'), cxx: $input->getOption('cxx'),
arch: $input->getOption('arch'), arch: $input->getOption('arch'),
), ),
default => throw new RuntimeException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'), default => throw new WrongUsageException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'),
}; };
} }
} }

View File

@ -6,6 +6,7 @@ namespace SPC\builder;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\store\Config; use SPC\store\Config;
class Extension class Extension
@ -62,7 +63,7 @@ class Extension
'enable' => '--enable-' . $_name, 'enable' => '--enable-' . $_name,
'with' => '--with-' . $_name, 'with' => '--with-' . $_name,
'none', 'custom' => '', 'none', 'custom' => '',
default => throw new RuntimeException("argType does not accept {$arg_type}, use [enable/with] ."), default => throw new WrongUsageException("argType does not accept {$arg_type}, use [enable/with] ."),
}; };
} }

View File

@ -9,6 +9,7 @@ use SPC\builder\linux\library\LinuxLibraryBase;
use SPC\builder\traits\UnixBuilderTrait; use SPC\builder\traits\UnixBuilderTrait;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher; use SPC\util\Patcher;
/** /**
@ -86,7 +87,7 @@ class LinuxBuilder extends BuilderBase
} }
} }
if (!empty($missing)) { if (!empty($missing)) {
throw new RuntimeException('missing system commands: ' . implode(', ', $missing)); throw new WrongUsageException('missing system commands: ' . implode(', ', $missing));
} }
// 创立 pkg-config 和放头文件的目录 // 创立 pkg-config 和放头文件的目录
@ -151,7 +152,7 @@ class LinuxBuilder extends BuilderBase
} }
break; break;
default: default:
throw new RuntimeException('libc ' . $this->libc . ' is not implemented yet'); throw new WrongUsageException('libc ' . $this->libc . ' is not implemented yet');
} }
$envs = "{$envs} CFLAGS='{$cflags}' LIBS='-ldl -lpthread'"; $envs = "{$envs} CFLAGS='{$cflags}' LIBS='-ldl -lpthread'";

View File

@ -7,6 +7,7 @@ namespace SPC\builder\linux;
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
use SPC\builder\traits\UnixSystemUtilTrait; use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
class SystemUtil class SystemUtil
{ {
@ -128,10 +129,10 @@ class SystemUtil
'clang' => match ($arch) { 'clang' => match ($arch) {
'x86_64' => '--target=x86_64-unknown-linux', 'x86_64' => '--target=x86_64-unknown-linux',
'arm64', 'aarch64' => '--target=arm64-unknown-linux', 'arm64', 'aarch64' => '--target=arm64-unknown-linux',
default => throw new RuntimeException('unsupported arch: ' . $arch), default => throw new WrongUsageException('unsupported arch: ' . $arch),
}, },
'gcc' => '', 'gcc' => '',
default => throw new RuntimeException('cc compiler ' . $cc . ' is not supported'), default => throw new WrongUsageException('cc compiler ' . $cc . ' is not supported'),
}; };
} }

View File

@ -9,6 +9,7 @@ use SPC\builder\macos\library\MacOSLibraryBase;
use SPC\builder\traits\UnixBuilderTrait; use SPC\builder\traits\UnixBuilderTrait;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher; use SPC\util\Patcher;
/** /**
@ -27,7 +28,11 @@ class MacOSBuilder extends BuilderBase
private bool $phar_patched = false; private bool $phar_patched = false;
/** /**
* @param null|string $cc C编译器名称如果不传入则默认使用clang
* @param null|string $cxx C++编译器名称如果不传入则默认使用clang++
* @param null|string $arch 当前架构,如果不传入则默认使用当前系统架构
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException
*/ */
public function __construct(?string $cc = null, ?string $cxx = null, ?string $arch = null) public function __construct(?string $cc = null, ?string $cxx = null, ?string $arch = null)
{ {
@ -218,6 +223,7 @@ class MacOSBuilder extends BuilderBase
if ($this->getExt('phar')) { if ($this->getExt('phar')) {
$this->phar_patched = true; $this->phar_patched = true;
try { try {
// TODO: 未来改进一下 patch让除了这种 patch 类型的文件以外可以恢复原文件
f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 < sapi/micro/patches/phar.patch'); f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 < sapi/micro/patches/phar.patch');
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
logger()->error('failed to patch phat due to patch exit with code ' . $e->getCode()); logger()->error('failed to patch phat due to patch exit with code ' . $e->getCode());
@ -257,6 +263,9 @@ class MacOSBuilder extends BuilderBase
); );
} }
/**
* 获取当前即将编译的 PHP 的版本 ID五位数那个
*/
public function getPHPVersionID(): int public function getPHPVersionID(): int
{ {
$file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h'); $file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h');

View File

@ -6,6 +6,7 @@ namespace SPC\builder\macos;
use SPC\builder\traits\UnixSystemUtilTrait; use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
class SystemUtil class SystemUtil
{ {
@ -30,14 +31,15 @@ class SystemUtil
/** /**
* 获取不同架构对应的 cflags 参数 * 获取不同架构对应的 cflags 参数
* *
* @throws RuntimeException * @param string $arch 架构名称
* @throws WrongUsageException
*/ */
public static function getArchCFlags(string $arch): string public static function getArchCFlags(string $arch): string
{ {
return match ($arch) { return match ($arch) {
'x86_64' => '--target=x86_64-apple-darwin', 'x86_64' => '--target=x86_64-apple-darwin',
'arm64','aarch64' => '--target=arm64-apple-darwin', 'arm64','aarch64' => '--target=arm64-apple-darwin',
default => throw new RuntimeException('unsupported arch: ' . $arch), default => throw new WrongUsageException('unsupported arch: ' . $arch),
}; };
} }
} }

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace SPC\builder\traits; namespace SPC\builder\traits;
use SPC\store\FileSystem;
/** /**
* Unix 系统的工具函数 Trait,适用于 Linux、macOS * Unix 系统的工具函数 Trait,适用于 Linux、macOS
*/ */
@ -48,7 +50,7 @@ CMAKE;
if (PHP_OS_FAMILY === 'Linux') { if (PHP_OS_FAMILY === 'Linux') {
$toolchain .= "\nSET(CMAKE_AR \"ar\")"; $toolchain .= "\nSET(CMAKE_AR \"ar\")";
} }
file_put_contents(SOURCE_PATH . '/toolchain.cmake', $toolchain); FileSystem::writeFile(SOURCE_PATH . '/toolchain.cmake', $toolchain);
return realpath(SOURCE_PATH . '/toolchain.cmake'); return realpath(SOURCE_PATH . '/toolchain.cmake');
} }

View File

@ -6,6 +6,7 @@ namespace SPC\command;
use SPC\builder\BuilderProvider; use SPC\builder\BuilderProvider;
use SPC\exception\ExceptionHandler; use SPC\exception\ExceptionHandler;
use SPC\exception\WrongUsageException;
use SPC\util\DependencyUtil; use SPC\util\DependencyUtil;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -73,6 +74,9 @@ class BuildCliCommand extends BuildCommand
logger()->info('phpmicro binary path: ' . BUILD_ROOT_PATH . '/bin/micro.sfx'); logger()->info('phpmicro binary path: ' . BUILD_ROOT_PATH . '/bin/micro.sfx');
} }
return 0; return 0;
} catch (WrongUsageException $e) {
logger()->critical($e->getMessage());
return 1;
} catch (\Throwable $e) { } catch (\Throwable $e) {
if ($input->getOption('debug')) { if ($input->getOption('debug')) {
ExceptionHandler::getInstance()->handle($e); ExceptionHandler::getInstance()->handle($e);

View File

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace SPC\exception;
class WrongUsageException extends \Exception
{
}

View File

@ -6,6 +6,7 @@ namespace SPC\store;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
/** /**
* 一个读取 config 配置的操作类 * 一个读取 config 配置的操作类
@ -38,7 +39,7 @@ class Config
* 对于 macOS 平台,支持 frameworks。 * 对于 macOS 平台,支持 frameworks。
* *
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException * @throws WrongUsageException
*/ */
public static function getLib(string $name, ?string $key = null, mixed $default = null) public static function getLib(string $name, ?string $key = null, mixed $default = null)
{ {
@ -46,7 +47,7 @@ class Config
self::$lib = FileSystem::loadConfigArray('lib'); self::$lib = FileSystem::loadConfigArray('lib');
} }
if (!isset(self::$lib[$name])) { if (!isset(self::$lib[$name])) {
throw new RuntimeException('lib [' . $name . '] is not supported yet for get'); throw new WrongUsageException('lib [' . $name . '] is not supported yet');
} }
$supported_sys_based = ['static-libs', 'headers', 'lib-depends', 'lib-suggests', 'frameworks']; $supported_sys_based = ['static-libs', 'headers', 'lib-depends', 'lib-suggests', 'frameworks'];
if ($key !== null && in_array($key, $supported_sys_based)) { if ($key !== null && in_array($key, $supported_sys_based)) {
@ -54,7 +55,7 @@ class Config
'Windows' => ['-windows', '-win', ''], 'Windows' => ['-windows', '-win', ''],
'Darwin' => ['-macos', '-unix', ''], 'Darwin' => ['-macos', '-unix', ''],
'Linux' => ['-linux', '-unix', ''], 'Linux' => ['-linux', '-unix', ''],
default => throw new RuntimeException('OS ' . PHP_OS_FAMILY . ' is not supported'), default => throw new WrongUsageException('OS ' . PHP_OS_FAMILY . ' is not supported'),
}; };
foreach ($m_key as $v) { foreach ($m_key as $v) {
if (isset(self::$lib[$name][$key . $v])) { if (isset(self::$lib[$name][$key . $v])) {
@ -83,6 +84,7 @@ class Config
/** /**
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException
*/ */
public static function getExt(string $name, ?string $key = null, mixed $default = null) public static function getExt(string $name, ?string $key = null, mixed $default = null)
{ {
@ -90,7 +92,7 @@ class Config
self::$ext = FileSystem::loadConfigArray('ext'); self::$ext = FileSystem::loadConfigArray('ext');
} }
if (!isset(self::$ext[$name])) { if (!isset(self::$ext[$name])) {
throw new RuntimeException('ext [' . $name . '] is not supported yet for get'); throw new WrongUsageException('ext [' . $name . '] is not supported yet');
} }
$supported_sys_based = ['lib-depends', 'lib-suggests', 'ext-depends', 'ext-suggests', 'arg-type']; $supported_sys_based = ['lib-depends', 'lib-suggests', 'ext-depends', 'ext-suggests', 'arg-type'];
if ($key !== null && in_array($key, $supported_sys_based)) { if ($key !== null && in_array($key, $supported_sys_based)) {
@ -98,7 +100,7 @@ class Config
'Windows' => ['-windows', '-win', ''], 'Windows' => ['-windows', '-win', ''],
'Darwin' => ['-macos', '-unix', ''], 'Darwin' => ['-macos', '-unix', ''],
'Linux' => ['-linux', '-unix', ''], 'Linux' => ['-linux', '-unix', ''],
default => throw new RuntimeException('OS ' . PHP_OS_FAMILY . ' is not supported'), default => throw new WrongUsageException('OS ' . PHP_OS_FAMILY . ' is not supported'),
}; };
foreach ($m_key as $v) { foreach ($m_key as $v) {
if (isset(self::$ext[$name][$key . $v])) { if (isset(self::$ext[$name][$key . $v])) {

View File

@ -385,4 +385,13 @@ class FileSystem
throw new FileSystemException(sprintf('无法建立目录:%s', $path)); throw new FileSystemException(sprintf('无法建立目录:%s', $path));
} }
} }
public static function writeFile(string $path, $content, ...$args): bool|string|int
{
$dir = pathinfo($path, PATHINFO_DIRNAME);
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
throw new FileSystemException('Write file failed, cannot create parent directory: ' . $dir);
}
return file_put_contents($path, $content, ...$args);
}
} }

View File

@ -196,7 +196,7 @@ class Patcher
case 'x86_64': case 'x86_64':
break; break;
default: default:
throw new RuntimeException('unsupported arch: ' . $builder->arch); throw new RuntimeException('unsupported arch while patching php configure: ' . $builder->arch);
} }
} }
} }

View File

@ -3,11 +3,13 @@
declare(strict_types=1); declare(strict_types=1);
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use SPC\exception\WrongUsageException;
use SPC\util\UnixShell; use SPC\util\UnixShell;
use ZM\Logger\ConsoleLogger; use ZM\Logger\ConsoleLogger;
/** /**
* 判断传入的数组是否为关联数组 * 判断传入的数组是否为关联数组
*
* @param mixed $array * @param mixed $array
*/ */
function is_assoc_array($array): bool function is_assoc_array($array): bool
@ -28,7 +30,8 @@ function logger(): LoggerInterface
} }
/** /**
* @throws \SPC\exception\RuntimeException * @param string $arch 架构名称转换为 GNU 标准形式
* @throws WrongUsageException
*/ */
function arch2gnu(string $arch): string function arch2gnu(string $arch): string
{ {
@ -36,7 +39,7 @@ function arch2gnu(string $arch): string
return match ($arch) { return match ($arch) {
'x86_64', 'x64', 'amd64' => 'x86_64', 'x86_64', 'x64', 'amd64' => 'x86_64',
'arm64', 'aarch64' => 'aarch64', 'arm64', 'aarch64' => 'aarch64',
default => throw new \SPC\exception\RuntimeException('Not support arch: ' . $arch), default => throw new WrongUsageException('Not support arch: ' . $arch),
// 'armv7' => 'arm', // 'armv7' => 'arm',
}; };
} }
@ -46,6 +49,11 @@ function quote(string $str, string $quote = '"'): string
return $quote . $str . $quote; return $quote . $str . $quote;
} }
/**
* 将不同系统环境的编译使用工具集的文件夹名称进行一个返回
*
* @throws WrongUsageException
*/
function osfamily2dir(): string function osfamily2dir(): string
{ {
return match (PHP_OS_FAMILY) { return match (PHP_OS_FAMILY) {
@ -53,7 +61,7 @@ function osfamily2dir(): string
'Windows', 'WINNT', 'Cygwin' => 'windows', 'Windows', 'WINNT', 'Cygwin' => 'windows',
'Darwin' => 'macos', 'Darwin' => 'macos',
'Linux' => 'linux', 'Linux' => 'linux',
default => throw new \SPC\exception\RuntimeException('Not support os: ' . PHP_OS_FAMILY), default => throw new WrongUsageException('Not support os: ' . PHP_OS_FAMILY),
}; };
} }