Skip to content

Commit f543b55

Browse files
authored
Merge pull request #826 from crazywhalecc/grpc
fix grpc support (use cmake, openssl)
2 parents c8cc013 + f2a3ae4 commit f543b55

File tree

13 files changed

+178
-90
lines changed

13 files changed

+178
-90
lines changed

.github/workflows/release-build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010

1111
env:
1212
PHP_VERSION: 8.4
13-
MICRO_VERSION: 8.4.4
13+
MICRO_VERSION: 8.4.10
1414

1515
jobs:
1616
build-release-artifacts:
@@ -42,12 +42,13 @@ jobs:
4242
run: echo "SPC_BUILD_DEBUG=--debug" >> $GITHUB_ENV
4343

4444
- name: "Install PHP for official runners"
45-
uses: "shivammathur/setup-php@v2"
45+
uses: shivammathur/setup-php@v2
4646
with:
4747
coverage: none
4848
tools: composer:v2
4949
php-version: "${{ env.PHP_VERSION }}"
5050
ini-values: memory_limit=-1
51+
extensions: curl, openssl, mbstring
5152

5253
- name: "Get Composer Cache Directory"
5354
id: composer-cache

.github/workflows/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ jobs:
121121
uses: shivammathur/setup-php@v2
122122
with:
123123
php-version: 8.4
124+
extensions: curl, openssl, mbstring
124125

125126
- name: Define
126127
id: gendef
@@ -198,3 +199,7 @@ jobs:
198199
- name: "Run Build Tests (build - embed for non-windows)"
199200
if: ${{ !startsWith(matrix.os, 'windows-') }}
200201
run: php src/globals/test-extensions.php build_embed_cmd ${{ matrix.os }} ${{ matrix.php }}
202+
203+
# - name: Setup tmate session
204+
# if: ${{ failure() }}
205+
# uses: mxschmitt/action-tmate@v3

