diff --git a/docs/capabilities.md b/docs/capabilities.md index 17e55d88..4e2c82b5 100644 --- a/docs/capabilities.md +++ b/docs/capabilities.md @@ -88,7 +88,7 @@ Auto-generated by `script/capability-matrix.php`. Do not edit by hand. | `sizeof` | yes | yes | yes | standard | | | `sort` | yes | yes | yes | standard | | | `sqrt` | yes | yes | yes | standard | | -| `str_contains` | yes | yes | yes | standard | | +| `str_contains` | yes | yes | yes | standard | AOT PHPT | | `str_ends_with` | yes | yes | yes | standard | AOT PHPT | | `str_pad` | yes | yes | yes | standard | AOT PHPT | | `str_repeat` | yes | yes | yes | standard | AOT PHPT | diff --git a/src/llvm-env.php b/src/llvm-env.php index 0d9f9d9b..a359a38b 100644 --- a/src/llvm-env.php +++ b/src/llvm-env.php @@ -8,7 +8,15 @@ * putenv('LD_LIBRARY_PATH') does not affect in-process dlopen on glibc; preload bundled * dependencies with dlopen(RTLD_GLOBAL) before libLLVM is opened via FFI. */ -$llvmDir = dirname(__DIR__) . '/.llvm'; +$repoRoot = dirname(__DIR__); +$llvmDir = ''; +if (is_file($repoRoot.'/.llvm/libLLVM-9.so.1')) { + $llvmDir = $repoRoot.'/.llvm'; +} elseif (getenv('PHP_COMPILER_LLVM_PATH') && is_file(getenv('PHP_COMPILER_LLVM_PATH').'/libLLVM-9.so.1')) { + $llvmDir = getenv('PHP_COMPILER_LLVM_PATH'); +} elseif (is_file('/opt/llvm9/libLLVM-9.so.1')) { + $llvmDir = '/opt/llvm9'; +} /** * @param list $names Basenames under $dir (e.g. libffi.so.7). @@ -50,7 +58,7 @@ function php_compiler_preload_llvm_deps(string $dir, array $names): void } } -if (is_file($llvmDir . '/libLLVM-9.so.1')) { +if ('' !== $llvmDir && is_file($llvmDir . '/libLLVM-9.so.1')) { putenv('PHP_COMPILER_LLVM_PATH=' . $llvmDir); $ldPath = getenv('LD_LIBRARY_PATH'); putenv('LD_LIBRARY_PATH=' . $llvmDir . (false === $ldPath || '' === $ldPath ? '' : ':' . $ldPath)); @@ -58,7 +66,7 @@ function php_compiler_preload_llvm_deps(string $dir, array $names): void $_SERVER['LD_LIBRARY_PATH'] = $_ENV['LD_LIBRARY_PATH']; php_compiler_preload_llvm_deps($llvmDir, ['libffi.so.7']); } -if (is_executable($llvmDir . '/clang-9')) { +if ('' !== $llvmDir && is_executable($llvmDir . '/clang-9')) { $path = getenv('PATH'); putenv('PATH=' . $llvmDir . (false === $path ? '' : ':' . $path)); $_ENV['PATH'] = getenv('PATH'); diff --git a/test/BaseTest.php b/test/BaseTest.php index f31ef304..e720b714 100755 --- a/test/BaseTest.php +++ b/test/BaseTest.php @@ -235,16 +235,7 @@ protected function normalize(string $string): string { */ protected static function applyLlvmToolchainEnv(array &$env): void { - $llvmDir = dirname(__DIR__).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; + LlvmToolchain::applyProcessEnv($env, dirname(__DIR__)); } /** @@ -252,22 +243,7 @@ protected static function applyLlvmToolchainEnv(array &$env): void */ protected static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__)); } } \ No newline at end of file diff --git a/test/LlvmToolchain.php b/test/LlvmToolchain.php new file mode 100644 index 00000000..49b1ead5 --- /dev/null +++ b/test/LlvmToolchain.php @@ -0,0 +1,103 @@ + $env + */ + public static function applyProcessEnv(array &$env, ?string $repoRoot = null): void + { + $dir = self::resolveDir($repoRoot); + if (null === $dir) { + return; + } + $env['PHP_COMPILER_LLVM_PATH'] = $dir; + $ld = $env['LD_LIBRARY_PATH'] ?? ''; + $env['LD_LIBRARY_PATH'] = '' === $ld ? $dir : $dir.':'.$ld; + $path = $env['PATH'] ?? ''; + $env['PATH'] = '' === $path ? $dir : $dir.':'.$path; + } + + /** + * @return list + */ + public static function envPrefix(?string $repoRoot = null): array + { + $dir = self::resolveDir($repoRoot); + if (null === $dir) { + return []; + } + $ld = getenv('LD_LIBRARY_PATH'); + $ldVal = false === $ld || '' === $ld ? $dir : $dir.':'.$ld; + $path = getenv('PATH'); + $pathVal = false === $path || '' === $path ? $dir : $dir.':'.$path; + + return [ + 'env', + 'LD_LIBRARY_PATH='.$ldVal, + 'PATH='.$pathVal, + 'PHP_COMPILER_LLVM_PATH='.$dir, + ]; + } +} diff --git a/test/aot/AotTest.php b/test/aot/AotTest.php index d8d9f2f7..24716360 100644 --- a/test/aot/AotTest.php +++ b/test/aot/AotTest.php @@ -32,23 +32,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - $_ENV['PHP_COMPILER_LLVM_PATH'] = $llvmDir; - $_SERVER['PHP_COMPILER_LLVM_PATH'] = $llvmDir; - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -77,15 +61,7 @@ public function testCases(string $name, string $code, array $sections): void $env[$key] = $value; } } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); if (isset($sections['ENV'])) { foreach (explode("\n", trim($sections['ENV'])) as $line) { $line = trim($line); diff --git a/test/aot/ExampleWebAotTest.php b/test/aot/ExampleWebAotTest.php index 1399c20a..77cb3c29 100644 --- a/test/aot/ExampleWebAotTest.php +++ b/test/aot/ExampleWebAotTest.php @@ -100,15 +100,7 @@ private function llvmProcessEnv(string $repoRoot): array $env[$key] = $value; } } - $llvmDir = $repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); return $env; } @@ -147,21 +139,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -171,21 +149,6 @@ private static function isLlvmReady(): bool */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } } diff --git a/test/aot/NestedSuperglobalsAotTest.php b/test/aot/NestedSuperglobalsAotTest.php index 9edaa8ad..290897cd 100644 --- a/test/aot/NestedSuperglobalsAotTest.php +++ b/test/aot/NestedSuperglobalsAotTest.php @@ -102,15 +102,7 @@ private static function llvmEnvironment(string $repoRoot): array $env[$key] = $value; } } - $llvmDir = $repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); return $env; } @@ -120,21 +112,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -171,21 +149,6 @@ private static function phpCommand(): array */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } } diff --git a/test/aot/RuntimeSuperglobalRefreshTest.php b/test/aot/RuntimeSuperglobalRefreshTest.php index d7047a3a..4ceecaf6 100644 --- a/test/aot/RuntimeSuperglobalRefreshTest.php +++ b/test/aot/RuntimeSuperglobalRefreshTest.php @@ -161,15 +161,7 @@ private function llvmProcessEnv(string $repoRoot): array $env[$key] = $value; } } - $llvmDir = $repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); return $env; } @@ -179,21 +171,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -230,21 +208,6 @@ private static function phpCommand(): array */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } } diff --git a/test/aot/StdlibWebBuiltinsTest.php b/test/aot/StdlibWebBuiltinsTest.php index c2d78e23..9e77be55 100644 --- a/test/aot/StdlibWebBuiltinsTest.php +++ b/test/aot/StdlibWebBuiltinsTest.php @@ -101,15 +101,7 @@ private static function llvmEnvironment(string $repoRoot): array $env[$key] = $value; } } - $llvmDir = $repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); return $env; } @@ -119,21 +111,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -172,21 +150,6 @@ private static function phpCommand(): array */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } } diff --git a/test/aot/WebAppNumberFormatTest.php b/test/aot/WebAppNumberFormatTest.php index 5aecba68..84136d99 100644 --- a/test/aot/WebAppNumberFormatTest.php +++ b/test/aot/WebAppNumberFormatTest.php @@ -98,15 +98,7 @@ private static function llvmEnvironment(string $repoRoot): array $env[$key] = $value; } } - $llvmDir = $repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); return $env; } @@ -116,21 +108,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -169,21 +147,6 @@ private static function phpCommand(): array */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } } diff --git a/test/bootstrap.php b/test/bootstrap.php index c273cc0c..62757b83 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -9,3 +9,4 @@ require __DIR__.'/../src/yay-php8-compat.php'; require __DIR__.'/../src/llvm-env.php'; require __DIR__.'/../vendor/autoload.php'; +require __DIR__.'/LlvmToolchain.php'; diff --git a/test/compliance/JITTest.php b/test/compliance/JITTest.php index 97639578..c3804dfc 100755 --- a/test/compliance/JITTest.php +++ b/test/compliance/JITTest.php @@ -16,17 +16,10 @@ class JITTest extends BaseTest { public function setUp(): void { $this->BIN = realpath(__DIR__ . '/../../bin/jit.php'); - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$prefix); - } - } - try { - \PHPLLVM\Chooser::choose(); - } catch (\Throwable $e) { - $this->markTestSkipped('LLVM not available for JIT tests: '.$e->getMessage()); + if (!LlvmToolchain::isReady(dirname(__DIR__, 2))) { + $this->markTestSkipped( + 'LLVM 9 toolchain not available. Run script/install-llvm9.sh or use the 22.04-dev Docker image.' + ); } } diff --git a/test/fixtures/aot/cases/str_contains.phpt b/test/fixtures/aot/cases/str_contains.phpt new file mode 100644 index 00000000..dfe7c1d6 --- /dev/null +++ b/test/fixtures/aot/cases/str_contains.phpt @@ -0,0 +1,12 @@ +--TEST-- +AOT: str_contains() for request path / user-agent style checks +--FILE-- +repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = getenv('LD_LIBRARY_PATH'); - $env['LD_LIBRARY_PATH'] = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $env['PATH'] = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $this->repoRoot); return $env; } @@ -197,18 +189,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -218,21 +199,6 @@ private static function isLlvmReady(): bool */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } } diff --git a/test/unit/ExamplesCompileTest.php b/test/unit/ExamplesCompileTest.php index 32dd096b..6545c4d4 100644 --- a/test/unit/ExamplesCompileTest.php +++ b/test/unit/ExamplesCompileTest.php @@ -140,15 +140,7 @@ private function llvmProcessEnv(string $repoRoot): array $env[$key] = $value; } } - $llvmDir = $repoRoot.'/.llvm'; - if (is_file($llvmDir.'/libLLVM-9.so.1')) { - $prefix = realpath($llvmDir) ?: $llvmDir; - $env['PHP_COMPILER_LLVM_PATH'] = $prefix; - $ld = $env['LD_LIBRARY_PATH'] ?? ''; - $env['LD_LIBRARY_PATH'] = '' === $ld ? $prefix : $prefix.':'.$ld; - $path = $env['PATH'] ?? ''; - $env['PATH'] = '' === $path ? $prefix : $prefix.':'.$path; - } + LlvmToolchain::applyProcessEnv($env, $repoRoot); return $env; } @@ -186,21 +178,7 @@ private static function isLlvmReady(): bool if (null !== self::$llvmReady) { return self::$llvmReady; } - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - self::$llvmReady = false; - - return false; - } - if ('' === getenv('PHP_COMPILER_LLVM_PATH')) { - putenv('PHP_COMPILER_LLVM_PATH='.$llvmDir); - } - try { - \PHPLLVM\Chooser::choose(); - self::$llvmReady = true; - } catch (\Throwable $e) { - self::$llvmReady = false; - } + self::$llvmReady = LlvmToolchain::isReady(dirname(__DIR__, 2)); return self::$llvmReady; } @@ -210,21 +188,6 @@ private static function isLlvmReady(): bool */ private static function llvmEnvPrefix(): array { - $llvmDir = dirname(__DIR__, 2).'/.llvm'; - if (!is_file($llvmDir.'/libLLVM-9.so.1')) { - return []; - } - $prefix = realpath($llvmDir) ?: $llvmDir; - $ld = getenv('LD_LIBRARY_PATH'); - $ldVal = false === $ld || '' === $ld ? $prefix : $prefix.':'.$ld; - $path = getenv('PATH'); - $pathVal = false === $path || '' === $path ? $prefix : $prefix.':'.$path; - - return [ - 'env', - 'LD_LIBRARY_PATH='.$ldVal, - 'PATH='.$pathVal, - 'PHP_COMPILER_LLVM_PATH='.$prefix, - ]; + return LlvmToolchain::envPrefix(dirname(__DIR__, 2)); } }