microbit-clang-wasm-codal 0.305.0-alpha.1
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/LICENSES +43 -0
- package/README.md +130 -0
- package/codal/manifest.json +222 -0
- package/codal/payload.tar +0 -0
- package/lib/core.js +151 -0
- package/lib/index.d.ts +66 -0
- package/lib/index.js +5 -0
- package/lib/node.d.ts +8 -0
- package/lib/node.js +15 -0
- package/lib/recipe.js +52 -0
- package/lib/tar.js +127 -0
- package/package.json +62 -0
package/LICENSES
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
This package ships several components under different licences.
|
|
2
|
+
Every component's own notice travels with it, inside codal/payload.tar at the path given below.
|
|
3
|
+
|
|
4
|
+
1. This package's own code: lib/, tools/, test/
|
|
5
|
+
-----------------------------------------------
|
|
6
|
+
MIT. Copyright (c) 2026 Carlos Pereira Atencio.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
9
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
10
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
11
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
15
|
+
portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
18
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
20
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
2. CODAL: codal-core, codal-nrf52, codal-microbit-v2
|
|
24
|
+
----------------------------------------------------
|
|
25
|
+
MIT, Lancaster University and the Micro:bit Educational Foundation. Sources, headers and the
|
|
26
|
+
prebuilt archives built from them.
|
|
27
|
+
|
|
28
|
+
codal/libraries/codal-core/LICENSE
|
|
29
|
+
codal/libraries/codal-nrf52/LICENSE
|
|
30
|
+
codal/libraries/codal-microbit-v2/LICENSE
|
|
31
|
+
|
|
32
|
+
3. Nordic Semiconductor: codal-microbit-nrf5sdk and the prebuilt binary objects
|
|
33
|
+
------------------------------------------------------------------------------
|
|
34
|
+
The nRF5 SDK sources, and the SoftDevice, MBR, bootloader, settings and UICR objects this package
|
|
35
|
+
links (codal/libraries/codal-microbit-v2/lib/*.o), are Nordic Semiconductor's. Their licence permits
|
|
36
|
+
use only with Nordic Semiconductor integrated circuits, which the BBC micro:bit V2 uses.
|
|
37
|
+
|
|
38
|
+
codal/libraries/codal-microbit-nrf5sdk/LICENCE.txt
|
|
39
|
+
|
|
40
|
+
4. The compiler
|
|
41
|
+
---------------
|
|
42
|
+
Not in this package. It comes from microbit-clang-wasm, which carries its own LICENSES file
|
|
43
|
+
covering LLVM, Arm Toolchain for Embedded's runtimes and the YoWASP packaging this is built on.
|
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# microbit-clang-wasm-codal
|
|
2
|
+
|
|
3
|
+
**Builds a BBC micro:bit C++ program to a hex file, in a browser or in Node, with nothing
|
|
4
|
+
installed.** Give it your sources, get back the hex, the link map and the compiler's output.
|
|
5
|
+
|
|
6
|
+
It is not a copy of CODAL on npm. Three things come together here: CODAL prebuilt as static
|
|
7
|
+
libraries, **the build recipe captured from CODAL's own native CMake build** so the flags are the
|
|
8
|
+
ones the supported toolchain uses rather than a hand-written imitation, and the logic that drives
|
|
9
|
+
[microbit-clang-wasm](https://github.com/carlosperate/microbit-clang-wasm)'s WebAssembly Clang
|
|
10
|
+
through the compile, link and hex steps. The CODAL sources ship too, because a program can include
|
|
11
|
+
any header in the tree.
|
|
12
|
+
|
|
13
|
+
On a desktop you install `arm-none-eabi-gcc` and provide CODAL yourself through
|
|
14
|
+
[microbit-v2-samples](https://github.com/lancaster-university/microbit-v2-samples). Here the
|
|
15
|
+
compiler is the toolchain package and CODAL is this one, so a browser, Node or a VS Code extension
|
|
16
|
+
can build a micro:bit program with nothing else installed and nothing downloaded at run time.
|
|
17
|
+
|
|
18
|
+
Status: pre-release, work in progress. Nothing is published yet.
|
|
19
|
+
|
|
20
|
+
## Using it
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { compile } from 'microbit-clang-wasm-codal';
|
|
24
|
+
|
|
25
|
+
const { ok, hex, map, output } = await compile({ 'main.cpp': source });
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`compile` takes the user's files, keyed by workspace-relative path, and returns the Intel hex to
|
|
29
|
+
flash, the link map, and everything the tools wrote to stderr. On a compile or link error `ok` is
|
|
30
|
+
false, `hex` is null, and `output` names the file, by the path you gave, with its line and column.
|
|
31
|
+
|
|
32
|
+
**Key order is link order.** The objects are numbered in the order the sources arrive and linked in
|
|
33
|
+
that order, so hand them over sorted, or in whatever fixed order you choose, if you want the same
|
|
34
|
+
bytes from the same input every time.
|
|
35
|
+
|
|
36
|
+
**C++ only for now** (`.cpp`, `.cc`, `.cxx`): the package carries one compile recipe, taken from a
|
|
37
|
+
C++ translation unit, so a `.c` file is rejected rather than quietly compiled as C++. Headers can be
|
|
38
|
+
any name, and go in the same object. Builds are serialised, so a second `compile` while one is
|
|
39
|
+
running waits rather than overwriting it.
|
|
40
|
+
|
|
41
|
+
Two options: `onStep` is called with each tool's arguments, exit code and output as it finishes, for
|
|
42
|
+
a build log that streams; `signal` is an `AbortSignal`, and aborting stops the build between steps
|
|
43
|
+
and rejects with the signal's reason.
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
await compile(files, { onStep: (step) => console.log(step.tool, step.exitCode), signal: controller.signal });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
In Node the package reads its own files from disk. Anywhere else, hand it a loader for the two
|
|
50
|
+
assets it ships, `codal/manifest.json` and `codal/payload.tar`:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import { createCodal } from 'microbit-clang-wasm-codal';
|
|
54
|
+
|
|
55
|
+
const codal = createCodal({ loadAsset: async (name) => bytesFor(name) });
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## What it does not do yet
|
|
59
|
+
|
|
60
|
+
- **`codal.json` is fixed.** This version ships one configuration, prebuilt: the repository default
|
|
61
|
+
from microbit-v2-samples, which has the SoftDevice present but the BLE stack off. Changing the
|
|
62
|
+
configuration means rebuilding CODAL, which comes later.
|
|
63
|
+
- **No CODAL sources are compiled.** The four archives ship prebuilt; the complete source and header
|
|
64
|
+
trees are here so that any header a program reaches is present, but nothing recompiles them.
|
|
65
|
+
- No diagnostics beyond the compiler's own text, no worker pool, no object cache.
|
|
66
|
+
|
|
67
|
+
## How it is built
|
|
68
|
+
|
|
69
|
+
CI checks out the build harness named by `codal.buildSystem` in `package.json` — a
|
|
70
|
+
[microbit-v2-samples](https://github.com/carlosperate/microbit-v2-samples) fork carrying the CLANG
|
|
71
|
+
toolchain files — points it at the pinned CODAL with `tools/configure-harness.mjs`, builds it with
|
|
72
|
+
`CODAL_TOOLCHAIN=CLANG` against the Arm Toolchain for Embedded release the installed
|
|
73
|
+
`microbit-clang-wasm` was built from (`npm view microbit-clang-wasm llvm`), and runs
|
|
74
|
+
`tools/generate.mjs` over that build's own `ninja -t commands`. Everything authored is in
|
|
75
|
+
`package.json`; the ATfE release is deliberately not repeated here. The
|
|
76
|
+
recipe in `codal/manifest.json` is therefore the native build's flags, rewritten for a virtual
|
|
77
|
+
filesystem rather than written by hand.
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
node tools/configure-harness.mjs <harness checkout> # then build it with CODAL_TOOLCHAIN=CLANG
|
|
81
|
+
node tools/generate.mjs --samples <harness checkout> --out .
|
|
82
|
+
npm test
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Versioning scheme
|
|
86
|
+
|
|
87
|
+
The package version follows `MAJOR.MINOR.PATCH`, but it combines the CODAL version with the version
|
|
88
|
+
of this package's own build logic, where:
|
|
89
|
+
|
|
90
|
+
- MAJOR is the CODAL major version
|
|
91
|
+
- MINOR is the CODAL minor and patch versions joined as one number, the patch padded to two digits:
|
|
92
|
+
CODAL 0.3.5 → `305`, CODAL 0.2.71 → `271`
|
|
93
|
+
- PATCH is a single number for the packaging: the recipe, the JavaScript, the payload
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
0 . 305 . 2
|
|
97
|
+
│ └┬┘ └┬┘
|
|
98
|
+
│ │ └── packaging version
|
|
99
|
+
│ └──────── CODAL minor.patch (3.05)
|
|
100
|
+
└───────────── CODAL major
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
So `~0.305.0` locks to CODAL 0.3.5 while taking packaging fixes. While the packaging of a CODAL
|
|
104
|
+
release is still changing it is published as a prerelease, `0.305.0-alpha.1`, under the `next`
|
|
105
|
+
dist-tag. Which CODAL is inside is pinned in `package.json` under `codal` — `buildSystem` names
|
|
106
|
+
the build harness, `codalJson` the keys written over its `codal.json` — and recorded in
|
|
107
|
+
`codal/manifest.json`.
|
|
108
|
+
|
|
109
|
+
## Tests
|
|
110
|
+
|
|
111
|
+
`npm test` compiles a program, checks the hex and checks an error is reported usefully. It needs
|
|
112
|
+
nothing installed.
|
|
113
|
+
|
|
114
|
+
One further test compares against the hex a native build produced, and runs only when pointed at
|
|
115
|
+
one:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
MICROBIT_SAMPLES=<built samples tree> npm test
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
It compiles that tree's own program through the WebAssembly compiler and requires the hex to match
|
|
122
|
+
byte for byte. That agreement is what says the browser build is the same build. CI does it on every
|
|
123
|
+
run, against a native build made minutes earlier from the pinned sources, so there is no reference
|
|
124
|
+
file here to go stale.
|
|
125
|
+
|
|
126
|
+
## Licences
|
|
127
|
+
|
|
128
|
+
Several, so `package.json` says `SEE LICENSE IN LICENSES`. This package's code is MIT, CODAL is MIT,
|
|
129
|
+
and the Nordic SDK sources and binary objects carry Nordic's licence, which permits use only with
|
|
130
|
+
Nordic Semiconductor chips. See [LICENSES](LICENSES).
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema": 1,
|
|
3
|
+
"generated": "2026-09-10",
|
|
4
|
+
"codal": {
|
|
5
|
+
"version": "0.3.5",
|
|
6
|
+
"versionHash": "f5d1682",
|
|
7
|
+
"samples": {
|
|
8
|
+
"url": "https://github.com/carlosperate/microbit-v2-samples",
|
|
9
|
+
"commit": "ca73d81ad67c13091d681d6ffe4da3ebd36db97e"
|
|
10
|
+
},
|
|
11
|
+
"libraries": [
|
|
12
|
+
{
|
|
13
|
+
"name": "codal-core",
|
|
14
|
+
"url": "https://github.com/lancaster-university/codal-core",
|
|
15
|
+
"commit": "e6b061f2a6d8977811e3025da387d3007e5796f2"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"name": "codal-microbit-nrf5sdk",
|
|
19
|
+
"url": "https://github.com/microbit-foundation/codal-microbit-nrf5sdk",
|
|
20
|
+
"commit": "4b8abc690f6c9fca6132e6db5ee13a795a263f88"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"name": "codal-microbit-v2",
|
|
24
|
+
"url": "https://github.com/lancaster-university/codal-microbit-v2",
|
|
25
|
+
"commit": "f5d1682fc3882022d3d5a7facb1fddb4b0ea7b52"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"name": "codal-nrf52",
|
|
29
|
+
"url": "https://github.com/lancaster-university/codal-nrf52",
|
|
30
|
+
"commit": "1fbb7240290fe36a55c61378f5cdeb7640f3ec4a"
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"pin": "v0.3.5"
|
|
34
|
+
},
|
|
35
|
+
"toolchain": {
|
|
36
|
+
"package": "microbit-clang-wasm",
|
|
37
|
+
"version": "21.11.0-alpha.1",
|
|
38
|
+
"llvm": {
|
|
39
|
+
"version": "21.1.1",
|
|
40
|
+
"repository": "https://github.com/arm/arm-toolchain.git",
|
|
41
|
+
"release": "release-21.1.1-ATfE",
|
|
42
|
+
"commit": "0bac08d50952133963671eeeb8c3e67695a32f49"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"config": {
|
|
46
|
+
"MICROBIT_BLE_ENABLED": 0,
|
|
47
|
+
"MICROBIT_BLE_PAIRING_MODE": 0
|
|
48
|
+
},
|
|
49
|
+
"layout": {
|
|
50
|
+
"build": "build",
|
|
51
|
+
"project": "project/source"
|
|
52
|
+
},
|
|
53
|
+
"compile": {
|
|
54
|
+
"tool": "clang++",
|
|
55
|
+
"flags": [
|
|
56
|
+
"-DMICROBIT_EXPORTS",
|
|
57
|
+
"-Icodal/libraries",
|
|
58
|
+
"-Icodal/libraries/codal-microbit-v2/inc",
|
|
59
|
+
"-Icodal/libraries/codal-microbit-v2/inc/bluetooth",
|
|
60
|
+
"-Icodal/libraries/codal-microbit-v2/inc/compat",
|
|
61
|
+
"-Icodal/libraries/codal-microbit-v2/model",
|
|
62
|
+
"-Icodal/libraries/codal-nrf52/inc",
|
|
63
|
+
"-Icodal/libraries/codal-nrf52/inc/cmsis",
|
|
64
|
+
"-Icodal/libraries/codal-nrf52/nrfx/mdk",
|
|
65
|
+
"-Icodal/libraries/codal-nrf52/nrfx",
|
|
66
|
+
"-Icodal/libraries/codal-nrf52/nrfx/templates",
|
|
67
|
+
"-Icodal/libraries/codal-nrf52/nrfx/drivers/include",
|
|
68
|
+
"-Icodal/libraries/codal-nrf52/nrfx/drivers/src",
|
|
69
|
+
"-Icodal/libraries/codal-core/inc/core",
|
|
70
|
+
"-Icodal/libraries/codal-core/inc/driver-models",
|
|
71
|
+
"-Icodal/libraries/codal-core/inc/drivers",
|
|
72
|
+
"-Icodal/libraries/codal-core/inc/streams",
|
|
73
|
+
"-Icodal/libraries/codal-core/inc/types",
|
|
74
|
+
"-Icodal/gen",
|
|
75
|
+
"-Icodal/libraries/codal-nrf52/nrfx/hal",
|
|
76
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/sdk_config",
|
|
77
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK_mods",
|
|
78
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK_mods/clang",
|
|
79
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/pwr_mgmt",
|
|
80
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/sortlist",
|
|
81
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/strerror",
|
|
82
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/softdevice/common",
|
|
83
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/crc32",
|
|
84
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/bootloader/dfu",
|
|
85
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/util",
|
|
86
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/ble/common",
|
|
87
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/balloc",
|
|
88
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/ble/peer_manager",
|
|
89
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/ringbuf",
|
|
90
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/timer",
|
|
91
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/log",
|
|
92
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/ble/nrf_ble_gatt",
|
|
93
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/bootloader",
|
|
94
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/fstorage",
|
|
95
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/experimental_section_vars",
|
|
96
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/softdevice/s113/headers",
|
|
97
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/mutex",
|
|
98
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/delay",
|
|
99
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/bootloader/ble_dfu",
|
|
100
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/atomic_fifo",
|
|
101
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/atomic",
|
|
102
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/memobj",
|
|
103
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/fds",
|
|
104
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/atomic_flags",
|
|
105
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/softdevice/s113/headers/nrf52",
|
|
106
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/ble/ble_services/ble_dfu",
|
|
107
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/external/fprintf",
|
|
108
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/svc",
|
|
109
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/libraries/log/src",
|
|
110
|
+
"-Icodal/libraries/codal-microbit-nrf5sdk/nRF5SDK/components/ble/ble_services/ble_dis",
|
|
111
|
+
"-Wno-expansion-to-defined",
|
|
112
|
+
"--target=arm-none-eabi",
|
|
113
|
+
"--sysroot={sysroot}",
|
|
114
|
+
"-Wno-unused-command-line-argument",
|
|
115
|
+
"-mcpu=cortex-m4",
|
|
116
|
+
"-mthumb",
|
|
117
|
+
"-mfpu=fpv4-sp-d16",
|
|
118
|
+
"-mfloat-abi=softfp",
|
|
119
|
+
"-fno-exceptions",
|
|
120
|
+
"-fno-unwind-tables",
|
|
121
|
+
"-ffunction-sections",
|
|
122
|
+
"-fdata-sections",
|
|
123
|
+
"-Wall",
|
|
124
|
+
"-Wextra",
|
|
125
|
+
"-Wno-unused-parameter",
|
|
126
|
+
"-std=c++11",
|
|
127
|
+
"-fwrapv",
|
|
128
|
+
"-fno-rtti",
|
|
129
|
+
"-fno-threadsafe-statics",
|
|
130
|
+
"-fno-exceptions",
|
|
131
|
+
"-fno-unwind-tables",
|
|
132
|
+
"-Wl,--gc-sections",
|
|
133
|
+
"-Wl,--sort-common",
|
|
134
|
+
"-Wl,--sort-section=alignment",
|
|
135
|
+
"-Wno-array-bounds",
|
|
136
|
+
"-include",
|
|
137
|
+
"codal/codal_extra_definitions.h",
|
|
138
|
+
"-Icodal/toolchain/CLANG",
|
|
139
|
+
"-O2",
|
|
140
|
+
"-g",
|
|
141
|
+
"-DNDEBUG",
|
|
142
|
+
"-DAPP_TIMER_V2",
|
|
143
|
+
"-DAPP_TIMER_V2_RTC1_ENABLED",
|
|
144
|
+
"-DNRF_DFU_TRANSPORT_BLE=1",
|
|
145
|
+
"-DNRF52833_XXAA",
|
|
146
|
+
"-DNRF52833",
|
|
147
|
+
"-DTARGET_MCU_NRF52833",
|
|
148
|
+
"-DNRF5",
|
|
149
|
+
"-DNRF52833",
|
|
150
|
+
"-D__CORTEX_M4",
|
|
151
|
+
"-DS113",
|
|
152
|
+
"-DTOOLCHAIN_GCC",
|
|
153
|
+
"-D__START=target_start"
|
|
154
|
+
]
|
|
155
|
+
},
|
|
156
|
+
"link": {
|
|
157
|
+
"tool": "clang++",
|
|
158
|
+
"flagsBefore": [
|
|
159
|
+
"-std=c++11",
|
|
160
|
+
"-fwrapv",
|
|
161
|
+
"-fno-rtti",
|
|
162
|
+
"-fno-threadsafe-statics",
|
|
163
|
+
"-fno-exceptions",
|
|
164
|
+
"-fno-unwind-tables",
|
|
165
|
+
"-Wl,--gc-sections",
|
|
166
|
+
"-Wl,--sort-common",
|
|
167
|
+
"-Wl,--sort-section=alignment",
|
|
168
|
+
"-Wno-array-bounds",
|
|
169
|
+
"--target=arm-none-eabi",
|
|
170
|
+
"--sysroot={sysroot}",
|
|
171
|
+
"-Wno-unused-command-line-argument",
|
|
172
|
+
"-Wl,--gc-sections",
|
|
173
|
+
"-Wl,--wrap,atexit",
|
|
174
|
+
"-mcpu=cortex-m4",
|
|
175
|
+
"-mthumb",
|
|
176
|
+
"-mfpu=fpv4-sp-d16",
|
|
177
|
+
"-mfloat-abi=softfp",
|
|
178
|
+
"-Wl,--start-group",
|
|
179
|
+
"-lm",
|
|
180
|
+
"-lc",
|
|
181
|
+
"-lnosys",
|
|
182
|
+
"-Wl,--end-group",
|
|
183
|
+
"-Tcodal/libraries/codal-microbit-v2/ld/nrf52833-softdevice.ld",
|
|
184
|
+
"-Wl,-Map,build/MICROBIT.map",
|
|
185
|
+
"-Wl,--start-group"
|
|
186
|
+
],
|
|
187
|
+
"flagsAfter": [
|
|
188
|
+
"build/libcodal-microbit-v2.a",
|
|
189
|
+
"build/libcodal-core.a",
|
|
190
|
+
"build/libcodal-nrf52.a",
|
|
191
|
+
"build/libcodal-microbit-nrf5sdk.a",
|
|
192
|
+
"build/libcodal-nrf52.a",
|
|
193
|
+
"build/libcodal-core.a",
|
|
194
|
+
"codal/libraries/codal-microbit-v2/lib/bootloader.o",
|
|
195
|
+
"codal/libraries/codal-microbit-v2/lib/mbr.o",
|
|
196
|
+
"codal/libraries/codal-microbit-v2/lib/settings.o",
|
|
197
|
+
"codal/libraries/codal-microbit-v2/lib/softdevice.o",
|
|
198
|
+
"codal/libraries/codal-microbit-v2/lib/uicr.o",
|
|
199
|
+
"-lnosys",
|
|
200
|
+
"-lm",
|
|
201
|
+
"-lc",
|
|
202
|
+
"-lm",
|
|
203
|
+
"-lc",
|
|
204
|
+
"-Wl,--end-group",
|
|
205
|
+
"-o",
|
|
206
|
+
"build/MICROBIT",
|
|
207
|
+
"-Wl,--print-memory-usage"
|
|
208
|
+
],
|
|
209
|
+
"output": "build/MICROBIT",
|
|
210
|
+
"map": "build/MICROBIT.map"
|
|
211
|
+
},
|
|
212
|
+
"objcopy": {
|
|
213
|
+
"tool": "llvm-objcopy",
|
|
214
|
+
"args": [
|
|
215
|
+
"-O",
|
|
216
|
+
"ihex",
|
|
217
|
+
"build/MICROBIT",
|
|
218
|
+
"-"
|
|
219
|
+
]
|
|
220
|
+
},
|
|
221
|
+
"digest": "40944b38e6fe54080f3fb2636b065f7375e8a66aedb06f890d2bbe7bece13ac1"
|
|
222
|
+
}
|
|
Binary file
|
package/lib/core.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Compiles a user program against the prebuilt CODAL in this package.
|
|
2
|
+
//
|
|
3
|
+
// The recipes come from `codal/manifest.json`, generated from the native build's own command list,
|
|
4
|
+
// so this runs the flags the native toolchain runs.
|
|
5
|
+
|
|
6
|
+
import { createSession, sysroot } from 'microbit-clang-wasm';
|
|
7
|
+
|
|
8
|
+
import { SOURCE, plan } from './recipe.js';
|
|
9
|
+
import { unpackTar } from './tar.js';
|
|
10
|
+
|
|
11
|
+
// C++ only: the manifest carries one compile recipe, taken from a C++ translation unit, so a .c file
|
|
12
|
+
// would be compiled as C++ and fail on valid C. A C recipe means capturing one in the generator.
|
|
13
|
+
const UNSUPPORTED_SOURCE = /\.(c|s|S|asm)$/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {{ loadAsset: (name: string) => Promise<Uint8Array> | Uint8Array }} options
|
|
17
|
+
*/
|
|
18
|
+
export function createCodal({ loadAsset } = {}) {
|
|
19
|
+
if (typeof loadAsset !== 'function') {
|
|
20
|
+
throw new Error('createCodal needs a loadAsset(name) function to read this package\'s codal/ files');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The promise, not its result: two callers arriving before the first finishes would otherwise
|
|
24
|
+
// each build a session. Dropped again if it fails, so a failed asset read can be retried.
|
|
25
|
+
let loaded = null;
|
|
26
|
+
const load = () => (loaded ??= open(loadAsset).catch((error) => {
|
|
27
|
+
loaded = null;
|
|
28
|
+
throw error;
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
// One session means one filesystem at fixed paths, so builds have to be serialised: a second one
|
|
32
|
+
// starting mid-build would delete the first one's sources under it.
|
|
33
|
+
let queue = Promise.resolve();
|
|
34
|
+
|
|
35
|
+
const build = async (files, { onStep, signal } = {}) => {
|
|
36
|
+
signal?.throwIfAborted();
|
|
37
|
+
const { manifest, session } = await load();
|
|
38
|
+
// Checked whether or not there are C++ sources: skipping a .c file that sits beside a .cpp one
|
|
39
|
+
// would report a successful build of only half the program.
|
|
40
|
+
const unsupported = Object.keys(files).filter((name) => UNSUPPORTED_SOURCE.test(name));
|
|
41
|
+
if (unsupported.length) {
|
|
42
|
+
throw new Error(`only C++ sources are supported for now, and these are not: ${unsupported.join(', ')}`);
|
|
43
|
+
}
|
|
44
|
+
const sources = Object.keys(files).filter((name) => SOURCE.test(name));
|
|
45
|
+
if (sources.length === 0) throw new Error('no source file to compile; expected at least one .cpp');
|
|
46
|
+
for (const name of Object.keys(files)) assertWorkspacePath(name);
|
|
47
|
+
|
|
48
|
+
const recipe = plan(manifest, Object.keys(files), { sysroot });
|
|
49
|
+
signal?.throwIfAborted();
|
|
50
|
+
for (const path of recipe.stale) await session.remove(path);
|
|
51
|
+
for (const [name, content] of Object.entries(files)) {
|
|
52
|
+
await session.writeFile(`${manifest.layout.project}/${name}`, content);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const steps = [];
|
|
56
|
+
let hex = null;
|
|
57
|
+
for (const command of recipe.steps) {
|
|
58
|
+
// A whole build is one unbroken chain of microtasks, so an abort arriving as a message or a
|
|
59
|
+
// timer is never delivered while it runs. This hands the event queue back between steps.
|
|
60
|
+
if (signal) await new Promise((resolve) => setTimeout(resolve, 0));
|
|
61
|
+
// Between steps only: a tool already running cannot be interrupted, so it finishes first.
|
|
62
|
+
signal?.throwIfAborted();
|
|
63
|
+
const step = await invoke(session, command, manifest.layout.project);
|
|
64
|
+
steps.push(step);
|
|
65
|
+
onStep?.(step);
|
|
66
|
+
if (step.exitCode !== 0) return failure(steps);
|
|
67
|
+
if (command.captureStdout) hex = step.stdout;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const map = await session.readFile(recipe.map);
|
|
71
|
+
return {
|
|
72
|
+
ok: true,
|
|
73
|
+
hex: new TextDecoder().decode(hex),
|
|
74
|
+
map: map === null ? null : new TextDecoder().decode(map),
|
|
75
|
+
output: steps.map((step) => step.stderr).join(''),
|
|
76
|
+
steps,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
async manifest() {
|
|
82
|
+
return (await load()).manifest;
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {Record<string, string | Uint8Array>} files user sources and headers, workspace-relative
|
|
87
|
+
* @param {{ onStep?: (step: object) => void, signal?: AbortSignal }} options
|
|
88
|
+
*/
|
|
89
|
+
compile(files, options) {
|
|
90
|
+
const result = queue.then(() => build(files, options));
|
|
91
|
+
queue = result.then(() => {}, () => {}); // a failed build must not poison the queue
|
|
92
|
+
return result;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Names go into a virtual filesystem that resolves `..` and an empty segment to nothing, so a path
|
|
98
|
+
// clang then cannot open. Rejecting here says which file, rather than leaving a puzzling error.
|
|
99
|
+
function assertWorkspacePath(name) {
|
|
100
|
+
const bad =
|
|
101
|
+
name.startsWith('/') ? 'is absolute'
|
|
102
|
+
: name.includes('\\') ? 'uses backslashes'
|
|
103
|
+
: name.split('/').some((part) => part === '' || part === '.' || part === '..') ? 'has an empty or relative segment'
|
|
104
|
+
: null;
|
|
105
|
+
if (bad) throw new Error(`file name ${JSON.stringify(name)} ${bad}; give a path relative to the workspace`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// One session for the life of the package: it holds the unpacked sysroot and the CODAL payload, so
|
|
109
|
+
// only the first build pays for putting a filesystem together.
|
|
110
|
+
async function open(loadAsset) {
|
|
111
|
+
const manifest = JSON.parse(new TextDecoder().decode(await loadAsset('codal/manifest.json')));
|
|
112
|
+
const session = createSession();
|
|
113
|
+
await session.writeTree(unpackTar(await loadAsset('codal/payload.tar')));
|
|
114
|
+
return { manifest, session };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Only the capturing step keeps its stdout; the others would hold a copy of the hex for nothing.
|
|
118
|
+
// Diagnostics name files as the caller did, so the virtual project prefix comes off them.
|
|
119
|
+
async function invoke(session, { tool, args, captureStdout }, project) {
|
|
120
|
+
const chunks = [];
|
|
121
|
+
const decoder = new TextDecoder();
|
|
122
|
+
let stderr = '';
|
|
123
|
+
|
|
124
|
+
const exitCode = await session.run([tool, ...args], {
|
|
125
|
+
stdout: captureStdout ? (bytes) => bytes && chunks.push(Uint8Array.from(bytes)) : null,
|
|
126
|
+
stderr: (bytes) => bytes && (stderr += decoder.decode(bytes, { stream: true })),
|
|
127
|
+
});
|
|
128
|
+
stderr = stripProject(stderr + decoder.decode(), project);
|
|
129
|
+
return { tool, args, exitCode, stderr, stdout: captureStdout ? join(chunks) : null };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Only where a path starts is the prefix ours: a caller whose own file sits under `project/source/`
|
|
133
|
+
// gets it twice in the virtual path, and removing both would name a file they do not have.
|
|
134
|
+
function stripProject(text, project) {
|
|
135
|
+
const escaped = project.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
136
|
+
return text.replace(new RegExp(`(^|\\s)${escaped}/`, 'gm'), '$1');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function failure(steps) {
|
|
140
|
+
return { ok: false, hex: null, map: null, output: steps.map((step) => step.stderr).join(''), steps };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function join(chunks) {
|
|
144
|
+
const out = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
|
|
145
|
+
let at = 0;
|
|
146
|
+
for (const chunk of chunks) {
|
|
147
|
+
out.set(chunk, at);
|
|
148
|
+
at += chunk.length;
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User files by workspace-relative path. **Key order is link order**, and the objects are numbered
|
|
3
|
+
* by it, so a caller that wants a reproducible binary has to hand them over in a fixed order.
|
|
4
|
+
*/
|
|
5
|
+
export type Files = Record<string, string | Uint8Array>;
|
|
6
|
+
|
|
7
|
+
export type AssetLoader = (name: string) => Promise<Uint8Array> | Uint8Array;
|
|
8
|
+
|
|
9
|
+
export type Step = {
|
|
10
|
+
tool: string;
|
|
11
|
+
args: string[];
|
|
12
|
+
exitCode: number;
|
|
13
|
+
stderr: string;
|
|
14
|
+
/** Only the step whose output is the hex keeps it; null everywhere else. */
|
|
15
|
+
stdout: Uint8Array | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type Result = {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
/** Intel hex, or null if a step failed. */
|
|
21
|
+
hex: string | null;
|
|
22
|
+
map: string | null;
|
|
23
|
+
/** Every step's stderr, in order. */
|
|
24
|
+
output: string;
|
|
25
|
+
steps: Step[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type CompileOptions = {
|
|
29
|
+
/** Called as each tool finishes; on a failure the last step reported is the one that failed. */
|
|
30
|
+
onStep?: (step: Step) => void;
|
|
31
|
+
/** Aborting rejects with the signal's reason between steps; a tool already running finishes first. */
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type Compile = (files: Files, options?: CompileOptions) => Promise<Result>;
|
|
36
|
+
|
|
37
|
+
export type Manifest = {
|
|
38
|
+
schema: number;
|
|
39
|
+
generated: string;
|
|
40
|
+
digest: string;
|
|
41
|
+
codal: {
|
|
42
|
+
version: string;
|
|
43
|
+
versionHash: string;
|
|
44
|
+
samples: { url: string | null, commit: string | null };
|
|
45
|
+
libraries: { name: string, url: string | null, commit: string | null }[];
|
|
46
|
+
};
|
|
47
|
+
toolchain: {
|
|
48
|
+
package: string;
|
|
49
|
+
version: string;
|
|
50
|
+
llvm: { version: string, repository: string, release: string, commit: string };
|
|
51
|
+
};
|
|
52
|
+
config: Record<string, string | number>;
|
|
53
|
+
layout: { build: string, project: string };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type Codal = {
|
|
57
|
+
compile: Compile;
|
|
58
|
+
manifest: () => Promise<Manifest>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** `loadAsset` is given a package-relative path, such as `codal/payload.tar`. */
|
|
62
|
+
export function createCodal(options: { loadAsset: AssetLoader }): Codal;
|
|
63
|
+
|
|
64
|
+
export function packTar(entries: Record<string, Uint8Array>): Uint8Array;
|
|
65
|
+
export function unpackTar(tar: Uint8Array): object;
|
|
66
|
+
export function flatten(tree: object, base?: string): Record<string, Uint8Array>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Browser and bundler entry point. The host supplies the two assets, because there is no filesystem
|
|
2
|
+
// to read them from: see lib/node.js for what that looks like when there is one.
|
|
3
|
+
|
|
4
|
+
export { createCodal } from './core.js';
|
|
5
|
+
export { packTar, unpackTar, flatten } from './tar.js';
|
package/lib/node.d.ts
ADDED
package/lib/node.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Node entry point: the assets come off disk, so nothing is fetched and nothing has to be supplied.
|
|
2
|
+
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
|
|
5
|
+
import { createCodal } from './core.js';
|
|
6
|
+
|
|
7
|
+
export * from './index.js';
|
|
8
|
+
|
|
9
|
+
let codal = null;
|
|
10
|
+
const shared = () => (codal ??= createCodal({ loadAsset: (name) => readFile(new URL(`../${name}`, import.meta.url)) }));
|
|
11
|
+
|
|
12
|
+
/** @type {import('./index.js').Compile} */
|
|
13
|
+
export const compile = (files, options) => shared().compile(files, options);
|
|
14
|
+
|
|
15
|
+
export const manifest = () => shared().manifest();
|
package/lib/recipe.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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.
|
|
5
|
+
|
|
6
|
+
const SYSROOT_TOKEN = '{sysroot}';
|
|
7
|
+
|
|
8
|
+
export const SOURCE = /\.(cc|cpp|cxx)$/;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} manifest from codal/manifest.json
|
|
12
|
+
* @param {string[]} names every user file, workspace-relative, sources in link order
|
|
13
|
+
* @param {{ sysroot: string }} options where the toolchain's libraries and headers are mounted
|
|
14
|
+
*/
|
|
15
|
+
export function plan(manifest, names, { sysroot }) {
|
|
16
|
+
const fill = (args) => args.map((arg) => arg.replace(SYSROOT_TOKEN, sysroot));
|
|
17
|
+
const { project, build } = manifest.layout;
|
|
18
|
+
const sources = names.filter((name) => SOURCE.test(name));
|
|
19
|
+
|
|
20
|
+
// Indexed, because flattening the path would collide `a/b.cpp` with `a_b.cpp` and link one object
|
|
21
|
+
// twice.
|
|
22
|
+
const objects = sources.map((name, index) => `${build}/user/${index}-${name.split('/').pop()}.obj`);
|
|
23
|
+
const includes = headerDirectories(names).map((dir) => `-I${dir ? `${project}/${dir}` : project}`);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
steps: [
|
|
27
|
+
...sources.map((name, index) => ({
|
|
28
|
+
tool: manifest.compile.tool,
|
|
29
|
+
args: [...fill(manifest.compile.flags), ...includes, '-c', `${project}/${name}`, '-o', objects[index]],
|
|
30
|
+
})),
|
|
31
|
+
{
|
|
32
|
+
tool: manifest.link.tool,
|
|
33
|
+
args: [...fill(manifest.link.flagsBefore), ...objects, ...fill(manifest.link.flagsAfter)],
|
|
34
|
+
},
|
|
35
|
+
// objcopy writes the hex to stdout: wasi-libc has no chmod, and objcopy otherwise fails
|
|
36
|
+
// mirroring the input's permissions onto a file it has already written correctly.
|
|
37
|
+
{ tool: manifest.objcopy.tool, args: fill(manifest.objcopy.args), captureStdout: true },
|
|
38
|
+
],
|
|
39
|
+
// Cleared before a build, so nothing survives a run that failed halfway.
|
|
40
|
+
stale: [project.split('/')[0], `${build}/user`, manifest.link.output, manifest.link.map],
|
|
41
|
+
map: manifest.link.map,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// The native build's rule (CMake RECURSIVE_FIND_DIR in microbit-v2-samples): every directory holding
|
|
46
|
+
// a .h, then every one holding a .hpp, without duplicates. '' is the project root.
|
|
47
|
+
function headerDirectories(names) {
|
|
48
|
+
const directoriesOf = (pattern) =>
|
|
49
|
+
[...new Set(names.filter((name) => pattern.test(name)).map((name) => name.slice(0, Math.max(0, name.lastIndexOf('/')))))].sort();
|
|
50
|
+
const h = directoriesOf(/\.h$/);
|
|
51
|
+
return [...h, ...directoriesOf(/\.hpp$/).filter((dir) => !h.includes(dir))];
|
|
52
|
+
}
|
package/lib/tar.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// A minimal ustar reader and writer, so the CODAL payload travels as one asset: a host that cannot
|
|
2
|
+
// read a directory would otherwise fetch a thousand headers one at a time. Hand-rolled because
|
|
3
|
+
// nanotar, already in the tree, writes only the 100-byte name field — too short for CODAL's paths.
|
|
4
|
+
|
|
5
|
+
const BLOCK = 512;
|
|
6
|
+
const NAME = 100;
|
|
7
|
+
const PREFIX = 155;
|
|
8
|
+
|
|
9
|
+
/** @param {Record<string, Uint8Array>} entries paths to contents @returns {Uint8Array} */
|
|
10
|
+
export function packTar(entries) {
|
|
11
|
+
const names = Object.keys(entries).sort();
|
|
12
|
+
const blocks = names.reduce((total, name) => total + 1 + Math.ceil(entries[name].length / BLOCK), 2);
|
|
13
|
+
|
|
14
|
+
const out = new Uint8Array(blocks * BLOCK);
|
|
15
|
+
let at = 0;
|
|
16
|
+
for (const name of names) {
|
|
17
|
+
out.set(header(name, entries[name].length), at);
|
|
18
|
+
at += BLOCK;
|
|
19
|
+
out.set(entries[name], at);
|
|
20
|
+
at += Math.ceil(entries[name].length / BLOCK) * BLOCK;
|
|
21
|
+
}
|
|
22
|
+
return out; // the two zero blocks that end the archive are already zero
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @param {Uint8Array} tar @returns {object} a tree of nested objects with Uint8Array leaves */
|
|
26
|
+
export function unpackTar(tar) {
|
|
27
|
+
const tree = Object.create(null);
|
|
28
|
+
for (let at = 0; at + BLOCK <= tar.length; ) {
|
|
29
|
+
const block = tar.subarray(at, at + BLOCK);
|
|
30
|
+
if (block.every((byte) => byte === 0)) break;
|
|
31
|
+
verifyChecksum(block, at);
|
|
32
|
+
|
|
33
|
+
const name = text(block, 0, NAME);
|
|
34
|
+
const prefix = text(block, 345, PREFIX);
|
|
35
|
+
const size = parseInt(text(block, 124, 12) || '0', 8);
|
|
36
|
+
const type = String.fromCharCode(block[156]);
|
|
37
|
+
// A size the archive cannot hold means truncation or corruption; a negative one would walk
|
|
38
|
+
// backwards for ever.
|
|
39
|
+
if (!Number.isSafeInteger(size) || size < 0 || at + BLOCK + size > tar.length) {
|
|
40
|
+
throw new Error(`tar entry at ${at} declares ${size} bytes, which the archive does not hold`);
|
|
41
|
+
}
|
|
42
|
+
at += BLOCK;
|
|
43
|
+
|
|
44
|
+
const full = prefix ? `${prefix}/${name}` : name;
|
|
45
|
+
if (type === '0' || type === '\0') {
|
|
46
|
+
// A view, not a copy: these become the session's files, so copying would duplicate 25 MB.
|
|
47
|
+
insert(tree, full, tar.subarray(at, at + size));
|
|
48
|
+
} else if (type !== '5') {
|
|
49
|
+
throw new Error(`unsupported tar entry type '${type}' for ${full}`);
|
|
50
|
+
}
|
|
51
|
+
at += Math.ceil(size / BLOCK) * BLOCK;
|
|
52
|
+
}
|
|
53
|
+
return tree;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @param {object} tree @returns {Record<string, Uint8Array>} flat paths, for packTar */
|
|
57
|
+
export function flatten(tree, base = '') {
|
|
58
|
+
const out = {};
|
|
59
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
60
|
+
const full = base ? `${base}/${key}` : key;
|
|
61
|
+
if (value instanceof Uint8Array) out[full] = value;
|
|
62
|
+
else Object.assign(out, flatten(value, full));
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function header(name, size) {
|
|
68
|
+
const block = new Uint8Array(BLOCK);
|
|
69
|
+
const [prefix, base] = split(name);
|
|
70
|
+
|
|
71
|
+
write(block, base, 0, NAME);
|
|
72
|
+
write(block, '000644 ', 100, 8);
|
|
73
|
+
write(block, '000000 ', 108, 8);
|
|
74
|
+
write(block, '000000 ', 116, 8);
|
|
75
|
+
write(block, size.toString(8).padStart(11, '0') + ' ', 124, 12);
|
|
76
|
+
write(block, '00000000000 ', 136, 12); // mtime 0: the archive has to be reproducible
|
|
77
|
+
write(block, ' ', 148, 8); // checksum field counts as spaces while summing
|
|
78
|
+
block[156] = '0'.charCodeAt(0);
|
|
79
|
+
write(block, 'ustar', 257, 6);
|
|
80
|
+
write(block, '00', 263, 2);
|
|
81
|
+
write(block, prefix, 345, PREFIX);
|
|
82
|
+
|
|
83
|
+
const sum = block.reduce((total, byte) => total + byte, 0);
|
|
84
|
+
write(block, sum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
|
|
85
|
+
return block;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ustar splits a long path into a 155-byte prefix and a 100-byte name, at a slash.
|
|
89
|
+
function split(name) {
|
|
90
|
+
if (name.length <= NAME) return ['', name];
|
|
91
|
+
for (let at = name.length - NAME - 1; at < name.length; at++) {
|
|
92
|
+
if (name[at] === '/' && name.length - at - 1 <= NAME && at <= PREFIX) {
|
|
93
|
+
return [name.slice(0, at), name.slice(at + 1)];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
throw new Error(`path too long for a ustar header: ${name}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The header sums its own bytes with the checksum field read as spaces; a mismatch means the block
|
|
100
|
+
// is not a header at all, so stopping here beats reading garbage sizes off it.
|
|
101
|
+
function verifyChecksum(block, at) {
|
|
102
|
+
const stored = parseInt(text(block, 148, 8), 8);
|
|
103
|
+
let sum = 0;
|
|
104
|
+
for (let i = 0; i < BLOCK; i++) sum += i >= 148 && i < 156 ? 0x20 : block[i];
|
|
105
|
+
if (sum !== stored) throw new Error(`tar header at ${at} fails its checksum; the archive is corrupt or misaligned`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function write(block, value, offset, length) {
|
|
109
|
+
const bytes = new TextEncoder().encode(value);
|
|
110
|
+
if (bytes.length > length) throw new Error(`field too long for a ustar header: ${value}`);
|
|
111
|
+
block.set(bytes, offset);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function text(block, offset, length) {
|
|
115
|
+
const field = block.subarray(offset, offset + length);
|
|
116
|
+
const end = field.indexOf(0);
|
|
117
|
+
return new TextDecoder().decode(end === -1 ? field : field.subarray(0, end)).trim();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Directories are prototype-free: an archive holding `__proto__/x` would otherwise walk into
|
|
121
|
+
// Object.prototype and write there instead.
|
|
122
|
+
function insert(tree, name, data) {
|
|
123
|
+
const parts = name.split('/');
|
|
124
|
+
let node = tree;
|
|
125
|
+
for (const part of parts.slice(0, -1)) node = node[part] ??= Object.create(null);
|
|
126
|
+
node[parts[parts.length - 1]] = data;
|
|
127
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "microbit-clang-wasm-codal",
|
|
3
|
+
"version": "0.305.0-alpha.1",
|
|
4
|
+
"description": "Compile BBC micro:bit C++ programs to a flashable hex in a browser or Node, with prebuilt CODAL and no toolchain to install",
|
|
5
|
+
"author": "Carlos Pereira Atencio",
|
|
6
|
+
"license": "SEE LICENSE IN LICENSES",
|
|
7
|
+
"homepage": "https://github.com/carlosperate/microbit-clang-wasm-codal",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/carlosperate/microbit-clang-wasm-codal.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/carlosperate/microbit-clang-wasm-codal/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=20"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib/*.js",
|
|
21
|
+
"lib/*.d.ts",
|
|
22
|
+
"codal/manifest.json",
|
|
23
|
+
"codal/payload.tar",
|
|
24
|
+
"LICENSES",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"node": {
|
|
30
|
+
"types": "./lib/node.d.ts",
|
|
31
|
+
"default": "./lib/node.js"
|
|
32
|
+
},
|
|
33
|
+
"default": {
|
|
34
|
+
"types": "./lib/index.d.ts",
|
|
35
|
+
"default": "./lib/index.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"microbit-clang-wasm": "21.11.0-alpha.1"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"configure": "node tools/configure-harness.mjs",
|
|
45
|
+
"generate": "node tools/generate.mjs",
|
|
46
|
+
"test": "node --test --test-concurrency=1 test/*.test.mjs"
|
|
47
|
+
},
|
|
48
|
+
"codal": {
|
|
49
|
+
"buildSystem": {
|
|
50
|
+
"repository": "carlosperate/microbit-v2-samples",
|
|
51
|
+
"ref": "ca73d81ad67c13091d681d6ffe4da3ebd36db97e"
|
|
52
|
+
},
|
|
53
|
+
"codalJson": {
|
|
54
|
+
"target": {
|
|
55
|
+
"name": "codal-microbit-v2",
|
|
56
|
+
"url": "https://github.com/lancaster-university/codal-microbit-v2",
|
|
57
|
+
"branch": "v0.3.5",
|
|
58
|
+
"type": "git"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|