Skip to content

Commit 3f7bad7

Browse files
authored
Feat/clickhouse (#1137)
2 parents 9b64ad8 + a3f135d commit 3f7bad7

6 files changed

Lines changed: 72 additions & 18 deletions

File tree

.github/pull_request_template.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
## What does this PR do?
22

3+
<!-- Please describe the changes made in this PR here. -->
34

45

56
## Checklist before merging
67

7-
> If your PR involves the changes mentioned below and completed the action, please tick the corresponding option.
8-
> If a modification is not involved, please skip it directly.
9-
10-
- If you modified `*.php` or `*.json`, run them locally to ensure your changes are valid:
8+
- If you modified `*.php` or `*.yml`, run them locally to ensure your changes are valid:
119
- [ ] `composer cs-fix`
1210
- [ ] `composer analyse`
1311
- [ ] `composer test`
14-
- [ ] `bin/spc dev:sort-config`
15-
- If it's an extension or dependency update, please ensure the following:
16-
- [ ] Add your test combination to `src/globals/test-extensions.php`.
17-
- [ ] If adding new or fixing bugs, add commit message containing `extension test` or `test extensions` to trigger full test suite.
12+
- [ ] `bin/spc dev:lint-config`

config/pkg/ext/ext-clickhouse.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ext-clickhouse:
2+
type: php-extension
3+
artifact:
4+
source:
5+
type: ghtar
6+
repo: iliaal/php_clickhouse
7+
extract: php-src/ext/clickhouse
8+
prefer-stable: true
9+
metadata:
10+
license-files: [LICENSE]
11+
license: PHP-3.01
12+
suggests@unix:
13+
- openssl
14+
lang: cpp
15+
php-extension:
16+
os:
17+
- Linux
18+
- Darwin
19+
arg-type@unix: custom
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 Package\Extension;
6+
7+
use Package\Target\php;
8+
use StaticPHP\Attribute\Package\BeforeStage;
9+
use StaticPHP\Attribute\Package\CustomPhpConfigureArg;
10+
use StaticPHP\Attribute\Package\Extension;
11+
use StaticPHP\Attribute\PatchDescription;
12+
use StaticPHP\Package\PackageInstaller;
13+
use StaticPHP\Package\PhpExtensionPackage;
14+
use StaticPHP\Util\FileSystem;
15+
16+
#[Extension('clickhouse')]
17+
class clickhouse extends PhpExtensionPackage
18+
{
19+
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-clickhouse')]
20+
#[PatchDescription('Replace THIS_DIR=`dirname $0` with PHP_EXT_SRCDIR() in config.m4 so include paths resolve to the ext source dir during PHP main configure (dirname $0 returns "." when run from php-src root).')]
21+
public function patchBeforeBuildconfUnix(): void
22+
{
23+
FileSystem::replaceFileRegex(
24+
"{$this->getSourceDir()}/config.m4",
25+
'/^(\s*)THIS_DIR=.*/m',
26+
'$1THIS_DIR=PHP_EXT_SRCDIR()',
27+
);
28+
}
29+
30+
#[CustomPhpConfigureArg('Darwin')]
31+
#[CustomPhpConfigureArg('Linux')]
32+
public function getUnixConfigureArg(bool $shared, PackageInstaller $installer): string
33+
{
34+
$arg = '--enable-clickhouse' . ($shared ? '=shared' : '');
35+
if ($installer->getLibraryPackage('openssl')) {
36+
$arg .= ' --enable-clickhouse-openssl';
37+
}
38+
return $arg;
39+
}
40+
}

src/Package/Target/php/unix.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,13 @@ public function makeEmbedForUnix(TargetPackage $package, PackageInstaller $insta
352352

353353
// ------------- SPC_CMD_VAR_PHP_EMBED_TYPE=static -------------
354354

355-
// process libphp.a for static embed
356-
if (!file_exists("{$package->getLibDir()}/libphp.a")) {
357-
return;
355+
// process libphp.a for static embed (only when present)
356+
if (file_exists("{$package->getLibDir()}/libphp.a")) {
357+
$ar = getenv('AR') ?: 'ar';
358+
$libphp_a = "{$package->getLibDir()}/libphp.a";
359+
shell()->exec("{$ar} -t {$libphp_a} | grep '\\.a$' | xargs -n1 {$ar} d {$libphp_a}");
360+
UnixUtil::exportDynamicSymbols($libphp_a);
358361
}
359-
$ar = getenv('AR') ?: 'ar';
360-
$libphp_a = "{$package->getLibDir()}/libphp.a";
361-
shell()->exec("{$ar} -t {$libphp_a} | grep '\\.a$' | xargs -n1 {$ar} d {$libphp_a}");
362-
UnixUtil::exportDynamicSymbols($libphp_a);
363362

364363
// deploy embed php scripts
365364
$package->runStage([$this, 'patchUnixEmbedScripts']);
@@ -508,7 +507,8 @@ public function patchUnixEmbedScripts(): void
508507
if (file_exists(BUILD_BIN_PATH . '/php-config')) {
509508
logger()->debug('Patching php-config prefix and libs order');
510509
$php_config_str = FileSystem::readFile(BUILD_BIN_PATH . '/php-config');
511-
$php_config_str = str_replace('prefix=""', 'prefix="' . BUILD_ROOT_PATH . '"', $php_config_str);
510+
// anchor to start-of-line so we don't also match `program_prefix=""`
511+
$php_config_str = preg_replace('/^prefix=""/m', 'prefix="' . BUILD_ROOT_PATH . '"', $php_config_str);
512512
// move mimalloc to the beginning of libs
513513
$php_config_str = preg_replace('/(libs=")(.*?)\s*(' . preg_quote(BUILD_LIB_PATH, '/') . '\/mimalloc\.o)\s*(.*?)"/', '$1$3 $2 $4"', $php_config_str);
514514
// move lstdc++ to the end of libs

src/StaticPHP/Package/PhpExtensionPackage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public function configureForUnix(array $env, PhpExtensionPackage $package): void
306306
shell()->cd($package->getSourceDir())
307307
->setEnv($env)
308308
->exec(
309-
'./configure ' . $this->getPhpConfigureArg(SystemTarget::getCurrentPlatformString(), true) .
309+
'./configure ' . $this->getPhpConfigureArg(SystemTarget::getTargetOS(), true) .
310310
' --with-php-config=' . BUILD_BIN_PATH . '/php-config ' .
311311
"--enable-shared --disable-static {$phpvars}"
312312
);

src/globals/test-extensions.php

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

5151
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
5252
$extensions = match (PHP_OS_FAMILY) {
53-
'Linux', 'Darwin' => 'curl,swoole',
53+
'Linux', 'Darwin' => 'openssl,zstd,clickhouse',
5454
'Windows' => 'intl',
5555
};
5656

0 commit comments

Comments
 (0)