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

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -514,7 +514,7 @@ void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
std::set<std::string> 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));

View File

@@ -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;

View File

@@ -4499,7 +4499,6 @@ bool cmGlobalXCodeGenerator::CreateGroups(
std::vector<cmLocalGenerator*>& 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, &gtgt](std::string const& source) {
cmSourceGroup* sourceGroup = mf->FindSourceGroup(source);
auto addSourceToGroup = [this, &gtgt,
&generator](std::string const& source) {
cmSourceGroup* sourceGroup = generator->FindSourceGroup(source);
cmXCodeObject* pbxgroup =
this->CreateOrGetPBXGroup(gtgt.get(), sourceGroup);
std::string key = GetGroupMapKeyFromPath(gtgt.get(), source);

View File

@@ -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)
{

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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
/**

View File

@@ -183,3 +183,33 @@ std::vector<cmSourceGroup> 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<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());
}

View File

@@ -110,6 +110,12 @@ public:
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:
/**
* The name of the source group.

View File

@@ -1999,14 +1999,14 @@ void cmVisualStudio10TargetGenerator::WriteGroups()
std::set<cmSourceGroup const*> 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(), '/',