mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-18 04:44:53 +08:00
refactor musl tool doctor
This commit is contained in:
parent
5cfba6e83b
commit
8191444fe9
@ -18,23 +18,68 @@ use SPC\store\FileSystem;
|
|||||||
class LinuxMuslCheck
|
class LinuxMuslCheck
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUnused */
|
/** @noinspection PhpUnused */
|
||||||
/**
|
#[AsCheckItem('if musl-wrapper is installed', limit_os: 'Linux', level: 800)]
|
||||||
* @throws WrongUsageException
|
|
||||||
*/
|
|
||||||
#[AsCheckItem('if musl-libc is installed', limit_os: 'Linux')]
|
|
||||||
public function checkMusl(): CheckResult
|
public function checkMusl(): CheckResult
|
||||||
{
|
{
|
||||||
if (SystemUtil::isMuslDist()) {
|
if (SystemUtil::isMuslDist()) {
|
||||||
|
return CheckResult::ok('musl-based distro, skipped');
|
||||||
|
}
|
||||||
|
|
||||||
|
$musl_wrapper_lib = sprintf('/lib/ld-musl-%s.so.1', php_uname('m'));
|
||||||
|
if (file_exists($musl_wrapper_lib)) {
|
||||||
return CheckResult::ok();
|
return CheckResult::ok();
|
||||||
}
|
}
|
||||||
|
return CheckResult::fail('musl-wrapper is not installed on your system', 'fix-musl-wrapper');
|
||||||
|
}
|
||||||
|
|
||||||
|
#[AsCheckItem('if musl-cross-make is installed', limit_os: 'Linux', level: 799)]
|
||||||
|
public function checkMuslCrossMake(): CheckResult
|
||||||
|
{
|
||||||
|
if (SystemUtil::isMuslDist()) {
|
||||||
|
return CheckResult::ok('musl-based distro, skipped');
|
||||||
|
}
|
||||||
$arch = arch2gnu(php_uname('m'));
|
$arch = arch2gnu(php_uname('m'));
|
||||||
$cross_compile_lib = "/usr/local/musl/{$arch}-linux-musl/lib/libc.a";
|
$cross_compile_lib = "/usr/local/musl/{$arch}-linux-musl/lib/libc.a";
|
||||||
$cross_compile_gcc = "/usr/local/musl/bin/{$arch}-linux-musl-gcc";
|
$cross_compile_gcc = "/usr/local/musl/bin/{$arch}-linux-musl-gcc";
|
||||||
$musl_wrapper_lib = sprintf('/lib/ld-musl-%s.so.1', php_uname('m'));
|
if (file_exists($cross_compile_lib) && file_exists($cross_compile_gcc)) {
|
||||||
if (file_exists($musl_wrapper_lib) && file_exists($cross_compile_lib) && file_exists($cross_compile_gcc)) {
|
|
||||||
return CheckResult::ok();
|
return CheckResult::ok();
|
||||||
}
|
}
|
||||||
return CheckResult::fail('musl-libc is not installed on your system', 'fix-musl');
|
return CheckResult::fail('musl-cross-make is not installed on your system', 'fix-musl-cross-make');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @noinspection PhpUnused */
|
||||||
|
/**
|
||||||
|
* @throws DownloaderException
|
||||||
|
* @throws FileSystemException
|
||||||
|
*/
|
||||||
|
#[AsFixItem('fix-musl-wrapper')]
|
||||||
|
public function fixMusl(): bool
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$prefix = '';
|
||||||
|
if (get_current_user() !== 'root') {
|
||||||
|
$prefix = 'sudo ';
|
||||||
|
logger()->warning('Current user is not root, using sudo for running command');
|
||||||
|
}
|
||||||
|
// The hardcoded version here is to be consistent with the version compiled by `musl-cross-toolchain`.
|
||||||
|
$musl_version_name = 'musl-1.2.4';
|
||||||
|
$musl_source = [
|
||||||
|
'type' => 'url',
|
||||||
|
'url' => "https://musl.libc.org/releases/{$musl_version_name}.tar.gz",
|
||||||
|
];
|
||||||
|
logger()->info('Downloading ' . $musl_source['url']);
|
||||||
|
Downloader::downloadSource($musl_version_name, $musl_source);
|
||||||
|
FileSystem::extractSource($musl_version_name, DOWNLOAD_PATH . "/{$musl_version_name}.tar.gz");
|
||||||
|
logger()->info('Installing musl wrapper');
|
||||||
|
shell(true)->cd(SOURCE_PATH . "/{$musl_version_name}")
|
||||||
|
->exec('./configure')
|
||||||
|
->exec('make -j')
|
||||||
|
->exec("{$prefix}make install");
|
||||||
|
// TODO: add path using putenv instead of editing /etc/profile
|
||||||
|
return true;
|
||||||
|
} catch (RuntimeException) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @noinspection PhpUnused */
|
/** @noinspection PhpUnused */
|
||||||
@ -43,74 +88,28 @@ class LinuxMuslCheck
|
|||||||
* @throws FileSystemException
|
* @throws FileSystemException
|
||||||
* @throws WrongUsageException
|
* @throws WrongUsageException
|
||||||
*/
|
*/
|
||||||
#[AsFixItem('fix-musl')]
|
#[AsFixItem('fix-musl-cross-make')]
|
||||||
public function fixMusl(): bool
|
public function fixMuslCrossMake(): bool
|
||||||
{
|
{
|
||||||
$arch = arch2gnu(php_uname('m'));
|
|
||||||
$musl_wrapper_lib = sprintf('/lib/ld-musl-%s.so.1', php_uname('m'));
|
|
||||||
$cross_compile_lib = "/usr/local/musl/{$arch}-linux-musl/lib/libc.a";
|
|
||||||
$cross_compile_gcc = "/usr/local/musl/bin/{$arch}-linux-musl-gcc";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!file_exists(DOWNLOAD_PATH)) {
|
$prefix = '';
|
||||||
FileSystem::createDir(DOWNLOAD_PATH);
|
if (get_current_user() !== 'root') {
|
||||||
|
$prefix = 'sudo ';
|
||||||
|
logger()->warning('Current user is not root, using sudo for running command');
|
||||||
}
|
}
|
||||||
if (!file_exists($musl_wrapper_lib)) {
|
$arch = arch2gnu(php_uname('m'));
|
||||||
$this->installMuslWrapper();
|
$musl_compile_source = [
|
||||||
}
|
'type' => 'url',
|
||||||
if (!file_exists($cross_compile_lib) || !file_exists($cross_compile_gcc)) {
|
'url' => "https://dl.static-php.dev/static-php-cli/deps/musl-toolchain/{$arch}-musl-toolchain.tgz",
|
||||||
$this->installMuslCrossMake();
|
];
|
||||||
}
|
logger()->info('Downloading ' . $musl_compile_source['url']);
|
||||||
// TODO: add path using putenv instead of editing /etc/profile
|
Downloader::downloadSource('musl-compile', $musl_compile_source);
|
||||||
|
logger()->info('Extracting musl-cross');
|
||||||
|
FileSystem::extractSource('musl-compile', DOWNLOAD_PATH . "/{$arch}-musl-toolchain.tgz");
|
||||||
|
shell(true)->exec($prefix . 'cp -rf ' . SOURCE_PATH . '/musl-compile/* /usr/local/musl');
|
||||||
return true;
|
return true;
|
||||||
} catch (RuntimeException) {
|
} catch (RuntimeException) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws DownloaderException
|
|
||||||
* @throws RuntimeException
|
|
||||||
* @throws FileSystemException
|
|
||||||
*/
|
|
||||||
public function installMuslWrapper(): void
|
|
||||||
{
|
|
||||||
$prefix = '';
|
|
||||||
if (get_current_user() !== 'root') {
|
|
||||||
$prefix = 'sudo ';
|
|
||||||
logger()->warning('Current user is not root, using sudo for running command');
|
|
||||||
}
|
|
||||||
// The hardcoded version here is to be consistent with the version compiled by `musl-cross-toolchain`.
|
|
||||||
$musl_version_name = 'musl-1.2.4';
|
|
||||||
$musl_source = [
|
|
||||||
'type' => 'url',
|
|
||||||
'url' => "https://musl.libc.org/releases/{$musl_version_name}.tar.gz",
|
|
||||||
];
|
|
||||||
Downloader::downloadSource($musl_version_name, $musl_source);
|
|
||||||
FileSystem::extractSource($musl_version_name, DOWNLOAD_PATH . "/{$musl_version_name}.tar.gz");
|
|
||||||
shell(true)->cd(SOURCE_PATH . "/{$musl_version_name}")
|
|
||||||
->exec('./configure')
|
|
||||||
->exec('make -j')
|
|
||||||
->exec("{$prefix}make install");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws DownloaderException
|
|
||||||
* @throws FileSystemException
|
|
||||||
* @throws RuntimeException
|
|
||||||
* @throws WrongUsageException
|
|
||||||
*/
|
|
||||||
public function installMuslCrossMake(): void
|
|
||||||
{
|
|
||||||
$arch = arch2gnu(php_uname('m'));
|
|
||||||
$musl_compile_source = [
|
|
||||||
'type' => 'url',
|
|
||||||
'url' => "https://dl.static-php.dev/static-php-cli/deps/musl-toolchain/{$arch}-musl-toolchain.tgz",
|
|
||||||
];
|
|
||||||
logger()->info('Downloading ' . $musl_compile_source['url']);
|
|
||||||
Downloader::downloadSource('musl-compile', $musl_compile_source);
|
|
||||||
logger()->info('Extracting musl-cross');
|
|
||||||
FileSystem::extractSource('musl-compile', DOWNLOAD_PATH . "/{$arch}-musl-toolchain.tgz");
|
|
||||||
shell(true)->exec('cp -rf ' . SOURCE_PATH . '/musl-compile/* /usr/local/musl');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -261,10 +261,13 @@ class Downloader
|
|||||||
|
|
||||||
if ($source === null) {
|
if ($source === null) {
|
||||||
logger()->warning('Source {name} unknown. Skipping.', ['name' => $name]);
|
logger()->warning('Source {name} unknown. Skipping.', ['name' => $name]);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!is_dir(DOWNLOAD_PATH)) {
|
||||||
|
FileSystem::createDir(DOWNLOAD_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
// load lock file
|
// load lock file
|
||||||
if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) {
|
if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) {
|
||||||
$lock = [];
|
$lock = [];
|
||||||
|
|||||||
@ -152,6 +152,9 @@ class FileSystem
|
|||||||
if (self::$_extract_hook === []) {
|
if (self::$_extract_hook === []) {
|
||||||
SourcePatcher::init();
|
SourcePatcher::init();
|
||||||
}
|
}
|
||||||
|
if (!is_dir(SOURCE_PATH)) {
|
||||||
|
self::createDir(SOURCE_PATH);
|
||||||
|
}
|
||||||
if ($move_path !== null) {
|
if ($move_path !== null) {
|
||||||
$move_path = SOURCE_PATH . '/' . $move_path;
|
$move_path = SOURCE_PATH . '/' . $move_path;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user