From 3409a87330d7e81a2780be5e4bd94a642e5a8241 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 31 Aug 2023 17:04:16 -0400 Subject: [PATCH 1/2] refactor(android,cli,desktop,ios,window): use 'hasEnv' for env value checks --- src/android/window.cc | 5 +++++ src/cli/cli.cc | 22 ++++++++++++---------- src/common.hh | 37 +++++++++++++++++++++++++++++++++++++ src/desktop/main.cc | 5 +++++ src/ios/main.mm | 5 +++++ src/window/window.hh | 10 ++++++++++ 6 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/android/window.cc b/src/android/window.cc index ee8d1fa9d8..d7c47a72ef 100644 --- a/src/android/window.cc +++ b/src/android/window.cc @@ -21,6 +21,11 @@ namespace SSC::android { for (auto const &var : parseStringList(this->config["build_env"])) { auto key = trim(var); + + if (!hasEnv(key)) { + continue; + } + auto value = getEnv(key.c_str()); if (value.size() > 0) { diff --git a/src/cli/cli.cc b/src/cli/cli.cc index 7446495274..e85c285a28 100644 --- a/src/cli/cli.cc +++ b/src/cli/cli.cc @@ -1655,8 +1655,8 @@ int main (const int argc, const char* argv[]) { auto parts = split(value, '='); if (parts.size() == 2) { stream << parts[0] << " = " << parts[1] << "\n"; - } else if (parts.size() == 1) { - stream << parts[0] << " = " << getEnv(parts[0].c_str()) << "\n"; + } else if (parts.size() == 1 && hasEnv(parts[0])) { + stream << parts[0] << " = " << getEnv(parts[0]) << "\n"; } } @@ -4553,14 +4553,16 @@ int main (const int argc, const char* argv[]) { compilerFlags += " -stdlib=libstdc++"; } - if (CXX.ends_with("clang++")) { - compiler = CXX.substr(0, CXX.size() - 2); - } else if (CXX.ends_with("clang++.exe")) { - compiler = CXX.substr(0, CXX.size() - 6) + ".exe"; - } else if (CXX.ends_with("g++")) { - compiler = CXX.substr(0, CXX.size() - 2) + "cc"; - } else if (CXX.ends_with("g++.exe")) { - compiler = CXX.substr(0, CXX.size() - 6) + "cc.exe"; + if (CXX.size() == 0) { + if (compiler.ends_with("clang++")) { + compiler = compiler.substr(0, compiler.size() - 2); + } else if (compiler.ends_with("clang++.exe")) { + compiler = compiler.substr(0, compiler.size() - 6) + ".exe"; + } else if (compiler.ends_with("g++")) { + compiler = compiler.substr(0, compiler.size() - 2) + "cc"; + } else if (compiler.ends_with("g++.exe")) { + compiler = compiler.substr(0, compiler.size() - 6) + "cc.exe"; + } } } else if (source.ends_with(".o") || source.ends_with(".a")) { objects << source << " "; diff --git a/src/common.hh b/src/common.hh index 551ff53ff1..c36dd0155b 100644 --- a/src/common.hh +++ b/src/common.hh @@ -573,6 +573,43 @@ namespace SSC { return r; } + inline bool hasEnv (const char* variableName) { + static auto appData = getUserConfig(); + + if (appData[String("env_") + variableName].size() > 0) { + return true; + } + + #if defined(_WIN32) + char* value = nullptr; + size_t size = 0; + auto result = _dupenv_s(&value, &size, variableName); + + if (value && value[0] == '\0') { + free(value); + return false; + } + + free(value); + + if (size == 0 || result != 0) { + return false; + } + #else + auto value = getenv(variableName); + + if (value == nullptr || value[0] == '\0') { + return false; + } + #endif + + return true; + } + + inline bool hasEnv (const String& variableName) { + return hasEnv(variableName.c_str()); + } + inline String getEnv (const char* variableName) { static auto appData = getUserConfig(); diff --git a/src/desktop/main.cc b/src/desktop/main.cc index 0ca8bda831..1a46db82f5 100644 --- a/src/desktop/main.cc +++ b/src/desktop/main.cc @@ -179,6 +179,11 @@ MAIN { SSC::StringStream env; for (auto const &envKey : parseStringList(app.appData["build_env"])) { auto cleanKey = trim(envKey); + + if (!hasEnv(cleanKey)) { + continue; + } + auto envValue = getEnv(cleanKey.c_str()); env << SSC::String( diff --git a/src/ios/main.mm b/src/ios/main.mm index 2ecc85f0b9..b5cf0e0e0e 100644 --- a/src/ios/main.mm +++ b/src/ios/main.mm @@ -201,6 +201,11 @@ - (BOOL) application: (UIApplication*) application for (auto const &envKey : parseStringList(appData["build_env"])) { auto cleanKey = trim(envKey); + + if (!hasEnv(cleanKey)) { + continue; + } + auto envValue = getEnv(cleanKey.c_str()); env << String( diff --git a/src/window/window.hh b/src/window/window.hh index 5fe8a33fcb..3269f73488 100644 --- a/src/window/window.hh +++ b/src/window/window.hh @@ -445,6 +445,11 @@ namespace SSC { if (opts.appData.size() > 0) { for (auto const &envKey : parseStringList(opts.appData["build_env"])) { auto cleanKey = trim(envKey); + + if (!hasEnv(cleanKey)) { + continue; + } + auto envValue = getEnv(cleanKey.c_str()); env << String( @@ -454,6 +459,11 @@ namespace SSC { } else { for (auto const &envKey : parseStringList(this->options.appData["build_env"])) { auto cleanKey = trim(envKey); + + if (!hasEnv(cleanKey)) { + continue; + } + auto envValue = getEnv(cleanKey.c_str()); env << String( From 84e03690b7d586f744130018aa4dc0a48dc23f8a Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 31 Aug 2023 17:12:32 -0400 Subject: [PATCH 2/2] chore(common.hh): remove '#include ' --- src/common.hh | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common.hh b/src/common.hh index c36dd0155b..2c4f93faed 100644 --- a/src/common.hh +++ b/src/common.hh @@ -96,7 +96,6 @@ static os_log_t SSC_OS_LOG_DEBUG_BUNDLE = nullptr; #include #include -#include #include #include #include