2023-10-15 13:07:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\builder\freebsd;
|
|
|
|
|
|
2024-01-10 21:08:25 +08:00
|
|
|
use SPC\builder\unix\UnixBuilderBase;
|
2023-10-15 13:07:13 +08:00
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
use SPC\exception\WrongUsageException;
|
|
|
|
|
use SPC\store\FileSystem;
|
|
|
|
|
use SPC\store\SourcePatcher;
|
|
|
|
|
|
2024-01-10 21:08:25 +08:00
|
|
|
class BSDBuilder extends UnixBuilderBase
|
2023-10-15 13:07:13 +08:00
|
|
|
{
|
|
|
|
|
/** @var bool Micro patch phar flag */
|
|
|
|
|
private bool $phar_patched = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $options = [])
|
|
|
|
|
{
|
|
|
|
|
$this->options = $options;
|
|
|
|
|
|
|
|
|
|
// ---------- set necessary options ----------
|
|
|
|
|
// set C Compiler (default: clang)
|
2023-10-23 00:37:28 +08:00
|
|
|
f_putenv('CC=' . $this->getOption('cc', 'clang'));
|
2024-05-16 10:51:31 +08:00
|
|
|
// set C++ Compiler (default: clang++)
|
2023-10-23 00:37:28 +08:00
|
|
|
f_putenv('CXX=' . $this->getOption('cxx', 'clang++'));
|
|
|
|
|
// set PATH
|
|
|
|
|
f_putenv('PATH=' . BUILD_ROOT_PATH . '/bin:' . getenv('PATH'));
|
|
|
|
|
// set PKG_CONFIG
|
|
|
|
|
f_putenv('PKG_CONFIG=' . BUILD_ROOT_PATH . '/bin/pkg-config');
|
|
|
|
|
// set PKG_CONFIG_PATH
|
|
|
|
|
f_putenv('PKG_CONFIG_PATH=' . BUILD_LIB_PATH . '/pkgconfig/');
|
|
|
|
|
|
2023-10-15 13:07:13 +08:00
|
|
|
// set arch (default: current)
|
|
|
|
|
$this->setOptionIfNotExist('arch', php_uname('m'));
|
|
|
|
|
$this->setOptionIfNotExist('gnu-arch', arch2gnu($this->getOption('arch')));
|
|
|
|
|
|
|
|
|
|
// ---------- set necessary compile environments ----------
|
|
|
|
|
// concurrency
|
|
|
|
|
$this->concurrency = SystemUtil::getCpuCount();
|
|
|
|
|
// cflags
|
|
|
|
|
$this->arch_c_flags = SystemUtil::getArchCFlags($this->getOption('arch'));
|
|
|
|
|
$this->arch_cxx_flags = SystemUtil::getArchCFlags($this->getOption('arch'));
|
|
|
|
|
|
|
|
|
|
// create pkgconfig and include dir (some libs cannot create them automatically)
|
|
|
|
|
f_mkdir(BUILD_LIB_PATH . '/pkgconfig', recursive: true);
|
|
|
|
|
f_mkdir(BUILD_INCLUDE_PATH, recursive: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Just start to build statically linked php binary
|
|
|
|
|
*
|
|
|
|
|
* @param int $build_target build target
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
*/
|
|
|
|
|
public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
|
|
|
|
|
{
|
|
|
|
|
// ---------- Update extra-libs ----------
|
|
|
|
|
$extra_libs = $this->getOption('extra-libs', '');
|
|
|
|
|
// add libc++, some extensions or libraries need it (C++ cannot be linked statically)
|
2023-10-23 00:37:28 +08:00
|
|
|
$extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCpp() ? '-lc++ ' : '');
|
2023-10-15 13:07:13 +08:00
|
|
|
if (!$this->getOption('bloat', false)) {
|
|
|
|
|
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', $this->getAllStaticLibFiles());
|
|
|
|
|
} else {
|
|
|
|
|
logger()->info('bloat linking');
|
|
|
|
|
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', array_map(fn ($x) => "-Wl,-force_load,{$x}", array_filter($this->getAllStaticLibFiles())));
|
|
|
|
|
}
|
|
|
|
|
$this->setOption('extra-libs', $extra_libs);
|
|
|
|
|
|
2024-01-03 15:57:05 +08:00
|
|
|
$this->emitPatchPoint('before-php-buildconf');
|
2023-10-15 13:07:13 +08:00
|
|
|
SourcePatcher::patchBeforeBuildconf($this);
|
|
|
|
|
|
|
|
|
|
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
|
|
|
|
|
|
2024-01-03 15:57:05 +08:00
|
|
|
$this->emitPatchPoint('before-php-configure');
|
2023-10-15 13:07:13 +08:00
|
|
|
SourcePatcher::patchBeforeConfigure($this);
|
|
|
|
|
|
|
|
|
|
$json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : '';
|
2024-05-16 13:01:11 +08:00
|
|
|
$zts_enable = $this->getPHPVersionID() < 80000 ? '--enable-maintainer-zts --disable-zend-signals' : '--enable-zts --disable-zend-signals';
|
|
|
|
|
$zts = $this->getOption('enable-zts', false) ? $zts_enable : '';
|
2023-10-15 13:07:13 +08:00
|
|
|
|
2024-10-18 14:46:00 +02:00
|
|
|
$config_file_path = $this->getOption('with-config-file-path', false) ?
|
|
|
|
|
('--with-config-file-path=' . $this->getOption('with-config-file-path') . ' ') : '';
|
|
|
|
|
$config_file_scan_dir = $this->getOption('with-config-file-scan-dir', false) ?
|
|
|
|
|
('--with-config-file-scan-dir=' . $this->getOption('with-config-file-scan-dir') . ' ') : '';
|
|
|
|
|
|
2023-10-15 13:07:13 +08:00
|
|
|
$enableCli = ($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI;
|
|
|
|
|
$enableFpm = ($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM;
|
|
|
|
|
$enableMicro = ($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO;
|
|
|
|
|
$enableEmbed = ($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED;
|
|
|
|
|
|
|
|
|
|
shell()->cd(SOURCE_PATH . '/php-src')
|
|
|
|
|
->exec(
|
|
|
|
|
'./configure ' .
|
|
|
|
|
'--prefix= ' .
|
|
|
|
|
'--with-valgrind=no ' . // Not detect memory leak
|
|
|
|
|
'--enable-shared=no ' .
|
|
|
|
|
'--enable-static=yes ' .
|
|
|
|
|
"CFLAGS='{$this->arch_c_flags} -Werror=unknown-warning-option' " .
|
|
|
|
|
'--disable-all ' .
|
|
|
|
|
'--disable-cgi ' .
|
|
|
|
|
'--disable-phpdbg ' .
|
|
|
|
|
($enableCli ? '--enable-cli ' : '--disable-cli ') .
|
|
|
|
|
($enableFpm ? '--enable-fpm ' : '--disable-fpm ') .
|
|
|
|
|
($enableEmbed ? '--enable-embed=static ' : '--disable-embed ') .
|
|
|
|
|
($enableMicro ? '--enable-micro ' : '--disable-micro ') .
|
2024-10-18 14:46:00 +02:00
|
|
|
$config_file_path .
|
|
|
|
|
$config_file_scan_dir .
|
2023-10-15 13:07:13 +08:00
|
|
|
$json_74 .
|
|
|
|
|
$zts .
|
2025-03-24 23:50:12 +08:00
|
|
|
$this->makeStaticExtensionArgs()
|
2023-10-15 13:07:13 +08:00
|
|
|
);
|
|
|
|
|
|
2024-01-03 15:57:05 +08:00
|
|
|
$this->emitPatchPoint('before-php-make');
|
2023-10-15 13:07:13 +08:00
|
|
|
SourcePatcher::patchBeforeMake($this);
|
|
|
|
|
|
|
|
|
|
$this->cleanMake();
|
|
|
|
|
|
|
|
|
|
if ($enableCli) {
|
|
|
|
|
logger()->info('building cli');
|
|
|
|
|
$this->buildCli();
|
|
|
|
|
}
|
|
|
|
|
if ($enableFpm) {
|
|
|
|
|
logger()->info('building fpm');
|
|
|
|
|
$this->buildFpm();
|
|
|
|
|
}
|
|
|
|
|
if ($enableMicro) {
|
|
|
|
|
logger()->info('building micro');
|
|
|
|
|
$this->buildMicro();
|
|
|
|
|
}
|
|
|
|
|
if ($enableEmbed) {
|
|
|
|
|
logger()->info('building embed');
|
|
|
|
|
if ($enableMicro) {
|
|
|
|
|
FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/Makefile', 'OVERALL_TARGET =', 'OVERALL_TARGET = libphp.la');
|
|
|
|
|
}
|
|
|
|
|
$this->buildEmbed();
|
|
|
|
|
}
|
2025-05-21 18:35:48 +07:00
|
|
|
}
|
2023-10-15 13:07:13 +08:00
|
|
|
|
2025-05-21 18:35:48 +07:00
|
|
|
public function testPHP(int $build_target = BUILD_TARGET_NONE)
|
|
|
|
|
{
|
2023-10-15 13:07:13 +08:00
|
|
|
if (php_uname('m') === $this->getOption('arch')) {
|
2024-01-03 15:57:05 +08:00
|
|
|
$this->emitPatchPoint('before-sanity-check');
|
2023-10-15 13:07:13 +08:00
|
|
|
$this->sanityCheck($build_target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build cli sapi
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
2024-01-10 11:10:40 +08:00
|
|
|
protected function buildCli(): void
|
2023-10-15 13:07:13 +08:00
|
|
|
{
|
|
|
|
|
$vars = SystemUtil::makeEnvVarString([
|
|
|
|
|
'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size
|
|
|
|
|
'EXTRA_LIBS' => "{$this->getOption('extra-libs')} /usr/lib/libm.a",
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$shell = shell()->cd(SOURCE_PATH . '/php-src');
|
|
|
|
|
$shell->exec('sed -ie "s|//lib|/lib|g" Makefile');
|
|
|
|
|
$shell->exec("make -j{$this->concurrency} {$vars} cli");
|
|
|
|
|
if (!$this->getOption('no-strip', false)) {
|
|
|
|
|
$shell->exec('strip sapi/cli/php');
|
|
|
|
|
}
|
|
|
|
|
$this->deployBinary(BUILD_TARGET_CLI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build phpmicro sapi
|
|
|
|
|
*
|
2023-10-23 00:37:28 +08:00
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws WrongUsageException
|
2023-10-15 13:07:13 +08:00
|
|
|
*/
|
2024-01-10 11:10:40 +08:00
|
|
|
protected function buildMicro(): void
|
2023-10-15 13:07:13 +08:00
|
|
|
{
|
|
|
|
|
if ($this->getPHPVersionID() < 80000) {
|
2023-10-23 00:37:28 +08:00
|
|
|
throw new WrongUsageException('phpmicro only support PHP >= 8.0!');
|
2023-10-15 13:07:13 +08:00
|
|
|
}
|
|
|
|
|
if ($this->getExt('phar')) {
|
|
|
|
|
$this->phar_patched = true;
|
2024-06-03 23:16:15 +08:00
|
|
|
SourcePatcher::patchMicroPhar($this->getPHPVersionID());
|
2023-10-15 13:07:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$enable_fake_cli = $this->getOption('with-micro-fake-cli', false) ? ' -DPHP_MICRO_FAKE_CLI' : '';
|
|
|
|
|
$vars = [
|
|
|
|
|
// with debug information, optimize for size, remove identifiers, patch fake cli for micro
|
|
|
|
|
'EXTRA_CFLAGS' => '-g -Os' . $enable_fake_cli,
|
|
|
|
|
// link resolv library (macOS needs it)
|
|
|
|
|
'EXTRA_LIBS' => "{$this->getOption('extra-libs')} /usr/lib/libm.a",
|
|
|
|
|
];
|
|
|
|
|
$vars = SystemUtil::makeEnvVarString($vars);
|
|
|
|
|
|
|
|
|
|
shell()->cd(SOURCE_PATH . '/php-src')
|
|
|
|
|
->exec("make -j{$this->concurrency} {$vars} micro");
|
2023-10-15 15:44:58 +08:00
|
|
|
|
|
|
|
|
if (!$this->getOption('no-strip', false)) {
|
|
|
|
|
shell()->cd(SOURCE_PATH . '/php-src/sapi/micro')->exec('strip --strip-all micro.sfx');
|
|
|
|
|
}
|
2023-10-15 13:07:13 +08:00
|
|
|
$this->deployBinary(BUILD_TARGET_MICRO);
|
|
|
|
|
|
|
|
|
|
if ($this->phar_patched) {
|
2024-06-03 23:16:15 +08:00
|
|
|
SourcePatcher::unpatchMicroPhar();
|
2023-10-15 13:07:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build fpm sapi
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
2024-01-10 11:10:40 +08:00
|
|
|
protected function buildFpm(): void
|
2023-10-15 13:07:13 +08:00
|
|
|
{
|
|
|
|
|
$vars = SystemUtil::makeEnvVarString([
|
|
|
|
|
'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size
|
|
|
|
|
'EXTRA_LIBS' => "{$this->getOption('extra-libs')} /usr/lib/libm.a", // link resolv library (macOS needs it)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$shell = shell()->cd(SOURCE_PATH . '/php-src');
|
|
|
|
|
$shell->exec("make -j{$this->concurrency} {$vars} fpm");
|
|
|
|
|
if (!$this->getOption('no-strip', false)) {
|
|
|
|
|
$shell->exec('strip sapi/fpm/php-fpm');
|
|
|
|
|
}
|
|
|
|
|
$this->deployBinary(BUILD_TARGET_FPM);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 00:37:28 +08:00
|
|
|
/**
|
|
|
|
|
* Build embed sapi
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2024-01-10 11:10:40 +08:00
|
|
|
protected function buildEmbed(): void
|
2023-10-15 13:07:13 +08:00
|
|
|
{
|
|
|
|
|
$vars = SystemUtil::makeEnvVarString([
|
|
|
|
|
'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size
|
|
|
|
|
'EXTRA_LIBS' => "{$this->getOption('extra-libs')} /usr/lib/libm.a", // link resolv library (macOS needs it)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
shell()
|
|
|
|
|
->cd(SOURCE_PATH . '/php-src')
|
|
|
|
|
->exec('make INSTALL_ROOT=' . BUILD_ROOT_PATH . " -j{$this->concurrency} {$vars} install")
|
|
|
|
|
// Workaround for https://github.com/php/php-src/issues/12082
|
|
|
|
|
->exec('rm -Rf ' . BUILD_ROOT_PATH . '/lib/php-o')
|
|
|
|
|
->exec('mkdir ' . BUILD_ROOT_PATH . '/lib/php-o')
|
|
|
|
|
->cd(BUILD_ROOT_PATH . '/lib/php-o')
|
|
|
|
|
->exec('ar x ' . BUILD_ROOT_PATH . '/lib/libphp.a')
|
|
|
|
|
->exec('rm ' . BUILD_ROOT_PATH . '/lib/libphp.a')
|
|
|
|
|
->exec('ar rcs ' . BUILD_ROOT_PATH . '/lib/libphp.a *.o')
|
|
|
|
|
->exec('rm -Rf ' . BUILD_ROOT_PATH . '/lib/php-o');
|
|
|
|
|
}
|
|
|
|
|
}
|