Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Package/Command/SwitchPhpVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<info>Downloading PHP {$php_ver} source...</info>");

Expand Down Expand Up @@ -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("<info>Removed {$removed} extracted extension source director" . ($removed === 1 ? 'y' : 'ies') . '; they will be re-extracted from the cached archives on the next build.</info>');
}
}

/**
* Validate PHP version format.
*
Expand Down
81 changes: 66 additions & 15 deletions src/StaticPHP/Package/PackageInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class PackageInstaller
/** @var array<string, array<string>> 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;

Expand Down Expand Up @@ -719,28 +722,38 @@ 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,
$this->target_additional_dependencies,
$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);
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/StaticPHP/Toolchain/MSVCToolchain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading