Merge remote-tracking branch 'origin/v3' into feat/pgo-v3

This commit is contained in:
henderkes
2026-05-11 21:05:54 +07:00
54 changed files with 1171 additions and 263 deletions

View File

@@ -23,13 +23,13 @@ class go_win
$pkgroot = PKG_ROOT_PATH;
// get version
[$version] = explode("\n", default_shell()->executeCurl('https://go.dev/VERSION?m=text') ?: '');
[$version] = explode("\n", default_shell()->executeCurl('https://go.dev/VERSION?m=text', retries: $downloader->getRetry()) ?: '');
if ($version === '') {
throw new DownloaderException('Failed to get latest Go version from https://go.dev/VERSION?m=text');
}
// find SHA256 hash from download page
$page = default_shell()->executeCurl('https://go.dev/dl/');
$page = default_shell()->executeCurl('https://go.dev/dl/', retries: $downloader->getRetry());
if ($page === '' || $page === false) {
throw new DownloaderException('Failed to get Go download page from https://go.dev/dl/');
}

View File

@@ -39,11 +39,11 @@ class go_xcaddy
};
// get version and hash
[$version] = explode("\n", default_shell()->executeCurl('https://go.dev/VERSION?m=text') ?: '');
[$version] = explode("\n", default_shell()->executeCurl('https://go.dev/VERSION?m=text', retries: $downloader->getRetry()) ?: '');
if ($version === '') {
throw new DownloaderException('Failed to get latest Go version from https://go.dev/VERSION?m=text');
}
$page = default_shell()->executeCurl('https://go.dev/dl/');
$page = default_shell()->executeCurl('https://go.dev/dl/', retries: $downloader->getRetry());
if ($page === '' || $page === false) {
throw new DownloaderException('Failed to get Go download page from https://go.dev/dl/');
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Package\Extension;
use Package\Target\php;
use StaticPHP\Attribute\Package\BeforeStage;
use StaticPHP\Attribute\Package\CustomPhpConfigureArg;
use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Attribute\PatchDescription;
use StaticPHP\Package\PackageInstaller;
use StaticPHP\Package\PhpExtensionPackage;
use StaticPHP\Util\FileSystem;
#[Extension('clickhouse')]
class clickhouse extends PhpExtensionPackage
{
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-clickhouse')]
#[PatchDescription('Replace THIS_DIR=`dirname $0` with PHP_EXT_SRCDIR() in config.m4 so include paths resolve to the ext source dir during PHP main configure (dirname $0 returns "." when run from php-src root).')]
public function patchBeforeBuildconfUnix(): void
{
FileSystem::replaceFileRegex(
"{$this->getSourceDir()}/config.m4",
'/^(\s*)THIS_DIR=.*/m',
'$1THIS_DIR=PHP_EXT_SRCDIR()',
);
}
#[CustomPhpConfigureArg('Darwin')]
#[CustomPhpConfigureArg('Linux')]
public function getUnixConfigureArg(bool $shared, PackageInstaller $installer): string
{
$arg = '--enable-clickhouse' . ($shared ? '=shared' : '');
if ($installer->getLibraryPackage('openssl')) {
$arg .= ' --enable-clickhouse-openssl';
}
return $arg;
}
}

View File

@@ -9,6 +9,7 @@ use StaticPHP\Attribute\Package\BeforeStage;
use StaticPHP\Attribute\Package\CustomPhpConfigureArg;
use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Attribute\Package\Validate;
use StaticPHP\Attribute\PatchDescription;
use StaticPHP\Exception\WrongUsageException;
use StaticPHP\Package\PackageBuilder;
use StaticPHP\Package\PackageInstaller;
@@ -26,6 +27,19 @@ class imap extends PhpExtensionPackage
}
}
#[BeforeStage('php', [php::class, 'makeCliForUnix'], 'ext-imap')]
#[PatchDescription('Fix imap zend_zval_value_name() call for PHP 8.2 compatibility')]
public function patchBeforeMake(): void
{
// zend_zval_value_name() was introduced in PHP 8.3; PHP 8.2 imap backported the call but not the declaration
// replace with the equivalent PHP 8.2-compatible function
FileSystem::replaceFileStr(
"{$this->getSourceDir()}/php_imap.c",
'zend_zval_value_name(data)',
'zend_zval_type_name(data)'
);
}
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-imap')]
public function patchBeforeBuildconf(PackageInstaller $installer): void
{

View File

@@ -16,7 +16,7 @@ use StaticPHP\Util\FileSystem;
class intl extends PhpExtensionPackage
{
#[BeforeStage('php', [php::class, 'buildconfForWindows'], 'ext-intl')]
#[PatchDescription('Fix intl config.w32: replace hardcoded true with PHP_INTL_SHARED for static build support')]
#[PatchDescription('Fix intl config.w32: replace hardcoded true with PHP_INTL_SHARED for static build support; add /std:c++17 required by ICU 73+')]
public function patchBeforeBuildconfForWindows(PackageInstaller $installer): void
{
$php_src = $installer->getTargetPackage('php')->getSourceDir();
@@ -25,5 +25,11 @@ class intl extends PhpExtensionPackage
'EXTENSION("intl", "php_intl.c intl_convert.c intl_convertcpp.cpp intl_error.c ", true,',
'EXTENSION("intl", "php_intl.c intl_convert.c intl_convertcpp.cpp intl_error.c ", PHP_INTL_SHARED,'
);
// ICU 73+ headers (char16ptr.h etc.) unconditionally include <string_view> which requires C++17.
FileSystem::replaceFileStr(
"{$php_src}/ext/intl/config.w32",
'ADD_FLAG("CFLAGS_INTL", "/EHsc',
'ADD_FLAG("CFLAGS_INTL", "/std:c++17 /EHsc'
);
}
}

View File

@@ -17,6 +17,20 @@ class memcache extends PhpExtensionPackage
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-memcache')]
public function patchBeforeBuildconf(): bool
{
// PHP 8.5 moved php_smart_string*.h from ext/standard/ to Zend/
foreach (['src/memcache_pool.h', 'src/memcache_pool.c', 'src/memcache_session.c', 'src/memcache_ascii_protocol.c', 'src/memcache_binary_protocol.c'] as $file) {
FileSystem::replaceFileStr(
"{$this->getSourceDir()}/{$file}",
'#include "ext/standard/php_smart_string_public.h"',
'#include "Zend/zend_smart_string_public.h"',
);
FileSystem::replaceFileStr(
"{$this->getSourceDir()}/{$file}",
'#include "ext/standard/php_smart_string.h"',
'#include "Zend/zend_smart_string.h"',
);
}
if (!$this->isBuildStatic()) {
return false;
}

View File

@@ -7,15 +7,21 @@ namespace Package\Extension;
use Package\Target\php;
use StaticPHP\Attribute\Package\BeforeStage;
use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Toolchain\Interface\ToolchainInterface;
use StaticPHP\Toolchain\ZigToolchain;
use StaticPHP\Util\GlobalEnvManager;
#[Extension('opentelemetry')]
class opentelemetry
{
#[BeforeStage('php', [php::class, 'makeForUnix'], 'ext-opentelemetry')]
public function patchBeforeMake(): void
public function patchBeforeMake(ToolchainInterface $toolchain): void
{
// add -Wno-strict-prototypes
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes');
$extra_cflags = getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') ?: '';
$extra_cflags .= ' -Wno-strict-prototypes';
if ($toolchain instanceof ZigToolchain) {
$extra_cflags .= ' -Wno-unknown-warning-option';
}
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . trim($extra_cflags));
}
}

View File

