-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to build standalone binary
- Loading branch information
Showing
6 changed files
with
288 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
namespace Castor\Console\Command; | ||
|
||
use Castor\PathHelper; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Component\Process\Process; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class CompileCommand extends Command | ||
{ | ||
private HttpClientInterface $httpClient; | ||
private Filesystem $fs; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->httpClient = HttpClient::create(); | ||
$this->fs = new Filesystem(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->getDefinition()->addOptions(RepackCommand::getOptions()); | ||
|
||
$this | ||
->setName('compile') | ||
->addOption('arch', null, InputOption::VALUE_REQUIRED, 'Target architecture for PHP compilation', 'x86_64', ['x86_64', 'aarch64']) | ||
->addOption('php-version', null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format', '8.2') | ||
->addOption('php-extensions', null, InputOption::VALUE_REQUIRED, 'PHP extensions required, in a comma-separated format. Defaults are the minimum required to run a basic "Hello World" task in Castor.', 'mbstring,phar,posix,tokenizer') | ||
->addOption('php-rebuild', null, InputOption::VALUE_NONE, 'Force PHP build compilation.') | ||
->setHidden(true) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$os = $input->getOption('os'); | ||
if ('windows' === $os) { | ||
throw new \RuntimeException('Standalone binary compilation is not yet supported for Windows.'); | ||
} | ||
|
||
$arch = $input->getOption('arch'); | ||
if (!\in_array($arch, ['x86_64', 'aarch64'])) { | ||
throw new \RuntimeException('Target architecture must be one of x86_64 or aarch64'); | ||
} | ||
|
||
$io = new SymfonyStyle($input, $output); | ||
|
||
$io->section('Executing the repack command to build your Castor app as a PHAR archive.'); | ||
$this->runRepackCommand($input, $output); | ||
|
||
$io->section('Compiling PHP and your Castor app PHAR archive into a standalone binary'); | ||
|
||
$spcBinaryPath = sys_get_temp_dir() . '/castor-php-static-compiler/spc'; | ||
$spcBinaryDir = \dirname($spcBinaryPath); | ||
|
||
if (!$this->fs->exists($spcBinaryDir)) { | ||
$io->text(sprintf('Creating directory "%s" as it does not exist.', $spcBinaryDir)); | ||
$this->fs->mkdir($spcBinaryDir, 0o755); | ||
} | ||
|
||
if (!$this->fs->exists($spcBinaryPath)) { | ||
$spcSourceUrl = sprintf('https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-%s-%s', $os, $arch); | ||
$io->text(sprintf('Downloading the static-php-cli (spc) tool from "%s" to "%s"', $spcSourceUrl, $spcBinaryPath)); | ||
$this->downloadSPC($spcSourceUrl, $spcBinaryPath, $output); | ||
$io->newLine(2); | ||
} else { | ||
$io->text(sprintf('Using the static-php-cli (spc) tool from "%s"', $spcBinaryPath)); | ||
} | ||
|
||
$forcePHPRebuild = $input->getOption('php-rebuild'); | ||
|
||
if (!$this->fs->exists($spcBinaryDir . '/buildroot/bin/micro.sfx') || $forcePHPRebuild) { | ||
$installSPCDepsProcess = new Process( | ||
command: [ | ||
$spcBinaryPath, 'doctor', | ||
'--auto-fix', | ||
], | ||
cwd: $spcBinaryDir, | ||
timeout: 5 * 60 | ||
); | ||
$io->text('Running command: ' . $installSPCDepsProcess->getCommandLine()); | ||
$installSPCDepsProcess->mustRun(fn ($type, $buffer) => print ($buffer)); | ||
|
||
$phpVersion = $input->getOption('php-version'); | ||
$phpExtensions = $input->getOption('php-extensions'); | ||
|
||
$downloadProcess = new Process( | ||
command: [ | ||
$spcBinaryPath, 'download', | ||
'--for-extensions=' . $phpExtensions, | ||
'--with-php=' . $phpVersion, | ||
], | ||
cwd: $spcBinaryDir, | ||
timeout: 5 * 60 | ||
); | ||
$io->text('Running command: ' . $downloadProcess->getCommandLine()); | ||
$downloadProcess->mustRun(fn ($type, $buffer) => print ($buffer)); | ||
|
||
$buildProcess = new Process( | ||
command: [ | ||
$spcBinaryPath, 'build', $phpExtensions, | ||
'--build-micro', | ||
'--arch=' . $arch, | ||
'-r', | ||
], | ||
cwd: $spcBinaryDir, | ||
timeout: 60 * 60 | ||
); | ||
$io->text('Running command: ' . $buildProcess->getCommandLine()); | ||
$buildProcess->mustRun(fn ($type, $buffer) => print ($buffer)); | ||
} | ||
|
||
// __DIR__ will be castor-app/vendor/jolicode/castor/src/Console/Command when running castor-app/vendor/bin/castor | ||
$castorAppSourceDir = PathHelper::realpath(__DIR__ . '/../../../../../../'); | ||
$appName = $input->getOption('app-name'); | ||
$pharFilePath = sprintf('%s/%s.%s.phar', $castorAppSourceDir, $appName, $os); | ||
|
||
$buildProcess = new Process( | ||
[ | ||
$spcBinaryPath, | ||
'micro:combine', $pharFilePath, | ||
'--output=' . $castorAppSourceDir . '/' . $appName, | ||
], | ||
cwd: $spcBinaryDir | ||
); | ||
|
||
$io->text('Running command: ' . $buildProcess->getCommandLine()); | ||
$buildProcess->mustRun(fn ($type, $buffer) => print ($buffer)); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function runRepackCommand(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$arguments = [ | ||
'command' => RepackCommand::COMMAND_NAME, | ||
]; | ||
|
||
foreach (RepackCommand::getOptions() as $option) { | ||
$arguments['--' . $option->getName()] = $input->getOption($option->getName()); | ||
} | ||
|
||
$commandInput = new ArrayInput($arguments); | ||
|
||
if (null === $this->getApplication()) { | ||
throw new \RuntimeException(sprintf('"%s::$application" must be set before running %s', Command::class, __FUNCTION__)); | ||
} | ||
|
||
$command = $this->getApplication()->find(RepackCommand::COMMAND_NAME); | ||
$command->run($commandInput, $output); | ||
} | ||
|
||
private function downloadSPC(string $spcSourceUrl, string $spcBinaryDestination, OutputInterface $output): void | ||
{ | ||
$response = $this->httpClient->request('GET', $spcSourceUrl); | ||
$contentLength = $response->getHeaders()['content-length'][0] ?? 0; | ||
|
||
$outputStream = fopen($spcBinaryDestination, 'w'); | ||
$progressBar = new ProgressBar($output, (int) $contentLength); | ||
|
||
if (false === $outputStream) { | ||
throw new \RuntimeException(sprintf('Failed to open file "%s" for writing', $spcBinaryDestination)); | ||
} | ||
|
||
foreach ($this->httpClient->stream($response) as $chunk) { | ||
fwrite($outputStream, $chunk->getContent()); | ||
$progressBar->advance(\strlen($chunk->getContent())); | ||
} | ||
|
||
fclose($outputStream); | ||
chmod($spcBinaryDestination, 0o755); | ||
|
||
$progressBar->finish(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Castor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Process\Process; | ||
|
||
class CompileCommandTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$castorAppDirPath = RepackCommandTest::setupRepackedCastorApp('castor-test-compile'); | ||
$castAppName = 'castor-compiled-app'; | ||
|
||
(new Process( | ||
[ | ||
'vendor/jolicode/castor/bin/castor', | ||
'compile', | ||
'--os', 'linux', | ||
'--app-name', $castAppName, | ||
'--php-extensions', 'mbstring,phar,posix,tokenizer', | ||
], | ||
cwd: $castorAppDirPath, | ||
timeout: 5 * 60) | ||
)->mustRun(); | ||
|
||
$binary = $castorAppDirPath . '/' . $castAppName; | ||
$this->assertFileExists($binary); | ||
|
||
(new Process([$binary], cwd: $castorAppDirPath))->mustRun(); | ||
|
||
$p = (new Process([$binary, 'hello'], cwd: $castorAppDirPath))->mustRun(); | ||
$this->assertSame('hello', $p->getOutput()); | ||
|
||
// Twice, because we want to be sure the phar is not corrupted after a | ||
// run | ||
$p = (new Process([$binary, 'hello'], cwd: $castorAppDirPath))->mustRun(); | ||
$this->assertSame('hello', $p->getOutput()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters