-
Notifications
You must be signed in to change notification settings - Fork 596
/
Copy pathbuildengine.ts
874 lines (765 loc) · 28.2 KB
/
buildengine.ts
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/// <reference path="../built/pxtlib.d.ts"/>
/// <reference path="../built/pxtsim.d.ts"/>
(global as any).pxt = pxt;
import * as nodeutil from './nodeutil';
import * as fs from 'fs';
import * as path from 'path';
import * as child_process from 'child_process';
import { promisify } from "util";
import * as hid from './hid';
import U = pxt.Util;
import Map = pxt.Map;
// abstract over build engine
export interface BuildEngine {
id: string;
updateEngineAsync: () => Promise<void>;
setPlatformAsync: () => Promise<void>;
buildAsync: () => Promise<void>;
patchHexInfo: (extInfo: pxtc.ExtensionInfo) => pxtc.HexInfo;
prepBuildDirAsync: () => Promise<void>;
buildPath: string;
appPath: string;
moduleConfig: string;
outputPath?: string;
deployAsync?: (r: pxtc.CompileResult) => Promise<void>;
}
// abstract over C++ runtime target (currently the DAL)
export interface TargetRuntime {
includePath: string;
}
interface BuildCache {
sha?: string;
modSha?: string;
}
function noopAsync() { return Promise.resolve() }
export const buildEngines: Map<BuildEngine> = {
yotta: {
id: "yotta",
updateEngineAsync: () => runYottaAsync(["update"]),
buildAsync: () => runYottaAsync(["build"]),
setPlatformAsync: () =>
runYottaAsync(["target", pxt.appTarget.compileService.yottaTarget]),
patchHexInfo: patchYottaHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/yt",
moduleConfig: "module.json",
deployAsync: msdDeployCoreAsync,
appPath: "source"
},
dockeryotta: {
id: "dockeryotta",
updateEngineAsync: () => runDockerYottaAsync(["yotta", "update"]),
buildAsync: () => runDockerYottaAsync(["yotta", "build"]),
setPlatformAsync: () =>
runDockerYottaAsync(["yotta", "target", pxt.appTarget.compileService.yottaTarget]),
patchHexInfo: patchYottaHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/dockeryt",
moduleConfig: "module.json",
deployAsync: msdDeployCoreAsync,
appPath: "source"
},
platformio: {
id: "platformio",
updateEngineAsync: noopAsync,
buildAsync: () => runPlatformioAsync(["run"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchPioHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/pio",
moduleConfig: "platformio.ini",
deployAsync: platformioDeployAsync,
appPath: "src"
},
codal: {
id: "codal",
updateEngineAsync: updateCodalBuildAsync,
buildAsync: () => runBuildCmdAsync("python", "build.py"),
setPlatformAsync: noopAsync,
patchHexInfo: patchCodalHexInfo,
prepBuildDirAsync: prepCodalBuildDirAsync,
buildPath: "built/codal",
moduleConfig: "codal.json",
deployAsync: msdDeployCoreAsync,
appPath: "pxtapp"
},
dockercodal: {
id: "dockercodal",
updateEngineAsync: updateCodalBuildAsync,
buildAsync: () => runDockerAsync(["python", "build.py"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchCodalHexInfo,
prepBuildDirAsync: prepCodalBuildDirAsync,
buildPath: "built/dockercodal",
moduleConfig: "codal.json",
deployAsync: msdDeployCoreAsync,
appPath: "pxtapp"
},
dockermake: {
id: "dockermake",
updateEngineAsync: () => runBuildCmdAsync(nodeutil.addCmd("npm"), "install"),
buildAsync: () => runDockerAsync(["make", "-j8"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchDockermakeHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/dockermake",
moduleConfig: "package.json",
deployAsync: msdDeployCoreAsync,
outputPath: "bld/pxt-app.elf",
appPath: "pxtapp"
},
dockercross: {
id: "dockercross",
updateEngineAsync: () => runBuildCmdAsync(nodeutil.addCmd("npm"), "install"),
buildAsync: () => runDockerAsync(["make"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchDockerCrossHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/dockercross",
moduleConfig: "package.json",
deployAsync: noopAsync,
appPath: "pxtapp"
},
dockerespidf: {
id: "dockerespidf",
updateEngineAsync: noopAsync,
buildAsync: () => runDockerAsync(["make"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchDockerEspIdfHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/dockerespidf",
moduleConfig: "sdkconfig.defaults",
deployAsync: noopAsync,
appPath: "main"
},
cs: {
id: "cs",
updateEngineAsync: noopAsync,
buildAsync: () => runBuildCmdAsync(getCSharpCommand(), "-t:library", "-out:pxtapp.dll", "lib.cs"),
setPlatformAsync: noopAsync,
patchHexInfo: patchCSharpDll,
prepBuildDirAsync: noopAsync,
buildPath: "built/cs",
moduleConfig: "module.json",
deployAsync: buildFinalCsAsync,
appPath: "pxtapp"
},
}
// once we have a different build engine, set this appropriately
export let thisBuild = buildEngines['yotta']
export function setThisBuild(b: BuildEngine) {
if (pxt.appTarget.compileService.dockerImage && !process.env["PXT_NODOCKER"]) {
if (b === buildEngines["codal"])
b = buildEngines["dockercodal"];
if (b === buildEngines["yotta"])
b = buildEngines["dockeryotta"];
}
pxt.debug(`set build engine: ${b.id}`)
thisBuild = b;
}
function patchYottaHexInfo(extInfo: pxtc.ExtensionInfo) {
let buildEngine = thisBuild
let hexPath = buildEngine.buildPath + "/build/" + pxt.appTarget.compileService.yottaTarget.split("@")[0]
+ "/source/" + pxt.appTarget.compileService.yottaBinary;
return {
hex: fs.readFileSync(hexPath, "utf8").split(/\r?\n/)
}
}
function patchCodalHexInfo(extInfo: pxtc.ExtensionInfo) {
let bin = pxt.appTarget.compileService.codalBinary
let hexPath = thisBuild.buildPath + "/build/" + bin + ".hex"
return {
hex: fs.readFileSync(hexPath, "utf8").split(/\r?\n/)
}
}
function patchDockermakeHexInfo(extInfo: pxtc.ExtensionInfo) {
let hexPath = thisBuild.buildPath + "/bld/pxt-app.hex"
return {
hex: fs.readFileSync(hexPath, "utf8").split(/\r?\n/)
}
}
function patchDockerCrossHexInfo(extInfo: pxtc.ExtensionInfo) {
let hexPath = thisBuild.buildPath + "/bld/all.tgz.b64"
return {
hex: fs.readFileSync(hexPath, "utf8").split(/\r?\n/)
}
}
function patchDockerEspIdfHexInfo(extInfo: pxtc.ExtensionInfo) {
let hexPath = thisBuild.buildPath + "/build/pxtapp.b64"
return {
hex: fs.readFileSync(hexPath, "utf8").split(/\r?\n/)
}
}
function patchCSharpDll(extInfo: pxtc.ExtensionInfo) {
let hexPath = thisBuild.buildPath + "/lib.cs"
return {
hex: [fs.readFileSync(hexPath, "utf8")]
}
}
function pioFirmwareHex() {
let buildEngine = buildEngines['platformio']
return buildEngine.buildPath + "/.pioenvs/myenv/firmware.hex"
}
function patchPioHexInfo(extInfo: pxtc.ExtensionInfo) {
return {
hex: fs.readFileSync(pioFirmwareHex(), "utf8").split(/\r?\n/)
}
}
function platformioDeployAsync(r: pxtc.CompileResult) {
if (pxt.appTarget.compile.useUF2) return msdDeployCoreAsync(r);
else return platformioUploadAsync(r);
}
function platformioUploadAsync(r: pxtc.CompileResult) {
// TODO maybe platformio has some option to do this?
let buildEngine = buildEngines['platformio']
let prevHex = fs.readFileSync(pioFirmwareHex())
fs.writeFileSync(pioFirmwareHex(), r.outfiles[pxtc.BINARY_HEX])
return runPlatformioAsync(["run", "--target", "upload", "--target", "nobuild", "-v"])
.finally(() => {
pxt.log('Restoring ' + pioFirmwareHex())
fs.writeFileSync(pioFirmwareHex(), prevHex)
})
}
export function buildHexAsync(buildEngine: BuildEngine, mainPkg: pxt.MainPackage, extInfo: pxtc.ExtensionInfo, forceBuild: boolean) {
let tasks = Promise.resolve()
let buildCachePath = buildEngine.buildPath + "/buildcache.json"
let buildCache: BuildCache = {}
if (fs.existsSync(buildCachePath)) {
buildCache = nodeutil.readJson(buildCachePath)
}
if (!forceBuild && (buildCache.sha == extInfo.sha && !process.env["PXT_RUNTIME_DEV"])) {
pxt.debug("Skipping C++ build.")
return tasks
}
pxt.debug("writing build files to " + buildEngine.buildPath)
let allFiles = U.clone(extInfo.generatedFiles)
U.jsonCopyFrom(allFiles, extInfo.extensionFiles)
let writeFiles = () => {
for (let f of nodeutil.allFiles(buildEngine.buildPath + "/" + buildEngine.appPath, { maxDepth: 8, allowMissing: true })) {
let bn = f.slice(buildEngine.buildPath.length)
bn = bn.replace(/\\/g, "/").replace(/^\//, "/")
if (U.startsWith(bn, "/" + buildEngine.appPath + "/") && !allFiles[bn]) {
pxt.log("removing stale " + bn)
fs.unlinkSync(f)
}
}
U.iterMap(allFiles, (fn, v) => {
fn = buildEngine.buildPath + fn
nodeutil.mkdirP(path.dirname(fn))
let existing: string = null
if (fs.existsSync(fn))
existing = fs.readFileSync(fn, "utf8")
if (existing !== v)
nodeutil.writeFileSync(fn, v)
})
}
tasks = tasks
.then(buildEngine.prepBuildDirAsync)
.then(writeFiles)
let saveCache = () => fs.writeFileSync(buildCachePath, JSON.stringify(buildCache, null, 4) + "\n")
let modSha = U.sha256(extInfo.generatedFiles["/" + buildEngine.moduleConfig])
let needDal = false
if (buildCache.modSha !== modSha || forceBuild) {
tasks = tasks
.then(buildEngine.setPlatformAsync)
.then(buildEngine.updateEngineAsync)
.then(() => {
buildCache.sha = ""
buildCache.modSha = modSha
saveCache();
needDal = true
})
} else {
pxt.debug(`Skipping C++ build update.`)
}
tasks = tasks
.then(buildEngine.buildAsync)
.then(() => {
buildCache.sha = extInfo.sha
saveCache()
if (needDal)
buildDalConst(buildEngine, mainPkg, true);
})
return tasks
}
function runYottaAsync(args: string[]) {
let ypath: string = process.env["YOTTA_PATH"]
let ytCommand = "yotta"
let env = U.clone(process.env)
if (/;[A-Z]:\\/.test(ypath)) {
for (let pp of ypath.split(";")) {
let q = path.join(pp, "yotta.exe")
if (fs.existsSync(q)) {
ytCommand = q
env["PATH"] = env["PATH"] + ";" + ypath
break
}
}
}
pxt.log("*** " + ytCommand + " " + args.join(" "))
let child = child_process.spawn("yotta", args, {
cwd: thisBuild.buildPath,
stdio: "inherit",
env: env
})
return new Promise<void>((resolve, reject) => {
child.on("close", (code: number) => {
if (code === 0) resolve()
else reject(new Error("yotta " + args.join(" ") + ": exit code " + code))
})
})
}
function runPlatformioAsync(args: string[]) {
pxt.log("*** platformio " + args.join(" "))
let child = child_process.spawn("platformio", args, {
cwd: thisBuild.buildPath,
stdio: "inherit",
env: process.env
})
return new Promise<void>((resolve, reject) => {
child.on("close", (code: number) => {
if (code === 0) resolve()
else reject(new Error("platformio " + args.join(" ") + ": exit code " + code))
})
})
}
function runDockerAsync(args: string[], flags?: string[]) {
if (process.env["PXT_NODOCKER"] == "force") {
const cmd = args.shift()
return nodeutil.spawnAsync({
cmd,
args,
cwd: thisBuild.buildPath
})
} else {
let fullpath = process.cwd() + "/" + thisBuild.buildPath + "/"
let cs = pxt.appTarget.compileService
let dargs = cs.dockerArgs || ["-u", "build"]
let mountArg = fullpath + ":/src"
// this speeds up docker build a lot on macOS,
// see https://docs.docker.com/docker-for-mac/osxfs-caching/
if (process.platform == "darwin")
mountArg += ":delegated"
let fullArgs = ["--rm", "-v", mountArg, "-w", "/src", ...dargs, cs.dockerImage, ...args];
if (flags) {
fullArgs = [...flags, ...fullArgs];
}
return nodeutil.spawnAsync({
cmd: "docker",
args: ["run", ...fullArgs],
cwd: thisBuild.buildPath
})
}
}
function runDockerYottaAsync(args: string[]) {
let fullpath = process.cwd() + "/" + thisBuild.buildPath + "/"
fs.copyFileSync(path.join(__dirname, "prepYotta.js"), path.join(fullpath, "prepYotta.js"));
let argVariable = args.join(" ");
return runDockerAsync(["/bin/bash", "-c", `node prepYotta.js; ${argVariable}`], ["--env", "GITHUB_ACCESS_TOKEN"])
}
let parseCppInt = pxt.cpp.parseCppInt;
export function codalGitAsync(...args: string[]) {
return nodeutil.spawnAsync({
cmd: "git",
args: args,
cwd: thisBuild.buildPath
})
}
function prepCodalBuildDirAsync() {
if (fs.existsSync(thisBuild.buildPath + "/.git/config"))
return Promise.resolve()
let cs = pxt.appTarget.compileService
let pkg = "https://github.com/" + cs.githubCorePackage
nodeutil.mkdirP("built")
return nodeutil.runGitAsync("clone", pkg, thisBuild.buildPath)
.then(() => codalGitAsync("checkout", cs.gittag))
}
function runBuildCmdAsync(cmd: string, ...args: string[]) {
return nodeutil.spawnAsync({
cmd,
args,
cwd: thisBuild.buildPath,
})
}
function updateCodalBuildAsync() {
let cs = pxt.appTarget.compileService
return codalGitAsync("checkout", cs.gittag)
.then(
() => /v\d+/.test(cs.gittag) ? Promise.resolve() : codalGitAsync("pull"),
e =>
codalGitAsync("checkout", "master")
.then(() => codalGitAsync("pull")))
.then(() => codalGitAsync("checkout", cs.gittag))
}
// TODO: DAL specific code should be lifted out
export function buildDalConst(buildEngine: BuildEngine, mainPkg: pxt.MainPackage, rebuild = false,
create = false) {
const constName = "dal.d.ts";
let constPath = constName;
const config = mainPkg && mainPkg.config;
const corePackage = config?.dalDTS?.corePackage;
if (corePackage)
constPath = path.join(corePackage, constName);
let vals: Map<string> = {}
let done: Map<string> = {}
let excludeSyms: string[] = []
function expandInt(s: string): number {
s = s.trim()
let existing = U.lookup(vals, s)
if (existing != null && existing != "?")
s = existing
let mm = /^\((.*)\)/.exec(s)
if (mm) s = mm[1]
let m = /^(\w+)\s*([\+\|])\s*(.*)$/.exec(s)
if (m) {
let k = expandInt(m[1])
if (k != null)
return m[2] == "+" ? k + expandInt(m[3]) : k | expandInt(m[3])
}
let pp = parseCppInt(s)
if (pp != null) return pp
return null
}
function extractConstants(fileName: string, src: string, dogenerate = false): string {
let lineNo = 0
// let err = (s: string) => U.userError(`${fileName}(${lineNo}): ${s}\n`)
let outp = ""
let inEnum = false
let enumVal = 0
let defineVal = (n: string, v: string) => {
if (excludeSyms.some(s => U.startsWith(n, s)))
return
let parsed = expandInt(v)
if (parsed != null) {
v = parsed.toString()
let curr = U.lookup(vals, n)
if (curr == null || curr == v) {
vals[n] = v
if (dogenerate && !done[n]) {
outp += ` ${n} = ${v},\n`
done[n] = v
}
} else {
vals[n] = "?"
// TODO: DAL-specific code
if (dogenerate && !/^MICROBIT_DISPLAY_(ROW|COLUMN)_COUNT|PXT_VTABLE_SHIFT$/.test(n))
pxt.log(`${fileName}(${lineNo}): #define conflict, ${n}`)
}
}
}
src.split(/\r?\n/).forEach(ln => {
++lineNo
ln = ln.replace(/\/\/.*/, "").replace(/\/\*.*/g, "")
let m = /^\s*#define\s+(\w+)\s+(.*)$/.exec(ln)
if (m) {
defineVal(m[1], m[2])
}
if (inEnum && /}/.test(ln))
inEnum = false
if (/^\s*enum\s+(\w+)/.test(ln)) {
inEnum = true;
enumVal = -1;
}
const shouldExpand = inEnum && (m = /^\s*(\w+)\s*(=\s*(.*?))?,?\s*$/.exec(ln));
if (shouldExpand) {
let v = m[3]
if (v) {
enumVal = expandInt(v)
if (enumVal == null) {
pxt.log(`${fileName}(${lineNo}): invalid enum initializer, ${ln}`)
inEnum = false
return
}
} else {
enumVal++
v = enumVal + ""
}
defineVal(m[1], v)
}
})
return outp
}
if (mainPkg && (create ||
(mainPkg.getFiles().indexOf(constName) >= 0 && (rebuild || !fs.existsSync(constName))))) {
pxt.log(`rebuilding ${constName} into ${constPath}...`)
let files: string[] = []
let foundConfig = false
for (let d of mainPkg.sortedDeps()) {
if (d.config.dalDTS) {
if (d.config.dalDTS.includeDirs)
for (let dn of d.config.dalDTS.includeDirs) {
dn = buildEngine.buildPath + "/" + dn
if (U.endsWith(dn, ".h")) files.push(dn)
else {
let here = nodeutil.allFiles(dn, { maxDepth: 20 }).filter(fn => U.endsWith(fn, ".h"))
U.pushRange(files, here)
}
}
excludeSyms = d.config.dalDTS.excludePrefix || excludeSyms
foundConfig = true
}
}
if (!foundConfig) {
let incPath = buildEngine.buildPath + "/yotta_modules/microbit-dal/inc/"
if (!fs.existsSync(incPath))
incPath = buildEngine.buildPath + "/yotta_modules/codal/inc/";
if (!fs.existsSync(incPath))
incPath = buildEngine.buildPath
if (!fs.existsSync(incPath))
U.userError("cannot find " + incPath);
files = nodeutil.allFiles(incPath, { maxDepth: 20 })
.filter(fn => U.endsWith(fn, ".h"))
.filter(fn => fn.indexOf("/mbed-classic/") < 0)
.filter(fn => fn.indexOf("/mbed-os/") < 0)
}
files.sort(U.strcmp)
let fc: Map<string> = {}
for (let fn of files) {
if (U.endsWith(fn, "Config.h")) continue
fc[fn] = fs.readFileSync(fn, "utf8")
}
files = Object.keys(fc)
// pre-pass - detect conflicts
for (let fn of files) {
extractConstants(fn, fc[fn])
}
// stabilize
for (let fn of files) {
extractConstants(fn, fc[fn])
}
let consts = "// Auto-generated. Do not edit.\ndeclare const enum DAL {\n"
for (let fn of files) {
let v = extractConstants(fn, fc[fn], true)
if (v) {
consts += " // " + fn.replace(/\\/g, "/").replace(buildEngine.buildPath, "") + "\n"
consts += v
}
}
consts += "}\n"
fs.writeFileSync(constPath, consts)
}
}
const writeFileAsync: any = promisify(fs.writeFile)
const cpExecAsync = promisify(child_process.exec);
const readDirAsync = promisify(fs.readdir)
function buildFinalCsAsync(res: ts.pxtc.CompileResult) {
return nodeutil.spawnAsync({
cmd: getCSharpCommand(),
args: ["-out:pxtapp.exe", "binary.cs"],
cwd: "built",
})
}
function getCSharpCommand() {
return process.platform == "win32" ? "mcs.bat" : "mcs";
}
function msdDeployCoreAsync(res: ts.pxtc.CompileResult): Promise<void> {
const firmwareName = [pxtc.BINARY_UF2, pxtc.BINARY_HEX, pxtc.BINARY_ELF].filter(f => !!res.outfiles[f])[0];
if (!firmwareName) { // something went wrong heres
pxt.reportError("compile", `firmware missing from built files (${Object.keys(res.outfiles).join(', ')})`)
return Promise.resolve();
}
const firmware = res.outfiles[firmwareName];
const encoding = firmwareName == pxtc.BINARY_HEX
? "utf8" : "base64";
function copyDeployAsync() {
return getBoardDrivesAsync()
.then(drives => filterDrives(drives))
.then(drives => {
if (drives.length == 0)
throw new Error("cannot find any drives to deploy to");
pxt.log(`copying ${firmwareName} to ` + drives.join(", "));
const writeHexFile = (drivename: string) => {
return writeFileAsync(path.join(drivename, firmwareName), firmware, encoding)
.then(() => pxt.debug(" wrote to " + drivename))
.catch((e: Error) => {
throw new Error(`failed writing to ${drivename}; ${e.message}`);
})
};
return U.promiseMapAll(drives, d => writeHexFile(d))
.then(() => drives.length);
}).then(() => { });
}
function hidDeployAsync() {
const f = firmware
const blocks = pxtc.UF2.parseFile(U.stringToUint8Array(atob(f)))
return hid.initAsync()
.then(dev => dev.flashAsync(blocks))
}
let p = Promise.resolve();
if (pxt.appTarget.compile
&& pxt.appTarget.compile.useUF2
&& !pxt.appTarget.serial.noDeploy
&& hid.isInstalled(true)) {
// try hid or simply bail out
p = p.then(() => hidDeployAsync())
.catch(e => copyDeployAsync());
} else {
p = p.then(() => copyDeployAsync())
}
return p;
}
function getBoardDrivesAsync(): Promise<string[]> {
if (process.platform == "win32") {
const rx = new RegExp("^([A-Z]:)\\s+(\\d+).* " + pxt.appTarget.compile.deployDrives)
return cpExecAsync("wmic PATH Win32_LogicalDisk get DeviceID, VolumeName, FileSystem, DriveType")
.then(({ stdout, stderr }) => {
let res: string[] = [];
stdout
.split(/\n/)
.forEach(ln => {
let m = rx.exec(ln);
if (m && m[2] == "2") {
res.push(m[1] + "/");
}
}
);
return res;
});
}
else if (process.platform == "darwin") {
const rx = new RegExp(pxt.appTarget.compile.deployDrives)
return readDirAsync("/Volumes")
.then(lst => lst.filter(s => rx.test(s)).map(s => "/Volumes/" + s + "/"))
} else if (process.platform == "linux") {
const rx = new RegExp(pxt.appTarget.compile.deployDrives)
const user = process.env["USER"]
if (nodeutil.existsDirSync(`/media/${user}`))
return readDirAsync(`/media/${user}`)
.then(lst => lst.filter(s => rx.test(s)).map(s => `/media/${user}/${s}/`))
return Promise.resolve([]);
} else {
return Promise.resolve([])
}
}
function filterDrives(drives: string[]): string[] {
const marker = pxt.appTarget.compile.deployFileMarker;
if (!marker) return drives;
return drives.filter(d => {
try {
return fs.existsSync(path.join(d, marker));
} catch (e) {
return false;
}
});
}
interface CompileExtReq {
config: string;
tag: string;
replaceFiles: pxt.Map<string>;
dependencies?: pxt.Map<string>;
}
interface CompileServiceConfig {
id: string;
repourl?: string;
binary?: string;
target?: string;
board?: string;
image?: string; // docker image
hexfile?: string;
clone?: string;
buildcmd?: string;
}
interface FileEntry {
name: string;
text: string;
}
interface CompileServiceResult {
stdout: string;
stderr: string;
status: number | null;
hexfile?: string
}
export async function compileWithLocalCompileService(extinfo: pxtc.ExtensionInfo): Promise<pxtc.HexInfo> {
const resp = await runDockerCompileAsync(extinfo.compileData);
if (resp.hexfile) {
pxt.log("Compile successful");
}
else {
pxt.log("Compile failed");
pxt.log(resp.stderr)
pxt.log(resp.stdout)
}
return resp.hexfile && {
hex: resp.hexfile.split(/\r?\n/)
};
}
async function runDockerCompileAsync(data: string) {
const compileReq: CompileExtReq = JSON.parse(Buffer.from(data, "base64").toString("utf8"));
const deploymentConfig = JSON.parse(fs.readFileSync(path.resolve("../../../pxt-deployment-config/production/config.json"), "utf8"));
const compileServiceConfig = (deploymentConfig.compileServices as CompileServiceConfig[]).find(cs => cs.id === compileReq.config);
const isPlatformio = !!compileServiceConfig.board
if (!(isPlatformio || compileServiceConfig.hexfile)) {
const tag = compileReq.tag || ""
if (compileServiceConfig.repourl) {
if (/^[\w.\-]+$/.test(tag)) {
compileServiceConfig.repourl = compileServiceConfig.repourl.replace(/#.*/g, "") + "#" + compileReq.tag;
}
}
const moduleName = compileServiceConfig.binary.replace(/-combined/, "").replace(/\.hex$/, "");
const modulejson = {
"name": moduleName,
"version": "0.0.0",
"description": "Auto-generated. Do not edit.",
"license": "n/a",
"dependencies": compileReq.dependencies || {},
"targetDependencies": {},
"bin": "./source"
};
if (compileServiceConfig.repourl) {
let repoSlug = compileServiceConfig.repourl.replace(/^https?:\/\/[^\/]+\//, "").replace(/\.git#/, "#")
let pkgName = repoSlug.replace(/#.*/, "").replace(/^.*\//, "")
modulejson.dependencies[pkgName] = repoSlug
}
compileReq.replaceFiles["/module.json"] = JSON.stringify(modulejson, null, 2) + "\n"
}
const mappedFiles: FileEntry[] = (
Object.keys(compileReq.replaceFiles).map(
filename => {
return {
name: filename.replace(/^\/+/, ""),
text: compileReq.replaceFiles[filename]
};
}
)
);
let image = compileServiceConfig.image
if (!image) {
if (isPlatformio) {
image = "pext/platformio:latest";
}
else {
image = "mcr.microsoft.com/makecode/yotta:main-gcc5";
}
}
let hexFile = compileServiceConfig.hexfile;
if (!hexFile && !isPlatformio) {
hexFile = "source/" + compileServiceConfig.binary;
}
let gittag = "";
if (compileServiceConfig.clone) {
gittag = compileReq.tag;
}
else if (compileServiceConfig.repourl) {
gittag = compileServiceConfig.repourl.replace(/.*#/, "");
}
const compileRequest = {
op: "buildex",
files: mappedFiles,
gittag: gittag,
empty: true,
hexfile: hexFile,
target: compileServiceConfig.target,
platformio: isPlatformio,
clone: compileServiceConfig.clone,
buildcmd: compileServiceConfig.buildcmd,
image: image,
githubToken: process.env["GITHUB_ACCESS_TOKEN"]
};
const stdout = await nodeutil.spawnWithPipeAsync({
cmd: "docker",
args: ["run", "-i", "--env", "LOCAL_BUILD='TRUE'", pxt.appTarget.compileService.dockerImage],
input: JSON.stringify(compileRequest)
})
const resp = JSON.parse(stdout.toString("utf8")) as CompileServiceResult;
return resp;
}