Skip to content

Commit 631a1b5

Browse files
committed
Add libc version for pre-built content name
1 parent 67d2ad5 commit 631a1b5

8 files changed

Lines changed: 43 additions & 7 deletions

File tree

config/pre-built.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"repo": "static-php/static-php-cli-hosted",
33
"prefer-stable": true,
4-
"match-pattern-linux": "{name}-{arch}-{os}-{libc}.txz",
4+
"match-pattern-linux": "{name}-{arch}-{os}-{libc}-{libcver}.txz",
55
"match-pattern": "{name}-{arch}-{os}.txz",
66
"suffix": "txz"
77
}

src/SPC/builder/LibraryBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function setup(bool $force = false): int
4646
$lock = json_decode(FileSystem::readFile(DOWNLOAD_PATH . '/.lock.json'), true) ?? [];
4747
$source = Config::getLib(static::NAME, 'source');
4848
// if source is locked as pre-built, we just tryInstall it
49-
$pre_built_name = Downloader::getPreBuiltName($source);
49+
$pre_built_name = Downloader::getPreBuiltLockName($source);
5050
if (isset($lock[$pre_built_name]) && ($lock[$pre_built_name]['lock_as'] ?? SPC_DOWN_SOURCE) === SPC_DOWN_PRE_BUILT) {
5151
return $this->tryInstall($lock[$pre_built_name]['filename'], $force);
5252
}

src/SPC/builder/linux/SystemUtil.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,35 @@ public static function getSupportedDistros(): array
182182
'arch', 'manjaro',
183183
];
184184
}
185+
186+
/**
187+
* Get libc version string from ldd
188+
*/
189+
public static function getLibcVersionIfExists(): ?string
190+
{
191+
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'glibc') {
192+
$result = shell()->execWithResult('ldd --version');
193+
if ($result[0] !== 0) {
194+
return null;
195+
}
196+
// get first line
197+
$first_line = $result[1][0];
198+
// match ldd version: "ldd (some useless text) 2.17" match 2.17
199+
$pattern = '/ldd\s+\(.*?\)\s+(\d+\.\d+)/';
200+
if (preg_match($pattern, $first_line, $matches)) {
201+
return $matches[1];
202+
}
203+
return null;
204+
}
205+
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'musl') {
206+
$result = shell()->execWithResult('ldd 2>&1');
207+
// Match Version * line
208+
// match ldd version: "Version 1.2.3" match 1.2.3
209+
$pattern = '/Version\s+(\d+\.\d+\.\d+)/';
210+
if (preg_match($pattern, $result[1][1], $matches)) {
211+
return $matches[1];
212+
}
213+
}
214+
return null;
215+
}
185216
}

src/SPC/command/DeleteDownloadCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function handle(): int
5151
$deleted_sources = [];
5252
foreach ($chosen_sources as $source) {
5353
$source = trim($source);
54-
foreach ([$source, Downloader::getPreBuiltName($source)] as $name) {
54+
foreach ([$source, Downloader::getPreBuiltLockName($source)] as $name) {
5555
if (isset($lock[$name])) {
5656
$deleted_sources[] = $name;
5757
}

src/SPC/command/DownloadCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SPC\command;
66

7+
use SPC\builder\linux\SystemUtil;
78
use SPC\builder\traits\UnixSystemUtilTrait;
89
use SPC\exception\DownloaderException;
910
use SPC\exception\FileSystemException;
@@ -236,6 +237,7 @@ public function handle(): int
236237
'{arch}' => arch2gnu(php_uname('m')),
237238
'{os}' => strtolower(PHP_OS_FAMILY),
238239
'{libc}' => getenv('SPC_LIBC') ?: 'default',
240+
'{libcver}' => PHP_OS_FAMILY === 'Linux' ? (SystemUtil::getLibcVersionIfExists() ?? 'default') : 'default',
239241
];
240242
$find = str_replace(array_keys($replace), array_values($replace), Config::getPreBuilt('match-pattern'));
241243
// find filename in asset list

src/SPC/command/dev/PackLibCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SPC\builder\BuilderProvider;
88
use SPC\builder\LibraryBase;
9+
use SPC\builder\linux\SystemUtil;
910
use SPC\command\BuildCommand;
1011
use SPC\exception\ExceptionHandler;
1112
use SPC\exception\FileSystemException;
@@ -23,6 +24,7 @@ class PackLibCommand extends BuildCommand
2324
public function configure(): void
2425
{
2526
$this->addArgument('library', InputArgument::REQUIRED, 'The library will be compiled');
27+
$this->addOption('show-libc-ver', null, null);
2628
}
2729

2830
public function handle(): int
@@ -75,6 +77,7 @@ public function handle(): int
7577
'{arch}' => arch2gnu(php_uname('m')),
7678
'{os}' => strtolower(PHP_OS_FAMILY),
7779
'{libc}' => getenv('SPC_LIBC') ?: 'default',
80+
'{libcver}' => PHP_OS_FAMILY === 'Linux' ? (SystemUtil::getLibcVersionIfExists() ?? 'default') : 'default',
7881
];
7982
$filename = str_replace(array_keys($replace), array_values($replace), $filename);
8083
$filename = WORKING_DIR . '/dist/' . $filename . '.' . Config::getPreBuilt('suffix');

src/SPC/store/Downloader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static function downloadFile(string $name, string $url, string $filename,
204204
self::unregisterCancelEvent();
205205
logger()->debug("Locking {$filename}");
206206
if ($download_as === SPC_DOWN_PRE_BUILT) {
207-
$name = self::getPreBuiltName($name);
207+
$name = self::getPreBuiltLockName($name);
208208
}
209209
self::lockSource($name, ['source_type' => 'archive', 'filename' => $filename, 'move_path' => $move_path, 'lock_as' => $download_as]);
210210
}
@@ -594,7 +594,7 @@ public static function curlDown(string $url, string $path, string $method = 'GET
594594
}
595595
}
596596

597-
public static function getPreBuiltName(string $source): string
597+
public static function getPreBuiltLockName(string $source): string
598598
{
599599
return "{$source}-" . PHP_OS_FAMILY . '-' . getenv('GNU_ARCH') . '-' . (getenv('SPC_LIBC') ?: 'default');
600600
}
@@ -653,7 +653,7 @@ private static function isAlreadyDownloaded(string $name, bool $force, int $down
653653
}
654654
}
655655
// If lock file exists for current arch and glibc target, skip downloading
656-
$lock_name = self::getPreBuiltName($name);
656+
$lock_name = self::getPreBuiltLockName($name);
657657
if (!$force && $download_as === SPC_DOWN_PRE_BUILT && isset($lock[$lock_name])) {
658658
// lock name with env
659659
if (

src/SPC/store/SourceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function initSource(?array $sources = null, ?array $libs = null, ?
5555
throw new WrongUsageException("Source [{$source}] does not exist, please check the name and correct it !");
5656
}
5757
// check source downloaded
58-
$pre_built_name = Downloader::getPreBuiltName($source);
58+
$pre_built_name = Downloader::getPreBuiltLockName($source);
5959
if (!isset($lock[$pre_built_name])) {
6060
if (!isset($lock[$source])) {
6161
throw new WrongUsageException("Source [{$source}] not downloaded or not locked, you should download it first !");

0 commit comments

Comments
 (0)