66 lines
2.2 KiB
PHP
Raw Normal View History

2023-05-17 15:24:45 +08:00
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
2023-12-10 18:27:52 +08:00
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\store\SourcePatcher;
2023-05-17 15:24:45 +08:00
use SPC\util\CustomExt;
#[CustomExt('opcache')]
class opcache extends Extension
{
2023-12-10 18:27:52 +08:00
/**
* @throws WrongUsageException
* @throws RuntimeException
*/
public function validate(): void
2023-12-10 18:27:52 +08:00
{
if ($this->builder->getPHPVersionID() < 80000 && getenv('SPC_SKIP_PHP_VERSION_CHECK') !== 'yes') {
2023-12-10 18:27:52 +08:00
throw new WrongUsageException('Statically compiled PHP with Zend Opcache only available for PHP >= 8.0 !');
}
}
public function patchBeforeBuildconf(): bool
{
if (file_exists(SOURCE_PATH . '/php-src/.opcache_patched')) {
return false;
}
// if 8.2.0 <= PHP_VERSION < 8.2.23, we need to patch from legacy patch file
if (version_compare($this->builder->getPHPVersion(), '8.2.0', '>=') && version_compare($this->builder->getPHPVersion(), '8.2.23', '<')) {
SourcePatcher::patchFile('spc_fix_static_opcache_before_80222.patch', SOURCE_PATH . '/php-src');
}
// if 8.3.0 <= PHP_VERSION < 8.3.11, we need to patch from legacy patch file
elseif (version_compare($this->builder->getPHPVersion(), '8.3.0', '>=') && version_compare($this->builder->getPHPVersion(), '8.3.11', '<')) {
SourcePatcher::patchFile('spc_fix_static_opcache_before_80310.patch', SOURCE_PATH . '/php-src');
} else {
SourcePatcher::patchMicro(items: ['static_opcache']);
}
return file_put_contents(SOURCE_PATH . '/php-src/.opcache_patched', '1') !== false;
}
public function getUnixConfigureArg(bool $shared = false): string
{
2023-12-10 18:27:52 +08:00
return '--enable-opcache';
}
2023-05-17 15:24:45 +08:00
public function getDistName(): string
{
return 'Zend Opcache';
}
public function patchBeforeMake(): bool
{
if (!str_contains(getenv('CC'), 'zig')) {
return false;
}
// opcache requires -lunwind, the equivalent to -lgcc_s that gcc automatically links
$extra_libs = trim(getenv('SPC_EXTRA_LIBS') . ' -lunwind');
f_putenv('SPC_EXTRA_LIBS=' . $extra_libs);
return true;
}
2023-05-17 15:24:45 +08:00
}