Debian Codegraph @

libphobos/scripts/gen_phobos_sources.d

raw
1 #!/usr/bin/env dub
2 /++dub.sdl:
3 name "gen_phobos_sources"
4 +/
5 // Written in the D programming language.
6 import std.stdio;
7 import std.file;
8 import std.path;
9 import std.range;
10 import std.string;
11 import std.algorithm;
12
13 string[] filterList = [
14 "./Makefile.in", "./Makefile.am",
15 "./index.dd",
16 "./libgphobos.spec.in", "./drtstuff.spec",
17 "./LICENSE_1_0.txt", "./MERGE",
18 "./std/experimental/note.md"
19 ];
20
21 struct Files
22 {
23 string[] baseList;
24 string[][string] sysList;
25 }
26
27 void main(string[] args)
28 {
29 Files[string] fileMap;
30
31 foreach (entry; "."
32 .dirEntries(SpanMode.depth)
33 .filter!(a => !filterList.canFind(a)))
34 {
35 if (entry.isFile)
36 {
37 auto ext = entry.extension.empty ? "" : entry.extension[1 .. $];
38 if (!(ext in fileMap))
39 fileMap[ext] = Files.init;
40
41 string sentry = entry[2 .. $];
42
43 if (entry.name.startsWith("./std/c/"))
44 {
45 if (entry.dirName == "./std/c")
46 {
47 fileMap[ext].sysList["stdc"] ~= sentry;
48 }
49 else
50 {
51 auto components = entry.pathSplitter;
52 components.popFrontN(3);
53 fileMap[ext].sysList[components.front] ~= sentry;
54 }
55 }
56 else
57 fileMap[ext].baseList ~= sentry;
58 }
59 }
60
61 writeln("if ENABLE_LIBDRUNTIME_ONLY");
62 foreach (extEntry; fileMap.byKeyValue.array.sort!"a.key < b.key")
63 {
64 auto ext = extEntry.key;
65 auto value = extEntry.value;
66 writeList("PHOBOS_" ~ ext.toUpper() ~ "SOURCES", [],
67 !value.baseList.empty);
68 foreach (entry; value.sysList.byKeyValue.array.sort!"a.key < b.key")
69 {
70 string name = "PHOBOS_" ~ ext.toUpper() ~ "SOURCES_"
71 ~ entry.key.toUpper();
72 writeList(name, [], !entry.value.empty);
73 }
74 }
75 writeln();
76 writeln("else");
77 foreach (extEntry; fileMap.byKeyValue.array.sort!"a.key < b.key")
78 {
79 auto ext = extEntry.key;
80 auto value = extEntry.value;
81 writeList("PHOBOS_" ~ ext.toUpper() ~ "SOURCES", value.baseList);
82 foreach (entry; value.sysList.byKeyValue.array.sort!"a.key < b.key")
83 {
84 string name = "PHOBOS_" ~ ext.toUpper() ~ "SOURCES_"
85 ~ entry.key.toUpper();
86 writeList(name, entry.value);
87 }
88 }
89 writeln();
90 writeln("endif");
91 }
92
93 void writeList(string name, string[] values, bool force = false)
94 {
95 if (!force && values.empty)
96 return;
97
98 values = sort(values).array();
99 writeln();
100 writef("%s =", name);
101 size_t line = name.length + 3;
102 foreach (entry; values)
103 {
104 if (line + entry.length > 70)
105 {
106 line = 0;
107 writeln(` \`);
108 write('\t');
109 }
110 else
111 write(" ");
112 write(entry);
113 line += entry.length + 1;
114 }
115 writeln();
116 }