From caf6e782e1d483460271968d398fbc7ced19de8f Mon Sep 17 00:00:00 2001 From: Cristiano Carvalheiro Date: Wed, 26 Nov 2025 11:41:13 +0000 Subject: [PATCH] cmSourceGroup: Move FindSourceGroup out of cmMakefile --- Source/cmExtraEclipseCDT4Generator.cxx | 2 +- Source/cmFileAPICodemodel.cxx | 2 +- Source/cmGhsMultiTargetGenerator.cxx | 2 +- Source/cmGlobalVisualStudioGenerator.cxx | 3 ++- Source/cmGlobalXCodeGenerator.cxx | 6 ++--- Source/cmLocalGenerator.cxx | 8 ++++++ Source/cmLocalGenerator.h | 6 +++++ Source/cmLocalVisualStudio7Generator.cxx | 2 +- Source/cmMakefile.cxx | 29 --------------------- Source/cmMakefile.h | 5 ---- Source/cmSourceGroup.cxx | 30 ++++++++++++++++++++++ Source/cmSourceGroup.h | 6 +++++ Source/cmVisualStudio10TargetGenerator.cxx | 9 ++++--- 13 files changed, 64 insertions(+), 46 deletions(-) diff --git a/Source/cmExtraEclipseCDT4Generator.cxx b/Source/cmExtraEclipseCDT4Generator.cxx index a81c4a11de..02f755b883 100644 --- a/Source/cmExtraEclipseCDT4Generator.cxx +++ b/Source/cmExtraEclipseCDT4Generator.cxx @@ -525,7 +525,7 @@ void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml) for (cmSourceFile* sf : files) { // Add the file to the list of sources. std::string const& source = sf->ResolveFullPath(); - cmSourceGroup* sourceGroup = makefile->FindSourceGroup(source); + cmSourceGroup* sourceGroup = lg->FindSourceGroup(source); sourceGroup->AssignSource(sf); } diff --git a/Source/cmFileAPICodemodel.cxx b/Source/cmFileAPICodemodel.cxx index 976ab7a005..fb4ec36875 100644 --- a/Source/cmFileAPICodemodel.cxx +++ b/Source/cmFileAPICodemodel.cxx @@ -1813,7 +1813,7 @@ Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk, source["fileSetIndex"] = fsit->second; } - if (cmSourceGroup* sg = this->GT->Makefile->FindSourceGroup(path)) { + if (cmSourceGroup* sg = this->GT->LocalGenerator->FindSourceGroup(path)) { source["sourceGroupIndex"] = this->AddSourceGroup(sg, si); } diff --git a/Source/cmGhsMultiTargetGenerator.cxx b/Source/cmGhsMultiTargetGenerator.cxx index 51554cef3b..9f34d60464 100644 --- a/Source/cmGhsMultiTargetGenerator.cxx +++ b/Source/cmGhsMultiTargetGenerator.cxx @@ -514,7 +514,7 @@ void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj) std::set groupNames; for (cmSourceFile* sf : sources) { cmSourceGroup* sourceGroup = - this->Makefile->FindSourceGroup(sf->ResolveFullPath()); + this->LocalGenerator->FindSourceGroup(sf->ResolveFullPath()); std::string gn = sourceGroup->GetFullName(); groupFiles[gn].push_back(sf); groupNames.insert(std::move(gn)); diff --git a/Source/cmGlobalVisualStudioGenerator.cxx b/Source/cmGlobalVisualStudioGenerator.cxx index 957c7c4e61..458be42146 100644 --- a/Source/cmGlobalVisualStudioGenerator.cxx +++ b/Source/cmGlobalVisualStudioGenerator.cxx @@ -1058,7 +1058,8 @@ cm::VS::Solution cmGlobalVisualStudioGenerator::CreateSolution( item = cmSystemTools::CollapseFullPath(item, mf->GetCurrentSourceDirectory()); } - cmSourceGroup* sg = mf->FindSourceGroup(item); + cmSourceGroup* sg = + cmSourceGroup::FindSourceGroup(item, mf->GetSourceGroups()); std::string folderName = sg->GetFullName(); if (folderName.empty()) { folderName = "Solution Items"_s; diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index e03b3d9d3c..405e1d6e1e 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -4499,7 +4499,6 @@ bool cmGlobalXCodeGenerator::CreateGroups( std::vector& generators) { for (auto& generator : generators) { - cmMakefile* mf = generator->GetMakefile(); for (auto const& gtgt : generator->GetGeneratorTargets()) { // Same skipping logic here as in CreateXCodeTargets so that we do not // end up with (empty anyhow) ZERO_CHECK, install, or test source @@ -4511,8 +4510,9 @@ bool cmGlobalXCodeGenerator::CreateGroups( continue; } - auto addSourceToGroup = [this, mf, >gt](std::string const& source) { - cmSourceGroup* sourceGroup = mf->FindSourceGroup(source); + auto addSourceToGroup = [this, >gt, + &generator](std::string const& source) { + cmSourceGroup* sourceGroup = generator->FindSourceGroup(source); cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt.get(), sourceGroup); std::string key = GetGroupMapKeyFromPath(gtgt.get(), source); diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 9750f93d96..80f07bb531 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -50,6 +50,7 @@ #include "cmSourceFile.h" #include "cmSourceFileLocation.h" #include "cmSourceFileLocationKind.h" +#include "cmSourceGroup.h" #include "cmStandardLevelResolver.h" #include "cmState.h" #include "cmStateDirectory.h" @@ -4103,6 +4104,13 @@ std::string cmLocalGenerator::CreateSafeObjectFileName( return ssin; } +cmSourceGroup* cmLocalGenerator::FindSourceGroup( + std::string const& source) const +{ + return cmSourceGroup::FindSourceGroup(source, + this->Makefile->GetSourceGroups()); +} + std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName( std::string const& sin, std::string const& dir_max) { diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index a26b96dc3c..3ef1c5b3b4 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -41,6 +41,7 @@ struct cmObjectLocation; struct cmObjectLocations; class cmRulePlaceholderExpander; class cmSourceFile; +class cmSourceGroup; class cmState; class cmTarget; class cmake; @@ -573,6 +574,11 @@ public: std::string CreateSafeObjectFileName(std::string const& sin) const; + /** + * find what source group this source is in + */ + cmSourceGroup* FindSourceGroup(std::string const& source) const; + protected: // The default implementation converts to a Windows shortpath to // help older toolchains handle spaces and such. A generator may diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx index 45c0e8a40a..674f6c492a 100644 --- a/Source/cmLocalVisualStudio7Generator.cxx +++ b/Source/cmLocalVisualStudio7Generator.cxx @@ -1447,7 +1447,7 @@ void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout, } // Add the file to the list of sources. std::string const source = sf->GetFullPath(); - cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); + cmSourceGroup* sourceGroup = this->FindSourceGroup(source); sourceGroup->AssignSource(sf); } diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index e006ebe8b6..a5ea74fadb 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2185,35 +2185,6 @@ cmSourceGroup* cmMakefile::GetOrCreateSourceGroup(std::string const& name) return this->GetOrCreateSourceGroup( cmTokenize(name, p ? cm::string_view(*p) : R"(\/)"_s)); } - -/** - * 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(std::string const& source) -{ - // First search for a group that lists the file explicitly. - for (auto sg = SourceGroups.rbegin(); sg != SourceGroups.rend(); ++sg) { - cmSourceGroup* result = sg->MatchChildrenFiles(source); - if (result) { - return result; - } - } - - // Now search for a group whose regex matches the file. - for (auto sg = SourceGroups.rbegin(); sg != SourceGroups.rend(); ++sg) { - cmSourceGroup* result = sg->MatchChildrenRegex(source); - if (result) { - return result; - } - } - - // Shouldn't get here, but just in case, return the default group. - return SourceGroups.data(); -} #endif bool cmMakefile::IsOn(std::string const& name) const diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index 851d4f40d2..38303097dc 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -654,11 +654,6 @@ public: * The name will be tokenized. */ cmSourceGroup* GetOrCreateSourceGroup(std::string const& name); - - /** - * find what source group this source is in - */ - cmSourceGroup* FindSourceGroup(std::string const& source); #endif /** diff --git a/Source/cmSourceGroup.cxx b/Source/cmSourceGroup.cxx index 297c0c4359..0a8af24e15 100644 --- a/Source/cmSourceGroup.cxx +++ b/Source/cmSourceGroup.cxx @@ -183,3 +183,33 @@ std::vector const& cmSourceGroup::GetGroupChildren() const { return this->Internal->GroupChildren; } + +/** + * 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* cmSourceGroup::FindSourceGroup( + std::string const& source, std::vector const& groups) +{ + // First search for a group that lists the file explicitly. + for (auto sg = groups.rbegin(); sg != groups.rend(); ++sg) { + cmSourceGroup const* result = sg->MatchChildrenFiles(source); + if (result) { + return const_cast(result); + } + } + + // Now search for a group whose regex matches the file. + for (auto sg = groups.rbegin(); sg != groups.rend(); ++sg) { + cmSourceGroup const* result = sg->MatchChildrenRegex(source); + if (result) { + return const_cast(result); + } + } + + // Shouldn't get here, but just in case, return the default group. + return const_cast(groups.data()); +} diff --git a/Source/cmSourceGroup.h b/Source/cmSourceGroup.h index d1d8a57d1c..2737d9a3a4 100644 --- a/Source/cmSourceGroup.h +++ b/Source/cmSourceGroup.h @@ -110,6 +110,12 @@ public: std::vector const& GetGroupChildren() const; + /** + * Given a source group collection, find the source group for a given source. + */ + static cmSourceGroup* FindSourceGroup( + std::string const& source, std::vector const& groups); + private: /** * The name of the source group. diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index bcba6d2ef7..9deef56ee6 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -1999,14 +1999,14 @@ void cmVisualStudio10TargetGenerator::WriteGroups() std::set groupsUsed; for (cmGeneratorTarget::AllConfigSource const& si : sources) { std::string const& source = si.Source->GetFullPath(); - cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); + cmSourceGroup* sourceGroup = this->LocalGenerator->FindSourceGroup(source); groupsUsed.insert(sourceGroup); } if (cmSourceFile const* srcCMakeLists = this->LocalGenerator->CreateVCProjBuildRule()) { std::string const& source = srcCMakeLists->GetFullPath(); - cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); + cmSourceGroup* sourceGroup = this->LocalGenerator->FindSourceGroup(source); groupsUsed.insert(sourceGroup); } @@ -2165,7 +2165,7 @@ void cmVisualStudio10TargetGenerator::WriteGroupSources( for (ToolSource const& s : sources) { cmSourceFile const* sf = s.SourceFile; std::string const& source = sf->GetFullPath(); - cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); + cmSourceGroup* sourceGroup = this->LocalGenerator->FindSourceGroup(source); std::string const& filter = sourceGroup->GetFullName(); std::string path = this->ConvertPath(source, s.RelativePath); ConvertToWindowsSlash(path); @@ -6036,7 +6036,8 @@ std::string cmVisualStudio10TargetGenerator::GetCSharpSourceLink( std::string const& fullFileName = source->GetFullPath(); std::string const& srcDir = this->Makefile->GetCurrentSourceDirectory(); std::string const& binDir = this->Makefile->GetCurrentBinaryDirectory(); - cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(fullFileName); + cmSourceGroup* sourceGroup = + this->LocalGenerator->FindSourceGroup(fullFileName); if (sourceGroup && !sourceGroup->GetFullName().empty()) { sourceGroupedFile = cmStrCat(sourceGroup->GetFullName(), '/',