diff --git a/book/src/config_ffi.md b/book/src/config_ffi.md index 8a05c57f7..463638ca0 100644 --- a/book/src/config_ffi.md +++ b/book/src/config_ffi.md @@ -35,6 +35,13 @@ work_mode = "sys" # and build.rs that generated only if not exists. # Defaults to false split_build_rs = false +# If true, then build.rs and tests do not get generated, and +# no #[link] attribute is emitted. This is supposed to be used +# when a library consists of an hybrid of C and Rust code, and +# the bindings to the C objects should be generated for Rust to +# use. +# Defaults to false +self_linking = false # Adds extra versions to features extra_versions = [ "3.15", diff --git a/src/codegen/sys/lib_.rs b/src/codegen/sys/lib_.rs index 551645098..d920cf4a7 100644 --- a/src/codegen/sys/lib_.rs +++ b/src/codegen/sys/lib_.rs @@ -72,7 +72,10 @@ fn generate_lib(w: &mut dyn Write, env: &Env) -> Result<()> { generate_classes_structs(w, env, &classes)?; generate_interfaces_structs(w, env, &interfaces)?; - write_link_attr(w, &env.namespaces.main().shared_libs)?; + if !env.config.self_linking { + write_link_attr(w, &env.namespaces.main().shared_libs)?; + } + writeln!(w, "extern \"C\" {{")?; functions::generate_enums_funcs(w, env, &enums)?; functions::generate_bitfields_funcs(w, env, &bitfields)?; diff --git a/src/codegen/sys/mod.rs b/src/codegen/sys/mod.rs index fb7523e40..2bb36c080 100644 --- a/src/codegen/sys/mod.rs +++ b/src/codegen/sys/mod.rs @@ -13,9 +13,12 @@ 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); + + if !env.config.self_linking { + build::generate(env); + let crate_name = cargo_toml::generate(env); + tests::generate(env, &crate_name); + } } pub fn collect_versions(env: &Env) -> BTreeMap { diff --git a/src/config/config.rs b/src/config/config.rs index 0098236c8..4450ceaed 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -115,6 +115,7 @@ pub struct Config { pub docs_rs_features: Vec, pub disable_format: bool, pub split_build_rs: bool, + pub self_linking: bool, pub extra_versions: Vec, pub lib_version_overrides: HashMap, pub feature_dependencies: HashMap>, @@ -333,6 +334,11 @@ impl Config { None => false, }; + let self_linking = match toml.lookup("options.self_linking") { + Some(v) => v.as_result_bool("options.self_linking")?, + None => false, + }; + let extra_versions = read_extra_versions(&toml)?; let lib_version_overrides = read_lib_version_overrides(&toml)?; let feature_dependencies = read_feature_dependencies(&toml)?; @@ -361,6 +367,7 @@ impl Config { docs_rs_features, disable_format, split_build_rs, + self_linking, extra_versions, lib_version_overrides, feature_dependencies,