Skip to content

Commit 086c855

Browse files
committed
Use custom config.m4 for grpc extension
1 parent 4fa5292 commit 086c855

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

src/SPC/builder/extension/grpc.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use SPC\util\CustomExt;
1212
use SPC\util\GlobalEnvManager;
1313
use SPC\util\SPCConfigUtil;
14-
use SPC\util\SPCTarget;
1514

1615
#[CustomExt('grpc')]
1716
class grpc extends Extension
@@ -21,20 +20,50 @@ public function patchBeforeBuildconf(): bool
2120
if ($this->builder instanceof WindowsBuilder) {
2221
throw new ValidationException('grpc extension does not support windows yet');
2322
}
23+
24+
// Fix deprecated PHP API usage in call.c
2425
FileSystem::replaceFileStr(
25-
$this->source_dir . '/src/php/ext/grpc/call.c',
26+
"{$this->source_dir}/src/php/ext/grpc/call.c",
2627
'zend_exception_get_default(TSRMLS_C),',
2728
'zend_ce_exception,',
2829
);
29-
if (SPCTarget::getTargetOS() === 'Darwin') {
30-
FileSystem::replaceFileRegex(
31-
$this->source_dir . '/config.m4',
32-
'/GRPC_LIBDIR=.*$/m',
33-
'GRPC_LIBDIR=' . BUILD_LIB_PATH . "\n" . 'LDFLAGS="$LDFLAGS -framework CoreFoundation"'
34-
);
35-
}
36-
FileSystem::replaceFileStr("{$this->source_dir}/config.m4", "CFLAGS=\"-std=c11 -g -O2\"\n", '');
37-
file_put_contents("{$this->source_dir}/php_grpc.h", '#include "src/php/ext/grpc/php_grpc.h"');
30+
31+
$config_m4 = <<<'M4'
32+
PHP_ARG_ENABLE(grpc, [whether to enable grpc support], [AS_HELP_STRING([--enable-grpc], [Enable grpc support])])
33+
34+
if test "$PHP_GRPC" != "no"; then
35+
PHP_ADD_INCLUDE(PHP_EXT_SRCDIR()/include)
36+
PHP_ADD_INCLUDE(PHP_EXT_SRCDIR()/src/php/ext/grpc)
37+
GRPC_LIBDIR=@@build_lib_path@@
38+
PHP_ADD_LIBPATH($GRPC_LIBDIR)
39+
PHP_ADD_LIBRARY(grpc,,GRPC_SHARED_LIBADD)
40+
LIBS="-lpthread $LIBS"
41+
PHP_ADD_LIBRARY(pthread)
42+
43+
case $host in
44+
*darwin*)
45+
PHP_ADD_LIBRARY(c++,1,GRPC_SHARED_LIBADD)
46+
;;
47+
*)
48+
PHP_ADD_LIBRARY(stdc++,1,GRPC_SHARED_LIBADD)
49+
PHP_ADD_LIBRARY(rt,,GRPC_SHARED_LIBADD)
50+
PHP_ADD_LIBRARY(rt)
51+
;;
52+
esac
53+
54+
PHP_NEW_EXTENSION(grpc, @grpc_c_files@, $ext_shared, , -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1)
55+
PHP_SUBST(GRPC_SHARED_LIBADD)
56+
PHP_INSTALL_HEADERS([ext/grpc], [php_grpc.h])
57+
fi
58+
M4;
59+
$replace = get_pack_replace();
60+
// load grpc c files from src/php/ext/grpc
61+
$c_files = glob($this->source_dir . '/src/php/ext/grpc/*.c');
62+
$replace['@grpc_c_files@'] = implode(" \\\n ", array_map(fn ($f) => 'src/php/ext/grpc/' . basename($f), $c_files));
63+
$config_m4 = str_replace(array_keys($replace), array_values($replace), $config_m4);
64+
file_put_contents($this->source_dir . '/config.m4', $config_m4);
65+
66+
copy($this->source_dir . '/src/php/ext/grpc/php_grpc.h', $this->source_dir . '/php_grpc.h');
3867
return true;
3968
}
4069

@@ -50,7 +79,6 @@ public function patchBeforeConfigure(): bool
5079
public function patchBeforeMake(): bool
5180
{
5281
parent::patchBeforeMake();
53-
// add -Wno-strict-prototypes
5482
GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes');
5583
return true;
5684
}

0 commit comments

Comments
 (0)