-
Notifications
You must be signed in to change notification settings - Fork 15
/
build.rs
51 lines (45 loc) · 1.52 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
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
fn main() {
build_capnp()
}
fn build_capnp() {
for filename in ["index.capnp"] {
let source_file = Path::new("schema").join(filename);
let target_file = Path::new("src").join(filename.replace(".", "_") + ".rs");
let hashes = HashInfo {
source: hex::encode(file_hash(&source_file)),
target: hex::encode(file_hash(&target_file)),
};
let hash_file = Path::new(".build").join("capnp").join(format!("{}.json", filename));
if hash_file.is_file() {
if let Ok(stored_hashes) = serde_json::from_reader::<_, HashInfo>(File::open(&hash_file).unwrap()) {
if stored_hashes == hashes {
continue;
}
}
}
capnpc::CompilerCommand::new()
.src_prefix("schema")
.file(source_file)
.output_path("src")
.run()
.expect("schema compiler command");
std::fs::write(&hash_file, serde_json::to_string_pretty(&hashes).unwrap()).unwrap();
}
}
fn file_hash(filename: &PathBuf) -> [u8; 32] {
let mut hasher = Sha256::new();
for line in BufReader::new(File::open(filename).unwrap()).lines() {
hasher.update(line.unwrap());
}
hasher.finalize().into()
}
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
struct HashInfo {
source: String,
target: String,
}