Jelajahi Sumber

LocalGenerator: refactoring

Introduce method AppendCompileOptions to support
future source file property COMPILE_OPTIONS.
Marc Chevrier 7 tahun lalu
induk
melakukan
3f935e690a

+ 1 - 1
Source/cmGlobalXCodeGenerator.cxx

@@ -3521,7 +3521,7 @@ void cmGlobalXCodeGenerator::AppendDefines(
 }
 
 void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
-                                        std::string const& flag)
+                                        std::string const& flag) const
 {
   // Short-circuit for an empty flag.
   if (flag.empty()) {

+ 1 - 1
Source/cmGlobalXCodeGenerator.h

@@ -104,7 +104,7 @@ public:
   bool ShouldStripResourcePath(cmMakefile*) const override;
 
   bool SetGeneratorToolset(std::string const& ts, cmMakefile* mf) override;
-  void AppendFlag(std::string& flags, std::string const& flag);
+  void AppendFlag(std::string& flags, std::string const& flag) const;
 
 protected:
   void AddExtraIDETargets() override;

+ 44 - 15
Source/cmLocalGenerator.cxx

@@ -792,19 +792,14 @@ void cmLocalGenerator::AddCompileOptions(std::string& flags,
   if (const char* langFlagRegexStr =
         this->Makefile->GetDefinition(langFlagRegexVar)) {
     // Filter flags acceptable to this language.
-    cmsys::RegularExpression r(langFlagRegexStr);
     std::vector<std::string> opts;
     if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
       cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
     }
     target->GetCompileOptions(opts, config, lang);
-    for (std::string const& opt : opts) {
-      if (r.find(opt.c_str())) {
-        // (Re-)Escape this flag.  COMPILE_FLAGS were already parsed
-        // as a command line above, and COMPILE_OPTIONS are escaped.
-        this->AppendFlagEscape(flags, opt);
-      }
-    }
+    // (Re-)Escape these flags.  COMPILE_FLAGS were already parsed
+    // as a command line above, and COMPILE_OPTIONS are escaped.
+    this->AppendCompileOptions(flags, opts, langFlagRegexStr);
   } else {
     // Use all flags.
     if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
@@ -813,10 +808,8 @@ void cmLocalGenerator::AddCompileOptions(std::string& flags,
     }
     std::vector<std::string> opts;
     target->GetCompileOptions(opts, config, lang);
-    for (std::string const& opt : opts) {
-      // COMPILE_OPTIONS are escaped.
-      this->AppendFlagEscape(flags, opt);
-    }
+    // COMPILE_OPTIONS are escaped.
+    this->AppendCompileOptions(flags, opts);
   }
 
   for (auto const& it : target->GetMaxLanguageStandards()) {
@@ -1876,7 +1869,7 @@ void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
 }
 
 void cmLocalGenerator::AppendFlags(std::string& flags,
-                                   const std::string& newFlags)
+                                   const std::string& newFlags) const
 {
   if (!newFlags.empty()) {
     if (!flags.empty()) {
@@ -1886,7 +1879,8 @@ void cmLocalGenerator::AppendFlags(std::string& flags,
   }
 }
 
-void cmLocalGenerator::AppendFlags(std::string& flags, const char* newFlags)
+void cmLocalGenerator::AppendFlags(std::string& flags,
+                                   const char* newFlags) const
 {
   if (newFlags && *newFlags) {
     this->AppendFlags(flags, std::string(newFlags));
@@ -1894,7 +1888,7 @@ void cmLocalGenerator::AppendFlags(std::string& flags, const char* newFlags)
 }
 
 void cmLocalGenerator::AppendFlagEscape(std::string& flags,
-                                        const std::string& rawFlag)
+                                        const std::string& rawFlag) const
 {
   this->AppendFlags(flags, this->EscapeForShell(rawFlag));
 }
@@ -1930,6 +1924,41 @@ void cmLocalGenerator::AppendIPOLinkerFlags(std::string& flags,
   }
 }
 
+void cmLocalGenerator::AppendCompileOptions(std::string& options,
+                                            const char* options_list,
+                                            const char* regex) const
+{
+  // Short-circuit if there are no options.
+  if (!options_list) {
+    return;
+  }
+
+  // Expand the list of options.
+  std::vector<std::string> options_vec;
+  cmSystemTools::ExpandListArgument(options_list, options_vec);
+  this->AppendCompileOptions(options, options_vec, regex);
+}
+
+void cmLocalGenerator::AppendCompileOptions(
+  std::string& options, const std::vector<std::string>& options_vec,
+  const char* regex) const
+{
+  if (regex != nullptr) {
+    // Filter flags upon specified reges.
+    cmsys::RegularExpression r(regex);
+
+    for (std::string const& opt : options_vec) {
+      if (r.find(opt.c_str())) {
+        this->AppendFlagEscape(options, opt);
+      }
+    }
+  } else {
+    for (std::string const& opt : options_vec) {
+      this->AppendFlagEscape(options, opt);
+    }
+  }
+}
+
 void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
                                      const char* defines_list) const
 {

+ 20 - 3
Source/cmLocalGenerator.h

@@ -118,10 +118,11 @@ public:
                                   cmGeneratorTarget const* target,
                                   const std::string& lang);
   ///! Append flags to a string.
-  virtual void AppendFlags(std::string& flags, const std::string& newFlags);
-  virtual void AppendFlags(std::string& flags, const char* newFlags);
+  virtual void AppendFlags(std::string& flags,
+                           const std::string& newFlags) const;
+  virtual void AppendFlags(std::string& flags, const char* newFlags) const;
   virtual void AppendFlagEscape(std::string& flags,
-                                const std::string& rawFlag);
+                                const std::string& rawFlag) const;
   void AppendIPOLinkerFlags(std::string& flags, cmGeneratorTarget* target,
                             const std::string& config,
                             const std::string& lang);
