Forward-port #1095

This commit is contained in:
crazywhalecc
2026-04-12 14:17:53 +08:00
parent c505430187
commit d2f007d4c4
5 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
ext-decimal:
type: php-extension
artifact:
source:
type: ghtagtar
repo: php-decimal/ext-decimal
match: 'v2\.\d.*'
extract: php-src/ext/decimal
metadata:
license-files: [LICENSE]
license: MIT
depends:
- libmpdec
php-extension:
arg-type@unix: '--enable-decimal --with-libmpdec-path=@build_root_path@'
arg-type@windows: '--with-decimal'

View File

@@ -0,0 +1,15 @@
libmpdec:
type: library
artifact:
source:
type: url
url: 'https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-4.0.1.tar.gz'
metadata:
license-files: [COPYRIGHT.txt]
license: BSD-2-Clause
headers:
- mpdecimal.h
static-libs@unix:
- libmpdec.a
static-libs@windows:
- libmpdec_a.lib

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Package\Extension;
use Package\Target\php;
use StaticPHP\Attribute\Package\BeforeStage;
use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Attribute\PatchDescription;
use StaticPHP\Package\PhpExtensionPackage;
use StaticPHP\Util\FileSystem;
#[Extension('decimal')]
class decimal extends PhpExtensionPackage
{
// TODO: remove this when https://github.com/php-decimal/ext-decimal/issues/92 is merged
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-decimal')]
#[BeforeStage('php', [php::class, 'buildconfForWindows'], 'ext-decimal')]
#[PatchDescription('Fix decimal extension module entry symbol name conflict')]
public function patchBeforeBuildconf(): void
{
FileSystem::replaceFileStr(
$this->getSourceDir() . '/php_decimal.c',
'zend_module_entry decimal_module_entry',
'zend_module_entry php_decimal_module_entry'
);
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Package\Library;
use StaticPHP\Attribute\Package\BuildFor;
use StaticPHP\Attribute\Package\Library;
use StaticPHP\Package\LibraryPackage;
use StaticPHP\Runtime\Executor\UnixAutoconfExecutor;
use StaticPHP\Util\FileSystem;
#[Library('libmpdec')]
class libmpdec
{
#[BuildFor('Linux')]
#[BuildFor('Darwin')]
public function build(LibraryPackage $lib): void
{
UnixAutoconfExecutor::create($lib)
->configure('--disable-cxx --disable-shared --enable-static')
->make();
}
#[BuildFor('Windows')]
public function buildWin(LibraryPackage $lib): void
{
$makefileDir = $lib->getSourceDir() . DIRECTORY_SEPARATOR . 'libmpdec';
cmd()->cd($makefileDir)
->exec('copy /y Makefile.vc Makefile')
->exec('nmake /nologo clean')
->exec('nmake /nologo MACHINE=x64');
// Copy static lib (rename from versioned name to libmpdec_a.lib)
$libs = glob($makefileDir . DIRECTORY_SEPARATOR . 'libmpdec-*.lib');
foreach ($libs as $libFile) {
if (!str_contains($libFile, '.dll.')) {
FileSystem::copy($libFile, $lib->getLibDir() . DIRECTORY_SEPARATOR . 'libmpdec_a.lib');
break;
}
}
FileSystem::copy($makefileDir . DIRECTORY_SEPARATOR . 'mpdecimal.h', $lib->getIncludeDir() . DIRECTORY_SEPARATOR . 'mpdecimal.h');
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
assert(class_exists('Decimal\Decimal'));
assert(method_exists('Decimal\Decimal', 'valueOf'));
assert(0.1 + 0.2 !== 0.3);
$result = Decimal\Decimal::valueOf('0.1') + Decimal\Decimal::valueOf('0.2');
$expected = Decimal\Decimal::valueOf('0.3');
assert($result == $expected);