1313use SPC \store \Config ;
1414use SPC \store \FileSystem ;
1515use SPC \store \pkg \GoXcaddy ;
16+ use SPC \store \SourceManager ;
1617use SPC \toolchain \GccNativeToolchain ;
1718use SPC \toolchain \ToolchainManager ;
1819use SPC \util \DependencyUtil ;
@@ -344,22 +345,69 @@ 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 = "--with github.com/dunglas/frankenphp= {$ frankenphpSourceDir } " .
404+ "--with github.com/dunglas/frankenphp/caddy= {$ frankenphpSourceDir }/caddy {$ xcaddyModules }" ;
357405 if ($ this ->getLib ('brotli ' ) === null && str_contains ($ xcaddyModules , '--with github.com/dunglas/caddy-cbrotli ' )) {
358406 logger ()->warning ('caddy-cbrotli module is enabled, but brotli library is not built. Disabling caddy-cbrotli. ' );
359407 $ xcaddyModules = str_replace ('--with github.com/dunglas/caddy-cbrotli ' , '' , $ xcaddyModules );
360408 }
361- [, $ out ] = shell ()-> execWithResult ( ' go list -m github.com/dunglas/frankenphp@latest ' );
362- $ frankenPhpVersion = str_replace ( ' github.com/dunglas/frankenphp v ' , '' , $ out [ 0 ] );
409+
410+ $ frankenPhpVersion = $ this -> getFrankenPHPVersion ( );
363411 $ libphpVersion = $ this ->getPHPVersion ();
364412 $ dynamic_exports = '' ;
365413 if (getenv ('SPC_CMD_VAR_PHP_EMBED_TYPE ' ) === 'shared ' ) {
@@ -369,7 +417,7 @@ protected function buildFrankenphp(): void
369417 $ dynamic_exports = ' ' . $ dynamicSymbolsArgument ;
370418 }
371419 }
372- $ debugFlags = $ this ->getOption ('no-strip ' ) ? '-w -s ' : ' ' ;
420+ $ debugFlags = $ this ->getOption ('no-strip ' ) ? '' : ' -w -s ' ;
373421 $ extLdFlags = "-extldflags '-pie {$ dynamic_exports } {$ this ->arch_ld_flags }' " ;
374422 $ muslTags = '' ;
375423 $ staticFlags = '' ;
0 commit comments