Skip to content

Commit d350a4e

Browse files
committed
Refactor driver::Build. Remove many functions.
1 parent b5dc80f commit d350a4e

11 files changed

Lines changed: 118 additions & 172 deletions

File tree

src/sw/builder/command.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(target).Storage.push_back(name)
1919

2020
#define SW_MAKE_CUSTOM_COMMAND(type, name, target, ...) \
21-
auto name = std::make_shared<type>((target).getSolution().getContext(), __VA_ARGS__)
21+
auto name = std::make_shared<type>((target).getMainBuild().getContext(), __VA_ARGS__)
2222

2323
#ifdef _MSC_VER
2424
#define SW_MAKE_CUSTOM_COMMAND_AND_ADD(type, name, target, ...) \

src/sw/driver/build.cpp

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -55,76 +55,32 @@ static void sw_check_abi_version(int v)
5555
}
5656

5757
Build::Build(SwBuild &mb)
58-
: checker(*this)
58+
: checker(mb)
5959
{
6060
main_build_ = &mb;
6161
}
6262

63-
SwContext &Build::getContext() const
64-
{
65-
return getMainBuild().getContext();
66-
}
67-
68-
TargetMap &Build::getChildren()
69-
{
70-
return getMainBuild().getTargets();
71-
}
72-
73-
const TargetMap &Build::getChildren() const
74-
{
75-
return getMainBuild().getTargets();
76-
}
77-
78-
const OS &Build::getHostOs() const
79-
{
80-
return getContext().HostOS;
81-
}
82-
83-
path Build::getChecksDir() const
84-
{
85-
return getServiceDir() / "checks";
86-
}
87-
88-
void Build::setModuleData(ModuleSwappableData &d)
89-
{
90-
module_data = &d;
91-
}
92-
93-
ModuleSwappableData &Build::getModuleData() const
94-
{
95-
if (!module_data)
96-
throw SW_LOGIC_ERROR("no module data was set");
97-
return *module_data;
98-
}
99-
100-
const TargetSettings &Build::getSettings() const
101-
{
102-
return getModuleData().current_settings;
103-
}
104-
10563
// can be used in configs to load subdir configs
10664
// s.build->loadModule("client/sw.cpp").call<void(Solution &)>("build", s);
107-
Module Build::loadModule(const path &p) const
65+
/*Module Build::loadModule(const path &p) const
10866
{
109-
SW_UNIMPLEMENTED;
110-
11167
auto fn2 = p;
11268
if (!fn2.is_absolute())
11369
fn2 = SourceDir / fn2;
11470
// driver->build_cpp_spec(swctx, p);
11571
//return getContext().getModuleStorage().get(dll);
116-
}
72+
}*/
11773

118-
bool Build::skipTarget(TargetScope Scope) const
74+
/*bool Build::skipTarget(TargetScope Scope) const
11975
{
12076
return false;
121-
}
77+
}*/
12278

12379
bool Build::isKnownTarget(const LocalPackage &p) const
12480
{
125-
return getModuleData().known_targets.empty() ||
81+
return module_data.known_targets.empty() ||
12682
p.getPath().is_loc() || // used by cfg targets and checks
127-
getModuleData().known_targets.find(p) != getModuleData().known_targets.end();
83+
module_data.known_targets.find(p) != module_data.known_targets.end();
12884
}
12985

13086
path Build::getSourceDir(const LocalPackage &p) const
@@ -142,10 +98,9 @@ std::optional<path> Build::getSourceDir(const Source &s, const Version &v) const
14298
return {};
14399
}
144100

145-
path Build::getTestDir() const
146-
{
147-
SW_UNIMPLEMENTED;
148-
//return BinaryDir / "test" / getSettings().getConfig();
101+
/*path Build::getTestDir() const
102+
{
103+
return BinaryDir / "test" / getSettings().getConfig();
149104
}
150105
151106
void Build::addTest(Test &cb, const String &name)
@@ -170,13 +125,12 @@ Test Build::addTest(const ExecutableTarget &t)
170125
}
171126
172127
Test Build::addTest(const String &name, const ExecutableTarget &tgt)
173-
{
174-
SW_UNIMPLEMENTED;
175-
/*auto c = tgt.addCommand();
128+
{
129+
auto c = tgt.addCommand();
176130
c << cmd::prog(tgt);
177131
Test t(c);
178132
addTest(t, name);
179-
return t;*/
133+
return t;
180134
}
181135
182136
Test Build::addTest()
@@ -189,6 +143,6 @@ Test Build::addTest(const String &name)
189143
Test cb(getContext());
190144
addTest(cb, name);
191145
return cb;
192-
}
146+
}*/
193147

194148
}

src/sw/driver/build.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ namespace driver::cpp { struct Driver; }
2727
struct Module;
2828
struct ModuleStorage;
2929
struct SwContext;
30-
struct ModuleSwappableData;
3130

3231
using FilesMap = std::unordered_map<path, path>;
3332

