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

Added self-linking support to sys mode #1207

Open
wants to merge 1 commit 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
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 a 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 @@ -71,7 +71,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 @@ -341,6 +342,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 @@ -369,6 +375,7 @@ impl Config {
docs_rs_features,
disable_format,
split_build_rs,
self_linking,
extra_versions,
lib_version_overrides,
feature_dependencies,
Expand Down