|
| 1 | +/* |
| 2 | + * Copyright 2021-2022 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_CUSTOM_GENERATOR_H_ |
| 18 | +#defineTARGET_CUSTOM_GENERATOR_H_ |
| 19 | + |
| 20 | +#include<mutex> |
| 21 | +#include<string> |
| 22 | +#include<unordered_map> |
| 23 | +#include<vector> |
| 24 | + |
| 25 | +#include"taskflow/taskflow.hpp" |
| 26 | + |
| 27 | +#include"env/command.h" |
| 28 | +#include"env/task_state.h" |
| 29 | + |
| 30 | +#include"target/interface/builder_interface.h" |
| 31 | + |
| 32 | +#include"schema/custom_generator_serialization.h" |
| 33 | +#include"schema/path.h" |
| 34 | + |
| 35 | +#include"target/common/target_env.h" |
| 36 | + |
| 37 | +namespacebuildcc { |
| 38 | + |
| 39 | +// TODO, Shift to a different file |
| 40 | +// TODO, Check if we need the "id" here as well |
| 41 | +classCustomGeneratorContext { |
| 42 | +public: |
| 43 | +CustomGeneratorContext(const env::Command &c,const fs_unordered_set &i, |
| 44 | +const fs_unordered_set &o) |
| 45 | + : command(c), inputs(i), outputs(o) {} |
| 46 | + |
| 47 | +public: |
| 48 | +const env::Command &command; |
| 49 | +const fs_unordered_set &inputs; |
| 50 | +const fs_unordered_set &outputs; |
| 51 | +}; |
| 52 | + |
| 53 | +// clang-format off |
| 54 | +typedef std::function<bool(CustomGeneratorContext &)> GenerateCb; |
| 55 | + |
| 56 | +typedef std::function<void(std::unordered_map<std::string, tf::Task> &)> DependencyCb; |
| 57 | +// clang-format on |
| 58 | + |
| 59 | +structUserRelInputOutputSchema : internal::RelInputOutputSchema { |
| 60 | + fs_unordered_set inputs; |
| 61 | + GenerateCb generate_cb; |
| 62 | +}; |
| 63 | + |
| 64 | +structUserCustomGeneratorSchema :publicinternal::CustomGeneratorSchema { |
| 65 | + std::unordered_map<std::string, UserRelInputOutputSchema> rels_map; |
| 66 | + |
| 67 | +voidConvertToInternal() { |
| 68 | +for (auto &r_miter : rels_map) { |
| 69 | + r_miter.second.internal_inputs =path_schema_convert( |
| 70 | + r_miter.second.inputs, internal::Path::CreateExistingPath); |
| 71 | +auto p = internal_rels_map.emplace(r_miter.first, r_miter.second); |
| 72 | +env::assert_fatal(p.second, |
| 73 | +fmt::format("Could not save {}", r_miter.first)); |
| 74 | + } |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +classCustomGenerator :publicinternal::BuilderInterface { |
| 79 | +public: |
| 80 | +CustomGenerator(const std::string &name,const TargetEnv &env) |
| 81 | + : name_(name), |
| 82 | +env_(env.GetTargetRootDir(), env.GetTargetBuildDir() / name), |
| 83 | + serialization_(env_.GetTargetBuildDir() / fmt::format("{}.bin", name)) { |
| 84 | +Initialize(); |
| 85 | + } |
| 86 | +virtual~CustomGenerator() =default; |
| 87 | +CustomGenerator(const CustomGenerator &) = delete; |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief Add default arguments for input, output and command requirements |
| 91 | + * |
| 92 | + * @param arguments Key-Value pair for arguments |
| 93 | +*/ |
| 94 | +voidAddDefaultArguments( |
| 95 | +const std::unordered_map<std::string, std::string> &arguments); |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Single Generator task for inputs->generate_cb->outputs |
| 99 | + * |
| 100 | + * @param id Unique id associated with Generator task |
| 101 | + * @param inputs File inputs |
| 102 | + * @param outputs File outputs |
| 103 | + * @param generate_cb User-defined generate callback to build outputs from the |
| 104 | + * provided inputs |
| 105 | +*/ |
| 106 | +voidAddGenInfo(const std::string &id,const fs_unordered_set &inputs, |
| 107 | +const fs_unordered_set &outputs, |
| 108 | +const GenerateCb &generate_cb); |
| 109 | + |
| 110 | +// Callbacks |
| 111 | +/** |
| 112 | + * @brief Setup dependencies between Tasks using their `id` |
| 113 | + * For example: `task_map["id1"].precede(task_map["id2"])` |
| 114 | + * |
| 115 | + * IMPORTANT: Successor tasks will not automatically run if dependent task is |
| 116 | + * run. |
| 117 | + * The Dependency callback only sets precedence (order in which your tasks |
| 118 | + * should run) |
| 119 | + * Default behaviour when dependency callback is not supplied: All task `id`s |
| 120 | + * run in parallel. |
| 121 | + * |
| 122 | + * @param dependency_cb Unordered map of `id` and `task` |
| 123 | + * The map can be safely mutated. |
| 124 | +*/ |
| 125 | +voidAddDependencyCb(const DependencyCb &dependency_cb); |
| 126 | + |
| 127 | +voidBuild()override; |
| 128 | + |
| 129 | +// Getters |
| 130 | +const fs::path &GetBinaryPath()const { |
| 131 | +return serialization_.GetSerializedFile(); |
| 132 | + } |
| 133 | +const fs::path &GetRootDir()const {return env_.GetTargetRootDir(); } |
| 134 | +const fs::path &GetBuildDir()const {return env_.GetTargetBuildDir(); } |
| 135 | + tf::Taskflow &GetTaskflow() {return tf_; } |
| 136 | + |
| 137 | +private: |
| 138 | +voidInitialize(); |
| 139 | + |
| 140 | +template<bool run>voidTaskRunner(const std::string &id); |
| 141 | + |
| 142 | +voidGenerateTask(); |
| 143 | +voidBuildGenerate(std::unordered_map<std::string, UserRelInputOutputSchema> |
| 144 | + &gen_selected_map, |
| 145 | + std::unordered_map<std::string, UserRelInputOutputSchema> |
| 146 | + &dummy_gen_selected_map); |
| 147 | + |
| 148 | +// Recheck states |
| 149 | +voidIdRemoved(); |
| 150 | +voidIdAdded(); |
| 151 | +voidIdUpdated(); |
| 152 | + |
| 153 | +private: |
| 154 | + std::string name_; |
| 155 | + TargetEnv env_; |
| 156 | + internal::CustomGeneratorSerialization serialization_; |
| 157 | + |
| 158 | +// Serialization |
| 159 | + UserCustomGeneratorSchema user_; |
| 160 | + |
| 161 | + std::mutex success_schema_mutex_; |
| 162 | + std::unordered_map<std::string, UserRelInputOutputSchema> success_schema_; |
| 163 | + |
| 164 | +// Internal |
| 165 | + env::Command command_; |
| 166 | + tf::Taskflow tf_; |
| 167 | + |
| 168 | +// Callbacks |
| 169 | + DependencyCb dependency_cb_; |
| 170 | +}; |
| 171 | + |
| 172 | +}// namespace buildcc |
| 173 | + |
| 174 | +#endif |