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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! `wrapgen` is a tool to automatically generate Rust wrappers around C functions called via FFI.
//! It will wrap pointer returns in an `Option` and `int` returns in a `Result`.
//! As of now, `wrapgen` only works if your functions adhere to the C convention of returning
//! 0 on a successful run and another value otherwise.
//!
//! ## How to use `wrapgen`
//!
//! You can use wrapgen as a standalone binary:
//!
//! `wrapgen input.rs output.rs`
//!
//! where `input.rs` contains one function declaration per line
//!
//! or include it in your `build.rs` file:
//!
//! ```rust
//!fn main() {
//!    WrapGen::new("input1.rs")
//!        .add_file("input2.rs")
//!        .function("fn my_test_fn(arg1: cty::c_int) -> cty::c_int")
//!        .prefix("rs_")
//!        .use_core(false)
//!        .generate("output.rs");
//!}
//! ```
mod fn_definition;
mod wrapper_type;

use fn_definition::FnDefinition;
use regex::Regex;
use std::collections::HashMap;
use std::path::Path;
pub use wrapper_type::WrapperType;

#[derive(Clone)]
/// The builder struct to create wrappers.
/// Create an instance, add the files,
/// types and functions you want to wrap
/// and finally call [`generate()`] to create
/// the wrappers.
///
/// # Examples
///
/// Wrap some functions
///
/// ```
/// WrapGen::new("examples/to_wrap.rs").generate("outfile.rs");
/// ```
///
/// Wrap a pointer type
///
/// ```
/// WrapGen::default()
///         .wrap_pointer_type(
///             WrapperType::new("inode", "Inode")
///                 .with_field("i_sb", "*mut super_block")
///                 .with_field_writeonly("i_ino", "cty::c_int"),
///         )
///         .generate("outfile.rs")
/// ```
///
/// [`generate()`]: #method.generate
pub struct WrapGen<'a> {
    functions: Vec<FnDefinition<'a>>,
    wrapped_types: Vec<WrapperType<'a>>,
    prefix: &'a str,
    use_core: bool,
    _included_files: Vec<String>,
}

impl<'a> WrapGen<'a> {
    /// Create a new `WrapGen` without reading from any files
    pub fn default() -> Self {
        WrapGen {
            functions: Vec::new(),
            wrapped_types: Vec::new(),
            prefix: "rs_",
            use_core: false,
            _included_files: Vec::new(),
        }
    }

    /// Add all the functions from `file`
    pub fn add_file<P: AsRef<Path>>(mut self, file: P) -> Self {
        let lines = std::fs::read_to_string(file).unwrap();
        self._included_files.push(lines);
        self
    }

    /// Add the single function from `function`.
    /// A semicolon at the end is optional here.
    pub fn add_function(mut self, function: &'a str) -> Self {
        self.functions
            .push(FnDefinition::from_str(function).unwrap());
        self
    }

    /// Set the prefix of the wrapped functions.
    /// Defaults to `rs_`
    pub fn prefix(mut self, prefix: &'a str) -> Self {
        self.prefix = prefix;
        self
    }

    /// Determines if `core::ptr` or `std::ptr` should be used
    pub fn use_core(mut self, use_core: bool) -> Self {
        self.use_core = use_core;
        self
    }

    /// Create a new `WrapGen` and add the functions
    /// listed in `file`
    pub fn new<P: AsRef<Path>>(file: P) -> Self {
        WrapGen::default().add_file(file)
    }

    /// Will create a wrapper around a type (usually a pointer)
    /// to allow safe access to fields
    pub fn wrap_pointer_type(mut self, to_wrap: WrapperType<'a>) -> Self {
        self.wrapped_types.push(to_wrap);
        self
    }

    fn wrapped_types(&self) -> HashMap<&str, &str> {
        let mut types = HashMap::new();
        for (original, wrapper) in self
            .wrapped_types
            .iter()
            .map(|v| (v.original.clone(), v.renamed.clone()))
        {
            types.insert(original, wrapper);
        }
        types
    }

    fn read_fns(lines: &'a str) -> Vec<FnDefinition> {
        let re =
            Regex::new(r"fn\s([a-z_0-9]+)\s?\(([a-z_:&*0-9,\s]*)\)\s?(->\s([a-z_:&*0-9\s]*))?;")
                .unwrap();

        let mut matches = Vec::new();

        for cap in re.captures_iter(lines) {
            matches.push(FnDefinition::from_cap(cap).unwrap());
        }

        matches
    }

    fn translate_function(&self, function: &FnDefinition) -> String {
        match &function.returns {
            Some(returns) => {
                if returns.ends_with("c_int") {
                    format!(
                        "fn {}{}({}) -> Result<(), {}> {{
    match unsafe {{ {}({}) }} {{
        0 => Ok(()),
        e => Err(e),
    }}
}}",
                        self.prefix,
                        function.get_name(),
                        function.get_params(),
                        function.get_returns(),
                        function.get_name(),
                        function.get_param_names().join(", ")
                    )
                } else {
                    format!(
                        "fn {}{}({}) -> Option<{}> {{
    let val = unsafe {{ {}({}) }};
    if val == {}::ptr::null_mut() {{
        return None;
    }} else {{
        return Some({});
    }}
}}",
                        self.prefix,
                        function.get_name(),
                        function.get_params(),
                        self.wrapped_types()
                            .get(function.get_returns().split_terminator(" ").nth(1).unwrap())
                            .unwrap_or(&function.get_returns()),
                        function.get_name(),
                        function.get_param_names().join(", "),
                        if self.use_core { "core" } else { "std" },
                        if let Some(wrapper) = self
                            .wrapped_types()
                            .get(function.get_returns().split_terminator(" ").nth(1).unwrap())
                        {
                            format!("{}::from_ptr(val)", wrapper)
                        } else {
                            "val".to_owned()
                        }
                    )
                }
            }
            None => format!(
                "fn {}{}({}) {{
    unsafe {{ {}({}) }};
}}",
                self.prefix,
                function.get_name(),
                function.get_params(),
                function.get_name(),
                function.get_param_names().join(", ")
            ),
        }
    }

    fn generate_extern_declarations(&self) -> String {
        if self.functions.is_empty() {
            String::new()
        } else {
            format!(
                "extern \"C\" {{
{}
}}",
                self.functions
                    .iter()
                    .map(|v| format!("    {}", v))
                    .collect::<Vec<String>>()
                    .join("\n")
            )
        }
    }

    /// Generate wrappers for all previously added functions
    /// and write them to `outfile_path`
    pub fn generate<P: AsRef<Path>>(&'a mut self, outfile_path: P) {
        for lines in &self._included_files {
            self.functions.append(&mut Self::read_fns(&lines))
        }
        let _ = std::fs::write(
            outfile_path,
            format!(
                "{}

pub struct Wrapper<T> {{
    ptr: *mut T
}}

impl<T> Wrapper<T> {{
    pub fn from_ptr(ptr: *mut T) -> Self {{
        Self {{ ptr }}
    }}

    pub fn get_ptr(&self) -> *mut T {{
        self.ptr
    }}
}}

{}

{}",
                self.generate_extern_declarations(),
                self.wrapped_types
                    .iter()
                    .map(|v| v.generate())
                    .collect::<Vec<String>>()
                    .join("\n"),
                self.functions
                    .iter()
                    .map(|v| self.translate_function(v))
                    .collect::<Vec<String>>()
                    .join("\n\n")
            ),
        );
    }
}