Skip to content

Commit

Permalink
Add extension gmssl support (#544)
Browse files Browse the repository at this point in the history
* Add extension gmssl support

* cs-fix

* Add framework for gmssl
  • Loading branch information
crazywhalecc authored Sep 20, 2024
1 parent e358369 commit 29efc2c
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 2 deletions.
10 changes: 10 additions & 0 deletions config/ext.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@
"gmp"
]
},
"gmssl": {
"support": {
"BSD": "wip"
},
"type": "external",
"source": "ext-gmssl",
"lib-depends": [
"gmssl"
]
},
"iconv": {
"support": {
"BSD": "wip"
Expand Down
12 changes: 12 additions & 0 deletions config/lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@
"gmp.h"
]
},
"gmssl": {
"source": "gmssl",
"static-libs-unix": [
"libgmssl.a"
],
"static-libs-windows": [
"gmssl.lib"
],
"frameworks": [
"Security"
]
},
"icu": {
"source": "icu",
"cpp-library": true,
Expand Down
17 changes: 17 additions & 0 deletions config/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
"path": "LICENSE"
}
},
"ext-gmssl": {
"type": "ghtar",
"repo": "gmssl/GmSSL-PHP",
"path": "php-src/ext/gmssl",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"ext-imagick": {
"type": "url",
"url": "https://pecl.php.net/get/imagick",
Expand Down Expand Up @@ -194,6 +203,14 @@
"text": "Since version 6, GMP is distributed under the dual licenses, GNU LGPL v3 and GNU GPL v2. These licenses make the library free to use, share, and improve, and allow you to pass on the result. The GNU licenses give freedoms, but also set firm restrictions on the use with non-free programs."
}
},
"gmssl": {
"type": "ghtar",
"repo": "guanzhi/GmSSL",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"icu": {
"type": "ghrel",
"repo": "unicode-org/icu",
Expand Down
22 changes: 22 additions & 0 deletions src/SPC/builder/extension/gmssl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace SPC\builder\extension;

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

#[CustomExt('gmssl')]
class gmssl extends Extension
{
public function patchBeforeBuildconf(): bool
{
if (str_contains(file_get_contents(SOURCE_PATH . '/php-src/ext/gmssl/config.w32'), 'CHECK_LIB(')) {
return false;
}
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/ext/gmssl/config.w32', 'AC_DEFINE(', 'CHECK_LIB("gmssl.lib", "gmssl", PHP_GMSSL);' . PHP_EOL . 'AC_DEFINE(');
return true;
}
}
12 changes: 12 additions & 0 deletions src/SPC/builder/linux/library/gmssl.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 gmssl extends LinuxLibraryBase
{
use \SPC\builder\unix\library\gmssl;

public const NAME = 'gmssl';
}
12 changes: 12 additions & 0 deletions src/SPC/builder/macos/library/gmssl.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 gmssl extends MacOSLibraryBase
{
use \SPC\builder\unix\library\gmssl;

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

declare(strict_types=1);

namespace SPC\builder\unix\library;

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

trait gmssl
{
/**
* @throws FileSystemException
* @throws RuntimeException
*/
protected function build(): void
{
// CMake needs a clean build directory
FileSystem::resetDir($this->source_dir . '/build');
// Start build
shell()->cd($this->source_dir . '/build')
->setEnv(['CFLAGS' => $this->getLibExtraCFlags(), 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
->execWithEnv("cmake {$this->builder->makeCmakeArgs()} -DBUILD_SHARED_LIBS=OFF ..")
->execWithEnv("cmake --build . -j {$this->builder->concurrency}")
->execWithEnv('make install DESTDIR=' . BUILD_ROOT_PATH);
}
}
33 changes: 33 additions & 0 deletions src/SPC/builder/windows/library/gmssl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace SPC\builder\windows\library;

use SPC\store\FileSystem;

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

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

// start build
cmd()->cd($this->source_dir . '\builddir')
->execWithWrapper(
$this->builder->makeSimpleWrapper('cmake .. -G "NMake Makefiles" -DWIN32=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="/MT /O2 /Ob2 /DNDEBUG" -DCMAKE_CXX_FLAGS_RELEASE="/MT /O2 /Ob2 /DNDEBUG" -DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH),
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH
);

FileSystem::writeFile($this->source_dir . '\builddir\cmake_install.cmake', 'set(CMAKE_INSTALL_PREFIX "' . str_replace('\\', '/', BUILD_ROOT_PATH) . '")' . PHP_EOL . FileSystem::readFile($this->source_dir . '\builddir\cmake_install.cmake'));

cmd()->cd($this->source_dir . '\builddir')
->execWithWrapper(
$this->builder->makeSimpleWrapper('nmake'),
'install XCFLAGS=/MT'
);
}
}
8 changes: 8 additions & 0 deletions src/globals/ext-tests/gmssl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

assert(function_exists('gmssl_rand_bytes'));
assert(function_exists('gmssl_sm3'));

assert(bin2hex(gmssl_sm3('123456')) === '207cf410532f92a47dee245ce9b11ff71f578ebd763eb3bbea44ebd043d018fb');
4 changes: 2 additions & 2 deletions src/globals/test-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,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' => 'redis,igbinary,msgpack',
'Windows' => 'redis,igbinary,msgpack',
'Linux', 'Darwin' => 'gmssl',
'Windows' => 'gmssl',
};

// If you want to test lib-suggests feature with extension, add them below (comma separated, example `libwebp,libavif`).
Expand Down

0 comments on commit 29efc2c

Please sign in to comment.