2025-07-22 17:23:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\util;
|
|
|
|
|
|
|
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
|
|
|
|
|
class PkgConfigUtil
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Returns --cflags-only-other output.
|
|
|
|
|
* The reason we return the string is we cannot use array_unique() on cflags,
|
|
|
|
|
* some cflags may contains spaces.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pkg_config_str .pc file str, accepts multiple files
|
|
|
|
|
* @return string cflags string, e.g. "-Wno-implicit-int-float-conversion ..."
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
public static function getCflags(string $pkg_config_str): string
|
|
|
|
|
{
|
|
|
|
|
// get other things
|
|
|
|
|
$result = self::execWithResult("pkg-config --static --cflags-only-other {$pkg_config_str}");
|
|
|
|
|
return trim($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns --libs-only-l and --libs-only-other output.
|
|
|
|
|
* The reason we return the array is to avoid duplicate lib defines.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pkg_config_str .pc file str, accepts multiple files
|
|
|
|
|
* @return array Unique libs array, e.g. [-lz, -lxml, ...]
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2025-07-23 09:53:31 +07:00
|
|
|
public static function getLibsArray(string $pkg_config_str): array
|
2025-07-22 17:23:13 +08:00
|
|
|
{
|
|
|
|
|
// 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
|
2025-07-23 09:46:32 +07:00
|
|
|
$result = self::execWithResult("pkg-config --static --libs-only-other {$pkg_config_str}");
|
2025-07-22 17:23:13 +08:00
|
|
|
// convert libxxx.a to -L{path} -lxxx
|
|
|
|
|
$exp = explode(' ', trim($result));
|
|
|
|
|
foreach ($exp as $item) {
|
2025-07-23 09:46:32 +07:00
|
|
|
if (str_starts_with($item, '-L')) {
|
|
|
|
|
$libs[] = $item;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-07-22 17:23:13 +08:00
|
|
|
// if item ends with .a, convert it to -lxxx
|
2025-07-23 09:46:32 +07:00
|
|
|
if (str_ends_with($item, '.a') && (str_starts_with($item, 'lib') || str_starts_with($item, BUILD_LIB_PATH))) {
|
2025-07-22 17:23:13 +08:00
|
|
|
$name = pathinfo($item, PATHINFO_BASENAME);
|
|
|
|
|
$name = substr($name, 3, -2); // remove 'lib' prefix and '.a' suffix
|
2025-07-23 09:46:32 +07:00
|
|
|
$shortlib = "-l{$name}";
|
|
|
|
|
if (!in_array($shortlib, $libs)) {
|
|
|
|
|
$libs[] = $shortlib;
|
|
|
|
|
}
|
|
|
|
|
} elseif (!in_array($item, $libs)) {
|
2025-07-22 17:23:13 +08:00
|
|
|
$libs[] = $item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 22:25:21 +08:00
|
|
|
// enhancement for linker
|
|
|
|
|
return array_reverse(array_unique(array_reverse($libs)));
|
2025-07-22 17:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function execWithResult(string $cmd): string
|
|
|
|
|
{
|
|
|
|
|
f_exec($cmd, $output, $result_code);
|
|
|
|
|
if ($result_code !== 0) {
|
|
|
|
|
throw new RuntimeException("pkg-config command failed with code {$result_code}: {$cmd}");
|
|
|
|
|
}
|
|
|
|
|
return implode("\n", $output);
|
|
|
|
|
}
|
|
|
|
|
}
|