1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/**
** \file task/task-register.cc
** \brief Implementation of task::TaskRegister.
**
*/
#include <algorithm>
#include <exception>
#include <filesystem>
#include <fstream>
#include <map>
#include <common.hh>
#include <misc/algorithm.hh>
#include <task/argument-task.hh>
#include <task/disjunctive-task.hh>
#include <task/simple-task.hh>
#include <task/task-register.hh>
namespace task
{
// Singleton stuff
TaskRegister& TaskRegister::instance()
{
static TaskRegister instance_;
return instance_;
}
// Register this task.
void TaskRegister::register_task(const SimpleTask& task)
{
auto it = register_task_(task);
// The task was already registered.
if (it == modules_.end())
return;
// Callback when the option is parsed.
auto cb = [this, &task](bool f) {
if (f)
enable_task(task.name_get());
};
namespace po = boost::program_options;
auto v = po::bool_switch()->notifier(cb);
it->second.add_options()(task.fullname_get(), v, task.desc_get());
}
void TaskRegister::register_task(const ArgumentTask& task)
{
auto it = register_task_(task);
// The taks was already registered.
if (it == modules_.end())
return;
// Callback when the option is parsed.
auto cb = [this, &task](const std::string& arg) {
task.arg_set(arg);
enable_task(task.name_get());
};
namespace po = boost::program_options;
auto v = po::value<std::string>()
->value_name(task.argname_get())
->required()
->notifier(cb);
it->second.add_options()(task.fullname_get(), v, task.desc_get());
}
// Internal task registration.
// Add the task to the list of tasks and return an iterrator to the module
// the option belongs to. The module is created if needed.
// In case something went wrong, the end of `modules_' is returned.
TaskRegister::indexed_module_type::iterator
TaskRegister::register_task_(const Task& task)
{
if (task_list_.find(task.name_get()) != task_list_.end())
{
task_error() << misc::error::error_type::failure << program_name
<< ": TaskRegister::register_task(" << task.name_get()
<< "): task name already registered.\n";
return modules_.end();
}
task_list_[task.name_get()] = &task;
// Short-hands.
namespace po = boost::program_options;
const std::string& module_name = task.module_name_get();
auto it = modules_.find(module_name);
if (it == modules_.end())
it = modules_.emplace(module_name, po::options_description(module_name))
.first;
// Return the iterator on the module the task belongs to.
return it;
}
// Request the execution of the task task_name.
void TaskRegister::enable_task(const std::string& task_name)
{
if (task_list_.find(task_name) == task_list_.end())
task_error() << misc::error::error_type::failure << program_name
<< ": TaskRegister::enable_task(" << task_name
<< "): this task has not been registered.\n";
else
{
const Task* task = task_list_.find(task_name)->second;
resolve_dependencies(*task);
// FIXME: for efficiency, resolve_dependency should be called once.
// FIXME: detect cycle.
task_order_.emplace_back(task);
}
}
// Return the number of tasks to execute.
int TaskRegister::nb_of_task_to_execute_get() { return task_order_.size(); }
// Resolve dependencies between tasks.
void TaskRegister::resolve_dependencies(const Task& task)
{
tasks_list_type enabled_tasks;
// Retrieved already active tasks.
for (const std::string& s : task.dependencies_get())
if (task_list_.find(s) == task_list_.end())
{
task_error() << misc::error::error_type::failure << program_name
<< ": TaskRegister::resolve_dependencies(\""
<< task.name_get() << "\"): unknown task: \"" << s << '"'
<< std::endl;
}
else
{
const Task* task_dep = task_list_.find(s)->second;
if (misc::has(task_order_, task_dep))
enabled_tasks.emplace_back(task_dep);
}
// Ask the task which dependent tasks should be activated.
const Task::deps_type dep_tasks = task.resolve_dependencies(enabled_tasks);
// Activate them.
for (const std::string& s : dep_tasks)
{
if (task_list_.find(s) != task_list_.end())
if (!misc::has(task_order_, task_list_.find(s)->second))
enable_task(s);
}
}
// Check whether one of the options in os has the string_key s.
template <typename T>
static bool
is_parsed(const std::string& s,
const std::vector<boost::program_options::basic_option<T>>& os)
{
using option = boost::program_options::basic_option<T>;
return std::ranges::find_if(
os, [&s](const option& o) { return s == o.string_key; })
!= end(os);
}
char* TaskRegister::parse_arg(int argc, char* argv[])
{
// Short-hand.
namespace po = boost::program_options;
std::string input_file;
// Create the category containing `help', `version' and `usage'.
po::options_description generic;
generic.add_options()("help,?", "Give this help list")(
"usage", "Give a short usage message")(
"version", "Print program version")("license", "Print program license");
// Positional parameter.
po::options_description hidden;
po::positional_options_description positional;
hidden.add_options()("input-file",
po::value<std::string>(&input_file)->required(),
"Input file");
positional.add("input-file", 1);
// Create the top-level category, with visible options.
po::options_description visible_desc(program_doc);
// Add each category to the top-level one.
for (const auto& i : modules_)
visible_desc.add(i.second);
visible_desc.add(generic);
// Sum of visible options and positional argument.
po::options_description all_opts = visible_desc;
all_opts.add(hidden);
// Give control to boost.
try
{
// Sadly we can't use `boost::program_options::variables_map'. By doing
// so, we would lose the chronological order of the tasks, which is
// capital for TC to work.
auto parsed = po::command_line_parser(argc, argv)
.options(all_opts)
.positional(positional)
.run();
// We don't want to fail if these options are present. So we take care
// of them right now.
if (is_parsed("help", parsed.options))
std::cout << visible_desc;
else if (is_parsed("version", parsed.options))
std::cout << program_version;
else if (is_parsed("usage", parsed.options))
std::cout << "tc [OPTIONS...] INPUT-FILE\n";
else if (is_parsed("license", parsed.options))
{
std::filesystem::path licenses("licenses");
if (!std::filesystem::exists(licenses))
{
std::cerr << program_name << ": cannot open licenses directory"
<< ": No such file or directory\n";
}
else if (!std::filesystem::is_directory(licenses))
{
std::cerr << program_name << ": cannot open licenses directory"
<< ": Not a directory\n";
}
else
{
using directory_iterator = std::filesystem::directory_iterator;
for (const auto& entry : directory_iterator(licenses))
{
const auto& path = entry.path();
if (!std::filesystem::is_regular_file(path)
|| !path.has_extension()
|| path.extension() != ".license")
continue;
std::ifstream license(path);
if (!license.is_open())
continue;
std::cout << "\n === " << path.stem().string()
<< " license notice ===\n\n"
<< license.rdbuf()
<< "\n === " << path.stem().string()
<< " license notice ===\n"
<< std::endl;
}
}
}
else
{
// Replace the traditional calls to `vm.store()' and `vm.notify()'.
for (const auto& i : parsed.options)
{
auto option = parsed.description->find(i.string_key, false);
po::variable_value v;
option.semantic()->parse(v.value(), i.value, true);
option.semantic()->notify(v.value());
}
// If no input file is given, throw.
if (input_file.size() == 0)
throw po::error("no file name");
}
}
catch (const po::error& e)
{
std::cerr << program_name << ": " << e.what()
<< "\ntc [OPTIONS...] INPUT-FILE\n"
"Try `tc --help' or `tc --usage' for more information.\n";
throw std::invalid_argument("command line parsing error");
}
catch (const std::invalid_argument& e)
{
throw;
}
char* input_file_ = nullptr;
if (!input_file.empty())
{
input_file_ = new char[input_file.size() + 1];
strcpy(input_file_, input_file.c_str());
}
return input_file_;
}
// Display registered Tasks.
std::ostream& TaskRegister::print_task_list(std::ostream& ostr)
{
ostr << "List of registered tasks:\n";
for (const tasks_by_name_type::value_type& i : task_list_)
ostr << "\t* " << i.first << '\n';
return ostr << std::endl;
}
// Dump task graph.
std::ostream& TaskRegister::print_task_graph(std::ostream& ostr)
{
ostr << "/* Task graph */\n"
<< "digraph Tasks {\n"
<< " node [shape=box, fontsize=14]\n"
// Preserve the order of the children.
<< " graph [ordering=out]\n";
for (const tasks_by_name_type::value_type& i : task_list_)
{
const Task& task = *i.second;
if (dynamic_cast<const DisjunctiveTask*>(&task))
ostr << " \"" << task.name_get() << "\" [shape=diamond]\n";
ostr << " \"" << task.name_get() << "\"";
if (task.dependencies_get().size())
{
ostr << " -> {";
for (const std::string& s : task.dependencies_get())
ostr << " \"" << s << "\"";
ostr << " } ";
}
ostr << '\n';
}
return ostr << "}\n";
}
// Display registered Tasks execution order.
std::ostream& TaskRegister::print_task_order(std::ostream& ostr)
{
ostr << "List of Task Order:\n";
for (const Task* t : task_order_)
ostr << "\t* " << t->name_get() << std::endl;
return ostr << std::endl;
}
// Execute tasks, checking dependencies.
void TaskRegister::execute()
{
// FIXME: should be the only one to call resolve_dependency.
for (const Task* t : task_order_)
{
std::string pref(t->module_name_get());
if (!pref.empty())
pref = pref[0] + std::string(": ");
timer_.push(pref + t->name_get());
t->execute();
timer_.pop(pref + t->name_get());
}
}
} // namespace task
|