Compare commits

...

7 Commits

Author SHA1 Message Date
DubbleClick
952ecb2eec test all affected operating systems! 2025-07-23 10:50:29 +07:00
DubbleClick
31e7cd31c1 make sure that mimalloc.o is *always* first, no matter if $_ENV['LIBS'] is set 2025-07-23 10:49:23 +07:00
DubbleClick
51ec8199e4 bring back static lib logic 2025-07-23 10:46:30 +07:00
DubbleClick
f75c58b7d2 revert to double array_reverse, because static libs with dependencies are backwards 2025-07-23 10:21:36 +07:00
DubbleClick
f8970a21be fix cs 2025-07-23 09:53:31 +07:00
DubbleClick
b6be20727d fix a few accidental array_reverse calls, pkg-config --static instead of pkg-config --libs 2025-07-23 09:47:35 +07:00
crazywhalecc
9c4a6b46b6 Fix pkg-config missing option 2025-07-23 09:57:09 +08:00
3 changed files with 44 additions and 20 deletions

View File

@@ -32,25 +32,30 @@ class PkgConfigUtil
* @return array Unique libs array, e.g. [-lz, -lxml, ...]
* @throws RuntimeException
*/
public static function getLibsArray(string $pkg_config_str, bool $force_short_name = true): array
public static function getLibsArray(string $pkg_config_str): array
{
// Use this instead of shell() to avoid unnecessary outputs
$result = self::execWithResult("pkg-config --static --libs-only-l {$pkg_config_str}");
$libs = explode(' ', trim($result));
// get other things
$result = self::execWithResult("pkg-config --libs --libs-only-other {$pkg_config_str}");
$result = self::execWithResult("pkg-config --static --libs-only-other {$pkg_config_str}");
// convert libxxx.a to -L{path} -lxxx
$exp = explode(' ', trim($result));
foreach ($exp as $item) {
if (str_starts_with($item, '-L')) {
$libs[] = $item;
continue;
}
// if item ends with .a, convert it to -lxxx
if (str_ends_with($item, '.a') && str_starts_with($item, 'lib') && $force_short_name) {
if (str_ends_with($item, '.a') && (str_starts_with($item, 'lib') || str_starts_with($item, BUILD_LIB_PATH))) {
$name = pathinfo($item, PATHINFO_BASENAME);
$name = substr($name, 3, -2); // remove 'lib' prefix and '.a' suffix
$libs[] = "-l{$name}";
} else {
// if item starts with -L, keep it as is
// if item starts with -l, keep it as is
$shortlib = "-l{$name}";
if (!in_array($shortlib, $libs)) {
$libs[] = $shortlib;
}
} elseif (!in_array($item, $libs)) {
$libs[] = $item;
}
}

View File

@@ -6,7 +6,6 @@ namespace SPC\util;
use SPC\builder\BuilderBase;
use SPC\builder\BuilderProvider;
use SPC\builder\macos\MacOSBuilder;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
@@ -87,8 +86,9 @@ class SPCConfigUtil
$libs .= " {$this->getFrameworksString($extensions)}";
}
if ($this->builder->hasCpp()) {
$libs .= $this->builder instanceof MacOSBuilder ? ' -lc++' : ' -lstdc++';
$libs .= SPCTarget::getTargetOS() === 'Darwin' ? ' -lc++' : ' -lstdc++';
}
if ($this->libs_only_deps) {
return [
'cflags' => trim(getenv('CFLAGS') . ' ' . $cflags),
@@ -99,16 +99,20 @@ class SPCConfigUtil
// embed
if (!$this->no_php) {
$libs = "-lphp -lc {$libs}";
$libs = "-lphp {$libs} -lc";
}
$allLibs = getenv('LIBS') . ' ' . $libs;
// mimalloc must come first
if (str_contains($libs, BUILD_LIB_PATH . '/mimalloc.o')) {
$libs = BUILD_LIB_PATH . '/mimalloc.o ' . str_replace(BUILD_LIB_PATH . '/mimalloc.o', '', $libs);
$allLibs = BUILD_LIB_PATH . '/mimalloc.o ' . str_replace(BUILD_LIB_PATH . '/mimalloc.o', '', $allLibs);
}
return [
'cflags' => trim(getenv('CFLAGS') . ' ' . $cflags),
'ldflags' => trim(getenv('LDFLAGS') . ' ' . $ldflags),
'libs' => trim(getenv('LIBS') . ' ' . $libs),
'libs' => trim($allLibs),
];
}
@@ -138,8 +142,7 @@ class SPCConfigUtil
}
}
$pc_cflags = implode(' ', $pc);
if ($pc_cflags !== '') {
$pc_cflags = PkgConfigUtil::getCflags($pc_cflags);
if ($pc_cflags !== '' && ($pc_cflags = PkgConfigUtil::getCflags($pc_cflags)) !== '') {
$includes[] = $pc_cflags;
}
}
@@ -165,7 +168,7 @@ class SPCConfigUtil
if (!file_exists(BUILD_LIB_PATH . "/{$lib}")) {
throw new WrongUsageException("Library file '{$lib}' for lib [{$library}] does not exist in '" . BUILD_LIB_PATH . "'. Please build it first.");
}
$lib_names[] = $use_short_libs ? $this->getShortLibName($lib) : (BUILD_LIB_PATH . "/{$lib}");
$lib_names[] = $this->getShortLibName($lib);
}
// add frameworks for macOS
if (SPCTarget::getTargetOS() === 'Darwin') {
@@ -180,13 +183,13 @@ class SPCConfigUtil
}
$pkg_configs = implode(' ', $pkg_configs);
if ($pkg_configs !== '') {
$pc_libs = array_reverse(PkgConfigUtil::getLibsArray($pkg_configs, $use_short_libs));
$pc_libs = array_reverse(PkgConfigUtil::getLibsArray($pkg_configs));
$lib_names = [...$lib_names, ...$pc_libs];
}
}
// post-process
$lib_names = array_reverse(array_unique($lib_names));
$lib_names = array_unique(array_reverse(array_filter($lib_names, fn ($x) => $x !== '')));
$frameworks = array_unique($frameworks);
// process frameworks to short_name
@@ -202,6 +205,9 @@ class SPCConfigUtil
if (in_array('imap', $libraries) && SPCTarget::getLibc() === 'glibc') {
$lib_names[] = '-lcrypt';
}
if (!$use_short_libs) {
$lib_names = array_map(fn ($l) => $this->getFullLibName($l), $lib_names);
}
return implode(' ', $lib_names);
}
@@ -214,6 +220,19 @@ class SPCConfigUtil
return '-l' . substr($lib, 3, -2);
}
private function getFullLibName(string $lib)
{
if (!str_starts_with($lib, '-l')) {
return $lib;
}
$libname = substr($lib, 2);
$staticLib = BUILD_LIB_PATH . '/' . "lib{$libname}.a";
if (file_exists($staticLib)) {
return $staticLib;
}
return $lib;
}
private function getFrameworksString(array $extensions): string
{
$list = [];

View File

@@ -21,11 +21,11 @@ $test_php_version = [
// test os (macos-13, macos-14, macos-15, ubuntu-latest, windows-latest are available)
$test_os = [
// 'macos-13', // bin/spc for x86_64
'macos-13', // bin/spc for x86_64
// 'macos-14', // bin/spc for arm64
// 'macos-15', // bin/spc for arm64
'macos-15', // bin/spc for arm64
'ubuntu-latest', // bin/spc-alpine-docker for x86_64
// 'ubuntu-22.04', // bin/spc-gnu-docker for x86_64
'ubuntu-22.04', // bin/spc-gnu-docker for x86_64
'ubuntu-24.04', // bin/spc for x86_64
// 'ubuntu-22.04-arm', // bin/spc-gnu-docker for arm64
// 'ubuntu-24.04-arm', // bin/spc for arm64