cmGlobalGenerator: Compute a global target ordering respecting dependencies

Move this up from `cmGlobalXCodeGenerator`.  It will be useful for all
generators.
This commit is contained in:
Brad King
2020-09-03 14:51:55 -04:00
parent b03bc81d62
commit c4e296a609
4 changed files with 56 additions and 42 deletions

View File

@@ -1471,6 +1471,7 @@ bool cmGlobalGenerator::Compute()
if (!this->ComputeTargetDepends()) {
return false;
}
this->ComputeTargetOrder();
if (this->CheckTargetsForType()) {
return false;
@@ -1581,6 +1582,50 @@ bool cmGlobalGenerator::ComputeTargetDepends()
return true;
}
std::vector<cmGeneratorTarget*>
cmGlobalGenerator::GetLocalGeneratorTargetsInOrder(cmLocalGenerator* lg) const
{
std::vector<cmGeneratorTarget*> gts;
cm::append(gts, lg->GetGeneratorTargets());
std::sort(gts.begin(), gts.end(),
[this](cmGeneratorTarget const* l, cmGeneratorTarget const* r) {
return this->TargetOrderIndex.at(l) <
this->TargetOrderIndex.at(r);
});
return gts;
}
void cmGlobalGenerator::ComputeTargetOrder()
{
size_t index = 0;
auto const& lgens = this->GetLocalGenerators();
for (auto const& lgen : lgens) {
const auto& targets = lgen->GetGeneratorTargets();
for (const auto& gt : targets) {
this->ComputeTargetOrder(gt.get(), index);
}
}
assert(index == this->TargetOrderIndex.size());
}
void cmGlobalGenerator::ComputeTargetOrder(cmGeneratorTarget const* gt,
size_t& index)
{
std::map<cmGeneratorTarget const*, size_t>::value_type value(gt, 0);
auto insertion = this->TargetOrderIndex.insert(value);
if (!insertion.second) {
return;
}
auto entry = insertion.first;
auto& deps = this->GetTargetDirectDepends(gt);
for (auto& d : deps) {
this->ComputeTargetOrder(d, index);
}
entry->second = index++;
}
bool cmGlobalGenerator::QtAutoGen()
{
#ifndef CMAKE_BOOTSTRAP
@@ -1760,6 +1805,7 @@ void cmGlobalGenerator::ClearGeneratorMembers()
this->GeneratorTargetSearchIndex.clear();
this->MakefileSearchIndex.clear();
this->LocalGeneratorSearchIndex.clear();
this->TargetOrderIndex.clear();
this->ProjectMap.clear();
this->RuleHashes.clear();
this->DirectoryContentMap.clear();

View File

@@ -4,6 +4,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include <cstddef>
#include <iosfwd>
#include <map>
#include <memory>
@@ -264,6 +265,9 @@ public:
return this->LocalGenerators;
}
std::vector<cmGeneratorTarget*> GetLocalGeneratorTargetsInOrder(
cmLocalGenerator* lg) const;
cmMakefile* GetCurrentMakefile() const
{
return this->CurrentConfigureMakefile;
@@ -613,6 +617,10 @@ private:
// Its order is not deterministic.
LocalGeneratorMap LocalGeneratorSearchIndex;
void ComputeTargetOrder();
void ComputeTargetOrder(cmGeneratorTarget const* gt, size_t& index);
std::map<cmGeneratorTarget const*, size_t> TargetOrderIndex;
cmMakefile* TryCompileOuterMakefile;
// If you add a new map here, make sure it is copied
// in EnableLanguagesFromGenerator

View File

@@ -424,37 +424,6 @@ void cmGlobalXCodeGenerator::AddExtraIDETargets()
}
}
void cmGlobalXCodeGenerator::ComputeTargetOrder()
{
size_t index = 0;
auto const& lgens = this->GetLocalGenerators();
for (auto const& lgen : lgens) {
const auto& targets = lgen->GetGeneratorTargets();
for (const auto& gt : targets) {
this->ComputeTargetOrder(gt.get(), index);
}
}
assert(index == this->TargetOrderIndex.size());
}
void cmGlobalXCodeGenerator::ComputeTargetOrder(cmGeneratorTarget const* gt,
size_t& index)
{
std::map<cmGeneratorTarget const*, size_t>::value_type value(gt, 0);
auto insertion = this->TargetOrderIndex.insert(value);
if (!insertion.second) {
return;
}
auto entry = insertion.first;
auto& deps = this->GetTargetDirectDepends(gt);
for (auto& d : deps) {
this->ComputeTargetOrder(d, index);
}
entry->second = index++;
}
void cmGlobalXCodeGenerator::Generate()
{
this->cmGlobalGenerator::Generate();
@@ -462,8 +431,6 @@ void cmGlobalXCodeGenerator::Generate()
return;
}
this->ComputeTargetOrder();
for (auto keyVal : this->ProjectMap) {
cmLocalGenerator* root = keyVal.second[0];
@@ -1243,12 +1210,8 @@ bool cmGlobalXCodeGenerator::CreateXCodeTargets(
cmLocalGenerator* gen, std::vector<cmXCodeObject*>& targets)
{
this->SetCurrentLocalGenerator(gen);
std::vector<cmGeneratorTarget*> gts;
cm::append(gts, this->CurrentLocalGenerator->GetGeneratorTargets());
std::sort(gts.begin(), gts.end(),
[this](cmGeneratorTarget const* l, cmGeneratorTarget const* r) {
return this->TargetOrderIndex[l] < this->TargetOrderIndex[r];
});
std::vector<cmGeneratorTarget*> gts =
this->GetLocalGeneratorTargetsInOrder(gen);
for (auto gtgt : gts) {
if (!this->CreateXCodeTarget(gtgt, targets)) {
return false;

View File

@@ -115,8 +115,6 @@ public:
protected:
void AddExtraIDETargets() override;
void ComputeTargetOrder();
void ComputeTargetOrder(cmGeneratorTarget const* gt, size_t& index);
void Generate() override;
private:
@@ -303,7 +301,6 @@ private:
std::string ObjectDirArch;
std::string SystemName;
std::string GeneratorToolset;
std::map<cmGeneratorTarget const*, size_t> TargetOrderIndex;
std::vector<std::string> EnabledLangs;
std::map<cmGeneratorTarget const*, std::set<cmSourceFile const*>>
CommandsVisited;