33+
struct ModuleSwappableData
34+
{
35+
PackageIdSet known_targets;
36+
TargetSettings current_settings;
37+
std::vector<ITargetPtr> added_targets;
38+
};
39+
3440
struct SW_DRIVER_CPP_API Test : driver::CommandBuilder
3541
{
3642
using driver::CommandBuilder::CommandBuilder;
@@ -55,41 +61,36 @@ struct SW_DRIVER_CPP_API Build : SimpleBuild
5561
{
5662
using Base = SimpleBuild;
5763

58-
ModuleSwappableData *module_data = nullptr;
64+
ModuleSwappableData module_data;
5965
SourceDirMap source_dirs_by_source;
6066
std::unordered_map<PackageId, path> source_dirs_by_package;
6167
Checker checker;
6268

63-
SwContext &getContext() const;
64-
const OS &getHostOs() const;
65-
const TargetSettings &getSettings() const;
69+
// old
70+
71+
//
6672
bool isKnownTarget(const LocalPackage &p) const;
6773
path getSourceDir(const LocalPackage &p) const;
6874
std::optional<path> getSourceDir(const Source &s, const Version &v) const;
69-
bool skipTarget(TargetScope Scope) const;
70-
TargetMap &getChildren();
71-
const TargetMap &getChildren() const;
72-
path getChecksDir() const;
73-
void setModuleData(ModuleSwappableData &);
74-
ModuleSwappableData &getModuleData() const;
75+
//bool skipTarget(TargetScope Scope) const;
7576

7677
// tests
7778
// TODO: implement some of https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-tests
78-
Commands tests;
79+
/*Commands tests;
7980
Test addTest(const ExecutableTarget &t);
8081
Test addTest(const String &name, const ExecutableTarget &t);
8182
Test addTest();
8283
Test addTest(const String &name);
8384
path getTestDir() const;
8485
8586
private:
86-
void addTest(Test &cb, const String &name);
87+
//void addTest(Test &cb, const String &name);*/
8788

8889
//
8990
public:
9091
Build(SwBuild &);
9192

92-
Module loadModule(const path &fn) const;
93+
//Module loadModule(const path &fn) const;
9394

9495
// move to some other place?
9596
void cppan_load(yaml &root, const String &root_name = {});

src/sw/driver/checks.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ static cl::opt<String> cc_checks_command("cc-checks-command", cl::desc("Automati
3434
namespace sw
3535
{
3636

37+
static path getServiceDir(const path &bdir)
38+
{
39+
return bdir / "misc";
40+
}
41+
42+
static path getChecksDir(const path &bdir)
43+
{
44+
return getServiceDir(bdir) / "checks";
45+
}
46+
3747
static String toString(CheckType t)
3848
{
3949
switch (t)
@@ -231,8 +241,8 @@ CheckSet::CheckSet(Checker &checker)
231241
{
232242
}
233243

234-
Checker::Checker(Build &build)
235-
: build(build)
244+
Checker::Checker(SwBuild &swbld)
245+
: swbld(swbld)
236246
{
237247
}
238248

@@ -246,7 +256,7 @@ CheckSet &Checker::addSet(const String &name)
246256

247257
void CheckSet::performChecks(const TargetSettings &ts)
248258
{
249-
static const auto checks_dir = checker.build.getContext().getLocalStorage().storage_dir_etc / "sw" / "checks";
259+
static const auto checks_dir = checker.swbld.getContext().getLocalStorage().storage_dir_etc / "sw" / "checks";
250260

251261
//std::unique_lock lk(m);
252262

@@ -381,7 +391,7 @@ int main() { return IsBigEndian(); }
381391
{
382392
// remove tmp dir
383393
error_code ec;
384-
fs::remove_all(checker.build.getChecksDir(), ec);
394+
fs::remove_all(getChecksDir(checker.swbld.getBuildDirectory()), ec);
385395
};
386396

387397
//auto &e = getExecutor();
@@ -555,7 +565,7 @@ int main() { return IsBigEndian(); }
555565
}
556566
s += "}";
557567

558-
auto d = checker.build.getServiceDir();
568+
auto d = getServiceDir(checker.swbld.getBuildDirectory());
559569
auto cyclic_path = d / "cyclic";
560570
write_file(cyclic_path / "deps_checks.dot", s);
561571

@@ -671,7 +681,7 @@ bool Check::lessDuringExecution(const CommandNode &in) const
671681

672682
path Check::getOutputFilename() const
673683
{
674-
auto d = check_set->checker.build.getChecksDir();
684+
auto d = getChecksDir(check_set->checker.swbld.getBuildDirectory());
675685
//static std::atomic_int64_t n = 0;
676686
auto up = unique_path();
677687
//auto up = std::to_string(++n);
@@ -796,14 +806,12 @@ int main(int ac, char* av[])
796806
}
797807

798808
#define SETUP_SOLUTION() \
799-
auto b = check_set->checker.build.getContext().createBuild(); \
809+
auto b = check_set->checker.swbld.getContext().createBuild(); \
800810
auto s = setupSolution(*b, f); \
801-
ModuleSwappableData msd; \
802-
msd.current_settings = getSettings(); \
803-
s.setModuleData(msd)
811+
s.module_data.current_settings = getSettings()
804812

805813
#define EXECUTE_SOLUTION() \
806-
for (auto &t : msd.added_targets) \
814+
for (auto &t : s.module_data.added_targets) \
807815
b->getTargets()[t->getPackage()].push_back(t); \
808816
if (!execute(*b)) \
809817
return
@@ -954,7 +962,7 @@ void TypeSize::run() const
954962
return;
955963
}
956964

957-
if (!check_set->t->getSolution().getHostOs().canRunTargetExecutables(check_set->t->getBuildSettings().TargetOS))
965+
if (!check_set->t->getContext().getHostOs().canRunTargetExecutables(check_set->t->getBuildSettings().TargetOS))
958966
{
959967
requires_manual_setup = true;
960968
executable = e.getOutputFile();
@@ -1028,7 +1036,7 @@ void TypeAlignment::run() const
10281036
return;
10291037
}
10301038

1031-
if (!check_set->t->getSolution().getHostOs().canRunTargetExecutables(check_set->t->getBuildSettings().TargetOS))
1039+
if (!check_set->t->getContext().getHostOs().canRunTargetExecutables(check_set->t->getBuildSettings().TargetOS))
10321040
{
10331041
requires_manual_setup = true;
10341042
executable = e.getOutputFile();
@@ -1338,7 +1346,7 @@ void SourceRuns::run() const
13381346
return;
13391347
}
13401348

1341-
if (!check_set->t->getSolution().getHostOs().canRunTargetExecutables(check_set->t->getBuildSettings().TargetOS))
1349+
if (!check_set->t->getContext().getHostOs().canRunTargetExecutables(check_set->t->getBuildSettings().TargetOS))
13421350
{
13431351
requires_manual_setup = true;
13441352
executable = e.getOutputFile();

src/sw/driver/checks.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace sw
2222

2323
struct Build;
2424
struct SwBuild;
25+
struct SwContext;
2526
struct Checker;
2627
struct CheckSet;
2728
struct ChecksStorage;
@@ -344,12 +345,12 @@ struct SW_DRIVER_CPP_API CheckSet
344345

345346
struct SW_DRIVER_CPP_API Checker
346347
{
347-
Build &build;
348+
SwBuild &swbld;
348349

349350
/// child sets
350351
std::unordered_map<String /* set name */, std::shared_ptr<CheckSet>> sets;
351352

352-
Checker(Build &build);
353+
Checker(SwBuild &swbld);
353354

354355
CheckSet &addSet(const String &name);
355356

src/sw/driver/compiler/compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ std::shared_ptr<builder::Command> CompilerBaseProgram::prepareCommand(const Targ
7878
{
7979
if (prepared)
8080
return cmd;
81-
createCommand(t.getSolution().getContext()); // do some init
81+
createCommand(t.getMainBuild().getContext()); // do some init
8282
prepareCommand1(t);
8383
prepared = true;
8484
return cmd;

src/sw/driver/entry_point.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ static std::tuple<FilesOrdered, UnresolvedPackages> getFileDependencies(const Sw
221221

222222
std::vector<ITargetPtr> NativeTargetEntryPoint::loadPackages(SwBuild &swb, const TargetSettings &s, const PackageIdSet &pkgs, const PackagePath &prefix) const
223223
{
224-
// TODO: memory leak
225-
auto nb = new Build(swb);
226-
auto &b = *nb;
224+
Build b(swb);
227225

228226
// we need to fix some settings before they go to targets
229227
auto settings = s;
@@ -237,14 +235,10 @@ std::vector<ITargetPtr> NativeTargetEntryPoint::loadPackages(SwBuild &swb, const
237235
settings["driver"].useInHash(false);
238236
settings["driver"].ignoreInComparison(true);
239237

240-
ModuleSwappableData module_data1;
241-
module_data1.known_targets = pkgs;
242-
module_data1.current_settings = settings;
243-
244-
b.module_data = &module_data1;
238+
b.module_data.known_targets = pkgs;
239+
b.module_data.current_settings = settings;
245240
b.NamePrefix = prefix;
246241

247-
// canonical makes disk letter uppercase on windows
248242
if (!source_dir.empty())
249243
b.setSourceDirectory(source_dir);
250244
else
@@ -253,7 +247,7 @@ std::vector<ITargetPtr> NativeTargetEntryPoint::loadPackages(SwBuild &swb, const
253247

254248
loadPackages1(b);
255249

256-
return module_data1.added_targets;
250+
return b.module_data.added_targets;
257251
}
258252

259253
NativeBuiltinTargetEntryPoint::NativeBuiltinTargetEntryPoint(BuildFunction bf)
@@ -410,7 +404,7 @@ void PrepareConfigEntryPoint::commonActions2(Build &b, SharedLibraryTarget &lib)
410404
lib.Definitions["SW_PACKAGE_API"] = "__attribute__ ((visibility (\"default\")))";
411405
}
412406

413-
BuildSettings bs(b.getModuleData().current_settings);
407+
BuildSettings bs(b.module_data.current_settings);
414408
if (bs.TargetOS.is(OSType::Windows))
415409
lib.NativeLinkerOptions::System.LinkLibraries.insert("Delayimp.lib");
416410

0 commit comments

Comments
 (0)