cmSourceGroup: Move FindSourceGroup out of cmMakefile

This commit is contained in:
Cristiano Carvalheiro
2025-11-26 11:41:13 +00:00
parent de2afc5d53
commit caf6e782e1
13 changed files with 64 additions and 46 deletions
+1 -1
View File
@@ -525,7 +525,7 @@ void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml)
for (cmSourceFile* sf : files) { for (cmSourceFile* sf : files) {
// Add the file to the list of sources. // Add the file to the list of sources.
std::string const& source = sf->ResolveFullPath(); std::string const& source = sf->ResolveFullPath();
cmSourceGroup* sourceGroup = makefile->FindSourceGroup(source); cmSourceGroup* sourceGroup = lg->FindSourceGroup(source);
sourceGroup->AssignSource(sf); sourceGroup->AssignSource(sf);
} }
+1 -1
View File
@@ -1813,7 +1813,7 @@ Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
source["fileSetIndex"] = fsit->second; 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); source["sourceGroupIndex"] = this->AddSourceGroup(sg, si);
} }
+1 -1
View File
@@ -514,7 +514,7 @@ void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
std::set<std::string> groupNames; std::set<std::string> groupNames;
for (cmSourceFile* sf : sources) { for (cmSourceFile* sf : sources) {
cmSourceGroup* sourceGroup = cmSourceGroup* sourceGroup =
this->Makefile->FindSourceGroup(sf->ResolveFullPath()); this->LocalGenerator->FindSourceGroup(sf->ResolveFullPath());
std::string gn = sourceGroup->GetFullName(); std::string gn = sourceGroup->GetFullName();
groupFiles[gn].push_back(sf); groupFiles[gn].push_back(sf);
groupNames.insert(std::move(gn)); groupNames.insert(std::move(gn));
+2 -1
View File
@@ -1058,7 +1058,8 @@ cm::VS::Solution cmGlobalVisualStudioGenerator::CreateSolution(
item = item =
cmSystemTools::CollapseFullPath(item, mf->GetCurrentSourceDirectory()); cmSystemTools::CollapseFullPath(item, mf->GetCurrentSourceDirectory());
} }
cmSourceGroup* sg = mf->FindSourceGroup(item); cmSourceGroup* sg =
cmSourceGroup::FindSourceGroup(item, mf->GetSourceGroups());
std::string folderName = sg->GetFullName(); std::string folderName = sg->GetFullName();
if (folderName.empty()) { if (folderName.empty()) {
folderName = "Solution Items"_s; folderName = "Solution Items"_s;
+3 -3
View File
@@ -4499,7 +4499,6 @@ bool cmGlobalXCodeGenerator::CreateGroups(
std::vector<cmLocalGenerator*>& generators) std::vector<cmLocalGenerator*>& generators)
{ {
for (auto& generator : generators) { for (auto& generator : generators) {
cmMakefile* mf = generator->GetMakefile();
for (auto const& gtgt : generator->GetGeneratorTargets()) { for (auto const& gtgt : generator->GetGeneratorTargets()) {
// Same skipping logic here as in CreateXCodeTargets so that we do not // Same skipping logic here as in CreateXCodeTargets so that we do not
// end up with (empty anyhow) ZERO_CHECK, install, or test source // end up with (empty anyhow) ZERO_CHECK, install, or test source
@@ -4511,8 +4510,9 @@ bool cmGlobalXCodeGenerator::CreateGroups(
continue; continue;
} }
auto addSourceToGroup = [this, mf, &gtgt](std::string const& source) { auto addSourceToGroup = [this, &gtgt,
cmSourceGroup* sourceGroup = mf->FindSourceGroup(source); &generator](std::string const& source) {
cmSourceGroup* sourceGroup = generator->FindSourceGroup(source);
cmXCodeObject* pbxgroup = cmXCodeObject* pbxgroup =
this->CreateOrGetPBXGroup(gtgt.get(), sourceGroup); this->CreateOrGetPBXGroup(gtgt.get(), sourceGroup);
std::string key = GetGroupMapKeyFromPath(gtgt.get(), source); std::string key = GetGroupMapKeyFromPath(gtgt.get(), source);
+8
View File
@@ -50,6 +50,7 @@
#include "cmSourceFile.h" #include "cmSourceFile.h"
#include "cmSourceFileLocation.h" #include "cmSourceFileLocation.h"
#include "cmSourceFileLocationKind.h" #include "cmSourceFileLocationKind.h"
#include "cmSourceGroup.h"
#include "cmStandardLevelResolver.h" #include "cmStandardLevelResolver.h"
#include "cmState.h" #include "cmState.h"
#include "cmStateDirectory.h" #include "cmStateDirectory.h"
@@ -4103,6 +4104,13 @@ std::string cmLocalGenerator::CreateSafeObjectFileName(
return ssin; return ssin;
} }
cmSourceGroup* cmLocalGenerator::FindSourceGroup(
std::string const& source) const
{
return cmSourceGroup::FindSourceGroup(source,
this->Makefile->GetSourceGroups());
}
std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName( std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
std::string const& sin, std::string const& dir_max) std::string const& sin, std::string const& dir_max)
{ {
+6
View File
@@ -41,6 +41,7 @@ struct cmObjectLocation;
struct cmObjectLocations; struct cmObjectLocations;
class cmRulePlaceholderExpander; class cmRulePlaceholderExpander;
class cmSourceFile; class cmSourceFile;
class cmSourceGroup;
class cmState; class cmState;
class cmTarget; class cmTarget;
class cmake; class cmake;
@@ -573,6 +574,11 @@ public:
std::string CreateSafeObjectFileName(std::string const& sin) const; std::string CreateSafeObjectFileName(std::string const& sin) const;
/**
* find what source group this source is in
*/
cmSourceGroup* FindSourceGroup(std::string const& source) const;
protected: protected:
// The default implementation converts to a Windows shortpath to // The default implementation converts to a Windows shortpath to
// help older toolchains handle spaces and such. A generator may // help older toolchains handle spaces and such. A generator may
+1 -1
View File
@@ -1447,7 +1447,7 @@ void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
} }
// Add the file to the list of sources. // Add the file to the list of sources.
std::string const source = sf->GetFullPath(); std::string const source = sf->GetFullPath();
cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); cmSourceGroup* sourceGroup = this->FindSourceGroup(source);
sourceGroup->AssignSource(sf); sourceGroup->AssignSource(sf);
} }
-29
View File
@@ -2185,35 +2185,6 @@ cmSourceGroup* cmMakefile::GetOrCreateSourceGroup(std::string const& name)
return this->GetOrCreateSourceGroup( return this->GetOrCreateSourceGroup(
cmTokenize(name, p ? cm::string_view(*p) : R"(\/)"_s)); 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 #endif
bool cmMakefile::IsOn(std::string const& name) const bool cmMakefile::IsOn(std::string const& name) const
-5
View File
@@ -654,11 +654,6 @@ public:
* The name will be tokenized. * The name will be tokenized.
*/ */
cmSourceGroup* GetOrCreateSourceGroup(std::string const& name); cmSourceGroup* GetOrCreateSourceGroup(std::string const& name);
/**
* find what source group this source is in
*/
cmSourceGroup* FindSourceGroup(std::string const& source);
#endif #endif
/** /**
+30
View File
@@ -183,3 +183,33 @@ std::vector<cmSourceGroup> const& cmSourceGroup::GetGroupChildren() const
{ {
return this->Internal->GroupChildren; 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<cmSourceGroup> 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<cmSourceGroup*>(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<cmSourceGroup*>(result);
}
}
// Shouldn't get here, but just in case, return the default group.
return const_cast<cmSourceGroup*>(groups.data());
}
+6
View File
@@ -110,6 +110,12 @@ public:
std::vector<cmSourceGroup> const& GetGroupChildren() const; std::vector<cmSourceGroup> 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<cmSourceGroup> const& groups);
private: private:
/** /**
* The name of the source group. * The name of the source group.
+5 -4
View File
@@ -1999,14 +1999,14 @@ void cmVisualStudio10TargetGenerator::WriteGroups()
std::set<cmSourceGroup const*> groupsUsed; std::set<cmSourceGroup const*> groupsUsed;
for (cmGeneratorTarget::AllConfigSource const& si : sources) { for (cmGeneratorTarget::AllConfigSource const& si : sources) {
std::string const& source = si.Source->GetFullPath(); std::string const& source = si.Source->GetFullPath();
cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); cmSourceGroup* sourceGroup = this->LocalGenerator->FindSourceGroup(source);
groupsUsed.insert(sourceGroup); groupsUsed.insert(sourceGroup);
} }
if (cmSourceFile const* srcCMakeLists = if (cmSourceFile const* srcCMakeLists =
this->LocalGenerator->CreateVCProjBuildRule()) { this->LocalGenerator->CreateVCProjBuildRule()) {
std::string const& source = srcCMakeLists->GetFullPath(); std::string const& source = srcCMakeLists->GetFullPath();
cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(source); cmSourceGroup* sourceGroup = this->LocalGenerator->FindSourceGroup(source);
groupsUsed.insert(sourceGroup); groupsUsed.insert(sourceGroup);
} }
@@ -2165,7 +2165,7 @@ void cmVisualStudio10TargetGenerator::WriteGroupSources(
for (ToolSource const& s : sources) { for (ToolSource const& s : sources) {
cmSourceFile const* sf = s.SourceFile; cmSourceFile const* sf = s.SourceFile;
std::string const& source = sf->GetFullPath(); 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 const& filter = sourceGroup->GetFullName();
std::string path = this->ConvertPath(source, s.RelativePath); std::string path = this->ConvertPath(source, s.RelativePath);
ConvertToWindowsSlash(path); ConvertToWindowsSlash(path);
@@ -6036,7 +6036,8 @@ std::string cmVisualStudio10TargetGenerator::GetCSharpSourceLink(
std::string const& fullFileName = source->GetFullPath(); std::string const& fullFileName = source->GetFullPath();
std::string const& srcDir = this->Makefile->GetCurrentSourceDirectory(); std::string const& srcDir = this->Makefile->GetCurrentSourceDirectory();
std::string const& binDir = this->Makefile->GetCurrentBinaryDirectory(); std::string const& binDir = this->Makefile->GetCurrentBinaryDirectory();
cmSourceGroup* sourceGroup = this->Makefile->FindSourceGroup(fullFileName); cmSourceGroup* sourceGroup =
this->LocalGenerator->FindSourceGroup(fullFileName);
if (sourceGroup && !sourceGroup->GetFullName().empty()) { if (sourceGroup && !sourceGroup->GetFullName().empty()) {
sourceGroupedFile = sourceGroupedFile =
cmStrCat(sourceGroup->GetFullName(), '/', cmStrCat(sourceGroup->GetFullName(), '/',