Skip to content

Commit 381a8e4

Browse files
committed
Improve inputs handling.
1 parent d2ded7b commit 381a8e4

25 files changed

Lines changed: 292 additions & 226 deletions

src/sw/client/client.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ void setup_log(const std::string &log_level, bool simple)
496496
void override_package_perform(sw::SwContext &swctx)
497497
{
498498
auto b = swctx.createBuild();
499-
auto &i = b->addInput(fs::current_path());
499+
sw::InputWithSettings i(swctx.addInput(fs::current_path()));
500+
b->addInput(i);
500501
auto ts = b->getContext().getHostSettings();
501502
ts["driver"]["dry-run"] = "true";
502503
i.addSettings(ts);

src/sw/client/command/build.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,10 @@ std::unique_ptr<sw::SwBuild> createBuildAndPrepare(sw::SwContext &swctx)
395395
auto b = swctx.createBuild();
396396
for (auto &a : build_arg)
397397
{
398-
auto &i = b->addInput(a);
398+
sw::InputWithSettings i(swctx.addInput(a));
399399
for (auto &s : createSettings(*b))
400400
i.addSettings(s);
401+
b->addInput(i);
401402
}
402403
b->load();
403404
b->setTargetsToBuild();
@@ -436,9 +437,10 @@ SUBCOMMAND_DECL2(build)
436437
auto b = swctx.createBuild();
437438
for (auto &a : build_arg)
438439
{
439-
auto &i = b->addInput(a);
440+
sw::InputWithSettings i(swctx.addInput(a));
440441
for (auto &s : createSettings(*b))
441442
i.addSettings(s);
443+
b->addInput(i);
442444
}
443445
if (build_default_explan)
444446
{

src/sw/client/command/fetch.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ std::pair<sw::SourceDirMap, const sw::Input &> fetch(sw::SwBuild &b)
3939
opts.ignore_existing_dirs = true;
4040
opts.existing_dirs_age = std::chrono::hours(1);
4141

42-
auto &i = b.addInput(fs::current_path());
42+
auto &ii = b.getContext().addInput(fs::current_path());
43+
sw::InputWithSettings i(ii);
44+
b.addInput(i);
4345
auto ts = createSettings(b.getContext());
4446
ts["driver"]["dry-run"] = "true";
4547
i.addSettings(ts);
@@ -95,7 +97,7 @@ std::pair<sw::SourceDirMap, const sw::Input &> fetch(sw::SwBuild &b)
9597
if (build_after_fetch)
9698
b.execute();
9799

98-
return { srcs, i };
100+
return { srcs, ii };
99101
}
100102

101103
SUBCOMMAND_DECL2(fetch)

src/sw/client/command/uri.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "commands.h"
2020

21+
#include <sw/core/input.h>
2122
#include <sw/manager/storage.h>
2223
#include <sw/support/filesystem.h>
2324

@@ -146,7 +147,8 @@ SUBCOMMAND_DECL(uri)
146147
fs::create_directories(d);
147148
ScopedCurrentPath scp(d, CurrentPathScope::All);
148149
auto b = swctx->createBuild();
149-
b->addInput(p);
150+
sw::InputWithSettings i(swctx->addInput(p));
151+
b->addInput(i);
150152
b->build();
151153
return;
152154
}

src/sw/core/build.cpp

Lines changed: 25 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -112,68 +112,22 @@ void SwBuild::load()
112112
{
113113
CHECK_STATE_AND_CHANGE(BuildState::NotStarted, BuildState::InputsLoaded);
114114

115-
// load entry points
116-
load(inputs, true);
117-
118-
// and load packages
119-
for (auto &i : inputs)
120-
i->load(*this);
121-
}
122-
123-
void SwBuild::load(Inputs &inputs, bool set_eps)
124-
{
125-
std::vector<Input*> v;
115+
std::vector<Input *> iv;
126116
for (auto &i : inputs)
127-
v.push_back(i.get());
128-
load(v, set_eps);
129-
}
117+
iv.push_back((Input*)&i.getInput());
118+
swctx.loadEntryPoints(iv, true);
130119

