|
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; |
18 | | -use SPC\util\GlobalEnvManager; |
19 | 19 | use SPC\util\SPCConfigUtil; |
20 | 20 | use SPC\util\SPCTarget; |
21 | 21 |
|
@@ -134,8 +134,8 @@ protected function sanityCheck(int $build_target): void |
134 | 134 | if (($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) { |
135 | 135 | logger()->info('running embed sanity check'); |
136 | 136 | $sample_file_path = SOURCE_PATH . '/embed-test'; |
137 | | - if (!is_dir($sample_file_path)) { |
138 | | - @mkdir($sample_file_path); |
| 137 | + if (!is_dir($sample_file_path) && !mkdir($sample_file_path) && !is_dir($sample_file_path)) { |
| 138 | + throw new SPCInternalException("Failed to create directory: {$sample_file_path}"); |
139 | 139 | } |
140 | 140 | // copy embed test files |
141 | 141 | copy(ROOT_DIR . '/src/globals/common-tests/embed.c', $sample_file_path . '/embed.c'); |
@@ -264,22 +264,72 @@ protected function patchPhpScripts(): void |
264 | 264 | } |
265 | 265 | } |
266 | 266 |
|
| 267 | + /** |
| 268 | + * Process the --with-frankenphp-app option |
| 269 | + * Creates app.tar and app.checksum in source/frankenphp directory |
| 270 | + * |
| 271 | + * @return bool True if the app.tar was created successfully, false otherwise |
| 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 | + |
267 | 314 | protected function buildFrankenphp(): void |
268 | 315 | { |
269 | | - 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 | + // Check if we have a custom app.tar in source/frankenphp |
| 321 | + $frankenphpSourceDir = SOURCE_PATH . '/frankenphp'; |
| 322 | + |
| 323 | + $xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp(=\S+)?#', '', $xcaddyModules); |
| 324 | + $xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp/caddy(=\S+)?#', '', $xcaddyModules); |
| 325 | + $xcaddyModules = "--with github.com/dunglas/frankenphp={$frankenphpSourceDir} " . |
| 326 | + "--with github.com/dunglas/frankenphp/caddy={$frankenphpSourceDir}/caddy {$xcaddyModules}"; |
277 | 327 | if ($this->getLib('brotli') === null && str_contains($xcaddyModules, '--with github.com/dunglas/caddy-cbrotli')) { |
278 | 328 | logger()->warning('caddy-cbrotli module is enabled, but brotli library is not built. Disabling caddy-cbrotli.'); |
279 | 329 | $xcaddyModules = str_replace('--with github.com/dunglas/caddy-cbrotli', '', $xcaddyModules); |
280 | 330 | } |
281 | | - [, $out] = shell()->execWithResult('go list -m github.com/dunglas/frankenphp@latest'); |
282 | | - $frankenPhpVersion = str_replace('github.com/dunglas/frankenphp v', '', $out[0]); |
| 331 | + |
| 332 | + $frankenPhpVersion = $this->getFrankenPHPVersion(); |
283 | 333 | $libphpVersion = $this->getPHPVersion(); |
284 | 334 | $dynamic_exports = ''; |
285 | 335 | if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'shared') { |
@@ -317,7 +367,7 @@ protected function buildFrankenphp(): void |
317 | 367 | 'XCADDY_GO_BUILD_FLAGS' => '-buildmode=pie ' . |
318 | 368 | '-ldflags \"-linkmode=external ' . $extLdFlags . ' ' . $debugFlags . |
319 | 369 | '-X \'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ' . |
320 | | - "{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " . |
| 370 | + "v{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " . |
321 | 371 | "-tags={$muslTags}nobadger,nomysql,nopgx{$nobrotli}{$nowatcher}", |
322 | 372 | 'LD_LIBRARY_PATH' => BUILD_LIB_PATH, |
323 | 373 | ]; |
|
0 commit comments