-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
153 lines (149 loc) · 4.54 KB
/
build.rs
File metadata and controls
153 lines (149 loc) · 4.54 KB
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
use anyhow::Result;
use flate2::write::GzEncoder;
use flate2::Compression;
use hex;
use std::env;
use std::fs;
use std::io::prelude::*;
use std::path::Path;
use std::process::{Command, Stdio};
fn compress(binary: Vec<u8>) -> Result<Vec<u8>> {
let mut writer = GzEncoder::new(Vec::<u8>::with_capacity(binary.len()), Compression::best());
writer.write_all(&binary)?;
Ok(writer.finish()?)
}
fn build_alkane(wasm_str: &str, features: Vec<&'static str>) -> Result<()> {
if features.len() != 0 {
let _ = Command::new("cargo")
.env("CARGO_TARGET_DIR", wasm_str)
.arg("build")
.arg("--release")
.arg("--features")
.arg(features.join(","))
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?
.wait()?;
Ok(())
} else {
Command::new("cargo")
.env("CARGO_TARGET_DIR", wasm_str)
.arg("build")
.arg("--release")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?
.wait()?;
Ok(())
}
}
fn main() {
let env_var = env::var_os("OUT_DIR").unwrap();
let base_dir = Path::new(&env_var)
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
let out_dir = base_dir.join("release");
let wasm_dir = base_dir.parent().unwrap().join("alkanes");
fs::create_dir_all(&wasm_dir).unwrap();
let wasm_str = wasm_dir.to_str().unwrap();
let write_dir = Path::new(&out_dir)
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("src")
.join("tests");
fs::create_dir_all(&write_dir.join("std")).unwrap();
let crates_dir = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("alkanes");
std::env::set_current_dir(&crates_dir).unwrap();
let mods = fs::read_dir(&crates_dir)
.unwrap()
.filter_map(|v| {
let name = v.ok()?.file_name().into_string().ok()?;
Some(name)
})
.collect::<Vec<String>>();
let files = mods
.clone()
.into_iter()
.filter_map(|name| {
Some(name)
})
.collect::<Vec<String>>();
files.into_iter()
.map(|v| -> Result<String> {
std::env::set_current_dir(&crates_dir.clone().join(v.clone()))?;
build_alkane(wasm_str, vec![])?;
std::env::set_current_dir(&crates_dir)?;
let subbed = v.clone().replace("-", "_");
eprintln!(
"write: {}",
write_dir
.join("std")
.join(subbed.clone() + "_build.rs")
.into_os_string()
.to_str()
.unwrap()
);
let f: Vec<u8> = fs::read(
&Path::new(&wasm_str)
.join("wasm32-unknown-unknown")
.join("release")
.join(subbed.clone() + ".wasm"),
)?;
let compressed: Vec<u8> = compress(f.clone())?;
fs::write(&Path::new(&wasm_str).join("wasm32-unknown-unknown").join("release").join(subbed.clone() + ".wasm.gz"), &compressed)?;
let data: String = hex::encode(&f);
fs::write(
&write_dir.join("std").join(subbed.clone() + "_build.rs"),
String::from("use hex_lit::hex;\n#[allow(long_running_const_eval)]\npub fn get_bytes() -> Vec<u8> { (&hex!(\"")
+ data.as_str()
+ "\")).to_vec() }",
)?;
eprintln!(
"build: {}",
write_dir
.join("std")
.join(subbed.clone() + "_build.rs")
.into_os_string()
.to_str()
.unwrap()
);
Ok(subbed)
})
.collect::<Result<Vec<String>>>()
.unwrap();
eprintln!(
"write test builds to: {}",
write_dir
.join("std")
.join("mod.rs")
.into_os_string()
.to_str()
.unwrap()
);
fs::write(
&write_dir.join("std").join("mod.rs"),
mods.into_iter()
.map(|v| v.replace("-", "_"))
.fold(String::default(), |r, v| {
r + "pub mod " + v.as_str() + "_build;\n"
}),
)
.unwrap();
}