@@ -144,6 +144,10 @@ pub fn compile_dry_file(db: &dyn Db, docs: FileCompileInput) -> Option<ModWrappe
144144
145145#[ salsa:: tracked]
146146pub fn compile ( db : & dyn Db , docs : MemDocsInput , out : String , op : Options ) {
147+ let targetdir = PathBuf :: from ( "target" ) ;
148+ if !targetdir. exists ( ) {
149+ fs:: create_dir ( & targetdir) . unwrap ( ) ;
150+ }
147151 let now = Instant :: now ( ) ;
148152 compile_dry ( db, docs) . unwrap ( ) ;
149153 let errs = compile_dry:: accumulated :: < Diagnostics > ( db, docs) ;
@@ -189,9 +193,15 @@ pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
189193 return ;
190194 }
191195 let mut mods = compile_dry:: accumulated :: < ModBuffer > ( db, docs) ;
196+ let mut objs = vec ! [ ] ;
192197 let ctx = Context :: create ( ) ;
193198 let m = mods. pop ( ) . unwrap ( ) ;
199+ let tm = get_target_machine ( op. optimization . to_llvm ( ) ) ;
194200 let llvmmod = Module :: parse_bitcode_from_path ( m. clone ( ) , & ctx) . unwrap ( ) ;
201+ let o = m. with_extension ( "o" ) ;
202+ tm. write_to_file ( & llvmmod, inkwell:: targets:: FileType :: Object , & o)
203+ . unwrap ( ) ;
204+ objs. push ( o) ;
195205 let mut set = FxHashSet :: default ( ) ;
196206 set. insert ( m. clone ( ) ) ;
197207 _ = remove_file ( m. clone ( ) ) . unwrap ( ) ;
@@ -201,8 +211,13 @@ pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
201211 continue ;
202212 }
203213 set. insert ( m. clone ( ) ) ;
214+ let o = m. with_extension ( "o" ) ;
204215 // println!("{}", m.clone().to_str().unwrap());
205- _ = llvmmod. link_in_module ( Module :: parse_bitcode_from_path ( m. clone ( ) , & ctx) . unwrap ( ) ) ;
216+ let module = Module :: parse_bitcode_from_path ( m. clone ( ) , & ctx) . unwrap ( ) ;
217+ tm. write_to_file ( & module, inkwell:: targets:: FileType :: Object , & o)
218+ . unwrap ( ) ;
219+ objs. push ( o) ;
220+ _ = llvmmod. link_in_module ( module) ;
206221 _ = remove_file ( m. clone ( ) ) . unwrap ( ) ;
207222 log:: debug!( "rm {}" , m. to_str( ) . unwrap( ) ) ;
208223 }
@@ -240,7 +255,6 @@ pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
240255 let mut out = out. to_string ( ) ;
241256 let pl_target = Target :: host_target ( ) . expect ( "get host target failed" ) ;
242257 out. push_str ( ".bc" ) ;
243- let tm = get_target_machine ( op. optimization . to_llvm ( ) ) ;
244258 llvmmod. set_triple ( & tm. get_triple ( ) ) ;
245259 llvmmod. set_data_layout ( & tm. get_target_data ( ) . get_data_layout ( ) ) ;
246260 llvmmod. write_bitcode_to_path ( Path :: new ( & out) ) ;
@@ -278,8 +292,9 @@ pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
278292 . to_string ( ) ;
279293 // cmd.arg("-pthread").arg("-ldl");
280294 }
281-
282- t. add_object ( Path :: new ( & out) ) . unwrap ( ) ;
295+ for o in objs {
296+ t. add_object ( o. as_path ( ) ) . unwrap ( ) ;
297+ }
283298 t. add_object ( Path :: new ( & vmpath) ) . unwrap ( ) ;
284299 t. output_to ( & fo) ;
285300 let res = t. finalize ( ) ;
0 commit comments