Skip to content

Commit 29efc2c

Browse files
authored
Add extension gmssl support (#544)
* Add extension gmssl support * cs-fix * Add framework for gmssl
1 parent e358369 commit 29efc2c

File tree

10 files changed

+156
-2
lines changed

10 files changed

+156
-2
lines changed

config/ext.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@
189189
"gmp"
190190
]
191191
},
192+
"gmssl": {
193+
"support": {
194+
"BSD": "wip"
195+
},
196+
"type": "external",
197+
"source": "ext-gmssl",
198+
"lib-depends": [
199+
"gmssl"
200+
]
201+
},
192202
"iconv": {
193203
"support": {
194204
"BSD": "wip"

config/lib.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@
127127
"gmp.h"
128128
]
129129
},
130+
"gmssl": {
131+
"source": "gmssl",
132+
"static-libs-unix": [
133+
"libgmssl.a"
134+
],
135+
"static-libs-windows": [
136+
"gmssl.lib"
137+
],
138+
"frameworks": [
139+
"Security"
140+
]
141+
},
130142
"icu": {
131143
"source": "icu",
132144
"cpp-library": true,

config/source.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@
8383
"path": "LICENSE"
8484
}
8585
},
86+
"ext-gmssl": {
87+
"type": "ghtar",
88+
"repo": "gmssl/GmSSL-PHP",
89+
"path": "php-src/ext/gmssl",
90+
"license": {
91+
"type": "file",
92+
"path": "LICENSE"
93+
}
94+
},
8695
"ext-imagick": {
8796
"type": "url",
8897
"url": "https://pecl.php.net/get/imagick",
@@ -194,6 +203,14 @@
194203
"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."
195204
}
196205
},
206+
"gmssl": {
207+
"type": "ghtar",
208+
"repo": "guanzhi/GmSSL",
209+
"license": {
210+
"type": "file",
211+
"path": "LICENSE"
212+
}
213+
},
197214
"icu": {
198215
"type": "ghrel",
199216
"repo": "unicode-org/icu",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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('gmssl')]
12+
class gmssl extends Extension
13+
{
14+
public function patchBeforeBuildconf(): bool
15+
{
16+
if (str_contains(file_get_contents(SOURCE_PATH . '/php-src/ext/gmssl/config.w32'), 'CHECK_LIB(')) {
17+
return false;
18+
}
19+
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/ext/gmssl/config.w32', 'AC_DEFINE(', 'CHECK_LIB("gmssl.lib", "gmssl", PHP_GMSSL);' . PHP_EOL . 'AC_DEFINE(');
20+
return true;
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
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\linux\library;
6+
7+
class gmssl extends LinuxLibraryBase
8+
{
9+
use \SPC\builder\unix\library\gmssl;
10+
11+
public const NAME = 'gmssl';
12+
}
Lines changed: 12 additions & 0 deletions
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 gmssl extends MacOSLibraryBase
8+
{
9+
use \SPC\builder\unix\library\gmssl;
10+
11+
public const NAME = 'gmssl';
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
use SPC\store\FileSystem;
10+
11+
trait gmssl
12+
{
13+
/**
14+
* @throws FileSystemException
15+
* @throws RuntimeException
16+
*/
17+
protected function build(): void
18+
{
19+
// CMake needs a clean build directory
20+
FileSystem::resetDir($this->source_dir . '/build');
21+
// Start build
22+
shell()->cd($this->source_dir . '/build')
23+
->setEnv(['CFLAGS' => $this->getLibExtraCFlags(), 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
24+
->execWithEnv("cmake {$this->builder->makeCmakeArgs()} -DBUILD_SHARED_LIBS=OFF ..")
25+
->execWithEnv("cmake --build . -j {$this->builder->concurrency}")
26+
->execWithEnv('make install DESTDIR=' . BUILD_ROOT_PATH);
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\windows\library;
6+
7+
use SPC\store\FileSystem;
8+
9+
class gmssl extends WindowsLibraryBase
10+
{
11+
public const NAME = 'gmssl';
12+
13+
protected function build(): void
14+
{
15+
// reset cmake
16+
FileSystem::resetDir($this->source_dir . '\builddir');
17+
18+
// start build
19+
cmd()->cd($this->source_dir . '\builddir')
20+
->execWithWrapper(
21+
$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),
22+
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH
23+
);
24+
25+
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'));
26+
27+
cmd()->cd($this->source_dir . '\builddir')
28+
->execWithWrapper(
29+
$this->builder->makeSimpleWrapper('nmake'),
30+
'install XCFLAGS=/MT'
31+
);
32+
}
33+
}

src/globals/ext-tests/gmssl.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
assert(function_exists('gmssl_rand_bytes'));
6+
assert(function_exists('gmssl_sm3'));
7+
8+
assert(bin2hex(gmssl_sm3('123456')) === '207cf410532f92a47dee245ce9b11ff71f578ebd763eb3bbea44ebd043d018fb');

src/globals/test-extensions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
2121
$extensions = match (PHP_OS_FAMILY) {
22-
'Linux', 'Darwin' => 'redis,igbinary,msgpack',
23-
'Windows' => 'redis,igbinary,msgpack',
22+
'Linux', 'Darwin' => 'gmssl',
23+
'Windows' => 'gmssl',
2424
};
2525

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

0 commit comments

Comments
 (0)