static-php-cli/src/SPC/util/GlobalEnvManager.php

111 lines
3.7 KiB
PHP
Raw Normal View History

2024-04-07 15:52:24 +08:00
<?php
declare(strict_types=1);
namespace SPC\util;
use SPC\builder\BuilderBase;
2024-10-01 15:37:37 +08:00
use SPC\builder\linux\SystemUtil;
2024-04-07 15:52:24 +08:00
use SPC\exception\RuntimeException;
2024-10-01 15:37:37 +08:00
use SPC\exception\WrongUsageException;
2024-04-07 15:52:24 +08:00
/**
* Environment variable manager
*/
class GlobalEnvManager
{
private static array $env_cache = [];
public static function getInitializedEnv(): array
{
return self::$env_cache;
}
/**
* Initialize the environment variables
*
2024-10-05 12:04:14 +08:00
* @param null|BuilderBase $builder Builder
2024-04-07 15:52:24 +08:00
* @throws RuntimeException
2024-10-05 12:04:14 +08:00
* @throws WrongUsageException
2024-04-07 15:52:24 +08:00
*/
2024-10-01 15:37:37 +08:00
public static function init(?BuilderBase $builder = null): void
2024-04-07 15:52:24 +08:00
{
2024-10-01 15:37:37 +08:00
// 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');
}
2024-04-07 15:52:24 +08:00
2024-10-01 15:37:37 +08:00
// Define env vars for unix
if (is_unix()) {
self::putenv('PATH=' . BUILD_ROOT_PATH . '/bin:' . getenv('PATH'));
self::putenv('PKG_CONFIG=' . BUILD_BIN_PATH . '/pkg-config');
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
if ($builder instanceof BuilderBase) {
self::putenv('SPC_PHP_DEFAULT_OPTIMIZE_CFLAGS=' . ($builder->getOption('no-strip') ? '-g -O0' : '-g -fstack-protector-strong -fpic -fpie -Os -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'));
}
2024-04-07 15:52:24 +08:00
}
2024-10-01 15:37:37 +08:00
// Define env vars for linux
if (PHP_OS_FAMILY === 'Linux') {
2024-10-03 10:44:49 +08:00
$arch = arch2gnu(php_uname('m'));
2024-10-01 15:37:37 +08:00
if (SystemUtil::isMuslDist()) {
self::putenv('SPC_LINUX_DEFAULT_CC=gcc');
self::putenv('SPC_LINUX_DEFAULT_CXX=g++');
self::putenv('SPC_LINUX_DEFAULT_AR=ar');
} else {
self::putenv("SPC_LINUX_DEFAULT_CC={$arch}-linux-musl-gcc");
self::putenv("SPC_LINUX_DEFAULT_CXX={$arch}-linux-musl-g++");
self::putenv("SPC_LINUX_DEFAULT_AR={$arch}-linux-musl-ar");
}
2024-10-01 16:17:11 +08:00
self::putenv("SPC_PHP_DEFAULT_LD_LIBRARY_PATH_CMD=LD_LIBRARY_PATH=/usr/local/musl/{$arch}-linux-musl/lib");
2024-10-01 16:05:42 +08:00
if (getenv('SPC_NO_MUSL_PATH') !== 'yes') {
self::putenv("PATH=/usr/local/musl/bin:/usr/local/musl/{$arch}-linux-musl/bin:" . getenv('PATH'));
}
2024-04-07 15:52:24 +08:00
}
2024-10-01 15:37:37 +08:00
// 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',
2024-04-07 15:52:24 +08:00
];
2024-10-01 15:37:37 +08:00
$ini = null;
foreach ($ini_files as $ini_file) {
if (file_exists($ini_file)) {
$ini = parse_ini_file($ini_file, true);
break;
}
2024-04-07 15:52:24 +08:00
}
2024-10-01 15:37:37 +08:00
if ($ini === null) {
throw new WrongUsageException('env.ini not found');
}
if ($ini === false || !isset($ini['global'])) {
throw new WrongUsageException('Failed to parse ' . $ini_file);
}
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,
};
2024-04-07 15:52:24 +08:00
}
2024-10-01 15:37:37 +08:00
public static function putenv(string $val): void
2024-04-07 15:52:24 +08:00
{
2024-10-01 15:37:37 +08:00
f_putenv($val);
self::$env_cache[] = $val;
2024-04-07 15:52:24 +08:00
}
2024-10-01 15:37:37 +08:00
private static function applyConfig(array $ini): void
2024-04-07 15:52:24 +08:00
{
2024-10-01 15:37:37 +08:00
foreach ($ini as $k => $v) {
if (getenv($k) === false) {
self::putenv($k . '=' . $v);
}
2024-04-07 15:52:24 +08:00
}
}
}