forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctool.ts
More file actions
202 lines (184 loc) · 6.27 KB
/
ctool.ts
File metadata and controls
202 lines (184 loc) · 6.27 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
import { readdirSync, writeFileSync } from "node:fs"
import { getHost, validateBoard } from "./build"
import { log } from "./command"
import * as path from "node:path"
import {
testCompiler,
RepoInfo,
resolveBuildConfig,
compileWithHost,
serverInfo,
} from "@devicescript/compiler"
import { runTest } from "./run"
import { writeFile } from "node:fs/promises"
import { strcmp } from "jacdac-ts"
import { readJSON5Sync } from "./jsonc"
export interface CToolOptions {
empty?: boolean
test?: boolean
fetchBoards?: string
localBoards?: string
serverInfo?: boolean
}
function readdir(folder: string) {
return readdirSync(folder).map(bn => path.join(folder, bn))
}
const ctest = "devs/compiler-tests"
const samples = "devs/samples"
const rtest = "devs/run-tests"
export async function ctool(options: CToolOptions) {
if (options.fetchBoards) {
const ginfo = await fetchBoards(options)
await writeFile(options.fetchBoards, JSON.stringify(ginfo, null, 2))
process.exit(0)
}
if (options.empty) {
const host = await getHost(
resolveBuildConfig({
hwInfo: {
// This doesn't work anyway, since settings are not fetched from the empty program
// "@name": "(empty)",
// "@version": `v${BinFmt.IMG_VERSION_MAJOR}.${BinFmt.IMG_VERSION_MINOR}.${BinFmt.IMG_VERSION_PATCH}`,
},
}),
{ verify: false },
"."
)
host.read = fn => {
if (fn.includes("package.json")) throw new Error("read empty")
return ""
}
const res = compileWithHost("src/main.ts", host)
const buf = res.binary
let r = `__attribute__((aligned(sizeof(void *)))) static const uint8_t devs_empty_program[${buf.length}] = {`
const repl: Record<string, string> = {
8: "0x00",
9: "0x00",
10: "MINR",
11: "MAJR",
}
for (let i = 0; i < buf.length; ++i) {
if ((i & 15) == 0) r += "\n"
let d = "0x" + ("0" + buf[i].toString(16)).slice(-2)
if (repl[i]) d = repl[i]
r += d + ", "
}
r += "\n};"
console.log(r)
process.exit(0)
}
if (options.serverInfo) {
const host = await getHost(resolveBuildConfig(), { verify: false }, ".")
const read = host.read
host.read = fn => {
if (fn == "src/main.ts") return " "
else return read(fn)
}
const info = serverInfo(host)
const path = "vscode/src/server-info.json"
writeFileSync(path, JSON.stringify(info, null, 4))
log(`wrote ${path}`)
process.exit(0)
}
if (options.test) {
const files = readdir(ctest)
.concat(readdir(samples))
.filter(f => /\.ts$/.test(f))
for (const fn of files) {
console.log(`*** test ${fn}`)
const host = await getHost(resolveBuildConfig(), {}, ".")
testCompiler(fn, host)
}
for (const fn of readdir(rtest)) {
console.log(`*** run ${fn}`)
await runTest(fn)
}
await runTest(path.join(rtest, "allcompile.ts"), {
flag: { allFunctions: true, allPrototypes: true },
})
process.exit(0)
}
}
const boardRepos = [
"https://github.com/microsoft/devicescript-esp32",
"https://github.com/microsoft/devicescript-pico",
]
function resolveSchema(sch: string, repo: string) {
if (/^https?:/.test(sch)) return sch
// https:///microsoft/devicescript-pico/main/boards/rp2040archconfig.schema.json
const boards =
repo.replace("github.com", "raw.githubusercontent.com") +
"/main/boards/"
if (sch.startsWith("../")) return boards + sch.slice(3)
return sch
}
export function sortJSON(obj: any): any {
if (Array.isArray(obj)) return obj.map(sortJSON)
else if (obj && typeof obj == "object") {
const keys = Object.keys(obj)
keys.sort(strcmp)
const r: any = {}
for (const k of keys) {
r[k] = sortJSON(obj[k])
}
return r
} else {
return obj
}
}
async function fetchBoards(options: CToolOptions) {
const ginfo: RepoInfo = { boards: {}, archs: {} }
for (const repo of boardRepos) {
let info: RepoInfo
if (options.localBoards) {
const p = path.join(
options.localBoards,
repo.replace(/.*\//, ""),
"dist",
"info.json"
)
log(`fetch from ${p}`)
info = readJSON5Sync(p)
} else {
const url = repo + "/releases/latest/download/info.json"
log(`fetch from ${url}`)
const resp = await fetch(url)
info = await resp.json()
}
// just in case
for (const a of Object.values(info?.archs ?? {})) {
if (a.pins) delete (a.pins as any)["#pinInfo"]
}
for (const archId of Object.keys(info.archs)) {
const arch = info.archs[archId]
log(` ARCH ${archId}: ${arch.name}`)
if (arch.id != archId) throw new Error(`arch.id wrong in ${archId}`)
const ex = ginfo.archs[arch.id]
if (ex && ex !== arch)
throw new Error(`arch ${arch.id} already defined`)
arch.$schema = resolveSchema(arch.$schema, repo)
arch.repoUrl = repo
ginfo.archs[arch.id] = arch
}
for (const bid of Object.keys(info.boards)) {
const board = info.boards[bid]
log(` ${bid}: ${board.devName}`)
board.$schema = resolveSchema(board.$schema, repo)
if (board.id != bid) throw new Error("board.id wrong")
validateBoard(board, ginfo)
ginfo.boards[bid] = board
}
}
for (const arch of Object.values(ginfo.archs)) {
const boards = Object.values(ginfo.boards).filter(
b => b.archId == arch.id
)
const fwBoards = boards.filter(b => !!b.$fwUrl)
if (!arch.bareUrl) {
const bareBoard =
fwBoards.find(b => /bare/.test(b.id)) ?? fwBoards[0]
arch.bareUrl = bareBoard?.$fwUrl
}
}
return sortJSON(ginfo)
}