<commit_before>/* -- Lots of reuse from: https://github.com/alexcrichton/git2-rs/blob/master/libgit2-sys/build.rs */
extern crate bindgen;
extern crate rustc_serialize;

use std::env;
use std::fs;
use rustc_serialize::json;
use std::fs::OpenOptions;
use std::io::{ErrorKind, Read, Write};
use std::path::PathBuf;
use std::process::Command;
use bindgen::*; //dirty
use std::convert::AsRef;

#[derive(RustcDecodable)]
struct Config {
  // Use the existing lib if it exists
  use_lib: bool,
  lib_dir: String,
  inc_dir: String,

  // Build related
  build_type: String,
  build_threads: String,
  build_cuda: String,
  build_opencl: String,
  build_cpu: String,
  build_examples: String,
  build_test: String,
  build_graphics: String,

  // backend upstream library options
  glew_static: String,
  freeimage_type: String,
  cpu_fft_type: String,
  cpu_blas_type: String,
  cpu_lapack_type: String,

  // backend upstream library install paths
  freeimage_dir: String,
  fftw_dir: String,
  acml_dir: String,
  mkl_dir: String,
  lapacke_dir: String,
  glew_dir: String,
  glfw_dir: String,
  boost_dir: String,
}

macro_rules! t {
  ($e:expr) => (match $e {
    Ok(n) => n,
    Err(e) => fail(&format!("\n{} failed with {}\n", stringify!($e), e)),
  })
}

fn fail(s: &str) -> ! {
    panic!("\n{}\n\nbuild script failed, must exit now", s)
}

fn run(cmd: &mut Command, program: &str) {
  println!("running: {:?}", cmd);
  let status = match cmd.status() {
    Ok(status) => status,
    Err(ref e) if e.kind() == ErrorKind::NotFound => {
      fail(&format!("failed to execute command: {}\nis `{}` not installed?",
                    e, program));
    }
    Err(e) => fail(&format!("failed to execute command: {}", e)),
  };
  if !status.success() {
    fail(&format!("command did not execute successfully, got: {}", status));
  }
}

// Original CLI command: bindgen -l lib/libafcuda.dylib -I . -builtins -o arrayfire.rs arrayfire.h
fn build_bindings(package_name: &str,
                  out_dir: &std::path::PathBuf,
                  include_path: &std::path::PathBuf) {
  let rust_header = package_name.to_string() + ".rs";
  let c_header = package_name.to_string() + ".h";

  let rs_dir = std::path::Path::new(&out_dir).join(rust_header);
  let rs_path = rs_dir.to_str().unwrap();

  let mut bindings = bindgen::builder();
  bindings.emit_builtins();

  // Blob in '-I arrayfire/include' to the "VPATH"
  if cfg!(target_os="windows") {
      bindings.header("-I");
      bindings.header("C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include");
  }
  bindings.header("-I");
  bindings.header(include_path.to_str().unwrap());

  let h_path = std::path::Path::new(&include_path).join(c_header);
  let h_path = String::from(h_path.to_str().unwrap());
  bindings.header(h_path);

  let bindings = bindings.generate();
  let bindings = bindings.unwrap();
  bindings.write_to_file(rs_path).unwrap();
}

