Browse Source

Merge topic 'get-or-create-source-group'

95b17c89 Use cmMakefile::GetOrCreateSourceGroup in cmQtAutogeneratorsInitializer
a451995f Use cmMakefile::GetOrCreateSourceGroup in cmSourceGroupCommand
1e6569c9 cmMakefile: Add GetOrCreateSourceGroup methods
3e8b3e94 cmMakefile: Collect source group methods in one place

Acked-by: Kitware Robot <[email protected]>
Merge-request: !1243
Brad King 8 years ago
parent
commit
cb17150228
4 changed files with 85 additions and 103 deletions
  1. 52 34
      Source/cmMakefile.cxx
  2. 30 23
      Source/cmMakefile.h
  3. 1 14
      Source/cmQtAutoGeneratorInitializer.cxx
  4. 2 32
      Source/cmSourceGroupCommand.cxx

+ 52 - 34
Source/cmMakefile.cxx

@@ -2010,6 +2010,58 @@ void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
   sg->SetGroupRegex(regex);
 }
 
+cmSourceGroup* cmMakefile::GetOrCreateSourceGroup(
+  const std::vector<std::string>& folders)
+{
+  cmSourceGroup* sg = this->GetSourceGroup(folders);
+  if (sg == nullptr) {
+    this->AddSourceGroup(folders);
+    sg = this->GetSourceGroup(folders);
+  }
+  return sg;
+}
+
+cmSourceGroup* cmMakefile::GetOrCreateSourceGroup(const std::string& name)
+{
+  const char* delimiter = this->GetDefinition("SOURCE_GROUP_DELIMITER");
+  if (delimiter == nullptr) {
+    delimiter = "\\";
+  }
+  return this->GetOrCreateSourceGroup(
+    cmSystemTools::tokenize(name, delimiter));
+}
+
+/**
+ * Find a source group whose regular expression matches the filename
+ * part of the given source name.  Search backward through the list of
+ * source groups, and take the first matching group found.  This way
+ * non-inherited SOURCE_GROUP commands will have precedence over
+ * inherited ones.
+ */
+cmSourceGroup* cmMakefile::FindSourceGroup(
+  const char* source, std::vector<cmSourceGroup>& groups) const
+{
+  // First search for a group that lists the file explicitly.
+  for (std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
+       sg != groups.rend(); ++sg) {
+    cmSourceGroup* result = sg->MatchChildrenFiles(source);
+    if (result) {
+      return result;
+    }
+  }
+
+  // Now search for a group whose regex matches the file.
+  for (std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
+       sg != groups.rend(); ++sg) {
+    cmSourceGroup* result = sg->MatchChildrenRegex(source);
+    if (result) {
+      return result;
+    }
+  }
+
+  // Shouldn't get here, but just in case, return the default group.
+  return &groups.front();
+}
 #endif
 
 static bool mightExpandVariablesCMP0019(const char* s)
@@ -2818,40 +2870,6 @@ std::string cmMakefile::GetConfigurations(std::vector<std::string>& configs,
   return buildType;
 }
 
-#if defined(CMAKE_BUILD_WITH_CMAKE)
-/**
- * Find a source group whose regular expression matches the filename
- * part of the given source name.  Search backward through the list of
- * source groups, and take the first matching group found.  This way
- * non-inherited SOURCE_GROUP commands will have precedence over
- * inherited ones.
- */
-cmSourceGroup* cmMakefile::FindSourceGroup(
-  const char* source, std::vector<cmSourceGroup>& groups) const
-{
-  // First search for a group that lists the file explicitly.
-  for (std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
-       sg != groups.rend(); ++sg) {
-    cmSourceGroup* result = sg->MatchChildrenFiles(source);
-    if (result) {
-      return result;
-    }
-  }
-
-  // Now search for a group whose regex matches the file.
-  for (std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
-       sg != groups.rend(); ++sg) {
-    cmSourceGroup* result = sg->MatchChildrenRegex(source);
-    if (result) {
-      return result;
-    }
-  }
-
-  // Shouldn't get here, but just in case, return the default group.
-  return &groups.front();
-}
-#endif
-
 bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff,
                                    cmExecutionStatus& status)
 {

+ 30 - 23
Source/cmMakefile.h

@@ -270,21 +270,6 @@ public:
                        bool excludeFromAll = false);
   void AddAlias(const std::string& libname, const std::string& tgt);
 
