Compare commits

...

11 Commits

Author SHA1 Message Date
DubbleClick
d66d7b3a73 should be fixed properly 2025-07-21 16:56:39 +07:00
DubbleClick
024542d361 oh, it will likely be this instead 2025-07-21 16:03:28 +07:00
DubbleClick
7d85aacace for some reason alpine docker fails in CI, but not locally, after I added this? 2025-07-21 16:00:28 +07:00
DubbleClick
31b2e6779d cs fix 2025-07-21 14:29:27 +07:00
DubbleClick
2b39a6d535 -static-libstdc++ and -static-libgcc for static builds (musl toolchain) 2025-07-21 14:29:01 +07:00
DubbleClick
e490a1763a use pkgconfig to determine libs fix cs 2025-07-21 12:12:18 +07:00
DubbleClick
bf4b35aa0a use pkgconfig to determine libs 2025-07-21 12:10:14 +07:00
DubbleClick
1f7d3ec91d use =package 2025-07-21 10:55:21 +07:00
DubbleClick
23412d6bfa enable tests 2025-07-21 09:46:06 +07:00
DubbleClick
bd0042f079 grpc can be built shared too 2025-07-21 09:43:01 +07:00
DubbleClick
0ccf1a1c5d remove grpc_php_ext_src 2025-07-21 09:41:36 +07:00
7 changed files with 92 additions and 36 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setUnsupportedPhpVersionAllowed(true)
->setRules([
'@PSR12' => true,
'@Symfony' => true,

View File

@@ -24,8 +24,6 @@ class grpc extends Extension
if (!is_link(SOURCE_PATH . '/php-src/ext/grpc')) {
if (is_dir($this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc')) {
shell()->exec('ln -s ' . $this->builder->getLib('grpc')->getSourceDir() . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
} elseif (is_dir(BUILD_ROOT_PATH . '/grpc_php_ext_src')) {
shell()->exec('ln -s ' . BUILD_ROOT_PATH . '/grpc_php_ext_src ' . SOURCE_PATH . '/php-src/ext/grpc');
} else {
throw new \RuntimeException('Cannot find grpc source code');
}
@@ -36,6 +34,7 @@ class grpc extends Extension
}
return false;
}
public function patchBeforeConfigure(): bool
{
$libs = ' -l' . join(' -l', $this->getLibraries());
@@ -57,13 +56,12 @@ class grpc extends Extension
public function getUnixConfigureArg(bool $shared = false): string
{
return '--enable-grpc=' . BUILD_ROOT_PATH . '/grpc GRPC_LIB_SUBDIR=' . BUILD_LIB_PATH;
return '--enable-grpc=' . ($shared ? 'shared,' : '') . BUILD_ROOT_PATH . '/grpc GRPC_LIB_SUBDIR=' . BUILD_LIB_PATH;
}
private function getLibraries(): array
{
[, $out] = shell()->execWithResult('$PKG_CONFIG --libs --static grpc');
$libs = join(' ', $out) . ' -lupb -lupb_message_lib -lupb_json_lib -lupb_textformat_lib -lupb_mini_descriptor_lib -lupb_wire_lib -lupb_mem_lib -lupb_base_lib -lutf8_range';
$libs = shell()->execWithResult('$PKG_CONFIG --libs --static grpc')[1][0];
$filtered = str_replace('-pthread', '', $libs);
$filtered = preg_replace('/-L\S+/', '', $filtered);
$filtered = preg_replace('/(?:\S*\/)?lib([a-zA-Z0-9_+-]+)\.a\b/', '-l$1', $filtered);

View File

@@ -4,35 +4,55 @@ declare(strict_types=1);
namespace SPC\builder\unix\library;
use SPC\builder\linux\SystemUtil;
use SPC\store\FileSystem;
use SPC\util\executor\UnixCMakeExecutor;
use SPC\util\SPCTarget;
trait grpc
{
public function patchBeforeBuild(): bool
{
FileSystem::replaceFileStr(
$this->source_dir . '/third_party/re2/util/pcre.h',
["#define UTIL_PCRE_H_\n#include <stdint.h>", '#define UTIL_PCRE_H_'],
['#define UTIL_PCRE_H_', "#define UTIL_PCRE_H_\n#include <stdint.h>"],
);
return true;
}
protected function build(): void
{
UnixCMakeExecutor::create($this)
$cmake = UnixCMakeExecutor::create($this)
->addConfigureArgs(
'-DgRPC_SSL_PROVIDER=openssl',
'-DgRPC_SSL_PROVIDER=package',
'-DgRPC_INSTALL_BINDIR=' . BUILD_BIN_PATH,
'-DgRPC_INSTALL_LIBDIR=' . BUILD_LIB_PATH,
'-DgRPC_INSTALL_SHAREDIR=' . BUILD_ROOT_PATH . '/share/grpc',
'-DOPENSSL_ROOT_DIR=' . BUILD_ROOT_PATH,
'-DOPENSSL_INCLUDE_DIR=' . BUILD_INCLUDE_PATH,
'-DCMAKE_CXX_FLAGS="-DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK"'
)
->build();
'-DCMAKE_C_FLAGS="-DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK -L' . BUILD_LIB_PATH . ' -I' . BUILD_INCLUDE_PATH . '"',
'-DCMAKE_CXX_FLAGS="-DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK -L' . BUILD_LIB_PATH . ' -I' . BUILD_INCLUDE_PATH . '"'
);
if (PHP_OS_FAMILY === 'Linux' && SPCTarget::isStatic() && !SystemUtil::isMuslDist()) {
$cmake->addConfigureArgs(
'-DCMAKE_EXE_LINKER_FLAGS="-static-libgcc -static-libstdc++"',
'-DCMAKE_SHARED_LINKER_FLAGS="-static-libgcc -static-libstdc++"',
'-DCMAKE_CXX_STANDARD_LIBRARIES="-static-libgcc -static-libstdc++"',
);
}
$cmake->build();
copy($this->source_dir . '/third_party/re2/re2.pc', BUILD_LIB_PATH . '/pkgconfig/re2.pc');
//shell()->cd($this->source_dir)
// shell()->cd($this->source_dir)
// ->exec('EXTRA_DEFINES=GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK EMBED_OPENSSL=false 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/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');
//FileSystem::copyDir($this->source_dir . '/src/php/ext/grpc', BUILD_ROOT_PATH . '/grpc_php_ext_src');
// }
// 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');
}
}

View File

@@ -94,6 +94,9 @@ class SourceManager
// if not, remove the source dir and extract again
logger()->notice("Source [{$source}] hash mismatch, removing old source dir and extracting again ...");
if ($source === 'micro' || $source === 'grpc') {
return;
}
FileSystem::removeDir($check);
$filename = LockFile::getLockFullPath($lock_content);
$move_path = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $source);

View File

@@ -106,10 +106,33 @@ class SPCConfigUtil
foreach (array_reverse($libraries) as $library) {
$libs = Config::getLib($library, 'static-libs', []);
foreach ($libs as $lib) {
if ($withDependencies) {
$noExt = str_replace('.a', '', $lib);
$noExtNoLib = str_replace('lib', '', $noExt);
$pkgconfFileNoExt = BUILD_LIB_PATH . "/pkgconfig/{$noExt}.pc";
$pkgconfFileNoExtNoLib = BUILD_LIB_PATH . "/pkgconfig/{$noExtNoLib}.pc";
$llibs = null;
if (file_exists($pkgconfFileNoExt)) {
$llibs = shell()->execWithResult("pkg-config --libs --static {$noExt}")[1][0];
} elseif (file_exists($pkgconfFileNoExtNoLib)) {
$llibs = shell()->execWithResult("pkg-config --libs --static {$noExtNoLib}")[1][0];
}
if (!empty($llibs)) {
$filtered = str_replace('-pthread', '', $llibs);
$filtered = preg_replace('/-L\S+/', '', $filtered);
$filtered = preg_replace('/(?:\S*\/)?lib([a-zA-Z0-9_+-]+)\.a\b/', '-l$1', $filtered);
$filtered = preg_replace('/\s+/', ' ', $filtered);
foreach (explode(' ', $filtered) as $item) {
$short_name[] = $item;
}
} elseif ($withDependencies) {
$noExt = str_replace('.a', '', $lib);
$requiredLibs = [];
$pkgconfFile = BUILD_LIB_PATH . "/pkgconfig/{$noExt}.pc";
if (!file_exists($pkgconfFile)) {
$noExtNoLib = str_replace('lib', '', $noExt);
$pkgconfFile = BUILD_LIB_PATH . "/pkgconfig/{$noExtNoLib}.pc";
}
if (file_exists($pkgconfFile)) {
$lines = file($pkgconfFile);
foreach ($lines as $value) {

View File

@@ -24,6 +24,8 @@ class UnixCMakeExecutor extends Executor
protected bool $reset = true;
protected array $extra_env = [];
public function build(string $build_pos = '..'): void
{
// set cmake dir
@@ -34,7 +36,7 @@ class UnixCMakeExecutor extends Executor
}
// prepare shell
$shell = shell()->cd($this->build_dir)->initializeEnv($this->library);
$shell = shell()->cd($this->build_dir)->initializeEnv($this->library)->appendEnv($this->extra_env);
// config
$this->steps >= 1 && $shell->exec("cmake {$this->getConfigureArgs()} {$this->getDefaultCMakeArgs()} {$build_pos}");
@@ -77,6 +79,15 @@ class UnixCMakeExecutor extends Executor
return $this;
}
/**
* Add extra environment flags
*/
public function addExtraEnv(array $env): static
{
$this->extra_env = [...$this->extra_env, ...$env];
return $this;
}
/**
* To build steps.
*

View File

@@ -13,23 +13,23 @@ declare(strict_types=1);
// test php version (8.1 ~ 8.4 available, multiple for matrix)
$test_php_version = [
'8.1',
'8.2',
'8.3',
// '8.1',
// '8.2',
// '8.3',
'8.4',
];
// test os (macos-13, macos-14, macos-15, ubuntu-latest, windows-latest are available)
$test_os = [
// 'macos-13', // bin/spc for x86_64
// 'macos-14', // bin/spc for arm64
// 'macos-15', // bin/spc for arm64
// 'ubuntu-latest', // bin/spc-alpine-docker for x86_64
// 'ubuntu-22.04', // bin/spc-gnu-docker for x86_64
// 'ubuntu-24.04', // bin/spc for x86_64
// 'ubuntu-22.04-arm', // bin/spc-gnu-docker for arm64
// 'ubuntu-24.04-arm', // bin/spc for arm64
'windows-latest', // .\bin\spc.ps1
'macos-13', // bin/spc for x86_64
'macos-14', // bin/spc for arm64
'macos-15', // bin/spc for arm64
'ubuntu-latest', // bin/spc-alpine-docker for x86_64
'ubuntu-22.04', // bin/spc-gnu-docker for x86_64
'ubuntu-24.04', // bin/spc for x86_64
'ubuntu-22.04-arm', // bin/spc-gnu-docker for arm64
'ubuntu-24.04-arm', // bin/spc for arm64
// 'windows-latest', // .\bin\spc.ps1
];
// whether enable thread safe
@@ -48,7 +48,7 @@ $prefer_pre_built = false;
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
$extensions = match (PHP_OS_FAMILY) {
'Linux', 'Darwin' => 'imap,swoole',
'Linux', 'Darwin' => 'grpc',
'Windows' => 'curl',
};