Skip to content

Commit 5681722

Browse files
committed
Fix global env manager, add in-ini variable parsing
1 parent 175aafe commit 5681722

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(array $options = [])
2828
// check musl-cross make installed if we use musl-cross-make
2929
$arch = arch2gnu(php_uname('m'));
3030

31-
GlobalEnvManager::init($this);
31+
GlobalEnvManager::init();
3232

3333
if (getenv('SPC_LIBC') === 'musl' && !SystemUtil::isMuslDist()) {
3434
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\"");

src/SPC/builder/macos/MacOSBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(array $options = [])
2828
$this->options = $options;
2929

3030
// apply global environment variables
31-
GlobalEnvManager::init($this);
31+
GlobalEnvManager::init();
3232

3333
// ---------- set necessary compile vars ----------
3434
// concurrency

src/SPC/builder/windows/WindowsBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(array $options = [])
3333
{
3434
$this->options = $options;
3535

36-
GlobalEnvManager::init($this);
36+
GlobalEnvManager::init();
3737

3838
// ---------- set necessary options ----------
3939
// set sdk (require visual studio 16 or 17)

src/SPC/util/GlobalEnvManager.php

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SPC\util;
66

7-
use SPC\builder\BuilderBase;
87
use SPC\builder\linux\SystemUtil;
98
use SPC\exception\RuntimeException;
109
use SPC\exception\WrongUsageException;
@@ -24,11 +23,10 @@ public static function getInitializedEnv(): array
2423
/**
2524
* Initialize the environment variables
2625
*
27-
* @param null|BuilderBase $builder Builder
2826
* @throws RuntimeException
2927
* @throws WrongUsageException
3028
*/
31-
public static function init(?BuilderBase $builder = null): void
29+
public static function init(): void
3230
{
3331
// Check pre-defined env vars exists
3432
if (getenv('BUILD_ROOT_PATH') === false) {
@@ -37,7 +35,7 @@ public static function init(?BuilderBase $builder = null): void
3735

3836
// Define env vars for unix
3937
if (is_unix()) {
40-
self::putenv('PATH=' . BUILD_ROOT_PATH . '/bin:' . getenv('PATH'));
38+
self::addPathIfNotExists(BUILD_BIN_PATH);
4139
self::putenv('PKG_CONFIG=' . BUILD_BIN_PATH . '/pkg-config');
4240
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
4341
}
@@ -55,10 +53,73 @@ public static function init(?BuilderBase $builder = null): void
5553
self::putenv("SPC_LINUX_DEFAULT_CXX={$arch}-linux-musl-g++");
5654
self::putenv("SPC_LINUX_DEFAULT_AR={$arch}-linux-musl-ar");
5755
self::putenv("SPC_LINUX_DEFAULT_LD={$arch}-linux-musl-ld");
58-
GlobalEnvManager::putenv("PATH=/usr/local/musl/bin:/usr/local/musl/{$arch}-linux-musl/bin:" . getenv('PATH'));
56+
self::addPathIfNotExists('/usr/local/musl/bin');
57+
self::addPathIfNotExists("/usr/local/musl/{$arch}-linux-musl/bin");
5958
}
6059
}
6160

61+
$ini = self::readIniFile();
62+
63+
$default_put_list = [];
64+
foreach ($ini['global'] as $k => $v) {
65+
if (getenv($k) === false) {
66+
$default_put_list[$k] = $v;
67+
self::putenv("{$k}={$v}");
68+
}
69+
}
70+
$os_ini = match (PHP_OS_FAMILY) {
71+
'Windows' => $ini['windows'] ?? [],
72+
'Darwin' => $ini['macos'] ?? [],
73+
'Linux' => $ini['linux'] ?? [],
74+
'BSD' => $ini['freebsd'] ?? [],
75+
default => [],
76+
};
77+
foreach ($os_ini as $k => $v) {
78+
if (getenv($k) === false) {
79+
$default_put_list[$k] = $v;
80+
self::putenv("{$k}={$v}");
81+
}
82+
}
83+
// apply second time
84+
$ini2 = self::readIniFile();
85+
86+
foreach ($ini2['global'] as $k => $v) {
87+
if (isset($default_put_list[$k]) && $default_put_list[$k] !== $v) {
88+
self::putenv("{$k}={$v}");
89+
}
90+
}
91+
$os_ini2 = match (PHP_OS_FAMILY) {
92+
'Windows' => $ini2['windows'] ?? [],
93+
'Darwin' => $ini2['macos'] ?? [],
94+
'Linux' => $ini2['linux'] ?? [],
95+
'BSD' => $ini2['freebsd'] ?? [],
96+
default => [],
97+
};
98+
foreach ($os_ini2 as $k => $v) {
99+
if (isset($default_put_list[$k]) && $default_put_list[$k] !== $v) {
100+
self::putenv("{$k}={$v}");
101+
}
102+
}
103+
}
104+
105+
public static function putenv(string $val): void
106+
{
107+
f_putenv($val);
108+
self::$env_cache[] = $val;
109+
}
110+
111+
private static function addPathIfNotExists(string $path): void
112+
{
113+
if (is_unix() && !str_contains(getenv('PATH'), $path)) {
114+
self::putenv("PATH={$path}:" . getenv('PATH'));
115+
}
116+
}
117+
118+
/**
119+
* @throws WrongUsageException
120+
*/
121+
private static function readIniFile(): array
122+
{
62123
// Init env.ini file, read order:
63124
// WORKING_DIR/config/env.ini
64125
// ROOT_DIR/config/env.ini
@@ -100,28 +161,6 @@ public static function init(?BuilderBase $builder = null): void
100161
break;
101162
}
102163
}
103-
self::applyConfig($ini['global']);
104-
match (PHP_OS_FAMILY) {
105-
'Windows' => self::applyConfig($ini['windows']),
106-
'Darwin' => self::applyConfig($ini['macos']),
107-
'Linux' => self::applyConfig($ini['linux']),
108-
'BSD' => self::applyConfig($ini['freebsd']),
109-
default => null,
110-
};
111-
}
112-
113-
public static function putenv(string $val): void
114-
{
115-
f_putenv($val);
116-
self::$env_cache[] = $val;
117-
}
118-
119-
private static function applyConfig(array $ini): void
120-
{
121-
foreach ($ini as $k => $v) {
122-
if (getenv($k) === false) {
123-
self::putenv($k . '=' . $v);
124-
}
125-
}
164+
return $ini;
126165
}
127166
}

0 commit comments

Comments
 (0)