Add sodium extension support for Windows (#651)

* Add sodium extension support for Windows

* cs-fix
This commit is contained in:
Jerry Ma 2025-03-15 01:29:44 +08:00 committed by GitHub
parent 99be7b078b
commit a2476690c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 10 deletions

View File

@ -674,7 +674,6 @@
}, },
"sodium": { "sodium": {
"support": { "support": {
"Windows": "wip",
"BSD": "wip" "BSD": "wip"
}, },
"type": "builtin", "type": "builtin",

View File

@ -442,6 +442,9 @@
"source": "libsodium", "source": "libsodium",
"static-libs-unix": [ "static-libs-unix": [
"libsodium.a" "libsodium.a"
],
"static-libs-windows": [
"libsodium.lib"
] ]
}, },
"libssh2": { "libssh2": {

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace SPC\builder\windows\library;
use SPC\builder\windows\SystemUtil;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;
class libsodium extends WindowsLibraryBase
{
public const NAME = 'libsodium';
protected function build()
{
FileSystem::replaceFileStr($this->source_dir . '\src\libsodium\include\sodium\export.h', '#ifdef SODIUM_STATIC', '#if 1');
$vs_ver_dir = match (SystemUtil::findVisualStudio()['version']) {
'vs17' => '\vs2022',
'vs16' => '\vs2019',
default => throw new RuntimeException('Current VS version is not supported yet!'),
};
// start build
cmd()->cd($this->source_dir . '\builds\msvc\\' . $vs_ver_dir)
->execWithWrapper(
$this->builder->makeSimpleWrapper('msbuild'),
'libsodium.sln /t:Rebuild /p:Configuration=StaticRelease /p:Platform=x64 /p:PreprocessorDefinitions="SODIUM_STATIC=1"'
);
FileSystem::createDir(BUILD_LIB_PATH);
FileSystem::createDir(BUILD_INCLUDE_PATH);
// copy include
FileSystem::copyDir($this->source_dir . '\src\libsodium\include\sodium', BUILD_INCLUDE_PATH . '\sodium');
copy($this->source_dir . '\src\libsodium\include\sodium.h', BUILD_INCLUDE_PATH . '\sodium.h');
// copy lib
$ls = FileSystem::scanDirFiles($this->source_dir . '\bin');
$find = false;
foreach ($ls as $file) {
if (str_ends_with($file, 'libsodium.lib')) {
copy($file, BUILD_LIB_PATH . '\libsodium.lib');
$find = true;
}
if (str_ends_with($file, 'libsodium.pdb')) {
copy($file, BUILD_LIB_PATH . '\libsodium.pdb');
}
}
if (!$find) {
throw new RuntimeException('libsodium.lib not found');
}
}
}

View File

@ -14,18 +14,17 @@ declare(strict_types=1);
// test php version // test php version
$test_php_version = [ $test_php_version = [
'8.1', '8.1',
// '8.2', '8.2',
// '8.3', '8.3',
'8.4', '8.4',
]; ];
// test os (macos-13, macos-14, ubuntu-latest, windows-latest are available) // test os (macos-13, macos-14, ubuntu-latest, windows-latest are available)
$test_os = [ $test_os = [
// 'macos-13', 'macos-13',
// 'macos-14', 'macos-14',
// 'ubuntu-latest', 'ubuntu-latest',
'windows-2019', // 'windows-latest',
'windows-latest',
]; ];
// whether enable thread safe // whether enable thread safe
@ -42,13 +41,13 @@ $prefer_pre_built = false;
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`). // If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
$extensions = match (PHP_OS_FAMILY) { $extensions = match (PHP_OS_FAMILY) {
'Linux', 'Darwin' => 'gettext', 'Linux', 'Darwin' => 'gettext',
'Windows' => 'gd', 'Windows' => 'bcmath',
}; };
// If you want to test lib-suggests feature with extension, add them below (comma separated, example `libwebp,libavif`). // If you want to test lib-suggests feature with extension, add them below (comma separated, example `libwebp,libavif`).
$with_libs = match (PHP_OS_FAMILY) { $with_libs = match (PHP_OS_FAMILY) {
'Linux', 'Darwin' => '', 'Linux', 'Darwin' => '',
'Windows' => 'libavif', 'Windows' => '',
}; };
// Please change your test base combination. We recommend testing with `common`. // Please change your test base combination. We recommend testing with `common`.