Skip to content

Commit 4f27d2d

Browse files
committed
add ext-decimal
1 parent 6e354b4 commit 4f27d2d

11 files changed

Lines changed: 172 additions & 6 deletions

File tree

config/ext.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
"qdbm"
7575
]
7676
},
77+
"decimal": {
78+
"type": "external",
79+
"source": "ext-decimal",
80+
"arg-type": "custom",
81+
"lib-depends": [
82+
"libmpdec"
83+
]
84+
},
7785
"dio": {
7886
"support": {
7987
"BSD": "wip"

config/lib.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@
528528
"maxminddb_config.h"
529529
]
530530
},
531+
"libmpdec": {
532+
"source": "libmpdec",
533+
"static-libs-unix": [
534+
"libmpdec.a"
535+
],
536+
"static-libs-windows": [
537+
"libmpdec_a.lib"
538+
],
539+
"headers": [
540+
"mpdecimal.h"
541+
]
542+
},
531543
"libmemcached": {
532544
"source": "libmemcached",
533545
"cpp-library": true,

config/source.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@
8484
"path": "COPYING"
8585
}
8686
},
87+
"ext-decimal": {
88+
"type": "ghtagtar",
89+
"repo": "php-decimal/ext-decimal",
90+
"match": "v2\\.\\d.*",
91+
"path": "php-src/ext/decimal",
92+
"license": {
93+
"type": "file",
94+
"path": "LICENSE"
95+
}
96+
},
8797
"dio": {
8898
"type": "url",
8999
"url": "https://pecl.php.net/get/dio",
@@ -681,6 +691,14 @@
681691
"path": "LICENSE"
682692
}
683693
},
694+
"libmpdec": {
695+
"type": "url",
696+
"url": "https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-4.0.1.tar.gz",
697+
"license": {
698+
"type": "file",
699+
"path": "COPYRIGHT.txt"
700+
}
701+
},
684702
"libmemcached": {
685703
"type": "ghtagtar",
686704
"repo": "awesomized/libmemcached",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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('decimal')]
12+
class decimal extends Extension
13+
{
14+
// TODO: remove this when https://github.com/php-decimal/ext-decimal/issues/92 is merged
15+
public function patchBeforeBuildconf(): bool
16+
{
17+
FileSystem::replaceFileStr(
18+
$this->source_dir . '/php_decimal.c',
19+
'zend_module_entry decimal_module_entry',
20+
'zend_module_entry php_decimal_module_entry'
21+
);
22+
return true;
23+
}
24+
25+
public function getUnixConfigureArg(bool $shared = false): string
26+
{
27+
return '--enable-decimal --with-libmpdec-path="' . BUILD_ROOT_PATH . '"';
28+
}
29+
30+
public function getWindowsConfigureArg(bool $shared = false): string
31+
{
32+
return '--with-decimal';
33+
}
34+
}
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\freebsd\library;
6+
7+
class libmpdec extends BSDLibraryBase
8+
{
9+
use \SPC\builder\unix\library\libmpdec;
10+
11+
public const NAME = 'libmpdec';
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\linux\library;
6+
7+
class libmpdec extends LinuxLibraryBase
8+
{
9+
use \SPC\builder\unix\library\libmpdec;
10+
11+
public const NAME = 'libmpdec';
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 libmpdec extends MacOSLibraryBase
8+
{
9+
use \SPC\builder\unix\library\libmpdec;
10+
11+
public const NAME = 'libmpdec';
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\unix\library;
6+
7+
use SPC\util\executor\UnixAutoconfExecutor;
8+
9+
trait libmpdec
10+
{
11+
protected function build(): void
12+
{
13+
UnixAutoconfExecutor::create($this)
14+
->configure('--disable-cxx --disable-shared --enable-static')
15+
->make();
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\windows\library;
6+
7+
class libmpdec extends WindowsLibraryBase
8+
{
9+
public const NAME = 'libmpdec';
10+
11+
protected function build(): void
12+
{
13+
$makefile_dir = $this->source_dir . '\libmpdec';
14+
$nmake = $this->builder->makeSimpleWrapper('nmake /nologo');
15+
16+
cmd()->cd($makefile_dir)
17+
->exec('copy /y Makefile.vc Makefile')
18+
->execWithWrapper($nmake, 'clean')
19+
->execWithWrapper($nmake, 'MACHINE=x64');
20+
21+
// Copy static lib (rename from versioned name to libmpdec_a.lib)
22+
$libs = glob($makefile_dir . '\libmpdec-*.lib');
23+
foreach ($libs as $lib) {
24+
if (!str_contains($lib, '.dll.')) {
25+
copy($lib, BUILD_LIB_PATH . '\libmpdec_a.lib');
26+
break;
27+
}
28+
}
29+
copy($makefile_dir . '\mpdecimal.h', BUILD_INCLUDE_PATH . '\mpdecimal.h');
30+
}
31+
}

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)