Merge topic 'ninja-status-unset-configure'

b5e06311c0 Ninja: Avoid non-standard output from `ninja` during `try_compile`
fe0e2fcaff ScopedEnv: add a RAII helper to temporarily manipulate the environment

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Alex Turbov <i.zaufi@gmail.com>
Merge-request: !11143
This commit is contained in:
Brad King
2025-09-10 14:06:17 +00:00
committed by Kitware Robot
3 changed files with 66 additions and 0 deletions
+8
View File
@@ -3239,6 +3239,14 @@ int cmMakefile::TryCompile(std::string const& srcdir,
return 1;
}
// unset the NINJA_STATUS environment variable while running try compile.
// since we parse the output, we need to ensure there aren't any unexpected
// characters that will cause issues, such as ANSI color escape codes.
cm::optional<cmSystemTools::ScopedEnv> maybeNinjaStatus;
if (this->GetGlobalGenerator()->IsNinja()) {
maybeNinjaStatus.emplace("NINJA_STATUS=");
}
// make sure the same generator is used
// use this program as the cmake to be run, it should not
// be run that way but the cmake object requires a valid path
+35
View File
@@ -2310,6 +2310,41 @@ cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment()
}
#endif
cmSystemTools::ScopedEnv::ScopedEnv(cm::string_view var)
{
std::string::size_type pos = var.find('=');
if (pos != std::string::npos) {
this->Key = std::string{ var.substr(0, pos) };
this->Original = cmSystemTools::GetEnvVar(this->Key);
cm::string_view value = var.substr(pos + 1);
if (!this->Original && value.empty()) {
// nothing to do if the environment variable wasn't already set and the
// new value is also empty. clear the Key member so the destructor also
// does nothing.
this->Key.clear();
} else {
if (value.empty()) {
cmSystemTools::UnPutEnv(this->Key);
} else {
cmSystemTools::PutEnv(cmStrCat(this->Key, '=', value));
}
}
}
}
cmSystemTools::ScopedEnv::~ScopedEnv()
{
if (!this->Key.empty()) {
if (this->Original) {
cmSystemTools::PutEnv(cmStrCat(this->Key, '=', *this->Original));
} else {
cmSystemTools::UnPutEnv(Key);
}
}
}
void cmSystemTools::EnableVSConsoleOutput()
{
#ifdef _WIN32
+23
View File
@@ -517,6 +517,29 @@ public:
};
#endif
/** \class ScopedEnv
* \brief An RAII class to temporarily set/unset an environment variable.
*
* The value passed to the constructor is put into the environment. This
* variable is of the form "var=value" and the original value of the "var"
* environment variable is saved. When the object is destroyed, the original
* value for the environment variable is restored. If the variable didn't
* exist, it will be unset.
*/
class ScopedEnv
{
public:
ScopedEnv(cm::string_view val);
~ScopedEnv();
ScopedEnv(ScopedEnv const&) = delete;
ScopedEnv& operator=(ScopedEnv const&) = delete;
private:
std::string Key;
cm::optional<std::string> Original;
};
/** Setup the environment to enable VS 8 IDE output. */
static void EnableVSConsoleOutput();