2024-11-09 22:07:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\builder\extension;
|
|
|
|
|
|
|
|
|
|
use SPC\builder\Extension;
|
|
|
|
|
use SPC\builder\windows\WindowsBuilder;
|
|
|
|
|
use SPC\store\FileSystem;
|
|
|
|
|
use SPC\util\CustomExt;
|
|
|
|
|
use SPC\util\GlobalEnvManager;
|
|
|
|
|
|
|
|
|
|
#[CustomExt('grpc')]
|
|
|
|
|
class grpc extends Extension
|
|
|
|
|
{
|
|
|
|
|
public function patchBeforeBuildconf(): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->builder instanceof WindowsBuilder) {
|
|
|
|
|
throw new \RuntimeException('grpc extension does not support windows yet');
|
|
|
|
|
}
|
2025-07-22 16:26:37 +07:00
|
|
|
if (file_exists(SOURCE_PATH . '/php-src/ext/grpc')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// soft link to the grpc source code
|
|
|
|
|
if (is_dir($this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc')) {
|
|
|
|
|
shell()->exec('ln -s ' . $this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
|
|
|
|
|
} else {
|
|
|
|
|
throw new \RuntimeException('Cannot find grpc source code');
|
2024-11-09 22:07:52 +08:00
|
|
|
}
|
2025-07-22 16:26:37 +07:00
|
|
|
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/ext/grpc/config.m4', 'PHP_ARG_ENABLE(grpc,', 'PHP_ARG_WITH(grpc,');
|
|
|
|
|
return true;
|
2024-11-09 22:07:52 +08:00
|
|
|
}
|
2025-07-21 09:41:36 +07:00
|
|
|
|
2025-07-21 00:32:43 +07:00
|
|
|
public function patchBeforeConfigure(): bool
|
|
|
|
|
{
|
2025-07-22 10:54:44 +07:00
|
|
|
$libs = join(' ', $this->getLibraries());
|
2025-07-21 00:32:43 +07:00
|
|
|
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/configure', '-lgrpc', $libs);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-11-09 22:07:52 +08:00
|
|
|
|
|
|
|
|
public function patchBeforeMake(): bool
|
|
|
|
|
{
|
2025-07-21 00:32:43 +07:00
|
|
|
$extra_libs = trim(getenv('SPC_EXTRA_LIBS'));
|
2025-07-22 10:54:44 +07:00
|
|
|
$libs = array_map(function (string $lib) {
|
|
|
|
|
if (str_starts_with($lib, '-l')) {
|
|
|
|
|
$staticLib = substr($lib, 2);
|
|
|
|
|
$staticLib = BUILD_LIB_PATH . '/lib' . $staticLib . '.a';
|
|
|
|
|
if (file_exists($staticLib)) {
|
|
|
|
|
return $staticLib;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $lib;
|
|
|
|
|
}, $this->getLibraries());
|
2025-07-22 11:30:00 +07:00
|
|
|
$extra_libs = str_replace(BUILD_LIB_PATH . '/libgrpc.a', join(' ', $libs), $extra_libs);
|
2025-07-21 00:32:43 +07:00
|
|
|
f_putenv('SPC_EXTRA_LIBS=' . $extra_libs);
|
2024-11-09 22:07:52 +08:00
|
|
|
// add -Wno-strict-prototypes
|
|
|
|
|
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 00:32:43 +07:00
|
|
|
private function getLibraries(): array
|
|
|
|
|
{
|
2025-07-21 12:10:14 +07:00
|
|
|
$libs = shell()->execWithResult('$PKG_CONFIG --libs --static grpc')[1][0];
|
2025-07-22 10:54:44 +07:00
|
|
|
$filtered = preg_replace('/-L\S+/', '', $libs);
|
2025-07-21 09:33:29 +07:00
|
|
|
$filtered = preg_replace('/(?:\S*\/)?lib([a-zA-Z0-9_+-]+)\.a\b/', '-l$1', $filtered);
|
2025-07-22 10:54:44 +07:00
|
|
|
$out = preg_replace('/\s+/', ' ', $filtered);
|
2025-07-21 09:33:29 +07:00
|
|
|
return explode(' ', trim($out));
|
2025-07-21 00:32:43 +07:00
|
|
|
}
|
2024-11-09 22:07:52 +08:00
|
|
|
}
|