Commit cdb1eefb authored by Ryan Hunt's avatar Ryan Hunt
Browse files

Rename some types and cleanup documentation

parent bfe3d222
......@@ -12,6 +12,7 @@ use bindgen::ir::{Constant, ItemContainer, Function, Static};
use bindgen::monomorph::TemplateSpecialization;
use bindgen::writer::{ListType, Source, SourceWriter};
/// A bindings header that can be written.
pub struct Bindings {
config: Config,
globals: Vec<Static>,
......@@ -22,12 +23,12 @@ pub struct Bindings {
}
impl Bindings {
pub fn new(config: Config,
constants: Vec<Constant>,
globals: Vec<Static>,
items: Vec<ItemContainer>,
functions: Vec<Function>,
template_specializations: Vec<TemplateSpecialization>) -> Bindings {
pub(crate) fn new(config: Config,
constants: Vec<Constant>,
globals: Vec<Static>,
items: Vec<ItemContainer>,
functions: Vec<Function>,
template_specializations: Vec<TemplateSpecialization>) -> Bindings {
Bindings {
config: config,
globals: globals,
......@@ -202,7 +203,7 @@ impl Bindings {
}
}
pub fn open_namespaces<F: Write>(&self, out: &mut SourceWriter<F>) {
pub(crate) fn open_namespaces<F: Write>(&self, out: &mut SourceWriter<F>) {
let mut wrote_namespace: bool = false;
if let Some(ref namespace) = self.config.namespace {
wrote_namespace = true;
......@@ -226,7 +227,7 @@ impl Bindings {
}
}
pub fn close_namespaces<F: Write>(&self, out: &mut SourceWriter<F>) {
pub(crate) fn close_namespaces<F: Write>(&self, out: &mut SourceWriter<F>) {
let mut wrote_namespace: bool = false;
if let Some(ref namespaces) = self.config.namespaces {
wrote_namespace = true;
......
......@@ -7,19 +7,21 @@ use std::path;
use bindgen::cargo::Cargo;
use bindgen::config::Config;
use bindgen::library::Library;
use bindgen::bindings::Bindings;
use bindgen::parser::{self, Parse};
/// A builder for generating a bindings header.
#[derive(Debug, Clone)]
pub struct LibraryBuilder {
pub struct Builder {
config: Config,
srcs: Vec<path::PathBuf>,
lib: Option<Cargo>,
std_types: bool,
}
impl LibraryBuilder {
pub fn new() -> LibraryBuilder {
LibraryBuilder {
impl Builder {
pub fn new() -> Builder {
Builder {
config: Config::default(),
srcs: Vec::new(),
lib: None,
......@@ -27,28 +29,28 @@ impl LibraryBuilder {
}
}
pub fn with_config(mut self, config: Config) -> LibraryBuilder {
pub fn with_config(mut self, config: Config) -> Builder {
self.config = config;
self
}
pub fn with_std_types(mut self) -> LibraryBuilder {
pub fn with_std_types(mut self) -> Builder {
self.std_types = true;
self
}
pub fn with_src(mut self, src: &path::Path) -> LibraryBuilder {
pub fn with_src(mut self, src: &path::Path) -> Builder {
self.srcs.push(src.to_owned());
self
}
pub fn with_crate(mut self, lib: Cargo) -> LibraryBuilder {
pub fn with_crate(mut self, lib: Cargo) -> Builder {
debug_assert!(self.lib.is_none());
self.lib = Some(lib);
self
}
pub fn build(self) -> Result<Library, String> {
pub fn generate(self) -> Result<Bindings, String> {
let mut result = Parse::new();
if self.std_types {
......@@ -67,17 +69,15 @@ impl LibraryBuilder {
&self.config.parse.expand)?);
}
result.functions.sort_by(|x, y| x.name.cmp(&y.name));
Ok(Library::new(self.config,
result.constants,
result.globals,
result.enums,
result.structs,
result.unions,
result.opaque_items,
result.typedefs,
result.specializations,
result.functions))
Library::new(self.config,
result.constants,
result.globals,
result.enums,
result.structs,
result.unions,
result.opaque_items,
result.typedefs,
result.specializations,
result.functions).generate()
}
}
......@@ -17,7 +17,7 @@ fn parse_dep_string(dep_string: &str) -> (&str, &str) {
}
/// A reference to a package including it's name and the specific version.
pub struct PackageRef {
pub(crate) struct PackageRef {
pub name: String,
pub version: String,
}
......@@ -66,11 +66,11 @@ impl Cargo {
})
}
pub fn binding_crate_name(&self) -> &str {
pub(crate) fn binding_crate_name(&self) -> &str {
&self.binding_crate_name
}
pub fn binding_crate_ref(&self) -> PackageRef {
pub(crate) fn binding_crate_ref(&self) -> PackageRef {
self.find_pkg_ref(&self.binding_crate_name).unwrap()
}
......@@ -90,7 +90,7 @@ impl Cargo {
/// Finds the package reference for a dependency of a crate using
/// `Cargo.lock`.
pub fn find_dep_ref(&self, package: &PackageRef, dependency_name: &str) -> Option<PackageRef> {
pub(crate) fn find_dep_ref(&self, package: &PackageRef, dependency_name: &str) -> Option<PackageRef> {
if self.lock.is_none() {
return None;
}
......@@ -141,7 +141,8 @@ impl Cargo {
}
/// Finds the directory for a specified package reference.
pub fn find_crate_dir(&self, package: &PackageRef) -> Option<PathBuf> {
#[allow(unused)]
pub(crate) fn find_crate_dir(&self, package: &PackageRef) -> Option<PathBuf> {
for meta_package in &self.metadata.packages {
if meta_package.name == package.name &&
meta_package.version == package.version {
......@@ -153,7 +154,7 @@ impl Cargo {
}
/// Finds `src/lib.rs` for a specified package reference.
pub fn find_crate_src(&self, package: &PackageRef) -> Option<PathBuf> {
pub(crate) fn find_crate_src(&self, package: &PackageRef) -> Option<PathBuf> {
let kind_lib = String::from("lib");
let kind_staticlib = String::from("staticlib");
let kind_rlib = String::from("rlib");
......@@ -176,7 +177,7 @@ impl Cargo {
None
}
pub fn expand_crate(&self, package: &PackageRef) -> Result<String, String> {
pub(crate) fn expand_crate(&self, package: &PackageRef) -> Result<String, String> {
cargo_expand::expand(&self.manifest_path, &package.name, &package.version)
}
}
......@@ -12,7 +12,7 @@ use std::str::FromStr;
use toml;
pub use bindgen::ir::annotation::AnnotationSet;
use bindgen::ir::annotation::AnnotationSet;
pub use bindgen::rename::RenameRule;
pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
......@@ -123,14 +123,14 @@ impl Default for FunctionConfig {
}
impl FunctionConfig {
pub fn prefix(&self, annotations: &AnnotationSet) -> Option<String> {
pub(crate) fn prefix(&self, annotations: &AnnotationSet) -> Option<String> {
if let Some(x) = annotations.atom("prefix") {
return x;
}
self.prefix.clone()
}
pub fn postfix(&self, annotations: &AnnotationSet) -> Option<String> {
pub(crate) fn postfix(&self, annotations: &AnnotationSet) -> Option<String> {
if let Some(x) = annotations.atom("postfix") {
return x;
}
......@@ -178,37 +178,37 @@ impl Default for StructConfig {
}
impl StructConfig {
pub fn derive_eq(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn derive_eq(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-eq") {
return x;
}
self.derive_eq
}
pub fn derive_neq(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn derive_neq(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-neq") {
return x;
}
self.derive_neq
}
pub fn derive_lt(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn derive_lt(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-lt") {
return x;
}
self.derive_lt
}
pub fn derive_lte(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn derive_lte(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-lte") {
return x;
}
self.derive_lte
}
pub fn derive_gt(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn derive_gt(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-gt") {
return x;
}
self.derive_gt
}
pub fn derive_gte(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn derive_gte(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-gte") {
return x;
}
......@@ -242,7 +242,7 @@ impl Default for EnumConfig {
}
impl EnumConfig {
pub fn add_sentinel(&self, annotations: &AnnotationSet) -> bool {
pub(crate) fn add_sentinel(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("add-sentinel") {
return x;
}
......@@ -341,9 +341,10 @@ pub struct Config {
/// The configuration options for enums
#[serde(rename = "enum")]
pub enumeration: EnumConfig,
/// The configuration options for constants
#[serde(rename = "const")]
pub constant: ConstantConfig,
// Preprocessor defines to use when generating #ifdef's for #[cfg]
/// Preprocessor defines to use when generating #ifdef's for #[cfg]
pub defines: HashMap<String, String>,
/// Include doc comments from rust as documentation
pub documentation: bool,
......
......@@ -52,5 +52,4 @@ mod writer;
pub use self::cargo::*;
pub use self::config::*;
pub use self::bindings::Bindings;
pub use self::builder::LibraryBuilder;
pub use self::library::Library;
pub use self::builder::Builder;
......@@ -32,8 +32,8 @@ pub fn generate_with_config<P: AsRef<Path>>(crate_dir: P, config: Config) -> Res
None,
config.parse.parse_deps)?;
LibraryBuilder::new().with_config(config)
.with_std_types()
.with_crate(cargo)
.build()?.generate()
Builder::new().with_config(config)
.with_std_types()
.with_crate(cargo)
.generate()
}
......@@ -21,7 +21,7 @@ use clap::{Arg, ArgMatches, App};
mod logging;
mod bindgen;
use bindgen::{Cargo, Config, Language, Library, LibraryBuilder};
use bindgen::{Bindings, Builder, Cargo, Config, Language};
fn apply_config_overrides<'a>(config: &mut Config, matches: &ArgMatches<'a>) {
// We allow specifying a language to override the config default. This is
......@@ -44,7 +44,7 @@ fn apply_config_overrides<'a>(config: &mut Config, matches: &ArgMatches<'a>) {
}
}
fn load_library<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result<Library, String> {
fn load_bindings<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result<Bindings, String> {
// If a file is specified then we load it as a single source
if !input.is_dir() {
// Load any config specified or search in the input directory
......@@ -55,10 +55,10 @@ fn load_library<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result<Library, S
apply_config_overrides(&mut config, &matches);
return LibraryBuilder::new().with_config(config)
.with_std_types()
.with_src(input)
.build();
return Builder::new().with_config(config)
.with_std_types()
.with_src(input)
.generate();
}
// We have to load a whole crate, so we use cargo to gather metadata
......@@ -83,10 +83,10 @@ fn load_library<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result<Library, S
apply_config_overrides(&mut config, &matches);
LibraryBuilder::new().with_config(config)
.with_std_types()
.with_crate(lib)
.build()
Builder::new().with_config(config)
.with_std_types()
.with_crate(lib)
.generate()
}
fn main() {
......@@ -142,8 +142,8 @@ fn main() {
None => env::current_dir().unwrap(),
};
let library = match load_library(&input, &matches) {
Ok(library) => library,
let bindings = match load_bindings(&input, &matches) {
Ok(bindings) => bindings,
Err(msg) => {
error!("{}", msg);
error!("Couldn't generate bindings for {}.", input.display());
......@@ -151,23 +151,13 @@ fn main() {
}
};
// Generate a bindings file
let built = match library.generate() {
Ok(x) => x,
Err(msg) => {
error!("{}", msg);
error!("Couldn't generate bindings for {}.", input.display());
std::process::exit(1);
},
};
// Write the bindings file
match matches.value_of("out") {
Some(file) => {
built.write_to_file(file);
bindings.write_to_file(file);
}
_ => {
built.write(io::stdout());
bindings.write(io::stdout());
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment