blob: 6e463d654685463b951bcf22470e26e80f0d4c37 [file] [log] [blame]
use crate::sdk::{
clang_base_path, clang_resource_dir, shared_libraries_path, sysroot_path, TargetOptions,
};
use crate::X64;
use crate::{get_target_triple, get_triple_cpu};
use failure::Error;
use std::fs::create_dir_all;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
pub fn build_rustc(rust_root: &Path, target_options: &TargetOptions<'_, '_>) -> Result<(), Error> {
let target_triple = get_target_triple(target_options);
let triple_cpu = get_triple_cpu(target_options);
let targets_name = if target_options.config.fuchsia_arch == X64 { "X86" } else { "AArch64" };
let clang_base_path = clang_base_path()?;
let clang_bin_path = clang_base_path.join("bin");
let staging = rust_root.join("fuchsia_staging");
create_dir_all(&staging)?;
let build = staging.join("build");
create_dir_all(&build)?;
let config_path = staging.join("config.toml");
let mut config = File::create(&config_path)?;
writeln!(
config,
r#"
[llvm]
optimize = true
static-libstdcpp = true
ninja = true
targets = "{targets}"
[build]
target = ["{target}-fuchsia"]
docs = false
extended = true
cargo-native-static = true
[install]
prefix = "{prefix}"
sysconfdir = "etc"
[rust]
optimize = true
channel = "nightly"
lld = true
[target.{target}-fuchsia]
cc = "{cc}"
cxx = "{cxx}"
ar = "{ar}"
linker = "{linker}"
[dist]
"#,
prefix = staging.to_string_lossy(),
cc = clang_bin_path.join("clang").to_string_lossy(),
cxx = clang_bin_path.join("clang++").to_string_lossy(),
ar = clang_bin_path.join("llvm-ar").to_string_lossy(),
linker = clang_bin_path.join("ld.lld").to_string_lossy(),
target = triple_cpu,
targets = targets_name,
)?;
let sysroot_path = sysroot_path(target_options)?;
let sysroot_lib_path = sysroot_path.join("lib");
let clang_resource_lib = clang_resource_dir(&target_triple)?.join(&target_triple).join("lib");
let cargo_dir = staging.join(".cargo");
create_dir_all(&cargo_dir)?;
let cargo_config_path = cargo_dir.join("config");
let mut cargo_config = File::create(&cargo_config_path)?;
writeln!(
cargo_config,
r#"
[target.{target}-fuchsia]
ar = "{ar}"
rustflags = [
"-C", "link-arg=--sysroot={sysroot}",
"-C", "link-arg=-L{sysroot}/lib",
"-C", "link-arg=-L{target_lib}",
"-C", "link-arg=-L{clang_resource_lib}",
]
"#,
target = triple_cpu,
sysroot = sysroot_path.to_string_lossy(),
ar = clang_bin_path.join("llvm-ar").to_string_lossy(),
target_lib = shared_libraries_path(target_options)?.to_string_lossy(),
clang_resource_lib = clang_resource_lib.to_string_lossy(),
)?;
let x_py = rust_root.join("x.py");
let common_c_flags =
format!("--sysroot={} --target={}-fuchsia", sysroot_path.to_string_lossy(), triple_cpu);
let ld_flags = format!(
"--sysroot={} --target={}-fuchsia -L{}",
sysroot_path.to_string_lossy(),
triple_cpu,
sysroot_lib_path.to_string_lossy()
);
let mut cmd = Command::new(x_py);
cmd.current_dir(build)
.env(format!("CFLAGS_{}-fuchsia", triple_cpu), &common_c_flags)
.env(format!("LDFLAGS_{}-fuchsia", triple_cpu), &ld_flags)
.arg("build")
.arg("--config")
.arg(&config_path)
.arg("--target")
.arg(format!("{}-fuchsia", triple_cpu));
println!("cmd = {:#?}", cmd);
cmd.status().expect("x_py command failed to start");
Ok(())
}