Skip to content

Commit

Permalink
Put extension and library compatibility checks before compilation (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc authored May 16, 2024
1 parent 3e84bec commit 3136d6e
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 36 deletions.
37 changes: 35 additions & 2 deletions src/SPC/builder/BuilderBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,35 @@ abstract class BuilderBase
protected string $patch_point = '';

/**
* Build libraries
* Convert libraries to class
*
* @param array<string> $sorted_libraries Libraries to build (if not empty, must sort first)
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
* @internal
*/
abstract public function buildLibs(array $sorted_libraries);
abstract public function proveLibs(array $sorted_libraries);

/**
* Build libraries
*
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
*/
public function buildLibs(): void
{
// build all libs
foreach ($this->libs as $lib) {
match ($lib->tryBuild($this->getOption('rebuild', false))) {
BUILD_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] build success'),
BUILD_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
BUILD_STATUS_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
};
}
}

/**
* Add library to build.
Expand Down Expand Up @@ -335,6 +355,19 @@ public function getPatchPoint(): string
return $this->patch_point;
}

/**
* Validate libs and exts can be compiled successfully in current environment
*/
public function validateLibsAndExts(): void
{
foreach ($this->libs as $lib) {
$lib->validate();
}
foreach ($this->exts as $ext) {
$ext->validate();
}
}

public function emitPatchPoint(string $point_name): void
{
$this->patch_point = $point_name;
Expand Down
5 changes: 5 additions & 0 deletions src/SPC/builder/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ public function runCliCheckWindows(): void
}
}

public function validate(): void
{
// do nothing, just throw wrong usage exception if not valid
}

/**
* @throws RuntimeException
*/
Expand Down
5 changes: 5 additions & 0 deletions src/SPC/builder/LibraryBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public function patchBeforeBuild(): bool
return false;
}

public function validate(): void
{
// do nothing, just throw wrong usage exception if not valid
}

/**
* Get current builder object.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/SPC/builder/extension/imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class imap extends Extension
/**
* @throws WrongUsageException
*/
public function getUnixConfigureArg(): string
public function validate(): void
{
if ($this->builder->getOption('enable-zts')) {
throw new WrongUsageException('ext-imap is not thread safe, do not build it with ZTS builds');
}
}

public function getUnixConfigureArg(): string
{
$arg = '--with-imap=' . BUILD_ROOT_PATH;
if ($this->builder->getLib('openssl') !== null) {
$arg .= ' --with-imap-ssl=' . BUILD_ROOT_PATH;
Expand Down
6 changes: 5 additions & 1 deletion src/SPC/builder/extension/opcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ class opcache extends Extension
* @throws WrongUsageException
* @throws RuntimeException
*/
public function getUnixConfigureArg(): string
public function validate(): void
{
if ($this->builder->getPHPVersionID() < 80000) {
throw new WrongUsageException('Statically compiled PHP with Zend Opcache only available for PHP >= 8.0 !');
}
}

public function getUnixConfigureArg(): string
{
return '--enable-opcache';
}

Expand Down
3 changes: 1 addition & 2 deletions src/SPC/builder/extension/parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
#[CustomExt('parallel')]
class parallel extends Extension
{
public function getConfigureArg(): string
public function validate(): void
{
if (!$this->builder->getOption('enable-zts')) {
throw new WrongUsageException('ext-parallel must be built with ZTS builds. Use "--enable-zts" option!');
}
return parent::getConfigureArg(); // TODO: Change the autogenerated stub
}
}
6 changes: 5 additions & 1 deletion src/SPC/builder/extension/swoole_hook_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public function getDistName(): string
return 'swoole';
}

public function getUnixConfigureArg(): string
public function validate(): void
{
// pdo_pgsql need to be disabled
if ($this->builder->getExt('pdo_pgsql') !== null) {
throw new WrongUsageException('swoole-hook-pgsql provides pdo_pgsql, if you enable pgsql hook for swoole, you must remove pdo_pgsql extension.');
}
}

public function getUnixConfigureArg(): string
{
// enable swoole pgsql hook
return '--enable-swoole-pgsql';
}
Expand Down
6 changes: 5 additions & 1 deletion src/SPC/builder/extension/swoole_hook_sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public function getDistName(): string
return 'swoole';
}

