-
Notifications
You must be signed in to change notification settings - Fork 31
/
build.rs
156 lines (137 loc) · 4.77 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
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
use std::collections::HashSet;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::Write;
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{anyhow, bail, Context};
fn main() -> anyhow::Result<()> {
let out_dir = env::var("OUT_DIR").context("OUT_DIR env variable is not set")?;
let out_dir = Path::new(&out_dir);
let out_dir = fs::canonicalize(out_dir).context("canonicalize OUT_DIR")?;
println!("Out dir: {}", out_dir.to_string_lossy());
let pari_dir = PathBuf::from_iter([".", "depend", "pari"]);
let pari_mirror = out_dir.join("pari-mirror");
let pari_install = out_dir.join("pari-install");
println!(
"cargo:rerun-if-changed={}/CHANGES",
pari_dir.to_string_lossy()
);
// Create a copy of pari directory inside of OUT_DIR
{
if pari_mirror.exists() {
fs::remove_dir_all(&pari_mirror).context("remove mirror copy of pari")?;
}
let opts = fs_extra::dir::CopyOptions {
copy_inside: true,
..Default::default()
};
fs_extra::dir::copy(&pari_dir, &pari_mirror, &opts)
.context("create a copy of pari directory to OUT_DIR")?;
// Ensure that the rest of build script doesn't refer to original pari directory by mistake
drop(pari_dir);
}
{
let output = Command::new(pari_mirror.join("Configure"))
.arg(OsString::from_iter([
OsStr::new("--prefix="),
pari_install.as_os_str(),
]))
.current_dir(&pari_mirror)
.output()
.context("run ./Configure")?;
if !output.status.success() {
std::io::stderr()
.write_all(&output.stderr)
.context("write error to stderr")?;
bail!("./Configure returned non-zero code")
}
}
{
let output = Command::new("make")
.arg("install-nodata")
.current_dir(&pari_mirror)
.output()
.context("run `make install-nodata`")?;
if !output.status.success() {
std::io::stderr()
.write_all(&output.stderr)
.context("write error to stderr")?;
bail!("`make install-nodata` returned non-zero code");
}
}
{
let output = Command::new("make")
.arg("install-lib-sta")
.current_dir(&pari_mirror)
.output()
.context("run `make install-lib-sta`")?;
if !output.status.success() {
std::io::stderr()
.write_all(&output.stderr)
.context("write error to stderr")?;
bail!("`make install-lib-sta` returned non-zero code")
}
}
let ignored_macros = IgnoreMacros(
vec![
"FP_INFINITE".into(),
"FP_NAN".into(),
"FP_NORMAL".into(),
"FP_SUBNORMAL".into(),
"FP_ZERO".into(),
"IPPORT_RESERVED".into(),
]
.into_iter()
.collect(),
);
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
.clang_arg(format!(
"-I{}",
pari_install.join("include").to_string_lossy()
))
.whitelist_type("GEN")
.whitelist_function("mkintn")
.whitelist_function("GENtostr")
.whitelist_function("compo")
.whitelist_function("qfi")
.whitelist_function("nupow")
.whitelist_function("qfbcompraw")
.whitelist_function("primeform")
.whitelist_function("pari_init")
.whitelist_function("gneg")
.whitelist_function("gadd")
.whitelist_function("shifti")
.whitelist_function("isprime")
.parse_callbacks(Box::new(ignored_macros))
// Finish the builder and generate the bindings.
.generate()
.map_err(|_| anyhow!("couldn't generate bindings"))?;
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.context("couldn't write bindings")?;
println!(
"cargo:rustc-link-search=native={}/lib",
pari_install.to_string_lossy()
);
println!("cargo:rustc-link-lib=static=pari");
Ok(())
}
#[derive(Debug)]
struct IgnoreMacros(HashSet<String>);
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}