drone-fatfs-raw 0.2.3

Bindings to ChaN's FatFs.
Documentation
extern crate bindgen;
extern crate cc;

use cc::Build;
use std::env::{var, var_os};
use std::path::PathBuf;

struct Flags(Vec<(&'static str, &'static str)>);

fn main() {
  let mut flags = Flags::new();
  flags.add("FF_CODE_PAGE", "850");
  flags.add("FF_LFN_UNICODE", "2");
  flags.add(
    "FF_FS_READONLY",
    var_os("CARGO_FEATURE_READONLY").map_or_else(|| "0", |_| "1"),
  );
  flags.add(
    "FF_USE_MKFS",
    var_os("CARGO_FEATURE_MKFS").map_or_else(|| "0", |_| "1"),
  );
  flags.add(
    "FF_USE_LFN",
    var_os("CARGO_FEATURE_LFN").map_or_else(|| "0", |_| "1"),
  );
  flags.add(
    "FF_USE_TRIM",
    var_os("CARGO_FEATURE_TRIM").map_or_else(|| "0", |_| "1"),
  );

  Build::new()
    .file("fatfs/ff.c")
    .flag("-nodefaultlibs")
    .flag("-fno-strict-aliasing")
    .define_flags(&flags)
    .compile("fatfs");

  let bindings = bindgen::builder()
    .header("wrapper.h")
    .define_flags(&flags)
    .use_core()
    .ctypes_prefix("::drone_ctypes")
    .rustified_enum("DRESULT")
    .rustified_enum("FRESULT")
    .generate()
    .expect("Unable to generate bindings");

  let out_path = PathBuf::from(var("OUT_DIR").unwrap());
  bindings
    .write_to_file(out_path.join("bindings.rs"))
    .expect("Couldn't write bindings!");
}

impl Flags {
  fn new() -> Self {
    Flags(Vec::new())
  }

  fn add(&mut self, name: &'static str, value: &'static str) {
    self.0.push((name, value));
  }
}

trait CcExt {
  fn define_flags(&mut self, flags: &Flags) -> &mut Self;
}

impl CcExt for Build {
  fn define_flags(&mut self, flags: &Flags) -> &mut Self {
    for &(name, value) in &flags.0 {
      self.define(name, value);
    }
    self
  }
}

trait BindgenExt {
  fn define_flags(self, flags: &Flags) -> Self;
}

impl BindgenExt for bindgen::Builder {
  fn define_flags(mut self, flags: &Flags) -> Self {
    for &(name, value) in &flags.0 {
      self = self.clang_arg(format!("-D{}={}", name, value));
    }
    self
  }
}