1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
extern crate walkdir;
use walkdir::WalkDir;
use std::path::Path;
use std::process::Command;
use std::env;
use std::io::Error;
//use std::collections::HashMap;
//const PROG_NOT_SET: &str = "cabal-rs: Program was not set using `with_prog`";
const STATIC_EXTENSION: &str = ".a";

pub struct Cabal<T>
    where T: AsRef<Path>
{
    path: T,
    build_dir: Option<T>,
    verbose: Option<Verbosity>,
    jobs: Option<usize>,
    //w_prog: Option<HashMap<Prog, Vec<&'a str>>>,
    only: bool
}

pub enum Verbosity {
    Zero,
    One,
    Two,
    Three,
}

// #[allow(non_camel_case_types)]
// #[derive(PartialEq, Eq, Hash, Clone, Copy)]
// pub enum Prog {
//     alex,
//     ar,
//     c2hs,
//     cpphs,
//     doctest,
//     gcc,
//     ghc,
//     ghc_pkg,
//     ghcjs,
//     ghcjs_pkg,
//     greencard,
//     haddock,
//     happy,
//     haskell_suite,
//     haskell_suite_pkg,
//     hmake,
//     hpc,
//     hsc2hs,
//     hscolour,
//     jhc,
//     ld,
//     lhc,
//     lhc_pkg,
//     pkg_config,
//     runghc,
//     strip,
//     tar,
//     uhc
// }

impl<T> Cabal<T>
    where T: AsRef<Path>
{
    pub fn src(path: T) -> Self {
        Self {
            path,
            build_dir: None,
            verbose: None,
            jobs: None,
            //w_prog: None,
            only: false,
        }
    }

    pub fn build_dir(mut self, path: T) -> Self {
        self.build_dir = Some(path);
        self
    }

    pub fn jobs(mut self, j: usize) -> Self {
        self.jobs = Some(j);
        self
    }

    // pub fn with_prog(mut self, prog: Prog) -> Self {
    //     self.w_prog = Some(self.w_prog.map_or_else(
    //         || {
    //             let mut map = HashMap::new();
    //             map.insert(prog, Vec::new());
    //             map
    //         },
    //         |mut map| {
    //             map.insert(prog, Vec::new());
    //             map
    //         }
    //     ));
    //     self
    // }

    // /// ## Panic
    // /// This will panic if you have not set the program in the given argument
    // /// with the function `with_prog` prior to this function call.
    // pub fn prog_option(mut self, prog: Prog, option: &'a str) -> Self {
    //     let mut map = self.w_prog.expect(PROG_NOT_SET);
    //     {
    //         let opts = map.get_mut(&prog).expect(PROG_NOT_SET);
    //         opts.push(option);
    //     }
    //     self.w_prog = Some(map);
    //     self
    // }

    // /// ## Panic
    // /// This will panic if you have not set the program in the given argument
    // /// with the function `with_prog` prior to this function call.
    // pub fn prog_options(mut self, prog: Prog, options: Vec<&'a str>) -> Self {
    //     let mut map = self.w_prog.expect(PROG_NOT_SET);
    //     {
    //         let opts = map.get_mut(&prog).expect(PROG_NOT_SET);
    //         opts.extend(&options);
    //     }
    //     self.w_prog = Some(map);
    //     self
    // }

    pub fn verbose(mut self, v: Verbosity) -> Self {
        self.verbose = Some(v);
        self
    }

    pub fn build(self) -> Result<(), Error> {
        let mut options = Vec::new();

        if self.only {
            options.push("--only".into());
        }

        let mut build_dir = None;
        if let Some(dir) = self.build_dir {
            let mut directory = String::from("--builddir=");
            directory.push_str(&dir.as_ref().to_str().unwrap());
            build_dir = Some(dir);
            options.push(directory);
        }

        if let Some(v) = self.verbose {
            let mut verbose = String::from("--verbose=");
            verbose.push({
                match v {
                    Verbosity::Zero  => '0',
                    Verbosity::One   => '1',
                    Verbosity::Two   => '2',
                    Verbosity::Three => '3',
                }
            });
            options.push(verbose);
        }

        if let Some(j) = self.jobs {
            let mut verbose = String::from("--jobs=");
            verbose.push_str(&j.to_string());
            options.push(verbose);

        }

        // if let Some(map) = self.w_prog {
        //     for (prog, opts) in map {
        //     }
        // }

        let previous_dir = env::current_dir()?;
        env::set_current_dir(self.path.as_ref())?;
        let output =
            Command::new("cabal")
                .arg("build")
                .args(&options)
                .output()
                .expect("cabal-rs: Failed to execute cabal build");
        if output.status.success() {
            let mut output_dir = env::current_dir()?;
            match build_dir {
                Some(dir) => {
                    output_dir.push(dir);
                    output_dir.push("build");
                },
                None      => {
                    output_dir.push("dist");
                    output_dir.push("build");
                },
            }

            for entry in WalkDir::new(output_dir) {
                let entry = entry?;
                let mut path = entry.path().to_path_buf();
                let file_name = entry.file_name().to_str().unwrap();
                path.pop();
                if file_name.contains(STATIC_EXTENSION) {
                    println!("cargo:rustc-link-search=native={}", path.display());
                    // Get rid of lib from the file name
                    let temp = file_name.split_at(3).1;
                    // Get rid of the .a from the file name
                    let trimmed = temp.split_at(temp.len() - STATIC_EXTENSION.len()).0;
                    println!("cargo:rustc-link-lib=static={}", trimmed);
                }
            }
        } else {
            panic!("Failed to compile program with cabal. stderr output: {}",
                    String::from_utf8_lossy(&output.stderr));
        }
            env::set_current_dir(previous_dir)?;

        Ok(())
    }
}