Skip to content

Commit 4ae4165

Browse files
authored
add --with-frankenphp-app=/path/to/app option (#891)
2 parents 7402fbf + e441a57 commit 4ae4165

File tree

8 files changed

+176
-15
lines changed

8 files changed

+176
-15
lines changed

bin/spc-alpine-docker

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,47 @@ if [ ! -z "$GITHUB_TOKEN" ]; then
162162
ENV_LIST="$ENV_LIST -e GITHUB_TOKEN=$GITHUB_TOKEN"
163163
fi
164164

165+
# Intercept and rewrite --with-frankenphp-app option, and mount host path to /app/app
166+
FRANKENPHP_APP_PATH=""
167+
NEW_ARGS=()
168+
while [ $# -gt 0 ]; do
169+
case "$1" in
170+
--with-frankenphp-app=*)
171+
FRANKENPHP_APP_PATH="${1#*=}"
172+
NEW_ARGS+=("--with-frankenphp-app=/app/app")
173+
shift
174+
;;
175+
--with-frankenphp-app)
176+
if [ -n "${2:-}" ]; then
177+
FRANKENPHP_APP_PATH="$2"
178+
NEW_ARGS+=("--with-frankenphp-app=/app/app")
179+
shift 2
180+
else
181+
NEW_ARGS+=("$1")
182+
shift
183+
fi
184+
;;
185+
*)
186+
NEW_ARGS+=("$1")
187+
shift
188+
;;
189+
esac
190+
done
191+
192+
# Normalize the path and add mount if provided
193+
if [ -n "$FRANKENPHP_APP_PATH" ]; then
194+
# expand ~ to $HOME
195+
if [ "${FRANKENPHP_APP_PATH#~}" != "$FRANKENPHP_APP_PATH" ]; then
196+
FRANKENPHP_APP_PATH="$HOME${FRANKENPHP_APP_PATH#~}"
197+
fi
198+
# make absolute if relative
199+
case "$FRANKENPHP_APP_PATH" in
200+
/*) ABS_APP_PATH="$FRANKENPHP_APP_PATH" ;;
201+
*) ABS_APP_PATH="$(pwd)/$FRANKENPHP_APP_PATH" ;;
202+
esac
203+
MOUNT_LIST="$MOUNT_LIST -v $ABS_APP_PATH:/app/app"
204+
fi
205+
165206
# Run docker
166207
# shellcheck disable=SC2068
167208
# shellcheck disable=SC2086
@@ -183,5 +224,5 @@ if [ "$SPC_DOCKER_DEBUG" = "yes" ]; then
183224
set -ex
184225
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION /bin/bash
185226
else
186-
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc $@
227+
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc "${NEW_ARGS[@]}"
187228
fi

bin/spc-gnu-docker

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,47 @@ if [ ! -z "$GITHUB_TOKEN" ]; then
174174
ENV_LIST="$ENV_LIST -e GITHUB_TOKEN=$GITHUB_TOKEN"
175175
fi
176176

177+
# Intercept and rewrite --with-frankenphp-app option, and mount host path to /app/app
178+
FRANKENPHP_APP_PATH=""
179+
NEW_ARGS=()
180+
while [ $# -gt 0 ]; do
181+
case "$1" in
182+
--with-frankenphp-app=*)
183+
FRANKENPHP_APP_PATH="${1#*=}"
184+
NEW_ARGS+=("--with-frankenphp-app=/app/app")
185+
shift
186+
;;
187+
--with-frankenphp-app)
188+
if [ -n "${2:-}" ]; then
189+
FRANKENPHP_APP_PATH="$2"
190+
NEW_ARGS+=("--with-frankenphp-app=/app/app")
191+
shift 2
192+
else
193+
NEW_ARGS+=("$1")
194+
shift
195+
fi
196+
;;
197+
*)
198+
NEW_ARGS+=("$1")
199+
shift
200+
;;
201+
esac
202+
done
203+
204+
# Normalize the path and add mount if provided
205+
if [ -n "$FRANKENPHP_APP_PATH" ]; then
206+
# expand ~ to $HOME
207+
if [ "${FRANKENPHP_APP_PATH#~}" != "$FRANKENPHP_APP_PATH" ]; then
208+
FRANKENPHP_APP_PATH="$HOME${FRANKENPHP_APP_PATH#~}"
209+
fi
210+
# make absolute if relative
211+
case "$FRANKENPHP_APP_PATH" in
212+
/*) ABS_APP_PATH="$FRANKENPHP_APP_PATH" ;;
213+
*) ABS_APP_PATH="$(pwd)/$FRANKENPHP_APP_PATH" ;;
214+
esac
215+
MOUNT_LIST="$MOUNT_LIST -v $ABS_APP_PATH:/app/app"
216+
fi
217+
177218
# Run docker
178219
# shellcheck disable=SC2068
179220
# shellcheck disable=SC2086
@@ -196,5 +237,5 @@ if [ "$SPC_DOCKER_DEBUG" = "yes" ]; then
196237
set -ex
197238
$DOCKER_EXECUTABLE run $PLATFORM_ARG --privileged --rm -it $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION /bin/bash
198239
else
199-
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc $@
240+
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc "${NEW_ARGS[@]}"
200241
fi

config/env.ini

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

config/lib.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"source": "php-src",
88
"lib-depends": [
99
"lib-base",
10-
"micro"
10+
"micro",
11+
"frankenphp"
1112
],
1213
"lib-depends-macos": [
1314
"lib-base",
@@ -968,5 +969,24 @@
968969
"zstd.h",
969970
"zstd_errors.h"
970971
]
972+
},
973+
"liburing": {
974+
"source": "liburing",
975+
"pkg-configs": [
976+
"liburing",
977+
"liburing-ffi"
978+
],
979+
"static-libs-linux": [
980+
"liburing.a",
981+
"liburing-ffi.a"
982+
],
983+
"headers-linux": [
984+
"liburing/",
985+
"liburing.h"
986+
]
987+
},
988+
"frankenphp": {
989+
"source": "frankenphp",
990+
"type": "target"
971991
}
972992
}

config/source.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@
302302
}
303303
},
304304
"freetype": {
305-
"type": "git",
306-
"rev": "VER-2-13-2",
307-
"url": "https://github.com/freetype/freetype",
305+
"type": "ghtagtar",
306+
"repo": "freetype/freetype",
307+
"match": "VER-2-\\d+-\\d+",
308308
"license": {
309309
"type": "file",
310310
"path": "LICENSE.TXT"
@@ -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 & 7 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;
@@ -265,22 +266,69 @@ protected function patchPhpScripts(): void
265266
}
266267
}
267268

269+
/**
270+
* Process the --with-frankenphp-app option
271+
* Creates app.tar and app.checksum in source/frankenphp directory
272+
*/
273+
protected function processFrankenphpApp(): void
274+
{
275+
$frankenphpSourceDir = SOURCE_PATH . '/frankenphp';
276+
SourceManager::initSource(['frankenphp'], ['frankenphp']);
277+
$frankenphpAppPath = $this->getOption('with-frankenphp-app');
278+
279+
if ($frankenphpAppPath) {
280+
if (!is_dir($frankenphpAppPath)) {
281+
throw new WrongUsageException("The path provided to --with-frankenphp-app is not a valid directory: {$frankenphpAppPath}");
282+
}
283+
$appTarPath = $frankenphpSourceDir . '/app.tar';
284+
logger()->info("Creating app.tar from {$frankenphpAppPath}");
285+
286+
shell()->exec('tar -cf ' . escapeshellarg($appTarPath) . ' -C ' . escapeshellarg($frankenphpAppPath) . ' .');
287+
288+
$checksum = hash_file('md5', $appTarPath);
289+
file_put_contents($frankenphpSourceDir . '/app_checksum.txt', $checksum);
290+
} else {
291+
FileSystem::removeFileIfExists($frankenphpSourceDir . '/app.tar');
292+
FileSystem::removeFileIfExists($frankenphpSourceDir . '/app_checksum.txt');
293+
file_put_contents($frankenphpSourceDir . '/app.tar', '');
294+
file_put_contents($frankenphpSourceDir . '/app_checksum.txt', '');
295+
}
296+
}
297+
298+
protected function getFrankenPHPVersion(): string
299+
{
300+
$goModPath = SOURCE_PATH . '/frankenphp/caddy/go.mod';
301+
302+
if (!file_exists($goModPath)) {
303+
throw new SPCInternalException("FrankenPHP caddy/go.mod file not found at {$goModPath}, why did we not download FrankenPHP?");
304+
}
305+
306+
$content = file_get_contents($goModPath);
307+
if (preg_match('/github\.com\/dunglas\/frankenphp\s+v?(\d+\.\d+\.\d+)/', $content, $matches)) {
308+
return $matches[1];
309+
}
310+
311+
throw new SPCInternalException('Could not find FrankenPHP version in caddy/go.mod');
312+
}
313+
268314
protected function buildFrankenphp(): void
269315
{
270316
GlobalEnvManager::addPathIfNotExists(GoXcaddy::getPath());
317+
$this->processFrankenphpApp();
271318
$nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : '';
272319
$nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : '';
273320
$xcaddyModules = getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES');
274-
// make it possible to build from a different frankenphp directory!
275-
if (!str_contains($xcaddyModules, '--with github.com/dunglas/frankenphp')) {
276-
$xcaddyModules = '--with github.com/dunglas/frankenphp ' . $xcaddyModules;
277-
}
321+
$frankenphpSourceDir = SOURCE_PATH . '/frankenphp';
322+
323+
$xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp\S*#', '', $xcaddyModules);
324+
$xcaddyModules = "--with github.com/dunglas/frankenphp={$frankenphpSourceDir} " .
325+
"--with github.com/dunglas/frankenphp/caddy={$frankenphpSourceDir}/caddy {$xcaddyModules}";
278326
if ($this->getLib('brotli') === null && str_contains($xcaddyModules, '--with github.com/dunglas/caddy-cbrotli')) {
279327
logger()->warning('caddy-cbrotli module is enabled, but brotli library is not built. Disabling caddy-cbrotli.');
280328
$xcaddyModules = str_replace('--with github.com/dunglas/caddy-cbrotli', '', $xcaddyModules);
281329
}
282-
[, $out] = shell()->execWithResult('go list -m github.com/dunglas/frankenphp@latest');
283-
$frankenPhpVersion = str_replace('github.com/dunglas/frankenphp v', '', $out[0]);
330+
331+
$frankenPhpVersion = $this->getFrankenPHPVersion();
284332
$libphpVersion = $this->getPHPVersion();
285333
$dynamic_exports = '';
286334
if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'shared') {
@@ -290,7 +338,7 @@ protected function buildFrankenphp(): void
290338
$dynamic_exports = ' ' . $dynamicSymbolsArgument;
291339
}
292340
}
293-
$debugFlags = $this->getOption('no-strip') ? '-w -s ' : '';
341+
$debugFlags = $this->getOption('no-strip') ? '' : '-w -s ';
294342
$extLdFlags = "-extldflags '-pie{$dynamic_exports} {$this->arch_ld_flags}'";
295343
$muslTags = '';
296344
$staticFlags = '';

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

src/SPC/store/source/PhpSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function fetch(bool $force = false, ?array $config = null, int $lock_as =
1616
{
1717
$major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.4';
1818
if ($major === '8.5') {
19-
Downloader::downloadSource('php-src', ['type' => 'url', 'url' => 'https://downloads.php.net/~daniels/php-8.5.0RC3.tar.xz'], $force);
19+
Downloader::downloadSource('php-src', ['type' => 'url', 'url' => 'https://downloads.php.net/~edorian/php-8.5.0RC4.tar.xz'], $force);
2020
} elseif ($major === 'git') {
2121
Downloader::downloadSource('php-src', ['type' => 'git', 'url' => 'https://github.com/php/php-src.git', 'rev' => 'master'], $force);
2222
} else {

0 commit comments

Comments
 (0)