Skip to content

Commit 1a7e436

Browse files
authored
Merge pull request #339 from crazywhalecc/ext/gettext
Add gettext support
2 parents e2ef195 + f11b36a commit 1a7e436

File tree

16 files changed

+160
-12
lines changed

16 files changed

+160
-12
lines changed

README-zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ bin/spc micro:combine my-app.phar -I "memory_limit=4G" -I "disable_functions=sys
248248

249249
## 赞助本项目
250250

251-
你可以在 [我的个人赞助页](https://github.com/crazywhalecc/crazywhalecc/blob/master/FUNDING.md) 支持我和我的项目。
251+
你可以在 [我的个人赞助页](https://github.com/crazywhalecc/crazywhalecc/blob/master/FUNDING.md) 支持我和我的项目。你捐赠的一部分将会被用于维护 **static-php.dev** 服务器。
252252

253253
## 开源协议
254254

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ Now there is a [static-php](https://github.com/static-php) organization, which i
272272

273273
## Sponsor this project
274274

275-
You can sponsor my project on [this page](https://github.com/crazywhalecc/crazywhalecc/blob/master/FUNDING.md).
275+
You can sponsor my project on [this page](https://github.com/crazywhalecc/crazywhalecc/blob/master/FUNDING.md). A portion of your donation will be used to maintain the **static-php.dev** server.
276276

277277
## Open-Source License
278278

config/ext.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
"freetype"
9494
]
9595
},
96+
"gettext": {
97+
"type": "builtin",
98+
"arg-type": "with-prefix",
99+
"lib-depends": [
100+
"gettext"
101+
]
102+
},
96103
"glfw": {
97104
"type": "external",
98105
"arg-type": "custom",

config/lib.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
8383
"brotli"
8484
]
8585
},
86+
"gettext": {
87+
"source": "gettext",
88+
"static-libs-unix": [
89+
"libintl.a"
90+
],
91+
"lib-depends": [
92+
"libiconv"
93+
],
94+
"lib-suggests": [
95+
"ncurses",
96+
"libxml2"
97+
],
98+
"frameworks": [
99+
"CoreFoundation"
100+
]
101+
},
86102
"glfw": {
87103
"source": "ext-glfw",
88104
"static-libs-unix": [

config/source.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@
129129
"path": "LICENSE.TXT"
130130
}
131131
},
132+
"gettext": {
133+
"type": "filelist",
134+
"url": "https://ftp.gnu.org/pub/gnu/gettext/",
135+
"regex": "/href=\"(?<file>gettext-(?<version>[^\"]+)\\.tar\\.xz)\"/",
136+
"license": {
137+
"type": "file",
138+
"path": "COPYING"
139+
}
140+
},
132141
"gmp": {
133142
"type": "ghtagtar",
134143
"repo": "alisw/GMP",

src/SPC/builder/Extension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ public function runCliCheckUnix(): void
185185
file_get_contents(ROOT_DIR . '/src/globals/tests/' . $this->getName() . '.php')
186186
);
187187

188-
[$ret] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -r "' . trim($test) . '"');
188+
[$ret, $out] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -r "' . trim($test) . '"');
189189
if ($ret !== 0) {
190+
if ($this->builder->getOption('debug')) {
191+
var_dump($out);
192+
}
190193
throw new RuntimeException('extension ' . $this->getName() . ' failed sanity check');
191194
}
192195
}

src/SPC/builder/extension/gettext.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\extension;
6+
7+
use SPC\builder\Extension;
8+
use SPC\builder\macos\MacOSBuilder;
9+
use SPC\exception\FileSystemException;
10+
use SPC\exception\WrongUsageException;
11+
use SPC\store\FileSystem;
12+
use SPC\util\CustomExt;
13+
14+
#[CustomExt('gettext')]
15+
class gettext extends Extension
16+
{
17+
/**
18+
* @throws FileSystemException
19+
*/
20+
public function patchBeforeBuildconf(): bool
21+
{
22+
if ($this->builder instanceof MacOSBuilder) {
23+
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/ext/gettext/config.m4', 'AC_CHECK_LIB($GETTEXT_CHECK_IN_LIB', 'AC_CHECK_LIB(intl');
24+
}
25+
return true;
26+
}
27+
28+
/**
29+
* @throws WrongUsageException
30+
* @throws FileSystemException
31+
*/
32+
public function patchBeforeConfigure(): bool
33+
{
34+
if ($this->builder instanceof MacOSBuilder) {
35+
$frameworks = ' ' . $this->builder->getFrameworks(true) . ' ';
36+
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/configure', '-lintl', $this->getLibFilesString() . $frameworks);
37+
}
38+
return true;
39+
}
40+
}

src/SPC/builder/linux/SystemUtil.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ public static function getArchCFlags(string $cc, string $arch): string
110110
public static function getTuneCFlags(string $arch): array
111111
{
112112
return match ($arch) {
113-
'x86_64' => [
114-
'-march=corei7',
115-
'-mtune=core-avx2',
116-
],
117-
'arm64', 'aarch64' => [],
113+
'x86_64', 'arm64', 'aarch64' => [],
118114
default => throw new RuntimeException('unsupported arch: ' . $arch),
119115
};
120116
}
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 gettext extends LinuxLibraryBase
8+
{
9+
use \SPC\builder\unix\library\gettext;
10+
11+
public const NAME = 'gettext';
12+
}

src/SPC/builder/linux/library/libxml2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class libxml2 extends LinuxLibraryBase
1818
*/
1919
public function build(): void
2020
{
21-
$enable_zlib = $this->builder->getLib('zlib') ? 'ON' : 'OFF';
21+
$enable_zlib = $this->builder->getLib('zlib') ? ('ON -DZLIB_LIBRARY=' . BUILD_LIB_PATH . '/libz.a -DZLIB_INCLUDE_DIR=' . BUILD_INCLUDE_PATH) : 'OFF';
2222
$enable_icu = $this->builder->getLib('icu') ? 'ON' : 'OFF';
2323
$enable_xz = $this->builder->getLib('xz') ? 'ON' : 'OFF';
2424

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 gettext extends MacOSLibraryBase
8+
{
9+
use \SPC\builder\unix\library\gettext;
10+
11+
public const NAME = 'gettext';
12+
}

src/SPC/builder/macos/library/libxml2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class libxml2 extends MacOSLibraryBase
1818
*/
1919
protected function build(): void
2020
{
21-
$enable_zlib = $this->builder->getLib('zlib') ? 'ON' : 'OFF';
21+
$enable_zlib = $this->builder->getLib('zlib') ? ('ON -DZLIB_LIBRARY=' . BUILD_LIB_PATH . '/libz.a -DZLIB_INCLUDE_DIR=' . BUILD_INCLUDE_PATH) : 'OFF';
2222
$enable_icu = $this->builder->getLib('icu') ? 'ON' : 'OFF';
2323
$enable_xz = $this->builder->getLib('xz') ? 'ON' : 'OFF';
2424

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\builder\unix\library;
6+
7+
trait gettext
8+
{
9+
protected function build(): void
10+
{
11+
$extra = $this->builder->getLib('ncurses') ? ('--with-libncurses-prefix=' . BUILD_ROOT_PATH . ' ') : '';
12+
$extra .= $this->builder->getLib('libxml2') ? ('--with-libxml2-prefix=' . BUILD_ROOT_PATH . ' ') : '';
13+
shell()->cd($this->source_dir)
14+
->exec(
15+
'./configure ' .
16+
'--enable-static ' .
17+
'--disable-shared ' .
18+
'--disable-java ' .
19+
'--disable-c+ ' .
20+
$extra .
21+
'--with-libiconv-prefix=' . BUILD_ROOT_PATH . ' ' .
22+
'--prefix=' . BUILD_ROOT_PATH
23+
)
24+
->exec('make clean')
25+
->exec("make -j{$this->builder->concurrency}")
26+
->exec('make install');
27+
}
28+
}

src/SPC/command/BuildCliCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function configure(): void
3232
$this->addOption('enable-zts', null, null, 'enable ZTS support');
3333
$this->addOption('disable-opcache-jit', null, null, 'disable opcache jit');
3434
$this->addOption('with-hardcoded-ini', 'I', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Patch PHP source code, inject hardcoded INI');
35-
$this->addOption('with-micro-fake-cli', null, null, 'Enable phpmicro fake cli');
35+
$this->addOption('with-micro-fake-cli', null, null, 'Let phpmicro\'s PHP_SAPI use "cli" instead of "micro"');
3636
$this->addOption('with-suggested-libs', 'L', null, 'Build with suggested libs for selected exts and libs');
3737
$this->addOption('with-suggested-exts', 'E', null, 'Build with suggested extensions for selected exts');
3838
$this->addOption('with-added-patch', 'P', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Inject patch script outside');

src/globals/test-extensions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
1515
$extensions = match (PHP_OS_FAMILY) {
16-
'Linux', 'Darwin' => '',
16+
'Linux', 'Darwin' => 'event,gettext',
1717
'Windows' => 'mbstring',
1818
};
1919

src/globals/tests/gettext.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
assert(function_exists('gettext'));
6+
assert(function_exists('bindtextdomain'));
7+
assert(function_exists('textdomain'));
8+
assert(function_exists('bind_textdomain_codeset'));
9+
10+
if (!is_dir('locale/en_US/LC_MESSAGES/')) {
11+
mkdir('locale/en_US/LC_MESSAGES/', 0755, true);
12+
}
13+
if (!file_exists('locale/en_US/LC_MESSAGES/test.mo')) {
14+
$mo = '3hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABgAAAFEAAAAXAQAAWAAAAAcAAABwAQAAAQAAAAAAAAAAAAAAAgAAAAAAAAAA56S65L6LAFByb2plY3QtSWQtVmVyc2lvbjogUEFDS0FHRSBWRVJTSU9OClJlcG9ydC1Nc2dpZC1CdWdzLVRvOiAKUE8tUmV2aXNpb24tRGF0ZTogWUVBUi1NTy1EQSBITzpNSStaT05FCkxhc3QtVHJhbnNsYXRvcjogRlVMTCBOQU1FIDxFTUFJTEBBRERSRVNTPgpMYW5ndWFnZS1UZWFtOiBMQU5HVUFHRSA8TExAbGkub3JnPgpMYW5ndWFnZTogCk1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD1VVEYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA4Yml0CgBFeGFtcGxlAA==';
15+
file_put_contents('locale/en_US/LC_MESSAGES/test.mo', base64_decode($mo));
16+
}
17+
putenv('LANG=en_US');
18+
setlocale(LC_ALL, 'en_US');
19+
20+
$domain = 'test';
21+
bindtextdomain($domain, 'locale/');
22+
bind_textdomain_codeset($domain, 'UTF-8');
23+
textdomain($domain);
24+
25+
assert(gettext(json_decode('"\u793a\u4f8b"', true)) === 'Example');

0 commit comments

Comments
 (0)