allow building and linking with shared instead of static libs

This commit is contained in:
henderkes
2026-02-19 21:26:18 +07:00
parent 8c4e3d58a3
commit cf69c02624
15 changed files with 35 additions and 55 deletions

View File

@@ -61,8 +61,9 @@ class PkgConfigUtil
*/
public static function getCflags(string $pkg_config_str): string
{
$static = getenv('SPC_STATIC_LIBS') ? '--static' : '';
// get other things
$result = self::execWithResult("pkg-config --static --cflags-only-other {$pkg_config_str}");
$result = self::execWithResult("pkg-config $static --cflags-only-other {$pkg_config_str}");
return trim($result);
}
@@ -78,11 +79,12 @@ class PkgConfigUtil
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}");
$static = getenv('SPC_STATIC_LIBS') ? '--static' : '';
$result = self::execWithResult("pkg-config $static --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 $static --libs-only-other {$pkg_config_str}");
// convert libxxx.a to -L{path} -lxxx
$exp = explode(' ', trim($result));
foreach ($exp as $item) {

View File

@@ -20,13 +20,10 @@ class SPCConfigUtil
private bool $libs_only_deps;
private bool $absolute_libs;
/**
* @param array{
* no_php?: bool,
* libs_only_deps?: bool,
* absolute_libs?: bool
* libs_only_deps?: bool
* } $options Options pass to spc-config
*/
public function __construct(?BuilderBase $builder = null, array $options = [])
@@ -36,7 +33,6 @@ class SPCConfigUtil
}
$this->no_php = $options['no_php'] ?? false;
$this->libs_only_deps = $options['libs_only_deps'] ?? false;
$this->absolute_libs = $options['absolute_libs'] ?? false;
}
/**
@@ -77,7 +73,7 @@ class SPCConfigUtil
ob_get_clean();
$ldflags = $this->getLdflagsString();
$cflags = $this->getIncludesString($libraries);
$libs = $this->getLibsString($libraries, !$this->absolute_libs);
$libs = $this->getLibsString($libraries);
// additional OS-specific libraries (e.g. macOS -lresolv)
if ($extra_libs = SPCTarget::getRuntimeLibs()) {
@@ -256,7 +252,7 @@ class SPCConfigUtil
return '-L' . BUILD_LIB_PATH;
}
private function getLibsString(array $libraries, bool $use_short_libs = true): string
private function getLibsString(array $libraries): string
{
$lib_names = [];
$frameworks = [];
@@ -316,8 +312,8 @@ 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);
if (getenv('SPC_STATIC_LIBS')) {
$lib_names = array_map(fn ($l) => $this->getStaticLibname($l), $lib_names);
}
return implode(' ', $lib_names);
}
@@ -331,7 +327,7 @@ class SPCConfigUtil
return '-l' . substr($lib, 3, -2);
}
private function getFullLibName(string $lib)
private function getStaticLibname(string $lib)
{
if (!str_starts_with($lib, '-l')) {
return $lib;
@@ -339,7 +335,7 @@ class SPCConfigUtil
$libname = substr($lib, 2);
$staticLib = BUILD_LIB_PATH . '/' . "lib{$libname}.a";
if (file_exists($staticLib)) {
return $staticLib;
return "-l:lib{$libname}.a";
}
return $lib;
}

View File

@@ -133,8 +133,8 @@ class UnixAutoconfExecutor extends Executor
private function getDefaultConfigureArgs(): array
{
return [
'--disable-shared',
'--enable-static',
getenv('SPC_STATIC_LIBS') ? '--disable-shared' : '--enable-shared',
getenv('SPC_STATIC_LIBS') ? '--enable-static' : '--disable-static',
"--prefix={$this->library->getBuildRootPath()}",
'--with-pic',
'--enable-pic',

View File

@@ -151,7 +151,7 @@ class UnixCMakeExecutor extends Executor
'-DCMAKE_INSTALL_LIBDIR=lib',
'-DCMAKE_INSTALL_INCLUDEDIR=include',
'-DPOSITION_INDEPENDENT_CODE=ON',
'-DBUILD_SHARED_LIBS=OFF',
'-DBUILD_SHARED_LIBS=' . getenv('SPC_STATIC_LIBS') ? 'OFF' : 'ON',
"-DCMAKE_TOOLCHAIN_FILE={$this->makeCmakeToolchainFile()}",
]);
}