-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
49 lines (39 loc) · 1.28 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
use std::process::Command;
use std::env;
use std::fs;
use std::os::unix::fs::symlink;
fn main() {
// allow configuration via env var. Set to the version number itself, like "9.5.3".
let env_var = env::var("RPGFFI_PG_VERSION");
let pg_vers = if env_var.is_ok() {
env_var.unwrap()
} else {
let cmd_vers = Command::new("pg_config").arg("--version").output().unwrap_or_else(|e| {
panic!("Failed to execute pg_config: {}", e)
});
if !cmd_vers.status.success() {
panic!("pg_config failed with {}", cmd_vers.status.code().unwrap());
}
// transform "PostgreSQL 9.6.1" to "9.6.1"
String::from_utf8(cmd_vers.stdout).unwrap().chars()
.skip_while(|c| *c != ' ')
.skip(1)
.collect::<String>()
};
// transform "9.6.1" to "96"
let short_vers = pg_vers.chars()
.filter(|c| *c != '.')
.take(2)
.collect::<String>();
match fs::remove_file("src/lib.rs") {
Ok(_) => (),
Err(e) => panic!("couldn't remove lib.rs: {}", e)
}
let dest_filename = format!("pg{}.rs", short_vers);
if let Err(_) = fs::File::open(format!("src/{}", dest_filename)) {
panic!("Destination file src/{} does not exist", dest_filename);
}
if let Err(e) = symlink(dest_filename, "src/lib.rs") {
panic!("Symlink failed: {}", e);
}
}