1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmSourceGroup.h"
#include <utility>
#include <cm/memory>
#include "cmGeneratorExpression.h"
#include "cmSourceFile.h"
#include "cmStringAlgorithms.h"
class cmSourceGroupInternals
{
public:
SourceGroupVector GroupChildren;
};
cmSourceGroup::cmSourceGroup(std::string name, char const* regex,
char const* parentName)
: Name(std::move(name))
{
this->Internal = cm::make_unique<cmSourceGroupInternals>();
this->SetGroupRegex(regex);
if (parentName) {
this->FullName = cmStrCat(parentName, '\\');
}
this->FullName += this->Name;
}
cmSourceGroup::~cmSourceGroup() = default;
void cmSourceGroup::SetGroupRegex(char const* regex)
{
if (regex) {
this->GroupRegex.compile(regex);
} else {
this->GroupRegex.compile("^$");
}
}
void cmSourceGroup::ResolveGenex(cmLocalGenerator* lg,
std::string const& config)
{
std::set<std::string> files;
for (std::string const& file : this->GroupFiles) {
files.emplace(cmGeneratorExpression::Evaluate(file, lg, config));
}
this->GroupFiles = std::move(files);
if (!this->Internal) {
return;
}
for (auto const& group : this->Internal->GroupChildren) {
group->ResolveGenex(lg, config);
}
}
void cmSourceGroup::AddGroupFile(std::string const& name)
{
this->GroupFiles.insert(name);
}
std::string const& cmSourceGroup::GetName() const
{
return this->Name;
}
std::string const& cmSourceGroup::GetFullName() const
{
return this->FullName;
}
bool cmSourceGroup::MatchesRegex(std::string const& name) const
{
cmsys::RegularExpressionMatch match;
return this->GroupRegex.find(name.c_str(), match);
}
bool cmSourceGroup::MatchesFiles(std::string const& name) const
{
return this->GroupFiles.find(name) != this->GroupFiles.cend();
}
void cmSourceGroup::AssignSource(cmSourceFile const* sf)
{
this->SourceFiles.push_back(sf);
}
std::set<std::string> const& cmSourceGroup::GetGroupFiles() const
{
return this->GroupFiles;
}
std::vector<cmSourceFile const*> const& cmSourceGroup::GetSourceFiles() const
{
return this->SourceFiles;
}
void cmSourceGroup::AddChild(std::unique_ptr<cmSourceGroup> child)
{
this->Internal->GroupChildren.push_back(std::move(child));
}
cmSourceGroup* cmSourceGroup::LookupChild(std::string const& name) const
{
for (auto& group : this->Internal->GroupChildren) {
// look if descendant is the one we're looking for
if (group->GetName() == name) {
return group.get(); // if so return it
}
}
// if no child with this name was found return NULL
return nullptr;
}
cmSourceGroup* cmSourceGroup::MatchChildrenFiles(std::string const& name)
{
if (this->MatchesFiles(name)) {
return this;
}
for (auto& group : this->Internal->GroupChildren) {
cmSourceGroup* result = group->MatchChildrenFiles(name);
if (result) {
return result;
}
}
return nullptr;
}
cmSourceGroup const* cmSourceGroup::MatchChildrenFiles(
std::string const& name) const
{
if (this->MatchesFiles(name)) {
return this;
}
for (auto const& group : this->Internal->GroupChildren) {
cmSourceGroup const* result = group->MatchChildrenFiles(name);
if (result) {
return result;
}
}
return nullptr;
}
cmSourceGroup* cmSourceGroup::MatchChildrenRegex(std::string const& name) const
{
for (auto& group : this->Internal->GroupChildren) {
cmSourceGroup* result = group->MatchChildrenRegex(name);
if (result) {
return result;
}
}
if (this->MatchesRegex(name)) {
return const_cast<cmSourceGroup*>(this);
}
return nullptr;
}
SourceGroupVector 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,
SourceGroupVector const& groups)
{
// First search for a group that lists the file explicitly.
for (auto sg = groups.rbegin(); sg != groups.rend(); ++sg) {
cmSourceGroup* result = (*sg)->MatchChildrenFiles(source);
if (result) {
return result;
}
}
// Now search for a group whose regex matches the file.
for (auto sg = groups.rbegin(); sg != groups.rend(); ++sg) {
cmSourceGroup* result = (*sg)->MatchChildrenRegex(source);
if (result) {
return result;
}
}
// Shouldn't get here, but just in case, return the default group.
return groups.data()->get();
}
|