// Builds the native EL0 programs a K2101+ feature experiment embeds. The // os-feature-user crate is copied first and built from the copy; each flat // image (code page, then data page) is bound to its ELF and disassembly. import assert from 'node:assert/strict'; import {copyFileSync,cpSync,mkdirSync,mkdtempSync,readFileSync,rmSync,writeFileSync} from 'node:fs'; import {join,relative} from 'node:path'; import {spawnSync} from 'node:child_process'; import {hashAt,frozenSnapshot} from './boot-contract-report.mjs'; assert.equal(process.argv.length,3,'usage: node feature-user-build.mjs '); const root=process.cwd(),spec=JSON.parse(readFileSync(process.argv[2],'utf8')),programs=spec.integration.programs; assert.ok(Array.isArray(programs)&&programs.length>0&&programs.every(p=>/^k2[1-5]\d\d-[a-z0-9-]+$/.test(p))); const directory=mkdtempSync(`reports/os-implementation/${spec.milestone}-${spec.slug}-user-build-`),source=join(directory,'source'); const artifact=p=>({path:relative(root,p),sha256:hashAt(root,relative(root,p))}); const report={schema:'turkos-feature-user-build/v1',milestone:spec.milestone,status:'RUNNING',guestAccepted:false,productAccepted:false,programs:[],frozenBefore:frozenSnapshot(root)}; copyFileSync('scripts/os-acceptance/feature-user-build.mjs',join(directory,'harness.mjs.txt'));report.harness=artifact(join(directory,'harness.mjs.txt')); cpSync('os-feature-user',join(source,'os-feature-user'),{recursive:true,filter:p=>!p.split('/').includes('target')}); report.sources=[];const walk=d=>{for(const e of spawnSync('find',[d,'-type','f'],{encoding:'utf8'}).stdout.trim().split('\n').sort())report.sources.push(artifact(e));};walk(join(source,'os-feature-user')); const target=join(directory,'target'),manifest=join(source,'os-feature-user/Cargo.toml'); const env={...process.env,CARGO_TARGET_DIR:join(root,target),RUSTFLAGS:`-C relocation-model=static -C link-arg=-T${join(root,source,'os-feature-user/user.ld')} -C link-arg=--build-id=none`}; delete env.CARGO_ENCODED_RUSTFLAGS; const sysroot=spawnSync('rustc',['--print','sysroot'],{encoding:'utf8'}).stdout.trim(),host=spawnSync('rustc',['-vV'],{encoding:'utf8'}).stdout.match(/^host: (.+)$/m)[1]; const tool=n=>join(sysroot,'lib/rustlib',host,'bin',n); try{ const args=['build','--manifest-path',manifest,'--offline','--release','--target','aarch64-unknown-none',...programs.flatMap(p=>['--bin',p])]; const r=spawnSync('cargo',args,{cwd:root,env,encoding:'utf8',timeout:600000,maxBuffer:32*1024*1024}); writeFileSync(join(directory,'build.log'),(r.stdout??'')+(r.stderr??''));report.build={args,exitCode:r.status,log:artifact(join(directory,'build.log')),rustflags:env.RUSTFLAGS.replaceAll(root+'/','')}; assert.equal(r.status,0,'user build failed'); for(const name of programs){ const elf=join(directory,name+'.elf'),bin=join(directory,name+'.bin'),dis=join(directory,name+'.objdump.txt'); copyFileSync(join(target,'aarch64-unknown-none/release',name),elf); const run=(name,args)=>{const t=spawnSync(tool(name),args,{encoding:'utf8',maxBuffer:16*1024*1024});assert.equal(t.status,0,t.stderr);return t.stdout;}; run('llvm-objcopy',['-O','binary',elf,bin]); writeFileSync(dis,run('llvm-objdump',['-d','--no-show-raw-insn',elf])); writeFileSync(join(directory,name+'.sections.txt'),run('llvm-objdump',['-h',elf])); const bytes=readFileSync(bin);assert.ok(bytes.length>0&&bytes.length<=8192,`${name} image size ${bytes.length}`); assert.match(readFileSync(dis,'utf8'),/^0000000080000000 <_start>:/m,`${name} entry must be the first code byte`); report.programs.push({name,elf:artifact(elf),image:artifact(bin),bytes:bytes.length,disassembly:artifact(dis),sections:artifact(join(directory,name+'.sections.txt'))}); } report.status='USER_BUILD_PASSED'; }catch(error){report.status='USER_BUILD_FAILED';report.error=String(error.stack??error).slice(0,4000);process.exitCode=1;} rmSync(target,{recursive:true,force:true});report.buildOutputsRemoved=true; report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,report.frozenBefore); writeFileSync(join(directory,'report.json'),JSON.stringify(report,null,2)+'\n'); console.log(JSON.stringify({status:report.status,report:join(directory,'report.json'),programs:report.programs.map(p=>[p.name,p.bytes])}));