@@ -9,6 +9,7 @@ extern crate regex;
99use cargo:: ops:: { ExecEngine , CommandPrototype } ;
1010use cargo:: util:: { mod, CargoResult , ProcessError , ProcessBuilder } ;
1111
12+ use std:: io:: TempDir ;
1213use std:: io:: fs:: { File , PathExtensions } ;
1314use std:: io:: process:: ProcessOutput ;
1415
@@ -32,16 +33,35 @@ impl ExecEngine for EmscriptenEngine {
3233fn exec ( command : CommandPrototype , with_output : bool , engine : & EmscriptenEngine )
3334 -> Result < Option < ProcessOutput > , ProcessError >
3435{
36+ let build_dir = TempDir :: new ( "cargo-emscripten" ) . unwrap ( ) ;
37+
38+ // finding out dir
39+ let out_dir = command. get_args ( ) . windows ( 2 )
40+ . filter_map ( |args| {
41+ if args[ 0 ] . as_str ( ) == Some ( "--out-dir" ) {
42+ Some ( args[ 1 ] . as_str ( ) . unwrap ( ) . to_string ( ) )
43+ } else {
44+ None
45+ }
46+ } )
47+ . next ( ) . unwrap ( ) ;
48+
49+ // writing `specs.json` in the out dir
50+ File :: create ( & build_dir. path ( ) . join ( "emscripten.json" ) ) . write ( include_bytes ! ( "../specs-target.json" ) ) . unwrap ( ) ;
51+
52+ // writing libstd in the target directory
53+ let ( libs_ll, libs_rlib) = std_inc:: write_std ( & Path :: new ( build_dir. path ( ) ) ) ;
54+
3555 // if we don't find `--crate-type bin`, returning immediatly
36- if command. get_args ( ) . windows ( 2 )
56+ /* if command.get_args().windows(2)
3757 .find(|&args| {
3858 args[0].as_str() == Some("--crate-type") &&
3959 args[1].as_str() == Some("bin")
4060 }).is_none()
4161 {
4262 let p = command.into_process_builder().unwrap();
4363 return do_exec(p, with_output);
44- }
64+ }*/
4565
4666 // finding crate name
4767 let crate_name = command. get_args ( ) . windows ( 2 )
@@ -54,17 +74,6 @@ fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
5474 } )
5575 . next ( ) . unwrap ( ) ;
5676
57- // finding out dir
58- let out_dir = command. get_args ( ) . windows ( 2 )
59- . filter_map ( |args| {
60- if args[ 0 ] . as_str ( ) == Some ( "--out-dir" ) {
61- Some ( args[ 1 ] . as_str ( ) . unwrap ( ) . to_string ( ) )
62- } else {
63- None
64- }
65- } )
66- . next ( ) . unwrap ( ) ;
67-
6877 // executing compiler
6978 {
7079 let mut new_command = CommandPrototype :: new ( command. get_type ( ) . clone ( ) ) . unwrap ( ) ;
@@ -74,9 +83,19 @@ fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
7483 for ( key, val) in command. get_envs ( ) . iter ( ) {
7584 new_command = new_command. env ( key. as_slice ( ) , val. as_ref ( ) . map ( |v| v. as_bytes_no_nul ( ) ) ) ;
7685 }
77- new_command = new_command. cwd ( command. get_cwd ( ) . clone ( ) ) ;
86+ //new_command = new_command.cwd(command.get_cwd().clone());
87+
88+ new_command = new_command. arg ( "--emit=llvm-ir,dep-info" ) ;
89+ new_command = new_command. arg ( "--target" ) . arg ( "emscripten.json" ) ;
90+ new_command = new_command. cwd ( build_dir. path ( ) . clone ( ) ) ;
91+
92+ for ( name, path) in libs_rlib. into_iter ( ) {
93+ new_command = new_command. arg ( "--extern" ) . arg ( format ! ( "{}={}" , name, path. as_str( ) . unwrap( ) ) ) ;
94+ }
95+
96+ // TODO: shouldn't be necessary with the "--extern" commands
97+ new_command = new_command. arg ( "-L" ) . arg ( build_dir. path ( ) ) ;
7898
79- let new_command = new_command. arg ( "--emit=llvm-ir,dep-info" ) ;
8099 try!( do_exec ( new_command. into_process_builder ( ) . unwrap ( ) , with_output) ) ;
81100 }
82101
@@ -94,12 +113,13 @@ fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
94113 let mut process = util:: process ( emcc) . unwrap ( ) ;
95114 process = process. arg ( ll_output_file) ;
96115
97- for lib in libstd . into_iter ( ) {
116+ for lib in libs_ll . into_iter ( ) {
98117 process = process. arg ( lib) ;
99118 }
100119
101120 process = process. arg ( "-lGL" ) . arg ( "-lSDL" ) . arg ( "-s" ) . arg ( "USE_SDL=2" ) ;
102121 process = process. arg ( "-o" ) . arg ( format ! ( "{}/{}.html" , out_dir, crate_name) ) ;
122+ process = process. cwd ( build_dir. path ( ) . clone ( ) ) ;
103123 process
104124 } ;
105125
0 commit comments