From 149e844d59c7c276415063503a6bae8b88df865e Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Wed, 3 Jan 2024 15:57:05 +0800 Subject: [PATCH] add `--with-added-patch` command --- src/SPC/builder/BuilderBase.php | 48 ++++++++++++++++++++++++++ src/SPC/builder/LibraryBase.php | 2 ++ src/SPC/builder/freebsd/BSDBuilder.php | 4 +++ src/SPC/builder/linux/LinuxBuilder.php | 5 +++ src/SPC/builder/macos/MacOSBuilder.php | 4 +++ src/SPC/command/BuildCliCommand.php | 2 ++ 6 files changed, 65 insertions(+) diff --git a/src/SPC/builder/BuilderBase.php b/src/SPC/builder/BuilderBase.php index 3c77c610..175aaf41 100644 --- a/src/SPC/builder/BuilderBase.php +++ b/src/SPC/builder/BuilderBase.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace SPC\builder; +use SPC\exception\ExceptionHandler; use SPC\exception\FileSystemException; use SPC\exception\RuntimeException; use SPC\exception\WrongUsageException; @@ -30,6 +31,9 @@ abstract class BuilderBase /** @var array compile options */ protected array $options = []; + /** @var string patch point name */ + protected string $patch_point = ''; + /** * Build libraries * @@ -78,9 +82,14 @@ abstract class BuilderBase $lib->calcDependency(); } + // patch point + $this->emitPatchPoint('before-libs-extract'); + // extract sources SourceExtractor::initSource(libs: $sorted_libraries); + $this->emitPatchPoint('after-libs-extract'); + // build all libs foreach ($this->libs as $lib) { match ($lib->tryBuild($this->getOption('rebuild', false))) { @@ -189,11 +198,17 @@ abstract class BuilderBase public function proveExts(array $extensions): void { CustomExt::loadCustomExt(); + $this->emitPatchPoint('before-php-extract'); SourceExtractor::initSource(sources: ['php-src']); + $this->emitPatchPoint('after-php-extract'); if ($this->getPHPVersionID() >= 80000) { + $this->emitPatchPoint('before-micro-extract'); SourceExtractor::initSource(sources: ['micro']); + $this->emitPatchPoint('after-micro-extract'); } + $this->emitPatchPoint('before-exts-extract'); SourceExtractor::initSource(exts: $extensions); + $this->emitPatchPoint('after-exts-extract'); foreach ($extensions as $extension) { $class = CustomExt::getExtClass($extension); $ext = new $class($extension, $this); @@ -342,6 +357,39 @@ abstract class BuilderBase return implode(' ', $env); } + /** + * Get builder patch point name. + */ + public function getPatchPoint(): string + { + return $this->patch_point; + } + + public function emitPatchPoint(string $point_name): void + { + $this->patch_point = $point_name; + if (($patches = $this->getOption('with-added-patch', [])) === []) { + return; + } + + foreach ($patches as $patch) { + try { + if (!file_exists($patch)) { + throw new RuntimeException("Additional patch script file {$patch} not found!"); + } + logger()->debug('Running additional patch script: ' . $patch); + require $patch; + } catch (\Throwable $e) { + logger()->critical('Patch script ' . $patch . ' failed to run.'); + if ($this->getOption('debug')) { + ExceptionHandler::getInstance()->handle($e); + } else { + logger()->critical('Please check with --debug option to see more details.'); + } + } + } + } + /** * Check if all libs are downloaded. * If not, throw exception. diff --git a/src/SPC/builder/LibraryBase.php b/src/SPC/builder/LibraryBase.php index bd7752ac..51ab7e30 100644 --- a/src/SPC/builder/LibraryBase.php +++ b/src/SPC/builder/LibraryBase.php @@ -140,7 +140,9 @@ abstract class LibraryBase if (!$this->patched && $this->patchBeforeBuild()) { file_put_contents($this->source_dir . '/.spc.patched', 'PATCHED!!!'); } + $this->getBuilder()->emitPatchPoint('before-library[ ' . static::NAME . ']-build'); $this->build(); + $this->getBuilder()->emitPatchPoint('after-library[ ' . static::NAME . ']-build'); return BUILD_STATUS_OK; } diff --git a/src/SPC/builder/freebsd/BSDBuilder.php b/src/SPC/builder/freebsd/BSDBuilder.php index 469c7534..ddad0ca9 100644 --- a/src/SPC/builder/freebsd/BSDBuilder.php +++ b/src/SPC/builder/freebsd/BSDBuilder.php @@ -81,10 +81,12 @@ class BSDBuilder extends BuilderBase } $this->setOption('extra-libs', $extra_libs); + $this->emitPatchPoint('before-php-buildconf'); SourcePatcher::patchBeforeBuildconf($this); shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force'); + $this->emitPatchPoint('before-php-configure'); SourcePatcher::patchBeforeConfigure($this); $json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : ''; @@ -115,6 +117,7 @@ class BSDBuilder extends BuilderBase $this->makeExtensionArgs() ); + $this->emitPatchPoint('before-php-make'); SourcePatcher::patchBeforeMake($this); $this->cleanMake(); @@ -140,6 +143,7 @@ class BSDBuilder extends BuilderBase } if (php_uname('m') === $this->getOption('arch')) { + $this->emitPatchPoint('before-sanity-check'); $this->sanityCheck($build_target); } } diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 8fe8010c..e01028be 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -147,10 +147,13 @@ class LinuxBuilder extends BuilderBase 'LDFLAGS' => '-L' . BUILD_LIB_PATH, 'LIBS' => '-ldl -lpthread', ]); + + $this->emitPatchPoint('before-php-buildconf'); SourcePatcher::patchBeforeBuildconf($this); shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force'); + $this->emitPatchPoint('before-php-configure'); SourcePatcher::patchBeforeConfigure($this); $phpVersionID = $this->getPHPVersionID(); @@ -193,6 +196,7 @@ class LinuxBuilder extends BuilderBase ' ' . $envs_build_php . ' ' ); + $this->emitPatchPoint('before-php-make'); SourcePatcher::patchBeforeMake($this); $this->cleanMake(); @@ -218,6 +222,7 @@ class LinuxBuilder extends BuilderBase } if (php_uname('m') === $this->getOption('arch')) { + $this->emitPatchPoint('before-sanity-check'); $this->sanityCheck($build_target); } } diff --git a/src/SPC/builder/macos/MacOSBuilder.php b/src/SPC/builder/macos/MacOSBuilder.php index 6a60b33d..c7ac7107 100644 --- a/src/SPC/builder/macos/MacOSBuilder.php +++ b/src/SPC/builder/macos/MacOSBuilder.php @@ -141,10 +141,12 @@ class MacOSBuilder extends BuilderBase } $this->setOption('extra-libs', $extra_libs); + $this->emitPatchPoint('before-php-buildconf'); SourcePatcher::patchBeforeBuildconf($this); shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force'); + $this->emitPatchPoint('before-php-configure'); SourcePatcher::patchBeforeConfigure($this); $json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : ''; @@ -190,6 +192,7 @@ class MacOSBuilder extends BuilderBase $envs_build_php ); + $this->emitPatchPoint('before-php-make'); SourcePatcher::patchBeforeMake($this); $this->cleanMake(); @@ -215,6 +218,7 @@ class MacOSBuilder extends BuilderBase } if (php_uname('m') === $this->getOption('arch')) { + $this->emitPatchPoint('before-sanity-check'); $this->sanityCheck($build_target); } } diff --git a/src/SPC/command/BuildCliCommand.php b/src/SPC/command/BuildCliCommand.php index 5bd0bba3..82f6b78a 100644 --- a/src/SPC/command/BuildCliCommand.php +++ b/src/SPC/command/BuildCliCommand.php @@ -35,6 +35,8 @@ class BuildCliCommand extends BuildCommand $this->addOption('with-micro-fake-cli', null, null, 'Enable phpmicro fake cli'); $this->addOption('with-suggested-libs', 'L', null, 'Build with suggested libs for selected exts and libs'); $this->addOption('with-suggested-exts', 'E', null, 'Build with suggested extensions for selected exts'); + $this->addOption('with-added-patch', 'P', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Inject patch script outside'); + } public function handle(): int