Skip to content

Commit 08362fb

Browse files
committed
Add helper function for SPCConfigUtil
1 parent 4d5641f commit 08362fb

File tree

6 files changed

+100
-48
lines changed

6 files changed

+100
-48
lines changed

src/SPC/builder/Extension.php

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function patchBeforeSharedConfigure(): bool
220220
*/
221221
public function patchBeforeSharedMake(): bool
222222
{
223-
$config = (new SPCConfigUtil($this->builder))->config([$this->getName()], array_map(fn ($l) => $l->getName(), $this->builder->getLibs()));
223+
$config = (new SPCConfigUtil($this->builder))->getExtensionConfig($this);
224224
[$staticLibs, $sharedLibs] = $this->splitLibsIntoStaticAndShared($config['libs']);
225225
$lstdcpp = str_contains($sharedLibs, '-l:libstdc++.a') ? '-l:libstdc++.a' : null;
226226
$lstdcpp ??= str_contains($sharedLibs, '-lstdc++') ? '-lstdc++' : '';
@@ -486,18 +486,46 @@ public function isBuildStatic(): bool
486486
return $this->build_static;
487487
}
488488

489+
public function getLibraryDependencies(bool $recursive = false): array
490+
{
491+
$ret = array_filter($this->dependencies, fn ($x) => $x instanceof LibraryBase);
492+
if (!$recursive) {
493+
return $ret;
494+
}
495+
496+
$deps = [];
497+
498+
$added = 1;
499+
while ($added !== 0) {
500+
$added = 0;
501+
foreach ($ret as $depName => $dep) {
502+
foreach ($dep->getDependencies(true) as $depdepName => $depdep) {
503+
if (!array_key_exists($depdepName, $deps)) {
504+
$deps[$depdepName] = $depdep;
505+
++$added;
506+
}
507+
}
508+
if (!array_key_exists($depName, $deps)) {
509+
$deps[$depName] = $dep;
510+
}
511+
}
512+
}
513+
514+
if (array_key_exists(0, $deps)) {
515+
$zero = [0 => $deps[0]];
516+
unset($deps[0]);
517+
return $zero + $deps;
518+
}
519+
return $deps;
520+
}
521+
489522
/**
490523
* Returns the environment variables a shared extension needs to be built.
491524
* CFLAGS, CXXFLAGS, LDFLAGS and so on.
492525
*/
493526
protected function getSharedExtensionEnv(): array
494527
{
495-
$config = (new SPCConfigUtil($this->builder))->config(
496-
[$this->getName()],
497-
array_map(fn ($l) => $l->getName(), $this->getLibraryDependencies(recursive: true)),
498-
$this->builder->getOption('with-suggested-exts'),
499-
$this->builder->getOption('with-suggested-libs'),
500-
);
528+
$config = (new SPCConfigUtil($this->builder))->getExtensionConfig($this);
501529
[$staticLibs, $sharedLibs] = $this->splitLibsIntoStaticAndShared($config['libs']);
502530
$preStatic = PHP_OS_FAMILY === 'Darwin' ? '' : '-Wl,--start-group ';
503531
$postStatic = PHP_OS_FAMILY === 'Darwin' ? '' : ' -Wl,--end-group ';
@@ -567,37 +595,4 @@ protected function splitLibsIntoStaticAndShared(string $allLibs): array
567595
}
568596
return [trim($staticLibString), trim($sharedLibString)];
569597
}
570-
571-
private function getLibraryDependencies(bool $recursive = false): array
572-
{
573-
$ret = array_filter($this->dependencies, fn ($x) => $x instanceof LibraryBase);
574-
if (!$recursive) {
575-
return $ret;
576-
}
577-
578-
$deps = [];
579-
580-
$added = 1;
581-
while ($added !== 0) {
582-
$added = 0;
583-
foreach ($ret as $depName => $dep) {
584-
foreach ($dep->getDependencies(true) as $depdepName => $depdep) {
585-
if (!array_key_exists($depdepName, $deps)) {
586-
$deps[$depdepName] = $depdep;
587-
++$added;
588-
}
589-
}
590-
if (!array_key_exists($depName, $deps)) {
591-
$deps[$depName] = $dep;
592-
}
593-
}
594-
}
595-
596-
if (array_key_exists(0, $deps)) {
597-
$zero = [0 => $deps[0]];
598-
unset($deps[0]);
599-
return $zero + $deps;
600-
}
601-
return $deps;
602-
}
603598
}

