-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
70 lines (56 loc) · 2.53 KB
/
build.rs
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
use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
// get the directory of the build script
let dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
// get the path to the src directory
let src_dir = dir.join("rust");
// get all subdirectories in the src directory
let entries = fs::read_dir(&src_dir).unwrap();
let subdirs = entries.filter_map(Result::ok)
.filter(|e| e.path().is_dir())
.collect::<Vec<_>>();
let mut all_modules = Vec::new();
// for each subdirectory...
for subdir in subdirs {
// get all .rs files in the subdirectory
let entries = fs::read_dir(&subdir.path()).unwrap();
let rs_files = entries.filter_map(Result::ok)
.filter(|e| {
let path = e.path();
path.is_file()
&& path.extension().map_or(false, |s| s == "rs")
&& path.file_stem().map_or(false, |s| s != "mod")
})
.collect::<Vec<_>>();
// get the names of the .rs files (without the .rs extension)
let mod_names = rs_files.iter()
.map(|e| e.file_name())
.filter_map(|n| n.to_str().map(|s| s.replace(".rs", "")))
.collect::<Vec<_>>();
all_modules.push((subdir.file_name().to_str().unwrap().to_string(), mod_names.clone()));
// write the mod declarations to the mod.rs file
let mod_rs = subdir.path().join("mod.rs");
let mut content = String::new();
for mod_name in mod_names {
content.push_str(&format!("pub mod {};\n", mod_name));
}
fs::write(&mod_rs, content).unwrap();
}
// after the loop over subdirs
let lib_rs = dir.join("rust").join("lib.rs");
let mut lib_rs_contents = String::from("use pyo3::prelude::*;\n\n");
for (set_name, _mod_names) in &all_modules {
lib_rs_contents.push_str(&format!("mod {};\n", set_name));
}
lib_rs_contents.push_str("\n#[pymodule]\nfn cryptopals_rust(_py: Python, m: &PyModule) -> PyResult<()> {\n");
for (set_name, mod_names) in &all_modules {
for mod_name in mod_names {
lib_rs_contents.push_str(&format!(" m.add_function(wrap_pyfunction!({}::{}::{}, m)?)?;\n", set_name, mod_name, mod_name));
lib_rs_contents.push_str(&format!(" m.add_function(wrap_pyfunction!({}::{}::{}_golf, m)?)?;\n", set_name, mod_name, mod_name));
}
}
lib_rs_contents.push_str(" Ok(())\n}\n");
fs::write(&lib_rs, lib_rs_contents).unwrap();
}