Skip to content

Commit c0b52fc

Browse files
committed
Add extension rdkafka support
1 parent ac7a236 commit c0b52fc

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

config/ext.json

+9
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@
385385
"libssh2"
386386
]
387387
},
388+
"rdkafka": {
389+
"type": "external",
390+
"source": "ext-rdkafka",
391+
"arg-type": "custom",
392+
"cpp-extension": true,
393+
"lib-depends": [
394+
"librdkafka"
395+
]
396+
},
388397
"swoole": {
389398
"type": "external",
390399
"source": "swoole",

config/lib.json

+12
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,18 @@
420420
"pkg-config": {
421421
"source": "pkg-config"
422422
},
423+
"librdkafka": {
424+
"source": "librdkafka",
425+
"static-libs-unix": [
426+
"librdkafka.a",
427+
"librdkafka++.a",
428+
"librdkafka-static.a"
429+
],
430+
"lib-depends": [
431+
"openssl",
432+
"zlib"
433+
]
434+
},
423435
"postgresql": {
424436
"source": "postgresql",
425437
"static-libs-unix": [

config/source.json

+17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@
4242
"path": "COPYING"
4343
}
4444
},
45+
"librdkafka": {
46+
"type": "ghtar",
47+
"repo": "confluentinc/librdkafka",
48+
"license": {
49+
"type": "file",
50+
"path": "LICENSE"
51+
}
52+
},
53+
"ext-rdkafka": {
54+
"type": "ghtar",
55+
"repo": "arnaud-lb/php-rdkafka",
56+
"path": "php-src/ext/rdkafka",
57+
"license": {
58+
"type": "file",
59+
"path": "LICENSE"
60+
}
61+
},
4562
"ext-event": {
4663
"type": "url",
4764
"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,35 @@
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+
shell()->cd($this->source_dir)
20+
->exec(
21+
'./configure ' .
22+
'--enable-static --disable-shared --disable-curl --disable-sasl --disable-valgrind ' .
23+
'--prefix='
24+
)
25+
->exec('make clean')
26+
->exec("make -j{$this->builder->concurrency}")
27+
->exec('make install DESTDIR=' . BUILD_ROOT_PATH);
28+
$this->patchPkgconfPrefix(['rdkafka.pc', 'rdkafka-static.pc', 'rdkafka++.pc', 'rdkafka++-static.pc']);
29+
// remove dynamic libs
30+
shell()
31+
->exec("rm -rf {$builddir}/lib/*.so.*")
32+
->exec("rm -rf {$builddir}/lib/*.so")
33+
->exec("rm -rf {$builddir}/lib/*.dylib");
34+
}
35+
}

0 commit comments

Comments
 (0)