src/SPC/builder/extension/grpc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function patchBeforeBuildconf(): bool
4343
public function patchBeforeConfigure(): bool
4444
{
4545
$util = new SPCConfigUtil($this->builder, ['libs_only_deps' => true]);
46-
$config = $util->config(['grpc']);
46+
$config = $util->getExtensionConfig($this);
4747
$libs = $config['libs'];
4848
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/configure', '-lgrpc', $libs);
4949
return true;

src/SPC/builder/extension/rdkafka.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SPC\builder\Extension;
88
use SPC\store\FileSystem;
99
use SPC\util\CustomExt;
10+
use SPC\util\SPCConfigUtil;
1011

1112
#[CustomExt('rdkafka')]
1213
class rdkafka extends Extension
@@ -38,10 +39,7 @@ public function patchBeforeMake(): bool
3839

3940
public function getUnixConfigureArg(bool $shared = false): string
4041
{
41-
$pkgconf_libs = shell()->execWithResult('pkg-config --libs --static rdkafka')[1];
42-
$pkgconf_libs = trim(implode('', $pkgconf_libs));
43-
$pkgconf_libs = str_replace(BUILD_LIB_PATH . '/lib', '-l', $pkgconf_libs);
44-
$pkgconf_libs = str_replace('.a', '', $pkgconf_libs);
45-
return '--with-rdkafka=' . ($shared ? 'shared,' : '') . BUILD_ROOT_PATH . ' RDKAFKA_LIBS="' . $pkgconf_libs . '"';
42+
$pkgconf_libs = (new SPCConfigUtil($this->builder, ['no_php' => true, 'libs_only_deps' => true]))->getExtensionConfig($this);
43+
return '--with-rdkafka=' . ($shared ? 'shared,' : '') . BUILD_ROOT_PATH . " RDKAFKA_LIBS=\"{$pkgconf_libs['libs']}\"";
4644
}
4745
}

src/SPC/builder/extension/swoole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getUnixConfigureArg(bool $shared = false): string
7070
$arg .= $this->builder->getExt('swoole-hook-mysql') ? ' --enable-mysqlnd' : ' --disable-mysqlnd';
7171
$arg .= $this->builder->getExt('swoole-hook-sqlite') ? ' --enable-swoole-sqlite' : ' --disable-swoole-sqlite';
7272
if ($this->builder->getExt('swoole-hook-odbc')) {
73-
$config = (new SPCConfigUtil($this->builder, ['libs_only_deps' => true]))->config([], ['unixodbc']);
73+
$config = (new SPCConfigUtil($this->builder))->getLibraryConfig($this->builder->getLib('unixodbc'));
7474
$arg .= ' --with-swoole-odbc=unixODBC,' . BUILD_ROOT_PATH . ' SWOOLE_ODBC_LIBS="' . $config['libs'] . '"';
7575
}
7676

