Skip to content

Commit 9edb941

Browse files
committed
add --with-frankenphp-app option to embed an app
# Conflicts: # config/lib.json # src/SPC/builder/unix/UnixBuilderBase.php
1 parent 987ad4b commit 9edb941

5 files changed

Lines changed: 75 additions & 9 deletions

File tree

config/env.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ SPC_SKIP_PHP_VERSION_CHECK="no"
4545
; Ignore some check item for bin/spc doctor command, comma separated (e.g. SPC_SKIP_DOCTOR_CHECK_ITEMS="if homebrew has installed")
4646
SPC_SKIP_DOCTOR_CHECK_ITEMS=""
4747
; extra modules that xcaddy will include in the FrankenPHP build
48-
SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES="--with github.com/dunglas/frankenphp/caddy --with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy --with github.com/dunglas/caddy-cbrotli"
48+
SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES="--with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy --with github.com/dunglas/caddy-cbrotli"
4949
; The display message for php version output (PHP >= 8.4 available)
5050
PHP_BUILD_PROVIDER="static-php-cli ${SPC_VERSION}"
5151

config/lib.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
"source": "php-src",
88
"lib-depends": [
99
"lib-base",
10-
"micro"
10+
"micro",
11+
"frankenphp"
1112
],
1213
"lib-depends-macos": [
1314
"lib-base",
1415
"micro",
15-
"libxml2"
16+
"libxml2",
17+
"frankenphp"
1618
],
1719
"lib-suggests-linux": [
1820
"libacl",
@@ -968,5 +970,9 @@
968970
"zstd.h",
969971
"zstd_errors.h"
970972
]
973+
},
974+
"frankenphp": {
975+
"source": "frankenphp",
976+
"type": "target"
971977
}
972978
}

config/source.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@
363363
"path": "LICENSE"
364364
}
365365
},
366+
"frankenphp": {
367+
"type": "ghtar",
368+
"repo": "php/frankenphp",
369+
"prefer-stable": true,
370+
"provide-pre-build": false,
371+
"license": {
372+
"type": "file",
373+
"path": "LICENSE"
374+
}
375+
},
366376
"icu-static-win": {
367377
"type": "url",
368378
"url": "https://dl.static-php.dev/static-php-cli/deps/icu-static-windows-x64/icu-static-windows-x64.zip",

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use SPC\store\Config;
1414
use SPC\store\FileSystem;
1515
use SPC\store\pkg\GoXcaddy;
16+
use SPC\store\SourceManager;
1617
use SPC\toolchain\GccNativeToolchain;
1718
use SPC\toolchain\ToolchainManager;
1819
use SPC\util\DependencyUtil;
@@ -344,22 +345,70 @@ protected function patchPhpScripts(): void
344345
}
345346
}
346347

348+
/**
349+
* Process the --with-frankenphp-app option
350+
* Creates app.tar and app.checksum in source/frankenphp directory
351+
*/
352+
protected function processFrankenphpApp(): void
353+
{
354+
$frankenphpSourceDir = SOURCE_PATH . '/frankenphp';
355+
SourceManager::initSource(['frankenphp'], ['frankenphp']);
356+
$frankenphpAppPath = $this->getOption('with-frankenphp-app');
357+
358+
if ($frankenphpAppPath) {
359+
if (!is_dir($frankenphpAppPath)) {
360+
throw new WrongUsageException("The path provided to --with-frankenphp-app is not a valid directory: {$frankenphpAppPath}");
361+
}
362+
$appTarPath = $frankenphpSourceDir . '/app.tar';
363+
logger()->info("Creating app.tar from {$frankenphpAppPath}");
364+
365+
shell()->exec('tar -cf ' . escapeshellarg($appTarPath) . ' -C ' . escapeshellarg($frankenphpAppPath) . ' .');
366+
367+
$checksum = hash_file('md5', $appTarPath);
368+
file_put_contents($frankenphpSourceDir . '/app_checksum.txt', $checksum);
369+
} else {
370+
FileSystem::removeFileIfExists($frankenphpSourceDir . '/app.tar');
371+
FileSystem::removeFileIfExists($frankenphpSourceDir . '/app_checksum.txt');
372+
file_put_contents($frankenphpSourceDir . '/app.tar', '');
373+
file_put_contents($frankenphpSourceDir . '/app_checksum.txt', '');
374+
}
375+
}
376+
377+
protected function getFrankenPHPVersion(): string
378+
{
379+
$goModPath = SOURCE_PATH . '/frankenphp/caddy/go.mod';
380+
381+
if (!file_exists($goModPath)) {
382+
throw new SPCInternalException("FrankenPHP caddy/go.mod file not found at {$goModPath}, why did we not download FrankenPHP?");
383+
}
384+
385+
$content = file_get_contents($goModPath);
386+
if (preg_match('/github\.com\/dunglas\/frankenphp\s+v?(\d+\.\d+\.\d+)/', $content, $matches)) {
387+
return $matches[1];
388+
}
389+
390+
throw new SPCInternalException('Could not find FrankenPHP version in caddy/go.mod');
391+
}
392+
347393
protected function buildFrankenphp(): void
348394
{
349395
GlobalEnvManager::addPathIfNotExists(GoXcaddy::getPath());
396+
$this->processFrankenphpApp();
350397
$nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : '';
351398
$nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : '';
352399
$xcaddyModules = getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES');
353-
// make it possible to build from a different frankenphp directory!
354-
if (!str_contains($xcaddyModules, '--with github.com/dunglas/frankenphp')) {
355-
$xcaddyModules = '--with github.com/dunglas/frankenphp ' . $xcaddyModules;
356-
}
400+
$frankenphpSourceDir = SOURCE_PATH . '/frankenphp';
401+
402+
$xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp(=\S+)?#', '', $xcaddyModules);
403+
$xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp/caddy(=\S+)?#', '', $xcaddyModules);
404+
$xcaddyModules = "--with github.com/dunglas/frankenphp={$frankenphpSourceDir} " .
405+
"--with github.com/dunglas/frankenphp/caddy={$frankenphpSourceDir}/caddy {$xcaddyModules}";
357406
if ($this->getLib('brotli') === null && str_contains($xcaddyModules, '--with github.com/dunglas/caddy-cbrotli')) {
358407
logger()->warning('caddy-cbrotli module is enabled, but brotli library is not built. Disabling caddy-cbrotli.');
359408
$xcaddyModules = str_replace('--with github.com/dunglas/caddy-cbrotli', '', $xcaddyModules);
360409
}
361-
[, $out] = shell()->execWithResult('go list -m github.com/dunglas/frankenphp@latest');
362-
$frankenPhpVersion = str_replace('github.com/dunglas/frankenphp v', '', $out[0]);
410+
411+
$frankenPhpVersion = $this->getFrankenPHPVersion();
363412
$libphpVersion = $this->getPHPVersion();
364413
$dynamic_exports = '';
365414
if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'shared') {

src/SPC/command/BuildPHPCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function configure(): void
4848
$this->addOption('with-upx-pack', null, null, 'Compress / pack binary using UPX tool (linux/windows only)');
4949
$this->addOption('with-micro-logo', null, InputOption::VALUE_REQUIRED, 'Use custom .ico for micro.sfx (windows only)');
5050
$this->addOption('enable-micro-win32', null, null, 'Enable win32 mode for phpmicro (Windows only)');
51+
$this->addOption('with-frankenphp-app', null, InputOption::VALUE_REQUIRED, 'Path to a folder to be embedded in FrankenPHP');
5152
}
5253

5354
public function handle(): int

0 commit comments

Comments
 (0)