static-php-cli/src/SPC/builder/BuilderBase.php

358 lines
9.9 KiB
PHP
Raw Normal View History

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;
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 int Concurrency */
2023-03-18 17:32:21 +08:00
public int $concurrency = 1;
/** @var array<string, LibraryBase> libraries */
2023-03-18 17:32:21 +08:00
protected array $libs = [];
/** @var array<string, Extension> extensions */
2023-03-18 17:32:21 +08:00
protected array $exts = [];
/** @var bool compile libs only (just mark it) */
2023-03-18 17:32:21 +08:00
protected bool $libs_only = false;
/** @var array<string, mixed> compile options */
protected array $options = [];
2023-05-10 21:59:33 +08:00
2023-03-18 17:32:21 +08:00
/**
* Build libraries
2023-03-18 17:32:21 +08:00
*
* @param array<string> $libraries Libraries to build
2023-03-18 17:32:21 +08:00
* @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
{
// search all supported libs
2023-03-18 17:32:21 +08:00
$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;
}
}
// if no libs specified, compile all supported libs
2023-03-18 17:32:21 +08:00
if ($libraries === [] && $this->isLibsOnly()) {
$libraries = array_keys($support_lib_list);
}
// pkg-config must be compiled first, whether it is specified or not
if (!in_array('pkg-config', $libraries)) {
array_unshift($libraries, 'pkg-config');
}
2023-03-18 17:32:21 +08:00
// append dependencies
2023-03-18 17:32:21 +08:00
$libraries = DependencyUtil::getLibsByDeps($libraries);
// add lib object for builder
2023-03-18 17:32:21 +08:00
foreach ($libraries as $library) {
// if some libs are not supported (but in config "lib.json", throw exception)
2023-03-18 17:32:21 +08:00
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);
}
// calculate and check dependencies
2023-03-18 17:32:21 +08:00
foreach ($this->libs as $lib) {
$lib->calcDependency();
}
2023-04-30 12:42:19 +08:00
// extract sources
SourceExtractor::initSource(libs: $libraries);
2023-04-30 12:42:19 +08:00
// build all libs
2023-03-18 17:32:21 +08:00
foreach ($this->libs as $lib) {
2023-09-11 23:44:30 +08:00
match ($lib->tryBuild($this->getOption('rebuild', false))) {
2023-03-18 17:32:21 +08:00
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'),
};
}
}
/**
* Add library to build.
2023-03-18 17:32:21 +08:00
*
* @param LibraryBase $library Library object
2023-03-18 17:32:21 +08:00
*/
public function addLib(LibraryBase $library): void
{
$this->libs[$library::NAME] = $library;
}
/**
* Get library object by name.
2023-03-18 17:32:21 +08:00
*/
public function getLib(string $name): ?LibraryBase
{
return $this->libs[$name] ?? null;
}
/**
* Get all library objects.
*
* @return LibraryBase[]
*/
public function getLibs(): array
{
return $this->libs;
}
2023-03-18 17:32:21 +08:00
/**
* Add extension to build.
2023-03-18 17:32:21 +08:00
*/
public function addExt(Extension $extension): void
{
$this->exts[$extension->getName()] = $extension;
}
/**
* Get extension object by name.
2023-03-18 17:32:21 +08:00
*/
public function getExt(string $name): ?Extension
{
return $this->exts[$name] ?? null;
}
2023-07-24 23:49:52 +08:00
/**
* Get all extension objects.
2023-07-24 23:49:52 +08:00
*
* @return Extension[]
*/
public function getExts(): array
{
return $this->exts;
}
/**
* Check if there is a cpp extensions or libraries.
2023-07-24 23:49:52 +08:00
*
* @throws FileSystemException
* @throws WrongUsageException
*/
public function hasCpp(): bool
2023-07-24 23:49:52 +08:00
{
// judge cpp-extension
$exts = array_keys($this->getExts());
foreach ($exts as $ext) {
if (Config::getExt($ext, 'cpp-extension', false) === true) {
2023-10-14 14:06:09 +08:00
return true;
2023-07-24 23:49:52 +08:00
}
}
$libs = array_keys($this->getLibs());
foreach ($libs as $lib) {
if (Config::getLib($lib, 'cpp-library', false) === true) {
return true;
}
}
2023-10-14 14:06:09 +08:00
return false;
2023-07-24 23:49:52 +08:00
}
2023-03-18 17:32:21 +08:00
/**
* Set libs only mode.
2023-03-18 17:32:21 +08:00
*/
public function setLibsOnly(bool $status = true): void
{
$this->libs_only = $status;
}
/**
* Verify the list of "ext" extensions for validity and declare an Extension object to check the dependencies of the extensions.
2023-03-18 17:32:21 +08:00
*
* @throws FileSystemException
* @throws RuntimeException
2023-07-28 23:44:14 +08:00
* @throws \ReflectionException
* @throws WrongUsageException
2023-03-18 17:32:21 +08:00
*/
public function proveExts(array $extensions): void
{
2023-04-15 18:46:21 +08:00
CustomExt::loadCustomExt();
SourceExtractor::initSource(sources: ['php-src']);
2023-04-30 12:42:19 +08:00
if ($this->getPHPVersionID() >= 80000) {
SourceExtractor::initSource(sources: ['micro']);
2023-04-30 12:42:19 +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) {
$ext->checkDependency();
}
}
/**
* Start to build PHP
2023-03-18 17:32:21 +08:00
*
* @param int $build_target Build target, see BUILD_TARGET_*
2023-03-18 17:32:21 +08:00
*/
abstract public function buildPHP(int $build_target = BUILD_TARGET_NONE);
2023-03-18 17:32:21 +08:00
/**
* Generate extension enable arguments for configure.
* e.g. --enable-mbstring
2023-03-18 17:32:21 +08:00
*
* @throws FileSystemException
* @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);
}
/**
* Get libs only mode.
2023-03-18 17:32:21 +08:00
*/
public function isLibsOnly(): bool
{
return $this->libs_only;
}
2023-04-03 20:47:24 +08:00
/**
* Get PHP Version ID from php-src/main/php_version.h
*
* @throws RuntimeException
* @throws WrongUsageException
2023-04-03 20:47:24 +08:00
*/
public function getPHPVersionID(): int
{
if (!file_exists(SOURCE_PATH . '/php-src/main/php_version.h')) {
throw new WrongUsageException('PHP source files are not available, you need to download them first');
}
2023-04-03 20:47:24 +08:00
$file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h');
if (preg_match('/PHP_VERSION_ID (\d+)/', $file, $match) !== 0) {
return intval($match[1]);
}
throw new RuntimeException('PHP version file format is malformed, please remove it and download again');
2023-04-03 20:47:24 +08:00
}
/**
* Get build type name string to display.
*
* @param int $type Build target type
*/
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';
}
if (($type & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) {
$ls[] = 'embed';
}
2023-04-23 20:31:58 +08:00
return implode(', ', $ls);
}
/**
* Get builder options (maybe changed by user)
*
* @param string $key Option key
* @param mixed $default If not exists, return this value
*/
public function getOption(string $key, mixed $default = null): mixed
{
return $this->options[$key] ?? $default;
}
/**
* Get all builder options
*/
public function getOptions(): array
{
return $this->options;
}
/**
* Set builder options if not exists.
*/
public function setOptionIfNotExist(string $key, mixed $value): void
{
if (!isset($this->options[$key])) {
$this->options[$key] = $value;
}
}
/**
* Set builder options.
*/
public function setOption(string $key, mixed $value): void
2023-05-10 21:59:33 +08:00
{
$this->options[$key] = $value;
2023-05-10 21:59:33 +08:00
}
public function getEnvString(array $vars = ['cc', 'cxx', 'ar', 'ld']): string
{
$env = [];
foreach ($vars as $var) {
$var = strtoupper($var);
if (getenv($var) !== false) {
$env[] = "{$var}=" . getenv($var);
}
}
return implode(' ', $env);
}
2023-03-18 17:32:21 +08:00
/**
* Check if all libs are downloaded.
* If not, throw exception.
2023-03-18 17:32:21 +08:00
*
* @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?'
);
}
}
}