From 75ee8f0604ce1770bdcf759b08343930293de006 Mon Sep 17 00:00:00 2001 From: David Miller Date: Fri, 24 Apr 2020 19:25:57 -0400 Subject: [PATCH] EnvironmentLoader, clean merge --- CMakeSettings.json | 34 ++++----- Source/Common/CMakeLists.txt | 1 + Source/Common/EnvironmentLoader.cpp | 111 ++++++++++++++++++++++++++++ Source/Common/EnvironmentLoader.h | 7 ++ 4 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 Source/Common/EnvironmentLoader.cpp create mode 100644 Source/Common/EnvironmentLoader.h diff --git a/CMakeSettings.json b/CMakeSettings.json index d6c50eef1..c39c2c045 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -25,8 +25,8 @@ "variables": [ { "name": "WSL", - "type": "BOOL", - "value": "TRUE" + "value": "TRUE", + "type": "BOOL" } ] }, @@ -46,8 +46,8 @@ "variables": [ { "name": "WSL", - "type": "BOOL", - "value": "TRUE" + "value": "TRUE", + "type": "BOOL" } ] }, @@ -64,18 +64,18 @@ "variables": [ { "name": "CMAKE_C_COMPILER", - "type": "STRING", - "value": "${env.cc}" + "value": "${env.cc}", + "type": "STRING" }, { "name": "CMAKE_CXX_COMPILER", - "type": "STRING", - "value": "${env.cxx}" + "value": "${env.cxx}", + "type": "STRING" }, { "name": "CMAKE_SYSROOT", - "type": "STRING", - "value": "${env.fexsysroot}" + "value": "${env.fexsysroot}", + "type": "STRING" } ] }, @@ -92,18 +92,18 @@ "variables": [ { "name": "CMAKE_C_COMPILER", - "type": "STRING", - "value": "${env.cc}" + "value": "${env.cc}", + "type": "STRING" }, { "name": "CMAKE_CXX_COMPILER", - "type": "STRING", - "value": "${env.cxx}" + "value": "${env.cxx}", + "type": "STRING" }, { "name": "CMAKE_SYSROOT", - "type": "STRING", - "value": "${env.fexsysroot}" + "value": "${env.fexsysroot}", + "type": "STRING" } ] }, @@ -117,7 +117,7 @@ "buildCommandArgs": "-v", "ctestCommandArgs": "", "inheritEnvironments": [ "linux_clang_x64" ], - "remoteMachineName": "${env.fexremote}", // string "-662331587;z.port0.org (username=ubuntu, port=22, authentication=PrivateKey)", + "remoteMachineName": "${env.fexremote}", "remoteCMakeListsRoot": "$HOME/projects/.vs/${projectDirName}/src", "remoteBuildRoot": "$HOME/projects/.vs/${projectDirName}/build/${name}", "remoteInstallRoot": "$HOME/projects/.vs/${projectDirName}/install/${name}", diff --git a/Source/Common/CMakeLists.txt b/Source/Common/CMakeLists.txt index ae76753c3..67fa5921a 100644 --- a/Source/Common/CMakeLists.txt +++ b/Source/Common/CMakeLists.txt @@ -1,6 +1,7 @@ set(NAME Common) set(SRCS ArgumentLoader.cpp + EnvironmentLoader.cpp Config.cpp StringUtil.cpp) diff --git a/Source/Common/EnvironmentLoader.cpp b/Source/Common/EnvironmentLoader.cpp new file mode 100644 index 000000000..77f9e1b12 --- /dev/null +++ b/Source/Common/EnvironmentLoader.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include "Common/Config.h" +#include "LogManager.h" + +namespace FEX::EnvLoader { + + using string = std::string; + using string_view = std::string_view; + + void Load(char *const envp[]) + { + std::unordered_map EnvMap; + + for(const char *const *pvar=envp; pvar && *pvar; pvar++) { + string_view Var(*pvar); + size_t pos = Var.rfind('='); + if (string::npos==pos) + continue; + + string_view Ident = Var.substr(0,pos); + string_view Value = Var.substr(pos+1); + EnvMap[Ident]=Value; + } + + std::function GetVar = [=](const string_view id) -> const string_view { + if (EnvMap.find(id) != EnvMap.end()) + return EnvMap.at(id); + + // If envp[] was empty, search using std::getenv() + const char* vs = std::getenv(id.data()); + string_view sv(vs?vs:""); + return sv; + }; + + string_view Value; + { + if ((Value = GetVar("FEX_CORE")).size()) { + // Accept Numeric or name // + if (isdigit(Value[0])) Config::Add("Core", Value); + else { + uint32_t CoreVal = 0; + if (Value == string_view("irint")) CoreVal = 0; // default + else if (Value == string_view("irjit")) CoreVal = 1; + else if (Value == string_view("llvm")) CoreVal = 2; + else if (Value == string_view("host")) CoreVal = 3; + else if (Value == string_view("vm")) CoreVal = 4; + else { LogMan::Msg::D("FEX_CORE has invalid identifier"); } + Config::Add("Core", std::to_string(CoreVal)); + } + } + + if ((Value = GetVar("FEX_BREAK")).size()) { + if (isdigit(Value[0])) Config::Add("Break", Value); + } + + if ((Value = GetVar("FEX_SINGLE_STEP")).size()) { + if (isdigit(Value[0])) { + Config::Add("SingleStep", Value); + Config::Add("MaxInst", std::to_string(1u)); + } + } + else if ((Value = GetVar("FEX_MAX_INST")).size()) { + if (isdigit(Value[0])) Config::Add("MaxInst", Value); + } + + if ((Value = GetVar("FEX_MULTIBLOCK")).size()) { + if (isdigit(Value[0])) Config::Add("Multiblock", Value); + } + + if ((Value = GetVar("FEX_GDB_SERVER")).size()) { + if (isdigit(Value[0])) Config::Add("GdbServer", Value); + } + } + + { + if ((Value = GetVar("FEX_ROOTFS")).size()) { + Config::Add("RootFS", Value); + if (!std::filesystem::exists(Value)) { + LogMan::Msg::D("FEX_ROOTFS '%s' doesn't exist", Value.data()); + Config::Add("RootFS", ""); + } + } + + if ((Value = GetVar("FEX_UNIFIED_MEM")).size()) { + if (isdigit(Value[0])) Config::Add("UnifiedMemory", Value); + } + } + + { + if ((Value = GetVar("FEX_DUMP_GPRS")).size()) { + if (isdigit(Value[0])) Config::Add("DumpGPRs", Value); + } + + if ((Value = GetVar("FEX_IPC_CLIENT")).size()) { + if (isdigit(Value[0])) Config::Add("IPCClient", Value); + } + + if ((Value = GetVar("FEX_ELF_TYPE")).size()) { + if (isdigit(Value[0])) Config::Add("ELFType", Value); + } + + if ((Value = GetVar("FEX_IPCID")).size()) { + Config::Add("IPCID", Value); + } + } + } + +} \ No newline at end of file diff --git a/Source/Common/EnvironmentLoader.h b/Source/Common/EnvironmentLoader.h new file mode 100644 index 000000000..fb0eab210 --- /dev/null +++ b/Source/Common/EnvironmentLoader.h @@ -0,0 +1,7 @@ +#pragma once + +namespace FEX::EnvLoader { + + void Load(char *const envp[]); + +} \ No newline at end of file