Skip to content

Commit 2bfc5e1

Browse files
committed
Add grpc support for macOS and Linux
1 parent 3c4d47d commit 2bfc5e1

File tree

9 files changed

+137
-4
lines changed

9 files changed

+137
-4
lines changed

config/ext.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@
199199
"gmssl"
200200
]
201201
},
202+
"grpc": {
203+
"support": {
204+
"Windows": "wip",
205+
"BSD": "wip"
206+
},
207+
"type": "external",
208+
"source": "grpc",
209+
"arg-type-unix": "custom",
210+
"cpp-extension": true,
211+
"lib-depends": [
212+
"grpc"
213+
]
214+
},
202215
"iconv": {
203216
"support": {
204217
"BSD": "wip"

config/lib.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@
139139
"Security"
140140
]
141141
},
142+
"grpc": {
143+
"source": "grpc",
144+
"static-libs-unix": [
145+
"libgrpc.a",
146+
"libboringssl.a",
147+
"libcares.a"
148+
],
149+
"lib-depends": [
150+
"zlib"
151+
],
152+
"frameworks": [
153+
"CoreFoundation"
154+
]
155+
},
142156
"icu": {
143157
"source": "icu",
144158
"cpp-library": true,

config/source.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@
211211
"path": "LICENSE"
212212
}
213213
},
214+
"grpc": {
215+
"type": "git",
216+
"rev": "v1.68.x",
217+
"url": "https://github.com/grpc/grpc.git",
218+
"provide-pre-built": true,
219+
"license": {
220+
"type": "file",
221+
"path": "LICENSE"
222+
}
223+
},
214224
"icu": {
215225
"type": "ghrel",
216226
"repo": "unicode-org/icu",

src/SPC/builder/extension/grpc.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\extension;
6+
7+
use SPC\builder\Extension;
8+
use SPC\builder\macos\MacOSBuilder;
9+
use SPC\builder\windows\WindowsBuilder;
10+
use SPC\store\FileSystem;
11+
use SPC\util\CustomExt;
12+
use SPC\util\GlobalEnvManager;
13+
14+
#[CustomExt('grpc')]
15+
class grpc extends Extension
16+
{
17+
public function patchBeforeBuildconf(): bool
18+
{
19+
// soft link to the grpc source code
20+
if ($this->builder instanceof WindowsBuilder) {
21+
// not support windows yet
22+
throw new \RuntimeException('grpc extension does not support windows yet');
23+
}
24+
if (!is_link(SOURCE_PATH . '/php-src/ext/grpc')) {
25+
shell()->exec('ln -s ' . $this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
26+
$macos = $this->builder instanceof MacOSBuilder ? "\n" . ' LDFLAGS="$LDFLAGS -framework CoreFoundation"' : '';
27+
FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/ext/grpc/config.m4', '/GRPC_LIBDIR=.*$/m', 'GRPC_LIBDIR=' . BUILD_LIB_PATH . $macos);
28+
FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/ext/grpc/config.m4', '/SEARCH_PATH=.*$/m', 'SEARCH_PATH="' . BUILD_ROOT_PATH . '"');
29+
return true;
30+
}
31+
return false;
32+
}
33+
34+
public function patchBeforeMake(): bool
35+
{
36+
// add -Wno-strict-prototypes
37+
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes');
38+
return true;
39+
}
40+
41+
public function getUnixConfigureArg(): string
42+
{
43+
return '--enable-grpc=' . BUILD_ROOT_PATH . '/grpc GRPC_LIB_SUBDIR=' . BUILD_LIB_PATH;
44+
}
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\linux\library;
6+
7+
class grpc extends LinuxLibraryBase
8+
{
9+
use \SPC\builder\unix\library\grpc;
10+
11+
public const NAME = 'grpc';
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\macos\library;
6+
7+
class grpc extends MacOSLibraryBase
8+
{
9+
use \SPC\builder\unix\library\grpc;
10+
11+
public const NAME = 'grpc';
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\unix\library;
6+
7+
use SPC\store\FileSystem;
8+
9+
trait grpc
10+
{
11+
protected function build(): void
12+
{
13+
shell()->cd($this->source_dir)
14+
->exec('EXTRA_DEFINES=GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK CXXFLAGS="-L' . BUILD_LIB_PATH . ' -I' . BUILD_INCLUDE_PATH . '" make static -j' . $this->builder->concurrency);
15+
copy($this->source_dir . '/libs/opt/libgrpc.a', BUILD_LIB_PATH . '/libgrpc.a');
16+
copy($this->source_dir . '/libs/opt/libboringssl.a', BUILD_LIB_PATH . '/libboringssl.a');
17+
if (!file_exists(BUILD_LIB_PATH . '/libcares.a')) {
18+
copy($this->source_dir . '/libs/opt/libcares.a', BUILD_LIB_PATH . '/libcares.a');
19+
}
20+
FileSystem::copyDir($this->source_dir . '/include/grpc', BUILD_INCLUDE_PATH . '/grpc');
21+
FileSystem::copyDir($this->source_dir . '/include/grpc++', BUILD_INCLUDE_PATH . '/grpc++');
22+
FileSystem::copyDir($this->source_dir . '/include/grpcpp', BUILD_INCLUDE_PATH . '/grpcpp');
23+
}
24+
}

src/globals/ext-tests/grpc.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
assert(class_exists('Grpc\ChannelCredentials'));

src/globals/test-extensions.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
$test_os = [
2424
'macos-14',
2525
'ubuntu-latest',
26-
'macos-13',
27-
'windows-latest',
2826
];
2927

3028
// whether enable thread safe
@@ -40,7 +38,7 @@
4038

4139
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
4240
$extensions = match (PHP_OS_FAMILY) {
43-
'Linux', 'Darwin' => '',
41+
'Linux', 'Darwin' => 'grpc',
4442
'Windows' => 'amqp,apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,ds,exif,ffi,fileinfo,filter,ftp,gd,iconv,igbinary,libxml,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pdo,pdo_mysql,pdo_sqlite,pdo_sqlsrv,phar,rar,redis,session,shmop,simdjson,simplexml,soap,sockets,sqlite3,sqlsrv,ssh2,swow,sysvshm,tokenizer,xml,xmlreader,xmlwriter,yac,yaml,zip,zlib',
4543
};
4644

@@ -54,7 +52,7 @@
5452
// You can use `common`, `bulk`, `minimal` or `none`.
5553
// note: combination is only available for *nix platform. Windows must use `none` combination
5654
$base_combination = match (PHP_OS_FAMILY) {
57-
'Linux', 'Darwin' => 'bulk',
55+
'Linux', 'Darwin' => 'minimal',
5856
'Windows' => 'none',
5957
};
6058

0 commit comments

Comments
 (0)