src/SPC/builder/unix/library/postgresql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function build(): void
4747
{
4848
$libs = array_map(fn ($x) => $x->getName(), $this->getDependencies(true));
4949
$spc = new SPCConfigUtil($this->builder, ['no_php' => true, 'libs_only_deps' => true]);
50-
$config = $spc->config(libraries: $libs, include_suggest_lib: $this->builder->getOption('with-suggested-libs'));
50+
$config = $spc->config(libraries: $libs, include_suggest_lib: $this->builder->getOption('with-suggested-libs', false));
5151

5252
$env_vars = [
5353
'CFLAGS' => $config['cflags'],

src/SPC/util/SPCConfigUtil.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SPC\builder\BuilderBase;
88
use SPC\builder\BuilderProvider;
99
use SPC\builder\Extension;
10+
use SPC\builder\LibraryBase;
1011
use SPC\exception\WrongUsageException;
1112
use SPC\store\Config;
1213
use Symfony\Component\Console\Input\ArgvInput;
@@ -53,6 +54,9 @@ public function __construct(?BuilderBase $builder = null, array $options = [])
5354
*/
5455
public function config(array $extensions = [], array $libraries = [], bool $include_suggest_ext = false, bool $include_suggest_lib = false): array
5556
{
57+
logger()->debug('config extensions: ' . implode(',', $extensions));
58+
logger()->debug('config libs: ' . implode(',', $libraries));
59+
logger()->debug('config suggest for [ext, lib]: ' . ($include_suggest_ext ? 'true' : 'false') . ',' . ($include_suggest_lib ? 'true' : 'false'));
5660
$extra_exts = [];
5761
foreach ($extensions as $ext) {
5862
$extra_exts = array_merge($extra_exts, Config::getExt($ext, 'ext-suggests', []));
@@ -124,6 +128,61 @@ public function config(array $extensions = [], array $libraries = [], bool $incl
124128
];
125129
}
126130

131+
/**
132+
* [Helper function]
133+
* Get configuration for a specific extension(s) dependencies.
134+
*
135+
* @param Extension|Extension[] $extension Extension instance or list
136+
* @param bool $include_suggest_ext Whether to include suggested extensions
137+
* @param bool $include_suggest_lib Whether to include suggested libraries
138+
* @return array{
139+
* cflags: string,
140+
* ldflags: string,
141+
* libs: string
142+
* }
143+
*/
144+
public function getExtensionConfig(array|Extension $extension, bool $include_suggest_ext = false, bool $include_suggest_lib = false): array
145+
{
146+
if (!is_array($extension)) {
147+
$extension = [$extension];
148+
}
149+
return $this->config(
150+
extensions: array_map(fn ($x) => $x->getName(), $extension),
151+
include_suggest_ext: $include_suggest_ext ?: $this->builder?->getOption('with-suggested-exts') ?? false,
152+
include_suggest_lib: $include_suggest_lib ?: $this->builder?->getOption('with-suggested-libs') ?? false,
153+
);
154+
}
155+
156+
/**
157+
* [Helper function]
158+
* Get configuration for a specific library(s) dependencies.
159+
*
160+
* @param LibraryBase|LibraryBase[] $lib Library instance or list
161+
* @param bool $include_suggest_lib Whether to include suggested libraries
162+
* @return array{
163+
* cflags: string,
164+
* ldflags: string,
165+
* libs: string
166+
* }
167+
*/
168+
public function getLibraryConfig(array|LibraryBase $lib, bool $include_suggest_lib = false): array
169+
{
170+
if (!is_array($lib)) {
171+
$lib = [$lib];
172+
}
173+
$save_no_php = $this->no_php;
174+
$this->no_php = true;
175+
$save_libs_only_deps = $this->libs_only_deps;
176+
$this->libs_only_deps = true;
177+
$ret = $this->config(
178+
libraries: array_map(fn ($x) => $x->getName(), $lib),
179+
include_suggest_lib: $include_suggest_lib ?: $this->builder?->getOption('with-suggested-libs') ?? false,
180+
);
181+
$this->no_php = $save_no_php;
182+
$this->libs_only_deps = $save_libs_only_deps;
183+
return $ret;
184+
}
185+
127186
private function hasCpp(array $extensions, array $libraries): bool
128187
{
129188
// judge cpp-extension

0 commit comments

Comments
 (0)