Skip to content

Commit 903cf13

Browse files
Add iOS environment variable handling in ConfigLoader
1 parent 9b87c7e commit 903cf13

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

MobileGL/ConfigLoader.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,57 @@
55
// https://www.gnu.org/licenses/lgpl-3.0.txt
66
// SPDX-License-Identifier: LGPL-3.0-only
77
// End of Source File Header
8-
98
#include "Config.h"
10-
9+
#if defined(__APPLE__)
10+
#include <TargetConditionals.h>
11+
#if TARGET_OS_IOS
12+
#include <Foundation/Foundation.h>
13+
#endif
14+
#endif
1115
namespace MobileGL::MG_ConfigLoader {
1216
static UniquePtr<UnorderedMap<String, String>> acceptedEnvVariablesMap;
13-
1417
static Bool IsAcceptedPrefix(const String& key) {
1518
return (key.compare(0, 6, "LIBGL_") == 0 || key.compare(0, 9, "MOBILEGL_") == 0);
1619
}
17-
1820
inline void InitializeAcceptedEnvVariables() {
1921
if (!acceptedEnvVariablesMap) {
2022
acceptedEnvVariablesMap = MakeUnique<UnorderedMap<String, String>>();
2123
} else {
2224
acceptedEnvVariablesMap->clear();
2325
}
24-
26+
#if defined(__APPLE__) && TARGET_OS_IOS
27+
// ::environ is not available on iOS — use NSProcessInfo instead
28+
NSDictionary<NSString*, NSString*>* nsEnv = [[NSProcessInfo processInfo] environment];
29+
for (NSString* nsKey in nsEnv) {
30+
String key([nsKey UTF8String]);
31+
if (IsAcceptedPrefix(key)) {
32+
String value([[nsEnv objectForKey:nsKey] UTF8String]);
33+
(*acceptedEnvVariablesMap)[key] = value;
34+
MGLOG_D("Config: Accepted env variable: %s=%s", key.c_str(), value.c_str());
35+
}
36+
}
37+
#else
2538
char** envPtr = nullptr;
26-
2739
#ifdef _WIN32
2840
envPtr = _environ;
29-
#else // POSIX
41+
#else // POSIX (macOS, Linux)
3042
envPtr = ::environ;
3143
#endif
32-
3344
if (envPtr == nullptr) return;
34-
3545
for (char** env = envPtr; *env != nullptr; ++env) {
3646
String entry(*env);
3747
SizeT pos = entry.find('=');
3848
if (pos != String::npos) {
3949
String key = entry.substr(0, pos);
4050
String value = entry.substr(pos + 1);
41-
4251
if (IsAcceptedPrefix(key)) {
4352
(*acceptedEnvVariablesMap)[key] = value;
4453
MGLOG_D("Config: Accepted env variable: %s=%s", key.c_str(), value.c_str());
4554
}
4655
}
4756
}
57+
#endif
4858
}
49-
5059
inline void QueryEnvVariable(const String& key, String& outValue, const String& defaultValue) {
5160
auto it = acceptedEnvVariablesMap->find(key);
5261
if (it != acceptedEnvVariablesMap->end()) {
@@ -55,7 +64,6 @@ namespace MobileGL::MG_ConfigLoader {
5564
outValue = defaultValue;
5665
}
5766
}
58-
5967
inline void InitBackendType() {
6068
String backendTypeStr;
6169
QueryEnvVariable("MOBILEGL_BACKEND_TYPE", backendTypeStr, "DirectGLES");
@@ -71,13 +79,10 @@ namespace MobileGL::MG_ConfigLoader {
7179
MG_Config::ActiveBackendType = BackendType::Unknown;
7280
#undef ENTRY
7381
}
74-
7582
void Init() {
7683
MGLOG_D("Loading configuration from environment variables...");
7784
InitializeAcceptedEnvVariables();
78-
7985
InitBackendType();
80-
8186
// Destroy the map since we won't need it anymore
8287
acceptedEnvVariablesMap.reset();
8388
}

0 commit comments

Comments
 (0)