2023-04-30 12:42:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\doctor\item;
|
|
|
|
|
|
|
|
|
|
use SPC\builder\linux\SystemUtil;
|
|
|
|
|
use SPC\doctor\AsCheckItem;
|
|
|
|
|
use SPC\doctor\AsFixItem;
|
|
|
|
|
use SPC\doctor\CheckResult;
|
2023-10-16 14:09:49 +02:00
|
|
|
use SPC\exception\DownloaderException;
|
|
|
|
|
use SPC\exception\FileSystemException;
|
2023-04-30 12:42:19 +08:00
|
|
|
use SPC\exception\RuntimeException;
|
2023-10-16 14:09:49 +02:00
|
|
|
use SPC\exception\WrongUsageException;
|
|
|
|
|
use SPC\store\Downloader;
|
|
|
|
|
use SPC\store\FileSystem;
|
2023-04-30 12:42:19 +08:00
|
|
|
|
|
|
|
|
class LinuxMuslCheck
|
|
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @noinspection PhpUnused */
|
2023-10-22 21:33:09 +08:00
|
|
|
#[AsCheckItem('if musl-wrapper is installed', limit_os: 'Linux', level: 800)]
|
2023-10-16 14:09:49 +02:00
|
|
|
public function checkMusl(): CheckResult
|
2023-04-30 12:42:19 +08:00
|
|
|
{
|
2023-10-16 14:09:49 +02:00
|
|
|
if (SystemUtil::isMuslDist()) {
|
2023-10-22 21:33:09 +08:00
|
|
|
return CheckResult::ok('musl-based distro, skipped');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$musl_wrapper_lib = sprintf('/lib/ld-musl-%s.so.1', php_uname('m'));
|
2023-10-29 18:02:02 +01:00
|
|
|
if (file_exists($musl_wrapper_lib) && file_exists('/usr/local/musl/lib/libc.a')) {
|
2023-10-16 14:09:49 +02:00
|
|
|
return CheckResult::ok();
|
|
|
|
|
}
|
2023-10-22 21:33:09 +08:00
|
|
|
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');
|
|
|
|
|
}
|
2023-10-16 14:09:49 +02:00
|
|
|
$arch = arch2gnu(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";
|
2023-10-22 21:33:09 +08:00
|
|
|
if (file_exists($cross_compile_lib) && file_exists($cross_compile_gcc)) {
|
2023-04-30 12:42:19 +08:00
|
|
|
return CheckResult::ok();
|
|
|
|
|
}
|
2023-10-22 21:33:09 +08:00
|
|
|
return CheckResult::fail('musl-cross-make is not installed on your system', 'fix-musl-cross-make');
|
2023-10-16 14:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @noinspection PhpUnused */
|
|
|
|
|
/**
|
|
|
|
|
* @throws DownloaderException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
2023-10-22 21:33:09 +08:00
|
|
|
#[AsFixItem('fix-musl-wrapper')]
|
2023-10-16 14:09:49 +02:00
|
|
|
public function fixMusl(): bool
|
|
|
|
|
{
|
|
|
|
|
try {
|
2023-10-22 21:33:09 +08:00
|
|
|
$prefix = '';
|
|
|
|
|
if (get_current_user() !== 'root') {
|
|
|
|
|
$prefix = 'sudo ';
|
|
|
|
|
logger()->warning('Current user is not root, using sudo for running command');
|
2023-10-16 14:09:49 +02:00
|
|
|
}
|
2023-10-22 21:33:09 +08:00
|
|
|
// 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');
|
2023-10-30 00:35:58 +08:00
|
|
|
shell()->cd(SOURCE_PATH . "/{$musl_version_name}")
|
2023-10-29 18:02:02 +01:00
|
|
|
->exec('./configure --disable-gcc-wrapper')
|
2023-10-22 21:33:09 +08:00
|
|
|
->exec('make -j')
|
|
|
|
|
->exec("{$prefix}make install");
|
2023-10-22 16:55:02 +08:00
|
|
|
// TODO: add path using putenv instead of editing /etc/profile
|
2023-10-16 14:09:49 +02:00
|
|
|
return true;
|
|
|
|
|
} catch (RuntimeException) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-30 12:42:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-22 21:33:09 +08:00
|
|
|
/** @noinspection PhpUnused */
|
2023-10-16 14:09:49 +02:00
|
|
|
/**
|
|
|
|
|
* @throws DownloaderException
|
|
|
|
|
* @throws FileSystemException
|
2023-10-22 16:55:02 +08:00
|
|
|
* @throws WrongUsageException
|
2023-10-16 14:09:49 +02:00
|
|
|
*/
|
2023-10-22 21:33:09 +08:00
|
|
|
#[AsFixItem('fix-musl-cross-make')]
|
|
|
|
|
public function fixMuslCrossMake(): bool
|
2023-10-16 14:09:49 +02:00
|
|
|
{
|
2023-10-22 21:33:09 +08:00
|
|
|
try {
|
|
|
|
|
$prefix = '';
|
|
|
|
|
if (get_current_user() !== 'root') {
|
|
|
|
|
$prefix = 'sudo ';
|
|
|
|
|
logger()->warning('Current user is not root, using sudo for running command');
|
|
|
|
|
}
|
|
|
|
|
$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");
|
2023-10-30 00:35:58 +08:00
|
|
|
shell()->exec($prefix . 'cp -rf ' . SOURCE_PATH . '/musl-compile/* /usr/local/musl');
|
2023-10-22 21:33:09 +08:00
|
|
|
return true;
|
|
|
|
|
} catch (RuntimeException) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-30 12:42:19 +08:00
|
|
|
}
|
|
|
|
|
}
|