|
| 1 | +/* |
| 2 | + * Copyright 2021 Niket Naidu. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef TARGET_GENERATOR_H_ |
| 18 | +#define TARGET_GENERATOR_H_ |
| 19 | + |
| 20 | +#include <functional> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "taskflow/taskflow.hpp" |
| 25 | + |
| 26 | +#include "env/env.h" |
| 27 | + |
| 28 | +#include "target/builder_interface.h" |
| 29 | + |
| 30 | +#include "target/generator_loader.h" |
| 31 | +#include "target/path.h" |
| 32 | + |
| 33 | +namespace buildcc::base { |
| 34 | + |
| 35 | +struct UserGenInfo { |
| 36 | + std::string name; |
| 37 | + internal::fs_unordered_set inputs; |
| 38 | + internal::fs_unordered_set outputs; |
| 39 | + std::vector<std::string> commands; |
| 40 | + bool parallel{false}; |
| 41 | + |
| 42 | + explicit UserGenInfo(const std::string &n, |
| 43 | + const internal::fs_unordered_set &i, |
| 44 | + const internal::fs_unordered_set &o, |
| 45 | + const std::vector<std::string> &c, bool p) |
| 46 | + : name(n), inputs(i), outputs(o), commands(c), parallel(p) {} |
| 47 | +}; |
| 48 | + |
| 49 | +class Generator : public BuilderInterface { |
| 50 | +public: |
| 51 | + Generator(const std::string &name, const fs::path &path) |
| 52 | + : loader_(name, path) {} |
| 53 | + Generator(const Generator &generator) = delete; |
| 54 | + |
| 55 | + void AddGenInfo(const UserGenInfo &info); |
| 56 | + void Build() override; |
| 57 | + |
| 58 | + // Getter |
| 59 | + fs::path GetBinaryPath() const { return loader_.GetBinaryPath(); } |
| 60 | + tf::Taskflow &GetTaskflow() { return tf_; } |
| 61 | + |
| 62 | +private: |
| 63 | + void GenerateTask(); |
| 64 | + // Convert from UserGenInfo to internal::GenInfo |
| 65 | + void Convert(); |
| 66 | + std::vector<const internal::GenInfo *> BuildGenerate(); |
| 67 | + bool Regenerate(std::vector<const internal::GenInfo *> &generated_files); |
| 68 | + |
| 69 | + bool Store() override; |
| 70 | + |
| 71 | + // Recheck states |
| 72 | + void InputRemoved(); |
| 73 | + void InputAdded(); |
| 74 | + void InputUpdated(); |
| 75 | + |
| 76 | + void OutputChanged(); |
| 77 | + void CommandChanged(); |
| 78 | + |
| 79 | +private: |
| 80 | + std::string name_; |
| 81 | + std::unordered_map<std::string, UserGenInfo> user_info_; |
| 82 | + std::unordered_map<std::string, internal::GenInfo> current_info_; |
| 83 | + |
| 84 | + internal::GeneratorLoader loader_; |
| 85 | + |
| 86 | + tf::Taskflow tf_; |
| 87 | + tf::Task build_task_; |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace buildcc::base |
| 91 | + |
| 92 | +#endif |
0 commit comments