Skip to content

Commit f586899

Browse files
committed
Split TargetData to TargetContainer and TargetData with persistent entry points and per package data.
1 parent be7240f commit f586899

14 files changed

Lines changed: 238 additions & 156 deletions

File tree

src/sw/core/build.cpp

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static cl::opt<bool> print_graph("print-graph", cl::desc("Print file with build
2929
namespace sw
3030
{
3131

32-
SwBuild::SwBuild(const SwContext &swctx)
32+
SwBuild::SwBuild(SwContext &swctx)
3333
: swctx(swctx)
3434
{
3535
}
@@ -103,71 +103,9 @@ void SwBuild::resolvePackages()
103103

104104
void SwBuild::loadPackages()
105105
{
106-
loadPackages(getTargets());
106+
swctx.loadPackages(getTargets());
107107
}
108108

109-
void SwBuild::loadPackages(TargetMap &targets) const
110-
{
111-
loadPackages(targets, swctx.getPredefinedTargets());
112-
}
113-
114-
void SwBuild::loadPackages(TargetMap &targets, const TargetMap &predefined)
115-
{
116-
// load
117-
while (1)
118-
{
119-
std::map<TargetSettings, std::pair<PackageId, TargetData *>> load;
120-
auto &chld = targets; // take a ref, because it won't be changed in this loop
121-
for (const auto &[pkg, tgts] : chld)
122-
{
123-
for (const auto &tgt : tgts)
124-
{
125-
auto deps = tgt->getDependencies();
126-
for (auto &d : deps)
127-
{
128-
if (d->isResolved())
129-
continue;
130-
131-
auto i = chld.find(d->getUnresolvedPackage());
132-
if (i == chld.end())
133-
throw SW_RUNTIME_ERROR("No target loaded: " + d->getUnresolvedPackage().toString());
134-
135-
auto k = i->second.find(d->getSettings());
136-
if (k != i->second.end())
137-
{
138-
d->setTarget(**k);
139-
continue;
140-
}
141-
142-
if (predefined.find(d->getUnresolvedPackage().ppath) != predefined.end(d->getUnresolvedPackage().ppath))
143-
{
144-
throw SW_LOGIC_ERROR(tgt->getPackage().toString() + ": predefined target is not resolved: " + d->getUnresolvedPackage().toString());
145-
}
146-
147-
load.insert({ d->getSettings(), { i->first, &i->second } });
148-
}
149-
}
150-
}
151-
if (load.empty())
152-
break;
153-
for (auto &[s, d] : load)
154-
{
155-
// empty settings mean we want dependency only to be present
156-
if (s.empty())
157-
continue;
158-
159-
d.second->loadPackages(s, {}/* { d.first }*/ );
160-
auto k = d.second->find(s);
161-
if (k == d.second->end())
162-
{
163-
//throw SW_RUNTIME_ERROR("cannot load package with current settings:\n" + s.toString());
164-
throw SW_RUNTIME_ERROR("cannot load package " + d.first.toString() + " with current settings\n" + s.toString());
165-
}
166-
}
167-
}
168-
}
169-
170-
171109
bool SwBuild::prepareStep()
172110
{
173111
std::atomic_bool next_pass = false;

src/sw/core/build.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ struct SwBuild
2424
{
2525
using CommandExecutionPlan = ExecutionPlan<builder::Command>;
2626

27-
const SwContext &swctx;
27+
SwContext &swctx;
2828

29-
SwBuild(const SwContext &swctx);
29+
SwBuild(SwContext &swctx);
3030
//SwBuild(const SwBuild &) = default;
3131
//SwBuild &operator=(const SwBuild &) = default;
3232

@@ -50,9 +50,6 @@ struct SwBuild
5050
void execute(CommandExecutionPlan &p) const;
5151
CommandExecutionPlan getExecutionPlan(const Commands &cmds) const;
5252

53-
void loadPackages(TargetMap &tm) const; // load tm with predefined
54-
static void loadPackages(TargetMap &tm, const TargetMap &predefined);
55-
5653
//
5754
CommandExecutionPlan getExecutionPlan() const;
5855
String getSpecification() const;

src/sw/core/sw_context.cpp

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,85 @@ const TargetSettings &SwCoreContext::getHostSettings() const
8080
return host_settings;
8181
}
8282

83+
TargetData &SwCoreContext::getTargetData(const PackageId &pkg)
84+
{
85+
return target_data[pkg];
86+
}
87+
88+
const TargetData &SwCoreContext::getTargetData(const PackageId &pkg) const
89+
{
90+
auto i = target_data.find(pkg);
91+
if (i == target_data.end())
92+
throw SW_RUNTIME_ERROR("No target data for package: " + pkg.toString());
93+
return i->second;
94+
}
95+
96+
void SwCoreContext::loadPackages(TargetMap &targets) const
97+
{
98+
loadPackages(targets, getPredefinedTargets());
99+
}
100+
101+
void SwCoreContext::loadPackages(TargetMap &targets, const TargetMap &predefined) const
102+
{
103+
// first, we create all package ids with EPs in targets
104+
for (auto &[p, _] : target_data)
105+
targets[p];
106+
107+
// load
108+
while (1)
109+
{
110+
std::map<TargetSettings, std::pair<PackageId, TargetContainer *>> load;
111+
auto &chld = targets; // take a ref, because it won't be changed in this loop
112+
for (const auto &[pkg, tgts] : chld)
113+
{
114+
for (const auto &tgt : tgts)
115+
{
116+
auto deps = tgt->getDependencies();
117+
for (auto &d : deps)
118+
{
119+
if (d->isResolved())
120+
continue;
121+
122+
auto i = chld.find(d->getUnresolvedPackage());
123+
if (i == chld.end())
124+
throw SW_RUNTIME_ERROR("No target loaded: " + d->getUnresolvedPackage().toString());
125+
126+
auto k = i->second.find(d->getSettings());
127+
if (k != i->second.end())
128+
{
129+
d->setTarget(**k);
130+
continue;
131+
}
132+
133+
if (predefined.find(d->getUnresolvedPackage().ppath) != predefined.end(d->getUnresolvedPackage().ppath))
134+
{
135+
throw SW_LOGIC_ERROR(tgt->getPackage().toString() + ": predefined target is not resolved: " + d->getUnresolvedPackage().toString());
136+
}
137+
138+
load.insert({ d->getSettings(), { i->first, &i->second } });
139+
}
140+
}
141+
}
142+
if (load.empty())
143+
break;
144+
for (auto &[s, d] : load)
145+
{
146+
// empty settings mean we want dependency only to be present
147+
if (s.empty())
148+
continue;
149+
150+
getTargetData(d.first).loadPackages(targets, s, {}/* { d.first }*/ );
151+
auto k = d.second->find(s);
152+
if (k == d.second->end())
153+
{
154+
//throw SW_RUNTIME_ERROR("cannot load package with current settings:\n" + s.toString());
155+
throw SW_RUNTIME_ERROR("cannot load package " + d.first.toString() + " with current settings\n" + s.toString());
156+
}
157+
}
158+
}
159+
}
160+
161+
83162
SwContext::SwContext(const path &local_storage_root_dir)
84163
: SwCoreContext(local_storage_root_dir)
85164
{
@@ -89,7 +168,7 @@ SwContext::~SwContext()
89168
{
90169
}
91170

92-
SwBuild SwContext::createBuild() const
171+
SwBuild SwContext::createBuild()
93172
{
94173
SwBuild b(*this);
95174
b.getTargets() = getPredefinedTargets();

src/sw/core/sw_context.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ struct SW_CORE_API SwCoreContext : SwBuilderContext
2929
const TargetMap &getPredefinedTargets() const { return predefined_targets; }
3030
const TargetSettings &getHostSettings() const;
3131

32+
TargetData &getTargetData(const PackageId &);
33+
const TargetData &getTargetData(const PackageId &) const;
34+
35+
void loadPackages(TargetMap &) const; // load tm with predefined
36+
void loadPackages(TargetMap &, const TargetMap &predefined) const;
37+
3238
private:
3339
TargetMap predefined_targets;
3440
TargetSettings host_settings;
41+
std::unordered_map<PackageId, TargetData> target_data;
3542
// also target data: entry point + common data
3643

3744
void createHostSettings();
@@ -48,7 +55,7 @@ struct SW_CORE_API SwContext : SwCoreContext
4855

4956
void registerDriver(std::unique_ptr<IDriver> driver);
5057
const Drivers &getDrivers() const { return drivers; }
51-
SwBuild createBuild() const;
58+
SwBuild createBuild();
5259

5360
private:
5461
Drivers drivers;

src/sw/core/target.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ TargetData::~TargetData()
1717
{
1818
}
1919

20-
void TargetData::loadPackages(const TargetSettings &s, const PackageIdSet &whitelist)
20+
void TargetData::loadPackages(TargetMap &m, const TargetSettings &s, const PackageIdSet &whitelist) const
2121
{
2222
if (!ep)
2323
throw SW_RUNTIME_ERROR("No entry point provided");
24-
ep->loadPackages(s, whitelist);
24+
ep->loadPackages(m, s, whitelist);
2525
}
2626

2727
void TargetData::setEntryPoint(const std::shared_ptr<TargetEntryPoint> &e)
2828
{
29-
ep = std::move(e);
29+
if (ep)
30+
throw SW_RUNTIME_ERROR("Setting entry point twice");
31+
ep = e;
3032
}
3133

32-
const ITarget *TargetData::getAnyTarget() const
34+
const ITarget *TargetContainer::getAnyTarget() const
3335
{
3436
if (!targets.empty())
3537
return targets.begin()->get();
@@ -38,38 +40,38 @@ const ITarget *TargetData::getAnyTarget() const
3840
return nullptr;
3941
}
4042

41-
void TargetData::push_back(const ITargetPtr &t)
43+
void TargetContainer::push_back(const ITargetPtr &t)
4244
{
4345
targets.push_back(t);
4446
}
4547

46-
void TargetData::push_back_inactive(const ITargetPtr &t)
48+
void TargetContainer::push_back_inactive(const ITargetPtr &t)
4749
{
4850
targets_inactive.push_back(t);
4951
}
5052

51-
void TargetData::clear()
53+
void TargetContainer::clear()
5254
{
5355
targets.clear();
5456
}
5557

56-
TargetData::Base::iterator TargetData::find(const TargetSettings &s)
58+
TargetContainer::Base::iterator TargetContainer::find(const TargetSettings &s)
5759
{
5860
return std::find_if(begin(), end(), [&s](const auto &t)
5961
{
6062
return *t == s;
6163
});
6264
}
6365

64-
TargetData::Base::const_iterator TargetData::find(const TargetSettings &s) const
66+
TargetContainer::Base::const_iterator TargetContainer::find(const TargetSettings &s) const
6567
{
6668
return std::find_if(begin(), end(), [&s](const auto &t)
6769
{
6870
return *t == s;
6971
});
7072
}
7173

72-
bool TargetData::empty() const
74+
bool TargetContainer::empty() const
7375
{
7476
return targets.empty();
7577
}

0 commit comments

Comments
 (0)