Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable build.rs to build each source file in parallel #1591

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ wasm-bindgen-test = { version = "0.3.26", default-features = false }
libc = { version = "0.2.100", default-features = false }

[build-dependencies]
cc = { version = "1.0.69", default-features = false }
cc = { version = "1.0.69", default-features = false, features = ["parallel"] }

[dev-dependencies]
criterion = { version = "0.4", default-features = false }
Expand Down
217 changes: 136 additions & 81 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use std::{
fs::{self, DirEntry},
io::Write,
path::{Path, PathBuf},
process::Command,
process::{self, Command},
sync::Arc,
thread,
};

const X86: &str = "x86";
Expand Down Expand Up @@ -311,7 +313,7 @@ fn ring_build_rs_main() {
use std::env;

let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = PathBuf::from(out_dir);
let out_dir = Arc::new(PathBuf::from(out_dir));

let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
Expand Down Expand Up @@ -341,17 +343,17 @@ fn ring_build_rs_main() {
// don't do this for packaged builds.
let force_warnings_into_errors = is_git;

let target = Target {
let target = Arc::new(Target {
arch,
os,
is_musl,
is_debug,
force_warnings_into_errors,
};
});
let pregenerated = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join(PREGENERATED);

build_c_code(
&target,
target,
pregenerated,
&out_dir,
&ring_core_prefix(),
Expand Down Expand Up @@ -422,9 +424,9 @@ struct Target {
}

fn build_c_code(
target: &Target,
target: Arc<Target>,
pregenerated: PathBuf,
out_dir: &Path,
out_dir: &Arc<PathBuf>,
ring_core_prefix: &str,
use_pregenerated: bool,
) {
Expand Down Expand Up @@ -475,18 +477,25 @@ fn build_c_code(

let test_srcs = RING_TEST_SRCS.iter().map(PathBuf::from).collect::<Vec<_>>();

let libs = [
("", &core_srcs[..], &asm_srcs[..]),
("test", &test_srcs[..], &[]),
];
let libs = [("", core_srcs, asm_srcs), ("test", test_srcs, vec![])];

// XXX: Ideally, ring-test would only be built for `cargo test`, but Cargo
// can't do that yet.
libs.iter()
.for_each(|&(lib_name_suffix, srcs, additional_srcs)| {
let lib_name = String::from(ring_core_prefix) + lib_name_suffix;
build_library(target, out_dir, &lib_name, srcs, additional_srcs)
});
let tasks: Vec<_> = libs
.into_iter()
.map(|(lib_name_suffix, c_srcs, asm_srcs)| {
let lib_name = format!("{}{}", ring_core_prefix, lib_name_suffix);
let target = Arc::clone(&target);
let out_dir = Arc::clone(out_dir);
thread::spawn(move || build_library(&target, &out_dir, &lib_name, c_srcs, asm_srcs))
})
.collect::<Vec<_>>();

tasks
.into_iter()
.map(thread::JoinHandle::join)
.map(Result::unwrap)
.for_each(|()| ());

println!(
"cargo:rustc-link-search=native={}",
Expand All @@ -498,46 +507,57 @@ fn build_library(
target: &Target,
out_dir: &Path,
lib_name: &str,
srcs: &[PathBuf],
additional_srcs: &[PathBuf],
c_srcs: Vec<PathBuf>,
asm_srcs: Vec<PathBuf>,
) {
// Compile all the (dirty) source files into object files.
let objs = additional_srcs
.iter()
.chain(srcs.iter())
.map(|f| compile(f, target, out_dir, out_dir))
let mut c = cc::Build::new();

c.files(c_srcs);

let tasks = asm_srcs
.into_iter()
.filter_map(|f| {
let ext = f.extension().and_then(|ext| ext.to_str());

if ext == Some("o") {
c.object(f);
None
} else if target.os == WINDOWS && ext == Some("asm") {
// On windows, we will use nasm to compile asm
let out_file = obj_path(out_dir, &f);
let mut cmd = nasm(&f, &target.arch, out_dir, &out_file);
Some((spawn_child(&mut cmd), cmd, out_file))
} else {
c.file(f);
None
}
})
.collect::<Vec<_>>();

// Rebuild the library if necessary.
let lib_path = PathBuf::from(out_dir).join(format!("lib{}.a", lib_name));
tasks.into_iter().for_each(|(child, cmd, out_file)| {
wait_for_child(child, &cmd);

let mut c = cc::Build::new();
c.object(out_file);
});

configure_cc(&mut c, "c", target, out_dir);

for f in LD_FLAGS {
let _ = c.flag(f);
}
match target.os.as_str() {
"macos" => {
let _ = c.flag("-fPIC");
let _ = c.flag("-Wl,-dead_strip");
}
_ => {
let _ = c.flag("-Wl,--gc-sections");
}
}
for o in objs {
let _ = c.object(o);

if target.os.as_str() == "macos" {
c.flag("-fPIC");
}

// Handled below.
let _ = c.cargo_metadata(false);

c.compile(
lib_path
.file_name()
.and_then(|f| f.to_str())
.expect("No filename"),
);
// Rebuild the library if necessary.
let lib_filename = format!("lib{}.a", lib_name);

c.out_dir(out_dir);
c.compile(&lib_filename);

// Link the library. This works even when the library doesn't need to be
// rebuilt.
Expand Down Expand Up @@ -573,6 +593,31 @@ fn obj_path(out_dir: &Path, src: &Path) -> PathBuf {
fn cc(file: &Path, ext: &str, target: &Target, include_dir: &Path, out_file: &Path) -> Command {
let mut c = cc::Build::new();

configure_cc(&mut c, ext, target, include_dir);

let compiler = c.get_compiler();

let obj_opt = if compiler.is_like_msvc() { "/Fo" } else { "-o" };

let mut c = compiler.to_command();
let _ = c
.arg("-c")
.arg(format!(
"{}{}",
obj_opt,
out_file.to_str().expect("Invalid path")
))
.arg(file);

c
}

fn configure_cc<'cc>(
c: &'cc mut cc::Build,
ext: &str,
target: &Target,
include_dir: &Path,
) -> &'cc mut cc::Build {
// FIXME: On Windows AArch64 we currently must use Clang to compile C code
if target.os == WINDOWS && target.arch == AARCH64 && !c.get_compiler().is_like_clang() {
let _ = c.compiler("clang");
Expand Down Expand Up @@ -650,16 +695,6 @@ fn cc(file: &Path, ext: &str, target: &Target, include_dir: &Path, out_file: &Pa
let _ = c.flag("-U_FORTIFY_SOURCE");
}

let obj_opt = if compiler.is_like_msvc() { "/Fo" } else { "-o" };
let mut c = c.get_compiler().to_command();
let _ = c
.arg("-c")
.arg(format!(
"{}{}",
obj_opt,
out_file.to_str().expect("Invalid path")
))
.arg(file);
c
}

Expand Down Expand Up @@ -692,22 +727,30 @@ fn nasm(file: &Path, arch: &str, include_dir: &Path, out_file: &Path) -> Command
c
}

fn run_command_with_args<S>(command_name: S, args: &[String])
where
S: AsRef<std::ffi::OsStr> + Copy,
{
let mut cmd = Command::new(command_name);
let _ = cmd.args(args);
run_command(cmd)
fn run_command(mut cmd: Command) {
let child = spawn_child(&mut cmd);
wait_for_child(child, &cmd);
}

fn run_command(mut cmd: Command) {
fn spawn_child(cmd: &mut Command) -> process::Child {
eprintln!("running {:?}", cmd);
let status = cmd.status().unwrap_or_else(|e| {
cmd.spawn().unwrap_or_else(|e| {
panic!("failed to execute [{:?}]: {}", cmd, e);
})
}

fn wait_for_child(mut child: process::Child, cmd: &Command) {
let status = child.wait().unwrap_or_else(|e| {
panic!(
"failed to wait on {:?} created by cmd [{:?}]: {}",
child, cmd, e
);
});
if !status.success() {
panic!("execution failed");
panic!(
"execution failed for {:?} created by cmd [{:?}]: {}",
child, cmd, status
);
}
}

Expand Down Expand Up @@ -771,24 +814,36 @@ fn asm_path(out_dir: &Path, src: &Path, asm_target: &AsmTarget) -> PathBuf {
}

fn perlasm(src_dst: &[(PathBuf, PathBuf)], asm_target: &AsmTarget) {
for (src, dst) in src_dst {
let mut args = vec![
src.to_string_lossy().into_owned(),
asm_target.perlasm_format.to_owned(),
];
if asm_target.arch == "x86" {
args.push("-fPIC".into());
args.push("-DOPENSSL_IA32_SSE2".into());
}
// Work around PerlAsm issue for ARM and AAarch64 targets by replacing
// back slashes with forward slashes.
let dst = dst
.to_str()
.expect("Could not convert path")
.replace('\\', "/");
args.push(dst);
run_command_with_args(&get_command("PERL_EXECUTABLE", "perl"), &args);
}
let perl = get_command("PERL_EXECUTABLE", "perl");

let children = src_dst
.iter()
.map(|(src, dst)| {
let mut cmd = Command::new(&perl);

cmd.arg(src).arg(asm_target.perlasm_format);

if asm_target.arch == "x86" {
cmd.arg("-fPIC");
cmd.arg("-DOPENSSL_IA32_SSE2");
}

// Work around PerlAsm issue for ARM and AAarch64 targets by replacing
// back slashes with forward slashes.
let dst = dst
.to_str()
.expect("Could not convert path")
.replace('\\', "/");

cmd.arg(dst);

(spawn_child(&mut cmd), cmd)
})
.collect::<Vec<_>>();

children.into_iter().for_each(|(child, cmd)| {
wait_for_child(child, &cmd);
});
}

fn get_command(var: &'static str, default: &str) -> String {
Expand Down