Skip to content

Commit c4b9660

Browse files
committed
Add embed sanity check
1 parent f433866 commit c4b9660

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

src/SPC/builder/BuilderBase.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ abstract class BuilderBase
2525
/** @var array<string, Extension> extensions */
2626
protected array $exts = [];
2727

28+
/** @var array<int, string> extension names */
29+
protected array $ext_list = [];
30+
31+
/** @var array<int, string> library names */
32+
protected array $lib_list = [];
33+
2834
/** @var bool compile libs only (just mark it) */
2935
protected bool $libs_only = false;
3036

@@ -161,7 +167,7 @@ public function setLibsOnly(bool $status = true): void
161167
* @throws FileSystemException
162168
* @throws RuntimeException
163169
* @throws \ReflectionException
164-
* @throws WrongUsageException
170+
* @throws \Throwable|WrongUsageException
165171
* @internal
166172
*/
167173
public function proveExts(array $extensions, bool $skip_check_deps = false): void
@@ -191,6 +197,7 @@ public function proveExts(array $extensions, bool $skip_check_deps = false): voi
191197
foreach ($this->exts as $ext) {
192198
$ext->checkDependency();
193199
}
200+
$this->ext_list = $extensions;
194201
}
195202

196203
/**

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SPC\store\Config;
1313
use SPC\store\FileSystem;
1414
use SPC\util\DependencyUtil;
15+
use SPC\util\SPCConfigUtil;
1516

1617
abstract class UnixBuilderBase extends BuilderBase
1718
{
@@ -128,6 +129,7 @@ public function proveLibs(array $sorted_libraries): void
128129
foreach ($this->libs as $lib) {
129130
$lib->calcDependency();
130131
}
132+
$this->lib_list = $sorted_libraries;
131133
}
132134

133135
/**
@@ -170,6 +172,32 @@ protected function sanityCheck(int $build_target): void
170172
}
171173
}
172174
}
175+
176+
// sanity check for embed
177+
if (($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) {
178+
logger()->info('running embed sanity check');
179+
$sample_file_path = SOURCE_PATH . '/embed-test';
180+
if (!is_dir($sample_file_path)) {
181+
@mkdir($sample_file_path);
182+
}
183+
// copy embed test files
184+
copy(ROOT_DIR . '/src/globals/common-tests/embed.c', $sample_file_path . '/embed.c');
185+
copy(ROOT_DIR . '/src/globals/common-tests/embed.php', $sample_file_path . '/embed.php');
186+
$util = new SPCConfigUtil($this);
187+
$config = $util->config($this->ext_list, $this->lib_list, $this->getOption('with-suggested-exts'), $this->getOption('with-suggested-libs'));
188+
$lens = "{$config['cflags']} {$config['ldflags']} {$config['libs']}";
189+
if (PHP_OS_FAMILY === 'Linux') {
190+
$lens .= ' -static';
191+
}
192+
[$ret, $out] = shell()->cd($sample_file_path)->execWithResult(getenv('CC') . ' -o embed embed.c ' . $lens);
193+
if ($ret !== 0) {
194+
throw new RuntimeException('embed failed sanity check: build failed. Error message: ' . implode("\n", $out));
195+
}
196+
[$ret, $output] = shell()->cd($sample_file_path)->execWithResult('./embed');
197+
if ($ret !== 0 || trim(implode('', $output)) !== 'hello') {
198+
throw new RuntimeException('embed failed sanity check: run failed. Error message: ' . implode("\n", $output));
199+
}
200+
}
173201
}
174202

175203
/**

src/SPC/builder/windows/WindowsBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public function proveLibs(array $sorted_libraries): void
248248
foreach ($this->libs as $lib) {
249249
$lib->calcDependency();
250250
}
251+
$this->lib_list = $sorted_libraries;
251252
}
252253

253254
/**

src/SPC/util/UnixShell.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function execWithResult(string $cmd, bool $with_log = true): array
6262
logger()->debug(ConsoleColor::blue('[EXEC] ') . ConsoleColor::gray($cmd));
6363
}
6464
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
65+
if ($this->cd !== null) {
66+
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
67+
}
6568
exec($cmd, $out, $code);
6669
return [$code, $out];
6770
}

src/globals/common-tests/embed.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <sapi/embed/php_embed.h>
2+
3+
int main(int argc,char **argv){
4+
5+
PHP_EMBED_START_BLOCK(argc,argv)
6+
7+
zend_file_handle file_handle;
8+
9+
zend_stream_init_filename(&file_handle,"embed.php");
10+
11+
if(php_execute_script(&file_handle) == FAILURE){
12+
php_printf("Failed to execute PHP script.\n");
13+
}
14+
15+
PHP_EMBED_END_BLOCK()
16+
return 0;
17+
}

src/globals/common-tests/embed.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
echo 'hello' . PHP_EOL;

0 commit comments

Comments
 (0)