Skip to content

Commit

Permalink
move abi checks to a feature and build the C code at build time
Browse files Browse the repository at this point in the history
The abi tests are now conditional to the abi-tests feature,
when that is enabled, the C code for the abi tests is now built
at compile time, using the cc crate rather than having our own
Compiler abstraction and having to compile stuff at runtime and
deal with temp directories etc.
  • Loading branch information
pbor committed Feb 10, 2021
1 parent d8408d8 commit 92a8bb1
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 168 deletions.
47 changes: 41 additions & 6 deletions src/codegen/sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{codegen::general, env::Env, file_saver::save_to_file};
use log::info;
use std::io::{Result, Write};

pub fn generate(env: &Env) {
pub fn generate(env: &Env, has_abi_tests: bool) {
info!(
"Generating sys build script for {}",
env.config.library_name
Expand All @@ -15,7 +15,7 @@ pub fn generate(env: &Env) {
if !split_build_rs || !path.exists() {
info!("Generating file {:?}", path);
save_to_file(&path, env.config.make_backup, |w| {
generate_build_script(w, env, split_build_rs)
generate_build_script(w, env, split_build_rs, has_abi_tests)
});
}

Expand All @@ -29,11 +29,17 @@ pub fn generate(env: &Env) {
}

#[allow(clippy::write_literal)]
fn generate_build_script(w: &mut dyn Write, env: &Env, split_build_rs: bool) -> Result<()> {
fn generate_build_script(
w: &mut dyn Write,
env: &Env,
split_build_rs: bool,
has_abi_tests: bool,
) -> Result<()> {
if !split_build_rs {
general::start_comments(w, &env.config)?;
writeln!(w)?;
}

writeln!(
w,
"{}",
Expand All @@ -55,13 +61,42 @@ fn main() {} // prevent linking libraries to avoid documentation failure
#[cfg(not(feature = "dox"))]
fn main() {
if let Err(s) = system_deps::Config::new().probe() {
let deps = system_deps::Config::new().probe();
if let Err(s) = deps {
println!("cargo:warning={}", s);
process::exit(1);
}"##
)?;

if has_abi_tests {
write!(
w,
"{}",
r##"
#[cfg(feature = "abi-tests")]
{
let mut cc = cc::Build::new();
cc.flag_if_supported("-Wno-deprecated-declarations");
cc.flag_if_supported("-std=c11"); // for _Generic
cc.flag_if_supported("-std:c11"); // for _Generic (MSVC)
cc.file("tests/constant.c");
cc.file("tests/layout.c");
for i in deps.unwrap().all_include_paths() {
cc.include(i);
}
cc.compile("cabitests");
}
}
}
"##
)
)
} else {
writeln!(w, "}}")
}
}

fn generate_build_version(w: &mut dyn Write, env: &Env) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/sys/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ fn fill_in(root: &mut Table, env: &Env) {

{
let build_deps = upsert_table(root, "build-dependencies");
set_string(build_deps, "cc", "1.0");
set_string(build_deps, "system-deps", "3");
}

{
let dev_deps = upsert_table(root, "dev-dependencies");
set_string(dev_deps, "shell-words", "1.0.0");
set_string(dev_deps, "tempfile", "3");
unset(dev_deps, "tempdir");
}

{
Expand All @@ -115,6 +114,7 @@ fn fill_in(root: &mut Table, env: &Env) {
.collect(),
),
);
features.insert("abi-tests".to_string(), Value::Array(Vec::new()));
}

{
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ mod tests;
pub fn generate(env: &Env) {
generate_single_version_file(env);
lib_::generate(env);
build::generate(env);
let crate_name = cargo_toml::generate(env);
tests::generate(env, &crate_name);
let has_abi_tests = tests::generate(env, &crate_name);
build::generate(env, has_abi_tests);
}

pub fn collect_versions(env: &Env) -> BTreeMap<Version, Version> {
Expand Down
Loading

0 comments on commit 92a8bb1

Please sign in to comment.