add wrong usage exception

This commit is contained in:
crazywhalecc
2023-03-29 21:39:36 +08:00
parent d31e1b00e8
commit b0ac50941f
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\windows\WindowsBuilder;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use Symfony\Component\Console\Input\InputInterface;
/**
@@ -36,7 +37,7 @@ class BuilderProvider
cxx: $input->getOption('cxx'),
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\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\store\Config;
class Extension
@@ -62,7 +63,7 @@ class Extension
'enable' => '--enable-' . $_name,
'with' => '--with-' . $_name,
'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\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher;
/**
@@ -86,7 +87,7 @@ class LinuxBuilder extends BuilderBase
}
}
if (!empty($missing)) {
throw new RuntimeException('missing system commands: ' . implode(', ', $missing));
throw new WrongUsageException('missing system commands: ' . implode(', ', $missing));
}
// 创立 pkg-config 和放头文件的目录
@@ -151,7 +152,7 @@ class LinuxBuilder extends BuilderBase
}
break;
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'";

View File

@@ -7,6 +7,7 @@ namespace SPC\builder\linux;
use JetBrains\PhpStorm\ArrayShape;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
class SystemUtil
{
@@ -128,10 +129,10 @@ class SystemUtil
'clang' => match ($arch) {
'x86_64' => '--target=x86_64-unknown-linux',
'arm64', 'aarch64' => '--target=arm64-unknown-linux',
default => throw new RuntimeException('unsupported arch: ' . $arch),
default => throw new WrongUsageException('unsupported arch: ' . $arch),
},
'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\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher;
/**
@@ -27,7 +28,11 @@ class MacOSBuilder extends BuilderBase
private bool $phar_patched = false;
/**
* @param null|string $cc C编译器名称如果不传入则默认使用clang
* @param null|string $cxx C++编译器名称如果不传入则默认使用clang++
* @param null|string $arch 当前架构,如果不传入则默认使用当前系统架构
* @throws RuntimeException
* @throws WrongUsageException
*/
public function __construct(?string $cc = null, ?string $cxx = null, ?string $arch = null)
{
@@ -218,6 +223,7 @@ class MacOSBuilder extends BuilderBase
if ($this->getExt('phar')) {
$this->phar_patched = true;
try {
// TODO: 未来改进一下 patch让除了这种 patch 类型的文件以外可以恢复原文件
f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 < sapi/micro/patches/phar.patch');
} catch (RuntimeException $e) {
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
{
$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\exception\RuntimeException;
use SPC\exception\WrongUsageException;
class SystemUtil
{
@@ -30,14 +31,15 @@ class SystemUtil
/**
* 获取不同架构对应的 cflags 参数
*
* @throws RuntimeException
* @param string $arch 架构名称
* @throws WrongUsageException
*/
public static function getArchCFlags(string $arch): string
{
return match ($arch) {
'x86_64' => '--target=x86_64-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;
use SPC\store\FileSystem;
/**
* Unix 系统的工具函数 Trait适用于 Linux、macOS
*/
@@ -48,7 +50,7 @@ CMAKE;
if (PHP_OS_FAMILY === 'Linux') {
$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');
}