refactor download

This commit is contained in:
crazywhalecc
2023-04-30 12:42:19 +08:00
parent 117cd93e8f
commit 0bed76da11
30 changed files with 901 additions and 673 deletions

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;
use SPC\store\FileSystem;
use SPC\util\CustomExt;
@@ -40,8 +41,9 @@ abstract class BuilderBase
/**
* 构建指定列表的 libs
*
* @throws RuntimeException
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
*/
public function buildLibs(array $libraries): void
{
@@ -80,13 +82,14 @@ abstract class BuilderBase
$this->addLib($lib);
}
// 统计还没 fetch 到本地的库
$this->checkLibsSource();
// 计算依赖,经过这里的遍历,如果没有抛出异常,说明依赖符合要求,可以继续下面的
foreach ($this->libs as $lib) {
$lib->calcDependency();
}
$this->initSource(libs: $libraries);
// 构建库
foreach ($this->libs as $lib) {
match ($lib->tryBuild()) {
BUILD_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] build success'),
@@ -154,6 +157,11 @@ abstract class BuilderBase
public function proveExts(array $extensions): void
{
CustomExt::loadCustomExt();
$this->initSource(sources: ['php-src']);
if ($this->getPHPVersionID() >= 80000) {
$this->initSource(sources: ['micro']);
}
$this->initSource(exts: $extensions);
foreach ($extensions as $extension) {
$class = CustomExt::getExtClass($extension);
$ext = new $class($extension, $this);
@@ -248,4 +256,52 @@ abstract class BuilderBase
);
}
}
protected function initSource(?array $sources = null, ?array $libs = null, ?array $exts = null): void
{
if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) {
throw new WrongUsageException('Download lock file "downloads/.lock.json" not found, maybe you need to download sources first ?');
}
$lock = json_decode(FileSystem::readFile(DOWNLOAD_PATH . '/.lock.json'), true);
$sources_extracted = [];
// source check exist
if (is_array($sources)) {
foreach ($sources as $source) {
$sources_extracted[$source] = true;
}
}
// lib check source exist
if (is_array($libs)) {
foreach ($libs as $lib) {
// get source name for lib
$source = Config::getLib($lib, 'source');
$sources_extracted[$source] = true;
}
}
// ext check source exist
if (is_array($exts)) {
foreach ($exts as $ext) {
// get source name for ext
if (Config::getExt($ext, 'type') !== 'external') {
continue;
}
$source = Config::getExt($ext, 'source');
$sources_extracted[$source] = true;
}
}
// start check
foreach ($sources_extracted as $source => $item) {
if (!isset($lock[$source])) {
throw new WrongUsageException('Source [' . $source . '] not downloaded, you should download it first !');
}
// check source dir exist
$check = $lock[$source]['move_path'] === null ? SOURCE_PATH . '/' . $source : SOURCE_PATH . '/' . $lock[$source]['move_path'];
if (!is_dir($check)) {
FileSystem::extractSource($source, DOWNLOAD_PATH . '/' . ($lock[$source]['filename'] ?? $lock[$source]['dirname']), $lock[$source]['move_path']);
}
}
}
}

View File

