-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcustom.d
174 lines (147 loc) · 4.18 KB
/
custom.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
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
module custom;
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.file;
import std.getopt;
import std.path;
import std.stdio;
import std.string;
import ae.sys.d.manager;
import ae.utils.array;
import ae.utils.json;
import ae.utils.regex;
import common;
import config : config, subDir;
import install;
import repo;
alias indexOf = std.string.indexOf;
// https://issues.dlang.org/show_bug.cgi?id=15777
alias strip = std.string.strip;
alias subDir!"result" resultDir;
/// We save a JSON file to the result directory with the build parameters.
struct BuildInfo
{
string diggerVersion;
string spec;
DiggerManager.Config.Build config;
DManager.SubmoduleState components;
}
enum buildInfoFileName = "build-info.json";
void prepareResult()
{
log("Moving...");
if (resultDir.exists)
resultDir.rmdirRecurse();
rename(d.buildDir, resultDir);
log("Build successful.\n\nTo start using it, run `digger install`, or add %s to your PATH.".format(
resultDir.buildPath("bin").absolutePath()
));
}
/// Build the customized D version.
/// The result will be in resultDir.
void runBuild(string spec, DManager.SubmoduleState submoduleState, bool asNeeded)
{
auto buildInfoPath = buildPath(resultDir, buildInfoFileName);
auto buildInfo = BuildInfo(diggerVersion, spec, d.config.build, submoduleState);
if (asNeeded && buildInfoPath.exists && buildInfoPath.readText.jsonParse!BuildInfo == buildInfo)
{
log("Reusing existing version in " ~ resultDir);
return;
}
d.build(submoduleState);
prepareResult();
std.file.write(buildInfoPath, buildInfo.toJson());
}
/// Perform an incremental build, i.e. don't clean or fetch anything from remote repos
void incrementalBuild()
{
d.rebuild();
prepareResult();
}
/// Run tests.
void runTests()
{
d.test();
}
DManager.SubmoduleState parseSpec(string spec)
{
auto parts = spec.split("+");
parts = parts.map!strip().array();
if (parts.empty)
parts = [null];
auto rev = parseRev(parts.shift());
auto state = d.begin(rev);
foreach (part; parts)
{
bool revert = part.skipOver("-");
void apply(string component, string[2] branch, DManager.MergeMode mode)
{
if (revert)
d.revert(state, component, branch, mode);
else
d.merge(state, component, branch, mode);
}
if (part.matchCaptures(re!`^(\w[\w\-\.]*)#(\d+)$`,
(string component, int pull)
{
apply(component, d.getPull(component, pull), DManager.MergeMode.cherryPick);
}))
continue;
if (part.matchCaptures(re!`^(?:([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])/)?(\w[\w\-\.]*)/(?:(\w[\w\-]*)\.\.)?(\w[\w\-]*)$`,
(string user, string component, string base, string tip)
{
// Some "do what I mean" logic here: if the user
// specified a range, or a single commit, cherry-pick;
// otherwise (just a branch name), do a git merge
auto branch = d.getBranch(component, user, base, tip);
auto mode = branch[0] ? DManager.MergeMode.cherryPick : DManager.MergeMode.merge;
apply(component, branch, mode);
}))
continue;
throw new Exception("Don't know how to apply customization: " ~ spec);
}
return state;
}
/// Build D according to the given spec string
/// (e.g. master+dmd#123).
void buildCustom(string spec, bool asNeeded = false)
{
log("Building spec: " ~ spec);
auto submoduleState = parseSpec(spec);
runBuild(spec, submoduleState, asNeeded);
}
void checkout(string spec)
{
log("Checking out: " ~ spec);
auto submoduleState = parseSpec(spec);
d.checkout(submoduleState);
log("Done.");
}
/// Build all D versions (for the purpose of caching them).
/// Build order is in steps of decreasing powers of two.
void buildAll(string spec)
{
d.needUpdate();
auto commits = d.getLog("refs/remotes/origin/" ~ spec);
commits.reverse(); // oldest first
for (int step = 1 << 30; step; step >>= 1)
{
if (step >= commits.length)
continue;
log("Building all revisions with step %d (%d/%d revisions)".format(step, commits.length/step, commits.length));
for (int n = step; n < commits.length; n += step)
try
{
auto state = d.begin(commits[n].hash);
if (!d.isCached(state))
{
log("Building revision %d/%d".format(n/step, commits.length/step));
d.build(state);
}
}
catch (Exception e)
log(e.toString());
}
}