Refactor micro patching logic in SourcePatcher

This commit is contained in:
crazywhalecc 2025-06-28 17:06:51 +08:00
parent 25c2def710
commit ad080da026
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
2 changed files with 17 additions and 14 deletions

View File

@ -14,6 +14,7 @@ use SPC\store\Config;
use SPC\store\FileSystem; use SPC\store\FileSystem;
use SPC\store\LockFile; use SPC\store\LockFile;
use SPC\store\SourceManager; use SPC\store\SourceManager;
use SPC\store\SourcePatcher;
use SPC\util\CustomExt; use SPC\util\CustomExt;
abstract class BuilderBase abstract class BuilderBase
@ -203,6 +204,8 @@ abstract class BuilderBase
$this->emitPatchPoint('before-exts-extract'); $this->emitPatchPoint('before-exts-extract');
SourceManager::initSource(exts: [...$static_extensions, ...$shared_extensions]); SourceManager::initSource(exts: [...$static_extensions, ...$shared_extensions]);
$this->emitPatchPoint('after-exts-extract'); $this->emitPatchPoint('after-exts-extract');
// patch micro
SourcePatcher::patchMicro();
} }
foreach ([...$static_extensions, ...$shared_extensions] as $extension) { foreach ([...$static_extensions, ...$shared_extensions] as $extension) {

View File

@ -17,7 +17,6 @@ class SourcePatcher
public static function init(): void public static function init(): void
{ {
// FileSystem::addSourceExtractHook('swow', [SourcePatcher::class, 'patchSwow']); // FileSystem::addSourceExtractHook('swow', [SourcePatcher::class, 'patchSwow']);
FileSystem::addSourceExtractHook('micro', [SourcePatcher::class, 'patchMicro']);
FileSystem::addSourceExtractHook('openssl', [SourcePatcher::class, 'patchOpenssl11Darwin']); FileSystem::addSourceExtractHook('openssl', [SourcePatcher::class, 'patchOpenssl11Darwin']);
FileSystem::addSourceExtractHook('swoole', [SourcePatcher::class, 'patchSwoole']); FileSystem::addSourceExtractHook('swoole', [SourcePatcher::class, 'patchSwoole']);
FileSystem::addSourceExtractHook('php-src', [SourcePatcher::class, 'patchPhpLibxml212']); FileSystem::addSourceExtractHook('php-src', [SourcePatcher::class, 'patchPhpLibxml212']);
@ -105,7 +104,7 @@ class SourcePatcher
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public static function patchMicro(string $name = '', string $target = '', ?array $items = null): bool public static function patchMicro(?array $items = null): bool
{ {
if (!file_exists(SOURCE_PATH . '/php-src/sapi/micro/php_micro.c')) { if (!file_exists(SOURCE_PATH . '/php-src/sapi/micro/php_micro.c')) {
return false; return false;
@ -152,11 +151,7 @@ class SourcePatcher
foreach ($patches as $patch) { foreach ($patches as $patch) {
logger()->info("Patching micro with {$patch}"); logger()->info("Patching micro with {$patch}");
$patchesStr = str_replace('/', DIRECTORY_SEPARATOR, $patch); self::patchFile(SOURCE_PATH . "/php-src{$patch}", SOURCE_PATH . '/php-src');
f_passthru(
'cd ' . SOURCE_PATH . '/php-src && ' .
(PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . $patchesStr . ' | patch -p1 '
);
} }
return true; return true;
@ -165,24 +160,29 @@ class SourcePatcher
/** /**
* Use existing patch file for patching * Use existing patch file for patching
* *
* @param string $patch_name Patch file name in src/globals/patch/ * @param string $patch_name Patch file name in src/globals/patch/ or absolute path
* @param string $cwd Working directory for patch command * @param string $cwd Working directory for patch command
* @param bool $reverse Reverse patches (default: False) * @param bool $reverse Reverse patches (default: False)
* @throws RuntimeException * @throws RuntimeException
*/ */
public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool
{ {
if (!file_exists(ROOT_DIR . "/src/globals/patch/{$patch_name}")) { if (FileSystem::isRelativePath($patch_name)) {
$patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}";
} else {
$patch_file = $patch_name;
}
if (!file_exists($patch_file)) {
return false; return false;
} }
$patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}"; $patch_str = FileSystem::convertPath($patch_file);
$patch_str = str_replace('/', DIRECTORY_SEPARATOR, $patch_file);
// Copy patch from phar // Copy patch from phar
if (\Phar::running() !== '') { if (str_starts_with($patch_str, 'phar://')) {
file_put_contents(SOURCE_PATH . '/' . $patch_name, file_get_contents($patch_file)); $filename = pathinfo($patch_file, PATHINFO_BASENAME);
$patch_str = str_replace('/', DIRECTORY_SEPARATOR, SOURCE_PATH . '/' . $patch_name); file_put_contents(SOURCE_PATH . "{$filename}", file_get_contents($patch_file));
$patch_str = FileSystem::convertPath(SOURCE_PATH . "/{$filename}");
} }
// detect // detect