microbit-clang-wasm-codal 0.305.0-alpha.3 → 0.305.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -3
- package/codal/manifest.json +2 -3
- package/lib/config.js +8 -10
- package/lib/core.js +117 -23
- package/lib/index.d.ts +30 -2
- package/lib/node.d.ts +3 -1
- package/lib/node.js +3 -0
- package/lib/recipe.js +41 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
**Builds a BBC micro:bit C++ program to a hex file, in a browser or in Node, with nothing
|
|
4
4
|
installed.** Give it your sources, get back the hex, the link map, and the compiler's output both as
|
|
5
|
-
text and read into errors and warnings.
|
|
5
|
+
text and read into errors and warnings. It also lists what can be typed at a position, for an
|
|
6
|
+
editor's completions.
|
|
6
7
|
|
|
7
8
|
It is not a copy of CODAL on npm. Three things come together here: CODAL prebuilt as static
|
|
8
9
|
libraries, **the build recipe captured from CODAL's own native CMake build** so the flags are the
|
|
@@ -58,6 +59,36 @@ and rejects with the signal's reason.
|
|
|
58
59
|
await compile(files, { onStep: (step) => console.log(step.tool, step.exitCode), signal: controller.signal });
|
|
59
60
|
```
|
|
60
61
|
|
|
62
|
+
## Completions
|
|
63
|
+
|
|
64
|
+
`complete` lists what can go at a position in one of your files, for an editor's completion list:
|
|
65
|
+
the members after `uBit.` or `uBit.display.`, anything in scope, a call's overloads, with each
|
|
66
|
+
one's type, parameters, defaults and the first sentence of CODAL's documentation. It runs the same
|
|
67
|
+
recipe and `codal.json` settings a build would, up to the position, and compiles no CODAL, so a
|
|
68
|
+
changed `codal.json` changes the list at once.
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
import { complete } from 'microbit-clang-wasm-codal';
|
|
72
|
+
|
|
73
|
+
const { completions } = await complete({ 'main.cpp': source }, { file: 'main.cpp', line: 7, column: 10 });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
It needs only the file, the headers it may include and `codal.json`. The line counts from 1 and the
|
|
77
|
+
column in bytes of UTF-8, as Clang does. The records are
|
|
78
|
+
[microbit-clang-wasm](https://github.com/carlosperate/microbit-clang-wasm)'s `readCompletions`,
|
|
79
|
+
with your own file names, and of the 14,000 macros CODAL brings in only the ones a program passes
|
|
80
|
+
to it or gets back: component IDs (`MICROBIT_ID_BUTTON_A`), event values
|
|
81
|
+
(`MICROBIT_BUTTON_EVT_CLICK`), listen flags (`MESSAGE_BUS_LISTENER_IMMEDIATE`), the event service's
|
|
82
|
+
(`MES_…`) and error codes (`MICROBIT_OK`, `MICROBIT_NO_DATA`). Your own `#define`s are listed too,
|
|
83
|
+
and so are the settings you write in `codal.json`'s `config`.
|
|
84
|
+
The deep-sleep members every CODAL component inherits from `CodalComponent` are left out. A
|
|
85
|
+
request takes a tenth to a quarter of a second in Node once loaded; the first after a
|
|
86
|
+
change of settings also lists CODAL's macros, about a quarter of a second more. Completions run in a filesystem of their
|
|
87
|
+
own, so one never waits for a build or changes it, and they queue only behind each other. A
|
|
88
|
+
`signal` aborted before a request starts drops it.
|
|
89
|
+
|
|
90
|
+
## Loading it
|
|
91
|
+
|
|
61
92
|
In Node the package reads its own files from disk. Anywhere else, hand it a loader for the two
|
|
62
93
|
assets it ships, `codal/manifest.json` and `codal/payload.tar`:
|
|
63
94
|
|
|
@@ -152,8 +183,9 @@ the build harness, `codalJson` the keys written over its `codal.json` — and re
|
|
|
152
183
|
|
|
153
184
|
`npm test` compiles a program, checks the hex, and checks that errors in the program, in CODAL's
|
|
154
185
|
headers because of it, and at the link are read into diagnostics naming the caller's files; that a
|
|
155
|
-
`codal.json` with other settings compiles CODAL once and is then reused;
|
|
156
|
-
every refusal
|
|
186
|
+
`codal.json` with other settings compiles CODAL once and is then reused; the header rule and
|
|
187
|
+
every refusal; and what `complete` lists, which macros among it, and that a build running beside it
|
|
188
|
+
gives the same hex. It needs nothing installed.
|
|
157
189
|
|
|
158
190
|
Further tests compare against native builds, and run only when pointed at them:
|
|
159
191
|
|
package/codal/manifest.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schema": 2,
|
|
3
|
-
"generated": "2026-10-
|
|
3
|
+
"generated": "2026-10-06",
|
|
4
4
|
"codal": {
|
|
5
5
|
"version": "0.3.5",
|
|
6
6
|
"versionHash": "f5d1682",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"commit": "1fbb7240290fe36a55c61378f5cdeb7640f3ec4a"
|
|
31
31
|
}
|
|
32
32
|
],
|
|
33
|
-
"pin": "v0.3.5",
|
|
34
33
|
"target": {
|
|
35
34
|
"name": "codal-microbit-v2",
|
|
36
35
|
"url": "https://github.com/lancaster-university/codal-microbit-v2",
|
|
@@ -40,7 +39,7 @@
|
|
|
40
39
|
},
|
|
41
40
|
"toolchain": {
|
|
42
41
|
"package": "microbit-clang-wasm",
|
|
43
|
-
"version": "21.11.0-alpha.
|
|
42
|
+
"version": "21.11.0-alpha.3",
|
|
44
43
|
"llvm": {
|
|
45
44
|
"version": "21.1.1",
|
|
46
45
|
"repository": "https://github.com/arm/arm-toolchain.git",
|
package/lib/config.js
CHANGED
|
@@ -14,7 +14,7 @@ export class ConfigError extends Error {
|
|
|
14
14
|
* @param {string} text the caller's codal.json
|
|
15
15
|
* @param {string} targetText the payload's target-locked.json
|
|
16
16
|
* @param {object} target the one target this package has
|
|
17
|
-
* @returns {{ header: string, softdevice: boolean }}
|
|
17
|
+
* @returns {{ header: string, softdevice: boolean, own: Set<string> }} `own` is the keys the caller wrote
|
|
18
18
|
*/
|
|
19
19
|
export function configure(text, targetText, target) {
|
|
20
20
|
const json = parse(text.replace(/^/, ''));
|
|
@@ -43,7 +43,7 @@ export function configure(text, targetText, target) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const lines = [...merged].map(([key, value]) => ` #define ${key}\t ${plain(value)}\n`).join('');
|
|
46
|
-
return { header: lines + (softdevice ? SOFTDEVICE : ''), softdevice };
|
|
46
|
+
return { header: lines + (softdevice ? SOFTDEVICE : ''), softdevice, own: new Set(config.keys()) };
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
function checkTarget(given, target) {
|
|
@@ -122,29 +122,27 @@ function parse(text) {
|
|
|
122
122
|
at++;
|
|
123
123
|
space();
|
|
124
124
|
if (text[at] !== close) {
|
|
125
|
-
do
|
|
126
|
-
|
|
125
|
+
do {
|
|
126
|
+
space();
|
|
127
|
+
item();
|
|
128
|
+
space();
|
|
129
|
+
} while (text[at] === ',' && ++at);
|
|
127
130
|
}
|
|
128
131
|
expect(close);
|
|
129
132
|
};
|
|
130
133
|
const object = () => {
|
|
131
134
|
const entries = new Map();
|
|
132
135
|
items('}', () => {
|
|
133
|
-
space();
|
|
134
136
|
const key = token(STRING);
|
|
135
137
|
if (key === undefined) fail('a quoted name');
|
|
136
138
|
expect(':');
|
|
137
139
|
entries.set(JSON.parse(key), value());
|
|
138
|
-
space();
|
|
139
140
|
});
|
|
140
141
|
return entries;
|
|
141
142
|
};
|
|
142
143
|
const array = () => {
|
|
143
144
|
const list = [];
|
|
144
|
-
items(']', () =>
|
|
145
|
-
list.push(value());
|
|
146
|
-
space();
|
|
147
|
-
});
|
|
145
|
+
items(']', () => list.push(value()));
|
|
148
146
|
return list;
|
|
149
147
|
};
|
|
150
148
|
|
package/lib/core.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// Compiles a user program against CODAL: the prebuilt archives, or CODAL compiled here for the
|
|
2
|
-
// settings in the caller's codal.json.
|
|
2
|
+
// settings in the caller's codal.json. Completions run the same recipe up to a position.
|
|
3
3
|
//
|
|
4
4
|
// The recipes come from `codal/manifest.json`, generated from the native build's own command list,
|
|
5
5
|
// so this runs the flags the native toolchain runs.
|
|
6
6
|
|
|
7
|
-
import { createSession, readDiagnostics, sysroot } from 'microbit-clang-wasm';
|
|
7
|
+
import { createSession, readCompletions, readDiagnostics, sysroot } from 'microbit-clang-wasm';
|
|
8
8
|
|
|
9
9
|
import { CONFIG_HEADER, configure } from './config.js';
|
|
10
|
-
import { SOURCE, plan } from './recipe.js';
|
|
11
|
-
import { unpackTar } from './tar.js';
|
|
10
|
+
import { SOURCE, completion, plan } from './recipe.js';
|
|
11
|
+
import { flatten, unpackTar } from './tar.js';
|
|
12
12
|
|
|
13
13
|
// C++ only: the manifest carries one compile recipe, taken from a C++ translation unit, so a .c file
|
|
14
14
|
// would be compiled as C++ and fail on valid C. A C recipe means capturing one in the generator.
|
|
@@ -20,6 +20,20 @@ const CODAL_JSON = 'codal.json';
|
|
|
20
20
|
// Configurations CODAL was compiled for, kept besides the prebuilt one: about 8 MB of archives each.
|
|
21
21
|
const KEEP = 4;
|
|
22
22
|
|
|
23
|
+
// A C++ source or a header, parsed on its own as C++.
|
|
24
|
+
const COMPLETABLE = /\.(cc|cpp|cxx|h|hh|hpp|hxx)$/;
|
|
25
|
+
|
|
26
|
+
// The macros a program passes to CODAL's API, chiefly to messageBus.listen, and the error codes its
|
|
27
|
+
// functions return. The 14,000 others MicroBit.h brings in, nRF registers, CMSIS and CODAL's own
|
|
28
|
+
// settings, stay out of completion lists.
|
|
29
|
+
const USEFUL_MACRO = new RegExp(
|
|
30
|
+
'^(?:(?:MICROBIT|DEVICE)_ID_|(?:MICROBIT|DEVICE)_(?:[A-Z0-9]+_)*(?:EVT|EVENT)_|MESSAGE_BUS_LISTENER_|MES_' +
|
|
31
|
+
'|MICROBIT_(?:OK|INVALID_PARAMETER|NOT_SUPPORTED|CALIBRATION_IN_PROGRESS|CALIBRATION_REQUIRED|NO_RESOURCES|BUSY|CANCELLED|I2C_ERROR|SERIAL_IN_USE|NO_DATA)$)'
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// Where the completion session lists CODAL's macros, outside the caller's files.
|
|
35
|
+
const MACROS_SOURCE = 'complete/macros.cpp';
|
|
36
|
+
|
|
23
37
|
/**
|
|
24
38
|
* @param {{ loadAsset: (name: string) => Promise<Uint8Array> | Uint8Array }} options
|
|
25
39
|
*/
|
|
@@ -54,12 +68,7 @@ export function createCodal({ loadAsset } = {}) {
|
|
|
54
68
|
if (sources.length === 0) throw new Error('no source file to compile; expected at least one .cpp');
|
|
55
69
|
for (const name of Object.keys(files)) assertWorkspacePath(name);
|
|
56
70
|
|
|
57
|
-
const
|
|
58
|
-
const { header, softdevice } = configure(
|
|
59
|
-
given === undefined ? codal.defaultJson : typeof given === 'string' ? given : new TextDecoder().decode(given),
|
|
60
|
-
codal.targetJson,
|
|
61
|
-
manifest.codal.target
|
|
62
|
-
);
|
|
71
|
+
const { header, softdevice } = configure(codalJsonOf(files, codal), codal.targetJson, manifest.codal.target);
|
|
63
72
|
const recipe = plan(manifest, Object.keys(files), { sysroot, softdevice });
|
|
64
73
|
signal?.throwIfAborted();
|
|
65
74
|
for (const path of recipe.stale) await session.remove(path);
|
|
@@ -75,6 +84,8 @@ export function createCodal({ loadAsset } = {}) {
|
|
|
75
84
|
if (signal) await new Promise((resolve) => setTimeout(resolve, 0));
|
|
76
85
|
// Between steps only: a tool already running cannot be interrupted, so it finishes first.
|
|
77
86
|
signal?.throwIfAborted();
|
|
87
|
+
// A tool writing over an existing file in the toolchain's filesystem leaves the old one, exit 0.
|
|
88
|
+
for (const path of command.outputs) await session.remove(path);
|
|
78
89
|
const step = { ...(await invoke(session, command, manifest.layout.project)), codal: progress };
|
|
79
90
|
steps.push(step);
|
|
80
91
|
onStep?.(step);
|
|
@@ -84,18 +95,16 @@ export function createCodal({ loadAsset } = {}) {
|
|
|
84
95
|
// CODAL first, so a log reads in order: the libraries, then the program built on them.
|
|
85
96
|
let archives = codal.archivesFor(header);
|
|
86
97
|
if (!archives) {
|
|
87
|
-
// Removed first, as in a clean native build: a tool writing over an existing file in the
|
|
88
|
-
// toolchain's filesystem leaves the old one in place, exit code 0.
|
|
89
98
|
codal.current = null;
|
|
90
|
-
for (const path of recipe.codalOutputs) await session.remove(path);
|
|
91
99
|
for (const [index, command] of recipe.codal.entries()) {
|
|
92
100
|
if (!(await run(command, { done: index + 1, total: recipe.codal.length, file: command.file }))) return failure(steps);
|
|
93
101
|
}
|
|
94
102
|
archives = {};
|
|
95
|
-
for (const
|
|
103
|
+
for (const { output } of manifest.library.archives) archives[output] = await session.readFile(output);
|
|
96
104
|
codal.keep(header, archives);
|
|
97
105
|
}
|
|
98
|
-
// Null after any rebuild, finished or not, so the
|
|
106
|
+
// Null after any rebuild, finished or not, so the archives are rewritten: the session then holds
|
|
107
|
+
// the kept copy rather than a second one.
|
|
99
108
|
if (codal.current !== header) {
|
|
100
109
|
for (const [path, bytes] of Object.entries(archives)) await session.writeFile(path, bytes);
|
|
101
110
|
codal.current = header;
|
|
@@ -121,6 +130,42 @@ export function createCodal({ loadAsset } = {}) {
|
|
|
121
130
|
};
|
|
122
131
|
};
|
|
123
132
|
|
|
133
|
+
// Completions queue among themselves only: their session is not the builds'.
|
|
134
|
+
let completing = Promise.resolve();
|
|
135
|
+
|
|
136
|
+
const complete = async (files, { file, line, column, signal } = {}) => {
|
|
137
|
+
// As for a build: a cancel sent while the request before this one ran arrives only now.
|
|
138
|
+
if (signal) await new Promise((resolve) => setTimeout(resolve, 0));
|
|
139
|
+
signal?.throwIfAborted();
|
|
140
|
+
const codal = await load();
|
|
141
|
+
const { manifest } = codal;
|
|
142
|
+
for (const name of Object.keys(files)) assertWorkspacePath(name);
|
|
143
|
+
if (!Object.hasOwn(files, file)) throw new Error(`${JSON.stringify(file)} is not among the files given`);
|
|
144
|
+
if (!COMPLETABLE.test(file)) throw new Error(`only C++ sources and headers can be completed in, and ${file} is neither`);
|
|
145
|
+
if (![line, column].every((at) => Number.isInteger(at) && at >= 1)) {
|
|
146
|
+
throw new Error(`line and column count from 1, and ${line}:${column} is not a position`);
|
|
147
|
+
}
|
|
148
|
+
const { header, own } = configure(codalJsonOf(files, codal), codal.targetJson, manifest.codal.target);
|
|
149
|
+
// Settings the caller wrote are their own macros, unlike the defaults used with no codal.json.
|
|
150
|
+
const yours = files[CODAL_JSON] === undefined ? new Set() : own;
|
|
151
|
+
|
|
152
|
+
const session = await codal.completionSession();
|
|
153
|
+
const { project } = manifest.layout;
|
|
154
|
+
await session.remove(project.split('/')[0]);
|
|
155
|
+
for (const [name, content] of Object.entries(files)) await session.writeFile(`${project}/${name}`, content);
|
|
156
|
+
await session.writeFile(CONFIG_HEADER, header);
|
|
157
|
+
|
|
158
|
+
// A failure is kept too: the same settings would fail the same way on every keystroke.
|
|
159
|
+
if (codal.macros?.header !== header) codal.macros = { header, names: await macroNames(session, manifest).catch((error) => error) };
|
|
160
|
+
const { names } = codal.macros;
|
|
161
|
+
if (names instanceof Error) throw names;
|
|
162
|
+
const { tool, args } = completion(manifest, Object.keys(files), { sysroot }, { line, column }, `${project}/${file}`);
|
|
163
|
+
const step = await invoke(session, { tool, args, captureStdout: true }, project);
|
|
164
|
+
const completions = readCompletions(stripProject(new TextDecoder().decode(step.stdout), project))
|
|
165
|
+
.filter((record) => record.kind !== 'candidate' || shown(record, names, yours));
|
|
166
|
+
return { completions, stderr: step.stderr };
|
|
167
|
+
};
|
|
168
|
+
|
|
124
169
|
return {
|
|
125
170
|
async manifest() {
|
|
126
171
|
return (await load()).manifest;
|
|
@@ -135,11 +180,46 @@ export function createCodal({ loadAsset } = {}) {
|
|
|
135
180
|
queue = result.then(() => {}, () => {}); // a failed build must not poison the queue
|
|
136
181
|
return result;
|
|
137
182
|
},
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {Record<string, string | Uint8Array>} files the file to complete in, the headers it may
|
|
186
|
+
* include and codal.json, workspace-relative
|
|
187
|
+
* @param {{ file: string, line: number, column: number, signal?: AbortSignal }} options
|
|
188
|
+
*/
|
|
189
|
+
complete(files, options) {
|
|
190
|
+
const result = completing.then(() => complete(files, options));
|
|
191
|
+
completing = result.then(() => {}, () => {});
|
|
192
|
+
return result;
|
|
193
|
+
},
|
|
138
194
|
};
|
|
139
195
|
}
|
|
140
196
|
|
|
141
|
-
|
|
142
|
-
|
|
197
|
+
function codalJsonOf(files, codal) {
|
|
198
|
+
const given = files[CODAL_JSON];
|
|
199
|
+
return given === undefined ? codal.defaultJson : typeof given === 'string' ? given : new TextDecoder().decode(given);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Every macro CODAL defines under this configuration, by the user recipe's own flags, so its `-D`s
|
|
203
|
+
// and the configuration header are among them. A user's own `#define` is not.
|
|
204
|
+
async function macroNames(session, manifest) {
|
|
205
|
+
await session.writeFile(MACROS_SOURCE, '#include "MicroBit.h"\n');
|
|
206
|
+
const { tool, args } = completion(manifest, [], { sysroot }, null, MACROS_SOURCE);
|
|
207
|
+
const { exitCode, stdout, stderr } = await invoke(session, { tool, args, captureStdout: true }, manifest.layout.project);
|
|
208
|
+
if (exitCode !== 0) throw new Error(`CODAL's headers did not preprocess with these settings, so nothing can be completed:\n${stderr}`);
|
|
209
|
+
return new Set(Array.from(new TextDecoder().decode(stdout).matchAll(/^#define ([A-Za-z_]\w*)/gm), (match) => match[1]));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Of CODAL's macros only the useful ones, the caller's own settings and anything typed, as a macro
|
|
213
|
+
// never is: a member of a header that leaves out MicroBit.h may share a macro's name. And not the
|
|
214
|
+
// deep-sleep plumbing every component inherits from CodalComponent, nine members a program never calls.
|
|
215
|
+
function shown({ name, chunks }, macros, yours) {
|
|
216
|
+
const typed = chunks.some((chunk) => chunk.kind === 'informative');
|
|
217
|
+
if (macros.has(name) && !typed && !yours.has(name)) return USEFUL_MACRO.test(name);
|
|
218
|
+
return !(name.startsWith('deepSleep') && chunks.some((chunk) => chunk.kind === 'informative' && chunk.text.includes('CodalComponent')));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// A name says where the file goes under the project folder, so `..` could put it outside and `.` or
|
|
222
|
+
// an empty segment give one file two names. Rejecting here says which file is wrong.
|
|
143
223
|
function assertWorkspacePath(name) {
|
|
144
224
|
const bad =
|
|
145
225
|
name.startsWith('/') ? 'is absolute'
|
|
@@ -155,18 +235,32 @@ function assertWorkspacePath(name) {
|
|
|
155
235
|
async function open(loadAsset) {
|
|
156
236
|
const manifest = JSON.parse(new TextDecoder().decode(await loadAsset('codal/manifest.json')));
|
|
157
237
|
const session = createSession();
|
|
158
|
-
|
|
159
|
-
|
|
238
|
+
const tree = unpackTar(await loadAsset('codal/payload.tar'));
|
|
239
|
+
await session.writeTree(tree);
|
|
240
|
+
// The session keeps these very buffers, and a tool replaces a file's buffer rather than writing into it.
|
|
241
|
+
const payload = flatten(tree);
|
|
242
|
+
const text = (path) => new TextDecoder().decode(payload[path]);
|
|
160
243
|
|
|
161
|
-
const prebuilt = { header:
|
|
162
|
-
for (const { output } of manifest.library.archives) prebuilt.archives[output] =
|
|
244
|
+
const prebuilt = { header: text(CONFIG_HEADER), archives: {} };
|
|
245
|
+
for (const { output } of manifest.library.archives) prebuilt.archives[output] = payload[output];
|
|
163
246
|
const built = new Map();
|
|
247
|
+
let second = null;
|
|
164
248
|
|
|
165
249
|
return {
|
|
166
250
|
manifest,
|
|
167
251
|
session,
|
|
168
|
-
|
|
169
|
-
|
|
252
|
+
// Over the same payload buffers, so a second filesystem costs little more than its folders.
|
|
253
|
+
completionSession() {
|
|
254
|
+
return (second ??= (async () => {
|
|
255
|
+
const completion = createSession();
|
|
256
|
+
await completion.writeTree(tree);
|
|
257
|
+
return completion;
|
|
258
|
+
})());
|
|
259
|
+
},
|
|
260
|
+
// CODAL's macro names for the configuration header last completed in.
|
|
261
|
+
macros: null,
|
|
262
|
+
defaultJson: text('codal/codal.json'),
|
|
263
|
+
targetJson: text(`codal/libraries/${manifest.codal.target.name}/target-locked.json`),
|
|
170
264
|
current: prebuilt.header,
|
|
171
265
|
archivesFor(header) {
|
|
172
266
|
if (header === prebuilt.header) return prebuilt.archives;
|
package/lib/index.d.ts
CHANGED
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export type Files = Record<string, string | Uint8Array>;
|
|
7
7
|
|
|
8
|
-
import type { Diagnostic, OtherOutput } from 'microbit-clang-wasm';
|
|
8
|
+
import type { CompletionRecord, Diagnostic, OtherOutput } from 'microbit-clang-wasm';
|
|
9
9
|
|
|
10
|
-
export type { Diagnostic, DiagnosticLocation, DiagnosticNote, OtherOutput } from 'microbit-clang-wasm';
|
|
10
|
+
export type { CompletionChunk, CompletionRecord, Diagnostic, DiagnosticLocation, DiagnosticNote, OtherOutput } from 'microbit-clang-wasm';
|
|
11
11
|
|
|
12
12
|
export type AssetLoader = (name: string) => Promise<Uint8Array> | Uint8Array;
|
|
13
13
|
|
|
@@ -53,6 +53,33 @@ export type CompileOptions = {
|
|
|
53
53
|
/** Rejects with a `ConfigError`, compiling nothing, for a codal.json it cannot follow. */
|
|
54
54
|
export type Compile = (files: Files, options?: CompileOptions) => Promise<Result>;
|
|
55
55
|
|
|
56
|
+
export type CompleteOptions = {
|
|
57
|
+
/** The file to complete in, named as in `files`: a C++ source, or a header parsed on its own. */
|
|
58
|
+
file: string;
|
|
59
|
+
/** From 1, the column in bytes of UTF-8, as Clang counts it. */
|
|
60
|
+
line: number;
|
|
61
|
+
column: number;
|
|
62
|
+
/** Aborting before the request starts rejects with the signal's reason; one running finishes. */
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type Completion = {
|
|
67
|
+
/** The toolchain's records, with the caller's own file names. Of the macros CODAL defines only
|
|
68
|
+
* the ones a program passes to it or gets back are kept: component IDs, event values, listen
|
|
69
|
+
* flags, the event service's and error codes, as are the caller's own `#define`s and
|
|
70
|
+
* `codal.json` settings. CodalComponent's deep-sleep members are not. */
|
|
71
|
+
completions: CompletionRecord[];
|
|
72
|
+
/** What Clang printed on stderr, with the caller's own file names. */
|
|
73
|
+
stderr: string;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* What can go at a position, by the recipe and the `codal.json` a build of these files would use,
|
|
78
|
+
* with no CODAL compiled. `files` needs only the file, the headers it may include and `codal.json`.
|
|
79
|
+
* Its own filesystem, so it never waits for a build or changes one. Rejects as `Compile` does.
|
|
80
|
+
*/
|
|
81
|
+
export type Complete = (files: Files, options: CompleteOptions) => Promise<Completion>;
|
|
82
|
+
|
|
56
83
|
/** Its message's first line says what is wrong and what to change; any further lines are detail. */
|
|
57
84
|
export class ConfigError extends Error {
|
|
58
85
|
name: 'ConfigError';
|
|
@@ -81,6 +108,7 @@ export type Manifest = {
|
|
|
81
108
|
|
|
82
109
|
export type Codal = {
|
|
83
110
|
compile: Compile;
|
|
111
|
+
complete: Complete;
|
|
84
112
|
manifest: () => Promise<Manifest>;
|
|
85
113
|
};
|
|
86
114
|
|
package/lib/node.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from './index.js';
|
|
2
2
|
|
|
3
|
-
import type { Compile, Manifest } from './index.js';
|
|
3
|
+
import type { Compile, Complete, Manifest } from './index.js';
|
|
4
4
|
|
|
5
5
|
/** Reads the package's own assets from disk, so no loader has to be supplied. */
|
|
6
6
|
export const compile: Compile;
|
|
7
7
|
|
|
8
|
+
export const complete: Complete;
|
|
9
|
+
|
|
8
10
|
export function manifest(): Promise<Manifest>;
|
package/lib/node.js
CHANGED
|
@@ -12,4 +12,7 @@ const shared = () => (codal ??= createCodal({ loadAsset: (name) => readFile(new
|
|
|
12
12
|
/** @type {import('./index.js').Compile} */
|
|
13
13
|
export const compile = (files, options) => shared().compile(files, options);
|
|
14
14
|
|
|
15
|
+
/** @type {import('./index.js').Complete} */
|
|
16
|
+
export const complete = (files, options) => shared().complete(files, options);
|
|
17
|
+
|
|
15
18
|
export const manifest = () => shared().manifest();
|
package/lib/recipe.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
// Turns the manifest into the exact commands a build runs.
|
|
2
|
-
//
|
|
3
|
-
// Shared so the WebAssembly path and the native replay cannot interpret the same manifest
|
|
4
|
-
// differently: the runner is then the only difference between them, which is what the replay tests.
|
|
1
|
+
// Turns the manifest into the exact commands a build runs, each with the files it writes.
|
|
5
2
|
|
|
6
3
|
const SYSROOT_TOKEN = '{sysroot}';
|
|
7
4
|
|
|
@@ -14,7 +11,7 @@ export const SOURCE = /\.(cc|cpp|cxx)$/;
|
|
|
14
11
|
* headers are mounted, and whether the configuration links the SoftDevice's linker script
|
|
15
12
|
*/
|
|
16
13
|
export function plan(manifest, names, { sysroot, softdevice }) {
|
|
17
|
-
const fill = (
|
|
14
|
+
const fill = filler(sysroot);
|
|
18
15
|
const { project, build } = manifest.layout;
|
|
19
16
|
const sources = names.filter((name) => SOURCE.test(name));
|
|
20
17
|
const { scripts } = manifest.link;
|
|
@@ -23,37 +20,67 @@ export function plan(manifest, names, { sysroot, softdevice }) {
|
|
|
23
20
|
// Indexed, because flattening the path would collide `a/b.cpp` with `a_b.cpp` and link one object
|
|
24
21
|
// twice.
|
|
25
22
|
const objects = sources.map((name, index) => `${build}/user/${index}-${name.split('/').pop()}.obj`);
|
|
26
|
-
const
|
|
23
|
+
const flags = userFlags(manifest, names, fill);
|
|
27
24
|
|
|
28
25
|
return {
|
|
29
26
|
compiles: sources.map((name, index) => ({
|
|
30
27
|
source: name,
|
|
31
28
|
tool: manifest.compile.tool,
|
|
32
|
-
args: [...
|
|
29
|
+
args: [...flags, '-c', `${project}/${name}`, '-o', objects[index]],
|
|
30
|
+
outputs: [objects[index]],
|
|
33
31
|
})),
|
|
34
32
|
// CODAL's own build, run only when no archives exist yet for this configuration. Each names what
|
|
35
33
|
// it builds as a reader knows it: a source within CODAL's libraries, or an archive.
|
|
36
34
|
codal: [
|
|
37
|
-
...manifest.library.compiles.map(({ tool, args, source }) => ({
|
|
38
|
-
|
|
35
|
+
...manifest.library.compiles.map(({ tool, args, source, output }) => ({
|
|
36
|
+
tool,
|
|
37
|
+
args: fill(args),
|
|
38
|
+
outputs: [output],
|
|
39
|
+
file: source.replace(/^codal\/libraries\//, ''),
|
|
40
|
+
})),
|
|
41
|
+
...manifest.library.archives.map(({ tool, args, output }) => ({ tool, args: fill(args), outputs: [output], file: output.split('/').pop() })),
|
|
39
42
|
],
|
|
40
|
-
codalOutputs: [...manifest.library.compiles, ...manifest.library.archives].map((step) => step.output),
|
|
41
|
-
archives: manifest.library.archives.map((archive) => archive.output),
|
|
42
43
|
tail: [
|
|
43
44
|
{
|
|
44
45
|
tool: manifest.link.tool,
|
|
45
46
|
args: script([...fill(manifest.link.flagsBefore), ...objects, ...fill(manifest.link.flagsAfter)]),
|
|
47
|
+
outputs: [manifest.link.output, manifest.link.map].filter(Boolean),
|
|
46
48
|
},
|
|
47
49
|
// objcopy writes the hex to stdout: wasi-libc has no chmod, and objcopy otherwise fails
|
|
48
50
|
// mirroring the input's permissions onto a file it has already written correctly.
|
|
49
|
-
{ tool: manifest.objcopy.tool, args: fill(manifest.objcopy.args), captureStdout: true },
|
|
51
|
+
{ tool: manifest.objcopy.tool, args: fill(manifest.objcopy.args), outputs: [], captureStdout: true },
|
|
50
52
|
],
|
|
51
|
-
//
|
|
52
|
-
stale: [project.split('/')[0], `${build}/user
|
|
53
|
+
// The last build's own files and objects, which none of this one's outputs would replace.
|
|
54
|
+
stale: [project.split('/')[0], `${build}/user`],
|
|
53
55
|
map: manifest.link.map,
|
|
54
56
|
};
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The user compile recipe run up to a position rather than compiled, macros listed with the rest,
|
|
61
|
+
* and a header parsed on its own as C++. `-E -dM` in place of the completion flags lists every
|
|
62
|
+
* macro the same flags define.
|
|
63
|
+
* @param {string[]} names every user file, workspace-relative, for the include directories
|
|
64
|
+
* @param {{ sysroot: string }} options
|
|
65
|
+
* @param {{ file: string, line: number, column: number } | null} at the position, null for `-E -dM`
|
|
66
|
+
* @param {string} path the file to run on, in the session
|
|
67
|
+
*/
|
|
68
|
+
export function completion(manifest, names, { sysroot }, at, path) {
|
|
69
|
+
// No warnings: CODAL's headers raise dozens on every request, and a list has no use for them.
|
|
70
|
+
const run = at
|
|
71
|
+
? ['-fsyntax-only', '-w', '-Xclang', `-code-completion-at=${path}:${at.line}:${at.column}`,
|
|
72
|
+
'-Xclang', '-code-completion-brief-comments', '-Xclang', '-code-completion-macros']
|
|
73
|
+
: ['-E', '-dM'];
|
|
74
|
+
return { tool: manifest.compile.tool, args: [...userFlags(manifest, names, filler(sysroot)), ...run, '-x', 'c++', path] };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const filler = (sysroot) => (args) => args.map((arg) => arg.replace(SYSROOT_TOKEN, sysroot));
|
|
78
|
+
|
|
79
|
+
function userFlags(manifest, names, fill) {
|
|
80
|
+
const { project } = manifest.layout;
|
|
81
|
+
return [...fill(manifest.compile.flags), ...headerDirectories(names).map((dir) => `-I${dir ? `${project}/${dir}` : project}`)];
|
|
82
|
+
}
|
|
83
|
+
|
|
57
84
|
// The native build's rule (CMake RECURSIVE_FIND_DIR in microbit-v2-samples): every directory holding
|
|
58
85
|
// a .h, then every one holding a .hpp, without duplicates. '' is the project root.
|
|
59
86
|
function headerDirectories(names) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "microbit-clang-wasm-codal",
|
|
3
|
-
"version": "0.305.0-alpha.
|
|
3
|
+
"version": "0.305.0-alpha.4",
|
|
4
4
|
"description": "Compile BBC micro:bit CODAL C++ programs to a flashable hex in a browser or Node, without any toolchain to install",
|
|
5
5
|
"author": "Carlos Pereira Atencio",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSES",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"./package.json": "./package.json"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"microbit-clang-wasm": "21.11.0-alpha.
|
|
41
|
+
"microbit-clang-wasm": "21.11.0-alpha.3"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"configure": "node tools/configure-harness.mjs",
|