|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// This test exercises bootstrapping the runtime from C to make sure that this |
| 12 | +// continues to work. The runtime is expected to have all local I/O services |
| 13 | +// that one would normally expect. |
| 14 | + |
| 15 | +#[feature(managed_boxes)]; |
| 16 | + |
| 17 | +extern mod extra; |
| 18 | +extern mod rustc; |
| 19 | +extern mod syntax; |
| 20 | + |
| 21 | +use extra::glob; |
| 22 | +use extra::tempfile::TempDir; |
| 23 | +use rustc::back::link; |
| 24 | +use rustc::driver::driver; |
| 25 | +use rustc::driver::session; |
| 26 | +use std::os; |
| 27 | +use std::rt::io::fs; |
| 28 | +use std::run; |
| 29 | +use std::str; |
| 30 | +use syntax::diagnostic; |
| 31 | + |
| 32 | +fn main() { |
| 33 | + // Sketchily figure out where our sysroot is |
| 34 | + let mut sysroot = Path::new(os::self_exe_path().unwrap()); |
| 35 | + sysroot.pop(); |
| 36 | + sysroot.pop(); |
| 37 | + sysroot.push("stage2"); |
| 38 | + |
| 39 | + // Give ourselves a little workspace |
| 40 | + let d = TempDir::new("mytest").unwrap(); |
| 41 | + let d = d.path(); |
| 42 | + |
| 43 | + // Figure out where we are |
| 44 | + let mut me = Path::new(file!()); |
| 45 | + me.pop(); |
| 46 | + let srcfile = me.join("bootstrap-from-c/lib.rs"); |
| 47 | + let cfile = me.join("bootstrap-from-c/main.c"); |
| 48 | + |
| 49 | + // Compile the rust crate |
| 50 | + let options = @session::options { |
| 51 | + maybe_sysroot: Some(@sysroot), |
| 52 | + debugging_opts: session::gen_crate_map, |
| 53 | + ..(*session::basic_options()).clone() |
| 54 | + }; |
| 55 | + let diagnostic = @diagnostic::DefaultEmitter as @diagnostic::Emitter; |
| 56 | + let session = driver::build_session(options, diagnostic); |
| 57 | + driver::compile_input(session, ~[], &driver::file_input(srcfile), |
| 58 | + &Some(d.clone()), &None); |
| 59 | + |
| 60 | + // Copy the C source into place |
| 61 | + let cdst = d.join("main.c"); |
| 62 | + let exe = d.join("out" + os::consts::EXE_SUFFIX); |
| 63 | + fs::copy(&cfile, &cdst); |
| 64 | + |
| 65 | + // Figure out where we put the dynamic library |
| 66 | + let dll = os::dll_filename("boot-*"); |
| 67 | + let dll = glob::glob(d.as_str().unwrap() + "/" + dll).next().unwrap(); |
| 68 | + |
| 69 | + // Compile the c program with all the appropriate arguments. We're compiling |
| 70 | + // the cfile and the library together, and may have to do some linker rpath |
| 71 | + // magic to make sure that the dll can get found when the executable is |
| 72 | + // running. |
| 73 | + let cc = link::get_cc_prog(session); |
| 74 | + let mut cc_args = session.targ_cfg.target_strs.cc_args.clone(); |
| 75 | + cc_args.push_all([~"-o", exe.as_str().unwrap().to_owned()]); |
| 76 | + cc_args.push(cdst.as_str().unwrap().to_owned()); |
| 77 | + cc_args.push(dll.as_str().unwrap().to_owned()); |
| 78 | + if cfg!(target_os = "macos") { |
| 79 | + cc_args.push("-Wl,-rpath," + d.as_str().unwrap().to_owned()); |
| 80 | + } |
| 81 | + let status = run::process_status(cc, cc_args); |
| 82 | + assert!(status.success()); |
| 83 | + |
| 84 | + // Finally, run the program and make sure that it tells us hello. |
| 85 | + let res = run::process_output(exe.as_str().unwrap().to_owned(), []); |
| 86 | + assert!(res.status.success()); |
| 87 | + assert_eq!(str::from_utf8(res.output), ~"hello\n"); |
| 88 | +} |
0 commit comments