forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
168 lines (152 loc) · 4.56 KB
/
build.ts
File metadata and controls
168 lines (152 loc) · 4.56 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
import { join } from "node:path"
import { existsSync, watch } from "node:fs"
import {
readFileSync,
writeFileSync,
ensureDirSync,
pathExistsSync,
} from "fs-extra"
const debounce = require("debounce-promise")
import {
compile,
jacdacDefaultSpecifications,
JacsDiagnostic,
DEVS_BYTECODE_FILE,
formatDiagnostics,
LogInfo,
DEVS_DBG_FILE,
prettySize,
} from "@devicescript/compiler"
import { BINDIR, CmdOptions, debug, error, log } from "./command"
import { devtools } from "./devtools"
function jacsFactory() {
let d = require("@devicescript/vm")
try {
require("websocket-polyfill")
// @ts-ignore
global.Blob = require("buffer").Blob
} catch {
log("can't load websocket-polyfill")
}
return d()
}
async function getHost(options: BuildOptions & CmdOptions) {
const inst = options.noVerify ? undefined : await jacsFactory()
inst?.jacsInit()
const outdir = options.outDir
ensureDirSync(outdir)
const jacsHost = {
write: (fn: string, cont: string) => {
const p = join(outdir, fn)
if (options.verbose) debug(`write ${p}`)
writeFileSync(p, cont)
if (
fn.endsWith(".jasm") &&
typeof cont == "string" &&
cont.indexOf("???oops") >= 0
)
throw new Error("bad disassembly")
},
log: (msg: string) => {
if (options.verbose) log(msg)
},
error: (err: JacsDiagnostic) => {
error(formatDiagnostics([err]))
},
mainFileName: () => options.mainFileName || "",
getSpecs: () => jacdacDefaultSpecifications,
verifyBytecode: (buf: Uint8Array) => {
if (!inst) return
const res = inst.jacsDeploy(buf)
if (res != 0) throw new Error("verification error: " + res)
},
}
return jacsHost
}
export class CompilationError extends Error {
constructor(message: string) {
super(message)
this.name = "CompilationError"
}
}
async function compileBuf(buf: Buffer, options: BuildOptions) {
const host = await getHost(options)
const res = compile(buf.toString("utf8"), {
host,
isLibrary: options.library,
})
return res
}
export interface BuildOptions {
noVerify?: boolean
library?: boolean
outDir?: string
watch?: boolean
stats?: boolean
// internal option
mainFileName?: string
}
export async function build(file: string, options: BuildOptions & CmdOptions) {
file = file || "main.ts"
options = options || {}
options.outDir = options.outDir || BINDIR
options.mainFileName = file
if (!existsSync(file)) {
// otherwise we throw
error(`${file} does not exist`)
return
}
log(`building ${file}`)
ensureDirSync(options.outDir)
await buildOnce(file, options)
if (options.watch) await buildWatch(file, options)
}
async function buildWatch(file: string, options: BuildOptions) {
const bytecodeFile = join(options.outDir, DEVS_BYTECODE_FILE)
const debugFile = join(options.outDir, DEVS_DBG_FILE)
// start watch source file
log(`watching ${file}...`)
const work = debounce(
async () => {
debug(`change detected...`)
await buildOnce(file, options)
},
500,
{ leading: true }
)
watch(file, work)
// start watching bytecode file
await devtools({ ...options, bytecodeFile, debugFile })
}
async function buildOnce(file: string, options: BuildOptions & CmdOptions) {
const { watch, stats } = options
if (!pathExistsSync(file)) throw new Error(`source file ${file} not found`)
const buf = readFileSync(file)
const { success, binary, dbg, clientSpecs } = await compileBuf(buf, {
...options,
mainFileName: file,
})
if (!success) {
if (watch) return
throw new CompilationError("compilation failed")
}
log(`binary: ${prettySize(binary.length)}`)
if (stats) {
const { sizes, functions } = dbg
log(
" " +
Object.keys(sizes)
.map(name => `${name}: ${prettySize(sizes[name])}`)
.join(", ")
)
log(` functions:`)
functions
.sort((l, r) => l.size - r.size)
.forEach(fn => {
log(` ${fn.name} (${prettySize(fn.size)})`)
fn.users.forEach(user =>
debug(` <-- ${user.file}: ${user.line}, ${user.col}`)
)
})
}
}