1 //! Script to invoke the bundled rust-lld with the correct flavor. The flavor is selected by
4 //! lld supports multiple command line interfaces. If `-flavor <flavor>` are passed as the first
5 //! two arguments the `<flavor>` command line interface is used to process the remaining arguments.
6 //! If no `-flavor` argument is present the flavor is determined by the executable name.
8 //! In Rust with `-Z gcc-ld=lld` we have gcc or clang invoke rust-lld. Since there is no way to
9 //! make gcc/clang pass `-flavor <flavor>` as the first two arguments in the linker invocation
10 //! and since Windows does not support symbolic links for files this wrapper is used in place of a
11 //! symblic link. It execs `../rust-lld -flavor ld` if the feature `ld` is enabled and
12 //! `../rust-lld -flavor ld64` if `ld64` is enabled. On Windows it spawns a `..\rust-lld.exe`
15 #[cfg(not(any(feature = "ld", feature = "ld64")))]
16 compile_error!("One of the features ld and ld64 must be enabled.");
18 #[cfg(all(feature = "ld", feature = "ld64"))]
19 compile_error!("Only one of the feature ld or ld64 can be enabled.");
21 #[cfg(feature = "ld")]
22 const FLAVOR: &str = "ld";
24 #[cfg(feature = "ld64")]
25 const FLAVOR: &str = "ld64";
28 use std::fmt::Display;
29 use std::path::{Path, PathBuf};
32 trait ResultExt<T, E> {
33 fn unwrap_or_exit_with(self, context: &str) -> T;
36 impl<T, E> ResultExt<T, E> for Result<T, E>
40 fn unwrap_or_exit_with(self, context: &str) -> T {
44 eprintln!("lld-wrapper: {}: {}", context, e);
52 fn unwrap_or_exit_with(self, context: &str) -> T;
55 impl<T> OptionExt<T> for Option<T> {
56 fn unwrap_or_exit_with(self, context: &str) -> T {
60 eprintln!("lld-wrapper: {}", context);
67 /// Returns the path to rust-lld in the parent directory.
69 /// Exits if the parent directory cannot be determined.
70 fn get_rust_lld_path(current_exe_path: &Path) -> PathBuf {
71 let mut rust_lld_exe_name = "rust-lld".to_owned();
72 rust_lld_exe_name.push_str(env::consts::EXE_SUFFIX);
73 let mut rust_lld_path = current_exe_path
75 .unwrap_or_exit_with("directory containing current executable could not be determined")
77 .unwrap_or_exit_with("parent directory could not be determined")
79 rust_lld_path.push(rust_lld_exe_name);
83 /// Returns the command for invoking rust-lld with the correct flavor.
86 fn get_rust_lld_command(current_exe_path: &Path) -> process::Command {
87 let rust_lld_path = get_rust_lld_path(current_exe_path);
88 let mut command = process::Command::new(rust_lld_path);
89 command.arg("-flavor");
91 command.args(env::args_os().skip(1));
96 fn exec_lld(mut command: process::Command) {
97 use std::os::unix::prelude::CommandExt;
98 Result::<(), _>::Err(command.exec()).unwrap_or_exit_with("could not exec rust-lld");
99 unreachable!("lld-wrapper: after exec without error");
103 fn exec_lld(mut command: process::Command) {
104 // Windows has no exec(), spawn a child process and wait for it
105 let exit_status = command.status().unwrap_or_exit_with("error running rust-lld child process");
106 if !exit_status.success() {
107 match exit_status.code() {
109 // return the original lld exit code
113 eprintln!("lld-wrapper: rust-lld child process exited with error: {}", exit_status,);
121 let current_exe_path =
122 env::current_exe().unwrap_or_exit_with("could not get the path of the current executable");
124 exec_lld(get_rust_lld_command(current_exe_path.as_ref()));