Skip to content

Commit

Permalink
Add amqp/librabbitmq support for linux, macos, windows (#366)
Browse files Browse the repository at this point in the history
* add amqp/librabbitmq support for linux, macos, windows

* add test for amqp
  • Loading branch information
crazywhalecc authored Mar 4, 2024
1 parent d4c0290 commit 96dd5ba
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 3 deletions.
11 changes: 11 additions & 0 deletions config/ext.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{
"amqp": {
"type": "external",
"arg-type": "custom",
"source": "amqp",
"lib-depends": [
"librabbitmq"
],
"ext-depends-windows": [
"openssl"
]
},
"apcu": {
"type": "external",
"source": "apcu"
Expand Down
12 changes: 12 additions & 0 deletions config/lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@
"zlib"
]
},
"librabbitmq": {
"source": "librabbitmq",
"static-libs-unix": [
"librabbitmq.a"
],
"static-libs-windows": [
"rabbitmq.4.lib"
],
"lib-depends": [
"openssl"
]
},
"libsodium": {
"source": "libsodium",
"static-libs-unix": [
Expand Down
19 changes: 19 additions & 0 deletions config/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"path": "LICENSE"
}
},
"amqp": {
"type": "url",
"url": "https://pecl.php.net/get/amqp",
"path": "php-src/ext/amqp",
"filename": "amqp.tgz",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"apcu": {
"type": "url",
"url": "https://pecl.php.net/get/APCu",
Expand Down Expand Up @@ -317,6 +327,15 @@
"path": "LICENSE"
}
},
"librabbitmq": {
"type": "git",
"url": "https://github.com/alanxz/rabbitmq-c.git",
"rev": "master",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"libsodium": {
"type": "ghrel",
"repo": "jedisct1/libsodium",
Expand Down
35 changes: 35 additions & 0 deletions src/SPC/builder/extension/amqp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SPC\builder\extension;

use SPC\builder\Extension;
use SPC\store\FileSystem;
use SPC\util\CustomExt;

#[CustomExt('amqp')]
class amqp extends Extension
{
public function patchBeforeMake(): bool
{
if (PHP_OS_FAMILY === 'Windows') {
FileSystem::replaceFileRegex(BUILD_INCLUDE_PATH . '\amqp.h', '/^#warning.*/m', '');
FileSystem::replaceFileRegex(BUILD_INCLUDE_PATH . '\amqp_framing.h', '/^#warning.*/m', '');
FileSystem::replaceFileRegex(BUILD_INCLUDE_PATH . '\amqp_ssl_socket.h', '/^#warning.*/m', '');
FileSystem::replaceFileRegex(BUILD_INCLUDE_PATH . '\amqp_tcp_socket.h', '/^#warning.*/m', '');
return true;
}
return false;
}

public function getUnixConfigureArg(): string
{
return '--with-amqp --with-librabbitmq-dir=' . BUILD_ROOT_PATH;
}

public function getWindowsConfigureArg(): string
{
return '--with-amqp';
}
}
12 changes: 12 additions & 0 deletions src/SPC/builder/linux/library/librabbitmq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace SPC\builder\linux\library;

class librabbitmq extends LinuxLibraryBase
{
use \SPC\builder\unix\library\librabbitmq;

public const NAME = 'librabbitmq';
}
12 changes: 12 additions & 0 deletions src/SPC/builder/macos/library/librabbitmq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace SPC\builder\macos\library;

class librabbitmq extends MacOSLibraryBase
{
use \SPC\builder\unix\library\librabbitmq;

public const NAME = 'librabbitmq';
}
35 changes: 35 additions & 0 deletions src/SPC/builder/unix/library/librabbitmq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SPC\builder\unix\library;

use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;

trait librabbitmq
{
/**
* @throws RuntimeException
* @throws FileSystemException
*/
protected function build(): void
{
// CMake needs a clean build directory
FileSystem::resetDir($this->source_dir . '/build');
// Start build
shell()->cd($this->source_dir . '/build')
->exec(
'cmake ' .
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_STATIC_LIBS=ON ' .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency}")
->exec('make install');
}
}
36 changes: 36 additions & 0 deletions src/SPC/builder/windows/library/librabbitmq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace SPC\builder\windows\library;

use SPC\store\FileSystem;

class librabbitmq extends WindowsLibraryBase
{
public const NAME = 'librabbitmq';

protected function build(): void
{
// reset cmake
FileSystem::resetDir($this->source_dir . '\build');

// start build
cmd()->cd($this->source_dir)
->execWithWrapper(
$this->builder->makeSimpleWrapper('cmake'),
'-B build ' .
'-A x64 ' .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_STATIC_LIBS=ON ' .
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' '
)
->execWithWrapper(
$this->builder->makeSimpleWrapper('cmake'),
"--build build --config Release --target install -j{$this->builder->concurrency}"
);
rename(BUILD_LIB_PATH . '\librabbitmq.4.lib', BUILD_LIB_PATH . '\rabbitmq.4.lib');
}
}
6 changes: 3 additions & 3 deletions src/globals/test-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
$extensions = match (PHP_OS_FAMILY) {
'Linux', 'Darwin' => 'uuid',
'Windows' => 'mbstring,pdo_sqlite,mbregex,ffi',
'Linux', 'Darwin' => 'amqp',
'Windows' => 'mbstring,pdo_sqlite,mbregex,ffi,amqp',
};

// If you want to test lib-suggests feature with extension, add them below (comma separated, example `libwebp,libavif`).
Expand All @@ -27,7 +27,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' => 'minimal',
'Linux', 'Darwin' => 'common',
'Windows' => 'none',
};

Expand Down

0 comments on commit 96dd5ba

Please sign in to comment.