fn read_file(file_name: &std::path::PathBuf) -> String {
  let file_path = file_name.to_str().unwrap();
  let options = OpenOptions::new()
    .read(true)
    .write(false)
    .create(false)
    .open(&file_path);
  let mut file = match options {
    Ok(file) => file,<commit_msg>Added c headers include path for bindgen parameters list<commit_after>/* -- Lots of reuse from: https://github.com/alexcrichton/git2-rs/blob/master/libgit2-sys/build.rs */
extern crate bindgen;
extern crate rustc_serialize;

use std::env;
use std::fs;
use rustc_serialize::json;
use std::fs::OpenOptions;
use std::io::{ErrorKind, Read, Write};
use std::path::PathBuf;
use std::process::Command;
use bindgen::*; //dirty
use std::convert::AsRef;

#[derive(RustcDecodable)]
struct Config {
  // Use the existing lib if it exists
  use_lib: bool,
  lib_dir: String,
  inc_dir: String,

  // Build related
  build_type: String,
  build_threads: String,
  build_cuda: String,
  build_opencl: String,
  build_cpu: String,
  build_examples: String,
  build_test: String,
  build_graphics: String,

  // backend upstream library options
  glew_static: String,
  freeimage_type: String,
  cpu_fft_type: String,
  cpu_blas_type: String,
  cpu_lapack_type: String,

  // backend upstream library install paths
  freeimage_dir: String,
  fftw_dir: String,
  acml_dir: String,
  mkl_dir: String,
  lapacke_dir: String,
  glew_dir: String,
  glfw_dir: String,
  boost_dir: String,
}

macro_rules! t {
  ($e:expr) => (match $e {
    Ok(n) => n,
    Err(e) => fail(&format!("\n{} failed with {}\n", stringify!($e), e)),
  })
}

fn fail(s: &str) -> ! {
    panic!("\n{}\n\nbuild script failed, must exit now", s)
}

fn run(cmd: &mut Command, program: &str) {
  println!("running: {:?}", cmd);
  let status = match cmd.status() {
    Ok(status) => status,
    Err(ref e) if e.kind() == ErrorKind::NotFound => {
      fail(&format!("failed to execute command: {}\nis `{}` not installed?",
                    e, program));
    }
    Err(e) => fail(&format!("failed to execute command: {}", e)),
  };
  if !status.success() {
    fail(&format!("command did not execute successfully, got: {}", status));
  }
}

#[cfg(not(windows))]
fn get_gcc_version_path() -> String {
    let gcc_cmd = Command::new("sh")
        .args(&["-c", "echo -n `gcc --version | grep ^gcc | cut -f3 -d ' '`"])
        .output()
        .unwrap_or_else(|e| {
            panic!("failed to find gcc version: {}", e)
        });

    if gcc_cmd.status.success() {
        return String::from_utf8(gcc_cmd.stdout).unwrap();
    } else {
        panic!("gcc --version execution failed");
    }
}

// Original CLI command: bindgen -l lib/libafcuda.dylib -I . -builtins -o arrayfire.rs arrayfire.h
fn build_bindings(package_name: &str,
                  out_dir: &std::path::PathBuf,
                  include_path: &std::path::PathBuf) {
  let rust_header = package_name.to_string() + ".rs";
  let c_header = package_name.to_string() + ".h";

  let rs_dir = std::path::Path::new(&out_dir).join(rust_header);
  let rs_path = rs_dir.to_str().unwrap();

  let mut bindings = bindgen::builder();
  bindings.emit_builtins();

  // Blob in '-I arrayfire/include' to the "VPATH"
  if cfg!(target_os="windows") {
      bindings.header("-I");
      bindings.header("C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include");
  } else if cfg!(target_os="linux") {
      bindings.header("-I");
      bindings.header(format!("/usr/lib/gcc/x86_64-linux-gnu/{}/include", get_gcc_version_path()));
      bindings.header("-I");
      bindings.header(format!("/usr/lib/gcc/x86_64-unknown-linux-gnu/{}/include", get_gcc_version_path()));
  }
  bindings.header("-I");
  bindings.header(include_path.to_str().unwrap());

  let h_path = std::path::Path::new(&include_path).join(c_header);
  let h_path = String::from(h_path.to_str().unwrap());
  bindings.header(h_path);

  let bindings = bindings.generate();
  let bindings = bindings.unwrap();
  bindings.write_to_file(rs_path).unwrap();
}

fn read_file(file_name: &std::path::PathBuf) -> String {
  let file_path = file_name.to_str().unwrap();
  let options = OpenOptions::new()
    .read(true)
    .write(false)
    .create(false)
    .open(&file_path);
  let mut file = match options {
    Ok(file) => file,
{'fraction_non_alphanumeric': 0.16953863257365204, 'fraction_numerical': 0.0033351862145636463, 'mean_word_length': 3.691655801825293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4449330', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2534, 'n_words': 568}