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

WIP: move abi checks to a feature and build the C code at build time #1052

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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