@@ -20,7 +20,7 @@ class swow extends PhpExtensionPackage
#[CustomPhpConfigureArg('Windows')]
public function configureArg(PackageInstaller $installer): string
{
$arg = '--enable-swow';
$arg = '--enable-swow --disable-swow-pdo-pgsql';
$arg .= $installer->getLibraryPackage('openssl') ? ' --enable-swow-ssl' : ' --disable-swow-ssl';
$arg .= $installer->getLibraryPackage('curl') ? ' --enable-swow-curl' : ' --disable-swow-curl';
return $arg;

View File

@@ -8,15 +8,16 @@ use Package\Target\php;
use StaticPHP\Attribute\Package\CustomPhpConfigureArg;
use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Package\PackageBuilder;
use StaticPHP\Package\PackageInstaller;
#[Extension('zlib')]
class zlib
{
#[CustomPhpConfigureArg('Darwin')]
#[CustomPhpConfigureArg('Linux')]
public function unixConfigureArg(PackageBuilder $builder): string
public function unixConfigureArg(PackageBuilder $builder, PackageInstaller $installer): string
{
$zlib_dir = php::getPHPVersionID() >= 80400 ? '' : ' --with-zlib-dir=' . $builder->getBuildRootPath();
return '--with-zlib' . $zlib_dir;
$zlib_dir = (php::getPHPVersionID() >= 80400 && !$installer->getPhpExtensionPackage('spx')) ? '' : " --with-zlib-dir={$builder->getBuildRootPath()}";
return "--with-zlib{$zlib_dir}";
}
}

View File

@@ -33,7 +33,7 @@ class mpir
{
$vs_ver_dir = ApplicationContext::get('mpir_vs_ver_dir');
cmd()->cd("{$lib->getSourceDir()}{$vs_ver_dir}\\lib_mpir_gc")
->exec('msbuild lib_mpir_gc.vcxproj /t:Rebuild /p:Configuration=Release /p:Platform=x64');
->exec('msbuild lib_mpir_gc.vcxproj /t:Rebuild /p:Configuration=Release /p:Platform=x64 /p:WindowsTargetPlatformVersion=10.0');
FileSystem::createDir($lib->getLibDir());
FileSystem::createDir($lib->getIncludeDir());
FileSystem::copy("{$lib->getSourceDir()}{$vs_ver_dir}\\lib_mpir_gc\\x64\\Release\\mpir_a.lib", "{$lib->getLibDir()}\\mpir_a.lib");

View File

@@ -26,6 +26,7 @@ class zstd
)
->build();
FileSystem::copy($package->getLibDir() . '\zstd_static.lib', $package->getLibDir() . '/zstd.lib');
FileSystem::copy($package->getLibDir() . '\zstd_static.lib', $package->getLibDir() . '/libzstd.lib');
}
#[BuildFor('Linux')]

View File

@@ -20,8 +20,10 @@ use StaticPHP\Package\PhpExtensionPackage;
use StaticPHP\Package\TargetPackage;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Toolchain\Interface\ToolchainInterface;
use StaticPHP\Toolchain\ZigToolchain;
use StaticPHP\Util\DirDiff;
use StaticPHP\Util\FileSystem;
use StaticPHP\Util\GlobalEnvManager;
use StaticPHP\Util\InteractiveTerm;
use StaticPHP\Util\SourcePatcher;
use StaticPHP\Util\SPCConfigUtil;
@@ -93,7 +95,8 @@ trait unix
// disable undefined behavior sanitizer when opcache JIT is enabled (Linux only)
if (SystemTarget::getTargetOS() === 'Linux' && !$package->getBuildOption('disable-opcache-jit', false)) {
if ($version_id >= 80500 || $installer->isPackageResolved('ext-opcache')) {
f_putenv('SPC_COMPILER_EXTRA=-fno-sanitize=undefined');
$compiler_extra = getenv('SPC_COMPILER_EXTRA') ?: '';
GlobalEnvManager::putenv('SPC_COMPILER_EXTRA=' . trim($compiler_extra . ' -fno-sanitize=undefined'));
}
}
// PHP JSON extension is built-in since PHP 8.0
@@ -165,14 +168,20 @@ trait unix
#[BeforeStage('php', [self::class, 'makeForUnix'], 'php')]
#[PatchDescription('Patch Makefile to fix //lib path for Linux builds')]
public function tryPatchMakefileUnix(): void
#[PatchDescription('Patch BUILD_CC to use system cc instead of zig-cc (prevents minilua crash)')]
public function tryPatchMakefileUnix(TargetPackage $package, ToolchainInterface $toolchain): void
{
if (SystemTarget::getTargetOS() !== 'Linux') {
return;
}
// replace //lib with /lib in Makefile
shell()->cd(SOURCE_PATH . '/php-src')->exec('sed -i "s|//lib|/lib|g" Makefile');
shell()->cd($package->getSourceDir())->exec('sed -i "s|//lib|/lib|g" Makefile');
if ($toolchain instanceof ZigToolchain) {
$makefile = "{$package->getSourceDir()}/Makefile";
FileSystem::replaceFileRegex($makefile, '/^BUILD_CC\s*=\s*zig-cc\s*$/m', 'BUILD_CC = cc');
}
}
#[BeforeStage('php', [self::class, 'makeForUnix'], 'php')]
@@ -392,14 +401,13 @@ trait unix
// ------------- SPC_CMD_VAR_PHP_EMBED_TYPE=static -------------
// process libphp.a for static embed
if (!file_exists("{$package->getLibDir()}/libphp.a")) {
return;
// process libphp.a for static embed (only when present)
if (file_exists("{$package->getLibDir()}/libphp.a")) {
$ar = getenv('AR') ?: 'ar';
$libphp_a = "{$package->getLibDir()}/libphp.a";
shell()->exec("{$ar} -t {$libphp_a} | grep '\\.a$' | xargs -n1 {$ar} d {$libphp_a}");
UnixUtil::exportDynamicSymbols($libphp_a);
}
$ar = getenv('AR') ?: 'ar';
$libphp_a = "{$package->getLibDir()}/libphp.a";
shell()->exec("{$ar} -t {$libphp_a} | grep '\\.a$' | xargs -n1 {$ar} d {$libphp_a}");
UnixUtil::exportDynamicSymbols($libphp_a);
}
#[Stage]
@@ -552,7 +560,8 @@ trait unix
if (file_exists(BUILD_BIN_PATH . '/php-config')) {
logger()->debug('Patching php-config prefix and libs order');
$php_config_str = FileSystem::readFile(BUILD_BIN_PATH . '/php-config');
$php_config_str = str_replace('prefix=""', 'prefix="' . BUILD_ROOT_PATH . '"', $php_config_str);
// anchor to start-of-line so we don't also match `program_prefix=""`
$php_config_str = preg_replace('/^prefix=""/m', 'prefix="' . BUILD_ROOT_PATH . '"', $php_config_str);
// move mimalloc to the beginning of libs
$php_config_str = preg_replace('/(libs=")(.*?)\s*(' . preg_quote(BUILD_LIB_PATH, '/') . '\/mimalloc\.o)\s*(.*?)"/', '$1$3 $2 $4"', $php_config_str);
// move lstdc++ to the end of libs

View File

@@ -293,21 +293,8 @@ trait windows
$fake_cli = $package->getBuildOption('with-micro-fake-cli', false) ? ' /DPHP_MICRO_FAKE_CLI' : '';
// phar patch for micro
$phar_patched = false;
if ($installer->isPackageResolved('ext-phar')) {
$phar_patched = true;
SourcePatcher::patchMicroPhar(self::getPHPVersionID());
}
try {
cmd()->cd($package->getSourceDir())
->exec("nmake /nologo {$debug_overrides}LIBS_MICRO=\"ws2_32.lib shell32.lib {$extra_libs}\" CFLAGS_MICRO=\"/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1{$fake_cli}\" EXTRA_LD_FLAGS_PROGRAM= micro");
} finally {
if ($phar_patched) {
SourcePatcher::unpatchMicroPhar();
}
}
cmd()->cd($package->getSourceDir())
->exec("nmake /nologo {$debug_overrides}LIBS_MICRO=\"ws2_32.lib shell32.lib {$extra_libs}\" CFLAGS_MICRO=\"/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1{$fake_cli}\" EXTRA_LD_FLAGS_PROGRAM= micro");
$this->deployWindowsBinary($builder, $package, 'php-micro');
}

View File

