diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 2f674a311fef4..20ff9d9af3c0e 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -158,6 +158,15 @@ fn main() { // to change a flag in a binary? if env::var("RUSTC_RPATH") == Ok("true".to_string()) { let rpath = if target.contains("apple") { + + // Note that we need to take one extra step on OSX to also pass + // `-Wl,-instal_name,@rpath/...` to get things to work right. To + // do that we pass a weird flag to the compiler to get it to do + // so. Note that this is definitely a hack, and we should likely + // flesh out rpath support more fully in the future. + if stage != "0" { + cmd.arg("-Z").arg("osx-rpath-install-name"); + } Some("-Wl,-rpath,@loader_path/../lib") } else if !target.contains("windows") { Some("-Wl,-rpath,$ORIGIN/../lib") diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index e500c08ce6e32..a415d1f991fcd 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -928,6 +928,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "print some statistics about MIR"), always_encode_mir: bool = (false, parse_bool, [TRACKED], "encode MIR of all functions into the crate metadata"), + osx_rpath_install_name: bool = (false, parse_bool, [TRACKED], + "pass `-install_name @rpath/...` to the OSX linker"), } pub fn default_lib_output() -> CrateType { diff --git a/src/librustc_trans/back/linker.rs b/src/librustc_trans/back/linker.rs index 040a05b69a3a5..a147b598940a2 100644 --- a/src/librustc_trans/back/linker.rs +++ b/src/librustc_trans/back/linker.rs @@ -207,7 +207,12 @@ impl<'a> Linker for GnuLinker<'a> { if self.sess.target.target.options.is_like_osx { self.cmd.args(&["-dynamiclib", "-Wl,-dylib"]); - if self.sess.opts.cg.rpath { + // Note that the `osx_rpath_install_name` option here is a hack + // purely to support rustbuild right now, we should get a more + // principled solution at some point to force the compiler to pass + // the right `-Wl,-install_name` with an `@rpath` in it. + if self.sess.opts.cg.rpath || + self.sess.opts.debugging_opts.osx_rpath_install_name { let mut v = OsString::from("-Wl,-install_name,@rpath/"); v.push(out_filename.file_name().unwrap()); self.cmd.arg(&v);