33// 8bws - 8bitworkshop CLI tool for compilation, ROM execution, and platform info
44
55import * as fs from 'fs' ;
6- import { initialize , compile , compileSourceFile , preload , listTools , listPlatforms , getToolForFilename , PLATFORM_PARAMS , TOOLS , TOOL_PRELOADFS } from './testlib' ;
6+ import * as path from 'path' ;
7+ import { initialize , compile , compileSourceFile , preload , listTools , listPlatforms , getToolForFilename , PLATFORM_PARAMS , TOOLS , TOOL_PRELOADFS , store } from './testlib' ;
78import { isDebuggable } from '../common/baseplatform' ;
89import { hex } from '../common/util' ;
910
@@ -91,7 +92,8 @@ function formatHelp(data: any): void {
9192 console . log ( ` ${ c . green } ${ cmd } ${ c . reset } ${ c . dim } - ${ usage } ${ c . reset } ` ) ;
9293 }
9394 console . log ( `\n${ c . bold } Global options:${ c . reset } ` ) ;
94- console . log ( ` ${ c . yellow } --json${ c . reset } ${ c . dim } Output raw JSON instead of formatted text${ c . reset } ` ) ;
95+ console . log ( ` ${ c . yellow } --json${ c . reset } ${ c . dim } Output raw JSON instead of formatted text${ c . reset } ` ) ;
96+ console . log ( ` ${ c . yellow } --save${ c . reset } ${ c . dim } Save all intermediate build files to /tmp/8bws-<name>${ c . reset } ` ) ;
9597 console . log ( ) ;
9698 }
9799}
@@ -114,6 +116,31 @@ function formatCompile(data: any): void {
114116 if ( data . outputFile ) console . log ( ` ${ c . dim } Output:${ c . reset } ${ c . cyan } ${ data . outputFile } ${ c . reset } ` ) ;
115117 if ( data . hasListings ) console . log ( ` ${ c . dim } Listings:${ c . reset } ${ c . green } yes${ c . reset } ` ) ;
116118 if ( data . hasSymbolmap ) console . log ( ` ${ c . dim } Symbols:${ c . reset } ${ c . green } yes${ c . reset } ` ) ;
119+
120+ // --symbols: dump symbol map
121+ if ( data . symbolmap ) {
122+ console . log ( `\n${ c . bold } Symbols${ c . reset } ${ c . dim } (${ Object . keys ( data . symbolmap ) . length } )${ c . reset } ` ) ;
123+ var sorted = Object . entries ( data . symbolmap ) . sort ( ( a : any , b : any ) => a [ 1 ] - b [ 1 ] ) ;
124+ for ( var [ name , addr ] of sorted ) {
125+ console . log ( ` ${ c . cyan } $${ hex ( addr as number , 4 ) } ${ c . reset } ${ name } ` ) ;
126+ }
127+ }
128+
129+ // --save: show saved files
130+ if ( data . saveDir ) {
131+ console . log ( `\n${ c . bold } Saved to${ c . reset } ${ c . cyan } ${ data . saveDir } ${ c . reset } ${ c . dim } (${ data . savedFiles . length } files)${ c . reset } ` ) ;
132+ for ( var f of data . savedFiles ) {
133+ console . log ( ` ${ c . dim } ●${ c . reset } ${ f } ` ) ;
134+ }
135+ }
136+
137+ // --symbols: dump segments
138+ if ( data . segments ) {
139+ console . log ( `\n${ c . bold } Segments${ c . reset } ${ c . dim } (${ data . segments . length } )${ c . reset } ` ) ;
140+ for ( var seg of data . segments ) {
141+ console . log ( ` ${ c . green } ${ seg . name . padEnd ( 16 ) } ${ c . reset } ${ c . cyan } $${ hex ( seg . start , 4 ) } ${ c . reset } ${ c . dim } size${ c . reset } ${ c . yellow } ${ seg . size } ${ c . reset } ` ) ;
142+ }
143+ }
117144}
118145
119146function formatListTools ( data : any ) : void {
@@ -152,7 +179,7 @@ function formatGeneric(data: any): void {
152179 }
153180}
154181
155- var BOOLEAN_FLAGS = new Set ( [ 'json' , 'info' ] ) ;
182+ var BOOLEAN_FLAGS = new Set ( [ 'json' , 'info' , 'symbols' , 'save' ] ) ;
156183
157184function parseArgs ( argv : string [ ] ) : { command: string ; args: { [ key : string ] : string } ; positional: string [ ] } {
158185 var command = argv [ 2 ] ;
@@ -183,7 +210,7 @@ function usage(): void {
183210 command : 'help' ,
184211 data : {
185212 commands : {
186- 'compile' : 'compile --platform <platform> [--tool <tool>] [--output <file>] <source>' ,
213+ 'compile' : 'compile --platform <platform> [--tool <tool>] [--output <file>] [--symbols] [--save] <source>' ,
187214 'check' : 'check --platform <platform> [--tool <tool>] <source>' ,
188215 'run' : 'run (--platform <id> | --machine <module:ClassName>) [--frames N] [--output <file.png>] [--memdump start,end] [--info] <rom>' ,
189216 'list-tools' : 'list-tools' ,
@@ -273,18 +300,45 @@ async function doCompile(args: { [key: string]: string }, positional: string[],
273300 outputSize = result . output . code ? result . output . code . length : result . output . length ;
274301 }
275302
303+ var compileData : any = {
304+ tool : tool ,
305+ platform : platform ,
306+ source : sourceFile ,
307+ outputSize : outputSize ,
308+ outputFile : outputFile || null ,
309+ hasListings : result . listings ? Object . keys ( result . listings ) . length > 0 : false ,
310+ hasSymbolmap : ! ! result . symbolmap ,
311+ } ;
312+
313+ if ( args [ 'symbols' ] === 'true' ) {
314+ if ( result . symbolmap ) compileData . symbolmap = result . symbolmap ;
315+ if ( result . segments ) compileData . segments = result . segments ;
316+ }
317+
318+ // --save: write all intermediate build files to /tmp/<dirname>
319+ if ( args [ 'save' ] === 'true' ) {
320+ var baseName = path . basename ( sourceFile , path . extname ( sourceFile ) ) ;
321+ var saveDir = path . join ( '/tmp' , `8bws-${ baseName } ` ) ;
322+ fs . mkdirSync ( saveDir , { recursive : true } ) ;
323+ var savedFiles : string [ ] = [ ] ;
324+ for ( var [ filePath , entry ] of Object . entries ( store . workfs ) ) {
325+ var outPath = path . join ( saveDir , filePath ) ;
326+ fs . mkdirSync ( path . dirname ( outPath ) , { recursive : true } ) ;
327+ if ( entry . data instanceof Uint8Array ) {
328+ fs . writeFileSync ( outPath , entry . data ) ;
329+ } else {
330+ fs . writeFileSync ( outPath , entry . data ) ;
331+ }
332+ savedFiles . push ( filePath ) ;
333+ }
334+ compileData . saveDir = saveDir ;
335+ compileData . savedFiles = savedFiles ;
336+ }
337+
276338 output ( {
277339 success : true ,
278340 command : 'compile' ,
279- data : {
280- tool : tool ,
281- platform : platform ,
282- source : sourceFile ,
283- outputSize : outputSize ,
284- outputFile : outputFile || null ,
285- hasListings : result . listings ? Object . keys ( result . listings ) . length > 0 : false ,
286- hasSymbolmap : ! ! result . symbolmap ,
287- }
341+ data : compileData ,
288342 } ) ;
289343}
290344
0 commit comments