VS: Refactor solution folder creation

* Add cmVisualStudioFolder struct containing names of projects within
  that folder.
* Separate folder creation out of WriteTargetsToSolution into a new
  CreateSolutionFolders function.
This commit is contained in:
Lauri Vasama
2024-11-20 18:10:22 +02:00
parent 321efcce60
commit f1bcb7276a
2 changed files with 47 additions and 26 deletions

View File

@@ -375,6 +375,40 @@ void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
}
}
cmVisualStudioFolder* cmGlobalVisualStudio7Generator::CreateSolutionFolders(
const std::string& path)
{
if (path.empty()) {
return nullptr;
}
std::vector<std::string> tokens =
cmSystemTools::SplitString(path, '/', false);
std::string cumulativePath;
for (std::string const& iter : tokens) {
if (iter.empty()) {
continue;
}
if (cumulativePath.empty()) {
cumulativePath = cmStrCat("CMAKE_FOLDER_GUID_", iter);
} else {
this->VisualStudioFolders[cumulativePath].Projects.insert(
cmStrCat(cumulativePath, '/', iter));
cumulativePath = cmStrCat(cumulativePath, '/', iter);
}
}
if (cumulativePath.empty()) {
return nullptr;
}
return &this->VisualStudioFolders[cumulativePath];
}
void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
std::ostream& fout, cmLocalGenerator* root,
OrderedTargetDependSet const& projectTargets)
@@ -421,31 +455,11 @@ void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
// Create "solution folder" information from FOLDER target property
//
if (written && this->UseFolderProperty()) {
const std::string targetFolder = target->GetEffectiveFolderName();
if (!targetFolder.empty()) {
std::vector<std::string> tokens =
cmSystemTools::SplitString(targetFolder, '/', false);
cmVisualStudioFolder* folder =
this->CreateSolutionFolders(target->GetEffectiveFolderName());
std::string cumulativePath;
for (std::string const& iter : tokens) {
if (iter.empty()) {
continue;
}
if (cumulativePath.empty()) {
cumulativePath = cmStrCat("CMAKE_FOLDER_GUID_", iter);
} else {
VisualStudioFolders[cumulativePath].insert(
cmStrCat(cumulativePath, '/', iter));
cumulativePath = cmStrCat(cumulativePath, '/', iter);
}
}
if (!cumulativePath.empty()) {
VisualStudioFolders[cumulativePath].insert(target->GetName());
}
if (folder != nullptr) {
folder->Projects.insert(target->GetName());
}
}
}
@@ -477,7 +491,7 @@ void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
std::string key(iter.first);
std::string guidParent(this->GetGUID(key));
for (std::string const& it : iter.second) {
for (std::string const& it : iter.second.Projects) {
std::string const& value(it);
std::string guid(this->GetGUID(value));

View File

@@ -25,6 +25,11 @@ class cmake;
template <typename T>
class BT;
struct cmVisualStudioFolder
{
std::set<std::string> Projects;
};
/** \class cmGlobalVisualStudio7Generator
* \brief Write a Unix makefiles.
*
@@ -154,6 +159,8 @@ protected:
virtual void WriteSLNFooter(std::ostream& fout);
std::string WriteUtilityDepend(const cmGeneratorTarget* target) override;
cmVisualStudioFolder* CreateSolutionFolders(const std::string& path);
virtual void WriteTargetsToSolution(
std::ostream& fout, cmLocalGenerator* root,
OrderedTargetDependSet const& projectTargets);
@@ -178,7 +185,7 @@ protected:
virtual void WriteFolders(std::ostream& fout);
virtual void WriteFoldersContent(std::ostream& fout);
std::map<std::string, std::set<std::string>> VisualStudioFolders;
std::map<std::string, cmVisualStudioFolder> VisualStudioFolders;
// Set during OutputSLNFile with the name of the current project.
// There is one SLN file per project.