Skip to content
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
11 changes: 11 additions & 0 deletions .github/workflows/cbindgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ jobs:
python -m pip install --upgrade pip wheel
pip install Cython==3.0.2

- name: Install dotnet
id: dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.302' # dotnet 7.0.302 is used by Unity 2021.3.39f1, 2022.3.33f1, 2023.2.20f1 and 6000.0.5f1

- name: Get dotnet version
run: |
DOTNET_VERSION=${{ steps.dotnet.outputs.dotnet-version }}
echo "DOTNET_VERSION=${DOTNET_VERSION}" >> $GITHUB_ENV

- name: Build
run: |
cargo +stable build --verbose
Expand Down
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ features = ["clone-impls", "extra-traits", "fold", "full", "parsing", "printing"
[dev-dependencies]
serial_test = { version = "2.0.0", default-features = false }
pretty_assertions = "1.4.0"
which = "4"

[features]
default = ["clap"]
Expand Down
3 changes: 2 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[toolchain]
channel = "nightly"
channel = "nightly"
components = ["rustfmt", "clippy"]
8 changes: 7 additions & 1 deletion src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::bindgen::ir::{
Constant, Function, ItemContainer, ItemMap, Path as BindgenPath, Static, Struct, Type, Typedef,
};
use crate::bindgen::language_backend::{
CLikeLanguageBackend, CythonLanguageBackend, LanguageBackend,
CLikeLanguageBackend, CSharpLanguageBackend, CythonLanguageBackend, LanguageBackend,
};
use crate::bindgen::writer::SourceWriter;

Expand All @@ -37,6 +37,7 @@ pub struct Bindings {
/// and shouldn't do anything when written anywhere.
noop: bool,
pub package_version: String,
binding_crate_lib_name: String,
}

impl Bindings {
Expand All @@ -52,6 +53,7 @@ impl Bindings {
source_files: Vec<path::PathBuf>,
noop: bool,
package_version: String,
binding_crate_lib_name: String,
) -> Bindings {
Bindings {
config,
Expand All @@ -65,6 +67,7 @@ impl Bindings {
source_files,
noop,
package_version,
binding_crate_lib_name,
}
}

Expand Down Expand Up @@ -210,6 +213,9 @@ impl Bindings {
Language::Cython => {
self.write_with_backend(file, &mut CythonLanguageBackend::new(&self.config))
}
Language::CSharp => {
self.write_with_backend(file, &mut CSharpLanguageBackend::new(&self.config))
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ impl Builder {
Default::default(),
true,
String::new(),
Default::default(),
));
}

Expand All @@ -375,6 +376,8 @@ impl Builder {
result.extend_with(&parser::parse_src(x, &self.config)?);
}

let binding_crate_lib_name;

if let Some((lib_dir, binding_lib_name)) = self.lib.clone() {
let lockfile = self.lockfile.as_deref();

Expand All @@ -388,9 +391,14 @@ impl Builder {
/* existing_metadata = */ None,
)?;

binding_crate_lib_name = cargo.binding_crate_lib_name().to_string();

result.extend_with(&parser::parse_lib(cargo, &self.config)?);
} else if let Some(cargo) = self.lib_cargo.clone() {
binding_crate_lib_name = cargo.binding_crate_lib_name().to_string();
result.extend_with(&parser::parse_lib(cargo, &self.config)?);
} else {
binding_crate_lib_name = String::new()
}

result.source_files.extend_from_slice(self.srcs.as_slice());
Expand All @@ -407,6 +415,7 @@ impl Builder {
result.functions,
result.source_files,
result.package_version,
binding_crate_lib_name,
)
.generate()
}
Expand Down
20 changes: 15 additions & 5 deletions src/bindgen/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn parse_dep_string(dep_string: &str) -> (&str, Option<&str>) {
pub(crate) struct Cargo {
manifest_path: PathBuf,
binding_crate_name: String,
binding_crate_lib_name: String,
lock: Option<Lock>,
metadata: Metadata,
clean: bool,
Expand Down Expand Up @@ -63,25 +64,34 @@ impl Cargo {
None
};

let manifest = cargo_toml::manifest(&toml_path)
.map_err(|x| Error::CargoToml(toml_path.to_str().unwrap().to_owned(), x))?;

// Use the specified binding crate name or infer it from the manifest
let binding_crate_name = match binding_crate_name {
Some(s) => s.to_owned(),
None => {
let manifest = cargo_toml::manifest(&toml_path)
.map_err(|x| Error::CargoToml(toml_path.to_str().unwrap().to_owned(), x))?;
manifest.package.name
}
None => manifest.package.name,
};

let binding_crate_lib_name = manifest
.lib
.and_then(|lib| lib.name)
.unwrap_or_else(|| binding_crate_name.clone());

Ok(Cargo {
manifest_path: toml_path,
binding_crate_name,
binding_crate_lib_name,
lock,
metadata,
clean,
})
}

pub(crate) fn binding_crate_lib_name(&self) -> &str {
&self.binding_crate_lib_name
}

pub(crate) fn binding_crate_name(&self) -> &str {
&self.binding_crate_name
}
Expand Down
6 changes: 6 additions & 0 deletions src/bindgen/cargo/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,19 @@ impl error::Error for Error {
#[derive(Clone, Deserialize, Debug)]
pub struct Manifest {
pub package: Package,
pub lib: Option<Lib>,
}

#[derive(Clone, Deserialize, Debug)]
pub struct Package {
pub name: String,
}

#[derive(Clone, Deserialize, Debug)]
pub struct Lib {
pub name: Option<String>,
}

/// Parse the Cargo.toml for a given path
pub fn manifest(manifest_path: &Path) -> Result<Manifest, Error> {
let mut s = String::new();
Expand Down
27 changes: 27 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Language {
Cxx,
C,
Cython,
CSharp,
}

impl FromStr for Language {
Expand All @@ -42,6 +43,12 @@ impl FromStr for Language {
"C" => Ok(Language::C),
"cython" => Ok(Language::Cython),
"Cython" => Ok(Language::Cython),
"csharp" => Ok(Language::CSharp),
"Csharp" => Ok(Language::CSharp),
"CSharp" => Ok(Language::CSharp),
"CSHARP" => Ok(Language::CSharp),
"c#" => Ok(Language::CSharp),
"C#" => Ok(Language::CSharp),
_ => Err(format!("Unrecognized Language: '{}'.", s)),
}
}
Expand All @@ -54,6 +61,7 @@ impl Language {
match self {
Language::Cxx | Language::C => "typedef",
Language::Cython => "ctypedef",
Language::CSharp => "using",
}
}
}
Expand Down Expand Up @@ -894,6 +902,22 @@ pub struct CythonConfig {
pub cimports: BTreeMap<String, Vec<String>>,
}

/// Settings specific to C# bindings.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct CSharpConfig {
/// Package to use
pub package: Option<String>,

/// Name of the generated interface
pub interface_name: Option<String>,

/// Extra definition to include inside the generated interface
pub extra_defs: Option<String>,
}

/// A collection of settings to customize the generated bindings.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -1016,6 +1040,8 @@ pub struct Config {
/// via the CLI, when ran from a build script cargo sets this variable
/// appropriately).
pub only_target_dependencies: bool,
/// Configuration options specific to C#.
pub csharp: CSharpConfig,
/// Configuration options specific to Cython.
pub cython: CythonConfig,
#[doc(hidden)]
Expand Down Expand Up @@ -1069,6 +1095,7 @@ impl Default for Config {
documentation_length: DocumentationLength::Full,
pointer: PtrConfig::default(),
only_target_dependencies: false,
csharp: CSharpConfig::default(),
cython: CythonConfig::default(),
config_path: None,
}
Expand Down
16 changes: 16 additions & 0 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,22 @@ impl Constant {
write!(out, " {} # = ", name);
language_backend.write_literal(out, value);
}
Language::CSharp => {
// if java_writable_literal(&self.ty, value) {
// out.write("public static final ");
// language_backend.write_type(out, &self.ty);
// write!(out, " {} = ", self.export_name);
// language_backend.write_literal(out, &wrap_java_value(value, &self.ty));
// out.write(";")
// } else {
// write!(
// out,
// "/* Unsupported literal for constant {} */",
// self.export_name
// )
// }
out.new_line();
}
}

condition.write_after(config, out);
Expand Down
Loading