-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcopy_translated_headers.d
99 lines (82 loc) · 2.82 KB
/
copy_translated_headers.d
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
int main(string[] args) {
try {
run(args);
return 0;
} catch(Exception e) {
import std.stdio: stderr;
stderr.writeln("ERROR: ", e.msg);
return 1;
}
}
void run(string[] args) @safe {
import std.path : buildPath, baseName;
import std.conv : text;
import std.file : copy, mkdirRecurse, exists;
if(args.length < 2)
throw new Exception("First argument must be directory of libclang repository");
const repoPath = args[1];
const majorVersion = () @trusted {
try
return clangVersion;
catch(Exception e) {
import std.stdio : stderr;
stderr.writeln("Could not get clang version, defaulting to 14:\n" ~ e.msg);
return 14;
}
}();
// treat every version before 15 as 14
const versionString = majorVersion < 15 ? "14" : "15";
const translationsPath = buildPath(repoPath, "pretranslated", versionString);
const codePath = buildPath(repoPath, "source", "clang", "c");
if(!codePath.exists)
mkdirRecurse(codePath);
foreach(string src; entries(translationsPath)) {
const dst = buildPath(codePath, baseName(src));
copy(src, dst);
}
}
private string[] entries(in string path) @trusted {
import std.file : dirEntries, SpanMode;
import std.algorithm : map;
import std.array : array;
return dirEntries(path, SpanMode.breadth)
.map!(a => a.name)
.array;
}
private int clangVersion() @safe {
import std.string: splitLines, split;
import std.conv: to, text;
import std.algorithm : countUntil;
const clangOutput = exe(["clang", "--version"]);
const clangLines = clangOutput.splitLines;
if(clangLines.length < 1)
throw new Exception("Could not get 1st line from clang output\n'" ~ clangOutput ~ "'\n");
const firstLine = clangLines[0];
const elements = firstLine.split(" ");
const versionIndex = elements.countUntil("version");
void fail(A...)(A args) {
import std.conv : text;
throw new Exception(
text(args, "\n",
"Full `clang --version` output:\n\n",
clangOutput,
)
);
}
if(versionIndex < 0 || versionIndex >= elements.length - 1)
fail("Could not get version from line '", firstLine, "'");
const version_ = elements[versionIndex + 1];
const versionParts = version_.split(".");
if(versionParts.length < 2)
fail("Could not get major version from '", version_, "'");
return versionParts[0].to!int;
}
private string exe(string[] args) @safe {
import std.process: execute;
import std.conv: text;
import std.string: join;
const res = execute(args);
if(res.status != 0)
throw new Exception(text("Could not execute ", args.join(" ")));
return res.output;
}