2025-06-09 19:32:31 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\util\executor;
|
|
|
|
|
|
|
|
|
|
use SPC\builder\freebsd\library\BSDLibraryBase;
|
|
|
|
|
use SPC\builder\linux\library\LinuxLibraryBase;
|
|
|
|
|
use SPC\builder\macos\library\MacOSLibraryBase;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
use SPC\util\UnixShell;
|
|
|
|
|
|
|
|
|
|
class UnixAutoconfExecutor extends Executor
|
|
|
|
|
{
|
|
|
|
|
protected ?UnixShell $shell = null;
|
|
|
|
|
|
|
|
|
|
protected array $configure_args = [];
|
|
|
|
|
|
2025-06-10 12:42:37 +08:00
|
|
|
protected array $ignore_args = [];
|
|
|
|
|
|
2025-06-09 19:32:31 +08:00
|
|
|
public function __construct(protected BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library)
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($library);
|
|
|
|
|
$this->initShell();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run ./configure
|
|
|
|
|
*/
|
|
|
|
|
public function configure(...$args): static
|
|
|
|
|
{
|
2025-06-10 12:42:37 +08:00
|
|
|
// remove all the ignored args
|
|
|
|
|
$args = array_merge($args, $this->getDefaultConfigureArgs(), $this->configure_args);
|
|
|
|
|
$args = array_diff($args, $this->ignore_args);
|
|
|
|
|
$configure_args = implode(' ', $args);
|
2025-06-09 19:32:31 +08:00
|
|
|
|
|
|
|
|
$this->shell->exec("./configure {$configure_args}");
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getConfigureArgsString(): string
|
|
|
|
|
{
|
|
|
|
|
return implode(' ', array_merge($this->getDefaultConfigureArgs(), $this->configure_args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run make
|
|
|
|
|
*
|
|
|
|
|
* @param string $target Build target
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
public function make(string $target = '', false|string $with_install = 'install', bool $with_clean = true, array $after_env_vars = []): static
|
|
|
|
|
{
|
|
|
|
|
if ($with_clean) {
|
|
|
|
|
$this->shell->exec('make clean');
|
|
|
|
|
}
|
|
|
|
|
$after_env_vars_str = $after_env_vars !== [] ? shell()->setEnv($after_env_vars)->getEnvString() : '';
|
|
|
|
|
$this->shell->exec("make -j{$this->library->getBuilder()->concurrency} {$target} {$after_env_vars_str}");
|
|
|
|
|
if ($with_install !== false) {
|
|
|
|
|
$this->shell->exec("make {$with_install}");
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exec(string $cmd): static
|
|
|
|
|
{
|
|
|
|
|
$this->shell->exec($cmd);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add optional library configuration.
|
|
|
|
|
* This method checks if a library is available and adds the corresponding arguments to the CMake configuration.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name library name to check
|
|
|
|
|
* @param \Closure|string $true_args arguments to use if the library is available (allow closure, returns string)
|
|
|
|
|
* @param string $false_args arguments to use if the library is not available
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function optionalLib(string $name, \Closure|string $true_args, string $false_args = ''): static
|
|
|
|
|
{
|
|
|
|
|
if ($get = $this->library->getBuilder()->getLib($name)) {
|
2025-06-10 15:37:54 +08:00
|
|
|
logger()->info("Building library [{$this->library->getName()}] with {$name} support");
|
2025-06-09 19:32:31 +08:00
|
|
|
$args = $true_args instanceof \Closure ? $true_args($get) : $true_args;
|
|
|
|
|
} else {
|
2025-06-10 15:37:54 +08:00
|
|
|
logger()->info("Building library [{$this->library->getName()}] without {$name} support");
|
2025-06-09 19:32:31 +08:00
|
|
|
$args = $false_args;
|
|
|
|
|
}
|
|
|
|
|
$this->addConfigureArgs($args);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add configure args.
|
|
|
|
|
*/
|
|
|
|
|
public function addConfigureArgs(...$args): static
|
|
|
|
|
{
|
|
|
|
|
$this->configure_args = [...$this->configure_args, ...$args];
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 12:42:37 +08:00
|
|
|
/**
|
2025-06-10 15:39:14 +08:00
|
|
|
* Remove some configure args, to bypass the configure option checking for some libs.
|
2025-06-10 12:42:37 +08:00
|
|
|
*/
|
2025-06-10 15:39:14 +08:00
|
|
|
public function removeConfigureArgs(...$args): static
|
2025-06-10 12:42:37 +08:00
|
|
|
{
|
|
|
|
|
$this->ignore_args = [...$this->ignore_args, ...$args];
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 19:32:31 +08:00
|
|
|
public function appendEnv(array $env): static
|
|
|
|
|
{
|
|
|
|
|
$this->shell->appendEnv($env);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the default autoconf ./configure arguments
|
|
|
|
|
*/
|
|
|
|
|
private function getDefaultConfigureArgs(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'--disable-shared',
|
|
|
|
|
'--enable-static',
|
|
|
|
|
"--prefix={$this->library->getBuildRootPath()}",
|
|
|
|
|
'--with-pic',
|
2025-06-09 19:54:46 +08:00
|
|
|
'--enable-pic',
|
2025-06-09 19:32:31 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize UnixShell class.
|
|
|
|
|
*/
|
|
|
|
|
private function initShell(): void
|
|
|
|
|
{
|
|
|
|
|
$this->shell = shell()->cd($this->library->getSourceDir())->initializeEnv($this->library)->appendEnv([
|
|
|
|
|
'CFLAGS' => "-I{$this->library->getIncludeDir()}",
|
|
|
|
|
'LDFLAGS' => "-L{$this->library->getLibDir()}",
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|