@geastack/cli 0.1.103 → 0.1.105
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/docs/DEVICE-CONTROL.md +12 -0
- package/docs/LINUX-NATIVE-DEPLOYMENT.md +154 -0
- package/docs/SPEC.md +11 -1
- package/package.json +2 -2
- package/src/boards/config.mjs +6 -2
- package/src/boards/resolve.mjs +1 -0
- package/src/boards/usb.mjs +16 -12
- package/src/build-config.mjs +42 -5
- package/src/commands/apps.mjs +7 -0
- package/src/commands/board.mjs +43 -5
- package/src/commands/boards.mjs +18 -1
- package/src/commands/doctor.mjs +2 -1
- package/src/device/serial.mjs +195 -29
- package/src/esp32/build.mjs +104 -34
- package/src/esp32/capabilities.mjs +6 -0
- package/src/esp32/dependencies.mjs +36 -0
- package/src/esp32/flash.mjs +7 -2
- package/src/esp32/idf-env.mjs +10 -1
- package/src/esp32/idf-version.mjs +2 -1
- package/src/esp32/native-css-features.mjs +3 -1
- package/src/esp32/partitions.mjs +4 -1
- package/src/gea.mjs +9 -0
- package/src/linux/adapter.mjs +23 -0
- package/src/linux/deploy.mjs +135 -0
- package/src/linux/transaction.py +216 -0
- package/src/manifest.mjs +33 -2
- package/src/native-link.mjs +37 -0
- package/src/native-resources.mjs +36 -0
- package/src/rp2350/adapter.mjs +4 -4
- package/src/run.mjs +8 -0
- package/src/serial-devices.mjs +18 -6
- package/src/setup-wizard.mjs +91 -12
- package/src/toolchain.mjs +23 -3
- package/src/windows/adapter.mjs +22 -8
package/src/esp32/partitions.mjs
CHANGED
|
@@ -95,7 +95,10 @@ export function buildFlashSettings(buildDir, fallbackSize) {
|
|
|
95
95
|
// flash_args once a build has configured; fall back to the chip defaults.
|
|
96
96
|
export function flashOffsetForBuildImage(buildDir, imagePath, fallback) {
|
|
97
97
|
const flashArgs = path.join(buildDir, 'flash_args')
|
|
98
|
-
|
|
98
|
+
// flash_args spells paths with `/` on every host; a Windows `bootloader\...`
|
|
99
|
+
// never matched, so the bootloader fell back to 0x0 on chips that boot from
|
|
100
|
+
// 0x2000 (ESP32-S31, P4, C5...) and the board came up with nothing to run.
|
|
101
|
+
const relative = (imagePath.startsWith(`${buildDir}${path.sep}`) ? imagePath.slice(buildDir.length + 1) : imagePath).replaceAll('\\', '/')
|
|
99
102
|
if (existsSync(flashArgs)) {
|
|
100
103
|
for (const line of readFileSync(flashArgs, 'utf8').split(/\r?\n/)) {
|
|
101
104
|
const fields = line.trim().split(/\s+/)
|
package/src/gea.mjs
CHANGED
|
@@ -101,6 +101,14 @@ export async function runGea(argv, io = {}) {
|
|
|
101
101
|
|
|
102
102
|
// Xbox is both a platform and a concrete built-in UWP target.
|
|
103
103
|
const target = option(parsed, 'target', '')
|
|
104
|
+
// Raspberry Pi OS builds a real SDL native executable. Running/deploying is
|
|
105
|
+
// intentionally not implicit in this build-only platform adapter.
|
|
106
|
+
if (command === 'build' && target === 'raspberry-pi-os') {
|
|
107
|
+
const app = parsed.options.app || rest[0] ? findAppById(ctx, String(parsed.options.app || rest[0])) : findCurrentApp(cwd)
|
|
108
|
+
if (!app?.targets?.['raspberry-pi-os']) fail(app ? `'${app.id}' does not declare gea.targets.raspberry-pi-os.` : 'No app selected.', ExitCode.usage)
|
|
109
|
+
const { runLinux } = await import('./linux/adapter.mjs')
|
|
110
|
+
return runLinux({ app, env, dryRun: flag(parsed, 'dry-run'), stdout })
|
|
111
|
+
}
|
|
104
112
|
// macOS never reaches board selection: it has no alias and no catalog entry,
|
|
105
113
|
// so an app declaring `targets.macos` is the whole configuration -- and on a
|
|
106
114
|
// Mac a bare `gea build` inside such an app's folder is the command a user
|
|
@@ -294,6 +302,7 @@ ${heading('Run on this machine (no board needed):')}
|
|
|
294
302
|
gea build --target web [app] [--out-dir d] build the app as a real web app
|
|
295
303
|
|
|
296
304
|
${heading('Native desktop, mobile and watch apps (scripts ship in @geastack/apple, @geastack/windows and @geastack/pebble):')}
|
|
305
|
+
gea build --target raspberry-pi-os [app] build an SDL Linux native executable (no launch)
|
|
297
306
|
gea build --target macos [app] build a Mac .app (a bare gea build does this on a Mac for a Mac-only app)
|
|
298
307
|
gea build --target ios [app] [--mode simulator|device] generate the Xcode project and build the iOS .app
|
|
299
308
|
gea run --target ios [app] [--mode simulator|device] build, then install and launch on a simulator or iPhone
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { existsSync } from 'node:fs'
|
|
4
|
+
import { spawnSync } from 'node:child_process'
|
|
5
|
+
import { nativeScriptBuildEnv } from '../build-config.mjs'
|
|
6
|
+
import { CliError, ExitCode, fail } from '../errors.mjs'
|
|
7
|
+
import { formatCommand } from '../run.mjs'
|
|
8
|
+
|
|
9
|
+
// A native SDL platform build, not board selection or a web simulator.
|
|
10
|
+
export function runLinux({ app, env, dryRun = false, stdout }) {
|
|
11
|
+
const require = createRequire(path.join(app.root, 'package.json'))
|
|
12
|
+
let root = env.GEA_LINUX_ROOT ? path.resolve(env.GEA_LINUX_ROOT) : ''
|
|
13
|
+
try { if (!root) root = path.dirname(require.resolve('@geastack/linux/package.json')) }
|
|
14
|
+
catch { fail(`'${app.id}' targets raspberry-pi-os but @geastack/linux is not installed in ${app.root}.`, ExitCode.missingDependency) }
|
|
15
|
+
const script = path.join(root, 'targets/raspberry-pi-os/build-raspberry-pi-os.sh')
|
|
16
|
+
if (!existsSync(script)) fail(`Linux native build script not found: ${script}`, ExitCode.missingDependency)
|
|
17
|
+
if (dryRun) { stdout(formatCommand(['bash', script, app.id])); return 0 }
|
|
18
|
+
const childEnv = nativeScriptBuildEnv(app, 'raspberry-pi-os', env)
|
|
19
|
+
const result = spawnSync('bash', [script, app.id], { cwd: app.root, env: childEnv, stdio: 'inherit' })
|
|
20
|
+
if (result.error) throw result.error
|
|
21
|
+
if (result.status !== 0) throw new CliError(`Linux native build failed (${result.status ?? 1}).`, ExitCode.deployFailed)
|
|
22
|
+
return 0
|
|
23
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import {createHash, randomUUID} from 'node:crypto'
|
|
4
|
+
import {spawnSync} from 'node:child_process'
|
|
5
|
+
import {runLinux} from './adapter.mjs'
|
|
6
|
+
|
|
7
|
+
const sha = bytes => createHash('sha256').update(bytes).digest('hex')
|
|
8
|
+
const identifier = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,95}$/
|
|
9
|
+
const digest = /^[a-f0-9]{64}$/
|
|
10
|
+
function refuse(message) { throw new Error(`Linux deployment: ${message}`) }
|
|
11
|
+
function regular(file, {privateKey=false}={}) {
|
|
12
|
+
if(!path.isAbsolute(file)) refuse('an explicit absolute local file is required')
|
|
13
|
+
const stat=fs.lstatSync(file)
|
|
14
|
+
if(!stat.isFile()||stat.isSymbolicLink()||(stat.mode&0o022)||stat.uid!==process.getuid()) refuse(`unsafe local file: ${file}`)
|
|
15
|
+
if(privateKey&&(stat.mode&0o077)) refuse('SSH identity must be private')
|
|
16
|
+
return fs.readFileSync(file)
|
|
17
|
+
}
|
|
18
|
+
// Machine-local registration is the authority. --host/ambient SSH configuration
|
|
19
|
+
// never substitutes a destination or key for this entry.
|
|
20
|
+
export function validateLinuxBoard(board,{transportOnly=false}={}) {
|
|
21
|
+
if(typeof process.getuid!=='function') refuse('native deployment currently requires a POSIX client')
|
|
22
|
+
if(board?.target!=='raspberry-pi-os'||board?.appPlatform!=='raspberry-pi-os'||board?.adapter!=='linux-native') refuse('target raspberry-pi-os and adapter linux-native required')
|
|
23
|
+
const s=board.transports?.ssh,d=board.linuxDeployment
|
|
24
|
+
if(!s||!d) refuse('registered SSH and linuxDeployment configuration required')
|
|
25
|
+
if(typeof s.host!=='string'||!s.host||s.host.length>253||!/^[A-Za-z0-9.:[\]-]+$/.test(s.host)||s.host.startsWith('-')) refuse('invalid registered SSH host')
|
|
26
|
+
if(!/^[a-z_][a-z0-9_-]{0,31}$/.test(s.user||'')) refuse('invalid registered SSH user')
|
|
27
|
+
if(!Number.isInteger(s.port)||s.port<1||s.port>65535) refuse('explicit SSH port required')
|
|
28
|
+
if(!identifier.test(s.hostKeyAlias||'')) refuse('explicit HostKeyAlias required')
|
|
29
|
+
const known=regular(s.knownHosts).toString('utf8').split(/\r?\n/).filter(line=>line.trim()&&!line.startsWith('#'))
|
|
30
|
+
if(known.length!==1) refuse('known_hosts must contain exactly one pinned identity')
|
|
31
|
+
const fields=known[0].trim().split(/\s+/)
|
|
32
|
+
if(fields.length!==3||fields[0]!==s.hostKeyAlias||!['ssh-ed25519','ssh-rsa','ecdsa-sha2-nistp256'].includes(fields[1])||!/^[A-Za-z0-9+/]+={0,2}$/.test(fields[2])) refuse('known_hosts identity does not match HostKeyAlias')
|
|
33
|
+
regular(s.identityFile,{privateKey:true})
|
|
34
|
+
if(!/^\/[A-Za-z0-9_./-]+$/.test(d.releaseRoot||'')||d.releaseRoot.includes('/../')||d.releaseRoot==='/'||path.posix.normalize(d.releaseRoot)!==d.releaseRoot||d.releaseRoot.endsWith('/')) refuse('explicit absolute releaseRoot required')
|
|
35
|
+
if(transportOnly)return {ssh:s,deployment:d}
|
|
36
|
+
const hook=regular(d.hookSource)
|
|
37
|
+
if(!digest.test(d.hookSha256||'')||sha(hook)!==d.hookSha256) refuse('reviewed transaction hook SHA mismatch')
|
|
38
|
+
if(!d.profile||typeof d.profile!=='object'||Array.isArray(d.profile)) refuse('explicit transaction profile required')
|
|
39
|
+
if(d.profile.audioOff!==true||d.profile.quiet!==true||typeof d.profile.stateDirectory!=='string'||!d.profile.stateDirectory.startsWith('/')) refuse('explicit quiet profile and absolute state directory required')
|
|
40
|
+
const phases=['prepare','quiesce','backup','activate','verify','protect','observe','rollback','inspect']
|
|
41
|
+
if(!d.profile.phaseTimeoutSeconds||Object.keys(d.profile.phaseTimeoutSeconds).sort().join(',')!==[...phases].sort().join(',')||phases.some(name=>!Number.isInteger(d.profile.phaseTimeoutSeconds[name])||d.profile.phaseTimeoutSeconds[name]<1||d.profile.phaseTimeoutSeconds[name]>600))refuse('explicit bounded per-phase timeouts required')
|
|
42
|
+
if(!Number.isInteger(d.profile.transferTimeoutSeconds)||d.profile.transferTimeoutSeconds<1||d.profile.transferTimeoutSeconds>300)refuse('explicit bounded transfer timeout required')
|
|
43
|
+
const p=d.profile
|
|
44
|
+
if(path.posix.normalize(p.stateDirectory)!==p.stateDirectory||p.stateDirectory==='/') refuse('canonical state directory required')
|
|
45
|
+
if(!/^[A-Za-z0-9][A-Za-z0-9_.@-]{0,127}\.service$/.test(p.service||'')) refuse('explicit service unit required')
|
|
46
|
+
if(!p.storage||!/^\/[A-Za-z0-9_./-]+$/.test(p.storage.program||'')||!digest.test(p.storage.sha256||'')||path.posix.normalize(p.storage.program)!==p.storage.program) refuse('explicit pinned storage program required')
|
|
47
|
+
const session=p.session
|
|
48
|
+
if(!session||!Number.isInteger(session.uid)||session.uid<0||session.runtimeDirectory!==`/run/user/${session.uid}`||!['x11','wayland'].includes(session.type)||typeof session.display!=='string'||!session.display||/[\r\n\0]/.test(session.display)) refuse('explicit graphical session profile required')
|
|
49
|
+
return {ssh:s,deployment:d,hook}
|
|
50
|
+
}
|
|
51
|
+
export function sshArguments(s) {
|
|
52
|
+
return ['-F','/dev/null','-T','-o','BatchMode=yes','-o','StrictHostKeyChecking=yes','-o',`HostKeyAlias=${s.hostKeyAlias}`,'-o',`UserKnownHostsFile=${s.knownHosts}`,'-o','GlobalKnownHostsFile=/dev/null','-o','IdentitiesOnly=yes','-o','PasswordAuthentication=no','-o','KbdInteractiveAuthentication=no','-o','ProxyCommand=none','-o','ProxyJump=none','-o','ClearAllForwardings=yes','-o','ControlMaster=no','-o','ControlPath=none','-o','ConnectTimeout=10','-o','ServerAliveInterval=5','-o','ServerAliveCountMax=2','-i',s.identityFile,'-p',String(s.port),'--',`${s.user}@${s.host}`]
|
|
53
|
+
}
|
|
54
|
+
export function verifyLinuxExecutable(file,env,execute=spawnSync) {
|
|
55
|
+
const readelf=env.GEA_NATIVE_READELF,nm=env.GEA_NATIVE_NM
|
|
56
|
+
for(const tool of [readelf,nm]) if(typeof tool!=='string'||!path.isAbsolute(tool)||!fs.statSync(tool).isFile()) refuse('explicit native readelf/nm tools required')
|
|
57
|
+
const run=(tool,args)=>{const r=execute(tool,args,{encoding:'utf8',timeout:15000,maxBuffer:16*1024*1024});if(r.error||r.status!==0)refuse('native ELF inspection failed');return r.stdout}
|
|
58
|
+
const header=run(readelf,['-h','-l',file]),dynamic=run(readelf,['-d',file]),symbols=run(nm,['-a','-C',file])
|
|
59
|
+
if(!/Class:\s+ELF64/.test(header)||!/Data:.*little endian/i.test(header)||!/Machine:\s+AArch64/.test(header)||!/\bINTERP\b/.test(header)) refuse('native executable architecture/interpreter refused')
|
|
60
|
+
if(!/Shared library: \[libSDL2[^\]]*\]/.test(dynamic)||/webkit|webview|javascriptcore/i.test(dynamic+symbols)) refuse('SDL renderer/no-WebView linkage refused')
|
|
61
|
+
for(const symbol of ['rpios_display_present','gea::platform::display::Display::flush()']) if(!symbols.split('\n').some(line=>/\b[Tt]\s/.test(line)&&line.endsWith(symbol))) refuse('actual SDL presentation producer absent')
|
|
62
|
+
for(const symbol of ['SDL_CreateWindow','SDL_CreateRenderer','SDL_RenderPresent']) if(!new RegExp('\\bU\\s+'+symbol+'(?:@[^\\s]+)?(?:\\s|$)').test(symbols)) refuse('actual SDL imports absent')
|
|
63
|
+
return {headerSha256:sha(Buffer.from(header)),dynamicSha256:sha(Buffer.from(dynamic)),symbolsSha256:sha(Buffer.from(symbols))}
|
|
64
|
+
}
|
|
65
|
+
export function releasePayload(app) {
|
|
66
|
+
if(!identifier.test(app.id)) refuse('invalid app id')
|
|
67
|
+
const root=path.join(app.root,'dist/raspberry-pi-os',app.id), files=[]
|
|
68
|
+
let total=0
|
|
69
|
+
function add(name) {
|
|
70
|
+
const file=path.join(root,name),before=fs.lstatSync(file)
|
|
71
|
+
if(!before.isFile()||before.isSymbolicLink()) refuse(`nonregular release file: ${name}`)
|
|
72
|
+
const fd=fs.openSync(file,fs.constants.O_RDONLY|fs.constants.O_NOFOLLOW)
|
|
73
|
+
let st,data
|
|
74
|
+
try {st=fs.fstatSync(fd);if(!st.isFile()||st.dev!==before.dev||st.ino!==before.ino||st.size>64*1024*1024)refuse(`nonregular/oversized release file: ${name}`);data=fs.readFileSync(fd);const after=fs.fstatSync(fd);if(data.length!==st.size||after.size!==st.size||after.mtimeMs!==st.mtimeMs)refuse('release changed while reading')} finally {fs.closeSync(fd)}
|
|
75
|
+
total+=data.length
|
|
76
|
+
if(total>128*1024*1024||files.length>=4096) refuse('release budget exceeded')
|
|
77
|
+
files.push({path:name,size:data.length,mode:name===app.id?0o755:st.mode&0o777,sha256:sha(data),data:data.toString('base64')})
|
|
78
|
+
}
|
|
79
|
+
function walk(dir) {
|
|
80
|
+
const st=fs.lstatSync(path.join(root,dir));if(!st.isDirectory()||st.isSymbolicLink()) refuse('unsafe resource directory')
|
|
81
|
+
for(const name of fs.readdirSync(path.join(root,dir)).sort()) {
|
|
82
|
+
if(!identifier.test(name)) refuse(`unsupported resource filename: ${name}`)
|
|
83
|
+
const rel=`${dir}/${name}`,st=fs.lstatSync(path.join(root,rel))
|
|
84
|
+
if(st.isDirectory()&&!st.isSymbolicLink())walk(rel);else add(rel)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
add(app.id)
|
|
88
|
+
const elf=Buffer.from(files[0].data,'base64')
|
|
89
|
+
if(elf.length<64||elf.subarray(0,4).toString('hex')!=='7f454c46'||elf[4]!==2||elf[5]!==1||elf.readUInt16LE(18)!==183||![2,3].includes(elf.readUInt16LE(16))||elf.readBigUInt64LE(24)===0n) refuse('executable must be nonzero-entry AArch64 ELF64')
|
|
90
|
+
walk('resources')
|
|
91
|
+
const manifest=files.map(({data,...entry})=>entry)
|
|
92
|
+
return {app:app.id,release:sha(Buffer.from(JSON.stringify(manifest))),manifest,files}
|
|
93
|
+
}
|
|
94
|
+
const quote=s=>`'${s.replaceAll("'","'\\''")}'`
|
|
95
|
+
export function deployLinux({app,selection,env,dryRun=false,stdout=console.log,transaction=randomUUID(),authorizationFile='',action='deploy',execute=spawnSync}) {
|
|
96
|
+
if(!selection.boardName)refuse('registered board alias required')
|
|
97
|
+
if(!['deploy','inspect','rollback'].includes(action))refuse('invalid transaction action')
|
|
98
|
+
if(env.GEA_LINUX_OUTPUT_DIR)refuse('output override refused for deployment')
|
|
99
|
+
if(!/^[a-f0-9-]{36}$/.test(transaction))refuse('invalid transaction id')
|
|
100
|
+
let authority=validateLinuxBoard(selection.linuxBoard,{transportOnly:action!=='deploy'})
|
|
101
|
+
if(dryRun){stdout(`Linux native ${action} through strict SSH for registered board ${selection.boardName}; no connection opened`);return 0}
|
|
102
|
+
let request={protocol:1,transaction,action,root:authority.deployment.releaseRoot}
|
|
103
|
+
if(action==='deploy') {
|
|
104
|
+
runLinux({app,env,stdout})
|
|
105
|
+
authority=validateLinuxBoard(selection.linuxBoard)
|
|
106
|
+
const executable=path.join(app.root,'dist/raspberry-pi-os',app.id,app.id),inspectedSha=sha(fs.readFileSync(executable))
|
|
107
|
+
const architecture=verifyLinuxExecutable(executable,env),payload=releasePayload(app)
|
|
108
|
+
if(payload.manifest[0].sha256!==inspectedSha)refuse('executable changed during inspection')
|
|
109
|
+
request={...request,architecture,...payload,profile:authority.deployment.profile,hook:authority.hook.toString('base64'),hookSha256:authority.deployment.hookSha256}
|
|
110
|
+
}
|
|
111
|
+
if(authorizationFile) {
|
|
112
|
+
const bytes=regular(authorizationFile);if(bytes.length>1024*1024)refuse('authorization budget')
|
|
113
|
+
const authorization=JSON.parse(bytes)
|
|
114
|
+
if(!['prepare','verify','observe','rollback'].includes(authorization.phase)||authorization.transaction!==transaction||(action==='deploy'&&(authorization.release!==request.release||authorization.hookSha256!==request.hookSha256)))refuse('authorization binding mismatch')
|
|
115
|
+
request.authorization=authorization;request.authorizationSha256=sha(bytes);request.authorizationBytes=bytes.toString('base64')
|
|
116
|
+
}
|
|
117
|
+
if(action==='rollback'&&request.authorization?.phase!=='rollback')refuse('explicit rollback inspection/evidence packet required')
|
|
118
|
+
const program=fs.readFileSync(new URL('./transaction.py',import.meta.url),'utf8')
|
|
119
|
+
const args=[...sshArguments(authority.ssh),`/usr/bin/python3 -I -c ${quote(program)}`]
|
|
120
|
+
// Recovery/inspection use the stored source/profile, whose maximum phase
|
|
121
|
+
// budget is600s. They require neither a rebuild nor a replacement artifact.
|
|
122
|
+
const seconds=action==='deploy'?Object.values(authority.deployment.profile.phaseTimeoutSeconds).reduce((a,b)=>a+b,0)+authority.deployment.profile.transferTimeoutSeconds+30:630
|
|
123
|
+
const result=execute('ssh',args,{input:JSON.stringify(request),encoding:'utf8',maxBuffer:2*1024*1024,timeout:seconds*1000,env:{PATH:env.PATH||'/usr/bin:/bin',LANG:'C'}})
|
|
124
|
+
if(result.error)throw result.error
|
|
125
|
+
if(result.status!==0)refuse(`transaction failed; retain transaction ${transaction} for review: ${(result.stderr||'').slice(-2000)}`)
|
|
126
|
+
let receipt;try{receipt=JSON.parse(result.stdout)}catch{refuse('invalid remote transaction receipt')}
|
|
127
|
+
const allowed=action==='inspect'?['inspection']:action==='rollback'?['rolled_back']:['complete','pending_maintenance','pending_protected_boot','pending_transition_review','pending_candidate']
|
|
128
|
+
if(receipt.transaction!==transaction||receipt.app!==app.id||(request.release&&receipt.release!==request.release)||!allowed.includes(receipt.status))refuse('unbound remote receipt')
|
|
129
|
+
const bytes=JSON.stringify(receipt,null,2)+'\n',directory=path.join(app.root,'dist/raspberry-pi-os',app.id)
|
|
130
|
+
fs.mkdirSync(directory,{recursive:true,mode:0o700})
|
|
131
|
+
const out=path.join(directory,`deployment-${transaction}-${receipt.status}-${receipt.receipts.length}-${sha(Buffer.from(bytes)).slice(0,12)}.json`)
|
|
132
|
+
if(fs.existsSync(out)){if(fs.readFileSync(out,'utf8')!==bytes)refuse('local receipt collision')}else fs.writeFileSync(out,bytes,{mode:0o600,flag:'wx'})
|
|
133
|
+
stdout(`Linux deployment ${transaction}: ${receipt.historical?'historical completion (not a current observation)':receipt.status}; receipt ${out}`)
|
|
134
|
+
return ['complete','inspection','rolled_back'].includes(receipt.status)?0:2
|
|
135
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"""Gea Linux transaction transport. Runs only over explicitly pinned SSH.
|
|
2
|
+
A reviewed, hash-pinned Python hook/profile is streamed with the release; no
|
|
3
|
+
preinstalled new hook, shell command interpolation or product-specific policy.
|
|
4
|
+
The hook owns service/session/storage authority. Transport never calls reboot,
|
|
5
|
+
changes a unit, selects a release or claims protection from command exit alone.
|
|
6
|
+
"""
|
|
7
|
+
import base64, hashlib, json, os, signal, stat, subprocess, sys
|
|
8
|
+
|
|
9
|
+
def sha(data): return hashlib.sha256(data).hexdigest()
|
|
10
|
+
def check(ok, reason):
|
|
11
|
+
if not ok: raise RuntimeError(reason)
|
|
12
|
+
def private_dir(path, create=False):
|
|
13
|
+
check(os.path.isabs(path) and os.path.realpath(path)==path, 'noncanonical directory')
|
|
14
|
+
parent=os.path.dirname(path)
|
|
15
|
+
while True:
|
|
16
|
+
ancestor=os.lstat(parent)
|
|
17
|
+
check(stat.S_ISDIR(ancestor.st_mode) and (ancestor.st_mode&0o022==0 or ancestor.st_mode&stat.S_ISVTX),'replaceable transaction ancestor')
|
|
18
|
+
if parent=='/': break
|
|
19
|
+
parent=os.path.dirname(parent)
|
|
20
|
+
if create:
|
|
21
|
+
try: os.mkdir(path, 0o700)
|
|
22
|
+
except FileExistsError: pass
|
|
23
|
+
s=os.lstat(path)
|
|
24
|
+
check(stat.S_ISDIR(s.st_mode) and s.st_uid==os.getuid() and s.st_mode&0o077==0, 'unsafe transaction directory')
|
|
25
|
+
def sync_dir(path):
|
|
26
|
+
fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW)
|
|
27
|
+
try: os.fsync(fd)
|
|
28
|
+
finally: os.close(fd)
|
|
29
|
+
def write(path,data,mode):
|
|
30
|
+
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode)
|
|
31
|
+
try:
|
|
32
|
+
os.fchmod(fd,mode)
|
|
33
|
+
with os.fdopen(fd,'wb',closefd=False) as stream:
|
|
34
|
+
stream.write(data);stream.flush();os.fsync(fd)
|
|
35
|
+
finally: os.close(fd)
|
|
36
|
+
def save(path,state):
|
|
37
|
+
tmp=path+'.new'
|
|
38
|
+
write(tmp,json.dumps(state,sort_keys=True).encode(),0o600)
|
|
39
|
+
os.replace(tmp,path);sync_dir(os.path.dirname(path))
|
|
40
|
+
def regular(path, expected=None):
|
|
41
|
+
fd=os.open(path,os.O_RDONLY|os.O_NOFOLLOW)
|
|
42
|
+
try:
|
|
43
|
+
st=os.fstat(fd);check(stat.S_ISREG(st.st_mode) and st.st_uid==os.getuid() and st.st_mode&0o022==0,'unsafe staged file')
|
|
44
|
+
with os.fdopen(fd,'rb',closefd=False) as stream: data=stream.read(64*1024*1024+1)
|
|
45
|
+
check(len(data)<=64*1024*1024 and (expected is None or sha(data)==expected),'staged hash mismatch')
|
|
46
|
+
return data
|
|
47
|
+
finally: os.close(fd)
|
|
48
|
+
def invoke(context,directory):
|
|
49
|
+
budget=context['profile']['phaseTimeoutSeconds'][context['phase']];check(type(budget)is int and 1<=budget<=600,'phase budget')
|
|
50
|
+
child=subprocess.Popen([sys.executable,'-I',os.path.join(directory,'hook.py'),context['phase']],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,start_new_session=True,env={'PATH':'/usr/sbin:/usr/bin:/sbin:/bin','LANG':'C'})
|
|
51
|
+
try: output,error=child.communicate(json.dumps(context),timeout=budget)
|
|
52
|
+
except BaseException:
|
|
53
|
+
try:
|
|
54
|
+
try: os.killpg(child.pid,signal.SIGKILL)
|
|
55
|
+
except ProcessLookupError: pass
|
|
56
|
+
finally: child.wait()
|
|
57
|
+
raise
|
|
58
|
+
check(child.returncode==0,'transaction hook failed: '+error[-1000:])
|
|
59
|
+
check(len(output)<=1024*1024,'hook receipt budget')
|
|
60
|
+
result=json.loads(output)
|
|
61
|
+
check(all(result.get(k)==context[k] for k in ('protocol','transaction','release','phase')),'unbound hook receipt')
|
|
62
|
+
check(isinstance(result.get('bootId'),str) and result['bootId'],'missing observed boot identity')
|
|
63
|
+
return result
|
|
64
|
+
def run(request):
|
|
65
|
+
check(request['protocol']==1,'protocol')
|
|
66
|
+
if 'authorization'in request:
|
|
67
|
+
raw=base64.b64decode(request['authorizationBytes'],validate=True);check(len(raw)<=1024*1024 and sha(raw)==request['authorizationSha256'] and json.loads(raw)==request['authorization'],'authorization bytes/pin mismatch')
|
|
68
|
+
check(request['authorization'].get('phase')in('prepare','verify','observe','rollback')and request['authorization'].get('transaction')==request['transaction'],'authorization binding')
|
|
69
|
+
transaction=request['transaction'];check(len(transaction)==36 and all(c in 'abcdef0123456789-' for c in transaction),'transaction id')
|
|
70
|
+
if request.get('action','deploy')=='deploy':
|
|
71
|
+
budgets=request['profile']['phaseTimeoutSeconds'];check(set(budgets)=={'prepare','quiesce','backup','activate','verify','protect','observe','rollback','inspect'}and all(type(v)is int and 1<=v<=600 for v in budgets.values()),'bounded phase budgets required')
|
|
72
|
+
action=request.get('action','deploy');check(action in('deploy','inspect','rollback'),'action')
|
|
73
|
+
root=request['root'];private_dir(root,action=='deploy')
|
|
74
|
+
# The root lock is never unlinked: every UUID shares one inode/authority.
|
|
75
|
+
# Durable active ownership outlives the invocation across reboot/pending.
|
|
76
|
+
import fcntl
|
|
77
|
+
lock=os.open(os.path.join(root,'deployment.lock'),os.O_RDWR|(os.O_CREAT if action=='deploy'else 0)|os.O_NOFOLLOW,0o600)
|
|
78
|
+
directory=os.path.join(root,'transaction-'+transaction)
|
|
79
|
+
active=os.path.join(root,'active.json')
|
|
80
|
+
binding={k:request[k] for k in ('transaction','release','app','profile','hookSha256')} if action=='deploy'else None
|
|
81
|
+
try:
|
|
82
|
+
lockstat=os.fstat(lock)
|
|
83
|
+
check(stat.S_ISREG(lockstat.st_mode) and lockstat.st_uid==os.getuid() and lockstat.st_mode&0o077==0,'unsafe root deployment lock')
|
|
84
|
+
try: fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
85
|
+
except BlockingIOError: raise RuntimeError('another deployment invocation owns this release root')
|
|
86
|
+
statepath=os.path.join(directory,'state.json')
|
|
87
|
+
if action in('inspect','rollback'):
|
|
88
|
+
private_dir(directory)
|
|
89
|
+
statebytes=regular(statepath);prior=json.loads(statebytes);binding=prior['binding']
|
|
90
|
+
hookpath=os.path.join(directory,'hook.py');regular(hookpath,binding['hookSha256'])
|
|
91
|
+
manifest=json.loads(regular(os.path.join(directory,'manifest.json')))
|
|
92
|
+
context={**binding,'protocol':1,'phase':'inspect'if action=='inspect'else'rollback','releaseDirectory':os.path.join(directory,'release'),'manifest':manifest,'previous':prior['receipts']}
|
|
93
|
+
if action=='inspect':
|
|
94
|
+
observation=invoke(context,directory)
|
|
95
|
+
check(observation.get('status')=='inspection'and observation.get('readOnly')is True,'product inspection refused')
|
|
96
|
+
return {**binding,'status':'inspection','receipts':prior['receipts'],'transportState':prior,'transportStateSHA256':sha(statebytes),'product':observation}
|
|
97
|
+
check(os.path.lexists(active)and json.loads(regular(active))==binding,'rollback requires exact active transaction owner')
|
|
98
|
+
auth=request.get('authorization',{})
|
|
99
|
+
check(auth.get('phase')=='rollback'and all(auth.get(k)==binding[k]for k in('transaction','release','hookSha256'))and auth.get('transportStateSHA256')==sha(statebytes),'explicit rollback state/owner authorization required')
|
|
100
|
+
prior.setdefault('recoveries',[]).append({'fromStateSHA256':sha(statebytes),'fromInflight':prior.get('inflight'),'authorizationSHA256':request['authorizationSha256']});prior['inflight']='rollback';save(statepath,prior)
|
|
101
|
+
context['authorization']=auth;context['authorizationSha256']=request['authorizationSha256']
|
|
102
|
+
result=invoke(context,directory)
|
|
103
|
+
check(result.get('status')=='rolled_back'and result.get('rollbackVerified')is True and result.get('statePreserved')is True,'explicit rollback verification refused')
|
|
104
|
+
prior['receipts'].append(result);prior.pop('inflight');prior['next']='failed';save(statepath,prior);os.unlink(active);sync_dir(root)
|
|
105
|
+
return {**binding,'status':'rolled_back','receipts':prior['receipts']}
|
|
106
|
+
if os.path.lexists(statepath):
|
|
107
|
+
prior=json.loads(regular(statepath))
|
|
108
|
+
if prior.get('next')=='complete':
|
|
109
|
+
check(prior['binding']==binding,'completed receipt binding changed')
|
|
110
|
+
return {**binding,'status':'complete','historical':True,'receipts':prior['receipts']}
|
|
111
|
+
if os.path.lexists(active): check(json.loads(regular(active))==binding,'another transaction retains active release-root ownership')
|
|
112
|
+
else: save(active,binding)
|
|
113
|
+
private_dir(directory,True)
|
|
114
|
+
try:
|
|
115
|
+
result=locked(request,directory)
|
|
116
|
+
if result['status']=='complete':
|
|
117
|
+
os.unlink(active);sync_dir(root)
|
|
118
|
+
return result
|
|
119
|
+
except Exception:
|
|
120
|
+
# Verified rollback is a terminal result; uncertain/failed rollback
|
|
121
|
+
# leaves ownership held. It can never enable a competing UUID.
|
|
122
|
+
statepath=os.path.join(directory,'state.json')
|
|
123
|
+
if os.path.lexists(statepath):
|
|
124
|
+
state=json.loads(regular(statepath));receipts=state.get('receipts',[])
|
|
125
|
+
if state.get('next')=='failed' and receipts and receipts[-1].get('phase')=='rollback' and receipts[-1].get('rollbackVerified') is True:
|
|
126
|
+
os.unlink(active);sync_dir(root)
|
|
127
|
+
raise
|
|
128
|
+
finally: os.close(lock)
|
|
129
|
+
def locked(r,directory):
|
|
130
|
+
manifest=r['manifest'];check(len(manifest)<=4096 and len(manifest)==len(r['files']),'manifest count')
|
|
131
|
+
check(sha(json.dumps(manifest,separators=(',',':'),ensure_ascii=False).encode())==r['release'],'release identity')
|
|
132
|
+
hook=base64.b64decode(r['hook'],validate=True);check(len(hook)<=1024*1024 and sha(hook)==r['hookSha256'],'hook identity')
|
|
133
|
+
binding={k:r[k] for k in ('transaction','release','app','profile','hookSha256')}
|
|
134
|
+
path=os.path.join(directory,'state.json')
|
|
135
|
+
if os.path.exists(path):
|
|
136
|
+
state=json.loads(regular(path))
|
|
137
|
+
check(state['binding']==binding,'resume binding changed')
|
|
138
|
+
check(not state.get('inflight'),'interrupted phase requires reviewed recovery; automatic retry refused')
|
|
139
|
+
check(state['next']!='failed','failed transaction cannot be silently retried')
|
|
140
|
+
else:
|
|
141
|
+
release=os.path.join(directory,'release');private_dir(release,True)
|
|
142
|
+
total=0;seen=set()
|
|
143
|
+
for entry,data in zip(manifest,r['files']):
|
|
144
|
+
name=entry['path'];parts=name.split('/')
|
|
145
|
+
check(name not in seen and len(parts)<=32 and all(p and p not in ('.','..') and all(c.isalnum() or c in '_.-' for c in p) for p in parts),'release path')
|
|
146
|
+
check(name==r['app'] or name.startswith('resources/'),'release namespace');seen.add(name)
|
|
147
|
+
check({k:v for k,v in data.items() if k!='data'}==entry,'manifest/data mismatch')
|
|
148
|
+
content=base64.b64decode(data['data'],validate=True);total+=len(content)
|
|
149
|
+
check(len(content)==entry['size'] and len(content)<=64*1024*1024 and total<=128*1024*1024 and sha(content)==entry['sha256'],'payload hash/budget')
|
|
150
|
+
check(entry['mode'] in (0o400,0o444,0o600,0o644,0o755),'release mode')
|
|
151
|
+
parent=release
|
|
152
|
+
for p in parts[:-1]: parent=os.path.join(parent,p);private_dir(parent,True)
|
|
153
|
+
write(os.path.join(release,name),content,entry['mode']);sync_dir(parent)
|
|
154
|
+
check(r['app'] in seen and any(p.startswith('resources/') for p in seen),'incomplete release')
|
|
155
|
+
write(os.path.join(directory,'hook.py'),hook,0o400)
|
|
156
|
+
write(os.path.join(directory,'manifest.json'),json.dumps(manifest).encode(),0o400)
|
|
157
|
+
state={'binding':binding,'next':'prepare','receipts':[]};save(path,state);sync_dir(os.path.dirname(directory))
|
|
158
|
+
release=os.path.join(directory,'release')
|
|
159
|
+
for entry in manifest:
|
|
160
|
+
data=regular(os.path.join(release,entry['path']),entry['sha256'])
|
|
161
|
+
check(len(data)==entry['size'] and stat.S_IMODE(os.lstat(os.path.join(release,entry['path'])).st_mode)==entry['mode'],'release size/mode changed')
|
|
162
|
+
regular(os.path.join(directory,'hook.py'),r['hookSha256'])
|
|
163
|
+
def phase(name):
|
|
164
|
+
state['inflight']=name;save(path,state)
|
|
165
|
+
context={**binding,'protocol':1,'phase':name,'releaseDirectory':release,'manifest':manifest,'previous':state['receipts']}
|
|
166
|
+
if 'authorization'in r and r['authorization']['phase']==name:
|
|
167
|
+
auth=r['authorization'];check(all(auth.get(k)==binding[k]for k in('transaction','release','hookSha256')),'authorization binding')
|
|
168
|
+
context['authorization']=auth;context['authorizationSha256']=r['authorizationSha256']
|
|
169
|
+
result=invoke(context,directory)
|
|
170
|
+
state['receipts'].append(result);state.pop('inflight');return result
|
|
171
|
+
if state['next'] in ('observe','complete'):
|
|
172
|
+
observed=phase('observe')
|
|
173
|
+
if observed.get('status')=='awaiting_transition_approval':
|
|
174
|
+
save(path,state);return {**binding,'status':'pending_transition_review','receipts':state['receipts']}
|
|
175
|
+
check(observed.get('status')=='protected' and observed.get('releaseVerified') is True and observed.get('statePreserved') is True and observed.get('sessionVerified') is True,'protected observation refused')
|
|
176
|
+
check(observed['bootId']!=state['activationBoot'],'protected reboot not observed')
|
|
177
|
+
state['next']='complete';save(path,state)
|
|
178
|
+
if state['next']=='prepare':
|
|
179
|
+
result=phase('prepare')
|
|
180
|
+
check(result.get('status') in ('maintenance_required','maintenance_ready','awaiting_transition_approval'),'maintenance admission refused')
|
|
181
|
+
if result['status']=='awaiting_transition_approval':
|
|
182
|
+
save(path,state);return {**binding,'status':'pending_transition_review','receipts':state['receipts']}
|
|
183
|
+
if result['status']=='maintenance_required':
|
|
184
|
+
save(path,state);return {**binding,'status':'pending_maintenance','receipts':state['receipts']}
|
|
185
|
+
check(result.get('profileVerified') is True and result.get('rollbackReady') is True,'profile/rollback admission refused')
|
|
186
|
+
state['activationBoot']=result['bootId'];state['next']='quiesce';save(path,state)
|
|
187
|
+
phases=['quiesce','backup','activate','verify','protect']
|
|
188
|
+
try:
|
|
189
|
+
while state['next'] in phases:
|
|
190
|
+
name=state['next'];result=phase(name)
|
|
191
|
+
if name=='verify' and result.get('status')=='awaiting_transition_approval':
|
|
192
|
+
save(path,state);return {**binding,'status':'pending_transition_review','receipts':state['receipts']}
|
|
193
|
+
if name=='verify' and result.get('status')=='awaiting_acceptance':
|
|
194
|
+
save(path,state);return {**binding,'status':'pending_candidate','receipts':state['receipts']}
|
|
195
|
+
check(result.get('status')=='ok','phase refused '+name)
|
|
196
|
+
if name=='quiesce': check(result.get('processReaped') is True and result.get('flushed') is True,'quiescence refused')
|
|
197
|
+
if name=='backup': check(result.get('backupVerified') is True and isinstance(result.get('backupSha256'),str) and len(result['backupSha256'])==64,'backup refused')
|
|
198
|
+
if name=='verify': check(result.get('releaseVerified') is True and result.get('statePreserved') is True and result.get('sessionVerified') is True,'candidate verification refused')
|
|
199
|
+
if name=='protect':
|
|
200
|
+
check(result.get('rebootRequired') is True,'protection must be observed in subsequent boot')
|
|
201
|
+
state['next']='observe'
|
|
202
|
+
else: state['next']=phases[phases.index(name)+1]
|
|
203
|
+
save(path,state)
|
|
204
|
+
except Exception:
|
|
205
|
+
# The hook may have mutated before an error or transport timeout. Keep
|
|
206
|
+
# its exact inflight phase; only a separate state-bound recovery may
|
|
207
|
+
# invoke rollback. Never retry or compensate an uncertain phase here.
|
|
208
|
+
state['failedPhase']=state.get('inflight',state['next']);save(path,state)
|
|
209
|
+
raise
|
|
210
|
+
return {**binding,'status':'complete' if state['next']=='complete' else 'pending_protected_boot','receipts':state['receipts']}
|
|
211
|
+
if __name__=='__main__':
|
|
212
|
+
try:
|
|
213
|
+
raw=sys.stdin.buffer.read(180*1024*1024+1);check(len(raw)<=180*1024*1024,'request budget')
|
|
214
|
+
print(json.dumps(run(json.loads(raw))))
|
|
215
|
+
except Exception as error:
|
|
216
|
+
print(str(error),file=sys.stderr);sys.exit(1)
|
package/src/manifest.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveNativeResources } from './native-resources.mjs'
|
|
2
|
+
import { resolveNativeLink } from './native-link.mjs'
|
|
1
3
|
import path from 'node:path'
|
|
2
4
|
import { validateBuildManifest } from './build-config.mjs'
|
|
3
5
|
import { createRequire } from 'node:module'
|
|
@@ -88,6 +90,15 @@ export function validateApp(app) {
|
|
|
88
90
|
}
|
|
89
91
|
for (const [target, config] of Object.entries(app.targetConfig)) {
|
|
90
92
|
const where = (field) => `gea.targets.${target}.${field}`
|
|
93
|
+
if (config.nativeSources.length && !TARGET_NATIVE_SOURCE_TARGETS.includes(target)) errors.push(`${where('nativeSources')} is supported only by ${TARGET_NATIVE_SOURCE_TARGETS.join(', ')}; use gea.nativeSources for every target`)
|
|
94
|
+
for (const source of config.nativeSources) {
|
|
95
|
+
if (!resolveAppSourcePath(app.root, source)) errors.push(`${where('nativeSources')} entry does not exist: ${source}`)
|
|
96
|
+
}
|
|
97
|
+
if (config.providers !== null && (typeof config.providers !== 'object' || Array.isArray(config.providers))) errors.push(`${where('providers')} must be an object keyed by capability`)
|
|
98
|
+
else if (Object.keys(config.providers).length && !TARGET_PROVIDER_TARGETS.includes(target)) errors.push(`${where('providers')} is supported only by ${TARGET_PROVIDER_TARGETS.join(', ')}`)
|
|
99
|
+
if (config.tsconfig && !TARGET_NATIVE_SOURCE_TARGETS.includes(target)) errors.push(`${where('tsconfig')} is supported only by ${TARGET_NATIVE_SOURCE_TARGETS.join(', ')}`)
|
|
100
|
+
else if (config.tsconfig && !exists(path.join(app.root, config.tsconfig))) errors.push(`${where('tsconfig')} does not exist: ${config.tsconfig}`)
|
|
101
|
+
try { resolveNativeResources(app.root, target, config.nativeResources); resolveNativeLink(app.root, target, config) } catch (error) { errors.push(`${where('nativeLibraries/frameworks')}: ${error.message}`) }
|
|
91
102
|
for (const fragment of config.ldFragments) {
|
|
92
103
|
if (!exists(path.join(app.root, fragment))) errors.push(`${where('ldFragments')} entry does not exist: ${fragment}`)
|
|
93
104
|
}
|
|
@@ -150,7 +161,7 @@ export function assertTargetEnabled(ctx, app, targetOrPlatform) {
|
|
|
150
161
|
fail(`App '${app.id}' does not enable target '${platform}'.`, ExitCode.targetUnavailable)
|
|
151
162
|
}
|
|
152
163
|
|
|
153
|
-
export const knownPlatforms = Object.freeze(['web', 'esp32', 'rp2350', 'geaos', 'macos', 'ios', 'android', 'windows', 'xbox', 'pebble'])
|
|
164
|
+
export const knownPlatforms = Object.freeze(['web', 'esp32', 'rp2350', 'geaos', 'raspberry-pi-os', 'macos', 'ios', 'android', 'windows', 'xbox', 'pebble'])
|
|
154
165
|
|
|
155
166
|
export function appPlatformForTarget(ctx, targetOrBoard) {
|
|
156
167
|
return appPlatformsForTarget(ctx, targetOrBoard)[0] || ''
|
|
@@ -232,10 +243,14 @@ export function appSummary(ctx, app) {
|
|
|
232
243
|
// Absolute, already resolved: a target consuming these must not re-join
|
|
233
244
|
// them to the app root, because a package entry does not live there.
|
|
234
245
|
nativeSourcePaths: app.nativeSources.map((source) => resolveAppSourcePath(app.root, source)).filter((resolved) => resolved !== null),
|
|
246
|
+
// The same for gea.targets.<target>.nativeSources, keyed by target.
|
|
247
|
+
targetNativeSourcePaths: Object.fromEntries(Object.entries(app.targetConfig).map(([target, config]) => [target, config.nativeSources.map((source) => resolveAppSourcePath(app.root, source)).filter((resolved) => resolved !== null)])),
|
|
235
248
|
compilerPlugins: app.compilerPlugins,
|
|
236
249
|
defines: app.defines,
|
|
237
250
|
cssDevicePixelRatio: app.cssDevicePixelRatio,
|
|
238
251
|
targetConfig: app.targetConfig,
|
|
252
|
+
nativeResources: Object.fromEntries(Object.keys(app.targets).map(target => [target, resolveNativeResources(app.root,target,appTargetConfig(app,target).nativeResources)])),
|
|
253
|
+
nativeLink: Object.fromEntries(Object.keys(app.targets).map(target => [target, resolveNativeLink(app.root, target, appTargetConfig(app, target))])),
|
|
239
254
|
launcher: app.launcher
|
|
240
255
|
}
|
|
241
256
|
}
|
|
@@ -345,6 +360,14 @@ export function resolveAppSourcePath(root, source) {
|
|
|
345
360
|
}
|
|
346
361
|
}
|
|
347
362
|
|
|
363
|
+
// Targets whose build consumes per-target gea.targets.<target>.nativeSources,
|
|
364
|
+
// gea.targets.<target>.providers (runtime implementations a package requires,
|
|
365
|
+
// staged with the executable) and gea.targets.<target>.tsconfig (the
|
|
366
|
+
// TypeScript project the compiler reads the program under). The other targets
|
|
367
|
+
// take gea.nativeSources only.
|
|
368
|
+
const TARGET_NATIVE_SOURCE_TARGETS = ['windows']
|
|
369
|
+
const TARGET_PROVIDER_TARGETS = ['windows']
|
|
370
|
+
|
|
348
371
|
export function normalizeNativeSources(raw) {
|
|
349
372
|
if (!Array.isArray(raw)) return []
|
|
350
373
|
// Shared native code can live beside an app's directory. These are explicit
|
|
@@ -393,7 +416,7 @@ export function normalizeDefines(raw) {
|
|
|
393
416
|
|
|
394
417
|
|
|
395
418
|
function emptyTargetConfig() {
|
|
396
|
-
return { ldFragments: [], componentDirs: [], componentRequires: [], linkOptions: [], embedFiles: {}, partitions: null, partitionsByTarget: {}, sdkconfig: '', prebuild: '', output: '' }
|
|
419
|
+
return { nativeSources: [], providers: {}, tsconfig: '', nativeIncludeDirectories: [], nativeSystemLibraries: [], nativeResources: {}, nativeLibraries: [], frameworks: [], ldFragments: [], componentDirs: [], componentRequires: [], linkOptions: [], embedFiles: {}, partitions: null, partitionsByTarget: {}, sdkconfig: '', prebuild: '', output: '' }
|
|
397
420
|
}
|
|
398
421
|
|
|
399
422
|
function normalizeStringList(raw) {
|
|
@@ -460,6 +483,14 @@ function normalizeTargetConfig(raw) {
|
|
|
460
483
|
for (const [name, value] of Object.entries(raw)) {
|
|
461
484
|
if (!name || !value || typeof value !== 'object' || value.enabled === false) continue
|
|
462
485
|
configs[name] = {
|
|
486
|
+
nativeSources: normalizeNativeSources(value.nativeSources),
|
|
487
|
+
providers: value.providers ?? {},
|
|
488
|
+
tsconfig: typeof value.tsconfig === 'string' && value.tsconfig.trim() ? normalizeManifestRelativePath(value.tsconfig) : '',
|
|
489
|
+
nativeIncludeDirectories: value.nativeIncludeDirectories ?? [],
|
|
490
|
+
nativeSystemLibraries: value.nativeSystemLibraries ?? [],
|
|
491
|
+
nativeResources: value.nativeResources ?? {},
|
|
492
|
+
nativeLibraries: value.nativeLibraries ?? [],
|
|
493
|
+
frameworks: value.frameworks ?? [],
|
|
463
494
|
ldFragments: normalizeStringList(value.ldFragments).filter((entry) => entry.endsWith('.lf')).map(normalizeManifestRelativePath),
|
|
464
495
|
componentDirs: normalizeStringList(value.componentDirs).map(normalizeManifestRelativePath),
|
|
465
496
|
componentRequires: normalizeStringList(value.componentRequires),
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
/** Explicit app-owned static archives; never arbitrary linker flags or search paths. */
|
|
4
|
+
export function resolveNativeLink(root, target, config = {}) {
|
|
5
|
+
const realRoot = fs.realpathSync(root)
|
|
6
|
+
const libraries = config.nativeLibraries ?? []
|
|
7
|
+
const frameworks = config.frameworks ?? []
|
|
8
|
+
const includeDirectories = config.nativeIncludeDirectories ?? []
|
|
9
|
+
const systemLibraries = config.nativeSystemLibraries ?? []
|
|
10
|
+
for (const [name, values] of [['nativeLibraries', libraries], ['frameworks', frameworks], ['nativeIncludeDirectories', includeDirectories], ['nativeSystemLibraries', systemLibraries]]) {
|
|
11
|
+
if (!Array.isArray(values) || values.some(value => typeof value !== 'string' || !value)) throw new Error(`${name} must be an array of nonempty strings`)
|
|
12
|
+
}
|
|
13
|
+
if (libraries.length && !['macos', 'raspberry-pi-os'].includes(target)) throw new Error(`nativeLibraries are not supported by ${target}`)
|
|
14
|
+
if (frameworks.length && target !== 'macos') throw new Error('frameworks are supported only by macos')
|
|
15
|
+
if ((includeDirectories.length || systemLibraries.length) && !['raspberry-pi-os', 'windows'].includes(target)) throw new Error('Native include/system library extensions are supported only by raspberry-pi-os and windows')
|
|
16
|
+
const includes = includeDirectories.map(value => {
|
|
17
|
+
if (path.isAbsolute(value) || value.split(/[\\/]/).includes('..') || /[\x00-\x1f\x7f]/.test(value)) throw new Error('Invalid nativeIncludeDirectories entry')
|
|
18
|
+
const directory = fs.realpathSync(path.join(realRoot,value)), relative=path.relative(realRoot,directory)
|
|
19
|
+
if (relative === '..' || relative.startsWith('..'+path.sep) || path.isAbsolute(relative) || !fs.statSync(directory).isDirectory()) throw new Error('nativeIncludeDirectories escapes app or is not directory')
|
|
20
|
+
return directory
|
|
21
|
+
})
|
|
22
|
+
for (const name of systemLibraries) if (!/^[A-Za-z][A-Za-z0-9_.+-]*$/.test(name)) throw new Error('Invalid nativeSystemLibraries identifier')
|
|
23
|
+
const resolved = libraries.map(value => {
|
|
24
|
+
if (path.isAbsolute(value) || value.split(/[\\/]/).includes('..') || /[\x00-\x1f\x7f]/.test(value) || !value.endsWith('.a')) throw new Error(`Invalid nativeLibraries archive: ${value}`)
|
|
25
|
+
const file = fs.realpathSync(path.join(realRoot, value))
|
|
26
|
+
const relative = path.relative(realRoot, file)
|
|
27
|
+
if (relative.startsWith('..' + path.sep) || path.isAbsolute(relative) || !fs.statSync(file).isFile()) throw new Error(`nativeLibraries archive escapes app or is not regular: ${value}`)
|
|
28
|
+
const fd = fs.openSync(file, 'r')
|
|
29
|
+
try {
|
|
30
|
+
const magic = Buffer.alloc(8)
|
|
31
|
+
if (fs.readSync(fd, magic, 0, 8, 0) !== 8 || magic.toString() !== '!<arch>\n') throw new Error(`nativeLibraries requires a regular static archive: ${value}`)
|
|
32
|
+
} finally { fs.closeSync(fd) }
|
|
33
|
+
return file
|
|
34
|
+
})
|
|
35
|
+
for (const name of frameworks) if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(name)) throw new Error(`Invalid framework: ${name}`)
|
|
36
|
+
return { nativeLibraries: [...new Set(resolved)], frameworks: [...new Set(frameworks)], nativeIncludeDirectories: [...new Set(includes)], nativeSystemLibraries: [...new Set(systemLibraries)] }
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import crypto from 'node:crypto'
|
|
4
|
+
const exists=file=>{try{fs.lstatSync(file);return true}catch(error){if(error.code==='ENOENT')return false;throw error}}
|
|
5
|
+
function fileHash(file){const fd=fs.openSync(file,'r'),hash=crypto.createHash('sha256'),buffer=Buffer.alloc(1048576);try{for(;;){const count=fs.readSync(fd,buffer,0,buffer.length,null);if(!count)break;hash.update(buffer.subarray(0,count))}return hash.digest('hex')}finally{fs.closeSync(fd)}}
|
|
6
|
+
const safeName=value=>typeof value==='string' && /^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(value) && !['Fonts','FontLicenses','Frameworks','MacOS'].includes(value)
|
|
7
|
+
export function resolveNativeResources(root, target, raw = {}) {
|
|
8
|
+
if(!raw||typeof raw!=='object'||Array.isArray(raw))throw Error('nativeResources must be a destination-to-directory object')
|
|
9
|
+
if(Object.keys(raw).length && !['macos','raspberry-pi-os','windows'].includes(target))throw Error(`nativeResources are not supported by ${target}`)
|
|
10
|
+
const base=fs.realpathSync(root),result={};let count=0,total=0
|
|
11
|
+
for(const [name,value]of Object.entries(raw)){
|
|
12
|
+
if(!safeName(name)||typeof value!=='string'||!value||path.isAbsolute(value)||value.split(/[\\/]/).includes('..')||/[\x00-\x1f\x7f]/.test(value))throw Error('Invalid nativeResources destination or source')
|
|
13
|
+
const source=path.resolve(base,value),relative=path.relative(base,fs.realpathSync(source));if(relative==='..'||relative.startsWith('..'+path.sep)||path.isAbsolute(relative))throw Error('nativeResources escapes app')
|
|
14
|
+
function walk(dir){if(!fs.lstatSync(dir).isDirectory())throw Error('nativeResources directory must not be a symlink');for(const entry of fs.readdirSync(dir)){const file=path.join(dir,entry),stat=fs.lstatSync(file);if(stat.isDirectory())walk(file);else if(stat.isFile()){if(++count>4096||(total+=stat.size)>1073741824)throw Error('nativeResources exceeds build asset bound')}else throw Error('nativeResources accepts only regular files and directories')}}
|
|
15
|
+
walk(source);result[name]=source
|
|
16
|
+
}
|
|
17
|
+
return result
|
|
18
|
+
}
|
|
19
|
+
export function syncNativeResources(root,target,raw,output){
|
|
20
|
+
const resources=resolveNativeResources(root,target,raw);fs.mkdirSync(output,{recursive:true});if(!fs.lstatSync(output).isDirectory())throw Error('Resource output must be a directory')
|
|
21
|
+
const ledger=path.join(output,'.gea-native-resources.json');let previous=[]
|
|
22
|
+
if(exists(ledger)){if(!fs.lstatSync(ledger).isFile())throw Error('Invalid resource ledger');previous=JSON.parse(fs.readFileSync(ledger,'utf8')).destinations;if(!Array.isArray(previous)||previous.some(v=>!safeName(v)))throw Error('Invalid resource ledger destinations')}
|
|
23
|
+
const hashes={};const desired={}
|
|
24
|
+
for(const [name,source]of Object.entries(resources)){const scan=dir=>{for(const entry of fs.readdirSync(dir).sort()){const file=path.join(dir,entry),stat=fs.lstatSync(file);if(stat.isDirectory())scan(file);else if(stat.isFile())desired[name+'/'+path.relative(source,file)]=fileHash(file);else throw Error('Resource source changed')}};scan(source)}
|
|
25
|
+
let unchanged=previous.length===Object.keys(resources).length && previous.every(name=>Object.hasOwn(resources,name))
|
|
26
|
+
const actual={};if(unchanged){for(const name of previous){const directory=path.join(output,name);if(!exists(directory)||!fs.lstatSync(directory).isDirectory()){unchanged=false;break}const scan=dir=>{for(const entry of fs.readdirSync(dir).sort()){const file=path.join(dir,entry),stat=fs.lstatSync(file);if(stat.isDirectory())scan(file);else if(stat.isFile())actual[path.relative(output,file)]=fileHash(file);else unchanged=false}};scan(directory)} }
|
|
27
|
+
if(unchanged && JSON.stringify(actual)===JSON.stringify(desired))return {changed:false,destinations:Object.keys(resources),files:desired}
|
|
28
|
+
for(const [name,source]of Object.entries(resources)){
|
|
29
|
+
const destination=path.join(output,name);if(exists(destination)&&!previous.includes(name))throw Error(`Native resource destination already exists without ownership: ${name}`)
|
|
30
|
+
if(exists(destination)&&!fs.lstatSync(destination).isDirectory())throw Error('Invalid owned resource destination')
|
|
31
|
+
const temporary=path.join(output,`.gea-resource-${name}-${crypto.randomUUID()}`)
|
|
32
|
+
try{fs.cpSync(source,temporary,{recursive:true,dereference:false});const verify=dir=>{for(const entry of fs.readdirSync(dir)){const f=path.join(dir,entry),s=fs.lstatSync(f);if(s.isDirectory())verify(f);else if(s.isFile()){const rel=path.relative(temporary,f);const sourceHash=fileHash(path.join(source,rel)),copyHash=fileHash(f);if(sourceHash!==copyHash||sourceHash!==desired[name+'/'+rel])throw Error('Resource copy changed');hashes[name+'/'+rel]=copyHash}else throw Error('Resource changed to nonregular entry')}};verify(temporary);const expectedKeys=Object.keys(desired).filter(key=>key.startsWith(name+'/'));if(expectedKeys.some(key=>!Object.hasOwn(hashes,key)))throw Error('Resource copy is incomplete');fs.rmSync(destination,{recursive:true,force:true});fs.renameSync(temporary,destination)}finally{fs.rmSync(temporary,{recursive:true,force:true})}
|
|
33
|
+
}
|
|
34
|
+
for(const name of previous)if(!(Object.hasOwn(resources,name))){const old=path.join(output,name);if(exists(old)&&!fs.lstatSync(old).isDirectory())throw Error('Invalid removed resource destination');fs.rmSync(old,{recursive:true,force:true})}
|
|
35
|
+
const result={changed:true,destinations:Object.keys(resources),files:hashes};fs.writeFileSync(ledger,JSON.stringify(result,null,2)+'\n');return result
|
|
36
|
+
}
|
package/src/rp2350/adapter.mjs
CHANGED
|
@@ -6,9 +6,9 @@ import path from 'node:path'
|
|
|
6
6
|
|
|
7
7
|
import { resolvePicotoolSelection } from '../boards/usb.mjs'
|
|
8
8
|
import { CliError, ExitCode, fail } from '../errors.mjs'
|
|
9
|
-
import { envPath } from '../env-path.mjs'
|
|
10
9
|
import { appCmakeMeta } from '../manifest.mjs'
|
|
11
10
|
import { formatCommand } from '../run.mjs'
|
|
11
|
+
import { findOnPath } from '../toolchain.mjs'
|
|
12
12
|
|
|
13
13
|
// Raspberry Pi RP2350 boards through the Pico SDK: cmake configure + build,
|
|
14
14
|
// then UF2 over the BOOTSEL volume or picotool.
|
|
@@ -81,9 +81,9 @@ export function prepareArmToolchain(env, stderr = () => {}) {
|
|
|
81
81
|
|
|
82
82
|
export function cmakeBinary(env) {
|
|
83
83
|
if (env.CMAKE) return env.CMAKE
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
// cmake.exe on Windows: the lookup honours PATHEXT.
|
|
85
|
+
const found = findOnPath('cmake', env)
|
|
86
|
+
if (found) return found
|
|
87
87
|
for (const candidate of ['/opt/homebrew/bin/cmake', '/usr/local/bin/cmake', '/Applications/CMake.app/Contents/bin/cmake']) {
|
|
88
88
|
if (existsSync(candidate)) return candidate
|
|
89
89
|
}
|