Support dylibs

This commit is contained in:
bjorn3 2018-12-10 15:10:12 +01:00
parent 3441f6b5ba
commit bd4b307b42
3 changed files with 38 additions and 12 deletions

View file

@ -267,7 +267,7 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
drop_place.write_place_ref(fx, arg_place); drop_place.write_place_ref(fx, arg_place);
match ty.sty { match ty.sty {
ty::Dynamic(..) => { ty::Dynamic(..) => {
unimpl!("Drop for trait object"); fx.tcx.sess.warn("Drop for trait object");
} }
_ => { _ => {
let drop_fn_ty = drop_fn.ty(fx.tcx); let drop_fn_ty = drop_fn.ty(fx.tcx);

View file

@ -312,7 +312,9 @@ impl CodegenBackend for CraneliftCodegenBackend {
let output_name = out_filename(sess, crate_type, &outputs, &res.crate_name.as_str()); let output_name = out_filename(sess, crate_type, &outputs, &res.crate_name.as_str());
match crate_type { match crate_type {
CrateType::Rlib => link::link_rlib(sess, &res, output_name), CrateType::Rlib => link::link_rlib(sess, &res, output_name),
CrateType::Executable => link::link_bin(sess, &res, &output_name), CrateType::Dylib | CrateType::Executable => {
link::link_natively(sess, crate_type, &res, &output_name);
}
_ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)), _ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)),
} }
} }

View file

@ -1,7 +1,10 @@
use std::ascii;
use std::char;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str;
use tempfile::Builder as TempFileBuilder; use tempfile::Builder as TempFileBuilder;
@ -65,13 +68,20 @@ pub(crate) fn link_rlib(sess: &Session, res: &CodegenResults, output_name: PathB
} }
} }
pub(crate) fn link_bin(sess: &Session, codegen_results: &CodegenResults, out_filename: &Path) { pub(crate) fn link_natively(
sess: &Session,
crate_type: CrateType,
codegen_results: &CodegenResults,
out_filename: &Path,
) {
let tmpdir = match TempFileBuilder::new().prefix("rustc").tempdir() { let tmpdir = match TempFileBuilder::new().prefix("rustc").tempdir() {
Ok(tmpdir) => tmpdir, Ok(tmpdir) => tmpdir,
Err(err) => sess.fatal(&format!("couldn't create a temp dir: {}", err)), Err(err) => sess.fatal(&format!("couldn't create a temp dir: {}", err)),
}; };
let (linker, flavor) = linker_and_flavor(sess); let (linker, flavor) = linker_and_flavor(sess);
// The invocations of cc share some flags across platforms
let (pname, mut cmd) = get_linker(sess, &linker, flavor); let (pname, mut cmd) = get_linker(sess, &linker, flavor);
let root = sess.target_filesearch(PathKind::Native).get_lib_path(); let root = sess.target_filesearch(PathKind::Native).get_lib_path();
@ -88,11 +98,16 @@ pub(crate) fn link_bin(sess: &Session, codegen_results: &CodegenResults, out_fil
} }
cmd.args(&sess.opts.debugging_opts.pre_link_arg); cmd.args(&sess.opts.debugging_opts.pre_link_arg);
for obj in &sess.target.target.options.pre_link_objects_exe { let pre_link_objects = if crate_type == config::CrateType::Executable {
&sess.target.target.options.pre_link_objects_exe
} else {
&sess.target.target.options.pre_link_objects_dll
};
for obj in pre_link_objects {
cmd.arg(root.join(obj)); cmd.arg(root.join(obj));
} }
if sess.crt_static() { if crate_type == config::CrateType::Executable && sess.crt_static() {
for obj in &sess.target.target.options.pre_link_objects_exe_crt { for obj in &sess.target.target.options.pre_link_objects_exe_crt {
cmd.arg(root.join(obj)); cmd.arg(root.join(obj));
} }
@ -108,9 +123,9 @@ pub(crate) fn link_bin(sess: &Session, codegen_results: &CodegenResults, out_fil
} }
{ {
let target_cpu = "x86_64-apple-darwin"; //::llvm_util::target_cpu(sess); let target_cpu = ::target_lexicon::HOST.to_string();
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor, target_cpu); let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor, &target_cpu);
link_args(&mut *linker, flavor, sess, CrateType::Executable, tmpdir.path(), link_args(&mut *linker, flavor, sess, crate_type, tmpdir.path(),
out_filename, codegen_results); out_filename, codegen_results);
cmd = linker.finalize(); cmd = linker.finalize();
} }
@ -203,6 +218,16 @@ pub(crate) fn link_bin(sess: &Session, codegen_results: &CodegenResults, out_fil
match prog { match prog {
Ok(prog) => { Ok(prog) => {
fn escape_string(s: &[u8]) -> String {
str::from_utf8(s).map(|s| s.to_owned())
.unwrap_or_else(|_| {
let mut x = "Non-UTF-8 output: ".to_string();
x.extend(s.iter()
.flat_map(|&b| ascii::escape_default(b))
.map(char::from));
x
})
}
if !prog.status.success() { if !prog.status.success() {
let mut output = prog.stderr.clone(); let mut output = prog.stderr.clone();
output.extend_from_slice(&prog.stdout); output.extend_from_slice(&prog.stdout);
@ -210,7 +235,7 @@ pub(crate) fn link_bin(sess: &Session, codegen_results: &CodegenResults, out_fil
pname.display(), pname.display(),
prog.status)) prog.status))
.note(&format!("{:?}", &cmd)) .note(&format!("{:?}", &cmd))
.note(&String::from_utf8_lossy(&output)) .note(&escape_string(&output))
.emit(); .emit();
sess.abort_if_errors(); sess.abort_if_errors();
} }
@ -251,9 +276,8 @@ pub(crate) fn link_bin(sess: &Session, codegen_results: &CodegenResults, out_fil
if sess.target.target.options.is_like_osx && if sess.target.target.options.is_like_osx &&
sess.opts.debuginfo != DebugInfo::None sess.opts.debuginfo != DebugInfo::None
{ {
match Command::new("dsymutil").arg(out_filename).output() { if let Err(e) = Command::new("dsymutil").arg(out_filename).output() {
Ok(..) => {} sess.fatal(&format!("failed to run dsymutil: {}", e))
Err(e) => sess.fatal(&format!("failed to run dsymutil: {}", e)),
} }
} }
} }