2023-03-18 17:32:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\builder\traits;
|
|
|
|
|
|
|
|
|
|
use SPC\builder\LibraryBase;
|
|
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
2023-08-20 19:51:45 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
2023-03-21 01:52:40 +08:00
|
|
|
use SPC\store\FileSystem;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
trait UnixLibraryTrait
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws FileSystemException
|
2023-08-20 19:51:45 +08:00
|
|
|
* @throws WrongUsageException
|
2023-03-18 17:32:21 +08:00
|
|
|
*/
|
|
|
|
|
public function getStaticLibFiles(string $style = 'autoconf', bool $recursive = true): string
|
|
|
|
|
{
|
|
|
|
|
$libs = [$this];
|
|
|
|
|
if ($recursive) {
|
|
|
|
|
array_unshift($libs, ...array_values($this->getDependencies(recursive: true)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sep = match ($style) {
|
|
|
|
|
'autoconf' => ' ',
|
|
|
|
|
'cmake' => ';',
|
|
|
|
|
default => throw new RuntimeException('style only support autoconf and cmake'),
|
|
|
|
|
};
|
|
|
|
|
$ret = [];
|
|
|
|
|
/** @var LibraryBase $lib */
|
|
|
|
|
foreach ($libs as $lib) {
|
|
|
|
|
$libFiles = [];
|
|
|
|
|
foreach ($lib->getStaticLibs() as $name) {
|
2023-03-21 01:52:40 +08:00
|
|
|
$name = str_replace(' ', '\ ', FileSystem::convertPath(BUILD_LIB_PATH . "/{$name}"));
|
2023-03-18 17:32:21 +08:00
|
|
|
$name = str_replace('"', '\"', $name);
|
|
|
|
|
$libFiles[] = $name;
|
|
|
|
|
}
|
|
|
|
|
array_unshift($ret, implode($sep, $libFiles));
|
|
|
|
|
}
|
|
|
|
|
return implode($sep, $ret);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
*/
|
2024-03-20 21:50:05 +08:00
|
|
|
public function makeAutoconfEnv(?string $prefix = null): string
|
2023-03-18 17:32:21 +08:00
|
|
|
{
|
|
|
|
|
if ($prefix === null) {
|
|
|
|
|
$prefix = str_replace('-', '_', strtoupper(static::NAME));
|
|
|
|
|
}
|
|
|
|
|
return $prefix . '_CFLAGS="-I' . BUILD_INCLUDE_PATH . '" ' .
|
|
|
|
|
$prefix . '_LIBS="' . $this->getStaticLibFiles() . '"';
|
|
|
|
|
}
|
2023-04-29 18:59:47 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Patch pkgconfig file prefix
|
|
|
|
|
*
|
|
|
|
|
* @param array $files file list
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-09-12 00:09:49 +08:00
|
|
|
public function patchPkgconfPrefix(array $files, int $patch_option = PKGCONF_PATCH_ALL, ?array $custom_replace = null): void
|
2023-04-29 18:59:47 +08:00
|
|
|
{
|
|
|
|
|
logger()->info('Patching library [' . static::NAME . '] pkgconfig');
|
|
|
|
|
foreach ($files as $name) {
|
|
|
|
|
$realpath = realpath(BUILD_ROOT_PATH . '/lib/pkgconfig/' . $name);
|
|
|
|
|
if ($realpath === false) {
|
|
|
|
|
throw new RuntimeException('Cannot find library [' . static::NAME . '] pkgconfig file [' . $name . '] !');
|
|
|
|
|
}
|
|
|
|
|
logger()->debug('Patching ' . $realpath);
|
|
|
|
|
// replace prefix
|
|
|
|
|
$file = FileSystem::readFile($realpath);
|
2024-07-09 11:14:58 +08:00
|
|
|
$file = ($patch_option & PKGCONF_PATCH_PREFIX) === PKGCONF_PATCH_PREFIX ? preg_replace('/^prefix\s*=.*$/m', 'prefix=${pcfiledir}/../..', $file) : $file;
|
|
|
|
|
$file = ($patch_option & PKGCONF_PATCH_EXEC_PREFIX) === PKGCONF_PATCH_EXEC_PREFIX ? preg_replace('/^exec_prefix\s*=.*$/m', 'exec_prefix=${prefix}', $file) : $file;
|
|
|
|
|
$file = ($patch_option & PKGCONF_PATCH_LIBDIR) === PKGCONF_PATCH_LIBDIR ? preg_replace('/^libdir\s*=.*$/m', 'libdir=${prefix}/lib', $file) : $file;
|
|
|
|
|
$file = ($patch_option & PKGCONF_PATCH_INCLUDEDIR) === PKGCONF_PATCH_INCLUDEDIR ? preg_replace('/^includedir\s*=.*$/m', 'includedir=${prefix}/include', $file) : $file;
|
2023-09-12 00:09:49 +08:00
|
|
|
$file = ($patch_option & PKGCONF_PATCH_CUSTOM) === PKGCONF_PATCH_CUSTOM && $custom_replace !== null ? preg_replace($custom_replace[0], $custom_replace[1], $file) : $file;
|
2023-04-29 18:59:47 +08:00
|
|
|
FileSystem::writeFile($realpath, $file);
|
2025-05-20 22:19:09 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function patchLaDependencyPrefix(array $files): void
|
|
|
|
|
{
|
|
|
|
|
logger()->info('Patching library [' . static::NAME . '] la files');
|
|
|
|
|
foreach ($files as $name) {
|
|
|
|
|
$realpath = realpath(BUILD_LIB_PATH . '/' . $name);
|
|
|
|
|
if ($realpath === false) {
|
|
|
|
|
throw new RuntimeException('Cannot find library [' . static::NAME . '] la file [' . $name . '] !');
|
|
|
|
|
}
|
|
|
|
|
logger()->debug('Patching ' . $realpath);
|
|
|
|
|
// replace prefix
|
|
|
|
|
$file = FileSystem::readFile($realpath);
|
2025-05-21 17:57:53 +07:00
|
|
|
$file = str_replace(
|
2025-05-21 17:58:11 +07:00
|
|
|
' /lib/',
|
|
|
|
|
' ' . BUILD_LIB_PATH . '/',
|
|
|
|
|
$file
|
|
|
|
|
);
|
2025-05-21 14:29:49 +07:00
|
|
|
$file = preg_replace('/^libdir=.*$/m', "libdir='" . BUILD_LIB_PATH . "'", $file);
|
2025-05-20 22:19:09 +07:00
|
|
|
FileSystem::writeFile($realpath, $file);
|
2023-04-29 18:59:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-10 02:04:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* remove libtool archive files
|
|
|
|
|
*
|
|
|
|
|
* @throws FileSystemException
|
2023-08-20 19:51:45 +08:00
|
|
|
* @throws WrongUsageException
|
2023-05-10 02:04:08 +08:00
|
|
|
*/
|
|
|
|
|
public function cleanLaFiles(): void
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getStaticLibs() as $lib) {
|
|
|
|
|
$filename = pathinfo($lib, PATHINFO_FILENAME) . '.la';
|
|
|
|
|
if (file_exists(BUILD_LIB_PATH . '/' . $filename)) {
|
|
|
|
|
unlink(BUILD_LIB_PATH . '/' . $filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 15:52:24 +08:00
|
|
|
|
|
|
|
|
public function getLibExtraCFlags(): string
|
|
|
|
|
{
|
2025-03-10 00:39:20 +08:00
|
|
|
$env = getenv($this->getSnakeCaseName() . '_CFLAGS') ?: '';
|
|
|
|
|
if (!str_contains($env, $this->builder->arch_c_flags)) {
|
|
|
|
|
$env .= $this->builder->arch_c_flags;
|
|
|
|
|
}
|
|
|
|
|
return $env;
|
2024-04-07 15:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getLibExtraLdFlags(): string
|
|
|
|
|
{
|
|
|
|
|
return getenv($this->getSnakeCaseName() . '_LDFLAGS') ?: '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getLibExtraLibs(): string
|
|
|
|
|
{
|
|
|
|
|
return getenv($this->getSnakeCaseName() . '_LIBS') ?: '';
|
|
|
|
|
}
|
2025-06-09 11:13:54 +08:00
|
|
|
|
|
|
|
|
public function getLibExtraCXXFlags(): string
|
|
|
|
|
{
|
|
|
|
|
$env = getenv($this->getSnakeCaseName() . '_CXXFLAGS') ?: '';
|
|
|
|
|
if (!str_contains($env, $this->builder->arch_cxx_flags)) {
|
|
|
|
|
$env .= $this->builder->arch_cxx_flags;
|
|
|
|
|
}
|
|
|
|
|
return $env;
|
|
|
|
|
}
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|