2023-03-18 17:32:21 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
namespace SPC\builder;
|
|
|
|
|
|
|
|
|
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
|
use SPC\exception\RuntimeException;
|
2023-04-30 12:42:19 +08:00
|
|
|
|
use SPC\exception\WrongUsageException;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
use SPC\store\Config;
|
|
|
|
|
|
use SPC\store\FileSystem;
|
2023-07-28 00:02:49 +08:00
|
|
|
|
use SPC\store\SourceExtractor;
|
2023-04-15 18:46:21 +08:00
|
|
|
|
use SPC\util\CustomExt;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
use SPC\util\DependencyUtil;
|
|
|
|
|
|
|
|
|
|
|
|
abstract class BuilderBase
|
|
|
|
|
|
{
|
|
|
|
|
|
/** @var bool 是否启用 ZTS 线程安全 */
|
|
|
|
|
|
public bool $zts = false;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var string 编译目标架构 */
|
|
|
|
|
|
public string $arch;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var string GNU 格式的编译目标架构 */
|
|
|
|
|
|
public string $gnu_arch;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var int 编译进程数 */
|
|
|
|
|
|
public int $concurrency = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var array<string, LibraryBase> 要编译的 libs 列表 */
|
|
|
|
|
|
protected array $libs = [];
|
|
|
|
|
|
|
|
|
|
|
|
/** @var array<string, Extension> 要编译的扩展列表 */
|
|
|
|
|
|
protected array $exts = [];
|
|
|
|
|
|
|
2023-03-26 23:59:34 +08:00
|
|
|
|
/** @var array<int, string> 要编译的扩展列表(仅名字列表,用于最后生成编译的扩展列表给 micro) */
|
|
|
|
|
|
protected array $plain_extensions = [];
|
|
|
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
|
/** @var bool 本次编译是否只编译 libs,不编译 PHP */
|
|
|
|
|
|
protected bool $libs_only = false;
|
|
|
|
|
|
|
2023-05-10 21:59:33 +08:00
|
|
|
|
/** @var bool 是否 strip 最终的二进制 */
|
|
|
|
|
|
protected bool $strip = true;
|
|
|
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建指定列表的 libs
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws FileSystemException
|
2023-04-30 12:42:19 +08:00
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
|
* @throws WrongUsageException
|
2023-03-18 17:32:21 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function buildLibs(array $libraries): void
|
|
|
|
|
|
{
|
|
|
|
|
|
// 通过扫描目录查找 lib
|
|
|
|
|
|
$support_lib_list = [];
|
|
|
|
|
|
$classes = FileSystem::getClassesPsr4(
|
|
|
|
|
|
ROOT_DIR . '/src/SPC/builder/' . osfamily2dir() . '/library',
|
|
|
|
|
|
'SPC\\builder\\' . osfamily2dir() . '\\library'
|
|
|
|
|
|
);
|
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
|
|
if (defined($class . '::NAME') && $class::NAME !== 'unknown' && Config::getLib($class::NAME) !== null) {
|
|
|
|
|
|
$support_lib_list[$class::NAME] = $class;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果传入了空,则默认检查和安置所有支持的lib,libraries为要build的,support_lib_list为支持的列表
|
|
|
|
|
|
if ($libraries === [] && $this->isLibsOnly()) {
|
|
|
|
|
|
$libraries = array_keys($support_lib_list);
|
|
|
|
|
|
}
|
2023-05-10 02:04:08 +08:00
|
|
|
|
if (!in_array('pkg-config', $libraries)) {
|
|
|
|
|
|
array_unshift($libraries, 'pkg-config');
|
|
|
|
|
|
}
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 排序 libs,根据依赖计算一个新的列表出来
|
|
|
|
|
|
$libraries = DependencyUtil::getLibsByDeps($libraries);
|
|
|
|
|
|
|
|
|
|
|
|
// 过滤不支持的库后添加
|
|
|
|
|
|
foreach ($libraries as $library) {
|
|
|
|
|
|
if (!isset($support_lib_list[$library])) {
|
|
|
|
|
|
throw new RuntimeException('library [' . $library . '] is in the lib.json list but not supported to compile, but in the future I will support it!');
|
|
|
|
|
|
}
|
|
|
|
|
|
$lib = new ($support_lib_list[$library])($this);
|
|
|
|
|
|
$this->addLib($lib);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 计算依赖,经过这里的遍历,如果没有抛出异常,说明依赖符合要求,可以继续下面的
|
|
|
|
|
|
foreach ($this->libs as $lib) {
|
|
|
|
|
|
$lib->calcDependency();
|
|
|
|
|
|
}
|
2023-04-30 12:42:19 +08:00
|
|
|
|
|
2023-07-28 00:02:49 +08:00
|
|
|
|
SourceExtractor::initSource(libs: $libraries);
|
2023-04-30 12:42:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 构建库
|
2023-03-18 17:32:21 +08:00
|
|
|
|
foreach ($this->libs as $lib) {
|
|
|
|
|
|
match ($lib->tryBuild()) {
|
|
|
|
|
|
BUILD_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] build success'),
|
|
|
|
|
|
BUILD_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
|
|
|
|
|
|
BUILD_STATUS_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
|
|
|
|
|
|
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加要编译的 Lib 库
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param LibraryBase $library Lib 库对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function addLib(LibraryBase $library): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->libs[$library::NAME] = $library;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取要编译的 Lib 库对象
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $name 库名称
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getLib(string $name): ?LibraryBase
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->libs[$name] ?? null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加要编译的扩展
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param Extension $extension 扩展对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function addExt(Extension $extension): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->exts[$extension->getName()] = $extension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取要编译的扩展对象
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $name 扩展名称
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getExt(string $name): ?Extension
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->exts[$name] ?? null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-24 23:49:52 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取所有要编译的扩展对象
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return Extension[]
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getExts(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->exts;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查 C++ 扩展是否存在
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function hasCppExtension(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
// judge cpp-extension
|
|
|
|
|
|
$exts = array_keys($this->getExts());
|
|
|
|
|
|
$cpp = false;
|
|
|
|
|
|
foreach ($exts as $ext) {
|
|
|
|
|
|
if (Config::getExt($ext, 'cpp-extension', false) === true) {
|
|
|
|
|
|
$cpp = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return $cpp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置本次 Builder 是否为仅编译库的模式
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function setLibsOnly(bool $status = true): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->libs_only = $status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检验 ext 扩展列表是否合理,并声明 Extension 对象,检查扩展的依赖
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function proveExts(array $extensions): void
|
|
|
|
|
|
{
|
2023-04-15 18:46:21 +08:00
|
|
|
|
CustomExt::loadCustomExt();
|
2023-07-28 00:02:49 +08:00
|
|
|
|
SourceExtractor::initSource(sources: ['php-src']);
|
2023-04-30 12:42:19 +08:00
|
|
|
|
if ($this->getPHPVersionID() >= 80000) {
|
2023-07-28 00:02:49 +08:00
|
|
|
|
SourceExtractor::initSource(sources: ['micro']);
|
2023-04-30 12:42:19 +08:00
|
|
|
|
}
|
2023-07-28 00:02:49 +08:00
|
|
|
|
SourceExtractor::initSource(exts: $extensions);
|
2023-03-18 17:32:21 +08:00
|
|
|
|
foreach ($extensions as $extension) {
|
2023-04-15 18:46:21 +08:00
|
|
|
|
$class = CustomExt::getExtClass($extension);
|
|
|
|
|
|
$ext = new $class($extension, $this);
|
2023-03-18 17:32:21 +08:00
|
|
|
|
$this->addExt($ext);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->exts as $ext) {
|
|
|
|
|
|
// 检查下依赖就行了,作用是导入依赖给 Extension 对象,今后可以对库依赖进行选择性处理
|
|
|
|
|
|
$ext->checkDependency();
|
|
|
|
|
|
}
|
2023-03-26 23:59:34 +08:00
|
|
|
|
|
|
|
|
|
|
$this->plain_extensions = $extensions;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 开始构建 PHP
|
|
|
|
|
|
*
|
2023-04-23 20:31:58 +08:00
|
|
|
|
* @param int $build_target 规则
|
|
|
|
|
|
* @param bool $bloat 保留
|
2023-03-18 17:32:21 +08:00
|
|
|
|
*/
|
2023-04-23 20:31:58 +08:00
|
|
|
|
abstract public function buildPHP(int $build_target = BUILD_TARGET_NONE, bool $bloat = false);
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 生成依赖的扩展编译启用参数
|
|
|
|
|
|
* 例如 --enable-mbstring 等
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
|
* @throws FileSystemException
|
2023-07-28 00:02:49 +08:00
|
|
|
|
* @throws WrongUsageException
|
2023-03-18 17:32:21 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function makeExtensionArgs(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
$ret = [];
|
|
|
|
|
|
foreach ($this->exts as $ext) {
|
2023-04-15 18:46:21 +08:00
|
|
|
|
$ret[] = trim($ext->getConfigureArg());
|
2023-03-18 17:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
logger()->info('Using configure: ' . implode(' ', $ret));
|
|
|
|
|
|
return implode(' ', $ret);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回是否只编译 libs 的模式
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function isLibsOnly(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->libs_only;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-03 20:47:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前即将编译的 PHP 的版本 ID,五位数那个
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getPHPVersionID(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
$file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h');
|
|
|
|
|
|
preg_match('/PHP_VERSION_ID (\d+)/', $file, $match);
|
|
|
|
|
|
return intval($match[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-23 20:31:58 +08:00
|
|
|
|
public function getBuildTypeName(int $type): string
|
|
|
|
|
|
{
|
|
|
|
|
|
$ls = [];
|
|
|
|
|
|
if (($type & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
|
|
|
|
|
|
$ls[] = 'cli';
|
|
|
|
|
|
}
|
|
|
|
|
|
if (($type & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
|
|
|
|
|
|
$ls[] = 'micro';
|
|
|
|
|
|
}
|
|
|
|
|
|
if (($type & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) {
|
|
|
|
|
|
$ls[] = 'fpm';
|
|
|
|
|
|
}
|
|
|
|
|
|
return implode(', ', $ls);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-10 21:59:33 +08:00
|
|
|
|
public function setStrip(bool $strip): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->strip = $strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 检查是否存在 lib 库对应的源码,如果不存在,则抛出异常
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function checkLibsSource(): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$not_downloaded = [];
|
|
|
|
|
|
foreach ($this->libs as $lib) {
|
|
|
|
|
|
if (!file_exists($lib->getSourceDir())) {
|
|
|
|
|
|
$not_downloaded[] = $lib::NAME;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($not_downloaded !== []) {
|
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
|
'"' . implode(', ', $not_downloaded) .
|
|
|
|
|
|
'" totally ' . count($not_downloaded) .
|
|
|
|
|
|
' source' . (count($not_downloaded) === 1 ? '' : 's') .
|
|
|
|
|
|
' not downloaded, maybe you need to "fetch" ' . (count($not_downloaded) === 1 ? 'it' : 'them') . ' first?'
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|