@@ -97,37 +97,43 @@ function shell_exec(cmd: string, cwd: string) {
9797 return result ;
9898}
9999
100- function get_optimization_options ( options : string ) {
101- const optimization_options = [
102- /* default '-O0' not included */ '-O1' , '-O2' , '-O3' , '-O4' , '-Os'
103- ] ;
104-
105- let safe_options = '' ;
106- for ( let o of optimization_options ) {
107- if ( options . includes ( o ) ) {
108- safe_options += ' ' + o ;
109- }
110- }
111-
112- return safe_options ;
100+ const optimization_level = '-O3'
101+
102+ function get_optimization_options ( ) {
103+ const options = [
104+ '--shrink-level=100000000' ,
105+ '--coalesce-locals-learning' ,
106+ '--vacuum' ,
107+ '--merge-blocks' ,
108+ '--merge-locals' ,
109+ '--flatten' ,
110+ '--ignore-implicit-traps' ,
111+ '-ffm' ,
112+ '--const-hoisting' ,
113+ '--code-folding' ,
114+ '--code-pushing' ,
115+ '--dae-optimizing' ,
116+ '--dce' ,
117+ '--simplify-globals-optimizing' ,
118+ '--simplify-locals-nonesting' ,
119+ '--reorder-locals' ,
120+ '--rereloop' ,
121+ '--precompute-propagate' ,
122+ '--local-cse' ,
123+ '--remove-unused-brs' ,
124+ '--memory-packing' ,
125+ '-c' ,
126+ '--avoid-reinterprets' ,
127+ optimization_level
128+ ]
129+
130+ return options . join ( ' ' ) ;
113131}
114132
115- function get_clang_options ( options : string ) {
133+ function get_clang_options ( ) {
116134 const clang_flags = `--sysroot=${ sysroot } -xc -I/app/clang/includes -fdiagnostics-print-source-range-info -Werror=implicit-function-declaration` ;
117- const miscellaneous_options = [
118- '-ffast-math' , '-fno-inline' , '-std=c99' , '-std=c89'
119- ] ;
120-
121- let safe_options = '' ;
122- for ( let o of miscellaneous_options ) {
123- if ( options . includes ( o ) ) {
124- safe_options += ' ' + o ;
125- } else if ( o . includes ( '-std=' ) && options . toLowerCase ( ) . includes ( o ) ) {
126- safe_options += ' ' + o ;
127- }
128- }
129135
130- return clang_flags + safe_options ;
136+ return clang_flags ;
131137}
132138
133139function get_lld_options ( options : string ) {
@@ -167,10 +173,12 @@ function validate_filename(name: string) {
167173 return parts ;
168174}
169175
170- function link_c_files ( source_files : string [ ] , compile_options : string , link_options : string , cwd : string , output : string , result_obj : Task ) {
176+ function link_c_files ( source_files : string [ ] , link_options : string , cwd : string , output : string , result_obj : Task ) {
171177 const files = source_files . join ( ' ' ) ;
172178 const clang = llvmDir + '/bin/clang' ;
173- const cmd = clang + ' ' + get_clang_options ( compile_options ) + ' ' + get_lld_options ( link_options ) + ' ' + files + ' -o ' + output ;
179+
180+ const cmd = clang + ' ' + optimization_level + ' ' + get_clang_options ( ) + ' ' + get_lld_options ( link_options ) + ' ' + files + ' -o ' + output ;
181+
174182 const out = shell_exec ( cmd , cwd ) ;
175183 result_obj . console = sanitize_shell_output ( out ) ;
176184 if ( ! existsSync ( output ) ) {
@@ -225,6 +233,27 @@ function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
225233 return success ;
226234}
227235
236+ function guard_check_wasm ( cwd : string , inplace : string , result_obj : Task ) {
237+ const cmd = 'guard_checker ' + inplace ;
238+ const out = openSync ( cwd + '/guardout.log' , 'w' ) ;
239+ let error = '' ;
240+ let success = true ;
241+ try {
242+ execSync ( cmd , { cwd, stdio : [ null , out , out ] , } ) ;
243+ } catch ( ex : unknown ) {
244+ success = false ;
245+ if ( ex instanceof Error ) {
246+ error = ex ?. message ;
247+ }
248+ } finally {
249+ closeSync ( out ) ;
250+ }
251+ const out_msg = readFileSync ( cwd + '/guardout.log' ) . toString ( ) || error ;
252+ result_obj . console = sanitize_shell_output ( out_msg ) ;
253+ result_obj . success = success ;
254+ return success ;
255+ }
256+
228257export function build_project ( project : RequestBody , base : string ) {
229258 const output = project . output ;
230259 const compress = project . compress ;
@@ -292,18 +321,18 @@ export function build_project(project: RequestBody, base: string) {
292321 name : 'building wasm'
293322 } ;
294323 build_result . tasks . push ( link_result_obj ) ;
295- if ( ! link_c_files ( sources , options || '' , link_options || '' , dir , result , link_result_obj ) ) {
324+ if ( ! link_c_files ( sources , link_options || '' , dir , result , link_result_obj ) ) {
296325 return complete ( false , 'Build error' ) ;
297326 }
298327
299- const opt_options = get_optimization_options ( options || '' ) ;
328+ const opt_options = get_optimization_options ( ) ;
300329 if ( opt_options ) {
301330 const opt_obj = {
302331 name : 'optimizing wasm'
303332 } ;
304333 build_result . tasks . push ( opt_obj ) ;
305334 if ( ! optimize_wasm ( dir , result , opt_options , opt_obj ) ) {
306- return complete ( false , 'Optimization error' ) ;
335+ return complete ( false , 'Pass 1 Optimization error' ) ;
307336 }
308337 }
309338
@@ -313,12 +342,40 @@ export function build_project(project: RequestBody, base: string) {
313342 } ;
314343 build_result . tasks . push ( clean_obj ) ;
315344 if ( ! clean_wasm ( dir , result , clean_obj ) ) {
316- return complete ( false , 'Post-build error' ) ;
345+ return complete ( false , 'Pass 1 Clean error' ) ;
317346 }
318347 }
319348
349+ if ( opt_options ) {
350+ const opt_obj = {
351+ name : 'optimizing wasm'
352+ } ;
353+ build_result . tasks . push ( opt_obj ) ;
354+ if ( ! optimize_wasm ( dir , result , opt_options , opt_obj ) ) {
355+ return complete ( false , 'Pass 2 Optimization error' ) ;
356+ }
357+ }
358+
359+ // if (strip) {
360+ // const clean_obj = {
361+ // name: 'cleaning wasm'
362+ // };
363+ // build_result.tasks.push(clean_obj);
364+ // if (!clean_wasm(dir, result, clean_obj)) {
365+ // return complete(false, 'Pass 2 Clean error');
366+ // }
367+ // }
368+
369+ const guard_result_obj = {
370+ name : 'guard checking wasm'
371+ } ;
372+ build_result . tasks . push ( guard_result_obj ) ;
373+ if ( ! guard_check_wasm ( dir , result , guard_result_obj ) ) {
374+ return complete ( false , 'Guard checking error' ) ;
375+ }
376+
320377 build_result . output = serialize_file_data ( result , compress || false ) ;
321378
322379 return complete ( true , 'Success' ) ;
323380}
324- // END Compile code
381+ // END Compile code
0 commit comments