Skip to content

[Serialization] JSON Serialization for Target #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions bootstrap/include/bootstrap/build_buildcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@

namespace buildcc {

void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe);

void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
const TargetInfo &flatbuffers_ho,
void buildcc_cb(BaseTarget &target, const TargetInfo &flatbuffers_ho,
const TargetInfo &nlohmann_json_ho, const TargetInfo &fmt_ho,
const TargetInfo &spdlog_ho, const TargetInfo &cli11_ho,
const TargetInfo &taskflow_ho, const TargetInfo &tl_optional_ho,
Expand All @@ -57,9 +54,6 @@ class BuildBuildCC {
// Executable
static constexpr const char *const kFlatcExeName = "flatc";

// Generator
static constexpr const char *const kSchemaGenName = "schema_gen";

// Libraries
static constexpr const char *const kTplLibName = "libtpl";
static constexpr const char *const kBuildccLibName = "libbuildcc";
Expand All @@ -86,9 +80,6 @@ class BuildBuildCC {
ExecutableTarget_generic &GetFlatc() {
return storage_.Ref<ExecutableTarget_generic>(kFlatcExeName);
}
FileGenerator &GetSchemaGen() {
return storage_.Ref<FileGenerator>(kSchemaGenName);
}
TargetInfo &GetFlatbuffersHo() {
return storage_.Ref<TargetInfo>(kFlatbuffersHoName);
}
Expand Down
46 changes: 4 additions & 42 deletions bootstrap/src/build_buildcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,13 @@

namespace buildcc {

void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe) {
generator.AddPattern("path_fbs", "{current_root_dir}/path.fbs");
generator.AddPattern("target_fbs", "{current_root_dir}/target.fbs");

generator.AddInput("{path_fbs}");
generator.AddInput("{target_fbs}");

generator.AddOutput("{current_build_dir}/path_generated.h");
generator.AddOutput("{current_build_dir}/target_generated.h");

generator.AddPatterns({
{"flatc_compiler", fmt::format("{}", flatc_exe.GetTargetPath())},
});
// generator.AddCommand("{flatc_compiler} --help");
generator.AddCommand("{flatc_compiler} -o {current_build_dir} -I "
"{current_root_dir} --gen-object-api "
"--cpp {path_fbs} {target_fbs}");

generator.Build();
}

void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
const TargetInfo &flatbuffers_ho,
void buildcc_cb(BaseTarget &target, const TargetInfo &flatbuffers_ho,
const TargetInfo &nlohmann_json_ho, const TargetInfo &fmt_ho,
const TargetInfo &spdlog_ho, const TargetInfo &cli11_ho,
const TargetInfo &taskflow_ho, const TargetInfo &tl_optional_ho,
const BaseTarget &tpl) {
// NOTE, Build as single lib
target.AddIncludeDir("", true);
const std::string &schema_build_dir = schema_gen.Get("current_build_dir");
target.AddIncludeDirAbsolute(schema_build_dir, true);

// ENV
target.GlobSources("lib/env/src");
Expand All @@ -60,7 +36,6 @@ void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
target.AddIncludeDir("schema/include");
target.GlobHeaders("schema/include/schema");
target.GlobHeaders("schema/include/schema/interface");
target.GlobHeaders("schema/include/schema/private");

// TOOLCHAIN
target.GlobSources("lib/toolchain/src/api");
Expand Down Expand Up @@ -205,12 +180,6 @@ void BuildBuildCC::Initialize() {
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
env_.GetTargetBuildDir()));

// Schema
(void)storage_.Add<FileGenerator>(
kSchemaGenName, kSchemaGenName,
TargetEnv(env_.GetTargetRootDir() / "buildcc" / "schema",
env_.GetTargetBuildDir() / toolchain_.GetName()));

// Flatbuffers HO lib
(void)storage_.Add<TargetInfo>(
kFlatbuffersHoName, toolchain_,
Expand Down Expand Up @@ -271,8 +240,6 @@ void BuildBuildCC::Initialize() {
}

void BuildBuildCC::Setup(const ArgToolchainState &state) {
auto &flatc_exe = GetFlatc();
auto &schema_gen = GetSchemaGen();
auto &flatbuffers_ho_lib = GetFlatbuffersHo();
auto &nlohmann_json_ho_lib = GetNlohmannJsonHo();
auto &cli11_ho_lib = GetCli11Ho();
Expand All @@ -283,10 +250,6 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
auto &tpl_lib = GetTpl();
auto &buildcc_lib = GetBuildcc();
Reg::Toolchain(state)
.Func(global_flags_cb, flatc_exe, toolchain_)
.Build(build_flatc_exe_cb, flatc_exe)
.Build(schema_gen_cb, schema_gen, flatc_exe)
.Dep(schema_gen, flatc_exe)
.Func(flatbuffers_ho_cb, flatbuffers_ho_lib)
.Func(nlohmann_json_ho_cb, nlohmann_json_ho_lib)
.Func(cli11_ho_cb, cli11_ho_lib)
Expand All @@ -297,10 +260,9 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
.Func(global_flags_cb, tpl_lib, toolchain_)
.Build(tpl_cb, tpl_lib)
.Func(global_flags_cb, buildcc_lib, toolchain_)
.Build(buildcc_cb, buildcc_lib, schema_gen, flatbuffers_ho_lib,
nlohmann_json_ho_lib, fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib,
taskflow_ho_lib, tl_optional_ho_lib, tpl_lib)
.Dep(buildcc_lib, schema_gen)
.Build(buildcc_cb, buildcc_lib, flatbuffers_ho_lib, nlohmann_json_ho_lib,
fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib, taskflow_ho_lib,
tl_optional_ho_lib, tpl_lib)
.Dep(buildcc_lib, tpl_lib);
}

Expand Down
1 change: 0 additions & 1 deletion buildcc/schema/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
include(cmake/schema_generate.cmake)
include(cmake/schema.cmake)
16 changes: 11 additions & 5 deletions buildcc/schema/cmake/schema.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# schema test
if (${TESTING})
add_library(mock_schema STATIC
include/schema/private/schema_util.h
include/schema/interface/serialization_interface.h

include/schema/path.h
Expand All @@ -11,6 +10,7 @@ if (${TESTING})
include/schema/custom_generator_serialization.h

src/target_serialization.cpp
include/schema/target_schema.h
include/schema/target_serialization.h
)
target_include_directories(mock_schema PUBLIC
Expand All @@ -27,7 +27,6 @@ if (${TESTING})
CppUTestExt
${TEST_LINK_LIBS}
)
add_dependencies(mock_schema fbs_to_header)

target_compile_options(mock_schema PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS})
target_link_options(mock_schema PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS})
Expand All @@ -38,21 +37,30 @@ if (${TESTING})
)
target_link_libraries(test_custom_generator_serialization PRIVATE mock_schema)

add_executable(test_target_serialization
test/test_target_serialization.cpp
)
target_link_libraries(test_target_serialization PRIVATE mock_schema)

add_test(NAME test_custom_generator_serialization COMMAND test_custom_generator_serialization
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
add_test(NAME test_target_serialization COMMAND test_target_serialization
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
endif()

set(SCHEMA_SRCS
include/schema/private/schema_util.h
include/schema/interface/serialization_interface.h

include/schema/path.h

src/custom_generator_serialization.cpp
include/schema/custom_generator_schema.h
include/schema/custom_generator_serialization.h

src/target_serialization.cpp
include/schema/target_schema.h
include/schema/target_serialization.h
)

Expand All @@ -67,7 +75,6 @@ if(${BUILDCC_BUILD_AS_SINGLE_LIB})
target_include_directories(buildcc PRIVATE
${SCHEMA_BUILD_DIR}
)
add_dependencies(buildcc fbs_to_header)
endif()

if(${BUILDCC_BUILD_AS_INTERFACE})
Expand All @@ -89,7 +96,6 @@ if(${BUILDCC_BUILD_AS_INTERFACE})
)
target_compile_options(schema PRIVATE ${BUILD_COMPILE_FLAGS})
target_link_options(schema PRIVATE ${BUILD_LINK_FLAGS})
add_dependencies(schema fbs_to_header)
endif()

if (${BUILDCC_INSTALL})
Expand Down
24 changes: 0 additions & 24 deletions buildcc/schema/cmake/schema_generate.cmake

This file was deleted.

36 changes: 18 additions & 18 deletions buildcc/schema/include/schema/custom_generator_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ namespace buildcc::internal {

struct CustomGeneratorSchema {
private:
static constexpr const char *const kSchemaName = "name";
static constexpr const char *const kIdsName = "ids";
static constexpr const char *const kGroupsName = "groups";
static constexpr const char *const kName = "name";
static constexpr const char *const kIds = "ids";
static constexpr const char *const kGroups = "groups";

public:
using IdKey = std::string;
using GroupKey = std::string;
struct IdInfo {
private:
static constexpr const char *const kInputsName = "inputs";
static constexpr const char *const kOutputsName = "outputs";
static constexpr const char *const kUserblobName = "userblob";
static constexpr const char *const kInputs = "inputs";
static constexpr const char *const kOutputs = "outputs";
static constexpr const char *const kUserblob = "userblob";

public:
path_unordered_set internal_inputs;
fs_unordered_set outputs;
std::vector<uint8_t> userblob;

friend void to_json(json &j, const IdInfo &info) {
j[kInputsName] = info.internal_inputs;
j[kOutputsName] = info.outputs;
j[kUserblobName] = info.userblob;
j[kInputs] = info.internal_inputs;
j[kOutputs] = info.outputs;
j[kUserblob] = info.userblob;
}

friend void from_json(const json &j, IdInfo &info) {
j.at(kInputsName).get_to(info.internal_inputs);
j.at(kOutputsName).get_to(info.outputs);
j.at(kUserblobName).get_to(info.userblob);
j.at(kInputs).get_to(info.internal_inputs);
j.at(kOutputs).get_to(info.outputs);
j.at(kUserblob).get_to(info.userblob);
}
};

Expand All @@ -65,15 +65,15 @@ struct CustomGeneratorSchema {
std::unordered_map<GroupKey, GroupInfo> internal_groups;

friend void to_json(json &j, const CustomGeneratorSchema &schema) {
j[kSchemaName] = schema.name;
j[kIdsName] = schema.internal_ids;
j[kGroupsName] = schema.internal_groups;
j[kName] = schema.name;
j[kIds] = schema.internal_ids;
j[kGroups] = schema.internal_groups;
}

friend void from_json(const json &j, CustomGeneratorSchema &schema) {
j.at(kSchemaName).get_to(schema.name);
j.at(kIdsName).get_to(schema.internal_ids);
j.at(kGroupsName).get_to(schema.internal_groups);
j.at(kName).get_to(schema.name);
j.at(kIds).get_to(schema.internal_ids);
j.at(kGroups).get_to(schema.internal_groups);
}
};

Expand Down
Loading