public function getUnixConfigureArg(): string
public function validate(): void
{
// pdo_pgsql need to be disabled
if ($this->builder->getExt('pdo_sqlite') !== null) {
throw new WrongUsageException('swoole-hook-sqlite provides pdo_sqlite, if you enable sqlite hook for swoole, you must remove pdo_sqlite extension.');
}
}

public function getUnixConfigureArg(): string
{
// enable swoole pgsql hook
return '--enable-swoole-sqlite';
}
Expand Down
3 changes: 2 additions & 1 deletion src/SPC/builder/freebsd/BSDBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
SourcePatcher::patchBeforeConfigure($this);

$json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : '';
$zts = $this->getOption('enable-zts', false) ? '--enable-zts --disable-zend-signals ' : '';
$zts_enable = $this->getPHPVersionID() < 80000 ? '--enable-maintainer-zts --disable-zend-signals' : '--enable-zts --disable-zend-signals';
$zts = $this->getOption('enable-zts', false) ? $zts_enable : '';

$enableCli = ($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI;
$enableFpm = ($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM;
Expand Down
12 changes: 1 addition & 11 deletions src/SPC/builder/unix/UnixBuilderBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function makeAutoconfFlags(int $flag = AUTOCONF_ALL): string
return $extra;
}

public function buildLibs(array $sorted_libraries): void
public function proveLibs(array $sorted_libraries): void
{
// search all supported libs
$support_lib_list = [];
Expand Down Expand Up @@ -137,16 +137,6 @@ public function buildLibs(array $sorted_libraries): void
SourceManager::initSource(libs: $sorted_libraries);

$this->emitPatchPoint('after-libs-extract');

// build all libs
foreach ($this->libs as $lib) {
match ($lib->tryBuild($this->getOption('rebuild', false))) {
BUILD_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] build success'),
BUILD_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
BUILD_STATUS_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
};
}
}

/**
Expand Down
12 changes: 1 addition & 11 deletions src/SPC/builder/windows/WindowsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function buildMicro(): void
$this->deployBinary(BUILD_TARGET_MICRO);
}

public function buildLibs(array $sorted_libraries): void
public function proveLibs(array $sorted_libraries): void
{
// search all supported libs
$support_lib_list = [];
Expand Down Expand Up @@ -242,16 +242,6 @@ public function buildLibs(array $sorted_libraries): void

// extract sources
SourceManager::initSource(libs: $sorted_libraries);

// build all libs
foreach ($this->libs as $lib) {
match ($lib->tryBuild($this->getOption('rebuild', false))) {
BUILD_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] build success'),
BUILD_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
BUILD_STATUS_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
};
}
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/SPC/command/BuildCliCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public function handle(): int
if ($this->getOption('no-strip')) {
logger()->warning('--with-upx-pack conflicts with --no-strip, --no-strip won\'t work!');
}
if (($rule & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
logger()->warning('Some cases micro.sfx cannot be packed via UPX due to dynamic size bug, be aware!');
}
}
try {
// create builder
Expand Down Expand Up @@ -133,17 +136,23 @@ public function handle(): int
}
$this->printFormatInfo($this->getDefinedEnvs(), true);
$this->printFormatInfo($indent_texts);

logger()->notice('Build will start after 2s ...');
sleep(2);

// compile libraries
$builder->proveLibs($libraries);
// check extensions
$builder->proveExts($extensions);
// validate libs and exts
$builder->validateLibsAndExts();
// build libraries
$builder->buildLibs();

if ($this->input->getOption('with-clean')) {
logger()->info('Cleaning source dir...');
FileSystem::removeDir(SOURCE_PATH);
}
// compile libraries
$builder->buildLibs($libraries);
// check extensions
$builder->proveExts($extensions);

// Process -I option
$custom_ini = [];
Expand Down
4 changes: 3 additions & 1 deletion src/SPC/command/BuildLibsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function handle(): int
$libraries = DependencyUtil::getLibs($libraries);
logger()->info('Building libraries: ' . implode(',', $libraries));
sleep(2);
$builder->buildLibs($libraries);
$builder->proveLibs($libraries);
$builder->validateLibsAndExts();
$builder->buildLibs();

$time = round(microtime(true) - START_TIME, 3);
logger()->info('Build libs complete, used ' . $time . ' s !');
Expand Down

0 comments on commit 3136d6e

Please sign in to comment.