2023-03-18 17:32:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\builder\traits;
|
|
|
|
|
|
2023-04-29 18:59:47 +08:00
|
|
|
use SPC\builder\linux\LinuxBuilder;
|
2023-03-26 22:27:51 +08:00
|
|
|
use SPC\exception\FileSystemException;
|
2023-03-18 17:32:21 +08:00
|
|
|
use SPC\exception\RuntimeException;
|
2023-08-20 19:51:45 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
2023-03-26 22:27:51 +08:00
|
|
|
use SPC\store\FileSystem;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
trait UnixBuilderTrait
|
|
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @var string cflags */
|
2023-03-18 17:32:21 +08:00
|
|
|
public string $arch_c_flags;
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @var string C++ flags */
|
2023-03-18 17:32:21 +08:00
|
|
|
public string $arch_cxx_flags;
|
|
|
|
|
|
|
|
|
|
/** @var string cmake toolchain file */
|
|
|
|
|
public string $cmake_toolchain_file;
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
2023-03-18 17:32:21 +08:00
|
|
|
public function getAllStaticLibFiles(): array
|
|
|
|
|
{
|
|
|
|
|
$libs = [];
|
|
|
|
|
|
|
|
|
|
// reorder libs
|
|
|
|
|
foreach ($this->libs as $lib) {
|
|
|
|
|
foreach ($lib->getDependencies() as $dep) {
|
|
|
|
|
$libs[] = $dep;
|
|
|
|
|
}
|
|
|
|
|
$libs[] = $lib;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$libFiles = [];
|
|
|
|
|
$libNames = [];
|
|
|
|
|
// merge libs
|
|
|
|
|
foreach ($libs as $lib) {
|
|
|
|
|
if (!in_array($lib::NAME, $libNames, true)) {
|
|
|
|
|
$libNames[] = $lib::NAME;
|
|
|
|
|
array_unshift($libFiles, ...$lib->getStaticLibs());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_map(fn ($x) => realpath(BUILD_LIB_PATH . "/{$x}"), $libFiles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-23 20:31:58 +08:00
|
|
|
* Sanity check after build complete
|
|
|
|
|
*
|
2023-03-18 17:32:21 +08:00
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-04-23 20:31:58 +08:00
|
|
|
public function sanityCheck(int $build_target): void
|
2023-03-18 17:32:21 +08:00
|
|
|
{
|
2023-04-23 20:31:58 +08:00
|
|
|
// sanity check for php-cli
|
|
|
|
|
if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
|
|
|
|
|
logger()->info('running cli sanity check');
|
2023-03-26 22:27:51 +08:00
|
|
|
[$ret, $output] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -r "echo \"hello\";"');
|
2023-03-18 17:32:21 +08:00
|
|
|
if ($ret !== 0 || trim(implode('', $output)) !== 'hello') {
|
|
|
|
|
throw new RuntimeException('cli failed sanity check');
|
|
|
|
|
}
|
2023-09-13 13:07:22 +02:00
|
|
|
|
2023-03-18 17:32:21 +08:00
|
|
|
foreach ($this->exts as $ext) {
|
2023-03-26 22:27:51 +08:00
|
|
|
logger()->debug('testing ext: ' . $ext->getName());
|
2023-11-01 01:49:48 +08:00
|
|
|
$ext->runCliCheck();
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-23 20:31:58 +08:00
|
|
|
|
|
|
|
|
// sanity check for phpmicro
|
|
|
|
|
if (($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
|
2023-03-18 17:32:21 +08:00
|
|
|
if (file_exists(SOURCE_PATH . '/hello.exe')) {
|
|
|
|
|
@unlink(SOURCE_PATH . '/hello.exe');
|
|
|
|
|
}
|
|
|
|
|
file_put_contents(
|
|
|
|
|
SOURCE_PATH . '/hello.exe',
|
|
|
|
|
file_get_contents(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx') .
|
|
|
|
|
'<?php echo "hello";'
|
|
|
|
|
);
|
|
|
|
|
chmod(SOURCE_PATH . '/hello.exe', 0755);
|
2023-03-26 22:27:51 +08:00
|
|
|
[$ret, $output2] = shell()->execWithResult(SOURCE_PATH . '/hello.exe');
|
2023-03-18 17:32:21 +08:00
|
|
|
if ($ret !== 0 || trim($out = implode('', $output2)) !== 'hello') {
|
|
|
|
|
throw new RuntimeException('micro failed sanity check, ret[' . $ret . '], out[' . ($out ?? 'NULL') . ']');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-26 22:27:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将编译好的二进制文件发布到 buildroot
|
|
|
|
|
*
|
|
|
|
|
* @param int $type 发布类型
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
public function deployBinary(int $type): bool
|
|
|
|
|
{
|
|
|
|
|
$src = match ($type) {
|
2023-04-23 20:31:58 +08:00
|
|
|
BUILD_TARGET_CLI => SOURCE_PATH . '/php-src/sapi/cli/php',
|
|
|
|
|
BUILD_TARGET_MICRO => SOURCE_PATH . '/php-src/sapi/micro/micro.sfx',
|
|
|
|
|
BUILD_TARGET_FPM => SOURCE_PATH . '/php-src/sapi/fpm/php-fpm',
|
2023-03-26 22:27:51 +08:00
|
|
|
default => throw new RuntimeException('Deployment does not accept type ' . $type),
|
|
|
|
|
};
|
2023-04-23 20:31:58 +08:00
|
|
|
logger()->info('Deploying ' . $this->getBuildTypeName($type) . ' file');
|
2023-03-26 22:27:51 +08:00
|
|
|
FileSystem::createDir(BUILD_ROOT_PATH . '/bin');
|
|
|
|
|
shell()->exec('cp ' . escapeshellarg($src) . ' ' . escapeshellarg(BUILD_ROOT_PATH . '/bin/'));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Run php clean
|
2023-03-26 22:27:51 +08:00
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
public function cleanMake(): void
|
|
|
|
|
{
|
|
|
|
|
logger()->info('cleaning up');
|
|
|
|
|
shell()->cd(SOURCE_PATH . '/php-src')->exec('make clean');
|
|
|
|
|
}
|
2023-04-29 18:59:47 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return generic cmake options when configuring cmake projects
|
|
|
|
|
*/
|
|
|
|
|
public function makeCmakeArgs(): string
|
|
|
|
|
{
|
2023-10-23 00:37:28 +08:00
|
|
|
$extra = $this instanceof LinuxBuilder ? '-DCMAKE_C_COMPILER=' . getenv('CC') . ' ' : '';
|
2023-10-16 19:26:29 +02:00
|
|
|
return $extra .
|
|
|
|
|
'-DCMAKE_BUILD_TYPE=Release ' .
|
2023-04-29 18:59:47 +08:00
|
|
|
'-DCMAKE_INSTALL_PREFIX=/ ' .
|
2023-10-16 19:26:29 +02:00
|
|
|
'-DCMAKE_INSTALL_BINDIR=/bin ' .
|
|
|
|
|
'-DCMAKE_INSTALL_LIBDIR=/lib ' .
|
|
|
|
|
'-DCMAKE_INSTALL_INCLUDEDIR=/include ' .
|
2023-04-29 18:59:47 +08:00
|
|
|
"-DCMAKE_TOOLCHAIN_FILE={$this->cmake_toolchain_file}";
|
|
|
|
|
}
|
2023-09-23 14:08:35 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate configure flags
|
|
|
|
|
*/
|
|
|
|
|
public function makeAutoconfFlags(int $flag = AUTOCONF_ALL): string
|
|
|
|
|
{
|
|
|
|
|
$extra = '';
|
|
|
|
|
// TODO: add auto pkg-config support
|
|
|
|
|
if (($flag & AUTOCONF_LIBS) === AUTOCONF_LIBS) {
|
|
|
|
|
$extra .= 'LIBS="' . BUILD_LIB_PATH . '" ';
|
|
|
|
|
}
|
|
|
|
|
if (($flag & AUTOCONF_CFLAGS) === AUTOCONF_CFLAGS) {
|
|
|
|
|
$extra .= 'CFLAGS="-I' . BUILD_INCLUDE_PATH . '" ';
|
|
|
|
|
}
|
|
|
|
|
if (($flag & AUTOCONF_CPPFLAGS) === AUTOCONF_CPPFLAGS) {
|
|
|
|
|
$extra .= 'CPPFLAGS="-I' . BUILD_INCLUDE_PATH . '" ';
|
|
|
|
|
}
|
|
|
|
|
if (($flag & AUTOCONF_LDFLAGS) === AUTOCONF_LDFLAGS) {
|
|
|
|
|
$extra .= 'LDFLAGS="-L' . BUILD_LIB_PATH . '" ';
|
|
|
|
|
}
|
|
|
|
|
return $extra;
|
|
|
|
|
}
|
2023-03-18 17:32:21 +08:00
|
|
|
}
|