Skip to content

Commit 3c0cc5b

Browse files
committed
allow downloading multiple php versions
1 parent 64f7a35 commit 3c0cc5b

File tree

6 files changed

+109
-20
lines changed

6 files changed

+109
-20
lines changed

src/SPC/builder/BuilderBase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function proveExts(array $static_extensions, array $shared_extensions = [
160160
}
161161
if (!$skip_extract) {
162162
$this->emitPatchPoint('before-php-extract');
163-
SourceManager::initSource(sources: ['php-src'], source_only: true);
163+
SourceManager::initSource(sources: [$this->getPhpSrcName()], source_only: true);
164164
$this->emitPatchPoint('after-php-extract');
165165
if ($this->getPHPVersionID() >= 80000) {
166166
$this->emitPatchPoint('before-micro-extract');
@@ -319,7 +319,7 @@ public function getPHPVersion(bool $exception_on_failure = true): string
319319
public function getPHPVersionFromArchive(?string $file = null): false|string
320320
{
321321
if ($file === null) {
322-
$lock = LockFile::get('php-src');
322+
$lock = LockFile::get($this->getPhpSrcName());
323323
if ($lock === null) {
324324
return false;
325325
}
@@ -498,6 +498,14 @@ public function checkBeforeBuildPHP(int $rule): void
498498
}
499499
}
500500

501+
/**
502+
* Get the php-src name to use for lock file lookups (supports version-specific names like php-src-8.2)
503+
*/
504+
protected function getPhpSrcName(): string
505+
{
506+
return getenv('SPC_PHP_SRC_NAME') ?: 'php-src';
507+
}
508+
501509
/**
502510
* Generate micro extension test php code.
503511
*/

src/SPC/command/BuildPHPCommand.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function configure(): void
4949
$this->addOption('with-micro-logo', null, InputOption::VALUE_REQUIRED, 'Use custom .ico for micro.sfx (windows only)');
5050
$this->addOption('enable-micro-win32', null, null, 'Enable win32 mode for phpmicro (Windows only)');
5151
$this->addOption('with-frankenphp-app', null, InputOption::VALUE_REQUIRED, 'Path to a folder to be embedded in FrankenPHP');
52+
$this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'PHP version to build (e.g., 8.2, 8.3, 8.4). Uses php-src-X.Y if available, otherwise php-src');
5253
}
5354

5455
public function handle(): int
@@ -120,6 +121,36 @@ public function handle(): int
120121
logger()->warning('Some cases micro.sfx cannot be packed via UPX due to dynamic size bug, be aware!');
121122
}
122123
}
124+
125+
// Determine which php-src to use based on --with-php option
126+
$php_version = $this->getOption('with-php');
127+
if ($php_version !== null) {
128+
// Check if version-specific php-src exists in lock file
129+
$version_specific_name = "php-src-{$php_version}";
130+
$lock_file_path = DOWNLOAD_PATH . '/.lock.json';
131+
if (file_exists($lock_file_path)) {
132+
$lock_content = json_decode(file_get_contents($lock_file_path), true);
133+
if (isset($lock_content[$version_specific_name])) {
134+
// Use version-specific php-src
135+
f_putenv("SPC_PHP_SRC_NAME={$version_specific_name}");
136+
logger()->info("Building with PHP {$php_version} (using {$version_specific_name})");
137+
} elseif (isset($lock_content['php-src'])) {
138+
// Fall back to regular php-src
139+
f_putenv('SPC_PHP_SRC_NAME=php-src');
140+
logger()->warning("php-src-{$php_version} not found, using default php-src");
141+
} else {
142+
logger()->error('No php-src found in downloads. Please run download command first.');
143+
return static::FAILURE;
144+
}
145+
} else {
146+
logger()->error('Lock file not found. Please download sources first.');
147+
return static::FAILURE;
148+
}
149+
} else {
150+
// No version specified, use default php-src
151+
f_putenv('SPC_PHP_SRC_NAME=php-src');
152+
}
153+
123154
// create builder
124155
$builder = BuilderProvider::makeBuilderByInput($this->input);
125156
$include_suggest_ext = $this->getOption('with-suggested-exts');

