Sfoglia il codice sorgente

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 <[email protected]>
Acked-by: Alex Turbov <[email protected]>
Merge-request: !11143
Brad King 3 mesi fa
parent
commit
29633b2f81
3 ha cambiato i file con 66 aggiunte e 0 eliminazioni
  1. 8 0
      Source/cmMakefile.cxx
  2. 35 0
      Source/cmSystemTools.cxx
  3. 23 0
      Source/cmSystemTools.h

+ 8 - 0
Source/cmMakefile.cxx

@@ -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 - 0
Source/cmSystemTools.cxx

@@ -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 - 0
Source/cmSystemTools.h

@@ -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();