.github/workflows/vitepress-deploy.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ jobs:
3232
cp -r config/* docs/.vitepress/config/
3333
3434
- name: "Install PHP for official runners"
35-
uses: "shivammathur/setup-php@v2"
35+
uses: shivammathur/setup-php@v2
3636
with:
3737
coverage: none
3838
tools: composer:v2
3939
php-version: 8.4
4040
ini-values: memory_limit=-1
41+
extensions: curl, openssl, mbstring
4142

4243
- name: "Get Composer Cache Directory"
4344
id: composer-cache

config/ext.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"bz2": {
3636
"type": "builtin",
37-
"arg-type-unix": "with-prefix",
37+
"arg-type-unix": "with-path",
3838
"arg-type-windows": "with",
3939
"lib-depends": [
4040
"bzip2"
@@ -185,7 +185,7 @@
185185
"BSD": "wip"
186186
},
187187
"type": "builtin",
188-
"arg-type": "with-prefix",
188+
"arg-type": "with-path",
189189
"lib-depends": [
190190
"gettext"
191191
]
@@ -211,7 +211,7 @@
211211
"BSD": "wip"
212212
},
213213
"type": "builtin",
214-
"arg-type": "with-prefix",
214+
"arg-type": "with-path",
215215
"lib-depends": [
216216
"gmp"
217217
]
@@ -233,7 +233,7 @@
233233
},
234234
"type": "external",
235235
"source": "grpc",
236-
"arg-type-unix": "custom",
236+
"arg-type-unix": "enable-path",
237237
"cpp-extension": true,
238238
"lib-depends": [
239239
"grpc"
@@ -244,7 +244,7 @@
244244
"BSD": "wip"
245245
},
246246
"type": "builtin",
247-
"arg-type": "with-prefix",
247+
"arg-type": "with-path",
248248
"arg-type-windows": "with",
249249
"lib-depends-unix": [
250250
"libiconv"
@@ -320,7 +320,7 @@
320320
"BSD": "wip"
321321
},
322322
"type": "builtin",
323-
"arg-type": "with-prefix",
323+
"arg-type": "with-path",
324324
"lib-depends": [
325325
"ldap"
326326
],
@@ -529,7 +529,7 @@
529529
},
530530
"notes": true,
531531
"type": "builtin",
532-
"arg-type": "with-prefix",
532+
"arg-type": "with-path",
533533
"lib-depends": [
534534
"libargon2"
535535
]
@@ -571,7 +571,7 @@
571571
"BSD": "wip"
572572
},
573573
"type": "builtin",
574-
"arg-type": "with-prefix",
574+
"arg-type": "with-path",
575575
"arg-type-windows": "custom",
576576
"ext-depends": [
577577
"pdo",
@@ -674,7 +674,7 @@
674674
"BSD": "wip"
675675
},
676676
"type": "builtin",
677-
"arg-type": "with-prefix",
677+
"arg-type": "with-path",
678678
"lib-depends": [
679679
"readline"
680680
],
@@ -785,7 +785,7 @@
785785
"BSD": "wip"
786786
},
787787
"type": "builtin",
788-
"arg-type": "with-prefix",
788+
"arg-type": "with-path",
789789
"arg-type-windows": "with",
790790
"lib-depends": [
791791
"sqlite"
@@ -811,7 +811,7 @@
811811
},
812812
"type": "external",
813813
"source": "ext-ssh2",
814-
"arg-type": "with-prefix",
814+
"arg-type": "with-path",
815815
"arg-type-windows": "with",
816816
"lib-depends": [
817817
"libssh2"
@@ -937,7 +937,7 @@
937937
"BSD": "wip"
938938
},
939939
"type": "builtin",
940-
"arg-type": "with-prefix",
940+
"arg-type": "with-path",
941941
"lib-depends": [
942942
"tidy"
943943
]
@@ -953,7 +953,7 @@
953953
},
954954
"type": "external",
955955
"source": "ext-uuid",
956-
"arg-type": "with-prefix",
956+
"arg-type": "with-path",
957957
"lib-depends": [
958958
"libuuid"
959959
]
@@ -965,7 +965,7 @@
965965
},
966966
"type": "external",
967967
"source": "ext-uv",
968-
"arg-type": "with-prefix",
968+
"arg-type": "with-path",
969969
"lib-depends": [
970970
"libuv"
971971
],
@@ -1067,7 +1067,7 @@
10671067
"BSD": "wip"
10681068
},
10691069
"type": "builtin",
1070-
"arg-type": "with-prefix",
1070+
"arg-type": "with-path",
10711071
"lib-depends": [
10721072
"libxslt"
10731073
],
@@ -1104,7 +1104,7 @@
11041104
},
11051105
"type": "external",
11061106
"source": "yaml",
1107-
"arg-type-unix": "with-prefix",
1107+
"arg-type-unix": "with-path",
11081108
"arg-type-windows": "with",
11091109
"lib-depends": [
11101110
"libyaml"
@@ -1115,7 +1115,7 @@
11151115
"BSD": "wip"
11161116
},
11171117
"type": "builtin",
1118-
"arg-type": "with-prefix",
1118+
"arg-type": "with-path",
11191119
"arg-type-windows": "enable",
11201120
"lib-depends-unix": [
11211121
"libzip"

config/lib.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,15 @@
186186
},
187187
"grpc": {
188188
"source": "grpc",
189-
"static-libs-unix": [
190-
"libgrpc.a",
191-
"libcares.a"
189+
"pkg-configs": [
190+
"grpc"
192191
],
193192
"lib-depends": [
194193
"zlib",
195-
"openssl"
194+
"openssl",
195+
"libcares"
196196
],
197+
"provide-pre-built": true,
197198
"frameworks": [
198199
"CoreFoundation"
199200
]

src/SPC/builder/Extension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ public function getConfigureArg(bool $shared = false): string
8383
*/
8484
public function getEnableArg(bool $shared = false): string
8585
{
86+
$escapedPath = str_replace("'", '', escapeshellarg(BUILD_ROOT_PATH)) !== BUILD_ROOT_PATH || str_contains(BUILD_ROOT_PATH, ' ') ? escapeshellarg(BUILD_ROOT_PATH) : BUILD_ROOT_PATH;
8687
$_name = str_replace('_', '-', $this->name);
8788
return match ($arg_type = Config::getExt($this->name, 'arg-type', 'enable')) {
8889
'enable' => '--enable-' . $_name . ($shared ? '=shared' : '') . ' ',
90+
'enable-path' => '--enable-' . $_name . '=' . ($shared ? 'shared,' : '') . $escapedPath . ' ',
8991
'with' => '--with-' . $_name . ($shared ? '=shared' : '') . ' ',
90-
'with-prefix' => '--with-' . $_name . '=' . ($shared ? 'shared,' : '') . '"' . BUILD_ROOT_PATH . '" ',
92+
'with-path' => '--with-' . $_name . '=' . ($shared ? 'shared,' : '') . $escapedPath . ' ',
9193
'none', 'custom' => '',
92-
default => throw new WrongUsageException("argType does not accept {$arg_type}, use [enable/with/with-prefix] ."),
94+
default => throw new WrongUsageException("argType does not accept {$arg_type}, use [enable/with/with-path] ."),
9395
};
9496
}
9597