src/SPC/command/DownloadCommand.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function configure(): void
3030
$this->addArgument('sources', InputArgument::REQUIRED, 'The sources will be compiled, comma separated');
3131
$this->addOption('shallow-clone', null, null, 'Clone shallow');
3232
$this->addOption('with-openssl11', null, null, 'Use openssl 1.1');
33-
$this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'version in major.minor format (default 8.4)', '8.4');
33+
$this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'version in major.minor format, comma-separated for multiple versions (default 8.4)', '8.4');
3434
$this->addOption('clean', null, null, 'Clean old download cache and source before fetch');
3535
$this->addOption('all', 'A', null, 'Fetch all sources that static-php-cli needed');
3636
$this->addOption('custom-url', 'U', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Specify custom source download url, e.g "php-src:https://downloads.php.net/~eric/php-8.3.0beta1.tar.gz"');
@@ -94,17 +94,25 @@ public function handle(): int
9494
return $this->downloadFromZip($path);
9595
}
9696

97-
// Define PHP major version
98-
$ver = $this->php_major_ver = $this->getOption('with-php');
99-
define('SPC_BUILD_PHP_VERSION', $ver);
100-
if ($ver !== 'git' && !preg_match('/^\d+\.\d+$/', $ver)) {
101-
// If not git, we need to check the version format
102-
if (!preg_match('/^\d+\.\d+(\.\d+)?$/', $ver)) {
103-
logger()->error("bad version arg: {$ver}, x.y or x.y.z required!");
104-
return static::FAILURE;
97+
// Define PHP major version(s)
98+
$php_versions_str = $this->getOption('with-php');
99+
$php_versions = array_map('trim', explode(',', $php_versions_str));
100+
101+
// Validate all versions
102+
foreach ($php_versions as $ver) {
103+
if ($ver !== 'git' && !preg_match('/^\d+\.\d+$/', $ver)) {
104+
// If not git, we need to check the version format
105+
if (!preg_match('/^\d+\.\d+(\.\d+)?$/', $ver)) {
106+
logger()->error("bad version arg: {$ver}, x.y or x.y.z required!");
107+
return static::FAILURE;
108+
}
105109
}
106110
}
107111

112+
// Set the first version as the default for backward compatibility
113+
$this->php_major_ver = $php_versions[0];
114+
define('SPC_BUILD_PHP_VERSION', $this->php_major_ver);
115+
108116
// retry
109117
$retry = (int) $this->getOption('retry');
110118
f_putenv('SPC_DOWNLOAD_RETRIES=' . $retry);
@@ -125,6 +133,20 @@ public function handle(): int
125133

126134
$chosen_sources = array_map('trim', array_filter(explode(',', $this->getArgument('sources'))));
127135

136+
// Handle multiple PHP versions
137+
// If php-src is in the sources, replace it with version-specific sources
138+
if (in_array('php-src', $chosen_sources)) {
139+
// Remove php-src from the list
140+
$chosen_sources = array_diff($chosen_sources, ['php-src']);
141+
// Add version-specific php-src for each version
142+
foreach ($php_versions as $ver) {
143+
$version_specific_name = "php-src-{$ver}";
144+
$chosen_sources[] = $version_specific_name;
145+
// Store the version for this specific php-src
146+
f_putenv("SPC_PHP_VERSION_{$version_specific_name}={$ver}");
147+
}
148+
}
149+
128150
$sss = $this->getOption('ignore-cache-sources');
129151
if ($sss === false) {
130152
// false is no-any-ignores, that is, default.
@@ -201,7 +223,16 @@ public function handle(): int
201223
logger()->info("[{$ni}/{$cnt}] Downloading source {$source} from custom git: {$new_config['url']}");
202224
Downloader::downloadSource($source, $new_config, true);
203225
} else {
204-
$config = Config::getSource($source);
226+
// Handle version-specific php-src (php-src-8.2, php-src-8.3, etc.)
227+
if (preg_match('/^php-src-[\d.]+$/', $source)) {
228+
$config = Config::getSource('php-src');
229+
if ($config === null) {
230+
logger()->error('php-src configuration not found in source.json');
231+
return static::FAILURE;
232+
}
233+
} else {
234+
$config = Config::getSource($source);
235+
}
205236
// Prefer pre-built, we need to search pre-built library
206237
if ($this->getOption('prefer-pre-built') && ($config['provide-pre-built'] ?? false) === true) {
207238
// We need to replace pattern

src/SPC/store/Downloader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,11 @@ private static function downloadByType(string $type, string $name, array $conf,
682682
...FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/pkg', 'SPC\store\pkg'),
683683
];
684684
foreach ($classes as $class) {
685-
if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) {
685+
// Support php-src and php-src-X.Y patterns
686+
$matches = ($class::NAME === $name) ||
687+
($class::NAME === 'php-src' && preg_match('/^php-src(-[\d.]+)?$/', $name));
688+
if (is_a($class, CustomSourceBase::class, true) && $matches) {
689+
$conf['source_name'] = $name; // Pass the actual source name
686690
(new $class())->fetch($force, $conf, $download_as);
687691
break;
688692
}

src/SPC/store/SourceManager.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ public static function initSource(?array $sources = null, ?array $libs = null, ?
3939

4040
// start check
4141
foreach ($sources_extracted as $source => $item) {
42-
if (Config::getSource($source) === null) {
42+
$extract_dir_name = $source;
43+
// Handle version-specific php-src (php-src-8.2, php-src-8.3, etc.)
44+
$source_config = Config::getSource($source);
45+
if ($source_config === null && preg_match('/^php-src-[\d.]+$/', $source)) {
46+
$source_config = Config::getSource('php-src');
47+
$extract_dir_name = 'php-src';
48+
}
49+
if ($source_config === null) {
4350
throw new WrongUsageException("Source [{$source}] does not exist, please check the name and correct it !");
4451
}
4552
// check source downloaded
@@ -56,12 +63,12 @@ public static function initSource(?array $sources = null, ?array $libs = null, ?
5663
$lock_content = LockFile::get($lock_name);
5764

5865
// check source dir exist
59-
$check = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $source);
66+
$check = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $extract_dir_name);
6067
// $check = $lock[$lock_name]['move_path'] === null ? (SOURCE_PATH . '/' . $source) : (SOURCE_PATH . '/' . $lock[$lock_name]['move_path']);
6168
if (!is_dir($check)) {
6269
logger()->debug("Extracting source [{$source}] to {$check} ...");
6370
$filename = LockFile::getLockFullPath($lock_content);
64-
FileSystem::extractSource($source, $lock_content['source_type'], $filename, $check);
71+
FileSystem::extractSource($extract_dir_name, $lock_content['source_type'], $filename, $check);
6572
LockFile::putLockSourceHash($lock_content, $check);
6673
continue;
6774
}
@@ -89,7 +96,7 @@ public static function initSource(?array $sources = null, ?array $libs = null, ?
8996
logger()->notice("Source [{$source}] hash mismatch, removing old source dir and extracting again ...");
9097
FileSystem::removeDir($check);
9198
$filename = LockFile::getLockFullPath($lock_content);
92-
$move_path = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $source);
99+
$move_path = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $extract_dir_name);
93100
FileSystem::extractSource($source, $lock_content['source_type'], $filename, $move_path);
94101
LockFile::putLockSourceHash($lock_content, $check);
95102
}

src/SPC/store/source/PhpSource.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ class PhpSource extends CustomSourceBase
1414

1515
public function fetch(bool $force = false, ?array $config = null, int $lock_as = SPC_DOWNLOAD_SOURCE): void
1616
{
17-
$major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.4';
17+
$source_name = $config['source_name'] ?? 'php-src';
18+
19+
// Try to extract version from source name (e.g., "php-src-8.2" -> "8.2")
20+
if (preg_match('/^php-src-([\d.]+)$/', $source_name, $matches)) {
21+
$major = $matches[1];
22+
} else {
23+
$major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.4';
24+
}
25+
1826
if ($major === 'git') {
19-
Downloader::downloadSource('php-src', ['type' => 'git', 'url' => 'https://github.com/php/php-src.git', 'rev' => 'master'], $force);
27+
Downloader::downloadSource($source_name, ['type' => 'git', 'url' => 'https://github.com/php/php-src.git', 'rev' => 'master'], $force);
2028
} else {
21-
Downloader::downloadSource('php-src', $this->getLatestPHPInfo($major), $force);
29+
Downloader::downloadSource($source_name, $this->getLatestPHPInfo($major), $force);
2230
}
2331
}
2432

0 commit comments

Comments
 (0)