From 085c1a159c0a6529af4c97c4bc40963f2c9e0687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 21 Aug 2023 09:30:46 +0200 Subject: [PATCH] feat: add support for libphp and the embed SAPI --- src/SPC/builder/BuilderBase.php | 3 ++ src/SPC/builder/linux/LinuxBuilder.php | 36 +++++++++++++++++++---- src/SPC/builder/macos/MacOSBuilder.php | 40 ++++++++++++++++++++------ src/SPC/command/BuildCliCommand.php | 7 +++-- src/globals/defines.php | 3 +- 5 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/SPC/builder/BuilderBase.php b/src/SPC/builder/BuilderBase.php index 6b49ca32..c0b66223 100644 --- a/src/SPC/builder/BuilderBase.php +++ b/src/SPC/builder/BuilderBase.php @@ -252,6 +252,9 @@ abstract class BuilderBase if (($type & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) { $ls[] = 'fpm'; } + if (($type & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) { + $ls[] = 'embed'; + } return implode(', ', $ls); } diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 431fe490..16533c65 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -179,6 +179,11 @@ class LinuxBuilder extends BuilderBase $zts = ''; } + $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 ' . @@ -189,12 +194,13 @@ class LinuxBuilder extends BuilderBase '--disable-all ' . '--disable-cgi ' . '--disable-phpdbg ' . - '--enable-cli ' . - '--enable-fpm ' . + ($enableCli ? '--enable-cli ' : '') . + ($enableFpm ? '--enable-fpm ' : '') . + ($enableEmbed ? '--enable-embed=static ' : '') . $json_74 . $zts . $maxExecutionTimers . - '--enable-micro=all-static ' . + ($enableMicro ? '--enable-micro=all-static ' : '') . $this->makeExtensionArgs() . ' ' . $envs ); @@ -203,18 +209,22 @@ class LinuxBuilder extends BuilderBase $this->cleanMake(); - if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) { + if ($enableCli) { logger()->info('building cli'); $this->buildCli($extra_libs, $use_lld); } - if (($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) { + if ($enableFpm) { logger()->info('building fpm'); $this->buildFpm($extra_libs, $use_lld); } - if (($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) { + if ($enableMicro) { logger()->info('building micro'); $this->buildMicro($extra_libs, $use_lld, $cflags); } + if ($enableEmbed) { + logger()->info('building embed'); + $this->buildEmbed($extra_libs, $use_lld); + } if (php_uname('m') === $this->getOption('arch')) { $this->sanityCheck($build_target); @@ -306,4 +316,18 @@ class LinuxBuilder extends BuilderBase $this->deployBinary(BUILD_TARGET_FPM); } + + public function buildEmbed(string $extra_libs, string $use_lld): void + { + $vars = SystemUtil::makeEnvVarString([ + 'EXTRA_CFLAGS' => '-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags)), + 'EXTRA_LIBS' => $extra_libs, + 'EXTRA_LDFLAGS_PROGRAM' => "{$use_lld} -all-static", + ]); + + shell() + ->cd(SOURCE_PATH . '/php-src') + ->exec('sed -i "s|//lib|/lib|g" Makefile') + ->exec('make INSTALL_ROOT=' . BUILD_ROOT_PATH . " -j{$this->concurrency} {$vars} install"); + } } diff --git a/src/SPC/builder/macos/MacOSBuilder.php b/src/SPC/builder/macos/MacOSBuilder.php index 0e8c98f0..c87c9d2c 100644 --- a/src/SPC/builder/macos/MacOSBuilder.php +++ b/src/SPC/builder/macos/MacOSBuilder.php @@ -151,6 +151,11 @@ class MacOSBuilder extends BuilderBase $json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : ''; $zts = $this->getOption('enable-zts', false) ? '--enable-zts --disable-zend-signals ' : ''; + $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 ' . @@ -162,9 +167,10 @@ class MacOSBuilder extends BuilderBase '--disable-all ' . '--disable-cgi ' . '--disable-phpdbg ' . - '--enable-cli ' . - '--enable-fpm ' . - '--enable-micro ' . + ($enableCli ? '--enable-cli ' : '') . + ($enableFpm ? '--enable-fpm ' : '') . + ($enableEmbed ? '--enable-embed=static ' : '') . + ($enableMicro ? '--enable-micro ' : '') . $json_74 . $zts . $this->makeExtensionArgs() . ' ' . @@ -175,18 +181,22 @@ class MacOSBuilder extends BuilderBase $this->cleanMake(); - if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) { + if ($enableCli) { logger()->info('building cli'); $this->buildCli(); } - if (($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) { + if ($enableFpm) { logger()->info('building fpm'); $this->buildFpm(); } - if (($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) { + if ($enableMicro) { logger()->info('building micro'); $this->buildMicro(); } + if ($enableEmbed) { + logger()->info('building embed'); + $this->buildEmbed(); + } if (php_uname('m') === $this->getOption('arch')) { $this->sanityCheck($build_target); @@ -207,7 +217,7 @@ class MacOSBuilder extends BuilderBase { $vars = SystemUtil::makeEnvVarString([ 'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size - 'EXTRA_LIBS' => "{$this->getOption('extra-libs')} -lresolv", // link resolv library (macOS need it) + 'EXTRA_LIBS' => "{$this->getOption('extra-libs')} -lresolv", // link resolv library (macOS needs it) ]); $shell = shell()->cd(SOURCE_PATH . '/php-src'); @@ -237,7 +247,7 @@ class MacOSBuilder extends BuilderBase $vars = [ // with debug information, optimize for size, remove identifiers, patch fake cli for micro 'EXTRA_CFLAGS' => '-g -Os -fno-ident' . $enable_fake_cli, - // link resolv library (macOS need it) + // link resolv library (macOS needs it) 'EXTRA_LIBS' => "{$this->getOption('extra-libs')} -lresolv", ]; if (!$this->getOption('no-strip', false)) { @@ -260,7 +270,7 @@ class MacOSBuilder extends BuilderBase { $vars = SystemUtil::makeEnvVarString([ 'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size - 'EXTRA_LIBS' => "{$this->getOption('extra-libs')} -lresolv", // link resolv library (macOS need it) + 'EXTRA_LIBS' => "{$this->getOption('extra-libs')} -lresolv", // link resolv library (macOS needs it) ]); $shell = shell()->cd(SOURCE_PATH . '/php-src'); @@ -270,4 +280,16 @@ class MacOSBuilder extends BuilderBase } $this->deployBinary(BUILD_TARGET_FPM); } + + public function buildEmbed(): void + { + $vars = SystemUtil::makeEnvVarString([ + 'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size + 'EXTRA_LIBS' => "{$this->getOption('extra-libs')} -lresolv", // link resolv library (macOS needs it) + ]); + + shell() + ->cd(SOURCE_PATH . '/php-src') + ->exec('make INSTALL_ROOT=' . BUILD_ROOT_PATH . " -j{$this->concurrency} {$vars} install"); + } } diff --git a/src/SPC/command/BuildCliCommand.php b/src/SPC/command/BuildCliCommand.php index 517f30d7..ee461793 100644 --- a/src/SPC/command/BuildCliCommand.php +++ b/src/SPC/command/BuildCliCommand.php @@ -25,7 +25,8 @@ class BuildCliCommand extends BuildCommand $this->addOption('build-micro', null, null, 'build micro'); $this->addOption('build-cli', null, null, 'build cli'); $this->addOption('build-fpm', null, null, 'build fpm'); - $this->addOption('build-all', null, null, 'build cli, micro, fpm'); + $this->addOption('build-embed', null, null, 'build embed'); + $this->addOption('build-all', null, null, 'build cli, micro, fpm, embed'); $this->addOption('no-strip', null, null, 'build without strip, in order to debug and load external extensions'); $this->addOption('enable-zts', null, null, 'enable ZTS support'); $this->addOption('with-hardcoded-ini', 'I', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Patch PHP source code, inject hardcoded INI'); @@ -43,13 +44,15 @@ class BuildCliCommand extends BuildCommand $rule = $rule | ($this->getOption('build-cli') ? BUILD_TARGET_CLI : BUILD_TARGET_NONE); $rule = $rule | ($this->getOption('build-micro') ? BUILD_TARGET_MICRO : BUILD_TARGET_NONE); $rule = $rule | ($this->getOption('build-fpm') ? BUILD_TARGET_FPM : BUILD_TARGET_NONE); + $rule = $rule | ($this->getOption('build-embed') ? BUILD_TARGET_EMBED : BUILD_TARGET_NONE); $rule = $rule | ($this->getOption('build-all') ? BUILD_TARGET_ALL : BUILD_TARGET_NONE); if ($rule === BUILD_TARGET_NONE) { $this->output->writeln('Please add at least one build target!'); $this->output->writeln("\t--build-cli\tBuild php-cli SAPI"); $this->output->writeln("\t--build-micro\tBuild phpmicro SAPI"); $this->output->writeln("\t--build-fpm\tBuild php-fpm SAPI"); - $this->output->writeln("\t--build-all\tBuild all SAPI: cli, micro, fpm"); + $this->output->writeln("\t--build-embed\tBuild embed SAPI/libphp"); + $this->output->writeln("\t--build-all\tBuild all SAPI: cli, micro, fpm, embed"); return static::FAILURE; } try { diff --git a/src/globals/defines.php b/src/globals/defines.php index 28473296..150b2753 100644 --- a/src/globals/defines.php +++ b/src/globals/defines.php @@ -43,7 +43,8 @@ const BUILD_TARGET_NONE = 0; // no target const BUILD_TARGET_CLI = 1; // build cli const BUILD_TARGET_MICRO = 2; // build micro const BUILD_TARGET_FPM = 4; // build fpm -const BUILD_TARGET_ALL = 7; // build all +const BUILD_TARGET_EMBED = 8; // build embed +const BUILD_TARGET_ALL = 15; // build all // doctor error fix policy const FIX_POLICY_DIE = 1; // die directly