Skip to content

Template generator #214

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 16 commits into from
May 15, 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
2 changes: 1 addition & 1 deletion bootstrap/main.buildcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char **argv) {
ExecutableTarget_generic buildcc_hybrid_simple_example(
"buildcc_hybrid_simple_example", toolchain, "example/hybrid/simple");
Reg::Toolchain(custom_toolchain_arg.state)
// .Func([&]() { toolchain.Verify(); })
.Func([&]() { toolchain.Verify(); })
.BuildPackage(buildcc)
.Build(hybrid_simple_example_cb, buildcc_hybrid_simple_example,
buildcc_lib)
Expand Down
5 changes: 2 additions & 3 deletions bootstrap/src/build_buildcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe) {
generator.AddOutput("{gen_build_dir}/custom_generator_generated.h");
generator.AddOutput("{gen_build_dir}/target_generated.h");

generator.AddDefaultArguments({
generator.AddPatterns({
{"flatc_compiler", fmt::format("{}", flatc_exe.GetTargetPath())},
});
// generator.AddCommand("{flatc_compiler} --help");
Expand All @@ -45,8 +45,7 @@ void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
const TargetInfo &taskflow_ho, const BaseTarget &tpl) {
// NOTE, Build as single lib
target.AddIncludeDir("", true);
const std::string &schema_build_dir =
schema_gen.GetValueByIdentifier("gen_build_dir");
const std::string &schema_build_dir = schema_gen.Get("gen_build_dir");
target.AddIncludeDirAbsolute(schema_build_dir, true);

// ENV
Expand Down
3 changes: 2 additions & 1 deletion buildcc/buildcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
// Base
#include "toolchain/toolchain.h"
#include "target/custom_generator.h"
#include "target/generator.h"
#include "target/file_generator.h"
#include "target/template_generator.h"
#include "target/target_info.h"
#include "target/target.h"

Expand Down
3 changes: 2 additions & 1 deletion buildcc/lib/args/include/args/register.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

#include "args.h"

#include "target/generator.h"
#include "target/custom_generator.h"
#include "target/file_generator.h"
#include "target/target.h"

#include "args/register/test_info.h"
Expand Down
6 changes: 4 additions & 2 deletions buildcc/lib/target/cmake/common_target_src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ set(COMMON_TARGET_SRCS
# Generator
src/custom_generator/custom_generator.cpp
include/target/custom_generator.h
src/generator/generator.cpp
include/target/generator.h
src/generator/file_generator.cpp
include/target/file_generator.h
src/generator/template_generator.cpp
include/target/template_generator.h

# Target Info
src/target_info/target_info.cpp
Expand Down
22 changes: 10 additions & 12 deletions buildcc/lib/target/include/target/custom_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,12 @@ class CustomGenerator : public internal::BuilderInterface {
virtual ~CustomGenerator() = default;
CustomGenerator(const CustomGenerator &) = delete;

// From env::Command module, forwarding here
// TODO, Create a Mixin
void AddDefaultArgument(const std::string &identifier,
const std::string &pattern);
void AddDefaultArguments(
const std::unordered_map<std::string, std::string> &arguments);
std::string Construct(
const std::string &pattern,
const std::unordered_map<const char *, std::string> &arguments = {});
const std::string &
GetValueByIdentifier(const std::string &file_identifier) const;
// TODO, Doc
void AddPattern(const std::string &identifier, const std::string &pattern);

// TODO, Doc
void
AddPatterns(const std::unordered_map<std::string, std::string> &pattern_map);

/**
* @brief Single Generator task for inputs->generate_cb->outputs
Expand Down Expand Up @@ -168,6 +163,7 @@ class CustomGenerator : public internal::BuilderInterface {
const fs::path &GetRootDir() const { return env_.GetTargetRootDir(); }
const fs::path &GetBuildDir() const { return env_.GetTargetBuildDir(); }
tf::Taskflow &GetTaskflow() { return tf_; }
const std::string &Get(const std::string &file_identifier) const;

private:
void Initialize();
Expand All @@ -184,6 +180,9 @@ class CustomGenerator : public internal::BuilderInterface {
void IdAdded();
void IdUpdated();

protected:
env::Command command_;

private:
std::string name_;
TargetEnv env_;
Expand All @@ -196,7 +195,6 @@ class CustomGenerator : public internal::BuilderInterface {
std::unordered_map<std::string, UserGenInfo> success_schema_;

// Internal
env::Command command_;
tf::Taskflow tf_;

// Callbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

#ifndef TARGET_GENERATOR_H_
#define TARGET_GENERATOR_H_
#ifndef TARGET_FILE_GENERATOR_H_
#define TARGET_FILE_GENERATOR_H_

#include "target/custom_generator.h"

Expand Down
54 changes: 20 additions & 34 deletions buildcc/lib/target/include/target/interface/builder_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace buildcc::internal {

enum PathState {
enum class PathState {
kNoChange,
kRemoved,
kAdded,
Expand Down Expand Up @@ -69,14 +69,14 @@ inline PathState CheckPaths(const internal::path_unordered_set &previous_path,
const bool added_cond = (find == previous_path.end());
if (added_cond) {
dirty = true;
state = kAdded;
state = PathState::kAdded;
} else {
const bool updated_cond =
(p.GetLastWriteTimestamp() >
find->GetLastWriteTimestamp());
if (updated_cond) {
dirty = true;
state = kUpdated;
state = PathState::kUpdated;
} else {
dirty = false;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ class BuilderInterface {
return;
}

if (previous != current) {
if (CheckChanged(previous, current)) {
callback();
dirty_ = true;
}
Expand All @@ -130,39 +130,25 @@ class BuilderInterface {
return;
}

// * Old path is removed
const bool removed =
std::any_of(previous_path.begin(), previous_path.end(),
[&](const internal::Path &p) {
return current_path.find(p) == current_path.end();
});
if (removed) {
auto path_state = CheckPaths(previous_path, current_path);
switch (path_state) {
case PathState::kRemoved:
path_removed_cb();
dirty_ = true;
return;
break;
case PathState::kAdded:
path_added_cb();
dirty_ = true;
break;
case PathState::kUpdated:
path_updated_cb();
dirty_ = true;
break;
case PathState::kNoChange:
default:
dirty_ = false;
break;
}

dirty_ = std::any_of(
current_path.cbegin(), current_path.cend(),
[&](const internal::Path &p) -> bool {
bool dirty = false;
const auto find = previous_path.find(p);
const bool added_cond = (find == previous_path.end());
if (added_cond) {
path_added_cb();
dirty = true;
} else {
const bool updated_cond =
(p.GetLastWriteTimestamp() > find->GetLastWriteTimestamp());
if (updated_cond) {
path_updated_cb();
dirty = true;
} else {
dirty = false;
}
}
return dirty;
});
}

protected:
Expand Down
58 changes: 58 additions & 0 deletions buildcc/lib/target/include/target/template_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 TARGET_TEMPLATE_GENERATOR_H_
#define TARGET_TEMPLATE_GENERATOR_H_

#include "target/custom_generator.h"

namespace buildcc {

class TemplateGenerator : public CustomGenerator {
public:
TemplateGenerator(const std::string &name, const TargetEnv &env)
: CustomGenerator(name, env) {}

void AddTemplate(const fs::path &input_filename,
const fs::path &output_filename);
std::string Parse(const std::string &pattern);

/**
* @brief Build FileGenerator Tasks
*
* Use `GetTaskflow` for the registered tasks
*/
void Build() override;

// Restrict access to certain custom generator APIs
private:
using CustomGenerator::AddDependencyCb;
using CustomGenerator::AddGenInfo;
using CustomGenerator::Build;

private:
struct TemplateInfo {
fs::path input;
fs::path output;
};

private:
std::vector<TemplateInfo> template_infos_;
};

} // namespace buildcc

#endif
34 changes: 18 additions & 16 deletions buildcc/lib/target/src/custom_generator/custom_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,20 @@ constexpr const char *const kGenerateTaskName = "Generate";

namespace buildcc {

void CustomGenerator::AddDefaultArgument(const std::string &identifier,
const std::string &pattern) {
void CustomGenerator::AddPattern(const std::string &identifier,
const std::string &pattern) {
command_.AddDefaultArgument(identifier, command_.Construct(pattern));
}

void CustomGenerator::AddDefaultArguments(
const std::unordered_map<std::string, std::string> &arguments) {
for (const auto &arg_iter : arguments) {
AddDefaultArgument(arg_iter.first, arg_iter.second);
void CustomGenerator::AddPatterns(
const std::unordered_map<std::string, std::string> &pattern_map) {
for (const auto &arg_iter : pattern_map) {
AddPattern(arg_iter.first, arg_iter.second);
}
}

std::string CustomGenerator::Construct(
const std::string &pattern,
const std::unordered_map<const char *, std::string> &arguments) {
return command_.Construct(pattern, arguments);
}

const std::string &CustomGenerator::GetValueByIdentifier(
const std::string &file_identifier) const {
const std::string &
CustomGenerator::Get(const std::string &file_identifier) const {
return command_.GetDefaultValueByKey(file_identifier);
}

Expand All @@ -60,10 +54,16 @@ void CustomGenerator::AddGenInfo(

UserGenInfo schema;
for (const auto &i : inputs) {
schema.inputs.emplace(command_.Construct(path_as_string(i)));
fs::path input =
internal::Path::CreateNewPath(command_.Construct(path_as_string(i)))
.GetPathname();
schema.inputs.emplace(std::move(input));
}
for (const auto &o : outputs) {
schema.outputs.emplace(command_.Construct(path_as_string(o)));
fs::path output =
internal::Path::CreateNewPath(command_.Construct(path_as_string(o)))
.GetPathname();
schema.outputs.emplace(std::move(output));
}
schema.generate_cb = generate_cb;
schema.blob_handler = std::move(blob_handler);
Expand All @@ -90,6 +90,8 @@ void CustomGenerator::Initialize() {
//
fs::create_directories(env_.GetTargetBuildDir());
command_.AddDefaultArguments({
{"project_root_dir", path_as_string(Project::GetRootDir())},
{"project_build_dir", path_as_string(Project::GetBuildDir())},
{"gen_root_dir", path_as_string(env_.GetTargetRootDir())},
{"gen_build_dir", path_as_string(env_.GetTargetBuildDir())},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "target/generator.h"
#include "target/file_generator.h"

#include <algorithm>

Expand Down Expand Up @@ -80,32 +80,35 @@ namespace buildcc {

void FileGenerator::AddInput(const std::string &absolute_input_pattern,
const char *identifier) {
std::string absolute_input_string = Construct(absolute_input_pattern);
std::string absolute_input_string =
command_.Construct(absolute_input_pattern);
const auto absolute_input_path =
internal::Path::CreateNewPath(absolute_input_string);
inputs_.insert(absolute_input_path.GetPathname());

if (identifier != nullptr) {
AddDefaultArgument(identifier, absolute_input_path.GetPathAsString());
AddPattern(identifier, absolute_input_path.GetPathAsString());
}
}

void FileGenerator::AddOutput(const std::string &absolute_output_pattern,
const char *identifier) {
std::string absolute_output_string = Construct(absolute_output_pattern);
std::string absolute_output_string =
command_.Construct(absolute_output_pattern);
const auto absolute_output_path =
internal::Path::CreateNewPath(absolute_output_string);
outputs_.insert(absolute_output_path.GetPathname());

if (identifier != nullptr) {
AddDefaultArgument(identifier, absolute_output_path.GetPathAsString());
AddPattern(identifier, absolute_output_path.GetPathAsString());
}
}

void FileGenerator::AddCommand(
const std::string &command_pattern,
const std::unordered_map<const char *, std::string> &arguments) {
std::string constructed_command = Construct(command_pattern, arguments);
std::string constructed_command =
command_.Construct(command_pattern, arguments);
commands_.emplace_back(std::move(constructed_command));
}

Expand Down
Loading