src/SPC/builder/extension/grpc.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,53 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8-
use SPC\builder\macos\MacOSBuilder;
98
use SPC\builder\windows\WindowsBuilder;
109
use SPC\store\FileSystem;
1110
use SPC\util\CustomExt;
1211
use SPC\util\GlobalEnvManager;
12+
use SPC\util\SPCConfigUtil;
13+
use SPC\util\SPCTarget;
1314

1415
#[CustomExt('grpc')]
1516
class grpc extends Extension
1617
{
1718
public function patchBeforeBuildconf(): bool
1819
{
19-
// soft link to the grpc source code
2020
if ($this->builder instanceof WindowsBuilder) {
21-
// not support windows yet
2221
throw new \RuntimeException('grpc extension does not support windows yet');
2322
}
24-
if (!is_link(SOURCE_PATH . '/php-src/ext/grpc')) {
25-
if (is_dir($this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc')) {
26-
shell()->exec('ln -s ' . $this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
27-
} elseif (is_dir(BUILD_ROOT_PATH . '/grpc_php_ext_src')) {
28-
shell()->exec('ln -s ' . BUILD_ROOT_PATH . '/grpc_php_ext_src ' . SOURCE_PATH . '/php-src/ext/grpc');
29-
} else {
30-
throw new \RuntimeException('Cannot find grpc source code');
31-
}
32-
$macos = $this->builder instanceof MacOSBuilder ? "\n" . ' LDFLAGS="$LDFLAGS -framework CoreFoundation"' : '';
33-
FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/ext/grpc/config.m4', '/GRPC_LIBDIR=.*$/m', 'GRPC_LIBDIR=' . BUILD_LIB_PATH . $macos);
34-
FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/ext/grpc/config.m4', '/SEARCH_PATH=.*$/m', 'SEARCH_PATH="' . BUILD_ROOT_PATH . '"');
35-
return true;
23+
if (file_exists(SOURCE_PATH . '/php-src/ext/grpc')) {
24+
return false;
25+
}
26+
// soft link to the grpc source code
27+
if (is_dir($this->source_dir . '/src/php/ext/grpc')) {
28+
shell()->exec('ln -s ' . $this->source_dir . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
29+
} else {
30+
throw new \RuntimeException('Cannot find grpc source code');
31+
}
32+
if (SPCTarget::getTargetOS() === 'Darwin') {
33+
FileSystem::replaceFileRegex(
34+
SOURCE_PATH . '/php-src/ext/grpc/config.m4',
35+
'/GRPC_LIBDIR=.*$/m',
36+
'GRPC_LIBDIR=' . BUILD_LIB_PATH . "\n" . 'LDFLAGS="$LDFLAGS -framework CoreFoundation"'
37+
);
3638
}
37-
return false;
39+
return true;
3840
}
3941

40-
public function patchBeforeMake(): bool
42+
public function patchBeforeConfigure(): bool
4143
{
42-
// add -Wno-strict-prototypes
43-
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes');
44+
$util = new SPCConfigUtil($this->builder, ['libs_only_deps' => true]);
45+
$config = $util->config(['grpc']);
46+
$libs = $config['libs'];
47+
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/configure', '-lgrpc', $libs);
4448
return true;
4549
}
4650

47-
public function getUnixConfigureArg(bool $shared = false): string
51+
public function patchBeforeMake(): bool
4852
{
49-
return '--enable-grpc=' . BUILD_ROOT_PATH . '/grpc GRPC_LIB_SUBDIR=' . BUILD_LIB_PATH;
53+
// add -Wno-strict-prototypes
54+
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes');
55+
return true;
5056
}
5157
}

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,22 @@ protected function sanityCheck(int $build_target): void
128128
}
129129
// if someone changed to --enable-embed=shared, we need to add LD_LIBRARY_PATH
130130
if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'shared') {
131-
$ext_path = 'LD_LIBRARY_PATH=' . BUILD_ROOT_PATH . '/lib:$LD_LIBRARY_PATH ';
132-
FileSystem::removeFileIfExists(BUILD_ROOT_PATH . '/lib/libphp.a');
131+
if (PHP_OS_FAMILY === 'Darwin') {
132+
$ext_path = 'DYLD_LIBRARY_PATH=' . BUILD_LIB_PATH . ':$DYLD_LIBRARY_PATH ';
133+
} else {
134+
$ext_path = 'LD_LIBRARY_PATH=' . BUILD_LIB_PATH . ':$LD_LIBRARY_PATH ';
135+
}
136+
FileSystem::removeFileIfExists(BUILD_LIB_PATH . '/libphp.a');
133137
} else {
134138
$ext_path = '';
135-
FileSystem::removeFileIfExists(BUILD_ROOT_PATH . '/lib/libphp.so');
139+
$suffix = PHP_OS_FAMILY === 'Darwin' ? 'dylib' : 'so';
140+
foreach (glob(BUILD_LIB_PATH . "/libphp*.{$suffix}") as $file) {
141+
unlink($file);
142+
}
143+
}
144+
[$ret, $out] = shell()->cd($sample_file_path)->execWithResult(getenv('CC') . ' -o embed embed.c ' . $lens);
145+
if ($ret !== 0) {
146+
throw new RuntimeException('embed failed sanity check: build failed. Error message: ' . implode("\n", $out));
136147
}
137148
[$ret, $output] = shell()->cd($sample_file_path)->execWithResult($ext_path . './embed');
138149
if ($ret !== 0 || trim(implode('', $output)) !== 'hello') {
@@ -147,8 +158,9 @@ protected function sanityCheck(int $build_target): void
147158
if (!file_exists($frankenphp)) {
148159
throw new RuntimeException('FrankenPHP binary not found: ' . $frankenphp);
149160
}
161+
$prefix = PHP_OS_FAMILY === 'Darwin' ? 'DYLD_' : 'LD_';
150162
[$ret, $output] = shell()
151-
->setEnv(['LD_LIBRARY_PATH' => BUILD_LIB_PATH])
163+
->setEnv(["{$prefix}LIBRARY_PATH" => BUILD_LIB_PATH])
152164
->execWithResult("{$frankenphp} version");
153165
if ($ret !== 0 || !str_contains(implode('', $output), 'FrankenPHP')) {
154166
throw new RuntimeException('FrankenPHP failed sanity check: ret[' . $ret . ']. out[' . implode('', $output) . ']');

0 commit comments

Comments
 (0)