Add gnu based static binary support

This commit is contained in:
crazywhalecc
2025-01-28 19:37:50 +08:00
parent f19e90afd7
commit 75ee69b07d
19 changed files with 201 additions and 18 deletions

View File

@@ -15,6 +15,8 @@ use SPC\util\GlobalEnvManager;
class LinuxBuilder extends UnixBuilderBase
{
public string $libc;
/** @var bool Micro patch phar flag */
private bool $phar_patched = false;
@@ -25,13 +27,18 @@ class LinuxBuilder extends UnixBuilderBase
public function __construct(array $options = [])
{
$this->options = $options;
SystemUtil::initLibcVar($this->options['libc'] ?? null);
$this->libc = getenv('SPC_LIBC') ?: LIBC_MUSL_WRAPPER;
// check musl-cross make installed if we use musl-cross-make
$arch = arch2gnu(php_uname('m'));
// set library path, some libraries need it. (We cannot use `putenv` here, because cmake will be confused)
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=/usr/local/musl/{$arch}-linux-musl/lib");
$this->setOptionIfNotExist('ld_library_path', "LD_LIBRARY_PATH=/usr/local/musl/{$arch}-linux-musl/lib");
if ($this->libc !== LIBC_GLIBC) {
// set library path, some libraries need it. (We cannot use `putenv` here, because cmake will be confused)
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=/usr/local/musl/{$arch}-linux-musl/lib");
$this->setOptionIfNotExist('ld_library_path', "LD_LIBRARY_PATH=/usr/local/musl/{$arch}-linux-musl/lib");
}
GlobalEnvManager::init($this);

View File

@@ -182,4 +182,12 @@ class SystemUtil
'arch', 'manjaro',
];
}
public static function initLibcVar(?string $libc = null): void
{
if ($libc === null) {
$libc = self::isMuslDist() ? 'musl' : 'musl-wrapper';
}
f_putenv('SPC_LIBC=' . $libc);
}
}

View File

@@ -44,7 +44,8 @@ class libpng extends LinuxLibraryBase
shell()->cd($this->source_dir)
->exec('chmod +x ./configure')
->exec('chmod +x ./install-sh')
->exec(
->setEnv(['CFLAGS' => $this->getLibExtraCFlags() ?: $this->builder->arch_c_flags, 'LIBS' => $this->getLibExtraLibs()])
->execWithEnv(
'LDFLAGS="-L' . BUILD_LIB_PATH . '" ' .
'./configure ' .
'--disable-shared ' .
@@ -54,9 +55,9 @@ class libpng extends LinuxLibraryBase
$optimizations .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency} DEFAULT_INCLUDES='-I{$this->source_dir} -I" . BUILD_INCLUDE_PATH . "' LIBS= libpng16.la")
->exec('make install-libLTLIBRARIES install-data-am DESTDIR=' . BUILD_ROOT_PATH);
->execWithEnv('make clean')
->execWithEnv("make -j{$this->builder->concurrency} DEFAULT_INCLUDES='-I{$this->source_dir} -I" . BUILD_INCLUDE_PATH . "' LIBS= libpng16.la")
->execWithEnv('make install-libLTLIBRARIES install-data-am DESTDIR=' . BUILD_ROOT_PATH);
$this->patchPkgconfPrefix(['libpng16.pc'], PKGCONF_PATCH_PREFIX);
$this->cleanLaFiles();
}

View File

@@ -43,7 +43,7 @@ class openssl extends LinuxLibraryBase
$extra = '';
$ex_lib = '-ldl -pthread';
$env = "CC='" . getenv('CC') . ' -static -idirafter ' . BUILD_INCLUDE_PATH .
$env = "CC='" . getenv('CC') . ' -idirafter ' . BUILD_INCLUDE_PATH .
' -idirafter /usr/include/ ' .
' -idirafter /usr/include/' . $this->builder->getOption('arch') . '-linux-gnu/ ' .
"' ";
@@ -70,7 +70,6 @@ class openssl extends LinuxLibraryBase
'--prefix=/ ' .
'--libdir=lib ' .
'--openssldir=/etc/ssl ' .
'-static ' .
"{$zlib_extra}" .
'no-legacy ' .
"linux-{$this->builder->getOption('arch')}{$clang_postfix}"

