|
4 | 4 |
|
5 | 5 | namespace StaticPHP\Doctor\Item; |
6 | 6 |
|
| 7 | +use StaticPHP\Artifact\ArtifactCache; |
| 8 | +use StaticPHP\Artifact\ArtifactDownloader; |
| 9 | +use StaticPHP\Artifact\ArtifactExtractor; |
7 | 10 | use StaticPHP\Attribute\Doctor\CheckItem; |
8 | 11 | use StaticPHP\Attribute\Doctor\FixItem; |
9 | 12 | use StaticPHP\Attribute\Doctor\OptionalCheck; |
| 13 | +use StaticPHP\DI\ApplicationContext; |
10 | 14 | use StaticPHP\Doctor\CheckResult; |
| 15 | +use StaticPHP\Runtime\Shell\Shell; |
| 16 | +use StaticPHP\Toolchain\Interface\ToolchainInterface; |
11 | 17 | use StaticPHP\Toolchain\MuslToolchain; |
12 | 18 | use StaticPHP\Toolchain\ZigToolchain; |
13 | 19 | use StaticPHP\Util\FileSystem; |
| 20 | +use StaticPHP\Util\InteractiveTerm; |
| 21 | +use StaticPHP\Util\SourcePatcher; |
14 | 22 | use StaticPHP\Util\System\LinuxUtil; |
15 | 23 |
|
16 | 24 | #[OptionalCheck([self::class, 'optionalCheck'])] |
17 | 25 | class LinuxMuslCheck |
18 | 26 | { |
19 | 27 | public static function optionalCheck(): bool |
20 | 28 | { |
21 | | - return getenv('SPC_TOOLCHAIN') === MuslToolchain::class || |
22 | | - (getenv('SPC_TOOLCHAIN') === ZigToolchain::class && !LinuxUtil::isMuslDist()); |
| 29 | + $toolchain = ApplicationContext::get(ToolchainInterface::class); |
| 30 | + return $toolchain instanceof MuslToolchain || $toolchain instanceof ZigToolchain && !LinuxUtil::isMuslDist(); |
23 | 31 | } |
24 | 32 |
|
25 | 33 | /** @noinspection PhpUnused */ |
@@ -51,23 +59,46 @@ public function checkMuslCrossMake(): CheckResult |
51 | 59 | #[FixItem('fix-musl-wrapper')] |
52 | 60 | public function fixMusl(): bool |
53 | 61 | { |
54 | | - // TODO: implement musl-wrapper installation |
55 | | - // This should: |
56 | | - // 1. Download musl source using Downloader::downloadSource() |
57 | | - // 2. Extract the source using FileSystem::extractSource() |
58 | | - // 3. Apply CVE patches using SourcePatcher::patchFile() |
59 | | - // 4. Build and install musl wrapper |
60 | | - // 5. Add path using putenv instead of editing /etc/profile |
61 | | - return false; |
| 62 | + $downloader = new ArtifactDownloader(); |
| 63 | + $downloader->add('musl-wrapper')->download(false); |
| 64 | + $extractor = new ArtifactExtractor(ApplicationContext::get(ArtifactCache::class)); |
| 65 | + $extractor->extract('musl-wrapper'); |
| 66 | + |
| 67 | + // Apply CVE-2025-26519 patch and install musl wrapper |
| 68 | + SourcePatcher::patchFile('musl-1.2.5_CVE-2025-26519_0001.patch', SOURCE_PATH . '/musl-wrapper'); |
| 69 | + SourcePatcher::patchFile('musl-1.2.5_CVE-2025-26519_0002.patch', SOURCE_PATH . '/musl-wrapper'); |
| 70 | + |
| 71 | + $prefix = ''; |
| 72 | + if (get_current_user() !== 'root') { |
| 73 | + $prefix = 'sudo '; |
| 74 | + logger()->warning('Current user is not root, using sudo for running command'); |
| 75 | + } |
| 76 | + shell()->cd(SOURCE_PATH . '/musl-wrapper') |
| 77 | + ->exec('CC=gcc CXX=g++ AR=ar LD=ld ./configure --disable-gcc-wrapper') |
| 78 | + ->exec('CC=gcc CXX=g++ AR=ar LD=ld make -j') |
| 79 | + ->exec("CC=gcc CXX=g++ AR=ar LD=ld {$prefix}make install"); |
| 80 | + return true; |
62 | 81 | } |
63 | 82 |
|
64 | 83 | #[FixItem('fix-musl-cross-make')] |
65 | 84 | public function fixMuslCrossMake(): bool |
66 | 85 | { |
67 | | - // TODO: implement musl-cross-make installation |
68 | | - // This should: |
69 | | - // 1. Install musl-toolchain package using PackageManager::installPackage() |
70 | | - // 2. Copy toolchain files to /usr/local/musl |
71 | | - return false; |
| 86 | + // sudo |
| 87 | + $prefix = ''; |
| 88 | + if (get_current_user() !== 'root') { |
| 89 | + $prefix = 'sudo '; |
| 90 | + logger()->warning('Current user is not root, using sudo for running command'); |
| 91 | + } |
| 92 | + Shell::passthruCallback(function () { |
| 93 | + InteractiveTerm::advance(); |
| 94 | + }); |
| 95 | + $downloader = new ArtifactDownloader(); |
| 96 | + $extractor = new ArtifactExtractor(ApplicationContext::get(ArtifactCache::class)); |
| 97 | + $downloader->add('musl-toolchain')->download(false); |
| 98 | + $extractor->extract('musl-toolchain'); |
| 99 | + $pkg_root = PKG_ROOT_PATH . '/musl-toolchain'; |
| 100 | + shell()->exec("{$prefix}cp -rf {$pkg_root}/* /usr/local/musl"); |
| 101 | + FileSystem::removeDir($pkg_root); |
| 102 | + return true; |
72 | 103 | } |
73 | 104 | } |
0 commit comments