Skip to content

Commit

Permalink
Added self-linking support to sys mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xanathar committed Aug 5, 2021
1 parent 5bbf6cb commit cf8039b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
7 changes: 7 additions & 0 deletions book/src/config_ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/sys/lib_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
9 changes: 6 additions & 3 deletions src/codegen/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version, Version> {
Expand Down
7 changes: 7 additions & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub struct Config {
pub docs_rs_features: Vec<String>,
pub disable_format: bool,
pub split_build_rs: bool,
pub self_linking: bool,
pub extra_versions: Vec<Version>,
pub lib_version_overrides: HashMap<Version, Version>,
pub feature_dependencies: HashMap<Version, Vec<String>>,
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -361,6 +367,7 @@ impl Config {
docs_rs_features,
disable_format,
split_build_rs,
self_linking,
extra_versions,
lib_version_overrides,
feature_dependencies,
Expand Down

0 comments on commit cf8039b

Please sign in to comment.