Przeglądaj źródła

Autogen: Use the same algorithm for RCC and UIC option merging

Sebastian Holtermann 8 lat temu
rodzic
commit
d8d064abbc

+ 77 - 0
Source/cmQtAutoGen.cxx

@@ -7,6 +7,7 @@
 #include "cmsys/FStream.hxx"
 #include "cmsys/RegularExpression.hxx"
 
+#include <algorithm>
 #include <sstream>
 #include <stddef.h>
 
@@ -19,6 +20,60 @@ const std::string genNameRcc = "AutoRcc";
 
 // - Static functions
 
+/// @brief Merges newOpts into baseOpts
+/// @arg valueOpts list of options that accept a value
+void MergeOptions(std::vector<std::string>& baseOpts,
+                  const std::vector<std::string>& newOpts,
+                  const std::vector<std::string>& valueOpts, bool isQt5)
+{
+  typedef std::vector<std::string>::iterator Iter;
+  typedef std::vector<std::string>::const_iterator CIter;
+  if (newOpts.empty()) {
+    return;
+  }
+  if (baseOpts.empty()) {
+    baseOpts = newOpts;
+    return;
+  }
+
+  std::vector<std::string> extraOpts;
+  for (CIter fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
+       ++fit) {
+    const std::string& newOpt = *fit;
+    Iter existIt = std::find(baseOpts.begin(), baseOpts.end(), newOpt);
+    if (existIt != baseOpts.end()) {
+      if (newOpt.size() >= 2) {
+        // Acquire the option name
+        std::string optName;
+        {
+          auto oit = newOpt.begin();
+          if (*oit == '-') {
+            ++oit;
+            if (isQt5 && (*oit == '-')) {
+              ++oit;
+            }
+            optName.assign(oit, newOpt.end());
+          }
+        }
+        // Test if this is a value option and change the existing value
+        if (!optName.empty() && (std::find(valueOpts.begin(), valueOpts.end(),
+                                           optName) != valueOpts.end())) {
+          const Iter existItNext(existIt + 1);
+          const CIter fitNext(fit + 1);
+          if ((existItNext != baseOpts.end()) && (fitNext != fitEnd)) {
+            *existItNext = *fitNext;
+            ++fit;
+          }
+        }
+      }
+    } else {
+      extraOpts.push_back(newOpt);
+    }
+  }
+  // Append options
+  baseOpts.insert(baseOpts.end(), extraOpts.begin(), extraOpts.end());
+}
+
 static std::string utilStripCR(std::string const& line)
 {
   // Strip CR characters rcc may have printed (possibly more than one!).
@@ -218,6 +273,28 @@ std::string cmQtAutoGen::Quoted(const std::string& text)
   return res;
 }
 
