Repository navigation
Expand file tree
/
Copy pathRun.hx
More file actions
402 lines (382 loc) · 11.4 KB
/
Copy pathRun.hx
File metadata and controls
402 lines (382 loc) · 11.4 KB
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
import haxe.io.Path;
import shared.Util;
import sys.FileSystem;
import sys.io.File;
import sys.io.Process;
#if !haxe5
#error "Requires Haxe 5"
#end
final goRequiredVersion = File.getContent(".gorc");
final home = Sys.getEnv(if (Sys.systemName() == "Windows") "UserProfile" else "HOME");
final goCommand = executable(home + "/.go/go" + goRequiredVersion + "/bin/go");
final goRoot = home + "/.go/go" + goRequiredVersion;
final goupCommand = executable(home + "/.go/bin/goup");
function main() {
var index = 0;
final args = Sys.args();
final ci = if (args.length > 0 && args[0] == "ci") {
args.shift();
true;
} else {
false;
}
var index = args.indexOf("goup");
if (args.length > 0 && index != -1) {
final command = goupCommand + " " + args.slice(index + 1, args.length - 1).join(" ");
Sys.println(command);
Sys.command(command);
return;
}
index = args.indexOf("go");
if (args.length > 0 && index != -1) {
final command = goCommand + " " + args.slice(index + 1, args.length - 1).join(" ");
Sys.println(command);
Sys.command(command);
return;
}
if (args.length > 0 && args.indexOf("clean") != -1) {
clean();
return;
}
if (args.length > 0 && args.indexOf("build") != -1) {
build(true);
return;
}
var rebuild = false;
var process = new Process('git', ['rev-parse', 'HEAD']);
if (process.exitCode() != 0) {
var message = process.stderr.readAll().toString();
trace("Cannot execute `git rev-arse HEAD` " + message);
return;
}
final version = process.stdout.readLine();
process.close();
if (FileSystem.exists("version.txt")) {
if (!rebuild && version != File.getContent("version.txt")) {
Sys.println("updating version rebuilding binaries");
rebuild = true;
}
} else {
trace("no version");
rebuild = true; // rebuild if no version present for good measure
}
File.saveContent("version.txt", version);
if ((index = args.indexOf("-rebuild")) != -1 || (index = args.indexOf("--rebuild")) != -1) { // used to rebuild the compiler each run
args.remove(args[index]);
Sys.println("rebuilding...");
rebuild = true;
}
build(rebuild);
if (args.length <= 1) {
Sys.command("haxe extra/scripts/build-interp.hxml --help");
return;
}
if (goCommand != "go") {
final last = args.pop();
args.push("-gocmd");
args.push(goCommand);
args.push("-goroot");
args.push(goRoot);
args.push(last);
}
var debug = false;
if ((index = args.indexOf("-compiler_debug")) != -1 || (index = args.indexOf("--compiler_debug")) != -1) {
args.remove(args[index]);
debug = true;
}
if ((index = args.indexOf("-compiler_hl")) != -1 || (index = args.indexOf("--compiler_hl")) != -1) {
args.remove(args[index]);
setupHashlink(rebuild, args);
return;
}
if ((index = args.indexOf("-compiler_cpp")) != -1 || (index = args.indexOf("--compiler_cpp")) != -1) {
args.remove(args[index]);
setupCPP(rebuild, args, debug);
return;
}
if ((index = args.indexOf("-compiler_interp")) != -1 || (index = args.indexOf("--compiler_interp")) != -1) {
args.remove(args[index]);
setupInterp(rebuild, args);
return;
}
if ((index = args.indexOf("-compiler_nodejs")) != -1 || (index = args.indexOf("--compiler_nodejs")) != -1) {
args.remove(args[index]);
setupNodeJS(rebuild, args);
return;
}
if ((index = args.indexOf("-compiler_java")) != -1 || (index = args.indexOf("--compiler_java")) != -1) {
args.remove(args[index]);
setupJava(rebuild, args);
return;
}
setupCPP(rebuild, args, debug);
}
function clean() {
return;
if (FileSystem.exists("golibs"))
deleteDirectoryRecursively("golibs");
for (path in FileSystem.readDirectory("go")) {
if (!FileSystem.isDirectory('go/$path'))
continue;
switch path {
case "_internal":
for (path2 in FileSystem.readDirectory('go/$path')) {
if (FileSystem.isDirectory('go/$path/$path2')) {
switch path2 {
case "unsafe":
continue;
case "internal":
for (path3 in FileSystem.readDirectory('go/$path/$path2')) {
if (FileSystem.isDirectory('go/$path/$path2/$path3')) {
switch path3 {
case "reflect":
default:
deleteDirectoryRecursively('go/$path/$path2/$path3');
}
}
}
continue;
default:
deleteDirectoryRecursively('go/$path/$path2');
}
}
}
continue;
}
deleteDirectoryRecursively('go/$path');
}
}
function deleteDirectoryRecursively(dir:String):Int {
trace(dir);
#if !js
return Sys.command('find $dir -type f -delete');
// return Sys.command('git rm --cache -r -d $dir');
return 0;
#else
return 0;
#end
}
function installRequiredGoVersion() {
final command = goupCommand + " install " + goRequiredVersion;
Sys.println(command);
Sys.println("Please wait a little...");
final proc = new Process(command);
if (proc.exitCode(true) != 0) {
final outMessage = proc.stdout.readAll().toString();
final errMessage = proc.stderr.readAll().toString();
if (errMessage.indexOf('msg="symlink') != -1 || outMessage.indexOf('msg="symlink') != -1)
return;
Sys.println("goup install command failed");
trace(outMessage);
trace(errMessage);
Sys.exit(1);
}
}
private function executable(path:String) {
if (isWindows()) {
return path + '.exe';
}
return path;
}
function installGoUp():Bool {
// prebuilt binaries
var arch = ""; // arch
var os = ""; // os
function getArch(line:String):String {
return switch line {
case "x86_64", "amd64":
"amd64";
case "arm64", "aarch64":
"arm64";
default:
throw "unknown arch: " + line;
}
}
var ext = "";
switch systemName.toLowerCase() {
case "linux":
os = systemName.toLowerCase();
final process = new Process("uname -m");
if (process.exitCode(true) != 0)
return false;
arch = getArch(process.stdout.readLine().toLowerCase());
case "mac":
os = "darwin";
// check if silicon or intel
final process = new Process("uname -m");
if (process.exitCode(true) != 0)
return false;
arch = getArch(process.stdout.readLine().toLowerCase());
case "windows":
os = systemName;
ext = ".exe";
final process = new Process("echo %PROCESSOR_ARCHITECTURE%");
if (process.exitCode(true) != 0)
return false;
arch = getArch(process.stdout.readLine().toLowerCase());
default:
Sys.println("Unknown systemName: " + systemName);
return false;
}
var file = '$os-$arch';
if (isWindows())
file += '.exe';
Sys.println("GoUp installing " + file);
if (!FileSystem.exists('bin'))
FileSystem.createDirectory('bin');
final url = 'https://github.com/owenthereal/goup/releases/download/v0.7.0/$file';
final command = 'curl --silent --show-error --fail --location $url --output bin/$file';
if (Sys.command(command) != 0) {
Sys.println("failed to run curl");
return false;
}
final goBinDir = home + "/.go/bin/";
if (!FileSystem.exists(goBinDir))
FileSystem.createDirectory(goBinDir);
File.copy('bin/$file', goBinDir + executable("goup"));
if (!isWindows())
Sys.command('chmod u+x $goupCommand');
var proc = new Process(goupCommand + " init --skip-prompt");
if (proc.exitCode(true) != 0) {
final outMessage = proc.stdout.readAll().toString();
final errMessage = proc.stderr.readAll().toString();
if (errMessage.indexOf('msg="symlink') != -1 || outMessage.indexOf('msg="symlink') != -1)
return true;
trace(outMessage);
trace(errMessage);
Sys.println("failed to run goup");
return false;
}
return true;
}
function isWindows():Bool {
return Sys.systemName().toLowerCase() == "windows";
}
function build(rebuild:Bool) {
final command = executable(home + "/.go/bin/goup") + " version";
var process = new Process(command);
if (process.exitCode(true) != 0) {
if (!installGoUp()) {
Sys.println("failed to install goup");
Sys.exit(1);
}
}
process = new Process(goCommand + " version");
var code = process.exitCode();
if (code != 0) {
installRequiredGoVersion();
} else {
var foundVersion = process.stdout.readAll().toString();
var index = foundVersion.indexOf("go", 10);
if (index == -1) {
Sys.println("not valid to go version: " + foundVersion);
Sys.exit(1);
}
foundVersion = foundVersion.substr(index + 2, goRequiredVersion.length);
if (foundVersion != goRequiredVersion) {
Sys.println("go version, got: " + foundVersion + " wanted: " + goRequiredVersion);
installRequiredGoVersion();
}
}
process.close();
// run go compiler
if (!FileSystem.exists("go4hx") || rebuild) {
if (!FileSystem.exists("git/tools") || !FileSystem.exists("git/hxparser") || !FileSystem.exists("git/hxparse")) {
// update tools submodule
Sys.println("updating git submodules");
Sys.command("git submodule update --init --recursive");
}
Sys.println("build Go part of the compiler");
final command = goCommand + ' build .';
Sys.println(command);
final code = Sys.command(command);
if (code != 0) {
Sys.println("Failed to build Go compiler");
Sys.exit(code);
}
}
}
function setupNodeJS(rebuild:Bool, args:Array<String>) {
Sys.println("NodeJS compiler version");
// run nodejs
if (!FileSystem.exists("export/build.js") || rebuild) {
Sys.command("haxe extra/scripts/build-js.hxml");
}
args.unshift("export/build.js");
// 4gb = 4096, 2gb = 2048
// args.unshift("--max-old-space-size=4096");
// args.unshift("--expose-gc");
Sys.command("node", args);
}
function setupCPP(rebuild:Bool, args:Array<String>, debug:Bool) {
Sys.println("Building Haxe part of the compiler");
final proc = new Process("haxe -cp extra/scripts --run SetupHxcpp");
if (proc.exitCode(true) != 0) {
trace(proc.stdout.readAll().toString(), proc.stderr.readAll().toString());
}
Sys.putEnv("HXCPP_COMPILE_THREADS", "4");
final debugSuffix = debug ? "-debug" : "";
final fileName = executable('export/cpp/Main$debugSuffix');
if (!FileSystem.exists("export/cpp") || !FileSystem.exists(fileName) || rebuild) {
var cmd = 'haxe extra/scripts/build-cpp$debugSuffix.hxml';
final code = Sys.command(cmd);
if (code != 0) {
Sys.println("BUILD FAILED");
Sys.exit(1);
}
}
if (args.length > 0) {
final command = fileName + " " + args.join(" ");
Sys.println(command);
final code = Sys.command(fileName, args);
if (code != 0) {
Sys.println("COMPILER RUN FAILED: " + command);
Sys.exit(code);
}
}
}
function setupJava(rebuild:Bool, args:Array<String>) {
Sys.println("Jvm compiler version");
// run build jvm
if (!FileSystem.exists("export/build.jvm") || rebuild) {
Sys.command("haxe extra/scripts/build-jvm.hxml");
}
args.unshift("export/build.jar");
args.unshift("-jar");
trace(args.join(" "));
Sys.command("java", args);
}
function setupHashlink(rebuild:Bool, args:Array<String>) {
Sys.println("Hashlink compiler version");
var no_uv = false;
var no_fmt = false;
var index = 0;
if ((index = args.indexOf("-no_uv")) != -1 || (index = args.indexOf("--no_uv")) != -1) {
no_uv = true;
args.remove(args[index]);
}
if ((index = args.indexOf("-no_fmt")) != -1 || (index = args.indexOf("--no_fmt")) != -1) {
no_fmt = true;
args.remove(args[index]);
}
if (!FileSystem.exists("build.hl") || rebuild) {
var cmd = "haxe extra/scripts/build-hl.hxml";
if (no_uv)
cmd += " -D no_uv";
if (no_fmt)
cmd += " -D no_fmt";
Sys.command(cmd);
}
args.unshift("export/build.hl");
final command = "hl" + " " + args.join(" ");
Sys.println(command);
final code = Sys.command(command);
if (code != 0) {
Sys.println("COMPILER RUN FAILED: " + command);
Sys.exit(code);
}
}
function setupInterp(rebuild:Bool, args:Array<String>) {
Sys.println("Interp compiler version");
Sys.command("haxe extra/scripts/build-interp.hxml " + args.join(" "));
}