131-
void SwBuild::load(const std::vector<Input*> &inputs, bool set_eps)
132-
{
133-
std::map<IDriver *, std::vector<Input*>> active_drivers;
120+
// and load packages
134121
for (auto &i : inputs)
135122
{
136-
if (!i->isLoaded())
137-
active_drivers[&i->getDriver()].push_back(i);
138-
}
139-
for (auto &[d, g] : active_drivers)
140-
{
141-
std::vector<RawInput> inputs;
142-
for (auto &i : g)
143-
inputs.push_back(*i);
144-
auto eps = d->load(getContext(), inputs);
145-
if (eps.size() != inputs.size())
146-
throw SW_RUNTIME_ERROR("Incorrect number of returned entry points");
147-
for (size_t i = 0; i < eps.size(); i++)
123+
auto tgts = i.load(*this);
124+
for (auto &tgt : tgts)
148125
{
149-
// when loading installed package, eps[i] may be empty
150-
// so we take ep from context
151-
if (eps[i].empty())
152-
{
153-
if (inputs[i].getType() != InputType::InstalledPackage)
154-
throw SW_RUNTIME_ERROR("unexpected input type");
155-
g[i]->addEntryPoint(swctx.getTargetData(inputs[i].getPackageId()).getEntryPoint());
156-
}
157-
for (auto &ep : eps[i])
158-
{
159-
g[i]->addEntryPoint(ep);
160-
if (inputs[i].getType() != InputType::InstalledPackage)
161-
continue;
162-
if (!set_eps)
163-
continue;
164-
// for packages we must also register all other group packages
165-
// which are located in this config AND which are deps of this input package id
166-
auto m = getContext().resolve(UnresolvedPackages{ inputs[i].getPackageId() });
167-
auto &p = m.find(inputs[i].getPackageId())->second;
168-
for (auto &d : p.getData().dependencies)
169-
{
170-
auto &p2 = m.find(d)->second;
171-
if (p2.getData().group_number != p.getData().group_number)
172-
continue;
173-
swctx.getTargetData(p2).setEntryPoint(ep);
174-
}
175-
}
126+
if (tgt->getSettings()["dry-run"] == "true")
127+
continue;
128+
getTargets()[tgt->getPackage()].push_back(tgt);
176129
}
130+
177131
}
178132
}
179133

@@ -236,15 +190,15 @@ void SwBuild::resolvePackages()
236190
auto m = install(upkgs);
237191

238192
// now we know all drivers
239-
std::vector<Input*> inputs;
193+
std::vector<Input *> iv;
240194
for (auto &[u, p] : m)
241195
{
242196
// use addInput to prevent doubling already existing and loaded inputs
243197
// like when we loading dependency that is already loaded from the input
244198
// test: sw build org.sw.demo.gnome.pango.pangocairo-1.44
245-
inputs.push_back(&addInput(p));
199+
iv.push_back(&swctx.addInput(p));
246200
}
247-
load(inputs, false);
201+
swctx.loadEntryPoints(iv, false);
248202
}
249203

250204
void SwBuild::loadPackages()
@@ -310,8 +264,16 @@ void SwBuild::loadPackages(const TargetMap &predefined)
310264
LOG_TRACE(logger, "build id " << this << " " __FUNCTION__ << " loading " << d.first.toString());
311265

