turn llvm-tools into a doctor check. llvm-runtime is a target now

This commit is contained in:
henderkes
2026-05-19 20:01:50 +07:00
parent 3eca044895
commit 7bb4a09a3c
12 changed files with 326 additions and 51 deletions

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace StaticPHP\Doctor\Item;
use Package\Artifact\llvm_tools;
use StaticPHP\Attribute\Doctor\CheckItem;
use StaticPHP\Attribute\Doctor\FixItem;
use StaticPHP\Doctor\CheckResult;
use StaticPHP\Package\PackageInstaller;
class LlvmToolsCheck
{
/** @noinspection PhpUnused */
#[CheckItem('if llvm-tools (objcopy/strip/profdata) are built', limit_os: 'Linux', level: 798)]
public function checkLlvmTools(): CheckResult
{
$binDir = PKG_ROOT_PATH . '/llvm-tools/bin';
if (new llvm_tools()->allBuilt($binDir)) {
return CheckResult::ok($binDir);
}
return CheckResult::fail('llvm-tools are not built', 'build-llvm-tools');
}
#[FixItem('build-llvm-tools')]
public function fixLlvmTools(): bool
{
$installer = new PackageInstaller(interactive: false);
$installer->addInstallPackage('llvm-tools');
$installer->run(true);
new llvm_tools()->buildForHost();
return new llvm_tools()->allBuilt(PKG_ROOT_PATH . '/llvm-tools/bin');
}
}

View File

