Skip to content

Commit f51e232

Browse files
authored
Refactor Register constructable class to Reg static class (#204)
1 parent b9df847 commit f51e232

File tree

34 files changed

+971
-832
lines changed

34 files changed

+971
-832
lines changed

bootstrap/include/bootstrap/build_buildcc.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ class BuildBuildCC {
5959
static constexpr const char *const kBuildccLibName = "libbuildcc";
6060

6161
public:
62-
BuildBuildCC(Register &reg, const BaseToolchain &toolchain,
63-
const TargetEnv &env)
64-
: reg_(reg), toolchain_(toolchain), env_(env) {}
62+
BuildBuildCC(const BaseToolchain &toolchain, const TargetEnv &env)
63+
: toolchain_(toolchain), env_(env) {}
6564
BuildBuildCC(const BuildBuildCC &) = delete;
6665

6766
void Setup(const ArgToolchainState &state);
@@ -75,7 +74,6 @@ class BuildBuildCC {
7574
}
7675

7776
private:
78-
Register &reg_;
7977
const BaseToolchain &toolchain_;
8078
TargetEnv env_;
8179

bootstrap/include/bootstrap/build_tpl.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121

2222
namespace buildcc {
2323

24-
struct TplConfig {
25-
TplConfig() = default;
26-
27-
OsId os_id{OsId::Linux};
28-
};
29-
30-
void tpl_cb(BaseTarget &target, const TplConfig &config = TplConfig());
24+
void tpl_cb(BaseTarget &target);
3125

3226
} // namespace buildcc
3327

bootstrap/main.buildcc.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,32 @@ int main(int argc, char **argv) {
3838
.AddToolchain("host", "Host Toolchain", custom_toolchain_arg)
3939
.Parse(argc, argv);
4040

41-
Register reg;
42-
reg.Clean(clean_cb);
41+
Reg::Init();
42+
Reg::Call(Args::Clean()).Func(clean_cb);
4343

4444
BaseToolchain toolchain = custom_toolchain_arg.ConstructToolchain();
45+
toolchain.Verify();
4546

4647
BuildBuildCC buildcc(
47-
reg, toolchain, TargetEnv(Project::GetRootDir(), Project::GetBuildDir()));
48+
toolchain, TargetEnv(Project::GetRootDir(), Project::GetBuildDir()));
4849
buildcc.Setup(custom_toolchain_arg.state);
4950

5051
const auto &buildcc_lib = buildcc.GetBuildcc();
5152
ExecutableTarget_generic buildcc_hybrid_simple_example(
5253
"buildcc_hybrid_simple_example", toolchain, "example/hybrid/simple");
53-
reg.Build(custom_toolchain_arg.state, hybrid_simple_example_cb,
54-
buildcc_hybrid_simple_example, buildcc_lib);
55-
reg.Dep(buildcc_hybrid_simple_example, buildcc_lib);
54+
Reg::Toolchain(custom_toolchain_arg.state)
55+
.Build(hybrid_simple_example_cb, buildcc_hybrid_simple_example,
56+
buildcc_lib)
57+
.Dep(buildcc_hybrid_simple_example, buildcc_lib);
5658

5759
// Runners
58-
reg.RunBuild();
59-
reg.RunTest();
60+
Reg::Run();
6061

6162
// - Clang Compile Commands
6263
plugin::ClangCompileCommands({&buildcc_lib}).Generate();
6364

6465
// - Plugin Graph
65-
std::string output = reg.GetTaskflow().dump();
66+
std::string output = Reg::GetTaskflow().dump();
6667
const bool saved = env::save_file("graph.dot", output, false);
6768
env::assert_fatal(saved, "Could not save graph.dot file");
6869

bootstrap/src/build_buildcc.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ void buildcc_cb(BaseTarget &target, const BaseGenerator &schema_gen,
131131
if constexpr (env::is_win()) {
132132
// TODO, Clang
133133
switch (target.GetToolchain().GetId()) {
134-
case ToolchainId::Gcc:
135134
case ToolchainId::MinGW: {
136135
target.AddPreprocessorFlag("-DFMT_HEADER_ONLY=1");
137136
target.AddPreprocessorFlag("-DSPDLOG_FMT_EXTERNAL");
@@ -147,7 +146,7 @@ void buildcc_cb(BaseTarget &target, const BaseGenerator &schema_gen,
147146
}
148147
}
149148

150-
if constexpr (env::is_linux()) {
149+
if constexpr (env::is_linux() || env::is_unix() || env::is_clang()) {
151150
// TODO, Clang
152151
switch (target.GetToolchain().GetId()) {
153152
case ToolchainId::Gcc: {
@@ -196,51 +195,41 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
196195
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
197196
env_.GetTargetBuildDir()));
198197

199-
reg_.CallbackIf(state, global_flags_cb, flatc_exe, toolchain_);
200-
reg_.Build(state, build_flatc_exe_cb, flatc_exe);
201-
202198
// Schema
203199
auto &schema_gen = storage_.Add<BaseGenerator>(
204200
kSchemaGenName, kSchemaGenName,
205201
TargetEnv(env_.GetTargetRootDir() / "buildcc" / "schema",
206202
env_.GetTargetBuildDir() / toolchain_.GetName()));
207-
reg_.Build(schema_gen_cb, schema_gen, flatc_exe);
208-
reg_.Dep(schema_gen, flatc_exe);
209203

210204
// Flatbuffers HO lib
211205
auto &flatbuffers_ho_lib = storage_.Add<TargetInfo>(
212206
kFlatbuffersHoName, toolchain_,
213207
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
214208
env_.GetTargetBuildDir()));
215-
reg_.CallbackIf(state, flatbuffers_ho_cb, flatbuffers_ho_lib);
216209

217210
// CLI11 HO lib
218211
auto &cli11_ho_lib = storage_.Add<TargetInfo>(
219212
kCli11HoName, toolchain_,
220213
TargetEnv(env_.GetTargetRootDir() / "third_party" / "CLI11",
221214
env_.GetTargetBuildDir()));
222-
reg_.CallbackIf(state, cli11_ho_cb, cli11_ho_lib);
223215

224216
// fmt HO lib
225217
auto &fmt_ho_lib = storage_.Add<TargetInfo>(
226218
kFmtHoName, toolchain_,
227219
TargetEnv(env_.GetTargetRootDir() / "third_party" / "fmt",
228220
env_.GetTargetBuildDir()));
229-
reg_.CallbackIf(state, fmt_ho_cb, fmt_ho_lib);
230221

231222
// spdlog HO lib
232223
auto &spdlog_ho_lib = storage_.Add<TargetInfo>(
233224
kSpdlogHoName, toolchain_,
234225
TargetEnv(env_.GetTargetRootDir() / "third_party" / "spdlog",
235226
env_.GetTargetBuildDir()));
236-
reg_.CallbackIf(state, spdlog_ho_cb, spdlog_ho_lib);
237227

238228
// taskflow HO lib
239229
auto &taskflow_ho_lib = storage_.Add<TargetInfo>(
240230
kTaskflowHoName, toolchain_,
241231
TargetEnv(env_.GetTargetRootDir() / "third_party" / "taskflow",
242232
env_.GetTargetBuildDir()));
243-
reg_.CallbackIf(state, taskflow_ho_cb, taskflow_ho_lib);
244233

245234
// Tiny-process-library lib
246235
// TODO, Make this a generic selection between StaticTarget and
@@ -250,21 +239,31 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
250239
TargetEnv(env_.GetTargetRootDir() / "third_party" /
251240
"tiny-process-library",
252241
env_.GetTargetBuildDir()));
253-
reg_.CallbackIf(state, global_flags_cb, tpl_lib, toolchain_);
254-
TplConfig tpl_config;
255-
tpl_config.os_id = get_host_os();
256-
reg_.Build(state, tpl_cb, tpl_lib, tpl_config);
257242

243+
// BuildCC lib
258244
// TODO, Make this a generic selection between StaticTarget and
259245
// DynamicTarget
260246
auto &buildcc_lib = storage_.Add<StaticTarget_generic>(
261247
kBuildccLibName, kBuildccLibName, toolchain_,
262248
TargetEnv(env_.GetTargetRootDir() / "buildcc", env_.GetTargetBuildDir()));
263-
reg_.CallbackIf(state, global_flags_cb, buildcc_lib, toolchain_);
264-
reg_.Build(state, buildcc_cb, buildcc_lib, schema_gen, flatbuffers_ho_lib,
265-
fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib, taskflow_ho_lib, tpl_lib);
266-
reg_.Dep(buildcc_lib, schema_gen);
267-
reg_.Dep(buildcc_lib, tpl_lib);
249+
250+
Reg::Toolchain(state)
251+
.Func(global_flags_cb, flatc_exe, toolchain_)
252+
.Build(build_flatc_exe_cb, flatc_exe)
253+
.Build(schema_gen_cb, schema_gen, flatc_exe)
254+
.Dep(schema_gen, flatc_exe)
255+
.Func(flatbuffers_ho_cb, flatbuffers_ho_lib)
256+
.Func(cli11_ho_cb, cli11_ho_lib)
257+
.Func(fmt_ho_cb, fmt_ho_lib)
258+
.Func(spdlog_ho_cb, spdlog_ho_lib)
259+
.Func(taskflow_ho_cb, taskflow_ho_lib)
260+
.Func(global_flags_cb, tpl_lib, toolchain_)
261+
.Build(tpl_cb, tpl_lib)
262+
.Func(global_flags_cb, buildcc_lib, toolchain_)
263+
.Build(buildcc_cb, buildcc_lib, schema_gen, flatbuffers_ho_lib,
264+
fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib, taskflow_ho_lib, tpl_lib)
265+
.Dep(buildcc_lib, schema_gen)
266+
.Dep(buildcc_lib, tpl_lib);
268267
}
269268

270269
} // namespace buildcc

bootstrap/src/build_tpl.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@
1818

1919
namespace buildcc {
2020

21-
void tpl_cb(BaseTarget &target, const TplConfig &config) {
21+
void tpl_cb(BaseTarget &target) {
2222
target.AddSource("process.cpp");
2323
target.AddIncludeDir("");
2424
target.AddHeader("process.hpp");
2525

26-
switch (config.os_id) {
27-
case OsId::Win:
26+
if constexpr (env::is_win()) {
2827
target.AddSource("process_win.cpp");
29-
break;
30-
case OsId::Linux:
31-
case OsId::Unix:
32-
case OsId::Mac:
28+
}
29+
30+
if constexpr (env::is_linux() || env::is_unix() || env::is_clang()) {
3331
target.AddSource("process_unix.cpp");
34-
break;
35-
default:
36-
break;
3732
}
3833

3934
target.Build();

buildcc/lib/args/include/args/args.h

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727

2828
#include "toolchain/toolchain.h"
2929

30+
#include "target/common/target_config.h"
31+
3032
namespace fs = std::filesystem;
3133

3234
namespace buildcc {
3335

3436
/**
35-
* @brief Toolchain State used by the Register module to selectively build or
36-
* test targets
37+
* @brief Toolchain State used to selectively build and test targets
3738
*/
3839
struct ArgToolchainState {
3940
bool build{false};
@@ -81,8 +82,15 @@ struct ArgToolchain {
8182
struct ArgTarget {
8283
ArgTarget(){};
8384

84-
std::string compile_command{""};
85-
std::string link_command{""};
85+
TargetConfig GetTargetConfig() {
86+
TargetConfig config;
87+
config.compile_command = compile_command;
88+
config.link_command = link_command;
89+
return config;
90+
}
91+
92+
std::string compile_command;
93+
std::string link_command;
8694
};
8795

8896
struct ArgCustom {
@@ -91,56 +99,8 @@ struct ArgCustom {
9199

92100
class Args {
93101
private:
94-
class Instance {
95-
public:
96-
/**
97-
* @brief Parse command line information to CLI11
98-
*
99-
* @param argc from int main(int argc, char ** argv)
100-
* @param argv from int main(int argc, char ** argv)
101-
*/
102-
static void Parse(int argc, const char *const *argv);
103-
104-
/**
105-
* @brief Add toolchain with a unique name and description
106-
*
107-
* @param out Receive the toolchain information through the CLI
108-
* @param initial Set the default toolchain information as a fallback
109-
*/
110-
Instance &AddToolchain(const std::string &name,
111-
const std::string &description, ArgToolchain &out,
112-
const ArgToolchain &initial = ArgToolchain());
113-
114-
/**
115-
* @brief Add toolchain with a unique name and description
116-
*
117-
* @param out Receive the toolchain information through the CLI
118-
* @param initial Set the default toolchain information as a fallback
119-
*/
120-
Instance &AddTarget(const std::string &name, const std::string &description,
121-
ArgTarget &out, const ArgTarget &initial = ArgTarget());
122-
123-
/**
124-
* @brief Custom callback for data
125-
*
126-
* @param add_cb Add callback that exposes underlying CLI::App
127-
*/
128-
Instance &AddCustomCallback(const std::function<void(CLI::App &)> &add_cb);
129-
130-
/**
131-
* @brief Add custom data
132-
*
133-
* @param data Derive from `buildcc::ArgCustom` and override the `Add` API
134-
*/
135-
Instance &AddCustomData(ArgCustom &data);
136-
};
137-
138-
struct Internal {
139-
Instance instance;
140-
CLI::App app{"BuildCC Buildsystem"};
141-
CLI::App *toolchain{nullptr};
142-
CLI::App *target{nullptr};
143-
};
102+
class Instance;
103+
struct Internal;
144104

145105
public:
146106
Args() = delete;
@@ -151,6 +111,8 @@ class Args {
151111
static void Deinit();
152112

153113
// Getters
114+
static bool IsInit();
115+
static bool IsParsed();
154116
static bool Clean();
155117
static env::LogLevel GetLogLevel();
156118

@@ -159,12 +121,64 @@ class Args {
159121

160122
private:
161123
static void RootArgs();
162-
static CLI::App &Ref();
124+
static Internal &RefInternal();
125+
static CLI::App &RefApp();
163126

164127
private:
165128
static std::unique_ptr<Internal> internal_;
166129
};
167130

131+
class Args::Instance {
132+
public:
133+
/**
134+
* @brief Parse command line information to CLI11
135+
*
136+
* @param argc from int main(int argc, char ** argv)
137+
* @param argv from int main(int argc, char ** argv)
138+
*/
139+
static void Parse(int argc, const char *const *argv);
140+
141+
/**
142+
* @brief Add toolchain with a unique name and description
143+
*
144+
* @param out Receive the toolchain information through the CLI
145+
* @param initial Set the default toolchain information as a fallback
146+
*/
147+
Instance &AddToolchain(const std::string &name,
148+
const std::string &description, ArgToolchain &out,
149+
const ArgToolchain &initial = ArgToolchain());
150+
151+
/**
152+
* @brief Add toolchain with a unique name and description
153+
*
154+
* @param out Receive the toolchain information through the CLI
155+
* @param initial Set the default toolchain information as a fallback
156+
*/
157+
Instance &AddTarget(const std::string &name, const std::string &description,
158+
ArgTarget &out, const ArgTarget &initial = ArgTarget());
159+
160+
/**
161+
* @brief Custom callback for data
162+
*
163+
* @param add_cb Add callback that exposes underlying CLI::App
164+
*/
165+
Instance &AddCustomCallback(const std::function<void(CLI::App &)> &add_cb);
166+
167+
/**
168+
* @brief Add custom data
169+
*
170+
* @param data Derive from `buildcc::ArgCustom` and override the `Add` API
171+
*/
172+
Instance &AddCustomData(ArgCustom &data);
173+
};
174+
175+
struct Args::Internal {
176+
Instance instance;
177+
CLI::App app{"BuildCC Buildsystem"};
178+
CLI::App *toolchain{nullptr};
179+
CLI::App *target{nullptr};
180+
};
181+
168182
} // namespace buildcc
169183

170184
#endif

0 commit comments

Comments
 (0)