Add libc version for pre-built content name

This commit is contained in:
crazywhalecc 2025-03-30 20:16:41 +08:00
parent 67d2ad5511
commit 631a1b5864
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
8 changed files with 43 additions and 7 deletions

View File

@ -1,7 +1,7 @@
{
"repo": "static-php/static-php-cli-hosted",
"prefer-stable": true,
"match-pattern-linux": "{name}-{arch}-{os}-{libc}.txz",
"match-pattern-linux": "{name}-{arch}-{os}-{libc}-{libcver}.txz",
"match-pattern": "{name}-{arch}-{os}.txz",
"suffix": "txz"
}

View File

@ -46,7 +46,7 @@ abstract class LibraryBase
$lock = json_decode(FileSystem::readFile(DOWNLOAD_PATH . '/.lock.json'), true) ?? [];
$source = Config::getLib(static::NAME, 'source');
// if source is locked as pre-built, we just tryInstall it
$pre_built_name = Downloader::getPreBuiltName($source);
$pre_built_name = Downloader::getPreBuiltLockName($source);
if (isset($lock[$pre_built_name]) && ($lock[$pre_built_name]['lock_as'] ?? SPC_DOWN_SOURCE) === SPC_DOWN_PRE_BUILT) {
return $this->tryInstall($lock[$pre_built_name]['filename'], $force);
}

View File

@ -182,4 +182,35 @@ class SystemUtil
'arch', 'manjaro',
];
}
/**
* Get libc version string from ldd
*/
public static function getLibcVersionIfExists(): ?string
{
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'glibc') {
$result = shell()->execWithResult('ldd --version');
if ($result[0] !== 0) {
return null;
}
// get first line
$first_line = $result[1][0];
// match ldd version: "ldd (some useless text) 2.17" match 2.17
$pattern = '/ldd\s+\(.*?\)\s+(\d+\.\d+)/';
if (preg_match($pattern, $first_line, $matches)) {
return $matches[1];
}
return null;
}
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'musl') {
$result = shell()->execWithResult('ldd 2>&1');
// Match Version * line
// match ldd version: "Version 1.2.3" match 1.2.3
$pattern = '/Version\s+(\d+\.\d+\.\d+)/';
if (preg_match($pattern, $result[1][1], $matches)) {
return $matches[1];
}
}
return null;
}
}

View File

@ -51,7 +51,7 @@ class DeleteDownloadCommand extends BaseCommand
$deleted_sources = [];
foreach ($chosen_sources as $source) {
$source = trim($source);
foreach ([$source, Downloader::getPreBuiltName($source)] as $name) {
foreach ([$source, Downloader::getPreBuiltLockName($source)] as $name) {
if (isset($lock[$name])) {
$deleted_sources[] = $name;
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace SPC\command;
use SPC\builder\linux\SystemUtil;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\exception\DownloaderException;
use SPC\exception\FileSystemException;
@ -236,6 +237,7 @@ class DownloadCommand extends BaseCommand
'{arch}' => arch2gnu(php_uname('m')),
'{os}' => strtolower(PHP_OS_FAMILY),
'{libc}' => getenv('SPC_LIBC') ?: 'default',
'{libcver}' => PHP_OS_FAMILY === 'Linux' ? (SystemUtil::getLibcVersionIfExists() ?? 'default') : 'default',
];
$find = str_replace(array_keys($replace), array_values($replace), Config::getPreBuilt('match-pattern'));
// find filename in asset list

View File

@ -6,6 +6,7 @@ namespace SPC\command\dev;
use SPC\builder\BuilderProvider;
use SPC\builder\LibraryBase;
use SPC\builder\linux\SystemUtil;
use SPC\command\BuildCommand;
use SPC\exception\ExceptionHandler;
use SPC\exception\FileSystemException;
@ -23,6 +24,7 @@ class PackLibCommand extends BuildCommand
public function configure(): void
{
$this->addArgument('library', InputArgument::REQUIRED, 'The library will be compiled');
$this->addOption('show-libc-ver', null, null);
}
public function handle(): int
@ -75,6 +77,7 @@ class PackLibCommand extends BuildCommand
'{arch}' => arch2gnu(php_uname('m')),
'{os}' => strtolower(PHP_OS_FAMILY),
'{libc}' => getenv('SPC_LIBC') ?: 'default',
'{libcver}' => PHP_OS_FAMILY === 'Linux' ? (SystemUtil::getLibcVersionIfExists() ?? 'default') : 'default',
];
$filename = str_replace(array_keys($replace), array_values($replace), $filename);
$filename = WORKING_DIR . '/dist/' . $filename . '.' . Config::getPreBuilt('suffix');

View File

@ -204,7 +204,7 @@ class Downloader
self::unregisterCancelEvent();
logger()->debug("Locking {$filename}");
if ($download_as === SPC_DOWN_PRE_BUILT) {
$name = self::getPreBuiltName($name);
$name = self::getPreBuiltLockName($name);
}
self::lockSource($name, ['source_type' => 'archive', 'filename' => $filename, 'move_path' => $move_path, 'lock_as' => $download_as]);
}
@ -594,7 +594,7 @@ class Downloader
}
}
public static function getPreBuiltName(string $source): string
public static function getPreBuiltLockName(string $source): string
{
return "{$source}-" . PHP_OS_FAMILY . '-' . getenv('GNU_ARCH') . '-' . (getenv('SPC_LIBC') ?: 'default');
}
@ -653,7 +653,7 @@ class Downloader
}
}
// If lock file exists for current arch and glibc target, skip downloading
$lock_name = self::getPreBuiltName($name);
$lock_name = self::getPreBuiltLockName($name);
if (!$force && $download_as === SPC_DOWN_PRE_BUILT && isset($lock[$lock_name])) {
// lock name with env
if (

View File

@ -55,7 +55,7 @@ class SourceManager
throw new WrongUsageException("Source [{$source}] does not exist, please check the name and correct it !");
}
// check source downloaded
$pre_built_name = Downloader::getPreBuiltName($source);
$pre_built_name = Downloader::getPreBuiltLockName($source);
if (!isset($lock[$pre_built_name])) {
if (!isset($lock[$source])) {
throw new WrongUsageException("Source [{$source}] not downloaded or not locked, you should download it first !");