add frankenphp sapi

This commit is contained in:
DubbleClick
2025-06-18 10:48:09 +07:00
parent 57b22782d3
commit c1870af1b1
10 changed files with 151 additions and 3 deletions

View File

@@ -114,6 +114,7 @@ class LinuxBuilder extends UnixBuilderBase
$enable_fpm = ($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM;
$enable_micro = ($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO;
$enable_embed = ($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED;
$enable_frankenphp = ($build_target & BUILD_TARGET_FRANKENPHP) === BUILD_TARGET_FRANKENPHP;
$mimallocLibs = $this->getLib('mimalloc') !== null ? BUILD_LIB_PATH . '/mimalloc.o ' : '';
// prepare build php envs
@@ -175,6 +176,10 @@ class LinuxBuilder extends UnixBuilderBase
}
$this->buildEmbed();
}
if ($enable_frankenphp) {
logger()->info('building frankenphp');
$this->buildFrankenphp();
}
}
public function testPHP(int $build_target = BUILD_TARGET_NONE)

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace SPC\builder\traits;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
use SPC\store\Downloader;
use SPC\store\FileSystem;
trait UnixGoCheckTrait
{
private function checkGoAndXcaddy(): ?CheckResult
{
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
$goroot = getenv('GOROOT') ?: '/usr/local/go';
$goBin = "{$goroot}/bin";
$paths[] = $goBin;
if ($this->findCommand('go', $paths) === null) {
$this->installGo();
}
$gobin = getenv('GOBIN') ?: (getenv('HOME') . '/go/bin');
putenv("GOBIN={$gobin}");
$paths[] = $gobin;
if ($this->findCommand('xcaddy', $paths) === null) {
shell(true)->exec('go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest');
}
return CheckResult::ok();
}
private function installGo(): bool
{
$prefix = '';
if (get_current_user() !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user is not root, using sudo for running command');
}
$arch = php_uname('m');
$go_arch = match ($arch) {
'x86_64' => 'amd64',
'aarch64' => 'arm64',
default => $arch
};
$os = strtolower(PHP_OS_FAMILY);
$go_version = '1.24.4';
$go_filename = "go{$go_version}.{$os}-{$go_arch}.tar.gz";
$go_url = "https://go.dev/dl/{$go_filename}";
logger()->info("Downloading Go {$go_version} for {$go_arch}");
try {
// Download Go binary
Downloader::downloadFile('go', $go_url, $go_filename);
// Extract the tarball
FileSystem::extractSource('go', SPC_SOURCE_ARCHIVE, DOWNLOAD_PATH . "/{$go_filename}");
// Move to /usr/local/go
logger()->info('Installing Go to /usr/local/go');
shell()->exec("{$prefix}rm -rf /usr/local/go");
shell()->exec("{$prefix}mv " . SOURCE_PATH . '/go /usr/local/');
if (!str_contains(getenv('PATH'), '/usr/local/go/bin')) {
logger()->info('Adding Go to PATH');
shell()->exec("{$prefix}echo 'export PATH=\$PATH:/usr/local/go/bin' >> /etc/profile");
putenv('PATH=' . getenv('PATH') . ':/usr/local/go/bin');
}
logger()->info('Go has been installed successfully');
return true;
} catch (RuntimeException $e) {
logger()->error('Failed to install Go: ' . $e->getMessage());
return false;
}
}
}

View File

@@ -277,4 +277,34 @@ abstract class UnixBuilderBase extends BuilderBase
FileSystem::writeFile(BUILD_BIN_PATH . '/php-config', $php_config_str);
}
}
protected function buildFrankenphp(): void
{
$path = getenv('PATH');
$xcaddyPath = getenv('GOBIN') ?: (getenv('HOME') . '/go/bin');
if (!str_contains($path, $xcaddyPath)) {
$path = $path . ':' . $xcaddyPath;
}
$path = BUILD_BIN_PATH . ':' . $path;
f_putenv("PATH={$path}");
$brotliLibs = $this->getLib('brotli') !== null ? '-lbrotlienc -lbrotlidec -lbrotlicommon' : '';
$nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : '';
$nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : '';
$env = [
'CGO_ENABLED' => '1',
'CGO_CFLAGS' => '$(php-config --includes) -I$(php-config --include-dir)/..',
'CGO_LDFLAGS' => "$(php-config --ldflags) $(php-config --libs) {$brotliLibs} -lwatcher-c -lphp -Wl,-rpath=" . BUILD_LIB_PATH,
'XCADDY_GO_BUILD_FLAGS' => "-ldflags='-w -s' -tags=nobadger,nomysql,nopgx" . $nobrotli . $nowatcher,
];
shell()->cd(BUILD_BIN_PATH)
->setEnv($env)
->exec(
'xcaddy build ' .
'--output frankenphp ' .
'--with github.com/dunglas/frankenphp/caddy ' .
getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES')
);
}
}