4
4
5
5
namespace SPC \util ;
6
6
7
- use SPC \builder \BuilderBase ;
8
7
use SPC \builder \linux \SystemUtil ;
9
8
use SPC \exception \RuntimeException ;
10
9
use SPC \exception \WrongUsageException ;
@@ -24,11 +23,10 @@ public static function getInitializedEnv(): array
24
23
/**
25
24
* Initialize the environment variables
26
25
*
27
- * @param null|BuilderBase $builder Builder
28
26
* @throws RuntimeException
29
27
* @throws WrongUsageException
30
28
*/
31
- public static function init (? BuilderBase $ builder = null ): void
29
+ public static function init (): void
32
30
{
33
31
// Check pre-defined env vars exists
34
32
if (getenv ('BUILD_ROOT_PATH ' ) === false ) {
@@ -37,7 +35,7 @@ public static function init(?BuilderBase $builder = null): void
37
35
38
36
// Define env vars for unix
39
37
if (is_unix ()) {
40
- self ::putenv ( ' PATH= ' . BUILD_ROOT_PATH . ' /bin: ' . getenv ( ' PATH ' ) );
38
+ self ::addPathIfNotExists ( BUILD_BIN_PATH );
41
39
self ::putenv ('PKG_CONFIG= ' . BUILD_BIN_PATH . '/pkg-config ' );
42
40
self ::putenv ('PKG_CONFIG_PATH= ' . BUILD_ROOT_PATH . '/lib/pkgconfig ' );
43
41
}
@@ -55,10 +53,73 @@ public static function init(?BuilderBase $builder = null): void
55
53
self ::putenv ("SPC_LINUX_DEFAULT_CXX= {$ arch }-linux-musl-g++ " );
56
54
self ::putenv ("SPC_LINUX_DEFAULT_AR= {$ arch }-linux-musl-ar " );
57
55
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 " );
59
58
}
60
59
}
61
60
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
+ {
62
123
// Init env.ini file, read order:
63
124
// WORKING_DIR/config/env.ini
64
125
// ROOT_DIR/config/env.ini
@@ -100,28 +161,6 @@ public static function init(?BuilderBase $builder = null): void
100
161
break ;
101
162
}
102
163
}
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 ;
126
165
}
127
166
}
0 commit comments