diff --git a/config/pkg/lib/librabbitmq.yml b/config/pkg/lib/librabbitmq.yml index c58b13a0..da4e9856 100644 --- a/config/pkg/lib/librabbitmq.yml +++ b/config/pkg/lib/librabbitmq.yml @@ -10,3 +10,5 @@ librabbitmq: license: MIT depends: - openssl + static-libs@unix: + - librabbitmq.a diff --git a/src/Package/Artifact/bzip2.php b/src/Package/Artifact/bzip2.php index a6a1e58e..b3cef415 100644 --- a/src/Package/Artifact/bzip2.php +++ b/src/Package/Artifact/bzip2.php @@ -15,10 +15,6 @@ class bzip2 #[PatchDescription('Patch bzip2 Makefile to add -fPIC flag for position-independent code')] public function patchBzip2Makefile(Artifact $artifact): void { - FileSystem::replaceFileStr( - $artifact->getSourceDir() . '/Makefile', - 'CFLAGS=-Wall', - 'CFLAGS=-fPIC -Wall' - ); + FileSystem::replaceFileStr("{$artifact->getSourceDir()}/Makefile", 'CFLAGS=-Wall', 'CFLAGS=-fPIC -Wall'); } } diff --git a/src/Package/Artifact/php_src.php b/src/Package/Artifact/php_src.php index 498abc66..ae9488d6 100644 --- a/src/Package/Artifact/php_src.php +++ b/src/Package/Artifact/php_src.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Package\Artifact; +use Package\Target\php; use StaticPHP\Attribute\Artifact\AfterSourceExtract; use StaticPHP\Attribute\PatchDescription; use StaticPHP\Runtime\SystemTarget; @@ -16,9 +17,8 @@ class php_src #[PatchDescription('Patch PHP source for libxml2 2.12 compatibility on Alpine Linux')] public function patchPhpLibxml212(): void { - $file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h'); - if (preg_match('/PHP_VERSION_ID (\d+)/', $file, $match) !== 0) { - $ver_id = intval($match[1]); + $ver_id = php::getPHPVersionID(return_null_if_failed: true); + if ($ver_id) { if ($ver_id < 80000) { SourcePatcher::patchFile('spc_fix_alpine_build_php80.patch', SOURCE_PATH . '/php-src'); return; @@ -39,9 +39,8 @@ class php_src #[PatchDescription('Patch GD extension for Windows builds')] public function patchGDWin32(): void { - $file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h'); - if (preg_match('/PHP_VERSION_ID (\d+)/', $file, $match) !== 0) { - $ver_id = intval($match[1]); + $ver_id = php::getPHPVersionID(return_null_if_failed: true); + if ($ver_id) { if ($ver_id < 80200) { // see: https://github.com/php/php-src/commit/243966177e39eb71822935042c3f13fa6c5b9eed FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/ext/gd/libgd/gdft.c', '#ifndef MSWIN32', '#ifndef _WIN32'); @@ -58,9 +57,8 @@ class php_src public function patchFfiCentos7FixO3strncmp(): void { spc_skip_if(!($ver = SystemTarget::getLibcVersion()) || version_compare($ver, '2.17', '>')); - spc_skip_if(!file_exists(SOURCE_PATH . '/php-src/main/php_version.h')); - $file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h'); - spc_skip_if(preg_match('/PHP_VERSION_ID (\d+)/', $file, $match) !== 0 && intval($match[1]) < 80316); + $ver_id = php::getPHPVersionID(return_null_if_failed: true); + spc_skip_if($ver_id === null || $ver_id < 80316); SourcePatcher::patchFile('ffi_centos7_fix_O3_strncmp.patch', SOURCE_PATH . '/php-src'); } diff --git a/src/Package/Command/SwitchPhpVersionCommand.php b/src/Package/Command/SwitchPhpVersionCommand.php index 3782a645..a6594713 100644 --- a/src/Package/Command/SwitchPhpVersionCommand.php +++ b/src/Package/Command/SwitchPhpVersionCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Package\Command; +use Package\Target\php; use StaticPHP\Artifact\ArtifactCache; use StaticPHP\Artifact\ArtifactDownloader; use StaticPHP\Artifact\DownloaderOptions; @@ -25,7 +26,7 @@ class SwitchPhpVersionCommand extends BaseCommand $this->addArgument( 'php-version', InputArgument::REQUIRED, - 'PHP version (e.g., 8.4, 8.3, 8.2, 8.1, 8.0, 7.4, or specific like 8.4.5)', + 'PHP version (e.g., ' . implode(', ', php::SUPPORTED_MAJOR_VERSIONS) . ' or specific like 8.4.5)', ); // Downloader options @@ -42,7 +43,7 @@ class SwitchPhpVersionCommand extends BaseCommand // Validate version format if (!$this->isValidPhpVersion($php_ver)) { $this->output->writeln("Invalid PHP version '{$php_ver}'!"); - $this->output->writeln('Supported formats: 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, or specific version like 8.4.5'); + $this->output->writeln('Supported formats: ' . implode(', ', php::SUPPORTED_MAJOR_VERSIONS) . ', or specific version like 8.4.5'); return static::FAILURE; } @@ -101,13 +102,13 @@ class SwitchPhpVersionCommand extends BaseCommand * Validate PHP version format. * * Accepts: - * - Major.Minor format: 7.4, 8.0, 8.1, 8.2, 8.3, 8.4 - * - Full version format: 8.4.5, 8.3.12, etc. + * - Major.Minor format, e.g. 7.4 + * - Full version format, e.g. 8.4.5, 8.3.12, etc. */ private function isValidPhpVersion(string $version): bool { // Check major.minor format (e.g., 8.4) - if (in_array($version, ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'], true)) { + if (in_array($version, php::SUPPORTED_MAJOR_VERSIONS, true)) { return true; } diff --git a/src/Package/Target/php.php b/src/Package/Target/php.php index 32e8a13b..239fcf43 100644 --- a/src/Package/Target/php.php +++ b/src/Package/Target/php.php @@ -43,18 +43,34 @@ class php extends TargetPackage use unix; use windows; - public static function getPHPVersionID(): int + /** @var string[] Supported major PHP versions */ + public const array SUPPORTED_MAJOR_VERSIONS = ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']; + + /** + * Get PHP version ID from php_version.h + * + * @param null|string $from_custom_source Where to read php_version.h from custom source + * @param bool $return_null_if_failed Whether to return null if failed to get version ID + * @return null|int PHP version ID (e.g., 80400 for PHP 8.4.0) or null if failed + */ + public static function getPHPVersionID(?string $from_custom_source = null, bool $return_null_if_failed = false): ?int { - $artifact = ArtifactLoader::getArtifactInstance('php-src'); - if (!file_exists("{$artifact->getSourceDir()}/main/php_version.h")) { + $source_dir = $from_custom_source ?? ArtifactLoader::getArtifactInstance('php-src')->getSourceDir(); + if (!file_exists("{$source_dir}/main/php_version.h")) { + if ($return_null_if_failed) { + return null; + } throw new WrongUsageException('PHP source files are not available, you need to download them first'); } - $file = file_get_contents("{$artifact->getSourceDir()}/main/php_version.h"); + $file = file_get_contents("{$source_dir}/main/php_version.h"); if (preg_match('/PHP_VERSION_ID (\d+)/', $file, $match) !== 0) { return intval($match[1]); } + if ($return_null_if_failed) { + return null; + } throw new WrongUsageException('PHP version file format is malformed, please remove "./source/php-src" dir and download/extract again'); } diff --git a/src/Package/Target/php/unix.php b/src/Package/Target/php/unix.php index edbc6dd2..18946ad4 100644 --- a/src/Package/Target/php/unix.php +++ b/src/Package/Target/php/unix.php @@ -262,7 +262,7 @@ trait unix UnixUtil::exportDynamicSymbols($libphp_a); // deploy embed php scripts - $package->runStage([$this, 'patchEmbedScripts']); + $package->runStage([$this, 'patchUnixEmbedScripts']); } #[Stage] diff --git a/src/Package/Target/pkgconfig.php b/src/Package/Target/pkgconfig.php index e99e2d7c..aa75fc88 100644 --- a/src/Package/Target/pkgconfig.php +++ b/src/Package/Target/pkgconfig.php @@ -40,6 +40,6 @@ class pkgconfig ) ->make(with_install: 'install-exec'); - shell()->exec('strip ' . BUILD_ROOT_PATH . '/bin/pkg-config'); + shell()->exec("strip {$package->getBinDir()}/pkg-config"); } } diff --git a/src/SPC/store/SourcePatcher.php b/src/SPC/store/SourcePatcher.php index 0068f53e..0a1e0299 100644 --- a/src/SPC/store/SourcePatcher.php +++ b/src/SPC/store/SourcePatcher.php @@ -18,22 +18,22 @@ class SourcePatcher public static function init(): void { // FileSystem::addSourceExtractHook('swow', [__CLASS__, 'patchSwow']); - FileSystem::addSourceExtractHook('openssl', [__CLASS__, 'patchOpenssl11Darwin']); + FileSystem::addSourceExtractHook('openssl', [__CLASS__, 'patchOpenssl11Darwin']); // migrated FileSystem::addSourceExtractHook('swoole', [__CLASS__, 'patchSwoole']); - FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchPhpLibxml212']); - FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchGDWin32']); - FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchFfiCentos7FixO3strncmp']); + FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchPhpLibxml212']); // migrated + FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchGDWin32']); // migrated + FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchFfiCentos7FixO3strncmp']); // migrated FileSystem::addSourceExtractHook('sqlsrv', [__CLASS__, 'patchSQLSRVWin32']); FileSystem::addSourceExtractHook('pdo_sqlsrv', [__CLASS__, 'patchSQLSRVWin32']); FileSystem::addSourceExtractHook('pdo_sqlsrv', [__CLASS__, 'patchSQLSRVPhp85']); FileSystem::addSourceExtractHook('yaml', [__CLASS__, 'patchYamlWin32']); - FileSystem::addSourceExtractHook('libyaml', [__CLASS__, 'patchLibYaml']); - FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchImapLicense']); + FileSystem::addSourceExtractHook('libyaml', [__CLASS__, 'patchLibYaml']); // removed + FileSystem::addSourceExtractHook('php-src', [__CLASS__, 'patchImapLicense']); // migrated FileSystem::addSourceExtractHook('ext-imagick', [__CLASS__, 'patchImagickWith84']); - FileSystem::addSourceExtractHook('libaom', [__CLASS__, 'patchLibaomForAlpine']); - FileSystem::addSourceExtractHook('pkg-config', [__CLASS__, 'patchPkgConfigForGcc15']); - FileSystem::addSourceExtractHook('attr', [__CLASS__, 'patchAttrForAlpine']); - FileSystem::addSourceExtractHook('gmssl', [__CLASS__, 'patchGMSSL']); + FileSystem::addSourceExtractHook('libaom', [__CLASS__, 'patchLibaomForAlpine']); // migrated + FileSystem::addSourceExtractHook('pkg-config', [__CLASS__, 'patchPkgConfigForGcc15']); // migrated + FileSystem::addSourceExtractHook('attr', [__CLASS__, 'patchAttrForAlpine']); // migrated + FileSystem::addSourceExtractHook('gmssl', [__CLASS__, 'patchGMSSL']); // migrated } public static function patchBeforeBuildconf(BuilderBase $builder): void diff --git a/src/StaticPHP/Package/LibraryPackage.php b/src/StaticPHP/Package/LibraryPackage.php index 8b392bab..aa24f057 100644 --- a/src/StaticPHP/Package/LibraryPackage.php +++ b/src/StaticPHP/Package/LibraryPackage.php @@ -14,6 +14,7 @@ use StaticPHP\Runtime\SystemTarget; use StaticPHP\Util\DependencyResolver; use StaticPHP\Util\DirDiff; use StaticPHP\Util\FileSystem; +use StaticPHP\Util\GlobalPathTrait; use StaticPHP\Util\SPCConfigUtil; /** @@ -21,6 +22,8 @@ use StaticPHP\Util\SPCConfigUtil; */ class LibraryPackage extends Package { + use GlobalPathTrait; + /** * Custom postinstall actions for this package. * @var array @@ -369,41 +372,6 @@ class LibraryPackage extends Package return getenv($this->getSnakeCaseName() . '_LIBS') ?: ''; } - /** - * Get the build root path for the package. - * - * TODO: Can be changed to support per-package build root path in the future. - */ - public function getBuildRootPath(): string - { - return BUILD_ROOT_PATH; - } - - /** - * Get the include directory for the package. - * - * TODO: Can be changed to support per-package include directory in the future. - */ - public function getIncludeDir(): string - { - return BUILD_INCLUDE_PATH; - } - - /** - * Get the library directory for the package. - * - * TODO: Can be changed to support per-package library directory in the future. - */ - public function getLibDir(): string - { - return BUILD_LIB_PATH; - } - - public function getBinDir(): string - { - return BUILD_BIN_PATH; - } - /** * Get tar compress options from suffix * diff --git a/src/StaticPHP/Package/PackageBuilder.php b/src/StaticPHP/Package/PackageBuilder.php index 5d603625..c9b1051f 100644 --- a/src/StaticPHP/Package/PackageBuilder.php +++ b/src/StaticPHP/Package/PackageBuilder.php @@ -11,11 +11,14 @@ use StaticPHP\Exception\WrongUsageException; use StaticPHP\Runtime\Shell\Shell; use StaticPHP\Runtime\SystemTarget; use StaticPHP\Util\FileSystem; +use StaticPHP\Util\GlobalPathTrait; use StaticPHP\Util\InteractiveTerm; use StaticPHP\Util\System\LinuxUtil; class PackageBuilder { + use GlobalPathTrait; + /** @var int make jobs count */ public readonly int $concurrency; diff --git a/src/StaticPHP/Util/GlobalPathTrait.php b/src/StaticPHP/Util/GlobalPathTrait.php new file mode 100644 index 00000000..f94c501b --- /dev/null +++ b/src/StaticPHP/Util/GlobalPathTrait.php @@ -0,0 +1,43 @@ +