Skip to content

Commit d22c750

Browse files
authored
Updated examples (#207)
1 parent e41bf00 commit d22c750

File tree

19 files changed

+135
-93
lines changed

19 files changed

+135
-93
lines changed

bootstrap/include/bootstrap/build_buildcc.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class BuildBuildCC {
6060

6161
public:
6262
BuildBuildCC(const BaseToolchain &toolchain, const TargetEnv &env)
63-
: toolchain_(toolchain), env_(env) {}
63+
: toolchain_(toolchain), env_(env) {
64+
Initialize();
65+
}
6466
BuildBuildCC(const BuildBuildCC &) = delete;
6567

6668
void Setup(const ArgToolchainState &state);
@@ -73,6 +75,24 @@ class BuildBuildCC {
7375
return storage_.Ref<StaticTarget_generic>(kBuildccLibName);
7476
}
7577

78+
private:
79+
void Initialize();
80+
ExecutableTarget_generic &GetFlatc() {
81+
return storage_.Ref<ExecutableTarget_generic>(kFlatcExeName);
82+
}
83+
BaseGenerator &GetSchemaGen() {
84+
return storage_.Ref<BaseGenerator>(kSchemaGenName);
85+
}
86+
TargetInfo &GetFlatbuffersHo() {
87+
return storage_.Ref<TargetInfo>(kFlatbuffersHoName);
88+
}
89+
TargetInfo &GetCli11Ho() { return storage_.Ref<TargetInfo>(kCli11HoName); }
90+
TargetInfo &GetFmtHo() { return storage_.Ref<TargetInfo>(kFmtHoName); }
91+
TargetInfo &GetSpdlogHo() { return storage_.Ref<TargetInfo>(kSpdlogHoName); }
92+
TargetInfo &GetTaskflowHo() {
93+
return storage_.Ref<TargetInfo>(kTaskflowHoName);
94+
}
95+
7696
private:
7797
const BaseToolchain &toolchain_;
7898
TargetEnv env_;

bootstrap/main.buildcc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ int main(int argc, char **argv) {
4242
Reg::Call(Args::Clean()).Func(clean_cb);
4343

4444
BaseToolchain toolchain = custom_toolchain_arg.ConstructToolchain();
45-
toolchain.Verify();
4645

4746
BuildBuildCC buildcc(
4847
toolchain, TargetEnv(Project::GetRootDir(), Project::GetBuildDir()));
49-
buildcc.Setup(custom_toolchain_arg.state);
48+
auto &buildcc_lib = buildcc.GetBuildcc();
5049

51-
const auto &buildcc_lib = buildcc.GetBuildcc();
5250
ExecutableTarget_generic buildcc_hybrid_simple_example(
5351
"buildcc_hybrid_simple_example", toolchain, "example/hybrid/simple");
5452
Reg::Toolchain(custom_toolchain_arg.state)
53+
.Func([&]() { toolchain.Verify(); })
54+
.BuildPackage(buildcc)
5555
.Build(hybrid_simple_example_cb, buildcc_hybrid_simple_example,
5656
buildcc_lib)
5757
.Dep(buildcc_hybrid_simple_example, buildcc_lib);

bootstrap/src/build_buildcc.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,52 +189,52 @@ static void global_flags_cb(TargetInfo &global_info,
189189
}
190190
}
191191

192-
void BuildBuildCC::Setup(const ArgToolchainState &state) {
193-
auto &flatc_exe = storage_.Add<ExecutableTarget_generic>(
192+
void BuildBuildCC::Initialize() {
193+
(void)storage_.Add<ExecutableTarget_generic>(
194194
kFlatcExeName, kFlatcExeName, toolchain_,
195195
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
196196
env_.GetTargetBuildDir()));
197197

198198
// Schema
199-
auto &schema_gen = storage_.Add<BaseGenerator>(
199+
(void)storage_.Add<BaseGenerator>(
200200
kSchemaGenName, kSchemaGenName,
201201
TargetEnv(env_.GetTargetRootDir() / "buildcc" / "schema",
202202
env_.GetTargetBuildDir() / toolchain_.GetName()));
203203

204204
// Flatbuffers HO lib
205-
auto &flatbuffers_ho_lib = storage_.Add<TargetInfo>(
205+
(void)storage_.Add<TargetInfo>(
206206
kFlatbuffersHoName, toolchain_,
207207
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
208208
env_.GetTargetBuildDir()));
209209

210210
// CLI11 HO lib
211-
auto &cli11_ho_lib = storage_.Add<TargetInfo>(
211+
(void)storage_.Add<TargetInfo>(
212212
kCli11HoName, toolchain_,
213213
TargetEnv(env_.GetTargetRootDir() / "third_party" / "CLI11",
214214
env_.GetTargetBuildDir()));
215215

216216
// fmt HO lib
217-
auto &fmt_ho_lib = storage_.Add<TargetInfo>(
217+
(void)storage_.Add<TargetInfo>(
218218
kFmtHoName, toolchain_,
219219
TargetEnv(env_.GetTargetRootDir() / "third_party" / "fmt",
220220
env_.GetTargetBuildDir()));
221221

222222
// spdlog HO lib
223-
auto &spdlog_ho_lib = storage_.Add<TargetInfo>(
223+
(void)storage_.Add<TargetInfo>(
224224
kSpdlogHoName, toolchain_,
225225
TargetEnv(env_.GetTargetRootDir() / "third_party" / "spdlog",
226226
env_.GetTargetBuildDir()));
227227

228228
// taskflow HO lib
229-
auto &taskflow_ho_lib = storage_.Add<TargetInfo>(
229+
(void)storage_.Add<TargetInfo>(
230230
kTaskflowHoName, toolchain_,
231231
TargetEnv(env_.GetTargetRootDir() / "third_party" / "taskflow",
232232
env_.GetTargetBuildDir()));
233233

234234
// Tiny-process-library lib
235235
// TODO, Make this a generic selection between StaticTarget and
236236
// DynamicTarget
237-
auto &tpl_lib = storage_.Add<StaticTarget_generic>(
237+
(void)storage_.Add<StaticTarget_generic>(
238238
kTplLibName, kTplLibName, toolchain_,
239239
TargetEnv(env_.GetTargetRootDir() / "third_party" /
240240
"tiny-process-library",
@@ -243,10 +243,21 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
243243
// BuildCC lib
244244
// TODO, Make this a generic selection between StaticTarget and
245245
// DynamicTarget
246-
auto &buildcc_lib = storage_.Add<StaticTarget_generic>(
246+
(void)storage_.Add<StaticTarget_generic>(
247247
kBuildccLibName, kBuildccLibName, toolchain_,
248248
TargetEnv(env_.GetTargetRootDir() / "buildcc", env_.GetTargetBuildDir()));
249+
}
249250

251+
void BuildBuildCC::Setup(const ArgToolchainState &state) {
252+
auto &flatc_exe = GetFlatc();
253+
auto &schema_gen = GetSchemaGen();
254+
auto &flatbuffers_ho_lib = GetFlatbuffersHo();
255+
auto &cli11_ho_lib = GetCli11Ho();
256+
auto &fmt_ho_lib = GetFmtHo();
257+
auto &spdlog_ho_lib = GetSpdlogHo();
258+
auto &taskflow_ho_lib = GetTaskflowHo();
259+
auto &tpl_lib = GetTpl();
260+
auto &buildcc_lib = GetBuildcc();
250261
Reg::Toolchain(state)
251262
.Func(global_flags_cb, flatc_exe, toolchain_)
252263
.Build(build_flatc_exe_cb, flatc_exe)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ namespace buildcc {
3737
* @brief Toolchain State used to selectively build and test targets
3838
*/
3939
struct ArgToolchainState {
40-
bool build{false};
41-
bool test{false};
40+
ArgToolchainState(bool b = false, bool t = false) : build(b), test(t) {}
41+
bool build;
42+
bool test;
4243
};
4344

4445
/**
@@ -47,7 +48,7 @@ struct ArgToolchainState {
4748
* Bundled with Toolchain State
4849
*/
4950
struct ArgToolchain {
50-
ArgToolchain(){};
51+
ArgToolchain() = default;
5152
ArgToolchain(ToolchainId initial_id, const std::string &initial_name,
5253
const ToolchainExecutables &initial_executables)
5354
: id(initial_id), name(initial_name), executables(initial_executables) {}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ class Reg::ToolchainInstance {
201201
Params &&...params) {
202202
return BuildInternal(build_cb, target, std::forward<Params>(params)...);
203203
}
204+
template <typename P> ToolchainInstance &BuildPackage(P &package) {
205+
return Func([&]() { package.Setup(condition_); });
206+
}
204207
ToolchainInstance &Dep(const internal::BuilderInterface &target,
205208
const internal::BuilderInterface &dependency);
206209
ToolchainInstance &Test(const std::string &command, const BaseTarget &target,

buildcc/lib/env/include/env/storage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class ScopedStorage {
6161
template <typename T> void Remove(T *ptr) { delete ptr; }
6262

6363
template <typename T> const T &ConstRef(const std::string &identifier) const {
64+
env::assert_fatal(ptrs_.find(identifier) != ptrs_.end(),
65+
fmt::format("Could not find '{}'", identifier));
6466
const PtrMetadata &metadata = ptrs_.at(identifier);
6567
env::assert_fatal(
6668
typeid(T).name() == metadata.typeid_name,

buildexe/buildexe.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ int main(int argc, char **argv) {
6767

6868
// Build Target
6969
BuildEnvSetup build_setup(toolchain, buildexe_args);
70-
build_setup.ConstructTarget();
70+
Reg::Toolchain(ArgToolchainState(true)).BuildPackage(build_setup);
71+
Reg::Run();
7172

7273
// Run Target if script mode
7374
if (buildexe_args.GetBuildMode() == BuildExeMode::Script) {

buildexe/include/buildexe/build_env_setup.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ class BuildEnvSetup {
3333
public:
3434
BuildEnvSetup(const BaseToolchain &toolchain,
3535
const BuildExeArgs &buildexe_args)
36-
: toolchain_(toolchain), buildexe_args_(buildexe_args) {
37-
state_.build = true;
38-
}
36+
: toolchain_(toolchain), buildexe_args_(buildexe_args) {}
3937

40-
void ConstructTarget();
38+
void Setup(const ArgToolchainState &state);
4139

4240
void RunUserTarget(const ArgScriptInfo &arg_script_info);
4341

buildexe/src/build_env_setup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ namespace buildcc {
2121

2222
constexpr const char *const kTag = "BuildExe";
2323

24-
void BuildEnvSetup::ConstructTarget() {
24+
void BuildEnvSetup::Setup(const ArgToolchainState &state) {
25+
state_ = state;
2526
if (buildexe_args_.GetBuildMode() == BuildExeMode::Script) {
2627
// buildcc and user target
2728
ConstructUserTargetWithBuildcc();
2829
} else {
2930
// user target
3031
ConstructUserTarget();
3132
}
32-
Reg::Run();
3333
}
3434

3535
void BuildEnvSetup::RunUserTarget(const ArgScriptInfo &arg_script_info) {

docs/source/arch/cmake_boilerplate.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ We can then ``add_subdirectory`` that particular folder. This helps us keep our
8787
src/assert_fatal.cpp
8888
src/logging.cpp
8989
include/env/assert_fatal.h
90-
include/env/assert_throw.h
9190
include/env/env.h
9291
include/env/logging.h
9392
include/env/util.h

docs/source/user_api/environment.rst

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ assert_fatal.h
3333

3434
.. doxygendefine:: ASSERT_FATAL
3535

36-
assert_throw.h
37-
--------------
38-
39-
.. doxygenfunction:: assert_throw([[maybe_unused]] const char *)
40-
41-
.. doxygenfunction:: assert_throw(const std::string &)
42-
43-
.. doxygenfunction:: assert_throw(bool, const char *)
44-
45-
.. doxygenfunction:: assert_throw(bool, const std::string &)
46-
47-
.. doxygendefine:: ASSERT_THROW
48-
4936
command.h
5037
---------
5138

example/hybrid/external_lib/build.main.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,45 @@
55
using namespace buildcc;
66

77
static void clean_cb();
8-
static void foolib_build_cb(BaseTarget &target);
8+
static void foolib_build_cb(BaseTarget &target, const TargetInfo &foolib);
99

1010
constexpr const char *const EXE = "build";
1111

1212
int main(int argc, char **argv) {
13-
// 1. Get arguments
13+
// Get arguments
1414
ArgToolchain arg_gcc;
1515
ArgToolchain arg_msvc;
1616
Args::Init()
1717
.AddToolchain("gcc", "Generic gcc toolchain", arg_gcc)
1818
.AddToolchain("msvc", "Generic msvc toolchain", arg_msvc)
1919
.Parse(argc, argv);
2020

21-
// 2. Initialize your environment
21+
// Initialize your environment
2222
Reg::Init();
2323

24-
// 3. Pre-build steps
24+
// Pre-build steps
2525
Reg::Call(Args::Clean()).Func(clean_cb);
2626

27-
// 4. Build steps
27+
// Build steps
2828
Toolchain_gcc gcc;
29-
ExecutableTarget_gcc g_foolib("cppflags", gcc, "");
30-
Reg::Toolchain(arg_gcc.state).Build(foolib_build_cb, g_foolib);
29+
TargetInfo g_foo(gcc, "../foolib");
30+
ExecutableTarget_gcc g_external("cppflags", gcc, "");
31+
Reg::Toolchain(arg_gcc.state)
32+
.Func(fooTarget, g_foo)
33+
.Build(foolib_build_cb, g_external, g_foo);
3134

3235
Toolchain_msvc msvc;
33-
ExecutableTarget_msvc m_foolib("cppflags", msvc, "");
34-
Reg::Toolchain(arg_msvc.state).Build(foolib_build_cb, m_foolib);
36+
ExecutableTarget_msvc m_external("cppflags", msvc, "");
37+
TargetInfo m_foo(gcc, "../foolib");
38+
Reg::Toolchain(arg_msvc.state)
39+
.Func(fooTarget, m_foo)
40+
.Build(foolib_build_cb, m_external, m_foo);
3541

36-
// 5.
42+
//
3743
Reg::Run();
3844

39-
// 6.
40-
plugin::ClangCompileCommands({&g_foolib, &m_foolib}).Generate();
45+
//
46+
plugin::ClangCompileCommands({&g_external, &m_external}).Generate();
4147

4248
return 0;
4349
}
@@ -47,8 +53,12 @@ static void clean_cb() {
4753
fs::remove_all(Project::GetBuildDir());
4854
}
4955

50-
static void foolib_build_cb(BaseTarget &target) {
51-
fooTarget(target, "../foolib");
56+
static void foolib_build_cb(BaseTarget &target, const TargetInfo &foolib) {
5257
target.AddSource("main.cpp");
58+
target.Insert(foolib, {
59+
SyncOption::SourceFiles,
60+
SyncOption::HeaderFiles,
61+
SyncOption::IncludeDirs,
62+
});
5363
target.Build();
5464
}

example/hybrid/foolib/build.foo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "build.foo.h"
22

3-
void fooTarget(buildcc::BaseTarget &target, const fs::path &relative_path) {
4-
target.AddSource(relative_path / "src/foo.cpp");
5-
target.AddIncludeDir(relative_path / "src", true);
3+
void fooTarget(buildcc::TargetInfo &target) {
4+
target.AddSource("src/foo.cpp");
5+
target.AddIncludeDir("src", true);
66
}

example/hybrid/foolib/build.foo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
#include "buildcc.h"
44

5-
void fooTarget(buildcc::BaseTarget &target, const fs::path &relative_path);
5+
void fooTarget(buildcc::TargetInfo &target);

0 commit comments

Comments
 (0)