11use std:: ffi:: OsString ;
2+ use std:: fs;
23use std:: path:: { Path , PathBuf } ;
34
4- use crate :: { execute_command, test_nasm_command} ;
5+ use crate :: { execute_command, test_nasm_command, use_prebuilt_nasm } ;
56
67#[ derive( Debug ) ]
78pub ( crate ) struct NasmBuilder {
@@ -10,16 +11,18 @@ pub(crate) struct NasmBuilder {
1011 defines : Vec < ( String , Option < String > ) > ,
1112 flags : Vec < String > ,
1213 out_dir : PathBuf ,
14+ manifest_dir : PathBuf ,
1315}
1416
1517impl NasmBuilder {
16- pub ( crate ) fn new ( out_dir : PathBuf ) -> Self {
18+ pub ( crate ) fn new ( manifest_dir : PathBuf , out_dir : PathBuf ) -> Self {
1719 Self {
1820 files : Vec :: new ( ) ,
1921 includes : Vec :: new ( ) ,
2022 defines : Vec :: new ( ) ,
2123 flags : Vec :: new ( ) ,
2224 out_dir,
25+ manifest_dir,
2326 }
2427 }
2528
@@ -45,61 +48,82 @@ impl NasmBuilder {
4548 }
4649
4750 pub ( crate ) fn compile_intermediates ( & self ) -> Vec < PathBuf > {
48- if !test_nasm_command ( ) {
49- panic ! ( "NASM command not found! Build cannot continue." ) ;
50- }
51-
5251 let mut objects = Vec :: new ( ) ;
5352
54- for src in & self . files {
55- let obj_ext = "obj" ;
56- let obj_name = src
57- . file_name ( )
58- . unwrap ( )
59- . to_str ( )
60- . unwrap ( )
61- . replace ( ".asm" , & format ! ( ".{}" , obj_ext) ) ;
62- let obj_path = self . out_dir . join ( obj_name) ;
63-
64- let mut args: Vec < OsString > = vec ! [
65- "-f" . into( ) ,
66- "win64" . into( ) ,
67- "-o" . into( ) ,
68- obj_path. as_os_str( ) . into( ) ,
69- ] ;
70-
71- for inc in & self . includes {
72- args. push ( "-I" . into ( ) ) ;
73- args. push ( inc. as_os_str ( ) . into ( ) ) ;
74- }
75-
76- for ( key, val) in & self . defines {
77- let def = if let Some ( v) = val {
78- format ! ( "-D{}={}" , key, v)
79- } else {
80- format ! ( "-D{}" , key)
81- } ;
82- args. push ( def. into ( ) ) ;
53+ if !test_nasm_command ( ) {
54+ if use_prebuilt_nasm ( ) {
55+ let prebuilt_dir = self . manifest_dir . join ( "builder" ) . join ( "prebuilt-nasm" ) ;
56+ for src in & self . files {
57+ let obj_name = src
58+ . file_name ( )
59+ . unwrap ( )
60+ . to_str ( )
61+ . unwrap ( )
62+ . replace ( ".asm" , ".obj" ) ;
63+ let obj_path = self . out_dir . join ( & obj_name) ;
64+ let base_name = obj_name. strip_suffix ( ".obj" ) . unwrap_or ( & obj_name) ;
65+ let prebuilt_src = prebuilt_dir. join ( format ! ( "{}.obj" , base_name) ) ;
66+ if prebuilt_src. exists ( ) {
67+ fs:: copy ( & prebuilt_src, & obj_path)
68+ . expect ( "Failed to copy prebuilt NASM object" ) ;
69+ } else {
70+ panic ! ( "Prebuilt NASM object not found: {}" , prebuilt_src. display( ) ) ;
71+ }
72+ objects. push ( obj_path) ;
73+ }
74+ } else {
75+ panic ! ( "NASM command not found! Build cannot continue." ) ;
8376 }
84-
85- args. extend ( self . flags . iter ( ) . map ( |s| s. into ( ) ) ) ;
86-
87- args. push ( src. as_os_str ( ) . into ( ) ) ;
88-
89- let result = execute_command (
90- "nasm" . as_ref ( ) ,
91- & args. iter ( ) . map ( |s| s. as_os_str ( ) ) . collect :: < Vec < _ > > ( ) ,
92- ) ;
93- if !result. status {
94- panic ! (
95- "NASM failed for {}:\n -----\n {}\n -----\n {}\n -----\n " ,
96- src. display( ) ,
97- result. stdout,
98- result. stderr
77+ } else {
78+ for src in & self . files {
79+ let obj_name = src
80+ . file_name ( )
81+ . unwrap ( )
82+ . to_str ( )
83+ . unwrap ( )
84+ . replace ( ".asm" , ".obj" ) ;
85+ let obj_path = self . out_dir . join ( obj_name) ;
86+
87+ let mut args: Vec < OsString > = vec ! [
88+ "-f" . into( ) ,
89+ "win64" . into( ) ,
90+ "-o" . into( ) ,
91+ obj_path. as_os_str( ) . into( ) ,
92+ ] ;
93+
94+ for inc in & self . includes {
95+ args. push ( "-I" . into ( ) ) ;
96+ args. push ( inc. as_os_str ( ) . into ( ) ) ;
97+ }
98+
99+ for ( key, val) in & self . defines {
100+ let def = if let Some ( v) = val {
101+ format ! ( "-D{}={}" , key, v)
102+ } else {
103+ format ! ( "-D{}" , key)
104+ } ;
105+ args. push ( def. into ( ) ) ;
106+ }
107+
108+ args. extend ( self . flags . iter ( ) . map ( |s| s. into ( ) ) ) ;
109+
110+ args. push ( src. as_os_str ( ) . into ( ) ) ;
111+
112+ let result = execute_command (
113+ "nasm" . as_ref ( ) ,
114+ & args. iter ( ) . map ( |s| s. as_os_str ( ) ) . collect :: < Vec < _ > > ( ) ,
99115 ) ;
116+ if !result. status {
117+ panic ! (
118+ "NASM failed for {}:\n -----\n {}\n -----\n {}\n -----\n " ,
119+ src. display( ) ,
120+ result. stdout,
121+ result. stderr
122+ ) ;
123+ }
124+
125+ objects. push ( obj_path) ;
100126 }
101-
102- objects. push ( obj_path) ;
103127 }
104128
105129 objects
0 commit comments