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

487 lines
14 KiB
PHP
Raw Normal View History

2023-03-18 17:32:21 +08:00
<?php
declare(strict_types=1);
namespace SPC\builder;
2024-01-03 15:57:05 +08:00
use SPC\exception\ExceptionHandler;
2023-03-18 17:32:21 +08:00
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;
2024-02-18 13:54:06 +08:00
use SPC\store\SourceManager;
2023-04-15 18:46:21 +08:00
use SPC\util\CustomExt;
2023-03-18 17:32:21 +08:00
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
2024-01-03 15:57:05 +08:00
/** @var string patch point name */
protected string $patch_point = '';
2023-03-18 17:32:21 +08:00
/**
* Convert libraries to class
2023-03-18 17:32:21 +08:00
*
2024-01-07 00:39:36 +08:00
* @param array<string> $sorted_libraries Libraries to build (if not empty, must sort first)
2023-03-18 17:32:21 +08:00
* @throws FileSystemException
2023-04-30 12:42:19 +08:00
* @throws RuntimeException
* @throws WrongUsageException
* @internal
2023-03-18 17:32:21 +08:00
*/
abstract public function proveLibs(array $sorted_libraries);
/**
2024-07-07 20:45:18 +08:00
* Set-Up libraries
*
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
*/
2024-07-07 20:45:18 +08:00
public function setupLibs(): void
{
// build all libs
foreach ($this->libs as $lib) {
2024-07-07 20:45:18 +08:00
match ($lib->setup($this->getOption('rebuild', false))) {
LIB_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] setup success'),
LIB_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
LIB_STATUS_BUILD_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
LIB_STATUS_INSTALL_FAILED => logger()->error('lib [' . $lib::NAME . '] install failed'),
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
};
}
}
2023-03-18 17:32:21 +08:00
/**
* 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.
*
* @internal
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
* @internal
2023-03-18 17:32:21 +08:00
*/
public function proveExts(array $extensions, bool $skip_check_deps = false): void
2023-03-18 17:32:21 +08:00
{
2023-04-15 18:46:21 +08:00
CustomExt::loadCustomExt();
2024-01-03 15:57:05 +08:00
$this->emitPatchPoint('before-php-extract');
2024-02-18 13:54:06 +08:00
SourceManager::initSource(sources: ['php-src']);
2024-01-03 15:57:05 +08:00
$this->emitPatchPoint('after-php-extract');
2023-04-30 12:42:19 +08:00
if ($this->getPHPVersionID() >= 80000) {
2024-01-03 15:57:05 +08:00
$this->emitPatchPoint('before-micro-extract');
2024-02-18 13:54:06 +08:00
SourceManager::initSource(sources: ['micro']);
2024-01-03 15:57:05 +08:00
$this->emitPatchPoint('after-micro-extract');
2023-04-30 12:42:19 +08:00
}
2024-01-03 15:57:05 +08:00
$this->emitPatchPoint('before-exts-extract');
2024-02-18 13:54:06 +08:00
SourceManager::initSource(exts: $extensions);
2024-01-03 15:57:05 +08:00
$this->emitPatchPoint('after-exts-extract');
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);
}
if ($skip_check_deps) {
return;
}
2023-03-18 17:32:21 +08:00
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) {
2024-01-10 21:08:25 +08:00
logger()->info($ext->getName() . ' is using ' . $ext->getConfigureArg());
2023-04-15 18:46:21 +08:00
$ret[] = trim($ext->getConfigureArg());
2023-03-18 17:32:21 +08:00
}
Feature perfect swoole extension config (#297) * improve swoole static build config * improve swoole static build config * improve swoole static build config * improve swoole static build config * improve swoole static build config * add cares config * update swoole depend config * update swoole depend config * update cares build config * update workflow tests.yaml config * fix setup-runtime * test with clang build * test with clang build * update cares build config * test * test * test * test * test * test * test * test * test * test * test * test * test * test * update cares license * test build * test build * test build * test build * test add enable libpq * test add enable libpq * test add enable libpq * test add enable libpq * test add enable libpq * test add enable libpq * test add enable libpq * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * update * update * update * update * update * update * update * update * update * compatible old * fix code format * fix code format * add swoole test case * add swoole test case * add phpstan ignore error * add phpstan ignore error * add phpstan ignore error * add phpstan ignore error * add phpstan ignore error * update phpstan.neon * update swoole extension test case * update swoole test case * adjust config order and depends * revert LinuxBuilder * remove swoole.phpt * re-adjust swoole args * update test-extensions and some PHPDoc * revert: debian and alpine clang doctor install * revert: MacOSBuilder * fix: extract hook for archive not working * revert: build tests * use addon mode to swoole database hook * add hook tests * test minimal * test minimal * sort config --------- Co-authored-by: crazywhalecc <jesse2061@outlook.com>
2024-01-03 10:31:21 +08:00
logger()->debug('Using configure: ' . implode(' ', $ret));
2023-03-18 17:32:21 +08:00
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
}
public function getPHPVersion(): string
{
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');
}
$file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h');
if (preg_match('/PHP_VERSION "(.*)"/', $file, $match) !== 0) {
return $match[1];
}
throw new RuntimeException('PHP version file format is malformed, please remove it and download again');
}
/**
* Get PHP version from archive file name.
*
* @param null|string $file php-*.*.*.tar.gz filename, read from lockfile if empty
*/
public function getPHPVersionFromArchive(?string $file = null): false|string
{
if ($file === null) {
$lock = file_exists(DOWNLOAD_PATH . '/.lock.json') ? file_get_contents(DOWNLOAD_PATH . '/.lock.json') : false;
if ($lock === false) {
return false;
}
$lock = json_decode($lock, true);
$file = $lock['php-src']['filename'] ?? null;
if ($file === null) {
return false;
}
}
if (preg_match('/php-(\d+\.\d+\.\d+)/', $file, $match)) {
return $match[1];
}
return false;
}
public function getMicroVersion(): false|string
{
$file = FileSystem::convertPath(SOURCE_PATH . '/php-src/sapi/micro/php_micro.h');
if (!file_exists($file)) {
return false;
}
$content = file_get_contents($file);
$ver = '';
preg_match('/#define PHP_MICRO_VER_MAJ (\d)/m', $content, $match);
$ver .= $match[1] . '.';
preg_match('/#define PHP_MICRO_VER_MIN (\d)/m', $content, $match);
$ver .= $match[1] . '.';
preg_match('/#define PHP_MICRO_VER_PAT (\d)/m', $content, $match);
$ver .= $match[1];
return $ver;
}
/**
* 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);
}
2024-01-03 15:57:05 +08:00
/**
* Get builder patch point name.
*/
public function getPatchPoint(): string
{
return $this->patch_point;
}
/**
* Validate libs and exts can be compiled successfully in current environment
*/
public function validateLibsAndExts(): void
{
foreach ($this->libs as $lib) {
$lib->validate();
}
foreach ($this->exts as $ext) {
$ext->validate();
}
}
2024-01-03 15:57:05 +08:00
public function emitPatchPoint(string $point_name): void
{
$this->patch_point = $point_name;
if (($patches = $this->getOption('with-added-patch', [])) === []) {
return;
}
foreach ($patches as $patch) {
try {
if (!file_exists($patch)) {
throw new RuntimeException("Additional patch script file {$patch} not found!");
}
logger()->debug('Running additional patch script: ' . $patch);
require $patch;
} catch (\Throwable $e) {
logger()->critical('Patch script ' . $patch . ' failed to run.');
if ($this->getOption('debug')) {
ExceptionHandler::getInstance()->handle($e);
} else {
logger()->critical('Please check with --debug option to see more details.');
}
}
}
}
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?'
);
}
}
2024-01-10 21:08:25 +08:00
/**
* Generate micro extension test php code.
*/
protected function generateMicroExtTests(): string
{
$php = "<?php\n\necho '[micro-test-start]' . PHP_EOL;\n";
foreach ($this->getExts() as $ext) {
$ext_name = $ext->getDistName();
2024-01-29 10:04:21 +08:00
if (!empty($ext_name)) {
$php .= "echo 'Running micro with {$ext_name} test' . PHP_EOL;\n";
$php .= "assert(extension_loaded('{$ext_name}'));\n\n";
}
2024-01-10 21:08:25 +08:00
}
$php .= "echo '[micro-test-end]';\n";
return $php;
}
protected function getMicroTestTasks(): array
{
return [
'micro_ext_test' => [
'content' => ($this->getOption('without-micro-ext-test') ? '<?php echo "[micro-test-start][micro-test-end]";' : $this->generateMicroExtTests()),
'conditions' => [
// program success
function ($ret) { return $ret === 0; },
// program returns expected output
function ($ret, $out) {
$raw_out = trim(implode('', $out));
return str_starts_with($raw_out, '[micro-test-start]') && str_ends_with($raw_out, '[micro-test-end]');
},
],
],
'micro_zend_bug_test' => [
'content' => ($this->getOption('without-micro-ext-test') ? '<?php echo "hello";' : file_get_contents(ROOT_DIR . '/src/globals/common-tests/micro_zend_mm_heap_corrupted.txt')),
'conditions' => [
// program success
function ($ret) { return $ret === 0; },
],
],
];
}
2023-03-18 17:32:21 +08:00
}