-#if defined(CMAKE_BUILD_WITH_CMAKE)
-  /**
-   * Add a root source group for consideration when adding a new source.
-   */
-  void AddSourceGroup(const std::string& name, const char* regex = nullptr);
-
-  /**
-   * Add a source group for consideration when adding a new source.
-   * name is tokenized.
-   */
-  void AddSourceGroup(const std::vector<std::string>& name,
-                      const char* regex = nullptr);
-
-#endif
-
   //@{
   /**
      * Set, Push, Pop policy values for CMake.
@@ -476,6 +461,36 @@ public:
    * Get the source group
    */
   cmSourceGroup* GetSourceGroup(const std::vector<std::string>& name) const;
+
+  /**
+   * Add a root source group for consideration when adding a new source.
+   */
+  void AddSourceGroup(const std::string& name, const char* regex = nullptr);
+
+  /**
+   * Add a source group for consideration when adding a new source.
+   * name is tokenized.
+   */
+  void AddSourceGroup(const std::vector<std::string>& name,
+                      const char* regex = nullptr);
+
+  /**
+   * Get and existing or create a new source group.
+   */
+  cmSourceGroup* GetOrCreateSourceGroup(
+    const std::vector<std::string>& folders);
+
+  /**
+   * Get and existing or create a new source group.
+   * The name will be tokenized.
+   */
+  cmSourceGroup* GetOrCreateSourceGroup(const std::string& name);
+
+  /**
+   * find what source group this source is in
+   */
+  cmSourceGroup* FindSourceGroup(const char* source,
+                                 std::vector<cmSourceGroup>& groups) const;
 #endif
 
   /**
@@ -552,14 +567,6 @@ public:
                     bool atOnly, bool escapeQuotes,
                     cmNewLineStyle = cmNewLineStyle());
 
-#if defined(CMAKE_BUILD_WITH_CMAKE)
-  /**
-   * find what source group this source is in
-   */
-  cmSourceGroup* FindSourceGroup(const char* source,
-                                 std::vector<cmSourceGroup>& groups) const;
-#endif
-
   /**
    * Print a command's invocation
    */

+ 1 - 14
Source/cmQtAutoGeneratorInitializer.cxx

@@ -237,20 +237,7 @@ static bool AddToSourceGroup(cmMakefile* makefile, const std::string& fileName,
     }
     // Generate a source group on demand
     if (!groupName.empty()) {
-      {
-        const char* delimiter =
-          makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
-        if (delimiter == nullptr) {
-          delimiter = "\\";
-        }
-        std::vector<std::string> folders =
-          cmSystemTools::tokenize(groupName, delimiter);
-        sourceGroup = makefile->GetSourceGroup(folders);
-        if (sourceGroup == nullptr) {
-          makefile->AddSourceGroup(folders);
-          sourceGroup = makefile->GetSourceGroup(folders);
-        }
-      }
+      sourceGroup = makefile->GetOrCreateSourceGroup(groupName);
       if (sourceGroup == nullptr) {
         std::ostringstream ost;
         ost << cmQtAutoGen::GeneratorNameUpper(genType);

+ 2 - 32
Source/cmSourceGroupCommand.cxx

@@ -61,23 +61,6 @@ bool rootIsPrefix(const std::string& root,
   return true;
 }
 
-cmSourceGroup* addSourceGroup(const std::vector<std::string>& tokenizedPath,
-                              cmMakefile& makefile)
-{
-  cmSourceGroup* sg;
-
-  sg = makefile.GetSourceGroup(tokenizedPath);
-  if (!sg) {
-    makefile.AddSourceGroup(tokenizedPath);
-    sg = makefile.GetSourceGroup(tokenizedPath);
-    if (!sg) {
-      return nullptr;
-    }
-  }
-
-  return sg;
-}
-
 std::string prepareFilePathForTree(const std::string& path,
                                    const std::string& currentSourceDir)
 {
@@ -121,7 +104,7 @@ bool addFilesToItsSourceGroups(const std::string& root,
     if (tokenizedPath.size() > 1) {
       tokenizedPath.pop_back();
 
-      sg = addSourceGroup(tokenizedPath, makefile);
+      sg = makefile.GetOrCreateSourceGroup(tokenizedPath);
 
       if (!sg) {
         errorMsg = "Could not create source group for file: " + *it;
@@ -158,20 +141,7 @@ bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
     return true;
   }
 
-  std::string delimiter = "\\";
-  if (this->Makefile->GetDefinition("SOURCE_GROUP_DELIMITER")) {
-    delimiter = this->Makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
-  }
-
-  std::vector<std::string> folders =
-    cmSystemTools::tokenize(args[0], delimiter);
-
-  cmSourceGroup* sg = nullptr;
-  sg = this->Makefile->GetSourceGroup(folders);
-  if (!sg) {
-    this->Makefile->AddSourceGroup(folders);
-    sg = this->Makefile->GetSourceGroup(folders);
-  }
+  cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
 
   if (!sg) {
     this->SetError("Could not create or find source group");