@@ -242,7 +242,10 @@ class ArtifactExtractor
}
logger()->info("Extracting binary [{$name}] to {$target_path}...");
$this->doStandardExtract($name, $cache_info, $target_path);
// When a binary artifact targets the shared buildroot, merge into it instead of wiping it.
// Wiping buildroot would destroy files installed by packages processed earlier in the build queue.
$merge = (FileSystem::convertPath($target_path) === FileSystem::convertPath(BUILD_ROOT_PATH));
$this->doStandardExtract($name, $cache_info, $target_path, $merge);
$artifact->emitAfterBinaryExtract($target_path, $platform);
logger()->debug("Emitted after-binary-extract hooks for [{$name}]");
@@ -256,8 +259,10 @@ class ArtifactExtractor
/**
* Standard extraction: extract entire archive to target directory.
*
* @param bool $merge when true, merge extracted files into existing target dir instead of wiping it
*/
protected function doStandardExtract(string $name, array $cache_info, string $target_path): void
protected function doStandardExtract(string $name, array $cache_info, string $target_path, bool $merge = false): void
{
$source_file = $this->cache->getCacheFullPath($cache_info);
$cache_type = $cache_info['cache_type'];
@@ -265,7 +270,7 @@ class ArtifactExtractor
// Validate source file exists before extraction
$this->validateSourceFile($name, $source_file, $cache_type);
$this->extractWithType($cache_type, $source_file, $target_path);
$this->extractWithType($cache_type, $source_file, $target_path, $merge);
}
/**
@@ -443,10 +448,10 @@ class ArtifactExtractor
* @param string $source_file Path to source file or directory
* @param string $target_path Target extraction path
*/
protected function extractWithType(string $cache_type, string $source_file, string $target_path): void
protected function extractWithType(string $cache_type, string $source_file, string $target_path, bool $merge = false): void
{
match ($cache_type) {
'archive' => $this->extractArchive($source_file, $target_path),
'archive' => $this->extractArchive($source_file, $target_path, $merge),
'file' => $this->copyFile($source_file, $target_path),
'git' => FileSystem::copyDir(FileSystem::convertPath($source_file), $target_path),
'local' => symlink(FileSystem::convertPath($source_file), $target_path),
@@ -458,8 +463,10 @@ class ArtifactExtractor
* Extract archive file to target directory.
*
* Supports: tar, tar.gz, tgz, tar.bz2, tar.xz, txz, zip, exe
*
* @param bool $merge when true, merge zip contents into existing target dir instead of wiping it
*/
protected function extractArchive(string $filename, string $target): void
protected function extractArchive(string $filename, string $target, bool $merge = false): void
{
$target = FileSystem::convertPath($target);
$filename = FileSystem::convertPath($filename);
@@ -476,7 +483,7 @@ class ArtifactExtractor
'Windows' => match ($extname) {
'tar' => default_shell()->executeTarExtract($filename, $target, 'none'),
'xz', 'txz', 'gz', 'tgz', 'bz2' => default_shell()->execute7zExtract($filename, $target),
'zip' => $this->unzipWithStrip($filename, $target),
'zip' => $this->unzipWithStrip($filename, $target, $merge),
'exe' => $this->copyFile($filename, $target),
default => throw new FileSystemException("Unknown archive format: {$filename}"),
},
@@ -485,7 +492,7 @@ class ArtifactExtractor
'gz', 'tgz' => default_shell()->executeTarExtract($filename, $target, 'gz'),
'bz2' => default_shell()->executeTarExtract($filename, $target, 'bz2'),
'xz', 'txz' => default_shell()->executeTarExtract($filename, $target, 'xz'),
'zip' => $this->unzipWithStrip($filename, $target),
'zip' => $this->unzipWithStrip($filename, $target, $merge),
'exe' => $this->copyFile($filename, $target),
default => throw new FileSystemException("Unknown archive format: {$filename}"),
},
@@ -496,7 +503,7 @@ class ArtifactExtractor
/**
* Unzip file with stripping top-level directory.
*/
protected function unzipWithStrip(string $zip_file, string $extract_path): bool
protected function unzipWithStrip(string $zip_file, string $extract_path, bool $merge = false): bool
{
$temp_dir = FileSystem::convertPath(sys_get_temp_dir() . '/spc_unzip_' . bin2hex(random_bytes(16)));
$zip_file = FileSystem::convertPath($zip_file);
@@ -517,15 +524,22 @@ class ArtifactExtractor
throw new FileSystemException('Cannot scan unzip temp dir: ' . $temp_dir);
}
// If extract path already exists, remove it
if (is_dir($extract_path)) {
FileSystem::removeDir($extract_path);
if (!$merge) {
// Replace mode: wipe the target directory before extracting
if (is_dir($extract_path)) {
FileSystem::removeDir($extract_path);
}
}
// If only one dir, move its contents to extract_path
// If only one dir, move/merge its contents to extract_path
$subdir = FileSystem::convertPath("{$temp_dir}/{$contents[0]}");
if (count($contents) === 1 && is_dir($subdir)) {
$this->moveFileOrDir($subdir, $extract_path);
if ($merge) {
$this->mergeDirContent($subdir, $extract_path);
FileSystem::removeDir($subdir);
} else {
$this->moveFileOrDir($subdir, $extract_path);
}
} else {
// Else, if it contains only one dir, strip dir and copy other files
$dircount = 0;
@@ -550,26 +564,36 @@ class ArtifactExtractor
throw new FileSystemException("Cannot scan unzip temp sub-dir: {$dir[0]}");
}
foreach ($sub_contents as $sub_item) {
$this->moveFileOrDir(
FileSystem::convertPath("{$temp_dir}/{$dir[0]}/{$sub_item}"),
FileSystem::convertPath("{$extract_path}/{$sub_item}")
);
$src = FileSystem::convertPath("{$temp_dir}/{$dir[0]}/{$sub_item}");
$dst = FileSystem::convertPath("{$extract_path}/{$sub_item}");
if ($merge && is_dir($src)) {
$this->mergeDirContent($src, $dst);
} else {
$this->moveFileOrDir($src, $dst);
}
}
} else {
foreach ($dir as $item) {
$this->moveFileOrDir(
FileSystem::convertPath("{$temp_dir}/{$item}"),
FileSystem::convertPath("{$extract_path}/{$item}")
);
$src = FileSystem::convertPath("{$temp_dir}/{$item}");
$dst = FileSystem::convertPath("{$extract_path}/{$item}");
if ($merge) {
$this->mergeDirContent($src, $dst);
} else {
$this->moveFileOrDir($src, $dst);
}
}
}
// Move top-level files to extract_path
// Move or copy top-level files to extract_path
foreach ($top_files as $top_file) {
$this->moveFileOrDir(
FileSystem::convertPath("{$temp_dir}/{$top_file}"),
FileSystem::convertPath("{$extract_path}/{$top_file}")
);
$src = FileSystem::convertPath("{$temp_dir}/{$top_file}");
$dst = FileSystem::convertPath("{$extract_path}/{$top_file}");
if ($merge) {
FileSystem::createDir(dirname($dst));
copy($src, $dst);
} else {
$this->moveFileOrDir($src, $dst);
}
}
}
@@ -595,6 +619,25 @@ class ArtifactExtractor
return str_replace(array_keys($replacement), array_values($replacement), $path);
}
private function mergeDirContent(string $src_dir, string $dest_dir): void
{
FileSystem::createDir($dest_dir);
$items = FileSystem::scanDirFiles($src_dir, false, true, true);
if ($items === false || empty($items)) {
return;
}
foreach ($items as $item) {
$src_item = FileSystem::convertPath("{$src_dir}/{$item}");
$dest_item = FileSystem::convertPath("{$dest_dir}/{$item}");
if (is_dir($src_item)) {
$this->mergeDirContent($src_item, $dest_item);
} else {
FileSystem::createDir(dirname($dest_item));
copy($src_item, $dest_item);
}
}
}
/**
* Move file or directory, handling cross-device scenarios
* Uses rename() if possible, falls back to copy+delete for cross-device moves

View File

@@ -20,7 +20,7 @@ class Git implements DownloadTypeInterface, CheckUpdateInterface
// direct branch clone
if (isset($config['rev'])) {
default_shell()->executeGitClone($config['url'], $config['rev'], $path, $shallow, $config['submodules'] ?? null);
default_shell()->executeGitClone($config['url'], $config['rev'], $path, $shallow, $config['submodules'] ?? null, $downloader->getRetry());
$shell = PHP_OS_FAMILY === 'Windows' ? cmd(false) : shell(false);
$hash_result = $shell->execWithResult(SPC_GIT_EXEC . ' -C ' . escapeshellarg($path) . ' rev-parse --short HEAD');
$hash = ($hash_result[0] === 0 && !empty($hash_result[1])) ? trim($hash_result[1][0]) : '';
@@ -66,7 +66,7 @@ class Git implements DownloadTypeInterface, CheckUpdateInterface
$version = array_key_first($matched_version_branch);
$branch = $matched_version_branch[$version];
logger()->info("Matched version {$version} from branch {$branch} for {$name}");
default_shell()->executeGitClone($config['url'], $branch, $path, $shallow, $config['submodules'] ?? null);
default_shell()->executeGitClone($config['url'], $branch, $path, $shallow, $config['submodules'] ?? null, $downloader->getRetry());
return DownloadResult::git($name, $config, extract: $config['extract'] ?? null, version: $version, downloader: static::class);
}
throw new DownloaderException("No matching branch found for regex {$config['regex']} (checked {$matched_count} branches).");

View File

@@ -21,13 +21,13 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface, CheckU
private ?string $version = null;
public function getGitHubReleases(string $name, string $repo, bool $prefer_stable = true, ?string $query = null): array
public function getGitHubReleases(string $name, string $repo, bool $prefer_stable = true, ?string $query = null, int $retries = 0): array
{
logger()->debug("Fetching {$name} GitHub releases from {$repo}");
$url = str_replace('{repo}', $repo, self::API_URL);
$url .= ($query ?? '');
$headers = $this->getGitHubTokenHeaders();
$data2 = default_shell()->executeCurl($url, headers: $headers);
$data2 = default_shell()->executeCurl($url, headers: $headers, retries: $retries);
$data = json_decode($data2 ?: '', true);
if (!is_array($data)) {
throw new DownloaderException("Failed to get GitHub release API info for {$repo} from {$url}");
@@ -46,13 +46,13 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface, CheckU
* Get the latest GitHub release assets for a given repository.
* match_asset is provided, only return the asset that matches the regex.
*/
public function getLatestGitHubRelease(string $name, string $repo, bool $prefer_stable, string $match_asset, ?string $query = null): array
public function getLatestGitHubRelease(string $name, string $repo, bool $prefer_stable, string $match_asset, ?string $query = null, int $retries = 0): array
{
logger()->debug("Fetching {$name} GitHub release from {$repo}");
$url = str_replace('{repo}', $repo, self::API_URL);
$url .= ($query ?? '');
$headers = $this->getGitHubTokenHeaders();
$data2 = default_shell()->executeCurl($url, headers: $headers);
$data2 = default_shell()->executeCurl($url, headers: $headers, retries: $retries);
$data = json_decode($data2 ?: '', true);
if (!is_array($data)) {
throw new DownloaderException("Failed to get GitHub release API info for {$repo} from {$url}");
@@ -84,7 +84,7 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface, CheckU
if (!isset($config['match'])) {
throw new DownloaderException("GitHubRelease downloader requires 'match' config for {$name}");
}
$rel = $this->getLatestGitHubRelease($name, $config['repo'], $config['prefer-stable'] ?? true, $config['match'], $config['query'] ?? null);
$rel = $this->getLatestGitHubRelease($name, $config['repo'], $config['prefer-stable'] ?? true, $config['match'], $config['query'] ?? null, $downloader->getRetry());
// download file using curl
$asset_url = str_replace(['{repo}', '{id}'], [$config['repo'], $rel['id']], self::ASSET_URL);
@@ -124,7 +124,7 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface, CheckU
if (!isset($config['match'])) {
throw new DownloaderException("GitHubRelease downloader requires 'match' config for {$name}");
}
$this->getLatestGitHubRelease($name, $config['repo'], $config['prefer-stable'] ?? true, $config['match'], $config['query'] ?? null);
$this->getLatestGitHubRelease($name, $config['repo'], $config['prefer-stable'] ?? true, $config['match'], $config['query'] ?? null, $downloader->getRetry());
$new_version = $this->version ?? $old_version ?? '';
return new CheckUpdateResult(
old: $old_version,

View File

@@ -22,11 +22,11 @@ class GitHubTarball implements DownloadTypeInterface, CheckUpdateInterface
* Get the GitHub tarball URL for a given repository and release type.
* If match_url is provided, only return the tarball that matches the regex.
*/
public function getGitHubTarballInfo(string $name, string $repo, string $rel_type, bool $prefer_stable = true, ?string $match_url = null, ?string $basename = null, ?string $query = null): array
public function getGitHubTarballInfo(string $name, string $repo, string $rel_type, bool $prefer_stable = true, ?string $match_url = null, ?string $basename = null, ?string $query = null, int $retries = 0): array
{
if ($rel_type === 'releases' && $match_url === null && $query === null && $prefer_stable) {
$api_url = str_replace(['{repo}', '{rel_type}'], [$repo, 'releases/latest'], self::API_URL);
$data = default_shell()->executeCurl($api_url, headers: $this->getGitHubTokenHeaders());
$data = default_shell()->executeCurl($api_url, headers: $this->getGitHubTokenHeaders(), retries: $retries);
$data = json_decode($data ?: '', true);
if (!is_array($data) || empty($data['tarball_url'])) {
throw new DownloaderException("Failed to get GitHub latest release for {$repo} from {$api_url}");
@@ -36,7 +36,7 @@ class GitHubTarball implements DownloadTypeInterface, CheckUpdateInterface
} else {
$api_url = str_replace(['{repo}', '{rel_type}'], [$repo, $rel_type], self::API_URL);
$api_url .= ($query ?? '');
$data = default_shell()->executeCurl($api_url, headers: $this->getGitHubTokenHeaders());
$data = default_shell()->executeCurl($api_url, headers: $this->getGitHubTokenHeaders(), retries: $retries);
$data = json_decode($data ?: '', true);
if (!is_array($data)) {
throw new DownloaderException("Failed to get GitHub tarball URL for {$repo} from {$api_url}");
@@ -65,7 +65,7 @@ class GitHubTarball implements DownloadTypeInterface, CheckUpdateInterface
}
$this->version = $version ?? null;
}
$head = default_shell()->executeCurl($rel_url, 'HEAD', headers: $this->getGitHubTokenHeaders()) ?: '';
$head = default_shell()->executeCurl($rel_url, 'HEAD', headers: $this->getGitHubTokenHeaders(), retries: $retries) ?: '';
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $head, $matches);
if ($matches) {
$filename = $matches['filename'];
@@ -84,9 +84,9 @@ class GitHubTarball implements DownloadTypeInterface, CheckUpdateInterface
'ghtagtar' => 'tags',
default => throw new DownloaderException("Invalid GitHubTarball type for {$name}"),
};
[$url, $filename] = $this->getGitHubTarballInfo($name, $config['repo'], $rel_type, $config['prefer-stable'] ?? true, $config['match'] ?? null, $name, $config['query'] ?? null);
[$url, $filename] = $this->getGitHubTarballInfo($name, $config['repo'], $rel_type, $config['prefer-stable'] ?? true, $config['match'] ?? null, $name, $config['query'] ?? null, $downloader->getRetry());
$path = DOWNLOAD_PATH . "/{$filename}";
default_shell()->executeCurlDownload($url, $path, headers: $this->getGitHubTokenHeaders());
default_shell()->executeCurlDownload($url, $path, headers: $this->getGitHubTokenHeaders(), retries: $downloader->getRetry());
return DownloadResult::archive($filename, $config, $config['extract'] ?? null, version: $this->version, downloader: static::class);
}
@@ -97,7 +97,7 @@ class GitHubTarball implements DownloadTypeInterface, CheckUpdateInterface
'ghtagtar' => 'tags',
default => throw new DownloaderException("Invalid GitHubTarball type for {$name}"),
};
$this->getGitHubTarballInfo($name, $config['repo'], $rel_type, $config['prefer-stable'] ?? true, $config['match'] ?? null, $name, $config['query'] ?? null);
$this->getGitHubTarballInfo($name, $config['repo'], $rel_type, $config['prefer-stable'] ?? true, $config['match'] ?? null, $name, $config['query'] ?? null, $downloader->getRetry());
$new_version = $this->version ?? $old_version ?? '';
return new CheckUpdateResult(
old: $old_version,

View File

@@ -24,7 +24,7 @@ class PhpRelease implements DownloadTypeInterface, ValidatorInterface, CheckUpda
public function download(string $name, array $config, ArtifactDownloader $downloader): DownloadResult
{
$phpver = $downloader->getOption('with-php', '8.4');
$phpver = $downloader->getOption('with-php', '8.5');
// Handle 'git' version to clone from php-src repository
if ($phpver === 'git') {
$this->sha256 = null;
@@ -74,7 +74,7 @@ class PhpRelease implements DownloadTypeInterface, ValidatorInterface, CheckUpda
public function checkUpdate(string $name, array $config, ?string $old_version, ArtifactDownloader $downloader): CheckUpdateResult
{
$phpver = $downloader->getOption('with-php', '8.4');
$phpver = $downloader->getOption('with-php', '8.5');
if ($phpver === 'git') {
// git version: delegate to Git checkUpdate with master branch
return (new Git())->checkUpdate($name, ['url' => 'https://github.com/php/php-src.git', 'rev' => 'master'], $old_version, $downloader);
@@ -90,7 +90,7 @@ class PhpRelease implements DownloadTypeInterface, ValidatorInterface, CheckUpda
protected function fetchPhpReleaseInfo(string $name, array $config, ArtifactDownloader $downloader): array
{
$phpver = $downloader->getOption('with-php', '8.4');
$phpver = $downloader->getOption('with-php', '8.5');
// Handle 'git' version to clone from php-src repository
if ($phpver === 'git') {
// cannot fetch release info for git version, return empty info to skip validation

View File

@@ -48,10 +48,11 @@ class DownloaderOptions
$shortU = $prefix ? null : 'U';
$shortG = $prefix ? null : 'G';
$shortL = $prefix ? null : 'L';
$shortI = $prefix ? null : 'i';
return [
// php version option
new InputOption("{$p}with-php", null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format (default 8.4)', '8.4'),
new InputOption("{$p}with-php", null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format (default 8.5)', '8.5'),
// download preference options
new InputOption("{$p}prefer-source", null, InputOption::VALUE_OPTIONAL, 'Prefer source downloads when both source and binary are available', false),
@@ -62,7 +63,7 @@ class DownloaderOptions
// download behavior options
new InputOption("{$p}parallel", $shortP, InputOption::VALUE_REQUIRED, 'Number of parallel downloads (default 1)', '1'),
new InputOption("{$p}retry", $shortR, InputOption::VALUE_REQUIRED, 'Number of download retries on failure (default 0)', '0'),
new InputOption("{$p}ignore-cache", null, InputOption::VALUE_OPTIONAL, 'Ignore some caches when downloading, comma separated, e.g "php-src,curl,openssl"', false),
new InputOption("{$p}ignore-cache", $shortI, InputOption::VALUE_OPTIONAL, 'Ignore some caches when downloading, comma separated, e.g "php-src,curl,openssl"', false),
new InputOption("{$p}no-alt", null, null, 'Do not use alternative mirror download artifacts for sources'),
new InputOption("{$p}no-shallow-clone", null, null, 'Do not clone shallowly repositories when downloading sources'),

View File

@@ -26,7 +26,7 @@ class CheckUpdateCommand extends BaseCommand
$this->addOption('parallel', 'p', InputOption::VALUE_REQUIRED, 'Number of parallel update checks (default: 10)', 10);
// --with-php option for checking updates with a specific PHP version context
$this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format (default 8.4)', '8.4');
$this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format (default 8.5)', '8.5');
}
public function handle(): int

View File

@@ -0,0 +1,377 @@
<?php
declare(strict_types=1);
namespace StaticPHP\Command\Dev;
use StaticPHP\Command\BaseCommand;
use StaticPHP\Config\PackageConfig;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
#[AsCommand('dev:gen-ext-test-matrix', 'Generate GitHub Actions extension test matrix JSON', [], true)]
class GenExtTestMatrixCommand extends BaseCommand
{
private const string BUILD_TARGETS = '--build-cli --build-cgi --build-micro --with-suggests -vvv';
private const array OS_RUNNERS = [
'linux' => ['arch' => 'x86_64', 'runner' => 'ubuntu-latest', 'os_key' => 'Linux'],
'windows' => ['arch' => 'x86_64', 'runner' => 'windows-latest', 'os_key' => 'Windows'],
'macos' => ['arch' => 'aarch64', 'runner' => 'macos-15', 'os_key' => 'Darwin'],
];
/**
* Tier 2 runners: Linux aarch64 + macOS x86_64, no Windows.
*/
private const array OS_RUNNERS_TIER2 = [
'linux' => ['arch' => 'aarch64', 'runner' => 'ubuntu-24.04-arm', 'os_key' => 'Linux'],
'macos' => ['arch' => 'x86_64', 'runner' => 'macos-15-intel', 'os_key' => 'Darwin'],
];
/**
* Extensions excluded from specific OS matrix entries.
*/
private const array OS_EXCLUDE = [
'linux' => ['glfw'],
];
/**
* Extra build flags appended when a matrix entry contains any of the listed extensions.
* Key: extension display name (without ext- prefix). Value: extra flags string.
*/
private const array EXTRA_BUILD_FLAGS = [
'parallel' => '--enable-zts',
];
/**
* Pairs of extensions that cannot be built together in the same matrix entry.
*/
private const array CONFLICTS = [
['grpc', 'protobuf'],
['swow', 'swoole'],
];
/**
* Extensions that must always appear alone in their own matrix entry.
* Use display names (without ext- prefix).
*/
private const array STANDALONE = [
'grpc',
'glfw',
'imagick',
'intl',
];
/**
* Extensions that are emitted as isolated standalone entries.
*/
private const array STANDALONE_ISOLATED = [
'swow' => '',
'swoole' => 'swoole-hook-',
];
/**
* Maximum number of orphan extensions per matrix entry.
*/
private const int ORPHAN_BATCH_SIZE = 15;
protected bool $no_motd = true;
public function configure(): void
{
$this->addOption('for-extensions', null, InputOption::VALUE_OPTIONAL, 'Filter by extension display names, comma-separated', '')
->addOption('for-libs', null, InputOption::VALUE_OPTIONAL, 'Filter by lib names (depends+suggests), comma-separated', '')
->addOption('os', null, InputOption::VALUE_OPTIONAL, 'Filter by OS (Linux/Darwin/Windows), comma-separated', '')
->addOption('tier2', null, InputOption::VALUE_NONE, 'Use Tier 2 runners (Linux aarch64 + macOS x86_64, no Windows)');
}
public function handle(): int
{
if (!spc_mode(SPC_MODE_SOURCE)) {
$this->output->writeln('<error>This command is only available in source mode.</error>');
return static::USER_ERROR;
}
$parse_option = fn (string $name): array => array_values(array_filter(array_map('trim', explode(',', (string) $this->input->getOption($name)))));
$filter_extensions = $parse_option('for-extensions');
$filter_libs = $parse_option('for-libs');
$filter_os_keys = $parse_option('os');
$tier2 = (bool) $this->input->getOption('tier2');
$base_runners = $tier2 ? self::OS_RUNNERS_TIER2 : self::OS_RUNNERS;
$all = PackageConfig::getAll();
// Separate into regular and virtual extensions (build-static:false excluded globally)
$all_regular = [];
$all_virtual = [];
foreach ($all as $pkg_name => $config) {
if (($config['type'] ?? '') !== 'php-extension') {
continue;
}
if (($config['php-extension']['build-static'] ?? null) === false) {
continue;
}
if (($config['php-extension']['arg-type'] ?? '') === 'none') {
$all_virtual[$pkg_name] = $config;
} else {
$all_regular[$pkg_name] = $config;
}
}
$os_runners = empty($filter_os_keys)
? $base_runners
: array_filter($base_runners, fn ($info) => in_array($info['os_key'], $filter_os_keys, true));
$entries = [];
$all_ext_lib_deps = [];
foreach ($os_runners as $os => $os_info) {
$os_key = $os_info['os_key'];
// Filter by OS support
$os_exclude = array_fill_keys(array_map(fn ($n) => 'ext-' . $n, self::OS_EXCLUDE[$os] ?? []), true);
$os_regular = array_filter($all_regular, fn ($c, $k) => $this->supportsOS($c, $os_key) && !isset($os_exclude[$k]), ARRAY_FILTER_USE_BOTH);
$os_virtual = array_filter($all_virtual, fn ($c, $k) => $this->supportsOS($c, $os_key) && !isset($os_exclude[$k]), ARRAY_FILTER_USE_BOTH);
// Pool: all ext-* names available on this OS (regular + virtual)
$pool_set = array_fill_keys(
array_merge(array_keys($os_regular), array_keys($os_virtual)),
true
);
// Compute ext_deps for every pool member: union of depends + suggests, limited to pool
$ext_deps = [];
$os_lib_deps = [];
foreach (array_merge($os_regular, $os_virtual) as $pkg_name => $config) {
$raw = array_merge(
$this->resolvePlatformList($config, 'depends', $os),
$this->resolvePlatformList($config, 'suggests', $os),
);
$ext_deps[$pkg_name] = array_values(array_filter(
$raw,
fn ($d) => isset($pool_set[$d]) && $d !== $pkg_name
));
$os_lib_deps[$this->displayName($pkg_name)] = array_values(array_filter(
$raw,
fn ($d) => !str_starts_with($d, 'ext-')
));
}
$all_ext_lib_deps[$os] = $os_lib_deps;
// Which regular exts are reachable as a dep/suggest from another regular ext?
$depended_on = [];
foreach ($os_regular as $pkg_name => $_) {
foreach ($ext_deps[$pkg_name] as $dep) {
$depended_on[$dep] = true;
}
}
// Process order: roots (not depended on) first, then non-roots; each group alpha-sorted
$roots = [];
$non_roots = [];
foreach (array_keys($os_regular) as $pkg_name) {
if (isset($depended_on[$pkg_name])) {
$non_roots[] = $pkg_name;
} else {
$roots[] = $pkg_name;
}
}
sort($roots);
sort($non_roots);
// DFS to collect dependency chains; true orphans (no ext-* relations) are batched
$covered = [];
$groups = [];
$orphans = [];
$standalone_set = array_fill_keys(self::STANDALONE, true);
$standalone_isolated = self::STANDALONE_ISOLATED;
foreach (array_merge($roots, $non_roots) as $ext) {
if (isset($covered[$ext])) {
continue;
}
$display = $this->displayName($ext);
if (array_key_exists($display, $standalone_isolated)) {
// Isolated standalone: mark only this ext + its hook virtuals as covered
$covered[$ext] = true;
$hook_prefix = $standalone_isolated[$display];
$group_names = [$display];
if ($hook_prefix !== '') {
foreach ($os_virtual as $vpkg => $_) {
$vdisplay = $this->displayName($vpkg);
if (str_starts_with($vdisplay, $hook_prefix) && !isset($covered[$vpkg])) {
$covered[$vpkg] = true;
$group_names[] = $vdisplay;
}
}
sort($group_names);
}
$groups[] = implode(',', $group_names);
continue;
}
$chain = $this->dfsCollect($ext, $ext_deps, $pool_set, $covered);
if (isset($standalone_set[$display])) {
// Always emit standalone extensions as their own single entry
$groups[] = $display;
} elseif (count($chain) === 1 && empty($ext_deps[$ext])) {
$orphans[] = $display;
} else {
$groups[] = implode(',', array_map($this->displayName(...), $chain));
}
}
// Batch orphans, splitting conflicting extensions into separate entries
if (!empty($orphans)) {
sort($orphans);
foreach ($this->splitOrphansByConflicts($orphans) as $batch) {
$groups[] = implode(',', $batch);
}
}
sort($groups);
foreach ($groups as $group) {
$extra = $this->extraBuildFlags($group);
$entries[] = [
'runner' => $os_info['runner'],
'os' => $os,
'arch' => $os_info['arch'],
'extension' => $group,
'build-args' => './bin/spc build "' . $group . '" ' . self::BUILD_TARGETS . ($extra !== '' ? ' ' . $extra : ''),
];
}
}
if (!empty($filter_extensions)) {
$entries = array_values(array_filter($entries, function (array $entry) use ($filter_extensions): bool {
$names = explode(',', $entry['extension']);
return count(array_intersect($names, $filter_extensions)) > 0;
}));
}
if (!empty($filter_libs)) {
$entries = array_values(array_filter($entries, function (array $entry) use ($filter_libs, $all_ext_lib_deps): bool {
$names = explode(',', $entry['extension']);
$lib_deps = $all_ext_lib_deps[$entry['os']] ?? [];
foreach ($names as $name) {
if (count(array_intersect($lib_deps[$name] ?? [], $filter_libs)) > 0) {
return true;
}
}
return false;
}));
}
$this->output->write(json_encode($entries, JSON_UNESCAPED_SLASHES));
return static::SUCCESS;
}
/**
* DFS-collect the dependency chain starting from $ext.
* Marks all visited nodes in $covered to prevent duplicates and handle cycles.
*/
private function dfsCollect(string $ext, array $ext_deps, array $pool_set, array &$covered): array
{
if (isset($covered[$ext])) {
return [];
}
$covered[$ext] = true;
$chain = [$ext];
foreach ($ext_deps[$ext] ?? [] as $dep) {
if (!isset($covered[$dep]) && isset($pool_set[$dep])) {
$chain = array_merge($chain, $this->dfsCollect($dep, $ext_deps, $pool_set, $covered));
}
}
return $chain;
}
private function supportsOS(array $config, string $os_key): bool
{
$os_list = $config['php-extension']['os'] ?? null;
return $os_list === null || in_array($os_key, $os_list, true);
}
private function displayName(string $pkg_name): string
{
return str_starts_with($pkg_name, 'ext-') ? substr($pkg_name, 4) : $pkg_name;
}
/**
* Split orphans into batches such that no two conflicting extensions share a batch.
* Uses a greedy graph-coloring approach.
*
* @param string[] $orphans display names, pre-sorted
* @return string[][] array of batches, each batch is an array of display names
*/
private function splitOrphansByConflicts(array $orphans): array
{
$adjacency = [];
foreach (self::CONFLICTS as [$a, $b]) {
$adjacency[$a][$b] = true;
$adjacency[$b][$a] = true;
}
$batches = [];
foreach ($orphans as $ext) {
$placed = false;
foreach ($batches as &$batch) {
if (count($batch) >= self::ORPHAN_BATCH_SIZE) {
continue;
}
$conflict = false;
foreach ($batch as $member) {
if (isset($adjacency[$ext][$member])) {
$conflict = true;
break;
}
}
if (!$conflict) {
$batch[] = $ext;
$placed = true;
break;
}
}
unset($batch);
if (!$placed) {
$batches[] = [$ext];
}
}
return $batches;
}
/**
* Returns any extra build flags required for an extension group string.
* Checks whether any extension in the comma-separated group matches EXTRA_BUILD_FLAGS.
*/
private function extraBuildFlags(string $group): string
{
$names = explode(',', $group);
$flags = [];
foreach (self::EXTRA_BUILD_FLAGS as $ext => $extra) {
if (in_array($ext, $names, true)) {
$flags[] = $extra;
}
}
return implode(' ', $flags);
}
/**
* Resolve the value of a platform-specific array field, applying the suffix fallback chain.
*
* Fallback rules (same as PackageConfig::get):
* linux : @linux → @unix → (base)
* macos : @macos → @unix → (base)
* windows : @windows → (base)
*/
private function resolvePlatformList(array $config, string $field, string $platform): array
{
return match ($platform) {
'linux' => $config["{$field}@linux"] ?? $config["{$field}@unix"] ?? $config[$field] ?? [],
'macos' => $config["{$field}@macos"] ?? $config["{$field}@unix"] ?? $config[$field] ?? [],
'windows' => $config["{$field}@windows"] ?? $config[$field] ?? [],
default => $config[$field] ?? [],
};
}
}

View File

@@ -0,0 +1,313 @@
<?php
declare(strict_types=1);
namespace StaticPHP\Command\Dev;
use StaticPHP\Artifact\Downloader\Type\GitHubTokenSetupTrait;
use StaticPHP\Command\BaseCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
#[AsCommand('dev:test-bot', 'Analyze PR changes and labels, output test-bot metadata JSON', [], true)]
class TestBotCommand extends BaseCommand
{
use GitHubTokenSetupTrait;
private const string API_BASE = 'https://api.github.com';
/** Platform labels → os_key used by dev:gen-ext-test-matrix --os= */
private const array PLATFORM_LABELS = [
'test/linux' => 'Linux',
'test/windows' => 'Windows',
'test/macos' => 'Darwin',
];
private const string TIER2_LABEL = 'test/tier2';
/** PHP version labels → version string (8.5 is always included as default) */
private const array PHP_VERSION_LABELS = [
'test/php-83' => '8.3',
'test/php-84' => '8.4',
];
private const string DEFAULT_PHP_VERSION = '8.5';
protected bool $no_motd = true;
public function configure(): void
{
$this->addOption('pr', null, InputOption::VALUE_REQUIRED, 'Pull request number')
->addOption('repo', null, InputOption::VALUE_REQUIRED, 'Repository in owner/repo format (e.g. owner/repo)')
->addOption('mock-files', null, InputOption::VALUE_REQUIRED, 'Comma-separated file paths to simulate PR changed files (skips GitHub API, for local testing)', '')
->addOption('mock-labels', null, InputOption::VALUE_REQUIRED, 'Comma-separated labels to simulate PR labels (skips GitHub API, for local testing)', '');
}
public function handle(): int
{
$mock_files_raw = (string) $this->input->getOption('mock-files');
$mock_labels_raw = (string) $this->input->getOption('mock-labels');
$is_mock = $mock_files_raw !== '' || $mock_labels_raw !== '';
if ($is_mock) {
// Local testing mode: skip all GitHub API calls
$changed_files = array_map(
fn ($f) => ['filename' => trim($f)],
array_filter(explode(',', $mock_files_raw))
);
$label_names = array_map('trim', array_filter(explode(',', $mock_labels_raw)));
} else {
$pr = (int) $this->input->getOption('pr');
$repo = (string) $this->input->getOption('repo');
if ($pr <= 0 || $repo === '') {
$this->output->writeln('<error>Either --mock-files/--mock-labels (local test) or --pr and --repo (live) are required.</error>');
return static::USER_ERROR;
}
$headers = array_merge(
$this->getGitHubTokenHeaders(),
['Accept: application/vnd.github+json', 'X-GitHub-Api-Version: 2022-11-28'],
);
// Fetch changed files (paginated, up to 300)
$changed_files = $this->fetchPaginatedFiles($repo, $pr, $headers);
// Fetch current labels on the PR/issue
$labels_raw = $this->apiGet(
sprintf('%s/repos/%s/issues/%d/labels', self::API_BASE, $repo, $pr),
$headers
);
$label_names = array_column($labels_raw ?? [], 'name');
}
// Analyze changed files → extensions, libs, targets
[$extensions, $libs, $targets] = $this->analyzeChangedFiles($changed_files);
// Resolve active platform OS keys (used as filters, not as trigger)
$os_keys = [];
foreach (self::PLATFORM_LABELS as $label => $os_key) {
if (in_array($label, $label_names, true)) {
$os_keys[] = $os_key;
}
}
$tier2 = in_array(self::TIER2_LABEL, $label_names, true);
$need_test = in_array('need-test', $label_names, true);
// Resolve PHP versions (default always included)
$php_versions = [self::DEFAULT_PHP_VERSION];
foreach (self::PHP_VERSION_LABELS as $label => $version) {
if (in_array($label, $label_names, true)) {
$php_versions[] = $version;
}
}
$php_versions = array_unique($php_versions);
sort($php_versions);
// Build gen_matrix_args whenever need-test is set.
// Platform labels narrow the OS scope; absent = no --os filter (all platforms).
$gen_matrix_args = '';
$gen_matrix_args_tier2 = '';
if ($need_test) {
$flag_parts = [];
if (!empty($extensions)) {
$flag_parts[] = '--for-extensions=' . implode(',', $extensions);
}
if (!empty($libs)) {
$flag_parts[] = '--for-libs=' . implode(',', $libs);
}
if (!empty($os_keys)) {
$flag_parts[] = '--os=' . implode(',', $os_keys);
}
$gen_matrix_args = implode(' ', $flag_parts);
if ($tier2) {
// Tier2 covers Linux + macOS only (never Windows)
$tier2_os = array_values(array_filter(
!empty($os_keys) ? $os_keys : ['Linux', 'Darwin'],
fn ($k) => $k !== 'Windows'
));
if (!empty($tier2_os)) {
$tier2_parts = array_values(array_filter($flag_parts, fn ($f) => !str_starts_with($f, '--os=')));
$tier2_parts[] = '--os=' . implode(',', $tier2_os);
$tier2_parts[] = '--tier2';
$gen_matrix_args_tier2 = implode(' ', $tier2_parts);
}
}
}
$comment_body = $this->buildCommentBody(
$extensions,
$libs,
$targets,
$label_names,
$os_keys,
$tier2,
$php_versions,
$need_test,
);
$result = [
'need_test' => $need_test,
'extensions' => array_values($extensions),
'libs' => array_values($libs),
'targets' => array_values($targets),
'gen_matrix_args' => $gen_matrix_args,
'gen_matrix_args_tier2' => $gen_matrix_args_tier2,
'php_versions' => array_values($php_versions),
'tier2' => $tier2,
'comment_body' => $comment_body,
];
$this->output->write(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
return static::SUCCESS;
}
/**
* Fetch all changed files for a PR across up to 3 pages (max 300 files).
*/
private function fetchPaginatedFiles(string $repo, int $pr, array $headers): array
{
$files = [];
for ($page = 1; $page <= 3; ++$page) {
$url = sprintf('%s/repos/%s/pulls/%d/files?per_page=100&page=%d', self::API_BASE, $repo, $pr, $page);
$batch = $this->apiGet($url, $headers);
if (empty($batch)) {
break;
}
$files = array_merge($files, $batch);
if (count($batch) < 100) {
break;
}
}
return $files;
}
/**
* Perform a GET request and return decoded JSON array, or null on failure.
*/
private function apiGet(string $url, array $headers): ?array
{
$data = default_shell()->executeCurl($url, headers: $headers);
$decoded = json_decode($data ?: '', true);
return is_array($decoded) ? $decoded : null;
}
/**
* Analyze changed file paths and classify them into extensions, libs, and targets.
*
* @return array{string[], string[], string[]}
*/
private function analyzeChangedFiles(array $files): array
{
$extensions = [];
$libs = [];
$targets = [];
foreach ($files as $file) {
$path = $file['filename'] ?? '';
if (preg_match('#^src/Package/Extension/([^/]+)\.php$#', $path, $m)) {
$name = strtolower($m[1]);
$extensions[$name] = $name;
} elseif (preg_match('#^config/pkg/ext/ext-([^/]+)\.yml$#', $path, $m)) {
$extensions[$m[1]] = $m[1];
} elseif (preg_match('#^src/Package/Library/([^/]+)\.php$#', $path, $m)) {
$name = strtolower($m[1]);
$libs[$name] = $name;
} elseif (preg_match('#^config/pkg/lib/([^/]+)\.yml$#', $path, $m)) {
$libs[$m[1]] = $m[1];
} elseif (preg_match('#^src/Package/Target/([^/]+)\.php$#', $path, $m)) {
$name = strtolower($m[1]);
$targets[$name] = $name;
} elseif (preg_match('#^config/pkg/target/([^/]+)\.yml$#', $path, $m)) {
$targets[$m[1]] = $m[1];
}
}
sort($extensions);
sort($libs);
sort($targets);
return [$extensions, $libs, $targets];
}
private function buildCommentBody(
array $extensions,
array $libs,
array $targets,
array $label_names,
array $os_keys,
bool $tier2,
array $php_versions,
bool $need_test,
): string {
$fmt = static fn (array $items): string => !empty($items)
? '`' . implode('`, `', $items) . '`'
: '_none_';
$detected = sprintf(
'**Detected**: Extensions: %s | Libraries: %s | Targets: %s',
$fmt($extensions),
$fmt($libs),
$fmt($targets),
);
// Case 1: need-test absent → invite the author to add it
if (!$need_test) {
return implode("\n", [
'<!-- spc-test-bot -->',
'**StaticPHP Test Bot**',
'',
$detected,
'',
'To trigger extension build tests on this PR, add the `need-test` label:',
'',
'**Gate**: `need-test`',
'**Platform filter** (optional, default all): `test/linux` `test/windows` `test/macos` · `test/tier2`',
'**PHP version** (optional, default 8.5): `test/php-83` `test/php-84`',
]);
}
// Case 2: need-test present → show what will run
// os_keys empty = no filter = all platforms
$effective_os = !empty($os_keys)
? $os_keys
: array_values(self::PLATFORM_LABELS); // all OS keys
$platform_parts = [];
foreach (self::PLATFORM_LABELS as $_label => $os_key) {
if (!in_array($os_key, $effective_os, true)) {
continue;
}
$platform_parts[] = match ($os_key) {
'Linux' => 'Linux x86_64',
'Darwin' => 'macOS arm64',
/* @phpstan-ignore-next-line */
'Windows' => 'Windows x86_64',
default => $os_key,
};
}
if ($tier2) {
if (in_array('Linux', $effective_os, true)) {
$platform_parts[] = 'Linux aarch64 (Tier2)';
}
if (in_array('Darwin', $effective_os, true)) {
$platform_parts[] = 'macOS x86_64 (Tier2)';
}
}
$php_str = implode(', ', array_map(fn ($v) => "PHP {$v}", $php_versions)) . ' NTS';
$active_test_labels = array_values(array_filter($label_names, fn ($l) => str_starts_with($l, 'test/')));
$labels_str = !empty($active_test_labels) ? '`' . implode('`, `', $active_test_labels) . '`' : '_none_';
return implode("\n", [
'<!-- spc-test-bot -->',
'**StaticPHP Test Bot**',
'',
$detected,
'**Active labels**: ' . $labels_str,
'**Config**: ' . implode(' + ', $platform_parts) . ' | ' . $php_str,
]);
}
}

View File

@@ -13,11 +13,13 @@ use StaticPHP\Command\Dev\DumpStagesCommand;
use StaticPHP\Command\Dev\EnvCommand;
use StaticPHP\Command\Dev\GenDepsDataCommand;
use StaticPHP\Command\Dev\GenExtDocsCommand;
use StaticPHP\Command\Dev\GenExtTestMatrixCommand;
use StaticPHP\Command\Dev\IsInstalledCommand;
use StaticPHP\Command\Dev\LintConfigCommand;
use StaticPHP\Command\Dev\PackageInfoCommand;
use StaticPHP\Command\Dev\PackLibCommand;
use StaticPHP\Command\Dev\ShellCommand;
use StaticPHP\Command\Dev\TestBotCommand;
use StaticPHP\Command\DoctorCommand;
use StaticPHP\Command\DownloadCommand;
use StaticPHP\Command\DumpExtensionsCommand;
@@ -85,6 +87,8 @@ class ConsoleApplication extends Application
new PackageInfoCommand(),
new GenExtDocsCommand(),
new GenDepsDataCommand(),
new GenExtTestMatrixCommand(),
new TestBotCommand(),
]);
// add additional commands from registries

View File

@@ -350,7 +350,10 @@ class PackageInstaller
}
// Fallback: if the download cache is missing (e.g. download failed or cache was cleared),
// still check whether the files are physically present in buildroot.
if ($package instanceof LibraryPackage) {
// Note: TargetPackage extends LibraryPackage, but target packages (e.g. zig) have no
// static-libs/headers configured, so isInstalled() would trivially return true for them.
// Only apply this fallback to pure library packages.
if ($package instanceof LibraryPackage && !($package instanceof TargetPackage)) {
return $package->isInstalled();
}
return false;

View File

@@ -499,6 +499,7 @@ class PackageLoader
throw new RegistryException('Package name must not be empty when no package context is available for BeforeStage attribute.');
}
$package_name = $method_instance->package_name === '' ? $pkg->getName() : $method_instance->package_name;
$conditionals = array_map(
fn (\ReflectionAttribute $a) => $a->newInstance()->class,
[...$method->getDeclaringClass()->getAttributes(ConditionalOn::class), ...$method->getAttributes(ConditionalOn::class)],
@@ -529,6 +530,7 @@ class PackageLoader
throw new RegistryException('Package name must not be empty when no package context is available for AfterStage attribute.');
}
$package_name = $method_instance->package_name === '' ? $pkg->getName() : $method_instance->package_name;
$conditionals = array_map(
fn (\ReflectionAttribute $a) => $a->newInstance()->class,
[...$method->getDeclaringClass()->getAttributes(ConditionalOn::class), ...$method->getAttributes(ConditionalOn::class)],

View File

@@ -44,6 +44,7 @@ class DefaultShell extends Shell
$cmd = SPC_CURL_EXEC . " -sfSL --max-time 3600 {$retry_arg} {$compressed_arg} {$method_arg} {$header_arg} {$url_arg}";
$this->logCommandInfo($cmd);
logger()->debug("[CURL EXECUTE] {$cmd}");
$result = $this->passthru($cmd, capture_output: true, throw_on_error: false);
$ret = $result['code'];
$output = $result['output'];
@@ -83,7 +84,7 @@ class DefaultShell extends Shell
/**
* Execute a Git clone command to clone a repository.
*/
public function executeGitClone(string $url, string $branch, string $path, bool $shallow = true, ?array $submodules = null): void
public function executeGitClone(string $url, string $branch, string $path, bool $shallow = true, ?array $submodules = null, int $retries = 0): void
{
$path = FileSystem::convertPath($path);
if (file_exists($path)) {
@@ -98,7 +99,21 @@ class DefaultShell extends Shell
$cmd = clean_spaces("{$git} clone -c http.lowSpeedLimit=1 -c http.lowSpeedTime=3600 --config core.autocrlf=false --branch {$branch_arg} {$shallow_arg} {$submodules_arg} {$url_arg} {$path_arg}");
$this->logCommandInfo($cmd);
logger()->debug("[GIT CLONE] {$cmd}");
$this->passthru($cmd, $this->console_putput);
try {
$this->passthru($cmd, $this->console_putput);
} catch (InterruptException $e) {
throw $e;
} catch (\Throwable $e) {
if ($retries > 0) {
logger()->warning("Git clone failed, retrying... ({$retries} retries left)");
if (is_dir($path)) {
FileSystem::removeDir($path);
}
$this->executeGitClone($url, $branch, $path, $shallow, $submodules, $retries - 1);
return;
}
throw $e;
}
if ($submodules !== null) {
$depth_flag = $shallow ? '--depth 1' : '';
foreach ($submodules as $submodule) {

View File

@@ -8,7 +8,7 @@ assert(function_exists('curl_exec'));
assert(function_exists('curl_close'));
assert(function_exists('curl_version'));
$curl_version = curl_version();
if (stripos($curl_version['ssl_version'], 'schannel') !== false) {
if (stripos($curl_version['ssl_version'], 'schannel') !== false && !extension_loaded('swow')) {
$domain_list = [
'https://captive.apple.com/',
'https://detectportal.firefox.com/',

View File

@@ -50,7 +50,7 @@ $prefer_pre_built = false;
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
$extensions = match (PHP_OS_FAMILY) {
'Linux', 'Darwin' => 'curl,swoole',
'Linux', 'Darwin' => 'openssl,zstd,clickhouse',
'Windows' => 'intl',
};