Skip to content

Commit d2f007d

Browse files
committed
Forward-port #1095
1 parent c505430 commit d2f007d

5 files changed

Lines changed: 116 additions & 0 deletions

File tree

config/pkg/ext/ext-decimal.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ext-decimal:
2+
type: php-extension
3+
artifact:
4+
source:
5+
type: ghtagtar
6+
repo: php-decimal/ext-decimal
7+
match: 'v2\.\d.*'
8+
extract: php-src/ext/decimal
9+
metadata:
10+
license-files: [LICENSE]
11+
license: MIT
12+
depends:
13+
- libmpdec
14+
php-extension:
15+
arg-type@unix: '--enable-decimal --with-libmpdec-path=@build_root_path@'
16+
arg-type@windows: '--with-decimal'

config/pkg/lib/libmpdec.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
libmpdec:
2+
type: library
3+
artifact:
4+
source:
5+
type: url
6+
url: 'https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-4.0.1.tar.gz'
7+
metadata:
8+
license-files: [COPYRIGHT.txt]
9+
license: BSD-2-Clause
10+
headers:
11+
- mpdecimal.h
12+
static-libs@unix:
13+
- libmpdec.a
14+
static-libs@windows:
15+
- libmpdec_a.lib

src/Package/Extension/decimal.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Package\Extension;
6+
7+
use Package\Target\php;
8+
use StaticPHP\Attribute\Package\BeforeStage;
9+
use StaticPHP\Attribute\Package\Extension;
10+
use StaticPHP\Attribute\PatchDescription;
11+
use StaticPHP\Package\PhpExtensionPackage;
12+
use StaticPHP\Util\FileSystem;
13+
14+
#[Extension('decimal')]
15+
class decimal extends PhpExtensionPackage
16+
{
17+
// TODO: remove this when https://github.com/php-decimal/ext-decimal/issues/92 is merged
18+
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-decimal')]
19+
#[BeforeStage('php', [php::class, 'buildconfForWindows'], 'ext-decimal')]
20+
#[PatchDescription('Fix decimal extension module entry symbol name conflict')]
21+
public function patchBeforeBuildconf(): void
22+
{
23+
FileSystem::replaceFileStr(
24+
$this->getSourceDir() . '/php_decimal.c',
25+
'zend_module_entry decimal_module_entry',
26+
'zend_module_entry php_decimal_module_entry'
27+
);
28+
}
29+
}

src/Package/Library/libmpdec.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Package\Library;
6+
7+
use StaticPHP\Attribute\Package\BuildFor;
8+
use StaticPHP\Attribute\Package\Library;
9+
use StaticPHP\Package\LibraryPackage;
10+
use StaticPHP\Runtime\Executor\UnixAutoconfExecutor;
11+
use StaticPHP\Util\FileSystem;
12+
13+
#[Library('libmpdec')]
14+
class libmpdec
15+
{
16+
#[BuildFor('Linux')]
17+
#[BuildFor('Darwin')]
18+
public function build(LibraryPackage $lib): void
19+
{
20+
UnixAutoconfExecutor::create($lib)
21+
->configure('--disable-cxx --disable-shared --enable-static')
22+
->make();
23+
}
24+
25+
#[BuildFor('Windows')]
26+
public function buildWin(LibraryPackage $lib): void
27+
{
28+
$makefileDir = $lib->getSourceDir() . DIRECTORY_SEPARATOR . 'libmpdec';
29+
30+
cmd()->cd($makefileDir)
31+
->exec('copy /y Makefile.vc Makefile')
32+
->exec('nmake /nologo clean')
33+
->exec('nmake /nologo MACHINE=x64');
34+
35+
// Copy static lib (rename from versioned name to libmpdec_a.lib)
36+
$libs = glob($makefileDir . DIRECTORY_SEPARATOR . 'libmpdec-*.lib');
37+
foreach ($libs as $libFile) {
38+
if (!str_contains($libFile, '.dll.')) {
39+
FileSystem::copy($libFile, $lib->getLibDir() . DIRECTORY_SEPARATOR . 'libmpdec_a.lib');
40+
break;
41+
}
42+
}
43+
44+
FileSystem::copy($makefileDir . DIRECTORY_SEPARATOR . 'mpdecimal.h', $lib->getIncludeDir() . DIRECTORY_SEPARATOR . 'mpdecimal.h');
45+
}
46+
}

src/globals/ext-tests/decimal.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
assert(class_exists('Decimal\Decimal'));
6+
assert(method_exists('Decimal\Decimal', 'valueOf'));
7+
assert(0.1 + 0.2 !== 0.3);
8+
$result = Decimal\Decimal::valueOf('0.1') + Decimal\Decimal::valueOf('0.2');
9+
$expected = Decimal\Decimal::valueOf('0.3');
10+
assert($result == $expected);

0 commit comments

Comments
 (0)