From f885bb6a8cf1f5aa9e3f6df7041fb8d2be117c0e Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sun, 1 Nov 2026 23:23:57 +0800 Subject: [PATCH] fix: improve toolchain package handling and clean extension sources during PHP version switch --- .../Command/SwitchPhpVersionCommand.php | 40 +++++++++ src/StaticPHP/Package/PackageInstaller.php | 81 +++++++++++++++---- src/StaticPHP/Toolchain/MSVCToolchain.php | 8 ++ 3 files changed, 114 insertions(+), 15 deletions(-) diff --git a/src/Package/Command/SwitchPhpVersionCommand.php b/src/Package/Command/SwitchPhpVersionCommand.php index 76416c79a..fbc335f7b 100644 --- a/src/Package/Command/SwitchPhpVersionCommand.php +++ b/src/Package/Command/SwitchPhpVersionCommand.php @@ -68,6 +68,11 @@ public function handle(): int InteractiveTerm::finish('Removed: ' . $source_dir); } + // Extension sources are extracted to source/ext-* and linked into php-src/ext, so their + // build state (objects, dependency files, patched config.m4, phpize leftovers) lives + // outside source/php-src and is only valid for the PHP version that produced it. + $this->cleanExtensionSources(); + // Download new PHP source $this->output->writeln("Downloading PHP {$php_ver} source..."); @@ -98,6 +103,41 @@ public function handle(): int return static::SUCCESS; } + /** + * Delete every extracted extension source tree under source/ext-*, returning the count. + */ + public static function removeExtensionSources(string $source_path): int + { + $removed = 0; + foreach (glob(FileSystem::convertPath("{$source_path}/ext-*"), GLOB_ONLYDIR) ?: [] as $ext_dir) { + if (FileSystem::isLink($ext_dir)) { + FileSystem::removeLink($ext_dir); + } else { + FileSystem::removeDir($ext_dir); + } + ++$removed; + } + + return $removed; + } + + /** + * Remove the extracted extension source trees (source/ext-*) of the previous PHP version. + * + * Extension sources are linked into php-src/ext for in-tree builds, so their objects, + * dependency files, patched config.m4 and phpize leftovers live outside source/php-src and + * would otherwise leak into the next PHP version's build. The original archives stay in + * downloads/ and are re-extracted automatically on the next build. + */ + private function cleanExtensionSources(): void + { + $removed = self::removeExtensionSources(SOURCE_PATH); + + if ($removed > 0) { + $this->output->writeln("Removed {$removed} extracted extension source director" . ($removed === 1 ? 'y' : 'ies') . '; they will be re-extracted from the cached archives on the next build.'); + } + } + /** * Validate PHP version format. * diff --git a/src/StaticPHP/Package/PackageInstaller.php b/src/StaticPHP/Package/PackageInstaller.php index 205995a2a..ed72388b1 100644 --- a/src/StaticPHP/Package/PackageInstaller.php +++ b/src/StaticPHP/Package/PackageInstaller.php @@ -45,6 +45,9 @@ class PackageInstaller /** @var array> Unresolved target additional dependencies defined in #[ResolveBuild] */ protected array $target_additional_dependencies = []; + /** @var bool Whether this run compiles something and therefore needs the toolchain packages */ + protected bool $toolchain_packages_required = false; + /** @var bool Whether to download missing sources automatically */ protected bool $download = true; @@ -719,21 +722,6 @@ private function resolvePackages(): void $pkgs[] = $package->getName(); } - // gather packages declared by the active toolchain (e.g. the compiler runtime zig does - // not ship). They are prepended so they are built before the packages that need them, and - // they join as regular install packages so the normal pipeline downloads/builds them and - // their own isInstalled() decides skip vs rebuild. - $toolchain_packages = array_values(array_filter( - $this->collectToolchainPackages(), - static fn (string $package): bool => !in_array($package, $pkgs, true) - )); - // They are fetched pre-built where a binary is published (llvm-tools): building them from - // source is only the fallback, unless the user explicitly asked for source-only downloads. - if ($toolchain_packages !== []) { - $this->preferBinary(...$toolchain_packages); - } - $pkgs = [...$toolchain_packages, ...$pkgs]; - // resolve dependencies string $resolved_packages = DependencyResolver::resolve( $pkgs, @@ -741,6 +729,31 @@ private function resolvePackages(): void $this->options['with-suggests'] ?? false ); + // gather packages declared by the active toolchain (e.g. the compiler runtime zig does + // not ship). They are prepended so they are built before the packages that need them, and + // they join as regular install packages so the normal pipeline downloads/builds them and + // their own isInstalled() decides skip vs rebuild. + $this->toolchain_packages_required = $this->runCompilesSomething($resolved_packages); + if ($this->toolchain_packages_required) { + $toolchain_packages = array_values(array_filter( + $this->collectToolchainPackages(), + static fn (string $package): bool => !in_array($package, $pkgs, true) + )); + // They are fetched pre-built where a binary is published (llvm-tools): building them + // from source is only the fallback, unless the user asked for source-only downloads. + if ($toolchain_packages !== []) { + $this->preferBinary(...$toolchain_packages); + $pkgs = [...$toolchain_packages, ...$pkgs]; + $resolved_packages = DependencyResolver::resolve( + $pkgs, + $this->target_additional_dependencies, + $this->options['with-suggests'] ?? false + ); + } + } else { + logger()->debug('Nothing is compiled from source in this run, skipping toolchain packages.'); + } + foreach ($resolved_packages as $pkg_name) { $this->packages[$pkg_name] = PackageLoader::getPackage($pkg_name); } @@ -776,6 +789,38 @@ private function resolvePackages(): void } } + /** + * Whether this run builds at least one package from source. + * + * The toolchain packages supply the compiler and its runtime, so they are only needed when + * something is actually compiled. Library and tool packages that are installed from a platform + * binary, and tool packages without a build stage (e.g. zig itself), need no compiler. + * + * @param string[] $resolved_packages Package names resolved for this run + */ + private function runCompilesSomething(array $resolved_packages): bool + { + return array_any($resolved_packages, fn ($pkg_name) => $this->willCompilePackage(PackageLoader::getPackage($pkg_name))); + } + + /** + * Whether the build loop compiles this package instead of installing a prebuilt binary. + * + * Mirrors the branch selection in run(): build packages are fetched as source and always + * compiled, and an install package is compiled only when it has a build stage with source but + * no platform binary to install. Every platform that publishes a binary installs it here, + * because run() only forces source-only downloads for build packages. + */ + private function willCompilePackage(Package $package): bool + { + if ($this->isBuildPackage($package)) { + return true; + } + return $package->hasStage('build') + && $package->hasSource() + && !($package->getArtifact()?->hasPlatformBinary() ?? false); + } + /** * Report, once per run, any package the active toolchain declared that is still missing. * @@ -786,6 +831,12 @@ private function resolvePackages(): void */ private function warnAboutMissingToolchainPackages(): void { + // A run that compiled nothing never asked for the toolchain packages, so a missing zig is + // not a problem here: warning about it would only tell the user to "build again" after a + // plain `install-pkg pkg-config`. + if (!$this->toolchain_packages_required) { + return; + } $toolchain = ToolchainManager::getToolchain(); if (!$toolchain instanceof PackageRequirementsInterface) { return; diff --git a/src/StaticPHP/Toolchain/MSVCToolchain.php b/src/StaticPHP/Toolchain/MSVCToolchain.php index b62c53efd..931b7416d 100644 --- a/src/StaticPHP/Toolchain/MSVCToolchain.php +++ b/src/StaticPHP/Toolchain/MSVCToolchain.php @@ -46,6 +46,14 @@ public function afterInit(): void exec('call "' . $vcvarsall . '" x64 > NUL && set', $output); file_put_contents(DOWNLOAD_PATH . '/.vcenv-cache', implode("\n", $output)); } + // The snapshot captures the whole environment, including the SPC_* values that were + // active when it was taken. Replaying those would silently revert configuration the + // caller changed for the current run (e.g. SPC_CURL_TLS_BACKEND), so keep the + // process' own SPC_* values and only import the Visual Studio environment. + $output = array_values(array_filter( + $output, + static fn (string $line): bool => !str_starts_with(strtoupper($line), 'SPC_') + )); array_map(fn ($x) => putenv($x), $output); } $after = count(getenv());