Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serialization] JSON Serialization for Custom Generator #223

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 2, 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "optional"]
path = third_party/tl_optional
url = https://github.com/TartanLlama/optional.git
[submodule "json"]
path = third_party/json
url = https://github.com/nlohmann/json.git
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ include(cmake/tool/doxygen.cmake)

# Libraries
include(cmake/target/flatbuffers.cmake)
include(cmake/target/json.cmake)

include(cmake/target/fmt.cmake)
include(cmake/target/spdlog.cmake)
include(cmake/target/cli11.cmake)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Build C, C++ and ASM files in C++
- `C++11 thread` library support
- Third Party Libraries (See License below)
- Flatbuffers v2.0.0
- Nlohmann::Json v3.11.2
- Taskflow v3.1.0
- CLI11 v2.1.0
- Tiny Process Library v2.0.4
Expand Down Expand Up @@ -176,6 +177,7 @@ _BuildCC_ is licensed under the Apache License, Version 2.0. See [LICENSE](LICEN
- [Tiny Process Library](https://gitlab.com/eidheim/tiny-process-library) (Process handling) [MIT License]
- [Taskflow](https://github.com/taskflow/taskflow) (Parallel Programming) [MIT License] [Header Only]
- See also [3rd-Party](https://github.com/taskflow/taskflow/tree/master/3rd-party) used by Taskflow
- [Nlohmann::Json](https://github.com/nlohmann/json) (JSON Serialization) [MIT License] [Header Only]
- [Flatbuffers](https://github.com/google/flatbuffers) (Serialization) [Apache-2.0 License]
- [CLI11](https://github.com/CLIUtils/CLI11) (Argument Parsing) [BSD-3-Clause License] [Header Only]
- [CppUTest](https://github.com/cpputest/cpputest) (Unit Testing/Mocking) [BSD-3-Clause License]
Expand Down
1 change: 1 addition & 0 deletions bootstrap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_executable(buildcc_lib_bootstrap
)
target_sources(buildcc_lib_bootstrap PRIVATE
src/build_flatbuffers.cpp
src/build_nlohmann_json.cpp
src/build_cli11.cpp
src/build_fmtlib.cpp
src/build_spdlog.cpp
Expand Down
8 changes: 7 additions & 1 deletion bootstrap/include/bootstrap/build_buildcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "build_cli11.h"
#include "build_flatbuffers.h"
#include "build_fmtlib.h"
#include "build_nlohmann_json.h"
#include "build_spdlog.h"
#include "build_taskflow.h"
#include "build_tl_optional.h"
Expand All @@ -32,7 +33,8 @@ 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, const TargetInfo &fmt_ho,
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);
Expand All @@ -45,6 +47,7 @@ class BuildBuildCC {
public:
// TargetInfo / Header Only
static constexpr const char *const kFlatbuffersHoName = "flatbuffers_ho";
static constexpr const char *const kNlohmannJsonHoName = "nlohmann_json_ho";
static constexpr const char *const kCli11HoName = "cli11_ho";
static constexpr const char *const kFmtHoName = "fmtlib_ho";
static constexpr const char *const kSpdlogHoName = "spdlog_ho";
Expand Down Expand Up @@ -89,6 +92,9 @@ class BuildBuildCC {
TargetInfo &GetFlatbuffersHo() {
return storage_.Ref<TargetInfo>(kFlatbuffersHoName);
}
TargetInfo &GetNlohmannJsonHo() {
return storage_.Ref<TargetInfo>(kNlohmannJsonHoName);
}
TargetInfo &GetCli11Ho() { return storage_.Ref<TargetInfo>(kCli11HoName); }
TargetInfo &GetFmtHo() { return storage_.Ref<TargetInfo>(kFmtHoName); }
TargetInfo &GetSpdlogHo() { return storage_.Ref<TargetInfo>(kSpdlogHoName); }
Expand Down
28 changes: 28 additions & 0 deletions bootstrap/include/bootstrap/build_nlohmann_json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef BOOTSTRAP_BUILD_NLOHMANN_JSON_H_
#define BOOTSTRAP_BUILD_NLOHMANN_JSON_H_

#include "buildcc.h"

namespace buildcc {

void nlohmann_json_ho_cb(TargetInfo &info);

} // namespace buildcc

#endif
24 changes: 16 additions & 8 deletions bootstrap/src/build_buildcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ namespace buildcc {

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

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

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

generator.AddPatterns({
Expand All @@ -38,13 +34,14 @@ void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe) {
// generator.AddCommand("{flatc_compiler} --help");
generator.AddCommand("{flatc_compiler} -o {current_build_dir} -I "
"{current_root_dir} --gen-object-api "
"--cpp {path_fbs} {custom_generator_fbs} {target_fbs}");
"--cpp {path_fbs} {target_fbs}");

generator.Build();
}

void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
const TargetInfo &flatbuffers_ho, const TargetInfo &fmt_ho,
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) {
Expand Down Expand Up @@ -119,6 +116,9 @@ void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
// FLATBUFFERS HO
target.Insert(flatbuffers_ho, kInsertOptions);

// NLOHMANN JSON HO
target.Insert(nlohmann_json_ho, kInsertOptions);

// FMT HO
target.Insert(fmt_ho, kInsertOptions);

Expand Down Expand Up @@ -217,6 +217,12 @@ void BuildBuildCC::Initialize() {
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
env_.GetTargetBuildDir()));

// Nlohmann json HO lib
(void)storage_.Add<TargetInfo>(
kNlohmannJsonHoName, toolchain_,
TargetEnv(env_.GetTargetRootDir() / "third_party" / "json",
env_.GetTargetBuildDir()));

// CLI11 HO lib
(void)storage_.Add<TargetInfo>(
kCli11HoName, toolchain_,
Expand Down Expand Up @@ -268,6 +274,7 @@ 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();
auto &fmt_ho_lib = GetFmtHo();
auto &spdlog_ho_lib = GetSpdlogHo();
Expand All @@ -281,6 +288,7 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
.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)
.Func(fmt_ho_cb, fmt_ho_lib)
.Func(spdlog_ho_cb, spdlog_ho_lib)
Expand All @@ -290,8 +298,8 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
.Build(tpl_cb, tpl_lib)
.Func(global_flags_cb, buildcc_lib, toolchain_)
.Build(buildcc_cb, buildcc_lib, schema_gen, flatbuffers_ho_lib,
fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib, taskflow_ho_lib,
tl_optional_ho_lib, tpl_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)
.Dep(buildcc_lib, tpl_lib);
}
Expand Down
34 changes: 34 additions & 0 deletions bootstrap/src/build_nlohmann_json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "bootstrap/build_nlohmann_json.h"

namespace buildcc {

void nlohmann_json_ho_cb(TargetInfo &info) {
info.AddIncludeDir("include");
info.GlobHeaders("include/nlohmann");
info.GlobHeaders("include/nlohmann/thirdparty/hedley");
info.GlobHeaders("include/nlohmann/detail");
info.GlobHeaders("include/nlohmann/detail/conversions");
info.GlobHeaders("include/nlohmann/detail/input");
info.GlobHeaders("include/nlohmann/detail/iterators");
info.GlobHeaders("include/nlohmann/detail/meta");
info.GlobHeaders("include/nlohmann/detail/meta/call_std");
info.GlobHeaders("include/nlohmann/detail/output");
}

} // namespace buildcc
1 change: 1 addition & 0 deletions buildcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if(${BUILDCC_BUILD_AS_SINGLE_LIB})
fmt::fmt
tl::optional
flatbuffers
nlohmann_json::nlohmann_json
Taskflow
CLI11::CLI11
)
Expand Down
4 changes: 2 additions & 2 deletions buildcc/lib/target/include/target/custom_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CustomBlobHandler {
virtual std::vector<uint8_t> Serialize() const = 0;
};

struct UserGenInfo : internal::GenInfo {
struct UserGenInfo : internal::CustomGeneratorSchema::IdInfo {
fs_unordered_set inputs;
GenerateCb generate_cb;
std::shared_ptr<CustomBlobHandler> blob_handler{nullptr};
Expand All @@ -99,7 +99,7 @@ struct UserCustomGeneratorSchema : public internal::CustomGeneratorSchema {
for (auto &[id, gen_info] : gen_info_map) {
gen_info.internal_inputs = path_schema_convert(
gen_info.inputs, internal::Path::CreateExistingPath);
auto [_, success] = internal_gen_info_map.try_emplace(id, gen_info);
auto [_, success] = internal_ids.try_emplace(id, gen_info);
env::assert_fatal(success, fmt::format("Could not save {}", id));
}
}
Expand Down
8 changes: 3 additions & 5 deletions buildcc/lib/target/src/custom_generator/custom_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ void CustomGenerator::BuildGenerate(
[&](const auto &iter) { gen_selected_ids.insert(iter.first); });
dirty_ = true;
} else {
// DONE, Conditionally select internal_gen_info_map depending on what has
// DONE, Conditionally select internal_ids depending on what has
// changed
const auto &prev_gen_info_map =
serialization_.GetLoad().internal_gen_info_map;
const auto &prev_gen_info_map = serialization_.GetLoad().internal_ids;
const auto &curr_gen_info_map = user_.gen_info_map;

// DONE, MAP REMOVED condition Check if prev_gen_info_map exists in
Expand Down Expand Up @@ -293,8 +292,7 @@ void CustomGenerator::TaskRunner(bool run, const std::string &id) {
if (run) {
rerun = true;
} else {
const auto &previous_info =
serialization_.GetLoad().internal_gen_info_map.at(id);
const auto &previous_info = serialization_.GetLoad().internal_ids.at(id);
rerun = internal::CheckPaths(previous_info.internal_inputs,
current_info.internal_inputs) !=
internal::PathState::kNoChange ||
Expand Down
Loading