2023-03-18 17:32:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\builder;
|
|
|
|
|
|
2025-08-06 20:45:16 +08:00
|
|
|
use SPC\exception\BuildFailureException;
|
2024-12-05 15:48:09 +08:00
|
|
|
use SPC\exception\InterruptException;
|
2023-04-30 12:42:19 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
2023-03-18 17:32:21 +08:00
|
|
|
use SPC\store\Config;
|
2024-06-03 23:16:15 +08:00
|
|
|
use SPC\store\FileSystem;
|
2025-06-18 14:05:43 +08:00
|
|
|
use SPC\store\LockFile;
|
2024-02-18 13:54:06 +08:00
|
|
|
use SPC\store\SourceManager;
|
2025-06-28 17:06:51 +08:00
|
|
|
use SPC\store\SourcePatcher;
|
2025-08-06 20:45:16 +08:00
|
|
|
use SPC\util\AttributeMapper;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
abstract class BuilderBase
|
|
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @var int Concurrency */
|
2023-03-18 17:32:21 +08:00
|
|
|
public int $concurrency = 1;
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @var array<string, LibraryBase> libraries */
|
2023-03-18 17:32:21 +08:00
|
|
|
protected array $libs = [];
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @var array<string, Extension> extensions */
|
2023-03-18 17:32:21 +08:00
|
|
|
protected array $exts = [];
|
|
|
|
|
|
2024-12-10 23:08:01 +08:00
|
|
|
/** @var array<int, string> extension names */
|
|
|
|
|
protected array $ext_list = [];
|
|
|
|
|
|
|
|
|
|
/** @var array<int, string> library names */
|
|
|
|
|
protected array $lib_list = [];
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @var bool compile libs only (just mark it) */
|
2023-03-18 17:32:21 +08:00
|
|
|
protected bool $libs_only = false;
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @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
|
|
|
/**
|
2024-05-16 13:01:11 +08:00
|
|
|
* Convert libraries to class
|
2023-03-18 17:32:21 +08:00
|
|
|
*
|
2025-08-06 20:35:52 +08:00
|
|
|
* @param array<string> $sorted_libraries Libraries to build (if not empty, must sort first)
|
|
|
|
|
*
|
2024-01-10 11:10:40 +08:00
|
|
|
* @internal
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
2024-05-16 13:01:11 +08:00
|
|
|
abstract public function proveLibs(array $sorted_libraries);
|
|
|
|
|
|
|
|
|
|
/**
|
2024-07-07 20:45:18 +08:00
|
|
|
* Set-Up libraries
|
2024-05-16 13:01:11 +08:00
|
|
|
*/
|
2024-07-07 20:45:18 +08:00
|
|
|
public function setupLibs(): void
|
2024-05-16 13:01:11 +08:00
|
|
|
{
|
|
|
|
|
// build all libs
|
|
|
|
|
foreach ($this->libs as $lib) {
|
2024-07-09 00:43:38 +08:00
|
|
|
$starttime = microtime(true);
|
2025-07-22 20:41:35 +08:00
|
|
|
$status = $lib->setup($this->getOption('rebuild', false));
|
|
|
|
|
match ($status) {
|
2024-07-09 00:43:38 +08:00
|
|
|
LIB_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] setup success, took ' . round(microtime(true) - $starttime, 2) . ' s'),
|
2024-07-07 20:45:18 +08:00
|
|
|
LIB_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
|
|
|
|
|
LIB_STATUS_INSTALL_FAILED => logger()->error('lib [' . $lib::NAME . '] install failed'),
|
2024-05-16 13:01:11 +08:00
|
|
|
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
|
|
|
|
|
};
|
2025-07-22 20:41:35 +08:00
|
|
|
if (in_array($status, [LIB_STATUS_BUILD_FAILED, LIB_STATUS_INSTALL_FAILED])) {
|
2025-08-06 20:45:16 +08:00
|
|
|
throw new BuildFailureException('Library [' . $lib::NAME . '] setup failed.');
|
2025-07-22 20:41:35 +08:00
|
|
|
}
|
2024-05-16 13:01:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Add library to build.
|
2023-03-18 17:32:21 +08:00
|
|
|
*
|
2023-08-20 19:51:45 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Get library object by name.
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
|
|
|
|
public function getLib(string $name): ?LibraryBase
|
|
|
|
|
{
|
|
|
|
|
return $this->libs[$name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 00:37:28 +08:00
|
|
|
/**
|
|
|
|
|
* Get all library objects.
|
|
|
|
|
*
|
|
|
|
|
* @return LibraryBase[]
|
|
|
|
|
*/
|
|
|
|
|
public function getLibs(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->libs;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
/**
|
2023-08-20 19:51:45 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* 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
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Get all extension objects.
|
2023-07-24 23:49:52 +08:00
|
|
|
*
|
|
|
|
|
* @return Extension[]
|
|
|
|
|
*/
|
2025-03-27 09:36:46 +07:00
|
|
|
public function getExts(bool $including_shared = true): array
|
2023-07-24 23:49:52 +08:00
|
|
|
{
|
2025-03-27 09:36:46 +07:00
|
|
|
if ($including_shared) {
|
2025-03-24 23:50:12 +08:00
|
|
|
return $this->exts;
|
|
|
|
|
}
|
2025-05-23 12:09:57 +07:00
|
|
|
return array_filter($this->exts, fn ($ext) => $ext->isBuildStatic());
|
2023-07-24 23:49:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-23 00:37:28 +08:00
|
|
|
* Check if there is a cpp extensions or libraries.
|
2023-07-24 23:49:52 +08:00
|
|
|
*/
|
2023-10-23 00:37:28 +08:00
|
|
|
public function hasCpp(): bool
|
2023-07-24 23:49:52 +08:00
|
|
|
{
|
|
|
|
|
// judge cpp-extension
|
2025-03-24 23:50:12 +08:00
|
|
|
$exts = array_keys($this->getExts(false));
|
2023-07-24 23:49:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2023-10-23 00:37:28 +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
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Set libs only mode.
|
2024-01-10 11:10:40 +08:00
|
|
|
*
|
|
|
|
|
* @internal
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
|
|
|
|
public function setLibsOnly(bool $status = true): void
|
|
|
|
|
{
|
|
|
|
|
$this->libs_only = $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* 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
|
|
|
*
|
2024-01-10 11:10:40 +08:00
|
|
|
* @internal
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
2025-04-18 14:50:58 +08:00
|
|
|
public function proveExts(array $static_extensions, array $shared_extensions = [], bool $skip_check_deps = false, bool $skip_extract = false): void
|
2023-03-18 17:32:21 +08:00
|
|
|
{
|
2025-03-24 23:50:12 +08:00
|
|
|
// judge ext
|
2025-03-27 09:36:46 +07:00
|
|
|
foreach ($static_extensions as $ext) {
|
|
|
|
|
// if extension does not support static build, throw exception
|
|
|
|
|
if (!in_array('static', Config::getExtTarget($ext))) {
|
|
|
|
|
throw new WrongUsageException('Extension [' . $ext . '] does not support static build!');
|
2025-03-24 23:50:12 +08:00
|
|
|
}
|
2025-03-27 09:36:46 +07:00
|
|
|
}
|
|
|
|
|
foreach ($shared_extensions as $ext) {
|
|
|
|
|
// if extension does not support shared build, throw exception
|
|
|
|
|
if (!in_array('shared', Config::getExtTarget($ext)) && !in_array($ext, $shared_extensions)) {
|
|
|
|
|
throw new WrongUsageException('Extension [' . $ext . '] does not support shared build!');
|
2025-03-24 23:50:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-18 14:50:58 +08:00
|
|
|
if (!$skip_extract) {
|
|
|
|
|
$this->emitPatchPoint('before-php-extract');
|
|
|
|
|
SourceManager::initSource(sources: ['php-src'], source_only: true);
|
|
|
|
|
$this->emitPatchPoint('after-php-extract');
|
|
|
|
|
if ($this->getPHPVersionID() >= 80000) {
|
|
|
|
|
$this->emitPatchPoint('before-micro-extract');
|
|
|
|
|
SourceManager::initSource(sources: ['micro'], source_only: true);
|
|
|
|
|
$this->emitPatchPoint('after-micro-extract');
|
|
|
|
|
}
|
|
|
|
|
$this->emitPatchPoint('before-exts-extract');
|
|
|
|
|
SourceManager::initSource(exts: [...$static_extensions, ...$shared_extensions]);
|
|
|
|
|
$this->emitPatchPoint('after-exts-extract');
|
2025-06-28 17:06:51 +08:00
|
|
|
// patch micro
|
|
|
|
|
SourcePatcher::patchMicro();
|
2023-04-30 12:42:19 +08:00
|
|
|
}
|
2025-04-18 14:50:58 +08:00
|
|
|
|
2025-03-27 09:36:46 +07:00
|
|
|
foreach ([...$static_extensions, ...$shared_extensions] as $extension) {
|
2025-08-06 20:35:52 +08:00
|
|
|
$class = AttributeMapper::getExtensionClassByName($extension) ?? Extension::class;
|
2025-03-24 23:50:12 +08:00
|
|
|
/** @var Extension $ext */
|
2023-04-15 18:46:21 +08:00
|
|
|
$ext = new $class($extension, $this);
|
2025-03-27 09:36:46 +07:00
|
|
|
if (in_array($extension, $static_extensions)) {
|
2025-03-24 23:50:12 +08:00
|
|
|
$ext->setBuildStatic();
|
|
|
|
|
}
|
2025-03-27 09:36:46 +07:00
|
|
|
if (in_array($extension, $shared_extensions)) {
|
|
|
|
|
$ext->setBuildShared();
|
|
|
|
|
}
|
2025-03-26 12:38:59 +08:00
|
|
|
$this->addExt($ext);
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-30 22:37:01 +08:00
|
|
|
if ($skip_check_deps) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 23:50:12 +08:00
|
|
|
foreach ($this->getExts() as $ext) {
|
2023-03-18 17:32:21 +08:00
|
|
|
$ext->checkDependency();
|
|
|
|
|
}
|
2025-03-27 09:36:46 +07:00
|
|
|
$this->ext_list = [...$static_extensions, ...$shared_extensions];
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Start to build PHP
|
2023-03-18 17:32:21 +08:00
|
|
|
*
|
2023-08-20 19:51:45 +08:00
|
|
|
* @param int $build_target Build target, see BUILD_TARGET_*
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
2023-08-20 19:51:45 +08:00
|
|
|
abstract public function buildPHP(int $build_target = BUILD_TARGET_NONE);
|
2023-03-18 17:32:21 +08:00
|
|
|
|
2025-05-21 18:35:48 +07:00
|
|
|
/**
|
|
|
|
|
* Test PHP
|
|
|
|
|
*/
|
|
|
|
|
abstract public function testPHP(int $build_target = BUILD_TARGET_NONE);
|
|
|
|
|
|
2025-08-11 10:48:48 +08:00
|
|
|
/**
|
|
|
|
|
* Build shared extensions.
|
|
|
|
|
*/
|
2025-03-25 16:13:41 +08:00
|
|
|
public function buildSharedExts(): void
|
2025-03-24 23:50:12 +08:00
|
|
|
{
|
2025-05-20 09:14:13 +07:00
|
|
|
$lines = file(BUILD_BIN_PATH . '/php-config');
|
|
|
|
|
$extension_dir_line = null;
|
|
|
|
|
foreach ($lines as $key => $value) {
|
|
|
|
|
if (str_starts_with($value, 'extension_dir=')) {
|
|
|
|
|
$lines[$key] = 'extension_dir="' . BUILD_MODULES_PATH . '"' . PHP_EOL;
|
|
|
|
|
$extension_dir_line = $value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file_put_contents(BUILD_BIN_PATH . '/php-config', implode('', $lines));
|
2025-07-18 20:45:46 +07:00
|
|
|
FileSystem::replaceFileStr(BUILD_LIB_PATH . '/php/build/phpize.m4', 'test "[$]$1" = "no" && $1=yes', '# test "[$]$1" = "no" && $1=yes');
|
2025-05-18 12:53:49 +07:00
|
|
|
FileSystem::createDir(BUILD_MODULES_PATH);
|
2025-05-21 17:57:53 +07:00
|
|
|
try {
|
|
|
|
|
foreach ($this->getExts() as $ext) {
|
|
|
|
|
if (!$ext->isBuildShared()) {
|
2025-05-21 13:19:51 +07:00
|
|
|
continue;
|
|
|
|
|
}
|
2025-05-21 17:57:53 +07:00
|
|
|
$ext->buildShared();
|
2025-05-21 12:01:00 +07:00
|
|
|
}
|
2025-08-06 20:45:16 +08:00
|
|
|
} finally {
|
2025-05-21 17:57:53 +07:00
|
|
|
FileSystem::replaceFileLineContainsString(BUILD_BIN_PATH . '/php-config', 'extension_dir=', $extension_dir_line);
|
2025-03-24 23:50:12 +08:00
|
|
|
}
|
2025-05-20 09:14:13 +07:00
|
|
|
FileSystem::replaceFileLineContainsString(BUILD_BIN_PATH . '/php-config', 'extension_dir=', $extension_dir_line);
|
2025-07-18 20:45:46 +07:00
|
|
|
FileSystem::replaceFileStr(BUILD_LIB_PATH . '/php/build/phpize.m4', '# test "[$]$1" = "no" && $1=yes', 'test "[$]$1" = "no" && $1=yes');
|
2025-03-24 23:50:12 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Generate extension enable arguments for configure.
|
|
|
|
|
* e.g. --enable-mbstring
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
2025-03-24 23:50:12 +08:00
|
|
|
public function makeStaticExtensionArgs(): string
|
2023-03-18 17:32:21 +08:00
|
|
|
{
|
|
|
|
|
$ret = [];
|
2025-05-21 12:01:00 +07:00
|
|
|
foreach ($this->getExts() as $ext) {
|
|
|
|
|
$arg = $ext->getConfigureArg();
|
2025-05-25 15:52:11 +07:00
|
|
|
if ($ext->isBuildShared() && !$ext->isBuildStatic()) {
|
2025-05-22 12:27:01 +07:00
|
|
|
if (
|
|
|
|
|
(Config::getExt($ext->getName(), 'type') === 'builtin' &&
|
|
|
|
|
!file_exists(SOURCE_PATH . '/php-src/ext/' . $ext->getName() . '/config.m4')) ||
|
|
|
|
|
Config::getExt($ext->getName(), 'build-with-php') === true
|
|
|
|
|
) {
|
2025-05-21 12:01:00 +07:00
|
|
|
$arg = $ext->getConfigureArg(true);
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
logger()->info($ext->getName() . ' is using ' . $arg);
|
|
|
|
|
$ret[] = trim($arg);
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* 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
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Get PHP Version ID from php-src/main/php_version.h
|
2023-04-03 20:47:24 +08:00
|
|
|
*/
|
|
|
|
|
public function getPHPVersionID(): int
|
|
|
|
|
{
|
2023-09-30 08:56:37 +02:00
|
|
|
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');
|
2023-09-30 08:56:37 +02:00
|
|
|
if (preg_match('/PHP_VERSION_ID (\d+)/', $file, $match) !== 0) {
|
|
|
|
|
return intval($match[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-06 20:45:16 +08:00
|
|
|
throw new WrongUsageException('PHP version file format is malformed, please remove "./source/php-src" dir and download/extract again');
|
2023-04-03 20:47:24 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 21:11:38 +08:00
|
|
|
public function getPHPVersion(bool $exception_on_failure = true): string
|
2023-12-15 01:31:01 +08:00
|
|
|
{
|
|
|
|
|
if (!file_exists(SOURCE_PATH . '/php-src/main/php_version.h')) {
|
2025-07-15 21:11:38 +08:00
|
|
|
if (!$exception_on_failure) {
|
|
|
|
|
return 'unknown';
|
|
|
|
|
}
|
2023-12-15 01:31:01 +08:00
|
|
|
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];
|
|
|
|
|
}
|
2025-07-15 21:11:38 +08:00
|
|
|
if (!$exception_on_failure) {
|
|
|
|
|
return 'unknown';
|
|
|
|
|
}
|
2025-08-06 20:45:16 +08:00
|
|
|
throw new WrongUsageException('PHP version file format is malformed, please remove it and download again');
|
2023-12-15 01:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-16 18:56:59 +08:00
|
|
|
/**
|
|
|
|
|
* 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) {
|
2025-06-18 14:05:43 +08:00
|
|
|
$lock = LockFile::get('php-src');
|
|
|
|
|
if ($lock === null) {
|
2024-02-16 18:56:59 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-06-18 14:05:43 +08:00
|
|
|
$file = LockFile::getLockFullPath($lock);
|
2024-02-16 18:56:59 +08:00
|
|
|
}
|
2025-07-15 21:11:38 +08:00
|
|
|
if (preg_match('/php-(\d+\.\d+\.\d+(?:RC\d+|alpha\d+|beta\d+)?)\.tar\.(?:gz|bz2|xz)/', $file, $match)) {
|
2024-02-16 18:56:59 +08:00
|
|
|
return $match[1];
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 23:16:15 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:51:45 +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';
|
|
|
|
|
}
|
2023-08-21 09:30:46 +02:00
|
|
|
if (($type & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) {
|
|
|
|
|
$ls[] = 'embed';
|
|
|
|
|
}
|
2025-06-18 20:54:54 +08:00
|
|
|
if (($type & BUILD_TARGET_FRANKENPHP) === BUILD_TARGET_FRANKENPHP) {
|
|
|
|
|
$ls[] = 'frankenphp';
|
|
|
|
|
}
|
2023-04-23 20:31:58 +08:00
|
|
|
return implode(', ', $ls);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
$this->options[$key] = $value;
|
2023-05-10 21:59:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-24 12:21:36 +02: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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 13:01:11 +08:00
|
|
|
/**
|
|
|
|
|
* Validate libs and exts can be compiled successfully in current environment
|
|
|
|
|
*/
|
|
|
|
|
public function validateLibsAndExts(): void
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->libs as $lib) {
|
|
|
|
|
$lib->validate();
|
|
|
|
|
}
|
2025-03-24 23:50:12 +08:00
|
|
|
foreach ($this->getExts() as $ext) {
|
2024-05-16 13:01:11 +08:00
|
|
|
$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)) {
|
2025-08-06 20:45:16 +08:00
|
|
|
throw new WrongUsageException("Additional patch script file {$patch} not found!");
|
2024-01-03 15:57:05 +08:00
|
|
|
}
|
|
|
|
|
logger()->debug('Running additional patch script: ' . $patch);
|
|
|
|
|
require $patch;
|
2024-12-05 15:48:09 +08:00
|
|
|
} catch (InterruptException $e) {
|
|
|
|
|
if ($e->getCode() === 0) {
|
|
|
|
|
logger()->notice('Patch script ' . $patch . ' interrupted' . ($e->getMessage() ? (': ' . $e->getMessage()) : '.'));
|
|
|
|
|
} else {
|
|
|
|
|
logger()->error('Patch script ' . $patch . ' interrupted with error code [' . $e->getCode() . ']' . ($e->getMessage() ? (': ' . $e->getMessage()) : '.'));
|
|
|
|
|
}
|
|
|
|
|
exit($e->getCode());
|
2024-01-03 15:57:05 +08:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
logger()->critical('Patch script ' . $patch . ' failed to run.');
|
2024-12-05 15:52:59 +08:00
|
|
|
throw $e;
|
2024-01-03 15:57:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 20:54:54 +08:00
|
|
|
public function checkBeforeBuildPHP(int $rule): void
|
|
|
|
|
{
|
|
|
|
|
if (($rule & BUILD_TARGET_FRANKENPHP) === BUILD_TARGET_FRANKENPHP) {
|
2025-06-19 10:08:51 +07:00
|
|
|
if (!$this->getOption('enable-zts')) {
|
|
|
|
|
throw new WrongUsageException('FrankenPHP SAPI requires ZTS enabled PHP, build with `--enable-zts`!');
|
|
|
|
|
}
|
|
|
|
|
// frankenphp doesn't support windows, BSD is currently not supported by static-php-cli
|
2025-06-18 20:54:54 +08:00
|
|
|
if (!in_array(PHP_OS_FAMILY, ['Linux', 'Darwin'])) {
|
|
|
|
|
throw new WrongUsageException('FrankenPHP SAPI is only available on Linux and macOS!');
|
|
|
|
|
}
|
2025-06-19 12:07:22 +07:00
|
|
|
// frankenphp needs package go-xcaddy installed
|
|
|
|
|
$pkg_dir = PKG_ROOT_PATH . '/go-xcaddy-' . arch2gnu(php_uname('m')) . '-' . osfamily2shortname();
|
2025-06-18 20:54:54 +08:00
|
|
|
if (!file_exists("{$pkg_dir}/bin/go") || !file_exists("{$pkg_dir}/bin/xcaddy")) {
|
|
|
|
|
global $argv;
|
2025-06-19 12:07:22 +07:00
|
|
|
throw new WrongUsageException("FrankenPHP SAPI requires the go-xcaddy package, please install it first: {$argv[0]} install-pkg go-xcaddy");
|
2025-06-18 20:54:54 +08:00
|
|
|
}
|
2025-06-19 10:08:51 +07:00
|
|
|
// frankenphp needs libxml2 lib on macos, see: https://github.com/php/frankenphp/blob/main/frankenphp.go#L17
|
2025-06-18 21:56:36 +08:00
|
|
|
if (PHP_OS_FAMILY === 'Darwin' && !$this->getLib('libxml2')) {
|
2025-06-19 10:08:51 +07:00
|
|
|
throw new WrongUsageException('FrankenPHP SAPI for macOS requires libxml2 library, please include the `xml` extension in your build.');
|
2025-06-18 20:54:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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";
|
|
|
|
|
|
2025-03-24 23:50:12 +08:00
|
|
|
foreach ($this->getExts(false) as $ext) {
|
2024-01-10 21:08:25 +08:00
|
|
|
$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;
|
|
|
|
|
}
|
2024-06-03 23:16:15 +08:00
|
|
|
|
|
|
|
|
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' => [
|
2024-06-20 14:46:08 +08:00
|
|
|
// program success
|
2024-06-03 23:16:15 +08:00
|
|
|
function ($ret) { return $ret === 0; },
|
2024-06-20 14:46:08 +08:00
|
|
|
// program returns expected output
|
2024-06-03 23:16:15 +08:00
|
|
|
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' => [
|
2024-06-20 14:46:08 +08:00
|
|
|
// program success
|
2024-06-03 23:16:15 +08:00
|
|
|
function ($ret) { return $ret === 0; },
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|