Browse Source

cmExportFileGenerator: Allow other message types

Export generators sometimes need to issue errors, and we had an existing
API for that. However, in the future, it will be useful to issue
non-fatal diagnostics as well. Rework the API to allow specifying the
type of diagnostic to issue.
Matthew Woehlke 1 week ago
parent
commit
fb66a14da2

+ 3 - 5
Source/cmExportBuildFileGenerator.cxx

@@ -16,7 +16,6 @@
 #include "cmList.h"
 #include "cmLocalGenerator.h"
 #include "cmMakefile.h"
-#include "cmMessageType.h"
 #include "cmStateTypes.h"
 #include "cmStringAlgorithms.h"
 #include "cmTarget.h"
@@ -242,12 +241,11 @@ void cmExportBuildFileGenerator::ComplainAboutDuplicateTarget(
   this->ReportError(e.str());
 }
 
-void cmExportBuildFileGenerator::ReportError(
-  std::string const& errorMessage) const
+void cmExportBuildFileGenerator::IssueMessage(MessageType type,
+                                              std::string const& message) const
 {
   this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
-    MessageType::FATAL_ERROR, errorMessage,
-    this->LG->GetMakefile()->GetBacktrace());
+    type, message, this->LG->GetMakefile()->GetBacktrace());
 }
 
 std::string cmExportBuildFileGenerator::InstallNameDir(

+ 2 - 1
Source/cmExportBuildFileGenerator.h

@@ -86,7 +86,8 @@ protected:
   void ComplainAboutDuplicateTarget(
     std::string const& targetName) const override;
 
-  void ReportError(std::string const& errorMessage) const override;
+  void IssueMessage(MessageType type,
+                    std::string const& message) const override;
 
   /** Fill in properties indicating built file locations.  */
   void SetImportLocationProperty(std::string const& config,

+ 8 - 1
Source/cmExportFileGenerator.h

@@ -13,6 +13,7 @@
 #include <cm/string_view>
 
 #include "cmGeneratorExpression.h"
+#include "cmMessageType.h"
 
 class cmExportSet;
 class cmGeneratorTarget;
@@ -120,7 +121,13 @@ protected:
     cmGeneratorExpression::PreprocessContext preprocessRule,
     ImportPropertyMap& properties);
 
-  virtual void ReportError(std::string const& errorMessage) const = 0;
+  virtual void IssueMessage(MessageType type,
+                            std::string const& message) const = 0;
+
+  void ReportError(std::string const& errorMessage) const
+  {
+    this->IssueMessage(MessageType::FATAL_ERROR, errorMessage);
+  }
 
   struct ExportInfo
   {

+ 3 - 3
Source/cmExportInstallFileGenerator.cxx

@@ -335,11 +335,11 @@ void cmExportInstallFileGenerator::ComplainAboutDuplicateTarget(
   this->ReportError(e.str());
 }
 
-void cmExportInstallFileGenerator::ReportError(
-  std::string const& errorMessage) const
+void cmExportInstallFileGenerator::IssueMessage(
+  MessageType type, std::string const& message) const
 {
   this->IEGen->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
-    MessageType::FATAL_ERROR, errorMessage,
+    type, message,
     this->IEGen->GetLocalGenerator()->GetMakefile()->GetBacktrace());
 }
 

+ 2 - 1
Source/cmExportInstallFileGenerator.h

@@ -94,7 +94,8 @@ protected:
 
   ExportInfo FindExportInfo(cmGeneratorTarget const* target) const override;
 
-  void ReportError(std::string const& errorMessage) const override;
+  void IssueMessage(MessageType type,
+                    std::string const& message) const override;
 
   /** Generate a per-configuration file for the targets.  */
   virtual bool GenerateImportFileConfig(std::string const& config);

+ 20 - 3
Source/cmExportTryCompileFileGenerator.cxx

@@ -6,6 +6,7 @@
 #include <utility>
 
 #include <cm/memory>
+#include <cmext/string_view>
 
 #include "cmFileSet.h"
 #include "cmGenExContext.h"
@@ -16,6 +17,7 @@
 #include "cmList.h"
 #include "cmLocalGenerator.h"
 #include "cmMakefile.h"
+#include "cmMessageType.h"
 #include "cmOutputConverter.h"
 #include "cmStateTypes.h"
 #include "cmStringAlgorithms.h"
@@ -31,10 +33,25 @@ cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator(
   gg->CreateImportedGenerationObjects(mf, targets, this->Exports);
 }
 
-void cmExportTryCompileFileGenerator::ReportError(
-  std::string const& errorMessage) const
+void cmExportTryCompileFileGenerator::IssueMessage(
+  MessageType type, std::string const& message) const
 {
-  cmSystemTools::Error(errorMessage);
+  switch (type) {
+    case MessageType::FATAL_ERROR:
+    case MessageType::AUTHOR_ERROR:
+    case MessageType::INTERNAL_ERROR:
+    case MessageType::DEPRECATION_ERROR:
+      cmSystemTools::Error(message);
+      break;
+    case MessageType::WARNING:
+    case MessageType::AUTHOR_WARNING:
+    case MessageType::DEPRECATION_WARNING:
+      cmSystemTools::Message(cmStrCat("CMake Warning: "_s, message),
+                             "Warning");
+      break;
+    default:
+      cmSystemTools::Message(message);
+  }
 }
 
 bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)

+ 2 - 1
Source/cmExportTryCompileFileGenerator.h

@@ -30,7 +30,8 @@ protected:
   // Implement virtual methods from the superclass.
   void ComplainAboutDuplicateTarget(
     std::string const& /*targetName*/) const override {};
-  void ReportError(std::string const& errorMessage) const override;
+  void IssueMessage(MessageType type,
+                    std::string const& message) const override;
 
   bool GenerateMainFile(std::ostream& os) override;