67 lines
2.1 KiB
PHP
Raw Normal View History

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\exception\ValidationException;
2024-11-09 22:07:52 +08:00
use SPC\store\FileSystem;
use SPC\util\CustomExt;
use SPC\util\GlobalEnvManager;
2025-07-23 13:55:09 +07:00
use SPC\util\SPCConfigUtil;
2025-07-22 17:17:11 +07:00
use SPC\util\SPCTarget;
2024-11-09 22:07:52 +08:00
#[CustomExt('grpc')]
class grpc extends Extension
{
public function patchBeforeBuildconf(): bool
{
if ($this->builder instanceof WindowsBuilder) {
throw new ValidationException('grpc extension does not support windows yet');
2024-11-09 22:07:52 +08:00
}
if (file_exists(SOURCE_PATH . '/php-src/ext/grpc')) {
return false;
}
// soft link to the grpc source code
2025-07-22 18:42:34 +07:00
if (is_dir($this->source_dir . '/src/php/ext/grpc')) {
shell()->exec('ln -s ' . $this->source_dir . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
} else {
throw new ValidationException('Cannot find grpc source code in ' . $this->source_dir . '/src/php/ext/grpc');
2024-11-09 22:07:52 +08:00
}
2025-07-22 17:17:11 +07:00
if (SPCTarget::getTargetOS() === 'Darwin') {
FileSystem::replaceFileRegex(
SOURCE_PATH . '/php-src/ext/grpc/config.m4',
'/GRPC_LIBDIR=.*$/m',
'GRPC_LIBDIR=' . BUILD_LIB_PATH . "\n" . 'LDFLAGS="$LDFLAGS -framework CoreFoundation"'
);
2024-11-09 22:07:52 +08:00
}
return true;
2024-11-09 22:07:52 +08:00
}
2025-07-21 09:41:36 +07:00
public function patchBeforeConfigure(): bool
{
2025-07-23 14:10:28 +07:00
$util = new SPCConfigUtil($this->builder, ['libs_only_deps' => true]);
2025-07-23 13:55:09 +07:00
$config = $util->config(['grpc']);
$libs = $config['libs'];
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/configure', '-lgrpc', $libs);
return true;
2024-11-09 22:07:52 +08:00
}
public function patchBeforeMake(): bool
{
parent::patchBeforeMake();
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;
}
protected function getSharedExtensionEnv(): array
{
$env = parent::getSharedExtensionEnv();
$env['CPPFLAGS'] = $env['CXXFLAGS'];
2025-09-18 18:17:48 +02:00
return $env;
}
2024-11-09 22:07:52 +08:00
}