147 lines
5.7 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;
use SPC\exception\BuildFailureException;
2023-06-30 20:36:51 +08:00
use SPC\store\FileSystem;
2025-10-06 22:10:28 +02:00
use SPC\util\SPCTarget;
2023-06-30 20:36:51 +08:00
trait postgresql
{
2025-10-06 22:10:28 +02:00
public function patchBeforeBuild(): bool
{
if (SPCTarget::getLibcVersion() === '2.17' && GNU_ARCH === 'aarch64') {
2025-10-06 22:26:00 +02:00
FileSystem::replaceFileStr(
$this->source_dir . '/src/port/pg_popcount_aarch64.c',
'value & HWCAP_SVE',
'value & 0',
);
2025-10-06 22:10:28 +02:00
FileSystem::replaceFileStr(
$this->source_dir . '/src/port/pg_crc32c_armv8_choose.c',
'#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)',
'#if defined(__linux__) && !defined(HWCAP_CRC32)',
);
return true;
}
return false;
}
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 .= " LDFLAGS=\"{$ldflags}\" ";
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++';
}
2025-10-06 22:10:28 +02:00
$envs .= " LIBS=\"{$libs}{$libcpp}\" ";
2023-06-30 20:36:51 +08:00
}
if ($error_exec_cnt > 0) {
throw new BuildFailureException('Failed to get pkg-config information!');
}
2023-06-30 20:36:51 +08:00
FileSystem::resetDir($this->source_dir . '/build');
2025-10-06 20:06:37 +02:00
# 有静态链接配置 参考文件: 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');
// php source relies on the non-private encoding functions in libpgcommon.a
FileSystem::replaceFileStr(
$this->source_dir . '/src/common/Makefile',
'$(OBJS_FRONTEND): CPPFLAGS += -DUSE_PRIVATE_ENCODING_FUNCS',
2025-10-06 21:28:57 +02:00
'$(OBJS_FRONTEND): CPPFLAGS += -UUSE_PRIVATE_ENCODING_FUNCS -DFRONTEND',
2025-10-06 20:06:37 +02:00
);
2023-06-30 20:36:51 +08:00
$env = [
'CFLAGS' => $cflags,
];
if ($ldLibraryPath = getenv('SPC_LD_LIBRARY_PATH')) {
$env['LD_LIBRARY_PATH'] = $ldLibraryPath;
}
2023-07-22 16:12:12 +08:00
// configure
2025-10-06 22:43:03 +02:00
$shell = shell()->cd($this->source_dir . '/build')->initializeEnv($this)
->appendEnv($env)
2025-06-09 14:48:05 +07:00
->exec(
2023-07-22 16:29:46 +08:00
"{$envs} ../configure " .
2023-07-22 16:12:12 +08:00
"--prefix={$builddir} " .
'--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 ') .
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 '
2025-10-06 22:43:03 +02:00
);
if (SPCTarget::getTargetOS() === 'Darwin') {
FileSystem::replaceFileStr($this->source_dir . '/build/src/Makefile.global', '-lldap', '-lldap -llber');
}
$shell
2025-06-09 14:48:05 +07:00
->exec($envs . ' make -C src/bin/pg_config install')
->exec($envs . ' make -C src/include install')
->exec($envs . ' make -C src/common install')
->exec($envs . ' make -C src/port install')
->exec($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");
2025-06-07 23:00:26 +07:00
FileSystem::replaceFileStr(BUILD_LIB_PATH . '/pkgconfig/libpq.pc', '-lldap', '-lldap -llber');
2023-06-30 20:36:51 +08:00
}
}