mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 09:55:37 +08:00
Add embed sanity check
This commit is contained in:
@@ -25,6 +25,12 @@ abstract class BuilderBase
|
|||||||
/** @var array<string, Extension> extensions */
|
/** @var array<string, Extension> extensions */
|
||||||
protected array $exts = [];
|
protected array $exts = [];
|
||||||
|
|
||||||
|
/** @var array<int, string> extension names */
|
||||||
|
protected array $ext_list = [];
|
||||||
|
|
||||||
|
/** @var array<int, string> library names */
|
||||||
|
protected array $lib_list = [];
|
||||||
|
|
||||||
/** @var bool compile libs only (just mark it) */
|
/** @var bool compile libs only (just mark it) */
|
||||||
protected bool $libs_only = false;
|
protected bool $libs_only = false;
|
||||||
|
|
||||||
@@ -161,7 +167,7 @@ abstract class BuilderBase
|
|||||||
* @throws FileSystemException
|
* @throws FileSystemException
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* @throws \ReflectionException
|
* @throws \ReflectionException
|
||||||
* @throws WrongUsageException
|
* @throws \Throwable|WrongUsageException
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public function proveExts(array $extensions, bool $skip_check_deps = false): void
|
public function proveExts(array $extensions, bool $skip_check_deps = false): void
|
||||||
@@ -191,6 +197,7 @@ abstract class BuilderBase
|
|||||||
foreach ($this->exts as $ext) {
|
foreach ($this->exts as $ext) {
|
||||||
$ext->checkDependency();
|
$ext->checkDependency();
|
||||||
}
|
}
|
||||||
|
$this->ext_list = $extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use SPC\exception\WrongUsageException;
|
|||||||
use SPC\store\Config;
|
use SPC\store\Config;
|
||||||
use SPC\store\FileSystem;
|
use SPC\store\FileSystem;
|
||||||
use SPC\util\DependencyUtil;
|
use SPC\util\DependencyUtil;
|
||||||
|
use SPC\util\SPCConfigUtil;
|
||||||
|
|
||||||
abstract class UnixBuilderBase extends BuilderBase
|
abstract class UnixBuilderBase extends BuilderBase
|
||||||
{
|
{
|
||||||
@@ -128,6 +129,7 @@ abstract class UnixBuilderBase extends BuilderBase
|
|||||||
foreach ($this->libs as $lib) {
|
foreach ($this->libs as $lib) {
|
||||||
$lib->calcDependency();
|
$lib->calcDependency();
|
||||||
}
|
}
|
||||||
|
$this->lib_list = $sorted_libraries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -170,6 +172,32 @@ abstract class UnixBuilderBase extends BuilderBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sanity check for embed
|
||||||
|
if (($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) {
|
||||||
|
logger()->info('running embed sanity check');
|
||||||
|
$sample_file_path = SOURCE_PATH . '/embed-test';
|
||||||
|
if (!is_dir($sample_file_path)) {
|
||||||
|
@mkdir($sample_file_path);
|
||||||
|
}
|
||||||
|
// copy embed test files
|
||||||
|
copy(ROOT_DIR . '/src/globals/common-tests/embed.c', $sample_file_path . '/embed.c');
|
||||||
|
copy(ROOT_DIR . '/src/globals/common-tests/embed.php', $sample_file_path . '/embed.php');
|
||||||
|
$util = new SPCConfigUtil($this);
|
||||||
|
$config = $util->config($this->ext_list, $this->lib_list, $this->getOption('with-suggested-exts'), $this->getOption('with-suggested-libs'));
|
||||||
|
$lens = "{$config['cflags']} {$config['ldflags']} {$config['libs']}";
|
||||||
|
if (PHP_OS_FAMILY === 'Linux') {
|
||||||
|
$lens .= ' -static';
|
||||||
|
}
|
||||||
|
[$ret, $out] = shell()->cd($sample_file_path)->execWithResult(getenv('CC') . ' -o embed embed.c ' . $lens);
|
||||||
|
if ($ret !== 0) {
|
||||||
|
throw new RuntimeException('embed failed sanity check: build failed. Error message: ' . implode("\n", $out));
|
||||||
|
}
|
||||||
|
[$ret, $output] = shell()->cd($sample_file_path)->execWithResult('./embed');
|
||||||
|
if ($ret !== 0 || trim(implode('', $output)) !== 'hello') {
|
||||||
|
throw new RuntimeException('embed failed sanity check: run failed. Error message: ' . implode("\n", $output));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -248,6 +248,7 @@ class WindowsBuilder extends BuilderBase
|
|||||||
foreach ($this->libs as $lib) {
|
foreach ($this->libs as $lib) {
|
||||||
$lib->calcDependency();
|
$lib->calcDependency();
|
||||||
}
|
}
|
||||||
|
$this->lib_list = $sorted_libraries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -62,6 +62,9 @@ class UnixShell
|
|||||||
logger()->debug(ConsoleColor::blue('[EXEC] ') . ConsoleColor::gray($cmd));
|
logger()->debug(ConsoleColor::blue('[EXEC] ') . ConsoleColor::gray($cmd));
|
||||||
}
|
}
|
||||||
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
|
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
|
||||||
|
if ($this->cd !== null) {
|
||||||
|
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
|
||||||
|
}
|
||||||
exec($cmd, $out, $code);
|
exec($cmd, $out, $code);
|
||||||
return [$code, $out];
|
return [$code, $out];
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/globals/common-tests/embed.c
Normal file
17
src/globals/common-tests/embed.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <sapi/embed/php_embed.h>
|
||||||
|
|
||||||
|
int main(int argc,char **argv){
|
||||||
|
|
||||||
|
PHP_EMBED_START_BLOCK(argc,argv)
|
||||||
|
|
||||||
|
zend_file_handle file_handle;
|
||||||
|
|
||||||
|
zend_stream_init_filename(&file_handle,"embed.php");
|
||||||
|
|
||||||
|
if(php_execute_script(&file_handle) == FAILURE){
|
||||||
|
php_printf("Failed to execute PHP script.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
PHP_EMBED_END_BLOCK()
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
4
src/globals/common-tests/embed.php
Normal file
4
src/globals/common-tests/embed.php
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
echo 'hello' . PHP_EOL;
|
||||||
Reference in New Issue
Block a user