@@ -165,6 +166,22 @@ public:
   void AppendDefines(std::set<std::string>& defines,
                      const std::vector<std::string>& defines_vec) const;
 
+  /**
+   * Encode a list of compile options for the compiler
+   * command line.
+   */
+  void AppendCompileOptions(std::string& options, const char* options_list,
+                            const char* regex = nullptr) const;
+  void AppendCompileOptions(std::string& options,
+                            std::string const& options_list,
+                            const char* regex = nullptr) const
+  {
+    this->AppendCompileOptions(options, options_list.c_str(), regex);
+  }
+  void AppendCompileOptions(std::string& options,
+                            const std::vector<std::string>& options_vec,
+                            const char* regex = nullptr) const;
+
   /**
    * Join a set of defines into a definesString with a space separator.
    */

+ 3 - 3
Source/cmLocalUnixMakefileGenerator3.cxx

@@ -830,8 +830,8 @@ std::string cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(
   return dir;
 }
 
-void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
-                                                const std::string& newFlags)
+void cmLocalUnixMakefileGenerator3::AppendFlags(
+  std::string& flags, const std::string& newFlags) const
 {
   if (this->IsWatcomWMake() && !newFlags.empty()) {
     std::string newf = newFlags;
@@ -845,7 +845,7 @@ void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
 }
 
 void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
-                                                const char* newFlags)
+                                                const char* newFlags) const
 {
   this->cmLocalGenerator::AppendFlags(flags, newFlags);
 }

+ 3 - 2
Source/cmLocalUnixMakefileGenerator3.h

@@ -88,8 +88,9 @@ public:
                                    const std::string& tgt);
 
   // append flags to a string
-  void AppendFlags(std::string& flags, const std::string& newFlags) override;
-  void AppendFlags(std::string& flags, const char* newFlags) override;
+  void AppendFlags(std::string& flags,
+                   const std::string& newFlags) const override;
+  void AppendFlags(std::string& flags, const char* newFlags) const override;
 
   // append an echo command
   enum EchoColor

+ 3 - 3
Source/cmLocalXCodeGenerator.cxx

@@ -31,10 +31,10 @@ std::string cmLocalXCodeGenerator::GetTargetDirectory(
 }
 
 void cmLocalXCodeGenerator::AppendFlagEscape(std::string& flags,
-                                             const std::string& rawFlag)
+                                             const std::string& rawFlag) const
 {
-  cmGlobalXCodeGenerator* gg =
-    static_cast<cmGlobalXCodeGenerator*>(this->GlobalGenerator);
+  const cmGlobalXCodeGenerator* gg =
+    static_cast<const cmGlobalXCodeGenerator*>(this->GlobalGenerator);
   gg->AppendFlag(flags, rawFlag);
 }
 

+ 1 - 1
Source/cmLocalXCodeGenerator.h

@@ -31,7 +31,7 @@ public:
   std::string GetTargetDirectory(
     cmGeneratorTarget const* target) const override;
   void AppendFlagEscape(std::string& flags,
-                        const std::string& rawFlag) override;
+                        const std::string& rawFlag) const override;
   void Generate() override;
   virtual void GenerateInstallRules();
   void ComputeObjectFilenames(