Skip to content

Commit 1fded03

Browse files
authored
[Serialization] Custom Generator unit tests (#224)
1 parent c2e2615 commit 1fded03

File tree

20 files changed

+376
-357
lines changed

20 files changed

+376
-357
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,21 @@ class CustomBlobHandler {
8686
virtual std::vector<uint8_t> Serialize() const = 0;
8787
};
8888

89-
struct UserGenInfo : internal::CustomGeneratorSchema::IdInfo {
89+
struct UserIdInfo : internal::CustomGeneratorSchema::IdInfo {
9090
fs_unordered_set inputs;
9191
GenerateCb generate_cb;
9292
std::shared_ptr<CustomBlobHandler> blob_handler{nullptr};
9393
};
9494

9595
struct UserCustomGeneratorSchema : public internal::CustomGeneratorSchema {
96-
std::unordered_map<std::string, UserGenInfo> gen_info_map;
96+
std::unordered_map<IdKey, UserIdInfo> ids;
9797

9898
void ConvertToInternal() {
99-
for (auto &[id, gen_info] : gen_info_map) {
100-
gen_info.internal_inputs = path_schema_convert(
101-
gen_info.inputs, internal::Path::CreateExistingPath);
102-
auto [_, success] = internal_ids.try_emplace(id, gen_info);
103-
env::assert_fatal(success, fmt::format("Could not save {}", id));
99+
for (auto &[id_key, id_info] : ids) {
100+
id_info.internal_inputs = path_schema_convert(
101+
id_info.inputs, internal::Path::CreateExistingPath);
102+
auto [_, success] = internal_ids.try_emplace(id_key, id_info);
103+
env::assert_fatal(success, fmt::format("Could not save {}", id_key));
104104
}
105105
}
106106
};
@@ -137,16 +137,16 @@ class CustomGenerator : public internal::BuilderInterface {
137137
* @param generate_cb User-defined generate callback to build outputs from the
138138
* provided inputs
139139
*/
140-
void AddGenInfo(const std::string &id,
141-
const std::unordered_set<std::string> &inputs,
142-
const std::unordered_set<std::string> &outputs,
143-
const GenerateCb &generate_cb,
144-
std::shared_ptr<CustomBlobHandler> blob_handler = nullptr);
140+
void AddIdInfo(const std::string &id,
141+
const std::unordered_set<std::string> &inputs,
142+
const std::unordered_set<std::string> &outputs,
143+
const GenerateCb &generate_cb,
144+
std::shared_ptr<CustomBlobHandler> blob_handler = nullptr);
145145

146146
// TODO, Doc
147-
void AddGroup(const std::string &group_id,
148-
std::initializer_list<std::string> ids,
149-
const DependencyCb &dependency_cb = DependencyCb());
147+
void AddGroupInfo(const std::string &group_id,
148+
std::initializer_list<std::string> ids,
149+
const DependencyCb &dependency_cb = DependencyCb());
150150

151151
// Callbacks
152152
/**
@@ -233,7 +233,7 @@ class CustomGenerator : public internal::BuilderInterface {
233233
std::unordered_set<std::string> ungrouped_ids_;
234234

235235
std::mutex success_schema_mutex_;
236-
std::unordered_map<std::string, UserGenInfo> success_schema_;
236+
std::unordered_map<std::string, UserIdInfo> success_schema_;
237237

238238
// Internal
239239
env::Command command_;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class FileGenerator : public CustomGenerator {
6969
// Restrict access to certain custom generator APIs
7070
private:
7171
using CustomGenerator::AddDependencyCb;
72-
using CustomGenerator::AddGenInfo;
73-
using CustomGenerator::AddGroup;
72+
using CustomGenerator::AddGroupInfo;
73+
using CustomGenerator::AddIdInfo;
7474
using CustomGenerator::Build;
7575

7676
private:

buildcc/lib/target/include/target/interface/builder_interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ inline PathState CheckPaths(const internal::path_unordered_set &previous_path,
7474
state = PathState::kAdded;
7575
} else {
7676
const bool updated_cond =
77-
(p.GetLastWriteTimestamp() >
78-
find->GetLastWriteTimestamp());
77+
(p.last_write_timestamp >
78+
find->last_write_timestamp);
7979
if (updated_cond) {
8080
dirty = true;
8181
state = PathState::kUpdated;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class TemplateGenerator : public CustomGenerator {
4343
// Restrict access to certain custom generator APIs
4444
private:
4545
using CustomGenerator::AddDependencyCb;
46-
using CustomGenerator::AddGenInfo;
47-
using CustomGenerator::AddGroup;
46+
using CustomGenerator::AddGroupInfo;
47+
using CustomGenerator::AddIdInfo;
4848
using CustomGenerator::Build;
4949

5050
private:

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

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ CustomGenerator::Get(const std::string &file_identifier) const {
5151
return command_.GetDefaultValueByKey(file_identifier);
5252
}
5353

54-
void CustomGenerator::AddGenInfo(
54+
void CustomGenerator::AddIdInfo(
5555
const std::string &id, const std::unordered_set<std::string> &inputs,
5656
const std::unordered_set<std::string> &outputs,
5757
const GenerateCb &generate_cb,
5858
std::shared_ptr<CustomBlobHandler> blob_handler) {
59-
env::assert_fatal(user_.gen_info_map.find(id) == user_.gen_info_map.end(),
59+
env::assert_fatal(user_.ids.find(id) == user_.ids.end(),
6060
fmt::format("Duplicate id {} detected", id));
6161
ASSERT_FATAL(generate_cb, "Invalid callback provided");
6262

63-
UserGenInfo schema;
63+
UserIdInfo schema;
6464
for (const auto &i : inputs) {
6565
fs::path input = string_as_path(command_.Construct(i));
6666
schema.inputs.emplace(std::move(input));
@@ -71,17 +71,17 @@ void CustomGenerator::AddGenInfo(
7171
}
7272
schema.generate_cb = generate_cb;
7373
schema.blob_handler = std::move(blob_handler);
74-
user_.gen_info_map.try_emplace(id, std::move(schema));
74+
user_.ids.try_emplace(id, std::move(schema));
7575
ungrouped_ids_.emplace(id);
7676
}
7777

78-
void CustomGenerator::AddGroup(const std::string &group_id,
79-
std::initializer_list<std::string> ids,
80-
const DependencyCb &dependency_cb) {
78+
void CustomGenerator::AddGroupInfo(const std::string &group_id,
79+
std::initializer_list<std::string> ids,
80+
const DependencyCb &dependency_cb) {
8181
// Verify that the ids exist
8282
// Remove those ids from ungrouped_ids
8383
for (const auto &id : ids) {
84-
env::assert_fatal(user_.gen_info_map.find(id) != user_.gen_info_map.end(),
84+
env::assert_fatal(user_.ids.find(id) != user_.ids.end(),
8585
fmt::format("Id '{}' is not found", id));
8686
ungrouped_ids_.erase(id);
8787
}
@@ -131,34 +131,34 @@ void CustomGenerator::BuildGenerate(
131131
std::unordered_set<std::string> &gen_selected_ids,
132132
std::unordered_set<std::string> &dummy_gen_selected_ids) {
133133
if (!serialization_.IsLoaded()) {
134-
std::for_each(
135-
user_.gen_info_map.begin(), user_.gen_info_map.end(),
136-
[&](const auto &iter) { gen_selected_ids.insert(iter.first); });
134+
std::for_each(user_.ids.begin(), user_.ids.end(), [&](const auto &iter) {
135+
gen_selected_ids.insert(iter.first);
136+
});
137137
dirty_ = true;
138138
} else {
139139
// DONE, Conditionally select internal_ids depending on what has
140140
// changed
141-
const auto &prev_gen_info_map = serialization_.GetLoad().internal_ids;
142-
const auto &curr_gen_info_map = user_.gen_info_map;
141+
const auto &prev_ids = serialization_.GetLoad().internal_ids;
142+
const auto &curr_ids = user_.ids;
143143

144-
// DONE, MAP REMOVED condition Check if prev_gen_info_map exists in
145-
// curr_gen_info_map If prev_gen_info_map does not exist in
146-
// curr_gen_info_map, has been removed from existing build We need this
144+
// DONE, MAP REMOVED condition Check if prev_ids exists in
145+
// curr_ids If prev_ids does not exist in
146+
// curr_ids, has been removed from existing build We need this
147147
// condition to only set the dirty_ flag
148-
for (const auto &[id, _] : prev_gen_info_map) {
149-
if (curr_gen_info_map.find(id) == curr_gen_info_map.end()) {
148+
for (const auto &[id, _] : prev_ids) {
149+
if (curr_ids.find(id) == curr_ids.end()) {
150150
// MAP REMOVED condition
151151
IdRemoved();
152152
dirty_ = true;
153153
break;
154154
}
155155
}
156156

157-
// DONE, MAP ADDED condition Check if curr_gen_info_map exists in
158-
// prev_gen_info_map If curr_gen_info_map does not exist in
159-
// prev_gen_info_map, has been added to existing build
160-
for (const auto &[id, _] : curr_gen_info_map) {
161-
if (prev_gen_info_map.find(id) == prev_gen_info_map.end()) {
157+
// DONE, MAP ADDED condition Check if curr_ids exists in
158+
// prev_ids If curr_ids does not exist in
159+
// prev_ids, has been added to existing build
160+
for (const auto &[id, _] : curr_ids) {
161+
if (prev_ids.find(id) == prev_ids.end()) {
162162
// MAP ADDED condition
163163
IdAdded();
164164
gen_selected_ids.insert(id);
@@ -232,8 +232,8 @@ void CustomGenerator::GenerateTask() {
232232
// Store dummy_selected and successfully run schema
233233
if (dirty_) {
234234
UserCustomGeneratorSchema user_final_schema;
235-
user_final_schema.gen_info_map.insert(success_schema_.begin(),
236-
success_schema_.end());
235+
user_final_schema.ids.insert(success_schema_.begin(),
236+
success_schema_.end());
237237

238238
user_final_schema.ConvertToInternal();
239239
serialization_.UpdateStore(user_final_schema);
@@ -278,42 +278,44 @@ tf::Task CustomGenerator::CreateTaskRunner(tf::Subflow &subflow, bool build,
278278

279279
void CustomGenerator::TaskRunner(bool run, const std::string &id) {
280280
// Convert
281-
auto &current_gen_info = user_.gen_info_map.at(id);
282-
current_gen_info.internal_inputs = internal::path_schema_convert(
283-
current_gen_info.inputs, internal::Path::CreateExistingPath);
284-
current_gen_info.userblob =
285-
current_gen_info.blob_handler != nullptr
286-
? current_gen_info.blob_handler->GetSerializedData()
287-
: std::vector<uint8_t>();
281+
{
282+
auto &curr_id_info = user_.ids.at(id);
283+
curr_id_info.internal_inputs = internal::path_schema_convert(
284+
curr_id_info.inputs, internal::Path::CreateExistingPath);
285+
curr_id_info.userblob = curr_id_info.blob_handler != nullptr
286+
? curr_id_info.blob_handler->GetSerializedData()
287+
: std::vector<uint8_t>();
288+
}
288289

289290
// Run
290-
const auto &current_info = user_.gen_info_map.at(id);
291+
const auto &current_id_info = user_.ids.at(id);
291292
bool rerun = false;
292293
if (run) {
293294
rerun = true;
294295
} else {
295296
const auto &previous_info = serialization_.GetLoad().internal_ids.at(id);
296-
rerun = internal::CheckPaths(previous_info.internal_inputs,
297-
current_info.internal_inputs) !=
298-
internal::PathState::kNoChange ||
299-
internal::CheckChanged(previous_info.outputs, current_info.outputs);
300-
if (!rerun && current_info.blob_handler != nullptr) {
301-
rerun = current_info.blob_handler->CheckChanged(previous_info.userblob,
302-
current_info.userblob);
297+
rerun =
298+
internal::CheckPaths(previous_info.internal_inputs,
299+
current_id_info.internal_inputs) !=
300+
internal::PathState::kNoChange ||
301+
internal::CheckChanged(previous_info.outputs, current_id_info.outputs);
302+
if (!rerun && current_id_info.blob_handler != nullptr) {
303+
rerun = current_id_info.blob_handler->CheckChanged(
304+
previous_info.userblob, current_id_info.userblob);
303305
}
304306
}
305307

306308
if (rerun) {
307309
dirty_ = true;
308-
buildcc::CustomGeneratorContext ctx(command_, current_info.inputs,
309-
current_info.outputs,
310-
current_info.userblob);
311-
bool success = current_info.generate_cb(ctx);
310+
buildcc::CustomGeneratorContext ctx(command_, current_id_info.inputs,
311+
current_id_info.outputs,
312+
current_id_info.userblob);
313+
bool success = current_id_info.generate_cb(ctx);
312314
env::assert_fatal(success, fmt::format("Generate Cb failed for id {}", id));
313315
}
314316

315317
std::scoped_lock<std::mutex> guard(success_schema_mutex_);
316-
success_schema_.try_emplace(id, current_info);
318+
success_schema_.try_emplace(id, current_id_info);
317319
}
318320

319321
} // namespace buildcc

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ void FileGenerator::AddCommand(
111111
}
112112

113113
void FileGenerator::Build() {
114-
AddGenInfo("Generate", inputs_, outputs_, FileGeneratorGenerateCb,
115-
std::make_shared<FileGeneratorBlobHandler>(commands_));
114+
AddIdInfo("Generate", inputs_, outputs_, FileGeneratorGenerateCb,
115+
std::make_shared<FileGeneratorBlobHandler>(commands_));
116116
this->CustomGenerator::Build();
117117
}
118118

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ void TemplateGenerator::Build() {
6666
std::string name = string_as_path(ParsePattern(info.input_pattern))
6767
.lexically_relative(Project::GetRootDir())
6868
.string();
69-
AddGenInfo(name, {info.input_pattern}, {info.output_pattern},
70-
template_generate_cb);
69+
AddIdInfo(name, {info.input_pattern}, {info.output_pattern},
70+
template_generate_cb);
7171
}
7272
this->CustomGenerator::Build();
7373
}

buildcc/lib/target/src/target/friend/compile_object.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ void CompileObject::RecompileSources(
254254
target_.SourceAdded();
255255
} else {
256256
// *2 Current file is updated
257-
if (current_file.GetLastWriteTimestamp() >
258-
iter->GetLastWriteTimestamp()) {
257+
if (current_file.last_write_timestamp > iter->last_write_timestamp) {
259258
source_files.push_back(current_file);
260259
target_.dirty_ = true;
261260
target_.SourceUpdated();

buildcc/lib/target/src/target/tasks.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ void CompileObject::Task() {
9494

9595
for (const auto &s : selected_source_files) {
9696
std::string name = fmt::format(
97-
"{}", s.GetPathname().lexically_relative(Project::GetRootDir()));
97+
"{}", s.pathname.lexically_relative(Project::GetRootDir()));
9898
(void)subflow
9999
.emplace([this, s]() {
100100
try {
101-
bool success = env::Command::Execute(
102-
GetObjectData(s.GetPathname()).command);
101+
bool success =
102+
env::Command::Execute(GetObjectData(s.pathname).command);
103103
env::assert_fatal(success, "Could not compile source");
104104
target_.serialization_.AddSource(s);
105105
} catch (...) {
@@ -112,7 +112,7 @@ void CompileObject::Task() {
112112
// For graph generation
113113
for (const auto &ds : selected_dummy_source_files) {
114114
std::string name = fmt::format(
115-
"{}", ds.GetPathname().lexically_relative(Project::GetRootDir()));
115+
"{}", ds.pathname.lexically_relative(Project::GetRootDir()));
116116
(void)subflow.placeholder().name(name);
117117
}
118118
} catch (...) {

buildcc/lib/target/test/path/test_path.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ static const auto current_file_path =
2626
TEST(PathTestGroup, Path_ExistingPathStaticConstructor) {
2727
auto existing_path =
2828
buildcc::internal::Path::CreateExistingPath(current_file_path);
29-
STRCMP_EQUAL(existing_path.GetPathname().string().c_str(),
29+
STRCMP_EQUAL(existing_path.pathname.string().c_str(),
3030
current_file_path.string().c_str());
3131
// * NOTE, Last write timestamp changes whenever we resave or re-download
3232
// This would not work well with Git
33-
// UNSIGNED_LONGLONGS_EQUAL(existing_path.GetLastWriteTimestamp(),
33+
// UNSIGNED_LONGLONGS_EQUAL(existing_path.last_write_timestamp,
3434
// 13623997187709551616ULL);
3535
}
3636

@@ -41,19 +41,18 @@ TEST(PathTestGroup, Path_ExistingPathStaticConstructor_ThrowFileException) {
4141

4242
TEST(PathTestGroup, PathConstructor_NewPathStaticConstructor) {
4343
buildcc::internal::Path p =
44-
buildcc::internal::Path::CreateNewPath("random_path_main.cpp", 12345ULL);
45-
STRCMP_EQUAL(p.GetPathname().string().c_str(), "random_path_main.cpp");
46-
UNSIGNED_LONGLONGS_EQUAL(p.GetLastWriteTimestamp(), 12345ULL);
44+
buildcc::internal::Path("random_path_main.cpp", 12345ULL);
45+
STRCMP_EQUAL(p.pathname.string().c_str(), "random_path_main.cpp");
46+
UNSIGNED_LONGLONGS_EQUAL(p.last_write_timestamp, 12345ULL);
4747
}
4848

4949
TEST(PathTestGroup, Path_EqualityOperator) {
5050
buildcc::internal::Path p =
5151
buildcc::internal::Path::CreateExistingPath(current_file_path);
52-
STRCMP_EQUAL(p.GetPathname().string().c_str(),
53-
current_file_path.string().c_str());
52+
STRCMP_EQUAL(p.pathname.string().c_str(), current_file_path.string().c_str());
5453

5554
buildcc::internal::Path newp =
56-
buildcc::internal::Path::CreateNewPath(current_file_path, 12345ULL);
55+
buildcc::internal::Path(current_file_path, 12345ULL);
5756

5857
// NOTE, Equality does not match the last_write_timestamp
5958
// ONLY matches the string
@@ -71,25 +70,24 @@ TEST(PathTestGroup, Path_UnorderedSet) {
7170
.insert(buildcc::internal::Path::CreateExistingPath(
7271
current_file_path))
7372
.second);
74-
CHECK_FALSE(unique_paths
75-
.insert(buildcc::internal::Path::CreateNewPath(
76-
current_file_path, 12345ULL))
77-
.second);
78-
CHECK_TRUE(unique_paths
79-
.insert(buildcc::internal::Path::CreateNewPath(
80-
"random_path_main.cpp", 98765ULL))
81-
.second);
73+
CHECK_FALSE(
74+
unique_paths.insert(buildcc::internal::Path(current_file_path, 12345ULL))
75+
.second);
76+
CHECK_TRUE(
77+
unique_paths
78+
.insert(buildcc::internal::Path("random_path_main.cpp", 98765ULL))
79+
.second);
8280

8381
// Check finds
8482
// * NOTE, Only matches pathname
8583
CHECK_FALSE(unique_paths.find(buildcc::internal::Path::CreateExistingPath(
8684
current_file_path)) == unique_paths.end());
8785

88-
CHECK_FALSE(unique_paths.find(buildcc::internal::Path::CreateNewPath(
86+
CHECK_FALSE(unique_paths.find(buildcc::internal::Path(
8987
current_file_path, 1111ULL)) == unique_paths.end());
90-
CHECK_FALSE(unique_paths.find(buildcc::internal::Path::CreateNewPath(
88+
CHECK_FALSE(unique_paths.find(buildcc::internal::Path(
9189
"random_path_main.cpp", 12345ULL)) == unique_paths.end());
92-
CHECK_TRUE(unique_paths.find(buildcc::internal::Path::CreateNewPath(
90+
CHECK_TRUE(unique_paths.find(buildcc::internal::Path(
9391
"incorrect_path_main.cpp", 0000ULL)) == unique_paths.end());
9492
}
9593

0 commit comments

Comments
 (0)