Files
rust/compiler/rustc_codegen_gcc/build_system/src/config.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
4.6 KiB
Rust
Raw Normal View History

2023-09-26 16:09:51 +02:00
use crate::utils::{get_gcc_path, get_os_name, get_rustc_host_triple};
use std::collections::HashMap;
use std::env as std_env;
pub struct ConfigInfo {
pub target_triple: String,
pub rustc_command: Vec<String>,
pub run_wrapper: Option<&'static str>,
}
// Returns the beginning for the command line of rustc.
pub fn set_config(
env: &mut HashMap<String, String>,
test_flags: &[String],
gcc_path: Option<&str>,
) -> Result<ConfigInfo, String> {
2023-10-04 16:01:02 +02:00
env.insert("CARGO_INCREMENTAL".to_string(), "0".to_string());
2023-09-26 16:09:51 +02:00
let gcc_path = match gcc_path {
2023-10-04 16:01:02 +02:00
Some(path) => path.to_string(),
2023-09-26 16:09:51 +02:00
None => get_gcc_path()?,
};
2023-10-04 16:01:02 +02:00
env.insert("GCC_PATH".to_string(), gcc_path.clone());
2023-09-26 16:09:51 +02:00
let os_name = get_os_name()?;
let dylib_ext = match os_name.as_str() {
"Linux" => "so",
"Darwin" => "dylib",
2023-10-04 16:01:02 +02:00
os => return Err(format!("unsupported OS `{}`", os)),
2023-09-26 16:09:51 +02:00
};
let host_triple = get_rustc_host_triple()?;
let mut linker = None;
let mut target_triple = host_triple.as_str();
let mut run_wrapper = None;
// FIXME: handle this with a command line flag?
// let mut target_triple = "m68k-unknown-linux-gnu";
if host_triple != target_triple {
if target_triple == "m68k-unknown-linux-gnu" {
target_triple = "mips-unknown-linux-gnu";
linker = Some("-Clinker=m68k-linux-gcc");
} else if target_triple == "aarch64-unknown-linux-gnu" {
// We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
linker = Some("-Clinker=aarch64-linux-gnu-gcc");
run_wrapper = Some("qemu-aarch64 -L /usr/aarch64-linux-gnu");
} else {
2023-10-04 16:01:02 +02:00
return Err(format!("unknown non-native platform `{}`", target_triple));
2023-09-26 16:09:51 +02:00
}
}
2023-10-04 16:01:02 +02:00
let current_dir =
std_env::current_dir().map_err(|error| format!("`current_dir` failed: {:?}", error))?;
let channel = if let Some(channel) = env.get("CHANNEL") {
channel.as_str()
} else {
"debug"
};
2023-09-26 16:09:51 +02:00
let cg_backend_path = current_dir
.join("target")
2023-10-04 16:01:02 +02:00
.join(channel)
.join(&format!("librustc_codegen_gcc.{}", dylib_ext));
2023-09-26 16:09:51 +02:00
let sysroot_path = current_dir.join("build_sysroot/sysroot");
let mut rustflags = Vec::new();
2023-10-04 16:01:02 +02:00
if let Some(cg_rustflags) = env.get("CG_RUSTFLAGS") {
2023-09-26 16:09:51 +02:00
rustflags.push(cg_rustflags.clone());
}
if let Some(linker) = linker {
2023-10-04 16:01:02 +02:00
rustflags.push(linker.to_string());
2023-09-26 16:09:51 +02:00
}
rustflags.extend_from_slice(&[
2023-10-04 16:01:02 +02:00
"-Csymbol-mangling-version=v0".to_string(),
"-Cdebuginfo=2".to_string(),
2023-09-26 16:09:51 +02:00
format!("-Zcodegen-backend={}", cg_backend_path.display()),
2023-10-04 16:01:02 +02:00
"--sysroot".to_string(),
sysroot_path.display().to_string(),
2023-09-26 16:09:51 +02:00
]);
2023-10-04 16:01:02 +02:00
// Since we don't support ThinLTO, disable LTO completely when not trying to do LTO.
// TODO(antoyo): remove when we can handle ThinLTO.
if !env.contains_key(&"FAT_LTO".to_string()) {
rustflags.push("-Clto=off".to_string());
}
2023-09-26 16:09:51 +02:00
rustflags.extend_from_slice(test_flags);
// FIXME(antoyo): remove once the atomic shim is gone
if os_name == "Darwin" {
rustflags.extend_from_slice(&[
2023-10-04 16:01:02 +02:00
"-Clink-arg=-undefined".to_string(),
"-Clink-arg=dynamic_lookup".to_string(),
2023-09-26 16:09:51 +02:00
]);
}
2023-10-04 16:01:02 +02:00
env.insert("RUSTFLAGS".to_string(), rustflags.join(" "));
2023-09-26 16:09:51 +02:00
// display metadata load errors
2023-10-04 16:01:02 +02:00
env.insert("RUSTC_LOG".to_string(), "warn".to_string());
2023-09-26 16:09:51 +02:00
2023-10-04 16:01:02 +02:00
let sysroot = current_dir.join(&format!(
"build_sysroot/sysroot/lib/rustlib/{}/lib",
target_triple
));
2023-09-26 16:09:51 +02:00
let ld_library_path = format!(
"{target}:{sysroot}:{gcc_path}",
target = current_dir.join("target/out").display(),
2023-10-04 16:01:02 +02:00
sysroot = sysroot.display(),
2023-09-26 16:09:51 +02:00
);
2023-10-04 16:01:02 +02:00
env.insert("LD_LIBRARY_PATH".to_string(), ld_library_path.clone());
env.insert("DYLD_LIBRARY_PATH".to_string(), ld_library_path);
2023-09-26 16:09:51 +02:00
// NOTE: To avoid the -fno-inline errors, use /opt/gcc/bin/gcc instead of cc.
// To do so, add a symlink for cc to /opt/gcc/bin/gcc in our PATH.
// Another option would be to add the following Rust flag: -Clinker=/opt/gcc/bin/gcc
let path = std::env::var("PATH").unwrap_or_default();
2023-10-04 16:01:02 +02:00
env.insert("PATH".to_string(), format!("/opt/gcc/bin:{}", path));
2023-09-26 16:09:51 +02:00
2023-10-04 16:01:02 +02:00
let mut rustc_command = vec!["rustc".to_string()];
2023-09-26 16:09:51 +02:00
rustc_command.extend_from_slice(&rustflags);
rustc_command.extend_from_slice(&[
2023-10-04 16:01:02 +02:00
"-L".to_string(),
"crate=target/out".to_string(),
"--out-dir".to_string(),
"target/out".to_string(),
2023-09-26 16:09:51 +02:00
]);
Ok(ConfigInfo {
2023-10-04 16:01:02 +02:00
target_triple: target_triple.to_string(),
2023-09-26 16:09:51 +02:00
rustc_command,
run_wrapper,
})
}