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 9, 2021
1 parent 1c7b41a commit c9c9f8f
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 169 deletions.
70 changes: 63 additions & 7 deletions src/codegen/sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::collect_versions;
use crate::{codegen::general, env::Env, file_saver::save_to_file};
use crate::{
codegen::general, env::Env, file_saver::save_to_file, library::MAIN_NAMESPACE, nameutil,
};
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 +17,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 +31,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 +63,61 @@ 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 {
let ns = env.library.namespace(MAIN_NAMESPACE);
let package_name = ns.package_name.as_ref().expect("Missing package name");
let lib_name = nameutil::crate_name(package_name);

write!(
w,
"{}",
r##"
#[cfg(feature = "abi-tests")]
{
let deps = deps.unwrap();
"##
)?;

write!(
w,
" let includes = deps.get(\"{}\").unwrap().include_paths.clone();",
lib_name
)?;

write!(
w,
"{}",
r##"
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
cc.file("tests/constant.c");
cc.file("tests/layout.c");
for i in includes {
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.0");
set_string(build_deps, "system-deps", "2.0");
}

{
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 c9c9f8f

Please sign in to comment.