@@ -10,6 +10,7 @@ use StaticPHP\Attribute\Doctor\OptionalCheck;
use StaticPHP\DI\ApplicationContext;
use StaticPHP\Doctor\CheckResult;
use StaticPHP\Package\PackageInstaller;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Toolchain\Interface\ToolchainInterface;
use StaticPHP\Toolchain\ZigToolchain;
@@ -41,30 +42,31 @@ class ZigCheck
}
/** @noinspection PhpUnused */
#[CheckItem('if clang runtime bits are built', limit_os: 'Linux', level: 799)]
public function checkClangRuntimeBits(): ?CheckResult
#[CheckItem('if llvm compiler-rt bits are built', limit_os: 'Linux', level: 799)]
public function checkCompilerRtBits(): ?CheckResult
{
// Skip if zig is not installed yet (zig check runs at level 800)
if (!new PackageInstaller()->addInstallPackage('zig')->isPackageInstalled('zig')) {
return null;
}
$libDir = PKG_ROOT_PATH . '/zig/lib';
$libDir = PKG_ROOT_PATH . '/zig/lib/' . SystemTarget::getCanonicalTriple();
if (file_exists("{$libDir}/libclang_rt.profile.a")
&& file_exists("{$libDir}/clang_rt.crtbegin.o")
&& file_exists("{$libDir}/clang_rt.crtend.o")
) {
return CheckResult::ok("{$libDir}/libclang_rt.profile.a");
}
return CheckResult::fail('clang runtime bits are not built', 'build-clang-runtime-bits');
return CheckResult::fail('llvm compiler-rt bits are not built for ' . SystemTarget::getCanonicalTriple(), 'build-llvm-compiler-rt');
}
#[FixItem('build-clang-runtime-bits')]
public function fixClangRuntimeBits(): bool
#[FixItem('build-llvm-compiler-rt')]
public function fixCompilerRtBits(): bool
{
$installer = new PackageInstaller(interactive: false);
$installer->addInstallPackage('clang-runtime-bits');
$installer->addInstallPackage('llvm-compiler-rt');
$installer->run(true);
$libDir = PKG_ROOT_PATH . '/zig/lib';
new \Package\Artifact\llvm_compiler_rt()->buildForCurrentTarget();
$libDir = PKG_ROOT_PATH . '/zig/lib/' . SystemTarget::getCanonicalTriple();
return file_exists("{$libDir}/libclang_rt.profile.a")
&& file_exists("{$libDir}/clang_rt.crtbegin.o")
&& file_exists("{$libDir}/clang_rt.crtend.o");

View File

@@ -15,7 +15,6 @@ use StaticPHP\Util\FileSystem;
use StaticPHP\Util\GlobalPathTrait;
use StaticPHP\Util\InteractiveTerm;
use StaticPHP\Util\System\LinuxUtil;
use StaticPHP\Util\System\UnixUtil;
class PackageBuilder
{
@@ -179,7 +178,7 @@ class PackageBuilder
if (SystemTarget::getTargetOS() === 'Darwin') {
shell()->exec("dsymutil -f {$binary_path} -o {$debug_file}");
} elseif (SystemTarget::getTargetOS() === 'Linux') {
$objcopy = LinuxUtil::findCommand('llvm-objcopy') ?: 'objcopy';
$objcopy = getenv('OBJCOPY');
if ($eu_strip = LinuxUtil::findCommand('eu-strip')) {
shell()
->exec("{$eu_strip} -f {$debug_file} {$binary_path}")
@@ -201,7 +200,7 @@ class PackageBuilder
*/
public function stripBinary(string $binary_path): void
{
$strip = UnixUtil::findCommand('llvm-strip') ?: 'strip';
$strip = PKG_ROOT_PATH . '/llvm-tools/bin/llvm-strip';
shell()->exec(match (SystemTarget::getTargetOS()) {
'Darwin' => "{$strip} -S {$binary_path}",
'Linux' => "{$strip} --strip-unneeded {$binary_path}",

View File

@@ -128,6 +128,31 @@ class SystemTarget
return in_array(self::getTargetOS(), ['Linux', 'Darwin', 'BSD']);
}
/**
* Returns the canonical target triple (arch-os-abi) for per-target build
* artifacts. Always returns a non-null triple, falling back to a host-derived
* triple when SPC_TARGET is unset or names 'native'.
* Strips libc version suffix (-gnu.2.17 → -gnu) and trailing flags (' -dynamic').
*/
public static function getCanonicalTriple(): string
{
$target = (string) getenv('SPC_TARGET');
if ($target !== '' && !str_contains($target, 'native')) {
$cleaned = (string) preg_replace('/(-gnu|-musl)\.[\d.]+/', '$1', $target);
$cleaned = preg_split('/\s+/', trim($cleaned))[0] ?? '';
if ($cleaned !== '') {
return $cleaned;
}
}
$arch = self::getTargetArch();
return match (self::getTargetOS()) {
'Linux' => $arch . '-linux-' . (self::getLibc() === 'musl' ? 'musl' : 'gnu'),
'Darwin' => $arch . '-macos-none',
'Windows' => $arch . '-windows-gnu',
default => $arch . '-unknown-unknown',
};
}
/**
* Returns a GNU host triple for autoconf --host= when SPC_TARGET names an
* architecture different from the build host (true cross-compile).

View File

@@ -38,6 +38,7 @@ class ClangNativeToolchain implements UnixToolchainInterface
default => throw new EnvironmentException(__CLASS__ . ' is not supported on ' . PHP_OS_FAMILY . '.'),
};
}
GlobalEnvManager::putenv('OBJCOPY=' . PKG_ROOT_PATH . '/llvm-tools/bin/llvm-objcopy');
}
public function getCompilerInfo(): ?string

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace StaticPHP\Toolchain;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Toolchain\Interface\UnixToolchainInterface;
use StaticPHP\Util\GlobalEnvManager;
use StaticPHP\Util\System\LinuxUtil;
@@ -34,7 +35,8 @@ class ZigToolchain implements UnixToolchainInterface
GlobalEnvManager::putenv("SPC_DEFAULT_CXXFLAGS={$cxxflags}");
GlobalEnvManager::putenv("SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS={$extraCflags}");
GlobalEnvManager::putenv('RANLIB=zig-ranlib');
GlobalEnvManager::putenv('OBJCOPY=zig-objcopy');
GlobalEnvManager::putenv('SPC_COMPILER_RT_DIR=' . PKG_ROOT_PATH . '/zig/lib/' . SystemTarget::getCanonicalTriple());
GlobalEnvManager::putenv('OBJCOPY=' . PKG_ROOT_PATH . '/llvm-tools/bin/llvm-objcopy');
$extra_libs = getenv('SPC_EXTRA_LIBS') ?: '';
if (!str_contains($extra_libs, '-lunwind')) {
// Add unwind library if not already present

View File

@@ -221,9 +221,6 @@ final class PgoContext
public function mergeProfiles(): void
{
if (trim((string) shell_exec('command -v llvm-profdata 2>/dev/null')) === '') {
throw new WrongUsageException('PGO --phase=use: llvm-profdata not on PATH');
}
foreach ($this->trainableSapis as $sapi) {
$this->mergeSapi($sapi);
}
@@ -248,7 +245,8 @@ final class PgoContext
$out = $this->profDataFile($sapi);
$inputs = array_merge($raws, $csRaws);
$argv = implode(' ', array_map('escapeshellarg', $inputs));
shell()->exec('llvm-profdata merge --failure-mode=warn -output=' . escapeshellarg($out) . ' ' . $argv);
$profdata = PKG_ROOT_PATH . '/llvm-tools/bin/llvm-profdata';
shell()->exec(escapeshellarg($profdata) . ' merge --failure-mode=warn -output=' . escapeshellarg($out) . ' ' . $argv);
if (!is_file($out) || filesize($out) === 0) {
throw new WrongUsageException("PGO --phase=use: empty merge output for {$sapi}");
}