137 lines
5.4 KiB
PHP
Raw Normal View History

2023-06-30 20:36:51 +08:00
<?php
declare(strict_types=1);
namespace SPC\builder\unix\library;
use SPC\builder\linux\library\LinuxLibraryBase;
2023-06-30 20:36:51 +08:00
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;
trait postgresql
{
/**
* @throws RuntimeException
* @throws FileSystemException
*/
protected function build(): void
2023-06-30 20:36:51 +08:00
{
$builddir = BUILD_ROOT_PATH;
$envs = '';
2023-12-15 00:34:22 +08:00
$packages = 'zlib openssl readline libxml-2.0';
$optional_packages = [
'zstd' => 'libzstd',
'ldap' => 'ldap',
'libxslt' => 'libxslt',
'icu' => 'icu-i18n',
];
$error_exec_cnt = 0;
2023-11-29 16:52:53 +08:00
foreach ($optional_packages as $lib => $pkg) {
2023-10-16 21:03:26 +02:00
if ($this->getBuilder()->getLib($lib)) {
$packages .= ' ' . $pkg;
2023-11-30 12:37:20 +08:00
$output = shell()->execWithResult("pkg-config --static {$pkg}");
$error_exec_cnt += $output[0] === 0 ? 0 : 1;
2023-11-30 12:53:19 +08:00
logger()->info(var_export($output[1], true));
2023-10-16 21:03:26 +02:00
}
}
2023-06-30 20:36:51 +08:00
$output = shell()->execWithResult("pkg-config --cflags-only-I --static {$packages}");
$error_exec_cnt += $output[0] === 0 ? 0 : 1;
2025-05-18 15:52:15 +07:00
$macos_15_bug_cflags = PHP_OS_FAMILY === 'Darwin' ? ' -Wno-unguarded-availability-new' : '';
$cflags = '';
2023-06-30 20:36:51 +08:00
if (!empty($output[1][0])) {
2025-05-18 15:52:15 +07:00
$cflags = $output[1][0];
$envs .= ' CPPFLAGS="-DPIC"';
$cflags = "{$cflags} -fno-ident{$macos_15_bug_cflags}";
2023-06-30 20:36:51 +08:00
}
$output = shell()->execWithResult("pkg-config --libs-only-L --static {$packages}");
$error_exec_cnt += $output[0] === 0 ? 0 : 1;
2023-06-30 20:36:51 +08:00
if (!empty($output[1][0])) {
$ldflags = $output[1][0];
$envs .= !($this instanceof LinuxLibraryBase) || getenv('SPC_LIBC') === 'glibc' ? " LDFLAGS=\"{$ldflags}\" " : " LDFLAGS=\"{$ldflags} -static\" ";
2023-06-30 20:36:51 +08:00
}
$output = shell()->execWithResult("pkg-config --libs-only-l --static {$packages}");
$error_exec_cnt += $output[0] === 0 ? 0 : 1;
2023-06-30 20:36:51 +08:00
if (!empty($output[1][0])) {
$libs = $output[1][0];
$libcpp = '';
if ($this->builder->getLib('icu')) {
$libcpp = $this instanceof LinuxLibraryBase ? ' -lstdc++' : ' -lc++';
}
$envs .= " LIBS=\"{$libs}{$libcpp}\" ";
2023-06-30 20:36:51 +08:00
}
if ($error_exec_cnt > 0) {
throw new RuntimeException('Failed to get pkg-config information!');
}
2023-06-30 20:36:51 +08:00
FileSystem::resetDir($this->source_dir . '/build');
$version = $this->getVersion();
// 16.1 workaround
if (version_compare($version, '16.1') >= 0) {
# 有静态链接配置 参考文件: src/interfaces/libpq/Makefile
shell()->cd($this->source_dir . '/build')
->exec('sed -i.backup "s/invokes exit\'; exit 1;/invokes exit\';/" ../src/interfaces/libpq/Makefile')
->exec('sed -i.backup "278 s/^/# /" ../src/Makefile.shlib')
->exec('sed -i.backup "402 s/^/# /" ../src/Makefile.shlib');
} else {
throw new RuntimeException('Unsupported version for postgresql: ' . $version . ' !');
}
2023-06-30 20:36:51 +08:00
$env = [
2025-05-18 15:52:15 +07:00
'CFLAGS' => $this->getLibExtraCFlags() . ' ' . $cflags,
'LDFLAGS' => $this->getLibExtraLdFlags(),
'LIBS' => $this->getLibExtraLibs(),
];
2023-07-22 16:12:12 +08:00
// configure
2023-06-30 20:36:51 +08:00
shell()->cd($this->source_dir . '/build')
->setEnv($env)
->execWithEnv(
2023-07-22 16:29:46 +08:00
"{$envs} ../configure " .
2023-07-22 16:12:12 +08:00
"--prefix={$builddir} " .
2025-06-07 20:50:54 +07:00
($this->builder->getOption('enable-zts') ? '--enable-thread-safety ' : '--disable-thread-safety ') .
2023-07-22 16:12:12 +08:00
'--enable-coverage=no ' .
'--with-ssl=openssl ' .
'--with-readline ' .
'--with-libxml ' .
($this->builder->getLib('icu') ? '--with-icu ' : '--without-icu ') .
($this->builder->getLib('ldap') ? '--with-ldap ' : '--without-ldap ') .
// '--without-ldap ' .
2023-10-16 21:03:26 +02:00
($this->builder->getLib('libxslt') ? '--with-libxslt ' : '--without-libxslt ') .
($this->builder->getLib('zstd') ? '--with-zstd ' : '--without-zstd ') .
2023-07-22 16:12:12 +08:00
'--without-lz4 ' .
'--without-perl ' .
'--without-python ' .
2023-10-31 13:09:01 +01:00
'--without-pam ' .
2023-07-22 16:12:12 +08:00
'--without-bonjour ' .
'--without-tcl '
)
->execWithEnv($envs . ' make -C src/bin/pg_config install')
->execWithEnv($envs . ' make -C src/include install')
->execWithEnv($envs . ' make -C src/common install')
->execWithEnv($envs . ' make -C src/port install')
->execWithEnv($envs . ' make -C src/interfaces/libpq install');
2023-07-22 16:12:12 +08:00
// remove dynamic libs
shell()->cd($this->source_dir . '/build')
->exec("rm -rf {$builddir}/lib/*.so.*")
->exec("rm -rf {$builddir}/lib/*.so")
->exec("rm -rf {$builddir}/lib/*.dylib");
2023-06-30 20:36:51 +08:00
}
private function getVersion(): string
{
try {
$file = FileSystem::readFile($this->source_dir . '/meson.build');
if (preg_match("/^\\s+version:\\s?'(.*)'/m", $file, $match)) {
return $match[1];
}
return 'unknown';
} catch (FileSystemException) {
return 'unknown';
}
}
2023-06-30 20:36:51 +08:00
}