forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildengine.ts
More file actions
617 lines (547 loc) · 20.5 KB
/
buildengine.ts
File metadata and controls
617 lines (547 loc) · 20.5 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
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
/// <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 * as hid from './hid';
import U = pxt.Util;
import Map = pxt.Map;
// abstract over build engine
export interface BuildEngine {
updateEngineAsync: () => Promise<void>;
setPlatformAsync: () => Promise<void>;
buildAsync: () => Promise<void>;
patchHexInfo: (extInfo: pxtc.ExtensionInfo) => pxtc.HexInfo;
prepBuildDirAsync: () => Promise<void>;
buildPath: string;
appPath: string;
moduleConfig: 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: {
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: {
updateEngineAsync: () => runDockerAsync(["yotta", "update"]),
buildAsync: () => runDockerAsync(["yotta", "build"]),
setPlatformAsync: () =>
runDockerAsync(["yotta", "target", pxt.appTarget.compileService.yottaTarget]),
patchHexInfo: patchYottaHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/dockeryt",
moduleConfig: "module.json",
deployAsync: msdDeployCoreAsync,
appPath: "source"
},
platformio: {
updateEngineAsync: noopAsync,
buildAsync: () => runPlatformioAsync(["run"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchPioHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/pio",
moduleConfig: "platformio.ini",
deployAsync: platformioDeployAsync,
appPath: "src"
},
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: {
updateEngineAsync: updateCodalBuildAsync,
buildAsync: () => runDockerAsync(["python", "build.py"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchCodalHexInfo,
prepBuildDirAsync: prepCodalBuildDirAsync,
buildPath: "built/dockercodal",
moduleConfig: "codal.json",
deployAsync: msdDeployCoreAsync,
appPath: "pxtapp"
},
dockermake: {
updateEngineAsync: () => runBuildCmdAsync(nodeutil.addCmd("npm"), "install"),
buildAsync: () => runDockerAsync(["make"]),
setPlatformAsync: noopAsync,
patchHexInfo: patchDockermakeHexInfo,
prepBuildDirAsync: noopAsync,
buildPath: "built/dockermake",
moduleConfig: "package.json",
deployAsync: msdDeployCoreAsync,
appPath: "pxtapp"
},
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"];
}
thisBuild = b;
}
function patchYottaHexInfo(extInfo: pxtc.ExtensionInfo) {
let buildEngine = thisBuild
let hexPath = buildEngine.buildPath + "/build/" + pxt.appTarget.compileService.yottaTarget
+ "/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 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, 8, 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[]) {
let fullpath = process.cwd() + "/" + thisBuild.buildPath + "/"
let cs = pxt.appTarget.compileService
return nodeutil.spawnAsync({
cmd: "docker",
args: ["run", "--rm", "-v", fullpath + ":/src", "-w", "/src", "-u", "build",
cs.dockerImage].concat(args),
cwd: thisBuild.buildPath
})
}
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 && config.dalDTS && 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, 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, 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 = Promise.promisify(fs.writeFile)
const execAsync: (cmd: string, options?: { cwd?: string }) => Promise<Buffer | string> = Promise.promisify(child_process.exec)
const readDirAsync = Promise.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) {
const firmware = pxt.outputName()
const encoding = pxt.isOutputText() ? "utf8" : "base64";
if (pxt.appTarget.serial && pxt.appTarget.serial.useHF2 && !pxt.appTarget.serial.noDeploy
&& hid.isInstalled(true)) {
let f = res.outfiles[pxtc.BINARY_UF2]
let blocks = pxtc.UF2.parseFile(U.stringToUint8Array(atob(f)))
return hid.initAsync()
.then(dev => dev.flashAsync(blocks))
}
return getBoardDrivesAsync()
.then(drives => filterDrives(drives))
.then(drives => {
if (drives.length == 0) {
pxt.log("cannot find any drives to deploy to");
return Promise.resolve(0);
}
pxt.log(`copying ${firmware} to ` + drives.join(", "));
const writeHexFile = (filename: string) => {
return writeFileAsync(path.join(filename, firmware), res.outfiles[firmware], encoding)
.then(() => pxt.log(" wrote hex file to " + filename));
};
return Promise.map(drives, d => writeHexFile(d))
.then(() => drives.length);
}).then(() => { });
}
function getBoardDrivesAsync(): Promise<string[]> {
if (process.platform == "win32") {
const rx = new RegExp("^([A-Z]:)\\s+(\\d+).* " + pxt.appTarget.compile.deployDrives)
return execAsync("wmic PATH Win32_LogicalDisk get DeviceID, VolumeName, FileSystem, DriveType")
.then((buf: Buffer) => {
let res: string[] = []
buf.toString("utf8").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"]
return readDirAsync(`/media/${user}`)
.then(lst => lst.filter(s => rx.test(s)).map(s => `/media/${user}/${s}/`))
} 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;
}
});
}