Skip to content

Commit bad28fa

Browse files
authored
Merge pull request #256 from crazywhalecc/ext/rdkafka
Add extension rdkafka support
2 parents 1bc7bc3 + 985cd67 commit bad28fa

File tree

8 files changed

+149
-6
lines changed

8 files changed

+149
-6
lines changed

config/ext.json

+13
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,19 @@
708708
"zlib"
709709
]
710710
},
711+
"rdkafka": {
712+
"support": {
713+
"BSD": "wip",
714+
"Windows": "wip"
715+
},
716+
"type": "external",
717+
"source": "ext-rdkafka",
718+
"arg-type": "custom",
719+
"cpp-extension": true,
720+
"lib-depends": [
721+
"librdkafka"
722+
]
723+
},
711724
"swoole": {
712725
"support": {
713726
"Windows": "no",

config/lib.json

+12
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,18 @@
558558
"pkg-config": {
559559
"source": "pkg-config"
560560
},
561+
"librdkafka": {
562+
"source": "librdkafka",
563+
"static-libs-unix": [
564+
"librdkafka.a",
565+
"librdkafka++.a",
566+
"librdkafka-static.a"
567+
],
568+
"cpp-library": true,
569+
"lib-suggests": [
570+
"zstd"
571+
]
572+
},
561573
"postgresql": {
562574
"source": "postgresql",
563575
"static-libs-unix": [

config/source.json

+17
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@
6969
"path": "LICENSE"
7070
}
7171
},
72+
"librdkafka": {
73+
"type": "ghtar",
74+
"repo": "confluentinc/librdkafka",
75+
"license": {
76+
"type": "file",
77+
"path": "LICENSE"
78+
}
79+
},
80+
"ext-rdkafka": {
81+
"type": "ghtar",
82+
"repo": "arnaud-lb/php-rdkafka",
83+
"path": "php-src/ext/rdkafka",
84+
"license": {
85+
"type": "file",
86+
"path": "LICENSE"
87+
}
88+
},
7289
"ext-event": {
7390
"type": "url",
7491
"url": "https://bitbucket.org/osmanov/pecl-event/get/3.0.8.tar.gz",

src/SPC/builder/extension/rdkafka.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\extension;
6+
7+
use SPC\builder\Extension;
8+
use SPC\store\FileSystem;
9+
use SPC\util\CustomExt;
10+
11+
#[CustomExt('rdkafka')]
12+
class rdkafka extends Extension
13+
{
14+
public function patchBeforeMake(): bool
15+
{
16+
// when compiling rdkafka with inline builds, it shows some errors, I don't know why.
17+
FileSystem::replaceFileStr(
18+
SOURCE_PATH . '/php-src/ext/rdkafka/rdkafka.c',
19+
"#ifdef HAS_RD_KAFKA_TRANSACTIONS\n#include \"kafka_error_exception.h\"\n#endif",
20+
'#include "kafka_error_exception.h"'
21+
);
22+
FileSystem::replaceFileStr(
23+
SOURCE_PATH . '/php-src/ext/rdkafka/kafka_error_exception.h',
24+
['#ifdef HAS_RD_KAFKA_TRANSACTIONS', '#endif'],
25+
''
26+
);
27+
return true;
28+
}
29+
30+
public function getConfigureArg(): string
31+
{
32+
$pkgconf_libs = shell()->execWithResult('pkg-config --libs --static rdkafka')[1];
33+
$pkgconf_libs = trim(implode('', $pkgconf_libs));
34+
return '--with-rdkafka=' . BUILD_ROOT_PATH . ' LIBS="' . $pkgconf_libs . '"';
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\linux\library;
6+
7+
class librdkafka extends LinuxLibraryBase
8+
{
9+
// TODO: Linux is buggy, see https://github.com/confluentinc/librdkafka/discussions/4495
10+
use \SPC\builder\unix\library\librdkafka;
11+
12+
public const NAME = 'librdkafka';
13+
}
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 librdkafka extends MacOSLibraryBase
8+
{
9+
use \SPC\builder\unix\library\librdkafka;
10+
11+
public const NAME = 'librdkafka';
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\unix\library;
6+
7+
use SPC\exception\FileSystemException;
8+
use SPC\exception\RuntimeException;
9+
10+
trait librdkafka
11+
{
12+
/**
13+
* @throws FileSystemException
14+
* @throws RuntimeException
15+
*/
16+
protected function build(): void
17+
{
18+
$builddir = BUILD_ROOT_PATH;
19+
20+
$zstd_option = $this->builder->getLib('zstd') ? ("STATIC_LIB_libzstd={$builddir}/lib/libzstd.a ") : '';
21+
shell()->cd($this->source_dir)
22+
->exec(
23+
$zstd_option .
24+
'./configure ' .
25+
'--enable-static --disable-shared --disable-curl --disable-sasl --disable-valgrind --disable-zlib --disable-ssl ' .
26+
($zstd_option == '' ? '--disable-zstd ' : '') .
27+
'--prefix='
28+
)
29+
->exec('make clean')
30+
->exec("make -j{$this->builder->concurrency}")
31+
->exec("make install DESTDIR={$builddir}");
32+
$this->patchPkgconfPrefix(['rdkafka.pc', 'rdkafka-static.pc', 'rdkafka++.pc', 'rdkafka++-static.pc']);
33+
// remove dynamic libs
34+
shell()
35+
->exec("rm -rf {$builddir}/lib/*.so.*")
36+
->exec("rm -rf {$builddir}/lib/*.so")
37+
->exec("rm -rf {$builddir}/lib/*.dylib");
38+
}
39+
}

src/globals/test-extensions.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121

2222
// test os (macos-13, macos-14, ubuntu-latest, windows-latest are available)
2323
$test_os = [
24-
'macos-14',
25-
'ubuntu-latest',
24+
// 'macos-14',
25+
'macos-13',
26+
// 'ubuntu-latest',
2627
];
2728

2829
// whether enable thread safe
29-
$zts = true;
30+
$zts = false;
3031

3132
$no_strip = false;
3233

@@ -38,8 +39,8 @@
3839

3940
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
4041
$extensions = match (PHP_OS_FAMILY) {
41-
'Linux', 'Darwin' => 'apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,ftp,gd,gmp,gettext,iconv,igbinary,imagick,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,parallel,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,protobuf,readline,redis,session,shmop,simplexml,soap,sockets,sodium,sqlite3,ssh2,sysvmsg,sysvsem,sysvshm,tidy,tokenizer,xlswriter,xml,xmlreader,xmlwriter,zip,zlib,yaml,zstd',
42-
'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',
42+
'Linux', 'Darwin' => 'rdkafka',
43+
'Windows' => 'zlib',
4344
};
4445

4546
// If you want to test lib-suggests feature with extension, add them below (comma separated, example `libwebp,libavif`).
@@ -52,7 +53,7 @@
5253
// You can use `common`, `bulk`, `minimal` or `none`.
5354
// note: combination is only available for *nix platform. Windows must use `none` combination
5455
$base_combination = match (PHP_OS_FAMILY) {
55-
'Linux', 'Darwin' => 'none',
56+
'Linux', 'Darwin' => 'bulk',
5657
'Windows' => 'none',
5758
};
5859

0 commit comments

Comments
 (0)