-
-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathGlobalEnvManager.php
More file actions
174 lines (158 loc) · 5.55 KB
/
GlobalEnvManager.php
File metadata and controls
174 lines (158 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace SPC\util;
use SPC\builder\macos\SystemUtil;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\toolchain\ToolchainManager;
/**
* Environment variable manager
*/
class GlobalEnvManager
{
private static array $env_cache = [];
public static function getInitializedEnv(): array
{
return self::$env_cache;
}
/**
* Initialize the environment variables
*
* @throws RuntimeException
* @throws WrongUsageException
*/
public static function init(): void
{
// Check pre-defined env vars exists
if (getenv('BUILD_ROOT_PATH') === false) {
throw new RuntimeException('You must include src/globals/internal-env.php before using GlobalEnvManager');
}
// Define env vars for unix
if (is_unix()) {
self::addPathIfNotExists(BUILD_BIN_PATH);
self::putenv('PKG_CONFIG=' . BUILD_BIN_PATH . '/pkg-config');
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
}
$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}");
}
}
ToolchainManager::initToolchain();
// 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;
}
public static function addPathIfNotExists(string $path): void
{
if (is_unix() && !str_contains(getenv('PATH'), $path)) {
self::putenv("PATH={$path}:" . getenv('PATH'));
}
}
/**
* Initialize the toolchain after the environment variables are set.
* The toolchain or environment availability check is done here.
*
* @throws WrongUsageException
*/
public static function afterInit(): void
{
if (!filter_var(getenv('SPC_SKIP_TOOLCHAIN_CHECK'), FILTER_VALIDATE_BOOL)) {
ToolchainManager::afterInitToolchain();
}
// test bison
if (PHP_OS_FAMILY === 'Darwin') {
if ($bison = SystemUtil::findCommand('bison', ['/opt/homebrew/opt/bison/bin', '/usr/local/homebrew/opt/bison/bin'])) {
self::putenv("BISON={$bison}");
}
if ($yacc = SystemUtil::findCommand('yacc', ['/opt/homebrew/opt/bison/bin', '/usr/local/homebrew/opt/bison/bin'])) {
self::putenv("YACC={$yacc}");
}
}
}
/**
* @throws WrongUsageException
*/
private static function readIniFile(): array
{
// Init env.ini file, read order:
// WORKING_DIR/config/env.ini
// ROOT_DIR/config/env.ini
$ini_files = [
WORKING_DIR . '/config/env.ini',
ROOT_DIR . '/config/env.ini',
];
$ini_custom = [
WORKING_DIR . '/config/env.custom.ini',
ROOT_DIR . '/config/env.custom.ini',
];
$ini = null;
foreach ($ini_files as $ini_file) {
if (file_exists($ini_file)) {
$ini = parse_ini_file($ini_file, true);
break;
}
}
if ($ini === null) {
throw new WrongUsageException('env.ini not found');
}
if ($ini === false || !isset($ini['global'])) {
throw new WrongUsageException('Failed to parse ' . $ini_file);
}
// apply custom env
foreach ($ini_custom as $ini_file) {
if (file_exists($ini_file)) {
$ini_custom = parse_ini_file($ini_file, true);
if ($ini_custom !== false) {
$ini['global'] = array_merge($ini['global'], $ini_custom['global'] ?? []);
match (PHP_OS_FAMILY) {
'Windows' => $ini['windows'] = array_merge($ini['windows'], $ini_custom['windows'] ?? []),
'Darwin' => $ini['macos'] = array_merge($ini['macos'], $ini_custom['macos'] ?? []),
'Linux' => $ini['linux'] = array_merge($ini['linux'], $ini_custom['linux'] ?? []),
'BSD' => $ini['freebsd'] = array_merge($ini['freebsd'], $ini_custom['freebsd'] ?? []),
default => null,
};
}
break;
}
}
return $ini;
}
}