/
main.cpp
587 lines (516 loc) · 15.4 KB
/
main.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
c++: 23
package_definitions: true
dependencies:
- org.sw.demo.nlohmann.json
- pub.egorpugin.primitives.yaml
- pub.egorpugin.primitives.sw.main
*/
#include <primitives/yaml.h>
#include <primitives/sw/main.h>
#include <primitives/sw/cl.h>
#include <nlohmann/json.hpp>
#include <iostream>
#include <fstream>
#include <map>
#include <regex>
#include <set>
#include <string>
#include <vector>
#include <unordered_map>
struct Library;
struct LibraryLess
{
bool operator()(const Library *l1, const Library *l2) const;
};
using Libraries = std::set<Library*, LibraryLess>;
using LibraryMap = std::map<String, Library*>;
const path out_dir = "out";
cl::opt<path> boost_dir("d", cl::desc("directory with boost sources"), cl::value_desc("boost dir"), cl::init("d:/dev/boost"));
cl::opt<String> version("v", cl::desc("used to open version.commits file"), cl::value_desc("boost version"), cl::Required);
String remote;
const String source = "tag";
String source_name = "boost-";
std::map<String, String> commits;
const std::map<String, std::set<String>> additional_src_deps = {
{"thread", {"date_time"}},
};
LibraryMap libraries;
struct Library
{
String name;
Libraries deps;
Libraries header_only_deps;
Libraries src_deps;
std::set<path> files;
bool has_source_dir = false;
bool contains_simple(Library* lib) const
{
if (deps.find(lib) != deps.end())
return true;
return false;
}
bool contains(Library* lib, std::set<const Library*> &checked) const
{
if (checked.find(this) != checked.end())
return false;
if (deps.find(lib) != deps.end())
return true;
checked.insert(this);
for (auto &dep : deps)
{
if (dep->contains(lib, checked))
return true;
}
checked.erase(this);
return false;
}
bool requires_building() const
{
return has_source_dir;
}
String get_url() const
{
String name = this->name;
if (name == "numeric/conversion")
name = "numeric_conversion";
if (name == "numeric/ublas")
name = "ublas";
if (name == "numeric/odeint")
name = "odeint";
if (name == "numeric/interval")
name = "interval";
String s = "https://github.com/boostorg/" + name;
return s;
}
String get_name() const
{
if (name == "numeric/conversion")
return "numeric";
if (name == "numeric/ublas")
return "ublas";
if (name == "numeric/odeint")
return "odeint";
if (name == "numeric/interval")
return "interval";
if (name.find("numeric") == 0)
return name.substr(strlen("numeric/"));
return name;
}
String get_dir() const
{
return name;
}
std::set<Library*> get_all_deps() const
{
std::set<Library*> all_deps{(Library*)this};
get_all_deps(all_deps);
all_deps.erase((Library*)this);
return all_deps;
}
private:
void get_all_deps(std::set<Library*> &all_deps) const
{
for (auto dep : deps)
{
if (all_deps.find(dep) != all_deps.end())
continue;
all_deps.insert(dep);
dep->get_all_deps(all_deps);
}
}
};
bool LibraryLess::operator()(const Library *l1, const Library *l2) const
{
return l1->get_name() < l2->get_name();
}
Library* get_library(String name)
{
boost::algorithm::to_lower(name);
Library *lib = libraries[name];
if (!lib)
lib = libraries[name] = new Library;
return lib;
}
Library* has_library(String name)
{
boost::algorithm::to_lower(name);
auto i = libraries.find(name);
if (i == libraries.end())
return nullptr;
return i->second;
}
String print_dot()
{
String s;
s += "digraph G {\n";
for (auto &lib : libraries)
{
for (auto &dep : lib.second->deps)
s += " \"" + lib.first + "\" -> \"" + dep->name + "\";\n";
}
s += "}\n";
return s;
}
void print_dot(const path &fn)
{
auto s = print_dot();
std::ofstream ofile(fn.string());
ofile << s;
}
void read_json(const path &file)
{
std::ifstream ifile(file.string());
auto data = nlohmann::json::parse(ifile);
for (auto it = data.begin(); it != data.end(); ++it)
{
auto lib = get_library(it.key());
lib->name = it.key();
for (auto &sdep : it.value())
{
String s = sdep;
lib->deps.insert(get_library(s));
}
libraries[lib->name] = lib;
}
}
void read_dir(const path &dir)
{
// find files
auto find_files = [](const path &dir, const String &prefix = String())
{
for (auto &&f : fs::directory_iterator(dir / prefix))
{
if (!fs::is_directory(f))
continue;
auto p = f.path();
auto lib = fs::relative(p, dir).string();
boost::algorithm::replace_all(lib, "\\", "/");
auto l = get_library(lib);
l->name = lib;
std::cout << "listing library dir: " << lib << "\n";
for (auto &d : { "include", "src" })
{
if (!fs::exists(p / d))
continue;
if (d == "src" && fs::exists(p / "build"))
l->has_source_dir = true;
for (auto &f : fs::recursive_directory_iterator(p / d))
{
if (!fs::is_regular_file(f))
continue;
l->files.insert(f);
}
}
}
};
find_files(dir);
find_files(dir, "numeric");
libraries.erase("numeric"); // remove empty
// read files
std::regex r_include("#\\s*?include[^<\"]*?[<\"](boost/.*?)[>\"]");
std::unordered_map<path, Library*> filemap;
for (auto &lp : libraries)
{
auto &lib = lp.second;
for (auto &p : lib->files)
{
auto rp = fs::relative(p, dir / lib->name / "include");
if (filemap.find(rp) != filemap.end())
std::cerr << "duplicate file: " << rp.string() << "\n";
filemap[rp] = lib;
}
}
for (auto &lp : libraries)
{
auto &lib = lp.second;
std::cout << "processing library dir: " << lib->name << "\n";
// read src deps
if (lib->requires_building())
{
auto f = dir / lib->get_dir() / "build";
String s;
if (fs::exists(f / "Jamfile.v2"))
s = read_file(f / "Jamfile.v2");
else if (fs::exists(f / "Jamfile"))
s = read_file(f / "Jamfile");
else
{
std::cerr << "no jamfile found\n";
continue;
}
std::regex r_lib1("<library>\\s*?/boost/([^/]*?)//boost_\\w+");
std::regex r_lib2("/boost//(\\w+)");
std::regex r_lib3("/build//boost_(\\w+)");
std::regex r_lib4("<library>.*?//boost_(\\w+)");
std::smatch m;
while (
std::regex_search(s, m, r_lib1) ||
std::regex_search(s, m, r_lib2) ||
std::regex_search(s, m, r_lib3) ||
std::regex_search(s, m, r_lib4))
{
lib->src_deps.insert(get_library(m[1].str()));
s = m.suffix();
}
}
for (auto &f : lib->files)
{
auto s = read_file(f);
std::smatch m;
while (std::regex_search(s, m, r_include))
{
auto include = m[1].str();
s = m.suffix();
auto i = filemap.find(include);
if (i != filemap.end())
lib->deps.insert(i->second);
else
std::cerr << "cannot add a file: " << include << "\n";
}
}
}
// remove self
for (auto &lib : libraries)
lib.second->deps.erase(lib.second);
}
void write_libraries(const path &fn)
{
nlohmann::json out;
for (auto &lib : libraries)
{
std::set<String> names;
for (auto &dep : lib.second->deps)
names.insert(dep->name);
nlohmann::json deps;
for (auto &n : names)
deps.push_back(n);
out[lib.first] = deps;
}
std::ofstream ofile1(fn.string());
ofile1.width(4);
ofile1 << out;
}
void prepare()
{
// gather all deps
for (auto &lp : libraries)
{
auto &lib = lp.second;
auto i = additional_src_deps.find(lib->get_name());
if (i != additional_src_deps.end())
{
for (auto dep : i->second)
lib->src_deps.insert(get_library(dep));
}
auto all_deps = lib->get_all_deps();
lib->deps.insert(all_deps.begin(), all_deps.end());
}
// process
for (auto &lp : libraries)
{
auto &lib = lp.second;
lib->header_only_deps = lib->deps;
lib->deps = lib->src_deps;
for (auto dep : lib->deps)
lib->header_only_deps.erase(dep);
}
}
void process_simple()
{
std::set<std::pair<Library*, Library*>> fails;
bool next = true;
for (auto it_lib = libraries.begin(); it_lib != libraries.end(); )
{
auto lib = it_lib->second;
if (next)
{
std::cout << "processing (simple) " << lib->name << "\n";
next = false;
}
bool stop = false;
for (auto &dep : lib->deps)
{
for (auto &dep2 : lib->deps)
{
if (dep == dep2 || dep->deps.empty() || fails.find({ dep,dep2 }) != fails.end())
continue;
if (dep->contains_simple(dep2))
{
lib->deps.erase(dep2);
stop = true;
break;
}
else
fails.insert({ dep,dep2 });
}
if (stop)
break;
}
if (!stop)
{
++it_lib;
next = true;
}
}
}
void process()
{
std::set<std::pair<Library*, Library*>> fails;
bool next = true;
for (auto it_lib = libraries.begin(); it_lib != libraries.end(); )
{
auto lib = it_lib->second;
if (next)
{
std::cout << "processing " << lib->name << "\n";
next = false;
}
bool stop = false;
for (auto &dep : lib->deps)
{
for (auto &dep2 : lib->deps)
{
if (dep == dep2 || dep->deps.empty() || fails.find({ dep,dep2 }) != fails.end())
continue;
std::set<const Library*> checked;
checked.insert(lib);
if (dep->contains(dep2, checked))
{
lib->deps.erase(dep2);
stop = true;
break;
}
else
fails.insert({ dep,dep2 });
}
if (stop)
break;
}
if (!stop)
{
++it_lib;
next = true;
}
}
}
void write_yaml_sw(const path &out_dir)
{
String root_path = "pvt.cppan.demo.boost";
YAML::Node root;
auto projects = root["projects"];
String s_cpp_libs_ho, s_cpp_libs_compiled;
String s_cpp_deps;
String s_cpp_commits_map;
// non static - causes destruction in shared object
//s_cpp_commits_map += "static const std::map<String, String> commits\n{\n";
s_cpp_commits_map += "const std::map<String, String> commits\n{\n";
for (auto &lp : libraries)
{
auto &lib = lp.second;
if (commits[lib->get_dir()].empty())
{
std::cerr << "no commit for lib: " << lib->get_name() << "\n";
//continue;
}
if (lib->requires_building())
s_cpp_libs_compiled += "\"" + lib->get_name() + "\",\n";
else
s_cpp_libs_ho += "\"" + lib->get_name() + "\",\n";
if (!commits[lib->get_dir()].empty())
s_cpp_commits_map += " { \"" + lib->get_name() + "\", " + "\"" + commits[lib->get_dir()] + "\" },\n";
YAML::Node project = projects[root_path + "." + lib->get_name()];
project["source"]["git"] = lib->get_url();
project["source"]["commit"] = commits[lib->get_dir()];
if (lib->get_name() == "log_setup")
project["source"] = projects[root_path + ".log"]["source"];
if (!project["files"].IsDefined())
{
project["files"].push_back("include/.*");
if (lib->requires_building())
project["files"].push_back("src/.*");
}
project["include_directories"]["public"].push_back("include");
if (lib->requires_building())
project["include_directories"]["private"].push_back("src");
YAML::Node deps;
for (auto &dep : lib->deps)
{
if (!lib->requires_building() && dep->requires_building())
{
YAML::Node header_dep;
header_dep["name"] = root_path + "." + dep->get_name();
header_dep["include_directories_only"] = true;
deps.push_back(header_dep);
s_cpp_deps += "add_public_dependency(\"" + lib->get_name() + "\", \"" + dep->get_name() + "\", true);\n";
continue;
}
deps.push_back(root_path + "." + dep->get_name());
s_cpp_deps += "add_public_dependency(\"" + lib->get_name() + "\", \"" + dep->get_name() + "\", false);\n";
}
for (auto &dep : lib->header_only_deps)
{
YAML::Node header_dep;
header_dep["name"] = root_path + "." + dep->get_name();
header_dep["include_directories_only"] = true;
deps.push_back(header_dep);
s_cpp_deps += "add_public_dependency(\"" + lib->get_name() + "\", \"" + dep->get_name() + "\", true);\n";
}
}
s_cpp_commits_map += "};\n";
{
std::ofstream ofile1((out_dir / "cpp_libs_header_only.txt").string());
ofile1 << s_cpp_libs_ho;
}
{
std::ofstream ofile1((out_dir / "cpp_libs_compiled.txt").string());
ofile1 << s_cpp_libs_compiled;
}
{
std::ofstream ofile1((out_dir / "cpp_deps.txt").string());
ofile1 << s_cpp_deps;
}
{
std::ofstream ofile1((out_dir / "cpp_commits_map.txt").string());
ofile1 << s_cpp_commits_map;
}
}
void debug()
{
read_json(out_dir / "processed.json");
write_libraries(out_dir / "processed.json");
print_dot(out_dir / "processed.dot");
write_yaml_sw(out_dir);
}
void release()
{
//read_json("initial.json");
read_dir(boost_dir / "libs");
write_libraries(out_dir / "initial.json");
print_dot(out_dir / "initial.dot");
prepare();
//process_simple();
//process();
//post_process();
write_libraries(out_dir / "processed.json");
print_dot(out_dir / "processed.dot");
write_yaml_sw(out_dir);
}
int main(int argc, char* argv[])
{
cl::ParseCommandLineOptions(argc, argv);
source_name += version;
std::istringstream ss(read_file(version + ".commits"));
while (1)
{
String lib, commit;
ss >> lib >> commit;
if (!ss)
break;
lib = lib.substr(5); // length of "libs/"
commits[lib] = commit;
}
fs::create_directories(out_dir);
release();
return 0;
}