Extract default build env to unix shell

This commit is contained in:
crazywhalecc
2025-06-09 10:24:06 +08:00
parent 9babe7f1d2
commit 123cc92756
29 changed files with 166 additions and 196 deletions

View File

@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace SPC\util;
use SPC\builder\freebsd\library\BSDLibraryBase;
use SPC\builder\linux\library\LinuxLibraryBase;
use SPC\builder\macos\library\MacOSLibraryBase;
use SPC\exception\RuntimeException;
use ZM\Logger\ConsoleColor;
@@ -48,7 +51,41 @@ class UnixShell
if (!$this->debug) {
$cmd .= ' 1>/dev/null 2>&1';
}
f_passthru($cmd);
$env_str = $this->getEnvString();
if (!empty($env_str)) {
$env_str = "{$env_str} ";
}
f_passthru("{$env_str}{$cmd}");
return $this;
}
/**
* Init the environment variable that common build will be used.
*
* @param BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library Library class
*/
public function initLibBuildEnv(BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library): UnixShell
{
$this->setEnv([
'CFLAGS' => $library->getLibExtraCFlags(),
'LDFLAGS' => $library->getLibExtraLdFlags(),
'LIBS' => $library->getLibExtraLibs(),
]);
return $this;
}
public function appendEnv(array $env): UnixShell
{
foreach ($env as $k => $v) {
if ($v === '') {
continue;
}
if (!isset($this->env[$k])) {
$this->env[$k] = $v;
} else {
$this->env[$k] = "{$v} {$this->env[$k]}";
}
}
return $this;
}
@@ -80,14 +117,6 @@ class UnixShell
return $this;
}
/**
* @throws RuntimeException
*/
public function execWithEnv(string $cmd): UnixShell
{
return $this->exec($this->getEnvString() . ' ' . $cmd);
}
private function getEnvString(): string
{
$str = '';

View File

@@ -33,24 +33,17 @@ class UnixCMakeExecutor extends Executor
FileSystem::resetDir($this->build_dir);
}
// prepare environment variables
$env = [
'CFLAGS' => $this->library->getLibExtraCFlags(),
'LDFLAGS' => $this->library->getLibExtraLdFlags(),
'LIBS' => $this->library->getLibExtraLibs(),
];
// prepare shell
$shell = shell()->cd($this->build_dir)->setEnv($env);
$shell = shell()->cd($this->build_dir)->initLibBuildEnv($this->library);
// config
$this->steps >= 1 && $shell->execWithEnv("cmake {$this->getConfigureArgs()} {$this->getDefaultCMakeArgs()} {$build_pos}");
$this->steps >= 1 && $shell->exec("cmake {$this->getConfigureArgs()} {$this->getDefaultCMakeArgs()} {$build_pos}");
// make
$this->steps >= 2 && $shell->execWithEnv("cmake --build . -j {$this->library->getBuilder()->concurrency}");
$this->steps >= 2 && $shell->exec("cmake --build . -j {$this->library->getBuilder()->concurrency}");
// install
$this->steps >= 3 && $shell->execWithEnv('make install');
$this->steps >= 3 && $shell->exec('make install');
}
/**