312266
loaded = true;
313-
swctx.getTargetData(d.first).loadPackages(*this, s, known_packages);
267+
268+
auto tgts = swctx.getTargetData(d.first).loadPackages(*this, s, known_packages);
314269
//swctx.getTargetData(d.first).loadPackages(*this, s, { d.first });
270+
for (auto &tgt : tgts)
271+
{
272+
if (tgt->getSettings()["dry-run"] == "true")
273+
continue;
274+
getTargets()[tgt->getPackage()].push_back(tgt);
275+
}
276+
315277
auto k = d.second->find(s);
316278
if (k == d.second->end())
317279
{
@@ -521,41 +483,13 @@ String SwBuild::getHash() const
521483
{
522484
String s;
523485
for (auto &i : inputs)
524-
s += i->getHash();
486+
s += i.getHash();
525487
return shorten_hash(blake2b_512(s), 8);
526488
}
527489

528-
Input &SwBuild::addInput(const String &i)
490+
void SwBuild::addInput(const InputWithSettings &i)
529491
{
530-
path p(i);
531-
if (fs::exists(p))
532-
return addInput(p);
533-
else
534-
return addInput(getContext().resolve(i));
535-
}
536-
537-
Input &SwBuild::addInput(const path &i)
538-
{
539-
return addInput1(i);
540-
}
541-
542-
Input &SwBuild::addInput(const PackageId &i)
543-
{
544-
return addInput1(i);
545-
}
546-
547-
template <class I>
548-
Input &SwBuild::addInput1(const I &i)
549-
{
550-
auto input = std::make_unique<Input>(i, getContext());
551-
auto it = std::find_if(inputs.begin(), inputs.end(), [&i = *input](const auto &p)
552-
{
553-
return *p == i;
554-
});
555-
if (it != inputs.end())
556-
return **it;
557-
inputs.push_back(std::move(input));
558-
return *inputs.back();
492+
inputs.push_back(i);
559493
}
560494

561495
path SwBuild::getExecutionPlanPath() const

src/sw/core/build.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace sw
1414
{
1515

1616
struct ExecutionPlan;
17-
struct IDriver;
1817
struct Input;
18+
struct InputWithSettings;
1919
struct SwContext;
2020

2121
enum class BuildState
@@ -41,9 +41,7 @@ struct SW_CORE_API SwBuild
4141
SwContext &getContext() { return swctx; }
4242
const SwContext &getContext() const { return swctx; }
4343

44-
Input &addInput(const String &);
45-
Input &addInput(const path &);
46-
Input &addInput(const PackageId &);
44+
void addInput(const InputWithSettings &);
4745

4846
// complete
4947
void build();
@@ -85,23 +83,19 @@ struct SW_CORE_API SwBuild
8583
void addKnownPackage(const PackageId &);
8684

8785
private:
88-
using InputPtr = std::unique_ptr<Input>;
89-
using Inputs = std::vector<InputPtr>;
86+
using Inputs = std::vector<InputWithSettings>;
9087

9188
SwContext &swctx;
9289
path build_dir;
93-
Inputs inputs;
9490
TargetMap targets;
9591
TargetMap targets_to_build;
9692
PackageIdSet known_packages;
93+
Inputs inputs;
9794
mutable BuildState state = BuildState::NotStarted;
9895

99-
void load(const std::vector<Input*> &inputs, bool set_eps);
100-
void load(Inputs &inputs, bool set_eps);
96+
void load(const Inputs &inputs, bool set_eps);
10197
Commands getCommands() const;
10298
void loadPackages(const TargetMap &predefined);
103-
template <class I>
104-
Input &addInput1(const I &);
10599
};
106100

107101
} // namespace sw

src/sw/core/c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool CDriver::canLoad(const RawInput &) const
5353
return d.can_load(0);
5454
}
5555

56-
CDriver::EntryPointsVector CDriver::load(SwContext &, const std::vector<RawInput> &) const
56+
CDriver::EntryPointsVector CDriver::createEntryPoints(SwContext &, const std::vector<RawInput> &) const
5757
{
5858
SW_UNIMPLEMENTED;
5959
d.load(0);

src/sw/core/c.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct SW_CORE_API CDriver : IDriver
2121

2222
PackageId getPackageId() const override;
2323
bool canLoad(const RawInput &) const override;
24-
EntryPointsVector load(SwContext &, const std::vector<RawInput> &) const override;
24+
EntryPointsVector createEntryPoints(SwContext &, const std::vector<RawInput> &) const override;
2525

2626
private:
2727
sw_driver_t d;

src/sw/core/core.natvis

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
3+
4+
<Type Name="sw::RawInput">
5+
<DisplayString>
6+
{data}
7+
</DisplayString>
8+
</Type>
9+
10+
</AutoVisualizer>

src/sw/core/driver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ struct SW_CORE_API IDriver
3131
/// test if driver is able to load this input
3232
virtual bool canLoad(const RawInput &) const = 0;
3333

34-
// load entry points for inputs
34+
// create entry points for inputs
3535
// inputs are unique and non null
3636
// inputs will receive their entry points
3737
// result is number of vectors of entry points equal to inputs, in the same order
3838
// one input may provide several entry points (yml)
3939
// we return shared points because we cannot load them into context because package ids is not known in advance
4040
// (in case of loading not installed package)
4141
// if entry points were already loaded (like for installed packages), internal vector may be empty
42-
virtual EntryPointsVector load(SwContext &, const std::vector<RawInput> &) const = 0;
42+
[[nodiscard]]
43+
virtual EntryPointsVector createEntryPoints(SwContext &, const std::vector<RawInput> &) const = 0;
4344

4445
// get raw spec
4546
// complex return value?

0 commit comments

Comments
 (0)