mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-12 11:25:35 +08:00
Fix global env manager, add in-ini variable parsing
This commit is contained in:
@@ -28,7 +28,7 @@ class LinuxBuilder extends UnixBuilderBase
|
|||||||
// check musl-cross make installed if we use musl-cross-make
|
// check musl-cross make installed if we use musl-cross-make
|
||||||
$arch = arch2gnu(php_uname('m'));
|
$arch = arch2gnu(php_uname('m'));
|
||||||
|
|
||||||
GlobalEnvManager::init($this);
|
GlobalEnvManager::init();
|
||||||
|
|
||||||
if (getenv('SPC_LIBC') === 'musl' && !SystemUtil::isMuslDist()) {
|
if (getenv('SPC_LIBC') === 'musl' && !SystemUtil::isMuslDist()) {
|
||||||
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\"");
|
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\"");
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class MacOSBuilder extends UnixBuilderBase
|
|||||||
$this->options = $options;
|
$this->options = $options;
|
||||||
|
|
||||||
// apply global environment variables
|
// apply global environment variables
|
||||||
GlobalEnvManager::init($this);
|
GlobalEnvManager::init();
|
||||||
|
|
||||||
// ---------- set necessary compile vars ----------
|
// ---------- set necessary compile vars ----------
|
||||||
// concurrency
|
// concurrency
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class WindowsBuilder extends BuilderBase
|
|||||||
{
|
{
|
||||||
$this->options = $options;
|
$this->options = $options;
|
||||||
|
|
||||||
GlobalEnvManager::init($this);
|
GlobalEnvManager::init();
|
||||||
|
|
||||||
// ---------- set necessary options ----------
|
// ---------- set necessary options ----------
|
||||||
// set sdk (require visual studio 16 or 17)
|
// set sdk (require visual studio 16 or 17)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace SPC\util;
|
namespace SPC\util;
|
||||||
|
|
||||||
use SPC\builder\BuilderBase;
|
|
||||||
use SPC\builder\linux\SystemUtil;
|
use SPC\builder\linux\SystemUtil;
|
||||||
use SPC\exception\RuntimeException;
|
use SPC\exception\RuntimeException;
|
||||||
use SPC\exception\WrongUsageException;
|
use SPC\exception\WrongUsageException;
|
||||||
@@ -24,11 +23,10 @@ class GlobalEnvManager
|
|||||||
/**
|
/**
|
||||||
* Initialize the environment variables
|
* Initialize the environment variables
|
||||||
*
|
*
|
||||||
* @param null|BuilderBase $builder Builder
|
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* @throws WrongUsageException
|
* @throws WrongUsageException
|
||||||
*/
|
*/
|
||||||
public static function init(?BuilderBase $builder = null): void
|
public static function init(): void
|
||||||
{
|
{
|
||||||
// Check pre-defined env vars exists
|
// Check pre-defined env vars exists
|
||||||
if (getenv('BUILD_ROOT_PATH') === false) {
|
if (getenv('BUILD_ROOT_PATH') === false) {
|
||||||
@@ -37,7 +35,7 @@ class GlobalEnvManager
|
|||||||
|
|
||||||
// Define env vars for unix
|
// Define env vars for unix
|
||||||
if (is_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=' . BUILD_BIN_PATH . '/pkg-config');
|
||||||
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
|
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
|
||||||
}
|
}
|
||||||
@@ -55,10 +53,73 @@ class GlobalEnvManager
|
|||||||
self::putenv("SPC_LINUX_DEFAULT_CXX={$arch}-linux-musl-g++");
|
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_AR={$arch}-linux-musl-ar");
|
||||||
self::putenv("SPC_LINUX_DEFAULT_LD={$arch}-linux-musl-ld");
|
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:
|
// Init env.ini file, read order:
|
||||||
// WORKING_DIR/config/env.ini
|
// WORKING_DIR/config/env.ini
|
||||||
// ROOT_DIR/config/env.ini
|
// ROOT_DIR/config/env.ini
|
||||||
@@ -100,28 +161,6 @@ class GlobalEnvManager
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self::applyConfig($ini['global']);
|
return $ini;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user