@@ -13,11 +13,8 @@ class swoole extends Extension
public function getUnixConfigureArg(): string
{
$arg = '--enable-swoole';
if ($this->builder->getLib('openssl')) {
$arg .= ' --enable-openssl';
} else {
$arg .= ' --disable-openssl --without-openssl';
}
$arg .= $this->builder->getLib('openssl') ? ' --enable-openssl' : ' --disable-openssl --without-openssl';
$arg .= $this->builder->getLib('brotli') ? (' --enable-brotli --with-brotli-dir=' . BUILD_ROOT_PATH) : '';
// curl hook is buggy for static php
$arg .= ' --disable-swoole-curl';
return $arg;

View File

@@ -10,7 +10,7 @@ use SPC\builder\traits\UnixBuilderTrait;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher;
use SPC\store\SourcePatcher;
/**
* Linux 系统环境下的构建器
@@ -46,7 +46,10 @@ class LinuxBuilder extends BuilderBase
public function __construct(?string $cc = null, ?string $cxx = null, ?string $arch = null)
{
// 初始化一些默认参数
$this->cc = $cc ?? 'musl-gcc';
$this->cc = $cc ?? match (SystemUtil::getOSRelease()['dist']) {
'alpine' => 'gcc',
default => 'musl-gcc'
};
$this->cxx = $cxx ?? 'g++';
$this->arch = $arch ?? php_uname('m');
$this->gnu_arch = arch2gnu($this->arch);
@@ -135,6 +138,9 @@ class LinuxBuilder extends BuilderBase
)
);
}
if ($this->getExt('swoole')) {
$extra_libs .= ' -lstdc++';
}
$envs = $this->pkgconf_env . ' ' .
"CC='{$this->cc}' " .
@@ -158,11 +164,11 @@ class LinuxBuilder extends BuilderBase
$envs = "{$envs} CFLAGS='{$cflags}' LIBS='-ldl -lpthread'";
Patcher::patchPHPBeforeConfigure($this);
SourcePatcher::patchPHPBuildconf($this);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
Patcher::patchPHPConfigure($this);
SourcePatcher::patchPHPConfigure($this);
shell()->cd(SOURCE_PATH . '/php-src')
->exec(
@@ -183,6 +189,8 @@ class LinuxBuilder extends BuilderBase
$envs
);
SourcePatcher::patchPHPAfterConfigure($this);
file_put_contents('/tmp/comment', $this->note_section);
// 清理

View File

@@ -13,44 +13,6 @@ class SystemUtil
{
use UnixSystemUtilTrait;
/**
* 查找并选择编译器命令
*
* @throws RuntimeException
*/
public static function selectCC(): string
{
logger()->debug('Choose cc');
if (self::findCommand('clang')) {
logger()->info('using clang');
return 'clang';
}
if (self::findCommand('gcc')) {
logger()->info('using gcc');
return 'gcc';
}
throw new RuntimeException('no supported cc found');
}
/**
* 查找并选择编译器命令
*
* @throws RuntimeException
*/
public static function selectCXX(): string
{
logger()->debug('Choose cxx');
if (self::findCommand('clang++')) {
logger()->info('using clang++');
return 'clang++';
}
if (self::findCommand('g++')) {
logger()->info('using g++');
return 'g++';
}
return self::selectCC();
}
#[ArrayShape(['dist' => 'mixed|string', 'ver' => 'mixed|string'])]
public static function getOSRelease(): array
{

View File

@@ -22,7 +22,7 @@ namespace SPC\builder\linux\library;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\util\Patcher;
use SPC\store\SourcePatcher;
class libpng extends LinuxLibraryBase
{
@@ -41,7 +41,7 @@ class libpng extends LinuxLibraryBase
};
// patch configure
Patcher::patchUnixLibpng();
SourcePatcher::patchUnixLibpng();
shell()->cd($this->source_dir)
->exec('chmod +x ./configure')

View File

@@ -10,7 +10,7 @@ use SPC\builder\traits\UnixBuilderTrait;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher;
use SPC\store\SourcePatcher;
/**
* macOS 系统环境下的构建器
@@ -136,11 +136,11 @@ class MacOSBuilder extends BuilderBase
}
// patch before configure
Patcher::patchPHPBeforeConfigure($this);
SourcePatcher::patchPHPBuildconf($this);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
Patcher::patchPHPConfigure($this);
SourcePatcher::patchPHPConfigure($this);
if ($this->getLib('libxml2') || $this->getExt('iconv')) {
$extra_libs .= ' -liconv';
@@ -166,6 +166,8 @@ class MacOSBuilder extends BuilderBase
$this->configure_env
);
SourcePatcher::patchPHPAfterConfigure($this);
$this->cleanMake();
if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {

View File

@@ -20,7 +20,7 @@ declare(strict_types=1);
namespace SPC\builder\macos\library;
use SPC\util\Patcher;
use SPC\store\SourcePatcher;
class curl extends MacOSLibraryBase
{
@@ -32,7 +32,7 @@ class curl extends MacOSLibraryBase
protected function build()
{
Patcher::patchCurlMacOS();
SourcePatcher::patchCurlMacOS();
$this->unixBuild();
}
}