From 1b4d2c741ac2006ba2ff17758c82254f6cac01f2 Mon Sep 17 00:00:00 2001 From: Mateusz Janek Date: Thu, 20 Apr 2017 21:45:28 +0200 Subject: source_group: Fix TREE with root that is not current source dir Also raise an error if files are given that are not below the root. Fixes: #16807 --- Source/cmSourceGroupCommand.cxx | 60 ++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/Source/cmSourceGroupCommand.cxx b/Source/cmSourceGroupCommand.cxx index 5555199..23048bf 100644 --- a/Source/cmSourceGroupCommand.cxx +++ b/Source/cmSourceGroupCommand.cxx @@ -2,6 +2,9 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmSourceGroupCommand.h" +#include +#include +#include #include #include "cmMakefile.h" @@ -34,21 +37,31 @@ std::string getFullFilePath(const std::string& currentPath, } std::set getSourceGroupFilesPaths( - const std::string& currentPath, const std::string& root, - const std::vector& files) + const std::string& root, const std::vector& files) { std::set 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& 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& tokenizedPath, cmMakefile& makefile) { @@ -66,7 +79,22 @@ cmSourceGroup* addSourceGroup(const std::vector& tokenizedPath, return sg; } -bool addFilesToItsSourceGroups(const std::set& sgFilesPaths, +std::string prepareFilePathForTree(const std::string& path) +{ + return cmSystemTools::CollapseFullPath(path); +} + +std::vector prepareFilesPathsForTree( + std::vector::const_iterator begin, + std::vector::const_iterator end) +{ + std::vector prepared(std::distance(begin, end)); + std::transform(begin, end, prepared.begin(), prepareFilePathForTree); + return prepared; +} + +bool addFilesToItsSourceGroups(const std::string& root, + const std::set& sgFilesPaths, const std::string& prefix, cmMakefile& makefile, std::string& errorMsg) { @@ -91,8 +119,7 @@ bool addFilesToItsSourceGroups(const std::set& 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); } } @@ -231,17 +258,18 @@ bool cmSourceGroupCommand::processTree(const std::vector& args, filesBegin = FilesWithPrefixKeywordIndex + 1; } - const std::vector filesVector(args.begin() + filesBegin, - args.end()); + const std::vector filesVector = + prepareFilesPathsForTree(args.begin() + filesBegin, args.end()); - std::set sourceGroupPaths = getSourceGroupFilesPaths( - this->Makefile->GetCurrentSourceDirectory(), root, filesVector); + if (!rootIsPrefix(root, filesVector, errorMsg)) { + return false; + } - addFilesToItsSourceGroups(sourceGroupPaths, prefix, *(this->Makefile), - errorMsg); + std::set sourceGroupPaths = + getSourceGroupFilesPaths(root, filesVector); - if (!errorMsg.empty()) { - this->SetError(errorMsg); + if (!addFilesToItsSourceGroups(root, sourceGroupPaths, prefix, + *(this->Makefile), errorMsg)) { return false; } -- cgit v0.12