Browse Source

Merge topic 'backport-sarif-path-encoding' into release-4.1

2eef2baf93 cmake: Fix SARIF diagnostics output path encoding on Windows

Acked-by: Kitware Robot <[email protected]>
Merge-request: !11538
Brad King 1 month ago
parent
commit
8039d1ffba
3 changed files with 13 additions and 21 deletions
  1. 8 12
      Source/cmSarifLog.cxx
  2. 2 4
      Source/cmSarifLog.h
  3. 3 5
      Source/cmake.cxx

+ 8 - 12
Source/cmSarifLog.cxx

@@ -5,8 +5,6 @@
 #include <memory>
 #include <stdexcept>
 
-#include <cm/filesystem>
-
 #include <cm3p/json/value.h>
 #include <cm3p/json/writer.h>
 
@@ -299,8 +297,7 @@ cmSarif::LogFileWriter::~LogFileWriter()
     if (this->TryWrite() == WriteResult::FAILURE) {
       // If the result is `FAILURE`, it means the write condition is true but
       // the file still wasn't written. This is an error.
-      cmSystemTools::Error("Failed to write SARIF log to " +
-                           this->FilePath.generic_string());
+      cmSystemTools::Error("Failed to write SARIF log to " + this->FilePath);
     }
   }
 }
@@ -308,16 +305,16 @@ cmSarif::LogFileWriter::~LogFileWriter()
 bool cmSarif::LogFileWriter::EnsureFileValid()
 {
   // First, ensure directory exists
-  cm::filesystem::path dir = this->FilePath.parent_path();
-  if (!cmSystemTools::FileIsDirectory(dir.generic_string())) {
+  std::string const dir = cmSystemTools::GetFilenamePath(this->FilePath);
+  if (!cmSystemTools::FileIsDirectory(dir)) {
     if (!this->CreateDirectories ||
-        !cmSystemTools::MakeDirectory(dir.generic_string()).IsSuccess()) {
+        !cmSystemTools::MakeDirectory(dir).IsSuccess()) {
       return false;
     }
   }
 
   // Open the file for writing
-  cmsys::ofstream outputFile(this->FilePath.generic_string().c_str());
+  cmsys::ofstream outputFile(this->FilePath.c_str());
   if (!outputFile.good()) {
     return false;
   }
@@ -335,7 +332,7 @@ cmSarif::LogFileWriter::WriteResult cmSarif::LogFileWriter::TryWrite()
   if (!this->EnsureFileValid()) {
     return WriteResult::FAILURE;
   }
-  cmsys::ofstream outputFile(this->FilePath.generic_string().c_str());
+  cmsys::ofstream outputFile(this->FilePath.c_str());
 
   // The file is available, so proceed to write the log
 
@@ -357,9 +354,8 @@ cmSarif::LogFileWriter::WriteResult cmSarif::LogFileWriter::TryWrite()
 bool cmSarif::LogFileWriter::ConfigureForCMakeRun(cmake& cm)
 {
   // If an explicit SARIF output path has been provided, set and check it
-  cm::optional<std::string> sarifFilePath = cm.GetSarifFilePath();
-  if (sarifFilePath) {
-    this->SetPath(cm::filesystem::path(*sarifFilePath));
+  if (cm::optional<std::string> sarifFilePath = cm.GetSarifFilePath()) {
+    this->SetPath(*sarifFilePath);
     if (!this->EnsureFileValid()) {
       cmSystemTools::Error(
         cmStrCat("Invalid SARIF output file path: ", *sarifFilePath));

+ 2 - 4
Source/cmSarifLog.h

@@ -9,7 +9,6 @@
 #include <utility>
 #include <vector>
 
-#include <cm/filesystem>
 #include <cm/optional>
 
 #include <cm3p/json/value.h>
@@ -269,8 +268,7 @@ public:
   ///
   /// The settings will apply when the log file is written. If the output
   /// file should be checked earlier, use `CheckFileValidity`.
-  void SetPath(cm::filesystem::path const& path,
-               bool createParentDirectories = false)
+  void SetPath(std::string const& path, bool createParentDirectories = false)
   {
     this->FilePath = path;
     this->CreateDirectories = createParentDirectories;
@@ -279,7 +277,7 @@ public:
 private:
   ResultsLog const& Log;
   std::function<bool()> WriteCondition;
-  cm::filesystem::path FilePath;
+  std::string FilePath;
   bool CreateDirectories = false;
   bool FileWritten = false;
 };

+ 3 - 5
Source/cmake.cxx

@@ -14,7 +14,6 @@
 #include <stdexcept>
 #include <utility>
 
-#include <cm/filesystem>
 #include <cm/memory>
 #include <cm/optional>
 #include <cm/string_view>
@@ -2945,10 +2944,9 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
     if (!this->SarifFileOutput) {
       // If no output file is specified, use the default path
       // Enable parent directory creation for the default path
-      sarifLogFileWriter.SetPath(
-        cm::filesystem::path(this->GetHomeOutputDirectory()) /
-          std::string(cmSarif::PROJECT_DEFAULT_SARIF_FILE),
-        true);
+      sarifLogFileWriter.SetPath(cmStrCat(this->GetHomeOutputDirectory(), '/',
+                                          cmSarif::PROJECT_DEFAULT_SARIF_FILE),
+                                 true);
     }
 #endif
   } else {