+void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
+                                  const std::vector<std::string>& newOpts,
+                                  bool isQt5)
+{
+  static const std::vector<std::string> valueOpts = {
+    "tr",      "translate", "postfix", "generator",
+    "include", // Since Qt 5.3
+    "g"
+  };
+  MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
+}
+
+void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
+                                  const std::vector<std::string>& newOpts,
+                                  bool isQt5)
+{
+  static const std::vector<std::string> valueOpts = { "name", "root",
+                                                      "compress",
+                                                      "threshold" };
+  MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
+}
+
 bool cmQtAutoGen::RccListInputs(const std::string& qtMajorVersion,
                                 const std::string& rccCommand,
                                 const std::string& fileName,

+ 10 - 0
Source/cmQtAutoGen.h

@@ -34,6 +34,16 @@ public:
   ///
   static std::string Quoted(const std::string& text);
 
+  /// @brief Merges newOpts into baseOpts
+  static void UicMergeOptions(std::vector<std::string>& baseOpts,
+                              const std::vector<std::string>& newOpts,
+                              bool isQt5);
+
+  /// @brief Merges newOpts into baseOpts
+  static void RccMergeOptions(std::vector<std::string>& baseOpts,
+                              const std::vector<std::string>& newOpts,
+                              bool isQt5);
+
   /// @brief Reads the resource files list from from a .qrc file
   /// @arg fileName Must be the absolute path of the .qrc file
   /// @return True if the rcc file was successfully parsed

+ 1 - 41
Source/cmQtAutoGeneratorInitializer.cxx

@@ -562,46 +562,6 @@ static std::string RccGetExecutable(cmGeneratorTarget const* target,
   return rccExec;
 }
 
-static void RccMergeOptions(std::vector<std::string>& opts,
-                            const std::vector<std::string>& fileOpts,
-                            bool isQt5)
-{
-  typedef std::vector<std::string>::iterator Iter;
-  typedef std::vector<std::string>::const_iterator CIter;
-  static const char* valueOptions[4] = { "name", "root", "compress",
-                                         "threshold" };
-
-  std::vector<std::string> extraOpts;
-  for (CIter fit = fileOpts.begin(), fitEnd = fileOpts.end(); fit != fitEnd;
-       ++fit) {
-    Iter existIt = std::find(opts.begin(), opts.end(), *fit);
-    if (existIt != opts.end()) {
-      const char* optName = fit->c_str();
-      if (*optName == '-') {
-        ++optName;
-        if (isQt5 && *optName == '-') {
-          ++optName;
-        }
-      }
-      // Test if this is a value option and change the existing value
-      if ((optName != fit->c_str()) &&
-          std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
-                       cmStrCmp(optName)) != cmArrayEnd(valueOptions)) {
-        const Iter existItNext(existIt + 1);
-        const CIter fitNext(fit + 1);
-        if ((existItNext != opts.end()) && (fitNext != fitEnd)) {
-          *existItNext = *fitNext;
-          ++fit;
-        }
-      }
-    } else {
-      extraOpts.push_back(*fit);
-    }
-  }
-  // Append options
-  opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
-}
-
 static void SetupAutoTargetRcc(const cmQtAutoGenDigest& digest)
 {
   cmGeneratorTarget const* target = digest.Target;
@@ -871,7 +831,7 @@ void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
       {
         std::vector<std::string> opts = optionsTarget;
         if (!qrcDigest.Options.empty()) {
-          RccMergeOptions(opts, qrcDigest.Options, QtV5);
+          cmQtAutoGen::RccMergeOptions(opts, qrcDigest.Options, QtV5);
         }
         qrcDigest.Options = std::move(opts);
       }

+ 2 - 37
Source/cmQtAutoGenerators.cxx

@@ -7,10 +7,8 @@
 #include "cmsys/Terminal.h"
 #include <algorithm>
 #include <array>
-#include <assert.h>
 #include <list>
 #include <sstream>
-#include <stdlib.h>
 #include <string.h>
 #include <utility>
 
@@ -201,40 +199,6 @@ static std::string JoinOptionsMap(
   return result;
 }
 
-static void UicMergeOptions(std::vector<std::string>& opts,
-                            const std::vector<std::string>& fileOpts,
-                            bool isQt5)
-{
-  static const char* valueOptions[] = { "tr",      "translate",
-                                        "postfix", "generator",
-                                        "include", // Since Qt 5.3
-                                        "g" };
-  std::vector<std::string> extraOpts;
-  for (std::vector<std::string>::const_iterator it = fileOpts.begin();
-       it != fileOpts.end(); ++it) {
-    std::vector<std::string>::iterator existingIt =
-      std::find(opts.begin(), opts.end(), *it);
-    if (existingIt != opts.end()) {
-      const char* o = it->c_str();
-      if (*o == '-') {
-        ++o;
-      }
-      if (isQt5 && *o == '-') {
-        ++o;
-      }
-      if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
-                       cmStrCmp(*it)) != cmArrayEnd(valueOptions)) {
-        assert(existingIt + 1 != opts.end());
-        *(existingIt + 1) = *(it + 1);
-        ++it;
-      }
-    } else {
-      extraOpts.push_back(*it);
-    }
-  }
-  opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
-}
-
 // -- Class methods
 
 cmQtAutoGenerators::cmQtAutoGenerators()
@@ -1580,7 +1544,8 @@ bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
         if (optionIt != this->UicOptions.end()) {
           std::vector<std::string> fileOpts;
           cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
-          UicMergeOptions(allOpts, fileOpts, (this->QtMajorVersion == "5"));
+          cmQtAutoGen::UicMergeOptions(allOpts, fileOpts,
+                                       (this->QtMajorVersion == "5"));
         }
         cmd.insert(cmd.end(), allOpts.begin(), allOpts.end());
       }

+ 0 - 1
Source/cmQtAutoGenerators.h

@@ -12,7 +12,6 @@
 #include <map>
 #include <set>
 #include <string>
-#include <utility>
 #include <vector>
 
 class cmMakefile;