From a24fae7a55e995543ee8a19cf004414920430419 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Wed, 18 Mar 2026 12:07:39 +0800 Subject: [PATCH] Add ext-swoole --- config/pkg/ext/ext-swoole.yml | 72 +++++++++++++++ src/Package/Extension/swoole.php | 150 +++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 config/pkg/ext/ext-swoole.yml create mode 100644 src/Package/Extension/swoole.php diff --git a/config/pkg/ext/ext-swoole.yml b/config/pkg/ext/ext-swoole.yml new file mode 100644 index 00000000..b6499e85 --- /dev/null +++ b/config/pkg/ext/ext-swoole.yml @@ -0,0 +1,72 @@ +ext-swoole: + type: php-extension + artifact: + source: + type: ghtar + repo: swoole/swoole-src + extract: php-src/ext/swoole + match: v6\.+ + prefer-stable: true + metadata: + license-files: [LICENSE] + license: Apache-2.0 + depends: + - libcares + - brotli + - nghttp2 + - zlib + - ext-openssl + - ext-curl + suggests: + - zstd + - ext-sockets + - ext-swoole-hook-pgsql + - ext-swoole-hook-mysql + - ext-swoole-hook-sqlite + - ext-swoole-hook-odbc + suggests@linux: + - zstd + - liburing + - ext-sockets + - ext-swoole-hook-pgsql + - ext-swoole-hook-mysql + - ext-swoole-hook-sqlite + - ext-swoole-hook-odbc + lang: cpp + php-extension: + arg-type: custom +ext-swoole-hook-mysql: + type: php-extension + depends: + - ext-mysqlnd + - ext-pdo + - ext-pdo_mysql + suggests: + - ext-mysqli + php-extension: + arg-type: none + display-name: swoole +ext-swoole-hook-odbc: + type: php-extension + depends: + - ext-pdo + - unixodbc + php-extension: + arg-type: none + display-name: swoole +ext-swoole-hook-pgsql: + type: php-extension + depends: + - ext-pgsql + - ext-pdo + php-extension: + arg-type: none + display-name: swoole +ext-swoole-hook-sqlite: + type: php-extension + depends: + - ext-sqlite3 + - ext-pdo + php-extension: + arg-type: none + display-name: swoole diff --git a/src/Package/Extension/swoole.php b/src/Package/Extension/swoole.php new file mode 100644 index 00000000..c269ba54 --- /dev/null +++ b/src/Package/Extension/swoole.php @@ -0,0 +1,150 @@ +getPhpExtensionPackage('swoole-hook-odbc') && $installer->getPhpExtensionPackage('pdo_odbc')?->isBuildStatic()) { + throw new WrongUsageException('swoole-hook-odbc provides pdo_odbc, if you enable odbc hook for swoole, you must remove pdo_odbc extension.'); + } + // swoole-hook-pgsql conflicts with pdo_pgsql + if ($installer->getPhpExtensionPackage('swoole-hook-pgsql') && $installer->getPhpExtensionPackage('pdo_pgsql')?->isBuildStatic()) { + throw new WrongUsageException('swoole-hook-pgsql provides pdo_pgsql, if you enable pgsql hook for swoole, you must remove pdo_pgsql extension.'); + } + // swoole-hook-sqlite conflicts with pdo_sqlite + if ($installer->getPhpExtensionPackage('swoole-hook-sqlite') && $installer->getPhpExtensionPackage('pdo_sqlite')?->isBuildStatic()) { + throw new WrongUsageException('swoole-hook-sqlite provides pdo_sqlite, if you enable sqlite hook for swoole, you must remove pdo_sqlite extension.'); + } + } + + #[BeforeStage('php', [php::class, 'makeForUnix'], 'ext-swoole')] + #[PatchDescription('Fix maximum version check for Swoole 6.2')] + public function patchBeforeMake(): void + { + FileSystem::replaceFileStr($this->getSourceDir() . '/ext-src/php_swoole_private.h', 'PHP_VERSION_ID > 80500', 'PHP_VERSION_ID >= 80600'); + } + + #[BeforeStage('php', [php::class, 'makeForUnix'], 'ext-swoole')] + #[PatchDescription('Fix swoole with event extension conflict bug on macOS')] + public function patchBeforeMake2(): void + { + if (SystemTarget::getTargetOS() === 'Darwin') { + // Fix swoole with event extension conflict bug + $util_path = shell()->execWithResult('xcrun --show-sdk-path', false)[1][0] . '/usr/include/util.h'; + FileSystem::replaceFileStr( + "{$this->getSourceDir()}/thirdparty/php/standard/proc_open.cc", + 'include ', + "include \"{$util_path}\"", + ); + } + } + + #[CustomPhpConfigureArg('Darwin')] + #[CustomPhpConfigureArg('Linux')] + public function getUnixConfigureArg(bool $shared, PackageBuilder $builder, PackageInstaller $installer): string + { + // enable swoole + $arg = '--enable-swoole' . ($shared ? '=shared' : ''); + + // commonly used feature: coroutine-time + $arg .= ' --enable-swoole-coro-time --with-pic'; + + $arg .= $builder->getOption('enable-zts') ? ' --enable-swoole-thread --disable-thread-context' : ' --disable-swoole-thread --enable-thread-context'; + + // required features: curl, openssl (but curl hook is buggy for php 8.0) + $arg .= php::getPHPVersionID() >= 80100 ? ' --enable-swoole-curl' : ' --disable-swoole-curl'; + $arg .= ' --enable-openssl'; + + // additional features that only require libraries + $arg .= $installer->getLibraryPackage('libcares') ? ' --enable-cares' : ''; + $arg .= $installer->getLibraryPackage('brotli') ? (' --enable-brotli --with-brotli-dir=' . BUILD_ROOT_PATH) : ''; + $arg .= $installer->getLibraryPackage('nghttp2') ? (' --with-nghttp2-dir=' . BUILD_ROOT_PATH) : ''; + $arg .= $installer->getLibraryPackage('zstd') ? ' --enable-zstd' : ''; + $arg .= $installer->getLibraryPackage('liburing') ? ' --enable-iouring' : ''; + $arg .= $installer->getPhpExtensionPackage('sockets') ? ' --enable-sockets' : ''; + + // enable additional features that require the pdo extension, but conflict with pdo_* extensions + // to make sure everything works as it should, this is done in fake addon extensions + $arg .= $installer->getPhpExtensionPackage('swoole-hook-pgsql') ? ' --enable-swoole-pgsql' : ' --disable-swoole-pgsql'; + $arg .= $installer->getPhpExtensionPackage('swoole-hook-mysql') ? ' --enable-mysqlnd' : ' --disable-mysqlnd'; + $arg .= $installer->getPhpExtensionPackage('swoole-hook-sqlite') ? ' --enable-swoole-sqlite' : ' --disable-swoole-sqlite'; + if ($installer->getPhpExtensionPackage('swoole-hook-odbc')) { + $config = new SPCConfigUtil()->getLibraryConfig($installer->getLibraryPackage('unixodbc')); + $arg .= " --with-swoole-odbc=unixODBC,{$builder->getBuildRootPath()} SWOOLE_ODBC_LIBS=\"{$config['libs']}\""; + } + + // Get version from source directory + $ver = null; + $file = SOURCE_PATH . '/php-src/ext/swoole/include/swoole_version.h'; + // Match #define SWOOLE_VERSION "5.1.3" + $pattern = '/#define SWOOLE_VERSION "(.+)"/'; + if (preg_match($pattern, file_get_contents($file), $matches)) { + $ver = $matches[1]; + } + + if ($ver && $ver >= '6.1.0') { + $arg .= ' --enable-swoole-stdext'; + } + + if (SystemTarget::getTargetOS() === 'Darwin') { + $arg .= ' ac_cv_lib_pthread_pthread_barrier_init=no'; + } + + return $arg; + } + + #[AfterStage('php', [php::class, 'smokeTestCliForUnix'], 'ext-swoole-hook-mysql')] + public function mysqlTest(PackageInstaller $installer): void + { + [$ret, $out] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -n' . $this->getSharedExtensionLoadString() . ' --ri "swoole"', false); + $out = implode('', $out); + if ($ret !== 0) { + throw new ValidationException("extension {$this->getName()} failed compile check: php-cli returned {$ret}", validation_module: 'extension swoole_hook_mysql sanity check'); + } + // mysqlnd + if ($installer->getPhpExtensionPackage('swoole-hook-mysql') && !str_contains($out, 'mysqlnd')) { + throw new ValidationException('swoole mysql hook is not enabled correctly.', validation_module: 'Extension swoole mysql hook availability check'); + } + // coroutine_odbc + if ($installer->getPhpExtensionPackage('swoole-hook-odbc') && !str_contains($out, 'coroutine_odbc')) { + throw new ValidationException('swoole odbc hook is not enabled correctly.', validation_module: 'Extension swoole odbc hook availability check'); + } + // coroutine_pgsql + if ($installer->getPhpExtensionPackage('swoole-hook-pgsql') && !str_contains($out, 'coroutine_pgsql')) { + throw new ValidationException( + 'swoole pgsql hook is not enabled correctly.', + validation_module: 'Extension swoole pgsql hook availability check' + ); + } + // coroutine_sqlite + if ($installer->getPhpExtensionPackage('swoole-hook-sqlite') && !str_contains($out, 'coroutine_sqlite')) { + throw new ValidationException( + 'swoole sqlite hook is not enabled correctly.', + validation_module: 'Extension swoole sqlite hook availability check' + ); + } + } +}