Skip to content

Commit

Permalink
Add ffi support for windows x64 (#357)
Browse files Browse the repository at this point in the history
* add ffi support for windows x64

* add ffi test
  • Loading branch information
crazywhalecc committed Feb 29, 2024
1 parent 2547647 commit 842e0ad
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
5 changes: 4 additions & 1 deletion config/ext.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@
"ffi": {
"arg-type": "custom",
"type": "builtin",
"lib-depends": [
"lib-depends-unix": [
"libffi"
],
"lib-depends-windows": [
"libffi-win"
]
},
"fileinfo": {
Expand Down
11 changes: 11 additions & 0 deletions config/lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@
"ffitarget.h"
]
},
"libffi-win": {
"source": "libffi-win",
"static-libs-windows": [
"libffi.lib"
],
"headers-windows": [
"ffi.h",
"ffitarget.h",
"fficonfig.h"
]
},
"libiconv": {
"source": "libiconv",
"static-libs-unix": [
Expand Down
9 changes: 9 additions & 0 deletions config/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@
"path": "LICENSE"
}
},
"libffi-win": {
"type": "git",
"rev": "master",
"url": "https://github.com/static-php/libffi-win.git",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"libiconv": {
"type": "filelist",
"url": "https://ftp.gnu.org/gnu/libiconv/",
Expand Down
5 changes: 5 additions & 0 deletions src/SPC/builder/extension/ffi.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public function getUnixConfigureArg(): string
{
return '--with-ffi --enable-zend-signals';
}

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

declare(strict_types=1);

namespace SPC\builder\windows\library;

use SPC\builder\windows\SystemUtil;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;

class libffi_win extends WindowsLibraryBase
{
public const NAME = 'libffi-win';

protected function build()
{
$vs_ver_dir = match (SystemUtil::findVisualStudio()['version']) {
'vs17' => '/win32/vs17_x64',
'vs16' => '/win32/vs16_x64',
default => throw new RuntimeException('Current VS version is not supported yet!'),
};

// start build
cmd()->cd($this->source_dir . $vs_ver_dir)
->execWithWrapper(
$this->builder->makeSimpleWrapper('msbuild'),
'libffi-msvc.sln /t:Rebuild /p:Configuration=Release /p:Platform=x64'
);
FileSystem::createDir(BUILD_LIB_PATH);
FileSystem::createDir(BUILD_INCLUDE_PATH);
copy($this->source_dir . $vs_ver_dir . '\x64\Release\libffi.lib', BUILD_LIB_PATH . '\libffi.lib');
copy($this->source_dir . $vs_ver_dir . '\x64\Release\libffi.pdb', BUILD_LIB_PATH . '\libffi.pdb');
copy($this->source_dir . '\include\ffi.h', BUILD_INCLUDE_PATH . '\ffi.h');

FileSystem::replaceFileStr(BUILD_INCLUDE_PATH . '\ffi.h', '#define LIBFFI_H', "#define LIBFFI_H\n#define FFI_BUILDING");
copy($this->source_dir . '\src\x86\ffitarget.h', BUILD_INCLUDE_PATH . '\ffitarget.h');
copy($this->source_dir . '\fficonfig.h', BUILD_INCLUDE_PATH . '\fficonfig.h');

// copy($this->source_dir . '\msvc_build\out\static-Release\X64\libffi.lib', BUILD_LIB_PATH . '\libffi.lib');
// copy($this->source_dir . '\msvc_build\include\ffi.h', BUILD_INCLUDE_PATH . '\ffi.h');
// copy($this->source_dir . '\msvc_build\include\fficonfig.h', BUILD_INCLUDE_PATH . '\fficonfig.h');
// copy($this->source_dir . '\src\x86\ffitarget.h', BUILD_INCLUDE_PATH . '\ffitarget.h');

// FileSystem::replaceFileStr(BUILD_INCLUDE_PATH . '\ffi.h', '..\..\src\x86\ffitarget.h', 'ffitarget.h');
}
}
2 changes: 1 addition & 1 deletion src/globals/test-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
$extensions = match (PHP_OS_FAMILY) {
'Linux', 'Darwin' => 'pgsql,pdo_pgsql',
'Windows' => 'mbstring,pdo_sqlite,mbregex',
'Windows' => 'mbstring,pdo_sqlite,mbregex,ffi',
};

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

0 comments on commit 842e0ad

Please sign in to comment.