From 2bfc5e1d7445e22e70954b39c108f4b7a9fb6d69 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 9 Nov 2024 22:07:52 +0800 Subject: [PATCH] Add grpc support for macOS and Linux --- config/ext.json | 13 ++++++++ config/lib.json | 14 ++++++++ config/source.json | 10 ++++++ src/SPC/builder/extension/grpc.php | 45 ++++++++++++++++++++++++++ src/SPC/builder/linux/library/grpc.php | 12 +++++++ src/SPC/builder/macos/library/grpc.php | 12 +++++++ src/SPC/builder/unix/library/grpc.php | 24 ++++++++++++++ src/globals/ext-tests/grpc.php | 5 +++ src/globals/test-extensions.php | 6 ++-- 9 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 src/SPC/builder/extension/grpc.php create mode 100644 src/SPC/builder/linux/library/grpc.php create mode 100644 src/SPC/builder/macos/library/grpc.php create mode 100644 src/SPC/builder/unix/library/grpc.php create mode 100644 src/globals/ext-tests/grpc.php diff --git a/config/ext.json b/config/ext.json index efd4aad9c..2f076f2bf 100644 --- a/config/ext.json +++ b/config/ext.json @@ -199,6 +199,19 @@ "gmssl" ] }, + "grpc": { + "support": { + "Windows": "wip", + "BSD": "wip" + }, + "type": "external", + "source": "grpc", + "arg-type-unix": "custom", + "cpp-extension": true, + "lib-depends": [ + "grpc" + ] + }, "iconv": { "support": { "BSD": "wip" diff --git a/config/lib.json b/config/lib.json index 94061623e..565a6112b 100644 --- a/config/lib.json +++ b/config/lib.json @@ -139,6 +139,20 @@ "Security" ] }, + "grpc": { + "source": "grpc", + "static-libs-unix": [ + "libgrpc.a", + "libboringssl.a", + "libcares.a" + ], + "lib-depends": [ + "zlib" + ], + "frameworks": [ + "CoreFoundation" + ] + }, "icu": { "source": "icu", "cpp-library": true, diff --git a/config/source.json b/config/source.json index c7ef1e8c1..9c24f3096 100644 --- a/config/source.json +++ b/config/source.json @@ -211,6 +211,16 @@ "path": "LICENSE" } }, + "grpc": { + "type": "git", + "rev": "v1.68.x", + "url": "https://github.com/grpc/grpc.git", + "provide-pre-built": true, + "license": { + "type": "file", + "path": "LICENSE" + } + }, "icu": { "type": "ghrel", "repo": "unicode-org/icu", diff --git a/src/SPC/builder/extension/grpc.php b/src/SPC/builder/extension/grpc.php new file mode 100644 index 000000000..7b4100748 --- /dev/null +++ b/src/SPC/builder/extension/grpc.php @@ -0,0 +1,45 @@ +builder instanceof WindowsBuilder) { + // not support windows yet + throw new \RuntimeException('grpc extension does not support windows yet'); + } + if (!is_link(SOURCE_PATH . '/php-src/ext/grpc')) { + shell()->exec('ln -s ' . $this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc'); + $macos = $this->builder instanceof MacOSBuilder ? "\n" . ' LDFLAGS="$LDFLAGS -framework CoreFoundation"' : ''; + FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/ext/grpc/config.m4', '/GRPC_LIBDIR=.*$/m', 'GRPC_LIBDIR=' . BUILD_LIB_PATH . $macos); + FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/ext/grpc/config.m4', '/SEARCH_PATH=.*$/m', 'SEARCH_PATH="' . BUILD_ROOT_PATH . '"'); + return true; + } + return false; + } + + public function patchBeforeMake(): bool + { + // 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; + } + + public function getUnixConfigureArg(): string + { + return '--enable-grpc=' . BUILD_ROOT_PATH . '/grpc GRPC_LIB_SUBDIR=' . BUILD_LIB_PATH; + } +} diff --git a/src/SPC/builder/linux/library/grpc.php b/src/SPC/builder/linux/library/grpc.php new file mode 100644 index 000000000..ccafc94cb --- /dev/null +++ b/src/SPC/builder/linux/library/grpc.php @@ -0,0 +1,12 @@ +cd($this->source_dir) + ->exec('EXTRA_DEFINES=GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK CXXFLAGS="-L' . BUILD_LIB_PATH . ' -I' . BUILD_INCLUDE_PATH . '" make static -j' . $this->builder->concurrency); + copy($this->source_dir . '/libs/opt/libgrpc.a', BUILD_LIB_PATH . '/libgrpc.a'); + copy($this->source_dir . '/libs/opt/libboringssl.a', BUILD_LIB_PATH . '/libboringssl.a'); + if (!file_exists(BUILD_LIB_PATH . '/libcares.a')) { + copy($this->source_dir . '/libs/opt/libcares.a', BUILD_LIB_PATH . '/libcares.a'); + } + FileSystem::copyDir($this->source_dir . '/include/grpc', BUILD_INCLUDE_PATH . '/grpc'); + FileSystem::copyDir($this->source_dir . '/include/grpc++', BUILD_INCLUDE_PATH . '/grpc++'); + FileSystem::copyDir($this->source_dir . '/include/grpcpp', BUILD_INCLUDE_PATH . '/grpcpp'); + } +} diff --git a/src/globals/ext-tests/grpc.php b/src/globals/ext-tests/grpc.php new file mode 100644 index 000000000..cb0bc27ce --- /dev/null +++ b/src/globals/ext-tests/grpc.php @@ -0,0 +1,5 @@ + '', + 'Linux', 'Darwin' => 'grpc', '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', }; @@ -54,7 +52,7 @@ // You can use `common`, `bulk`, `minimal` or `none`. // note: combination is only available for *nix platform. Windows must use `none` combination $base_combination = match (PHP_OS_FAMILY) { - 'Linux', 'Darwin' => 'bulk', + 'Linux', 'Darwin' => 'minimal', 'Windows' => 'none', };