15 #if !defined(__GNUC__) || (__GNUC__ >= 10)
23 #if not(defined(WIN) && (_MSC_VER >= 1900))
25 extern char **environ;
28 namespace Teuchos::SystemInformation {
32 std::map<std::string, std::pair<std::string, std::string>>& getCommandsMap() {
33 static std::map<std::string, std::pair<std::string, std::string>> commands;
37 std::set<std::string>& getEnvironmentVariablesSet() {
38 static std::set<std::string> environmentVariables;
39 return environmentVariables;
46 std::string(
"command -v " + command +
" > /dev/null 2>&1").c_str());
51 std::string result =
"";
52 FILE *pipe = popen(command.c_str(),
"r");
54 return "command \"" + command +
"\"failed";
56 while (fgets(buffer,
sizeof buffer, pipe) != NULL) {
61 return "command \"" + command +
"\"failed";
69 auto &environmentVariables = getEnvironmentVariablesSet();
71 if (
auto search = environmentVariables.find(variableName);
72 search != environmentVariables.end()) {
77 environmentVariables.insert(variableName);
84 #if defined(WIN) && (_MSC_VER >= 1900)
85 env = *__p__environ();
94 std::string name = substrings[0];
96 if (name.size() >= prefix.size() &&
97 name.substr(0, prefix.size()) == prefix) {
105 const std::string &commandToRunAndCapture,
106 const std::string &commandToCheckForExistence) {
107 auto &commands = getCommandsMap();
109 std::string myCommandToRunAndCapture = commandToRunAndCapture;
110 if (myCommandToRunAndCapture.empty())
111 myCommandToRunAndCapture = commandLabel;
113 std::string myCommandToCheckForExistence = commandToCheckForExistence;
114 if (myCommandToCheckForExistence.empty())
115 myCommandToCheckForExistence = myCommandToRunAndCapture;
117 if (
auto search = commands.find(commandLabel); search != commands.end()) {
118 if ((commands[commandLabel].first == myCommandToCheckForExistence) &&
119 (commands[commandLabel].second == myCommandToRunAndCapture))
123 <<
"Teuchos::SystemInformation: Attempted to register a command (\""
124 << commandLabel <<
"\", \"" << myCommandToRunAndCapture <<
"\", \""
125 << myCommandToCheckForExistence <<
"\") "
126 <<
"that clashes with already registered command: (" << commandLabel
127 <<
"\", \"" << commands[commandLabel].first <<
"\", \""
128 << commands[commandLabel].second <<
"\")." << std::endl;
133 commands[commandLabel] = {myCommandToCheckForExistence, myCommandToRunAndCapture};
140 std::string userProvidedEnvVariables =
141 Teuchos::getEnvironmentVariable<std::string>(
142 "TEUCHOS_USER_ENVIRONMENT_VARIABLES",
"");
143 if (!userProvidedEnvVariables.empty()) {
145 bool isValid = (strings.size() >= 1);
148 <<
"Teuchos::SystemInformation: The value of the environment "
150 "TEUCHOS_USER_ENVIRONMENT_VARIABLES needs to be a semi-colon "
151 "seperated string. Value: "
152 << userProvidedEnvVariables << std::endl;
153 for (
int itemNo = 0; itemNo < strings.size(); ++itemNo) {
154 std::string &variableName = strings[itemNo];
159 !isValid, std::runtime_error,
160 "Teuchos::SystemInformation: Invalid environment variable "
161 "TEUCHOS_USER_ENVIRONMENT_VARIABLES");
164 std::string userProvidedCommands =
165 Teuchos::getEnvironmentVariable<std::string>(
"TEUCHOS_USER_COMMANDS",
"");
166 if (!userProvidedCommands.empty()) {
168 bool isValid = (strings.size() % 3 == 0) && (strings.size() >= 3);
170 std::cerr <<
"Teuchos::SystemInformation: The value of the environment "
171 "variable TEUCHOS_USER_COMMANDS "
172 "needs to be a semi-colon seperated string with a number of "
173 "elements that is a multiple of 3. Value: "
174 << userProvidedCommands << std::endl;
176 while (isValid && (3 * tupleNo + 2 < strings.size())) {
177 std::string &commandLabel = strings[3 * tupleNo];
178 std::string &commandToRunAndCapture = strings[3 * tupleNo + 1];
179 std::string &commandToCheckForExistence = strings[3 * tupleNo + 2];
181 commandToCheckForExistence) ==
FAILURE) {
187 "Teuchos::SystemInformation: Invalid "
188 "environment variable TEUCHOS_USER_COMMANDS");
191 #if !defined(__GNUC__) || (__GNUC__ >= 10)
193 const std::string executable = std::filesystem::canonical(
"/proc/self/exe");
194 if (!executable.empty()) {
197 }
catch (std::filesystem::filesystem_error &) {
236 for (
auto &prefix : {
"TEUCHOS",
"KOKKOS",
"TPETRA",
"STK"})
242 std::map<std::string, std::string> data;
244 const std::string DATA_NOT_AVAILABLE =
"NOT AVAILABLE";
246 auto &commands = getCommandsMap();
247 for (
auto &command : commands) {
252 data[command.first] = DATA_NOT_AVAILABLE;
256 auto &environmentVariables = getEnvironmentVariablesSet();
257 for (
auto &envVariable : environmentVariables) {
258 const char *varVal = std::getenv(envVariable.c_str());
259 if (varVal ==
nullptr)
260 data[envVariable] =
"NOT SET";
263 Teuchos::getEnvironmentVariable<std::string>(envVariable,
"");
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
A std::string utilities class for Teuchos.
static Array< std::string > splitString(const std::string_view s, const char sep= ',')
Split an input std::string using a seperator char sep.
Standard test and throw macros.