static-php-cli/src/SPC/doctor/item/LinuxToolCheckList.php

149 lines
5.0 KiB
PHP
Raw Normal View History

2023-07-17 20:26:48 +08:00
<?php
declare(strict_types=1);
namespace SPC\doctor\item;
use SPC\builder\linux\SystemUtil;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\doctor\AsCheckItem;
use SPC\doctor\AsFixItem;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
class LinuxToolCheckList
{
use UnixSystemUtilTrait;
2023-07-17 21:11:06 +08:00
public const TOOLS_ALPINE = [
2023-09-18 13:43:58 +02:00
'make', 'bison', 'flex',
'git', 'autoconf', 'automake', 'gettext-dev',
2023-07-17 21:11:06 +08:00
'tar', 'unzip', 'gzip',
'bzip2', 'cmake', 'gcc',
'g++', 'patch', 'binutils-gold',
2025-06-06 23:51:58 +07:00
'libtoolize', 'which',
'patchelf',
2023-07-17 21:11:06 +08:00
];
public const TOOLS_DEBIAN = [
2023-09-18 13:43:58 +02:00
'make', 'bison', 'flex',
'git', 'autoconf', 'automake', 'autopoint',
2023-07-17 21:11:06 +08:00
'tar', 'unzip', 'gzip',
'bzip2', 'cmake', 'patch',
2025-06-06 23:51:58 +07:00
'xz', 'libtoolize', 'which',
'patchelf', 'musl'
2023-07-17 21:11:06 +08:00
];
2023-09-18 13:43:58 +02:00
public const TOOLS_RHEL = [
'perl', 'make', 'bison', 'flex',
'git', 'autoconf', 'automake',
'tar', 'unzip', 'gzip', 'gcc',
2025-06-06 23:51:58 +07:00
'bzip2', 'cmake', 'patch', 'which',
'xz', 'libtool', 'gettext-devel',
'perl', 'patchelf', 'musl-libc'
2023-09-18 13:43:58 +02:00
];
2024-04-13 16:12:05 +08:00
public const TOOLS_ARCH = [
'base-devel', 'cmake',
];
2024-03-15 20:14:04 +01:00
private const PROVIDED_COMMAND = [
'binutils-gold' => 'ld.gold',
2024-04-13 16:12:05 +08:00
'base-devel' => 'automake',
'gettext-devel' => 'gettextize',
'gettext-dev' => 'gettextize',
2024-03-15 20:14:04 +01:00
];
/** @noinspection PhpUnused */
2023-09-30 23:18:53 +08:00
#[AsCheckItem('if necessary tools are installed', limit_os: 'Linux', level: 999)]
2023-07-17 20:26:48 +08:00
public function checkCliTools(): ?CheckResult
{
$distro = SystemUtil::getOSRelease();
$required = match ($distro['dist']) {
2023-07-17 21:11:06 +08:00
'alpine' => self::TOOLS_ALPINE,
'redhat' => self::TOOLS_RHEL,
'centos' => array_merge(self::TOOLS_RHEL, ['perl-IPC-Cmd']),
2024-04-13 16:12:05 +08:00
'arch' => self::TOOLS_ARCH,
2023-07-17 21:11:06 +08:00
default => self::TOOLS_DEBIAN,
2023-07-17 20:26:48 +08:00
};
$missing = [];
2024-03-15 20:14:04 +01:00
foreach ($required as $package) {
if (self::findCommand(self::PROVIDED_COMMAND[$package] ?? $package) === null) {
2024-03-15 20:14:04 +01:00
$missing[] = $package;
2023-07-17 20:26:48 +08:00
}
}
if (!empty($missing)) {
return match ($distro['dist']) {
2023-09-18 12:52:12 +02:00
'ubuntu',
'alpine',
2023-10-31 14:56:43 +01:00
'redhat',
'centos',
2024-01-08 23:34:17 +08:00
'Deepin',
2024-04-13 16:12:05 +08:00
'arch',
2023-09-18 12:52:12 +02:00
'debian' => CheckResult::fail(implode(', ', $missing) . ' not installed on your system', 'install-linux-tools', [$distro, $missing]),
2023-07-17 20:26:48 +08:00
default => CheckResult::fail(implode(', ', $missing) . ' not installed on your system'),
};
}
return CheckResult::ok();
}
#[AsCheckItem('if cmake version >= 3.18', limit_os: 'Linux')]
public function checkCMakeVersion(): ?CheckResult
{
$ver = get_cmake_version();
if ($ver === null) {
return CheckResult::fail('Failed to get cmake version');
}
if (version_compare($ver, '3.18.0') < 0) {
return CheckResult::fail('cmake version is too low (' . $ver . '), please update it manually!');
}
return CheckResult::ok($ver);
}
/** @noinspection PhpUnused */
2024-01-08 23:34:17 +08:00
#[AsCheckItem('if necessary linux headers are installed', limit_os: 'Linux')]
2023-07-17 20:58:48 +08:00
public function checkSystemOSPackages(): ?CheckResult
{
2023-10-31 14:56:43 +01:00
if (SystemUtil::isMuslDist()) {
2023-07-17 20:58:48 +08:00
// check linux-headers installation
if (!file_exists('/usr/include/linux/mman.h')) {
2023-10-31 14:56:43 +01:00
return CheckResult::fail('linux-headers not installed on your system', 'install-linux-tools', [SystemUtil::getOSRelease(), ['linux-headers']]);
2023-07-17 20:58:48 +08:00
}
}
return CheckResult::ok();
}
/**
* @throws RuntimeException
* @noinspection PhpUnused
*/
2023-07-17 20:26:48 +08:00
#[AsFixItem('install-linux-tools')]
public function fixBuildTools(array $distro, array $missing): bool
{
$install_cmd = match ($distro['dist']) {
2024-01-08 23:34:17 +08:00
'ubuntu', 'debian', 'Deepin' => 'apt-get install -y',
2023-07-17 20:26:48 +08:00
'alpine' => 'apk add',
2023-10-31 14:56:43 +01:00
'redhat' => 'dnf install -y',
'centos' => 'yum install -y',
2024-04-13 16:12:05 +08:00
'arch' => 'pacman -S --noconfirm',
2023-09-18 12:52:12 +02:00
default => throw new RuntimeException('Current linux distro does not have an auto-install script for musl packages yet.'),
2023-07-17 20:26:48 +08:00
};
$prefix = '';
if (($user = exec('whoami')) !== 'root') {
2023-07-17 20:26:48 +08:00
$prefix = 'sudo ';
logger()->warning('Current user (' . $user . ') is not root, using sudo for running command');
2023-07-17 20:26:48 +08:00
}
try {
2024-01-08 23:34:17 +08:00
$is_debian = in_array($distro['dist'], ['debian', 'ubuntu', 'Deepin']);
2023-10-31 14:56:43 +01:00
$to_install = $is_debian ? str_replace('xz', 'xz-utils', $missing) : $missing;
2023-12-10 18:27:19 +08:00
// debian, alpine libtool -> libtoolize
$to_install = str_replace('libtoolize', 'libtool', $to_install);
2023-09-18 12:52:12 +02:00
shell(true)->exec($prefix . $install_cmd . ' ' . implode(' ', $to_install));
2023-07-17 20:26:48 +08:00
} catch (RuntimeException) {
return false;
}
return true;
}
}