|
12 | 12 | use SPC\store\Config; |
13 | 13 | use SPC\store\FileSystem; |
14 | 14 | use SPC\store\pkg\GoXcaddy; |
| 15 | +use SPC\store\SourceManager; |
15 | 16 | use SPC\toolchain\GccNativeToolchain; |
16 | 17 | use SPC\toolchain\ToolchainManager; |
17 | 18 | use SPC\util\DependencyUtil; |
@@ -264,22 +265,70 @@ protected function patchPhpScripts(): void |
264 | 265 | } |
265 | 266 | } |
266 | 267 |
|
| 268 | + /** |
| 269 | + * Process the --with-frankenphp-app option |
| 270 | + * Creates app.tar and app.checksum in source/frankenphp directory |
| 271 | + */ |
| 272 | + protected function processFrankenphpApp(): void |
| 273 | + { |
| 274 | + $frankenphpSourceDir = SOURCE_PATH . '/frankenphp'; |
| 275 | + SourceManager::initSource(['frankenphp'], ['frankenphp']); |
| 276 | + $frankenphpAppPath = $this->getOption('with-frankenphp-app'); |
| 277 | + |
| 278 | + if ($frankenphpAppPath) { |
| 279 | + if (!is_dir($frankenphpAppPath)) { |
| 280 | + throw new WrongUsageException("The path provided to --with-frankenphp-app is not a valid directory: {$frankenphpAppPath}"); |
| 281 | + } |
| 282 | + $appTarPath = $frankenphpSourceDir . '/app.tar'; |
| 283 | + logger()->info("Creating app.tar from {$frankenphpAppPath}"); |
| 284 | + |
| 285 | + shell()->exec('tar -cf ' . escapeshellarg($appTarPath) . ' -C ' . escapeshellarg($frankenphpAppPath) . ' .'); |
| 286 | + |
| 287 | + $checksum = hash_file('md5', $appTarPath); |
| 288 | + file_put_contents($frankenphpSourceDir . '/app_checksum.txt', $checksum); |
| 289 | + } else { |
| 290 | + FileSystem::removeFileIfExists($frankenphpSourceDir . '/app.tar'); |
| 291 | + FileSystem::removeFileIfExists($frankenphpSourceDir . '/app_checksum.txt'); |
| 292 | + file_put_contents($frankenphpSourceDir . '/app.tar', ''); |
| 293 | + file_put_contents($frankenphpSourceDir . '/app_checksum.txt', ''); |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + protected function getFrankenPHPVersion(): string |
| 298 | + { |
| 299 | + $goModPath = SOURCE_PATH . '/frankenphp/caddy/go.mod'; |
| 300 | + |
| 301 | + if (!file_exists($goModPath)) { |
| 302 | + throw new SPCInternalException("FrankenPHP caddy/go.mod file not found at {$goModPath}, why did we not download FrankenPHP?"); |
| 303 | + } |
| 304 | + |
| 305 | + $content = file_get_contents($goModPath); |
| 306 | + if (preg_match('/github\.com\/dunglas\/frankenphp\s+v?(\d+\.\d+\.\d+)/', $content, $matches)) { |
| 307 | + return $matches[1]; |
| 308 | + } |
| 309 | + |
| 310 | + throw new SPCInternalException('Could not find FrankenPHP version in caddy/go.mod'); |
| 311 | + } |
| 312 | + |
267 | 313 | protected function buildFrankenphp(): void |
268 | 314 | { |
269 | 315 | GlobalEnvManager::addPathIfNotExists(GoXcaddy::getEnvironment()['PATH']); |
| 316 | + $this->processFrankenphpApp(); |
270 | 317 | $nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : ''; |
271 | 318 | $nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : ''; |
272 | 319 | $xcaddyModules = getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES'); |
273 | | - // make it possible to build from a different frankenphp directory! |
274 | | - if (!str_contains($xcaddyModules, '--with github.com/dunglas/frankenphp')) { |
275 | | - $xcaddyModules = '--with github.com/dunglas/frankenphp ' . $xcaddyModules; |
276 | | - } |
| 320 | + $frankenphpSourceDir = SOURCE_PATH . '/frankenphp'; |
| 321 | + |
| 322 | + $xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp(=\S+)?#', '', $xcaddyModules); |
| 323 | + $xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp/caddy(=\S+)?#', '', $xcaddyModules); |
| 324 | + $xcaddyModules = "--with github.com/dunglas/frankenphp={$frankenphpSourceDir} " . |
| 325 | + "--with github.com/dunglas/frankenphp/caddy={$frankenphpSourceDir}/caddy {$xcaddyModules}"; |
277 | 326 | if ($this->getLib('brotli') === null && str_contains($xcaddyModules, '--with github.com/dunglas/caddy-cbrotli')) { |
278 | 327 | logger()->warning('caddy-cbrotli module is enabled, but brotli library is not built. Disabling caddy-cbrotli.'); |
279 | 328 | $xcaddyModules = str_replace('--with github.com/dunglas/caddy-cbrotli', '', $xcaddyModules); |
280 | 329 | } |
281 | | - [, $out] = shell()->execWithResult('go list -m github.com/dunglas/frankenphp@latest'); |
282 | | - $frankenPhpVersion = str_replace('github.com/dunglas/frankenphp v', '', $out[0]); |
| 330 | + |
| 331 | + $frankenPhpVersion = $this->getFrankenPHPVersion(); |
283 | 332 | $libphpVersion = $this->getPHPVersion(); |
284 | 333 | $dynamic_exports = ''; |
285 | 334 | if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'shared') { |
@@ -317,7 +366,7 @@ protected function buildFrankenphp(): void |
317 | 366 | 'XCADDY_GO_BUILD_FLAGS' => '-buildmode=pie ' . |
318 | 367 | '-ldflags \"-linkmode=external ' . $extLdFlags . ' ' . $debugFlags . |
319 | 368 | '-X \'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ' . |
320 | | - "{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " . |
| 369 | + "v{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " . |
321 | 370 | "-tags={$muslTags}nobadger,nomysql,nopgx{$nobrotli}{$nowatcher}", |
322 | 371 | 'LD_LIBRARY_PATH' => BUILD_LIB_PATH, |
323 | 372 | ]; |
|
0 commit comments