From 880bb8799b012710fbe857900b8139db38e40550 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Fri, 6 Feb 2026 10:55:59 +0800 Subject: [PATCH] Add libevent and postinstall action adder for library package --- config/pkg/lib/libevent.yml | 18 ++++++ src/Package/Library/libevent.php | 81 ++++++++++++++++++++++++ src/StaticPHP/Package/LibraryPackage.php | 71 +++++++++++++++++++-- 3 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 config/pkg/lib/libevent.yml create mode 100644 src/Package/Library/libevent.php diff --git a/config/pkg/lib/libevent.yml b/config/pkg/lib/libevent.yml new file mode 100644 index 00000000..aa4d0d52 --- /dev/null +++ b/config/pkg/lib/libevent.yml @@ -0,0 +1,18 @@ +libevent: + type: library + artifact: + source: + type: ghrel + repo: libevent/libevent + match: libevent.+\.tar\.gz + prefer-stable: true + metadata: + license-files: [LICENSE] + license: BSD-3-Clause + depends@unix: + - openssl + static-libs@unix: + - libevent.a + - libevent_core.a + - libevent_extra.a + - libevent_openssl.a diff --git a/src/Package/Library/libevent.php b/src/Package/Library/libevent.php new file mode 100644 index 00000000..0e3dffe6 --- /dev/null +++ b/src/Package/Library/libevent.php @@ -0,0 +1,81 @@ +addPostinstallAction([ + 'action' => 'replace-path', + 'files' => [$cmake_file], + ]); + } + } + + #[BuildFor('Darwin')] + #[BuildFor('Linux')] + public function buildUnix(LibraryPackage $lib): void + { + $cmake = UnixCMakeExecutor::create($lib) + ->addConfigureArgs( + '-DEVENT__LIBRARY_TYPE=STATIC', + '-DEVENT__DISABLE_BENCHMARK=ON', + '-DEVENT__DISABLE_THREAD_SUPPORT=ON', + '-DEVENT__DISABLE_TESTS=ON', + '-DEVENT__DISABLE_SAMPLES=ON', + '-DEVENT__DISABLE_MBEDTLS=ON ', + ); + if (version_compare(get_cmake_version(), '4.0.0', '>=')) { + $cmake->addConfigureArgs('-DCMAKE_POLICY_VERSION_MINIMUM=3.10'); + } + $cmake->build(); + + $lib->patchPkgconfPrefix(['libevent.pc', 'libevent_core.pc', 'libevent_extra.pc', 'libevent_openssl.pc']); + + $lib->patchPkgconfPrefix( + ['libevent_openssl.pc'], + PKGCONF_PATCH_CUSTOM, + [ + '/Libs.private:.*/m', + 'Libs.private: -lssl -lcrypto', + ] + ); + } +} diff --git a/src/StaticPHP/Package/LibraryPackage.php b/src/StaticPHP/Package/LibraryPackage.php index 0567f92b..8b392bab 100644 --- a/src/StaticPHP/Package/LibraryPackage.php +++ b/src/StaticPHP/Package/LibraryPackage.php @@ -21,6 +21,12 @@ use StaticPHP\Util\SPCConfigUtil; */ class LibraryPackage extends Package { + /** + * Custom postinstall actions for this package. + * @var array + */ + private array $customPostinstallActions = []; + public function isInstalled(): bool { foreach (PackageConfig::get($this->getName(), 'static-libs', []) as $lib) { @@ -52,6 +58,24 @@ class LibraryPackage extends Package return true; } + /** + * Add a custom postinstall action for this package. + * Available actions: + * - replace-path: Replace placeholders with actual paths + * Example: ['action' => 'replace-path', 'files' => ['lib/cmake/xxx.cmake']] + * - replace-to-env: Replace string with environment variable value + * Example: ['action' => 'replace-to-env', 'file' => 'bin/xxx-config', 'search' => 'XXX', 'replace-env' => 'BUILD_ROOT_PATH'] + * + * @param array $action Action array with 'action' key and other required keys + */ + public function addPostinstallAction(array $action): void + { + if (!isset($action['action'])) { + throw new WrongUsageException('Postinstall action must have "action" key.'); + } + $this->customPostinstallActions[] = $action; + } + public function patchLaDependencyPrefix(?array $files = null): void { logger()->info("Patching library {$this->name} la files"); @@ -234,14 +258,49 @@ class LibraryPackage extends Package } } - // generate postinstall action file if there are files to process + // collect all postinstall actions + $postinstall_actions = []; + + // add default replace-path action if there are .pc/.la files if ($postinstall_files !== []) { - $postinstall_actions = [ - [ - 'action' => 'replace-path', - 'files' => $postinstall_files, - ], + $postinstall_actions[] = [ + 'action' => 'replace-path', + 'files' => $postinstall_files, ]; + } + + // merge custom postinstall actions and handle files for replace-path actions + foreach ($this->customPostinstallActions as $action) { + // if action is replace-path, process the files with placeholder replacement + if ($action['action'] === 'replace-path') { + $files = $action['files'] ?? []; + if (!is_array($files)) { + $files = [$files]; + } + foreach ($files as $file) { + if (file_exists(BUILD_ROOT_PATH . '/' . $file)) { + $content = FileSystem::readFile(BUILD_ROOT_PATH . '/' . $file); + $origin_files[$file] = $content; + // replace actual paths with placeholders + $content = str_replace( + array_keys($placeholder), + array_values($placeholder), + $content + ); + FileSystem::writeFile(BUILD_ROOT_PATH . '/' . $file, $content); + // ensure this file is included in the package + if (!in_array($file, $increase_files, true)) { + $increase_files[] = $file; + } + } + } + } + // add custom action to postinstall actions + $postinstall_actions[] = $action; + } + + // generate postinstall action file if there are actions to process + if ($postinstall_actions !== []) { FileSystem::writeFile($postinstall_file, json_encode($postinstall_actions, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $increase_files[] = '.package.' . $this->getName() . '.postinstall.json'; }