From 1e6ddb86f26fc0f6bc2401a01345586425bc2bb6 Mon Sep 17 00:00:00 2001 From: henderkes Date: Sat, 21 Feb 2026 12:36:44 +0700 Subject: [PATCH] support static-only libs --- config/lib.json | 3 +++ src/SPC/builder/Extension.php | 3 ++- src/SPC/util/PkgConfigUtil.php | 12 +++++------- src/SPC/util/SPCConfigUtil.php | 10 ++++++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/config/lib.json b/config/lib.json index 491aea1f..184f27aa 100644 --- a/config/lib.json +++ b/config/lib.json @@ -303,6 +303,9 @@ }, "krb5": { "source": "krb5", + "target": [ + "static" + ], "pkg-configs": [ "krb5-gssapi" ], diff --git a/src/SPC/builder/Extension.php b/src/SPC/builder/Extension.php index f5a5d956..9077269e 100644 --- a/src/SPC/builder/Extension.php +++ b/src/SPC/builder/Extension.php @@ -96,7 +96,8 @@ class Extension fn ($x) => $x->getStaticLibFiles(), $this->getLibraryDependencies(recursive: true) ); - return implode(' ', $ret); + $libs = implode(' ', $ret); + return deduplicate_flags($libs); } /** diff --git a/src/SPC/util/PkgConfigUtil.php b/src/SPC/util/PkgConfigUtil.php index f1243c41..6ddb55d7 100644 --- a/src/SPC/util/PkgConfigUtil.php +++ b/src/SPC/util/PkgConfigUtil.php @@ -59,11 +59,10 @@ class PkgConfigUtil * @param string $pkg_config_str .pc file string, accepts multiple files * @return string CFLAGS string, e.g. "-Wno-implicit-int-float-conversion ..." */ - public static function getCflags(string $pkg_config_str): string + public static function getCflags(string $pkg_config_str, string $extra = '--static'): string { - $static = getenv('SPC_LINK_STATIC') ? '--static' : ''; // get other things - $result = self::execWithResult("pkg-config {$static} --cflags-only-other {$pkg_config_str}"); + $result = self::execWithResult("pkg-config {$extra} --cflags-only-other {$pkg_config_str}"); return trim($result); } @@ -76,15 +75,14 @@ class PkgConfigUtil * @param string $pkg_config_str .pc file string, accepts multiple files * @return array Unique libs array, e.g. [-lz, -lxml, ...] */ - public static function getLibsArray(string $pkg_config_str): array + public static function getLibsArray(string $pkg_config_str, string $extra = '--static'): array { // Use this instead of shell() to avoid unnecessary outputs - $static = getenv('SPC_LINK_STATIC') ? '--static' : ''; - $result = self::execWithResult("pkg-config {$static} --libs-only-l {$pkg_config_str}"); + $result = self::execWithResult("pkg-config {$extra} --libs-only-l {$pkg_config_str}"); $libs = explode(' ', trim($result)); // get other things - $result = self::execWithResult("pkg-config {$static} --libs-only-other {$pkg_config_str}"); + $result = self::execWithResult("pkg-config {$extra} --libs-only-other {$pkg_config_str}"); // convert libxxx.a to -L{path} -lxxx $exp = explode(' ', trim($result)); foreach ($exp as $item) { diff --git a/src/SPC/util/SPCConfigUtil.php b/src/SPC/util/SPCConfigUtil.php index 3be152f8..f6d4e790 100644 --- a/src/SPC/util/SPCConfigUtil.php +++ b/src/SPC/util/SPCConfigUtil.php @@ -235,7 +235,8 @@ class SPCConfigUtil } } $pc_cflags = implode(' ', $pc); - if ($pc_cflags !== '' && ($pc_cflags = PkgConfigUtil::getCflags($pc_cflags)) !== '') { + $static = getenv('SPC_LINK_STATIC') ? '--static' : ''; + if ($pc_cflags !== '' && ($pc_cflags = PkgConfigUtil::getCflags($pc_cflags, $static)) !== '') { $arr = explode(' ', $pc_cflags); $arr = array_unique($arr); $arr = array_filter($arr, fn ($x) => !str_starts_with($x, 'SHELL:-Xarch_')); @@ -260,6 +261,11 @@ class SPCConfigUtil foreach ($libraries as $library) { // add pkg-configs libs $pkg_configs = Config::getLib($library, 'pkg-configs', []); + $static = getenv('SPC_LINK_STATIC') ? '--static' : null; + if (!$static) { + $target = Config::getLib($library, 'target'); + $static = $target && !in_array('shared', $target) ? '--static' : ''; + } $pkg_config_path = getenv('PKG_CONFIG_PATH') ?: ''; $search_paths = array_filter(explode(is_unix() ? ':' : ';', $pkg_config_path)); foreach ($pkg_configs as $file) { @@ -276,7 +282,7 @@ class SPCConfigUtil $pkg_configs = implode(' ', $pkg_configs); if ($pkg_configs !== '') { // static libs with dependencies come in reverse order, so reverse this too - $pc_libs = array_reverse(PkgConfigUtil::getLibsArray($pkg_configs)); + $pc_libs = array_reverse(PkgConfigUtil::getLibsArray($static, $pkg_configs)); $lib_names = [...$lib_names, ...$pc_libs]; } // convert all static-libs to short names