Add ext-opcache

This commit is contained in:
crazywhalecc 2026-03-12 15:08:11 +08:00
parent 6f372a74a2
commit 371a1af572
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
2 changed files with 83 additions and 0 deletions

View File

@ -140,6 +140,13 @@ ext-odbc:
- unixodbc
php-extension:
arg-type@unix: '--with-unixODBC@shared_path_suffix@'
ext-opcache:
type: php-extension
php-extension:
arg-type@unix: custom
arg-type@windows: enable
zend-extension: true
display-name: 'Zend Opcache'
ext-openssl:
type: php-extension
depends:

View File

@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Package\Extension;
use Package\Target\php;
use StaticPHP\Attribute\Package\BeforeStage;
use StaticPHP\Attribute\Package\CustomPhpConfigureArg;
use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Attribute\Package\Validate;
use StaticPHP\Attribute\PatchDescription;
use StaticPHP\Exception\WrongUsageException;
use StaticPHP\Package\PackageBuilder;
use StaticPHP\Package\PackageInstaller;
use StaticPHP\Package\PhpExtensionPackage;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Util\SourcePatcher;
#[Extension('opcache')]
class opcache extends PhpExtensionPackage
{
#[Validate]
public function validate(): void
{
if (php::getPHPVersionID() < 80000 && getenv('SPC_SKIP_PHP_VERSION_CHECK') !== 'yes') {
throw new WrongUsageException('Statically compiled PHP with Zend Opcache only available for PHP >= 8.0 !');
}
}
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-opcache')]
#[PatchDescription('Fix static opcache build for PHP 8.2.0 to 8.4.x')]
public function patchBeforeBuildconf(PackageInstaller $installer): bool
{
$version = php::getPHPVersion();
$php_src = $installer->getTargetPackage('php')->getSourceDir();
if (file_exists("{$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($version, '8.2.0', '>=') && version_compare($version, '8.2.23', '<')) {
SourcePatcher::patchFile('spc_fix_static_opcache_before_80222.patch', $php_src);
}
// if 8.3.0 <= PHP_VERSION < 8.3.11, we need to patch from legacy patch file
elseif (version_compare($version, '8.3.0', '>=') && version_compare($version, '8.3.11', '<')) {
SourcePatcher::patchFile('spc_fix_static_opcache_before_80310.patch', $php_src);
}
// if 8.3.12 <= PHP_VERSION < 8.5.0-dev, we need to patch from legacy patch file
elseif (version_compare($version, '8.5.0-dev', '<')) {
SourcePatcher::patchPhpSrc(items: ['static_opcache']);
}
// PHP 8.5.0-dev and later supports static opcache without patching
else {
return false;
}
return file_put_contents($php_src . '/.opcache_patched', '1') !== false;
}
#[CustomPhpConfigureArg('Darwin')]
#[CustomPhpConfigureArg('Linux')]
public function getUnixConfigureArg(bool $shared, PackageBuilder $builder): string
{
$phpVersionID = php::getPHPVersionID();
$opcache_jit = ' --enable-opcache-jit';
if ((SystemTarget::getTargetOS() === 'Linux' &&
SystemTarget::getLibc() === 'musl' &&
$builder->getOption('enable-zts') &&
SystemTarget::getTargetArch() === 'x86_64' &&
$phpVersionID < 80500) ||
$builder->getOption('disable-opcache-jit')
) {
$opcache_jit = ' --disable-opcache-jit';
}
return '--enable-opcache' . ($shared ? '=shared' : '') . $opcache_jit;
}
}