Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix global env manager, add in-ini variable parsing
  • Loading branch information
crazywhalecc committed Jun 22, 2025
commit 602de98053f7ff4d6272032336f30ba437f95af9
2 changes: 1 addition & 1 deletion src/SPC/builder/linux/LinuxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(array $options = [])
// check musl-cross make installed if we use musl-cross-make
$arch = arch2gnu(php_uname('m'));

GlobalEnvManager::init($this);
GlobalEnvManager::init();

if (getenv('SPC_LIBC') === 'musl' && !SystemUtil::isMuslDist()) {
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\"");
Expand Down
2 changes: 1 addition & 1 deletion src/SPC/builder/macos/MacOSBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(array $options = [])
$this->options = $options;

// apply global environment variables
GlobalEnvManager::init($this);
GlobalEnvManager::init();

// ---------- set necessary compile vars ----------
// concurrency
Expand Down
2 changes: 1 addition & 1 deletion src/SPC/builder/windows/WindowsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(array $options = [])
{
$this->options = $options;

GlobalEnvManager::init($this);
GlobalEnvManager::init();

// ---------- set necessary options ----------
// set sdk (require visual studio 16 or 17)
Expand Down
95 changes: 67 additions & 28 deletions src/SPC/util/GlobalEnvManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SPC\util;

use SPC\builder\BuilderBase;
use SPC\builder\linux\SystemUtil;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
Expand All @@ -24,11 +23,10 @@ public static function getInitializedEnv(): array
/**
* Initialize the environment variables
*
* @param null|BuilderBase $builder Builder
* @throws RuntimeException
* @throws WrongUsageException
*/
public static function init(?BuilderBase $builder = null): void
public static function init(): void
{
// Check pre-defined env vars exists
if (getenv('BUILD_ROOT_PATH') === false) {
Expand All @@ -37,7 +35,7 @@ public static function init(?BuilderBase $builder = null): void

// Define env vars for unix
if (is_unix()) {
self::putenv('PATH=' . BUILD_ROOT_PATH . '/bin:' . getenv('PATH'));
self::addPathIfNotExists(BUILD_BIN_PATH);
self::putenv('PKG_CONFIG=' . BUILD_BIN_PATH . '/pkg-config');
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
}
Expand All @@ -55,10 +53,73 @@ public static function init(?BuilderBase $builder = null): void
self::putenv("SPC_LINUX_DEFAULT_CXX={$arch}-linux-musl-g++");
self::putenv("SPC_LINUX_DEFAULT_AR={$arch}-linux-musl-ar");
self::putenv("SPC_LINUX_DEFAULT_LD={$arch}-linux-musl-ld");
GlobalEnvManager::putenv("PATH=/usr/local/musl/bin:/usr/local/musl/{$arch}-linux-musl/bin:" . getenv('PATH'));
self::addPathIfNotExists('/usr/local/musl/bin');
self::addPathIfNotExists("/usr/local/musl/{$arch}-linux-musl/bin");
}
}

$ini = self::readIniFile();

$default_put_list = [];
foreach ($ini['global'] as $k => $v) {
if (getenv($k) === false) {
$default_put_list[$k] = $v;
self::putenv("{$k}={$v}");
}
}
$os_ini = match (PHP_OS_FAMILY) {
'Windows' => $ini['windows'] ?? [],
'Darwin' => $ini['macos'] ?? [],
'Linux' => $ini['linux'] ?? [],
'BSD' => $ini['freebsd'] ?? [],
default => [],
};
foreach ($os_ini as $k => $v) {
if (getenv($k) === false) {
$default_put_list[$k] = $v;
self::putenv("{$k}={$v}");
}
}
// apply second time
$ini2 = self::readIniFile();

foreach ($ini2['global'] as $k => $v) {
if (isset($default_put_list[$k]) && $default_put_list[$k] !== $v) {
self::putenv("{$k}={$v}");
}
}
$os_ini2 = match (PHP_OS_FAMILY) {
'Windows' => $ini2['windows'] ?? [],
'Darwin' => $ini2['macos'] ?? [],
'Linux' => $ini2['linux'] ?? [],
'BSD' => $ini2['freebsd'] ?? [],
default => [],
};
foreach ($os_ini2 as $k => $v) {
if (isset($default_put_list[$k]) && $default_put_list[$k] !== $v) {
self::putenv("{$k}={$v}");
}
}
}

public static function putenv(string $val): void
{
f_putenv($val);
self::$env_cache[] = $val;
}

private static function addPathIfNotExists(string $path): void
{
if (is_unix() && !str_contains(getenv('PATH'), $path)) {
self::putenv("PATH={$path}:" . getenv('PATH'));
}
}

/**
* @throws WrongUsageException
*/
private static function readIniFile(): array
{
// Init env.ini file, read order:
// WORKING_DIR/config/env.ini
// ROOT_DIR/config/env.ini
Expand Down Expand Up @@ -100,28 +161,6 @@ public static function init(?BuilderBase $builder = null): void
break;
}
}
self::applyConfig($ini['global']);
match (PHP_OS_FAMILY) {
'Windows' => self::applyConfig($ini['windows']),
'Darwin' => self::applyConfig($ini['macos']),
'Linux' => self::applyConfig($ini['linux']),
'BSD' => self::applyConfig($ini['freebsd']),
default => null,
};
}

public static function putenv(string $val): void
{
f_putenv($val);
self::$env_cache[] = $val;
}

private static function applyConfig(array $ini): void
{
foreach ($ini as $k => $v) {
if (getenv($k) === false) {
self::putenv($k . '=' . $v);
}
}
return $ini;
}
}