summaryrefslogtreecommitdiffstats
path: root/Source/cmSourceGroupCommand.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2017-04-21 19:36:17 (GMT)
committerBrad King <brad.king@kitware.com>2017-04-21 19:36:17 (GMT)
commitd286d24af3eeb0e8726bb175cde342f496d40a3f (patch)
tree29dc9449650535fc0e36f0e07b31d85f28dfc5b9 /Source/cmSourceGroupCommand.cxx
parent294cf948dc37799e1980685afbf1fa585ad5b0fb (diff)
parent1b4d2c741ac2006ba2ff17758c82254f6cac01f2 (diff)
downloadCMake-d286d24af3eeb0e8726bb175cde342f496d40a3f.zip
CMake-d286d24af3eeb0e8726bb175cde342f496d40a3f.tar.gz
CMake-d286d24af3eeb0e8726bb175cde342f496d40a3f.tar.bz2
Merge branch 'backport-source_group-TREE-alt-root' into source_group-TREE-alt-root
Diffstat (limited to 'Source/cmSourceGroupCommand.cxx')
-rw-r--r--Source/cmSourceGroupCommand.cxx59
1 files changed, 43 insertions, 16 deletions
diff --git a/Source/cmSourceGroupCommand.cxx b/Source/cmSourceGroupCommand.cxx
index 631f2a6..f54d777 100644
--- a/Source/cmSourceGroupCommand.cxx
+++ b/Source/cmSourceGroupCommand.cxx
@@ -2,6 +2,8 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmSourceGroupCommand.h"
+#include <algorithm>
+#include <iterator>
#include <set>
#include <sstream>
#include <stddef.h>
@@ -36,21 +38,31 @@ std::string getFullFilePath(const std::string& currentPath,
}
std::set<std::string> getSourceGroupFilesPaths(
- const std::string& currentPath, const std::string& root,
- const std::vector<std::string>& files)
+ const std::string& root, const std::vector<std::string>& files)
{
std::set<std::string> ret;
const std::string::size_type rootLength = root.length();
for (size_t i = 0; i < files.size(); ++i) {
- const std::string fullPath = getFullFilePath(currentPath, files[i]);
-
- ret.insert(fullPath.substr(rootLength + 1)); // +1 to also omnit last '/'
+ ret.insert(files[i].substr(rootLength + 1)); // +1 to also omnit last '/'
}
return ret;
}
+bool rootIsPrefix(const std::string& root,
+ const std::vector<std::string>& files, std::string& error)
+{
+ for (size_t i = 0; i < files.size(); ++i) {
+ if (!cmSystemTools::StringStartsWith(files[i], root.c_str())) {
+ error = "ROOT: " + root + " is not a prefix of file: " + files[i];
+ return false;
+ }
+ }
+
+ return true;
+}
+
cmSourceGroup* addSourceGroup(const std::vector<std::string>& tokenizedPath,
cmMakefile& makefile)
{
@@ -68,7 +80,22 @@ cmSourceGroup* addSourceGroup(const std::vector<std::string>& tokenizedPath,
return sg;
}
-bool addFilesToItsSourceGroups(const std::set<std::string>& sgFilesPaths,
+std::string prepareFilePathForTree(const std::string& path)
+{
+ return cmSystemTools::CollapseFullPath(path);
+}
+
+std::vector<std::string> prepareFilesPathsForTree(
+ std::vector<std::string>::const_iterator begin,
+ std::vector<std::string>::const_iterator end)
+{
+ std::vector<std::string> prepared(std::distance(begin, end));
+ std::transform(begin, end, prepared.begin(), prepareFilePathForTree);
+ return prepared;
+}
+
+bool addFilesToItsSourceGroups(const std::string& root,
+ const std::set<std::string>& sgFilesPaths,
const std::string& prefix, cmMakefile& makefile,
std::string& errorMsg)
{
@@ -93,8 +120,7 @@ bool addFilesToItsSourceGroups(const std::set<std::string>& sgFilesPaths,
errorMsg = "Could not create source group for file: " + *it;
return false;
}
- const std::string fullPath =
- getFullFilePath(makefile.GetCurrentSourceDirectory(), *it);
+ const std::string fullPath = getFullFilePath(root, *it);
sg->AddGroupFile(fullPath);
}
}
@@ -233,17 +259,18 @@ bool cmSourceGroupCommand::processTree(const std::vector<std::string>& args,
filesBegin = FilesWithPrefixKeywordIndex + 1;
}
- const std::vector<std::string> filesVector(args.begin() + filesBegin,
- args.end());
+ const std::vector<std::string> filesVector =
+ prepareFilesPathsForTree(args.begin() + filesBegin, args.end());
- std::set<std::string> sourceGroupPaths = getSourceGroupFilesPaths(
- this->Makefile->GetCurrentSourceDirectory(), root, filesVector);
+ if (!rootIsPrefix(root, filesVector, errorMsg)) {
+ return false;
+ }
- addFilesToItsSourceGroups(sourceGroupPaths, prefix, *(this->Makefile),
- errorMsg);
+ std::set<std::string> sourceGroupPaths =
+ getSourceGroupFilesPaths(root, filesVector);
- if (!errorMsg.empty()) {
- this->SetError(errorMsg);
+ if (!addFilesToItsSourceGroups(root, sourceGroupPaths, prefix,
+ *(this->Makefile), errorMsg)) {
return false;
}