View File

@@ -52,6 +52,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_EXE_LINKER_FLAGS "-ldl -lpthread -lm -lutil")
CMAKE;
// 有时候系统的 cmake 找不到 ar 命令,真奇怪
if (PHP_OS_FAMILY === 'Linux') {

View File

@@ -4,8 +4,16 @@ declare(strict_types=1);
namespace SPC\builder\unix\library;
use SPC\store\FileSystem;
trait bzip2
{
public function patchBeforeBuild(): bool
{
FileSystem::replaceFileStr($this->source_dir . '/Makefile', 'CFLAGS=-Wall', 'CFLAGS=-fPIC -Wall');
return true;
}
protected function build(): void
{
shell()->cd($this->source_dir)

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace SPC\builder\unix\library;
use SPC\builder\linux\library\LinuxLibraryBase;
use SPC\builder\linux\LinuxBuilder;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;
@@ -51,9 +53,11 @@ trait curl
$extra .= $this->builder->getLib('libcares') ? '-DENABLE_ARES=ON ' : '';
FileSystem::resetDir($this->source_dir . '/build');
$cflags = $this instanceof LinuxLibraryBase && $this->builder->libc === 'glibc' ? '-fPIC' : '';
// compile
shell()->cd($this->source_dir . '/build')
->setEnv(['CFLAGS' => $this->getLibExtraCFlags(), 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
->setEnv(['CFLAGS' => $this->getLibExtraCFlags() ?: $cflags, 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
->exec('sed -i.save s@\${CMAKE_C_IMPLICIT_LINK_LIBRARIES}@@ ../CMakeLists.txt')
->execWithEnv("cmake {$this->builder->makeCmakeArgs()} -DBUILD_SHARED_LIBS=OFF -DBUILD_CURL_EXE=OFF -DBUILD_LIBCURL_DOCS=OFF {$extra} ..")
->execWithEnv("make -j{$this->builder->concurrency}")

View File

@@ -16,7 +16,7 @@ trait gmp
protected function build(): void
{
shell()->cd($this->source_dir)
->setEnv(['CFLAGS' => $this->getLibExtraCFlags(), 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
->setEnv(['CFLAGS' => $this->getLibExtraCFlags() ?: $this->builder->arch_c_flags, 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
->execWithEnv(
'./configure ' .
'--enable-static --disable-shared ' .

View File

@@ -18,9 +18,10 @@ trait onig
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec('./configure --enable-static --disable-shared --prefix=')
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->setEnv(['CFLAGS' => $this->getLibExtraCFlags() ?: $this->builder->arch_c_flags, 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
->execWithEnv('./configure --enable-static --disable-shared --prefix=')
->execWithEnv('make clean')
->execWithEnv("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
$this->patchPkgconfPrefix(['oniguruma.pc']);
}

View File

@@ -4,12 +4,14 @@ declare(strict_types=1);
namespace SPC\builder\unix\library;
use SPC\builder\linux\library\LinuxLibraryBase;
trait pkgconfig
{
protected function build(): void
{
$cflags = PHP_OS_FAMILY !== 'Linux' ? "{$this->builder->arch_c_flags} -Wimplicit-function-declaration -Wno-int-conversion" : '';
$ldflags = PHP_OS_FAMILY !== 'Linux' ? '' : '--static';
$ldflags = !($this instanceof LinuxLibraryBase) || $this->builder->libc === 'glibc' ? '' : '--static';
shell()->cd($this->source_dir)
->setEnv(['CFLAGS' => $this->getLibExtraCFlags() ?: $cflags, 'LDFLAGS' => $this->getLibExtraLdFlags() ?: $ldflags, 'LIBS' => $this->getLibExtraLibs()])

View File

@@ -191,7 +191,7 @@ class BuildCliCommand extends BuildCommand
$fixed = '';
if (!empty(getenv('SPC_FIX_DEPLOY_ROOT'))) {
str_replace($cwd, '', $build_root_path);
$build_root_path = getenv('SPC_FIX_DEPLOY_ROOT') . $build_root_path;
$build_root_path = getenv('SPC_FIX_DEPLOY_ROOT') . '/' . basename($build_root_path);
$fixed = ' (host system)';
}
if (($rule & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {

View File

@@ -19,6 +19,8 @@ abstract class BuildCommand extends BaseCommand
$this->addOption('arch', null, InputOption::VALUE_REQUIRED, 'architecture, "x64" or "arm64"', 'x64');
break;
case 'Linux':
$this->addOption('libc', null, InputOption::VALUE_REQUIRED, 'glibc, musl or musl-wrapper', 'musl-wrapper');
// no break
case 'Darwin':
$this->addOption('cc', null, InputOption::VALUE_REQUIRED, 'C compiler');
$this->addOption('cxx', null, InputOption::VALUE_REQUIRED, 'C++ compiler');

View File

@@ -57,7 +57,8 @@ class LinuxToolCheckList
$required = match ($distro['dist']) {
'alpine' => self::TOOLS_ALPINE,
'redhat', 'centos' => self::TOOLS_RHEL,
'redhat' => self::TOOLS_RHEL,
'centos' => array_merge(self::TOOLS_RHEL, ['perl-IPC-Cmd']),
'arch' => self::TOOLS_ARCH,
default => self::TOOLS_DEBIAN,
};

View File

@@ -86,6 +86,9 @@ class SourcePatcher
}
// patch capstone
FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/configure', '/have_capstone="yes"/', 'have_capstone="no"');
if ($builder instanceof LinuxBuilder && $builder->libc === 'glibc') {
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/Zend/zend_operators.h', '# define ZEND_USE_ASM_ARITHMETIC 1', '# define ZEND_USE_ASM_ARITHMETIC 0');
}
}
/**

View File

@@ -70,6 +70,10 @@ class GlobalEnvManager
WORKING_DIR . '/config/env.ini',
ROOT_DIR . '/config/env.ini',
];
$ini_custom = [
WORKING_DIR . '/config/env.custom.ini',
ROOT_DIR . '/config/env.custom.ini',
];
$ini = null;
foreach ($ini_files as $ini_file) {
if (file_exists($ini_file)) {
@@ -83,6 +87,23 @@ class GlobalEnvManager
if ($ini === false || !isset($ini['global'])) {
throw new WrongUsageException('Failed to parse ' . $ini_file);
}
// apply custom env
foreach ($ini_custom as $ini_file) {
if (file_exists($ini_file)) {
$ini_custom = parse_ini_file($ini_file, true);
if ($ini_custom !== false) {
$ini['global'] = array_merge($ini['global'], $ini_custom['global'] ?? []);
match (PHP_OS_FAMILY) {
'Windows' => $ini['windows'] = array_merge($ini['windows'], $ini_custom['windows'] ?? []),
'Darwin' => $ini['macos'] = array_merge($ini['macos'], $ini_custom['macos'] ?? []),
'Linux' => $ini['linux'] = array_merge($ini['linux'], $ini_custom['linux'] ?? []),
'BSD' => $ini['freebsd'] = array_merge($ini['freebsd'], $ini_custom['freebsd'] ?? []),
default => null,
};
}
break;
}
}
self::applyConfig($ini['global']);
match (PHP_OS_FAMILY) {
'Windows' => self::applyConfig($ini['windows']),

View File

@@ -83,4 +83,9 @@ const AUTOCONF_CPPFLAGS = 4;
const AUTOCONF_LDFLAGS = 8;
const AUTOCONF_ALL = 15;
const LIBC_MUSL_WRAPPER = 'musl-wrapper';
const LIBC_MUSL = 'musl';
const LIBC_GLIBC = 'glibc';
ConsoleLogger::$date_format = 'H:i:s';
ConsoleLogger::$format = '[%date%] [I] %body%';

View File

@@ -4,4 +4,6 @@ declare(strict_types=1);
assert(function_exists('openssl_digest'));
assert(openssl_digest('123456', 'md5') === 'e10adc3949ba59abbe56e057f20f883e');
assert(file_get_contents('https://example.com/') !== false);
if (file_exists('/etc/ssl/openssl.cnf')) {
assert(file_get_contents('https://example.com/') !== false);
}