From c4b9660cd7507d4f8ecd4c5fef5173f4b8ed4cf7 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Tue, 10 Dec 2024 23:08:01 +0800 Subject: [PATCH] Add embed sanity check --- src/SPC/builder/BuilderBase.php | 9 ++++++- src/SPC/builder/unix/UnixBuilderBase.php | 28 ++++++++++++++++++++++ src/SPC/builder/windows/WindowsBuilder.php | 1 + src/SPC/util/UnixShell.php | 3 +++ src/globals/common-tests/embed.c | 17 +++++++++++++ src/globals/common-tests/embed.php | 4 ++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/globals/common-tests/embed.c create mode 100644 src/globals/common-tests/embed.php diff --git a/src/SPC/builder/BuilderBase.php b/src/SPC/builder/BuilderBase.php index b61a10e5..0456af8b 100644 --- a/src/SPC/builder/BuilderBase.php +++ b/src/SPC/builder/BuilderBase.php @@ -25,6 +25,12 @@ abstract class BuilderBase /** @var array extensions */ protected array $exts = []; + /** @var array extension names */ + protected array $ext_list = []; + + /** @var array library names */ + protected array $lib_list = []; + /** @var bool compile libs only (just mark it) */ protected bool $libs_only = false; @@ -161,7 +167,7 @@ abstract class BuilderBase * @throws FileSystemException * @throws RuntimeException * @throws \ReflectionException - * @throws WrongUsageException + * @throws \Throwable|WrongUsageException * @internal */ public function proveExts(array $extensions, bool $skip_check_deps = false): void @@ -191,6 +197,7 @@ abstract class BuilderBase foreach ($this->exts as $ext) { $ext->checkDependency(); } + $this->ext_list = $extensions; } /** diff --git a/src/SPC/builder/unix/UnixBuilderBase.php b/src/SPC/builder/unix/UnixBuilderBase.php index 3ec21f06..347e1f16 100644 --- a/src/SPC/builder/unix/UnixBuilderBase.php +++ b/src/SPC/builder/unix/UnixBuilderBase.php @@ -12,6 +12,7 @@ use SPC\exception\WrongUsageException; use SPC\store\Config; use SPC\store\FileSystem; use SPC\util\DependencyUtil; +use SPC\util\SPCConfigUtil; abstract class UnixBuilderBase extends BuilderBase { @@ -128,6 +129,7 @@ abstract class UnixBuilderBase extends BuilderBase foreach ($this->libs as $lib) { $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)); + } + } } /** diff --git a/src/SPC/builder/windows/WindowsBuilder.php b/src/SPC/builder/windows/WindowsBuilder.php index bd391abb..46b8ac48 100644 --- a/src/SPC/builder/windows/WindowsBuilder.php +++ b/src/SPC/builder/windows/WindowsBuilder.php @@ -248,6 +248,7 @@ class WindowsBuilder extends BuilderBase foreach ($this->libs as $lib) { $lib->calcDependency(); } + $this->lib_list = $sorted_libraries; } /** diff --git a/src/SPC/util/UnixShell.php b/src/SPC/util/UnixShell.php index 16ae3f8c..44e97bfe 100644 --- a/src/SPC/util/UnixShell.php +++ b/src/SPC/util/UnixShell.php @@ -62,6 +62,9 @@ class UnixShell logger()->debug(ConsoleColor::blue('[EXEC] ') . ConsoleColor::gray($cmd)); } 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); return [$code, $out]; } diff --git a/src/globals/common-tests/embed.c b/src/globals/common-tests/embed.c new file mode 100644 index 00000000..85c29921 --- /dev/null +++ b/src/globals/common-tests/embed.c @@ -0,0 +1,17 @@ +#include + +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; +} \ No newline at end of file diff --git a/src/globals/common-tests/embed.php b/src/globals/common-tests/embed.php new file mode 100644 index 00000000..aa1d7e41 --- /dev/null +++ b/src/globals/common-tests/embed.php @@ -0,0 +1,4 @@ +