import assert from'node:assert/strict';import{cpSync,copyFileSync,mkdtempSync,readFileSync,writeFileSync}from'node:fs';import{join,relative}from'node:path';import{spawnSync}from'node:child_process';import{hashAt,frozenSnapshot}from'./boot-contract-report.mjs'; const root=process.cwd(),before=frozenSnapshot(root),directory=mkdtempSync('reports/os-implementation/K2045-percpu-runqueue-staging-build-'),sources=join(directory,'source');cpSync('reports/os-implementation/K2045-percpu-runqueue-work-sources',sources,{recursive:true,filter:p=>!p.split('/').includes('target')});const artifact=path=>({path,sha256:hashAt(root,path)}),env={...process.env,CARGO_TARGET_DIR:join(root,directory,'target'),RUSTFLAGS:'-C opt-level=s -C llvm-args=--enable-machine-outliner=always -C relocation-model=static -C link-arg=-T'+join(root,sources,'kernel/linker.ld')+' -C link-arg=--build-id=none -A warnings'};delete env.CARGO_ENCODED_RUSTFLAGS; const build=spawnSync('cargo',['build','--manifest-path',join(sources,'kernel/Cargo.toml'),'--offline','--release','--target','aarch64-unknown-none','--bin','turkos-kernel','--no-default-features','--features','os-runtime-product'],{env,encoding:'utf8',timeout:600000,maxBuffer:128*1024*1024});const log=join(directory,'build.log');writeFileSync(log,(build.stdout??'')+(build.stderr??''));const report={schema:'turkos-k2045-staging-link/v1',status:'STAGING_BUILD_FAILED',guestAccepted:false,productAccepted:false,sourceDirectory:sources,build:{exitCode:build.status,error:build.error?.message??null,log:artifact(log)},harness:artifact('scripts/os-acceptance/percpu-runqueue-stage-link.mjs'),rustflags:env.RUSTFLAGS,frozenBefore:before}; if(build.status===0&&!build.error){const elf=join(directory,'product.elf');copyFileSync(join(directory,'target/aarch64-unknown-none/release/turkos-kernel'),elf);const sysroot=spawnSync('rustc',['--print','sysroot'],{encoding:'utf8'}),rust=spawnSync('rustc',['-vV'],{encoding:'utf8'});assert.equal(sysroot.status,0);assert.equal(rust.status,0);const host=rust.stdout.match(/^host: (.+)$/m)[1];const command=(name,args)=>{const r=spawnSync(join(sysroot.stdout.trim(),'lib/rustlib',host,'bin',name),args,{encoding:'utf8',maxBuffer:64*1024*1024});assert.equal(r.status,0,r.stderr);return r.stdout;};const image=join(directory,'product.Image'),nm=join(directory,'product.nm');command('llvm-objcopy',['-O','binary',elf,image]);writeFileSync(nm,command('llvm-nm',['-n','-S',elf]));report.artifacts={elf:artifact(elf),image:artifact(image),nm:artifact(nm)};const text=readFileSync(nm,'utf8'),addr=name=>Number.parseInt(text.match(new RegExp('^([a-f0-9]{16}) [a-f0-9]{16} [A-Za-z] '+name+'$','m'))?.[1],16);report.imageSpan={start:addr('_image_start'),end:addr('_image_end')};report.imageSpan.bytes=report.imageSpan.end-report.imageSpan.start;report.imageSpan.maximum=8*1024*1024;if(report.imageSpan.bytes>0&&report.imageSpan.bytes<=report.imageSpan.maximum)report.status='STAGING_BUILD_PASSED';} report.frozenAfter=frozenSnapshot(root);assert.deepEqual(before,report.frozenAfter);const path=join(directory,'report.json');writeFileSync(path,JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:path,imageSpan:report.imageSpan}));if(report.status!=='STAGING_BUILD_PASSED')process.exitCode=1;