Skip to content

Commit 05ad3df

Browse files
authored
Generator code refactoring (#216)
1 parent 8b00500 commit 05ad3df

22 files changed

+347
-363
lines changed

bootstrap/src/build_buildcc.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@
1919
namespace buildcc {
2020

2121
void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe) {
22-
generator.AddInput("{gen_root_dir}/path.fbs", "path_fbs");
23-
generator.AddInput("{gen_root_dir}/custom_generator.fbs",
24-
"custom_generator_fbs");
25-
generator.AddInput("{gen_root_dir}/target.fbs", "target_fbs");
22+
generator.AddPattern("path_fbs", "{current_root_dir}/path.fbs");
23+
generator.AddPattern("custom_generator_fbs",
24+
"{current_root_dir}/custom_generator.fbs");
25+
generator.AddPattern("target_fbs", "{current_root_dir}/target.fbs");
2626

27-
generator.AddOutput("{gen_build_dir}/path_generated.h");
28-
generator.AddOutput("{gen_build_dir}/custom_generator_generated.h");
29-
generator.AddOutput("{gen_build_dir}/target_generated.h");
27+
generator.AddInput("{path_fbs}");
28+
generator.AddInput("{custom_generator_fbs}");
29+
generator.AddInput("{target_fbs}");
30+
31+
generator.AddOutput("{current_build_dir}/path_generated.h");
32+
generator.AddOutput("{current_build_dir}/custom_generator_generated.h");
33+
generator.AddOutput("{current_build_dir}/target_generated.h");
3034

3135
generator.AddPatterns({
3236
{"flatc_compiler", fmt::format("{}", flatc_exe.GetTargetPath())},
3337
});
3438
// generator.AddCommand("{flatc_compiler} --help");
35-
generator.AddCommand(
36-
"{flatc_compiler} -o {gen_build_dir} -I {gen_root_dir} --gen-object-api "
37-
"--cpp {path_fbs} {custom_generator_fbs} {target_fbs}");
39+
generator.AddCommand("{flatc_compiler} -o {current_build_dir} -I "
40+
"{current_root_dir} --gen-object-api "
41+
"--cpp {path_fbs} {custom_generator_fbs} {target_fbs}");
3842

3943
generator.Build();
4044
}
@@ -45,7 +49,7 @@ void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
4549
const TargetInfo &taskflow_ho, const BaseTarget &tpl) {
4650
// NOTE, Build as single lib
4751
target.AddIncludeDir("", true);
48-
const std::string &schema_build_dir = schema_gen.Get("gen_build_dir");
52+
const std::string &schema_build_dir = schema_gen.Get("current_build_dir");
4953
target.AddIncludeDirAbsolute(schema_build_dir, true);
5054

5155
// ENV

buildcc/lib/target/cmake/common_target_src.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ set(COMMON_TARGET_SRCS
5656
# Target
5757
src/target/target.cpp
5858
src/target/build.cpp
59+
src/target/tasks.cpp
5960
include/target/target.h
6061
)

buildcc/lib/target/cmake/mock_target.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ add_library(mock_target STATIC
55
mock/custom_generator/recheck_states.cpp
66

77
# Target mocks
8-
src/target/tasks.cpp
98
mock/target/runner.cpp
109
mock/target/recheck_states.cpp
1110
)

buildcc/lib/target/cmake/target.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ set(TARGET_SRCS
44
src/custom_generator/recheck_states.cpp
55

66
src/target/recheck_states.cpp
7-
src/target/tasks.cpp
87
)
98

109
if(${BUILDCC_BUILD_AS_SINGLE_LIB})

buildcc/lib/target/include/target/custom_generator.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ class CustomGenerator : public internal::BuilderInterface {
134134
* @param generate_cb User-defined generate callback to build outputs from the
135135
* provided inputs
136136
*/
137-
void AddGenInfo(const std::string &id, const fs_unordered_set &inputs,
138-
const fs_unordered_set &outputs,
137+
void AddGenInfo(const std::string &id,
138+
const std::unordered_set<std::string> &inputs,
139+
const std::unordered_set<std::string> &outputs,
139140
const GenerateCb &generate_cb,
140141
std::shared_ptr<CustomBlobHandler> blob_handler = nullptr);
141142

@@ -192,6 +193,11 @@ class CustomGenerator : public internal::BuilderInterface {
192193
void IdUpdated();
193194

194195
protected:
196+
std::string ParsePattern(const std::string &pattern,
197+
const std::unordered_map<const char *, std::string>
198+
&arguments = {}) const {
199+
return command_.Construct(pattern, arguments);
200+
}
195201
const env::Command &ConstCommand() const { return command_; }
196202
env::Command &RefCommand() { return command_; }
197203

buildcc/lib/target/include/target/file_generator.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,23 @@ class FileGenerator : public CustomGenerator {
2929

3030
/**
3131
* @brief Add absolute input path pattern to generator
32-
* NOTE: We can use {gen_root_dir} and {gen_build_dir} in the
32+
* NOTE: We can use {current_root_dir} and {current_build_dir} in the
3333
* absolute_input_pattern
3434
*
3535
* If `identifier` is supplied it is added to default arguments as a key
3636
* Example: fmt::format("{identifier}") -> "absolute_input_pattern"
3737
*/
38-
void AddInput(const std::string &absolute_input_pattern,
39-
const char *identifier = nullptr);
38+
void AddInput(const std::string &absolute_input_pattern);
4039

4140
/**
4241
* @brief Add absolute output path pattern to generator
43-
* NOTE: We can use {gen_root_dir} and {gen_build_dir} in the
42+
* NOTE: We can use {current_root_dir} and {current_build_dir} in the
4443
* absolute_output_pattern
4544
*
4645
* If `identifier` is supplied it is added to default arguments as a key
4746
* Example: fmt::format("{identifier}") -> "absolute_output_pattern"
4847
*/
49-
void AddOutput(const std::string &absolute_output_pattern,
50-
const char *identifier = nullptr);
48+
void AddOutput(const std::string &absolute_output_pattern);
5149

5250
/**
5351
* @brief Add a command_pattern that is fed to `Command::Execute` internally
@@ -77,8 +75,8 @@ class FileGenerator : public CustomGenerator {
7775

7876
private:
7977
//
80-
internal::fs_unordered_set inputs_;
81-
internal::fs_unordered_set outputs_;
78+
std::unordered_set<std::string> inputs_;
79+
std::unordered_set<std::string> outputs_;
8280
std::vector<std::string> commands_;
8381
};
8482

buildcc/lib/target/include/target/target.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ class Target : public internal::BuilderInterface,
7777
compile_pch_(*this), compile_object_(*this), link_target_(*this) {
7878
Initialize();
7979
}
80-
virtual ~Target() {}
80+
virtual ~Target() = default;
8181
Target(const Target &target) = delete;
8282

8383
// Builders
8484
void Build() override;
8585

86-
// Getters
87-
env::TaskState GetTaskState() const noexcept { return task_state_; }
88-
8986
private:
9087
friend class internal::CompilePch;
9188
friend class internal::CompileObject;
@@ -112,14 +109,7 @@ class Target : public internal::BuilderInterface,
112109
const std::vector<std::string> &current_external_libs);
113110

114111
// Tasks
115-
void SetTaskStateFailure();
116-
int GetTaskStateAsInt() const noexcept {
117-
return static_cast<int>(task_state_);
118-
}
119-
120-
void StartTask();
121112
void EndTask();
122-
tf::Task CheckStateTask();
123113
void TaskDeps();
124114

125115
// Callbacks for unit tests
@@ -151,8 +141,6 @@ class Target : public internal::BuilderInterface,
151141
// Task states
152142
tf::Task target_start_task_;
153143
tf::Task target_end_task_;
154-
std::mutex task_state_mutex_;
155-
env::TaskState task_state_{env::TaskState::SUCCESS};
156144
};
157145

158146
typedef Target BaseTarget;

buildcc/lib/target/include/target/template_generator.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef TARGET_TEMPLATE_GENERATOR_H_
1818
#define TARGET_TEMPLATE_GENERATOR_H_
1919

20+
#include <string_view>
21+
2022
#include "target/custom_generator.h"
2123

2224
namespace buildcc {
@@ -27,8 +29,8 @@ class TemplateGenerator : public CustomGenerator {
2729
~TemplateGenerator() override = default;
2830
TemplateGenerator(const TemplateGenerator &) = delete;
2931

30-
void AddTemplate(const fs::path &input_filename,
31-
const fs::path &output_filename);
32+
void AddTemplate(std::string_view absolute_input_pattern,
33+
std::string_view absolute_output_pattern);
3234
std::string Parse(const std::string &pattern) const;
3335

3436
/**
@@ -47,8 +49,8 @@ class TemplateGenerator : public CustomGenerator {
4749

4850
private:
4951
struct TemplateInfo {
50-
fs::path input;
51-
fs::path output;
52+
std::string input_pattern;
53+
std::string output_pattern;
5254
};
5355

5456
private:

buildcc/lib/target/src/custom_generator/custom_generator.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
namespace {
2020

2121
constexpr const char *const kGenerateTaskName = "Generate";
22+
constexpr const char *const kProjectRootDirName = "project_root_dir";
23+
constexpr const char *const kProjectBuildDirName = "project_build_dir";
24+
constexpr const char *const kCurrentRootDirName = "current_root_dir";
25+
constexpr const char *const kCurrentBuildDirName = "current_build_dir";
2226

2327
} // namespace
2428

@@ -42,24 +46,21 @@ CustomGenerator::Get(const std::string &file_identifier) const {
4246
}
4347

4448
void CustomGenerator::AddGenInfo(
45-
const std::string &id, const fs_unordered_set &inputs,
46-
const fs_unordered_set &outputs, const GenerateCb &generate_cb,
49+
const std::string &id, const std::unordered_set<std::string> &inputs,
50+
const std::unordered_set<std::string> &outputs,
51+
const GenerateCb &generate_cb,
4752
std::shared_ptr<CustomBlobHandler> blob_handler) {
4853
env::assert_fatal(user_.gen_info_map.find(id) == user_.gen_info_map.end(),
4954
fmt::format("Duplicate id {} detected", id));
5055
ASSERT_FATAL(generate_cb, "Invalid callback provided");
5156

5257
UserGenInfo schema;
5358
for (const auto &i : inputs) {
54-
fs::path input =
55-
internal::Path::CreateNewPath(command_.Construct(path_as_string(i)))
56-
.GetPathname();
59+
fs::path input = string_as_path(command_.Construct(i));
5760
schema.inputs.emplace(std::move(input));
5861
}
5962
for (const auto &o : outputs) {
60-
fs::path output =
61-
internal::Path::CreateNewPath(command_.Construct(path_as_string(o)))
62-
.GetPathname();
63+
fs::path output = string_as_path(command_.Construct(o));
6364
schema.outputs.emplace(std::move(output));
6465
}
6566
schema.generate_cb = generate_cb;
@@ -109,10 +110,10 @@ void CustomGenerator::Initialize() {
109110
//
110111
fs::create_directories(env_.GetTargetBuildDir());
111112
command_.AddDefaultArguments({
112-
{"project_root_dir", path_as_string(Project::GetRootDir())},
113-
{"project_build_dir", path_as_string(Project::GetBuildDir())},
114-
{"gen_root_dir", path_as_string(env_.GetTargetRootDir())},
115-
{"gen_build_dir", path_as_string(env_.GetTargetBuildDir())},
113+
{kProjectRootDirName, path_as_string(Project::GetRootDir())},
114+
{kProjectBuildDirName, path_as_string(Project::GetBuildDir())},
115+
{kCurrentRootDirName, path_as_string(env_.GetTargetRootDir())},
116+
{kCurrentBuildDirName, path_as_string(env_.GetTargetBuildDir())},
116117
});
117118

118119
//

buildcc/lib/target/src/generator/file_generator.cpp

+5-24
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,18 @@ bool FileGeneratorGenerateCb(const buildcc::CustomGeneratorContext &ctx) {
9595

9696
namespace buildcc {
9797

98-
void FileGenerator::AddInput(const std::string &absolute_input_pattern,
99-
const char *identifier) {
100-
std::string absolute_input_string =
101-
RefCommand().Construct(absolute_input_pattern);
102-
const auto absolute_input_path =
103-
internal::Path::CreateNewPath(absolute_input_string);
104-
inputs_.insert(absolute_input_path.GetPathname());
105-
106-
if (identifier != nullptr) {
107-
AddPattern(identifier, absolute_input_path.GetPathAsString());
108-
}
98+
void FileGenerator::AddInput(const std::string &absolute_input_pattern) {
99+
inputs_.emplace(absolute_input_pattern);
109100
}
110101

111-
void FileGenerator::AddOutput(const std::string &absolute_output_pattern,
112-
const char *identifier) {
113-
std::string absolute_output_string =
114-
RefCommand().Construct(absolute_output_pattern);
115-
const auto absolute_output_path =
116-
internal::Path::CreateNewPath(absolute_output_string);
117-
outputs_.insert(absolute_output_path.GetPathname());
118-
119-
if (identifier != nullptr) {
120-
AddPattern(identifier, absolute_output_path.GetPathAsString());
121-
}
102+
void FileGenerator::AddOutput(const std::string &absolute_output_pattern) {
103+
outputs_.emplace(absolute_output_pattern);
122104
}
123105

124106
void FileGenerator::AddCommand(
125107
const std::string &command_pattern,
126108
const std::unordered_map<const char *, std::string> &arguments) {
127-
std::string constructed_command =
128-
RefCommand().Construct(command_pattern, arguments);
109+
std::string constructed_command = ParsePattern(command_pattern, arguments);
129110
commands_.emplace_back(std::move(constructed_command));
130111
}
131112

buildcc/lib/target/src/generator/template_generator.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,16 @@ bool template_generate_cb(const buildcc::CustomGeneratorContext &ctx) {
4444

4545
namespace buildcc {
4646

47-
void TemplateGenerator::AddTemplate(const fs::path &input_filename,
48-
const fs::path &output_filename) {
47+
void TemplateGenerator::AddTemplate(std::string_view absolute_input_pattern,
48+
std::string_view absolute_output_pattern) {
4949
TemplateInfo info;
50-
info.input = internal::Path::CreateNewPath(
51-
RefCommand().Construct(path_as_string(input_filename)))
52-
.GetPathname();
53-
info.output = internal::Path::CreateNewPath(
54-
RefCommand().Construct(path_as_string(output_filename)))
55-
.GetPathname();
50+
info.input_pattern = absolute_input_pattern;
51+
info.output_pattern = absolute_output_pattern;
5652
template_infos_.emplace_back(std::move(info));
5753
}
5854

5955
std::string TemplateGenerator::Parse(const std::string &pattern) const {
60-
return ConstCommand().Construct(pattern);
56+
return ParsePattern(pattern);
6157
}
6258

6359
/**
@@ -67,9 +63,11 @@ std::string TemplateGenerator::Parse(const std::string &pattern) const {
6763
*/
6864
void TemplateGenerator::Build() {
6965
for (const auto &info : template_infos_) {
70-
std::string name =
71-
info.input.lexically_relative(Project::GetRootDir()).string();
72-
AddGenInfo(name, {info.input}, {info.output}, template_generate_cb);
66+
std::string name = string_as_path(ParsePattern(info.input_pattern))
67+
.lexically_relative(Project::GetRootDir())
68+
.string();
69+
AddGenInfo(name, {info.input_pattern}, {info.output_pattern},
70+
template_generate_cb);
7371
}
7472
this->CustomGenerator::Build();
7573
}

buildcc/lib/target/src/target/build.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ void Target::Build() {
9797
// Load the serialized file
9898
(void)serialization_.LoadFromFile();
9999

100-
// Target State Tasks
101-
StartTask();
102-
EndTask();
103-
104100
// PCH Compile
105101
if (state_.ContainsPch()) {
106102
command_.AddDefaultArguments({
@@ -119,6 +115,9 @@ void Target::Build() {
119115
});
120116
}
121117

118+
// Target State Tasks
119+
EndTask();
120+
122121
// Compile Command
123122
compile_object_.CacheCompileCommands();
124123
compile_object_.Task();

0 commit comments

Comments
 (0)